4 static uint64_t load_3(const unsigned char *in) {
8 result |= shlu64(in[1], 8);
9 result |= shlu64(in[2], 16);
14 static uint64_t load_4(const unsigned char *in) {
18 result |= shlu64(in[1], 8);
19 result |= shlu64(in[2], 16);
20 result |= shlu64(in[3], 24);
27 s[0]+256*s[1]+...+256^63*s[63] = s
30 s[0]+256*s[1]+...+256^31*s[31] = s mod l
31 where l = 2^252 + 27742317777372353535851937790883648493.
32 Overwrites s in place.
35 void sc_reduce(unsigned char *s) {
36 int64_t s0 = 2097151 & load_3(s);
37 int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
38 int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
39 int64_t s3 = 2097151 & (load_4(s + 7) >> 7);
40 int64_t s4 = 2097151 & (load_4(s + 10) >> 4);
41 int64_t s5 = 2097151 & (load_3(s + 13) >> 1);
42 int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
43 int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
44 int64_t s8 = 2097151 & load_3(s + 21);
45 int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
46 int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
47 int64_t s11 = 2097151 & (load_4(s + 28) >> 7);
48 int64_t s12 = 2097151 & (load_4(s + 31) >> 4);
49 int64_t s13 = 2097151 & (load_3(s + 34) >> 1);
50 int64_t s14 = 2097151 & (load_4(s + 36) >> 6);
51 int64_t s15 = 2097151 & (load_3(s + 39) >> 3);
52 int64_t s16 = 2097151 & load_3(s + 42);
53 int64_t s17 = 2097151 & (load_4(s + 44) >> 5);
54 int64_t s18 = 2097151 & (load_3(s + 47) >> 2);
55 int64_t s19 = 2097151 & (load_4(s + 49) >> 7);
56 int64_t s20 = 2097151 & (load_4(s + 52) >> 4);
57 int64_t s21 = 2097151 & (load_3(s + 55) >> 1);
58 int64_t s22 = 2097151 & (load_4(s + 57) >> 6);
59 int64_t s23 = (load_4(s + 60) >> 3);
114 carry6 = (s6 + (1 << 20)) >> 21;
116 s6 -= shl64(carry6, 21);
117 carry8 = (s8 + (1 << 20)) >> 21;
119 s8 -= shl64(carry8, 21);
120 carry10 = (s10 + (1 << 20)) >> 21;
122 s10 -= shl64(carry10, 21);
123 carry12 = (s12 + (1 << 20)) >> 21;
125 s12 -= shl64(carry12, 21);
126 carry14 = (s14 + (1 << 20)) >> 21;
128 s14 -= shl64(carry14, 21);
129 carry16 = (s16 + (1 << 20)) >> 21;
131 s16 -= shl64(carry16, 21);
132 carry7 = (s7 + (1 << 20)) >> 21;
134 s7 -= shl64(carry7, 21);
135 carry9 = (s9 + (1 << 20)) >> 21;
137 s9 -= shl64(carry9, 21);
138 carry11 = (s11 + (1 << 20)) >> 21;
140 s11 -= shl64(carry11, 21);
141 carry13 = (s13 + (1 << 20)) >> 21;
143 s13 -= shl64(carry13, 21);
144 carry15 = (s15 + (1 << 20)) >> 21;
146 s15 -= shl64(carry15, 21);
184 carry0 = (s0 + (1 << 20)) >> 21;
186 s0 -= shl64(carry0, 21);
187 carry2 = (s2 + (1 << 20)) >> 21;
189 s2 -= shl64(carry2, 21);
190 carry4 = (s4 + (1 << 20)) >> 21;
192 s4 -= shl64(carry4, 21);
193 carry6 = (s6 + (1 << 20)) >> 21;
195 s6 -= shl64(carry6, 21);
196 carry8 = (s8 + (1 << 20)) >> 21;
198 s8 -= shl64(carry8, 21);
199 carry10 = (s10 + (1 << 20)) >> 21;
201 s10 -= shl64(carry10, 21);
202 carry1 = (s1 + (1 << 20)) >> 21;
204 s1 -= shl64(carry1, 21);
205 carry3 = (s3 + (1 << 20)) >> 21;
207 s3 -= shl64(carry3, 21);
208 carry5 = (s5 + (1 << 20)) >> 21;
210 s5 -= shl64(carry5, 21);
211 carry7 = (s7 + (1 << 20)) >> 21;
213 s7 -= shl64(carry7, 21);
214 carry9 = (s9 + (1 << 20)) >> 21;
216 s9 -= shl64(carry9, 21);
217 carry11 = (s11 + (1 << 20)) >> 21;
219 s11 -= shl64(carry11, 21);
229 s0 -= shl64(carry0, 21);
232 s1 -= shl64(carry1, 21);
235 s2 -= shl64(carry2, 21);
238 s3 -= shl64(carry3, 21);
241 s4 -= shl64(carry4, 21);
244 s5 -= shl64(carry5, 21);
247 s6 -= shl64(carry6, 21);
250 s7 -= shl64(carry7, 21);
253 s8 -= shl64(carry8, 21);
256 s9 -= shl64(carry9, 21);
259 s10 -= shl64(carry10, 21);
262 s11 -= shl64(carry11, 21);
271 s0 -= shl64(carry0, 21);
274 s1 -= shl64(carry1, 21);
277 s2 -= shl64(carry2, 21);
280 s3 -= shl64(carry3, 21);
283 s4 -= shl64(carry4, 21);
286 s5 -= shl64(carry5, 21);
289 s6 -= shl64(carry6, 21);
292 s7 -= shl64(carry7, 21);
295 s8 -= shl64(carry8, 21);
298 s9 -= shl64(carry9, 21);
301 s10 -= shl64(carry10, 21);
303 s[0] = (unsigned char)(s0 >> 0);
304 s[1] = (unsigned char)(s0 >> 8);
305 s[2] = (unsigned char)((s0 >> 16) | shl64(s1, 5));
306 s[3] = (unsigned char)(s1 >> 3);
307 s[4] = (unsigned char)(s1 >> 11);
308 s[5] = (unsigned char)((s1 >> 19) | shl64(s2, 2));
309 s[6] = (unsigned char)(s2 >> 6);
310 s[7] = (unsigned char)((s2 >> 14) | shl64(s3, 7));
311 s[8] = (unsigned char)(s3 >> 1);
312 s[9] = (unsigned char)(s3 >> 9);
313 s[10] = (unsigned char)((s3 >> 17) | shl64(s4, 4));
314 s[11] = (unsigned char)(s4 >> 4);
315 s[12] = (unsigned char)(s4 >> 12);
316 s[13] = (unsigned char)((s4 >> 20) | shl64(s5, 1));
317 s[14] = (unsigned char)(s5 >> 7);
318 s[15] = (unsigned char)((s5 >> 15) | shl64(s6, 6));
319 s[16] = (unsigned char)(s6 >> 2);
320 s[17] = (unsigned char)(s6 >> 10);
321 s[18] = (unsigned char)((s6 >> 18) | shl64(s7, 3));
322 s[19] = (unsigned char)(s7 >> 5);
323 s[20] = (unsigned char)(s7 >> 13);
324 s[21] = (unsigned char)(s8 >> 0);
325 s[22] = (unsigned char)(s8 >> 8);
326 s[23] = (unsigned char)((s8 >> 16) | shl64(s9, 5));
327 s[24] = (unsigned char)(s9 >> 3);
328 s[25] = (unsigned char)(s9 >> 11);
329 s[26] = (unsigned char)((s9 >> 19) | shl64(s10, 2));
330 s[27] = (unsigned char)(s10 >> 6);
331 s[28] = (unsigned char)((s10 >> 14) | shl64(s11, 7));
332 s[29] = (unsigned char)(s11 >> 1);
333 s[30] = (unsigned char)(s11 >> 9);
334 s[31] = (unsigned char)(s11 >> 17);
341 a[0]+256*a[1]+...+256^31*a[31] = a
342 b[0]+256*b[1]+...+256^31*b[31] = b
343 c[0]+256*c[1]+...+256^31*c[31] = c
346 s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
347 where l = 2^252 + 27742317777372353535851937790883648493.
350 void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c) {
351 int64_t a0 = 2097151 & load_3(a);
352 int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
353 int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
354 int64_t a3 = 2097151 & (load_4(a + 7) >> 7);
355 int64_t a4 = 2097151 & (load_4(a + 10) >> 4);
356 int64_t a5 = 2097151 & (load_3(a + 13) >> 1);
357 int64_t a6 = 2097151 & (load_4(a + 15) >> 6);
358 int64_t a7 = 2097151 & (load_3(a + 18) >> 3);
359 int64_t a8 = 2097151 & load_3(a + 21);
360 int64_t a9 = 2097151 & (load_4(a + 23) >> 5);
361 int64_t a10 = 2097151 & (load_3(a + 26) >> 2);
362 int64_t a11 = (load_4(a + 28) >> 7);
363 int64_t b0 = 2097151 & load_3(b);
364 int64_t b1 = 2097151 & (load_4(b + 2) >> 5);
365 int64_t b2 = 2097151 & (load_3(b + 5) >> 2);
366 int64_t b3 = 2097151 & (load_4(b + 7) >> 7);
367 int64_t b4 = 2097151 & (load_4(b + 10) >> 4);
368 int64_t b5 = 2097151 & (load_3(b + 13) >> 1);
369 int64_t b6 = 2097151 & (load_4(b + 15) >> 6);
370 int64_t b7 = 2097151 & (load_3(b + 18) >> 3);
371 int64_t b8 = 2097151 & load_3(b + 21);
372 int64_t b9 = 2097151 & (load_4(b + 23) >> 5);
373 int64_t b10 = 2097151 & (load_3(b + 26) >> 2);
374 int64_t b11 = (load_4(b + 28) >> 7);
375 int64_t c0 = 2097151 & load_3(c);
376 int64_t c1 = 2097151 & (load_4(c + 2) >> 5);
377 int64_t c2 = 2097151 & (load_3(c + 5) >> 2);
378 int64_t c3 = 2097151 & (load_4(c + 7) >> 7);
379 int64_t c4 = 2097151 & (load_4(c + 10) >> 4);
380 int64_t c5 = 2097151 & (load_3(c + 13) >> 1);
381 int64_t c6 = 2097151 & (load_4(c + 15) >> 6);
382 int64_t c7 = 2097151 & (load_3(c + 18) >> 3);
383 int64_t c8 = 2097151 & load_3(c + 21);
384 int64_t c9 = 2097151 & (load_4(c + 23) >> 5);
385 int64_t c10 = 2097151 & (load_3(c + 26) >> 2);
386 int64_t c11 = (load_4(c + 28) >> 7);
436 s1 = c1 + a0 * b1 + a1 * b0;
437 s2 = c2 + a0 * b2 + a1 * b1 + a2 * b0;
438 s3 = c3 + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0;
439 s4 = c4 + a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0;
440 s5 = c5 + a0 * b5 + a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1 + a5 * b0;
441 s6 = c6 + a0 * b6 + a1 * b5 + a2 * b4 + a3 * b3 + a4 * b2 + a5 * b1 + a6 * b0;
442 s7 = c7 + a0 * b7 + a1 * b6 + a2 * b5 + a3 * b4 + a4 * b3 + a5 * b2 + a6 * b1 + a7 * b0;
443 s8 = c8 + a0 * b8 + a1 * b7 + a2 * b6 + a3 * b5 + a4 * b4 + a5 * b3 + a6 * b2 + a7 * b1 + a8 * b0;
444 s9 = c9 + a0 * b9 + a1 * b8 + a2 * b7 + a3 * b6 + a4 * b5 + a5 * b4 + a6 * b3 + a7 * b2 + a8 * b1 + a9 * b0;
445 s10 = c10 + a0 * b10 + a1 * b9 + a2 * b8 + a3 * b7 + a4 * b6 + a5 * b5 + a6 * b4 + a7 * b3 + a8 * b2 + a9 * b1 + a10 * b0;
446 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;
447 s12 = a1 * b11 + a2 * b10 + a3 * b9 + a4 * b8 + a5 * b7 + a6 * b6 + a7 * b5 + a8 * b4 + a9 * b3 + a10 * b2 + a11 * b1;
448 s13 = a2 * b11 + a3 * b10 + a4 * b9 + a5 * b8 + a6 * b7 + a7 * b6 + a8 * b5 + a9 * b4 + a10 * b3 + a11 * b2;
449 s14 = a3 * b11 + a4 * b10 + a5 * b9 + a6 * b8 + a7 * b7 + a8 * b6 + a9 * b5 + a10 * b4 + a11 * b3;
450 s15 = a4 * b11 + a5 * b10 + a6 * b9 + a7 * b8 + a8 * b7 + a9 * b6 + a10 * b5 + a11 * b4;
451 s16 = a5 * b11 + a6 * b10 + a7 * b9 + a8 * b8 + a9 * b7 + a10 * b6 + a11 * b5;
452 s17 = a6 * b11 + a7 * b10 + a8 * b9 + a9 * b8 + a10 * b7 + a11 * b6;
453 s18 = a7 * b11 + a8 * b10 + a9 * b9 + a10 * b8 + a11 * b7;
454 s19 = a8 * b11 + a9 * b10 + a10 * b9 + a11 * b8;
455 s20 = a9 * b11 + a10 * b10 + a11 * b9;
456 s21 = a10 * b11 + a11 * b10;
459 carry0 = (s0 + (1 << 20)) >> 21;
461 s0 -= shl64(carry0, 21);
462 carry2 = (s2 + (1 << 20)) >> 21;
464 s2 -= shl64(carry2, 21);
465 carry4 = (s4 + (1 << 20)) >> 21;
467 s4 -= shl64(carry4, 21);
468 carry6 = (s6 + (1 << 20)) >> 21;
470 s6 -= shl64(carry6, 21);
471 carry8 = (s8 + (1 << 20)) >> 21;
473 s8 -= shl64(carry8, 21);
474 carry10 = (s10 + (1 << 20)) >> 21;
476 s10 -= shl64(carry10, 21);
477 carry12 = (s12 + (1 << 20)) >> 21;
479 s12 -= shl64(carry12, 21);
480 carry14 = (s14 + (1 << 20)) >> 21;
482 s14 -= shl64(carry14, 21);
483 carry16 = (s16 + (1 << 20)) >> 21;
485 s16 -= shl64(carry16, 21);
486 carry18 = (s18 + (1 << 20)) >> 21;
488 s18 -= shl64(carry18, 21);
489 carry20 = (s20 + (1 << 20)) >> 21;
491 s20 -= shl64(carry20, 21);
492 carry22 = (s22 + (1 << 20)) >> 21;
494 s22 -= shl64(carry22, 21);
495 carry1 = (s1 + (1 << 20)) >> 21;
497 s1 -= shl64(carry1, 21);
498 carry3 = (s3 + (1 << 20)) >> 21;
500 s3 -= shl64(carry3, 21);
501 carry5 = (s5 + (1 << 20)) >> 21;
503 s5 -= shl64(carry5, 21);
504 carry7 = (s7 + (1 << 20)) >> 21;
506 s7 -= shl64(carry7, 21);
507 carry9 = (s9 + (1 << 20)) >> 21;
509 s9 -= shl64(carry9, 21);
510 carry11 = (s11 + (1 << 20)) >> 21;
512 s11 -= shl64(carry11, 21);
513 carry13 = (s13 + (1 << 20)) >> 21;
515 s13 -= shl64(carry13, 21);
516 carry15 = (s15 + (1 << 20)) >> 21;
518 s15 -= shl64(carry15, 21);
519 carry17 = (s17 + (1 << 20)) >> 21;
521 s17 -= shl64(carry17, 21);
522 carry19 = (s19 + (1 << 20)) >> 21;
524 s19 -= shl64(carry19, 21);
525 carry21 = (s21 + (1 << 20)) >> 21;
527 s21 -= shl64(carry21, 21);
564 carry6 = (s6 + (1 << 20)) >> 21;
566 s6 -= shl64(carry6, 21);
567 carry8 = (s8 + (1 << 20)) >> 21;
569 s8 -= shl64(carry8, 21);
570 carry10 = (s10 + (1 << 20)) >> 21;
572 s10 -= shl64(carry10, 21);
573 carry12 = (s12 + (1 << 20)) >> 21;
575 s12 -= shl64(carry12, 21);
576 carry14 = (s14 + (1 << 20)) >> 21;
578 s14 -= shl64(carry14, 21);
579 carry16 = (s16 + (1 << 20)) >> 21;
581 s16 -= shl64(carry16, 21);
582 carry7 = (s7 + (1 << 20)) >> 21;
584 s7 -= shl64(carry7, 21);
585 carry9 = (s9 + (1 << 20)) >> 21;
587 s9 -= shl64(carry9, 21);
588 carry11 = (s11 + (1 << 20)) >> 21;
590 s11 -= shl64(carry11, 21);
591 carry13 = (s13 + (1 << 20)) >> 21;
593 s13 -= shl64(carry13, 21);
594 carry15 = (s15 + (1 << 20)) >> 21;
596 s15 -= shl64(carry15, 21);
634 carry0 = (s0 + (1 << 20)) >> 21;
636 s0 -= shl64(carry0, 21);
637 carry2 = (s2 + (1 << 20)) >> 21;
639 s2 -= shl64(carry2, 21);
640 carry4 = (s4 + (1 << 20)) >> 21;
642 s4 -= shl64(carry4, 21);
643 carry6 = (s6 + (1 << 20)) >> 21;
645 s6 -= shl64(carry6, 21);
646 carry8 = (s8 + (1 << 20)) >> 21;
648 s8 -= shl64(carry8, 21);
649 carry10 = (s10 + (1 << 20)) >> 21;
651 s10 -= shl64(carry10, 21);
652 carry1 = (s1 + (1 << 20)) >> 21;
654 s1 -= shl64(carry1, 21);
655 carry3 = (s3 + (1 << 20)) >> 21;
657 s3 -= shl64(carry3, 21);
658 carry5 = (s5 + (1 << 20)) >> 21;
660 s5 -= shl64(carry5, 21);
661 carry7 = (s7 + (1 << 20)) >> 21;
663 s7 -= shl64(carry7, 21);
664 carry9 = (s9 + (1 << 20)) >> 21;
666 s9 -= shl64(carry9, 21);
667 carry11 = (s11 + (1 << 20)) >> 21;
669 s11 -= shl64(carry11, 21);
679 s0 -= shl64(carry0, 21);
682 s1 -= shl64(carry1, 21);
685 s2 -= shl64(carry2, 21);
688 s3 -= shl64(carry3, 21);
691 s4 -= shl64(carry4, 21);
694 s5 -= shl64(carry5, 21);
697 s6 -= shl64(carry6, 21);
700 s7 -= shl64(carry7, 21);
703 s8 -= shl64(carry8, 21);
706 s9 -= shl64(carry9, 21);
709 s10 -= shl64(carry10, 21);
712 s11 -= shl64(carry11, 21);
721 s0 -= shl64(carry0, 21);
724 s1 -= shl64(carry1, 21);
727 s2 -= shl64(carry2, 21);
730 s3 -= shl64(carry3, 21);
733 s4 -= shl64(carry4, 21);
736 s5 -= shl64(carry5, 21);
739 s6 -= shl64(carry6, 21);
742 s7 -= shl64(carry7, 21);
745 s8 -= shl64(carry8, 21);
748 s9 -= shl64(carry9, 21);
751 s10 -= shl64(carry10, 21);
753 s[0] = (unsigned char)(s0 >> 0);
754 s[1] = (unsigned char)(s0 >> 8);
755 s[2] = (unsigned char)((s0 >> 16) | shl64(s1, 5));
756 s[3] = (unsigned char)(s1 >> 3);
757 s[4] = (unsigned char)(s1 >> 11);
758 s[5] = (unsigned char)((s1 >> 19) | shl64(s2, 2));
759 s[6] = (unsigned char)(s2 >> 6);
760 s[7] = (unsigned char)((s2 >> 14) | shl64(s3, 7));
761 s[8] = (unsigned char)(s3 >> 1);
762 s[9] = (unsigned char)(s3 >> 9);
763 s[10] = (unsigned char)((s3 >> 17) | shl64(s4, 4));
764 s[11] = (unsigned char)(s4 >> 4);
765 s[12] = (unsigned char)(s4 >> 12);
766 s[13] = (unsigned char)((s4 >> 20) | shl64(s5, 1));
767 s[14] = (unsigned char)(s5 >> 7);
768 s[15] = (unsigned char)((s5 >> 15) | shl64(s6, 6));
769 s[16] = (unsigned char)(s6 >> 2);
770 s[17] = (unsigned char)(s6 >> 10);
771 s[18] = (unsigned char)((s6 >> 18) | shl64(s7, 3));
772 s[19] = (unsigned char)(s7 >> 5);
773 s[20] = (unsigned char)(s7 >> 13);
774 s[21] = (unsigned char)(s8 >> 0);
775 s[22] = (unsigned char)(s8 >> 8);
776 s[23] = (unsigned char)((s8 >> 16) | shl64(s9, 5));
777 s[24] = (unsigned char)(s9 >> 3);
778 s[25] = (unsigned char)(s9 >> 11);
779 s[26] = (unsigned char)((s9 >> 19) | shl64(s10, 2));
780 s[27] = (unsigned char)(s10 >> 6);
781 s[28] = (unsigned char)((s10 >> 14) | shl64(s11, 7));
782 s[29] = (unsigned char)(s11 >> 1);
783 s[30] = (unsigned char)(s11 >> 9);
784 s[31] = (unsigned char)(s11 >> 17);