/
extra.v
352 lines (313 loc) · 9.99 KB
/
extra.v
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
module edwards25519
// extended_coordinates returns v in extended coordinates (X:Y:Z:T) where
// x = X/Z, y = Y/Z, and xy = T/Z as in https://eprint.iacr.org/2008/522.
fn (mut v Point) extended_coordinates() (Element, Element, Element, Element) {
// This function is outlined to make the allocations inline in the caller
// rather than happen on the heap. Don't change the style without making
// sure it doesn't increase the inliner cost.
mut e := []Element{len: 4}
x, y, z, t := v.extended_coordinates_generic(mut e)
return x, y, z, t
}
fn (mut v Point) extended_coordinates_generic(mut e []Element) (Element, Element, Element, Element) {
check_initialized(v)
x := e[0].set(v.x)
y := e[1].set(v.y)
z := e[2].set(v.z)
t := e[3].set(v.t)
return x, y, z, t
}
// Given k > 0, set s = s**(2*i).
fn (mut s Scalar) pow2k(k int) {
for i := 0; i < k; i++ {
s.multiply(s, s)
}
}
// set_extended_coordinates sets v = (X:Y:Z:T) in extended coordinates where
// x = X/Z, y = Y/Z, and xy = T/Z as in https://eprint.iacr.org/2008/522.
//
// If the coordinates are invalid or don't represent a valid point on the curve,
// set_extended_coordinates returns an error and the receiver is
// unchanged. Otherwise, set_extended_coordinates returns v.
fn (mut v Point) set_extended_coordinates(x Element, y Element, z Element, t Element) !Point {
if !is_on_curve(x, y, z, t) {
return error('edwards25519: invalid point coordinates')
}
v.x.set(x)
v.y.set(y)
v.z.set(z)
v.t.set(t)
return v
}
fn is_on_curve(x Element, y Element, z Element, t Element) bool {
mut lhs := Element{}
mut rhs := Element{}
mut xx := Element{}
xx.square(x)
mut yy := Element{}
yy.square(y)
mut zz := Element{}
zz.square(z)
// zz := new(Element).Square(Z)
mut tt := Element{}
tt.square(t)
// tt := new(Element).Square(T)
// -x² + y² = 1 + dx²y²
// -(X/Z)² + (Y/Z)² = 1 + d(T/Z)²
// -X² + Y² = Z² + dT²
lhs.subtract(yy, xx)
mut d := d_const
rhs.multiply(d, tt)
rhs.add(rhs, zz)
if lhs.equal(rhs) != 1 {
return false
}
// xy = T/Z
// XY/Z² = T/Z
// XY = TZ
lhs.multiply(x, y)
rhs.multiply(t, z)
return lhs.equal(rhs) == 1
}
// bytes_montgomery converts v to a point on the birationally-equivalent
// Curve25519 Montgomery curve, and returns its canonical 32 bytes encoding
// according to RFC 7748.
//
// Note that bytes_montgomery only encodes the u-coordinate, so v and -v encode
// to the same value. If v is the identity point, bytes_montgomery returns 32
// zero bytes, analogously to the X25519 function.
pub fn (mut v Point) bytes_montgomery() []u8 {
// This function is outlined to make the allocations inline in the caller
// rather than happen on the heap.
mut buf := [32]u8{}
return v.bytes_montgomery_generic(mut buf)
}
fn (mut v Point) bytes_montgomery_generic(mut buf [32]u8) []u8 {
check_initialized(v)
// RFC 7748, Section 4.1 provides the bilinear map to calculate the
// Montgomery u-coordinate
//
// u = (1 + y) / (1 - y)
//
// where y = Y / Z.
mut y := Element{}
mut recip := Element{}
mut u := Element{}
y.multiply(v.y, y.invert(v.z)) // y = Y / Z
recip.invert(recip.subtract(fe_one, &y)) // r = 1/(1 - y)
u.multiply(u.add(fe_one, y), recip) // u = (1 + y)*r
return copy_field_element(mut buf, mut u)
}
// mult_by_cofactor sets v = 8 * p, and returns v.
pub fn (mut v Point) mult_by_cofactor(p Point) Point {
check_initialized(p)
mut result := ProjectiveP1{}
mut pp := ProjectiveP2{}
pp.from_p3(p)
result.double(pp)
pp.from_p1(result)
result.double(pp)
pp.from_p1(result)
result.double(pp)
return v.from_p1(result)
}
// invert sets s to the inverse of a nonzero scalar v, and returns s.
//
// If t is zero, invert returns zero.
pub fn (mut s Scalar) invert(t Scalar) Scalar {
// Uses a hardcoded sliding window of width 4.
mut table := [8]Scalar{}
mut tt := Scalar{}
tt.multiply(t, t)
table[0] = t
for i := 0; i < 7; i++ {
table[i + 1].multiply(table[i], tt)
}
// Now table = [t**1, t**3, t**7, t**11, t**13, t**15]
// so t**k = t[k/2] for odd k
// To compute the sliding window digits, use the following Sage script:
// sage: import itertools
// sage: def sliding_window(w,k):
// ....: digits = []
// ....: while k > 0:
// ....: if k % 2 == 1:
// ....: kmod = k % (2**w)
// ....: digits.append(kmod)
// ....: k = k - kmod
// ....: else:
// ....: digits.append(0)
// ....: k = k // 2
// ....: return digits
// Now we can compute s roughly as follows:
// sage: s = 1
// sage: for coeff in reversed(sliding_window(4,l-2)):
// ....: s = s*s
// ....: if coeff > 0 :
// ....: s = s*t**coeff
// This works on one bit at a time, with many runs of zeros.
// The digits can be collapsed into [(count, coeff)] as follows:
// sage: [(len(list(group)),d) for d,group in itertools.groupby(sliding_window(4,l-2))]
// Entries of the form (k, 0) turn into pow2k(k)
// Entries of the form (1, coeff) turn into a squaring and then a table lookup.
// We can fold the squaring into the previous pow2k(k) as pow2k(k+1).
s = table[1 / 2]
s.pow2k(127 + 1)
s.multiply(s, table[1 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[9 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[11 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[13 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[15 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[7 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[15 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[5 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[1 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[15 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[15 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[7 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[3 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[11 / 2])
s.pow2k(5 + 1)
s.multiply(s, table[11 / 2])
s.pow2k(9 + 1)
s.multiply(s, table[9 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[3 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[3 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[3 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[9 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[7 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[3 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[13 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[7 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[9 / 2])
s.pow2k(3 + 1)
s.multiply(s, table[15 / 2])
s.pow2k(4 + 1)
s.multiply(s, table[11 / 2])
return s
}
// multi_scalar_mult sets v = sum(scalars[i] * points[i]), and returns v.
//
// Execution time depends only on the lengths of the two slices, which must match.
pub fn (mut v Point) multi_scalar_mult(scalars []Scalar, points []Point) Point {
if scalars.len != points.len {
panic('edwards25519: called multi_scalar_mult with different size inputs')
}
check_initialized(...points)
mut sc := scalars.clone()
// Proceed as in the single-base case, but share doublings
// between each point in the multiscalar equation.
// Build lookup tables for each point
mut tables := []ProjLookupTable{len: points.len}
for i, _ in tables {
tables[i].from_p3(points[i])
}
// Compute signed radix-16 digits for each scalar
// digits := make([][64]int8, len(scalars))
mut digits := [][]i8{len: sc.len, init: []i8{len: 64, cap: 64}}
for j, _ in digits {
digits[j] = sc[j].signed_radix16()
}
// Unwrap first loop iteration to save computing 16*identity
mut multiple := ProjectiveCached{}
mut tmp1 := ProjectiveP1{}
mut tmp2 := ProjectiveP2{}
// Lookup-and-add the appropriate multiple of each input point
for k, _ in tables {
tables[k].select_into(mut multiple, digits[k][63])
tmp1.add(v, multiple) // tmp1 = v + x_(j,63)*Q in P1xP1 coords
v.from_p1(tmp1) // update v
}
tmp2.from_p3(v) // set up tmp2 = v in P2 coords for next iteration
for r := 62; r >= 0; r-- {
tmp1.double(tmp2) // tmp1 = 2*(prev) in P1xP1 coords
tmp2.from_p1(tmp1) // tmp2 = 2*(prev) in P2 coords
tmp1.double(tmp2) // tmp1 = 4*(prev) in P1xP1 coords
tmp2.from_p1(tmp1) // tmp2 = 4*(prev) in P2 coords
tmp1.double(tmp2) // tmp1 = 8*(prev) in P1xP1 coords
tmp2.from_p1(tmp1) // tmp2 = 8*(prev) in P2 coords
tmp1.double(tmp2) // tmp1 = 16*(prev) in P1xP1 coords
v.from_p1(tmp1) // v = 16*(prev) in P3 coords
// Lookup-and-add the appropriate multiple of each input point
for s, _ in tables {
tables[s].select_into(mut multiple, digits[s][r])
tmp1.add(v, multiple) // tmp1 = v + x_(j,i)*Q in P1xP1 coords
v.from_p1(tmp1) // update v
}
tmp2.from_p3(v) // set up tmp2 = v in P2 coords for next iteration
}
return v
}
// vartime_multiscalar_mult sets v = sum(scalars[i] * points[i]), and returns v.
//
// Execution time depends on the inputs.
pub fn (mut v Point) vartime_multiscalar_mult(scalars []Scalar, points []Point) Point {
if scalars.len != points.len {
panic('edwards25519: called VarTimeMultiScalarMult with different size inputs')
}
check_initialized(...points)
// Generalize double-base NAF computation to arbitrary sizes.
// Here all the points are dynamic, so we only use the smaller
// tables.
// Build lookup tables for each point
mut tables := []NafLookupTable5{len: points.len}
for i, _ in tables {
tables[i].from_p3(points[i])
}
mut sc := scalars.clone()
// Compute a NAF for each scalar
// mut nafs := make([][256]int8, len(scalars))
mut nafs := [][]i8{len: sc.len, init: []i8{len: 256, cap: 256}}
for j, _ in nafs {
nafs[j] = sc[j].non_adjacent_form(5)
}
mut multiple := ProjectiveCached{}
mut tmp1 := ProjectiveP1{}
mut tmp2 := ProjectiveP2{}
tmp2.zero()
// Move from high to low bits, doubling the accumulator
// at each iteration and checking whether there is a nonzero
// coefficient to look up a multiple of.
//
// Skip trying to find the first nonzero coefficient, because
// searching might be more work than a few extra doublings.
// k == i, l == j
for k := 255; k >= 0; k-- {
tmp1.double(tmp2)
for l, _ in nafs {
if nafs[l][k] > 0 {
v.from_p1(tmp1)
tables[l].select_into(mut multiple, nafs[l][k])
tmp1.add(v, multiple)
} else if nafs[l][k] < 0 {
v.from_p1(tmp1)
tables[l].select_into(mut multiple, -nafs[l][k])
tmp1.sub(v, multiple)
}
}
tmp2.from_p1(tmp1)
}
v.from_p2(tmp2)
return v
}