1
2
3
4
5
6 package field
7
8 import (
9 "crypto/subtle"
10 "encoding/binary"
11 "math/bits"
12 )
13
14
15
16
17
18
19
20
21
22 type Element struct {
23
24
25
26
27 l0 uint64
28 l1 uint64
29 l2 uint64
30 l3 uint64
31 l4 uint64
32 }
33
34 const maskLow51Bits uint64 = (1 << 51) - 1
35
36 var feZero = &Element{0, 0, 0, 0, 0}
37
38
39 func (v *Element) Zero() *Element {
40 *v = *feZero
41 return v
42 }
43
44 var feOne = &Element{1, 0, 0, 0, 0}
45
46
47 func (v *Element) One() *Element {
48 *v = *feOne
49 return v
50 }
51
52
53 func (v *Element) reduce() *Element {
54 v.carryPropagate()
55
56
57
58
59
60
61 c := (v.l0 + 19) >> 51
62 c = (v.l1 + c) >> 51
63 c = (v.l2 + c) >> 51
64 c = (v.l3 + c) >> 51
65 c = (v.l4 + c) >> 51
66
67
68
69 v.l0 += 19 * c
70
71 v.l1 += v.l0 >> 51
72 v.l0 = v.l0 & maskLow51Bits
73 v.l2 += v.l1 >> 51
74 v.l1 = v.l1 & maskLow51Bits
75 v.l3 += v.l2 >> 51
76 v.l2 = v.l2 & maskLow51Bits
77 v.l4 += v.l3 >> 51
78 v.l3 = v.l3 & maskLow51Bits
79
80 v.l4 = v.l4 & maskLow51Bits
81
82 return v
83 }
84
85
86 func (v *Element) Add(a, b *Element) *Element {
87 v.l0 = a.l0 + b.l0
88 v.l1 = a.l1 + b.l1
89 v.l2 = a.l2 + b.l2
90 v.l3 = a.l3 + b.l3
91 v.l4 = a.l4 + b.l4
92
93
94
95
96 return v.carryPropagateGeneric()
97 }
98
99
100 func (v *Element) Subtract(a, b *Element) *Element {
101
102
103 v.l0 = (a.l0 + 0xFFFFFFFFFFFDA) - b.l0
104 v.l1 = (a.l1 + 0xFFFFFFFFFFFFE) - b.l1
105 v.l2 = (a.l2 + 0xFFFFFFFFFFFFE) - b.l2
106 v.l3 = (a.l3 + 0xFFFFFFFFFFFFE) - b.l3
107 v.l4 = (a.l4 + 0xFFFFFFFFFFFFE) - b.l4
108 return v.carryPropagate()
109 }
110
111
112 func (v *Element) Negate(a *Element) *Element {
113 return v.Subtract(feZero, a)
114 }
115
116
117
118
119 func (v *Element) Invert(z *Element) *Element {
120
121
122 var z2, z9, z11, z2_5_0, z2_10_0, z2_20_0, z2_50_0, z2_100_0, t Element
123
124 z2.Square(z)
125 t.Square(&z2)
126 t.Square(&t)
127 z9.Multiply(&t, z)
128 z11.Multiply(&z9, &z2)
129 t.Square(&z11)
130 z2_5_0.Multiply(&t, &z9)
131
132 t.Square(&z2_5_0)
133 for i := 0; i < 4; i++ {
134 t.Square(&t)
135 }
136 z2_10_0.Multiply(&t, &z2_5_0)
137
138 t.Square(&z2_10_0)
139 for i := 0; i < 9; i++ {
140 t.Square(&t)
141 }
142 z2_20_0.Multiply(&t, &z2_10_0)
143
144 t.Square(&z2_20_0)
145 for i := 0; i < 19; i++ {
146 t.Square(&t)
147 }
148 t.Multiply(&t, &z2_20_0)
149
150 t.Square(&t)
151 for i := 0; i < 9; i++ {
152 t.Square(&t)
153 }
154 z2_50_0.Multiply(&t, &z2_10_0)
155
156 t.Square(&z2_50_0)
157 for i := 0; i < 49; i++ {
158 t.Square(&t)
159 }
160 z2_100_0.Multiply(&t, &z2_50_0)
161
162 t.Square(&z2_100_0)
163 for i := 0; i < 99; i++ {
164 t.Square(&t)
165 }
166 t.Multiply(&t, &z2_100_0)
167
168 t.Square(&t)
169 for i := 0; i < 49; i++ {
170 t.Square(&t)
171 }
172 t.Multiply(&t, &z2_50_0)
173
174 t.Square(&t)
175 t.Square(&t)
176 t.Square(&t)
177 t.Square(&t)
178 t.Square(&t)
179
180 return v.Multiply(&t, &z11)
181 }
182
183
184 func (v *Element) Set(a *Element) *Element {
185 *v = *a
186 return v
187 }
188
189
190
191
192
193
194 func (v *Element) SetBytes(x []byte) *Element {
195 if len(x) != 32 {
196 panic("edwards25519: invalid field element input size")
197 }
198
199
200 v.l0 = binary.LittleEndian.Uint64(x[0:8])
201 v.l0 &= maskLow51Bits
202
203 v.l1 = binary.LittleEndian.Uint64(x[6:14]) >> 3
204 v.l1 &= maskLow51Bits
205
206 v.l2 = binary.LittleEndian.Uint64(x[12:20]) >> 6
207 v.l2 &= maskLow51Bits
208
209 v.l3 = binary.LittleEndian.Uint64(x[19:27]) >> 1
210 v.l3 &= maskLow51Bits
211
212
213 v.l4 = binary.LittleEndian.Uint64(x[24:32]) >> 12
214 v.l4 &= maskLow51Bits
215
216 return v
217 }
218
219
220 func (v *Element) Bytes() []byte {
221
222
223 var out [32]byte
224 return v.bytes(&out)
225 }
226
227 func (v *Element) bytes(out *[32]byte) []byte {
228 t := *v
229 t.reduce()
230
231 var buf [8]byte
232 for i, l := range [5]uint64{t.l0, t.l1, t.l2, t.l3, t.l4} {
233 bitsOffset := i * 51
234 binary.LittleEndian.PutUint64(buf[:], l<<uint(bitsOffset%8))
235 for i, bb := range buf {
236 off := bitsOffset/8 + i
237 if off >= len(out) {
238 break
239 }
240 out[off] |= bb
241 }
242 }
243
244 return out[:]
245 }
246
247
248 func (v *Element) Equal(u *Element) int {
249 sa, sv := u.Bytes(), v.Bytes()
250 return subtle.ConstantTimeCompare(sa, sv)
251 }
252
253
254 func mask64Bits(cond int) uint64 { return ^(uint64(cond) - 1) }
255
256
257 func (v *Element) Select(a, b *Element, cond int) *Element {
258 m := mask64Bits(cond)
259 v.l0 = (m & a.l0) | (^m & b.l0)
260 v.l1 = (m & a.l1) | (^m & b.l1)
261 v.l2 = (m & a.l2) | (^m & b.l2)
262 v.l3 = (m & a.l3) | (^m & b.l3)
263 v.l4 = (m & a.l4) | (^m & b.l4)
264 return v
265 }
266
267
268 func (v *Element) Swap(u *Element, cond int) {
269 m := mask64Bits(cond)
270 t := m & (v.l0 ^ u.l0)
271 v.l0 ^= t
272 u.l0 ^= t
273 t = m & (v.l1 ^ u.l1)
274 v.l1 ^= t
275 u.l1 ^= t
276 t = m & (v.l2 ^ u.l2)
277 v.l2 ^= t
278 u.l2 ^= t
279 t = m & (v.l3 ^ u.l3)
280 v.l3 ^= t
281 u.l3 ^= t
282 t = m & (v.l4 ^ u.l4)
283 v.l4 ^= t
284 u.l4 ^= t
285 }
286
287
288 func (v *Element) IsNegative() int {
289 return int(v.Bytes()[0] & 1)
290 }
291
292
293 func (v *Element) Absolute(u *Element) *Element {
294 return v.Select(new(Element).Negate(u), u, u.IsNegative())
295 }
296
297
298 func (v *Element) Multiply(x, y *Element) *Element {
299 feMul(v, x, y)
300 return v
301 }
302
303
304 func (v *Element) Square(x *Element) *Element {
305 feSquare(v, x)
306 return v
307 }
308
309
310 func (v *Element) Mult32(x *Element, y uint32) *Element {
311 x0lo, x0hi := mul51(x.l0, y)
312 x1lo, x1hi := mul51(x.l1, y)
313 x2lo, x2hi := mul51(x.l2, y)
314 x3lo, x3hi := mul51(x.l3, y)
315 x4lo, x4hi := mul51(x.l4, y)
316 v.l0 = x0lo + 19*x4hi
317 v.l1 = x1lo + x0hi
318 v.l2 = x2lo + x1hi
319 v.l3 = x3lo + x2hi
320 v.l4 = x4lo + x3hi
321
322
323 return v
324 }
325
326
327 func mul51(a uint64, b uint32) (lo uint64, hi uint64) {
328 mh, ml := bits.Mul64(a, uint64(b))
329 lo = ml & maskLow51Bits
330 hi = (mh << 13) | (ml >> 51)
331 return
332 }
333
334
335 func (v *Element) Pow22523(x *Element) *Element {
336 var t0, t1, t2 Element
337
338 t0.Square(x)
339 t1.Square(&t0)
340 t1.Square(&t1)
341 t1.Multiply(x, &t1)
342 t0.Multiply(&t0, &t1)
343 t0.Square(&t0)
344 t0.Multiply(&t1, &t0)
345 t1.Square(&t0)
346 for i := 1; i < 5; i++ {
347 t1.Square(&t1)
348 }
349 t0.Multiply(&t1, &t0)
350 t1.Square(&t0)
351 for i := 1; i < 10; i++ {
352 t1.Square(&t1)
353 }
354 t1.Multiply(&t1, &t0)
355 t2.Square(&t1)
356 for i := 1; i < 20; i++ {
357 t2.Square(&t2)
358 }
359 t1.Multiply(&t2, &t1)
360 t1.Square(&t1)
361 for i := 1; i < 10; i++ {
362 t1.Square(&t1)
363 }
364 t0.Multiply(&t1, &t0)
365 t1.Square(&t0)
366 for i := 1; i < 50; i++ {
367 t1.Square(&t1)
368 }
369 t1.Multiply(&t1, &t0)
370 t2.Square(&t1)
371 for i := 1; i < 100; i++ {
372 t2.Square(&t2)
373 }
374 t1.Multiply(&t2, &t1)
375 t1.Square(&t1)
376 for i := 1; i < 50; i++ {
377 t1.Square(&t1)
378 }
379 t0.Multiply(&t1, &t0)
380 t0.Square(&t0)
381 t0.Square(&t0)
382 return v.Multiply(&t0, x)
383 }
384
385
386 var sqrtM1 = &Element{1718705420411056, 234908883556509,
387 2233514472574048, 2117202627021982, 765476049583133}
388
389
390
391
392
393
394 func (r *Element) SqrtRatio(u, v *Element) (rr *Element, wasSquare int) {
395 var a, b Element
396
397
398 v2 := a.Square(v)
399 uv3 := b.Multiply(u, b.Multiply(v2, v))
400 uv7 := a.Multiply(uv3, a.Square(v2))
401 r.Multiply(uv3, r.Pow22523(uv7))
402
403 check := a.Multiply(v, a.Square(r))
404
405 uNeg := b.Negate(u)
406 correctSignSqrt := check.Equal(u)
407 flippedSignSqrt := check.Equal(uNeg)
408 flippedSignSqrtI := check.Equal(uNeg.Multiply(uNeg, sqrtM1))
409
410 rPrime := b.Multiply(r, sqrtM1)
411
412 r.Select(rPrime, r, flippedSignSqrt|flippedSignSqrtI)
413
414 r.Absolute(r)
415 return r, correctSignSqrt | flippedSignSqrt
416 }
417
View as plain text