Debian Codegraph @

src/make_ecc_table.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 import argparse
4
5 declaration = """\
6 /* This file was automatically generated, do not edit */
7 #include "common.h"
8 extern const unsigned {0}_n_tables;
9 extern const unsigned {0}_window_size;
10 extern const unsigned {0}_points_per_table;
11 extern const uint64_t {0}_tables[{1}][{2}][2][{3}];
12 """
13
14 definition = """\
15 /* This file was automatically generated, do not edit */
16 #include "common.h"
17 const unsigned {0}_n_tables = {1};
18 const unsigned {0}_window_size = {2};
19 const unsigned {0}_points_per_table = {3};
20 /* {4} */
21 /* Table size: {5} kbytes */
22 const uint64_t {0}_tables[{1}][{3}][2][{6}] = {{\
23 """
24
25 point = """\
26 {{ /* Point #{0} */
27 {{ {1} }},
28 {{ {2} }}
29 }}{3}\
30 """
31
32 parser = argparse.ArgumentParser()
33 parser.add_argument("curve")
34 parser.add_argument("window_size", type=int)
35 parser.add_argument("basename")
36 args = parser.parse_args()
37
38 if args.curve == "p256":
39 bits = 256
40 p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff
41 Gx = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296
42 Gy = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5
43 msg = "Affine coordinates in Montgomery form"
44 elif args.curve == "p384":
45 bits = 384
46 p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff
47 Gx = 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760aB7
48 Gy = 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5F
49 msg = "Affine coordinates in Montgomery form"
50 elif args.curve == "p521":
51 bits = 521
52 p = 0x000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
53 Gx = 0x000000c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66
54 Gy = 0x0000011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650
55 msg = "Affine coordinates in plain form (not Montgomery)"
56 else:
57 raise ValueError("Unsupported curve: " + args.curve)
58
59
60 c_file = open(args.basename + ".c", "wt")
61 h_file = open(args.basename + ".h", "wt")
62
63 words = (bits + 63) // 64
64 window_size = args.window_size
65 points_per_table = 2**window_size
66 n_tables = (bits + window_size - 1) // window_size
67 byte_size = n_tables * points_per_table * 2 * (bits // 64) * (64 // 8) // 1024
68 G = Gx, Gy
69
70
71 def double(X1, Y1):
72 if X1 == 0 and Y1 == 0:
73 return (0, 0)
74
75 XX = pow(X1, 2, p)
76 w = -3 + 3 * XX
77 Y1Y1 = pow(Y1, 2, p)
78 R = 2 * Y1Y1
79 sss = 4 * Y1 * R
80 RR = pow(R, 2, p)
81 B = pow(X1 + R, 2, p) - XX - RR
82 h = pow(w, 2, p) - 2 * B
83 X3 = 2 * h * Y1 % p
84 Y3 = w * (B - h) - 2 * RR % p
85 Z3 = sss
86
87 Z3inv = pow(Z3, p - 2, p)
88 x3 = X3 * Z3inv % p
89 y3 = Y3 * Z3inv % p
90 return (x3, y3)
91
92
93 def add(X1, Y1, X2, Y2):
94 if X1 == 0 and Y1 == 0:
95 return (X2, Y2)
96 if X1 == X2 and Y1 == Y2:
97 return double(X1, Y1)
98 if X1 == X2 and (Y1 + Y2) % p == 0:
99 return (0, 0)
100
101 u = Y2 - Y1
102 uu = pow(u, 2, p)
103 v = X2 - X1
104 vv = pow(v, 2, p)
105 vvv = v * vv % p
106 R = vv * X1 % p
107 A = uu - vvv - 2 * R
108 X3 = v * A % p
109 Y3 = (u * (R - A) - vvv * Y1) % p
110 Z3 = vvv
111
112 Z3inv = pow(Z3, p - 2, p)
113 x3 = X3 * Z3inv % p
114 y3 = Y3 * Z3inv % p
115 return (x3, y3)
116
117
118 def get64(z, words):
119 """Return a C string with the number encoded into 64-bit words"""
120
121 # Convert to Montgomery form, but only if it's not P521
122 if words != 9:
123 R = 2**(words * 64)
124 x = z * R % p
125 else:
126 x = z
127
128 result = []
129 for _ in range(words):
130 masked = x & ((1 << 64) - 1)
131 result.append("0x%016XULL" % masked)
132 x >>= 64
133 return ",".join(result)
134
135
136 # Create table with points 0, G, 2G, 3G, .. (2**window_size-1)G
137 window = [(0, 0)]
138 for _ in range(points_per_table - 1):
139 new_point = add(*window[-1], *G)
140 window.append(new_point)
141
142 print(declaration.format(args.curve, n_tables, points_per_table, words), file=h_file)
143 print(definition.format(args.curve, n_tables, window_size, points_per_table, msg,
144 byte_size, words), file=c_file)
145
146 for i in range(n_tables):
147 print(" { /* Table #%u */" % i, file=c_file)
148 for j, w in enumerate(window):
149 endc = "" if (j == points_per_table - 1) else ","
150 print(point.format(j, get64(w[0], words), get64(w[1], words), endc),
151 file=c_file)
152 endc = "" if (i == n_tables - 1) else ","
153 print(" }%s" % endc, file=c_file)
154
155 # Move from G to G*2^{w}
156 for j in range(window_size):
157 G = double(*G)
158
159 # Update window
160 for j in range(1, points_per_table):
161 window[j] = add(*window[j-1], *G)
162
163 print("};", file=c_file)