Botan  2.4.0
Crypto and TLS for C++11
sc_muladd.cpp
Go to the documentation of this file.
1 /*
2 * Ed25519
3 * (C) 2017 Ribose Inc
4 *
5 * Based on the public domain code from SUPERCOP ref10 by
6 * Peter Schwabe, Daniel J. Bernstein, Niels Duif, Tanja Lange, Bo-Yin Yang
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 */
10 
11 #include <botan/internal/ed25519_internal.h>
12 
13 namespace Botan {
14 
15 /*
16 Input:
17  a[0]+256*a[1]+...+256^31*a[31] = a
18  b[0]+256*b[1]+...+256^31*b[31] = b
19  c[0]+256*c[1]+...+256^31*c[31] = c
20 
21 Output:
22  s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
23  where l = 2^252 + 27742317777372353535851937790883648493.
24 */
25 
26 void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
27  {
28  const int64_t X20 = (1 << 20);
29  const int64_t X21 = (1 << 21);
30 
31  int64_t a0 = 2097151 & load_3(a);
32  int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
33  int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
34  int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
35  int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
36  int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
37  int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
38  int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
39  int64_t a8 = 2097151 & load_3(a + 21);
40  int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
41  int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
42  int64_t a11 = (load_4(a + 28) >> 7);
43  int64_t b0 = 2097151 & load_3(b);
44  int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
45  int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
46  int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
47  int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
48  int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
49  int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
50  int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
51  int64_t b8 = 2097151 & load_3(b + 21);
52  int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
53  int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
54  int64_t b11 = (load_4(b + 28) >> 7);
55  int64_t c0 = 2097151 & load_3(c);
56  int64_t c1 = 2097151 & (load_4(c + 2) >> 5);
57  int64_t c2 = 2097151 & (load_3(c + 5) >> 2);
58  int64_t c3 = 2097151 & (load_4(c + 7) >> 7);
59  int64_t c4 = 2097151 & (load_4(c + 10) >> 4);
60  int64_t c5 = 2097151 & (load_3(c + 13) >> 1);
61  int64_t c6 = 2097151 & (load_4(c + 15) >> 6);
62  int64_t c7 = 2097151 & (load_3(c + 18) >> 3);
63  int64_t c8 = 2097151 & load_3(c + 21);
64  int64_t c9 = 2097151 & (load_4(c + 23) >> 5);
65  int64_t c10 = 2097151 & (load_3(c + 26) >> 2);
66  int64_t c11 = (load_4(c + 28) >> 7);
67  int64_t s0;
68  int64_t s1;
69  int64_t s2;
70  int64_t s3;
71  int64_t s4;
72  int64_t s5;
73  int64_t s6;
74  int64_t s7;
75  int64_t s8;
76  int64_t s9;
77  int64_t s10;
78  int64_t s11;
79  int64_t s12;
80  int64_t s13;
81  int64_t s14;
82  int64_t s15;
83  int64_t s16;
84  int64_t s17;
85  int64_t s18;
86  int64_t s19;
87  int64_t s20;
88  int64_t s21;
89  int64_t s22;
90  int64_t s23;
91  int64_t carry0;
92  int64_t carry1;
93  int64_t carry2;
94  int64_t carry3;
95  int64_t carry4;
96  int64_t carry5;
97  int64_t carry6;
98  int64_t carry7;
99  int64_t carry8;
100  int64_t carry9;
101  int64_t carry10;
102  int64_t carry11;
103  int64_t carry12;
104  int64_t carry13;
105  int64_t carry14;
106  int64_t carry15;
107  int64_t carry16;
108  int64_t carry17;
109  int64_t carry18;
110  int64_t carry19;
111  int64_t carry20;
112  int64_t carry21;
113  int64_t carry22;
114 
115  s0 = c0 + a0*b0;
116  s1 = c1 + a0*b1 + a1*b0;
117  s2 = c2 + a0*b2 + a1*b1 + a2*b0;
118  s3 = c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0;
119  s4 = c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0;
120  s5 = c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0;
121  s6 = c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0;
122  s7 = c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0;
123  s8 = c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0;
124  s9 = c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0;
125  s10 = c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0;
126  s11 = c11 + a0*b11 + a1*b10 + a2*b9 + a3*b8 + a4*b7 + a5*b6 + a6*b5 + a7*b4 + a8*b3 + a9*b2 + a10*b1 + a11*b0;
127  s12 = a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1;
128  s13 = a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2;
129  s14 = a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3;
130  s15 = a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4;
131  s16 = a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5;
132  s17 = a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6;
133  s18 = a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7;
134  s19 = a8*b11 + a9*b10 + a10*b9 + a11*b8;
135  s20 = a9*b11 + a10*b10 + a11*b9;
136  s21 = a10*b11 + a11*b10;
137  s22 = a11*b11;
138  s23 = 0;
139 
140  carry0 = (s0 + X20) >> 21;
141  s1 += carry0;
142  s0 -= carry0 * X21;
143  carry2 = (s2 + X20) >> 21;
144  s3 += carry2;
145  s2 -= carry2 * X21;
146  carry4 = (s4 + X20) >> 21;
147  s5 += carry4;
148  s4 -= carry4 * X21;
149  carry6 = (s6 + X20) >> 21;
150  s7 += carry6;
151  s6 -= carry6 * X21;
152  carry8 = (s8 + X20) >> 21;
153  s9 += carry8;
154  s8 -= carry8 * X21;
155  carry10 = (s10 + X20) >> 21;
156  s11 += carry10;
157  s10 -= carry10 * X21;
158  carry12 = (s12 + X20) >> 21;
159  s13 += carry12;
160  s12 -= carry12 * X21;
161  carry14 = (s14 + X20) >> 21;
162  s15 += carry14;
163  s14 -= carry14 * X21;
164  carry16 = (s16 + X20) >> 21;
165  s17 += carry16;
166  s16 -= carry16 * X21;
167  carry18 = (s18 + X20) >> 21;
168  s19 += carry18;
169  s18 -= carry18 * X21;
170  carry20 = (s20 + X20) >> 21;
171  s21 += carry20;
172  s20 -= carry20 * X21;
173  carry22 = (s22 + X20) >> 21;
174  s23 += carry22;
175  s22 -= carry22 * X21;
176 
177  carry1 = (s1 + X20) >> 21;
178  s2 += carry1;
179  s1 -= carry1 * X21;
180  carry3 = (s3 + X20) >> 21;
181  s4 += carry3;
182  s3 -= carry3 * X21;
183  carry5 = (s5 + X20) >> 21;
184  s6 += carry5;
185  s5 -= carry5 * X21;
186  carry7 = (s7 + X20) >> 21;
187  s8 += carry7;
188  s7 -= carry7 * X21;
189  carry9 = (s9 + X20) >> 21;
190  s10 += carry9;
191  s9 -= carry9 * X21;
192  carry11 = (s11 + X20) >> 21;
193  s12 += carry11;
194  s11 -= carry11 * X21;
195  carry13 = (s13 + X20) >> 21;
196  s14 += carry13;
197  s13 -= carry13 * X21;
198  carry15 = (s15 + X20) >> 21;
199  s16 += carry15;
200  s15 -= carry15 * X21;
201  carry17 = (s17 + X20) >> 21;
202  s18 += carry17;
203  s17 -= carry17 * X21;
204  carry19 = (s19 + X20) >> 21;
205  s20 += carry19;
206  s19 -= carry19 * X21;
207  carry21 = (s21 + X20) >> 21;
208  s22 += carry21;
209  s21 -= carry21 * X21;
210 
211  s11 += s23 * 666643;
212  s12 += s23 * 470296;
213  s13 += s23 * 654183;
214  s14 -= s23 * 997805;
215  s15 += s23 * 136657;
216  s16 -= s23 * 683901;
217  s23 = 0;
218 
219  s10 += s22 * 666643;
220  s11 += s22 * 470296;
221  s12 += s22 * 654183;
222  s13 -= s22 * 997805;
223  s14 += s22 * 136657;
224  s15 -= s22 * 683901;
225  s22 = 0;
226 
227  s9 += s21 * 666643;
228  s10 += s21 * 470296;
229  s11 += s21 * 654183;
230  s12 -= s21 * 997805;
231  s13 += s21 * 136657;
232  s14 -= s21 * 683901;
233  s21 = 0;
234 
235  s8 += s20 * 666643;
236  s9 += s20 * 470296;
237  s10 += s20 * 654183;
238  s11 -= s20 * 997805;
239  s12 += s20 * 136657;
240  s13 -= s20 * 683901;
241  s20 = 0;
242 
243  s7 += s19 * 666643;
244  s8 += s19 * 470296;
245  s9 += s19 * 654183;
246  s10 -= s19 * 997805;
247  s11 += s19 * 136657;
248  s12 -= s19 * 683901;
249  s19 = 0;
250 
251  s6 += s18 * 666643;
252  s7 += s18 * 470296;
253  s8 += s18 * 654183;
254  s9 -= s18 * 997805;
255  s10 += s18 * 136657;
256  s11 -= s18 * 683901;
257  s18 = 0;
258 
259  carry6 = (s6 + X20) >> 21;
260  s7 += carry6;
261  s6 -= carry6 * X21;
262  carry8 = (s8 + X20) >> 21;
263  s9 += carry8;
264  s8 -= carry8 * X21;
265  carry10 = (s10 + X20) >> 21;
266  s11 += carry10;
267  s10 -= carry10 * X21;
268  carry12 = (s12 + X20) >> 21;
269  s13 += carry12;
270  s12 -= carry12 * X21;
271  carry14 = (s14 + X20) >> 21;
272  s15 += carry14;
273  s14 -= carry14 * X21;
274  carry16 = (s16 + X20) >> 21;
275  s17 += carry16;
276  s16 -= carry16 * X21;
277 
278  carry7 = (s7 + X20) >> 21;
279  s8 += carry7;
280  s7 -= carry7 * X21;
281  carry9 = (s9 + X20) >> 21;
282  s10 += carry9;
283  s9 -= carry9 * X21;
284  carry11 = (s11 + X20) >> 21;
285  s12 += carry11;
286  s11 -= carry11 * X21;
287  carry13 = (s13 + X20) >> 21;
288  s14 += carry13;
289  s13 -= carry13 * X21;
290  carry15 = (s15 + X20) >> 21;
291  s16 += carry15;
292  s15 -= carry15 * X21;
293 
294  s5 += s17 * 666643;
295  s6 += s17 * 470296;
296  s7 += s17 * 654183;
297  s8 -= s17 * 997805;
298  s9 += s17 * 136657;
299  s10 -= s17 * 683901;
300  s17 = 0;
301 
302  s4 += s16 * 666643;
303  s5 += s16 * 470296;
304  s6 += s16 * 654183;
305  s7 -= s16 * 997805;
306  s8 += s16 * 136657;
307  s9 -= s16 * 683901;
308  s16 = 0;
309 
310  s3 += s15 * 666643;
311  s4 += s15 * 470296;
312  s5 += s15 * 654183;
313  s6 -= s15 * 997805;
314  s7 += s15 * 136657;
315  s8 -= s15 * 683901;
316  s15 = 0;
317 
318  s2 += s14 * 666643;
319  s3 += s14 * 470296;
320  s4 += s14 * 654183;
321  s5 -= s14 * 997805;
322  s6 += s14 * 136657;
323  s7 -= s14 * 683901;
324  s14 = 0;
325 
326  s1 += s13 * 666643;
327  s2 += s13 * 470296;
328  s3 += s13 * 654183;
329  s4 -= s13 * 997805;
330  s5 += s13 * 136657;
331  s6 -= s13 * 683901;
332  s13 = 0;
333 
334  s0 += s12 * 666643;
335  s1 += s12 * 470296;
336  s2 += s12 * 654183;
337  s3 -= s12 * 997805;
338  s4 += s12 * 136657;
339  s5 -= s12 * 683901;
340  s12 = 0;
341 
342  carry0 = (s0 + X20) >> 21;
343  s1 += carry0;
344  s0 -= carry0 * X21;
345  carry2 = (s2 + X20) >> 21;
346  s3 += carry2;
347  s2 -= carry2 * X21;
348  carry4 = (s4 + X20) >> 21;
349  s5 += carry4;
350  s4 -= carry4 * X21;
351  carry6 = (s6 + X20) >> 21;
352  s7 += carry6;
353  s6 -= carry6 * X21;
354  carry8 = (s8 + X20) >> 21;
355  s9 += carry8;
356  s8 -= carry8 * X21;
357  carry10 = (s10 + X20) >> 21;
358  s11 += carry10;
359  s10 -= carry10 * X21;
360 
361  carry1 = (s1 + X20) >> 21;
362  s2 += carry1;
363  s1 -= carry1 * X21;
364  carry3 = (s3 + X20) >> 21;
365  s4 += carry3;
366  s3 -= carry3 * X21;
367  carry5 = (s5 + X20) >> 21;
368  s6 += carry5;
369  s5 -= carry5 * X21;
370  carry7 = (s7 + X20) >> 21;
371  s8 += carry7;
372  s7 -= carry7 * X21;
373  carry9 = (s9 + X20) >> 21;
374  s10 += carry9;
375  s9 -= carry9 * X21;
376  carry11 = (s11 + X20) >> 21;
377  s12 += carry11;
378  s11 -= carry11 * X21;
379 
380  s0 += s12 * 666643;
381  s1 += s12 * 470296;
382  s2 += s12 * 654183;
383  s3 -= s12 * 997805;
384  s4 += s12 * 136657;
385  s5 -= s12 * 683901;
386  s12 = 0;
387 
388  carry0 = s0 >> 21;
389  s1 += carry0;
390  s0 -= carry0 * X21;
391  carry1 = s1 >> 21;
392  s2 += carry1;
393  s1 -= carry1 * X21;
394  carry2 = s2 >> 21;
395  s3 += carry2;
396  s2 -= carry2 * X21;
397  carry3 = s3 >> 21;
398  s4 += carry3;
399  s3 -= carry3 * X21;
400  carry4 = s4 >> 21;
401  s5 += carry4;
402  s4 -= carry4 * X21;
403  carry5 = s5 >> 21;
404  s6 += carry5;
405  s5 -= carry5 * X21;
406  carry6 = s6 >> 21;
407  s7 += carry6;
408  s6 -= carry6 * X21;
409  carry7 = s7 >> 21;
410  s8 += carry7;
411  s7 -= carry7 * X21;
412  carry8 = s8 >> 21;
413  s9 += carry8;
414  s8 -= carry8 * X21;
415  carry9 = s9 >> 21;
416  s10 += carry9;
417  s9 -= carry9 * X21;
418  carry10 = s10 >> 21;
419  s11 += carry10;
420  s10 -= carry10 * X21;
421  carry11 = s11 >> 21;
422  s12 += carry11;
423  s11 -= carry11 * X21;
424 
425  s0 += s12 * 666643;
426  s1 += s12 * 470296;
427  s2 += s12 * 654183;
428  s3 -= s12 * 997805;
429  s4 += s12 * 136657;
430  s5 -= s12 * 683901;
431  s12 = 0;
432 
433  carry0 = s0 >> 21;
434  s1 += carry0;
435  s0 -= carry0 * X21;
436  carry1 = s1 >> 21;
437  s2 += carry1;
438  s1 -= carry1 * X21;
439  carry2 = s2 >> 21;
440  s3 += carry2;
441  s2 -= carry2 * X21;
442  carry3 = s3 >> 21;
443  s4 += carry3;
444  s3 -= carry3 * X21;
445  carry4 = s4 >> 21;
446  s5 += carry4;
447  s4 -= carry4 * X21;
448  carry5 = s5 >> 21;
449  s6 += carry5;
450  s5 -= carry5 * X21;
451  carry6 = s6 >> 21;
452  s7 += carry6;
453  s6 -= carry6 * X21;
454  carry7 = s7 >> 21;
455  s8 += carry7;
456  s7 -= carry7 * X21;
457  carry8 = s8 >> 21;
458  s9 += carry8;
459  s8 -= carry8 * X21;
460  carry9 = s9 >> 21;
461  s10 += carry9;
462  s9 -= carry9 * X21;
463  carry10 = s10 >> 21;
464  s11 += carry10;
465  s10 -= carry10 * X21;
466 
467  s[0] = s0 >> 0;
468  s[1] = s0 >> 8;
469  s[2] = (s0 >> 16) | (s1 << 5);
470  s[3] = s1 >> 3;
471  s[4] = s1 >> 11;
472  s[5] = (s1 >> 19) | (s2 << 2);
473  s[6] = s2 >> 6;
474  s[7] = (s2 >> 14) | (s3 << 7);
475  s[8] = s3 >> 1;
476  s[9] = s3 >> 9;
477  s[10] = (s3 >> 17) | (s4 << 4);
478  s[11] = s4 >> 4;
479  s[12] = s4 >> 12;
480  s[13] = (s4 >> 20) | (s5 << 1);
481  s[14] = s5 >> 7;
482  s[15] = (s5 >> 15) | (s6 << 6);
483  s[16] = s6 >> 2;
484  s[17] = s6 >> 10;
485  s[18] = (s6 >> 18) | (s7 << 3);
486  s[19] = s7 >> 5;
487  s[20] = s7 >> 13;
488  s[21] = s8 >> 0;
489  s[22] = s8 >> 8;
490  s[23] = (s8 >> 16) | (s9 << 5);
491  s[24] = s9 >> 3;
492  s[25] = s9 >> 11;
493  s[26] = (s9 >> 19) | (s10 << 2);
494  s[27] = s10 >> 6;
495  s[28] = (s10 >> 14) | (s11 << 7);
496  s[29] = s11 >> 1;
497  s[30] = s11 >> 9;
498  s[31] = s11 >> 17;
499  }
500 
501 }
void sc_muladd(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *)
Definition: sc_muladd.cpp:26
uint64_t load_4(const uint8_t *in)
uint64_t load_3(const uint8_t in[3])
Definition: alg_id.cpp:13