| 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 Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #include "f25519.h" |
| 33 | |
| 34 | /*----- Basic setup -------------------------------------------------------*/ |
| 35 | |
| 36 | #if F25519_IMPL == 26 |
| 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 |
| 39 | * paper. |
| 40 | */ |
| 41 | |
| 42 | typedef int32 piece; typedef int64 dblpiece; |
| 43 | typedef uint32 upiece; typedef uint64 udblpiece; |
| 44 | #define P p26 |
| 45 | #define PIECEWD(i) ((i)%2 ? 25 : 26) |
| 46 | #define NPIECE 10 |
| 47 | |
| 48 | #define M26 0x03ffffffu |
| 49 | #define M25 0x01ffffffu |
| 50 | #define B26 0x04000000u |
| 51 | #define B25 0x02000000u |
| 52 | #define B24 0x01000000u |
| 53 | |
| 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]; \ |
| 61 | } while (0) |
| 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; \ |
| 68 | } while (0) |
| 69 | |
| 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. |
| 74 | */ |
| 75 | |
| 76 | typedef int16 piece; typedef int32 dblpiece; |
| 77 | typedef uint16 upiece; typedef uint32 udblpiece; |
| 78 | #define P p10 |
| 79 | #define PIECEWD(i) \ |
| 80 | ((i) == 5 || (i) == 10 || (i) == 15 || (i) == 20 || (i) == 25 ? 9 : 10) |
| 81 | #define NPIECE 26 |
| 82 | |
| 83 | #define B10 0x0400 |
| 84 | #define B9 0x200 |
| 85 | #define B8 0x100 |
| 86 | #define M10 0x3ff |
| 87 | #define M9 0x1ff |
| 88 | |
| 89 | #endif |
| 90 | |
| 91 | /*----- Debugging machinery -----------------------------------------------*/ |
| 92 | |
| 93 | #if defined(F25519_DEBUG) || defined(TEST_RIG) |
| 94 | |
| 95 | #include <stdio.h> |
| 96 | |
| 97 | #include "mp.h" |
| 98 | #include "mptext.h" |
| 99 | |
| 100 | static mp *get_2p255m91(void) |
| 101 | { |
| 102 | mpw w19 = 19; |
| 103 | mp *p = MP_NEW, m19; |
| 104 | |
| 105 | p = mp_setbit(p, MP_ZERO, 255); |
| 106 | mp_build(&m19, &w19, &w19 + 1); |
| 107 | p = mp_sub(p, p, &m19); |
| 108 | return (p); |
| 109 | } |
| 110 | |
| 111 | DEF_FDUMP(fdump, piece, PIECEWD, NPIECE, 32, get_2p255m91()) |
| 112 | |
| 113 | #endif |
| 114 | |
| 115 | /*----- Loading and storing -----------------------------------------------*/ |
| 116 | |
| 117 | /* --- @f25519_load@ --- * |
| 118 | * |
| 119 | * Arguments: @f25519 *z@ = where to store the result |
| 120 | * @const octet xv[32]@ = source to read |
| 121 | * |
| 122 | * Returns: --- |
| 123 | * |
| 124 | * Use: Reads an element of %$\gf{2^{255} - 19}$% in external |
| 125 | * representation from @xv@ and stores it in @z@. |
| 126 | * |
| 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 |
| 131 | * feature. |
| 132 | */ |
| 133 | |
| 134 | void f25519_load(f25519 *z, const octet xv[32]) |
| 135 | { |
| 136 | #if F25519_IMPL == 26 |
| 137 | |
| 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; |
| 143 | |
| 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. |
| 147 | * |
| 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. |
| 150 | * |
| 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. |
| 153 | */ |
| 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); |
| 164 | |
| 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 |
| 172 | * bit. |
| 173 | * |
| 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: |
| 176 | * |
| 177 | * { 2^25 if i even |
| 178 | * |x_i| <= { |
| 179 | * { 2^24 if i odd |
| 180 | * |
| 181 | * Note that we don't try for a canonical representation here: both upper |
| 182 | * and lower bounds are achievable. |
| 183 | * |
| 184 | * All of the x_i at this point are positive, so we don't need to do |
| 185 | * anything wierd when masking them. |
| 186 | */ |
| 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); |
| 197 | x0 += c; |
| 198 | |
| 199 | /* And with that, we're done. */ |
| 200 | STASH(z, x); |
| 201 | |
| 202 | #elif F25519_IMPL == 10 |
| 203 | |
| 204 | piece x[NPIECE]; |
| 205 | unsigned i, j, n, wd; |
| 206 | uint32 a; |
| 207 | int b, c; |
| 208 | |
| 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; |
| 212 | if (n >= wd) { |
| 213 | x[j++] = a&MASK(wd); |
| 214 | a >>= wd; n -= wd; |
| 215 | wd = PIECEWD(j); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* There's a little bit left over from the top byte. Carry it into the low |
| 220 | * piece. |
| 221 | */ |
| 222 | x[0] += 19*(int)(a&MASK(n)); |
| 223 | |
| 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. |
| 227 | */ |
| 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--) { |
| 232 | wd = PIECEWD(i) - 1; |
| 233 | b = x[i]&BIT(wd); |
| 234 | x[i + 1] += b >> wd; |
| 235 | x[i] -= b << 1; |
| 236 | } |
| 237 | b = x[0]&B9; |
| 238 | x[1] += (b >> 9) + (x[0] >> 10); |
| 239 | x[0] = (x[0]&M10) - (b << 1) + c; |
| 240 | |
| 241 | /* And we're done. */ |
| 242 | for (i = 0; i < NPIECE; i++) z->P[i] = x[i]; |
| 243 | |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | /* --- @f25519_store@ --- * |
| 248 | * |
| 249 | * Arguments: @octet zv[32]@ = where to write the result |
| 250 | * @const f25519 *x@ = the field element to write |
| 251 | * |
| 252 | * Returns: --- |
| 253 | * |
| 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. |
| 257 | */ |
| 258 | |
| 259 | void f25519_store(octet zv[32], const f25519 *x) |
| 260 | { |
| 261 | #if F25519_IMPL == 26 |
| 262 | |
| 263 | piece PIECES(x), PIECES(y), c, d; |
| 264 | uint32 zw0, zw1, zw2, zw3, zw4, zw5, zw6, zw7; |
| 265 | mask32 m; |
| 266 | |
| 267 | FETCH(x, x); |
| 268 | |
| 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. |
| 274 | * |
| 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 |
| 280 | * before this? |
| 281 | * |
| 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 |
| 286 | * x_9, so |
| 287 | * |
| 288 | * -2^235 < x + 19 c_9 < 2^255 + 2^235 |
| 289 | * |
| 290 | * Here, the x_i are signed, so we must be cautious about bithacking them. |
| 291 | */ |
| 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; |
| 303 | |
| 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 |
| 307 | * do nothing. |
| 308 | * |
| 309 | * But conditional behaviour is bad, m'kay. So here's what we do instead. |
| 310 | * |
| 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. |
| 314 | */ |
| 315 | m = SIGN(c); |
| 316 | d = m&1; |
| 317 | |
| 318 | /* Now do the addition/subtraction. Remember that all of the x_i are |
| 319 | * nonnegative, so shifting and masking are safe and easy. |
| 320 | */ |
| 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; |
| 331 | |
| 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). |
| 338 | */ |
| 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); |
| 345 | |
| 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); |
| 355 | |
| 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); |
| 361 | |
| 362 | #elif F25519_IMPL == 10 |
| 363 | |
| 364 | piece y[NPIECE], yy[NPIECE], c, d; |
| 365 | unsigned i, j, n, wd; |
| 366 | uint32 m, a; |
| 367 | |
| 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]; |
| 370 | |
| 371 | /* First, propagate the carries throughout the pieces. |
| 372 | * |
| 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 |
| 378 | * y before this? |
| 379 | * |
| 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 |
| 384 | * y_25, so |
| 385 | * |
| 386 | * -2^251 < y + 19 c_25 < 2^255 + 2^251 |
| 387 | * |
| 388 | * Here, the y_i are signed, so we must be cautious about bithacking them. |
| 389 | * |
| 390 | * (Rather closer than the 10-piece case above, but still doable in one |
| 391 | * pass.) |
| 392 | */ |
| 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++) { |
| 396 | wd = PIECEWD(i); |
| 397 | y[i] += c; |
| 398 | c = ASR(piece, y[i], wd); |
| 399 | y[i] = (upiece)y[i]&MASK(wd); |
| 400 | } |
| 401 | |
| 402 | /* Now the addition or subtraction. */ |
| 403 | m = SIGN(c); |
| 404 | d = m&1; |
| 405 | |
| 406 | d += y[0] + (19 ^ (M10&m)); |
| 407 | yy[0] = d&M10; |
| 408 | d >>= 10; |
| 409 | for (i = 1; i < NPIECE; i++) { |
| 410 | wd = PIECEWD(i); |
| 411 | d += y[i] + (MASK(wd)&m); |
| 412 | yy[i] = d&MASK(wd); |
| 413 | d >>= wd; |
| 414 | } |
| 415 | |
| 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); |
| 419 | |
| 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); |
| 423 | while (n >= 8) { |
| 424 | zv[j++] = a&0xff; |
| 425 | a >>= 8; n -= 8; |
| 426 | } |
| 427 | } |
| 428 | zv[j++] = a; |
| 429 | |
| 430 | #endif |
| 431 | } |
| 432 | |
| 433 | /* --- @f25519_set@ --- * |
| 434 | * |
| 435 | * Arguments: @f25519 *z@ = where to write the result |
| 436 | * @int a@ = a small-ish constant |
| 437 | * |
| 438 | * Returns: --- |
| 439 | * |
| 440 | * Use: Sets @z@ to equal @a@. |
| 441 | */ |
| 442 | |
| 443 | void f25519_set(f25519 *x, int a) |
| 444 | { |
| 445 | unsigned i; |
| 446 | |
| 447 | x->P[0] = a; |
| 448 | for (i = 1; i < NPIECE; i++) x->P[i] = 0; |
| 449 | } |
| 450 | |
| 451 | /*----- Basic arithmetic --------------------------------------------------*/ |
| 452 | |
| 453 | /* --- @f25519_add@ --- * |
| 454 | * |
| 455 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) |
| 456 | * @const f25519 *x, *y@ = two operands |
| 457 | * |
| 458 | * Returns: --- |
| 459 | * |
| 460 | * Use: Set @z@ to the sum %$x + y$%. |
| 461 | */ |
| 462 | |
| 463 | void f25519_add(f25519 *z, const f25519 *x, const f25519 *y) |
| 464 | { |
| 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 |
| 472 | unsigned i; |
| 473 | for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] + y->P[i]; |
| 474 | #endif |
| 475 | } |
| 476 | |
| 477 | /* --- @f25519_sub@ --- * |
| 478 | * |
| 479 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) |
| 480 | * @const f25519 *x, *y@ = two operands |
| 481 | * |
| 482 | * Returns: --- |
| 483 | * |
| 484 | * Use: Set @z@ to the difference %$x - y$%. |
| 485 | */ |
| 486 | |
| 487 | void f25519_sub(f25519 *z, const f25519 *x, const f25519 *y) |
| 488 | { |
| 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 |
| 496 | unsigned i; |
| 497 | for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] - y->P[i]; |
| 498 | #endif |
| 499 | } |
| 500 | |
| 501 | /*----- Constant-time utilities -------------------------------------------*/ |
| 502 | |
| 503 | /* --- @f25519_condswap@ --- * |
| 504 | * |
| 505 | * Arguments: @f25519 *x, *y@ = two operands |
| 506 | * @uint32 m@ = a mask |
| 507 | * |
| 508 | * Returns: --- |
| 509 | * |
| 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. |
| 513 | */ |
| 514 | |
| 515 | void f25519_condswap(f25519 *x, f25519 *y, uint32 m) |
| 516 | { |
| 517 | mask32 mm = FIX_MASK32(m); |
| 518 | |
| 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 |
| 531 | unsigned i; |
| 532 | for (i = 0; i < NPIECE; i++) CONDSWAP(x->P[i], y->P[i], mm); |
| 533 | #endif |
| 534 | } |
| 535 | |
| 536 | /*----- Multiplication ----------------------------------------------------*/ |
| 537 | |
| 538 | #if F25519_IMPL == 26 |
| 539 | |
| 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. |
| 544 | */ |
| 545 | #define CARRYSTEP(z, x, m, b, f, xx, n) do { \ |
| 546 | (z) = (dblpiece)((udblpiece)(x)&(m)) - (b) + \ |
| 547 | (f)*ASR(dblpiece, (xx), (n)); \ |
| 548 | } while (0) |
| 549 | #define CARRY_REDUCE(z, x) do { \ |
| 550 | dblpiece PIECES(_t); \ |
| 551 | \ |
| 552 | /* Bias the input pieces. This keeps the carries and so on centred \ |
| 553 | * around zero rather than biased positive. \ |
| 554 | */ \ |
| 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; \ |
| 560 | \ |
| 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); \ |
| 572 | } while (0) |
| 573 | |
| 574 | #elif F25519_IMPL == 10 |
| 575 | |
| 576 | /* Perform carry propagation on X. */ |
| 577 | static void carry_reduce(dblpiece x[NPIECE]) |
| 578 | { |
| 579 | /* Initial bounds: we assume |x_i| < 2^31 - 2^27. */ |
| 580 | |
| 581 | unsigned i, j; |
| 582 | dblpiece c; |
| 583 | |
| 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. |
| 587 | * |
| 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. |
| 591 | * |
| 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. |
| 597 | * |
| 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 |
| 603 | * propagation. |
| 604 | * |
| 605 | * Be careful with the bit hacking because the quantities involved are |
| 606 | * signed. |
| 607 | */ |
| 608 | |
| 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 |
| 611 | * right thing. |
| 612 | */ |
| 613 | #define CARRY(i, wd, b, m) do { \ |
| 614 | x[i] += (b); \ |
| 615 | c = ASR(dblpiece, x[i], (wd)); \ |
| 616 | x[i] = (dblpiece)((udblpiece)x[i]&(m)) - (b); \ |
| 617 | } while (0) |
| 618 | |
| 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++; } |
| 626 | } |
| 627 | while (i < 25) { x[i] += c; CARRY( i, 10, B9, M10); i++; } |
| 628 | x[25] += c; |
| 629 | |
| 630 | #undef CARRY |
| 631 | } |
| 632 | |
| 633 | #endif |
| 634 | |
| 635 | /* --- @f25519_mulconst@ --- * |
| 636 | * |
| 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}$%. |
| 640 | * |
| 641 | * Returns: --- |
| 642 | * |
| 643 | * Use: Set @z@ to the product %$a x$%. |
| 644 | */ |
| 645 | |
| 646 | void f25519_mulconst(f25519 *z, const f25519 *x, long a) |
| 647 | { |
| 648 | #if F25519_IMPL == 26 |
| 649 | |
| 650 | piece PIECES(x); |
| 651 | dblpiece PIECES(z), aa = a; |
| 652 | |
| 653 | FETCH(x, x); |
| 654 | |
| 655 | /* Suppose that |x_i| <= 2^27, and |a| <= 2^23. Then we'll have |
| 656 | * |z_i| <= 2^50. |
| 657 | */ |
| 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; |
| 660 | |
| 661 | /* Following `CARRY_REDUCE', we'll have |z_i| <= 2^26. */ |
| 662 | CARRY_REDUCE(z, z); |
| 663 | STASH(z, z); |
| 664 | |
| 665 | #elif F25519_IMPL == 10 |
| 666 | |
| 667 | dblpiece y[NPIECE]; |
| 668 | unsigned i; |
| 669 | |
| 670 | for (i = 0; i < NPIECE; i++) y[i] = a*x->P[i]; |
| 671 | carry_reduce(y); |
| 672 | for (i = 0; i < NPIECE; i++) z->P[i] = y[i]; |
| 673 | |
| 674 | #endif |
| 675 | } |
| 676 | |
| 677 | /* --- @f25519_mul@ --- * |
| 678 | * |
| 679 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) |
| 680 | * @const f25519 *x, *y@ = two operands |
| 681 | * |
| 682 | * Returns: --- |
| 683 | * |
| 684 | * Use: Set @z@ to the product %$x y$%. |
| 685 | */ |
| 686 | |
| 687 | void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y) |
| 688 | { |
| 689 | #if F25519_IMPL == 26 |
| 690 | |
| 691 | piece PIECES(x), PIECES(y); |
| 692 | dblpiece PIECES(z); |
| 693 | unsigned i; |
| 694 | |
| 695 | FETCH(x, x); FETCH(y, y); |
| 696 | |
| 697 | /* Suppose that |x_i|, |y_i| <= 2^27. Then we'll have |
| 698 | * |
| 699 | * |z_0| <= 267*2^54 |
| 700 | * |z_1| <= 154*2^54 |
| 701 | * |z_2| <= 213*2^54 |
| 702 | * |z_3| <= 118*2^54 |
| 703 | * |z_4| <= 159*2^54 |
| 704 | * |z_5| <= 82*2^54 |
| 705 | * |z_6| <= 105*2^54 |
| 706 | * |z_7| <= 46*2^54 |
| 707 | * |z_8| <= 51*2^54 |
| 708 | * |z_9| <= 10*2^54 |
| 709 | * |
| 710 | * all of which are less than 2^63 - 2^25. |
| 711 | */ |
| 712 | |
| 713 | #define M(a, b) ((dblpiece)(a)*(b)) |
| 714 | z0 = M(x0, y0) + |
| 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) + |
| 721 | 2* M(x1, y1) + |
| 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)) + |
| 736 | 19* M(x8, y8) + |
| 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)) + |
| 743 | 38* M(x9, y9); |
| 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); |
| 746 | #undef M |
| 747 | |
| 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 |
| 751 | * multiplication. |
| 752 | */ |
| 753 | for (i = 0; i < 2; i++) CARRY_REDUCE(z, z); |
| 754 | STASH(z, z); |
| 755 | |
| 756 | #elif F25519_IMPL == 10 |
| 757 | |
| 758 | dblpiece u[NPIECE], t, tt, p; |
| 759 | unsigned i, j, k; |
| 760 | |
| 761 | /* This is unpleasant. Honestly, this table seems to be the best way of |
| 762 | * doing it. |
| 763 | */ |
| 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 |
| 767 | }; |
| 768 | |
| 769 | /* First pass: things we must multiply by 19 or 38. */ |
| 770 | for (i = 0; i < NPIECE - 1; i++) { |
| 771 | t = tt = 0; |
| 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; |
| 775 | else t += p; |
| 776 | } |
| 777 | u[i] = 19*(t + 2*tt); |
| 778 | } |
| 779 | u[NPIECE - 1] = 0; |
| 780 | |
| 781 | /* Second pass: things we must multiply by 1 or 2. */ |
| 782 | for (i = 0; i < NPIECE; i++) { |
| 783 | t = tt = 0; |
| 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; |
| 787 | else t += p; |
| 788 | } |
| 789 | u[i] += t + 2*tt; |
| 790 | } |
| 791 | |
| 792 | /* And we're done. */ |
| 793 | carry_reduce(u); |
| 794 | for (i = 0; i < NPIECE; i++) z->P[i] = u[i]; |
| 795 | |
| 796 | #endif |
| 797 | } |
| 798 | |
| 799 | /* --- @f25519_sqr@ --- * |
| 800 | * |
| 801 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@ or @y@) |
| 802 | * @const f25519 *x@ = an operand |
| 803 | * |
| 804 | * Returns: --- |
| 805 | * |
| 806 | * Use: Set @z@ to the square %$x^2$%. |
| 807 | */ |
| 808 | |
| 809 | void f25519_sqr(f25519 *z, const f25519 *x) |
| 810 | { |
| 811 | #if F25519_IMPL == 26 |
| 812 | |
| 813 | piece PIECES(x); |
| 814 | dblpiece PIECES(z); |
| 815 | unsigned i; |
| 816 | |
| 817 | FETCH(x, x); |
| 818 | |
| 819 | /* See `f25519_mul' for bounds. */ |
| 820 | |
| 821 | #define M(a, b) ((dblpiece)(a)*(b)) |
| 822 | z0 = M(x0, x0) + |
| 823 | 38*(M(x2, x8) + M(x4, x6) + M(x5, x5)) + |
| 824 | 76*(M(x1, x9) + M(x3, x7)); |
| 825 | z1 = 2* M(x0, x1) + |
| 826 | 38*(M(x2, x9) + M(x3, x8) + M(x4, x7) + M(x5, x6)); |
| 827 | z2 = 2*(M(x0, x2) + M(x1, x1)) + |
| 828 | 19* M(x6, x6) + |
| 829 | 38* M(x4, x8) + |
| 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)); |
| 833 | z4 = M(x2, x2) + |
| 834 | 2* M(x0, x4) + |
| 835 | 4* M(x1, x3) + |
| 836 | 38*(M(x6, x8) + M(x7, x7)) + |
| 837 | 76* M(x5, x9); |
| 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)) + |
| 841 | 4* M(x1, x5) + |
| 842 | 19* M(x8, x8) + |
| 843 | 76* M(x7, x9); |
| 844 | z7 = 2*(M(x0, x7) + M(x1, x6) + M(x2, x5) + M(x3, x4)) + |
| 845 | 38* M(x8, x9); |
| 846 | z8 = M(x4, x4) + |
| 847 | 2*(M(x0, x8) + M(x2, x6)) + |
| 848 | 4*(M(x1, x7) + M(x3, x5)) + |
| 849 | 38* M(x9, x9); |
| 850 | z9 = 2*(M(x0, x9) + M(x1, x8) + M(x2, x7) + M(x3, x6) + M(x4, x5)); |
| 851 | #undef M |
| 852 | |
| 853 | /* See `f25519_mul' for details. */ |
| 854 | for (i = 0; i < 2; i++) CARRY_REDUCE(z, z); |
| 855 | STASH(z, z); |
| 856 | |
| 857 | #elif F25519_IMPL == 10 |
| 858 | f25519_mul(z, x, x); |
| 859 | #endif |
| 860 | } |
| 861 | |
| 862 | /*----- More complicated things -------------------------------------------*/ |
| 863 | |
| 864 | /* --- @f25519_inv@ --- * |
| 865 | * |
| 866 | * Arguments: @f25519 *z@ = where to put the result (may alias @x@) |
| 867 | * @const f25519 *x@ = an operand |
| 868 | * |
| 869 | * Returns: --- |
| 870 | * |
| 871 | * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If |
| 872 | * %$x = 0$% then @z@ is set to zero. This is considered a |
| 873 | * feature. |
| 874 | */ |
| 875 | |
| 876 | void f25519_inv(f25519 *z, const f25519 *x) |
| 877 | { |
| 878 | f25519 t, u, t2, t11, t2p10m1, t2p50m1; |
| 879 | unsigned i; |
| 880 | |
| 881 | #define SQRN(z, x, n) do { \ |
| 882 | f25519_sqr((z), (x)); \ |
| 883 | for (i = 1; i < (n); i++) f25519_sqr((z), (z)); \ |
| 884 | } while (0) |
| 885 | |
| 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 */ |
| 912 | |
| 913 | #undef SQRN |
| 914 | } |
| 915 | |
| 916 | /*----- Test rig ----------------------------------------------------------*/ |
| 917 | |
| 918 | #ifdef TEST_RIG |
| 919 | |
| 920 | #include <mLib/report.h> |
| 921 | #include <mLib/testrig.h> |
| 922 | |
| 923 | static void fixdstr(dstr *d) |
| 924 | { |
| 925 | if (d->len > 32) |
| 926 | die(1, "invalid length for f25519"); |
| 927 | else if (d->len < 32) { |
| 928 | dstr_ensure(d, 32); |
| 929 | memset(d->buf + d->len, 0, 32 - d->len); |
| 930 | d->len = 32; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | static void cvt_f25519(const char *buf, dstr *d) |
| 935 | { |
| 936 | dstr dd = DSTR_INIT; |
| 937 | |
| 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); |
| 941 | dstr_destroy(&dd); |
| 942 | } |
| 943 | |
| 944 | static void dump_f25519(dstr *d, FILE *fp) |
| 945 | { fdump(stderr, "???", (const piece *)d->buf); } |
| 946 | |
| 947 | static void cvt_f25519_ref(const char *buf, dstr *d) |
| 948 | { type_hex.cvt(buf, d); fixdstr(d); } |
| 949 | |
| 950 | static void dump_f25519_ref(dstr *d, FILE *fp) |
| 951 | { |
| 952 | f25519 x; |
| 953 | |
| 954 | f25519_load(&x, (const octet *)d->buf); |
| 955 | fdump(stderr, "???", x.P); |
| 956 | } |
| 957 | |
| 958 | static int eq(const f25519 *x, dstr *d) |
| 959 | { octet b[32]; f25519_store(b, x); return (memcmp(b, d->buf, 32) == 0); } |
| 960 | |
| 961 | static const test_type |
| 962 | type_f25519 = { cvt_f25519, dump_f25519 }, |
| 963 | type_f25519_ref = { cvt_f25519_ref, dump_f25519_ref }; |
| 964 | |
| 965 | #define TEST_UNOP(op) \ |
| 966 | static int vrf_##op(dstr dv[]) \ |
| 967 | { \ |
| 968 | f25519 *x = (f25519 *)dv[0].buf; \ |
| 969 | f25519 z, zz; \ |
| 970 | int ok = 1; \ |
| 971 | \ |
| 972 | f25519_##op(&z, x); \ |
| 973 | if (!eq(&z, &dv[1])) { \ |
| 974 | ok = 0; \ |
| 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); \ |
| 980 | } \ |
| 981 | \ |
| 982 | return (ok); \ |
| 983 | } |
| 984 | |
| 985 | TEST_UNOP(sqr) |
| 986 | TEST_UNOP(inv) |
| 987 | |
| 988 | #define TEST_BINOP(op) \ |
| 989 | static int vrf_##op(dstr dv[]) \ |
| 990 | { \ |
| 991 | f25519 *x = (f25519 *)dv[0].buf, *y = (f25519 *)dv[1].buf; \ |
| 992 | f25519 z, zz; \ |
| 993 | int ok = 1; \ |
| 994 | \ |
| 995 | f25519_##op(&z, x, y); \ |
| 996 | if (!eq(&z, &dv[2])) { \ |
| 997 | ok = 0; \ |
| 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); \ |
| 1004 | } \ |
| 1005 | \ |
| 1006 | return (ok); \ |
| 1007 | } |
| 1008 | |
| 1009 | TEST_BINOP(add) |
| 1010 | TEST_BINOP(sub) |
| 1011 | TEST_BINOP(mul) |
| 1012 | |
| 1013 | static int vrf_mulc(dstr dv[]) |
| 1014 | { |
| 1015 | f25519 *x = (f25519 *)dv[0].buf; |
| 1016 | long a = *(const long *)dv[1].buf; |
| 1017 | f25519 z, zz; |
| 1018 | int ok = 1; |
| 1019 | |
| 1020 | f25519_mulconst(&z, x, a); |
| 1021 | if (!eq(&z, &dv[2])) { |
| 1022 | ok = 0; |
| 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); |
| 1029 | } |
| 1030 | |
| 1031 | return (ok); |
| 1032 | } |
| 1033 | |
| 1034 | static int vrf_condswap(dstr dv[]) |
| 1035 | { |
| 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; |
| 1039 | int ok = 1; |
| 1040 | |
| 1041 | f25519_condswap(&xx, &yy, m); |
| 1042 | if (!eq(&xx, &dv[3]) || !eq(&yy, &dv[4])) { |
| 1043 | ok = 0; |
| 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); |
| 1054 | } |
| 1055 | |
| 1056 | return (ok); |
| 1057 | } |
| 1058 | |
| 1059 | static int vrf_sub_mulc_add_sub_mul(dstr dv[]) |
| 1060 | { |
| 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; |
| 1066 | int ok = 1; |
| 1067 | |
| 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); |
| 1073 | |
| 1074 | if (!eq(&z, &dv[6])) { |
| 1075 | ok = 0; |
| 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); |
| 1088 | } |
| 1089 | |
| 1090 | return (ok); |
| 1091 | } |
| 1092 | |
| 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 } }, |
| 1106 | { 0, 0, { 0 } } |
| 1107 | }; |
| 1108 | |
| 1109 | int main(int argc, char *argv[]) |
| 1110 | { |
| 1111 | test_run(argc, argv, tests, SRCDIR "/t/f25519"); |
| 1112 | return (0); |
| 1113 | } |
| 1114 | |
| 1115 | #endif |
| 1116 | |
| 1117 | /*----- That's all, folks -------------------------------------------------*/ |