sm9/bn256: add bilinearity test case

This commit is contained in:
Sun Yimin 2023-07-12 15:09:25 +08:00 committed by GitHub
parent 9ec8d3bc04
commit fc287b6e96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 2 deletions

View File

@ -50,7 +50,7 @@ func millerB6(q *twistPoint, p *curvePoint) *gfP12b6 {
r := &twistPoint{} r := &twistPoint{}
r.Set(aAffine) r.Set(aAffine)
r2 := (&gfP2{}).Square(&aAffine.y) r2 := (&gfP2{}).SquareNC(&aAffine.y)
a, b, c := &gfP2{}, &gfP2{}, &gfP2{} a, b, c := &gfP2{}, &gfP2{}, &gfP2{}
newR := &twistPoint{} newR := &twistPoint{}

View File

@ -1,6 +1,7 @@
package bn256 package bn256
import ( import (
"crypto/rand"
"fmt" "fmt"
"math/big" "math/big"
"testing" "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) { func BenchmarkFinalExponentiationB6(b *testing.B) {
x := &gfP12b6{ x := &gfP12b6{
p6, p6,

View File

@ -1,6 +1,7 @@
package bn256 package bn256
import ( import (
"crypto/rand"
"math/big" "math/big"
"testing" "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) { func BenchmarkFinalExponentiation(b *testing.B) {
x := testGfp12 x := testGfp12
exp := new(big.Int).Exp(p, big.NewInt(12), nil) exp := new(big.Int).Exp(p, big.NewInt(12), nil)

View File

@ -156,7 +156,7 @@ func (e *gfP6) MulS(a *gfP6) *gfP6 {
tz := &gfP2{} tz := &gfP2{}
tz.x.Set(&a.x.y) 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) gfpNeg(&tz.y, &tz.y)
e.y.Set(&a.z) e.y.Set(&a.z)

View File

@ -286,3 +286,23 @@ func BenchmarkGfPNeg2(b *testing.B) {
gfpSub(ret, zero, x) 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)
}
}