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