forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp511_test.go
407 lines (347 loc) · 7.95 KB
/
fp511_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package csidh
import (
"crypto/rand"
"math/big"
"testing"
"github.com/cloudflare/circl/internal/test"
)
func testFp512Mul3Nominal(t *testing.T, f func(*fp, *fp, uint64)) {
var mod, two64 big.Int
// modulus: 2^512
mod.SetUint64(1).Lsh(&mod, 512)
two64.SetUint64(1).Lsh(&two64, 64)
for i := 0; i < numIter; i++ {
multiplier64, _ := rand.Int(rand.Reader, &two64)
mul64 := multiplier64.Uint64()
fV := randomFp()
exp, _ := new(big.Int).SetString(fp2S(fV), 16)
exp.Mul(exp, multiplier64)
// Truncate to 512 bits
exp.Mod(exp, &mod)
f(&fV, &fV, mul64)
res, _ := new(big.Int).SetString(fp2S(fV), 16)
if exp.Cmp(res) != 0 {
test.ReportError(t, exp, res, fV)
}
}
}
// Check if mul512 produces result
// z = x*y mod 2^512.
func TestFp512Mul3_Nominal(t *testing.T) {
testFp512Mul3Nominal(t, mul512)
testFp512Mul3Nominal(t, mul512Generic)
}
func TestAddRdcRandom(t *testing.T) {
for i := 0; i < numIter; i++ {
a := randomFp()
bigA, _ := new(big.Int).SetString(fp2S(a), 16)
bigA.Mod(bigA, modulus)
copy(a[:], intGetU64(bigA))
b := randomFp()
bigB, _ := new(big.Int).SetString(fp2S(b), 16)
bigB.Mod(bigB, modulus)
copy(b[:], intGetU64(bigB))
addRdc(&a, &a, &b)
bigRet, _ := new(big.Int).SetString(fp2S(a), 16)
bigA.Add(bigA, bigB)
bigA.Mod(bigA, modulus)
if bigRet.Cmp(bigA) != 0 {
test.ReportError(t, bigRet, bigA, a, b)
}
}
}
func TestAddRdcNominal(t *testing.T) {
var res fp
tmp := oneFp512
addRdc(&res, &tmp, &p)
if !eqFp(&res, &tmp) {
t.Errorf("Wrong value\n%X", res)
}
tmp = zeroFp512
addRdc(&res, &p, &p)
if !eqFp(&res, &p) {
t.Errorf("Wrong value\n%X", res)
}
tmp = fp{1, 1, 1, 1, 1, 1, 1, 1}
addRdc(&res, &p, &tmp)
if !eqFp(&res, &tmp) {
t.Errorf("Wrong value\n%X", res)
}
tmp = fp{1, 1, 1, 1, 1, 1, 1, 1}
exp := fp{2, 2, 2, 2, 2, 2, 2, 2}
addRdc(&res, &tmp, &tmp)
if !eqFp(&res, &exp) {
t.Errorf("Wrong value\n%X", res)
}
}
func TestFp512Sub3_Nominal(t *testing.T) {
var ret fp
var mod big.Int
// modulus: 2^512
mod.SetUint64(1).Lsh(&mod, 512)
for i := 0; i < numIter; i++ {
a := randomFp()
bigA, _ := new(big.Int).SetString(fp2S(a), 16)
b := randomFp()
bigB, _ := new(big.Int).SetString(fp2S(b), 16)
sub512(&ret, &a, &b)
bigRet, _ := new(big.Int).SetString(fp2S(ret), 16)
bigA.Sub(bigA, bigB)
// Truncate to 512 bits
bigA.Mod(bigA, &mod)
if bigRet.Cmp(bigA) != 0 {
test.ReportError(t, bigRet, bigA, a, b)
}
}
}
func TestFp512Sub3_DoesntReturnCarry(t *testing.T) {
a := fp{}
b := fp{
0xFFFFFFFFFFFFFFFF, 1,
0, 0,
0, 0,
0, 0,
}
c := fp{
0xFFFFFFFFFFFFFFFF, 2,
0, 0,
0, 0,
0, 0,
}
if sub512(&a, &b, &c) != 1 {
t.Error("Carry not returned")
}
}
func TestFp512Sub3_ReturnsCarry(t *testing.T) {
a := fp{}
b := fp{
0xFFFFFFFFFFFFFFFF, 2,
0, 0,
0, 0,
0, 0,
}
c := fp{
0xFFFFFFFFFFFFFFFF, 1,
0, 0,
0, 0,
0, 0,
}
if sub512(&a, &b, &c) != 0 {
t.Error("Carry not returned")
}
}
func testCswap(t *testing.T, f func(*fp, *fp, uint8)) {
arg1 := randomFp()
arg2 := randomFp()
arg1cpy := arg1
f(&arg1, &arg2, 0)
if !eqFp(&arg1, &arg1cpy) {
t.Error("cswap swapped")
}
arg1cpy = arg1
f(&arg1, &arg2, 1)
if eqFp(&arg1, &arg1cpy) {
t.Error("cswap didn't swapped")
}
arg1cpy = arg1
f(&arg1, &arg2, 0xF2)
if eqFp(&arg1, &arg1cpy) {
t.Error("cswap didn't swapped")
}
}
func TestCswap(t *testing.T) {
testCswap(t, cswap512Generic)
testCswap(t, cswap512)
}
func TestSubRdc(t *testing.T) {
var res fp
// 1 - 1 mod P
tmp := oneFp512
subRdc(&res, &tmp, &tmp)
if !eqFp(&res, &zeroFp512) {
t.Errorf("Wrong value\n%X", res)
}
zero(&res)
// 0 - 1 mod P
exp := p
exp[0]--
subRdc(&res, &zeroFp512, &oneFp512)
if !eqFp(&res, &exp) {
t.Errorf("Wrong value\n%X\n%X", res, exp)
}
zero(&res)
// P - (P-1)
pMinusOne := p
pMinusOne[0]--
subRdc(&res, &p, &pMinusOne)
if !eqFp(&res, &oneFp512) {
t.Errorf("Wrong value\n[%X != %X]", res, oneFp512)
}
zero(&res)
subRdc(&res, &p, &oneFp512)
if !eqFp(&res, &pMinusOne) {
t.Errorf("Wrong value\n[%X != %X]", res, pMinusOne)
}
}
func testMulRdc(t *testing.T, f func(*fp, *fp, *fp)) {
var res fp
m1 := fp{
0x85E2579C786882D0, 0x4E3433657E18DA95,
0x850AE5507965A0B3, 0xA15BC4E676475964,
}
m2 := fp{
0x85E2579C786882CF, 0x4E3433657E18DA95,
0x850AE5507965A0B3, 0xA15BC4E676475964,
}
// Expected
m1m1 := fp{
0xAEBF46E92C88A4B4, 0xCFE857977B946347,
0xD3B264FF08493901, 0x6EEB3D23746B6C7C,
0xC0CA874A349D64B4, 0x7AD4A38B406F8504,
0x38B6B6CEB82472FB, 0x1587015FD7DDFC7D,
}
m1m2 := fp{
0x51534771258C4624, 0x2BFEDE86504E2160,
0xE8127D5E9329670B, 0x0C84DBD584491D75,
0x656C73C68B16E38C, 0x01C0DA470B30B8DE,
0x2532E3903EAA950B, 0x3F2C28EA97FE6FEC,
}
// 0*0
tmp := zeroFp512
f(&res, &tmp, &tmp)
if !eqFp(&res, &tmp) {
t.Errorf("Wrong value\n%X", res)
}
// 1*m1 == m1
zero(&res)
f(&res, &m1, &one)
if !eqFp(&res, &m1) {
t.Errorf("Wrong value\n%X", res)
}
// m1*m2 < p
zero(&res)
f(&res, &m1, &m2)
if !eqFp(&res, &m1m2) {
t.Errorf("Wrong value\n%X", res)
}
// m1*m1 > p
zero(&res)
f(&res, &m1, &m1)
if !eqFp(&res, &m1m1) {
t.Errorf("Wrong value\n%X", res)
}
}
func TestMulRdc(t *testing.T) {
testMulRdc(t, mulRdcGeneric)
testMulRdc(t, mulRdc)
}
func TestModExp(t *testing.T) {
var resExp, base, exp big.Int
var baseFp, expFp, resFp, resFpExp fp
for i := 0; i < numIter; i++ {
// Perform modexp with reference implementation
// in Montgomery domain
base.SetString(fp2S(randomFp()), 16)
exp.SetString(fp2S(randomFp()), 16)
resExp.Exp(&base, &exp, modulus)
toMont(&base, true)
toMont(&resExp, true)
// Convert to fp
copy(baseFp[:], intGetU64(&base))
copy(expFp[:], intGetU64(&exp))
copy(resFpExp[:], intGetU64(&resExp))
// Perform modexp with our implementation
modExpRdc512(&resFp, &baseFp, &expFp)
if !eqFp(&resFp, &resFpExp) {
test.ReportError(t, resFp, intGetU64(&resExp), base, exp)
}
}
}
// Test uses Euler's Criterion.
func TestIsNonQuadRes(t *testing.T) {
var n, nMont big.Int
var pm1o2, rawP big.Int
var nMontFp fp
// (p-1)/2
pm1o2.SetString("0x32da4747ba07c4dffe455868af1f26255a16841d76e446212d7dfe63499164e6d3d56362b3f9aa83a8b398660f85a792e1390dfa2bd6541a8dc0dc8299e3643d", 0)
// modulus value (not in montgomery)
rawP.SetString("0x65b48e8f740f89bffc8ab0d15e3e4c4ab42d083aedc88c425afbfcc69322c9cda7aac6c567f35507516730cc1f0b4f25c2721bf457aca8351b81b90533c6c87b", 0)
// There is 641 quadratic residues in this range
for i := uint64(1); i < uint64(numIter); i++ {
n.SetUint64(i)
n.Exp(&n, &pm1o2, &rawP)
// exp == 1 iff n is quadratic non-residue
exp := n.Cmp(big.NewInt(1))
if exp < 0 {
panic("Should never happen")
}
nMont.SetUint64(i)
toMont(&nMont, true)
copy(nMontFp[:], intGetU64(&nMont))
ret := nMontFp.isNonQuadRes()
if ret != exp {
toMont(&nMont, false)
t.Errorf("Test failed for value %s", nMont.Text(10))
}
}
}
func TestCheckSmaller(t *testing.T) {
// p-1
pMin1 := p
pMin1[0]--
// p-1 < p => 1
if !isLess(&pMin1, &p) {
t.Error("pMin1>p")
}
// p < p-1 => 0
if isLess(&p, &pMin1) {
t.Error("p>pMin1")
}
// p == p => 0
if isLess(&p, &p) {
t.Error("p==p")
}
}
func BenchmarkFp(b *testing.B) {
var res, arg1 fp
var two64 big.Int
two64.SetUint64(1).Lsh(&two64, 64)
n, _ := rand.Int(rand.Reader, &two64)
u64 := n.Uint64()
arg2, arg3 := randomFp(), randomFp()
b.Run("sub512", func(b *testing.B) {
for n := 0; n < b.N; n++ {
sub512(&arg1, &arg2, &arg3)
}
})
b.Run("mul", func(b *testing.B) {
for n := 0; n < b.N; n++ {
mul512(&arg2, &arg3, u64)
}
})
b.Run("cswap", func(b *testing.B) {
for n := 0; n < b.N; n++ {
cswap512(&arg1, &arg2, uint8(n%2))
}
})
b.Run("add", func(b *testing.B) {
for n := 0; n < b.N; n++ {
addRdc(&res, &arg1, &arg2)
}
})
b.Run("sub", func(b *testing.B) {
for n := 0; n < b.N; n++ {
subRdc(&res, &arg1, &arg2)
}
})
b.Run("mul", func(b *testing.B) {
for n := 0; n < b.N; n++ {
mulRdc(&res, &arg1, &arg2)
}
})
b.Run("exp", func(b *testing.B) {
for n := 0; n < b.N; n++ {
modExpRdc512(&res, &arg1, &arg2)
}
})
}