Debian Codegraph @

edwards25519/field_generic.go

raw symbols SCIP
1 //go:build (!amd64 && !go1.13) || forcegeneric
2 // +build !amd64,!go1.13 forcegeneric
3
4 package edwards25519
5
6 // Element of the field GF(2^255 - 19) over which the elliptic
7 // curve Edwards25519 is defined.
8 type FieldElement [10]int32
9
10 var (
11 feZero FieldElement
12 feOne = FieldElement{1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
13 feMinusOne = FieldElement{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
14
15 // sqrt(-1)
16 feI = FieldElement{
17 -32595792, -7943725, 9377950, 3500415, 12389472,
18 -272473, -25146209, -2005654, 326686, 11406482,
19 }
20
21 // -sqrt(-1)
22 feMinusI = FieldElement{
23 32595792, 7943725, -9377950, -3500415, -12389472,
24 272473, 25146209, 2005654, -326686, -11406482,
25 }
26
27 // parameter d of Edwards25519
28 feD = FieldElement{
29 -10913610, 13857413, -15372611, 6949391, 114729,
30 -8787816, -6275908, -3247719, -18696448, -12055116,
31 }
32
33 // double feD
34 fe2D = FieldElement{
35 -21827239, -5839606, -30745221, 13898782, 229458,
36 15978800, -12551817, -6495438, 29715968, 9444199,
37 }
38
39 // 1 / sqrt(-1-d)
40 feInvSqrtMinusDMinusOne = FieldElement{
41 -6111485, -4156064, 27798727, -12243468, 25904040,
42 -120897, -20826367, 7060776, -6093568, 1986012,
43 }
44
45 // 2 / sqrt(-1-d)
46 feDoubleInvSqrtMinusDMinusOne = FieldElement{
47 -12222970, -8312128, -11511410, 9067497, -15300785,
48 -241793, 25456130, 14121551, -12187136, 3972024,
49 }
50
51 // 2i / sqrt(-1-d)
52 feDoubleIInvSqrtMinusDMinusOne = FieldElement{
53 -8930344, -9583591, 26444492, -3752533, -26044487,
54 743697, 2900628, -5634116, -25139868, 5270574,
55 }
56
57 // (d-1)^2
58 feDMinusOneSquared = FieldElement{
59 15551795, -11097455, -13425098, -10125071, -11896535,
60 10178284, -26634327, 4729244, -5282110, -10116402,
61 }
62
63 // 1 - d^2
64 feOneMinusDSquared = FieldElement{
65 6275446, -16617371, -22938544, -3773710, 11667077,
66 7397348, -27922721, 1766195, -24433858, 672203,
67 }
68
69 // (d+1)/(d-1)
70 feDp1OverDm1 = FieldElement{
71 -8275156, -1370137, -4651792, -7444191, 19032992,
72 -6350812, 7122893, -15485473, -16089458, 3776289,
73 }
74
75 // sqrt(i*d)
76 feSqrtID = FieldElement{
77 -27518040, 701139, 28659366, -9930925, -13176155,
78 -1348074, -30782278, -9245017, 26167231, 1494357,
79 }
80
81 // 1 / sqrt(1+d)
82 feInvSqrt1pD = FieldElement{
83 -29089260, 4791796, 20332186, -14900950, -20532188,
84 -371848, -1450314, 2817058, 12569934, -2635287,
85 }
86
87 epZero = ExtendedPoint{feZero, feOne, feOne, feZero}
88
89 epBase = ExtendedPoint{
90 FieldElement{-41032219, -27199451, -7502359, -2800332, -50176896,
91 -33336453, -33570123, -31949908, -53948439, -29257844},
92 FieldElement{20163995, 28827709, 65616271, 30544542, 24400674,
93 29683035, 27175815, 26206403, 10372291, 5663137},
94 feOne,
95 FieldElement{38281802, 6116118, 27349572, 33310069, 58473857,
96 22289538, 47757517, 20140834, 50497352, 6414979},
97 }
98 )
99
100 // Sets fe to -a. Returns fe.
101 func (fe *FieldElement) Neg(a *FieldElement) *FieldElement {
102 fe[0] = -a[0]
103 fe[1] = -a[1]
104 fe[2] = -a[2]
105 fe[3] = -a[3]
106 fe[4] = -a[4]
107 fe[5] = -a[5]
108 fe[6] = -a[6]
109 fe[7] = -a[7]
110 fe[8] = -a[8]
111 fe[9] = -a[9]
112 return fe
113 }
114
115 // Sets fe to a + b without normalizing. Returns fe.
116 func (fe *FieldElement) add(a, b *FieldElement) *FieldElement {
117 fe[0] = a[0] + b[0]
118 fe[1] = a[1] + b[1]
119 fe[2] = a[2] + b[2]
120 fe[3] = a[3] + b[3]
121 fe[4] = a[4] + b[4]
122 fe[5] = a[5] + b[5]
123 fe[6] = a[6] + b[6]
124 fe[7] = a[7] + b[7]
125 fe[8] = a[8] + b[8]
126 fe[9] = a[9] + b[9]
127 return fe
128 }
129
130 // Sets fe to a - b without normalizing. Returns fe.
131 func (fe *FieldElement) sub(a, b *FieldElement) *FieldElement {
132 fe[0] = a[0] - b[0]
133 fe[1] = a[1] - b[1]
134 fe[2] = a[2] - b[2]
135 fe[3] = a[3] - b[3]
136 fe[4] = a[4] - b[4]
137 fe[5] = a[5] - b[5]
138 fe[6] = a[6] - b[6]
139 fe[7] = a[7] - b[7]
140 fe[8] = a[8] - b[8]
141 fe[9] = a[9] - b[9]
142 return fe
143 }
144
145 // Interprets a 3-byte unsigned little endian byte-slice as int64
146 func load3(in []byte) int64 {
147 var r int64
148 r = int64(in[0])
149 r |= int64(in[1]) << 8
150 r |= int64(in[2]) << 16
151 return r
152 }
153
154 // Interprets a 4-byte unsigned little endian byte-slice as int64
155 func load4(in []byte) int64 {
156 var r int64
157 r = int64(in[0])
158 r |= int64(in[1]) << 8
159 r |= int64(in[2]) << 16
160 r |= int64(in[3]) << 24
161 return r
162 }
163
164 // Reduce the even coefficients to below 1.01*2^25 and the odd coefficients
165 // to below 1.01*2^24. Returns fe.
166 func (fe *FieldElement) normalize() *FieldElement {
167 return fe.setReduced(
168 int64(fe[0]), int64(fe[1]), int64(fe[2]), int64(fe[3]), int64(fe[4]),
169 int64(fe[5]), int64(fe[6]), int64(fe[7]), int64(fe[8]), int64(fe[9]))
170 }
171
172 // Set fe to h0 + h1*2^26 + h2*2^51 + ... + h9*2^230. Requires a little
173 // headroom in the inputs to store the carries. Returns fe.
174 func (fe *FieldElement) setReduced(
175 h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) *FieldElement {
176 var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9 int64
177
178 c0 = (h0 + (1 << 25)) >> 26
179 h1 += c0
180 h0 -= c0 << 26
181 c4 = (h4 + (1 << 25)) >> 26
182 h5 += c4
183 h4 -= c4 << 26
184
185 c1 = (h1 + (1 << 24)) >> 25
186 h2 += c1
187 h1 -= c1 << 25
188 c5 = (h5 + (1 << 24)) >> 25
189 h6 += c5
190 h5 -= c5 << 25
191
192 c2 = (h2 + (1 << 25)) >> 26
193 h3 += c2
194 h2 -= c2 << 26
195 c6 = (h6 + (1 << 25)) >> 26
196 h7 += c6
197 h6 -= c6 << 26
198
199 c3 = (h3 + (1 << 24)) >> 25
200 h4 += c3
201 h3 -= c3 << 25
202 c7 = (h7 + (1 << 24)) >> 25
203 h8 += c7
204 h7 -= c7 << 25
205
206 c4 = (h4 + (1 << 25)) >> 26
207 h5 += c4
208 h4 -= c4 << 26
209 c8 = (h8 + (1 << 25)) >> 26
210 h9 += c8
211 h8 -= c8 << 26
212
213 c9 = (h9 + (1 << 24)) >> 25
214 h0 += c9 * 19
215 h9 -= c9 << 25
216
217 c0 = (h0 + (1 << 25)) >> 26
218 h1 += c0
219 h0 -= c0 << 26
220
221 fe[0] = int32(h0)
222 fe[1] = int32(h1)
223 fe[2] = int32(h2)
224 fe[3] = int32(h3)
225 fe[4] = int32(h4)
226 fe[5] = int32(h5)
227 fe[6] = int32(h6)
228 fe[7] = int32(h7)
229 fe[8] = int32(h8)
230 fe[9] = int32(h9)
231
232 return fe
233 }
234
235 // Set fe to a if b == 1. Requires b to be either 0 or 1.
236 func (fe *FieldElement) ConditionalSet(a *FieldElement, b int32) {
237 b = -b // b == 0b11111111111111111111111111111111 or 0.
238 fe[0] ^= b & (fe[0] ^ a[0])
239 fe[1] ^= b & (fe[1] ^ a[1])
240 fe[2] ^= b & (fe[2] ^ a[2])
241 fe[3] ^= b & (fe[3] ^ a[3])
242 fe[4] ^= b & (fe[4] ^ a[4])
243 fe[5] ^= b & (fe[5] ^ a[5])
244 fe[6] ^= b & (fe[6] ^ a[6])
245 fe[7] ^= b & (fe[7] ^ a[7])
246 fe[8] ^= b & (fe[8] ^ a[8])
247 fe[9] ^= b & (fe[9] ^ a[9])
248 }
249
250 // Write fe to s in little endian. Returns fe.
251 func (fe *FieldElement) BytesInto(s *[32]byte) *FieldElement {
252 var carry [10]int32
253
254 q := (19*fe[9] + (1 << 24)) >> 25
255 q = (fe[0] + q) >> 26
256 q = (fe[1] + q) >> 25
257 q = (fe[2] + q) >> 26
258 q = (fe[3] + q) >> 25
259 q = (fe[4] + q) >> 26
260 q = (fe[5] + q) >> 25
261 q = (fe[6] + q) >> 26
262 q = (fe[7] + q) >> 25
263 q = (fe[8] + q) >> 26
264 q = (fe[9] + q) >> 25
265
266 fe[0] += 19 * q
267
268 carry[0] = fe[0] >> 26
269 fe[1] += carry[0]
270 fe[0] -= carry[0] << 26
271 carry[1] = fe[1] >> 25
272 fe[2] += carry[1]
273 fe[1] -= carry[1] << 25
274 carry[2] = fe[2] >> 26
275 fe[3] += carry[2]
276 fe[2] -= carry[2] << 26
277 carry[3] = fe[3] >> 25
278 fe[4] += carry[3]
279 fe[3] -= carry[3] << 25
280 carry[4] = fe[4] >> 26
281 fe[5] += carry[4]
282 fe[4] -= carry[4] << 26
283 carry[5] = fe[5] >> 25
284 fe[6] += carry[5]
285 fe[5] -= carry[5] << 25
286 carry[6] = fe[6] >> 26
287 fe[7] += carry[6]
288 fe[6] -= carry[6] << 26
289 carry[7] = fe[7] >> 25
290 fe[8] += carry[7]
291 fe[7] -= carry[7] << 25
292 carry[8] = fe[8] >> 26
293 fe[9] += carry[8]
294 fe[8] -= carry[8] << 26
295 carry[9] = fe[9] >> 25
296 fe[9] -= carry[9] << 25
297
298 s[0] = byte(fe[0] >> 0)
299 s[1] = byte(fe[0] >> 8)
300 s[2] = byte(fe[0] >> 16)
301 s[3] = byte((fe[0] >> 24) | (fe[1] << 2))
302 s[4] = byte(fe[1] >> 6)
303 s[5] = byte(fe[1] >> 14)
304 s[6] = byte((fe[1] >> 22) | (fe[2] << 3))
305 s[7] = byte(fe[2] >> 5)
306 s[8] = byte(fe[2] >> 13)
307 s[9] = byte((fe[2] >> 21) | (fe[3] << 5))
308 s[10] = byte(fe[3] >> 3)
309 s[11] = byte(fe[3] >> 11)
310 s[12] = byte((fe[3] >> 19) | (fe[4] << 6))
311 s[13] = byte(fe[4] >> 2)
312 s[14] = byte(fe[4] >> 10)
313 s[15] = byte(fe[4] >> 18)
314 s[16] = byte(fe[5] >> 0)
315 s[17] = byte(fe[5] >> 8)
316 s[18] = byte(fe[5] >> 16)
317 s[19] = byte((fe[5] >> 24) | (fe[6] << 1))
318 s[20] = byte(fe[6] >> 7)
319 s[21] = byte(fe[6] >> 15)
320 s[22] = byte((fe[6] >> 23) | (fe[7] << 3))
321 s[23] = byte(fe[7] >> 5)
322 s[24] = byte(fe[7] >> 13)
323 s[25] = byte((fe[7] >> 21) | (fe[8] << 4))
324 s[26] = byte(fe[8] >> 4)
325 s[27] = byte(fe[8] >> 12)
326 s[28] = byte((fe[8] >> 20) | (fe[9] << 6))
327 s[29] = byte(fe[9] >> 2)
328 s[30] = byte(fe[9] >> 10)
329 s[31] = byte(fe[9] >> 18)
330 return fe
331 }
332
333 // Sets fe to the little endian number encoded in buf modulo 2^255-19.
334 // Ignores the highest bit in buf. Returns fe.
335 func (fe *FieldElement) SetBytes(buf *[32]byte) *FieldElement {
336 return fe.setReduced(
337 load4(buf[:]),
338 load3(buf[4:])<<6,
339 load3(buf[7:])<<5,
340 load3(buf[10:])<<3,
341 load3(buf[13:])<<2,
342 load4(buf[16:]),
343 load3(buf[20:])<<7,
344 load3(buf[23:])<<5,
345 load3(buf[26:])<<4,
346 (load3(buf[29:])&8388607)<<2,
347 )
348 }
349
350 // Sets fe to a * b. Returns fe.
351 func (fe *FieldElement) Mul(a, b *FieldElement) *FieldElement {
352 a0 := int64(a[0])
353 a1 := int64(a[1])
354 a2 := int64(a[2])
355 a3 := int64(a[3])
356 a4 := int64(a[4])
357 a5 := int64(a[5])
358 a6 := int64(a[6])
359 a7 := int64(a[7])
360 a8 := int64(a[8])
361 a9 := int64(a[9])
362
363 a1_2 := int64(2 * a[1])
364 a3_2 := int64(2 * a[3])
365 a5_2 := int64(2 * a[5])
366 a7_2 := int64(2 * a[7])
367 a9_2 := int64(2 * a[9])
368
369 b0 := int64(b[0])
370 b1 := int64(b[1])
371 b2 := int64(b[2])
372 b3 := int64(b[3])
373 b4 := int64(b[4])
374 b5 := int64(b[5])
375 b6 := int64(b[6])
376 b7 := int64(b[7])
377 b8 := int64(b[8])
378 b9 := int64(b[9])
379
380 b1_19 := int64(19 * b[1])
381 b2_19 := int64(19 * b[2])
382 b3_19 := int64(19 * b[3])
383 b4_19 := int64(19 * b[4])
384 b5_19 := int64(19 * b[5])
385 b6_19 := int64(19 * b[6])
386 b7_19 := int64(19 * b[7])
387 b8_19 := int64(19 * b[8])
388 b9_19 := int64(19 * b[9])
389
390 h0 := a0*b0 + a1_2*b9_19 + a2*b8_19 + a3_2*b7_19 + a4*b6_19 + a5_2*b5_19 + a6*b4_19 + a7_2*b3_19 + a8*b2_19 + a9_2*b1_19
391 h1 := a0*b1 + a1*b0 + a2*b9_19 + a3*b8_19 + a4*b7_19 + a5*b6_19 + a6*b5_19 + a7*b4_19 + a8*b3_19 + a9*b2_19
392 h2 := a0*b2 + a1_2*b1 + a2*b0 + a3_2*b9_19 + a4*b8_19 + a5_2*b7_19 + a6*b6_19 + a7_2*b5_19 + a8*b4_19 + a9_2*b3_19
393 h3 := a0*b3 + a1*b2 + a2*b1 + a3*b0 + a4*b9_19 + a5*b8_19 + a6*b7_19 + a7*b6_19 + a8*b5_19 + a9*b4_19
394 h4 := a0*b4 + a1_2*b3 + a2*b2 + a3_2*b1 + a4*b0 + a5_2*b9_19 + a6*b8_19 + a7_2*b7_19 + a8*b6_19 + a9_2*b5_19
395 h5 := a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0 + a6*b9_19 + a7*b8_19 + a8*b7_19 + a9*b6_19
396 h6 := a0*b6 + a1_2*b5 + a2*b4 + a3_2*b3 + a4*b2 + a5_2*b1 + a6*b0 + a7_2*b9_19 + a8*b8_19 + a9_2*b7_19
397 h7 := a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0 + a8*b9_19 + a9*b8_19
398 h8 := a0*b8 + a1_2*b7 + a2*b6 + a3_2*b5 + a4*b4 + a5_2*b3 + a6*b2 + a7_2*b1 + a8*b0 + a9_2*b9_19
399 h9 := a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0
400
401 return fe.setReduced(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
402 }
403
404 // Returns the unnormalized coefficients of fe^2.
405 func (fe *FieldElement) square() (h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) {
406 f0 := int64(fe[0])
407 f1 := int64(fe[1])
408 f2 := int64(fe[2])
409 f3 := int64(fe[3])
410 f4 := int64(fe[4])
411 f5 := int64(fe[5])
412 f6 := int64(fe[6])
413 f7 := int64(fe[7])
414 f8 := int64(fe[8])
415 f9 := int64(fe[9])
416 f0_2 := int64(2 * fe[0])
417 f1_2 := int64(2 * fe[1])
418 f2_2 := int64(2 * fe[2])
419 f3_2 := int64(2 * fe[3])
420 f4_2 := int64(2 * fe[4])
421 f5_2 := int64(2 * fe[5])
422 f6_2 := int64(2 * fe[6])
423 f7_2 := int64(2 * fe[7])
424 f5_38 := 38 * f5
425 f6_19 := 19 * f6
426 f7_38 := 38 * f7
427 f8_19 := 19 * f8
428 f9_38 := 38 * f9
429
430 h0 = f0*f0 + f1_2*f9_38 + f2_2*f8_19 + f3_2*f7_38 + f4_2*f6_19 + f5*f5_38
431 h1 = f0_2*f1 + f2*f9_38 + f3_2*f8_19 + f4*f7_38 + f5_2*f6_19
432 h2 = f0_2*f2 + f1_2*f1 + f3_2*f9_38 + f4_2*f8_19 + f5_2*f7_38 + f6*f6_19
433 h3 = f0_2*f3 + f1_2*f2 + f4*f9_38 + f5_2*f8_19 + f6*f7_38
434 h4 = f0_2*f4 + f1_2*f3_2 + f2*f2 + f5_2*f9_38 + f6_2*f8_19 + f7*f7_38
435 h5 = f0_2*f5 + f1_2*f4 + f2_2*f3 + f6*f9_38 + f7_2*f8_19
436 h6 = f0_2*f6 + f1_2*f5_2 + f2_2*f4 + f3_2*f3 + f7_2*f9_38 + f8*f8_19
437 h7 = f0_2*f7 + f1_2*f6 + f2_2*f5 + f3_2*f4 + f8*f9_38
438 h8 = f0_2*f8 + f1_2*f7_2 + f2_2*f6 + f3_2*f5_2 + f4*f4 + f9*f9_38
439 h9 = f0_2*f9 + f1_2*f8 + f2_2*f7 + f3_2*f6 + f4_2*f5
440
441 return
442 }
443
444 // Sets fe to a^2. Returns fe.
445 func (fe *FieldElement) Square(a *FieldElement) *FieldElement {
446 h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := a.square()
447 return fe.setReduced(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
448 }
449
450 // Sets fe to 2 * a^2. Returns fe.
451 func (fe *FieldElement) DoubledSquare(a *FieldElement) *FieldElement {
452 h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := a.square()
453 h0 += h0
454 h1 += h1
455 h2 += h2
456 h3 += h3
457 h4 += h4
458 h5 += h5
459 h6 += h6
460 h7 += h7
461 h8 += h8
462 h9 += h9
463 return fe.setReduced(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
464 }