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