1 package bls12381
2
3 import (
4 "fmt"
5 "math/rand"
6 "testing"
7
8 "github.com/cloudflare/circl/ecc/bls12381/ff"
9 "github.com/cloudflare/circl/internal/test"
10 )
11
12 func TestProdPair(t *testing.T) {
13 const testTimes = 1 << 5
14 const N = 3
15
16 listG1 := [N]*G1{}
17 listG2 := [N]*G2{}
18 listSc := [N]*Scalar{}
19 var ePQn, got Gt
20
21 for i := 0; i < testTimes; i++ {
22 got.SetIdentity()
23 for j := 0; j < N; j++ {
24 listG1[j] = randomG1(t)
25 listG2[j] = randomG2(t)
26 listSc[j] = randomScalar(t)
27
28 ePQ := Pair(listG1[j], listG2[j])
29 ePQn.Exp(ePQ, listSc[j])
30 got.Mul(&got, &ePQn)
31 }
32
33 want := ProdPair(listG1[:], listG2[:], listSc[:])
34
35 if !got.IsEqual(want) {
36 test.ReportError(t, got, want)
37 }
38 }
39 }
40
41 func TestProdPairFrac(t *testing.T) {
42 const testTimes = 1 << 5
43 const N = 5
44
45 listG1 := [N]*G1{}
46 listG2 := [N]*G2{}
47 listSc := [N]*Scalar{}
48 listSigns := [N]int{}
49 var ePQn, got Gt
50
51 for i := 0; i < testTimes; i++ {
52 got.SetIdentity()
53 for j := 0; j < N; j++ {
54 listG1[j] = randomG1(t)
55 listG2[j] = randomG2(t)
56 listSc[j] = &Scalar{}
57 coin := rand.Int31n(2)
58 switch coin {
59 case 0:
60 listSc[j].SetOne()
61 listSc[j].Neg()
62 listSigns[j] = -1
63
64 case 1:
65 listSc[j].SetOne()
66 listSigns[j] = 1
67 }
68
69 ePQ := Pair(listG1[j], listG2[j])
70 ePQn.Exp(ePQ, listSc[j])
71 got.Mul(&got, &ePQn)
72 }
73
74 want := ProdPairFrac(listG1[:], listG2[:], listSigns[:])
75
76 if !got.IsEqual(want) {
77 test.ReportError(t, got, want)
78 }
79 }
80 }
81
82 func TestPairBilinear(t *testing.T) {
83 testTimes := 1 << 5
84 for i := 0; i < testTimes; i++ {
85 g1 := G1Generator()
86 g2 := G2Generator()
87 a := randomScalar(t)
88 b := randomScalar(t)
89
90 ab := &Scalar{}
91 ab.Mul(a, b)
92 p := &G1{}
93 q := &G2{}
94 p.ScalarMult(a, g1)
95 q.ScalarMult(b, g2)
96 lhs := Pair(p, q)
97 tmp := Pair(g1, g2)
98 rhs := &Gt{}
99 rhs.Exp(tmp, ab)
100 if !lhs.IsEqual(rhs) {
101 test.ReportError(t, lhs, rhs)
102 }
103 }
104 }
105
106 func TestPairIdentity(t *testing.T) {
107 g1id := &G1{}
108 g2id := &G2{}
109 g1 := G1Generator()
110 g2 := G2Generator()
111 g1id.SetIdentity()
112 g2id.SetIdentity()
113 one := &Gt{}
114 one.SetIdentity()
115 ans := Pair(g1id, g2)
116 if !ans.IsEqual(one) {
117 test.ReportError(t, ans, one)
118 }
119 ans = Pair(g1, g2id)
120 if !ans.IsEqual(one) {
121 test.ReportError(t, ans, one)
122 }
123 }
124
125 func BenchmarkMiller(b *testing.B) {
126 g1 := G1Generator()
127 g2 := G2Generator()
128 mi := new(ff.Fp12)
129 b.ResetTimer()
130 for i := 0; i < b.N; i++ {
131 miller(mi, g1, g2)
132 }
133 }
134
135 func BenchmarkFinalExpo(b *testing.B) {
136 g1 := G1Generator()
137 g2 := G2Generator()
138 mi := new(ff.Fp12)
139 miller(mi, g1, g2)
140 c := &ff.Cyclo6{}
141 u := &ff.URoot{}
142 g := &Gt{}
143
144 ff.EasyExponentiation(c, mi)
145
146 b.Run("EasyExp", func(b *testing.B) {
147 for i := 0; i < b.N; i++ {
148 ff.EasyExponentiation(c, mi)
149 }
150 })
151 b.Run("HardExp", func(b *testing.B) {
152 for i := 0; i < b.N; i++ {
153 ff.HardExponentiation(u, c)
154 }
155 })
156 b.Run("FinalExp", func(b *testing.B) {
157 for i := 0; i < b.N; i++ {
158 finalExp(g, mi)
159 }
160 })
161 }
162
163 func BenchmarkPair(b *testing.B) {
164 g1 := G1Generator()
165 g2 := G2Generator()
166
167 const N = 3
168 listG1 := [N]*G1{}
169 listG2 := [N]*G2{}
170 listExp := [N]*Scalar{}
171 for i := 0; i < N; i++ {
172 listG1[i] = new(G1)
173 *listG1[i] = *g1
174 listG2[i] = new(G2)
175 *listG2[i] = *g2
176 listExp[i] = randomScalar(b)
177 }
178
179 b.Run("Pair", func(b *testing.B) {
180 for i := 0; i < b.N; i++ {
181 Pair(g1, g2)
182 }
183 })
184 b.Run(fmt.Sprintf("ProdPair%v", N), func(b *testing.B) {
185 for i := 0; i < b.N; i++ {
186 ProdPair(listG1[:], listG2[:], listExp[:])
187 }
188 })
189 }
190
View as plain text