mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-27 04:36:19 +08:00
sm2p256_asm.go中切片直接转数组指针 #74
This commit is contained in:
parent
17a3dd8d4b
commit
83cf55a137
@ -33,7 +33,7 @@ func P256OrdInverse(k []byte) ([]byte, error) {
|
|||||||
return nil, errors.New("invalid scalar length")
|
return nil, errors.New("invalid scalar length")
|
||||||
}
|
}
|
||||||
x := new(p256OrdElement)
|
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.
|
// 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")
|
return nil, errors.New("invalid scalar length")
|
||||||
}
|
}
|
||||||
x1 := new(p256OrdElement)
|
x1 := new(p256OrdElement)
|
||||||
p256OrdBigToLittle(x1, toElementArray(in1))
|
p256OrdBigToLittle(x1, (*[32]byte)(in1))
|
||||||
p256OrdMul(x1, x1, RR)
|
p256OrdMul(x1, x1, RR)
|
||||||
|
|
||||||
x2 := new(p256OrdElement)
|
x2 := new(p256OrdElement)
|
||||||
p256OrdBigToLittle(x2, toElementArray(in2))
|
p256OrdBigToLittle(x2, (*[32]byte)(in2))
|
||||||
p256OrdMul(x2, x2, RR)
|
p256OrdMul(x2, x2, RR)
|
||||||
|
|
||||||
res := new(p256OrdElement)
|
res := new(p256OrdElement)
|
||||||
|
@ -68,10 +68,10 @@ const p256ElementLength = 32
|
|||||||
const p256UncompressedLength = 1 + 2*p256ElementLength
|
const p256UncompressedLength = 1 + 2*p256ElementLength
|
||||||
const p256CompressedLength = 1 + 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
|
// This function is required for low version of golang, can type cast directly
|
||||||
// since golang 1.17.
|
// since golang 1.17.
|
||||||
func toElementArray(b []byte) *[32]byte {
|
func (*[32]byte)(b []byte) *[32]byte {
|
||||||
tmpPtr := (*unsafe.Pointer)(unsafe.Pointer(&b))
|
tmpPtr := (*unsafe.Pointer)(unsafe.Pointer(&b))
|
||||||
return (*[32]byte)(*tmpPtr)
|
return (*[32]byte)(*tmpPtr)
|
||||||
}
|
}
|
||||||
@ -95,8 +95,8 @@ func (p *SM2P256Point) SetBytes(b []byte) (*SM2P256Point, error) {
|
|||||||
// Uncompressed form.
|
// Uncompressed form.
|
||||||
case len(b) == p256UncompressedLength && b[0] == 4:
|
case len(b) == p256UncompressedLength && b[0] == 4:
|
||||||
var r SM2P256Point
|
var r SM2P256Point
|
||||||
p256BigToLittle(&r.x, toElementArray(b[1:33]))
|
p256BigToLittle(&r.x, (*[32]byte)(b[1:33]))
|
||||||
p256BigToLittle(&r.y, toElementArray(b[33:65]))
|
p256BigToLittle(&r.y, (*[32]byte)(b[33:65]))
|
||||||
if p256LessThanP(&r.x) == 0 || p256LessThanP(&r.y) == 0 {
|
if p256LessThanP(&r.x) == 0 || p256LessThanP(&r.y) == 0 {
|
||||||
return nil, errors.New("invalid P256 element encoding")
|
return nil, errors.New("invalid P256 element encoding")
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ func (p *SM2P256Point) SetBytes(b []byte) (*SM2P256Point, error) {
|
|||||||
// Compressed form.
|
// Compressed form.
|
||||||
case len(b) == p256CompressedLength && (b[0] == 2 || b[0] == 3):
|
case len(b) == p256CompressedLength && (b[0] == 2 || b[0] == 3):
|
||||||
var r SM2P256Point
|
var r SM2P256Point
|
||||||
p256BigToLittle(&r.x, toElementArray(b[1:33]))
|
p256BigToLittle(&r.x, (*[32]byte)(b[1:33]))
|
||||||
if p256LessThanP(&r.x) == 0 {
|
if p256LessThanP(&r.x) == 0 {
|
||||||
return nil, errors.New("invalid P256 element encoding")
|
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")
|
return nil, errors.New("invalid scalar length")
|
||||||
}
|
}
|
||||||
scalarReversed := new(p256OrdElement)
|
scalarReversed := new(p256OrdElement)
|
||||||
p256OrdBigToLittle(scalarReversed, toElementArray(scalar))
|
p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar))
|
||||||
p256OrdReduce(scalarReversed)
|
p256OrdReduce(scalarReversed)
|
||||||
r.p256BaseMult(scalarReversed)
|
r.p256BaseMult(scalarReversed)
|
||||||
return r, nil
|
return r, nil
|
||||||
@ -471,7 +471,7 @@ func (r *SM2P256Point) ScalarMult(q *SM2P256Point, scalar []byte) (*SM2P256Point
|
|||||||
return nil, errors.New("invalid scalar length")
|
return nil, errors.New("invalid scalar length")
|
||||||
}
|
}
|
||||||
scalarReversed := new(p256OrdElement)
|
scalarReversed := new(p256OrdElement)
|
||||||
p256OrdBigToLittle(scalarReversed, toElementArray(scalar))
|
p256OrdBigToLittle(scalarReversed, (*[32]byte)(scalar))
|
||||||
p256OrdReduce(scalarReversed)
|
p256OrdReduce(scalarReversed)
|
||||||
r.Set(q).p256ScalarMult(scalarReversed)
|
r.Set(q).p256ScalarMult(scalarReversed)
|
||||||
return r, nil
|
return r, nil
|
||||||
@ -523,8 +523,8 @@ func (p *SM2P256Point) bytes(out *[p256UncompressedLength]byte) []byte {
|
|||||||
p.affineFromMont(x, y)
|
p.affineFromMont(x, y)
|
||||||
|
|
||||||
out[0] = 4 // Uncompressed form.
|
out[0] = 4 // Uncompressed form.
|
||||||
p256LittleToBig(toElementArray(out[1:33]), x)
|
p256LittleToBig((*[32]byte)(out[1:33]), x)
|
||||||
p256LittleToBig(toElementArray(out[33:65]), y)
|
p256LittleToBig((*[32]byte)(out[33:65]), y)
|
||||||
|
|
||||||
return out[:]
|
return out[:]
|
||||||
}
|
}
|
||||||
@ -562,7 +562,7 @@ func (p *SM2P256Point) bytesX(out *[p256ElementLength]byte) ([]byte, error) {
|
|||||||
p256Sqr(x, x, 1)
|
p256Sqr(x, x, 1)
|
||||||
p256Mul(x, &p.x, x)
|
p256Mul(x, &p.x, x)
|
||||||
p256FromMont(x, x)
|
p256FromMont(x, x)
|
||||||
p256LittleToBig(toElementArray(out[:]), x)
|
p256LittleToBig((*[32]byte)(out[:]), x)
|
||||||
|
|
||||||
return out[:], nil
|
return out[:], nil
|
||||||
}
|
}
|
||||||
@ -586,7 +586,7 @@ func (p *SM2P256Point) bytesCompressed(out *[p256CompressedLength]byte) []byte {
|
|||||||
p.affineFromMont(x, y)
|
p.affineFromMont(x, y)
|
||||||
|
|
||||||
out[0] = 2 | byte(y[0]&1)
|
out[0] = 2 | byte(y[0]&1)
|
||||||
p256LittleToBig(toElementArray(out[1:33]), x)
|
p256LittleToBig((*[32]byte)(out[1:33]), x)
|
||||||
|
|
||||||
return out[:]
|
return out[:]
|
||||||
}
|
}
|
||||||
|
@ -120,16 +120,8 @@ func (e *gfP) Sqrt(f *gfP) {
|
|||||||
e.Set(i)
|
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) {
|
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.
|
// 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 {
|
func (e *gfP) Unmarshal(in []byte) error {
|
||||||
gfpUnmarshal(e, toElementArray(in))
|
gfpUnmarshal(e, (*[32]byte)(in))
|
||||||
// Ensure the point respects the curve modulus
|
// Ensure the point respects the curve modulus
|
||||||
// TODO: Do we need to change it to constant time version ?
|
// TODO: Do we need to change it to constant time version ?
|
||||||
for i := 3; i >= 0; i-- {
|
for i := 3; i >= 0; i-- {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user