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