Crypto++  8.0
Free C++ class library of cryptographic schemes
donna_32.cpp
1 // donna_32.cpp - written and placed in public domain by Jeffrey Walton
2 // Crypto++ specific implementation wrapped around Andrew
3 // Moon's public domain curve25519-donna and ed25519-donna,
4 // https://github.com/floodyberry/curve25519-donna and
5 // https://github.com/floodyberry/ed25519-donna.
6 
7 // The curve25519 and ed25519 source files multiplex different repos and
8 // architectures using namespaces. The repos are Andrew Moon's
9 // curve25519-donna and ed25519-donna. The architectures are 32-bit, 64-bit
10 // and SSE. For example, 32-bit x25519 uses symbols from Donna::X25519 and
11 // Donna::Arch32.
12 
13 // A fair amount of duplication happens below, but we could not directly
14 // use curve25519 for both x25519 and ed25519. A close examination reveals
15 // slight differences in the implementation. For example, look at the
16 // two curve25519_sub functions.
17 
18 // If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
19 // https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
20 
21 #include "pch.h"
22 
23 #include "config.h"
24 #include "donna.h"
25 #include "secblock.h"
26 #include "sha.h"
27 #include "misc.h"
28 #include "cpu.h"
29 
30 // Squash MS LNK4221 and libtool warnings
31 extern const char DONNA32_FNAME[] = __FILE__;
32 
33 #if defined(CRYPTOPP_CURVE25519_32BIT)
34 
35 #include "donna_32.h"
36 
37 ANONYMOUS_NAMESPACE_BEGIN
38 
39 using CryptoPP::byte;
40 using CryptoPP::word32;
41 using CryptoPP::GetWord;
42 using CryptoPP::PutWord;
44 
45 inline word32 U8TO32_LE(const byte* p)
46 {
47  return GetWord<word32>(false, LITTLE_ENDIAN_ORDER, p);
48 }
49 
50 inline void U32TO8_LE(byte* p, word32 w)
51 {
52  PutWord(false, LITTLE_ENDIAN_ORDER, p, w);
53 }
54 
55 ANONYMOUS_NAMESPACE_END
56 
57 NAMESPACE_BEGIN(CryptoPP)
58 NAMESPACE_BEGIN(Donna)
59 NAMESPACE_BEGIN(X25519)
60 ANONYMOUS_NAMESPACE_BEGIN
61 
62 using CryptoPP::byte;
63 using CryptoPP::word32;
64 using CryptoPP::sword32;
65 using CryptoPP::word64;
66 using CryptoPP::sword64;
67 
68 using CryptoPP::GetBlock;
70 
71 // Bring in all the symbols from the 32-bit header
72 using namespace CryptoPP::Donna::Arch32;
73 
74 /* out = in */
75 inline void
76 curve25519_copy(bignum25519 out, const bignum25519 in) {
77  out[0] = in[0]; out[1] = in[1];
78  out[2] = in[2]; out[3] = in[3];
79  out[4] = in[4]; out[5] = in[5];
80  out[6] = in[6]; out[7] = in[7];
81  out[8] = in[8]; out[9] = in[9];
82 }
83 
84 /* out = a + b */
85 inline void
86 curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
87  out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
88  out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
89  out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
90  out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
91  out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
92 }
93 
94 /* out = a - b */
95 inline void
96 curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
97  word32 c;
98  out[0] = 0x7ffffda + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
99  out[1] = 0x3fffffe + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
100  out[2] = 0x7fffffe + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
101  out[3] = 0x3fffffe + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
102  out[4] = 0x7fffffe + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
103  out[5] = 0x3fffffe + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
104  out[6] = 0x7fffffe + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
105  out[7] = 0x3fffffe + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
106  out[8] = 0x7fffffe + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
107  out[9] = 0x3fffffe + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
108  out[0] += 19 * c;
109 }
110 
111 /* out = in * scalar */
112 inline void
113 curve25519_scalar_product(bignum25519 out, const bignum25519 in, const word32 scalar) {
114  word64 a;
115  word32 c;
116  a = mul32x32_64(in[0], scalar); out[0] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
117  a = mul32x32_64(in[1], scalar) + c; out[1] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
118  a = mul32x32_64(in[2], scalar) + c; out[2] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
119  a = mul32x32_64(in[3], scalar) + c; out[3] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
120  a = mul32x32_64(in[4], scalar) + c; out[4] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
121  a = mul32x32_64(in[5], scalar) + c; out[5] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
122  a = mul32x32_64(in[6], scalar) + c; out[6] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
123  a = mul32x32_64(in[7], scalar) + c; out[7] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
124  a = mul32x32_64(in[8], scalar) + c; out[8] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
125  a = mul32x32_64(in[9], scalar) + c; out[9] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
126  out[0] += c * 19;
127 }
128 
129 /* out = a * b */
130 inline void
131 curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
132  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
133  word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
134  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
135  word32 p;
136 
137  r0 = b[0]; r1 = b[1]; r2 = b[2]; r3 = b[3]; r4 = b[4];
138  r5 = b[5]; r6 = b[6]; r7 = b[7]; r8 = b[8]; r9 = b[9];
139 
140  s0 = a[0]; s1 = a[1]; s2 = a[2]; s3 = a[3]; s4 = a[4];
141  s5 = a[5]; s6 = a[6]; s7 = a[7]; s8 = a[8]; s9 = a[9];
142 
143  m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
144  m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
145  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);
146  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);
147  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);
148 
149  r1 *= 2; r3 *= 2; r5 *= 2; r7 *= 2;
150 
151  m0 = mul32x32_64(r0, s0);
152  m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
153  m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
154  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);
155  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);
156 
157  r1 *= 19; r2 *= 19;
158  r3 = (r3 / 2) * 19;
159  r4 *= 19;
160  r5 = (r5 / 2) * 19;
161  r6 *= 19;
162  r7 = (r7 / 2) * 19;
163  r8 *= 19; r9 *= 19;
164 
165  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));
166  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));
167  m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
168  m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
169 
170  r3 *= 2; r5 *= 2; r7 *= 2; r9 *= 2;
171 
172  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));
173  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));
174  m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
175  m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
176  m8 += (mul32x32_64(r9, s9));
177 
178  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
179  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
180  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
181  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
182  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
183  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
184  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
185  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
186  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
187  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
188  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
189  r1 += p;
190 
191  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
192  out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
193 }
194 
195 /* out = in * in */
196 inline void
197 curve25519_square(bignum25519 out, const bignum25519 in) {
198  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
199  word32 d6,d7,d8,d9;
200  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
201  word32 p;
202 
203  r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
204  r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
205 
206  m0 = mul32x32_64(r0, r0);
207  r0 *= 2;
208  m1 = mul32x32_64(r0, r1);
209  m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
210  r1 *= 2;
211  m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
212  m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
213  r2 *= 2;
214  m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
215  m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
216  r3 *= 2;
217  m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
218  m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
219  m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
220 
221  d6 = r6 * 19; d7 = r7 * 2 * 19;
222  d8 = r8 * 19; d9 = r9 * 2 * 19;
223 
224  m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
225  m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
226  m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
227  m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
228  m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
229  m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
230  m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
231  m7 += (mul32x32_64(d9, r8 ));
232  m8 += (mul32x32_64(d9, r9 ));
233 
234  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
235  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
236  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
237  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
238  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
239  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
240  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
241  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
242  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
243  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
244  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
245  r1 += p;
246 
247  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
248  out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
249 }
250 
251 /* out = in^(2 * count) */
252 void
253 curve25519_square_times(bignum25519 out, const bignum25519 in, int count) {
254  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
255  word32 d6,d7,d8,d9;
256  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
257  word32 p;
258 
259  r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
260  r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
261 
262  do {
263  m0 = mul32x32_64(r0, r0);
264  r0 *= 2;
265  m1 = mul32x32_64(r0, r1);
266  m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
267  r1 *= 2;
268  m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
269  m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
270  r2 *= 2;
271  m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
272  m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
273  r3 *= 2;
274  m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
275  m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
276  m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
277 
278  d6 = r6 * 19; d7 = r7 * 2 * 19;
279  d8 = r8 * 19; d9 = r9 * 2 * 19;
280 
281  m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
282  m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
283  m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
284  m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
285  m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
286  m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
287  m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
288  m7 += (mul32x32_64(d9, r8 ));
289  m8 += (mul32x32_64(d9, r9 ));
290 
291  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
292  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
293  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
294  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
295  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
296  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
297  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
298  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
299  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
300  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
301  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
302  r1 += p;
303  } while (--count);
304 
305  out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
306  out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
307 }
308 
309 /* Take a little-endian, 32-byte number and expand it into polynomial form */
310 void
311 curve25519_expand(bignum25519 out, const byte in[32]) {
312  word32 x0,x1,x2,x3,x4,x5,x6,x7;
314  block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
315 
316  out[0] = ( x0 ) & reduce_mask_26;
317  out[1] = ((((word64)x1 << 32) | x0) >> 26) & reduce_mask_25;
318  out[2] = ((((word64)x2 << 32) | x1) >> 19) & reduce_mask_26;
319  out[3] = ((((word64)x3 << 32) | x2) >> 13) & reduce_mask_25;
320  out[4] = (( x3) >> 6) & reduce_mask_26;
321  out[5] = ( x4 ) & reduce_mask_25;
322  out[6] = ((((word64)x5 << 32) | x4) >> 25) & reduce_mask_26;
323  out[7] = ((((word64)x6 << 32) | x5) >> 19) & reduce_mask_25;
324  out[8] = ((((word64)x7 << 32) | x6) >> 12) & reduce_mask_26;
325  out[9] = (( x7) >> 6) & reduce_mask_25; /* ignore the top bit */
326 }
327 
328 /* Take a fully reduced polynomial form number and contract it into a little-endian, 32-byte array */
329 void
330 curve25519_contract(byte out[32], const bignum25519 in) {
331  bignum25519 f;
332  curve25519_copy(f, in);
333 
334  #define carry_pass() \
335  f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
336  f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
337  f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
338  f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
339  f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
340  f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
341  f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
342  f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
343  f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
344 
345  #define carry_pass_full() \
346  carry_pass() \
347  f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
348 
349  #define carry_pass_final() \
350  carry_pass() \
351  f[9] &= reduce_mask_25;
352 
353  carry_pass_full()
354  carry_pass_full()
355 
356  /* now t is between 0 and 2^255-1, properly carried. */
357  /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
358  f[0] += 19;
359  carry_pass_full()
360 
361  /* now between 19 and 2^255-1 in both cases, and offset by 19. */
362  f[0] += (1 << 26) - 19;
363  f[1] += (1 << 25) - 1;
364  f[2] += (1 << 26) - 1;
365  f[3] += (1 << 25) - 1;
366  f[4] += (1 << 26) - 1;
367  f[5] += (1 << 25) - 1;
368  f[6] += (1 << 26) - 1;
369  f[7] += (1 << 25) - 1;
370  f[8] += (1 << 26) - 1;
371  f[9] += (1 << 25) - 1;
372 
373  /* now between 2^255 and 2^256-20, and offset by 2^255. */
374  carry_pass_final()
375 
376  #undef carry_pass
377  #undef carry_full
378  #undef carry_final
379 
380  f[1] <<= 2;
381  f[2] <<= 3;
382  f[3] <<= 5;
383  f[4] <<= 6;
384  f[6] <<= 1;
385  f[7] <<= 3;
386  f[8] <<= 4;
387  f[9] <<= 6;
388 
389  #define F(i, s) \
390  out[s+0] |= (byte)( f[i] & 0xff); \
391  out[s+1] = (byte)((f[i] >> 8) & 0xff); \
392  out[s+2] = (byte)((f[i] >> 16) & 0xff); \
393  out[s+3] = (byte)((f[i] >> 24) & 0xff);
394 
395  out[0] = out[16] = 0;
396  F(0,0); F(1,3);
397  F(2,6); F(3,9);
398  F(4,12); F(5,16);
399  F(6,19); F(7,22);
400  F(8,25); F(9,28);
401  #undef F
402 }
403 
404 inline void
405 curve25519_swap_conditional(bignum25519 x, bignum25519 qpx, word32 iswap) {
406  const word32 swap = (word32)(-(sword32)iswap);
407  word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
408 
409  x0 = swap & (x[0] ^ qpx[0]); x[0] ^= x0; qpx[0] ^= x0;
410  x1 = swap & (x[1] ^ qpx[1]); x[1] ^= x1; qpx[1] ^= x1;
411  x2 = swap & (x[2] ^ qpx[2]); x[2] ^= x2; qpx[2] ^= x2;
412  x3 = swap & (x[3] ^ qpx[3]); x[3] ^= x3; qpx[3] ^= x3;
413  x4 = swap & (x[4] ^ qpx[4]); x[4] ^= x4; qpx[4] ^= x4;
414  x5 = swap & (x[5] ^ qpx[5]); x[5] ^= x5; qpx[5] ^= x5;
415  x6 = swap & (x[6] ^ qpx[6]); x[6] ^= x6; qpx[6] ^= x6;
416  x7 = swap & (x[7] ^ qpx[7]); x[7] ^= x7; qpx[7] ^= x7;
417  x8 = swap & (x[8] ^ qpx[8]); x[8] ^= x8; qpx[8] ^= x8;
418  x9 = swap & (x[9] ^ qpx[9]); x[9] ^= x9; qpx[9] ^= x9;
419 }
420 
421 /*
422  * In: b = 2^5 - 2^0
423  * Out: b = 2^250 - 2^0
424  */
425 void
426 curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
427  ALIGN(16) bignum25519 t0,c;
428 
429  /* 2^5 - 2^0 */ /* b */
430  /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
431  /* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
432  /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
433  /* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
434  /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
435  /* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
436  /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
437  /* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
438  /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
439  /* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
440  /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
441  /* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
442  /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
443  /* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
444 }
445 
446 /*
447  * z^(p - 2) = z(2^255 - 21)
448  */
449 void
450 curve25519_recip(bignum25519 out, const bignum25519 z) {
451  ALIGN(16) bignum25519 a, t0, b;
452 
453  /* 2 */ curve25519_square(a, z); /* a = 2 */
454  /* 8 */ curve25519_square_times(t0, a, 2);
455  /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
456  /* 11 */ curve25519_mul(a, b, a); /* a = 11 */
457  /* 22 */ curve25519_square(t0, a);
458  /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
459  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
460  /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
461  /* 2^255 - 21 */ curve25519_mul(out, b, a);
462 }
463 
464 ANONYMOUS_NAMESPACE_END
465 NAMESPACE_END // X25519
466 NAMESPACE_END // Donna
467 NAMESPACE_END // CryptoPP
468 
469 //******************************* ed25519 *******************************//
470 
471 NAMESPACE_BEGIN(CryptoPP)
472 NAMESPACE_BEGIN(Donna)
473 NAMESPACE_BEGIN(Ed25519)
474 ANONYMOUS_NAMESPACE_BEGIN
475 
476 using CryptoPP::byte;
477 using CryptoPP::word32;
478 using CryptoPP::sword32;
479 using CryptoPP::word64;
480 using CryptoPP::sword64;
481 
482 using CryptoPP::GetBlock;
483 using CryptoPP::LittleEndian;
484 
485 using CryptoPP::SHA512;
486 
487 // Bring in all the symbols from the 32-bit header
488 using namespace CryptoPP::Donna::Arch32;
489 
490 /* out = in */
491 inline void
492 curve25519_copy(bignum25519 out, const bignum25519 in) {
493  out[0] = in[0]; out[1] = in[1];
494  out[2] = in[2]; out[3] = in[3];
495  out[4] = in[4]; out[5] = in[5];
496  out[6] = in[6]; out[7] = in[7];
497  out[8] = in[8]; out[9] = in[9];
498 }
499 
500 /* out = a + b */
501 inline void
502 curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
503  out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
504  out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
505  out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
506  out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
507  out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
508 }
509 
510 inline void
511 curve25519_add_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
512  word32 c;
513  out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
514  out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
515  out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
516  out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
517  out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
518  out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
519  out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
520  out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
521  out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
522  out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
523  out[0] += 19 * c;
524 }
525 
526 inline void
527 curve25519_add_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
528  word32 c;
529  out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
530  out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
531  out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
532  out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
533  out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
534  out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
535  out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
536  out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
537  out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
538  out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
539  out[0] += 19 * c;
540 }
541 
542 /* out = a - b */
543 inline void
544 curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
545  word32 c;
546  out[0] = twoP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
547  out[1] = twoP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
548  out[2] = twoP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
549  out[3] = twoP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
550  out[4] = twoP2468 + a[4] - b[4] + c;
551  out[5] = twoP13579 + a[5] - b[5] ;
552  out[6] = twoP2468 + a[6] - b[6] ;
553  out[7] = twoP13579 + a[7] - b[7] ;
554  out[8] = twoP2468 + a[8] - b[8] ;
555  out[9] = twoP13579 + a[9] - b[9] ;
556 }
557 
558 /* out = a - b, where a is the result of a basic op (add,sub) */
559 inline void
560 curve25519_sub_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
561  word32 c;
562  out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
563  out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
564  out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
565  out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
566  out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
567  out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
568  out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
569  out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
570  out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
571  out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
572  out[0] += 19 * c;
573 }
574 
575 inline void
576 curve25519_sub_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
577  word32 c;
578  out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
579  out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
580  out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
581  out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
582  out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
583  out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
584  out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
585  out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
586  out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
587  out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
588  out[0] += 19 * c;
589 }
590 
591 /* out = -a */
592 inline void
593 curve25519_neg(bignum25519 out, const bignum25519 a) {
594  word32 c;
595  out[0] = twoP0 - a[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
596  out[1] = twoP13579 - a[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
597  out[2] = twoP2468 - a[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
598  out[3] = twoP13579 - a[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
599  out[4] = twoP2468 - a[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
600  out[5] = twoP13579 - a[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
601  out[6] = twoP2468 - a[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
602  out[7] = twoP13579 - a[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
603  out[8] = twoP2468 - a[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
604  out[9] = twoP13579 - a[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
605  out[0] += 19 * c;
606 }
607 
608 /* out = a * b */
609 void
610 curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
611  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
612  word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
613  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
614  word32 p;
615 
616  r0 = b[0]; r1 = b[1];
617  r2 = b[2]; r3 = b[3];
618  r4 = b[4]; r5 = b[5];
619  r6 = b[6]; r7 = b[7];
620  r8 = b[8]; r9 = b[9];
621 
622  s0 = a[0]; s1 = a[1];
623  s2 = a[2]; s3 = a[3];
624  s4 = a[4]; s5 = a[5];
625  s6 = a[6]; s7 = a[7];
626  s8 = a[8]; s9 = a[9];
627 
628  m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
629  m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
630  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);
631  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);
632  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);
633 
634  r1 *= 2; r3 *= 2;
635  r5 *= 2; r7 *= 2;
636 
637  m0 = mul32x32_64(r0, s0);
638  m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
639  m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
640  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);
641  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);
642 
643  r1 *= 19; r2 *= 19;
644  r3 = (r3 / 2) * 19;
645  r4 *= 19;
646  r5 = (r5 / 2) * 19;
647  r6 *= 19;
648  r7 = (r7 / 2) * 19;
649  r8 *= 19; r9 *= 19;
650 
651  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));
652  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));
653  m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
654  m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
655 
656  r3 *= 2; r5 *= 2;
657  r7 *= 2; r9 *= 2;
658 
659  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));
660  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));
661  m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
662  m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
663  m8 += (mul32x32_64(r9, s9));
664 
665  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
666  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
667  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
668  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
669  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
670  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
671  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
672  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
673  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
674  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
675  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
676  r1 += p;
677 
678  out[0] = r0; out[1] = r1;
679  out[2] = r2; out[3] = r3;
680  out[4] = r4; out[5] = r5;
681  out[6] = r6; out[7] = r7;
682  out[8] = r8; out[9] = r9;
683 }
684 
685 /* out = in*in */
686 void
687 curve25519_square(bignum25519 out, const bignum25519 in) {
688  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
689  word32 d6,d7,d8,d9;
690  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
691  word32 p;
692 
693  r0 = in[0]; r1 = in[1];
694  r2 = in[2]; r3 = in[3];
695  r4 = in[4]; r5 = in[5];
696  r6 = in[6]; r7 = in[7];
697  r8 = in[8]; r9 = in[9];
698 
699  m0 = mul32x32_64(r0, r0);
700  r0 *= 2;
701  m1 = mul32x32_64(r0, r1);
702  m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
703  r1 *= 2;
704  m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
705  m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
706  r2 *= 2;
707  m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
708  m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
709  r3 *= 2;
710  m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
711  m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
712  m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
713 
714  d6 = r6 * 19;
715  d7 = r7 * 2 * 19;
716  d8 = r8 * 19;
717  d9 = r9 * 2 * 19;
718 
719  m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
720  m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
721  m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
722  m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
723  m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
724  m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
725  m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
726  m7 += (mul32x32_64(d9, r8 ));
727  m8 += (mul32x32_64(d9, r9 ));
728 
729  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
730  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
731  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
732  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
733  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
734  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
735  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
736  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
737  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
738  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
739  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
740  r1 += p;
741 
742  out[0] = r0; out[1] = r1;
743  out[2] = r2; out[3] = r3;
744  out[4] = r4; out[5] = r5;
745  out[6] = r6; out[7] = r7;
746  out[8] = r8; out[9] = r9;
747 }
748 
749 /* out = in ^ (2 * count) */
750 void
751 curve25519_square_times(bignum25519 out, const bignum25519 in, int count) {
752  word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
753  word32 d6,d7,d8,d9,p;
754  word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
755 
756  r0 = in[0]; r1 = in[1];
757  r2 = in[2]; r3 = in[3];
758  r4 = in[4]; r5 = in[5];
759  r6 = in[6]; r7 = in[7];
760  r8 = in[8]; r9 = in[9];
761 
762  do {
763  m0 = mul32x32_64(r0, r0);
764  r0 *= 2;
765  m1 = mul32x32_64(r0, r1);
766  m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
767  r1 *= 2;
768  m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
769  m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
770  r2 *= 2;
771  m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
772  m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
773  r3 *= 2;
774  m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
775  m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
776  m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
777 
778  d6 = r6 * 19;
779  d7 = r7 * 2 * 19;
780  d8 = r8 * 19;
781  d9 = r9 * 2 * 19;
782 
783  m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
784  m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
785  m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
786  m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
787  m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
788  m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
789  m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
790  m7 += (mul32x32_64(d9, r8 ));
791  m8 += (mul32x32_64(d9, r9 ));
792 
793  r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
794  m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
795  m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
796  m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
797  m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
798  m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
799  m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
800  m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
801  m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
802  m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
803  m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
804  r1 += p;
805  } while (--count);
806 
807  out[0] = r0; out[1] = r1;
808  out[2] = r2; out[3] = r3;
809  out[4] = r4; out[5] = r5;
810  out[6] = r6; out[7] = r7;
811  out[8] = r8; out[9] = r9;
812 }
813 
814 /* Take a little-endian, 32-byte number and expand it into polynomial form */
815 void
816 curve25519_expand(bignum25519 out, const byte in[32]) {
817  word32 x0,x1,x2,x3,x4,x5,x6,x7;
819  block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
820 
821  out[0] = ( x0 ) & 0x3ffffff;
822  out[1] = ((((word64)x1 << 32) | x0) >> 26) & 0x1ffffff;
823  out[2] = ((((word64)x2 << 32) | x1) >> 19) & 0x3ffffff;
824  out[3] = ((((word64)x3 << 32) | x2) >> 13) & 0x1ffffff;
825  out[4] = (( x3) >> 6) & 0x3ffffff;
826  out[5] = ( x4 ) & 0x1ffffff;
827  out[6] = ((((word64)x5 << 32) | x4) >> 25) & 0x3ffffff;
828  out[7] = ((((word64)x6 << 32) | x5) >> 19) & 0x1ffffff;
829  out[8] = ((((word64)x7 << 32) | x6) >> 12) & 0x3ffffff;
830  out[9] = (( x7) >> 6) & 0x1ffffff;
831 }
832 
833 /* Take a fully reduced polynomial form number and contract it into a
834  * little-endian, 32-byte array
835  */
836 void
837 curve25519_contract(byte out[32], const bignum25519 in) {
838  bignum25519 f;
839  curve25519_copy(f, in);
840 
841  #define carry_pass() \
842  f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
843  f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
844  f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
845  f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
846  f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
847  f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
848  f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
849  f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
850  f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
851 
852  #define carry_pass_full() \
853  carry_pass() \
854  f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
855 
856  #define carry_pass_final() \
857  carry_pass() \
858  f[9] &= reduce_mask_25;
859 
860  carry_pass_full()
861  carry_pass_full()
862 
863  /* now t is between 0 and 2^255-1, properly carried. */
864  /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
865  f[0] += 19;
866  carry_pass_full()
867 
868  /* now between 19 and 2^255-1 in both cases, and offset by 19. */
869  f[0] += (reduce_mask_26 + 1) - 19;
870  f[1] += (reduce_mask_25 + 1) - 1;
871  f[2] += (reduce_mask_26 + 1) - 1;
872  f[3] += (reduce_mask_25 + 1) - 1;
873  f[4] += (reduce_mask_26 + 1) - 1;
874  f[5] += (reduce_mask_25 + 1) - 1;
875  f[6] += (reduce_mask_26 + 1) - 1;
876  f[7] += (reduce_mask_25 + 1) - 1;
877  f[8] += (reduce_mask_26 + 1) - 1;
878  f[9] += (reduce_mask_25 + 1) - 1;
879 
880  /* now between 2^255 and 2^256-20, and offset by 2^255. */
881  carry_pass_final()
882 
883  #undef carry_pass
884  #undef carry_full
885  #undef carry_final
886 
887  f[1] <<= 2; f[2] <<= 3;
888  f[3] <<= 5; f[4] <<= 6;
889  f[6] <<= 1; f[7] <<= 3;
890  f[8] <<= 4; f[9] <<= 6;
891 
892  #define F(i, s) \
893  out[s+0] |= (byte)( f[i] & 0xff); \
894  out[s+1] = (byte)((f[i] >> 8) & 0xff); \
895  out[s+2] = (byte)((f[i] >> 16) & 0xff); \
896  out[s+3] = (byte)((f[i] >> 24) & 0xff);
897 
898  out[0] = out[16] = 0;
899  F(0,0); F(1,3);
900  F(2,6); F(3,9);
901  F(4,12); F(5,16);
902  F(6,19); F(7,22);
903  F(8,25); F(9,28);
904  #undef F
905 }
906 
907 /* out = (flag) ? in : out */
908 inline void
909 curve25519_move_conditional_bytes(byte out[96], const byte in[96], word32 flag) {
910  const word32 nb = flag - 1, b = ~nb;
911  const word32 *inl = (const word32 *)in;
912  word32 *outl = (word32 *)out;
913  outl[0] = (outl[0] & nb) | (inl[0] & b);
914  outl[1] = (outl[1] & nb) | (inl[1] & b);
915  outl[2] = (outl[2] & nb) | (inl[2] & b);
916  outl[3] = (outl[3] & nb) | (inl[3] & b);
917  outl[4] = (outl[4] & nb) | (inl[4] & b);
918  outl[5] = (outl[5] & nb) | (inl[5] & b);
919  outl[6] = (outl[6] & nb) | (inl[6] & b);
920  outl[7] = (outl[7] & nb) | (inl[7] & b);
921  outl[8] = (outl[8] & nb) | (inl[8] & b);
922  outl[9] = (outl[9] & nb) | (inl[9] & b);
923  outl[10] = (outl[10] & nb) | (inl[10] & b);
924  outl[11] = (outl[11] & nb) | (inl[11] & b);
925  outl[12] = (outl[12] & nb) | (inl[12] & b);
926  outl[13] = (outl[13] & nb) | (inl[13] & b);
927  outl[14] = (outl[14] & nb) | (inl[14] & b);
928  outl[15] = (outl[15] & nb) | (inl[15] & b);
929  outl[16] = (outl[16] & nb) | (inl[16] & b);
930  outl[17] = (outl[17] & nb) | (inl[17] & b);
931  outl[18] = (outl[18] & nb) | (inl[18] & b);
932  outl[19] = (outl[19] & nb) | (inl[19] & b);
933  outl[20] = (outl[20] & nb) | (inl[20] & b);
934  outl[21] = (outl[21] & nb) | (inl[21] & b);
935  outl[22] = (outl[22] & nb) | (inl[22] & b);
936  outl[23] = (outl[23] & nb) | (inl[23] & b);
937 }
938 
939 /* if (iswap) swap(a, b) */
940 inline void
941 curve25519_swap_conditional(bignum25519 a, bignum25519 b, word32 iswap) {
942  const word32 swap = (word32)(-(sword32)iswap);
943  word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
944 
945  x0 = swap & (a[0] ^ b[0]); a[0] ^= x0; b[0] ^= x0;
946  x1 = swap & (a[1] ^ b[1]); a[1] ^= x1; b[1] ^= x1;
947  x2 = swap & (a[2] ^ b[2]); a[2] ^= x2; b[2] ^= x2;
948  x3 = swap & (a[3] ^ b[3]); a[3] ^= x3; b[3] ^= x3;
949  x4 = swap & (a[4] ^ b[4]); a[4] ^= x4; b[4] ^= x4;
950  x5 = swap & (a[5] ^ b[5]); a[5] ^= x5; b[5] ^= x5;
951  x6 = swap & (a[6] ^ b[6]); a[6] ^= x6; b[6] ^= x6;
952  x7 = swap & (a[7] ^ b[7]); a[7] ^= x7; b[7] ^= x7;
953  x8 = swap & (a[8] ^ b[8]); a[8] ^= x8; b[8] ^= x8;
954  x9 = swap & (a[9] ^ b[9]); a[9] ^= x9; b[9] ^= x9;
955 }
956 
957 /*
958  * In: b = 2^5 - 2^0
959  * Out: b = 2^250 - 2^0
960  */
961 void
962 curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
963  ALIGN(16) bignum25519 t0,c;
964 
965  /* 2^5 - 2^0 */ /* b */
966  /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
967  /* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
968  /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
969  /* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
970  /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
971  /* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
972  /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
973  /* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
974  /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
975  /* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
976  /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
977  /* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
978  /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
979  /* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
980 }
981 
982 /*
983  * z^(p - 2) = z(2^255 - 21)
984  */
985 void
986 curve25519_recip(bignum25519 out, const bignum25519 z) {
987  ALIGN(16) bignum25519 a,t0,b;
988 
989  /* 2 */ curve25519_square_times(a, z, 1); /* a = 2 */
990  /* 8 */ curve25519_square_times(t0, a, 2);
991  /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
992  /* 11 */ curve25519_mul(a, b, a); /* a = 11 */
993  /* 22 */ curve25519_square_times(t0, a, 1);
994  /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
995  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
996  /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
997  /* 2^255 - 21 */ curve25519_mul(out, b, a);
998 }
999 
1000 /*
1001  * z^((p-5)/8) = z^(2^252 - 3)
1002  */
1003 void
1004 curve25519_pow_two252m3(bignum25519 two252m3, const bignum25519 z) {
1005  ALIGN(16) bignum25519 b,c,t0;
1006 
1007  /* 2 */ curve25519_square_times(c, z, 1); /* c = 2 */
1008  /* 8 */ curve25519_square_times(t0, c, 2); /* t0 = 8 */
1009  /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
1010  /* 11 */ curve25519_mul(c, b, c); /* c = 11 */
1011  /* 22 */ curve25519_square_times(t0, c, 1);
1012  /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
1013  /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
1014  /* 2^252 - 2^2 */ curve25519_square_times(b, b, 2);
1015  /* 2^252 - 3 */ curve25519_mul(two252m3, b, z);
1016 }
1017 
1018 inline void
1019 ed25519_hash(byte *hash, const byte *in, size_t inlen) {
1020  SHA512().CalculateDigest(hash, in, inlen);
1021 }
1022 
1023 inline void
1024 ed25519_extsk(hash_512bits extsk, const byte sk[32]) {
1025  ed25519_hash(extsk, sk, 32);
1026  extsk[0] &= 248;
1027  extsk[31] &= 127;
1028  extsk[31] |= 64;
1029 }
1030 
1031 void
1032 ed25519_hram(hash_512bits hram, const byte RS[64], const byte pk[32], const unsigned char *m, size_t mlen) {
1033  SHA512 hash;
1034  hash.Update(RS, 32);
1035  hash.Update(pk, 32);
1036  hash.Update(m, mlen);
1037  hash.Final(hram);
1038 }
1039 
1040 inline bignum256modm_element_t
1041 lt_modm(bignum256modm_element_t a, bignum256modm_element_t b) {
1042  return (a - b) >> 31;
1043 }
1044 
1045 /* see HAC, Alg. 14.42 Step 4 */
1046 void
1047 reduce256_modm(bignum256modm r) {
1048  bignum256modm t;
1049  bignum256modm_element_t b = 0, pb, mask;
1050 
1051  /* t = r - m */
1052  pb = 0;
1053  pb += modm_m[0]; b = lt_modm(r[0], pb); t[0] = (r[0] - pb + (b << 30)); pb = b;
1054  pb += modm_m[1]; b = lt_modm(r[1], pb); t[1] = (r[1] - pb + (b << 30)); pb = b;
1055  pb += modm_m[2]; b = lt_modm(r[2], pb); t[2] = (r[2] - pb + (b << 30)); pb = b;
1056  pb += modm_m[3]; b = lt_modm(r[3], pb); t[3] = (r[3] - pb + (b << 30)); pb = b;
1057  pb += modm_m[4]; b = lt_modm(r[4], pb); t[4] = (r[4] - pb + (b << 30)); pb = b;
1058  pb += modm_m[5]; b = lt_modm(r[5], pb); t[5] = (r[5] - pb + (b << 30)); pb = b;
1059  pb += modm_m[6]; b = lt_modm(r[6], pb); t[6] = (r[6] - pb + (b << 30)); pb = b;
1060  pb += modm_m[7]; b = lt_modm(r[7], pb); t[7] = (r[7] - pb + (b << 30)); pb = b;
1061  pb += modm_m[8]; b = lt_modm(r[8], pb); t[8] = (r[8] - pb + (b << 16));
1062 
1063  /* keep r if r was smaller than m */
1064  mask = b - 1;
1065  r[0] ^= mask & (r[0] ^ t[0]);
1066  r[1] ^= mask & (r[1] ^ t[1]);
1067  r[2] ^= mask & (r[2] ^ t[2]);
1068  r[3] ^= mask & (r[3] ^ t[3]);
1069  r[4] ^= mask & (r[4] ^ t[4]);
1070  r[5] ^= mask & (r[5] ^ t[5]);
1071  r[6] ^= mask & (r[6] ^ t[6]);
1072  r[7] ^= mask & (r[7] ^ t[7]);
1073  r[8] ^= mask & (r[8] ^ t[8]);
1074 }
1075 
1076 /* Barrett reduction, see HAC, Alg. 14.42 */
1077 void
1078 barrett_reduce256_modm(bignum256modm r, const bignum256modm q1, const bignum256modm r1) {
1079  bignum256modm q3, r2;
1080  word64 c;
1081  bignum256modm_element_t f, b, pb;
1082 
1083  /* q1 = x >> 248 = 264 bits = 9 30 bit elements
1084  q2 = mu * q1
1085  q3 = (q2 / 256(32+1)) = q2 / (2^8)^(32+1) = q2 >> 264
1086  */
1087  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]);
1088  c >>= 30;
1089  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]);
1090  f = (bignum256modm_element_t)c; q3[0] = (f >> 24) & 0x3f; c >>= 30;
1091  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]);
1092  f = (bignum256modm_element_t)c; q3[0] |= (f << 6) & 0x3fffffff; q3[1] = (f >> 24) & 0x3f; c >>= 30;
1093  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]);
1094  f = (bignum256modm_element_t)c; q3[1] |= (f << 6) & 0x3fffffff; q3[2] = (f >> 24) & 0x3f; c >>= 30;
1095  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]);
1096  f = (bignum256modm_element_t)c; q3[2] |= (f << 6) & 0x3fffffff; q3[3] = (f >> 24) & 0x3f; c >>= 30;
1097  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]);
1098  f = (bignum256modm_element_t)c; q3[3] |= (f << 6) & 0x3fffffff; q3[4] = (f >> 24) & 0x3f; c >>= 30;
1099  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]);
1100  f = (bignum256modm_element_t)c; q3[4] |= (f << 6) & 0x3fffffff; q3[5] = (f >> 24) & 0x3f; c >>= 30;
1101  c += mul32x32_64(modm_mu[6], q1[8]) + mul32x32_64(modm_mu[7], q1[7]) + mul32x32_64(modm_mu[8], q1[6]);
1102  f = (bignum256modm_element_t)c; q3[5] |= (f << 6) & 0x3fffffff; q3[6] = (f >> 24) & 0x3f; c >>= 30;
1103  c += mul32x32_64(modm_mu[7], q1[8]) + mul32x32_64(modm_mu[8], q1[7]);
1104  f = (bignum256modm_element_t)c; q3[6] |= (f << 6) & 0x3fffffff; q3[7] = (f >> 24) & 0x3f; c >>= 30;
1105  c += mul32x32_64(modm_mu[8], q1[8]);
1106  f = (bignum256modm_element_t)c; q3[7] |= (f << 6) & 0x3fffffff; q3[8] = (bignum256modm_element_t)(c >> 24);
1107 
1108  /* r1 = (x mod 256^(32+1)) = x mod (2^8)(31+1) = x & ((1 << 264) - 1)
1109  r2 = (q3 * m) mod (256^(32+1)) = (q3 * m) & ((1 << 264) - 1)
1110  */
1111  c = mul32x32_64(modm_m[0], q3[0]);
1112  r2[0] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1113  c += mul32x32_64(modm_m[0], q3[1]) + mul32x32_64(modm_m[1], q3[0]);
1114  r2[1] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1115  c += mul32x32_64(modm_m[0], q3[2]) + mul32x32_64(modm_m[1], q3[1]) + mul32x32_64(modm_m[2], q3[0]);
1116  r2[2] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1117  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]);
1118  r2[3] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1119  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]);
1120  r2[4] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1121  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]);
1122  r2[5] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1123  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]);
1124  r2[6] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1125  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]);
1126  r2[7] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1127  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]);
1128  r2[8] = (bignum256modm_element_t)(c & 0xffffff);
1129 
1130  /* r = r1 - r2
1131  if (r < 0) r += (1 << 264) */
1132  pb = 0;
1133  pb += r2[0]; b = lt_modm(r1[0], pb); r[0] = (r1[0] - pb + (b << 30)); pb = b;
1134  pb += r2[1]; b = lt_modm(r1[1], pb); r[1] = (r1[1] - pb + (b << 30)); pb = b;
1135  pb += r2[2]; b = lt_modm(r1[2], pb); r[2] = (r1[2] - pb + (b << 30)); pb = b;
1136  pb += r2[3]; b = lt_modm(r1[3], pb); r[3] = (r1[3] - pb + (b << 30)); pb = b;
1137  pb += r2[4]; b = lt_modm(r1[4], pb); r[4] = (r1[4] - pb + (b << 30)); pb = b;
1138  pb += r2[5]; b = lt_modm(r1[5], pb); r[5] = (r1[5] - pb + (b << 30)); pb = b;
1139  pb += r2[6]; b = lt_modm(r1[6], pb); r[6] = (r1[6] - pb + (b << 30)); pb = b;
1140  pb += r2[7]; b = lt_modm(r1[7], pb); r[7] = (r1[7] - pb + (b << 30)); pb = b;
1141  pb += r2[8]; b = lt_modm(r1[8], pb); r[8] = (r1[8] - pb + (b << 24));
1142 
1143  reduce256_modm(r);
1144  reduce256_modm(r);
1145 }
1146 
1147 /* addition modulo m */
1148 void
1149 add256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
1150  bignum256modm_element_t c;
1151 
1152  c = x[0] + y[0]; r[0] = c & 0x3fffffff; c >>= 30;
1153  c += x[1] + y[1]; r[1] = c & 0x3fffffff; c >>= 30;
1154  c += x[2] + y[2]; r[2] = c & 0x3fffffff; c >>= 30;
1155  c += x[3] + y[3]; r[3] = c & 0x3fffffff; c >>= 30;
1156  c += x[4] + y[4]; r[4] = c & 0x3fffffff; c >>= 30;
1157  c += x[5] + y[5]; r[5] = c & 0x3fffffff; c >>= 30;
1158  c += x[6] + y[6]; r[6] = c & 0x3fffffff; c >>= 30;
1159  c += x[7] + y[7]; r[7] = c & 0x3fffffff; c >>= 30;
1160  c += x[8] + y[8]; r[8] = c;
1161 
1162  reduce256_modm(r);
1163 }
1164 
1165 /* multiplication modulo m */
1166 void
1167 mul256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
1168  bignum256modm r1, q1;
1169  word64 c;
1170  bignum256modm_element_t f;
1171 
1172  c = mul32x32_64(x[0], y[0]);
1173  f = (bignum256modm_element_t)c; r1[0] = (f & 0x3fffffff); c >>= 30;
1174  c += mul32x32_64(x[0], y[1]) + mul32x32_64(x[1], y[0]);
1175  f = (bignum256modm_element_t)c; r1[1] = (f & 0x3fffffff); c >>= 30;
1176  c += mul32x32_64(x[0], y[2]) + mul32x32_64(x[1], y[1]) + mul32x32_64(x[2], y[0]);
1177  f = (bignum256modm_element_t)c; r1[2] = (f & 0x3fffffff); c >>= 30;
1178  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]);
1179  f = (bignum256modm_element_t)c; r1[3] = (f & 0x3fffffff); c >>= 30;
1180  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]);
1181  f = (bignum256modm_element_t)c; r1[4] = (f & 0x3fffffff); c >>= 30;
1182  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]);
1183  f = (bignum256modm_element_t)c; r1[5] = (f & 0x3fffffff); c >>= 30;
1184  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]);
1185  f = (bignum256modm_element_t)c; r1[6] = (f & 0x3fffffff); c >>= 30;
1186  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]);
1187  f = (bignum256modm_element_t)c; r1[7] = (f & 0x3fffffff); c >>= 30;
1188  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]);
1189  f = (bignum256modm_element_t)c; r1[8] = (f & 0x00ffffff); q1[0] = (f >> 8) & 0x3fffff; c >>= 30;
1190  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]);
1191  f = (bignum256modm_element_t)c; q1[0] = (q1[0] | (f << 22)) & 0x3fffffff; q1[1] = (f >> 8) & 0x3fffff; c >>= 30;
1192  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]);
1193  f = (bignum256modm_element_t)c; q1[1] = (q1[1] | (f << 22)) & 0x3fffffff; q1[2] = (f >> 8) & 0x3fffff; c >>= 30;
1194  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]);
1195  f = (bignum256modm_element_t)c; q1[2] = (q1[2] | (f << 22)) & 0x3fffffff; q1[3] = (f >> 8) & 0x3fffff; c >>= 30;
1196  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]);
1197  f = (bignum256modm_element_t)c; q1[3] = (q1[3] | (f << 22)) & 0x3fffffff; q1[4] = (f >> 8) & 0x3fffff; c >>= 30;
1198  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]);
1199  f = (bignum256modm_element_t)c; q1[4] = (q1[4] | (f << 22)) & 0x3fffffff; q1[5] = (f >> 8) & 0x3fffff; c >>= 30;
1200  c += mul32x32_64(x[6], y[8]) + mul32x32_64(x[7], y[7]) + mul32x32_64(x[8], y[6]);
1201  f = (bignum256modm_element_t)c; q1[5] = (q1[5] | (f << 22)) & 0x3fffffff; q1[6] = (f >> 8) & 0x3fffff; c >>= 30;
1202  c += mul32x32_64(x[7], y[8]) + mul32x32_64(x[8], y[7]);
1203  f = (bignum256modm_element_t)c; q1[6] = (q1[6] | (f << 22)) & 0x3fffffff; q1[7] = (f >> 8) & 0x3fffff; c >>= 30;
1204  c += mul32x32_64(x[8], y[8]);
1205  f = (bignum256modm_element_t)c; q1[7] = (q1[7] | (f << 22)) & 0x3fffffff; q1[8] = (f >> 8) & 0x3fffff;
1206 
1207  barrett_reduce256_modm(r, q1, r1);
1208 }
1209 
1210 void
1211 expand256_modm(bignum256modm out, const byte *in, size_t len) {
1212  byte work[64] = {0};
1213  bignum256modm_element_t x[16];
1214  bignum256modm q1;
1215 
1216  memcpy(work, in, len);
1217  x[0] = U8TO32_LE(work + 0);
1218  x[1] = U8TO32_LE(work + 4);
1219  x[2] = U8TO32_LE(work + 8);
1220  x[3] = U8TO32_LE(work + 12);
1221  x[4] = U8TO32_LE(work + 16);
1222  x[5] = U8TO32_LE(work + 20);
1223  x[6] = U8TO32_LE(work + 24);
1224  x[7] = U8TO32_LE(work + 28);
1225  x[8] = U8TO32_LE(work + 32);
1226  x[9] = U8TO32_LE(work + 36);
1227  x[10] = U8TO32_LE(work + 40);
1228  x[11] = U8TO32_LE(work + 44);
1229  x[12] = U8TO32_LE(work + 48);
1230  x[13] = U8TO32_LE(work + 52);
1231  x[14] = U8TO32_LE(work + 56);
1232  x[15] = U8TO32_LE(work + 60);
1233 
1234  /* r1 = (x mod 256^(32+1)) = x mod (2^8)(31+1) = x & ((1 << 264) - 1) */
1235  out[0] = ( x[0]) & 0x3fffffff;
1236  out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1237  out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1238  out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1239  out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1240  out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1241  out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1242  out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1243  out[8] = ((x[ 7] >> 16) | (x[ 8] << 16)) & 0x00ffffff;
1244 
1245  /* 8*31 = 248 bits, no need to reduce */
1246  if (len < 32)
1247  return;
1248 
1249  /* q1 = x >> 248 = 264 bits = 9 30 bit elements */
1250  q1[0] = ((x[ 7] >> 24) | (x[ 8] << 8)) & 0x3fffffff;
1251  q1[1] = ((x[ 8] >> 22) | (x[ 9] << 10)) & 0x3fffffff;
1252  q1[2] = ((x[ 9] >> 20) | (x[10] << 12)) & 0x3fffffff;
1253  q1[3] = ((x[10] >> 18) | (x[11] << 14)) & 0x3fffffff;
1254  q1[4] = ((x[11] >> 16) | (x[12] << 16)) & 0x3fffffff;
1255  q1[5] = ((x[12] >> 14) | (x[13] << 18)) & 0x3fffffff;
1256  q1[6] = ((x[13] >> 12) | (x[14] << 20)) & 0x3fffffff;
1257  q1[7] = ((x[14] >> 10) | (x[15] << 22)) & 0x3fffffff;
1258  q1[8] = ((x[15] >> 8) );
1259 
1260  barrett_reduce256_modm(out, q1, out);
1261 }
1262 
1263 void
1264 expand_raw256_modm(bignum256modm out, const byte in[32]) {
1265  bignum256modm_element_t x[8];
1266 
1267  x[0] = U8TO32_LE(in + 0);
1268  x[1] = U8TO32_LE(in + 4);
1269  x[2] = U8TO32_LE(in + 8);
1270  x[3] = U8TO32_LE(in + 12);
1271  x[4] = U8TO32_LE(in + 16);
1272  x[5] = U8TO32_LE(in + 20);
1273  x[6] = U8TO32_LE(in + 24);
1274  x[7] = U8TO32_LE(in + 28);
1275 
1276  out[0] = ( x[0]) & 0x3fffffff;
1277  out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1278  out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1279  out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1280  out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1281  out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1282  out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1283  out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1284  out[8] = ((x[ 7] >> 16) ) & 0x0000ffff;
1285 }
1286 
1287 void
1288 contract256_modm(byte out[32], const bignum256modm in) {
1289  U32TO8_LE(out + 0, (in[0] ) | (in[1] << 30));
1290  U32TO8_LE(out + 4, (in[1] >> 2) | (in[2] << 28));
1291  U32TO8_LE(out + 8, (in[2] >> 4) | (in[3] << 26));
1292  U32TO8_LE(out + 12, (in[3] >> 6) | (in[4] << 24));
1293  U32TO8_LE(out + 16, (in[4] >> 8) | (in[5] << 22));
1294  U32TO8_LE(out + 20, (in[5] >> 10) | (in[6] << 20));
1295  U32TO8_LE(out + 24, (in[6] >> 12) | (in[7] << 18));
1296  U32TO8_LE(out + 28, (in[7] >> 14) | (in[8] << 16));
1297 }
1298 
1299 void
1300 contract256_window4_modm(signed char r[64], const bignum256modm in) {
1301  char carry;
1302  signed char *quads = r;
1303  bignum256modm_element_t i, j, v;
1304 
1305  for (i = 0; i < 8; i += 2) {
1306  v = in[i];
1307  for (j = 0; j < 7; j++) {
1308  *quads++ = (v & 15);
1309  v >>= 4;
1310  }
1311  v |= (in[i+1] << 2);
1312  for (j = 0; j < 8; j++) {
1313  *quads++ = (v & 15);
1314  v >>= 4;
1315  }
1316  }
1317 
1318  v = in[8];
1319  *quads++ = (v & 15); v >>= 4;
1320  *quads++ = (v & 15); v >>= 4;
1321  *quads++ = (v & 15); v >>= 4;
1322  *quads++ = (v & 15); v >>= 4;
1323 
1324  /* making it signed */
1325  carry = 0;
1326  for(i = 0; i < 63; i++) {
1327  r[i] += carry;
1328  r[i+1] += (r[i] >> 4);
1329  r[i] &= 15;
1330  carry = (r[i] >> 3);
1331  r[i] -= (carry << 4);
1332  }
1333  r[63] += carry;
1334 }
1335 
1336 void
1337 contract256_slidingwindow_modm(signed char r[256], const bignum256modm s, int windowsize) {
1338  int i,j,k,b;
1339  int m = (1 << (windowsize - 1)) - 1, soplen = 256;
1340  signed char *bits = r;
1341  bignum256modm_element_t v;
1342 
1343  /* first put the binary expansion into r */
1344  for (i = 0; i < 8; i++) {
1345  v = s[i];
1346  for (j = 0; j < 30; j++, v >>= 1)
1347  *bits++ = (v & 1);
1348  }
1349  v = s[8];
1350  for (j = 0; j < 16; j++, v >>= 1)
1351  *bits++ = (v & 1);
1352 
1353  /* Making it sliding window */
1354  for (j = 0; j < soplen; j++) {
1355  if (!r[j])
1356  continue;
1357 
1358  for (b = 1; (b < (soplen - j)) && (b <= 6); b++) {
1359  if ((r[j] + (r[j + b] << b)) <= m) {
1360  r[j] += r[j + b] << b;
1361  r[j + b] = 0;
1362  } else if ((r[j] - (r[j + b] << b)) >= -m) {
1363  r[j] -= r[j + b] << b;
1364  for (k = j + b; k < soplen; k++) {
1365  if (!r[k]) {
1366  r[k] = 1;
1367  break;
1368  }
1369  r[k] = 0;
1370  }
1371  } else if (r[j + b]) {
1372  break;
1373  }
1374  }
1375  }
1376 }
1377 
1378 inline void
1379 ge25519_p1p1_to_partial(ge25519 *r, const ge25519_p1p1 *p) {
1380  curve25519_mul(r->x, p->x, p->t);
1381  curve25519_mul(r->y, p->y, p->z);
1382  curve25519_mul(r->z, p->z, p->t);
1383 }
1384 
1385 inline void
1386 ge25519_p1p1_to_full(ge25519 *r, const ge25519_p1p1 *p) {
1387  curve25519_mul(r->x, p->x, p->t);
1388  curve25519_mul(r->y, p->y, p->z);
1389  curve25519_mul(r->z, p->z, p->t);
1390  curve25519_mul(r->t, p->x, p->y);
1391 }
1392 
1393 void
1394 ge25519_full_to_pniels(ge25519_pniels *p, const ge25519 *r) {
1395  curve25519_sub(p->ysubx, r->y, r->x);
1396  curve25519_add(p->xaddy, r->y, r->x);
1397  curve25519_copy(p->z, r->z);
1398  curve25519_mul(p->t2d, r->t, ge25519_ec2d);
1399 }
1400 
1401 void
1402 ge25519_add_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519 *q) {
1403  bignum25519 a,b,c,d,t,u;
1404 
1405  curve25519_sub(a, p->y, p->x);
1406  curve25519_add(b, p->y, p->x);
1407  curve25519_sub(t, q->y, q->x);
1408  curve25519_add(u, q->y, q->x);
1409  curve25519_mul(a, a, t);
1410  curve25519_mul(b, b, u);
1411  curve25519_mul(c, p->t, q->t);
1412  curve25519_mul(c, c, ge25519_ec2d);
1413  curve25519_mul(d, p->z, q->z);
1414  curve25519_add(d, d, d);
1415  curve25519_sub(r->x, b, a);
1416  curve25519_add(r->y, b, a);
1417  curve25519_add_after_basic(r->z, d, c);
1418  curve25519_sub_after_basic(r->t, d, c);
1419 }
1420 
1421 void
1422 ge25519_double_p1p1(ge25519_p1p1 *r, const ge25519 *p) {
1423  bignum25519 a,b,c;
1424 
1425  curve25519_square(a, p->x);
1426  curve25519_square(b, p->y);
1427  curve25519_square(c, p->z);
1428  curve25519_add_reduce(c, c, c);
1429  curve25519_add(r->x, p->x, p->y);
1430  curve25519_square(r->x, r->x);
1431  curve25519_add(r->y, b, a);
1432  curve25519_sub(r->z, b, a);
1433  curve25519_sub_after_basic(r->x, r->x, r->y);
1434  curve25519_sub_after_basic(r->t, c, r->z);
1435 }
1436 
1437 void
1438 ge25519_nielsadd2_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_niels *q, byte signbit) {
1439  const bignum25519 *qb = (const bignum25519 *)q;
1440  bignum25519 *rb = (bignum25519 *)r;
1441  bignum25519 a,b,c;
1442 
1443  curve25519_sub(a, p->y, p->x);
1444  curve25519_add(b, p->y, p->x);
1445  curve25519_mul(a, a, qb[signbit]); /* x for +, y for - */
1446  curve25519_mul(r->x, b, qb[signbit^1]); /* y for +, x for - */
1447  curve25519_add(r->y, r->x, a);
1448  curve25519_sub(r->x, r->x, a);
1449  curve25519_mul(c, p->t, q->t2d);
1450  curve25519_add_reduce(r->t, p->z, p->z);
1451  curve25519_copy(r->z, r->t);
1452  curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1453  curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1454 }
1455 
1456 void
1457 ge25519_pnielsadd_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_pniels *q, byte signbit) {
1458  const bignum25519 *qb = (const bignum25519 *)q;
1459  bignum25519 *rb = (bignum25519 *)r;
1460  bignum25519 a,b,c;
1461 
1462  curve25519_sub(a, p->y, p->x);
1463  curve25519_add(b, p->y, p->x);
1464  curve25519_mul(a, a, qb[signbit]); /* ysubx for +, xaddy for - */
1465  curve25519_mul(r->x, b, qb[signbit^1]); /* xaddy for +, ysubx for - */
1466  curve25519_add(r->y, r->x, a);
1467  curve25519_sub(r->x, r->x, a);
1468  curve25519_mul(c, p->t, q->t2d);
1469  curve25519_mul(r->t, p->z, q->z);
1470  curve25519_add_reduce(r->t, r->t, r->t);
1471  curve25519_copy(r->z, r->t);
1472  curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1473  curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1474 }
1475 
1476 void
1477 ge25519_double_partial(ge25519 *r, const ge25519 *p) {
1478  ge25519_p1p1 t;
1479  ge25519_double_p1p1(&t, p);
1480  ge25519_p1p1_to_partial(r, &t);
1481 }
1482 
1483 void
1484 ge25519_double(ge25519 *r, const ge25519 *p) {
1485  ge25519_p1p1 t;
1486  ge25519_double_p1p1(&t, p);
1487  ge25519_p1p1_to_full(r, &t);
1488 }
1489 
1490 void
1491 ge25519_add(ge25519 *r, const ge25519 *p, const ge25519 *q) {
1492  ge25519_p1p1 t;
1493  ge25519_add_p1p1(&t, p, q);
1494  ge25519_p1p1_to_full(r, &t);
1495 }
1496 
1497 void
1498 ge25519_nielsadd2(ge25519 *r, const ge25519_niels *q) {
1499  bignum25519 a,b,c,e,f,g,h;
1500 
1501  curve25519_sub(a, r->y, r->x);
1502  curve25519_add(b, r->y, r->x);
1503  curve25519_mul(a, a, q->ysubx);
1504  curve25519_mul(e, b, q->xaddy);
1505  curve25519_add(h, e, a);
1506  curve25519_sub(e, e, a);
1507  curve25519_mul(c, r->t, q->t2d);
1508  curve25519_add(f, r->z, r->z);
1509  curve25519_add_after_basic(g, f, c);
1510  curve25519_sub_after_basic(f, f, c);
1511  curve25519_mul(r->x, e, f);
1512  curve25519_mul(r->y, h, g);
1513  curve25519_mul(r->z, g, f);
1514  curve25519_mul(r->t, e, h);
1515 }
1516 
1517 void
1518 ge25519_pnielsadd(ge25519_pniels *r, const ge25519 *p, const ge25519_pniels *q) {
1519  bignum25519 a,b,c,x,y,z,t;
1520 
1521  curve25519_sub(a, p->y, p->x);
1522  curve25519_add(b, p->y, p->x);
1523  curve25519_mul(a, a, q->ysubx);
1524  curve25519_mul(x, b, q->xaddy);
1525  curve25519_add(y, x, a);
1526  curve25519_sub(x, x, a);
1527  curve25519_mul(c, p->t, q->t2d);
1528  curve25519_mul(t, p->z, q->z);
1529  curve25519_add(t, t, t);
1530  curve25519_add_after_basic(z, t, c);
1531  curve25519_sub_after_basic(t, t, c);
1532  curve25519_mul(r->xaddy, x, t);
1533  curve25519_mul(r->ysubx, y, z);
1534  curve25519_mul(r->z, z, t);
1535  curve25519_mul(r->t2d, x, y);
1536  curve25519_copy(y, r->ysubx);
1537  curve25519_sub(r->ysubx, r->ysubx, r->xaddy);
1538  curve25519_add(r->xaddy, r->xaddy, y);
1539  curve25519_mul(r->t2d, r->t2d, ge25519_ec2d);
1540 }
1541 
1542 void
1543 ge25519_pack(byte r[32], const ge25519 *p) {
1544  bignum25519 tx, ty, zi;
1545  byte parity[32];
1546  curve25519_recip(zi, p->z);
1547  curve25519_mul(tx, p->x, zi);
1548  curve25519_mul(ty, p->y, zi);
1549  curve25519_contract(r, ty);
1550  curve25519_contract(parity, tx);
1551  r[31] ^= ((parity[0] & 1) << 7);
1552 }
1553 
1554 int
1555 ed25519_verify(const unsigned char *x, const unsigned char *y, size_t len) {
1556  size_t differentbits = 0;
1557  while (len--)
1558  differentbits |= (*x++ ^ *y++);
1559  return (int) (1 & ((differentbits - 1) >> 8));
1560 }
1561 
1562 int
1563 ge25519_unpack_negative_vartime(ge25519 *r, const byte p[32]) {
1564  const byte zero[32] = {0};
1565  const bignum25519 one = {1};
1566  byte parity = p[31] >> 7;
1567  byte check[32];
1568  bignum25519 t, root, num, den, d3;
1569 
1570  curve25519_expand(r->y, p);
1571  curve25519_copy(r->z, one);
1572  curve25519_square(num, r->y); /* x = y^2 */
1573  curve25519_mul(den, num, ge25519_ecd); /* den = dy^2 */
1574  curve25519_sub_reduce(num, num, r->z); /* x = y^1 - 1 */
1575  curve25519_add(den, den, r->z); /* den = dy^2 + 1 */
1576 
1577  /* Computation of sqrt(num/den) */
1578  /* 1.: computation of num^((p-5)/8)*den^((7p-35)/8) = (num*den^7)^((p-5)/8) */
1579  curve25519_square(t, den);
1580  curve25519_mul(d3, t, den);
1581  curve25519_square(r->x, d3);
1582  curve25519_mul(r->x, r->x, den);
1583  curve25519_mul(r->x, r->x, num);
1584  curve25519_pow_two252m3(r->x, r->x);
1585 
1586  /* 2. computation of r->x = num * den^3 * (num*den^7)^((p-5)/8) */
1587  curve25519_mul(r->x, r->x, d3);
1588  curve25519_mul(r->x, r->x, num);
1589 
1590  /* 3. Check if either of the roots works: */
1591  curve25519_square(t, r->x);
1592  curve25519_mul(t, t, den);
1593  curve25519_sub_reduce(root, t, num);
1594  curve25519_contract(check, root);
1595  if (!ed25519_verify(check, zero, 32)) {
1596  curve25519_add_reduce(t, t, num);
1597  curve25519_contract(check, t);
1598  if (!ed25519_verify(check, zero, 32))
1599  return 0;
1600  curve25519_mul(r->x, r->x, ge25519_sqrtneg1);
1601  }
1602 
1603  curve25519_contract(check, r->x);
1604  if ((check[0] & 1) == parity) {
1605  curve25519_copy(t, r->x);
1606  curve25519_neg(r->x, t);
1607  }
1608  curve25519_mul(r->t, r->x, r->y);
1609  return 1;
1610 }
1611 
1612 /* computes [s1]p1 + [s2]basepoint */
1613 void
1614 ge25519_double_scalarmult_vartime(ge25519 *r, const ge25519 *p1, const bignum256modm s1, const bignum256modm s2) {
1615  signed char slide1[256], slide2[256];
1616  ge25519_pniels pre1[S1_TABLE_SIZE];
1617  ge25519 d1;
1618  ge25519_p1p1 t;
1619  sword32 i;
1620 
1621  contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
1622  contract256_slidingwindow_modm(slide2, s2, S2_SWINDOWSIZE);
1623 
1624  ge25519_double(&d1, p1);
1625  ge25519_full_to_pniels(pre1, p1);
1626  for (i = 0; i < S1_TABLE_SIZE - 1; i++)
1627  ge25519_pnielsadd(&pre1[i+1], &d1, &pre1[i]);
1628 
1629  /* set neutral */
1630  memset(r, 0, sizeof(ge25519));
1631  r->y[0] = 1;
1632  r->z[0] = 1;
1633 
1634  i = 255;
1635  while ((i >= 0) && !(slide1[i] | slide2[i]))
1636  i--;
1637 
1638  for (; i >= 0; i--) {
1639  ge25519_double_p1p1(&t, r);
1640 
1641  if (slide1[i]) {
1642  ge25519_p1p1_to_full(r, &t);
1643  ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (byte)slide1[i] >> 7);
1644  }
1645 
1646  if (slide2[i]) {
1647  ge25519_p1p1_to_full(r, &t);
1648  ge25519_nielsadd2_p1p1(&t, r, &ge25519_niels_sliding_multiples[abs(slide2[i]) / 2], (byte)slide2[i] >> 7);
1649  }
1650 
1651  ge25519_p1p1_to_partial(r, &t);
1652  }
1653 }
1654 
1655 #if !defined(HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS)
1656 
1657 word32
1658 ge25519_windowb_equal(word32 b, word32 c) {
1659  return ((b ^ c) - 1) >> 31;
1660 }
1661 
1662 void
1663 ge25519_scalarmult_base_choose_niels(ge25519_niels *t, const byte table[256][96], word32 pos, signed char b) {
1664  bignum25519 neg;
1665  word32 sign = (word32)((byte)b >> 7);
1666  word32 mask = ~(sign - 1);
1667  word32 u = (b + mask) ^ mask;
1668  word32 i;
1669 
1670  /* ysubx, xaddy, t2d in packed form. initialize to ysubx = 1, xaddy = 1, t2d = 0 */
1671  byte packed[96] = {0};
1672  packed[0] = 1;
1673  packed[32] = 1;
1674 
1675  for (i = 0; i < 8; i++)
1676  curve25519_move_conditional_bytes(packed, table[(pos * 8) + i], ge25519_windowb_equal(u, i + 1));
1677 
1678  /* expand in to t */
1679  curve25519_expand(t->ysubx, packed + 0);
1680  curve25519_expand(t->xaddy, packed + 32);
1681  curve25519_expand(t->t2d , packed + 64);
1682 
1683  /* adjust for sign */
1684  curve25519_swap_conditional(t->ysubx, t->xaddy, sign);
1685  curve25519_neg(neg, t->t2d);
1686  curve25519_swap_conditional(t->t2d, neg, sign);
1687 }
1688 
1689 #endif /* HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS */
1690 
1691 /* computes [s]basepoint */
1692 void
1693 ge25519_scalarmult_base_niels(ge25519 *r, const byte basepoint_table[256][96], const bignum256modm s) {
1694  signed char b[64];
1695  word32 i;
1696  ge25519_niels t;
1697 
1698  contract256_window4_modm(b, s);
1699 
1700  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[1]);
1701  curve25519_sub_reduce(r->x, t.xaddy, t.ysubx);
1702  curve25519_add_reduce(r->y, t.xaddy, t.ysubx);
1703  memset(r->z, 0, sizeof(bignum25519));
1704  curve25519_copy(r->t, t.t2d);
1705  r->z[0] = 2;
1706  for (i = 3; i < 64; i += 2) {
1707  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1708  ge25519_nielsadd2(r, &t);
1709  }
1710  ge25519_double_partial(r, r);
1711  ge25519_double_partial(r, r);
1712  ge25519_double_partial(r, r);
1713  ge25519_double(r, r);
1714  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[0]);
1715  curve25519_mul(t.t2d, t.t2d, ge25519_ecd);
1716  ge25519_nielsadd2(r, &t);
1717  for(i = 2; i < 64; i += 2) {
1718  ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1719  ge25519_nielsadd2(r, &t);
1720  }
1721 }
1722 
1723 ANONYMOUS_NAMESPACE_END
1724 NAMESPACE_END // Ed25519
1725 NAMESPACE_END // Donna
1726 NAMESPACE_END // CryptoPP
1727 
1728 //***************************** curve25519 *****************************//
1729 
1730 NAMESPACE_BEGIN(CryptoPP)
1731 NAMESPACE_BEGIN(Donna)
1732 
1733 int curve25519_mult_CXX(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1734 {
1735  using namespace CryptoPP::Donna::X25519;
1736 
1738  for (size_t i = 0;i < 32;++i)
1739  e[i] = secretKey[i];
1740  e[0] &= 0xf8; e[31] &= 0x7f; e[31] |= 0x40;
1741 
1742  bignum25519 nqpqx = {1}, nqpqz = {0}, nqz = {1}, nqx;
1743  bignum25519 q, qx, qpqx, qqx, zzz, zmone;
1744  size_t bit, lastbit;
1745 
1746  curve25519_expand(q, othersKey);
1747  curve25519_copy(nqx, q);
1748 
1749  /* bit 255 is always 0, and bit 254 is always 1, so skip bit 255 and
1750  start pre-swapped on bit 254 */
1751  lastbit = 1;
1752 
1753  /* we are doing bits 254..3 in the loop, but are swapping in bits 253..2 */
1754  for (int i = 253; i >= 2; i--) {
1755  curve25519_add(qx, nqx, nqz);
1756  curve25519_sub(nqz, nqx, nqz);
1757  curve25519_add(qpqx, nqpqx, nqpqz);
1758  curve25519_sub(nqpqz, nqpqx, nqpqz);
1759  curve25519_mul(nqpqx, qpqx, nqz);
1760  curve25519_mul(nqpqz, qx, nqpqz);
1761  curve25519_add(qqx, nqpqx, nqpqz);
1762  curve25519_sub(nqpqz, nqpqx, nqpqz);
1763  curve25519_square(nqpqz, nqpqz);
1764  curve25519_square(nqpqx, qqx);
1765  curve25519_mul(nqpqz, nqpqz, q);
1766  curve25519_square(qx, qx);
1767  curve25519_square(nqz, nqz);
1768  curve25519_mul(nqx, qx, nqz);
1769  curve25519_sub(nqz, qx, nqz);
1770  curve25519_scalar_product(zzz, nqz, 121665);
1771  curve25519_add(zzz, zzz, qx);
1772  curve25519_mul(nqz, nqz, zzz);
1773 
1774  bit = (e[i/8] >> (i & 7)) & 1;
1775  curve25519_swap_conditional(nqx, nqpqx, bit ^ lastbit);
1776  curve25519_swap_conditional(nqz, nqpqz, bit ^ lastbit);
1777  lastbit = bit;
1778  }
1779 
1780  /* the final 3 bits are always zero, so we only need to double */
1781  for (int i = 0; i < 3; i++) {
1782  curve25519_add(qx, nqx, nqz);
1783  curve25519_sub(nqz, nqx, nqz);
1784  curve25519_square(qx, qx);
1785  curve25519_square(nqz, nqz);
1786  curve25519_mul(nqx, qx, nqz);
1787  curve25519_sub(nqz, qx, nqz);
1788  curve25519_scalar_product(zzz, nqz, 121665);
1789  curve25519_add(zzz, zzz, qx);
1790  curve25519_mul(nqz, nqz, zzz);
1791  }
1792 
1793  curve25519_recip(zmone, nqz);
1794  curve25519_mul(nqz, nqx, zmone);
1795  curve25519_contract(sharedKey, nqz);
1796 
1797  return 0;
1798 }
1799 
1800 int curve25519_mult(byte publicKey[32], const byte secretKey[32])
1801 {
1802  using namespace CryptoPP::Donna::X25519;
1803 
1804 #if (CRYPTOPP_CURVE25519_SSE2)
1805  if (HasSSE2())
1806  return curve25519_mult_SSE2(publicKey, secretKey, basePoint);
1807  else
1808 #endif
1809 
1810  return curve25519_mult_CXX(publicKey, secretKey, basePoint);
1811 }
1812 
1813 int curve25519_mult(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1814 {
1815 #if (CRYPTOPP_CURVE25519_SSE2)
1816  if (HasSSE2())
1817  return curve25519_mult_SSE2(sharedKey, secretKey, othersKey);
1818  else
1819 #endif
1820 
1821  return curve25519_mult_CXX(sharedKey, secretKey, othersKey);
1822 }
1823 
1824 NAMESPACE_END // Donna
1825 NAMESPACE_END // CryptoPP
1826 
1827 //******************************* ed25519 *******************************//
1828 
1829 NAMESPACE_BEGIN(CryptoPP)
1830 NAMESPACE_BEGIN(Donna)
1831 
1832 int
1833 ed25519_publickey_CXX(byte publicKey[32], const byte secretKey[32])
1834 {
1835  using namespace CryptoPP::Donna::Ed25519;
1836 
1837  bignum256modm a;
1838  ALIGN(16) ge25519 A;
1839  hash_512bits extsk;
1840 
1841  /* A = aB */
1842  ed25519_extsk(extsk, secretKey);
1843  expand256_modm(a, extsk, 32);
1844  ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
1845  ge25519_pack(publicKey, &A);
1846 
1847  return 0;
1848 }
1849 
1850 int
1851 ed25519_publickey(byte publicKey[32], const byte secretKey[32])
1852 {
1853  return ed25519_publickey_CXX(publicKey, secretKey);
1854 }
1855 
1856 int
1857 ed25519_sign_CXX(const byte *m, size_t mlen, const byte sk[32], const byte pk[32], byte RS[64])
1858 {
1859  using namespace CryptoPP::Donna::Ed25519;
1860 
1861  bignum256modm r, S, a;
1862  ALIGN(16) ge25519 R;
1863  hash_512bits extsk, hashr, hram;
1864 
1865  ed25519_extsk(extsk, sk);
1866 
1867  /* r = H(aExt[32..64], m) */
1868  SHA512 hash;
1869  hash.Update(extsk + 32, 32);
1870  hash.Update(m, mlen);
1871  hash.Final(hashr);
1872  expand256_modm(r, hashr, 64);
1873 
1874  /* R = rB */
1875  ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1876  ge25519_pack(RS, &R);
1877 
1878  /* S = H(R,A,m).. */
1879  ed25519_hram(hram, RS, pk, m, mlen);
1880  expand256_modm(S, hram, 64);
1881 
1882  /* S = H(R,A,m)a */
1883  expand256_modm(a, extsk, 32);
1884  mul256_modm(S, S, a);
1885 
1886  /* S = (r + H(R,A,m)a) */
1887  add256_modm(S, S, r);
1888 
1889  /* S = (r + H(R,A,m)a) mod L */
1890  contract256_modm(RS + 32, S);
1891 
1892  return 0;
1893 }
1894 
1895 int
1896 ed25519_sign(const byte* message, size_t messageLength, const byte secretKey[32],
1897  const byte publicKey[32], byte signature[64])
1898 {
1899  return ed25519_sign_CXX(message, messageLength, secretKey, publicKey, signature);
1900 }
1901 
1902 int
1903 ed25519_sign_open_CXX(const byte *m, size_t mlen, const byte pk[32], const byte RS[64]) {
1904 
1905  using namespace CryptoPP::Donna::Ed25519;
1906 
1907  ALIGN(16) ge25519 R, A;
1908  hash_512bits hash;
1909  bignum256modm hram, S;
1910  unsigned char checkR[32];
1911 
1912  if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
1913  return -1;
1914 
1915  /* hram = H(R,A,m) */
1916  ed25519_hram(hash, RS, pk, m, mlen);
1917  expand256_modm(hram, hash, 64);
1918 
1919  /* S */
1920  expand256_modm(S, RS + 32, 32);
1921 
1922  /* SB - H(R,A,m)A */
1923  ge25519_double_scalarmult_vartime(&R, &A, hram, S);
1924  ge25519_pack(checkR, &R);
1925 
1926  /* check that R = SB - H(R,A,m)A */
1927  return ed25519_verify(RS, checkR, 32) ? 0 : -1;
1928 }
1929 
1930 int
1931 ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
1932 {
1933  return ed25519_sign_open_CXX(message, messageLength, publicKey, signature);
1934 }
1935 
1936 NAMESPACE_END // Donna
1937 NAMESPACE_END // CryptoPP
1938 
1939 #endif // CRYPTOPP_CURVE25519_32BIT
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.
Definition: misc.h:2362
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:135
EnumToType< ByteOrder, LITTLE_ENDIAN_ORDER > LittleEndian
Provides a constant for LittleEndian.
Definition: cryptlib.h:150
Library configuration file.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Definition: iterhash.cpp:13
byte order is little-endian
Definition: cryptlib.h:145
Classes and functions for secure memory allocations.
T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
Access a block of memory.
Definition: misc.h:2320
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 curve25519_mult(byte publicKey[32], const byte secretKey[32])
Generate a public key.
SHA-512 message digest.
Definition: sha.h:141
Precompiled header file.
int ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
Verifies a signature on a message.
Fixed size stack-based SecBlock.
Definition: secblock.h:1071
Functions for CPU features and intrinsics.
Classes for SHA-1 and SHA-2 family of message digests.
virtual void CalculateDigest(byte *digest, const byte *input, size_t length)
Updates the hash with additional input and computes the hash of the current message.
Definition: cryptlib.h:1158
bool HasSSE2()
Determines SSE2 availability.
Definition: cpu.h:116
Access a block of memory.
Definition: misc.h:2388
Crypto++ library namespace.
int ed25519_publickey(byte publicKey[32], const byte secretKey[32])
Creates a public key from a secret key.
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1114