mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 12:16:20 +08:00
test cases to original file
This commit is contained in:
parent
35fb02af53
commit
b77041404d
@ -1,7 +1,6 @@
|
|||||||
package sm9
|
package sm9
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -51,131 +50,6 @@ func init() {
|
|||||||
expected_b2_2.z.y.y = *fromBigInt(bigFromHex("934FDDA6D3AB48C8571CE2354B79742AA498CB8CDDE6BD1FA5946345A1A652F6"))
|
expected_b2_2.z.y.y = *fromBigInt(bigFromHex("934FDDA6D3AB48C8571CE2354B79742AA498CB8CDDE6BD1FA5946345A1A652F6"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_gfpBasicOperations(t *testing.T) {
|
|
||||||
x := fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141"))
|
|
||||||
y := fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B"))
|
|
||||||
expectedAdd := fromBigInt(bigFromHex("0691692307d370af56226e57920199fbbe10f216c67fbc9468c7f225a4b1f21f"))
|
|
||||||
expectedSub := fromBigInt(bigFromHex("67b381821c52a5624f3304a8149be8461e3bc07adcb872c38aa65051ba53ba97"))
|
|
||||||
expectedNeg := fromBigInt(bigFromHex("7f1d8aad70909be90358f1d02240062433cc3a0248ded72febb879ec33ce6f22"))
|
|
||||||
expectedMul := fromBigInt(bigFromHex("3d08bbad376584e4f74bd31f78f716372b96ba8c3f939c12b8d54e79b6489e76"))
|
|
||||||
expectedMul2 := fromBigInt(bigFromHex("1df94a9e05a559ff38e0ab50cece734dc058d33738ceacaa15986a67cbff1ef6"))
|
|
||||||
|
|
||||||
ret := &gfP{}
|
|
||||||
gfpAdd(ret, x, y)
|
|
||||||
if *expectedAdd != *ret {
|
|
||||||
t.Errorf("add not same")
|
|
||||||
}
|
|
||||||
|
|
||||||
gfpSub(ret, y, x)
|
|
||||||
if *expectedSub != *ret {
|
|
||||||
t.Errorf("sub not same")
|
|
||||||
}
|
|
||||||
|
|
||||||
gfpNeg(ret, y)
|
|
||||||
if *expectedNeg != *ret {
|
|
||||||
t.Errorf("neg not same")
|
|
||||||
}
|
|
||||||
|
|
||||||
gfpMul(ret, x, y)
|
|
||||||
if *expectedMul != *ret {
|
|
||||||
t.Errorf("mul not same")
|
|
||||||
}
|
|
||||||
|
|
||||||
gfpMul(ret, ret, ret)
|
|
||||||
if *expectedMul2 != *ret {
|
|
||||||
t.Errorf("mul not same")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGfpExp(t *testing.T) {
|
|
||||||
xI := bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596")
|
|
||||||
x := fromBigInt(xI)
|
|
||||||
ret, ret3 := &gfP{}, &gfP{}
|
|
||||||
ret.exp(x, pMinus2)
|
|
||||||
|
|
||||||
gfpMul(ret3, x, ret)
|
|
||||||
if *ret3 != *one {
|
|
||||||
t.Errorf("got %v, expected %v\n", ret3, one)
|
|
||||||
}
|
|
||||||
montDecode(ret, ret)
|
|
||||||
|
|
||||||
ret2 := new(big.Int).Exp(xI, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"), p)
|
|
||||||
if hex.EncodeToString(ret2.Bytes()) != ret.String() {
|
|
||||||
t.Errorf("exp not same, got %v, expected %v\n", ret, hex.EncodeToString(ret2.Bytes()))
|
|
||||||
}
|
|
||||||
|
|
||||||
ret1 := &gfP{}
|
|
||||||
ret1.exp2(x, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"))
|
|
||||||
montDecode(ret1, ret1)
|
|
||||||
if hex.EncodeToString(ret2.Bytes()) != ret1.String() {
|
|
||||||
t.Errorf("exp not same, got %v, expected %v\n", ret1, hex.EncodeToString(ret2.Bytes()))
|
|
||||||
}
|
|
||||||
|
|
||||||
xInv := new(big.Int).ModInverse(xI, p)
|
|
||||||
if hex.EncodeToString(ret2.Bytes()) != hex.EncodeToString(xInv.Bytes()) {
|
|
||||||
t.Errorf("exp not same, got %v, expected %v\n", hex.EncodeToString(ret2.Bytes()), hex.EncodeToString(xInv.Bytes()))
|
|
||||||
}
|
|
||||||
|
|
||||||
x2 := new(big.Int).Mul(xI, xInv)
|
|
||||||
x2.Mod(x2, p)
|
|
||||||
if big.NewInt(1).Cmp(x2) != 0 {
|
|
||||||
t.Errorf("not same")
|
|
||||||
}
|
|
||||||
|
|
||||||
xInvGfp := fromBigInt(xInv)
|
|
||||||
gfpMul(ret, x, xInvGfp)
|
|
||||||
if *ret != *one {
|
|
||||||
t.Errorf("got %v, expected %v", ret, one)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGfpInvert(t *testing.T) {
|
|
||||||
x := fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141"))
|
|
||||||
xInv := &gfP{}
|
|
||||||
xInv.Invert(x)
|
|
||||||
y := &gfP{}
|
|
||||||
gfpMul(y, x, xInv)
|
|
||||||
if *y != *one {
|
|
||||||
t.Errorf("got %v, expected %v", y, one)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGfpDiv(t *testing.T) {
|
|
||||||
x := fromBigInt(bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596"))
|
|
||||||
ret := &gfP{}
|
|
||||||
ret.Div2(x)
|
|
||||||
gfpAdd(ret, ret, ret)
|
|
||||||
if *ret != *x {
|
|
||||||
t.Errorf("got %v, expected %v", ret, x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGfpSqrt(t *testing.T) {
|
|
||||||
tests := []string{
|
|
||||||
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
|
||||||
"92fe90b700fbd4d8cc177d300ed16e4e15471a681b2c9e3728c1b82c885e49c2",
|
|
||||||
}
|
|
||||||
for i, test := range tests {
|
|
||||||
y2 := bigFromHex(test)
|
|
||||||
y21 := new(big.Int).ModSqrt(y2, p)
|
|
||||||
|
|
||||||
y3 := new(big.Int).Mul(y21, y21)
|
|
||||||
y3.Mod(y3, p)
|
|
||||||
if y2.Cmp(y3) != 0 {
|
|
||||||
t.Error("Invalid sqrt")
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := fromBigInt(y2)
|
|
||||||
tmp.Sqrt(tmp)
|
|
||||||
montDecode(tmp, tmp)
|
|
||||||
var res [32]byte
|
|
||||||
tmp.Marshal(res[:])
|
|
||||||
if hex.EncodeToString(res[:]) != hex.EncodeToString(y21.Bytes()) {
|
|
||||||
t.Errorf("case %v, got %v, expected %v\n", i, hex.EncodeToString(res[:]), hex.EncodeToString(y21.Bytes()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Pairing_A2(t *testing.T) {
|
func Test_Pairing_A2(t *testing.T) {
|
||||||
pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
|
pk := bigFromHex("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
|
||||||
g2 := &G2{}
|
g2 := &G2{}
|
||||||
|
18
sm9/gfp.go
18
sm9/gfp.go
@ -103,24 +103,6 @@ func (e *gfP) exp(f *gfP, bits [4]uint64) {
|
|||||||
e.Set(sum)
|
e.Set(sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *gfP) exp2(f *gfP, power *big.Int) *gfP {
|
|
||||||
sum := &gfP{}
|
|
||||||
sum.Set(one)
|
|
||||||
t := &gfP{}
|
|
||||||
|
|
||||||
for i := power.BitLen() - 1; i >= 0; i-- {
|
|
||||||
gfpMul(t, sum, sum)
|
|
||||||
if power.Bit(i) != 0 {
|
|
||||||
gfpMul(sum, t, f)
|
|
||||||
} else {
|
|
||||||
sum.Set(t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
e.Set(sum)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *gfP) Invert(f *gfP) {
|
func (e *gfP) Invert(f *gfP) {
|
||||||
e.exp(f, pMinus2)
|
e.exp(f, pMinus2)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,77 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_gfpBasicOperations(t *testing.T) {
|
||||||
|
x := fromBigInt(bigFromHex("85AEF3D078640C98597B6027B441A01FF1DD2C190F5E93C454806C11D8806141"))
|
||||||
|
y := fromBigInt(bigFromHex("3722755292130B08D2AAB97FD34EC120EE265948D19C17ABF9B7213BAF82D65B"))
|
||||||
|
expectedAdd := fromBigInt(bigFromHex("0691692307d370af56226e57920199fbbe10f216c67fbc9468c7f225a4b1f21f"))
|
||||||
|
expectedSub := fromBigInt(bigFromHex("67b381821c52a5624f3304a8149be8461e3bc07adcb872c38aa65051ba53ba97"))
|
||||||
|
expectedNeg := fromBigInt(bigFromHex("7f1d8aad70909be90358f1d02240062433cc3a0248ded72febb879ec33ce6f22"))
|
||||||
|
expectedMul := fromBigInt(bigFromHex("3d08bbad376584e4f74bd31f78f716372b96ba8c3f939c12b8d54e79b6489e76"))
|
||||||
|
expectedMul2 := fromBigInt(bigFromHex("1df94a9e05a559ff38e0ab50cece734dc058d33738ceacaa15986a67cbff1ef6"))
|
||||||
|
|
||||||
|
ret := &gfP{}
|
||||||
|
gfpAdd(ret, x, y)
|
||||||
|
if *expectedAdd != *ret {
|
||||||
|
t.Errorf("add not same")
|
||||||
|
}
|
||||||
|
|
||||||
|
gfpSub(ret, y, x)
|
||||||
|
if *expectedSub != *ret {
|
||||||
|
t.Errorf("sub not same")
|
||||||
|
}
|
||||||
|
|
||||||
|
gfpNeg(ret, y)
|
||||||
|
if *expectedNeg != *ret {
|
||||||
|
t.Errorf("neg not same")
|
||||||
|
}
|
||||||
|
|
||||||
|
gfpMul(ret, x, y)
|
||||||
|
if *expectedMul != *ret {
|
||||||
|
t.Errorf("mul not same")
|
||||||
|
}
|
||||||
|
|
||||||
|
gfpMul(ret, ret, ret)
|
||||||
|
if *expectedMul2 != *ret {
|
||||||
|
t.Errorf("mul not same")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGfpExp(t *testing.T) {
|
||||||
|
xI := bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596")
|
||||||
|
x := fromBigInt(xI)
|
||||||
|
ret, ret3 := &gfP{}, &gfP{}
|
||||||
|
ret.exp(x, pMinus2)
|
||||||
|
|
||||||
|
gfpMul(ret3, x, ret)
|
||||||
|
if *ret3 != *one {
|
||||||
|
t.Errorf("got %v, expected %v\n", ret3, one)
|
||||||
|
}
|
||||||
|
montDecode(ret, ret)
|
||||||
|
|
||||||
|
ret2 := new(big.Int).Exp(xI, bigFromHex("b640000002a3a6f1d603ab4ff58ec74521f2934b1a7aeedbe56f9b27e351457b"), p)
|
||||||
|
if hex.EncodeToString(ret2.Bytes()) != ret.String() {
|
||||||
|
t.Errorf("exp not same, got %v, expected %v\n", ret, hex.EncodeToString(ret2.Bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
xInv := new(big.Int).ModInverse(xI, p)
|
||||||
|
if hex.EncodeToString(ret2.Bytes()) != hex.EncodeToString(xInv.Bytes()) {
|
||||||
|
t.Errorf("exp not same, got %v, expected %v\n", hex.EncodeToString(ret2.Bytes()), hex.EncodeToString(xInv.Bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
|
x2 := new(big.Int).Mul(xI, xInv)
|
||||||
|
x2.Mod(x2, p)
|
||||||
|
if big.NewInt(1).Cmp(x2) != 0 {
|
||||||
|
t.Errorf("not same")
|
||||||
|
}
|
||||||
|
|
||||||
|
xInvGfp := fromBigInt(xInv)
|
||||||
|
gfpMul(ret, x, xInvGfp)
|
||||||
|
if *ret != *one {
|
||||||
|
t.Errorf("got %v, expected %v", ret, one)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSqrt(t *testing.T) {
|
func TestSqrt(t *testing.T) {
|
||||||
tests := []string{
|
tests := []string{
|
||||||
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
"9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user