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