| 1 | /* |
| 2 | * fgoldi.c: arithmetic modulo 2^448 - 2^224 - 1 |
| 3 | */ |
| 4 | /* |
| 5 | * This file is Free Software. It has been modified to as part of its |
| 6 | * incorporation into secnet. |
| 7 | * |
| 8 | * Copyright 2017 Mark Wooding |
| 9 | * |
| 10 | * You may redistribute this file and/or modify it under the terms of |
| 11 | * the permissive licence shown below. |
| 12 | * |
| 13 | * You may redistribute secnet as a whole and/or modify it under the |
| 14 | * terms of the GNU General Public License as published by the Free |
| 15 | * Software Foundation; either version 3, or (at your option) any |
| 16 | * later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, see |
| 25 | * https://www.gnu.org/licenses/gpl.html. |
| 26 | */ |
| 27 | /* |
| 28 | * Imported from Catacomb, and modified for Secnet (2017-04-30): |
| 29 | * |
| 30 | * * Use `fake-mLib-bits.h' in place of the real <mLib/bits.h>. |
| 31 | * |
| 32 | * * Remove the 16/32-bit implementation, since C99 always has 64-bit |
| 33 | * arithmetic. |
| 34 | * |
| 35 | * * Remove the test rig code: a replacement is in a separate source file. |
| 36 | * |
| 37 | * The file's original comment headers are preserved below. |
| 38 | */ |
| 39 | /* -*-c-*- |
| 40 | * |
| 41 | * Arithmetic in the Goldilocks field GF(2^448 - 2^224 - 1) |
| 42 | * |
| 43 | * (c) 2017 Straylight/Edgeware |
| 44 | */ |
| 45 | |
| 46 | /*----- Licensing notice --------------------------------------------------* |
| 47 | * |
| 48 | * This file is part of Catacomb. |
| 49 | * |
| 50 | * Catacomb is free software; you can redistribute it and/or modify |
| 51 | * it under the terms of the GNU Library General Public License as |
| 52 | * published by the Free Software Foundation; either version 2 of the |
| 53 | * License, or (at your option) any later version. |
| 54 | * |
| 55 | * Catacomb is distributed in the hope that it will be useful, |
| 56 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 57 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 58 | * GNU Library General Public License for more details. |
| 59 | * |
| 60 | * You should have received a copy of the GNU Library General Public |
| 61 | * License along with Catacomb; if not, write to the Free |
| 62 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 63 | * MA 02111-1307, USA. |
| 64 | */ |
| 65 | |
| 66 | /*----- Header files ------------------------------------------------------*/ |
| 67 | |
| 68 | #include "fgoldi.h" |
| 69 | |
| 70 | /*----- Basic setup -------------------------------------------------------* |
| 71 | * |
| 72 | * Let φ = 2^224; then p = φ^2 - φ - 1, and, in GF(p), we have φ^2 = φ + 1 |
| 73 | * (hence the name). |
| 74 | */ |
| 75 | |
| 76 | /* We represent an element of GF(p) as 16 28-bit signed integer pieces x_i: |
| 77 | * x = SUM_{0<=i<16} x_i 2^(28i). |
| 78 | */ |
| 79 | |
| 80 | typedef int32 piece; typedef int64 dblpiece; |
| 81 | typedef uint32 upiece; typedef uint64 udblpiece; |
| 82 | #define PIECEWD(i) 28 |
| 83 | #define NPIECE 16 |
| 84 | #define P p28 |
| 85 | |
| 86 | #define B28 0x10000000u |
| 87 | #define B27 0x08000000u |
| 88 | #define M28 0x0fffffffu |
| 89 | #define M27 0x07ffffffu |
| 90 | #define M32 0xffffffffu |
| 91 | |
| 92 | /*----- Debugging machinery -----------------------------------------------*/ |
| 93 | |
| 94 | #if defined(FGOLDI_DEBUG) |
| 95 | |
| 96 | #include <stdio.h> |
| 97 | |
| 98 | #include "mp.h" |
| 99 | #include "mptext.h" |
| 100 | |
| 101 | static mp *get_pgoldi(void) |
| 102 | { |
| 103 | mp *p = MP_NEW, *t = MP_NEW; |
| 104 | |
| 105 | p = mp_setbit(p, MP_ZERO, 448); |
| 106 | t = mp_setbit(t, MP_ZERO, 224); |
| 107 | p = mp_sub(p, p, t); |
| 108 | p = mp_sub(p, p, MP_ONE); |
| 109 | mp_drop(t); |
| 110 | return (p); |
| 111 | } |
| 112 | |
| 113 | DEF_FDUMP(fdump, piece, PIECEWD, NPIECE, 56, get_pgoldi()) |
| 114 | |
| 115 | #endif |
| 116 | |
| 117 | /*----- Loading and storing -----------------------------------------------*/ |
| 118 | |
| 119 | /* --- @fgoldi_load@ --- * |
| 120 | * |
| 121 | * Arguments: @fgoldi *z@ = where to store the result |
| 122 | * @const octet xv[56]@ = source to read |
| 123 | * |
| 124 | * Returns: --- |
| 125 | * |
| 126 | * Use: Reads an element of %$\gf{2^{448} - 2^{224} - 19}$% in |
| 127 | * external representation from @xv@ and stores it in @z@. |
| 128 | * |
| 129 | * External representation is little-endian base-256. Some |
| 130 | * elements have multiple encodings, which are not produced by |
| 131 | * correct software; use of noncanonical encodings is not an |
| 132 | * error, and toleration of them is considered a performance |
| 133 | * feature. |
| 134 | */ |
| 135 | |
| 136 | void fgoldi_load(fgoldi *z, const octet xv[56]) |
| 137 | { |
| 138 | unsigned i; |
| 139 | uint32 xw[14]; |
| 140 | piece b, c; |
| 141 | |
| 142 | /* First, read the input value as words. */ |
| 143 | for (i = 0; i < 14; i++) xw[i] = LOAD32_L(xv + 4*i); |
| 144 | |
| 145 | /* Extract unsigned 28-bit pieces from the words. */ |
| 146 | z->P[ 0] = (xw[ 0] >> 0)&M28; |
| 147 | z->P[ 7] = (xw[ 6] >> 4)&M28; |
| 148 | z->P[ 8] = (xw[ 7] >> 0)&M28; |
| 149 | z->P[15] = (xw[13] >> 4)&M28; |
| 150 | for (i = 1; i < 7; i++) { |
| 151 | z->P[i + 0] = ((xw[i + 0] << (4*i)) | (xw[i - 1] >> (32 - 4*i)))&M28; |
| 152 | z->P[i + 8] = ((xw[i + 7] << (4*i)) | (xw[i + 6] >> (32 - 4*i)))&M28; |
| 153 | } |
| 154 | |
| 155 | /* Convert the nonnegative pieces into a balanced signed representation, so |
| 156 | * each piece ends up in the interval |z_i| <= 2^27. For each piece, if |
| 157 | * its top bit is set, lend a bit leftwards; in the case of z_15, reduce |
| 158 | * this bit by adding it onto z_0 and z_8, since this is the φ^2 bit, and |
| 159 | * φ^2 = φ + 1. We delay this carry until after all of the pieces have |
| 160 | * been balanced. If we don't do this, then we have to do a more expensive |
| 161 | * test for nonzeroness to decide whether to lend a bit leftwards rather |
| 162 | * than just testing a single bit. |
| 163 | * |
| 164 | * Note that we don't try for a canonical representation here: both upper |
| 165 | * and lower bounds are achievable. |
| 166 | */ |
| 167 | b = z->P[15]&B27; z->P[15] -= b << 1; c = b >> 27; |
| 168 | for (i = NPIECE - 1; i--; ) |
| 169 | { b = z->P[i]&B27; z->P[i] -= b << 1; z->P[i + 1] += b >> 27; } |
| 170 | z->P[0] += c; z->P[8] += c; |
| 171 | } |
| 172 | |
| 173 | /* --- @fgoldi_store@ --- * |
| 174 | * |
| 175 | * Arguments: @octet zv[56]@ = where to write the result |
| 176 | * @const fgoldi *x@ = the field element to write |
| 177 | * |
| 178 | * Returns: --- |
| 179 | * |
| 180 | * Use: Stores a field element in the given octet vector in external |
| 181 | * representation. A canonical encoding is always stored. |
| 182 | */ |
| 183 | |
| 184 | void fgoldi_store(octet zv[56], const fgoldi *x) |
| 185 | { |
| 186 | piece y[NPIECE], yy[NPIECE], c, d; |
| 187 | uint32 u, v; |
| 188 | mask32 m; |
| 189 | unsigned i; |
| 190 | |
| 191 | for (i = 0; i < NPIECE; i++) y[i] = x->P[i]; |
| 192 | |
| 193 | /* First, propagate the carries. By the end of this, we'll have all of the |
| 194 | * the pieces canonically sized and positive, and maybe there'll be |
| 195 | * (signed) carry out. The carry c is in { -1, 0, +1 }, and the remaining |
| 196 | * value will be in the half-open interval [0, φ^2). The whole represented |
| 197 | * value is then y + φ^2 c. |
| 198 | * |
| 199 | * Assume that we start out with |y_i| <= 2^30. We start off by cutting |
| 200 | * off and reducing the carry c_15 from the topmost piece, y_15. This |
| 201 | * leaves 0 <= y_15 < 2^28; and we'll have |c_15| <= 4. We'll add this |
| 202 | * onto y_0 and y_8, and propagate the carries. It's very clear that we'll |
| 203 | * end up with |y + (φ + 1) c_15 - φ^2/2| << φ^2. |
| 204 | * |
| 205 | * Here, the y_i are signed, so we must be cautious about bithacking them. |
| 206 | */ |
| 207 | c = ASR(piece, y[15], 28); y[15] = (upiece)y[15]&M28; y[8] += c; |
| 208 | for (i = 0; i < NPIECE; i++) |
| 209 | { y[i] += c; c = ASR(piece, y[i], 28); y[i] = (upiece)y[i]&M28; } |
| 210 | |
| 211 | /* Now we have a slightly fiddly job to do. If c = +1, or if c = 0 and |
| 212 | * y >= p, then we should subtract p from the whole value; if c = -1 then |
| 213 | * we should add p; and otherwise we should do nothing. |
| 214 | * |
| 215 | * But conditional behaviour is bad, m'kay. So here's what we do instead. |
| 216 | * |
| 217 | * The first job is to sort out what we wanted to do. If c = -1 then we |
| 218 | * want to (a) invert the constant addend and (b) feed in a carry-in; |
| 219 | * otherwise, we don't. |
| 220 | */ |
| 221 | m = SIGN(c)&M28; |
| 222 | d = m&1; |
| 223 | |
| 224 | /* Now do the addition/subtraction. Remember that all of the y_i are |
| 225 | * nonnegative, so shifting and masking are safe and easy. |
| 226 | */ |
| 227 | d += y[0] + (1 ^ m); yy[0] = d&M28; d >>= 28; |
| 228 | for (i = 1; i < 8; i++) |
| 229 | { d += y[i] + m; yy[i] = d&M28; d >>= 28; } |
| 230 | d += y[8] + (1 ^ m); yy[8] = d&M28; d >>= 28; |
| 231 | for (i = 9; i < 16; i++) |
| 232 | { d += y[i] + m; yy[i] = d&M28; d >>= 28; } |
| 233 | |
| 234 | /* The final carry-out is in d; since we only did addition, and the y_i are |
| 235 | * nonnegative, then d is in { 0, 1 }. We want to keep y', rather than y, |
| 236 | * if (a) c /= 0 (in which case we know that the old value was |
| 237 | * unsatisfactory), or (b) if d = 1 (in which case, if c = 0, we know that |
| 238 | * the subtraction didn't cause a borrow, so we must be in the case where |
| 239 | * p <= y < φ^2. |
| 240 | */ |
| 241 | m = NONZEROP(c) | ~NONZEROP(d - 1); |
| 242 | for (i = 0; i < NPIECE; i++) y[i] = (yy[i]&m) | (y[i]&~m); |
| 243 | |
| 244 | /* Extract 32-bit words from the value. */ |
| 245 | for (i = 0; i < 7; i++) { |
| 246 | u = ((y[i + 0] >> (4*i)) | ((uint32)y[i + 1] << (28 - 4*i)))&M32; |
| 247 | v = ((y[i + 8] >> (4*i)) | ((uint32)y[i + 9] << (28 - 4*i)))&M32; |
| 248 | STORE32_L(zv + 4*i, u); |
| 249 | STORE32_L(zv + 4*i + 28, v); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* --- @fgoldi_set@ --- * |
| 254 | * |
| 255 | * Arguments: @fgoldi *z@ = where to write the result |
| 256 | * @int a@ = a small-ish constant |
| 257 | * |
| 258 | * Returns: --- |
| 259 | * |
| 260 | * Use: Sets @z@ to equal @a@. |
| 261 | */ |
| 262 | |
| 263 | void fgoldi_set(fgoldi *x, int a) |
| 264 | { |
| 265 | unsigned i; |
| 266 | |
| 267 | x->P[0] = a; |
| 268 | for (i = 1; i < NPIECE; i++) x->P[i] = 0; |
| 269 | } |
| 270 | |
| 271 | /*----- Basic arithmetic --------------------------------------------------*/ |
| 272 | |
| 273 | /* --- @fgoldi_add@ --- * |
| 274 | * |
| 275 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) |
| 276 | * @const fgoldi *x, *y@ = two operands |
| 277 | * |
| 278 | * Returns: --- |
| 279 | * |
| 280 | * Use: Set @z@ to the sum %$x + y$%. |
| 281 | */ |
| 282 | |
| 283 | void fgoldi_add(fgoldi *z, const fgoldi *x, const fgoldi *y) |
| 284 | { |
| 285 | unsigned i; |
| 286 | for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] + y->P[i]; |
| 287 | } |
| 288 | |
| 289 | /* --- @fgoldi_sub@ --- * |
| 290 | * |
| 291 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) |
| 292 | * @const fgoldi *x, *y@ = two operands |
| 293 | * |
| 294 | * Returns: --- |
| 295 | * |
| 296 | * Use: Set @z@ to the difference %$x - y$%. |
| 297 | */ |
| 298 | |
| 299 | void fgoldi_sub(fgoldi *z, const fgoldi *x, const fgoldi *y) |
| 300 | { |
| 301 | unsigned i; |
| 302 | for (i = 0; i < NPIECE; i++) z->P[i] = x->P[i] - y->P[i]; |
| 303 | } |
| 304 | |
| 305 | /*----- Constant-time utilities -------------------------------------------*/ |
| 306 | |
| 307 | /* --- @fgoldi_condswap@ --- * |
| 308 | * |
| 309 | * Arguments: @fgoldi *x, *y@ = two operands |
| 310 | * @uint32 m@ = a mask |
| 311 | * |
| 312 | * Returns: --- |
| 313 | * |
| 314 | * Use: If @m@ is zero, do nothing; if @m@ is all-bits-set, then |
| 315 | * exchange @x@ and @y@. If @m@ has some other value, then |
| 316 | * scramble @x@ and @y@ in an unhelpful way. |
| 317 | */ |
| 318 | |
| 319 | void fgoldi_condswap(fgoldi *x, fgoldi *y, uint32 m) |
| 320 | { |
| 321 | unsigned i; |
| 322 | mask32 mm = FIX_MASK32(m); |
| 323 | |
| 324 | for (i = 0; i < NPIECE; i++) CONDSWAP(x->P[i], y->P[i], mm); |
| 325 | } |
| 326 | |
| 327 | /*----- Multiplication ----------------------------------------------------*/ |
| 328 | |
| 329 | /* Let B = 2^63 - 1 be the largest value such that +B and -B can be |
| 330 | * represented in a double-precision piece. On entry, it must be the case |
| 331 | * that |X_i| <= M <= B - 2^27 for some M. If this is the case, then, on |
| 332 | * exit, we will have |Z_i| <= 2^27 + M/2^27. |
| 333 | */ |
| 334 | #define CARRY_REDUCE(z, x) do { \ |
| 335 | dblpiece _t[NPIECE], _c; \ |
| 336 | unsigned _i; \ |
| 337 | \ |
| 338 | /* Bias the input pieces. This keeps the carries and so on centred \ |
| 339 | * around zero rather than biased positive. \ |
| 340 | */ \ |
| 341 | for (_i = 0; _i < NPIECE; _i++) _t[_i] = (x)[_i] + B27; \ |
| 342 | \ |
| 343 | /* Calculate the reduced pieces. Careful with the bithacking. */ \ |
| 344 | _c = ASR(dblpiece, _t[15], 28); \ |
| 345 | (z)[0] = (dblpiece)((udblpiece)_t[0]&M28) - B27 + _c; \ |
| 346 | for (_i = 1; _i < NPIECE; _i++) { \ |
| 347 | (z)[_i] = (dblpiece)((udblpiece)_t[_i]&M28) - B27 + \ |
| 348 | ASR(dblpiece, _t[_i - 1], 28); \ |
| 349 | } \ |
| 350 | (z)[8] += _c; \ |
| 351 | } while (0) |
| 352 | |
| 353 | /* --- @fgoldi_mulconst@ --- * |
| 354 | * |
| 355 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@) |
| 356 | * @const fgoldi *x@ = an operand |
| 357 | * @long a@ = a small-ish constant; %$|a| < 2^{20}$%. |
| 358 | * |
| 359 | * Returns: --- |
| 360 | * |
| 361 | * Use: Set @z@ to the product %$a x$%. |
| 362 | */ |
| 363 | |
| 364 | void fgoldi_mulconst(fgoldi *z, const fgoldi *x, long a) |
| 365 | { |
| 366 | unsigned i; |
| 367 | dblpiece zz[NPIECE], aa = a; |
| 368 | |
| 369 | for (i = 0; i < NPIECE; i++) zz[i] = aa*x->P[i]; |
| 370 | CARRY_REDUCE(z->P, zz); |
| 371 | } |
| 372 | |
| 373 | /* --- @fgoldi_mul@ --- * |
| 374 | * |
| 375 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) |
| 376 | * @const fgoldi *x, *y@ = two operands |
| 377 | * |
| 378 | * Returns: --- |
| 379 | * |
| 380 | * Use: Set @z@ to the product %$x y$%. |
| 381 | */ |
| 382 | |
| 383 | void fgoldi_mul(fgoldi *z, const fgoldi *x, const fgoldi *y) |
| 384 | { |
| 385 | dblpiece zz[NPIECE], u[NPIECE]; |
| 386 | piece ab[NPIECE/2], cd[NPIECE/2]; |
| 387 | const piece |
| 388 | *a = x->P + NPIECE/2, *b = x->P, |
| 389 | *c = y->P + NPIECE/2, *d = y->P; |
| 390 | unsigned i, j; |
| 391 | |
| 392 | # define M(x,i, y,j) ((dblpiece)(x)[i]*(y)[j]) |
| 393 | |
| 394 | /* Behold the magic. |
| 395 | * |
| 396 | * Write x = a φ + b, and y = c φ + d. Then x y = a c φ^2 + |
| 397 | * (a d + b c) φ + b d. Karatsuba and Ofman observed that a d + b c = |
| 398 | * (a + b) (c + d) - a c - b d, saving a multiplication, and Hamburg chose |
| 399 | * the prime p so that φ^2 = φ + 1. So |
| 400 | * |
| 401 | * x y = ((a + b) (c + d) - b d) φ + a c + b d |
| 402 | */ |
| 403 | |
| 404 | for (i = 0; i < NPIECE; i++) zz[i] = 0; |
| 405 | |
| 406 | /* Our first job will be to calculate (1 - φ) b d, and write the result |
| 407 | * into z. As we do this, an interesting thing will happen. Write |
| 408 | * b d = u φ + v; then (1 - φ) b d = u φ + v - u φ^2 - v φ = (1 - φ) v - u. |
| 409 | * So, what we do is to write the product end-swapped and negated, and then |
| 410 | * we'll subtract the (negated, remember) high half from the low half. |
| 411 | */ |
| 412 | for (i = 0; i < NPIECE/2; i++) { |
| 413 | for (j = 0; j < NPIECE/2 - i; j++) |
| 414 | zz[i + j + NPIECE/2] -= M(b,i, d,j); |
| 415 | for (; j < NPIECE/2; j++) |
| 416 | zz[i + j - NPIECE/2] -= M(b,i, d,j); |
| 417 | } |
| 418 | for (i = 0; i < NPIECE/2; i++) |
| 419 | zz[i] -= zz[i + NPIECE/2]; |
| 420 | |
| 421 | /* Next, we add on a c. There are no surprises here. */ |
| 422 | for (i = 0; i < NPIECE/2; i++) |
| 423 | for (j = 0; j < NPIECE/2; j++) |
| 424 | zz[i + j] += M(a,i, c,j); |
| 425 | |
| 426 | /* Now, calculate a + b and c + d. */ |
| 427 | for (i = 0; i < NPIECE/2; i++) |
| 428 | { ab[i] = a[i] + b[i]; cd[i] = c[i] + d[i]; } |
| 429 | |
| 430 | /* Finally (for the multiplication) we must add on (a + b) (c + d) φ. |
| 431 | * Write (a + b) (c + d) as u φ + v; then we actually want u φ^2 + v φ = |
| 432 | * v φ + (1 + φ) u. We'll store u in a temporary place and add it on |
| 433 | * twice. |
| 434 | */ |
| 435 | for (i = 0; i < NPIECE; i++) u[i] = 0; |
| 436 | for (i = 0; i < NPIECE/2; i++) { |
| 437 | for (j = 0; j < NPIECE/2 - i; j++) |
| 438 | zz[i + j + NPIECE/2] += M(ab,i, cd,j); |
| 439 | for (; j < NPIECE/2; j++) |
| 440 | u[i + j - NPIECE/2] += M(ab,i, cd,j); |
| 441 | } |
| 442 | for (i = 0; i < NPIECE/2; i++) |
| 443 | { zz[i] += u[i]; zz[i + NPIECE/2] += u[i]; } |
| 444 | |
| 445 | #undef M |
| 446 | |
| 447 | /* That wraps it up for the multiplication. Let's figure out some bounds. |
| 448 | * Fortunately, Karatsuba is a polynomial identity, so all of the pieces |
| 449 | * end up the way they'd be if we'd done the thing the easy way, which |
| 450 | * simplifies the analysis. Suppose we started with |x_i|, |y_i| <= 9/5 |
| 451 | * 2^28. The overheads in the result are given by the coefficients of |
| 452 | * |
| 453 | * ((u^16 - 1)/(u - 1))^2 mod u^16 - u^8 - 1 |
| 454 | * |
| 455 | * the greatest of which is 38. So |z_i| <= 38*81/25*2^56 < 2^63. |
| 456 | * |
| 457 | * Anyway, a round of `CARRY_REDUCE' will leave us with |z_i| < 2^27 + |
| 458 | * 2^36; and a second round will leave us with |z_i| < 2^27 + 512. |
| 459 | */ |
| 460 | for (i = 0; i < 2; i++) CARRY_REDUCE(zz, zz); |
| 461 | for (i = 0; i < NPIECE; i++) z->P[i] = zz[i]; |
| 462 | } |
| 463 | |
| 464 | /* --- @fgoldi_sqr@ --- * |
| 465 | * |
| 466 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@ or @y@) |
| 467 | * @const fgoldi *x@ = an operand |
| 468 | * |
| 469 | * Returns: --- |
| 470 | * |
| 471 | * Use: Set @z@ to the square %$x^2$%. |
| 472 | */ |
| 473 | |
| 474 | void fgoldi_sqr(fgoldi *z, const fgoldi *x) |
| 475 | { |
| 476 | dblpiece zz[NPIECE], u[NPIECE]; |
| 477 | piece ab[NPIECE]; |
| 478 | const piece *a = x->P + NPIECE/2, *b = x->P; |
| 479 | unsigned i, j; |
| 480 | |
| 481 | # define M(x,i, y,j) ((dblpiece)(x)[i]*(y)[j]) |
| 482 | |
| 483 | /* The magic is basically the same as `fgoldi_mul' above. We write |
| 484 | * x = a φ + b and use Karatsuba and the special prime shape. This time, |
| 485 | * we have |
| 486 | * |
| 487 | * x^2 = ((a + b)^2 - b^2) φ + a^2 + b^2 |
| 488 | */ |
| 489 | |
| 490 | for (i = 0; i < NPIECE; i++) zz[i] = 0; |
| 491 | |
| 492 | /* Our first job will be to calculate (1 - φ) b^2, and write the result |
| 493 | * into z. Again, this interacts pleasantly with the prime shape. |
| 494 | */ |
| 495 | for (i = 0; i < NPIECE/4; i++) { |
| 496 | zz[2*i + NPIECE/2] -= M(b,i, b,i); |
| 497 | for (j = i + 1; j < NPIECE/2 - i; j++) |
| 498 | zz[i + j + NPIECE/2] -= 2*M(b,i, b,j); |
| 499 | for (; j < NPIECE/2; j++) |
| 500 | zz[i + j - NPIECE/2] -= 2*M(b,i, b,j); |
| 501 | } |
| 502 | for (; i < NPIECE/2; i++) { |
| 503 | zz[2*i - NPIECE/2] -= M(b,i, b,i); |
| 504 | for (j = i + 1; j < NPIECE/2; j++) |
| 505 | zz[i + j - NPIECE/2] -= 2*M(b,i, b,j); |
| 506 | } |
| 507 | for (i = 0; i < NPIECE/2; i++) |
| 508 | zz[i] -= zz[i + NPIECE/2]; |
| 509 | |
| 510 | /* Next, we add on a^2. There are no surprises here. */ |
| 511 | for (i = 0; i < NPIECE/2; i++) { |
| 512 | zz[2*i] += M(a,i, a,i); |
| 513 | for (j = i + 1; j < NPIECE/2; j++) |
| 514 | zz[i + j] += 2*M(a,i, a,j); |
| 515 | } |
| 516 | |
| 517 | /* Now, calculate a + b. */ |
| 518 | for (i = 0; i < NPIECE/2; i++) |
| 519 | ab[i] = a[i] + b[i]; |
| 520 | |
| 521 | /* Finally (for the multiplication) we must add on (a + b)^2 φ. |
| 522 | * Write (a + b)^2 as u φ + v; then we actually want (u + v) φ + u. We'll |
| 523 | * store u in a temporary place and add it on twice. |
| 524 | */ |
| 525 | for (i = 0; i < NPIECE; i++) u[i] = 0; |
| 526 | for (i = 0; i < NPIECE/4; i++) { |
| 527 | zz[2*i + NPIECE/2] += M(ab,i, ab,i); |
| 528 | for (j = i + 1; j < NPIECE/2 - i; j++) |
| 529 | zz[i + j + NPIECE/2] += 2*M(ab,i, ab,j); |
| 530 | for (; j < NPIECE/2; j++) |
| 531 | u[i + j - NPIECE/2] += 2*M(ab,i, ab,j); |
| 532 | } |
| 533 | for (; i < NPIECE/2; i++) { |
| 534 | u[2*i - NPIECE/2] += M(ab,i, ab,i); |
| 535 | for (j = i + 1; j < NPIECE/2; j++) |
| 536 | u[i + j - NPIECE/2] += 2*M(ab,i, ab,j); |
| 537 | } |
| 538 | for (i = 0; i < NPIECE/2; i++) |
| 539 | { zz[i] += u[i]; zz[i + NPIECE/2] += u[i]; } |
| 540 | |
| 541 | #undef M |
| 542 | |
| 543 | /* Finally, carrying. */ |
| 544 | for (i = 0; i < 2; i++) CARRY_REDUCE(zz, zz); |
| 545 | for (i = 0; i < NPIECE; i++) z->P[i] = zz[i]; |
| 546 | |
| 547 | } |
| 548 | |
| 549 | /*----- More advanced operations ------------------------------------------*/ |
| 550 | |
| 551 | /* --- @fgoldi_inv@ --- * |
| 552 | * |
| 553 | * Arguments: @fgoldi *z@ = where to put the result (may alias @x@) |
| 554 | * @const fgoldi *x@ = an operand |
| 555 | * |
| 556 | * Returns: --- |
| 557 | * |
| 558 | * Use: Stores in @z@ the multiplicative inverse %$x^{-1}$%. If |
| 559 | * %$x = 0$% then @z@ is set to zero. This is considered a |
| 560 | * feature. |
| 561 | */ |
| 562 | |
| 563 | void fgoldi_inv(fgoldi *z, const fgoldi *x) |
| 564 | { |
| 565 | fgoldi t, u; |
| 566 | unsigned i; |
| 567 | |
| 568 | #define SQRN(z, x, n) do { \ |
| 569 | fgoldi_sqr((z), (x)); \ |
| 570 | for (i = 1; i < (n); i++) fgoldi_sqr((z), (z)); \ |
| 571 | } while (0) |
| 572 | |
| 573 | /* Calculate x^-1 = x^(p - 2) = x^(2^448 - 2^224 - 3), which also handles |
| 574 | * x = 0 as intended. The addition chain is home-made. |
| 575 | */ /* step | value */ |
| 576 | fgoldi_sqr(&u, x); /* 1 | 2 */ |
| 577 | fgoldi_mul(&t, &u, x); /* 2 | 3 */ |
| 578 | SQRN(&u, &t, 2); /* 4 | 12 */ |
| 579 | fgoldi_mul(&t, &u, &t); /* 5 | 15 */ |
| 580 | SQRN(&u, &t, 4); /* 9 | 240 */ |
| 581 | fgoldi_mul(&u, &u, &t); /* 10 | 255 = 2^8 - 1 */ |
| 582 | SQRN(&u, &u, 4); /* 14 | 2^12 - 16 */ |
| 583 | fgoldi_mul(&t, &u, &t); /* 15 | 2^12 - 1 */ |
| 584 | SQRN(&u, &t, 12); /* 27 | 2^24 - 2^12 */ |
| 585 | fgoldi_mul(&u, &u, &t); /* 28 | 2^24 - 1 */ |
| 586 | SQRN(&u, &u, 12); /* 40 | 2^36 - 2^12 */ |
| 587 | fgoldi_mul(&t, &u, &t); /* 41 | 2^36 - 1 */ |
| 588 | fgoldi_sqr(&t, &t); /* 42 | 2^37 - 2 */ |
| 589 | fgoldi_mul(&t, &t, x); /* 43 | 2^37 - 1 */ |
| 590 | SQRN(&u, &t, 37); /* 80 | 2^74 - 2^37 */ |
| 591 | fgoldi_mul(&u, &u, &t); /* 81 | 2^74 - 1 */ |
| 592 | SQRN(&u, &u, 37); /* 118 | 2^111 - 2^37 */ |
| 593 | fgoldi_mul(&t, &u, &t); /* 119 | 2^111 - 1 */ |
| 594 | SQRN(&u, &t, 111); /* 230 | 2^222 - 2^111 */ |
| 595 | fgoldi_mul(&t, &u, &t); /* 231 | 2^222 - 1 */ |
| 596 | fgoldi_sqr(&u, &t); /* 232 | 2^223 - 2 */ |
| 597 | fgoldi_mul(&u, &u, x); /* 233 | 2^223 - 1 */ |
| 598 | SQRN(&u, &u, 223); /* 456 | 2^446 - 2^223 */ |
| 599 | fgoldi_mul(&t, &u, &t); /* 457 | 2^446 - 2^222 - 1 */ |
| 600 | SQRN(&t, &t, 2); /* 459 | 2^448 - 2^224 - 4 */ |
| 601 | fgoldi_mul(z, &t, x); /* 460 | 2^448 - 2^224 - 3 */ |
| 602 | |
| 603 | #undef SQRN |
| 604 | } |
| 605 | |
| 606 | /*----- That's all, folks -------------------------------------------------*/ |