26 |
7u83 |
1 |
/* Copyright 2008, Google Inc.
|
|
|
2 |
* All rights reserved.
|
|
|
3 |
*
|
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
|
5 |
* modification, are permitted provided that the following conditions are
|
|
|
6 |
* met:
|
|
|
7 |
*
|
|
|
8 |
* * Redistributions of source code must retain the above copyright
|
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
|
10 |
* * Redistributions in binary form must reproduce the above
|
|
|
11 |
* copyright notice, this list of conditions and the following disclaimer
|
|
|
12 |
* in the documentation and/or other materials provided with the
|
|
|
13 |
* distribution.
|
|
|
14 |
* * Neither the name of Google Inc. nor the names of its
|
|
|
15 |
* contributors may be used to endorse or promote products derived from
|
|
|
16 |
* this software without specific prior written permission.
|
|
|
17 |
*
|
|
|
18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
29 |
*
|
|
|
30 |
* curve25519: Curve25519 elliptic curve, public key function
|
|
|
31 |
*
|
|
|
32 |
* http://code.google.com/p/curve25519-donna/
|
|
|
33 |
*
|
|
|
34 |
* Adam Langley <agl@imperialviolet.org>
|
|
|
35 |
*
|
|
|
36 |
* Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
|
|
|
37 |
*
|
|
|
38 |
* More information about curve25519 can be found here
|
|
|
39 |
* http://cr.yp.to/ecdh.html
|
|
|
40 |
*
|
|
|
41 |
* djb's sample implementation of curve25519 is written in a special assembly
|
|
|
42 |
* language called qhasm and uses the floating point registers.
|
|
|
43 |
*
|
|
|
44 |
* This is, almost, a clean room reimplementation from the curve25519 paper. It
|
|
|
45 |
* uses many of the tricks described therein. Only the crecip function is taken
|
|
|
46 |
* from the sample implementation.
|
|
|
47 |
*/
|
|
|
48 |
#include "os.h"
|
|
|
49 |
#include <libsec.h>
|
|
|
50 |
|
|
|
51 |
typedef vlong felem;
|
|
|
52 |
|
|
|
53 |
/* Sum two numbers: output += in */
|
|
|
54 |
static void fsum(felem *output, felem *in) {
|
|
|
55 |
unsigned i;
|
|
|
56 |
for (i = 0; i < 10; i += 2) {
|
|
|
57 |
output[0+i] = (output[0+i] + in[0+i]);
|
|
|
58 |
output[1+i] = (output[1+i] + in[1+i]);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/* Find the difference of two numbers: output = in - output
|
|
|
63 |
* (note the order of the arguments!)
|
|
|
64 |
*/
|
|
|
65 |
static void fdifference(felem *output, felem *in) {
|
|
|
66 |
unsigned i;
|
|
|
67 |
for (i = 0; i < 10; ++i) {
|
|
|
68 |
output[i] = (in[i] - output[i]);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/* Multiply a number my a scalar: output = in * scalar */
|
|
|
73 |
static void fscalar_product(felem *output, felem *in, felem scalar) {
|
|
|
74 |
unsigned i;
|
|
|
75 |
for (i = 0; i < 10; ++i) {
|
|
|
76 |
output[i] = in[i] * scalar;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/* Multiply two numbers: output = in2 * in
|
|
|
81 |
*
|
|
|
82 |
* output must be distinct to both inputs. The inputs are reduced coefficient
|
|
|
83 |
* form, the output is not.
|
|
|
84 |
*/
|
|
|
85 |
static void fproduct(felem *output, felem *in2, felem *in) {
|
|
|
86 |
output[0] = in2[0] * in[0];
|
|
|
87 |
output[1] = in2[0] * in[1] +
|
|
|
88 |
in2[1] * in[0];
|
|
|
89 |
output[2] = 2 * in2[1] * in[1] +
|
|
|
90 |
in2[0] * in[2] +
|
|
|
91 |
in2[2] * in[0];
|
|
|
92 |
output[3] = in2[1] * in[2] +
|
|
|
93 |
in2[2] * in[1] +
|
|
|
94 |
in2[0] * in[3] +
|
|
|
95 |
in2[3] * in[0];
|
|
|
96 |
output[4] = in2[2] * in[2] +
|
|
|
97 |
2 * (in2[1] * in[3] +
|
|
|
98 |
in2[3] * in[1]) +
|
|
|
99 |
in2[0] * in[4] +
|
|
|
100 |
in2[4] * in[0];
|
|
|
101 |
output[5] = in2[2] * in[3] +
|
|
|
102 |
in2[3] * in[2] +
|
|
|
103 |
in2[1] * in[4] +
|
|
|
104 |
in2[4] * in[1] +
|
|
|
105 |
in2[0] * in[5] +
|
|
|
106 |
in2[5] * in[0];
|
|
|
107 |
output[6] = 2 * (in2[3] * in[3] +
|
|
|
108 |
in2[1] * in[5] +
|
|
|
109 |
in2[5] * in[1]) +
|
|
|
110 |
in2[2] * in[4] +
|
|
|
111 |
in2[4] * in[2] +
|
|
|
112 |
in2[0] * in[6] +
|
|
|
113 |
in2[6] * in[0];
|
|
|
114 |
output[7] = in2[3] * in[4] +
|
|
|
115 |
in2[4] * in[3] +
|
|
|
116 |
in2[2] * in[5] +
|
|
|
117 |
in2[5] * in[2] +
|
|
|
118 |
in2[1] * in[6] +
|
|
|
119 |
in2[6] * in[1] +
|
|
|
120 |
in2[0] * in[7] +
|
|
|
121 |
in2[7] * in[0];
|
|
|
122 |
output[8] = in2[4] * in[4] +
|
|
|
123 |
2 * (in2[3] * in[5] +
|
|
|
124 |
in2[5] * in[3] +
|
|
|
125 |
in2[1] * in[7] +
|
|
|
126 |
in2[7] * in[1]) +
|
|
|
127 |
in2[2] * in[6] +
|
|
|
128 |
in2[6] * in[2] +
|
|
|
129 |
in2[0] * in[8] +
|
|
|
130 |
in2[8] * in[0];
|
|
|
131 |
output[9] = in2[4] * in[5] +
|
|
|
132 |
in2[5] * in[4] +
|
|
|
133 |
in2[3] * in[6] +
|
|
|
134 |
in2[6] * in[3] +
|
|
|
135 |
in2[2] * in[7] +
|
|
|
136 |
in2[7] * in[2] +
|
|
|
137 |
in2[1] * in[8] +
|
|
|
138 |
in2[8] * in[1] +
|
|
|
139 |
in2[0] * in[9] +
|
|
|
140 |
in2[9] * in[0];
|
|
|
141 |
output[10] = 2 * (in2[5] * in[5] +
|
|
|
142 |
in2[3] * in[7] +
|
|
|
143 |
in2[7] * in[3] +
|
|
|
144 |
in2[1] * in[9] +
|
|
|
145 |
in2[9] * in[1]) +
|
|
|
146 |
in2[4] * in[6] +
|
|
|
147 |
in2[6] * in[4] +
|
|
|
148 |
in2[2] * in[8] +
|
|
|
149 |
in2[8] * in[2];
|
|
|
150 |
output[11] = in2[5] * in[6] +
|
|
|
151 |
in2[6] * in[5] +
|
|
|
152 |
in2[4] * in[7] +
|
|
|
153 |
in2[7] * in[4] +
|
|
|
154 |
in2[3] * in[8] +
|
|
|
155 |
in2[8] * in[3] +
|
|
|
156 |
in2[2] * in[9] +
|
|
|
157 |
in2[9] * in[2];
|
|
|
158 |
output[12] = in2[6] * in[6] +
|
|
|
159 |
2 * (in2[5] * in[7] +
|
|
|
160 |
in2[7] * in[5] +
|
|
|
161 |
in2[3] * in[9] +
|
|
|
162 |
in2[9] * in[3]) +
|
|
|
163 |
in2[4] * in[8] +
|
|
|
164 |
in2[8] * in[4];
|
|
|
165 |
output[13] = in2[6] * in[7] +
|
|
|
166 |
in2[7] * in[6] +
|
|
|
167 |
in2[5] * in[8] +
|
|
|
168 |
in2[8] * in[5] +
|
|
|
169 |
in2[4] * in[9] +
|
|
|
170 |
in2[9] * in[4];
|
|
|
171 |
output[14] = 2 * (in2[7] * in[7] +
|
|
|
172 |
in2[5] * in[9] +
|
|
|
173 |
in2[9] * in[5]) +
|
|
|
174 |
in2[6] * in[8] +
|
|
|
175 |
in2[8] * in[6];
|
|
|
176 |
output[15] = in2[7] * in[8] +
|
|
|
177 |
in2[8] * in[7] +
|
|
|
178 |
in2[6] * in[9] +
|
|
|
179 |
in2[9] * in[6];
|
|
|
180 |
output[16] = in2[8] * in[8] +
|
|
|
181 |
2 * (in2[7] * in[9] +
|
|
|
182 |
in2[9] * in[7]);
|
|
|
183 |
output[17] = in2[8] * in[9] +
|
|
|
184 |
in2[9] * in[8];
|
|
|
185 |
output[18] = 2 * in2[9] * in[9];
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/* Reduce a long form to a short form by taking the input mod 2^255 - 19. */
|
|
|
189 |
static void freduce_degree(felem *output) {
|
|
|
190 |
output[8] += 19 * output[18];
|
|
|
191 |
output[7] += 19 * output[17];
|
|
|
192 |
output[6] += 19 * output[16];
|
|
|
193 |
output[5] += 19 * output[15];
|
|
|
194 |
output[4] += 19 * output[14];
|
|
|
195 |
output[3] += 19 * output[13];
|
|
|
196 |
output[2] += 19 * output[12];
|
|
|
197 |
output[1] += 19 * output[11];
|
|
|
198 |
output[0] += 19 * output[10];
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/* Reduce all coefficients of the short form input to be -2**25 <= x <= 2**25
|
|
|
202 |
*/
|
|
|
203 |
static void freduce_coefficients(felem *output) {
|
|
|
204 |
unsigned i;
|
|
|
205 |
do {
|
|
|
206 |
output[10] = 0;
|
|
|
207 |
|
|
|
208 |
for (i = 0; i < 10; i += 2) {
|
|
|
209 |
felem over = output[i] / 0x2000000l;
|
|
|
210 |
felem over2 = (over + ((over >> 63) * 2) + 1) / 2;
|
|
|
211 |
output[i+1] += over2;
|
|
|
212 |
output[i] -= over2 * 0x4000000l;
|
|
|
213 |
|
|
|
214 |
over = output[i+1] / 0x2000000;
|
|
|
215 |
output[i+2] += over;
|
|
|
216 |
output[i+1] -= over * 0x2000000;
|
|
|
217 |
}
|
|
|
218 |
output[0] += 19 * output[10];
|
|
|
219 |
} while (output[10]);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/* A helpful wrapper around fproduct: output = in * in2.
|
|
|
223 |
*
|
|
|
224 |
* output must be distinct to both inputs. The output is reduced degree and
|
|
|
225 |
* reduced coefficient.
|
|
|
226 |
*/
|
|
|
227 |
static void
|
|
|
228 |
fmul(felem *output, felem *in, felem *in2) {
|
|
|
229 |
felem t[19];
|
|
|
230 |
fproduct(t, in, in2);
|
|
|
231 |
freduce_degree(t);
|
|
|
232 |
freduce_coefficients(t);
|
|
|
233 |
memcpy(output, t, sizeof(felem) * 10);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
static void fsquare_inner(felem *output, felem *in) {
|
|
|
237 |
felem tmp;
|
|
|
238 |
output[0] = in[0] * in[0];
|
|
|
239 |
output[1] = 2 * in[0] * in[1];
|
|
|
240 |
output[2] = 2 * (in[1] * in[1] +
|
|
|
241 |
in[0] * in[2]);
|
|
|
242 |
output[3] = 2 * (in[1] * in[2] +
|
|
|
243 |
in[0] * in[3]);
|
|
|
244 |
output[4] = in[2] * in[2] +
|
|
|
245 |
4 * in[1] * in[3] +
|
|
|
246 |
2 * in[0] * in[4];
|
|
|
247 |
output[5] = 2 * (in[2] * in[3] +
|
|
|
248 |
in[1] * in[4] +
|
|
|
249 |
in[0] * in[5]);
|
|
|
250 |
output[6] = 2 * (in[3] * in[3] +
|
|
|
251 |
in[2] * in[4] +
|
|
|
252 |
in[0] * in[6] +
|
|
|
253 |
2 * in[1] * in[5]);
|
|
|
254 |
output[7] = 2 * (in[3] * in[4] +
|
|
|
255 |
in[2] * in[5] +
|
|
|
256 |
in[1] * in[6] +
|
|
|
257 |
in[0] * in[7]);
|
|
|
258 |
tmp = in[1] * in[7] + in[3] * in[5];
|
|
|
259 |
output[8] = in[4] * in[4] +
|
|
|
260 |
2 * (in[2] * in[6] +
|
|
|
261 |
in[0] * in[8] +
|
|
|
262 |
2 * tmp);
|
|
|
263 |
output[9] = 2 * (in[4] * in[5] +
|
|
|
264 |
in[3] * in[6] +
|
|
|
265 |
in[2] * in[7] +
|
|
|
266 |
in[1] * in[8] +
|
|
|
267 |
in[0] * in[9]);
|
|
|
268 |
tmp = in[3] * in[7] + in[1] * in[9];
|
|
|
269 |
output[10] = 2 * (in[5] * in[5] +
|
|
|
270 |
in[4] * in[6] +
|
|
|
271 |
in[2] * in[8] +
|
|
|
272 |
2 * tmp);
|
|
|
273 |
output[11] = 2 * (in[5] * in[6] +
|
|
|
274 |
in[4] * in[7] +
|
|
|
275 |
in[3] * in[8] +
|
|
|
276 |
in[2] * in[9]);
|
|
|
277 |
output[12] = in[6] * in[6] +
|
|
|
278 |
2 * (in[4] * in[8] +
|
|
|
279 |
2 * (in[5] * in[7] +
|
|
|
280 |
in[3] * in[9]));
|
|
|
281 |
output[13] = 2 * (in[6] * in[7] +
|
|
|
282 |
in[5] * in[8] +
|
|
|
283 |
in[4] * in[9]);
|
|
|
284 |
output[14] = 2 * (in[7] * in[7] +
|
|
|
285 |
in[6] * in[8] +
|
|
|
286 |
2 * in[5] * in[9]);
|
|
|
287 |
output[15] = 2 * (in[7] * in[8] +
|
|
|
288 |
in[6] * in[9]);
|
|
|
289 |
output[16] = in[8] * in[8] +
|
|
|
290 |
4 * in[7] * in[9];
|
|
|
291 |
output[17] = 2 * in[8] * in[9];
|
|
|
292 |
output[18] = 2 * in[9] * in[9];
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
static void
|
|
|
296 |
fsquare(felem *output, felem *in) {
|
|
|
297 |
felem t[19];
|
|
|
298 |
fsquare_inner(t, in);
|
|
|
299 |
freduce_degree(t);
|
|
|
300 |
freduce_coefficients(t);
|
|
|
301 |
memcpy(output, t, sizeof(felem) * 10);
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
/* Take a little-endian, 32-byte number and expand it into polynomial form */
|
|
|
305 |
static void
|
|
|
306 |
fexpand(felem *output, uchar *input) {
|
|
|
307 |
#define F(n,start,shift,mask) \
|
|
|
308 |
output[n] = ((((felem) input[start + 0]) | \
|
|
|
309 |
((felem) input[start + 1]) << 8 | \
|
|
|
310 |
((felem) input[start + 2]) << 16 | \
|
|
|
311 |
((felem) input[start + 3]) << 24) >> shift) & mask;
|
|
|
312 |
F(0, 0, 0, 0x3ffffff);
|
|
|
313 |
F(1, 3, 2, 0x1ffffff);
|
|
|
314 |
F(2, 6, 3, 0x3ffffff);
|
|
|
315 |
F(3, 9, 5, 0x1ffffff);
|
|
|
316 |
F(4, 12, 6, 0x3ffffff);
|
|
|
317 |
F(5, 16, 0, 0x1ffffff);
|
|
|
318 |
F(6, 19, 1, 0x3ffffff);
|
|
|
319 |
F(7, 22, 3, 0x1ffffff);
|
|
|
320 |
F(8, 25, 4, 0x3ffffff);
|
|
|
321 |
F(9, 28, 6, 0x1ffffff);
|
|
|
322 |
#undef F
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
/* Take a fully reduced polynomial form number and contract it into a
|
|
|
326 |
* little-endian, 32-byte array
|
|
|
327 |
*/
|
|
|
328 |
static void
|
|
|
329 |
fcontract(uchar *output, felem *input) {
|
|
|
330 |
int i;
|
|
|
331 |
|
|
|
332 |
do {
|
|
|
333 |
for (i = 0; i < 9; ++i) {
|
|
|
334 |
if ((i & 1) == 1) {
|
|
|
335 |
while (input[i] < 0) {
|
|
|
336 |
input[i] += 0x2000000;
|
|
|
337 |
input[i + 1]--;
|
|
|
338 |
}
|
|
|
339 |
} else {
|
|
|
340 |
while (input[i] < 0) {
|
|
|
341 |
input[i] += 0x4000000;
|
|
|
342 |
input[i + 1]--;
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
}
|
|
|
346 |
while (input[9] < 0) {
|
|
|
347 |
input[9] += 0x2000000;
|
|
|
348 |
input[0] -= 19;
|
|
|
349 |
}
|
|
|
350 |
} while (input[0] < 0);
|
|
|
351 |
|
|
|
352 |
input[1] <<= 2;
|
|
|
353 |
input[2] <<= 3;
|
|
|
354 |
input[3] <<= 5;
|
|
|
355 |
input[4] <<= 6;
|
|
|
356 |
input[6] <<= 1;
|
|
|
357 |
input[7] <<= 3;
|
|
|
358 |
input[8] <<= 4;
|
|
|
359 |
input[9] <<= 6;
|
|
|
360 |
#define F(i, s) \
|
|
|
361 |
output[s+0] |= input[i] & 0xff; \
|
|
|
362 |
output[s+1] = (input[i] >> 8) & 0xff; \
|
|
|
363 |
output[s+2] = (input[i] >> 16) & 0xff; \
|
|
|
364 |
output[s+3] = (input[i] >> 24) & 0xff;
|
|
|
365 |
output[0] = 0;
|
|
|
366 |
output[16] = 0;
|
|
|
367 |
F(0,0);
|
|
|
368 |
F(1,3);
|
|
|
369 |
F(2,6);
|
|
|
370 |
F(3,9);
|
|
|
371 |
F(4,12);
|
|
|
372 |
F(5,16);
|
|
|
373 |
F(6,19);
|
|
|
374 |
F(7,22);
|
|
|
375 |
F(8,25);
|
|
|
376 |
F(9,28);
|
|
|
377 |
#undef F
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
/* Input: Q, Q', Q-Q'
|
|
|
381 |
* Output: 2Q, Q+Q'
|
|
|
382 |
*
|
|
|
383 |
* x2 z3: long form
|
|
|
384 |
* x3 z3: long form
|
|
|
385 |
* x z: short form, destroyed
|
|
|
386 |
* xprime zprime: short form, destroyed
|
|
|
387 |
* qmqp: short form, preserved
|
|
|
388 |
*/
|
|
|
389 |
static void fmonty(felem *x2, felem *z2, /* output 2Q */
|
|
|
390 |
felem *x3, felem *z3, /* output Q + Q' */
|
|
|
391 |
felem *x, felem *z, /* input Q */
|
|
|
392 |
felem *xprime, felem *zprime, /* input Q' */
|
|
|
393 |
felem *qmqp /* input Q - Q' */) {
|
|
|
394 |
felem origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19],
|
|
|
395 |
zzprime[19], zzzprime[19], xxxprime[19];
|
|
|
396 |
|
|
|
397 |
memcpy(origx, x, 10 * sizeof(felem));
|
|
|
398 |
fsum(x, z);
|
|
|
399 |
fdifference(z, origx); // does x - z
|
|
|
400 |
|
|
|
401 |
memcpy(origxprime, xprime, sizeof(felem) * 10);
|
|
|
402 |
fsum(xprime, zprime);
|
|
|
403 |
fdifference(zprime, origxprime);
|
|
|
404 |
fproduct(xxprime, xprime, z);
|
|
|
405 |
fproduct(zzprime, x, zprime);
|
|
|
406 |
freduce_degree(xxprime);
|
|
|
407 |
freduce_coefficients(xxprime);
|
|
|
408 |
freduce_degree(zzprime);
|
|
|
409 |
freduce_coefficients(zzprime);
|
|
|
410 |
memcpy(origxprime, xxprime, sizeof(felem) * 10);
|
|
|
411 |
fsum(xxprime, zzprime);
|
|
|
412 |
fdifference(zzprime, origxprime);
|
|
|
413 |
fsquare(xxxprime, xxprime);
|
|
|
414 |
fsquare(zzzprime, zzprime);
|
|
|
415 |
fproduct(zzprime, zzzprime, qmqp);
|
|
|
416 |
freduce_degree(zzprime);
|
|
|
417 |
freduce_coefficients(zzprime);
|
|
|
418 |
memcpy(x3, xxxprime, sizeof(felem) * 10);
|
|
|
419 |
memcpy(z3, zzprime, sizeof(felem) * 10);
|
|
|
420 |
|
|
|
421 |
fsquare(xx, x);
|
|
|
422 |
fsquare(zz, z);
|
|
|
423 |
fproduct(x2, xx, zz);
|
|
|
424 |
freduce_degree(x2);
|
|
|
425 |
freduce_coefficients(x2);
|
|
|
426 |
fdifference(zz, xx); // does zz = xx - zz
|
|
|
427 |
memset(zzz + 10, 0, sizeof(felem) * 9);
|
|
|
428 |
fscalar_product(zzz, zz, 121665);
|
|
|
429 |
freduce_degree(zzz);
|
|
|
430 |
freduce_coefficients(zzz);
|
|
|
431 |
fsum(zzz, xx);
|
|
|
432 |
fproduct(z2, zz, zzz);
|
|
|
433 |
freduce_degree(z2);
|
|
|
434 |
freduce_coefficients(z2);
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
/* Calculates nQ where Q is the x-coordinate of a point on the curve
|
|
|
438 |
*
|
|
|
439 |
* resultx/resultz: the x coordinate of the resulting curve point (short form)
|
|
|
440 |
* n: a little endian, 32-byte number
|
|
|
441 |
* q: a point of the curve (short form)
|
|
|
442 |
*/
|
|
|
443 |
static void
|
|
|
444 |
cmult(felem *resultx, felem *resultz, uchar *n, felem *q) {
|
|
|
445 |
felem a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0};
|
|
|
446 |
felem *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
|
|
|
447 |
felem e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1};
|
|
|
448 |
felem *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
|
|
|
449 |
|
|
|
450 |
unsigned i, j;
|
|
|
451 |
|
|
|
452 |
memcpy(nqpqx, q, sizeof(felem) * 10);
|
|
|
453 |
|
|
|
454 |
for (i = 0; i < 32; ++i) {
|
|
|
455 |
uchar byte = n[31 - i];
|
|
|
456 |
for (j = 0; j < 8; ++j) {
|
|
|
457 |
if (byte & 0x80) {
|
|
|
458 |
fmonty(nqpqx2, nqpqz2,
|
|
|
459 |
nqx2, nqz2,
|
|
|
460 |
nqpqx, nqpqz,
|
|
|
461 |
nqx, nqz,
|
|
|
462 |
q);
|
|
|
463 |
} else {
|
|
|
464 |
fmonty(nqx2, nqz2,
|
|
|
465 |
nqpqx2, nqpqz2,
|
|
|
466 |
nqx, nqz,
|
|
|
467 |
nqpqx, nqpqz,
|
|
|
468 |
q);
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
t = nqx;
|
|
|
472 |
nqx = nqx2;
|
|
|
473 |
nqx2 = t;
|
|
|
474 |
t = nqz;
|
|
|
475 |
nqz = nqz2;
|
|
|
476 |
nqz2 = t;
|
|
|
477 |
t = nqpqx;
|
|
|
478 |
nqpqx = nqpqx2;
|
|
|
479 |
nqpqx2 = t;
|
|
|
480 |
t = nqpqz;
|
|
|
481 |
nqpqz = nqpqz2;
|
|
|
482 |
nqpqz2 = t;
|
|
|
483 |
|
|
|
484 |
byte <<= 1;
|
|
|
485 |
}
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
memcpy(resultx, nqx, sizeof(felem) * 10);
|
|
|
489 |
memcpy(resultz, nqz, sizeof(felem) * 10);
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
// -----------------------------------------------------------------------------
|
|
|
493 |
// Shamelessly copied from djb's code
|
|
|
494 |
// -----------------------------------------------------------------------------
|
|
|
495 |
static void
|
|
|
496 |
crecip(felem *out, felem *z) {
|
|
|
497 |
felem z2[10];
|
|
|
498 |
felem z9[10];
|
|
|
499 |
felem z11[10];
|
|
|
500 |
felem z2_5_0[10];
|
|
|
501 |
felem z2_10_0[10];
|
|
|
502 |
felem z2_20_0[10];
|
|
|
503 |
felem z2_50_0[10];
|
|
|
504 |
felem z2_100_0[10];
|
|
|
505 |
felem t0[10];
|
|
|
506 |
felem t1[10];
|
|
|
507 |
int i;
|
|
|
508 |
|
|
|
509 |
/* 2 */ fsquare(z2,z);
|
|
|
510 |
/* 4 */ fsquare(t1,z2);
|
|
|
511 |
/* 8 */ fsquare(t0,t1);
|
|
|
512 |
/* 9 */ fmul(z9,t0,z);
|
|
|
513 |
/* 11 */ fmul(z11,z9,z2);
|
|
|
514 |
/* 22 */ fsquare(t0,z11);
|
|
|
515 |
/* 2^5 - 2^0 = 31 */ fmul(z2_5_0,t0,z9);
|
|
|
516 |
|
|
|
517 |
/* 2^6 - 2^1 */ fsquare(t0,z2_5_0);
|
|
|
518 |
/* 2^7 - 2^2 */ fsquare(t1,t0);
|
|
|
519 |
/* 2^8 - 2^3 */ fsquare(t0,t1);
|
|
|
520 |
/* 2^9 - 2^4 */ fsquare(t1,t0);
|
|
|
521 |
/* 2^10 - 2^5 */ fsquare(t0,t1);
|
|
|
522 |
/* 2^10 - 2^0 */ fmul(z2_10_0,t0,z2_5_0);
|
|
|
523 |
|
|
|
524 |
/* 2^11 - 2^1 */ fsquare(t0,z2_10_0);
|
|
|
525 |
/* 2^12 - 2^2 */ fsquare(t1,t0);
|
|
|
526 |
/* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
|
|
|
527 |
/* 2^20 - 2^0 */ fmul(z2_20_0,t1,z2_10_0);
|
|
|
528 |
|
|
|
529 |
/* 2^21 - 2^1 */ fsquare(t0,z2_20_0);
|
|
|
530 |
/* 2^22 - 2^2 */ fsquare(t1,t0);
|
|
|
531 |
/* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
|
|
|
532 |
/* 2^40 - 2^0 */ fmul(t0,t1,z2_20_0);
|
|
|
533 |
|
|
|
534 |
/* 2^41 - 2^1 */ fsquare(t1,t0);
|
|
|
535 |
/* 2^42 - 2^2 */ fsquare(t0,t1);
|
|
|
536 |
/* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
|
|
|
537 |
/* 2^50 - 2^0 */ fmul(z2_50_0,t0,z2_10_0);
|
|
|
538 |
|
|
|
539 |
/* 2^51 - 2^1 */ fsquare(t0,z2_50_0);
|
|
|
540 |
/* 2^52 - 2^2 */ fsquare(t1,t0);
|
|
|
541 |
/* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
|
|
|
542 |
/* 2^100 - 2^0 */ fmul(z2_100_0,t1,z2_50_0);
|
|
|
543 |
|
|
|
544 |
/* 2^101 - 2^1 */ fsquare(t1,z2_100_0);
|
|
|
545 |
/* 2^102 - 2^2 */ fsquare(t0,t1);
|
|
|
546 |
/* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
|
|
|
547 |
/* 2^200 - 2^0 */ fmul(t1,t0,z2_100_0);
|
|
|
548 |
|
|
|
549 |
/* 2^201 - 2^1 */ fsquare(t0,t1);
|
|
|
550 |
/* 2^202 - 2^2 */ fsquare(t1,t0);
|
|
|
551 |
/* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
|
|
|
552 |
/* 2^250 - 2^0 */ fmul(t0,t1,z2_50_0);
|
|
|
553 |
|
|
|
554 |
/* 2^251 - 2^1 */ fsquare(t1,t0);
|
|
|
555 |
/* 2^252 - 2^2 */ fsquare(t0,t1);
|
|
|
556 |
/* 2^253 - 2^3 */ fsquare(t1,t0);
|
|
|
557 |
/* 2^254 - 2^4 */ fsquare(t0,t1);
|
|
|
558 |
/* 2^255 - 2^5 */ fsquare(t1,t0);
|
|
|
559 |
/* 2^255 - 21 */ fmul(out,t1,z11);
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
void
|
|
|
563 |
curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]) {
|
|
|
564 |
felem bp[10], x[10], z[10], zmone[10];
|
|
|
565 |
fexpand(bp, basepoint);
|
|
|
566 |
cmult(x, z, secret, bp);
|
|
|
567 |
crecip(zmone, z);
|
|
|
568 |
fmul(z, x, zmone);
|
|
|
569 |
fcontract(mypublic, z);
|
|
|
570 |
}
|