diff --git a/internal/sm2ec/p256_asm_ord.go b/internal/sm2ec/p256_asm_ord.go index dffe6bbd..a466e475 100644 --- a/internal/sm2ec/p256_asm_ord.go +++ b/internal/sm2ec/p256_asm_ord.go @@ -33,7 +33,7 @@ func P256OrdInverse(k []byte) ([]byte, error) { return nil, errors.New("invalid scalar length") } x := new(p256OrdElement) - p256OrdBigToLittle(x, toElementArray(k)) + p256OrdBigToLittle(x, (*[32]byte)(k)) // Inversion is implemented as exponentiation by n - 2, per Fermat's little theorem. // @@ -106,11 +106,11 @@ func P256OrdMul(in1, in2 []byte) ([]byte, error) { return nil, errors.New("invalid scalar length") } x1 := new(p256OrdElement) - p256OrdBigToLittle(x1, toElementArray(in1)) + p256OrdBigToLittle(x1, (*[32]byte)(in1)) p256OrdMul(x1, x1, RR) x2 := new(p256OrdElement) - p256OrdBigToLittle(x2, toElementArray(in2)) + p256OrdBigToLittle(x2, (*[32]byte)(in2)) p256OrdMul(x2, x2, RR) res := new(p256OrdElement) diff --git a/internal/sm2ec/sm2p256_asm.go b/internal/sm2ec/sm2p256_asm.go index a901bb1f..00ace0f1 100644 --- a/internal/sm2ec/sm2p256_asm.go +++ b/internal/sm2ec/sm2p256_asm.go @@ -68,10 +68,10 @@ const p256ElementLength = 32 const p256UncompressedLength = 1 + 2*p256ElementLength const p256CompressedLength = 1 + p256ElementLength -// toElementArray, convert slice of bytes to pointer to [32]byte. +// (*[32]byte), convert slice of bytes to pointer to [32]byte. // This function is required for low version of golang, can type cast directly // since golang 1.17. -func toElementArray(b []byte) *[32]byte { +func (*[32]byte)(b []byte) *[32]byte { tmpPtr := (*unsafe.Pointer)(unsafe.Pointer(&b)) return (*[32]byte)(*tmpPtr) } @@ -95,8 +95,8 @@ func (p *SM2P256Point) SetBytes(b []byte) (*SM2P256Point, error) { // Uncompressed form. case len(b) == p256UncompressedLength && b[0] == 4: var r SM2P256Point - p256BigToLittle(&r.x, toElementArray(b[1:33])) - p256BigToLittle(&r.y, toElementArray(b[33:65])) + p256BigToLittle(&r.x, (*[32]byte)(b[1:33])) + p256BigToLittle(&r.y, (*[32]byte)(b[33:65])) if p256LessThanP(&r.x) == 0 || p256LessThanP(&r.y) == 0 { return nil, errors.New("invalid P256 element encoding") } @@ -111,7 +111,7 @@ func (p *SM2P256Point) SetBytes(b []byte) (*SM2P256Point, error) { // Compressed form. case len(b) == p256CompressedLength && (b[0] == 2 || b[0] == 3): var r SM2P256Point - p256BigToLittle(&r.x, toElementArray(b[1:33])) + p256BigToLittle(&r.x, (*[32]byte)(b[1:33])) if p256LessThanP(&r.x) == 0 { return nil, errors.New("invalid P256 element encoding") } @@ -457,7 +457,7 @@ func (r *SM2P256Point) ScalarBaseMult(scalar []byte) (*SM2P256Point, error) { return nil, errors.New("invalid scalar length") } scalarReversed := new(p256OrdElement) - p256OrdBigToLittle(scalarReversed, toElementArray(scalar)) + p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar)) p256OrdReduce(scalarReversed) r.p256BaseMult(scalarReversed) return r, nil @@ -471,7 +471,7 @@ func (r *SM2P256Point) ScalarMult(q *SM2P256Point, scalar []byte) (*SM2P256Point return nil, errors.New("invalid scalar length") } scalarReversed := new(p256OrdElement) - p256OrdBigToLittle(scalarReversed, toElementArray(scalar)) + p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar)) p256OrdReduce(scalarReversed) r.Set(q).p256ScalarMult(scalarReversed) return r, nil @@ -523,8 +523,8 @@ func (p *SM2P256Point) bytes(out *[p256UncompressedLength]byte) []byte { p.affineFromMont(x, y) out[0] = 4 // Uncompressed form. - p256LittleToBig(toElementArray(out[1:33]), x) - p256LittleToBig(toElementArray(out[33:65]), y) + p256LittleToBig((*[32]byte)(out[1:33]), x) + p256LittleToBig((*[32]byte)(out[33:65]), y) return out[:] } @@ -562,7 +562,7 @@ func (p *SM2P256Point) bytesX(out *[p256ElementLength]byte) ([]byte, error) { p256Sqr(x, x, 1) p256Mul(x, &p.x, x) p256FromMont(x, x) - p256LittleToBig(toElementArray(out[:]), x) + p256LittleToBig((*[32]byte)(out[:]), x) return out[:], nil } @@ -586,7 +586,7 @@ func (p *SM2P256Point) bytesCompressed(out *[p256CompressedLength]byte) []byte { p.affineFromMont(x, y) out[0] = 2 | byte(y[0]&1) - p256LittleToBig(toElementArray(out[1:33]), x) + p256LittleToBig((*[32]byte)(out[1:33]), x) return out[:] } diff --git a/sm9/bn256/gfp.go b/sm9/bn256/gfp.go index 609f9dee..c3f59476 100644 --- a/sm9/bn256/gfp.go +++ b/sm9/bn256/gfp.go @@ -120,16 +120,8 @@ func (e *gfP) Sqrt(f *gfP) { e.Set(i) } -// toElementArray, convert slice of bytes to pointer to [32]byte. -// This function is required for low version of golang, can type cast directly -// since golang 1.17. -func toElementArray(b []byte) *[32]byte { - tmpPtr := (*unsafe.Pointer)(unsafe.Pointer(&b)) - return (*[32]byte)(*tmpPtr) -} - func (e *gfP) Marshal(out []byte) { - gfpMarshal(toElementArray(out), e) + gfpMarshal((*[32]byte)(out), e) } // uint64IsZero returns 1 if x is zero and zero otherwise. @@ -154,7 +146,7 @@ func lessThanP(x *gfP) int { } func (e *gfP) Unmarshal(in []byte) error { - gfpUnmarshal(e, toElementArray(in)) + gfpUnmarshal(e, (*[32]byte)(in)) // Ensure the point respects the curve modulus // TODO: Do we need to change it to constant time version ? for i := 3; i >= 0; i-- {