gmsm/internal/sm2ec/sm2p256_asm_s390x_test.go

87 lines
2.2 KiB
Go
Raw Normal View History

2024-08-23 14:42:49 +08:00
//go:build s390x && !purego
package sm2ec
import (
"math/big"
"testing"
)
var bigOne = big.NewInt(1)
// fromBig converts a *big.Int into a format used by this code.
func fromBig(out *[4]uint64, big *big.Int) {
for i := range out {
out[i] = 0
}
for i, v := range big.Bits() {
out[i] = uint64(v)
}
}
func montFromBig(out *[4]uint64, n *big.Int) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
r := new(big.Int).Lsh(bigOne, 256)
// out = big * R mod P
outBig := new(big.Int).Mul(n, r)
outBig.Mod(outBig, p)
fromBig(out, outBig)
}
func toBigInt(in *p256Element) *big.Int {
var valBytes [32]byte
p256LittleToBig(&valBytes, in)
return new(big.Int).SetBytes(valBytes[:])
}
func ordElmToBigInt(in *p256OrdElement) *big.Int {
var valBytes [32]byte
p256OrdLittleToBig(&valBytes, in)
return new(big.Int).SetBytes(valBytes[:])
}
func testP256FromMont(v *big.Int, t *testing.T) {
val := new(p256Element)
montFromBig((*[4]uint64)(val), v)
res := new(p256Element)
p256FromMont(res, val)
if toBigInt(res).Cmp(v) != 0 {
2024-08-23 15:29:36 +08:00
t.Errorf("p256FromMont failed for %x", v.Bytes())
2024-08-23 14:42:49 +08:00
}
}
func TestP256FromMont(t *testing.T) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16)
for i := 0; i < 20; i++ {
bigVal := big.NewInt(int64(i))
testP256FromMont(bigVal, t)
bigVal = new(big.Int).Sub(p, big.NewInt(int64(i)))
testP256FromMont(bigVal, t)
}
}
2024-08-23 15:25:42 +08:00
func testP256OrderReduce(v, expected *big.Int, t *testing.T) {
2024-08-23 14:42:49 +08:00
val := new(p256OrdElement)
2024-08-23 15:25:42 +08:00
fromBig((*[4]uint64)(val), v)
2024-08-23 14:42:49 +08:00
p256OrdReduce(val)
2024-08-23 15:25:42 +08:00
if ordElmToBigInt(val).Cmp(expected) != 0 {
2024-08-23 16:01:41 +08:00
t.Errorf("p256OrdReduce failed for %x, expected %x", v.Bytes(), expected.Bytes())
2024-08-23 14:42:49 +08:00
}
}
func TestP256OrderReduce(t *testing.T) {
p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
for i := 0; i < 20; i++ {
bigVal := big.NewInt(int64(i))
2024-08-23 15:25:42 +08:00
testP256OrderReduce(bigVal, bigVal, t)
bigVal = new(big.Int).Add(p, big.NewInt(int64(i)))
testP256OrderReduce(bigVal, big.NewInt(int64(i)), t)
2024-08-23 14:42:49 +08:00
}
2024-08-23 16:01:41 +08:00
testP256OrderReduce(p, big.NewInt(0), t)
for i := 1; i < 20; i++ {
bigVal := new(big.Int).Sub(p, big.NewInt(int64(i)))
testP256OrderReduce(bigVal, bigVal, t)
}
2024-08-23 14:42:49 +08:00
}