Debian Codegraph @

src/test/make_tests_mul_25519.py

raw symbols SCIP
1 """Make unit test for mul_25519() in x25519.c"""
2
3 from common import counter, make_main, split64, bin2int
4 from hashlib import sha256
5 import struct
6
7
8 def make_test(f, g):
9
10 assert(len(f) == 10)
11 assert(len(g) == 10)
12 for i in range(10):
13 assert(f[i] < 2**27)
14 assert(g[i] < 2**27)
15 fx = ["0x%08X" % x for x in f]
16 gx = ["0x%08X" % x for x in g]
17 max26 = hex(2**26 - 1)
18 max25 = hex(2**25 - 1)
19
20 modulus = 2**255 - 19
21 base = [0, 26, 51, 77, 102, 128, 153, 179, 204, 230]
22 fv = 0
23 gv = 0
24 for i in range(10):
25 fv += f[i] * (2**(base[i]))
26 gv += g[i] * (2**(base[i]))
27
28 canonical = (fv * gv) % modulus
29 results = [canonical, canonical + modulus]
30 if canonical < 38:
31 results.append(canonical + modulus * 2)
32
33 # Turn results[] into arrays of 64-bit words
34 results_hex = []
35 for result in results:
36 words = split64(result)
37 words = words + ["0"] * (4 - len(words))
38 results_hex.append(words)
39
40 print("")
41 print("void test_%d(void) {" % next(counter))
42 print(" const uint32_t f[10] = {" + ", ".join(fx) + "};")
43 print(" const uint32_t g[10] = {" + ", ".join(gx) + "};")
44 print(" uint32_t out[10];")
45 print(" uint64_t out64[4];")
46 print(" uint64_t exp[%d][4] = {" % len(results))
47 print(" { " + ",".join(results_hex[0]) + " },")
48 print(" { " + ",".join(results_hex[1]) + " }")
49 if len(results_hex) == 3:
50 print(" ,{ " + ",".join(results_hex[2]) + " }")
51 print(" };")
52 print(" unsigned match;")
53 print("")
54 print(" mul_25519(out, f, g);")
55 print(" assert(out[0] <= " + max26 + ");")
56 print(" assert(out[1] <= " + max25 + ");")
57 print(" assert(out[2] <= " + max26 + ");")
58 print(" assert(out[3] <= " + max25 + ");")
59 print(" assert(out[4] <= " + max26 + ");")
60 print(" assert(out[5] <= " + max25 + ");")
61 print(" assert(out[6] <= " + max26 + ");")
62 print(" assert(out[7] <= " + max25 + ");")
63 print(" assert(out[8] <= " + max26 + ");")
64 print(" assert(out[9] <= " + max26 + ");")
65 print(" convert_le25p5_to_le64(out64, out);")
66 print(" match = 0;")
67 print(" match |= !memcmp(exp[0], out64, 32);")
68 print(" match |= !memcmp(exp[1], out64, 32);")
69 if len(results_hex) == 3:
70 print(" match |= !memcmp(exp[2], out64, 32);")
71 print(" assert(match);")
72 print("}")
73
74
75 def make_limb(seed):
76 result = bin2int(sha256(struct.pack(">I", seed)).digest()) & ((2**27)-1)
77 return result
78
79
80 print("#include <assert.h>")
81 print("#include <string.h>")
82 print("#include <stdint.h>")
83 print("#include <stdio.h>")
84 print("void convert_le25p5_to_le64(uint64_t out[4], const uint32_t in[10]);")
85 print("void mul_25519(uint32_t out[10], const uint32_t f[10], const uint32_t g[10]);")
86
87 modulus = [0x3ffffed, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff]
88 modulus_m1 = [0x3ffffec, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff]
89 modulus_m2 = [0x3ffffeb, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff]
90 modulus_m40 = [0x3ffffc5, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff]
91
92 make_test([0]*10, [0]*10)
93 make_test([1] + [0]*9, [1] + [0]*9)
94 make_test([30] + [0]*9, [30] + [0]*9)
95 make_test([0x7ffffed] + [0]*9, [0x7ffffed] + [0]*9)
96 make_test(modulus, modulus)
97 make_test(modulus_m1, modulus_m1)
98 make_test(modulus_m2, modulus_m2)
99 make_test(modulus, modulus_m2)
100 make_test(modulus_m40, modulus_m40)
101
102 for x in range(100):
103 f = [make_limb(1000*x + y) for y in range(10)]
104 g = [make_limb(2000*x + y) for y in range(10)]
105 make_test(f, g)
106
107 make_main()