Skip to content

Commit

Permalink
sm9/bn256: add bilinearity test case
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Jul 12, 2023
1 parent 9ec8d3b commit fc287b6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sm9/bn256/bn_pair_b6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
17 changes: 17 additions & 0 deletions sm9/bn256/bn_pair_b6_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bn256

import (
"crypto/rand"
"fmt"
"math/big"
"testing"
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions sm9/bn256/bn_pair_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bn256

import (
"crypto/rand"
"math/big"
"testing"
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sm9/bn256/gfp6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions sm9/bn256/gfp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit fc287b6

Please sign in to comment.