chiark / gitweb /
d16943c85a3604041aad717e23c0b52830a478e4
[secnet] / f25519.c
1 /* -*-c-*-
2  *
3  * Arithmetic modulo 2^255 - 19
4  *
5  * (c) 2017 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of secnet.
11  * See README for full list of copyright holders.
12  *
13  * secnet is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version d of the License, or
16  * (at your option) any later version.
17  *
18  * secnet is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * version 3 along with secnet; if not, see
25  * https://www.gnu.org/licenses/gpl.html.
26  *
27  * This file was originally part of Catacomb, but has been automatically
28  * modified for incorporation into secnet: see `import-catacomb-crypto'
29  * for details.
30  *
31  * Catacomb is free software; you can redistribute it and/or modify
32  * it under the terms of the GNU Library General Public License as
33  * published by the Free Software Foundation; either version 2 of the
34  * License, or (at your option) any later version.
35  *
36  * Catacomb is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39  * GNU Library General Public License for more details.
40  *
41  * You should have received a copy of the GNU Library General Public
42  * License along with Catacomb; if not, write to the Free
43  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
44  * MA 02111-1307, USA.
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include "f25519.h"
50
51 /*----- Basic setup -------------------------------------------------------*/
52
53 /* Elements x of GF(2^255 - 19) are represented by ten signed integers x_i: x
54  * = SUM_{0<=i<10} x_i 2^ceil(51i/2), mostly following Bernstein's original
55  * paper.
56  */
57
58 typedef  int32  piece;  typedef  int64  dblpiece;
59 typedef uint32 upiece;  typedef uint64 udblpiece;
60 #define P p26
61 #define PIECEWD(i) ((i)%2 ? 25 : 26)
62 #define NPIECE 10
63
64 #define M26 0x03ffffffu
65 #define M25 0x01ffffffu
66 #define B26 0x04000000u
67 #define B25 0x02000000u
68 #define B24 0x01000000u
69
70 #define PIECES(v) v##0, v##1, v##2, v##3, v##4, v##5, v##6, v##7, v##8, v##9
71 #define FETCH(v, w) do {                                                \
72   v##0 = (w)->P[0]; v##1 = (w)->P[1];                                   \
73   v##2 = (w)->P[2]; v##3 = (w)->P[3];                                   \
74   v##4 = (w)->P[4]; v##5 = (w)->P[5];                                   \
75   v##6 = (w)->P[6]; v##7 = (w)->P[7];                                   \
76   v##8 = (w)->P[8]; v##9 = (w)->P[9];                                   \
77 } while (0)
78 #define STASH(w, v) do {                                                \
79   (w)->P[0] = v##0; (w)->P[1] = v##1;                                   \
80   (w)->P[2] = v##2; (w)->P[3] = v##3;                                   \
81   (w)->P[4] = v##4; (w)->P[5] = v##5;                                   \
82   (w)->P[6] = v##6; (w)->P[7] = v##7;                                   \
83   (w)->P[8] = v##8; (w)->P[9] = v##9;                                   \
84 } while (0)
85
86 /*----- Debugging machinery -----------------------------------------------*/
87
88 #if defined(F25519_DEBUG)
89
90 #include <stdio.h>
91
92 #include "mp.h"
93 #include "mptext.h"
94
95 static mp *get_2p255m91(void)
96 {
97   mpw w19 = 19;
98   mp *p = MP_NEW, m19;
99
100   p = mp_setbit(p, MP_ZERO, 255);
101   mp_build(&m19, &w19, &w19 + 1);
102   p = mp_sub(p, p, &m19);
103   return (p);
104 }
105
106 DEF_FDUMP(fdump, piece, PIECEWD, NPIECE, 32, get_2p255m91())
107
108 #endif
109
110 /*----- Loading and storing -----------------------------------------------*/
111
112 /* --- @f25519_load@ --- *
113  *
114  * Arguments:   @f25519 *z@ = where to store the result
115  *              @const octet xv[32]@ = source to read
116  *
117  * Returns:     ---
118  *
119  * Use:         Reads an element of %$\gf{2^{255} - 19}$% in external
120  *              representation from @xv@ and stores it in @z@.
121  *
122  *              External representation is little-endian base-256.  Some
123  *              elements have multiple encodings, which are not produced by
124  *              correct software; use of noncanonical encodings is not an
125  *              error, and toleration of them is considered a performance
126  *              feature.
127  */
128
129 void f25519_load(f25519 *z, const octet xv[32])
130 {
131
132   uint32 xw0 = LOAD32_L(xv +  0), xw1 = LOAD32_L(xv +  4),
133          xw2 = LOAD32_L(xv +  8), xw3 = LOAD32_L(xv + 12),
134          xw4 = LOAD32_L(xv + 16), xw5 = LOAD32_L(xv + 20),
135          xw6 = LOAD32_L(xv + 24), xw7 = LOAD32_L(xv + 28);
136   piece PIECES(x), b, c;
137
138   /* First, split the 32-bit words into the irregularly-sized pieces we need
139    * for the field representation.  These pieces are all positive.  We'll do
140    * the sign correction afterwards.
141    *
142    * It may be that the top bit of the input is set.  Avoid trouble by
143    * folding that back round into the bottom piece of the representation.
144    *
145    * Here, we briefly have 0 <= x_0 < 2^26 + 19, but will resolve this later.
146    * Otherwise, we have 0 <= x_{2i} < 2^26, and 0 <= x_{2i+1} < 2^25.
147    */
148   x0 = ((xw0 <<  0)&0x03ffffff) + 19*((xw7 >> 31)&0x00000001);
149   x1 = ((xw1 <<  6)&0x01ffffc0) |    ((xw0 >> 26)&0x0000003f);
150   x2 = ((xw2 << 13)&0x03ffe000) |    ((xw1 >> 19)&0x00001fff);
151   x3 = ((xw3 << 19)&0x01f80000) |    ((xw2 >> 13)&0x0007ffff);
152   x4 =                               ((xw3 >>  6)&0x03ffffff);
153   x5 =  (xw4 <<  0)&0x01ffffff;
154   x6 = ((xw5 <<  7)&0x03ffff80) |    ((xw4 >> 25)&0x0000007f);
155   x7 = ((xw6 << 13)&0x01ffe000) |    ((xw5 >> 19)&0x00001fff);
156   x8 = ((xw7 << 20)&0x03f00000) |    ((xw6 >> 12)&0x000fffff);
157   x9 =                               ((xw7 >>  6)&0x01ffffff);
158
159   /* Next, we convert these pieces into a roughly balanced signed
160    * representation.  For each piece, check to see if its top bit is set.  If
161    * it is, then lend a bit to the next piece over.  For x_9, this needs to
162    * be carried around, which is a little fiddly.  In particular, we delay
163    * the carry until after all of the pieces have been balanced.  If we don't
164    * do this, then we have to do a more expensive test for nonzeroness to
165    * decide whether to lend a bit leftwards rather than just testing a single
166    * bit.
167    *
168    * This fixes up the anomalous size of x_0: the loan of a bit becomes an
169    * actual carry if x_0 >= 2^26.  By the end, then, we have:
170    *
171    *             { 2^25         if i even
172    *    |x_i| <= {
173    *             { 2^24         if i odd
174    *
175    * Note that we don't try for a canonical representation here: both upper
176    * and lower bounds are achievable.
177    *
178    * All of the x_i at this point are positive, so we don't need to do
179    * anything wierd when masking them.
180    */
181   b = x9&B24; c   = 19&((b >> 19) - (b >> 24)); x9 -= b << 1;
182   b = x8&B25; x9 +=      b >> 25;               x8 -= b << 1;
183   b = x7&B24; x8 +=      b >> 24;               x7 -= b << 1;
184   b = x6&B25; x7 +=      b >> 25;               x6 -= b << 1;
185   b = x5&B24; x6 +=      b >> 24;               x5 -= b << 1;
186   b = x4&B25; x5 +=      b >> 25;               x4 -= b << 1;
187   b = x3&B24; x4 +=      b >> 24;               x3 -= b << 1;
188   b = x2&B25; x3 +=      b >> 25;               x2 -= b << 1;
189   b = x1&B24; x2 +=      b >> 24;               x1 -= b << 1;
190   b = x0&B25; x1 +=     (b >> 25) + (x0 >> 26); x0 = (x0&M26) - (b << 1);
191               x0 +=      c;
192
193   /* And with that, we're done. */
194   STASH(z, x);
195 }
196
197 /* --- @f25519_store@ --- *
198  *
199  * Arguments:   @octet zv[32]@ = where to write the result
200  *              @const f25519 *x@ = the field element to write
201  *
202  * Returns:     ---
203  *
204  * Use:         Stores a field element in the given octet vector in external
205  *              representation.  A canonical encoding is always stored, so,
206  *              in particular, the top bit of @xv[31]@ is always left clear.
207  */
208
209 void f25519_store(octet zv[32], const f25519 *x)
210 {
211
212   piece PIECES(x), PIECES(y), c, d;
213   uint32 zw0, zw1, zw2, zw3, zw4, zw5, zw6, zw7;
214   mask32 m;
215
216   FETCH(x, x);
217
218   /* First, propagate the carries throughout the pieces.  By the end of this,
219    * we'll have all of the pieces canonically sized and positive, and maybe
220    * there'll be (signed) carry out.  The carry c is in { -1, 0, +1 }, and
221    * the remaining value will be in the half-open interval [0, 2^255).  The
222    * whole represented value is then x + 2^255 c.
223    *
224    * It's worth paying careful attention to the bounds.  We assume that we
225    * start out with |x_i| <= 2^30.  We start by cutting off and reducing the
226    * carry c_9 from the topmost piece, x_9.  This leaves 0 <= x_9 < 2^25; and
227    * we'll have |c_9| <= 2^5.  We multiply this by 19 and we'll add it onto
228    * x_0 and propagate the carries: but what bounds can we calculate on x
229    * before this?
230    *
231    * Let o_i = floor(51 i/2).  We have X_i = SUM_{0<=j<i} x_j 2^{o_i}, so
232    * x = X_10.  We see, inductively, that |X_i| < 2^{31+o_{i-1}}: X_0 = 0;
233    * |x_i| <= 2^30; and |X_{i+1}| = |X_i + x_i 2^{o_i}| <= |X_i| + 2^{30+o_i}
234    * < 2^{31+o_i}.  Then x = X_9 + 2^230 x_9, and we have better bounds for
235    * x_9, so
236    *
237    *    -2^235 < x + 19 c_9 < 2^255 + 2^235
238    *
239    * Here, the x_i are signed, so we must be cautious about bithacking them.
240    */
241               c = ASR(piece, x9, 25); x9 = (upiece)x9&M25;
242   x0 += 19*c; c = ASR(piece, x0, 26); x0 = (upiece)x0&M26;
243   x1 +=    c; c = ASR(piece, x1, 25); x1 = (upiece)x1&M25;
244   x2 +=    c; c = ASR(piece, x2, 26); x2 = (upiece)x2&M26;
245   x3 +=    c; c = ASR(piece, x3, 25); x3 = (upiece)x3&M25;
246   x4 +=    c; c = ASR(piece, x4, 26); x4 = (upiece)x4&M26;
247   x5 +=    c; c = ASR(piece, x5, 25); x5 = (upiece)x5&M25;
248   x6 +=    c; c = ASR(piece, x6, 26); x6 = (upiece)x6&M26;
249   x7 +=    c; c = ASR(piece, x7, 25); x7 = (upiece)x7&M25;
250   x8 +=    c; c = ASR(piece, x8, 26); x8 = (upiece)x8&M26;
251   x9 +=    c; c = ASR(piece, x9, 25); x9 = (upiece)x9&M25;
252
253   /* Now we have a slightly fiddly job to do.  If c = +1, or if c = 0 and
254    * x >= 2^255 - 19, then we should subtract 2^255 - 19 from the whole
255    * value; if c = -1 then we should add 2^255 - 19; and otherwise we should
256    * do nothing.
257    *
258    * But conditional behaviour is bad, m'kay.  So here's what we do instead.
259    *
260    * The first job is to sort out what we wanted to do.  If c = -1 then we
261    * want to (a) invert the constant addend and (b) feed in a carry-in;
262    * otherwise, we don't.
263    */
264   m = SIGN(c);
265   d = m&1;
266
267   /* Now do the addition/subtraction.  Remember that all of the x_i are
268    * nonnegative, so shifting and masking are safe and easy.
269    */
270   d += x0 + (19 ^ (M26&m)); y0 = d&M26; d >>= 26;
271   d += x1 +       (M25&m);  y1 = d&M25; d >>= 25;
272   d += x2 +       (M26&m);  y2 = d&M26; d >>= 26;
273   d += x3 +       (M25&m);  y3 = d&M25; d >>= 25;
274   d += x4 +       (M26&m);  y4 = d&M26; d >>= 26;
275   d += x5 +       (M25&m);  y5 = d&M25; d >>= 25;
276   d += x6 +       (M26&m);  y6 = d&M26; d >>= 26;
277   d += x7 +       (M25&m);  y7 = d&M25; d >>= 25;
278   d += x8 +       (M26&m);  y8 = d&M26; d >>= 26;
279   d += x9 +       (M25&m);  y9 = d&M25; d >>= 25;
280
281   /* The final carry-out is in d; since we only did addition, and the x_i are
282    * nonnegative, then d is in { 0, 1 }.  We want to keep y, rather than x,
283    * if (a) c /= 0 (in which case we know that the old value was
284    * unsatisfactory), or (b) if d = 1 (in which case, if c = 0, we know that
285    * the subtraction didn't cause a borrow, so we must be in the case where
286    * 2^255 - 19 <= x < 2^255).
287    */
288   m = NONZEROP(c) | ~NONZEROP(d - 1);
289   x0 = (y0&m) | (x0&~m); x1 = (y1&m) | (x1&~m);
290   x2 = (y2&m) | (x2&~m); x3 = (y3&m) | (x3&~m);
291   x4 = (y4&m) | (x4&~m); x5 = (y5&m) | (x5&~m);
292   x6 = (y6&m) | (x6&~m); x7 = (y7&m) | (x7&~m);
293   x8 = (y8&m) | (x8&~m); x9 = (y9&m) | (x9&~m);
294
295   /* Extract 32-bit words from the value. */
296   zw0 = ((x0 >>  0)&0x03ffffff) | (((uint32)x1 << 26)&0xfc000000);
297   zw1 = ((x1 >>  6)&0x0007ffff) | (((uint32)x2 << 19)&0xfff80000);
298   zw2 = ((x2 >> 13)&0x00001fff) | (((uint32)x3 << 13)&0xffffe000);
299   zw3 = ((x3 >> 19)&0x0000003f) | (((uint32)x4 <<  6)&0xffffffc0);
300   zw4 = ((x5 >>  0)&0x01ffffff) | (((uint32)x6 << 25)&0xfe000000);
301   zw5 = ((x6 >>  7)&0x0007ffff) | (((uint32)x7 << 19)&0xfff80000);
302   zw6 = ((x7 >> 13)&0x00000fff) | (((uint32)x8 << 12)&0xfffff000);
303   zw7 = ((x8 >> 20)&0x0000003f) | (((uint32)x9 <<  6)&0x7fffffc0);
304
305   /* Store the result as an octet string. */
306   STORE32_L(zv +  0, zw0); STORE32_L(zv +  4, zw1);
307   STORE32_L(zv +  8, zw2); STORE32_L(zv + 12, zw3);
308   STORE32_L(zv + 16, zw4); STORE32_L(zv + 20, zw5);
309   STORE32_L(zv + 24, zw6); STORE32_L(zv + 28, zw7);
310 }
311
312 /* --- @f25519_set@ --- *
313  *
314  * Arguments:   @f25519 *z@ = where to write the result
315  *              @int a@ = a small-ish constant
316  *
317  * Returns:     ---
318  *
319  * Use:         Sets @z@ to equal @a@.
320  */
321
322 void f25519_set(f25519 *x, int a)
323 {
324   unsigned i;
325
326   x->P[0] = a;
327   for (i = 1; i < NPIECE; i++) x->P[i] = 0;
328 }
329
330 /*----- Basic arithmetic --------------------------------------------------*/
331
332 /* --- @f25519_add@ --- *
333  *
334  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
335  *              @const f25519 *x, *y@ = two operands
336  *
337  * Returns:     ---
338  *
339  * Use:         Set @z@ to the sum %$x + y$%.
340  */
341
342 void f25519_add(f25519 *z, const f25519 *x, const f25519 *y)
343 {
344   z->P[0] = x->P[0] + y->P[0]; z->P[1] = x->P[1] + y->P[1];
345   z->P[2] = x->P[2] + y->P[2]; z->P[3] = x->P[3] + y->P[3];
346   z->P[4] = x->P[4] + y->P[4]; z->P[5] = x->P[5] + y->P[5];
347   z->P[6] = x->P[6] + y->P[6]; z->P[7] = x->P[7] + y->P[7];
348   z->P[8] = x->P[8] + y->P[8]; z->P[9] = x->P[9] + y->P[9];
349 }
350
351 /* --- @f25519_sub@ --- *
352  *
353  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
354  *              @const f25519 *x, *y@ = two operands
355  *
356  * Returns:     ---
357  *
358  * Use:         Set @z@ to the difference %$x - y$%.
359  */
360
361 void f25519_sub(f25519 *z, const f25519 *x, const f25519 *y)
362 {
363   z->P[0] = x->P[0] - y->P[0]; z->P[1] = x->P[1] - y->P[1];
364   z->P[2] = x->P[2] - y->P[2]; z->P[3] = x->P[3] - y->P[3];
365   z->P[4] = x->P[4] - y->P[4]; z->P[5] = x->P[5] - y->P[5];
366   z->P[6] = x->P[6] - y->P[6]; z->P[7] = x->P[7] - y->P[7];
367   z->P[8] = x->P[8] - y->P[8]; z->P[9] = x->P[9] - y->P[9];
368 }
369
370 /* --- @f25519_neg@ --- *
371  *
372  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@)
373  *              @const f25519 *x@ = an operand
374  *
375  * Returns:     ---
376  *
377  * Use:         Set @z = -x@.
378  */
379
380 void f25519_neg(f25519 *z, const f25519 *x)
381 {
382   z->P[0] = -x->P[0]; z->P[1] = -x->P[1];
383   z->P[2] = -x->P[2]; z->P[3] = -x->P[3];
384   z->P[4] = -x->P[4]; z->P[5] = -x->P[5];
385   z->P[6] = -x->P[6]; z->P[7] = -x->P[7];
386   z->P[8] = -x->P[8]; z->P[9] = -x->P[9];
387 }
388
389 /*----- Constant-time utilities -------------------------------------------*/
390
391 /* --- @f25519_pick2@ --- *
392  *
393  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
394  *              @const f25519 *x, *y@ = two operands
395  *              @uint32 m@ = a mask
396  *
397  * Returns:     ---
398  *
399  * Use:         If @m@ is zero, set @z = y@; if @m@ is all-bits-set, then set
400  *              @z = x@.  If @m@ has some other value, then scramble @z@ in
401  *              an unhelpful way.
402  */
403
404 void f25519_pick2(f25519 *z, const f25519 *x, const f25519 *y, uint32 m)
405 {
406   mask32 mm = FIX_MASK32(m);
407
408   z->P[0] = PICK2(x->P[0], y->P[0], mm);
409   z->P[1] = PICK2(x->P[1], y->P[1], mm);
410   z->P[2] = PICK2(x->P[2], y->P[2], mm);
411   z->P[3] = PICK2(x->P[3], y->P[3], mm);
412   z->P[4] = PICK2(x->P[4], y->P[4], mm);
413   z->P[5] = PICK2(x->P[5], y->P[5], mm);
414   z->P[6] = PICK2(x->P[6], y->P[6], mm);
415   z->P[7] = PICK2(x->P[7], y->P[7], mm);
416   z->P[8] = PICK2(x->P[8], y->P[8], mm);
417   z->P[9] = PICK2(x->P[9], y->P[9], mm);
418 }
419
420 /* --- @f25519_pickn@ --- *
421  *
422  * Arguments:   @f25519 *z@ = where to put the result
423  *              @const f25519 *v@ = a table of entries
424  *              @size_t n@ = the number of entries in @v@
425  *              @size_t i@ = an index
426  *
427  * Returns:     ---
428  *
429  * Use:         If @0 <= i < n < 32@ then set @z = v[i]@.  If @n >= 32@ then
430  *              do something unhelpful; otherwise, if @i >= n@ then set @z@
431  *              to zero.
432  */
433
434 void f25519_pickn(f25519 *z, const f25519 *v, size_t n, size_t i)
435 {
436   uint32 b = (uint32)1 << (31 - i);
437   mask32 m;
438
439   z->P[0] = z->P[1] = z->P[2] = z->P[3] = z->P[4] =
440     z->P[5] = z->P[6] = z->P[7] = z->P[8] = z->P[9] = 0;
441   while (n--) {
442     m = SIGN(b);
443     CONDPICK(z->P[0], v->P[0], m);
444     CONDPICK(z->P[1], v->P[1], m);
445     CONDPICK(z->P[2], v->P[2], m);
446     CONDPICK(z->P[3], v->P[3], m);
447     CONDPICK(z->P[4], v->P[4], m);
448     CONDPICK(z->P[5], v->P[5], m);
449     CONDPICK(z->P[6], v->P[6], m);
450     CONDPICK(z->P[7], v->P[7], m);
451     CONDPICK(z->P[8], v->P[8], m);
452     CONDPICK(z->P[9], v->P[9], m);
453     v++; b <<= 1;
454   }
455 }
456
457 /* --- @f25519_condswap@ --- *
458  *
459  * Arguments:   @f25519 *x, *y@ = two operands
460  *              @uint32 m@ = a mask
461  *
462  * Returns:     ---
463  *
464  * Use:         If @m@ is zero, do nothing; if @m@ is all-bits-set, then
465  *              exchange @x@ and @y@.  If @m@ has some other value, then
466  *              scramble @x@ and @y@ in an unhelpful way.
467  */
468
469 void f25519_condswap(f25519 *x, f25519 *y, uint32 m)
470 {
471   mask32 mm = FIX_MASK32(m);
472
473   CONDSWAP(x->P[0], y->P[0], mm);
474   CONDSWAP(x->P[1], y->P[1], mm);
475   CONDSWAP(x->P[2], y->P[2], mm);
476   CONDSWAP(x->P[3], y->P[3], mm);
477   CONDSWAP(x->P[4], y->P[4], mm);
478   CONDSWAP(x->P[5], y->P[5], mm);
479   CONDSWAP(x->P[6], y->P[6], mm);
480   CONDSWAP(x->P[7], y->P[7], mm);
481   CONDSWAP(x->P[8], y->P[8], mm);
482   CONDSWAP(x->P[9], y->P[9], mm);
483 }
484
485 /* --- @f25519_condneg@ --- *
486  *
487  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@)
488  *              @const f25519 *x@ = an operand
489  *              @uint32 m@ = a mask
490  *
491  * Returns:     ---
492  *
493  * Use:         If @m@ is zero, set @z = x@; if @m@ is all-bits-set, then set
494  *              @z = -x@.  If @m@ has some other value then scramble @z@ in
495  *              an unhelpful way.
496  */
497
498 void f25519_condneg(f25519 *z, const f25519 *x, uint32 m)
499 {
500   mask32 m_xor = FIX_MASK32(m);
501   piece m_add = m&1;
502 # define CONDNEG(x) (((x) ^ m_xor) + m_add)
503
504   z->P[0] = CONDNEG(x->P[0]);
505   z->P[1] = CONDNEG(x->P[1]);
506   z->P[2] = CONDNEG(x->P[2]);
507   z->P[3] = CONDNEG(x->P[3]);
508   z->P[4] = CONDNEG(x->P[4]);
509   z->P[5] = CONDNEG(x->P[5]);
510   z->P[6] = CONDNEG(x->P[6]);
511   z->P[7] = CONDNEG(x->P[7]);
512   z->P[8] = CONDNEG(x->P[8]);
513   z->P[9] = CONDNEG(x->P[9]);
514
515 #undef CONDNEG
516 }
517
518 /*----- Multiplication ----------------------------------------------------*/
519
520 /* Let B = 2^63 - 1 be the largest value such that +B and -B can be
521  * represented in a double-precision piece.  On entry, it must be the case
522  * that |X_i| <= M <= B - 2^25 for some M.  If this is the case, then, on
523  * exit, we will have |Z_i| <= 2^25 + 19 M/2^25.
524  */
525 #define CARRYSTEP(z, x, m, b, f, xx, n) do {                            \
526   (z) = (dblpiece)((udblpiece)(x)&(m)) - (b) +                          \
527     (f)*ASR(dblpiece, (xx), (n));                                       \
528 } while (0)
529 #define CARRY_REDUCE(z, x) do {                                         \
530   dblpiece PIECES(_t);                                                  \
531                                                                         \
532   /* Bias the input pieces.  This keeps the carries and so on centred   \
533    * around zero rather than biased positive.                           \
534    */                                                                   \
535   _t0 = (x##0) + B25; _t1 = (x##1) + B24;                               \
536   _t2 = (x##2) + B25; _t3 = (x##3) + B24;                               \
537   _t4 = (x##4) + B25; _t5 = (x##5) + B24;                               \
538   _t6 = (x##6) + B25; _t7 = (x##7) + B24;                               \
539   _t8 = (x##8) + B25; _t9 = (x##9) + B24;                               \
540                                                                         \
541   /* Calculate the reduced pieces.  Careful with the bithacking. */     \
542   CARRYSTEP(z##0, _t0, M26, B25, 19, _t9, 25);                          \
543   CARRYSTEP(z##1, _t1, M25, B24,  1, _t0, 26);                          \
544   CARRYSTEP(z##2, _t2, M26, B25,  1, _t1, 25);                          \
545   CARRYSTEP(z##3, _t3, M25, B24,  1, _t2, 26);                          \
546   CARRYSTEP(z##4, _t4, M26, B25,  1, _t3, 25);                          \
547   CARRYSTEP(z##5, _t5, M25, B24,  1, _t4, 26);                          \
548   CARRYSTEP(z##6, _t6, M26, B25,  1, _t5, 25);                          \
549   CARRYSTEP(z##7, _t7, M25, B24,  1, _t6, 26);                          \
550   CARRYSTEP(z##8, _t8, M26, B25,  1, _t7, 25);                          \
551   CARRYSTEP(z##9, _t9, M25, B24,  1, _t8, 26);                          \
552 } while (0)
553
554 /* --- @f25519_mulconst@ --- *
555  *
556  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@)
557  *              @const f25519 *x@ = an operand
558  *              @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
559  *
560  * Returns:     ---
561  *
562  * Use:         Set @z@ to the product %$a x$%.
563  */
564
565 void f25519_mulconst(f25519 *z, const f25519 *x, long a)
566 {
567
568   piece PIECES(x);
569   dblpiece PIECES(z), aa = a;
570
571   FETCH(x, x);
572
573   /* Suppose that |x_i| <= 2^27, and |a| <= 2^23.  Then we'll have
574    * |z_i| <= 2^50.
575    */
576   z0 = aa*x0; z1 = aa*x1; z2 = aa*x2; z3 = aa*x3; z4 = aa*x4;
577   z5 = aa*x5; z6 = aa*x6; z7 = aa*x7; z8 = aa*x8; z9 = aa*x9;
578
579   /* Following `CARRY_REDUCE', we'll have |z_i| <= 2^26. */
580   CARRY_REDUCE(z, z);
581   STASH(z, z);
582 }
583
584 /* --- @f25519_mul@ --- *
585  *
586  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
587  *              @const f25519 *x, *y@ = two operands
588  *
589  * Returns:     ---
590  *
591  * Use:         Set @z@ to the product %$x y$%.
592  */
593
594 void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y)
595 {
596
597   piece PIECES(x), PIECES(y);
598   dblpiece PIECES(z);
599   unsigned i;
600
601   FETCH(x, x); FETCH(y, y);
602
603   /* Suppose that |x_i|, |y_i| <= 2^27.  Then we'll have
604    *
605    *    |z_0| <= 267*2^54
606    *    |z_1| <= 154*2^54
607    *    |z_2| <= 213*2^54
608    *    |z_3| <= 118*2^54
609    *    |z_4| <= 159*2^54
610    *    |z_5| <=  82*2^54
611    *    |z_6| <= 105*2^54
612    *    |z_7| <=  46*2^54
613    *    |z_8| <=  51*2^54
614    *    |z_9| <=  10*2^54
615    *
616    * all of which are less than 2^63 - 2^25.
617    */
618
619 #define M(a, b) ((dblpiece)(a)*(b))
620   z0 =     M(x0, y0) +
621        19*(M(x2, y8) + M(x4, y6) + M(x6, y4) + M(x8, y2)) +
622        38*(M(x1, y9) + M(x3, y7) + M(x5, y5) + M(x7, y3) + M(x9, y1));
623   z1 =     M(x0, y1) + M(x1, y0) +
624        19*(M(x2, y9) + M(x3, y8) + M(x4, y7) + M(x5, y6) +
625            M(x6, y5) + M(x7, y4) + M(x8, y3) + M(x9, y2));
626   z2 =     M(x0, y2) + M(x2, y0) +
627         2* M(x1, y1) +
628        19*(M(x4, y8) + M(x6, y6) + M(x8, y4)) +
629        38*(M(x3, y9) + M(x5, y7) + M(x7, y5) + M(x9, y3));
630   z3 =     M(x0, y3) + M(x1, y2) + M(x2, y1) + M(x3, y0) +
631        19*(M(x4, y9) + M(x5, y8) + M(x6, y7) +
632            M(x7, y6) + M(x8, y5) + M(x9, y4));
633   z4 =     M(x0, y4) + M(x2, y2) + M(x4, y0) +
634         2*(M(x1, y3) + M(x3, y1)) +
635        19*(M(x6, y8) + M(x8, y6)) +
636        38*(M(x5, y9) + M(x7, y7) + M(x9, y5));
637   z5 =     M(x0, y5) + M(x1, y4) + M(x2, y3) +
638            M(x3, y2) + M(x4, y1) + M(x5, y0) +
639        19*(M(x6, y9) + M(x7, y8) + M(x8, y7) + M(x9, y6));
640   z6 =     M(x0, y6) + M(x2, y4) + M(x4, y2) + M(x6, y0) +
641         2*(M(x1, y5) + M(x3, y3) + M(x5, y1)) +
642        19* M(x8, y8) +
643        38*(M(x7, y9) + M(x9, y7));
644   z7 =     M(x0, y7) + M(x1, y6) + M(x2, y5) + M(x3, y4) +
645            M(x4, y3) + M(x5, y2) + M(x6, y1) + M(x7, y0) +
646        19*(M(x8, y9) + M(x9, y8));
647   z8 =     M(x0, y8) + M(x2, y6) + M(x4, y4) + M(x6, y2) + M(x8, y0) +
648         2*(M(x1, y7) + M(x3, y5) + M(x5, y3) + M(x7, y1)) +
649        38* M(x9, y9);
650   z9 =     M(x0, y9) + M(x1, y8) + M(x2, y7) + M(x3, y6) + M(x4, y5) +
651            M(x5, y4) + M(x6, y3) + M(x7, y2) + M(x8, y1) + M(x9, y0);
652 #undef M
653
654   /* From above, we have |z_i| <= 2^63 - 2^25.  A pass of `CARRY_REDUCE' will
655    * leave |z_i| <= 2^38 + 2^25; and a second pass will leave |z_i| <= 2^25 +
656    * 2^13, which is comfortable for an addition prior to the next
657    * multiplication.
658    */
659   for (i = 0; i < 2; i++) CARRY_REDUCE(z, z);
660   STASH(z, z);
661 }
662
663 /* --- @f25519_sqr@ --- *
664  *
665  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
666  *              @const f25519 *x@ = an operand
667  *
668  * Returns:     ---
669  *
670  * Use:         Set @z@ to the square %$x^2$%.
671  */
672
673 void f25519_sqr(f25519 *z, const f25519 *x)
674 {
675
676   piece PIECES(x);
677   dblpiece PIECES(z);
678   unsigned i;
679
680   FETCH(x, x);
681
682   /* See `f25519_mul' for bounds. */
683
684 #define M(a, b) ((dblpiece)(a)*(b))
685   z0 =     M(x0, x0) +
686        38*(M(x2, x8) + M(x4, x6) + M(x5, x5)) +
687        76*(M(x1, x9) + M(x3, x7));
688   z1 =  2* M(x0, x1) +
689        38*(M(x2, x9) + M(x3, x8) + M(x4, x7) + M(x5, x6));
690   z2 =  2*(M(x0, x2) + M(x1, x1)) +
691        19* M(x6, x6) +
692        38* M(x4, x8) +
693        76*(M(x3, x9) + M(x5, x7));
694   z3 =  2*(M(x0, x3) + M(x1, x2)) +
695        38*(M(x4, x9) + M(x5, x8) + M(x6, x7));
696   z4 =     M(x2, x2) +
697         2* M(x0, x4) +
698         4* M(x1, x3) +
699        38*(M(x6, x8) + M(x7, x7)) +
700        76* M(x5, x9);
701   z5 =  2*(M(x0, x5) + M(x1, x4) + M(x2, x3)) +
702        38*(M(x6, x9) + M(x7, x8));
703   z6 =  2*(M(x0, x6) + M(x2, x4) + M(x3, x3)) +
704         4* M(x1, x5) +
705        19* M(x8, x8) +
706        76* M(x7, x9);
707   z7 =  2*(M(x0, x7) + M(x1, x6) + M(x2, x5) + M(x3, x4)) +
708        38* M(x8, x9);
709   z8 =     M(x4, x4) +
710         2*(M(x0, x8) + M(x2, x6)) +
711         4*(M(x1, x7) + M(x3, x5)) +
712        38* M(x9, x9);
713   z9 =  2*(M(x0, x9) + M(x1, x8) + M(x2, x7) + M(x3, x6) + M(x4, x5));
714 #undef M
715
716   /* See `f25519_mul' for details. */
717   for (i = 0; i < 2; i++) CARRY_REDUCE(z, z);
718   STASH(z, z);
719 }
720
721 /*----- More complicated things -------------------------------------------*/
722
723 /* --- @f25519_inv@ --- *
724  *
725  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@)
726  *              @const f25519 *x@ = an operand
727  *
728  * Returns:     ---
729  *
730  * Use:         Stores in @z@ the multiplicative inverse %$x^{-1}$%.  If
731  *              %$x = 0$% then @z@ is set to zero.  This is considered a
732  *              feature.
733  */
734
735 void f25519_inv(f25519 *z, const f25519 *x)
736 {
737   f25519 t, u, t2, t11, t2p10m1, t2p50m1;
738   unsigned i;
739
740 #define SQRN(z, x, n) do {                                              \
741   f25519_sqr((z), (x));                                                 \
742   for (i = 1; i < (n); i++) f25519_sqr((z), (z));                       \
743 } while (0)
744
745   /* Calculate x^-1 = x^(p - 2) = x^(2^255 - 21), which also handles x = 0 as
746    * intended.  The addition chain here is from Bernstein's implementation; I
747    * couldn't find a better one.
748    */                                   /* step | value */
749   f25519_sqr(&t2, x);                   /*    1 | 2 */
750   SQRN(&u, &t2, 2);                     /*    3 | 8 */
751   f25519_mul(&t, &u, x);                /*    4 | 9 */
752   f25519_mul(&t11, &t, &t2);            /*    5 | 11 = 2^5 - 21 */
753   f25519_sqr(&u, &t11);                 /*    6 | 22 */
754   f25519_mul(&t, &t, &u);               /*    7 | 31 = 2^5 - 1 */
755   SQRN(&u, &t, 5);                      /*   12 | 2^10 - 2^5 */
756   f25519_mul(&t2p10m1, &t, &u);         /*   13 | 2^10 - 1 */
757   SQRN(&u, &t2p10m1, 10);               /*   23 | 2^20 - 2^10 */
758   f25519_mul(&t, &t2p10m1, &u);         /*   24 | 2^20 - 1 */
759   SQRN(&u, &t, 20);                     /*   44 | 2^40 - 2^20 */
760   f25519_mul(&t, &t, &u);               /*   45 | 2^40 - 1 */
761   SQRN(&u, &t, 10);                     /*   55 | 2^50 - 2^10 */
762   f25519_mul(&t2p50m1, &t2p10m1, &u);   /*   56 | 2^50 - 1 */
763   SQRN(&u, &t2p50m1, 50);               /*  106 | 2^100 - 2^50 */
764   f25519_mul(&t, &t2p50m1, &u);         /*  107 | 2^100 - 1 */
765   SQRN(&u, &t, 100);                    /*  207 | 2^200 - 2^100 */
766   f25519_mul(&t, &t, &u);               /*  208 | 2^200 - 1 */
767   SQRN(&u, &t, 50);                     /*  258 | 2^250 - 2^50 */
768   f25519_mul(&t, &t2p50m1, &u);         /*  259 | 2^250 - 1 */
769   SQRN(&u, &t, 5);                      /*  264 | 2^255 - 2^5 */
770   f25519_mul(z, &u, &t11);              /*  265 | 2^255 - 21 */
771
772 #undef SQRN
773 }
774
775 /* --- @f25519_quosqrt@ --- *
776  *
777  * Arguments:   @f25519 *z@ = where to put the result (may alias @x@ or @y@)
778  *              @const f25519 *x, *y@ = two operands
779  *
780  * Returns:     Zero if successful, @-1@ if %$x/y$% is not a square.
781  *
782  * Use:         Stores in @z@ the one of the square roots %$\pm\sqrt{x/y}$%.
783  *              If %$x = y = 0% then the result is zero; if %$y = 0$% but %$x
784  *              \ne 0$% then the operation fails.  If you wanted a specific
785  *              square root then you'll have to pick it yourself.
786  */
787
788 static const piece sqrtm1_pieces[NPIECE] = {
789   -32595792,  -7943725,   9377950,   3500415,  12389472,
790     -272473, -25146209,  -2005654,    326686,  11406482
791 };
792 #define SQRTM1 ((const f25519 *)sqrtm1_pieces)
793
794 int f25519_quosqrt(f25519 *z, const f25519 *x, const f25519 *y)
795 {
796   f25519 t, u, w, beta, xy3, t2p50m1;
797   octet xb[32], b0[32], b1[32];
798   int32 rc = -1;
799   mask32 m;
800   unsigned i;
801
802 #define SQRN(z, x, n) do {                                              \
803   f25519_sqr((z), (x));                                                 \
804   for (i = 1; i < (n); i++) f25519_sqr((z), (z));                       \
805 } while (0)
806
807   /* This is a bit tricky; the algorithm is from Bernstein, Duif, Lange,
808    * Schwabe, and Yang, `High-speed high-security signatures', 2011-09-26,
809    * https://ed25519.cr.yp.to/ed25519-20110926.pdf.
810    *
811    * First of all, a complicated exponentation.  The addition chain here is
812    * mine.  We start with some preliminary values.
813    */                                   /* step | value */
814   SQRN(&u, y, 1);                       /*    1 | 0, 2 */
815   f25519_mul(&t, &u, y);                /*    2 | 0, 3 */
816   f25519_mul(&xy3, &t, x);              /*    3 | 1, 3 */
817   SQRN(&u, &u, 1);                      /*    4 | 0, 4 */
818   f25519_mul(&w, &u, &xy3);             /*    5 | 1, 7 */
819
820   /* And now we calculate w^((p - 5)/8) = w^(252 - 3). */
821   SQRN(&u, &w, 1);                      /*    6 | 2 */
822   f25519_mul(&t, &w, &u);               /*    7 | 3 */
823   SQRN(&u, &t, 1);                      /*    8 | 6 */
824   f25519_mul(&t, &u, &w);               /*    9 | 7 */
825   SQRN(&u, &t, 3);                      /*   12 | 56 */
826   f25519_mul(&t, &t, &u);               /*   13 | 63 = 2^6 - 1 */
827   SQRN(&u, &t, 6);                      /*   19 | 2^12 - 2^6 */
828   f25519_mul(&t, &t, &u);               /*   20 | 2^12 - 1 */
829   SQRN(&u, &t, 12);                     /*   32 | 2^24 - 2^12 */
830   f25519_mul(&t, &t, &u);               /*   33 | 2^24 - 1 */
831   SQRN(&u, &t, 1);                      /*   34 | 2^25 - 2 */
832   f25519_mul(&t, &u, &w);               /*   35 | 2^25 - 1 */
833   SQRN(&u, &t, 25);                     /*   60 | 2^50 - 2^25 */
834   f25519_mul(&t2p50m1, &t, &u);         /*   61 | 2^50 - 1 */
835   SQRN(&u, &t2p50m1, 50);               /*  111 | 2^100 - 2^50 */
836   f25519_mul(&t, &t2p50m1, &u);         /*  112 | 2^100 - 1 */
837   SQRN(&u, &t, 100);                    /*  212 | 2^200 - 2^100 */
838   f25519_mul(&t, &t, &u);               /*  213 | 2^200 - 1 */
839   SQRN(&u, &t, 50);                     /*  263 | 2^250 - 2^50 */
840   f25519_mul(&t, &t2p50m1, &u);         /*  264 | 2^250 - 1 */
841   SQRN(&u, &t, 2);                      /*  266 | 2^252 - 4 */
842   f25519_mul(&t, &u, &w);               /*  267 | 2^252 - 3 */
843
844   /* And finally... */
845   f25519_mul(&beta, &t, &xy3);          /*  268 | ... */
846
847   /* Now we have beta = (x y^3) (x y^7)^((p - 5)/8) = (x/y)^((p + 3)/8), and
848    * we're ready to finish the computation.  Suppose that alpha^2 = u/w.
849    * Then beta^4 = (x/y)^((p + 3)/2) = alpha^(p + 3) = alpha^4 = (x/y)^2, so
850    * we have beta^2 = ±x/y.  If y beta^2 = x then beta is the one we wanted;
851    * if -y beta^2 = x, then we want beta sqrt(-1), which we already know.  Of
852    * course, it might not match either, in which case we fail.
853    *
854    * The easiest way to compare is to encode.  This isn't as wasteful as it
855    * sounds: the hard part is normalizing the representations, which we have
856    * to do anyway.
857    */
858   f25519_sqr(&t, &beta);
859   f25519_mul(&t, &t, y);
860   f25519_neg(&u, &t);
861   f25519_store(xb, x);
862   f25519_store(b0, &t);
863   f25519_store(b1, &u);
864   f25519_mul(&u, &beta, SQRTM1);
865
866   m = -consttime_memeq(b0, xb, 32);
867   rc = PICK2(0, rc, m);
868   f25519_pick2(z, &beta, &u, m);
869   m = -consttime_memeq(b1, xb, 32);
870   rc = PICK2(0, rc, m);
871
872   /* And we're done. */
873   return (rc);
874 }
875
876 /*----- That's all, folks -------------------------------------------------*/