123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #include "sc.h"
- #include "crypto_int64.h"
- #include "crypto_uint32.h"
- #include "crypto_uint64.h"
- static crypto_uint64 load_3(const unsigned char *in)
- {
- crypto_uint64 result;
- result = (crypto_uint64) in[0];
- result |= ((crypto_uint64) in[1]) << 8;
- result |= ((crypto_uint64) in[2]) << 16;
- return result;
- }
- static crypto_uint64 load_4(const unsigned char *in)
- {
- crypto_uint64 result;
- result = (crypto_uint64) in[0];
- result |= ((crypto_uint64) in[1]) << 8;
- result |= ((crypto_uint64) in[2]) << 16;
- result |= ((crypto_uint64) in[3]) << 24;
- return result;
- }
- /*
- Input:
- s[0]+256*s[1]+...+256^63*s[63] = s
- Output:
- s[0]+256*s[1]+...+256^31*s[31] = s mod l
- where l = 2^252 + 27742317777372353535851937790883648493.
- Overwrites s in place.
- */
- void sc_reduce(unsigned char *s)
- {
- crypto_int64 s0 = 2097151 & load_3(s);
- crypto_int64 s1 = 2097151 & (load_4(s + 2) >> 5);
- crypto_int64 s2 = 2097151 & (load_3(s + 5) >> 2);
- crypto_int64 s3 = 2097151 & (load_4(s + 7) >> 7);
- crypto_int64 s4 = 2097151 & (load_4(s + 10) >> 4);
- crypto_int64 s5 = 2097151 & (load_3(s + 13) >> 1);
- crypto_int64 s6 = 2097151 & (load_4(s + 15) >> 6);
- crypto_int64 s7 = 2097151 & (load_3(s + 18) >> 3);
- crypto_int64 s8 = 2097151 & load_3(s + 21);
- crypto_int64 s9 = 2097151 & (load_4(s + 23) >> 5);
- crypto_int64 s10 = 2097151 & (load_3(s + 26) >> 2);
- crypto_int64 s11 = 2097151 & (load_4(s + 28) >> 7);
- crypto_int64 s12 = 2097151 & (load_4(s + 31) >> 4);
- crypto_int64 s13 = 2097151 & (load_3(s + 34) >> 1);
- crypto_int64 s14 = 2097151 & (load_4(s + 36) >> 6);
- crypto_int64 s15 = 2097151 & (load_3(s + 39) >> 3);
- crypto_int64 s16 = 2097151 & load_3(s + 42);
- crypto_int64 s17 = 2097151 & (load_4(s + 44) >> 5);
- crypto_int64 s18 = 2097151 & (load_3(s + 47) >> 2);
- crypto_int64 s19 = 2097151 & (load_4(s + 49) >> 7);
- crypto_int64 s20 = 2097151 & (load_4(s + 52) >> 4);
- crypto_int64 s21 = 2097151 & (load_3(s + 55) >> 1);
- crypto_int64 s22 = 2097151 & (load_4(s + 57) >> 6);
- crypto_int64 s23 = (load_4(s + 60) >> 3);
- crypto_int64 carry0;
- crypto_int64 carry1;
- crypto_int64 carry2;
- crypto_int64 carry3;
- crypto_int64 carry4;
- crypto_int64 carry5;
- crypto_int64 carry6;
- crypto_int64 carry7;
- crypto_int64 carry8;
- crypto_int64 carry9;
- crypto_int64 carry10;
- crypto_int64 carry11;
- crypto_int64 carry12;
- crypto_int64 carry13;
- crypto_int64 carry14;
- crypto_int64 carry15;
- crypto_int64 carry16;
- s11 += s23 * 666643;
- s12 += s23 * 470296;
- s13 += s23 * 654183;
- s14 -= s23 * 997805;
- s15 += s23 * 136657;
- s16 -= s23 * 683901;
- s23 = 0;
- s10 += s22 * 666643;
- s11 += s22 * 470296;
- s12 += s22 * 654183;
- s13 -= s22 * 997805;
- s14 += s22 * 136657;
- s15 -= s22 * 683901;
- s22 = 0;
- s9 += s21 * 666643;
- s10 += s21 * 470296;
- s11 += s21 * 654183;
- s12 -= s21 * 997805;
- s13 += s21 * 136657;
- s14 -= s21 * 683901;
- s21 = 0;
- s8 += s20 * 666643;
- s9 += s20 * 470296;
- s10 += s20 * 654183;
- s11 -= s20 * 997805;
- s12 += s20 * 136657;
- s13 -= s20 * 683901;
- s20 = 0;
- s7 += s19 * 666643;
- s8 += s19 * 470296;
- s9 += s19 * 654183;
- s10 -= s19 * 997805;
- s11 += s19 * 136657;
- s12 -= s19 * 683901;
- s19 = 0;
- s6 += s18 * 666643;
- s7 += s18 * 470296;
- s8 += s18 * 654183;
- s9 -= s18 * 997805;
- s10 += s18 * 136657;
- s11 -= s18 * 683901;
- s18 = 0;
- carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
- carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
- carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
- carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= SHL64(carry12,21);
- carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= SHL64(carry14,21);
- carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= SHL64(carry16,21);
- carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
- carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
- carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
- carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= SHL64(carry13,21);
- carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= SHL64(carry15,21);
- s5 += s17 * 666643;
- s6 += s17 * 470296;
- s7 += s17 * 654183;
- s8 -= s17 * 997805;
- s9 += s17 * 136657;
- s10 -= s17 * 683901;
- s17 = 0;
- s4 += s16 * 666643;
- s5 += s16 * 470296;
- s6 += s16 * 654183;
- s7 -= s16 * 997805;
- s8 += s16 * 136657;
- s9 -= s16 * 683901;
- s16 = 0;
- s3 += s15 * 666643;
- s4 += s15 * 470296;
- s5 += s15 * 654183;
- s6 -= s15 * 997805;
- s7 += s15 * 136657;
- s8 -= s15 * 683901;
- s15 = 0;
- s2 += s14 * 666643;
- s3 += s14 * 470296;
- s4 += s14 * 654183;
- s5 -= s14 * 997805;
- s6 += s14 * 136657;
- s7 -= s14 * 683901;
- s14 = 0;
- s1 += s13 * 666643;
- s2 += s13 * 470296;
- s3 += s13 * 654183;
- s4 -= s13 * 997805;
- s5 += s13 * 136657;
- s6 -= s13 * 683901;
- s13 = 0;
- s0 += s12 * 666643;
- s1 += s12 * 470296;
- s2 += s12 * 654183;
- s3 -= s12 * 997805;
- s4 += s12 * 136657;
- s5 -= s12 * 683901;
- s12 = 0;
- carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
- carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
- carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
- carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
- carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
- carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
- carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
- carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
- carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
- carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
- carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
- carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
- s0 += s12 * 666643;
- s1 += s12 * 470296;
- s2 += s12 * 654183;
- s3 -= s12 * 997805;
- s4 += s12 * 136657;
- s5 -= s12 * 683901;
- s12 = 0;
- carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
- carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
- carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
- carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
- carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
- carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
- carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
- carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
- carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
- carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
- carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
- carry11 = s11 >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
- s0 += s12 * 666643;
- s1 += s12 * 470296;
- s2 += s12 * 654183;
- s3 -= s12 * 997805;
- s4 += s12 * 136657;
- s5 -= s12 * 683901;
- s12 = 0;
- carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
- carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
- carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
- carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
- carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
- carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
- carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
- carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
- carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
- carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
- carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
- s[0] = s0 >> 0;
- s[1] = s0 >> 8;
- s[2] = (s0 >> 16) | SHL64(s1,5);
- s[3] = s1 >> 3;
- s[4] = s1 >> 11;
- s[5] = (s1 >> 19) | SHL64(s2,2);
- s[6] = s2 >> 6;
- s[7] = (s2 >> 14) | SHL64(s3,7);
- s[8] = s3 >> 1;
- s[9] = s3 >> 9;
- s[10] = (s3 >> 17) | SHL64(s4,4);
- s[11] = s4 >> 4;
- s[12] = s4 >> 12;
- s[13] = (s4 >> 20) | SHL64(s5,1);
- s[14] = s5 >> 7;
- s[15] = (s5 >> 15) | SHL64(s6,6);
- s[16] = s6 >> 2;
- s[17] = s6 >> 10;
- s[18] = (s6 >> 18) | SHL64(s7,3);
- s[19] = s7 >> 5;
- s[20] = s7 >> 13;
- s[21] = s8 >> 0;
- s[22] = s8 >> 8;
- s[23] = (s8 >> 16) | SHL64(s9,5);
- s[24] = s9 >> 3;
- s[25] = s9 >> 11;
- s[26] = (s9 >> 19) | SHL64(s10,2);
- s[27] = s10 >> 6;
- s[28] = (s10 >> 14) | SHL64(s11,7);
- s[29] = s11 >> 1;
- s[30] = s11 >> 9;
- s[31] = s11 >> 17;
- }
|