v
Зеркало из https://github.com/vlang/v
1module edwards25519
2
3// extended_coordinates returns v in extended coordinates (X:Y:Z:T) where
4// x = X/Z, y = Y/Z, and xy = T/Z as in https://eprint.iacr.org/2008/522.
5fn (mut v Point) extended_coordinates() (Element, Element, Element, Element) {
6// This function is outlined to make the allocations inline in the caller
7// rather than happen on the heap. Don't change the style without making
8// sure it doesn't increase the inliner cost.
9mut e := []Element{len: 4}
10x, y, z, t := v.extended_coordinates_generic(mut e)
11return x, y, z, t
12}
13
14fn (mut v Point) extended_coordinates_generic(mut e []Element) (Element, Element, Element, Element) {
15check_initialized(v)
16x := e[0].set(v.x)
17y := e[1].set(v.y)
18z := e[2].set(v.z)
19t := e[3].set(v.t)
20return x, y, z, t
21}
22
23// Given k > 0, set s = s**(2*i).
24fn (mut s Scalar) pow2k(k int) {
25for i := 0; i < k; i++ {
26s.multiply(s, s)
27}
28}
29
30// set_extended_coordinates sets v = (X:Y:Z:T) in extended coordinates where
31// x = X/Z, y = Y/Z, and xy = T/Z as in https://eprint.iacr.org/2008/522.
32//
33// If the coordinates are invalid or don't represent a valid point on the curve,
34// set_extended_coordinates returns an error and the receiver is
35// unchanged. Otherwise, set_extended_coordinates returns v.
36fn (mut v Point) set_extended_coordinates(x Element, y Element, z Element, t Element) !Point {
37if !is_on_curve(x, y, z, t) {
38return error('edwards25519: invalid point coordinates')
39}
40v.x.set(x)
41v.y.set(y)
42v.z.set(z)
43v.t.set(t)
44return v
45}
46
47fn is_on_curve(x Element, y Element, z Element, t Element) bool {
48mut lhs := Element{}
49mut rhs := Element{}
50
51mut xx := Element{}
52xx.square(x)
53
54mut yy := Element{}
55yy.square(y)
56
57mut zz := Element{}
58zz.square(z)
59// zz := new(Element).Square(Z)
60
61mut tt := Element{}
62tt.square(t)
63// tt := new(Element).Square(T)
64// -x² + y² = 1 + dx²y²
65// -(X/Z)² + (Y/Z)² = 1 + d(T/Z)²
66// -X² + Y² = Z² + dT²
67lhs.subtract(yy, xx)
68mut d := d_const
69rhs.multiply(d, tt)
70rhs.add(rhs, zz)
71if lhs.equal(rhs) != 1 {
72return false
73}
74// xy = T/Z
75// XY/Z² = T/Z
76// XY = TZ
77lhs.multiply(x, y)
78rhs.multiply(t, z)
79return lhs.equal(rhs) == 1
80}
81
82// bytes_montgomery converts v to a point on the birationally-equivalent
83// Curve25519 Montgomery curve, and returns its canonical 32 bytes encoding
84// according to RFC 7748.
85//
86// Note that bytes_montgomery only encodes the u-coordinate, so v and -v encode
87// to the same value. If v is the identity point, bytes_montgomery returns 32
88// zero bytes, analogously to the X25519 function.
89pub fn (mut v Point) bytes_montgomery() []u8 {
90// This function is outlined to make the allocations inline in the caller
91// rather than happen on the heap.
92mut buf := [32]u8{}
93return v.bytes_montgomery_generic(mut buf)
94}
95
96fn (mut v Point) bytes_montgomery_generic(mut buf [32]u8) []u8 {
97check_initialized(v)
98
99// RFC 7748, Section 4.1 provides the bilinear map to calculate the
100// Montgomery u-coordinate
101//
102// u = (1 + y) / (1 - y)
103//
104// where y = Y / Z.
105
106mut y := Element{}
107mut recip := Element{}
108mut u := Element{}
109
110y.multiply(v.y, y.invert(v.z)) // y = Y / Z
111recip.invert(recip.subtract(fe_one, y)) // r = 1/(1 - y)
112u.multiply(u.add(fe_one, y), recip) // u = (1 + y)*r
113
114return copy_field_element(mut buf, mut u)
115}
116
117// mult_by_cofactor sets v = 8 * p, and returns v.
118pub fn (mut v Point) mult_by_cofactor(p Point) Point {
119check_initialized(p)
120mut result := ProjectiveP1{}
121mut pp := ProjectiveP2{}
122pp.from_p3(p)
123result.double(pp)
124pp.from_p1(result)
125result.double(pp)
126pp.from_p1(result)
127result.double(pp)
128return v.from_p1(result)
129}
130
131// invert sets s to the inverse of a nonzero scalar v, and returns s.
132//
133// If t is zero, invert returns zero.
134pub fn (mut s Scalar) invert(t Scalar) Scalar {
135// Uses a hardcoded sliding window of width 4.
136mut table := [8]Scalar{}
137mut tt := Scalar{}
138tt.multiply(t, t)
139table[0] = t
140for i := 0; i < 7; i++ {
141table[i + 1].multiply(table[i], tt)
142}
143// Now table = [t**1, t**3, t**7, t**11, t**13, t**15]
144// so t**k = t[k/2] for odd k
145
146// To compute the sliding window digits, use the following Sage script:
147
148// sage: import itertools
149// sage: def sliding_window(w,k):
150// ....: digits = []
151// ....: while k > 0:
152// ....: if k % 2 == 1:
153// ....: kmod = k % (2**w)
154// ....: digits.append(kmod)
155// ....: k = k - kmod
156// ....: else:
157// ....: digits.append(0)
158// ....: k = k // 2
159// ....: return digits
160
161// Now we can compute s roughly as follows:
162
163// sage: s = 1
164// sage: for coeff in reversed(sliding_window(4,l-2)):
165// ....: s = s*s
166// ....: if coeff > 0 :
167// ....: s = s*t**coeff
168
169// This works on one bit at a time, with many runs of zeros.
170// The digits can be collapsed into [(count, coeff)] as follows:
171
172// sage: [(len(list(group)),d) for d,group in itertools.groupby(sliding_window(4,l-2))]
173
174// Entries of the form (k, 0) turn into pow2k(k)
175// Entries of the form (1, coeff) turn into a squaring and then a table lookup.
176// We can fold the squaring into the previous pow2k(k) as pow2k(k+1).
177
178s = table[1 / 2]
179s.pow2k(127 + 1)
180s.multiply(s, table[1 / 2])
181s.pow2k(4 + 1)
182s.multiply(s, table[9 / 2])
183s.pow2k(3 + 1)
184s.multiply(s, table[11 / 2])
185s.pow2k(3 + 1)
186s.multiply(s, table[13 / 2])
187s.pow2k(3 + 1)
188s.multiply(s, table[15 / 2])
189s.pow2k(4 + 1)
190s.multiply(s, table[7 / 2])
191s.pow2k(4 + 1)
192s.multiply(s, table[15 / 2])
193s.pow2k(3 + 1)
194s.multiply(s, table[5 / 2])
195s.pow2k(3 + 1)
196s.multiply(s, table[1 / 2])
197s.pow2k(4 + 1)
198s.multiply(s, table[15 / 2])
199s.pow2k(4 + 1)
200s.multiply(s, table[15 / 2])
201s.pow2k(4 + 1)
202s.multiply(s, table[7 / 2])
203s.pow2k(3 + 1)
204s.multiply(s, table[3 / 2])
205s.pow2k(4 + 1)
206s.multiply(s, table[11 / 2])
207s.pow2k(5 + 1)
208s.multiply(s, table[11 / 2])
209s.pow2k(9 + 1)
210s.multiply(s, table[9 / 2])
211s.pow2k(3 + 1)
212s.multiply(s, table[3 / 2])
213s.pow2k(4 + 1)
214s.multiply(s, table[3 / 2])
215s.pow2k(4 + 1)
216s.multiply(s, table[3 / 2])
217s.pow2k(4 + 1)
218s.multiply(s, table[9 / 2])
219s.pow2k(3 + 1)
220s.multiply(s, table[7 / 2])
221s.pow2k(3 + 1)
222s.multiply(s, table[3 / 2])
223s.pow2k(3 + 1)
224s.multiply(s, table[13 / 2])
225s.pow2k(3 + 1)
226s.multiply(s, table[7 / 2])
227s.pow2k(4 + 1)
228s.multiply(s, table[9 / 2])
229s.pow2k(3 + 1)
230s.multiply(s, table[15 / 2])
231s.pow2k(4 + 1)
232s.multiply(s, table[11 / 2])
233
234return s
235}
236
237// multi_scalar_mult sets v = sum(scalars[i] * points[i]), and returns v.
238//
239// Execution time depends only on the lengths of the two slices, which must match.
240pub fn (mut v Point) multi_scalar_mult(scalars []Scalar, points []Point) Point {
241if scalars.len != points.len {
242panic('edwards25519: called multi_scalar_mult with different size inputs')
243}
244check_initialized(...points)
245
246mut sc := scalars.clone()
247// Proceed as in the single-base case, but share doublings
248// between each point in the multiscalar equation.
249
250// Build lookup tables for each point
251mut tables := []ProjLookupTable{len: points.len}
252for i, _ in tables {
253tables[i].from_p3(points[i])
254}
255// Compute signed radix-16 digits for each scalar
256// digits := make([][64]int8, len(scalars))
257mut digits := [][]i8{len: sc.len, init: []i8{len: 64, cap: 64}}
258
259for j, _ in digits {
260digits[j] = sc[j].signed_radix16()
261}
262
263// Unwrap first loop iteration to save computing 16*identity
264mut multiple := ProjectiveCached{}
265mut tmp1 := ProjectiveP1{}
266mut tmp2 := ProjectiveP2{}
267// Lookup-and-add the appropriate multiple of each input point
268for k, _ in tables {
269tables[k].select_into(mut multiple, digits[k][63])
270tmp1.add(v, multiple) // tmp1 = v + x_(j,63)*Q in P1xP1 coords
271v.from_p1(tmp1) // update v
272}
273tmp2.from_p3(v) // set up tmp2 = v in P2 coords for next iteration
274for r := 62; r >= 0; r-- {
275tmp1.double(tmp2) // tmp1 = 2*(prev) in P1xP1 coords
276tmp2.from_p1(tmp1) // tmp2 = 2*(prev) in P2 coords
277tmp1.double(tmp2) // tmp1 = 4*(prev) in P1xP1 coords
278tmp2.from_p1(tmp1) // tmp2 = 4*(prev) in P2 coords
279tmp1.double(tmp2) // tmp1 = 8*(prev) in P1xP1 coords
280tmp2.from_p1(tmp1) // tmp2 = 8*(prev) in P2 coords
281tmp1.double(tmp2) // tmp1 = 16*(prev) in P1xP1 coords
282v.from_p1(tmp1) // v = 16*(prev) in P3 coords
283// Lookup-and-add the appropriate multiple of each input point
284for s, _ in tables {
285tables[s].select_into(mut multiple, digits[s][r])
286tmp1.add(v, multiple) // tmp1 = v + x_(j,i)*Q in P1xP1 coords
287v.from_p1(tmp1) // update v
288}
289tmp2.from_p3(v) // set up tmp2 = v in P2 coords for next iteration
290}
291return v
292}
293
294// vartime_multiscalar_mult sets v = sum(scalars[i] * points[i]), and returns v.
295//
296// Execution time depends on the inputs.
297pub fn (mut v Point) vartime_multiscalar_mult(scalars []Scalar, points []Point) Point {
298if scalars.len != points.len {
299panic('edwards25519: called VarTimeMultiScalarMult with different size inputs')
300}
301check_initialized(...points)
302
303// Generalize double-base NAF computation to arbitrary sizes.
304// Here all the points are dynamic, so we only use the smaller
305// tables.
306
307// Build lookup tables for each point
308mut tables := []NafLookupTable5{len: points.len}
309for i, _ in tables {
310tables[i].from_p3(points[i])
311}
312mut sc := scalars.clone()
313// Compute a NAF for each scalar
314// mut nafs := make([][256]int8, len(scalars))
315mut nafs := [][]i8{len: sc.len, init: []i8{len: 256, cap: 256}}
316for j, _ in nafs {
317nafs[j] = sc[j].non_adjacent_form(5)
318}
319
320mut multiple := ProjectiveCached{}
321mut tmp1 := ProjectiveP1{}
322mut tmp2 := ProjectiveP2{}
323tmp2.zero()
324
325// Move from high to low bits, doubling the accumulator
326// at each iteration and checking whether there is a nonzero
327// coefficient to look up a multiple of.
328//
329// Skip trying to find the first nonzero coefficient, because
330// searching might be more work than a few extra doublings.
331// k == i, l == j
332for k := 255; k >= 0; k-- {
333tmp1.double(tmp2)
334
335for l, _ in nafs {
336if nafs[l][k] > 0 {
337v.from_p1(tmp1)
338tables[l].select_into(mut multiple, nafs[l][k])
339tmp1.add(v, multiple)
340} else if nafs[l][k] < 0 {
341v.from_p1(tmp1)
342tables[l].select_into(mut multiple, -nafs[l][k])
343tmp1.sub(v, multiple)
344}
345}
346
347tmp2.from_p1(tmp1)
348}
349
350v.from_p2(tmp2)
351return v
352}
353