Commit | Line | Data |
---|---|---|
8a7654a6 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Definitions for Montgomery's ladder | |
4 | * | |
5 | * (c) 2017 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of secnet. | |
11 | * See README for full list of copyright holders. | |
12 | * | |
13 | * secnet is free software; you can redistribute it and/or modify it | |
14 | * under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version d of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * secnet is distributed in the hope that it will be useful, but | |
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | * General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * version 3 along with secnet; if not, see | |
25 | * https://www.gnu.org/licenses/gpl.html. | |
26 | * | |
27 | * This file was originally part of Catacomb, but has been automatically | |
28 | * modified for incorporation into secnet: see `import-catacomb-crypto' | |
29 | * for details. | |
30 | * | |
31 | * Catacomb is free software; you can redistribute it and/or modify | |
32 | * it under the terms of the GNU Library General Public License as | |
33 | * published by the Free Software Foundation; either version 2 of the | |
34 | * License, or (at your option) any later version. | |
35 | * | |
36 | * Catacomb is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
39 | * GNU Library General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU Library General Public | |
42 | * License along with Catacomb; if not, write to the Free | |
43 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
44 | * MA 02111-1307, USA. | |
45 | */ | |
46 | ||
47 | #ifndef CATACOMB_MONTLADDER_H | |
48 | #define CATACOMB_MONTLADDER_H | |
49 | ||
50 | #ifdef __cplusplus | |
51 | extern "C" { | |
52 | #endif | |
53 | ||
54 | /*----- Notes on the Montgomery ladder ------------------------------------* | |
55 | * | |
56 | * The algorithm here is Montgomery's famous binary ladder for calculating | |
57 | * x-coordinates of scalar products on a particular shape of elliptic curve, | |
58 | * as elucidated by Daniel Bernstein. | |
59 | * | |
60 | * Let Q = (x_1, y_1) be the base point, for some unknown y_1 (which will | |
61 | * turn out to be unimportant). Define x_n, z_n by x(n Q) = (x_n : z_n). | |
62 | * Given x_n, z_n, x_{n+1}, z_{n+1}, Montgomery's differential addition | |
63 | * formulae calculate x_{2i}, z_{2i}, x_{2i+1}, z_{2i+1}. Furthermore, | |
64 | * calculating x_{2i}, z_{2i} requires only x_n, z_n, and the calculation of | |
65 | * x_{2i+1}, z_{2i+1} is symmetrical. | |
66 | */ | |
67 | ||
68 | /*----- Functions provided ------------------------------------------------*/ | |
69 | ||
70 | /* F designates a field, both naming the type of its elements and acting as a | |
71 | * prefix for the standard field operations `F_add', `F_sub', `F_mul', | |
72 | * `F_sqr', and `F_inv' (the last of which should return zero its own | |
73 | * inverse); and the constant-time utility `F_condswap'. | |
74 | * | |
75 | * The macro calculates the x-coordinate of the product k Q, where Q is a | |
76 | * point on the elliptic curve B y^2 = x^3 + A x^2 + x or its quadratic | |
77 | * twist, for some irrelevant B. The x-coordinate of Q is given as X1 (a | |
78 | * pointer to a field element). The scalar k is given as a vector of NK | |
79 | * unsigned integers KW, each containing NBITS significant bits, with the | |
80 | * least-significant element first. The result is written to the field | |
81 | * element pointed to by Z. | |
82 | * | |
83 | * The curve coefficient A is given indirectly, as the name of a macro MULA0 | |
84 | * such that | |
85 | * | |
86 | * MULA0(z, x) | |
87 | * | |
88 | * will store in z the value (A - 2)/4 x. | |
89 | */ | |
90 | #define MONT_LADDER(f, mula0, kw, nk, nbits, z, x1) do { \ | |
91 | f _x, _z, _u, _w; \ | |
92 | f _t0, _t1, _t2, _t3, _t4; \ | |
93 | uint32 _m = 0, _mm = 0, _k; \ | |
94 | unsigned _i, _j; \ | |
95 | \ | |
96 | /* Initialize the main variables. We'll have, (x, z) and (u, w) \ | |
97 | * holding (x_n, z_n) and (x_{n+1}, z_{n+1}) in some order, but \ | |
98 | * there's some weirdness: if m = 0 then (x, z) = (x_n, z_n) and \ | |
99 | * (u, v) = (x_{n+1}, z_{n+1}); if m /= 0, then the pairs are \ | |
100 | * swapped over. \ | |
101 | * \ | |
102 | * Initially, we have (x_0, z_0) = (1, 0), representing the identity \ | |
103 | * at projective-infinity, which works fine; and we have z_1 = 1. \ | |
104 | */ \ | |
105 | _u = *(x1); f##_set(&_w, 1); f##_set(&_x, 1); f##_set(&_z, 0); \ | |
106 | \ | |
107 | /* The main ladder loop. Work through each bit of the clamped key. */ \ | |
108 | for (_i = (nk); _i--; ) { \ | |
109 | _k = (kw)[_i]; \ | |
110 | for (_j = 0; _j < (nbits); _j++) { \ | |
111 | /* We're at bit i of the scalar key (represented by 32 (7 - i) + \ | |
112 | * (31 - j) in our loop variables -- don't worry about that). \ | |
113 | * Let k = 2^i k_i + k'_i, with 0 <= k'_i < 2^i. In particular, \ | |
114 | * then, k_0 = k. Write Q(i) = (x_i, z_i). \ | |
115 | * \ | |
116 | * We currently have, in (x, z) and (u, w), Q(k_i) and Q(k_i + \ | |
117 | * 1), in some order. The ladder step will double the point in \ | |
118 | * (x, z), and leave the sum of (x : z) and (u : w) in (u, w). \ | |
119 | */ \ | |
120 | \ | |
121 | _mm = -((_k >> ((nbits) - 1))&1u); _k <<= 1; \ | |
122 | f##_condswap(&_x, &_u, _m ^ _mm); \ | |
123 | f##_condswap(&_z, &_w, _m ^ _mm); \ | |
124 | _m = _mm; \ | |
125 | \ | |
126 | f##_add(&_t0, &_x, &_z); /* x + z */ \ | |
127 | f##_sub(&_t1, &_x, &_z); /* x - z */ \ | |
128 | f##_add(&_t2, &_u, &_w); /* u + w */ \ | |
129 | f##_sub(&_t3, &_u, &_w); /* u - w */ \ | |
130 | f##_mul(&_t2, &_t2, &_t1); /* (x - z) (u + w) */ \ | |
131 | f##_mul(&_t3, &_t3, &_t0); /* (x + z) (u - w) */ \ | |
132 | f##_sqr(&_t0, &_t0); /* (x + z)^2 */ \ | |
133 | f##_sqr(&_t1, &_t1); /* (x - z)^2 */ \ | |
134 | f##_mul(&_x, &_t0, &_t1); /* (x + z)^2 (x - z)^2 */ \ | |
135 | f##_sub(&_t1, &_t0, &_t1); /* (x + z)^2 - (x - z)^2 */ \ | |
136 | mula0(&_t4, &_t1); /* A_0 ((x + z)^2 - (x - z)^2) */ \ | |
137 | f##_add(&_t0, &_t0, &_t4); /* A_0 ... + (x + z)^2 */ \ | |
138 | f##_mul(&_z, &_t0, &_t1); /* (...^2 - ...^2) (A_0 ... + ...) */ \ | |
139 | f##_add(&_t0, &_t2, &_t3); /* (x - z) (u + w) + (x + z) (u - w) */ \ | |
140 | f##_sub(&_t1, &_t2, &_t3); /* (x - z) (u + w) - (x + z) (u - w) */ \ | |
141 | f##_sqr(&_u, &_t0); /* (... + ...)^2 */ \ | |
142 | f##_sqr(&_t1, &_t1); /* (... - ...)^2 */ \ | |
143 | f##_mul(&_w, &_t1, (x1)); /* x_1 (... - ...)^2 */ \ | |
144 | } \ | |
145 | } \ | |
146 | \ | |
147 | /* Almost done. Undo the swap, if any. */ \ | |
148 | f##_condswap(&_x, &_u, _m); \ | |
149 | f##_condswap(&_z, &_w, _m); \ | |
150 | \ | |
151 | /* And convert to affine. */ \ | |
152 | f##_inv(&_t0, &_z); \ | |
153 | f##_mul((z), &_x, &_t0); \ | |
154 | } while (0) | |
155 | ||
156 | /*----- That's all, folks -------------------------------------------------*/ | |
157 | ||
158 | #ifdef __cplusplus | |
159 | } | |
160 | #endif | |
161 | ||
162 | #endif |