33#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
34# pragma GCC diagnostic ignored "-Wunused-function"
37#if CRYPTOPP_MSC_VERSION
38# pragma warning(disable: 4244)
42extern const char DONNA32_FNAME[] = __FILE__;
44ANONYMOUS_NAMESPACE_BEGIN
48#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
54ANONYMOUS_NAMESPACE_END
56#if defined(CRYPTOPP_CURVE25519_32BIT)
60ANONYMOUS_NAMESPACE_BEGIN
63using CryptoPP::word32;
64using CryptoPP::GetWord;
65using CryptoPP::PutWord;
66using CryptoPP::LITTLE_ENDIAN_ORDER;
68inline word32 U8TO32_LE(
const byte* p)
73inline void U32TO8_LE(
byte* p,
word32 w)
78ANONYMOUS_NAMESPACE_END
82NAMESPACE_BEGIN(X25519)
83ANONYMOUS_NAMESPACE_BEGIN
86using CryptoPP::word32;
87using CryptoPP::sword32;
88using CryptoPP::word64;
89using CryptoPP::sword64;
91using CryptoPP::GetBlock;
92using CryptoPP::LittleEndian;
95using namespace CryptoPP::Donna::Arch32;
99curve25519_copy(bignum25519 out,
const bignum25519 in) {
100 out[0] = in[0]; out[1] = in[1];
101 out[2] = in[2]; out[3] = in[3];
102 out[4] = in[4]; out[5] = in[5];
103 out[6] = in[6]; out[7] = in[7];
104 out[8] = in[8]; out[9] = in[9];
109curve25519_add(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
110 out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
111 out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
112 out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
113 out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
114 out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
119curve25519_sub(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
121 out[0] = 0x7ffffda + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
122 out[1] = 0x3fffffe + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
123 out[2] = 0x7fffffe + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
124 out[3] = 0x3fffffe + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
125 out[4] = 0x7fffffe + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
126 out[5] = 0x3fffffe + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
127 out[6] = 0x7fffffe + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
128 out[7] = 0x3fffffe + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
129 out[8] = 0x7fffffe + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
130 out[9] = 0x3fffffe + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
136curve25519_scalar_product(bignum25519 out,
const bignum25519 in,
const word32 scalar) {
139 a = mul32x32_64(in[0], scalar); out[0] = (
word32)a & reduce_mask_26; c = (
word32)(a >> 26);
140 a = mul32x32_64(in[1], scalar) + c; out[1] = (
word32)a & reduce_mask_25; c = (
word32)(a >> 25);
141 a = mul32x32_64(in[2], scalar) + c; out[2] = (
word32)a & reduce_mask_26; c = (
word32)(a >> 26);
142 a = mul32x32_64(in[3], scalar) + c; out[3] = (
word32)a & reduce_mask_25; c = (
word32)(a >> 25);
143 a = mul32x32_64(in[4], scalar) + c; out[4] = (
word32)a & reduce_mask_26; c = (
word32)(a >> 26);
144 a = mul32x32_64(in[5], scalar) + c; out[5] = (
word32)a & reduce_mask_25; c = (
word32)(a >> 25);
145 a = mul32x32_64(in[6], scalar) + c; out[6] = (
word32)a & reduce_mask_26; c = (
word32)(a >> 26);
146 a = mul32x32_64(in[7], scalar) + c; out[7] = (
word32)a & reduce_mask_25; c = (
word32)(a >> 25);
147 a = mul32x32_64(in[8], scalar) + c; out[8] = (
word32)a & reduce_mask_26; c = (
word32)(a >> 26);
148 a = mul32x32_64(in[9], scalar) + c; out[9] = (
word32)a & reduce_mask_25; c = (
word32)(a >> 25);
154curve25519_mul(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
155 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
156 word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
157 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
160 r0 = b[0]; r1 = b[1]; r2 = b[2]; r3 = b[3]; r4 = b[4];
161 r5 = b[5]; r6 = b[6]; r7 = b[7]; r8 = b[8]; r9 = b[9];
163 s0 = a[0]; s1 = a[1]; s2 = a[2]; s3 = a[3]; s4 = a[4];
164 s5 = a[5]; s6 = a[6]; s7 = a[7]; s8 = a[8]; s9 = a[9];
166 m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
167 m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
168 m5 = mul32x32_64(r0, s5) + mul32x32_64(r1, s4) + mul32x32_64(r2, s3) + mul32x32_64(r3, s2) + mul32x32_64(r4, s1) + mul32x32_64(r5, s0);
169 m7 = mul32x32_64(r0, s7) + mul32x32_64(r1, s6) + mul32x32_64(r2, s5) + mul32x32_64(r3, s4) + mul32x32_64(r4, s3) + mul32x32_64(r5, s2) + mul32x32_64(r6, s1) + mul32x32_64(r7, s0);
170 m9 = mul32x32_64(r0, s9) + mul32x32_64(r1, s8) + mul32x32_64(r2, s7) + mul32x32_64(r3, s6) + mul32x32_64(r4, s5) + mul32x32_64(r5, s4) + mul32x32_64(r6, s3) + mul32x32_64(r7, s2) + mul32x32_64(r8, s1) + mul32x32_64(r9, s0);
172 r1 *= 2; r3 *= 2; r5 *= 2; r7 *= 2;
174 m0 = mul32x32_64(r0, s0);
175 m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
176 m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
177 m6 = mul32x32_64(r0, s6) + mul32x32_64(r1, s5) + mul32x32_64(r2, s4) + mul32x32_64(r3, s3) + mul32x32_64(r4, s2) + mul32x32_64(r5, s1) + mul32x32_64(r6, s0);
178 m8 = mul32x32_64(r0, s8) + mul32x32_64(r1, s7) + mul32x32_64(r2, s6) + mul32x32_64(r3, s5) + mul32x32_64(r4, s4) + mul32x32_64(r5, s3) + mul32x32_64(r6, s2) + mul32x32_64(r7, s1) + mul32x32_64(r8, s0);
188 m1 += (mul32x32_64(r9, s2) + mul32x32_64(r8, s3) + mul32x32_64(r7, s4) + mul32x32_64(r6, s5) + mul32x32_64(r5, s6) + mul32x32_64(r4, s7) + mul32x32_64(r3, s8) + mul32x32_64(r2, s9));
189 m3 += (mul32x32_64(r9, s4) + mul32x32_64(r8, s5) + mul32x32_64(r7, s6) + mul32x32_64(r6, s7) + mul32x32_64(r5, s8) + mul32x32_64(r4, s9));
190 m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
191 m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
193 r3 *= 2; r5 *= 2; r7 *= 2; r9 *= 2;
195 m0 += (mul32x32_64(r9, s1) + mul32x32_64(r8, s2) + mul32x32_64(r7, s3) + mul32x32_64(r6, s4) + mul32x32_64(r5, s5) + mul32x32_64(r4, s6) + mul32x32_64(r3, s7) + mul32x32_64(r2, s8) + mul32x32_64(r1, s9));
196 m2 += (mul32x32_64(r9, s3) + mul32x32_64(r8, s4) + mul32x32_64(r7, s5) + mul32x32_64(r6, s6) + mul32x32_64(r5, s7) + mul32x32_64(r4, s8) + mul32x32_64(r3, s9));
197 m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
198 m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
199 m8 += (mul32x32_64(r9, s9));
201 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
202 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
203 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
204 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
205 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
206 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
207 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
208 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
209 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
210 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
211 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
214 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
215 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
220curve25519_square(bignum25519 out,
const bignum25519 in) {
221 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
223 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
226 r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
227 r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
229 m0 = mul32x32_64(r0, r0);
231 m1 = mul32x32_64(r0, r1);
232 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
234 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
235 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
237 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
238 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
240 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
241 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
242 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
244 d6 = r6 * 19; d7 = r7 * 2 * 19;
245 d8 = r8 * 19; d9 = r9 * 2 * 19;
247 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
248 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
249 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
250 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
251 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
252 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
253 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
254 m7 += (mul32x32_64(d9, r8 ));
255 m8 += (mul32x32_64(d9, r9 ));
257 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
258 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
259 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
260 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
261 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
262 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
263 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
264 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
265 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
266 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
267 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
270 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
271 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
276curve25519_square_times(bignum25519 out,
const bignum25519 in,
int count) {
277 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
279 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
282 r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
283 r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
286 m0 = mul32x32_64(r0, r0);
288 m1 = mul32x32_64(r0, r1);
289 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
291 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
292 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
294 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
295 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
297 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
298 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
299 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
301 d6 = r6 * 19; d7 = r7 * 2 * 19;
302 d8 = r8 * 19; d9 = r9 * 2 * 19;
304 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
305 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
306 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
307 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
308 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
309 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
310 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
311 m7 += (mul32x32_64(d9, r8 ));
312 m8 += (mul32x32_64(d9, r9 ));
314 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
315 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
316 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
317 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
318 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
319 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
320 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
321 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
322 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
323 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
324 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
328 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
329 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
334curve25519_expand(bignum25519 out,
const byte in[32]) {
335 word32 x0,x1,x2,x3,x4,x5,x6,x7;
337 block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
339 out[0] = ( x0 ) & reduce_mask_26;
340 out[1] = ((((
word64)x1 << 32) | x0) >> 26) & reduce_mask_25;
341 out[2] = ((((
word64)x2 << 32) | x1) >> 19) & reduce_mask_26;
342 out[3] = ((((
word64)x3 << 32) | x2) >> 13) & reduce_mask_25;
343 out[4] = (( x3) >> 6) & reduce_mask_26;
344 out[5] = ( x4 ) & reduce_mask_25;
345 out[6] = ((((
word64)x5 << 32) | x4) >> 25) & reduce_mask_26;
346 out[7] = ((((
word64)x6 << 32) | x5) >> 19) & reduce_mask_25;
347 out[8] = ((((
word64)x7 << 32) | x6) >> 12) & reduce_mask_26;
348 out[9] = (( x7) >> 6) & reduce_mask_25;
353curve25519_contract(
byte out[32],
const bignum25519 in) {
355 curve25519_copy(f, in);
357 #define carry_pass() \
358 f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
359 f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
360 f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
361 f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
362 f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
363 f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
364 f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
365 f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
366 f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
368 #define carry_pass_full() \
370 f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
372 #define carry_pass_final() \
374 f[9] &= reduce_mask_25;
385 f[0] += (1 << 26) - 19;
386 f[1] += (1 << 25) - 1;
387 f[2] += (1 << 26) - 1;
388 f[3] += (1 << 25) - 1;
389 f[4] += (1 << 26) - 1;
390 f[5] += (1 << 25) - 1;
391 f[6] += (1 << 26) - 1;
392 f[7] += (1 << 25) - 1;
393 f[8] += (1 << 26) - 1;
394 f[9] += (1 << 25) - 1;
413 out[s+0] |= (byte)( f[i] & 0xff); \
414 out[s+1] = (byte)((f[i] >> 8) & 0xff); \
415 out[s+2] = (byte)((f[i] >> 16) & 0xff); \
416 out[s+3] = (byte)((f[i] >> 24) & 0xff);
418 out[0] = out[16] = 0;
428curve25519_swap_conditional(bignum25519 x, bignum25519 qpx,
word32 iswap) {
430 word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
432 x0 = swap & (x[0] ^ qpx[0]); x[0] ^= x0; qpx[0] ^= x0;
433 x1 = swap & (x[1] ^ qpx[1]); x[1] ^= x1; qpx[1] ^= x1;
434 x2 = swap & (x[2] ^ qpx[2]); x[2] ^= x2; qpx[2] ^= x2;
435 x3 = swap & (x[3] ^ qpx[3]); x[3] ^= x3; qpx[3] ^= x3;
436 x4 = swap & (x[4] ^ qpx[4]); x[4] ^= x4; qpx[4] ^= x4;
437 x5 = swap & (x[5] ^ qpx[5]); x[5] ^= x5; qpx[5] ^= x5;
438 x6 = swap & (x[6] ^ qpx[6]); x[6] ^= x6; qpx[6] ^= x6;
439 x7 = swap & (x[7] ^ qpx[7]); x[7] ^= x7; qpx[7] ^= x7;
440 x8 = swap & (x[8] ^ qpx[8]); x[8] ^= x8; qpx[8] ^= x8;
441 x9 = swap & (x[9] ^ qpx[9]); x[9] ^= x9; qpx[9] ^= x9;
449curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
450 ALIGN(ALIGN_SPEC) bignum25519 t0,c;
453 curve25519_square_times(t0, b, 5);
454 curve25519_mul(b, t0, b);
455 curve25519_square_times(t0, b, 10);
456 curve25519_mul(c, t0, b);
457 curve25519_square_times(t0, c, 20);
458 curve25519_mul(t0, t0, c);
459 curve25519_square_times(t0, t0, 10);
460 curve25519_mul(b, t0, b);
461 curve25519_square_times(t0, b, 50);
462 curve25519_mul(c, t0, b);
463 curve25519_square_times(t0, c, 100);
464 curve25519_mul(t0, t0, c);
465 curve25519_square_times(t0, t0, 50);
466 curve25519_mul(b, t0, b);
473curve25519_recip(bignum25519 out,
const bignum25519 z) {
474 ALIGN(ALIGN_SPEC) bignum25519 a, t0, b;
476 curve25519_square(a, z);
477 curve25519_square_times(t0, a, 2);
478 curve25519_mul(b, t0, z);
479 curve25519_mul(a, b, a);
480 curve25519_square(t0, a);
481 curve25519_mul(b, t0, b);
482 curve25519_pow_two5mtwo0_two250mtwo0(b);
483 curve25519_square_times(b, b, 5);
484 curve25519_mul(out, b, a);
487ANONYMOUS_NAMESPACE_END
495NAMESPACE_BEGIN(Donna)
496NAMESPACE_BEGIN(Ed25519)
497ANONYMOUS_NAMESPACE_BEGIN
500using CryptoPP::word32;
501using CryptoPP::sword32;
502using CryptoPP::word64;
503using CryptoPP::sword64;
505using CryptoPP::GetBlock;
506using CryptoPP::LittleEndian;
508using CryptoPP::SHA512;
511using namespace CryptoPP::Donna::Arch32;
515curve25519_copy(bignum25519 out,
const bignum25519 in) {
516 out[0] = in[0]; out[1] = in[1];
517 out[2] = in[2]; out[3] = in[3];
518 out[4] = in[4]; out[5] = in[5];
519 out[6] = in[6]; out[7] = in[7];
520 out[8] = in[8]; out[9] = in[9];
525curve25519_add(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
526 out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
527 out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
528 out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
529 out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
530 out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
534curve25519_add_after_basic(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
536 out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
537 out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
538 out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
539 out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
540 out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
541 out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
542 out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
543 out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
544 out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
545 out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
550curve25519_add_reduce(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
552 out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
553 out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
554 out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
555 out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
556 out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
557 out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
558 out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
559 out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
560 out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
561 out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
567curve25519_sub(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
569 out[0] = twoP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
570 out[1] = twoP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
571 out[2] = twoP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
572 out[3] = twoP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
573 out[4] = twoP2468 + a[4] - b[4] + c;
574 out[5] = twoP13579 + a[5] - b[5] ;
575 out[6] = twoP2468 + a[6] - b[6] ;
576 out[7] = twoP13579 + a[7] - b[7] ;
577 out[8] = twoP2468 + a[8] - b[8] ;
578 out[9] = twoP13579 + a[9] - b[9] ;
583curve25519_sub_after_basic(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
585 out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
586 out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
587 out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
588 out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
589 out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
590 out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
591 out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
592 out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
593 out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
594 out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
599curve25519_sub_reduce(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
601 out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
602 out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
603 out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
604 out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
605 out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
606 out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
607 out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
608 out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
609 out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
610 out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
616curve25519_neg(bignum25519 out,
const bignum25519 a) {
618 out[0] = twoP0 - a[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
619 out[1] = twoP13579 - a[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
620 out[2] = twoP2468 - a[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
621 out[3] = twoP13579 - a[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
622 out[4] = twoP2468 - a[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
623 out[5] = twoP13579 - a[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
624 out[6] = twoP2468 - a[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
625 out[7] = twoP13579 - a[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
626 out[8] = twoP2468 - a[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
627 out[9] = twoP13579 - a[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
633curve25519_mul(bignum25519 out,
const bignum25519 a,
const bignum25519 b) {
634 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
635 word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
636 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
639 r0 = b[0]; r1 = b[1];
640 r2 = b[2]; r3 = b[3];
641 r4 = b[4]; r5 = b[5];
642 r6 = b[6]; r7 = b[7];
643 r8 = b[8]; r9 = b[9];
645 s0 = a[0]; s1 = a[1];
646 s2 = a[2]; s3 = a[3];
647 s4 = a[4]; s5 = a[5];
648 s6 = a[6]; s7 = a[7];
649 s8 = a[8]; s9 = a[9];
651 m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
652 m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
653 m5 = mul32x32_64(r0, s5) + mul32x32_64(r1, s4) + mul32x32_64(r2, s3) + mul32x32_64(r3, s2) + mul32x32_64(r4, s1) + mul32x32_64(r5, s0);
654 m7 = mul32x32_64(r0, s7) + mul32x32_64(r1, s6) + mul32x32_64(r2, s5) + mul32x32_64(r3, s4) + mul32x32_64(r4, s3) + mul32x32_64(r5, s2) + mul32x32_64(r6, s1) + mul32x32_64(r7, s0);
655 m9 = mul32x32_64(r0, s9) + mul32x32_64(r1, s8) + mul32x32_64(r2, s7) + mul32x32_64(r3, s6) + mul32x32_64(r4, s5) + mul32x32_64(r5, s4) + mul32x32_64(r6, s3) + mul32x32_64(r7, s2) + mul32x32_64(r8, s1) + mul32x32_64(r9, s0);
660 m0 = mul32x32_64(r0, s0);
661 m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
662 m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
663 m6 = mul32x32_64(r0, s6) + mul32x32_64(r1, s5) + mul32x32_64(r2, s4) + mul32x32_64(r3, s3) + mul32x32_64(r4, s2) + mul32x32_64(r5, s1) + mul32x32_64(r6, s0);
664 m8 = mul32x32_64(r0, s8) + mul32x32_64(r1, s7) + mul32x32_64(r2, s6) + mul32x32_64(r3, s5) + mul32x32_64(r4, s4) + mul32x32_64(r5, s3) + mul32x32_64(r6, s2) + mul32x32_64(r7, s1) + mul32x32_64(r8, s0);
674 m1 += (mul32x32_64(r9, s2) + mul32x32_64(r8, s3) + mul32x32_64(r7, s4) + mul32x32_64(r6, s5) + mul32x32_64(r5, s6) + mul32x32_64(r4, s7) + mul32x32_64(r3, s8) + mul32x32_64(r2, s9));
675 m3 += (mul32x32_64(r9, s4) + mul32x32_64(r8, s5) + mul32x32_64(r7, s6) + mul32x32_64(r6, s7) + mul32x32_64(r5, s8) + mul32x32_64(r4, s9));
676 m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
677 m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
682 m0 += (mul32x32_64(r9, s1) + mul32x32_64(r8, s2) + mul32x32_64(r7, s3) + mul32x32_64(r6, s4) + mul32x32_64(r5, s5) + mul32x32_64(r4, s6) + mul32x32_64(r3, s7) + mul32x32_64(r2, s8) + mul32x32_64(r1, s9));
683 m2 += (mul32x32_64(r9, s3) + mul32x32_64(r8, s4) + mul32x32_64(r7, s5) + mul32x32_64(r6, s6) + mul32x32_64(r5, s7) + mul32x32_64(r4, s8) + mul32x32_64(r3, s9));
684 m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
685 m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
686 m8 += (mul32x32_64(r9, s9));
688 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
689 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
690 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
691 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
692 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
693 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
694 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
695 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
696 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
697 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
698 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
701 out[0] = r0; out[1] = r1;
702 out[2] = r2; out[3] = r3;
703 out[4] = r4; out[5] = r5;
704 out[6] = r6; out[7] = r7;
705 out[8] = r8; out[9] = r9;
710curve25519_square(bignum25519 out,
const bignum25519 in) {
711 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
713 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
716 r0 = in[0]; r1 = in[1];
717 r2 = in[2]; r3 = in[3];
718 r4 = in[4]; r5 = in[5];
719 r6 = in[6]; r7 = in[7];
720 r8 = in[8]; r9 = in[9];
722 m0 = mul32x32_64(r0, r0);
724 m1 = mul32x32_64(r0, r1);
725 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
727 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
728 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
730 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
731 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
733 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
734 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
735 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
742 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
743 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
744 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
745 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
746 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
747 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
748 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
749 m7 += (mul32x32_64(d9, r8 ));
750 m8 += (mul32x32_64(d9, r9 ));
752 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
753 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
754 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
755 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
756 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
757 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
758 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
759 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
760 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
761 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
762 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
765 out[0] = r0; out[1] = r1;
766 out[2] = r2; out[3] = r3;
767 out[4] = r4; out[5] = r5;
768 out[6] = r6; out[7] = r7;
769 out[8] = r8; out[9] = r9;
774curve25519_square_times(bignum25519 out,
const bignum25519 in,
int count) {
775 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
777 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
779 r0 = in[0]; r1 = in[1];
780 r2 = in[2]; r3 = in[3];
781 r4 = in[4]; r5 = in[5];
782 r6 = in[6]; r7 = in[7];
783 r8 = in[8]; r9 = in[9];
786 m0 = mul32x32_64(r0, r0);
788 m1 = mul32x32_64(r0, r1);
789 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
791 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
792 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
794 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
795 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
797 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
798 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
799 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
806 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
807 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
808 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
809 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
810 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
811 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
812 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
813 m7 += (mul32x32_64(d9, r8 ));
814 m8 += (mul32x32_64(d9, r9 ));
816 r0 = (
word32)m0 & reduce_mask_26; c = (m0 >> 26);
817 m1 += c; r1 = (
word32)m1 & reduce_mask_25; c = (m1 >> 25);
818 m2 += c; r2 = (
word32)m2 & reduce_mask_26; c = (m2 >> 26);
819 m3 += c; r3 = (
word32)m3 & reduce_mask_25; c = (m3 >> 25);
820 m4 += c; r4 = (
word32)m4 & reduce_mask_26; c = (m4 >> 26);
821 m5 += c; r5 = (
word32)m5 & reduce_mask_25; c = (m5 >> 25);
822 m6 += c; r6 = (
word32)m6 & reduce_mask_26; c = (m6 >> 26);
823 m7 += c; r7 = (
word32)m7 & reduce_mask_25; c = (m7 >> 25);
824 m8 += c; r8 = (
word32)m8 & reduce_mask_26; c = (m8 >> 26);
825 m9 += c; r9 = (
word32)m9 & reduce_mask_25; p = (
word32)(m9 >> 25);
826 m0 = r0 + mul32x32_64(p,19); r0 = (
word32)m0 & reduce_mask_26; p = (
word32)(m0 >> 26);
830 out[0] = r0; out[1] = r1;
831 out[2] = r2; out[3] = r3;
832 out[4] = r4; out[5] = r5;
833 out[6] = r6; out[7] = r7;
834 out[8] = r8; out[9] = r9;
839curve25519_expand(bignum25519 out,
const byte in[32]) {
840 word32 x0,x1,x2,x3,x4,x5,x6,x7;
842 block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
844 out[0] = ( x0 ) & 0x3ffffff;
845 out[1] = ((((
word64)x1 << 32) | x0) >> 26) & 0x1ffffff;
846 out[2] = ((((
word64)x2 << 32) | x1) >> 19) & 0x3ffffff;
847 out[3] = ((((
word64)x3 << 32) | x2) >> 13) & 0x1ffffff;
848 out[4] = (( x3) >> 6) & 0x3ffffff;
849 out[5] = ( x4 ) & 0x1ffffff;
850 out[6] = ((((
word64)x5 << 32) | x4) >> 25) & 0x3ffffff;
851 out[7] = ((((
word64)x6 << 32) | x5) >> 19) & 0x1ffffff;
852 out[8] = ((((
word64)x7 << 32) | x6) >> 12) & 0x3ffffff;
853 out[9] = (( x7) >> 6) & 0x1ffffff;
860curve25519_contract(
byte out[32],
const bignum25519 in) {
862 curve25519_copy(f, in);
864 #define carry_pass() \
865 f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
866 f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
867 f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
868 f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
869 f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
870 f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
871 f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
872 f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
873 f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
875 #define carry_pass_full() \
877 f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
879 #define carry_pass_final() \
881 f[9] &= reduce_mask_25;
892 f[0] += (reduce_mask_26 + 1) - 19;
893 f[1] += (reduce_mask_25 + 1) - 1;
894 f[2] += (reduce_mask_26 + 1) - 1;
895 f[3] += (reduce_mask_25 + 1) - 1;
896 f[4] += (reduce_mask_26 + 1) - 1;
897 f[5] += (reduce_mask_25 + 1) - 1;
898 f[6] += (reduce_mask_26 + 1) - 1;
899 f[7] += (reduce_mask_25 + 1) - 1;
900 f[8] += (reduce_mask_26 + 1) - 1;
901 f[9] += (reduce_mask_25 + 1) - 1;
910 f[1] <<= 2; f[2] <<= 3;
911 f[3] <<= 5; f[4] <<= 6;
912 f[6] <<= 1; f[7] <<= 3;
913 f[8] <<= 4; f[9] <<= 6;
916 out[s+0] |= (byte)( f[i] & 0xff); \
917 out[s+1] = (byte)((f[i] >> 8) & 0xff); \
918 out[s+2] = (byte)((f[i] >> 16) & 0xff); \
919 out[s+3] = (byte)((f[i] >> 24) & 0xff);
921 out[0] = out[16] = 0;
932curve25519_move_conditional_bytes(
byte out[96],
const byte in[96],
word32 flag)
937#if defined(__GNUC__) && defined(__i686__) && 0
940 const word32* inl =
reinterpret_cast<const word32*
>(in);
943 __asm__ __volatile__ (
945 "cmpl $0, %[flag] ;\n"
946 "movl %[iter], %%ecx ;\n"
948 " movl (%[idx],%[out]), %[val] ;\n"
949 " cmovnzl (%[idx],%[in]), %[val] ;\n"
950 " movl %[val], (%[idx],%[out]) ;\n"
951 " leal 4(%[idx]), %[idx] ;\n"
953 : [out]
"+S" (outl), [in]
"+D" (inl),
954 [idx]
"+b" (idx), [val]
"=r" (val)
955 : [flag]
"g" (flag), [iter]
"I" (iter)
956 :
"ecx",
"memory",
"cc"
959 const word32 nb = flag - 1, b = ~nb;
962 outl[0] = (outl[0] & nb) | (inl[0] & b);
963 outl[1] = (outl[1] & nb) | (inl[1] & b);
964 outl[2] = (outl[2] & nb) | (inl[2] & b);
965 outl[3] = (outl[3] & nb) | (inl[3] & b);
966 outl[4] = (outl[4] & nb) | (inl[4] & b);
967 outl[5] = (outl[5] & nb) | (inl[5] & b);
968 outl[6] = (outl[6] & nb) | (inl[6] & b);
969 outl[7] = (outl[7] & nb) | (inl[7] & b);
970 outl[8] = (outl[8] & nb) | (inl[8] & b);
971 outl[9] = (outl[9] & nb) | (inl[9] & b);
972 outl[10] = (outl[10] & nb) | (inl[10] & b);
973 outl[11] = (outl[11] & nb) | (inl[11] & b);
974 outl[12] = (outl[12] & nb) | (inl[12] & b);
975 outl[13] = (outl[13] & nb) | (inl[13] & b);
976 outl[14] = (outl[14] & nb) | (inl[14] & b);
977 outl[15] = (outl[15] & nb) | (inl[15] & b);
978 outl[16] = (outl[16] & nb) | (inl[16] & b);
979 outl[17] = (outl[17] & nb) | (inl[17] & b);
980 outl[18] = (outl[18] & nb) | (inl[18] & b);
981 outl[19] = (outl[19] & nb) | (inl[19] & b);
982 outl[20] = (outl[20] & nb) | (inl[20] & b);
983 outl[21] = (outl[21] & nb) | (inl[21] & b);
984 outl[22] = (outl[22] & nb) | (inl[22] & b);
985 outl[23] = (outl[23] & nb) | (inl[23] & b);
991curve25519_swap_conditional(bignum25519 a, bignum25519 b,
word32 iswap) {
993 word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
995 x0 = swap & (a[0] ^ b[0]); a[0] ^= x0; b[0] ^= x0;
996 x1 = swap & (a[1] ^ b[1]); a[1] ^= x1; b[1] ^= x1;
997 x2 = swap & (a[2] ^ b[2]); a[2] ^= x2; b[2] ^= x2;
998 x3 = swap & (a[3] ^ b[3]); a[3] ^= x3; b[3] ^= x3;
999 x4 = swap & (a[4] ^ b[4]); a[4] ^= x4; b[4] ^= x4;
1000 x5 = swap & (a[5] ^ b[5]); a[5] ^= x5; b[5] ^= x5;
1001 x6 = swap & (a[6] ^ b[6]); a[6] ^= x6; b[6] ^= x6;
1002 x7 = swap & (a[7] ^ b[7]); a[7] ^= x7; b[7] ^= x7;
1003 x8 = swap & (a[8] ^ b[8]); a[8] ^= x8; b[8] ^= x8;
1004 x9 = swap & (a[9] ^ b[9]); a[9] ^= x9; b[9] ^= x9;
1012curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
1013 ALIGN(ALIGN_SPEC) bignum25519 t0,c;
1016 curve25519_square_times(t0, b, 5);
1017 curve25519_mul(b, t0, b);
1018 curve25519_square_times(t0, b, 10);
1019 curve25519_mul(c, t0, b);
1020 curve25519_square_times(t0, c, 20);
1021 curve25519_mul(t0, t0, c);
1022 curve25519_square_times(t0, t0, 10);
1023 curve25519_mul(b, t0, b);
1024 curve25519_square_times(t0, b, 50);
1025 curve25519_mul(c, t0, b);
1026 curve25519_square_times(t0, c, 100);
1027 curve25519_mul(t0, t0, c);
1028 curve25519_square_times(t0, t0, 50);
1029 curve25519_mul(b, t0, b);
1036curve25519_recip(bignum25519 out,
const bignum25519 z) {
1037 ALIGN(ALIGN_SPEC) bignum25519 a,t0,b;
1039 curve25519_square_times(a, z, 1);
1040 curve25519_square_times(t0, a, 2);
1041 curve25519_mul(b, t0, z);
1042 curve25519_mul(a, b, a);
1043 curve25519_square_times(t0, a, 1);
1044 curve25519_mul(b, t0, b);
1045 curve25519_pow_two5mtwo0_two250mtwo0(b);
1046 curve25519_square_times(b, b, 5);
1047 curve25519_mul(out, b, a);
1054curve25519_pow_two252m3(bignum25519 two252m3,
const bignum25519 z) {
1055 ALIGN(ALIGN_SPEC) bignum25519 b,c,t0;
1057 curve25519_square_times(c, z, 1);
1058 curve25519_square_times(t0, c, 2);
1059 curve25519_mul(b, t0, z);
1060 curve25519_mul(c, b, c);
1061 curve25519_square_times(t0, c, 1);
1062 curve25519_mul(b, t0, b);
1063 curve25519_pow_two5mtwo0_two250mtwo0(b);
1064 curve25519_square_times(b, b, 2);
1065 curve25519_mul(two252m3, b, z);
1069ed25519_hash(
byte *hash,
const byte *in,
size_t inlen) {
1070 SHA512().CalculateDigest(hash, in, inlen);
1074ed25519_extsk(hash_512bits extsk,
const byte sk[32]) {
1075 ed25519_hash(extsk, sk, 32);
1085 while (stream.read((
char*)block.begin(), block.size()))
1086 hash.
Update(block, block.size());
1088 std::streamsize rem = stream.gcount();
1090 hash.
Update(block, (
size_t)rem);
1096ed25519_hram(hash_512bits hram,
const byte RS[64],
const byte pk[32],
const byte *m,
size_t mlen) {
1098 hash.Update(RS, 32);
1099 hash.Update(pk, 32);
1100 hash.Update(m, mlen);
1105ed25519_hram(hash_512bits hram,
const byte RS[64],
const byte pk[32], std::istream& stream) {
1107 hash.Update(RS, 32);
1108 hash.Update(pk, 32);
1109 UpdateFromStream(hash, stream);
1113inline bignum256modm_element_t
1114lt_modm(bignum256modm_element_t a, bignum256modm_element_t b) {
1115 return (a - b) >> 31;
1120reduce256_modm(bignum256modm r) {
1122 bignum256modm_element_t b = 0, pb, mask;
1126 pb += modm_m[0]; b = lt_modm(r[0], pb); t[0] = (r[0] - pb + (b << 30)); pb = b;
1127 pb += modm_m[1]; b = lt_modm(r[1], pb); t[1] = (r[1] - pb + (b << 30)); pb = b;
1128 pb += modm_m[2]; b = lt_modm(r[2], pb); t[2] = (r[2] - pb + (b << 30)); pb = b;
1129 pb += modm_m[3]; b = lt_modm(r[3], pb); t[3] = (r[3] - pb + (b << 30)); pb = b;
1130 pb += modm_m[4]; b = lt_modm(r[4], pb); t[4] = (r[4] - pb + (b << 30)); pb = b;
1131 pb += modm_m[5]; b = lt_modm(r[5], pb); t[5] = (r[5] - pb + (b << 30)); pb = b;
1132 pb += modm_m[6]; b = lt_modm(r[6], pb); t[6] = (r[6] - pb + (b << 30)); pb = b;
1133 pb += modm_m[7]; b = lt_modm(r[7], pb); t[7] = (r[7] - pb + (b << 30)); pb = b;
1134 pb += modm_m[8]; b = lt_modm(r[8], pb); t[8] = (r[8] - pb + (b << 16));
1138 r[0] ^= mask & (r[0] ^ t[0]);
1139 r[1] ^= mask & (r[1] ^ t[1]);
1140 r[2] ^= mask & (r[2] ^ t[2]);
1141 r[3] ^= mask & (r[3] ^ t[3]);
1142 r[4] ^= mask & (r[4] ^ t[4]);
1143 r[5] ^= mask & (r[5] ^ t[5]);
1144 r[6] ^= mask & (r[6] ^ t[6]);
1145 r[7] ^= mask & (r[7] ^ t[7]);
1146 r[8] ^= mask & (r[8] ^ t[8]);
1151barrett_reduce256_modm(bignum256modm r,
const bignum256modm q1,
const bignum256modm r1) {
1152 bignum256modm q3, r2;
1154 bignum256modm_element_t f, b, pb;
1160 c = mul32x32_64(modm_mu[0], q1[7]) + mul32x32_64(modm_mu[1], q1[6]) + mul32x32_64(modm_mu[2], q1[5]) + mul32x32_64(modm_mu[3], q1[4]) + mul32x32_64(modm_mu[4], q1[3]) + mul32x32_64(modm_mu[5], q1[2]) + mul32x32_64(modm_mu[6], q1[1]) + mul32x32_64(modm_mu[7], q1[0]);
1162 c += mul32x32_64(modm_mu[0], q1[8]) + mul32x32_64(modm_mu[1], q1[7]) + mul32x32_64(modm_mu[2], q1[6]) + mul32x32_64(modm_mu[3], q1[5]) + mul32x32_64(modm_mu[4], q1[4]) + mul32x32_64(modm_mu[5], q1[3]) + mul32x32_64(modm_mu[6], q1[2]) + mul32x32_64(modm_mu[7], q1[1]) + mul32x32_64(modm_mu[8], q1[0]);
1163 f = (bignum256modm_element_t)c; q3[0] = (f >> 24) & 0x3f; c >>= 30;
1164 c += mul32x32_64(modm_mu[1], q1[8]) + mul32x32_64(modm_mu[2], q1[7]) + mul32x32_64(modm_mu[3], q1[6]) + mul32x32_64(modm_mu[4], q1[5]) + mul32x32_64(modm_mu[5], q1[4]) + mul32x32_64(modm_mu[6], q1[3]) + mul32x32_64(modm_mu[7], q1[2]) + mul32x32_64(modm_mu[8], q1[1]);
1165 f = (bignum256modm_element_t)c; q3[0] |= (f << 6) & 0x3fffffff; q3[1] = (f >> 24) & 0x3f; c >>= 30;
1166 c += mul32x32_64(modm_mu[2], q1[8]) + mul32x32_64(modm_mu[3], q1[7]) + mul32x32_64(modm_mu[4], q1[6]) + mul32x32_64(modm_mu[5], q1[5]) + mul32x32_64(modm_mu[6], q1[4]) + mul32x32_64(modm_mu[7], q1[3]) + mul32x32_64(modm_mu[8], q1[2]);
1167 f = (bignum256modm_element_t)c; q3[1] |= (f << 6) & 0x3fffffff; q3[2] = (f >> 24) & 0x3f; c >>= 30;
1168 c += mul32x32_64(modm_mu[3], q1[8]) + mul32x32_64(modm_mu[4], q1[7]) + mul32x32_64(modm_mu[5], q1[6]) + mul32x32_64(modm_mu[6], q1[5]) + mul32x32_64(modm_mu[7], q1[4]) + mul32x32_64(modm_mu[8], q1[3]);
1169 f = (bignum256modm_element_t)c; q3[2] |= (f << 6) & 0x3fffffff; q3[3] = (f >> 24) & 0x3f; c >>= 30;
1170 c += mul32x32_64(modm_mu[4], q1[8]) + mul32x32_64(modm_mu[5], q1[7]) + mul32x32_64(modm_mu[6], q1[6]) + mul32x32_64(modm_mu[7], q1[5]) + mul32x32_64(modm_mu[8], q1[4]);
1171 f = (bignum256modm_element_t)c; q3[3] |= (f << 6) & 0x3fffffff; q3[4] = (f >> 24) & 0x3f; c >>= 30;
1172 c += mul32x32_64(modm_mu[5], q1[8]) + mul32x32_64(modm_mu[6], q1[7]) + mul32x32_64(modm_mu[7], q1[6]) + mul32x32_64(modm_mu[8], q1[5]);
1173 f = (bignum256modm_element_t)c; q3[4] |= (f << 6) & 0x3fffffff; q3[5] = (f >> 24) & 0x3f; c >>= 30;
1174 c += mul32x32_64(modm_mu[6], q1[8]) + mul32x32_64(modm_mu[7], q1[7]) + mul32x32_64(modm_mu[8], q1[6]);
1175 f = (bignum256modm_element_t)c; q3[5] |= (f << 6) & 0x3fffffff; q3[6] = (f >> 24) & 0x3f; c >>= 30;
1176 c += mul32x32_64(modm_mu[7], q1[8]) + mul32x32_64(modm_mu[8], q1[7]);
1177 f = (bignum256modm_element_t)c; q3[6] |= (f << 6) & 0x3fffffff; q3[7] = (f >> 24) & 0x3f; c >>= 30;
1178 c += mul32x32_64(modm_mu[8], q1[8]);
1179 f = (bignum256modm_element_t)c; q3[7] |= (f << 6) & 0x3fffffff; q3[8] = (bignum256modm_element_t)(c >> 24);
1184 c = mul32x32_64(modm_m[0], q3[0]);
1185 r2[0] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1186 c += mul32x32_64(modm_m[0], q3[1]) + mul32x32_64(modm_m[1], q3[0]);
1187 r2[1] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1188 c += mul32x32_64(modm_m[0], q3[2]) + mul32x32_64(modm_m[1], q3[1]) + mul32x32_64(modm_m[2], q3[0]);
1189 r2[2] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1190 c += mul32x32_64(modm_m[0], q3[3]) + mul32x32_64(modm_m[1], q3[2]) + mul32x32_64(modm_m[2], q3[1]) + mul32x32_64(modm_m[3], q3[0]);
1191 r2[3] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1192 c += mul32x32_64(modm_m[0], q3[4]) + mul32x32_64(modm_m[1], q3[3]) + mul32x32_64(modm_m[2], q3[2]) + mul32x32_64(modm_m[3], q3[1]) + mul32x32_64(modm_m[4], q3[0]);
1193 r2[4] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1194 c += mul32x32_64(modm_m[0], q3[5]) + mul32x32_64(modm_m[1], q3[4]) + mul32x32_64(modm_m[2], q3[3]) + mul32x32_64(modm_m[3], q3[2]) + mul32x32_64(modm_m[4], q3[1]) + mul32x32_64(modm_m[5], q3[0]);
1195 r2[5] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1196 c += mul32x32_64(modm_m[0], q3[6]) + mul32x32_64(modm_m[1], q3[5]) + mul32x32_64(modm_m[2], q3[4]) + mul32x32_64(modm_m[3], q3[3]) + mul32x32_64(modm_m[4], q3[2]) + mul32x32_64(modm_m[5], q3[1]) + mul32x32_64(modm_m[6], q3[0]);
1197 r2[6] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1198 c += mul32x32_64(modm_m[0], q3[7]) + mul32x32_64(modm_m[1], q3[6]) + mul32x32_64(modm_m[2], q3[5]) + mul32x32_64(modm_m[3], q3[4]) + mul32x32_64(modm_m[4], q3[3]) + mul32x32_64(modm_m[5], q3[2]) + mul32x32_64(modm_m[6], q3[1]) + mul32x32_64(modm_m[7], q3[0]);
1199 r2[7] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1200 c += mul32x32_64(modm_m[0], q3[8]) + mul32x32_64(modm_m[1], q3[7]) + mul32x32_64(modm_m[2], q3[6]) + mul32x32_64(modm_m[3], q3[5]) + mul32x32_64(modm_m[4], q3[4]) + mul32x32_64(modm_m[5], q3[3]) + mul32x32_64(modm_m[6], q3[2]) + mul32x32_64(modm_m[7], q3[1]) + mul32x32_64(modm_m[8], q3[0]);
1201 r2[8] = (bignum256modm_element_t)(c & 0xffffff);
1206 pb += r2[0]; b = lt_modm(r1[0], pb); r[0] = (r1[0] - pb + (b << 30)); pb = b;
1207 pb += r2[1]; b = lt_modm(r1[1], pb); r[1] = (r1[1] - pb + (b << 30)); pb = b;
1208 pb += r2[2]; b = lt_modm(r1[2], pb); r[2] = (r1[2] - pb + (b << 30)); pb = b;
1209 pb += r2[3]; b = lt_modm(r1[3], pb); r[3] = (r1[3] - pb + (b << 30)); pb = b;
1210 pb += r2[4]; b = lt_modm(r1[4], pb); r[4] = (r1[4] - pb + (b << 30)); pb = b;
1211 pb += r2[5]; b = lt_modm(r1[5], pb); r[5] = (r1[5] - pb + (b << 30)); pb = b;
1212 pb += r2[6]; b = lt_modm(r1[6], pb); r[6] = (r1[6] - pb + (b << 30)); pb = b;
1213 pb += r2[7]; b = lt_modm(r1[7], pb); r[7] = (r1[7] - pb + (b << 30)); pb = b;
1214 pb += r2[8]; b = lt_modm(r1[8], pb); r[8] = (r1[8] - pb + (b << 24));
1222add256_modm(bignum256modm r,
const bignum256modm x,
const bignum256modm y) {
1223 bignum256modm_element_t c;
1225 c = x[0] + y[0]; r[0] = c & 0x3fffffff; c >>= 30;
1226 c += x[1] + y[1]; r[1] = c & 0x3fffffff; c >>= 30;
1227 c += x[2] + y[2]; r[2] = c & 0x3fffffff; c >>= 30;
1228 c += x[3] + y[3]; r[3] = c & 0x3fffffff; c >>= 30;
1229 c += x[4] + y[4]; r[4] = c & 0x3fffffff; c >>= 30;
1230 c += x[5] + y[5]; r[5] = c & 0x3fffffff; c >>= 30;
1231 c += x[6] + y[6]; r[6] = c & 0x3fffffff; c >>= 30;
1232 c += x[7] + y[7]; r[7] = c & 0x3fffffff; c >>= 30;
1233 c += x[8] + y[8]; r[8] = c;
1240mul256_modm(bignum256modm r,
const bignum256modm x,
const bignum256modm y) {
1241 bignum256modm r1, q1;
1243 bignum256modm_element_t f;
1245 c = mul32x32_64(x[0], y[0]);
1246 f = (bignum256modm_element_t)c; r1[0] = (f & 0x3fffffff); c >>= 30;
1247 c += mul32x32_64(x[0], y[1]) + mul32x32_64(x[1], y[0]);
1248 f = (bignum256modm_element_t)c; r1[1] = (f & 0x3fffffff); c >>= 30;
1249 c += mul32x32_64(x[0], y[2]) + mul32x32_64(x[1], y[1]) + mul32x32_64(x[2], y[0]);
1250 f = (bignum256modm_element_t)c; r1[2] = (f & 0x3fffffff); c >>= 30;
1251 c += mul32x32_64(x[0], y[3]) + mul32x32_64(x[1], y[2]) + mul32x32_64(x[2], y[1]) + mul32x32_64(x[3], y[0]);
1252 f = (bignum256modm_element_t)c; r1[3] = (f & 0x3fffffff); c >>= 30;
1253 c += mul32x32_64(x[0], y[4]) + mul32x32_64(x[1], y[3]) + mul32x32_64(x[2], y[2]) + mul32x32_64(x[3], y[1]) + mul32x32_64(x[4], y[0]);
1254 f = (bignum256modm_element_t)c; r1[4] = (f & 0x3fffffff); c >>= 30;
1255 c += mul32x32_64(x[0], y[5]) + mul32x32_64(x[1], y[4]) + mul32x32_64(x[2], y[3]) + mul32x32_64(x[3], y[2]) + mul32x32_64(x[4], y[1]) + mul32x32_64(x[5], y[0]);
1256 f = (bignum256modm_element_t)c; r1[5] = (f & 0x3fffffff); c >>= 30;
1257 c += mul32x32_64(x[0], y[6]) + mul32x32_64(x[1], y[5]) + mul32x32_64(x[2], y[4]) + mul32x32_64(x[3], y[3]) + mul32x32_64(x[4], y[2]) + mul32x32_64(x[5], y[1]) + mul32x32_64(x[6], y[0]);
1258 f = (bignum256modm_element_t)c; r1[6] = (f & 0x3fffffff); c >>= 30;
1259 c += mul32x32_64(x[0], y[7]) + mul32x32_64(x[1], y[6]) + mul32x32_64(x[2], y[5]) + mul32x32_64(x[3], y[4]) + mul32x32_64(x[4], y[3]) + mul32x32_64(x[5], y[2]) + mul32x32_64(x[6], y[1]) + mul32x32_64(x[7], y[0]);
1260 f = (bignum256modm_element_t)c; r1[7] = (f & 0x3fffffff); c >>= 30;
1261 c += mul32x32_64(x[0], y[8]) + mul32x32_64(x[1], y[7]) + mul32x32_64(x[2], y[6]) + mul32x32_64(x[3], y[5]) + mul32x32_64(x[4], y[4]) + mul32x32_64(x[5], y[3]) + mul32x32_64(x[6], y[2]) + mul32x32_64(x[7], y[1]) + mul32x32_64(x[8], y[0]);
1262 f = (bignum256modm_element_t)c; r1[8] = (f & 0x00ffffff); q1[0] = (f >> 8) & 0x3fffff; c >>= 30;
1263 c += mul32x32_64(x[1], y[8]) + mul32x32_64(x[2], y[7]) + mul32x32_64(x[3], y[6]) + mul32x32_64(x[4], y[5]) + mul32x32_64(x[5], y[4]) + mul32x32_64(x[6], y[3]) + mul32x32_64(x[7], y[2]) + mul32x32_64(x[8], y[1]);
1264 f = (bignum256modm_element_t)c; q1[0] = (q1[0] | (f << 22)) & 0x3fffffff; q1[1] = (f >> 8) & 0x3fffff; c >>= 30;
1265 c += mul32x32_64(x[2], y[8]) + mul32x32_64(x[3], y[7]) + mul32x32_64(x[4], y[6]) + mul32x32_64(x[5], y[5]) + mul32x32_64(x[6], y[4]) + mul32x32_64(x[7], y[3]) + mul32x32_64(x[8], y[2]);
1266 f = (bignum256modm_element_t)c; q1[1] = (q1[1] | (f << 22)) & 0x3fffffff; q1[2] = (f >> 8) & 0x3fffff; c >>= 30;
1267 c += mul32x32_64(x[3], y[8]) + mul32x32_64(x[4], y[7]) + mul32x32_64(x[5], y[6]) + mul32x32_64(x[6], y[5]) + mul32x32_64(x[7], y[4]) + mul32x32_64(x[8], y[3]);
1268 f = (bignum256modm_element_t)c; q1[2] = (q1[2] | (f << 22)) & 0x3fffffff; q1[3] = (f >> 8) & 0x3fffff; c >>= 30;
1269 c += mul32x32_64(x[4], y[8]) + mul32x32_64(x[5], y[7]) + mul32x32_64(x[6], y[6]) + mul32x32_64(x[7], y[5]) + mul32x32_64(x[8], y[4]);
1270 f = (bignum256modm_element_t)c; q1[3] = (q1[3] | (f << 22)) & 0x3fffffff; q1[4] = (f >> 8) & 0x3fffff; c >>= 30;
1271 c += mul32x32_64(x[5], y[8]) + mul32x32_64(x[6], y[7]) + mul32x32_64(x[7], y[6]) + mul32x32_64(x[8], y[5]);
1272 f = (bignum256modm_element_t)c; q1[4] = (q1[4] | (f << 22)) & 0x3fffffff; q1[5] = (f >> 8) & 0x3fffff; c >>= 30;
1273 c += mul32x32_64(x[6], y[8]) + mul32x32_64(x[7], y[7]) + mul32x32_64(x[8], y[6]);
1274 f = (bignum256modm_element_t)c; q1[5] = (q1[5] | (f << 22)) & 0x3fffffff; q1[6] = (f >> 8) & 0x3fffff; c >>= 30;
1275 c += mul32x32_64(x[7], y[8]) + mul32x32_64(x[8], y[7]);
1276 f = (bignum256modm_element_t)c; q1[6] = (q1[6] | (f << 22)) & 0x3fffffff; q1[7] = (f >> 8) & 0x3fffff; c >>= 30;
1277 c += mul32x32_64(x[8], y[8]);
1278 f = (bignum256modm_element_t)c; q1[7] = (q1[7] | (f << 22)) & 0x3fffffff; q1[8] = (f >> 8) & 0x3fffff;
1280 barrett_reduce256_modm(r, q1, r1);
1284expand256_modm(bignum256modm out,
const byte *in,
size_t len) {
1285 byte work[64] = {0};
1286 bignum256modm_element_t x[16];
1289 std::memcpy(work, in, len);
1290 x[0] = U8TO32_LE(work + 0);
1291 x[1] = U8TO32_LE(work + 4);
1292 x[2] = U8TO32_LE(work + 8);
1293 x[3] = U8TO32_LE(work + 12);
1294 x[4] = U8TO32_LE(work + 16);
1295 x[5] = U8TO32_LE(work + 20);
1296 x[6] = U8TO32_LE(work + 24);
1297 x[7] = U8TO32_LE(work + 28);
1298 x[8] = U8TO32_LE(work + 32);
1299 x[9] = U8TO32_LE(work + 36);
1300 x[10] = U8TO32_LE(work + 40);
1301 x[11] = U8TO32_LE(work + 44);
1302 x[12] = U8TO32_LE(work + 48);
1303 x[13] = U8TO32_LE(work + 52);
1304 x[14] = U8TO32_LE(work + 56);
1305 x[15] = U8TO32_LE(work + 60);
1308 out[0] = ( x[0]) & 0x3fffffff;
1309 out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1310 out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1311 out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1312 out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1313 out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1314 out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1315 out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1316 out[8] = ((x[ 7] >> 16) | (x[ 8] << 16)) & 0x00ffffff;
1323 q1[0] = ((x[ 7] >> 24) | (x[ 8] << 8)) & 0x3fffffff;
1324 q1[1] = ((x[ 8] >> 22) | (x[ 9] << 10)) & 0x3fffffff;
1325 q1[2] = ((x[ 9] >> 20) | (x[10] << 12)) & 0x3fffffff;
1326 q1[3] = ((x[10] >> 18) | (x[11] << 14)) & 0x3fffffff;
1327 q1[4] = ((x[11] >> 16) | (x[12] << 16)) & 0x3fffffff;
1328 q1[5] = ((x[12] >> 14) | (x[13] << 18)) & 0x3fffffff;
1329 q1[6] = ((x[13] >> 12) | (x[14] << 20)) & 0x3fffffff;
1330 q1[7] = ((x[14] >> 10) | (x[15] << 22)) & 0x3fffffff;
1331 q1[8] = ((x[15] >> 8) );
1333 barrett_reduce256_modm(out, q1, out);
1337expand_raw256_modm(bignum256modm out,
const byte in[32]) {
1338 bignum256modm_element_t x[8];
1340 x[0] = U8TO32_LE(in + 0);
1341 x[1] = U8TO32_LE(in + 4);
1342 x[2] = U8TO32_LE(in + 8);
1343 x[3] = U8TO32_LE(in + 12);
1344 x[4] = U8TO32_LE(in + 16);
1345 x[5] = U8TO32_LE(in + 20);
1346 x[6] = U8TO32_LE(in + 24);
1347 x[7] = U8TO32_LE(in + 28);
1349 out[0] = ( x[0]) & 0x3fffffff;
1350 out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1351 out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1352 out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1353 out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1354 out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1355 out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1356 out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1357 out[8] = ((x[ 7] >> 16) ) & 0x0000ffff;
1361contract256_modm(
byte out[32],
const bignum256modm in) {
1362 U32TO8_LE(out + 0, (in[0] ) | (in[1] << 30));
1363 U32TO8_LE(out + 4, (in[1] >> 2) | (in[2] << 28));
1364 U32TO8_LE(out + 8, (in[2] >> 4) | (in[3] << 26));
1365 U32TO8_LE(out + 12, (in[3] >> 6) | (in[4] << 24));
1366 U32TO8_LE(out + 16, (in[4] >> 8) | (in[5] << 22));
1367 U32TO8_LE(out + 20, (in[5] >> 10) | (in[6] << 20));
1368 U32TO8_LE(out + 24, (in[6] >> 12) | (in[7] << 18));
1369 U32TO8_LE(out + 28, (in[7] >> 14) | (in[8] << 16));
1373contract256_window4_modm(
signed char r[64],
const bignum256modm in) {
1375 signed char *quads = r;
1376 bignum256modm_element_t i, j, v;
1378 for (i = 0; i < 8; i += 2) {
1380 for (j = 0; j < 7; j++) {
1381 *quads++ = (v & 15);
1384 v |= (in[i+1] << 2);
1385 for (j = 0; j < 8; j++) {
1386 *quads++ = (v & 15);
1392 *quads++ = (v & 15); v >>= 4;
1393 *quads++ = (v & 15); v >>= 4;
1394 *quads++ = (v & 15); v >>= 4;
1395 *quads++ = (v & 15); v >>= 4;
1399 for(i = 0; i < 63; i++) {
1401 r[i+1] += (r[i] >> 4);
1403 carry = (r[i] >> 3);
1404 r[i] -= (carry << 4);
1410contract256_slidingwindow_modm(
signed char r[256],
const bignum256modm s,
int windowsize) {
1412 int m = (1 << (windowsize - 1)) - 1, soplen = 256;
1413 signed char *bits = r;
1414 bignum256modm_element_t v;
1417 for (i = 0; i < 8; i++) {
1419 for (j = 0; j < 30; j++, v >>= 1)
1423 for (j = 0; j < 16; j++, v >>= 1)
1427 for (j = 0; j < soplen; j++) {
1431 for (b = 1; (b < (soplen - j)) && (b <= 6); b++) {
1432 if ((r[j] + (r[j + b] << b)) <= m) {
1433 r[j] += r[j + b] << b;
1435 }
else if ((r[j] - (r[j + b] << b)) >= -m) {
1436 r[j] -= r[j + b] << b;
1437 for (k = j + b; k < soplen; k++) {
1444 }
else if (r[j + b]) {
1452ge25519_p1p1_to_partial(ge25519 *r,
const ge25519_p1p1 *p) {
1453 curve25519_mul(r->x, p->x, p->t);
1454 curve25519_mul(r->y, p->y, p->z);
1455 curve25519_mul(r->z, p->z, p->t);
1459ge25519_p1p1_to_full(ge25519 *r,
const ge25519_p1p1 *p) {
1460 curve25519_mul(r->x, p->x, p->t);
1461 curve25519_mul(r->y, p->y, p->z);
1462 curve25519_mul(r->z, p->z, p->t);
1463 curve25519_mul(r->t, p->x, p->y);
1467ge25519_full_to_pniels(ge25519_pniels *p,
const ge25519 *r) {
1468 curve25519_sub(p->ysubx, r->y, r->x);
1469 curve25519_add(p->xaddy, r->y, r->x);
1470 curve25519_copy(p->z, r->z);
1471 curve25519_mul(p->t2d, r->t, ge25519_ec2d);
1475ge25519_add_p1p1(ge25519_p1p1 *r,
const ge25519 *p,
const ge25519 *q) {
1476 bignum25519 a,b,c,d,t,u;
1478 curve25519_sub(a, p->y, p->x);
1479 curve25519_add(b, p->y, p->x);
1480 curve25519_sub(t, q->y, q->x);
1481 curve25519_add(u, q->y, q->x);
1482 curve25519_mul(a, a, t);
1483 curve25519_mul(b, b, u);
1484 curve25519_mul(c, p->t, q->t);
1485 curve25519_mul(c, c, ge25519_ec2d);
1486 curve25519_mul(d, p->z, q->z);
1487 curve25519_add(d, d, d);
1488 curve25519_sub(r->x, b, a);
1489 curve25519_add(r->y, b, a);
1490 curve25519_add_after_basic(r->z, d, c);
1491 curve25519_sub_after_basic(r->t, d, c);
1495ge25519_double_p1p1(ge25519_p1p1 *r,
const ge25519 *p) {
1498 curve25519_square(a, p->x);
1499 curve25519_square(b, p->y);
1500 curve25519_square(c, p->z);
1501 curve25519_add_reduce(c, c, c);
1502 curve25519_add(r->x, p->x, p->y);
1503 curve25519_square(r->x, r->x);
1504 curve25519_add(r->y, b, a);
1505 curve25519_sub(r->z, b, a);
1506 curve25519_sub_after_basic(r->x, r->x, r->y);
1507 curve25519_sub_after_basic(r->t, c, r->z);
1511ge25519_nielsadd2_p1p1(ge25519_p1p1 *r,
const ge25519 *p,
const ge25519_niels *q,
byte signbit) {
1512 const bignum25519 *qb = (
const bignum25519 *)q;
1513 bignum25519 *rb = (bignum25519 *)r;
1516 curve25519_sub(a, p->y, p->x);
1517 curve25519_add(b, p->y, p->x);
1518 curve25519_mul(a, a, qb[signbit]);
1519 curve25519_mul(r->x, b, qb[signbit^1]);
1520 curve25519_add(r->y, r->x, a);
1521 curve25519_sub(r->x, r->x, a);
1522 curve25519_mul(c, p->t, q->t2d);
1523 curve25519_add_reduce(r->t, p->z, p->z);
1524 curve25519_copy(r->z, r->t);
1525 curve25519_add(rb[2+signbit], rb[2+signbit], c);
1526 curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c);
1530ge25519_pnielsadd_p1p1(ge25519_p1p1 *r,
const ge25519 *p,
const ge25519_pniels *q,
byte signbit) {
1531 const bignum25519 *qb = (
const bignum25519 *)q;
1532 bignum25519 *rb = (bignum25519 *)r;
1535 curve25519_sub(a, p->y, p->x);
1536 curve25519_add(b, p->y, p->x);
1537 curve25519_mul(a, a, qb[signbit]);
1538 curve25519_mul(r->x, b, qb[signbit^1]);
1539 curve25519_add(r->y, r->x, a);
1540 curve25519_sub(r->x, r->x, a);
1541 curve25519_mul(c, p->t, q->t2d);
1542 curve25519_mul(r->t, p->z, q->z);
1543 curve25519_add_reduce(r->t, r->t, r->t);
1544 curve25519_copy(r->z, r->t);
1545 curve25519_add(rb[2+signbit], rb[2+signbit], c);
1546 curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c);
1550ge25519_double_partial(ge25519 *r,
const ge25519 *p) {
1552 ge25519_double_p1p1(&t, p);
1553 ge25519_p1p1_to_partial(r, &t);
1557ge25519_double(ge25519 *r,
const ge25519 *p) {
1559 ge25519_double_p1p1(&t, p);
1560 ge25519_p1p1_to_full(r, &t);
1564ge25519_add(ge25519 *r,
const ge25519 *p,
const ge25519 *q) {
1566 ge25519_add_p1p1(&t, p, q);
1567 ge25519_p1p1_to_full(r, &t);
1571ge25519_nielsadd2(ge25519 *r,
const ge25519_niels *q) {
1572 bignum25519 a,b,c,e,f,g,h;
1574 curve25519_sub(a, r->y, r->x);
1575 curve25519_add(b, r->y, r->x);
1576 curve25519_mul(a, a, q->ysubx);
1577 curve25519_mul(e, b, q->xaddy);
1578 curve25519_add(h, e, a);
1579 curve25519_sub(e, e, a);
1580 curve25519_mul(c, r->t, q->t2d);
1581 curve25519_add(f, r->z, r->z);
1582 curve25519_add_after_basic(g, f, c);
1583 curve25519_sub_after_basic(f, f, c);
1584 curve25519_mul(r->x, e, f);
1585 curve25519_mul(r->y, h, g);
1586 curve25519_mul(r->z, g, f);
1587 curve25519_mul(r->t, e, h);
1591ge25519_pnielsadd(ge25519_pniels *r,
const ge25519 *p,
const ge25519_pniels *q) {
1592 bignum25519 a,b,c,x,y,z,t;
1594 curve25519_sub(a, p->y, p->x);
1595 curve25519_add(b, p->y, p->x);
1596 curve25519_mul(a, a, q->ysubx);
1597 curve25519_mul(x, b, q->xaddy);
1598 curve25519_add(y, x, a);
1599 curve25519_sub(x, x, a);
1600 curve25519_mul(c, p->t, q->t2d);
1601 curve25519_mul(t, p->z, q->z);
1602 curve25519_add(t, t, t);
1603 curve25519_add_after_basic(z, t, c);
1604 curve25519_sub_after_basic(t, t, c);
1605 curve25519_mul(r->xaddy, x, t);
1606 curve25519_mul(r->ysubx, y, z);
1607 curve25519_mul(r->z, z, t);
1608 curve25519_mul(r->t2d, x, y);
1609 curve25519_copy(y, r->ysubx);
1610 curve25519_sub(r->ysubx, r->ysubx, r->xaddy);
1611 curve25519_add(r->xaddy, r->xaddy, y);
1612 curve25519_mul(r->t2d, r->t2d, ge25519_ec2d);
1616ge25519_pack(
byte r[32],
const ge25519 *p) {
1617 bignum25519 tx, ty, zi;
1619 curve25519_recip(zi, p->z);
1620 curve25519_mul(tx, p->x, zi);
1621 curve25519_mul(ty, p->y, zi);
1622 curve25519_contract(r, ty);
1623 curve25519_contract(parity, tx);
1624 r[31] ^= ((parity[0] & 1) << 7);
1628ed25519_verify(
const byte *x,
const byte *y,
size_t len) {
1629 size_t differentbits = 0;
1631 differentbits |= (*x++ ^ *y++);
1632 return (
int) (1 & ((differentbits - 1) >> 8));
1636ge25519_unpack_negative_vartime(ge25519 *r,
const byte p[32]) {
1637 const byte zero[32] = {0};
1638 const bignum25519 one = {1};
1639 byte parity = p[31] >> 7;
1641 bignum25519 t, root, num, den, d3;
1643 curve25519_expand(r->y, p);
1644 curve25519_copy(r->z, one);
1645 curve25519_square(num, r->y);
1646 curve25519_mul(den, num, ge25519_ecd);
1647 curve25519_sub_reduce(num, num, r->z);
1648 curve25519_add(den, den, r->z);
1652 curve25519_square(t, den);
1653 curve25519_mul(d3, t, den);
1654 curve25519_square(r->x, d3);
1655 curve25519_mul(r->x, r->x, den);
1656 curve25519_mul(r->x, r->x, num);
1657 curve25519_pow_two252m3(r->x, r->x);
1660 curve25519_mul(r->x, r->x, d3);
1661 curve25519_mul(r->x, r->x, num);
1664 curve25519_square(t, r->x);
1665 curve25519_mul(t, t, den);
1666 curve25519_sub_reduce(root, t, num);
1667 curve25519_contract(check, root);
1668 if (!ed25519_verify(check, zero, 32)) {
1669 curve25519_add_reduce(t, t, num);
1670 curve25519_contract(check, t);
1671 if (!ed25519_verify(check, zero, 32))
1673 curve25519_mul(r->x, r->x, ge25519_sqrtneg1);
1676 curve25519_contract(check, r->x);
1677 if ((check[0] & 1) == parity) {
1678 curve25519_copy(t, r->x);
1679 curve25519_neg(r->x, t);
1681 curve25519_mul(r->t, r->x, r->y);
1687ge25519_double_scalarmult_vartime(ge25519 *r,
const ge25519 *p1,
const bignum256modm s1,
const bignum256modm s2) {
1688 signed char slide1[256], slide2[256];
1689 ge25519_pniels pre1[S1_TABLE_SIZE];
1694 contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
1695 contract256_slidingwindow_modm(slide2, s2, S2_SWINDOWSIZE);
1697 ge25519_double(&d1, p1);
1698 ge25519_full_to_pniels(pre1, p1);
1699 for (i = 0; i < S1_TABLE_SIZE - 1; i++)
1700 ge25519_pnielsadd(&pre1[i+1], &d1, &pre1[i]);
1703 std::memset(r, 0,
sizeof(ge25519));
1708 while ((i >= 0) && !(slide1[i] | slide2[i]))
1711 for (; i >= 0; i--) {
1712 ge25519_double_p1p1(&t, r);
1715 ge25519_p1p1_to_full(r, &t);
1716 ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (
byte)slide1[i] >> 7);
1720 ge25519_p1p1_to_full(r, &t);
1721 ge25519_nielsadd2_p1p1(&t, r, &ge25519_niels_sliding_multiples[abs(slide2[i]) / 2], (
byte)slide2[i] >> 7);
1724 ge25519_p1p1_to_partial(r, &t);
1728#if !defined(HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS)
1732 return ((b ^ c) - 1) >> 31;
1736ge25519_scalarmult_base_choose_niels(ge25519_niels *t,
const byte table[256][96],
word32 pos,
signed char b) {
1739 word32 mask = ~(sign - 1);
1740 word32 u = (b + mask) ^ mask;
1744 byte packed[96] = {0};
1748 for (i = 0; i < 8; i++)
1749 curve25519_move_conditional_bytes(packed, table[(pos * 8) + i], ge25519_windowb_equal(u, i + 1));
1752 curve25519_expand(t->ysubx, packed + 0);
1753 curve25519_expand(t->xaddy, packed + 32);
1754 curve25519_expand(t->t2d , packed + 64);
1757 curve25519_swap_conditional(t->ysubx, t->xaddy, sign);
1758 curve25519_neg(neg, t->t2d);
1759 curve25519_swap_conditional(t->t2d, neg, sign);
1766ge25519_scalarmult_base_niels(ge25519 *r,
const byte basepoint_table[256][96],
const bignum256modm s) {
1771 contract256_window4_modm(b, s);
1773 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[1]);
1774 curve25519_sub_reduce(r->x, t.xaddy, t.ysubx);
1775 curve25519_add_reduce(r->y, t.xaddy, t.ysubx);
1776 std::memset(r->z, 0,
sizeof(bignum25519));
1777 curve25519_copy(r->t, t.t2d);
1779 for (i = 3; i < 64; i += 2) {
1780 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1781 ge25519_nielsadd2(r, &t);
1783 ge25519_double_partial(r, r);
1784 ge25519_double_partial(r, r);
1785 ge25519_double_partial(r, r);
1786 ge25519_double(r, r);
1787 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[0]);
1788 curve25519_mul(t.t2d, t.t2d, ge25519_ecd);
1789 ge25519_nielsadd2(r, &t);
1790 for(i = 2; i < 64; i += 2) {
1791 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1792 ge25519_nielsadd2(r, &t);
1796ANONYMOUS_NAMESPACE_END
1804NAMESPACE_BEGIN(Donna)
1806int curve25519_mult_CXX(
byte sharedKey[32],
const byte secretKey[32],
const byte othersKey[32])
1808 using namespace CryptoPP::Donna::X25519;
1811 for (
size_t i = 0; i < 32; ++i)
1812 e[i] = secretKey[i];
1813 e[0] &= 0xf8; e[31] &= 0x7f; e[31] |= 0x40;
1815 bignum25519 nqpqx = {1}, nqpqz = {0}, nqz = {1}, nqx;
1816 bignum25519 q, qx, qpqx, qqx, zzz, zmone;
1817 size_t bit, lastbit;
1819 curve25519_expand(q, othersKey);
1820 curve25519_copy(nqx, q);
1827 for (
int i = 253; i >= 2; i--) {
1828 curve25519_add(qx, nqx, nqz);
1829 curve25519_sub(nqz, nqx, nqz);
1830 curve25519_add(qpqx, nqpqx, nqpqz);
1831 curve25519_sub(nqpqz, nqpqx, nqpqz);
1832 curve25519_mul(nqpqx, qpqx, nqz);
1833 curve25519_mul(nqpqz, qx, nqpqz);
1834 curve25519_add(qqx, nqpqx, nqpqz);
1835 curve25519_sub(nqpqz, nqpqx, nqpqz);
1836 curve25519_square(nqpqz, nqpqz);
1837 curve25519_square(nqpqx, qqx);
1838 curve25519_mul(nqpqz, nqpqz, q);
1839 curve25519_square(qx, qx);
1840 curve25519_square(nqz, nqz);
1841 curve25519_mul(nqx, qx, nqz);
1842 curve25519_sub(nqz, qx, nqz);
1843 curve25519_scalar_product(zzz, nqz, 121665);
1844 curve25519_add(zzz, zzz, qx);
1845 curve25519_mul(nqz, nqz, zzz);
1847 bit = (e[i/8] >> (i & 7)) & 1;
1848 curve25519_swap_conditional(nqx, nqpqx, (
word32)(bit ^ lastbit));
1849 curve25519_swap_conditional(nqz, nqpqz, (
word32)(bit ^ lastbit));
1854 for (
int i = 0; i < 3; i++) {
1855 curve25519_add(qx, nqx, nqz);
1856 curve25519_sub(nqz, nqx, nqz);
1857 curve25519_square(qx, qx);
1858 curve25519_square(nqz, nqz);
1859 curve25519_mul(nqx, qx, nqz);
1860 curve25519_sub(nqz, qx, nqz);
1861 curve25519_scalar_product(zzz, nqz, 121665);
1862 curve25519_add(zzz, zzz, qx);
1863 curve25519_mul(nqz, nqz, zzz);
1866 curve25519_recip(zmone, nqz);
1867 curve25519_mul(nqz, nqx, zmone);
1868 curve25519_contract(sharedKey, nqz);
1875 using namespace CryptoPP::Donna::X25519;
1877#if (CRYPTOPP_CURVE25519_SSE2)
1879 return curve25519_mult_SSE2(publicKey, secretKey, basePoint);
1883 return curve25519_mult_CXX(publicKey, secretKey, basePoint);
1886int curve25519_mult(
byte sharedKey[32],
const byte secretKey[32],
const byte othersKey[32])
1888#if (CRYPTOPP_CURVE25519_SSE2)
1890 return curve25519_mult_SSE2(sharedKey, secretKey, othersKey);
1894 return curve25519_mult_CXX(sharedKey, secretKey, othersKey);
1903NAMESPACE_BEGIN(Donna)
1906ed25519_publickey_CXX(
byte publicKey[32],
const byte secretKey[32])
1908 using namespace CryptoPP::Donna::Ed25519;
1911 ALIGN(ALIGN_SPEC) ge25519 A;
1915 ed25519_extsk(extsk, secretKey);
1916 expand256_modm(a, extsk, 32);
1917 ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
1918 ge25519_pack(publicKey, &A);
1926 return ed25519_publickey_CXX(publicKey, secretKey);
1930ed25519_sign_CXX(std::istream& stream,
const byte sk[32],
const byte pk[32],
byte RS[64])
1932 using namespace CryptoPP::Donna::Ed25519;
1934 bignum256modm r, S, a;
1935 ALIGN(ALIGN_SPEC) ge25519 R;
1936 hash_512bits extsk, hashr, hram;
1942 std::streampos where = stream.tellg();
1944 ed25519_extsk(extsk, sk);
1948 hash.Update(extsk + 32, 32);
1949 UpdateFromStream(hash, stream);
1951 expand256_modm(r, hashr, 64);
1954 ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1955 ge25519_pack(RS, &R);
1959 stream.seekg(where);
1962 ed25519_hram(hram, RS, pk, stream);
1963 expand256_modm(S, hram, 64);
1966 expand256_modm(a, extsk, 32);
1967 mul256_modm(S, S, a);
1970 add256_modm(S, S, r);
1973 contract256_modm(RS + 32, S);
1979ed25519_sign_CXX(
const byte *m,
size_t mlen,
const byte sk[32],
const byte pk[32],
byte RS[64])
1981 using namespace CryptoPP::Donna::Ed25519;
1983 bignum256modm r, S, a;
1984 ALIGN(ALIGN_SPEC) ge25519 R;
1985 hash_512bits extsk, hashr, hram;
1987 ed25519_extsk(extsk, sk);
1991 hash.Update(extsk + 32, 32);
1992 hash.Update(m, mlen);
1994 expand256_modm(r, hashr, 64);
1997 ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1998 ge25519_pack(RS, &R);
2001 ed25519_hram(hram, RS, pk, m, mlen);
2002 expand256_modm(S, hram, 64);
2005 expand256_modm(a, extsk, 32);
2006 mul256_modm(S, S, a);
2009 add256_modm(S, S, r);
2012 contract256_modm(RS + 32, S);
2018ed25519_sign(std::istream& stream,
const byte secretKey[32],
const byte publicKey[32],
2021 return ed25519_sign_CXX(stream, secretKey, publicKey, signature);
2025ed25519_sign(
const byte* message,
size_t messageLength,
const byte secretKey[32],
2026 const byte publicKey[32],
byte signature[64])
2028 return ed25519_sign_CXX(message, messageLength, secretKey, publicKey, signature);
2032ed25519_sign_open_CXX(std::istream& stream,
const byte pk[32],
const byte RS[64]) {
2034 using namespace CryptoPP::Donna::Ed25519;
2036 ALIGN(ALIGN_SPEC) ge25519 R, A;
2038 bignum256modm hram, S;
2041 if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
2045 ed25519_hram(hash, RS, pk, stream);
2046 expand256_modm(hram, hash, 64);
2049 expand256_modm(S, RS + 32, 32);
2052 ge25519_double_scalarmult_vartime(&R, &A, hram, S);
2053 ge25519_pack(checkR, &R);
2056 return ed25519_verify(RS, checkR, 32) ? 0 : -1;
2060ed25519_sign_open_CXX(
const byte *m,
size_t mlen,
const byte pk[32],
const byte RS[64]) {
2062 using namespace CryptoPP::Donna::Ed25519;
2064 ALIGN(ALIGN_SPEC) ge25519 R, A;
2066 bignum256modm hram, S;
2069 if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
2073 ed25519_hram(hash, RS, pk, m, mlen);
2074 expand256_modm(hram, hash, 64);
2077 expand256_modm(S, RS + 32, 32);
2080 ge25519_double_scalarmult_vartime(&R, &A, hram, S);
2081 ge25519_pack(checkR, &R);
2084 return ed25519_verify(RS, checkR, 32) ? 0 : -1;
2088ed25519_sign_open(
const byte *message,
size_t messageLength,
const byte publicKey[32],
const byte signature[64])
2090 return ed25519_sign_open_CXX(message, messageLength, publicKey, signature);
2094ed25519_sign_open(std::istream& stream,
const byte publicKey[32],
const byte signature[64])
2096 return ed25519_sign_open_CXX(stream, publicKey, signature);
Fixed size stack-based SecBlock.
Access a block of memory.
Library configuration file.
signed int sword32
32-bit signed datatype
unsigned int word32
32-bit unsigned datatype
unsigned long long word64
64-bit unsigned datatype
Functions for CPU features and intrinsics.
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
int ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
Verifies a signature on a message.
int ed25519_sign(const byte *message, size_t messageLength, const byte secretKey[32], const byte publicKey[32], byte signature[64])
Creates a signature on a message.
int ed25519_publickey(byte publicKey[32], const byte secretKey[32])
Creates a public key from a secret key.
int curve25519_mult(byte publicKey[32], const byte secretKey[32])
Generate a public key.
Utility functions for the Crypto++ library.
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Classes for SHA-1 and SHA-2 family of message digests.