Debian Codegraph @

edwards25519/curve.go

raw symbols SCIP
1 // Go implementation of the elliptic curve Edwards25519 of which the
2 // Ristretto group is a subquotient.
3 package edwards25519
4
5 import (
6 "crypto/subtle"
7 "encoding/hex"
8 "fmt"
9 )
10
11 // (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, X*Y=Z*T. Aka P3.
12 type ExtendedPoint struct {
13 X, Y, Z, T FieldElement
14 }
15
16 // ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T. Aka P1P1.
17 type CompletedPoint struct {
18 X, Y, Z, T FieldElement
19 }
20
21 // (X:Y:Z) satisfying x=X/Z, y=Y/Z.
22 type ProjectivePoint struct {
23 X, Y, Z FieldElement
24 }
25
26 // Set p to (-i,0), a point Ristretto-equivalent to 0. Returns p.
27 func (p *ExtendedPoint) SetTorsion3() *ExtendedPoint {
28 p.X.Set(&feMinusI)
29 p.Y.SetZero()
30 p.Z.Set(&feOne)
31 p.T.SetZero()
32 return p
33 }
34
35 // Set p to (i,0), a point Ristretto-equivalent to 0. Returns p.
36 func (p *ExtendedPoint) SetTorsion2() *ExtendedPoint {
37 p.X.Set(&feI)
38 p.Y.SetZero()
39 p.Z.Set(&feOne)
40 p.T.SetZero()
41 return p
42 }
43
44 // Set p to (0,-1), a point Ristretto-equivalent to 0. Returns p.
45 func (p *ExtendedPoint) SetTorsion1() *ExtendedPoint {
46 p.X.SetZero()
47 p.Y.Set(&feMinusOne)
48 p.Z.Set(&feOne)
49 p.T.SetZero()
50 return p
51 }
52
53 // Set p to zero, the neutral element. Return p.
54 func (p *ProjectivePoint) SetZero() *ProjectivePoint {
55 p.X.SetZero()
56 p.Y.SetOne()
57 p.Z.SetOne()
58 return p
59 }
60
61 // Set p to zero, the neutral element. Return p.
62 func (p *ExtendedPoint) SetZero() *ExtendedPoint {
63 p.X.SetZero()
64 p.Y.SetOne()
65 p.Z.SetOne()
66 p.T.SetZero()
67 return p
68 }
69
70 // Set p to the basepoint (x,4/5) with x>=0. Returns p
71 func (p *ExtendedPoint) SetBase() *ExtendedPoint {
72 return p.Set(&epBase)
73 }
74
75 // Set p to q. Returns p.
76 func (p *ExtendedPoint) Set(q *ExtendedPoint) *ExtendedPoint {
77 p.X.Set(&q.X)
78 p.Y.Set(&q.Y)
79 p.Z.Set(&q.Z)
80 p.T.Set(&q.T)
81 return p
82 }
83
84 // Set p to q if b == 1. Assumes b is 0 or 1. Returns p.
85 func (p *ExtendedPoint) ConditionalSet(q *ExtendedPoint, b int32) *ExtendedPoint {
86 p.X.ConditionalSet(&q.X, b)
87 p.Y.ConditionalSet(&q.Y, b)
88 p.Z.ConditionalSet(&q.Z, b)
89 p.T.ConditionalSet(&q.T, b)
90 return p
91 }
92
93 // Sets p to q+r. Returns p
94 func (p *CompletedPoint) AddExtended(q, r *ExtendedPoint) *CompletedPoint {
95 var a, b, c, d, t FieldElement
96
97 a.sub(&q.Y, &q.X)
98 t.sub(&r.Y, &r.X)
99 a.Mul(&a, &t)
100 b.add(&q.X, &q.Y)
101 t.add(&r.X, &r.Y)
102 b.Mul(&b, &t)
103 c.Mul(&q.T, &r.T)
104 c.Mul(&c, &fe2D)
105 d.Mul(&q.Z, &r.Z)
106 d.add(&d, &d)
107 p.X.sub(&b, &a)
108 p.T.sub(&d, &c)
109 p.Z.add(&d, &c)
110 p.Y.add(&b, &a)
111
112 return p
113 }
114
115 // Sets p to q-r. Returns p
116 func (p *CompletedPoint) SubExtended(q, r *ExtendedPoint) *CompletedPoint {
117 var a, b, c, d, t FieldElement
118
119 a.sub(&q.Y, &q.X)
120 t.add(&r.Y, &r.X)
121 a.Mul(&a, &t)
122 b.add(&q.X, &q.Y)
123 t.sub(&r.Y, &r.X)
124 b.Mul(&b, &t)
125 c.Mul(&q.T, &r.T)
126 c.Mul(&c, &fe2D)
127 d.Mul(&q.Z, &r.Z)
128 d.add(&d, &d)
129 p.X.sub(&b, &a)
130 p.T.add(&d, &c)
131 p.Z.sub(&d, &c)
132 p.Y.add(&b, &a)
133
134 return p
135 }
136
137 // Set p to 2 * q. Returns p.
138 func (p *CompletedPoint) DoubleProjective(q *ProjectivePoint) *CompletedPoint {
139 var t0 FieldElement
140
141 p.X.Square(&q.X)
142 p.Z.Square(&q.Y)
143 p.T.DoubledSquare(&q.Z)
144 p.Y.add(&q.X, &q.Y)
145 t0.Square(&p.Y)
146 p.Y.add(&p.Z, &p.X)
147 p.Z.sub(&p.Z, &p.X)
148 p.X.sub(&t0, &p.Y)
149 p.T.sub(&p.T, &p.Z)
150
151 return p
152 }
153
154 // Set p to 2 * q. Returns p.
155 func (p *CompletedPoint) DoubleExtended(q *ExtendedPoint) *CompletedPoint {
156 var a, b, c, d FieldElement
157
158 a.Square(&q.X)
159 b.Square(&q.Y)
160 c.DoubledSquare(&q.Z)
161 d.Neg(&a)
162 p.X.add(&q.X, &q.Y)
163 p.X.Square(&p.X)
164 p.X.sub(&p.X, &a)
165 p.X.sub(&p.X, &b)
166 p.Z.add(&d, &b)
167 p.T.sub(&p.Z, &c)
168 p.Y.sub(&d, &b)
169
170 return p
171 }
172
173 // Set p to q. Returns p.
174 func (p *ProjectivePoint) SetExtended(q *ExtendedPoint) *ProjectivePoint {
175 p.X.Set(&q.X)
176 p.Y.Set(&q.Y)
177 p.Z.Set(&q.Z)
178 return p
179 }
180
181 // Set p to q. Returns p.
182 func (p *ProjectivePoint) SetCompleted(q *CompletedPoint) *ProjectivePoint {
183 p.X.Mul(&q.X, &q.T)
184 p.Y.Mul(&q.Y, &q.Z)
185 p.Z.Mul(&q.Z, &q.T)
186 return p
187 }
188
189 // Set p to 2 * q. Returns p.
190 func (p *ExtendedPoint) Double(q *ExtendedPoint) *ExtendedPoint {
191 var tmp CompletedPoint
192 tmp.DoubleExtended(q)
193 p.SetCompleted(&tmp)
194 return p
195 }
196
197 // Set p to q + r. Returns p.
198 func (p *ExtendedPoint) Add(q, r *ExtendedPoint) *ExtendedPoint {
199 var tmp CompletedPoint
200 tmp.AddExtended(q, r)
201 p.SetCompleted(&tmp)
202 return p
203 }
204
205 // Set p to q - r. Returns p.
206 func (p *ExtendedPoint) Sub(q, r *ExtendedPoint) *ExtendedPoint {
207 var tmp CompletedPoint
208 tmp.SubExtended(q, r)
209 p.SetCompleted(&tmp)
210 return p
211 }
212
213 // Sets p to q. Returns p.
214 func (p *ExtendedPoint) SetCompleted(q *CompletedPoint) *ExtendedPoint {
215 p.X.Mul(&q.X, &q.T)
216 p.Y.Mul(&q.Y, &q.Z)
217 p.Z.Mul(&q.Z, &q.T)
218 p.T.Mul(&q.X, &q.Y)
219 return p
220 }
221
222 // Set p to a point corresponding to the encoded group element of
223 // the ristretto group. Returns whether the buffer encoded a group element.
224 func (p *ExtendedPoint) SetRistretto(buf *[32]byte) bool {
225 var s, s2, chk, yDen, yNum, yDen2, xDen2, isr, xDenInv FieldElement
226 var yDenInv, t FieldElement
227 var b, ret int32
228 var buf2 [32]byte
229
230 s.SetBytes(buf)
231
232 // ensures 0 ≤ s < 2^255-19
233 s.BytesInto(&buf2)
234 ret = int32(1 - subtle.ConstantTimeCompare(buf[:], buf2[:]))
235 ret |= int32(buf2[0] & 1) // ensure s is positive
236
237 s2.Square(&s)
238 yDen.add(&feOne, &s2)
239 yNum.sub(&feOne, &s2)
240 yDen2.Square(&yDen)
241 xDen2.Square(&yNum)
242 xDen2.Mul(&xDen2, &feD)
243 xDen2.add(&xDen2, &yDen2)
244 xDen2.Neg(&xDen2)
245 t.Mul(&xDen2, &yDen2)
246 isr.InvSqrt(&t)
247 chk.Square(&isr)
248 chk.Mul(&chk, &t)
249 ret |= 1 - chk.IsOneI()
250 xDenInv.Mul(&isr, &yDen)
251 yDenInv.Mul(&xDenInv, &isr)
252 yDenInv.Mul(&yDenInv, &xDen2)
253 p.X.Mul(&s, &xDenInv)
254 p.X.add(&p.X, &p.X)
255 b = p.X.IsNegativeI()
256 t.Neg(&p.X)
257 p.X.ConditionalSet(&t, b)
258 p.Y.Mul(&yNum, &yDenInv)
259 p.Z.SetOne()
260 p.T.Mul(&p.X, &p.Y)
261 ret |= p.T.IsNegativeI()
262 ret |= 1 - p.Y.IsNonZeroI()
263 p.X.ConditionalSet(&feZero, ret)
264 p.Y.ConditionalSet(&feZero, ret)
265 p.Z.ConditionalSet(&feZero, ret)
266 p.T.ConditionalSet(&feZero, ret)
267 return ret == 0
268 }
269
270 // Pack p using the Ristretto encoding and return it.
271 // Requires p to be even.
272 func (p *ExtendedPoint) Ristretto() []byte {
273 var buf [32]byte
274 p.RistrettoInto(&buf)
275 return buf[:]
276 }
277
278 // Pack p using the Ristretto encoding and write to buf. Returns p.
279 // Requires p to be even.
280 func (p *ExtendedPoint) RistrettoInto(buf *[32]byte) *ExtendedPoint {
281 var d, u1, u2, isr, i1, i2, zInv, denInv, nx, ny, s FieldElement
282 var b int32
283
284 d.add(&p.Z, &p.Y)
285 u1.sub(&p.Z, &p.Y)
286 u1.Mul(&u1, &d)
287
288 u2.Mul(&p.X, &p.Y)
289
290 isr.Square(&u2)
291 isr.Mul(&isr, &u1)
292 isr.InvSqrt(&isr)
293
294 i1.Mul(&isr, &u1)
295 i2.Mul(&isr, &u2)
296
297 zInv.Mul(&i1, &i2)
298 zInv.Mul(&zInv, &p.T)
299
300 d.Mul(&zInv, &p.T)
301
302 nx.Mul(&p.Y, &feI)
303 ny.Mul(&p.X, &feI)
304 denInv.Mul(&feInvSqrtMinusDMinusOne, &i1)
305
306 b = 1 - d.IsNegativeI()
307 nx.ConditionalSet(&p.X, b)
308 ny.ConditionalSet(&p.Y, b)
309 denInv.ConditionalSet(&i2, b)
310
311 d.Mul(&nx, &zInv)
312 b = d.IsNegativeI()
313 d.Neg(&ny)
314 ny.ConditionalSet(&d, b)
315
316 s.sub(&p.Z, &ny)
317 s.Mul(&s, &denInv)
318
319 b = s.IsNegativeI()
320 d.Neg(&s)
321 s.ConditionalSet(&d, b)
322
323 s.BytesInto(buf)
324 return p
325 }
326
327 // Compute 5-bit window for the scalar s.
328 func computeScalarWindow5(s *[32]byte, w *[51]int8) {
329 for i := 0; i < 6; i++ {
330 w[8*i+0] = int8(s[5*i+0] & 31)
331 w[8*i+1] = int8((s[5*i+0] >> 5) & 31)
332 w[8*i+1] ^= int8((s[5*i+1] << 3) & 31)
333 w[8*i+2] = int8((s[5*i+1] >> 2) & 31)
334 w[8*i+3] = int8((s[5*i+1] >> 7) & 31)
335 w[8*i+3] ^= int8((s[5*i+2] << 1) & 31)
336 w[8*i+4] = int8((s[5*i+2] >> 4) & 31)
337 w[8*i+4] ^= int8((s[5*i+3] << 4) & 31)
338 w[8*i+5] = int8((s[5*i+3] >> 1) & 31)
339 w[8*i+6] = int8((s[5*i+3] >> 6) & 31)
340 w[8*i+6] ^= int8((s[5*i+4] << 2) & 31)
341 w[8*i+7] = int8((s[5*i+4] >> 3) & 31)
342 }
343 w[8*6+0] = int8(s[5*6+0] & 31)
344 w[8*6+1] = int8((s[5*6+0] >> 5) & 31)
345 w[8*6+1] ^= int8((s[5*6+1] << 3) & 31)
346 w[8*6+2] = int8((s[5*6+1] >> 2) & 31)
347
348 /* Making it signed */
349 var carry int8 = 0
350 for i := 0; i < 50; i++ {
351 w[i] += carry
352 w[i+1] += w[i] >> 5
353 w[i] &= 31
354 carry = w[i] >> 4
355 w[i] -= carry << 5
356 }
357 w[50] += carry
358 }
359
360 // Set p to s * q. Returns p.
361 func (p *ExtendedPoint) ScalarMult(q *ExtendedPoint, s *[32]byte) *ExtendedPoint {
362 // See eg. https://cryptojedi.org/peter/data/eccss-20130911b.pdf
363 var lut [17]ExtendedPoint
364 var t ExtendedPoint
365 var window [51]int8
366
367 // Precomputations.
368 computeScalarWindow5(s, &window)
369 lut[0].SetZero()
370 lut[1].Set(q)
371 for i := 2; i < 16; i += 2 {
372 lut[i].Double(&lut[i>>1])
373 lut[i+1].Add(&lut[i], q)
374 }
375 lut[16].Double(&lut[8])
376
377 // Compute!
378 p.SetZero()
379 for i := 50; i >= 0; i-- {
380 var pp ProjectivePoint
381 var cp CompletedPoint
382 cp.DoubleExtended(p)
383 for z := 0; z < 4; z++ {
384 pp.SetCompleted(&cp)
385 cp.DoubleProjective(&pp)
386 }
387 p.SetCompleted(&cp)
388
389 t.Set(&lut[0])
390 b := int32(window[i])
391 for j := 1; j <= 16; j++ {
392 c := equal15(b, int32(-j)) | equal15(b, int32(j))
393 t.ConditionalSet(&lut[j], c)
394 }
395 var v FieldElement
396 c := negative(b)
397 v.Neg(&t.X)
398 t.X.ConditionalSet(&v, c)
399 v.Neg(&t.T)
400 t.T.ConditionalSet(&v, c)
401
402 p.Add(p, &t)
403 }
404
405 return p
406 }
407
408 // Sets p to -q. Returns p.
409 func (p *ExtendedPoint) Neg(q *ExtendedPoint) *ExtendedPoint {
410 p.X.Neg(&q.X)
411 p.Y.Set(&q.Y)
412 p.Z.Set(&q.Z)
413 p.T.Neg(&q.T)
414 return p
415 }
416
417 // Returns 1 if p and q are in the same Ristretto equivalence class.
418 // Assumes p and q are both even.
419 func (p *ExtendedPoint) RistrettoEqualsI(q *ExtendedPoint) int32 {
420 var x1y2, x2y1, x1x2, y1y2 FieldElement
421 x1y2.Mul(&p.X, &q.Y)
422 x2y1.Mul(&q.X, &p.Y)
423 x1x2.Mul(&p.X, &q.X)
424 y1y2.Mul(&p.Y, &q.Y)
425 return 1 - ((1 - x1y2.EqualsI(&x2y1)) & (1 - x1x2.EqualsI(&y1y2)))
426 }
427
428 // WARNING This operation is not constant-time. Do not use for cryptography
429 //
430 // unless you're sure this is not an issue.
431 func (p *ExtendedPoint) String() string {
432 return fmt.Sprintf("ExtendedPoint(%v, %v, %v, %v; %v)",
433 p.X, p.Y, p.Z, p.T, hex.EncodeToString(p.Ristretto()))
434 }
435
436 // WARNING This operation is not constant-time. Do not use for cryptography
437 //
438 // unless you're sure this is not an issue.
439 func (p *CompletedPoint) String() string {
440 var ep ExtendedPoint
441 ep.SetCompleted(p)
442 return fmt.Sprintf("CompletedPoint(%v, %v, %v, %v; %v)",
443 p.X, p.Y, p.Z, p.T, hex.EncodeToString(ep.Ristretto()))
444 }