Skip to content

Commit

Permalink
sm2p256_asm.go中切片直接转数组指针 #74
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Nov 9, 2023
1 parent 17a3dd8 commit 83cf55a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
6 changes: 3 additions & 3 deletions internal/sm2ec/p256_asm_ord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions internal/sm2ec/sm2p256_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check failure on line 74 in internal/sm2ec/sm2p256_asm.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, arm64)

syntax error: unexpected (, expecting name or (

Check failure on line 74 in internal/sm2ec/sm2p256_asm.go

View workflow job for this annotation

GitHub Actions / build (1.19)

syntax error: unexpected (, expecting name or (

Check failure on line 74 in internal/sm2ec/sm2p256_asm.go

View workflow job for this annotation

GitHub Actions / build (1.21)

syntax error: unexpected (, expected name
tmpPtr := (*unsafe.Pointer)(unsafe.Pointer(&b))
return (*[32]byte)(*tmpPtr)
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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[:]
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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[:]
}
Expand Down
12 changes: 2 additions & 10 deletions sm9/bn256/gfp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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-- {
Expand Down

0 comments on commit 83cf55a

Please sign in to comment.