-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcl_test.go
354 lines (316 loc) · 7.58 KB
/
mcl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package mcl
import (
"flag"
"fmt"
"os"
"testing"
"github.com/alinush/go-mcl/utils"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var _curveArg = utils.GetCurveArgument()
// This TestMain() function will be shared by all mcl *_test.go files.
func TestMain(m *testing.M) {
flag.Parse()
log.SetLevel(log.DebugLevel)
// Initializes the mcl library with the specified curve
InitFromString(*_curveArg)
os.Exit(m.Run())
}
func TestCopy(t *testing.T) {
var a Fr
var x, y G1
var w, z G1
a.Random()
x.Random()
y = x
w.Random()
z = w
assert.True(t, x.IsEqual(&y))
assert.True(t, w.IsEqual(&z))
y.Mul(&a)
z.Mul(&a)
assert.False(t, x.IsEqual(&y))
assert.False(t, w.IsEqual(&z))
}
func TestBadPointOfG2(t *testing.T) {
var Q G2
// this value is not in G2 so should return an error
err := Q.SetString("1 18d3d8c085a5a5e7553c3a4eb628e88b8465bf4de2612e35a0a4eb018fb0c82e9698896031e62fd7633ffd824a859474 1dc6edfcf33e29575d4791faed8e7203832217423bf7f7fbf1f6b36625b12e7132c15fbc15562ce93362a322fb83dd0d 65836963b1f7b6959030ddfa15ab38ce056097e91dedffd996c1808624fa7e2644a77be606290aa555cda8481cfb3cb 1b77b708d3d4f65aeedf54b58393463a42f0dc5856baadb5ce608036baeca398c5d9e6b169473a8838098fd72fd28b50", 16)
if err == nil {
t.Error(err)
}
}
func TestGT(t *testing.T) {
var x GT
x.Clear()
if !x.IsZero() {
t.Errorf("not zero")
}
x.SetInt64(1)
if !x.IsOne() {
t.Errorf("not one")
}
}
func TestHash(t *testing.T) {
var x Fr
if !x.SetHashOf([]byte("abc")) {
t.Error("SetHashOf")
}
//fmt.Printf("x=%s\n", x.GetString(16))
}
func TestNegAdd(t *testing.T) {
var x Fr
var P1, P2, P3 G1
var Q1, Q2, Q3 G2
err := P1.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
}
err = Q1.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
}
//fmt.Printf("P1=%s\n", P1.GetString(16))
//fmt.Printf("Q1=%s\n", Q1.GetString(16))
G1Neg(&P2, &P1)
G2Neg(&Q2, &Q1)
//fmt.Printf("P2=%s\n", P2.GetString(16))
//fmt.Printf("Q2=%s\n", Q2.GetString(16))
x.SetInt64(-1)
G1Mul(&P3, &P1, &x)
G2Mul(&Q3, &Q1, &x)
if !P2.IsEqual(&P3) {
t.Errorf("P2 != P3 %s\n", P3.GetString(16))
}
if !Q2.IsEqual(&Q3) {
t.Errorf("Q2 != Q3 %s\n", Q3.GetString(16))
}
G1Add(&P2, &P2, &P1)
G2Add(&Q2, &Q2, &Q1)
if !P2.IsZero() {
t.Errorf("P2 is not zero %s\n", P2.GetString(16))
}
if !Q2.IsZero() {
t.Errorf("Q2 is not zero %s\n", Q2.GetString(16))
}
}
func TestVecG1(t *testing.T) {
N := 50
xVec := make([]G1, N)
yVec := make([]Fr, N)
xVec[0].HashAndMapTo([]byte("aa"))
var R1, R2 G1
for i := 0; i < N; i++ {
if i > 0 {
G1Dbl(&xVec[i], &xVec[i-1])
}
yVec[i].Random()
G1Mul(&R1, &xVec[i], &yVec[i])
G1Add(&R2, &R2, &R1)
}
G1MulVec(&R1, xVec, yVec)
if !R1.IsEqual(&R2) {
t.Errorf("wrong G1MulVec")
}
}
func TestVecG2(t *testing.T) {
N := 50
xVec := make([]G2, N)
yVec := make([]Fr, N)
xVec[0].HashAndMapTo([]byte("aa"))
var R1, R2 G2
for i := 0; i < N; i++ {
if i > 0 {
G2Dbl(&xVec[i], &xVec[i-1])
}
yVec[i].Random()
G2Mul(&R1, &xVec[i], &yVec[i])
G2Add(&R2, &R2, &R1)
}
G2MulVec(&R1, xVec, yVec)
if !R1.IsEqual(&R2) {
t.Errorf("wrong G2MulVec")
}
}
func TestVecPairing(t *testing.T) {
N := 50
xVec := make([]G1, N)
yVec := make([]G2, N)
var e1, e2 GT
e1.SetInt64(1)
for i := 0; i < N; i++ {
xVec[0].HashAndMapTo([]byte("aa"))
yVec[0].HashAndMapTo([]byte("aa"))
Pairing(&e2, &xVec[i], &yVec[i])
GTMul(&e1, &e1, &e2)
}
MillerLoopVec(&e2, xVec, yVec)
FinalExp(&e2, &e2)
if !e1.IsEqual(&e2) {
t.Errorf("wrong MillerLoopVec")
}
}
func TestPairing(t *testing.T) {
var a, b, ab Fr
err := a.SetString("123", 10)
if err != nil {
t.Error(err)
return
}
err = b.SetString("456", 10)
if err != nil {
t.Error(err)
return
}
FrMul(&ab, &a, &b)
var P, aP G1
var Q, bQ G2
err = P.HashAndMapTo([]byte("this"))
if err != nil {
t.Error(err)
return
}
//fmt.Printf("P=%s\n", P.GetString(16))
G1Mul(&aP, &P, &a)
//fmt.Printf("aP=%s\n", aP.GetString(16))
err = Q.HashAndMapTo([]byte("that"))
if err != nil {
t.Error(err)
return
}
//fmt.Printf("Q=%s\n", Q.GetString(16))
G2Mul(&bQ, &Q, &b)
//fmt.Printf("bQ=%s\n", bQ.GetString(16))
var e1, e2 GT
Pairing(&e1, &P, &Q)
//fmt.Printf("e1=%s\n", e1.GetString(16))
Pairing(&e2, &aP, &bQ)
//fmt.Printf("e2=%s\n", e1.GetString(16))
GTPow(&e1, &e1, &ab)
//fmt.Printf("e1=%s\n", e1.GetString(16))
if !e1.IsEqual(&e2) {
t.Errorf("not equal pairing\n%s\n%s", e1.GetString(16), e2.GetString(16))
}
{
s := P.GetString(IoSerializeHexStr)
var P1 G1
P1.SetString(s, IoSerializeHexStr)
if !P1.IsEqual(&P) {
t.Error("not equal to P")
return
}
s = Q.GetString(IoSerializeHexStr)
var Q1 G2
Q1.SetString(s, IoSerializeHexStr)
if !Q1.IsEqual(&Q) {
t.Error("not equal to Q")
return
}
}
}
func TestSerialize(t *testing.T) {
var x, xx Fr
var y, yy Fp
var P, PP G1
var Q, QQ G2
var e, ee GT
x.Random()
y.Random()
P.HashAndMapTo([]byte("abc"))
G1Dbl(&P, &P)
Q.HashAndMapTo([]byte("abc"))
G2Dbl(&Q, &Q)
Pairing(&e, &P, &Q)
if xx.Deserialize(x.Serialize()) != nil || !x.IsEqual(&xx) {
t.Error("Serialize Fr")
}
if yy.Deserialize(y.Serialize()) != nil || !y.IsEqual(&yy) {
t.Error("Serialize Fp")
}
if PP.Deserialize(P.Serialize()) != nil || !P.IsEqual(&PP) {
t.Error("Serialize G1")
}
if QQ.Deserialize(Q.Serialize()) != nil || !Q.IsEqual(&QQ) {
t.Error("Serialize G2")
}
if ee.Deserialize(e.Serialize()) != nil || !e.IsEqual(&ee) {
t.Error("Serialize GT")
}
G1Dbl(&PP, &PP)
if PP.DeserializeUncompressed(P.SerializeUncompressed()) != nil || !P.IsEqual(&PP) {
t.Error("SerializeUncompressed G1")
}
G2Dbl(&QQ, &QQ)
if QQ.DeserializeUncompressed(Q.SerializeUncompressed()) != nil || !Q.IsEqual(&QQ) {
t.Error("SerializeUncompressed G2")
}
}
func TestETHserialize(t *testing.T) {
b := make([]byte, 32)
b[0] = 0x12
b[1] = 0x34
var x Fr
SetETHserialization(false)
x.Deserialize(b)
//fmt.Printf("AAA x=%s\n", x.GetString(16))
SetETHserialization(true)
x.Deserialize(b)
//fmt.Printf("AAA x=%s\n", x.GetString(16))
}
func TestSerializedSize(t *testing.T) {
var x Fr
var P G1
var Q G2
var e GT
fmt.Printf("GetCurveOrder(): %v\n", GetCurveOrder())
fmt.Printf("GetFieldOrder(): %v\n", GetFieldOrder())
x.Random()
P.HashAndMapTo([]byte("abc"))
Q.HashAndMapTo([]byte("abc"))
Pairing(&e, &P, &Q)
xs := x.Serialize()
ps := P.Serialize()
qs := Q.Serialize()
es := e.Serialize()
fmt.Printf("Fr serialized to %d bytes\n", len(xs))
fmt.Printf("G1 serialized to %d bytes\n", len(ps))
fmt.Printf("G2 serialized to %d bytes\n", len(qs))
fmt.Printf("GT serialized to %d bytes\n", len(es))
}
func TestRootsOfUnityGeneration(t *testing.T) {
// Repeat this several times
for c := 0; c <= 1000; c++ {
// Find a generator for the multiplicative subgroup of the field
g := RandomFieldGenerator()
//log.Printf("Generator of G_{r-1}: %v", g.GetString(10))
k := 1
for i := 0; i < 10; i++ {
// Compute n = 2^k
var n Fr
FrPow2(&n, k)
nn := int(n.ToBigInt().Int64())
//log.Printf("k = %v, n = %v", k, n.GetString(10))
// Find an nth root of unity
omega := GetRootOfUnityFromGen(&g, &n)
//log.Printf("Primitive nth root of unity: %v", omega.GetString(10))
// Make sure it forms a subgroup
var acc Fr = omega
for j := 1; j < nn-1; j++ {
FrMul(&acc, &acc, &omega)
log.Tracef("omega^%v = %v", j+1, acc.GetString(10))
if acc.IsOne() {
log.Panicf("omega^%v is equal to 1, and %v < %v", j+1, j+1, nn)
}
}
// Lastly, make sure omega^n = 1
FrMul(&acc, &acc, &omega)
log.Tracef("omega^%v = %v", nn, acc.GetString(10))
if !acc.IsOne() {
log.Panicf("omega^%v is NOT equal to 1", nn)
}
// Try the next k
k++
}
}
}