v
Зеркало из https://github.com/vlang/v
1module edwards25519
2
3import os
4import rand
5import math.bits
6import math.big
7import encoding.hex
8
9const github_job = os.getenv('GITHUB_JOB')
10
11fn testsuite_begin() {
12if github_job != '' {
13// ensure that the CI does not run flaky tests:
14rand.seed([u32(0xffff24), 0xabcd])
15}
16}
17
18fn (mut v Element) str() string {
19return hex.encode(v.bytes())
20}
21
22const mask_low_52_bits = (u64(1) << 52) - 1
23
24fn generate_field_element() Element {
25return Element{
26l0: rand.u64() & mask_low_52_bits
27l1: rand.u64() & mask_low_52_bits
28l2: rand.u64() & mask_low_52_bits
29l3: rand.u64() & mask_low_52_bits
30l4: rand.u64() & mask_low_52_bits
31}
32}
33
34// weirdLimbs can be combined to generate a range of edge-case edwards25519 elements.
35// 0 and -1 are intentionally more weighted, as they combine well.
36const two_to_51 = u64(1) << 51
37const two_to_52 = u64(1) << 52
38const weird_limbs_51 = [
39u64(0),
400,
410,
420,
431,
4419 - 1,
4519,
460x2aaaaaaaaaaaa,
470x5555555555555,
48two_to_51 - 20,
49two_to_51 - 19,
50two_to_51 - 1,
51two_to_51 - 1,
52two_to_51 - 1,
53two_to_51 - 1,
54]
55const weird_limbs_52 = [
56u64(0),
570,
580,
590,
600,
610,
621,
6319 - 1,
6419,
650x2aaaaaaaaaaaa,
660x5555555555555,
67two_to_51 - 20,
68two_to_51 - 19,
69two_to_51 - 1,
70two_to_51 - 1,
71two_to_51 - 1,
72two_to_51 - 1,
73two_to_51 - 1,
74two_to_51 - 1,
75two_to_51,
76two_to_51 + 1,
77two_to_52 - 19,
78two_to_52 - 1,
79]
80
81fn generate_weird_field_element() Element {
82return Element{
83l0: weird_limbs_52[rand.intn(weird_limbs_52.len) or { 0 }]
84l1: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
85l2: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
86l3: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
87l4: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
88}
89}
90
91fn (e Element) generate_element() Element {
92if rand.intn(2) or { 0 } == 0 {
93return generate_weird_field_element()
94}
95return generate_field_element()
96}
97
98fn is_in_bounds(x Element) bool {
99return bits.len_64(x.l0) <= 52 && bits.len_64(x.l1) <= 52 && bits.len_64(x.l2) <= 52
100&& bits.len_64(x.l3) <= 52 && bits.len_64(x.l4) <= 52
101}
102
103fn carry_gen(a [5]u64) bool {
104mut t1 := Element{a[0], a[1], a[2], a[3], a[4]}
105mut t2 := Element{a[0], a[1], a[2], a[3], a[4]}
106
107t1.carry_propagate_generic()
108t2.carry_propagate_generic()
109
110return t1 == t2 && is_in_bounds(t2)
111}
112
113fn test_carry_propagate_generic() {
114// closures not supported on windows
115for i := 0; i <= 10; i++ {
116els := [rand.u64(), rand.u64(), rand.u64(), rand.u64(),
117rand.u64()]!
118p := carry_gen(els)
119assert p == true
120}
121res := carry_gen([u64(0xffffffffffffffff), 0xffffffffffffffff, 0xffffffffffffffff,
1220xffffffffffffffff, 0xffffffffffffffff]!)
123assert res == true
124}
125
126fn test_fe_mul_generic() {
127for i in 0 .. 20 {
128el := Element{}
129a := el.generate_element()
130b := el.generate_element()
131a1 := a
132a2 := a
133
134b1 := b
135b2 := b
136
137a1b1 := fe_mul_generic(a1, b1)
138a2b2 := fe_mul_generic(a2, b2)
139assert a1b1 == a2b2 && is_in_bounds(a1b1) && is_in_bounds(a2b2)
140}
141}
142
143fn test_fe_square_generic() {
144for i in 0 .. 20 {
145a := generate_field_element()
146
147a1 := a
148a2 := a
149
150a11 := fe_square_generic(a1)
151a22 := fe_square_generic(a2)
152assert a11 == a22 && is_in_bounds(a11) && is_in_bounds(a22)
153}
154}
155
156struct SqrtRatioTest {
157u string
158v string
159was_square int
160r string
161}
162
163fn test_sqrt_ratio() {
164// From draft-irtf-cfrg-ristretto255-decaf448-00, Appendix A.4.
165
166tests := [
167// If u is 0, the function is defined to return (0, TRUE), even if v
168// is zero. Note that where used in this package, the denominator v
169// is never zero.
170SqrtRatioTest{'0000000000000000000000000000000000000000000000000000000000000000', '0000000000000000000000000000000000000000000000000000000000000000', 1, '0000000000000000000000000000000000000000000000000000000000000000'},
171// 0/1 == 0²
172SqrtRatioTest{'0000000000000000000000000000000000000000000000000000000000000000', '0100000000000000000000000000000000000000000000000000000000000000', 1, '0000000000000000000000000000000000000000000000000000000000000000'},
173// If u is non-zero and v is zero, defined to return (0, FALSE).
174SqrtRatioTest{'0100000000000000000000000000000000000000000000000000000000000000', '0000000000000000000000000000000000000000000000000000000000000000', 0, '0000000000000000000000000000000000000000000000000000000000000000'},
175// 2/1 is not square in this edwards25519.
176SqrtRatioTest{'0200000000000000000000000000000000000000000000000000000000000000', '0100000000000000000000000000000000000000000000000000000000000000', 0, '3c5ff1b5d8e4113b871bd052f9e7bcd0582804c266ffb2d4f4203eb07fdb7c54'},
177// 4/1 == 2²
178SqrtRatioTest{'0400000000000000000000000000000000000000000000000000000000000000', '0100000000000000000000000000000000000000000000000000000000000000', 1, '0200000000000000000000000000000000000000000000000000000000000000'},
179// 1/4 == (2⁻¹)² == (2^(p-2))² per Euler's theorem
180SqrtRatioTest{'0100000000000000000000000000000000000000000000000000000000000000', '0400000000000000000000000000000000000000000000000000000000000000', 1, 'f6ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3f'},
181]
182
183for i, tt in tests {
184mut elu := Element{}
185mut elv := Element{}
186mut elw := Element{}
187mut elg := Element{}
188
189u := elu.set_bytes(hex.decode(tt.u)!)!
190v := elv.set_bytes(hex.decode(tt.v)!)!
191want := elw.set_bytes(hex.decode(tt.r)!)!
192mut got, was_square := elg.sqrt_ratio(u, v)
193
194assert got.equal(want) != 0
195assert was_square == tt.was_square
196// if got.Equal(want) == 0 || wasSquare != tt.wasSquare {
197// t.Errorf("%d: got (%v, %v), want (%v, %v)", i, got, wasSquare, want, tt.wasSquare)
198// }
199}
200}
201
202fn test_set_bytes_normal() {
203for i in 0 .. 15 {
204mut el := Element{}
205mut random_inp := rand.bytes(32)!
206
207el = el.set_bytes(random_inp.clone())!
208random_inp[random_inp.len - 1] &= (1 << 7) - 1
209// assert f1(random_inp, el) == true
210
211assert random_inp == el.bytes()
212assert is_in_bounds(el) == true
213}
214}
215
216fn test_set_bytes_reduced() {
217mut fe := Element{}
218mut r := Element{}
219mut random_inp := rand.bytes(32) or { return }
220
221fe.set_bytes(random_inp) or { return }
222r.set_bytes(fe.bytes()) or { return }
223
224assert fe == r
225}
226
227// Check some fixed vectors from dalek
228struct FeRTTest {
229mut:
230fe Element
231b []u8
232}
233
234fn test_set_bytes_from_dalek_test_vectors() {
235mut tests := [
236FeRTTest{
237fe: Element{358744748052810, 1691584618240980, 977650209285361, 1429865912637724, 560044844278676}
238b: [u8(74), 209, 69, 197, 70, 70, 161, 222, 56, 226, 229, 19, 112, 60, 25, 92, 187,
23974, 222, 56, 50, 153, 51, 233, 40, 74, 57, 6, 160, 185, 213, 31]
240},
241FeRTTest{
242fe: Element{84926274344903, 473620666599931, 365590438845504, 1028470286882429, 2146499180330972}
243b: [u8(199), 23, 106, 112, 61, 77, 216, 79, 186, 60, 11, 118, 13, 16, 103, 15, 42,
24432, 83, 250, 44, 57, 204, 198, 78, 199, 253, 119, 146, 172, 3, 122]
245},
246]
247for _, mut tt in tests {
248b := tt.fe.bytes()
249mut el := Element{}
250mut fe := el.set_bytes(tt.b)!
251
252assert b == tt.b
253assert fe.equal(tt.fe) == 1
254}
255}
256
257fn test_equal() {
258mut x := Element{1, 1, 1, 1, 1}
259y := Element{5, 4, 3, 2, 1}
260
261mut eq1 := x.equal(x)
262assert eq1 == 1
263
264eq1 = x.equal(y)
265assert eq1 == 0
266}
267
268fn test_invert() {
269mut x := Element{1, 1, 1, 1, 1}
270mut one := Element{1, 0, 0, 0, 0}
271mut xinv := Element{}
272mut r := Element{}
273
274xinv.invert(x)
275r.multiply(x, xinv)
276r.reduce()
277
278assert one == r
279bytes := rand.bytes(32)!
280
281x.set_bytes(bytes)!
282
283xinv.invert(x)
284r.multiply(x, xinv)
285r.reduce()
286
287assert one == r
288
289zero := Element{}
290x.set(zero)
291
292xx := xinv.invert(x)
293assert xx == xinv
294assert xinv.equal(zero) == 1
295// s := if num % 2 == 0 { 'even' } else { 'odd' }
296}
297
298fn test_mult_32() {
299for j in 0 .. 10 {
300mut x := Element{}
301mut t1 := Element{}
302y := u32(0)
303for i := 0; i < 100; i++ {
304t1.mult_32(x, y)
305}
306mut ty := Element{}
307ty.l0 = u64(y)
308mut t2 := Element{}
309for i := 0; i < 100; i++ {
310t2.multiply(x, ty)
311}
312assert t1.equal(t2) == 1 && is_in_bounds(t1) && is_in_bounds(t2)
313}
314}
315
316fn test_selected_and_swap() {
317a := Element{358744748052810, 1691584618240980, 977650209285361, 1429865912637724, 560044844278676}
318b := Element{84926274344903, 473620666599931, 365590438845504, 1028470286882429, 2146499180330972}
319
320mut c := Element{}
321mut d := Element{}
322
323c.selected(a, b, 1)
324d.selected(a, b, 0)
325
326assert c.equal(a) == 1
327assert d.equal(b) == 1
328
329c.swap(mut d, 0)
330assert c.equal(a) == 1
331assert d.equal(b) == 1
332
333c.swap(mut d, 1)
334assert c.equal(b) == 1
335assert d.equal(a) == 1
336}
337
338// Tests self-consistency between multiply and Square.
339fn test_consistency_between_mult_and_square() {
340mut x := Element{1, 1, 1, 1, 1}
341mut x2 := Element{}
342mut x2sq := Element{}
343
344x2.multiply(x, x)
345x2sq.square(x)
346
347assert x2 == x2sq
348
349bytes := rand.bytes(32) or { return }
350x.set_bytes(bytes) or { return }
351x2.multiply(x, x)
352x2sq.square(x)
353
354assert x2 == x2sq
355}
356
357// to_big_integer returns v as a big.Integer.
358fn (mut v Element) to_big_integer() big.Integer {
359buf := v.bytes()
360return big.integer_from_bytes(buf)
361}
362
363// from_big_integer sets v = n, and returns v. The bit length of n must not exceed 256.
364fn (mut v Element) from_big_integer(n big.Integer) !Element {
365if n.bin_str().len > 32 * 8 {
366return error('invalid edwards25519 element input size')
367}
368mut bytes, _ := n.bytes()
369swap_endianness(mut bytes) // SHOULD I SWAP IT?
370v.set_bytes(bytes)!
371
372return v
373}
374
375fn (mut v Element) from_decimal_string(s string) !Element {
376num := big.integer_from_string(s)!
377
378v = v.from_big_integer(num)!
379return v
380}
381
382fn test_bytes_big_equivalence() {
383mut inp := rand.bytes(32)!
384el := Element{}
385mut fe := el.generate_element()
386mut fe1 := el.generate_element()
387
388fe.set_bytes(inp) or { panic(err) }
389inp[inp.len - 1] &= (1 << 7) - 1 // mask the most significant bit
390
391mut b := big.integer_from_bytes(swap_endianness(mut inp)) // need swap_endianness
392fe1.from_big_integer(b) or { panic(err) } // do swap_endianness internally
393
394assert fe == fe1
395
396mut buf := []u8{len: 32} // pad with zeroes
397fedtobig := fe1.to_big_integer()
398mut fedbig_bytes, _ := fedtobig.bytes()
399copy(mut buf, fedbig_bytes) // does not need to do swap_endianness
400
401assert fe.bytes() == buf && is_in_bounds(fe) && is_in_bounds(fe1)
402// assert big_equivalence(inp, fe, fe1) == true
403}
404
405fn test_decimal_constants() {
406sqrtm1string := '19681161376707505956807079304988542015446066515923890162744021073123829784752'
407mut el := Element{}
408mut exp := el.from_decimal_string(sqrtm1string)!
409
410assert sqrt_m1.equal(exp) == 1
411
412dstring := '37095705934669439343138083508754565189542113879843219016388785533085940283555'
413exp = el.from_decimal_string(dstring)!
414mut d := d_const
415
416assert d.equal(exp) == 1
417}
418
419fn test_mul_64_to_128() {
420mut a := u64(5)
421mut b := u64(5)
422mut r := mul_64(a, b)
423
424assert r.lo == 0x19
425assert r.hi == 0
426
427a = u64(18014398509481983) // 2^54 - 1
428b = u64(18014398509481983) // 2^54 - 1
429r = mul_64(a, b)
430
431assert r.lo == 0xff80000000000001 && r.hi == 0xfffffffffff
432
433a = u64(1125899906842661)
434b = u64(2097155)
435r = mul_64(a, b)
436r = add_mul_64(r, a, b)
437r = add_mul_64(r, a, b)
438r = add_mul_64(r, a, b)
439r = add_mul_64(r, a, b)
440
441assert r.lo == 16888498990613035 && r.hi == 640
442}
443
444fn test_multiply_distributes_over_add() {
445for i in 0 .. 10 {
446el := Element{}
447x := el.generate_element()
448y := el.generate_element()
449z := el.generate_element()
450mut t1 := Element{}
451t1.add(x, y)
452t1.multiply(t1, z)
453
454// Compute t2 = x*z + y*z
455mut t2 := Element{}
456mut t3 := Element{}
457t2.multiply(x, z)
458t3.multiply(y, z)
459t2.add(t2, t3)
460assert t1.equal(t2) == 1 && is_in_bounds(t1) && is_in_bounds(t2)
461}
462}
463