From fc287b6e961349090f04cda95a590cf5e500fb38 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Wed, 12 Jul 2023 15:09:25 +0800 Subject: [PATCH] sm9/bn256: add bilinearity test case --- sm9/bn256/bn_pair_b6.go | 2 +- sm9/bn256/bn_pair_b6_test.go | 17 +++++++++++++++++ sm9/bn256/bn_pair_test.go | 17 +++++++++++++++++ sm9/bn256/gfp6.go | 2 +- sm9/bn256/gfp_test.go | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/sm9/bn256/bn_pair_b6.go b/sm9/bn256/bn_pair_b6.go index 2bd6a1c7..c65682eb 100644 --- a/sm9/bn256/bn_pair_b6.go +++ b/sm9/bn256/bn_pair_b6.go @@ -50,7 +50,7 @@ func millerB6(q *twistPoint, p *curvePoint) *gfP12b6 { r := &twistPoint{} r.Set(aAffine) - r2 := (&gfP2{}).Square(&aAffine.y) + r2 := (&gfP2{}).SquareNC(&aAffine.y) a, b, c := &gfP2{}, &gfP2{}, &gfP2{} newR := &twistPoint{} diff --git a/sm9/bn256/bn_pair_b6_test.go b/sm9/bn256/bn_pair_b6_test.go index 8ef6957d..8f76a4d6 100644 --- a/sm9/bn256/bn_pair_b6_test.go +++ b/sm9/bn256/bn_pair_b6_test.go @@ -1,6 +1,7 @@ package bn256 import ( + "crypto/rand" "fmt" "math/big" "testing" @@ -81,6 +82,22 @@ func Test_PairingB6_B2_2(t *testing.T) { } } +func TestBilinearityB6(t *testing.T) { + for i := 0; i < 2; i++ { + a, p1, _ := RandomG1(rand.Reader) + b, p2, _ := RandomG2(rand.Reader) + e1 := pairingB6(p2.p, p1.p) + + e2 := pairingB6(twistGen, curveGen) + e2.Exp(e2, a) + e2.Exp(e2, b) + + if *e1 != *e2 { + t.Fatalf("bad pairing result: %s", e1) + } + } +} + func BenchmarkFinalExponentiationB6(b *testing.B) { x := &gfP12b6{ p6, diff --git a/sm9/bn256/bn_pair_test.go b/sm9/bn256/bn_pair_test.go index 5a60bafc..d18ef09e 100644 --- a/sm9/bn256/bn_pair_test.go +++ b/sm9/bn256/bn_pair_test.go @@ -1,6 +1,7 @@ package bn256 import ( + "crypto/rand" "math/big" "testing" ) @@ -145,6 +146,22 @@ func Test_finalExponentiation(t *testing.T) { } } +func TestBilinearity(t *testing.T) { + for i := 0; i < 2; i++ { + a, p1, _ := RandomG1(rand.Reader) + b, p2, _ := RandomG2(rand.Reader) + e1 := Pair(p1, p2) + + e2 := Pair(&G1{curveGen}, &G2{twistGen}) + e2.ScalarMult(e2, a) + e2.ScalarMult(e2, b) + + if *e1.p != *e2.p { + t.Fatalf("bad pairing result: %s", e1) + } + } +} + func BenchmarkFinalExponentiation(b *testing.B) { x := testGfp12 exp := new(big.Int).Exp(p, big.NewInt(12), nil) diff --git a/sm9/bn256/gfp6.go b/sm9/bn256/gfp6.go index 44d64d00..b6cb982a 100644 --- a/sm9/bn256/gfp6.go +++ b/sm9/bn256/gfp6.go @@ -156,7 +156,7 @@ func (e *gfP6) MulS(a *gfP6) *gfP6 { tz := &gfP2{} tz.x.Set(&a.x.y) - gfpAdd(&tz.y, &a.x.x, &a.x.x) + gfpDouble(&tz.y, &a.x.x) gfpNeg(&tz.y, &tz.y) e.y.Set(&a.z) diff --git a/sm9/bn256/gfp_test.go b/sm9/bn256/gfp_test.go index 016e2ab0..49720048 100644 --- a/sm9/bn256/gfp_test.go +++ b/sm9/bn256/gfp_test.go @@ -286,3 +286,23 @@ func BenchmarkGfPNeg2(b *testing.B) { gfpSub(ret, zero, x) } } + +func BenchmarkGfPInvert(b *testing.B) { + x := fromBigInt(bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596")) + b.ReportAllocs() + b.ResetTimer() + ret := &gfP{} + for i := 0; i < b.N; i++ { + ret.Invert(x) + } +} + +func BenchmarkGfPInvert2(b *testing.B) { + x := fromBigInt(bigFromHex("9093a2b979e6186f43a9b28d41ba644d533377f2ede8c66b19774bf4a9c7a596")) + b.ReportAllocs() + b.ResetTimer() + ret := &gfP{} + for i := 0; i < b.N; i++ { + ret.exp(x, pMinus2) + } +}