220a73c887625a52f77e187804618e44bcf29b86
3 * Arithmetic modulo 2^255 - 19
5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 /*----- Basic setup -------------------------------------------------------*/
37 /* Elements x of GF(2^255 - 19) are represented by ten signed integers x_i: x
38 * = SUM_{0<=i<10} x_i 2^ceil(51i/2), mostly following Bernstein's original
42 typedef int32 piece
; typedef int64 dblpiece
;
43 typedef uint32 upiece
; typedef uint64 udblpiece
;
45 #define PIECEWD(i) ((i)%2 ? 25 : 26)
48 #define M26 0x03ffffffu
49 #define M25 0x01ffffffu
50 #define B26 0x04000000u
51 #define B25 0x02000000u
52 #define B24 0x01000000u
54 #define PIECES(v) v##0, v##1, v##2, v##3, v##4, v##5, v##6, v##7, v##8, v##9
55 #define FETCH(v, w) do { \
56 v##0 = (w)->P[0]; v##1 = (w)->P[1]; \
57 v##2 = (w)->P[2]; v##3 = (w)->P[3]; \
58 v##4 = (w)->P[4]; v##5 = (w)->P[5]; \
59 v##6 = (w)->P[6]; v##7 = (w)->P[7]; \
60 v##8 = (w)->P[8]; v##9 = (w)->P[9]; \
62 #define STASH(w, v) do { \
63 (w)->P[0] = v##0; (w)->P[1] = v##1; \
64 (w)->P[2] = v##2; (w)->P[3] = v##3; \
65 (w)->P[4] = v##4; (w)->P[5] = v##5; \
66 (w)->P[6] = v##6; (w)->P[7] = v##7; \
67 (w)->P[8] = v##8; (w)->P[9] = v##9; \
70 #elif F25519_IMPL == 10
71 /* Elements x of GF(2^255 - 19) are represented by 26 signed integers x_i: x
72 * = SUM_{0<=i<26} x_i 2^ceil(255i/26); i.e., most pieces are 10 bits wide,
73 * except for pieces 5, 10, 15, 20, and 25 which have 9 bits.
76 typedef int16 piece
; typedef int32 dblpiece
;
77 typedef uint16 upiece
; typedef uint32 udblpiece
;
80 ((i) == 5 || (i) == 10 || (i) == 15 || (i) == 20 || (i) == 25 ? 9 : 10)
91 /*----- Debugging machinery -----------------------------------------------*/
93 #if defined(F25519_DEBUG) || defined(TEST_RIG)
100 static mp
*get_2p255m91(void)
105 p
= mp_setbit(p
, MP_ZERO
, 255);
106 mp_build(&m19
, &w19
, &w19
+ 1);
107 p
= mp_sub(p
, p
, &m19
);
111 DEF_FDUMP(fdump
, piece
, PIECEWD
, NPIECE
, 32, get_2p255m91())
115 /*----- Loading and storing -----------------------------------------------*/
117 /* --- @f25519_load@ --- *
119 * Arguments: @f25519 *z@ = where to store the result
120 * @const octet xv[32]@ = source to read
124 * Use: Reads an element of %$\gf{2^{255} - 19}$% in external
125 * representation from @xv@ and stores it in @z@.
127 * External representation is little-endian base-256. Some
128 * elements have multiple encodings, which are not produced by
129 * correct software; use of noncanonical encodings is not an
130 * error, and toleration of them is considered a performance
134 void f25519_load(f25519
*z
, const octet xv
[32])
136 #if F25519_IMPL == 26
138 uint32 xw0
= LOAD32_L(xv
+ 0), xw1
= LOAD32_L(xv
+ 4),
139 xw2
= LOAD32_L(xv
+ 8), xw3
= LOAD32_L(xv
+ 12),
140 xw4
= LOAD32_L(xv
+ 16), xw5
= LOAD32_L(xv
+ 20),
141 xw6
= LOAD32_L(xv
+ 24), xw7
= LOAD32_L(xv
+ 28);
142 piece
PIECES(x
), b
, c
;
144 /* First, split the 32-bit words into the irregularly-sized pieces we need
145 * for the field representation. These pieces are all positive. We'll do
146 * the sign correction afterwards.
148 * It may be that the top bit of the input is set. Avoid trouble by
149 * folding that back round into the bottom piece of the representation.
151 * Here, we briefly have 0 <= x_0 < 2^26 + 19, but will resolve this later.
152 * Otherwise, we have 0 <= x_{2i} < 2^26, and 0 <= x_{2i+1} < 2^25.
154 x0
= ((xw0
<< 0)&0x03ffffff) + 19*((xw7
>> 31)&0x00000001);
155 x1
= ((xw1
<< 6)&0x01ffffc0) | ((xw0
>> 26)&0x0000003f);
156 x2
= ((xw2
<< 13)&0x03ffe000) | ((xw1
>> 19)&0x00001fff);
157 x3
= ((xw3
<< 19)&0x01f80000) | ((xw2
>> 13)&0x0007ffff);
158 x4
= ((xw3
>> 6)&0x03ffffff);
159 x5
= (xw4
<< 0)&0x01ffffff;
160 x6
= ((xw5
<< 7)&0x03ffff80) | ((xw4
>> 25)&0x0000007f);
161 x7
= ((xw6
<< 13)&0x01ffe000) | ((xw5
>> 19)&0x00001fff);
162 x8
= ((xw7
<< 20)&0x03f00000) | ((xw6
>> 12)&0x000fffff);
163 x9
= ((xw7
>> 6)&0x01ffffff);
165 /* Next, we convert these pieces into a roughly balanced signed
166 * representation. For each piece, check to see if its top bit is set. If
167 * it is, then lend a bit to the next piece over. For x_9, this needs to
168 * be carried around, which is a little fiddly. In particular, we delay
169 * the carry until after all of the pieces have been balanced. If we don't
170 * do this, then we have to do a more expensive test for nonzeroness to
171 * decide whether to lend a bit leftwards rather than just testing a single
174 * This fixes up the anomalous size of x_0: the loan of a bit becomes an
175 * actual carry if x_0 >= 2^26. By the end, then, we have:
181 * Note that we don't try for a canonical representation here: both upper
182 * and lower bounds are achievable.
184 * All of the x_i at this point are positive, so we don't need to do
185 * anything wierd when masking them.
187 b
= x9
&B24
; c
= 19&((b
>> 19) - (b
>> 24)); x9
-= b
<< 1;
188 b
= x8
&B25
; x9
+= b
>> 25; x8
-= b
<< 1;
189 b
= x7
&B24
; x8
+= b
>> 24; x7
-= b
<< 1;
190 b
= x6
&B25
; x7
+= b
>> 25; x6
-= b
<< 1;
191 b
= x5
&B24
; x6
+= b
>> 24; x5
-= b
<< 1;
192 b
= x4
&B25
; x5
+= b
>> 25; x4
-= b
<< 1;
193 b
= x3
&B24
; x4
+= b
>> 24; x3
-= b
<< 1;
194 b
= x2
&B25
; x3
+= b
>> 25; x2
-= b
<< 1;
195 b
= x1
&B24
; x2
+= b
>> 24; x1
-= b
<< 1;
196 b
= x0
&B25
; x1
+= (b
>> 25) + (x0
>> 26); x0
= (x0
&M26
) - (b
<< 1);
199 /* And with that, we're done. */
202 #elif F25519_IMPL == 10
205 unsigned i
, j
, n
, wd
;
209 /* First, just get the content out of the buffer. */
210 for (i
= j
= a
= n
= 0, wd
= 10; j
< NPIECE
; i
++) {
211 a
|= (uint32
)xv
[i
] << n
; n
+= 8;
219 /* There's a little bit left over from the top byte. Carry it into the low
222 x
[0] += 19*(int)(a
&MASK(n
));
224 /* Next, convert the pieces into a roughly balanced signed representation.
225 * If a piece's top bit is set, lend a bit to the next piece over. For
226 * x_25, this needs to be carried around, which is a bit fiddly.
228 b
= x
[NPIECE
- 1]&B8
;
229 c
= 19&((b
>> 3) - (b
>> 8));
230 x
[NPIECE
- 1] -= b
<< 1;
231 for (i
= NPIECE
- 2; i
> 0; i
--) {
238 x
[1] += (b
>> 9) + (x
[0] >> 10);
239 x
[0] = (x
[0]&M10
) - (b
<< 1) + c
;
241 /* And we're done. */
242 for (i
= 0; i
< NPIECE
; i
++) z
->P
[i
] = x
[i
];
247 /* --- @f25519_store@ --- *
249 * Arguments: @octet zv[32]@ = where to write the result
250 * @const f25519 *x@ = the field element to write
254 * Use: Stores a field element in the given octet vector in external
255 * representation. A canonical encoding is always stored, so,
256 * in particular, the top bit of @xv[31]@ is always left clear.
259 void f25519_store(octet zv
[32], const f25519
*x
)
261 #if F25519_IMPL == 26
263 piece
PIECES(x
), PIECES(y
), c
, d
;
264 uint32 zw0
, zw1
, zw2
, zw3
, zw4
, zw5
, zw6
, zw7
;
269 /* First, propagate the carries throughout the pieces. By the end of this,
270 * we'll have all of the pieces canonically sized and positive, and maybe
271 * there'll be (signed) carry out. The carry c is in { -1, 0, +1 }, and
272 * the remaining value will be in the half-open interval [0, 2^255). The
273 * whole represented value is then x + 2^255 c.
275 * It's worth paying careful attention to the bounds. We assume that we
276 * start out with |x_i| <= 2^30. We start by cutting off and reducing the
277 * carry c_9 from the topmost piece, x_9. This leaves 0 <= x_9 < 2^25; and
278 * we'll have |c_9| <= 2^5. We multiply this by 19 and we'll add it onto
279 * x_0 and propagate the carries: but what bounds can we calculate on x
282 * Let o_i = floor(51 i/2). We have X_i = SUM_{0<=j<i} x_j 2^{o_i}, so
283 * x = X_10. We see, inductively, that |X_i| < 2^{31+o_{i-1}}: X_0 = 0;
284 * |x_i| <= 2^30; and |X_{i+1}| = |X_i + x_i 2^{o_i}| <= |X_i| + 2^{30+o_i}
285 * < 2^{31+o_i}. Then x = X_9 + 2^230 x_9, and we have better bounds for
288 * -2^235 < x + 19 c_9 < 2^255 + 2^235
290 * Here, the x_i are signed, so we must be cautious about bithacking them.
292 c
= ASR(piece
, x9
, 25); x9
= (upiece
)x9
&M25
;
293 x0
+= 19*c
; c
= ASR(piece
, x0
, 26); x0
= (upiece
)x0
&M26
;
294 x1
+= c
; c
= ASR(piece
, x1
, 25); x1
= (upiece
)x1
&M25
;
295 x2
+= c
; c
= ASR(piece
, x2
, 26); x2
= (upiece
)x2
&M26
;
296 x3
+= c
; c
= ASR(piece
, x3
, 25); x3
= (upiece
)x3
&M25
;
297 x4
+= c
; c
= ASR(piece
, x4
, 26); x4
= (upiece
)x4
&M26
;
298 x5
+= c
; c
= ASR(piece
, x5
, 25); x5
= (upiece
)x5
&M25
;
299 x6
+= c
; c
= ASR(piece
, x6
, 26); x6
= (upiece
)x6
&M26
;
300 x7
+= c
; c
= ASR(piece
, x7
, 25); x7
= (upiece
)x7
&M25
;
301 x8
+= c
; c
= ASR(piece
, x8
, 26); x8
= (upiece
)x8
&M26
;
302 x9
+= c
; c
= ASR(piece
, x9
, 25); x9
= (upiece
)x9
&M25
;
304 /* Now we have a slightly fiddly job to do. If c = +1, or if c = 0 and
305 * x >= 2^255 - 19, then we should subtract 2^255 - 19 from the whole
306 * value; if c = -1 then we should add 2^255 - 19; and otherwise we should
309 * But conditional behaviour is bad, m'kay. So here's what we do instead.
311 * The first job is to sort out what we wanted to do. If c = -1 then we
312 * want to (a) invert the constant addend and (b) feed in a carry-in;
313 * otherwise, we don't.
318 /* Now do the addition/subtraction. Remember that all of the x_i are
319 * nonnegative, so shifting and masking are safe and easy.
321 d
+= x0
+ (19 ^ (M26
&m
)); y0
= d
&M26
; d
>>= 26;
322 d
+= x1
+ (M25
&m
); y1
= d
&M25
; d
>>= 25;
323 d
+= x2
+ (M26
&m
); y2
= d
&M26
; d
>>= 26;
324 d
+= x3
+ (M25
&m
); y3
= d
&M25
; d
>>= 25;
325 d
+= x4
+ (M26
&m
); y4
= d
&M26
; d
>>= 26;
326 d
+= x5
+ (M25
&m
); y5
= d
&M25
; d
>>= 25;
327 d
+= x6
+ (M26
&m
); y6
= d
&M26
; d
>>= 26;
328 d
+= x7
+ (M25
&m
); y7
= d
&M25
; d
>>= 25;
329 d
+= x8
+ (M26
&m
); y8
= d
&M26
; d
>>= 26;
330 d
+= x9
+ (M25
&m
); y9
= d
&M25
; d
>>= 25;
332 /* The final carry-out is in d; since we only did addition, and the x_i are
333 * nonnegative, then d is in { 0, 1 }. We want to keep y, rather than x,
334 * if (a) c /= 0 (in which case we know that the old value was
335 * unsatisfactory), or (b) if d = 1 (in which case, if c = 0, we know that
336 * the subtraction didn't cause a borrow, so we must be in the case where
337 * 2^255 - 19 <= x < 2^255).
339 m
= NONZEROP(c
) | ~NONZEROP(d
- 1);
340 x0
= (y0
&m
) | (x0
&~m
); x1
= (y1
&m
) | (x1
&~m
);
341 x2
= (y2
&m
) | (x2
&~m
); x3
= (y3
&m
) | (x3
&~m
);
342 x4
= (y4
&m
) | (x4
&~m
); x5
= (y5
&m
) | (x5
&~m
);
343 x6
= (y6
&m
) | (x6
&~m
); x7
= (y7
&m
) | (x7
&~m
);
344 x8
= (y8
&m
) | (x8
&~m
); x9
= (y9
&m
) | (x9
&~m
);
346 /* Extract 32-bit words from the value. */
347 zw0
= ((x0
>> 0)&0x03ffffff) | (((uint32
)x1
<< 26)&0xfc000000);
348 zw1
= ((x1
>> 6)&0x0007ffff) | (((uint32
)x2
<< 19)&0xfff80000);
349 zw2
= ((x2
>> 13)&0x00001fff) | (((uint32
)x3
<< 13)&0xffffe000);
350 zw3
= ((x3
>> 19)&0x0000003f) | (((uint32
)x4
<< 6)&0xffffffc0);
351 zw4
= ((x5
>> 0)&0x01ffffff) | (((uint32
)x6
<< 25)&0xfe000000);
352 zw5
= ((x6
>> 7)&0x0007ffff) | (((uint32
)x7
<< 19)&0xfff80000);
353 zw6
= ((x7
>> 13)&0x00000fff) | (((uint32
)x8
<< 12)&0xfffff000);
354 zw7
= ((x8
>> 20)&0x0000003f) | (((uint32
)x9
<< 6)&0x7fffffc0);
356 /* Store the result as an octet string. */
357 STORE32_L(zv
+ 0, zw0
); STORE32_L(zv
+ 4, zw1
);
358 STORE32_L(zv
+ 8, zw2
); STORE32_L(zv
+ 12, zw3
);
359 STORE32_L(zv
+ 16, zw4
); STORE32_L(zv
+ 20, zw5
);
360 STORE32_L(zv
+ 24, zw6
); STORE32_L(zv
+ 28, zw7
);
362 #elif F25519_IMPL == 10
364 piece y
[NPIECE
], yy
[NPIECE
], c
, d
;
365 unsigned i
, j
, n
, wd
;
368 /* Before we do anything, copy the input so we can hack on it. */
369 for (i
= 0; i
< NPIECE
; i
++) y
[i
] = x
->P
[i
];
371 /* First, propagate the carries throughout the pieces.
373 * It's worth paying careful attention to the bounds. We assume that we
374 * start out with |y_i| <= 2^14. We start by cutting off and reducing the
375 * carry c_25 from the topmost piece, y_25. This leaves 0 <= y_25 < 2^9;
376 * and we'll have |c_25| <= 2^5. We multiply this by 19 and we'll ad it
377 * onto y_0 and propagte the carries: but what bounds can we calculate on
380 * Let o_i = floor(255 i/26). We have Y_i = SUM_{0<=j<i} y_j 2^{o_i}, so
381 * y = Y_26. We see, inductively, that |Y_i| < 2^{31+o_{i-1}}: Y_0 = 0;
382 * |y_i| <= 2^14; and |Y_{i+1}| = |Y_i + y_i 2^{o_i}| <= |Y_i| + 2^{14+o_i}
383 * < 2^{15+o_i}. Then x = Y_25 + 2^246 y_25, and we have better bounds for
386 * -2^251 < y + 19 c_25 < 2^255 + 2^251
388 * Here, the y_i are signed, so we must be cautious about bithacking them.
390 * (Rather closer than the 10-piece case above, but still doable in one
393 c
= 19*ASR(piece
, y
[NPIECE
- 1], 9);
394 y
[NPIECE
- 1] = (upiece
)y
[NPIECE
- 1]&M9
;
395 for (i
= 0; i
< NPIECE
; i
++) {
398 c
= ASR(piece
, y
[i
], wd
);
399 y
[i
] = (upiece
)y
[i
]&MASK(wd
);
402 /* Now the addition or subtraction. */
406 d
+= y
[0] + (19 ^ (M10
&m
));
409 for (i
= 1; i
< NPIECE
; i
++) {
411 d
+= y
[i
] + (MASK(wd
)&m
);
416 /* Choose which value to keep. */
417 m
= NONZEROP(c
) | ~NONZEROP(d
- 1);
418 for (i
= 0; i
< NPIECE
; i
++) y
[i
] = (yy
[i
]&m
) | (y
[i
]&~m
);
420 /* Store the result as an octet string. */
421 for (i
= j
= a
= n
= 0; i
< NPIECE
; i
++) {
422 a
|= (upiece
)y
[i
] << n
; n
+= PIECEWD(i
);
433 /* --- @f25519_set@ --- *
435 * Arguments: @f25519 *z@ = where to write the result
436 * @int a@ = a small-ish constant
440 * Use: Sets @z@ to equal @a@.
443 void f25519_set(f25519
*x
, int a
)
448 for (i
= 1; i
< NPIECE
; i
++) x
->P
[i
] = 0;
451 /*----- Basic arithmetic --------------------------------------------------*/
453 /* --- @f25519_add@ --- *
455 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
456 * @const f25519 *x, *y@ = two operands
460 * Use: Set @z@ to the sum %$x + y$%.
463 void f25519_add(f25519
*z
, const f25519
*x
, const f25519
*y
)
465 #if F25519_IMPL == 26
466 z
->P
[0] = x
->P
[0] + y
->P
[0]; z
->P
[1] = x
->P
[1] + y
->P
[1];
467 z
->P
[2] = x
->P
[2] + y
->P
[2]; z
->P
[3] = x
->P
[3] + y
->P
[3];
468 z
->P
[4] = x
->P
[4] + y
->P
[4]; z
->P
[5] = x
->P
[5] + y
->P
[5];
469 z
->P
[6] = x
->P
[6] + y
->P
[6]; z
->P
[7] = x
->P
[7] + y
->P
[7];
470 z
->P
[8] = x
->P
[8] + y
->P
[8]; z
->P
[9] = x
->P
[9] + y
->P
[9];
471 #elif F25519_IMPL == 10
473 for (i
= 0; i
< NPIECE
; i
++) z
->P
[i
] = x
->P
[i
] + y
->P
[i
];
477 /* --- @f25519_sub@ --- *
479 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
480 * @const f25519 *x, *y@ = two operands
484 * Use: Set @z@ to the difference %$x - y$%.
487 void f25519_sub(f25519
*z
, const f25519
*x
, const f25519
*y
)
489 #if F25519_IMPL == 26
490 z
->P
[0] = x
->P
[0] - y
->P
[0]; z
->P
[1] = x
->P
[1] - y
->P
[1];
491 z
->P
[2] = x
->P
[2] - y
->P
[2]; z
->P
[3] = x
->P
[3] - y
->P
[3];
492 z
->P
[4] = x
->P
[4] - y
->P
[4]; z
->P
[5] = x
->P
[5] - y
->P
[5];
493 z
->P
[6] = x
->P
[6] - y
->P
[6]; z
->P
[7] = x
->P
[7] - y
->P
[7];
494 z
->P
[8] = x
->P
[8] - y
->P
[8]; z
->P
[9] = x
->P
[9] - y
->P
[9];
495 #elif F25519_IMPL == 10
497 for (i
= 0; i
< NPIECE
; i
++) z
->P
[i
] = x
->P
[i
] - y
->P
[i
];
501 /*----- Constant-time utilities -------------------------------------------*/
503 /* --- @f25519_condswap@ --- *
505 * Arguments: @f25519 *x, *y@ = two operands
506 * @uint32 m@ = a mask
510 * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then
511 * exchange @x@ and @y@. If @m@ has some other value, then
512 * scramble @x@ and @y@ in an unhelpful way.
515 void f25519_condswap(f25519
*x
, f25519
*y
, uint32 m
)
517 mask32 mm
= FIX_MASK32(m
);
519 #if F25519_IMPL == 26
520 CONDSWAP(x
->P
[0], y
->P
[0], mm
);
521 CONDSWAP(x
->P
[1], y
->P
[1], mm
);
522 CONDSWAP(x
->P
[2], y
->P
[2], mm
);
523 CONDSWAP(x
->P
[3], y
->P
[3], mm
);
524 CONDSWAP(x
->P
[4], y
->P
[4], mm
);
525 CONDSWAP(x
->P
[5], y
->P
[5], mm
);
526 CONDSWAP(x
->P
[6], y
->P
[6], mm
);
527 CONDSWAP(x
->P
[7], y
->P
[7], mm
);
528 CONDSWAP(x
->P
[8], y
->P
[8], mm
);
529 CONDSWAP(x
->P
[9], y
->P
[9], mm
);
530 #elif F25519_IMPL == 10
532 for (i
= 0; i
< NPIECE
; i
++) CONDSWAP(x
->P
[i
], y
->P
[i
], mm
);
536 /*----- Multiplication ----------------------------------------------------*/
538 #if F25519_IMPL == 26
540 /* Let B = 2^63 - 1 be the largest value such that +B and -B can be
541 * represented in a double-precision piece. On entry, it must be the case
542 * that |X_i| <= M <= B - 2^25 for some M. If this is the case, then, on
543 * exit, we will have |Z_i| <= 2^25 + 19 M/2^25.
545 #define CARRYSTEP(z, x, m, b, f, xx, n) do { \
546 (z) = (dblpiece)((udblpiece)(x)&(m)) - (b) + \
547 (f)*ASR(dblpiece, (xx), (n)); \
549 #define CARRY_REDUCE(z, x) do { \
550 dblpiece PIECES(_t); \
552 /* Bias the input pieces. This keeps the carries and so on centred \
553 * around zero rather than biased positive. \
555 _t0 = (x##0) + B25; _t1 = (x##1) + B24; \
556 _t2 = (x##2) + B25; _t3 = (x##3) + B24; \
557 _t4 = (x##4) + B25; _t5 = (x##5) + B24; \
558 _t6 = (x##6) + B25; _t7 = (x##7) + B24; \
559 _t8 = (x##8) + B25; _t9 = (x##9) + B24; \
561 /* Calculate the reduced pieces. Careful with the bithacking. */ \
562 CARRYSTEP(z##0, _t0, M26, B25, 19, _t9, 25); \
563 CARRYSTEP(z##1, _t1, M25, B24, 1, _t0, 26); \
564 CARRYSTEP(z##2, _t2, M26, B25, 1, _t1, 25); \
565 CARRYSTEP(z##3, _t3, M25, B24, 1, _t2, 26); \
566 CARRYSTEP(z##4, _t4, M26, B25, 1, _t3, 25); \
567 CARRYSTEP(z##5, _t5, M25, B24, 1, _t4, 26); \
568 CARRYSTEP(z##6, _t6, M26, B25, 1, _t5, 25); \
569 CARRYSTEP(z##7, _t7, M25, B24, 1, _t6, 26); \
570 CARRYSTEP(z##8, _t8, M26, B25, 1, _t7, 25); \
571 CARRYSTEP(z##9, _t9, M25, B24, 1, _t8, 26); \
574 #elif F25519_IMPL == 10
576 /* Perform carry propagation on X. */
577 static void carry_reduce(dblpiece x
[NPIECE
])
579 /* Initial bounds: we assume |x_i| < 2^31 - 2^27. */
584 /* The result is nearly canonical, because we do sequential carry
585 * propagation, because smaller processors are more likely to prefer the
586 * smaller working set than the instruction-level parallelism.
588 * Start at x_23; truncate it to 10 bits, and propagate the carry to x_24.
589 * Truncate x_24 to 10 bits, and add the carry onto x_25. Truncate x_25 to
590 * 9 bits, and add 19 times the carry onto x_0. And so on.
592 * Let c_i be the portion of x_i to be carried onto x_{i+1}. I claim that
593 * |c_i| <= 2^22. Then the carry /into/ any x_i has magnitude at most
594 * 19*2^22 < 2^27 (allowing for the reduction as we carry from x_25 to
595 * x_0), and x_i after carry is bounded above by 2^31. Hence, the carry
596 * out is at most 2^22, as claimed.
598 * Once we reach x_23 for the second time, we start with |x_23| <= 2^9.
599 * The carry into x_23 is at most 2^27 as calculated above; so the carry
600 * out into x_24 has magnitude at most 2^17. In turn, |x_24| <= 2^9 before
601 * the carry, so is now no more than 2^18 in magnitude, and the carry out
602 * into x_25 is at most 2^8. This leaves |x_25| < 2^9 after carry
605 * Be careful with the bit hacking because the quantities involved are
609 /*For each piece, we bias it so that floor division (as done by an
610 * arithmetic right shift) and modulus (as done by bitwise-AND) does the
613 #define CARRY(i, wd, b, m) do { \
615 c = ASR(dblpiece, x[i], (wd)); \
616 x[i] = (dblpiece)((udblpiece)x[i]&(m)) - (b); \
619 { CARRY(23, 10, B9
, M10
); }
620 { x
[24] += c
; CARRY(24, 10, B9
, M10
); }
621 { x
[25] += c
; CARRY(25, 9, B8
, M9
); }
622 { x
[0] += 19*c
; CARRY( 0, 10, B9
, M10
); }
623 for (i
= 1; i
< 21; ) {
624 for (j
= i
+ 4; i
< j
; ) { x
[i
] += c
; CARRY( i
, 10, B9
, M10
); i
++; }
625 { x
[i
] += c
; CARRY( i
, 9, B8
, M9
); i
++; }
627 while (i
< 25) { x
[i
] += c
; CARRY( i
, 10, B9
, M10
); i
++; }
635 /* --- @f25519_mulconst@ --- *
637 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
638 * @const f25519 *x@ = an operand
639 * @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
643 * Use: Set @z@ to the product %$a x$%.
646 void f25519_mulconst(f25519
*z
, const f25519
*x
, long a
)
648 #if F25519_IMPL == 26
651 dblpiece
PIECES(z
), aa
= a
;
655 /* Suppose that |x_i| <= 2^27, and |a| <= 2^23. Then we'll have
658 z0
= aa
*x0
; z1
= aa
*x1
; z2
= aa
*x2
; z3
= aa
*x3
; z4
= aa
*x4
;
659 z5
= aa
*x5
; z6
= aa
*x6
; z7
= aa
*x7
; z8
= aa
*x8
; z9
= aa
*x9
;
661 /* Following `CARRY_REDUCE', we'll have |z_i| <= 2^26. */
665 #elif F25519_IMPL == 10
670 for (i
= 0; i
< NPIECE
; i
++) y
[i
] = a
*x
->P
[i
];
672 for (i
= 0; i
< NPIECE
; i
++) z
->P
[i
] = y
[i
];
677 /* --- @f25519_mul@ --- *
679 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
680 * @const f25519 *x, *y@ = two operands
684 * Use: Set @z@ to the product %$x y$%.
687 void f25519_mul(f25519
*z
, const f25519
*x
, const f25519
*y
)
689 #if F25519_IMPL == 26
691 piece
PIECES(x
), PIECES(y
);
695 FETCH(x
, x
); FETCH(y
, y
);
697 /* Suppose that |x_i|, |y_i| <= 2^27. Then we'll have
710 * all of which are less than 2^63 - 2^25.
713 #define M(a, b) ((dblpiece)(a)*(b))
715 19*(M(x2
, y8
) + M(x4
, y6
) + M(x6
, y4
) + M(x8
, y2
)) +
716 38*(M(x1
, y9
) + M(x3
, y7
) + M(x5
, y5
) + M(x7
, y3
) + M(x9
, y1
));
717 z1
= M(x0
, y1
) + M(x1
, y0
) +
718 19*(M(x2
, y9
) + M(x3
, y8
) + M(x4
, y7
) + M(x5
, y6
) +
719 M(x6
, y5
) + M(x7
, y4
) + M(x8
, y3
) + M(x9
, y2
));
720 z2
= M(x0
, y2
) + M(x2
, y0
) +
722 19*(M(x4
, y8
) + M(x6
, y6
) + M(x8
, y4
)) +
723 38*(M(x3
, y9
) + M(x5
, y7
) + M(x7
, y5
) + M(x9
, y3
));
724 z3
= M(x0
, y3
) + M(x1
, y2
) + M(x2
, y1
) + M(x3
, y0
) +
725 19*(M(x4
, y9
) + M(x5
, y8
) + M(x6
, y7
) +
726 M(x7
, y6
) + M(x8
, y5
) + M(x9
, y4
));
727 z4
= M(x0
, y4
) + M(x2
, y2
) + M(x4
, y0
) +
728 2*(M(x1
, y3
) + M(x3
, y1
)) +
729 19*(M(x6
, y8
) + M(x8
, y6
)) +
730 38*(M(x5
, y9
) + M(x7
, y7
) + M(x9
, y5
));
731 z5
= M(x0
, y5
) + M(x1
, y4
) + M(x2
, y3
) +
732 M(x3
, y2
) + M(x4
, y1
) + M(x5
, y0
) +
733 19*(M(x6
, y9
) + M(x7
, y8
) + M(x8
, y7
) + M(x9
, y6
));
734 z6
= M(x0
, y6
) + M(x2
, y4
) + M(x4
, y2
) + M(x6
, y0
) +
735 2*(M(x1
, y5
) + M(x3
, y3
) + M(x5
, y1
)) +
737 38*(M(x7
, y9
) + M(x9
, y7
));
738 z7
= M(x0
, y7
) + M(x1
, y6
) + M(x2
, y5
) + M(x3
, y4
) +
739 M(x4
, y3
) + M(x5
, y2
) + M(x6
, y1
) + M(x7
, y0
) +
740 19*(M(x8
, y9
) + M(x9
, y8
));
741 z8
= M(x0
, y8
) + M(x2
, y6
) + M(x4
, y4
) + M(x6
, y2
) + M(x8
, y0
) +
742 2*(M(x1
, y7
) + M(x3
, y5
) + M(x5
, y3
) + M(x7
, y1
)) +
744 z9
= M(x0
, y9
) + M(x1
, y8
) + M(x2
, y7
) + M(x3
, y6
) + M(x4
, y5
) +
745 M(x5
, y4
) + M(x6
, y3
) + M(x7
, y2
) + M(x8
, y1
) + M(x9
, y0
);
748 /* From above, we have |z_i| <= 2^63 - 2^25. A pass of `CARRY_REDUCE' will
749 * leave |z_i| <= 2^38 + 2^25; and a second pass will leave |z_i| <= 2^25 +
750 * 2^13, which is comfortable for an addition prior to the next
753 for (i
= 0; i
< 2; i
++) CARRY_REDUCE(z
, z
);
756 #elif F25519_IMPL == 10
758 dblpiece u
[NPIECE
], t
, tt
, p
;
761 /* This is unpleasant. Honestly, this table seems to be the best way of
764 static const unsigned short off
[NPIECE
] = {
765 0, 10, 20, 30, 40, 50, 59, 69, 79, 89, 99, 108, 118,
766 128, 138, 148, 157, 167, 177, 187, 197, 206, 216, 226, 236, 246
769 /* First pass: things we must multiply by 19 or 38. */
770 for (i
= 0; i
< NPIECE
- 1; i
++) {
772 for (j
= i
+ 1; j
< NPIECE
; j
++) {
773 k
= NPIECE
+ i
- j
; p
= (dblpiece
)x
->P
[j
]*y
->P
[k
];
774 if (off
[i
] < off
[j
] + off
[k
] - 255) tt
+= p
;
777 u
[i
] = 19*(t
+ 2*tt
);
781 /* Second pass: things we must multiply by 1 or 2. */
782 for (i
= 0; i
< NPIECE
; i
++) {
784 for (j
= 0; j
<= i
; j
++) {
785 k
= i
- j
; p
= (dblpiece
)x
->P
[j
]*y
->P
[k
];
786 if (off
[i
] < off
[j
] + off
[k
]) tt
+= p
;
792 /* And we're done. */
794 for (i
= 0; i
< NPIECE
; i
++) z
->P
[i
] = u
[i
];
799 /* --- @f25519_sqr@ --- *
801 * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@)
802 * @const f25519 *x@ = an operand
806 * Use: Set @z@ to the square %$x^2$%.
809 void f25519_sqr(f25519
*z
, const f25519
*x
)
811 #if F25519_IMPL == 26
819 /* See `f25519_mul' for bounds. */
821 #define M(a, b) ((dblpiece)(a)*(b))
823 38*(M(x2
, x8
) + M(x4
, x6
) + M(x5
, x5
)) +
824 76*(M(x1
, x9
) + M(x3
, x7
));
826 38*(M(x2
, x9
) + M(x3
, x8
) + M(x4
, x7
) + M(x5
, x6
));
827 z2
= 2*(M(x0
, x2
) + M(x1
, x1
)) +
830 76*(M(x3
, x9
) + M(x5
, x7
));
831 z3
= 2*(M(x0
, x3
) + M(x1
, x2
)) +
832 38*(M(x4
, x9
) + M(x5
, x8
) + M(x6
, x7
));
836 38*(M(x6
, x8
) + M(x7
, x7
)) +
838 z5
= 2*(M(x0
, x5
) + M(x1
, x4
) + M(x2
, x3
)) +
839 38*(M(x6
, x9
) + M(x7
, x8
));
840 z6
= 2*(M(x0
, x6
) + M(x2
, x4
) + M(x3
, x3
)) +
844 z7
= 2*(M(x0
, x7
) + M(x1
, x6
) + M(x2
, x5
) + M(x3
, x4
)) +
847 2*(M(x0
, x8
) + M(x2
, x6
)) +
848 4*(M(x1
, x7
) + M(x3
, x5
)) +
850 z9
= 2*(M(x0
, x9
) + M(x1
, x8
) + M(x2
, x7
) + M(x3
, x6
) + M(x4
, x5
));
853 /* See `f25519_mul' for details. */
854 for (i
= 0; i
< 2; i
++) CARRY_REDUCE(z
, z
);
857 #elif F25519_IMPL == 10
862 /*----- More complicated things -------------------------------------------*/
864 /* --- @f25519_inv@ --- *
866 * Arguments: @f25519 *z@ = where to put the result (may alias @x@)
867 * @const f25519 *x@ = an operand
871 * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If
872 * %$x = 0$% then @z@ is set to zero. This is considered a
876 void f25519_inv(f25519
*z
, const f25519
*x
)
878 f25519 t
, u
, t2
, t11
, t2p10m1
, t2p50m1
;
881 #define SQRN(z, x, n) do { \
882 f25519_sqr((z), (x)); \
883 for (i = 1; i < (n); i++) f25519_sqr((z), (z)); \
886 /* Calculate x^-1 = x^(p - 2) = x^(2^255 - 21), which also handles x = 0 as
887 * intended. The addition chain here is from Bernstein's implementation; I
888 * couldn't find a better one.
889 */ /* step | value */
890 f25519_sqr(&t2
, x
); /* 1 | 2 */
891 SQRN(&u
, &t2
, 2); /* 3 | 8 */
892 f25519_mul(&t
, &u
, x
); /* 4 | 9 */
893 f25519_mul(&t11
, &t
, &t2
); /* 5 | 11 = 2^5 - 21 */
894 f25519_sqr(&u
, &t11
); /* 6 | 22 */
895 f25519_mul(&t
, &t
, &u
); /* 7 | 31 = 2^5 - 1 */
896 SQRN(&u
, &t
, 5); /* 12 | 2^10 - 2^5 */
897 f25519_mul(&t2p10m1
, &t
, &u
); /* 13 | 2^10 - 1 */
898 SQRN(&u
, &t2p10m1
, 10); /* 23 | 2^20 - 2^10 */
899 f25519_mul(&t
, &t2p10m1
, &u
); /* 24 | 2^20 - 1 */
900 SQRN(&u
, &t
, 20); /* 44 | 2^40 - 2^20 */
901 f25519_mul(&t
, &t
, &u
); /* 45 | 2^40 - 1 */
902 SQRN(&u
, &t
, 10); /* 55 | 2^50 - 2^10 */
903 f25519_mul(&t2p50m1
, &t2p10m1
, &u
); /* 56 | 2^50 - 1 */
904 SQRN(&u
, &t2p50m1
, 50); /* 106 | 2^100 - 2^50 */
905 f25519_mul(&t
, &t2p50m1
, &u
); /* 107 | 2^100 - 1 */
906 SQRN(&u
, &t
, 100); /* 207 | 2^200 - 2^100 */
907 f25519_mul(&t
, &t
, &u
); /* 208 | 2^200 - 1 */
908 SQRN(&u
, &t
, 50); /* 258 | 2^250 - 2^50 */
909 f25519_mul(&t
, &t2p50m1
, &u
); /* 259 | 2^250 - 1 */
910 SQRN(&u
, &t
, 5); /* 264 | 2^255 - 2^5 */
911 f25519_mul(z
, &u
, &t11
); /* 265 | 2^255 - 21 */
916 /*----- Test rig ----------------------------------------------------------*/
920 #include <mLib/report.h>
921 #include <mLib/testrig.h>
923 static void fixdstr(dstr
*d
)
926 die(1, "invalid length for f25519");
927 else if (d
->len
< 32) {
929 memset(d
->buf
+ d
->len
, 0, 32 - d
->len
);
934 static void cvt_f25519(const char *buf
, dstr
*d
)
938 type_hex
.cvt(buf
, &dd
); fixdstr(&dd
);
939 dstr_ensure(d
, sizeof(f25519
)); d
->len
= sizeof(f25519
);
940 f25519_load((f25519
*)d
->buf
, (const octet
*)dd
.buf
);
944 static void dump_f25519(dstr
*d
, FILE *fp
)
945 { fdump(stderr
, "???", (const piece
*)d
->buf
); }
947 static void cvt_f25519_ref(const char *buf
, dstr
*d
)
948 { type_hex
.cvt(buf
, d
); fixdstr(d
); }
950 static void dump_f25519_ref(dstr
*d
, FILE *fp
)
954 f25519_load(&x
, (const octet
*)d
->buf
);
955 fdump(stderr
, "???", x
.P
);
958 static int eq(const f25519
*x
, dstr
*d
)
959 { octet b
[32]; f25519_store(b
, x
); return (memcmp(b
, d
->buf
, 32) == 0); }
961 static const test_type
962 type_f25519
= { cvt_f25519
, dump_f25519
},
963 type_f25519_ref
= { cvt_f25519_ref
, dump_f25519_ref
};
965 #define TEST_UNOP(op) \
966 static int vrf_##op(dstr dv[]) \
968 f25519 *x = (f25519 *)dv[0].buf; \
972 f25519_##op(&z, x); \
973 if (!eq(&z, &dv[1])) { \
975 fprintf(stderr, "failed!\n"); \
976 fdump(stderr, "x", x->P); \
977 fdump(stderr, "calc", z.P); \
978 f25519_load(&zz, (const octet *)dv[1].buf); \
979 fdump(stderr, "z", zz.P); \
988 #define TEST_BINOP(op) \
989 static int vrf_##op(dstr dv[]) \
991 f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; \
995 f25519_##op(&z, x, y); \
996 if (!eq(&z, &dv[2])) { \
998 fprintf(stderr, "failed!\n"); \
999 fdump(stderr, "x", x->P); \
1000 fdump(stderr, "y", y->P); \
1001 fdump(stderr, "calc", z.P); \
1002 f25519_load(&zz, (const octet *)dv[2].buf); \
1003 fdump(stderr, "z", zz.P); \
1013 static int vrf_mulc(dstr dv
[])
1015 f25519
*x
= (f25519
*)dv
[0].buf
;
1016 long a
= *(const long *)dv
[1].buf
;
1020 f25519_mulconst(&z
, x
, a
);
1021 if (!eq(&z
, &dv
[2])) {
1023 fprintf(stderr
, "failed!\n");
1024 fdump(stderr
, "x", x
->P
);
1025 fprintf(stderr
, "a = %ld\n", a
);
1026 fdump(stderr
, "calc", z
.P
);
1027 f25519_load(&zz
, (const octet
*)dv
[2].buf
);
1028 fdump(stderr
, "z", zz
.P
);
1034 static int vrf_condswap(dstr dv
[])
1036 f25519
*x
= (f25519
*)dv
[0].buf
, *y
= (f25519
*)dv
[1].buf
;
1037 f25519 xx
= *x
, yy
= *y
;
1038 uint32 m
= *(uint32
*)dv
[2].buf
;
1041 f25519_condswap(&xx
, &yy
, m
);
1042 if (!eq(&xx
, &dv
[3]) || !eq(&yy
, &dv
[4])) {
1044 fprintf(stderr
, "failed!\n");
1045 fdump(stderr
, "x", x
->P
);
1046 fdump(stderr
, "y", y
->P
);
1047 fprintf(stderr
, "m = 0x%08lx\n", (unsigned long)m
);
1048 fdump(stderr
, "calc xx", xx
.P
);
1049 fdump(stderr
, "calc yy", yy
.P
);
1050 f25519_load(&xx
, (const octet
*)dv
[3].buf
);
1051 f25519_load(&yy
, (const octet
*)dv
[4].buf
);
1052 fdump(stderr
, "want xx", xx
.P
);
1053 fdump(stderr
, "want yy", yy
.P
);
1059 static int vrf_sub_mulc_add_sub_mul(dstr dv
[])
1061 f25519
*u
= (f25519
*)dv
[0].buf
, *v
= (f25519
*)dv
[1].buf
,
1062 *w
= (f25519
*)dv
[3].buf
, *x
= (f25519
*)dv
[4].buf
,
1063 *y
= (f25519
*)dv
[5].buf
;
1064 long a
= *(const long *)dv
[2].buf
;
1065 f25519 umv
, aumv
, wpaumv
, xmy
, z
, zz
;
1068 f25519_sub(&umv
, u
, v
);
1069 f25519_mulconst(&aumv
, &umv
, a
);
1070 f25519_add(&wpaumv
, w
, &aumv
);
1071 f25519_sub(&xmy
, x
, y
);
1072 f25519_mul(&z
, &wpaumv
, &xmy
);
1074 if (!eq(&z
, &dv
[6])) {
1076 fprintf(stderr
, "failed!\n");
1077 fdump(stderr
, "u", u
->P
);
1078 fdump(stderr
, "v", v
->P
);
1079 fdump(stderr
, "u - v", umv
.P
);
1080 fprintf(stderr
, "a = %ld\n", a
);
1081 fdump(stderr
, "a (u - v)", aumv
.P
);
1082 fdump(stderr
, "w + a (u - v)", wpaumv
.P
);
1083 fdump(stderr
, "x", x
->P
);
1084 fdump(stderr
, "y", y
->P
);
1085 fdump(stderr
, "x - y", xmy
.P
);
1086 fdump(stderr
, "(x - y) (w + a (u - v))", z
.P
);
1087 f25519_load(&zz
, (const octet
*)dv
[6].buf
); fdump(stderr
, "z", zz
.P
);
1093 static test_chunk tests
[] = {
1094 { "add", vrf_add
, { &type_f25519
, &type_f25519
, &type_f25519_ref
} },
1095 { "sub", vrf_sub
, { &type_f25519
, &type_f25519
, &type_f25519_ref
} },
1096 { "mul", vrf_mul
, { &type_f25519
, &type_f25519
, &type_f25519_ref
} },
1097 { "mulconst", vrf_mulc
, { &type_f25519
, &type_long
, &type_f25519_ref
} },
1098 { "condswap", vrf_condswap
,
1099 { &type_f25519
, &type_f25519
, &type_uint32
,
1100 &type_f25519_ref
, &type_f25519_ref
} },
1101 { "sqr", vrf_sqr
, { &type_f25519
, &type_f25519_ref
} },
1102 { "inv", vrf_inv
, { &type_f25519
, &type_f25519_ref
} },
1103 { "sub-mulc-add-sub-mul", vrf_sub_mulc_add_sub_mul
,
1104 { &type_f25519
, &type_f25519
, &type_long
, &type_f25519
,
1105 &type_f25519
, &type_f25519
, &type_f25519_ref
} },
1109 int main(int argc
, char *argv
[])
1111 test_run(argc
, argv
, tests
, SRCDIR
"/t/f25519");
1117 /*----- That's all, folks -------------------------------------------------*/