[sm9] fix 32-bit GOARCH issue

This commit is contained in:
Sun Yimin 2022-07-07 11:43:00 +08:00 committed by GitHub
parent 1ccbc35f12
commit eca13ba8c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,8 +36,18 @@ func fromBigInt(x *big.Int) (out *gfP) {
} else {
a = new(big.Int).Neg(x)
}
for i, v := range a.Bits() {
out[i] = uint64(v)
bytes := a.Bytes()
if len(bytes) > 32 {
panic("sm9: invalid byte length")
} else if len(bytes) < 32 {
fixedBytes := make([]byte, 32)
copy(fixedBytes[32-len(bytes):], bytes)
bytes = fixedBytes
}
for i := 0; i < 4; i++ {
start := len(bytes) - 8
out[i] = binary.BigEndian.Uint64(bytes[start:])
bytes = bytes[:start]
}
if x.Sign() < 0 {
gfpNeg(out, out)