Import Curve 448 support
[openssl.git] / crypto / ec / curve448 / GENERATED / c / ed448goldilocks / decaf.c
1 /**
2  * @file ed448goldilocks/decaf.c
3  * @author Mike Hamburg
4  *
5  * @copyright
6  *   Copyright (c) 2015-2016 Cryptography Research, Inc.  \n
7  *   Released under the MIT License.  See LICENSE.txt for license information.
8  *
9  * @brief Decaf high-level functions.
10  *
11  * @warning This file was automatically generated in Python.
12  * Please do not edit it.
13  */
14 #define _XOPEN_SOURCE 600 /* for posix_memalign */
15 #include "word.h"
16 #include "field.h"
17
18 #include <decaf.h>
19 #include <decaf/ed448.h>
20
21 /* Template stuff */
22 #define API_NS(_id) decaf_448_##_id
23 #define SCALAR_BITS DECAF_448_SCALAR_BITS
24 #define SCALAR_SER_BYTES DECAF_448_SCALAR_BYTES
25 #define SCALAR_LIMBS DECAF_448_SCALAR_LIMBS
26 #define scalar_t API_NS(scalar_t)
27 #define point_t API_NS(point_t)
28 #define precomputed_s API_NS(precomputed_s)
29 #define IMAGINE_TWIST 0
30 #define COFACTOR 4
31
32 /* Comb config: number of combs, n, t, s. */
33 #define COMBS_N 5
34 #define COMBS_T 5
35 #define COMBS_S 18
36 #define DECAF_WINDOW_BITS 5
37 #define DECAF_WNAF_FIXED_TABLE_BITS 5
38 #define DECAF_WNAF_VAR_TABLE_BITS 3
39
40 #define EDDSA_USE_SIGMA_ISOGENY 0
41
42 static const int EDWARDS_D = -39081;
43 static const scalar_t point_scalarmul_adjustment = {{{
44     SC_LIMB(0xc873d6d54a7bb0cf), SC_LIMB(0xe933d8d723a70aad), SC_LIMB(0xbb124b65129c96fd), SC_LIMB(0x00000008335dc163)
45 }}}, precomputed_scalarmul_adjustment = {{{
46     SC_LIMB(0xc873d6d54a7bb0cf), SC_LIMB(0xe933d8d723a70aad), SC_LIMB(0xbb124b65129c96fd), SC_LIMB(0x00000008335dc163)
47 }}};
48
49 const uint8_t decaf_x448_base_point[DECAF_X448_PUBLIC_BYTES] = { 0x05 };
50
51 #define RISTRETTO_FACTOR DECAF_448_RISTRETTO_FACTOR
52 const gf RISTRETTO_FACTOR = {{{
53     0x42ef0f45572736, 0x7bf6aa20ce5296, 0xf4fd6eded26033, 0x968c14ba839a66, 0xb8d54b64a2d780, 0x6aa0a1f1a7b8a5, 0x683bf68d722fa2, 0x22d962fbeb24f7
54 }}};
55
56 #if IMAGINE_TWIST
57 #define TWISTED_D (-(EDWARDS_D))
58 #else
59 #define TWISTED_D ((EDWARDS_D)-1)
60 #endif
61
62 #if TWISTED_D < 0
63 #define EFF_D (-(TWISTED_D))
64 #define NEG_D 1
65 #else
66 #define EFF_D TWISTED_D
67 #define NEG_D 0
68 #endif
69
70 /* End of template stuff */
71
72 /* Sanity */
73 #if (COFACTOR == 8) && !IMAGINE_TWIST && !UNSAFE_CURVE_HAS_POINTS_AT_INFINITY
74 /* FUTURE MAGIC: Curve41417 doesn't have these properties. */
75 #error "Currently require IMAGINE_TWIST (and thus p=5 mod 8) for cofactor 8"
76         /* OK, but why?
77          * Two reasons: #1: There are bugs when COFACTOR == && IMAGINE_TWIST
78          # #2: 
79          */
80 #endif
81
82 #if IMAGINE_TWIST && (P_MOD_8 != 5)
83     #error "Cannot use IMAGINE_TWIST except for p == 5 mod 8"
84 #endif
85
86 #if (COFACTOR != 8) && (COFACTOR != 4)
87     #error "COFACTOR must be 4 or 8"
88 #endif
89  
90 #if IMAGINE_TWIST
91     extern const gf SQRT_MINUS_ONE;
92 #endif
93
94 #define WBITS DECAF_WORD_BITS /* NB this may be different from ARCH_WORD_BITS */
95
96 extern const point_t API_NS(point_base);
97
98 /* Projective Niels coordinates */
99 typedef struct { gf a, b, c; } niels_s, niels_t[1];
100 typedef struct { niels_t n; gf z; } VECTOR_ALIGNED pniels_s, pniels_t[1];
101
102 /* Precomputed base */
103 struct precomputed_s { niels_t table [COMBS_N<<(COMBS_T-1)]; };
104
105 extern const gf API_NS(precomputed_base_as_fe)[];
106 const precomputed_s *API_NS(precomputed_base) =
107     (const precomputed_s *) &API_NS(precomputed_base_as_fe);
108
109 const size_t API_NS(sizeof_precomputed_s) = sizeof(precomputed_s);
110 const size_t API_NS(alignof_precomputed_s) = sizeof(big_register_t);
111
112 /** Inverse. */
113 static void
114 gf_invert(gf y, const gf x, int assert_nonzero) {
115     gf t1, t2;
116     gf_sqr(t1, x); // o^2
117     mask_t ret = gf_isr(t2, t1); // +-1/sqrt(o^2) = +-1/o
118     (void)ret;
119     if (assert_nonzero) assert(ret);
120     gf_sqr(t1, t2);
121     gf_mul(t2, t1, x); // not direct to y in case of alias.
122     gf_copy(y, t2);
123 }
124
125 /** identity = (0,1) */
126 const point_t API_NS(point_identity) = {{{{{0}}},{{{1}}},{{{1}}},{{{0}}}}};
127
128 /* Predeclare because not static: called by elligator */
129 void API_NS(deisogenize) (
130     gf_s *__restrict__ s,
131     gf_s *__restrict__ inv_el_sum,
132     gf_s *__restrict__ inv_el_m1,
133     const point_t p,
134     mask_t toggle_s,
135     mask_t toggle_altx,
136     mask_t toggle_rotation
137 );
138
139 void API_NS(deisogenize) (
140     gf_s *__restrict__ s,
141     gf_s *__restrict__ inv_el_sum,
142     gf_s *__restrict__ inv_el_m1,
143     const point_t p,
144     mask_t toggle_s,
145     mask_t toggle_altx,
146     mask_t toggle_rotation
147 ) {
148 #if COFACTOR == 4 && !IMAGINE_TWIST
149     (void)toggle_rotation; /* Only applies to cofactor 8 */
150     gf t1;
151     gf_s *t2 = s, *t3=inv_el_sum, *t4=inv_el_m1;
152     
153     gf_add(t1,p->x,p->t);
154     gf_sub(t2,p->x,p->t);
155     gf_mul(t3,t1,t2); /* t3 = num */
156     gf_sqr(t2,p->x);
157     gf_mul(t1,t2,t3);
158     gf_mulw(t2,t1,-1-TWISTED_D); /* -x^2 * (a-d) * num */
159     gf_isr(t1,t2);    /* t1 = isr */
160     gf_mul(t2,t1,t3); /* t2 = ratio */
161     gf_mul(t4,t2,RISTRETTO_FACTOR);
162     mask_t negx = gf_lobit(t4) ^ toggle_altx;
163     gf_cond_neg(t2, negx);
164     gf_mul(t3,t2,p->z);
165     gf_sub(t3,t3,p->t);
166     gf_mul(t2,t3,p->x);
167     gf_mulw(t4,t2,-1-TWISTED_D);
168     gf_mul(s,t4,t1);
169     mask_t lobs = gf_lobit(s);
170     gf_cond_neg(s,lobs);
171     gf_copy(inv_el_m1,p->x);
172     gf_cond_neg(inv_el_m1,~lobs^negx^toggle_s);
173     gf_add(inv_el_m1,inv_el_m1,p->t);
174     
175 #elif COFACTOR == 8 && IMAGINE_TWIST
176     /* More complicated because of rotation */
177     gf t1,t2,t3,t4,t5;
178     gf_add(t1,p->z,p->y);
179     gf_sub(t2,p->z,p->y);
180     gf_mul(t3,t1,t2);      /* t3 = num */
181     gf_mul(t2,p->x,p->y);  /* t2 = den */
182     gf_sqr(t1,t2);
183     gf_mul(t4,t1,t3);
184     gf_mulw(t1,t4,-1-TWISTED_D);
185     gf_isr(t4,t1);         /* isqrt(num*(a-d)*den^2) */
186     gf_mul(t1,t2,t4);
187     gf_mul(t2,t1,RISTRETTO_FACTOR); /* t2 = "iden" in ristretto.sage */
188     gf_mul(t1,t3,t4);                 /* t1 = "inum" in ristretto.sage */
189
190     /* Calculate altxy = iden*inum*i*t^2*(d-a) */
191     gf_mul(t3,t1,t2);
192     gf_mul_i(t4,t3);
193     gf_mul(t3,t4,p->t);
194     gf_mul(t4,t3,p->t);
195     gf_mulw(t3,t4,TWISTED_D+1);      /* iden*inum*i*t^2*(d-1) */
196     mask_t rotate = toggle_rotation ^ gf_lobit(t3);
197     
198     /* Rotate if altxy is negative */
199     gf_cond_swap(t1,t2,rotate);
200     gf_mul_i(t4,p->x);
201     gf_cond_sel(t4,p->y,t4,rotate);  /* t4 = "fac" = ix if rotate, else y */
202     
203     gf_mul_i(t5,RISTRETTO_FACTOR); /* t5 = imi */
204     gf_mul(t3,t5,t2);                /* iden * imi */
205     gf_mul(t2,t5,t1);
206     gf_mul(t5,t2,p->t);              /* "altx" = iden*imi*t */
207     mask_t negx = gf_lobit(t5) ^ toggle_altx;
208     
209     gf_cond_neg(t1,negx^rotate);
210     gf_mul(t2,t1,p->z);
211     gf_add(t2,t2,ONE);
212     gf_mul(inv_el_sum,t2,t4);
213     gf_mul(s,inv_el_sum,t3);
214     
215     mask_t negs = gf_lobit(s);
216     gf_cond_neg(s,negs);
217     
218     mask_t negz = ~negs ^ toggle_s ^ negx;
219     gf_copy(inv_el_m1,p->z);
220     gf_cond_neg(inv_el_m1,negz);
221     gf_sub(inv_el_m1,inv_el_m1,t4);
222 #else
223 #error "Cofactor must be 4 (with no IMAGINE_TWIST) or 8 (with IMAGINE_TWIST)"
224 #endif
225 }
226
227 void API_NS(point_encode)( unsigned char ser[SER_BYTES], const point_t p ) {
228     gf s,ie1,ie2;
229     API_NS(deisogenize)(s,ie1,ie2,p,0,0,0);
230     gf_serialize(ser,s,1);
231 }
232
233 decaf_error_t API_NS(point_decode) (
234     point_t p,
235     const unsigned char ser[SER_BYTES],
236     decaf_bool_t allow_identity
237 ) {
238     gf s, s2, num, tmp;
239     gf_s *tmp2=s2, *ynum=p->z, *isr=p->x, *den=p->t;
240     
241     mask_t succ = gf_deserialize(s, ser, 1, 0);
242     succ &= bool_to_mask(allow_identity) | ~gf_eq(s, ZERO);
243     succ &= ~gf_lobit(s);
244     
245     gf_sqr(s2,s);                  /* s^2 = -as^2 */
246 #if IMAGINE_TWIST
247     gf_sub(s2,ZERO,s2);            /* -as^2 */
248 #endif
249     gf_sub(den,ONE,s2);            /* 1+as^2 */
250     gf_add(ynum,ONE,s2);           /* 1-as^2 */
251     gf_mulw(num,s2,-4*TWISTED_D);
252     gf_sqr(tmp,den);               /* tmp = den^2 */
253     gf_add(num,tmp,num);           /* num = den^2 - 4*d*s^2 */
254     gf_mul(tmp2,num,tmp);          /* tmp2 = num*den^2 */
255     succ &= gf_isr(isr,tmp2);      /* isr = 1/sqrt(num*den^2) */
256     gf_mul(tmp,isr,den);           /* isr*den */
257     gf_mul(p->y,tmp,ynum);         /* isr*den*(1-as^2) */
258     gf_mul(tmp2,tmp,s);            /* s*isr*den */
259     gf_add(tmp2,tmp2,tmp2);        /* 2*s*isr*den */
260     gf_mul(tmp,tmp2,isr);          /* 2*s*isr^2*den */
261     gf_mul(p->x,tmp,num);          /* 2*s*isr^2*den*num */
262     gf_mul(tmp,tmp2,RISTRETTO_FACTOR); /* 2*s*isr*den*magic */
263     gf_cond_neg(p->x,gf_lobit(tmp)); /* flip x */
264     
265 #if COFACTOR==8
266     /* Additionally check y != 0 and x*y*isomagic nonegative */
267     succ &= ~gf_eq(p->y,ZERO);
268     gf_mul(tmp,p->x,p->y);
269     gf_mul(tmp2,tmp,RISTRETTO_FACTOR);
270     succ &= ~gf_lobit(tmp2);
271 #endif
272
273 #if IMAGINE_TWIST
274     gf_copy(tmp,p->x);
275     gf_mul_i(p->x,tmp);
276 #endif
277
278     /* Fill in z and t */
279     gf_copy(p->z,ONE);
280     gf_mul(p->t,p->x,p->y);
281     
282     assert(API_NS(point_valid)(p) | ~succ);
283     return decaf_succeed_if(mask_to_bool(succ));
284 }
285
286 void API_NS(point_sub) (
287     point_t p,
288     const point_t q,
289     const point_t r
290 ) {
291     gf a, b, c, d;
292     gf_sub_nr ( b, q->y, q->x ); /* 3+e */
293     gf_sub_nr ( d, r->y, r->x ); /* 3+e */
294     gf_add_nr ( c, r->y, r->x ); /* 2+e */
295     gf_mul ( a, c, b );
296     gf_add_nr ( b, q->y, q->x ); /* 2+e */
297     gf_mul ( p->y, d, b );
298     gf_mul ( b, r->t, q->t );
299     gf_mulw ( p->x, b, 2*EFF_D );
300     gf_add_nr ( b, a, p->y );    /* 2+e */
301     gf_sub_nr ( c, p->y, a );    /* 3+e */
302     gf_mul ( a, q->z, r->z );
303     gf_add_nr ( a, a, a );       /* 2+e */
304     if (GF_HEADROOM <= 3) gf_weak_reduce(a); /* or 1+e */
305 #if NEG_D
306     gf_sub_nr ( p->y, a, p->x ); /* 4+e or 3+e */
307     gf_add_nr ( a, a, p->x );    /* 3+e or 2+e */
308 #else
309     gf_add_nr ( p->y, a, p->x ); /* 3+e or 2+e */
310     gf_sub_nr ( a, a, p->x );    /* 4+e or 3+e */
311 #endif
312     gf_mul ( p->z, a, p->y );
313     gf_mul ( p->x, p->y, c );
314     gf_mul ( p->y, a, b );
315     gf_mul ( p->t, b, c );
316 }
317     
318 void API_NS(point_add) (
319     point_t p,
320     const point_t q,
321     const point_t r
322 ) {
323     gf a, b, c, d;
324     gf_sub_nr ( b, q->y, q->x ); /* 3+e */
325     gf_sub_nr ( c, r->y, r->x ); /* 3+e */
326     gf_add_nr ( d, r->y, r->x ); /* 2+e */
327     gf_mul ( a, c, b );
328     gf_add_nr ( b, q->y, q->x ); /* 2+e */
329     gf_mul ( p->y, d, b );
330     gf_mul ( b, r->t, q->t );
331     gf_mulw ( p->x, b, 2*EFF_D );
332     gf_add_nr ( b, a, p->y );    /* 2+e */
333     gf_sub_nr ( c, p->y, a );    /* 3+e */
334     gf_mul ( a, q->z, r->z );
335     gf_add_nr ( a, a, a );       /* 2+e */
336     if (GF_HEADROOM <= 3) gf_weak_reduce(a); /* or 1+e */
337 #if NEG_D
338     gf_add_nr ( p->y, a, p->x ); /* 3+e or 2+e */
339     gf_sub_nr ( a, a, p->x );    /* 4+e or 3+e */
340 #else
341     gf_sub_nr ( p->y, a, p->x ); /* 4+e or 3+e */
342     gf_add_nr ( a, a, p->x );    /* 3+e or 2+e */
343 #endif
344     gf_mul ( p->z, a, p->y );
345     gf_mul ( p->x, p->y, c );
346     gf_mul ( p->y, a, b );
347     gf_mul ( p->t, b, c );
348 }
349
350 static DECAF_NOINLINE void
351 point_double_internal (
352     point_t p,
353     const point_t q,
354     int before_double
355 ) {
356     gf a, b, c, d;
357     gf_sqr ( c, q->x );
358     gf_sqr ( a, q->y );
359     gf_add_nr ( d, c, a );             /* 2+e */
360     gf_add_nr ( p->t, q->y, q->x );    /* 2+e */
361     gf_sqr ( b, p->t );
362     gf_subx_nr ( b, b, d, 3 );         /* 4+e */
363     gf_sub_nr ( p->t, a, c );          /* 3+e */
364     gf_sqr ( p->x, q->z );
365     gf_add_nr ( p->z, p->x, p->x );    /* 2+e */
366     gf_subx_nr ( a, p->z, p->t, 4 );   /* 6+e */
367     if (GF_HEADROOM == 5) gf_weak_reduce(a); /* or 1+e */
368     gf_mul ( p->x, a, b );
369     gf_mul ( p->z, p->t, a );
370     gf_mul ( p->y, p->t, d );
371     if (!before_double) gf_mul ( p->t, b, d );
372 }
373
374 void API_NS(point_double)(point_t p, const point_t q) {
375     point_double_internal(p,q,0);
376 }
377
378 void API_NS(point_negate) (
379    point_t nega,
380    const point_t a
381 ) {
382     gf_sub(nega->x, ZERO, a->x);
383     gf_copy(nega->y, a->y);
384     gf_copy(nega->z, a->z);
385     gf_sub(nega->t, ZERO, a->t);
386 }
387
388 /* Operations on [p]niels */
389 static DECAF_INLINE void
390 cond_neg_niels (
391     niels_t n,
392     mask_t neg
393 ) {
394     gf_cond_swap(n->a, n->b, neg);
395     gf_cond_neg(n->c, neg);
396 }
397
398 static DECAF_NOINLINE void pt_to_pniels (
399     pniels_t b,
400     const point_t a
401 ) {
402     gf_sub ( b->n->a, a->y, a->x );
403     gf_add ( b->n->b, a->x, a->y );
404     gf_mulw ( b->n->c, a->t, 2*TWISTED_D );
405     gf_add ( b->z, a->z, a->z );
406 }
407
408 static DECAF_NOINLINE void pniels_to_pt (
409     point_t e,
410     const pniels_t d
411 ) {
412     gf eu;
413     gf_add ( eu, d->n->b, d->n->a );
414     gf_sub ( e->y, d->n->b, d->n->a );
415     gf_mul ( e->t, e->y, eu);
416     gf_mul ( e->x, d->z, e->y );
417     gf_mul ( e->y, d->z, eu );
418     gf_sqr ( e->z, d->z );
419 }
420
421 static DECAF_NOINLINE void
422 niels_to_pt (
423     point_t e,
424     const niels_t n
425 ) {
426     gf_add ( e->y, n->b, n->a );
427     gf_sub ( e->x, n->b, n->a );
428     gf_mul ( e->t, e->y, e->x );
429     gf_copy ( e->z, ONE );
430 }
431
432 static DECAF_NOINLINE void
433 add_niels_to_pt (
434     point_t d,
435     const niels_t e,
436     int before_double
437 ) {
438     gf a, b, c;
439     gf_sub_nr ( b, d->y, d->x ); /* 3+e */
440     gf_mul ( a, e->a, b );
441     gf_add_nr ( b, d->x, d->y ); /* 2+e */
442     gf_mul ( d->y, e->b, b );
443     gf_mul ( d->x, e->c, d->t );
444     gf_add_nr ( c, a, d->y );    /* 2+e */
445     gf_sub_nr ( b, d->y, a );    /* 3+e */
446     gf_sub_nr ( d->y, d->z, d->x ); /* 3+e */
447     gf_add_nr ( a, d->x, d->z ); /* 2+e */
448     gf_mul ( d->z, a, d->y );
449     gf_mul ( d->x, d->y, b );
450     gf_mul ( d->y, a, c );
451     if (!before_double) gf_mul ( d->t, b, c );
452 }
453
454 static DECAF_NOINLINE void
455 sub_niels_from_pt (
456     point_t d,
457     const niels_t e,
458     int before_double
459 ) {
460     gf a, b, c;
461     gf_sub_nr ( b, d->y, d->x ); /* 3+e */
462     gf_mul ( a, e->b, b );
463     gf_add_nr ( b, d->x, d->y ); /* 2+e */
464     gf_mul ( d->y, e->a, b );
465     gf_mul ( d->x, e->c, d->t );
466     gf_add_nr ( c, a, d->y );    /* 2+e */
467     gf_sub_nr ( b, d->y, a );    /* 3+e */
468     gf_add_nr ( d->y, d->z, d->x ); /* 2+e */
469     gf_sub_nr ( a, d->z, d->x ); /* 3+e */
470     gf_mul ( d->z, a, d->y );
471     gf_mul ( d->x, d->y, b );
472     gf_mul ( d->y, a, c );
473     if (!before_double) gf_mul ( d->t, b, c );
474 }
475
476 static void
477 add_pniels_to_pt (
478     point_t p,
479     const pniels_t pn,
480     int before_double
481 ) {
482     gf L0;
483     gf_mul ( L0, p->z, pn->z );
484     gf_copy ( p->z, L0 );
485     add_niels_to_pt( p, pn->n, before_double );
486 }
487
488 static void
489 sub_pniels_from_pt (
490     point_t p,
491     const pniels_t pn,
492     int before_double
493 ) {
494     gf L0;
495     gf_mul ( L0, p->z, pn->z );
496     gf_copy ( p->z, L0 );
497     sub_niels_from_pt( p, pn->n, before_double );
498 }
499
500 static DECAF_NOINLINE void
501 prepare_fixed_window(
502     pniels_t *multiples,
503     const point_t b,
504     int ntable
505 ) {
506     point_t tmp;
507     pniels_t pn;
508     int i;
509     
510     point_double_internal(tmp, b, 0);
511     pt_to_pniels(pn, tmp);
512     pt_to_pniels(multiples[0], b);
513     API_NS(point_copy)(tmp, b);
514     for (i=1; i<ntable; i++) {
515         add_pniels_to_pt(tmp, pn, 0);
516         pt_to_pniels(multiples[i], tmp);
517     }
518     
519     decaf_bzero(pn,sizeof(pn));
520     decaf_bzero(tmp,sizeof(tmp));
521 }
522
523 void API_NS(point_scalarmul) (
524     point_t a,
525     const point_t b,
526     const scalar_t scalar
527 ) {
528     const int WINDOW = DECAF_WINDOW_BITS,
529         WINDOW_MASK = (1<<WINDOW)-1,
530         WINDOW_T_MASK = WINDOW_MASK >> 1,
531         NTABLE = 1<<(WINDOW-1);
532         
533     scalar_t scalar1x;
534     API_NS(scalar_add)(scalar1x, scalar, point_scalarmul_adjustment);
535     API_NS(scalar_halve)(scalar1x,scalar1x);
536     
537     /* Set up a precomputed table with odd multiples of b. */
538     pniels_t pn, multiples[NTABLE];
539     point_t tmp;
540     prepare_fixed_window(multiples, b, NTABLE);
541
542     /* Initialize. */
543     int i,j,first=1;
544     i = SCALAR_BITS - ((SCALAR_BITS-1) % WINDOW) - 1;
545
546     for (; i>=0; i-=WINDOW) {
547         /* Fetch another block of bits */
548         word_t bits = scalar1x->limb[i/WBITS] >> (i%WBITS);
549         if (i%WBITS >= WBITS-WINDOW && i/WBITS<SCALAR_LIMBS-1) {
550             bits ^= scalar1x->limb[i/WBITS+1] << (WBITS - (i%WBITS));
551         }
552         bits &= WINDOW_MASK;
553         mask_t inv = (bits>>(WINDOW-1))-1;
554         bits ^= inv;
555     
556         /* Add in from table.  Compute t only on last iteration. */
557         constant_time_lookup(pn, multiples, sizeof(pn), NTABLE, bits & WINDOW_T_MASK);
558         cond_neg_niels(pn->n, inv);
559         if (first) {
560             pniels_to_pt(tmp, pn);
561             first = 0;
562         } else {
563            /* Using Hisil et al's lookahead method instead of extensible here
564             * for no particular reason.  Double WINDOW times, but only compute t on
565             * the last one.
566             */
567             for (j=0; j<WINDOW-1; j++)
568                 point_double_internal(tmp, tmp, -1);
569             point_double_internal(tmp, tmp, 0);
570             add_pniels_to_pt(tmp, pn, i ? -1 : 0);
571         }
572     }
573     
574     /* Write out the answer */
575     API_NS(point_copy)(a,tmp);
576     
577     decaf_bzero(scalar1x,sizeof(scalar1x));
578     decaf_bzero(pn,sizeof(pn));
579     decaf_bzero(multiples,sizeof(multiples));
580     decaf_bzero(tmp,sizeof(tmp));
581 }
582
583 void API_NS(point_double_scalarmul) (
584     point_t a,
585     const point_t b,
586     const scalar_t scalarb,
587     const point_t c,
588     const scalar_t scalarc
589 ) {
590     const int WINDOW = DECAF_WINDOW_BITS,
591         WINDOW_MASK = (1<<WINDOW)-1,
592         WINDOW_T_MASK = WINDOW_MASK >> 1,
593         NTABLE = 1<<(WINDOW-1);
594         
595     scalar_t scalar1x, scalar2x;
596     API_NS(scalar_add)(scalar1x, scalarb, point_scalarmul_adjustment);
597     API_NS(scalar_halve)(scalar1x,scalar1x);
598     API_NS(scalar_add)(scalar2x, scalarc, point_scalarmul_adjustment);
599     API_NS(scalar_halve)(scalar2x,scalar2x);
600     
601     /* Set up a precomputed table with odd multiples of b. */
602     pniels_t pn, multiples1[NTABLE], multiples2[NTABLE];
603     point_t tmp;
604     prepare_fixed_window(multiples1, b, NTABLE);
605     prepare_fixed_window(multiples2, c, NTABLE);
606
607     /* Initialize. */
608     int i,j,first=1;
609     i = SCALAR_BITS - ((SCALAR_BITS-1) % WINDOW) - 1;
610
611     for (; i>=0; i-=WINDOW) {
612         /* Fetch another block of bits */
613         word_t bits1 = scalar1x->limb[i/WBITS] >> (i%WBITS),
614                      bits2 = scalar2x->limb[i/WBITS] >> (i%WBITS);
615         if (i%WBITS >= WBITS-WINDOW && i/WBITS<SCALAR_LIMBS-1) {
616             bits1 ^= scalar1x->limb[i/WBITS+1] << (WBITS - (i%WBITS));
617             bits2 ^= scalar2x->limb[i/WBITS+1] << (WBITS - (i%WBITS));
618         }
619         bits1 &= WINDOW_MASK;
620         bits2 &= WINDOW_MASK;
621         mask_t inv1 = (bits1>>(WINDOW-1))-1;
622         mask_t inv2 = (bits2>>(WINDOW-1))-1;
623         bits1 ^= inv1;
624         bits2 ^= inv2;
625     
626         /* Add in from table.  Compute t only on last iteration. */
627         constant_time_lookup(pn, multiples1, sizeof(pn), NTABLE, bits1 & WINDOW_T_MASK);
628         cond_neg_niels(pn->n, inv1);
629         if (first) {
630             pniels_to_pt(tmp, pn);
631             first = 0;
632         } else {
633            /* Using Hisil et al's lookahead method instead of extensible here
634             * for no particular reason.  Double WINDOW times, but only compute t on
635             * the last one.
636             */
637             for (j=0; j<WINDOW-1; j++)
638                 point_double_internal(tmp, tmp, -1);
639             point_double_internal(tmp, tmp, 0);
640             add_pniels_to_pt(tmp, pn, 0);
641         }
642         constant_time_lookup(pn, multiples2, sizeof(pn), NTABLE, bits2 & WINDOW_T_MASK);
643         cond_neg_niels(pn->n, inv2);
644         add_pniels_to_pt(tmp, pn, i?-1:0);
645     }
646     
647     /* Write out the answer */
648     API_NS(point_copy)(a,tmp);
649     
650
651     decaf_bzero(scalar1x,sizeof(scalar1x));
652     decaf_bzero(scalar2x,sizeof(scalar2x));
653     decaf_bzero(pn,sizeof(pn));
654     decaf_bzero(multiples1,sizeof(multiples1));
655     decaf_bzero(multiples2,sizeof(multiples2));
656     decaf_bzero(tmp,sizeof(tmp));
657 }
658
659 void API_NS(point_dual_scalarmul) (
660     point_t a1,
661     point_t a2,
662     const point_t b,
663     const scalar_t scalar1,
664     const scalar_t scalar2
665 ) {
666     const int WINDOW = DECAF_WINDOW_BITS,
667         WINDOW_MASK = (1<<WINDOW)-1,
668         WINDOW_T_MASK = WINDOW_MASK >> 1,
669         NTABLE = 1<<(WINDOW-1);
670         
671     scalar_t scalar1x, scalar2x;
672     API_NS(scalar_add)(scalar1x, scalar1, point_scalarmul_adjustment);
673     API_NS(scalar_halve)(scalar1x,scalar1x);
674     API_NS(scalar_add)(scalar2x, scalar2, point_scalarmul_adjustment);
675     API_NS(scalar_halve)(scalar2x,scalar2x);
676     
677     /* Set up a precomputed table with odd multiples of b. */
678     point_t multiples1[NTABLE], multiples2[NTABLE], working, tmp;
679     pniels_t pn;
680     
681     API_NS(point_copy)(working, b);
682
683     /* Initialize. */
684     int i,j;
685     
686     for (i=0; i<NTABLE; i++) {
687         API_NS(point_copy)(multiples1[i], API_NS(point_identity));
688         API_NS(point_copy)(multiples2[i], API_NS(point_identity));
689     }
690
691     for (i=0; i<SCALAR_BITS; i+=WINDOW) {   
692         if (i) {
693             for (j=0; j<WINDOW-1; j++)
694                 point_double_internal(working, working, -1);
695             point_double_internal(working, working, 0);
696         }
697         
698         /* Fetch another block of bits */
699         word_t bits1 = scalar1x->limb[i/WBITS] >> (i%WBITS),
700                bits2 = scalar2x->limb[i/WBITS] >> (i%WBITS);
701         if (i%WBITS >= WBITS-WINDOW && i/WBITS<SCALAR_LIMBS-1) {
702             bits1 ^= scalar1x->limb[i/WBITS+1] << (WBITS - (i%WBITS));
703             bits2 ^= scalar2x->limb[i/WBITS+1] << (WBITS - (i%WBITS));
704         }
705         bits1 &= WINDOW_MASK;
706         bits2 &= WINDOW_MASK;
707         mask_t inv1 = (bits1>>(WINDOW-1))-1;
708         mask_t inv2 = (bits2>>(WINDOW-1))-1;
709         bits1 ^= inv1;
710         bits2 ^= inv2;
711         
712         pt_to_pniels(pn, working);
713
714         constant_time_lookup(tmp, multiples1, sizeof(tmp), NTABLE, bits1 & WINDOW_T_MASK);
715         cond_neg_niels(pn->n, inv1);
716         /* add_pniels_to_pt(multiples1[bits1 & WINDOW_T_MASK], pn, 0); */
717         add_pniels_to_pt(tmp, pn, 0);
718         constant_time_insert(multiples1, tmp, sizeof(tmp), NTABLE, bits1 & WINDOW_T_MASK);
719         
720         
721         constant_time_lookup(tmp, multiples2, sizeof(tmp), NTABLE, bits2 & WINDOW_T_MASK);
722         cond_neg_niels(pn->n, inv1^inv2);
723         /* add_pniels_to_pt(multiples2[bits2 & WINDOW_T_MASK], pn, 0); */
724         add_pniels_to_pt(tmp, pn, 0);
725         constant_time_insert(multiples2, tmp, sizeof(tmp), NTABLE, bits2 & WINDOW_T_MASK);
726     }
727     
728     if (NTABLE > 1) {
729         API_NS(point_copy)(working, multiples1[NTABLE-1]);
730         API_NS(point_copy)(tmp    , multiples2[NTABLE-1]);
731     
732         for (i=NTABLE-1; i>1; i--) {
733             API_NS(point_add)(multiples1[i-1], multiples1[i-1], multiples1[i]);
734             API_NS(point_add)(multiples2[i-1], multiples2[i-1], multiples2[i]);
735             API_NS(point_add)(working, working, multiples1[i-1]);
736             API_NS(point_add)(tmp,     tmp,     multiples2[i-1]);
737         }
738     
739         API_NS(point_add)(multiples1[0], multiples1[0], multiples1[1]);
740         API_NS(point_add)(multiples2[0], multiples2[0], multiples2[1]);
741         point_double_internal(working, working, 0);
742         point_double_internal(tmp,         tmp, 0);
743         API_NS(point_add)(a1, working, multiples1[0]);
744         API_NS(point_add)(a2, tmp,     multiples2[0]);
745     } else {
746         API_NS(point_copy)(a1, multiples1[0]);
747         API_NS(point_copy)(a2, multiples2[0]);
748     }
749
750     decaf_bzero(scalar1x,sizeof(scalar1x));
751     decaf_bzero(scalar2x,sizeof(scalar2x));
752     decaf_bzero(pn,sizeof(pn));
753     decaf_bzero(multiples1,sizeof(multiples1));
754     decaf_bzero(multiples2,sizeof(multiples2));
755     decaf_bzero(tmp,sizeof(tmp));
756     decaf_bzero(working,sizeof(working));
757 }
758
759 decaf_bool_t API_NS(point_eq) ( const point_t p, const point_t q ) {
760     /* equality mod 2-torsion compares x/y */
761     gf a, b;
762     gf_mul ( a, p->y, q->x );
763     gf_mul ( b, q->y, p->x );
764     mask_t succ = gf_eq(a,b);
765     
766     #if (COFACTOR == 8) && IMAGINE_TWIST
767         gf_mul ( a, p->y, q->y );
768         gf_mul ( b, q->x, p->x );
769         #if !(IMAGINE_TWIST)
770             gf_sub ( a, ZERO, a );
771         #else
772            /* Interesting note: the 4tor would normally be rotation.
773             * But because of the *i twist, it's actually
774             * (x,y) <-> (iy,ix)
775             */
776     
777            /* No code, just a comment. */
778         #endif
779         succ |= gf_eq(a,b);
780     #endif
781     
782     return mask_to_bool(succ);
783 }
784
785 decaf_bool_t API_NS(point_valid) (
786     const point_t p
787 ) {
788     gf a,b,c;
789     gf_mul(a,p->x,p->y);
790     gf_mul(b,p->z,p->t);
791     mask_t out = gf_eq(a,b);
792     gf_sqr(a,p->x);
793     gf_sqr(b,p->y);
794     gf_sub(a,b,a);
795     gf_sqr(b,p->t);
796     gf_mulw(c,b,TWISTED_D);
797     gf_sqr(b,p->z);
798     gf_add(b,b,c);
799     out &= gf_eq(a,b);
800     out &= ~gf_eq(p->z,ZERO);
801     return mask_to_bool(out);
802 }
803
804 void API_NS(point_debugging_torque) (
805     point_t q,
806     const point_t p
807 ) {
808 #if COFACTOR == 8 && IMAGINE_TWIST
809     gf tmp;
810     gf_mul(tmp,p->x,SQRT_MINUS_ONE);
811     gf_mul(q->x,p->y,SQRT_MINUS_ONE);
812     gf_copy(q->y,tmp);
813     gf_copy(q->z,p->z);
814     gf_sub(q->t,ZERO,p->t);
815 #else
816     gf_sub(q->x,ZERO,p->x);
817     gf_sub(q->y,ZERO,p->y);
818     gf_copy(q->z,p->z);
819     gf_copy(q->t,p->t);
820 #endif
821 }
822
823 void API_NS(point_debugging_pscale) (
824     point_t q,
825     const point_t p,
826     const uint8_t factor[SER_BYTES]
827 ) {
828     gf gfac,tmp;
829     /* NB this means you'll never pscale by negative numbers for p521 */
830     ignore_result(gf_deserialize(gfac,factor,0,0));
831     gf_cond_sel(gfac,gfac,ONE,gf_eq(gfac,ZERO));
832     gf_mul(tmp,p->x,gfac);
833     gf_copy(q->x,tmp);
834     gf_mul(tmp,p->y,gfac);
835     gf_copy(q->y,tmp);
836     gf_mul(tmp,p->z,gfac);
837     gf_copy(q->z,tmp);
838     gf_mul(tmp,p->t,gfac);
839     gf_copy(q->t,tmp);
840 }
841
842 static void gf_batch_invert (
843     gf *__restrict__ out,
844     const gf *in,
845     unsigned int n
846 ) {
847     gf t1;
848     assert(n>1);
849   
850     gf_copy(out[1], in[0]);
851     int i;
852     for (i=1; i<(int) (n-1); i++) {
853         gf_mul(out[i+1], out[i], in[i]);
854     }
855     gf_mul(out[0], out[n-1], in[n-1]);
856
857     gf_invert(out[0], out[0], 1);
858
859     for (i=n-1; i>0; i--) {
860         gf_mul(t1, out[i], out[0]);
861         gf_copy(out[i], t1);
862         gf_mul(t1, out[0], in[i]);
863         gf_copy(out[0], t1);
864     }
865 }
866
867 static void batch_normalize_niels (
868     niels_t *table,
869     const gf *zs,
870     gf *__restrict__ zis,
871     int n
872 ) {
873     int i;
874     gf product;
875     gf_batch_invert(zis, zs, n);
876
877     for (i=0; i<n; i++) {
878         gf_mul(product, table[i]->a, zis[i]);
879         gf_strong_reduce(product);
880         gf_copy(table[i]->a, product);
881         
882         gf_mul(product, table[i]->b, zis[i]);
883         gf_strong_reduce(product);
884         gf_copy(table[i]->b, product);
885         
886         gf_mul(product, table[i]->c, zis[i]);
887         gf_strong_reduce(product);
888         gf_copy(table[i]->c, product);
889     }
890     
891     decaf_bzero(product,sizeof(product));
892 }
893
894 void API_NS(precompute) (
895     precomputed_s *table,
896     const point_t base
897 ) { 
898     const unsigned int n = COMBS_N, t = COMBS_T, s = COMBS_S;
899     assert(n*t*s >= SCALAR_BITS);
900   
901     point_t working, start, doubles[t-1];
902     API_NS(point_copy)(working, base);
903     pniels_t pn_tmp;
904   
905     gf zs[n<<(t-1)], zis[n<<(t-1)];
906   
907     unsigned int i,j,k;
908     
909     /* Compute n tables */
910     for (i=0; i<n; i++) {
911
912         /* Doubling phase */
913         for (j=0; j<t; j++) {
914             if (j) API_NS(point_add)(start, start, working);
915             else API_NS(point_copy)(start, working);
916
917             if (j==t-1 && i==n-1) break;
918
919             point_double_internal(working, working,0);
920             if (j<t-1) API_NS(point_copy)(doubles[j], working);
921
922             for (k=0; k<s-1; k++)
923                 point_double_internal(working, working, k<s-2);
924         }
925
926         /* Gray-code phase */
927         for (j=0;; j++) {
928             int gray = j ^ (j>>1);
929             int idx = (((i+1)<<(t-1))-1) ^ gray;
930
931             pt_to_pniels(pn_tmp, start);
932             memcpy(table->table[idx], pn_tmp->n, sizeof(pn_tmp->n));
933             gf_copy(zs[idx], pn_tmp->z);
934                         
935             if (j >= (1u<<(t-1)) - 1) break;
936             int delta = (j+1) ^ ((j+1)>>1) ^ gray;
937
938             for (k=0; delta>1; k++)
939                 delta >>=1;
940             
941             if (gray & (1<<k)) {
942                 API_NS(point_add)(start, start, doubles[k]);
943             } else {
944                 API_NS(point_sub)(start, start, doubles[k]);
945             }
946         }
947     }
948     
949     batch_normalize_niels(table->table,(const gf *)zs,zis,n<<(t-1));
950     
951     decaf_bzero(zs,sizeof(zs));
952     decaf_bzero(zis,sizeof(zis));
953     decaf_bzero(pn_tmp,sizeof(pn_tmp));
954     decaf_bzero(working,sizeof(working));
955     decaf_bzero(start,sizeof(start));
956     decaf_bzero(doubles,sizeof(doubles));
957 }
958
959 static DECAF_INLINE void
960 constant_time_lookup_niels (
961     niels_s *__restrict__ ni,
962     const niels_t *table,
963     int nelts,
964     int idx
965 ) {
966     constant_time_lookup(ni, table, sizeof(niels_s), nelts, idx);
967 }
968
969 void API_NS(precomputed_scalarmul) (
970     point_t out,
971     const precomputed_s *table,
972     const scalar_t scalar
973 ) {
974     int i;
975     unsigned j,k;
976     const unsigned int n = COMBS_N, t = COMBS_T, s = COMBS_S;
977     
978     scalar_t scalar1x;
979     API_NS(scalar_add)(scalar1x, scalar, precomputed_scalarmul_adjustment);
980     API_NS(scalar_halve)(scalar1x,scalar1x);
981     
982     niels_t ni;
983     
984     for (i=s-1; i>=0; i--) {
985         if (i != (int)s-1) point_double_internal(out,out,0);
986         
987         for (j=0; j<n; j++) {
988             int tab = 0;
989          
990             for (k=0; k<t; k++) {
991                 unsigned int bit = i + s*(k + j*t);
992                 if (bit < SCALAR_BITS) {
993                     tab |= (scalar1x->limb[bit/WBITS] >> (bit%WBITS) & 1) << k;
994                 }
995             }
996             
997             mask_t invert = (tab>>(t-1))-1;
998             tab ^= invert;
999             tab &= (1<<(t-1)) - 1;
1000
1001             constant_time_lookup_niels(ni, &table->table[j<<(t-1)], 1<<(t-1), tab);
1002
1003             cond_neg_niels(ni, invert);
1004             if ((i!=(int)s-1)||j) {
1005                 add_niels_to_pt(out, ni, j==n-1 && i);
1006             } else {
1007                 niels_to_pt(out, ni);
1008             }
1009         }
1010     }
1011     
1012     decaf_bzero(ni,sizeof(ni));
1013     decaf_bzero(scalar1x,sizeof(scalar1x));
1014 }
1015
1016 void API_NS(point_cond_sel) (
1017     point_t out,
1018     const point_t a,
1019     const point_t b,
1020     decaf_bool_t pick_b
1021 ) {
1022     constant_time_select(out,a,b,sizeof(point_t),bool_to_mask(pick_b),0);
1023 }
1024
1025 /* FUTURE: restore Curve25519 Montgomery ladder? */
1026 decaf_error_t API_NS(direct_scalarmul) (
1027     uint8_t scaled[SER_BYTES],
1028     const uint8_t base[SER_BYTES],
1029     const scalar_t scalar,
1030     decaf_bool_t allow_identity,
1031     decaf_bool_t short_circuit
1032 ) {
1033     point_t basep;
1034     decaf_error_t succ = API_NS(point_decode)(basep, base, allow_identity);
1035     if (short_circuit && succ != DECAF_SUCCESS) return succ;
1036     API_NS(point_cond_sel)(basep, API_NS(point_base), basep, succ);
1037     API_NS(point_scalarmul)(basep, basep, scalar);
1038     API_NS(point_encode)(scaled, basep);
1039     API_NS(point_destroy)(basep);
1040     return succ;
1041 }
1042
1043 void API_NS(point_mul_by_ratio_and_encode_like_eddsa) (
1044     uint8_t enc[DECAF_EDDSA_448_PUBLIC_BYTES],
1045     const point_t p
1046 ) {
1047     
1048     /* The point is now on the twisted curve.  Move it to untwisted. */
1049     gf x, y, z, t;
1050     point_t q;
1051 #if COFACTOR == 8
1052     API_NS(point_double)(q,p);
1053 #else
1054     API_NS(point_copy)(q,p);
1055 #endif
1056     
1057 #if EDDSA_USE_SIGMA_ISOGENY
1058     {
1059         /* Use 4-isogeny like ed25519:
1060          *   2*x*y*sqrt(d/a-1)/(ax^2 + y^2 - 2)
1061          *   (y^2 - ax^2)/(y^2 + ax^2)
1062          * with a = -1, d = -EDWARDS_D:
1063          *   -2xysqrt(EDWARDS_D-1)/(2z^2-y^2+x^2)
1064          *   (y^2+x^2)/(y^2-x^2)
1065          */
1066         gf u;
1067         gf_sqr ( x, q->x ); // x^2
1068         gf_sqr ( t, q->y ); // y^2
1069         gf_add( u, x, t ); // x^2 + y^2
1070         gf_add( z, q->y, q->x );
1071         gf_sqr ( y, z);
1072         gf_sub ( y, u, y ); // -2xy
1073         gf_sub ( z, t, x ); // y^2 - x^2
1074         gf_sqr ( x, q->z );
1075         gf_add ( t, x, x);
1076         gf_sub ( t, t, z);  // 2z^2 - y^2 + x^2
1077         gf_mul ( x, y, z ); // 2xy(y^2-x^2)
1078         gf_mul ( y, u, t ); // (x^2+y^2)(2z^2-y^2+x^2)
1079         gf_mul ( u, z, t );
1080         gf_copy( z, u );
1081         gf_mul ( u, x, RISTRETTO_FACTOR );
1082 #if IMAGINE_TWIST
1083         gf_mul_i( x, u );
1084 #else
1085 #error "... probably wrong"
1086         gf_copy( x, u );
1087 #endif
1088         decaf_bzero(u,sizeof(u));
1089     }
1090 #elif IMAGINE_TWIST
1091     {
1092         API_NS(point_double)(q,q);
1093         API_NS(point_double)(q,q);
1094         gf_mul_i(x, q->x);
1095         gf_copy(y, q->y);
1096         gf_copy(z, q->z);
1097     }
1098 #else
1099     {
1100         /* 4-isogeny: 2xy/(y^+x^2), (y^2-x^2)/(2z^2-y^2+x^2) */
1101         gf u;
1102         gf_sqr ( x, q->x );
1103         gf_sqr ( t, q->y );
1104         gf_add( u, x, t );
1105         gf_add( z, q->y, q->x );
1106         gf_sqr ( y, z);
1107         gf_sub ( y, y, u );
1108         gf_sub ( z, t, x );
1109         gf_sqr ( x, q->z );
1110         gf_add ( t, x, x); 
1111         gf_sub ( t, t, z);
1112         gf_mul ( x, t, y );
1113         gf_mul ( y, z, u );
1114         gf_mul ( z, u, t );
1115         decaf_bzero(u,sizeof(u));
1116     }
1117 #endif
1118     /* Affinize */
1119     gf_invert(z,z,1);
1120     gf_mul(t,x,z);
1121     gf_mul(x,y,z);
1122     
1123     /* Encode */
1124     enc[DECAF_EDDSA_448_PRIVATE_BYTES-1] = 0;
1125     gf_serialize(enc, x, 1);
1126     enc[DECAF_EDDSA_448_PRIVATE_BYTES-1] |= 0x80 & gf_lobit(t);
1127
1128     decaf_bzero(x,sizeof(x));
1129     decaf_bzero(y,sizeof(y));
1130     decaf_bzero(z,sizeof(z));
1131     decaf_bzero(t,sizeof(t));
1132     API_NS(point_destroy)(q);
1133 }
1134
1135
1136 decaf_error_t API_NS(point_decode_like_eddsa_and_mul_by_ratio) (
1137     point_t p,
1138     const uint8_t enc[DECAF_EDDSA_448_PUBLIC_BYTES]
1139 ) {
1140     uint8_t enc2[DECAF_EDDSA_448_PUBLIC_BYTES];
1141     memcpy(enc2,enc,sizeof(enc2));
1142
1143     mask_t low = ~word_is_zero(enc2[DECAF_EDDSA_448_PRIVATE_BYTES-1] & 0x80);
1144     enc2[DECAF_EDDSA_448_PRIVATE_BYTES-1] &= ~0x80;
1145     
1146     mask_t succ = gf_deserialize(p->y, enc2, 1, 0);
1147 #if 0 == 0
1148     succ &= word_is_zero(enc2[DECAF_EDDSA_448_PRIVATE_BYTES-1]);
1149 #endif
1150
1151     gf_sqr(p->x,p->y);
1152     gf_sub(p->z,ONE,p->x); /* num = 1-y^2 */
1153     #if EDDSA_USE_SIGMA_ISOGENY
1154         gf_mulw(p->t,p->z,EDWARDS_D); /* d-dy^2 */
1155         gf_mulw(p->x,p->z,EDWARDS_D-1); /* num = (1-y^2)(d-1) */
1156         gf_copy(p->z,p->x);
1157     #else
1158         gf_mulw(p->t,p->x,EDWARDS_D); /* dy^2 */
1159     #endif
1160     gf_sub(p->t,ONE,p->t); /* denom = 1-dy^2 or 1-d + dy^2 */
1161     
1162     gf_mul(p->x,p->z,p->t);
1163     succ &= gf_isr(p->t,p->x); /* 1/sqrt(num * denom) */
1164     
1165     gf_mul(p->x,p->t,p->z); /* sqrt(num / denom) */
1166     gf_cond_neg(p->x,gf_lobit(p->x)^low);
1167     gf_copy(p->z,ONE);
1168   
1169     #if EDDSA_USE_SIGMA_ISOGENY
1170     {
1171        /* Use 4-isogeny like ed25519:
1172         *   2*x*y/sqrt(1-d/a)/(ax^2 + y^2 - 2)
1173         *   (y^2 - ax^2)/(y^2 + ax^2)
1174         * (MAGIC: above formula may be off by a factor of -a
1175         * or something somewhere; check it for other a)
1176         *
1177         * with a = -1, d = -EDWARDS_D:
1178         *   -2xy/sqrt(1-EDWARDS_D)/(2z^2-y^2+x^2)
1179         *   (y^2+x^2)/(y^2-x^2)
1180         */
1181         gf a, b, c, d;
1182         gf_sqr ( c, p->x );
1183         gf_sqr ( a, p->y );
1184         gf_add ( d, c, a ); // x^2 + y^2
1185         gf_add ( p->t, p->y, p->x );
1186         gf_sqr ( b, p->t );
1187         gf_sub ( b, b, d ); // 2xy
1188         gf_sub ( p->t, a, c ); // y^2 - x^2
1189         gf_sqr ( p->x, p->z );
1190         gf_add ( p->z, p->x, p->x );
1191         gf_sub ( c, p->z, p->t ); // 2z^2 - y^2 + x^2
1192         gf_div_i ( a, c );
1193         gf_mul ( c, a, RISTRETTO_FACTOR );
1194         gf_mul ( p->x, b, p->t); // (2xy)(y^2-x^2)
1195         gf_mul ( p->z, p->t, c ); // (y^2-x^2)sd(2z^2 - y^2 + x^2)
1196         gf_mul ( p->y, d, c ); // (y^2+x^2)sd(2z^2 - y^2 + x^2)
1197         gf_mul ( p->t, d, b );
1198         decaf_bzero(a,sizeof(a));
1199         decaf_bzero(b,sizeof(b));
1200         decaf_bzero(c,sizeof(c));
1201         decaf_bzero(d,sizeof(d));
1202     } 
1203     #elif IMAGINE_TWIST
1204     {
1205         gf_mul(p->t,p->x,SQRT_MINUS_ONE);
1206         gf_copy(p->x,p->t);
1207         gf_mul(p->t,p->x,p->y);
1208     }
1209     #else
1210     {
1211         /* 4-isogeny 2xy/(y^2-ax^2), (y^2+ax^2)/(2-y^2-ax^2) */
1212         gf a, b, c, d;
1213         gf_sqr ( c, p->x );
1214         gf_sqr ( a, p->y );
1215         gf_add ( d, c, a );
1216         gf_add ( p->t, p->y, p->x );
1217         gf_sqr ( b, p->t );
1218         gf_sub ( b, b, d );
1219         gf_sub ( p->t, a, c );
1220         gf_sqr ( p->x, p->z );
1221         gf_add ( p->z, p->x, p->x );
1222         gf_sub ( a, p->z, d );
1223         gf_mul ( p->x, a, b );
1224         gf_mul ( p->z, p->t, a );
1225         gf_mul ( p->y, p->t, d );
1226         gf_mul ( p->t, b, d );
1227         decaf_bzero(a,sizeof(a));
1228         decaf_bzero(b,sizeof(b));
1229         decaf_bzero(c,sizeof(c));
1230         decaf_bzero(d,sizeof(d));
1231     }
1232     #endif
1233     
1234     decaf_bzero(enc2,sizeof(enc2));
1235     assert(API_NS(point_valid)(p) || ~succ);
1236     
1237     return decaf_succeed_if(mask_to_bool(succ));
1238 }
1239
1240 decaf_error_t decaf_x448 (
1241     uint8_t out[X_PUBLIC_BYTES],
1242     const uint8_t base[X_PUBLIC_BYTES],
1243     const uint8_t scalar[X_PRIVATE_BYTES]
1244 ) {
1245     gf x1, x2, z2, x3, z3, t1, t2;
1246     ignore_result(gf_deserialize(x1,base,1,0));
1247     gf_copy(x2,ONE);
1248     gf_copy(z2,ZERO);
1249     gf_copy(x3,x1);
1250     gf_copy(z3,ONE);
1251     
1252     int t;
1253     mask_t swap = 0;
1254     
1255     for (t = X_PRIVATE_BITS-1; t>=0; t--) {
1256         uint8_t sb = scalar[t/8];
1257         
1258         /* Scalar conditioning */
1259         if (t/8==0) sb &= -(uint8_t)COFACTOR;
1260         else if (t == X_PRIVATE_BITS-1) sb = -1;
1261         
1262         mask_t k_t = (sb>>(t%8)) & 1;
1263         k_t = -k_t; /* set to all 0s or all 1s */
1264         
1265         swap ^= k_t;
1266         gf_cond_swap(x2,x3,swap);
1267         gf_cond_swap(z2,z3,swap);
1268         swap = k_t;
1269         
1270         gf_add_nr(t1,x2,z2); /* A = x2 + z2 */        /* 2+e */
1271         gf_sub_nr(t2,x2,z2); /* B = x2 - z2 */        /* 3+e */
1272         gf_sub_nr(z2,x3,z3); /* D = x3 - z3 */        /* 3+e */
1273         gf_mul(x2,t1,z2);    /* DA */
1274         gf_add_nr(z2,z3,x3); /* C = x3 + z3 */        /* 2+e */
1275         gf_mul(x3,t2,z2);    /* CB */
1276         gf_sub_nr(z3,x2,x3); /* DA-CB */              /* 3+e */
1277         gf_sqr(z2,z3);       /* (DA-CB)^2 */
1278         gf_mul(z3,x1,z2);    /* z3 = x1(DA-CB)^2 */
1279         gf_add_nr(z2,x2,x3); /* (DA+CB) */            /* 2+e */
1280         gf_sqr(x3,z2);       /* x3 = (DA+CB)^2 */
1281         
1282         gf_sqr(z2,t1);       /* AA = A^2 */
1283         gf_sqr(t1,t2);       /* BB = B^2 */
1284         gf_mul(x2,z2,t1);    /* x2 = AA*BB */
1285         gf_sub_nr(t2,z2,t1); /* E = AA-BB */          /* 3+e */
1286         
1287         gf_mulw(t1,t2,-EDWARDS_D); /* E*-d = a24*E */
1288         gf_add_nr(t1,t1,z2); /* AA + a24*E */         /* 2+e */
1289         gf_mul(z2,t2,t1); /* z2 = E(AA+a24*E) */
1290     }
1291     
1292     /* Finish */
1293     gf_cond_swap(x2,x3,swap);
1294     gf_cond_swap(z2,z3,swap);
1295     gf_invert(z2,z2,0);
1296     gf_mul(x1,x2,z2);
1297     gf_serialize(out,x1,1);
1298     mask_t nz = ~gf_eq(x1,ZERO);
1299     
1300     decaf_bzero(x1,sizeof(x1));
1301     decaf_bzero(x2,sizeof(x2));
1302     decaf_bzero(z2,sizeof(z2));
1303     decaf_bzero(x3,sizeof(x3));
1304     decaf_bzero(z3,sizeof(z3));
1305     decaf_bzero(t1,sizeof(t1));
1306     decaf_bzero(t2,sizeof(t2));
1307     
1308     return decaf_succeed_if(mask_to_bool(nz));
1309 }
1310
1311 /* Thanks Johan Pascal */
1312 void decaf_ed448_convert_public_key_to_x448 (
1313     uint8_t x[DECAF_X448_PUBLIC_BYTES],
1314     const uint8_t ed[DECAF_EDDSA_448_PUBLIC_BYTES]
1315 ) {
1316     gf y;
1317     const uint8_t mask = (uint8_t)(0xFE<<(7));
1318     ignore_result(gf_deserialize(y, ed, 1, mask));
1319     
1320     {
1321         gf n,d;
1322         
1323 #if EDDSA_USE_SIGMA_ISOGENY
1324         /* u = (1+y)/(1-y)*/
1325         gf_add(n, y, ONE); /* n = y+1 */
1326         gf_sub(d, ONE, y); /* d = 1-y */
1327         gf_invert(d, d, 0); /* d = 1/(1-y) */
1328         gf_mul(y, n, d); /* u = (y+1)/(1-y) */
1329         gf_serialize(x,y,1);
1330 #else /* EDDSA_USE_SIGMA_ISOGENY */
1331         /* u = y^2 * (1-dy^2) / (1-y^2) */
1332         gf_sqr(n,y); /* y^2*/
1333         gf_sub(d,ONE,n); /* 1-y^2*/
1334         gf_invert(d,d,0); /* 1/(1-y^2)*/
1335         gf_mul(y,n,d); /* y^2 / (1-y^2) */
1336         gf_mulw(d,n,EDWARDS_D); /* dy^2*/
1337         gf_sub(d, ONE, d); /* 1-dy^2*/
1338         gf_mul(n, y, d); /* y^2 * (1-dy^2) / (1-y^2) */
1339         gf_serialize(x,n,1);
1340 #endif /* EDDSA_USE_SIGMA_ISOGENY */
1341         
1342         decaf_bzero(y,sizeof(y));
1343         decaf_bzero(n,sizeof(n));
1344         decaf_bzero(d,sizeof(d));
1345     }
1346 }
1347
1348 void decaf_x448_generate_key (
1349     uint8_t out[X_PUBLIC_BYTES],
1350     const uint8_t scalar[X_PRIVATE_BYTES]
1351 ) {
1352     decaf_x448_derive_public_key(out,scalar);
1353 }
1354
1355 void API_NS(point_mul_by_ratio_and_encode_like_x448) (
1356     uint8_t out[X_PUBLIC_BYTES],
1357     const point_t p
1358 ) {
1359     point_t q;
1360 #if COFACTOR == 8
1361     point_double_internal(q,p,1);
1362 #else
1363     API_NS(point_copy)(q,p);
1364 #endif
1365     gf_invert(q->t,q->x,0); /* 1/x */
1366     gf_mul(q->z,q->t,q->y); /* y/x */
1367     gf_sqr(q->y,q->z); /* (y/x)^2 */
1368 #if IMAGINE_TWIST
1369     gf_sub(q->y,ZERO,q->y);
1370 #endif
1371     gf_serialize(out,q->y,1);
1372     API_NS(point_destroy(q));
1373 }
1374
1375 void decaf_x448_derive_public_key (
1376     uint8_t out[X_PUBLIC_BYTES],
1377     const uint8_t scalar[X_PRIVATE_BYTES]
1378 ) {
1379     /* Scalar conditioning */
1380     uint8_t scalar2[X_PRIVATE_BYTES];
1381     memcpy(scalar2,scalar,sizeof(scalar2));
1382     scalar2[0] &= -(uint8_t)COFACTOR;
1383     
1384     scalar2[X_PRIVATE_BYTES-1] &= ~(-1u<<((X_PRIVATE_BITS+7)%8));
1385     scalar2[X_PRIVATE_BYTES-1] |= 1<<((X_PRIVATE_BITS+7)%8);
1386     
1387     scalar_t the_scalar;
1388     API_NS(scalar_decode_long)(the_scalar,scalar2,sizeof(scalar2));
1389     
1390     /* Compensate for the encoding ratio */
1391     for (unsigned i=1; i<DECAF_X448_ENCODE_RATIO; i<<=1) {
1392         API_NS(scalar_halve)(the_scalar,the_scalar);
1393     }
1394     point_t p;
1395     API_NS(precomputed_scalarmul)(p,API_NS(precomputed_base),the_scalar);
1396     API_NS(point_mul_by_ratio_and_encode_like_x448)(out,p);
1397     API_NS(point_destroy)(p);
1398 }
1399
1400 /**
1401  * @cond internal
1402  * Control for variable-time scalar multiply algorithms.
1403  */
1404 struct smvt_control {
1405   int power, addend;
1406 };
1407
1408 static int recode_wnaf (
1409     struct smvt_control *control, /* [nbits/(table_bits+1) + 3] */
1410     const scalar_t scalar,
1411     unsigned int table_bits
1412 ) {
1413     unsigned int table_size = SCALAR_BITS/(table_bits+1) + 3;
1414     int position = table_size - 1; /* at the end */
1415     
1416     /* place the end marker */
1417     control[position].power = -1;
1418     control[position].addend = 0;
1419     position--;
1420
1421     /* PERF: Could negate scalar if it's large.  But then would need more cases
1422      * in the actual code that uses it, all for an expected reduction of like 1/5 op.
1423      * Probably not worth it.
1424      */
1425     
1426     uint64_t current = scalar->limb[0] & 0xFFFF;
1427     uint32_t mask = (1<<(table_bits+1))-1;
1428
1429     unsigned int w;
1430     const unsigned int B_OVER_16 = sizeof(scalar->limb[0]) / 2;
1431     for (w = 1; w<(SCALAR_BITS-1)/16+3; w++) {
1432         if (w < (SCALAR_BITS-1)/16+1) {
1433             /* Refill the 16 high bits of current */
1434             current += (uint32_t)((scalar->limb[w/B_OVER_16]>>(16*(w%B_OVER_16)))<<16);
1435         }
1436         
1437         while (current & 0xFFFF) {
1438             assert(position >= 0);
1439             uint32_t pos = __builtin_ctz((uint32_t)current), odd = (uint32_t)current >> pos;
1440             int32_t delta = odd & mask;
1441             if (odd & 1<<(table_bits+1)) delta -= (1<<(table_bits+1));
1442             current -= delta << pos;
1443             control[position].power = pos + 16*(w-1);
1444             control[position].addend = delta;
1445             position--;
1446         }
1447         current >>= 16;
1448     }
1449     assert(current==0);
1450     
1451     position++;
1452     unsigned int n = table_size - position;
1453     unsigned int i;
1454     for (i=0; i<n; i++) {
1455         control[i] = control[i+position];
1456     }
1457     return n-1;
1458 }
1459
1460 static void
1461 prepare_wnaf_table(
1462     pniels_t *output,
1463     const point_t working,
1464     unsigned int tbits
1465 ) {
1466     point_t tmp;
1467     int i;
1468     pt_to_pniels(output[0], working);
1469
1470     if (tbits == 0) return;
1471
1472     API_NS(point_double)(tmp,working);
1473     pniels_t twop;
1474     pt_to_pniels(twop, tmp);
1475
1476     add_pniels_to_pt(tmp, output[0],0);
1477     pt_to_pniels(output[1], tmp);
1478
1479     for (i=2; i < 1<<tbits; i++) {
1480         add_pniels_to_pt(tmp, twop,0);
1481         pt_to_pniels(output[i], tmp);
1482     }
1483     
1484     API_NS(point_destroy)(tmp);
1485     decaf_bzero(twop,sizeof(twop));
1486 }
1487
1488 extern const gf API_NS(precomputed_wnaf_as_fe)[];
1489 static const niels_t *API_NS(wnaf_base) = (const niels_t *)API_NS(precomputed_wnaf_as_fe);
1490 const size_t API_NS(sizeof_precomputed_wnafs) __attribute((visibility("hidden")))
1491     = sizeof(niels_t)<<DECAF_WNAF_FIXED_TABLE_BITS;
1492
1493 void API_NS(precompute_wnafs) (
1494     niels_t out[1<<DECAF_WNAF_FIXED_TABLE_BITS],
1495     const point_t base
1496 ) __attribute__ ((visibility ("hidden")));
1497
1498 void API_NS(precompute_wnafs) (
1499     niels_t out[1<<DECAF_WNAF_FIXED_TABLE_BITS],
1500     const point_t base
1501 ) {
1502     pniels_t tmp[1<<DECAF_WNAF_FIXED_TABLE_BITS];
1503     gf zs[1<<DECAF_WNAF_FIXED_TABLE_BITS], zis[1<<DECAF_WNAF_FIXED_TABLE_BITS];
1504     int i;
1505     prepare_wnaf_table(tmp,base,DECAF_WNAF_FIXED_TABLE_BITS);
1506     for (i=0; i<1<<DECAF_WNAF_FIXED_TABLE_BITS; i++) {
1507         memcpy(out[i], tmp[i]->n, sizeof(niels_t));
1508         gf_copy(zs[i], tmp[i]->z);
1509     }
1510     batch_normalize_niels(out, (const gf *)zs, zis, 1<<DECAF_WNAF_FIXED_TABLE_BITS);
1511     
1512     decaf_bzero(tmp,sizeof(tmp));
1513     decaf_bzero(zs,sizeof(zs));
1514     decaf_bzero(zis,sizeof(zis));
1515 }
1516
1517 void API_NS(base_double_scalarmul_non_secret) (
1518     point_t combo,
1519     const scalar_t scalar1,
1520     const point_t base2,
1521     const scalar_t scalar2
1522 ) {
1523     const int table_bits_var = DECAF_WNAF_VAR_TABLE_BITS,
1524         table_bits_pre = DECAF_WNAF_FIXED_TABLE_BITS;
1525     struct smvt_control control_var[SCALAR_BITS/(table_bits_var+1)+3];
1526     struct smvt_control control_pre[SCALAR_BITS/(table_bits_pre+1)+3];
1527     
1528     int ncb_pre = recode_wnaf(control_pre, scalar1, table_bits_pre);
1529     int ncb_var = recode_wnaf(control_var, scalar2, table_bits_var);
1530   
1531     pniels_t precmp_var[1<<table_bits_var];
1532     prepare_wnaf_table(precmp_var, base2, table_bits_var);
1533   
1534     int contp=0, contv=0, i = control_var[0].power;
1535
1536     if (i < 0) {
1537         API_NS(point_copy)(combo, API_NS(point_identity));
1538         return;
1539     } else if (i > control_pre[0].power) {
1540         pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
1541         contv++;
1542     } else if (i == control_pre[0].power && i >=0 ) {
1543         pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
1544         add_niels_to_pt(combo, API_NS(wnaf_base)[control_pre[0].addend >> 1], i);
1545         contv++; contp++;
1546     } else {
1547         i = control_pre[0].power;
1548         niels_to_pt(combo, API_NS(wnaf_base)[control_pre[0].addend >> 1]);
1549         contp++;
1550     }
1551     
1552     for (i--; i >= 0; i--) {
1553         int cv = (i==control_var[contv].power), cp = (i==control_pre[contp].power);
1554         point_double_internal(combo,combo,i && !(cv||cp));
1555
1556         if (cv) {
1557             assert(control_var[contv].addend);
1558
1559             if (control_var[contv].addend > 0) {
1560                 add_pniels_to_pt(combo, precmp_var[control_var[contv].addend >> 1], i&&!cp);
1561             } else {
1562                 sub_pniels_from_pt(combo, precmp_var[(-control_var[contv].addend) >> 1], i&&!cp);
1563             }
1564             contv++;
1565         }
1566
1567         if (cp) {
1568             assert(control_pre[contp].addend);
1569
1570             if (control_pre[contp].addend > 0) {
1571                 add_niels_to_pt(combo, API_NS(wnaf_base)[control_pre[contp].addend >> 1], i);
1572             } else {
1573                 sub_niels_from_pt(combo, API_NS(wnaf_base)[(-control_pre[contp].addend) >> 1], i);
1574             }
1575             contp++;
1576         }
1577     }
1578     
1579     /* This function is non-secret, but whatever this is cheap. */
1580     decaf_bzero(control_var,sizeof(control_var));
1581     decaf_bzero(control_pre,sizeof(control_pre));
1582     decaf_bzero(precmp_var,sizeof(precmp_var));
1583
1584     assert(contv == ncb_var); (void)ncb_var;
1585     assert(contp == ncb_pre); (void)ncb_pre;
1586 }
1587
1588 void API_NS(point_destroy) (
1589     point_t point
1590 ) {
1591     decaf_bzero(point, sizeof(point_t));
1592 }
1593
1594 void API_NS(precomputed_destroy) (
1595     precomputed_s *pre
1596 ) {
1597     decaf_bzero(pre, API_NS(sizeof_precomputed_s));
1598 }