sc_reduce.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "sc.h"
  2. #include "crypto_int64.h"
  3. #include "crypto_uint32.h"
  4. #include "crypto_uint64.h"
  5. static crypto_uint64 load_3(const unsigned char *in)
  6. {
  7. crypto_uint64 result;
  8. result = (crypto_uint64) in[0];
  9. result |= ((crypto_uint64) in[1]) << 8;
  10. result |= ((crypto_uint64) in[2]) << 16;
  11. return result;
  12. }
  13. static crypto_uint64 load_4(const unsigned char *in)
  14. {
  15. crypto_uint64 result;
  16. result = (crypto_uint64) in[0];
  17. result |= ((crypto_uint64) in[1]) << 8;
  18. result |= ((crypto_uint64) in[2]) << 16;
  19. result |= ((crypto_uint64) in[3]) << 24;
  20. return result;
  21. }
  22. /*
  23. Input:
  24. s[0]+256*s[1]+...+256^63*s[63] = s
  25. Output:
  26. s[0]+256*s[1]+...+256^31*s[31] = s mod l
  27. where l = 2^252 + 27742317777372353535851937790883648493.
  28. Overwrites s in place.
  29. */
  30. void sc_reduce(unsigned char *s)
  31. {
  32. crypto_int64 s0 = 2097151 & load_3(s);
  33. crypto_int64 s1 = 2097151 & (load_4(s + 2) >> 5);
  34. crypto_int64 s2 = 2097151 & (load_3(s + 5) >> 2);
  35. crypto_int64 s3 = 2097151 & (load_4(s + 7) >> 7);
  36. crypto_int64 s4 = 2097151 & (load_4(s + 10) >> 4);
  37. crypto_int64 s5 = 2097151 & (load_3(s + 13) >> 1);
  38. crypto_int64 s6 = 2097151 & (load_4(s + 15) >> 6);
  39. crypto_int64 s7 = 2097151 & (load_3(s + 18) >> 3);
  40. crypto_int64 s8 = 2097151 & load_3(s + 21);
  41. crypto_int64 s9 = 2097151 & (load_4(s + 23) >> 5);
  42. crypto_int64 s10 = 2097151 & (load_3(s + 26) >> 2);
  43. crypto_int64 s11 = 2097151 & (load_4(s + 28) >> 7);
  44. crypto_int64 s12 = 2097151 & (load_4(s + 31) >> 4);
  45. crypto_int64 s13 = 2097151 & (load_3(s + 34) >> 1);
  46. crypto_int64 s14 = 2097151 & (load_4(s + 36) >> 6);
  47. crypto_int64 s15 = 2097151 & (load_3(s + 39) >> 3);
  48. crypto_int64 s16 = 2097151 & load_3(s + 42);
  49. crypto_int64 s17 = 2097151 & (load_4(s + 44) >> 5);
  50. crypto_int64 s18 = 2097151 & (load_3(s + 47) >> 2);
  51. crypto_int64 s19 = 2097151 & (load_4(s + 49) >> 7);
  52. crypto_int64 s20 = 2097151 & (load_4(s + 52) >> 4);
  53. crypto_int64 s21 = 2097151 & (load_3(s + 55) >> 1);
  54. crypto_int64 s22 = 2097151 & (load_4(s + 57) >> 6);
  55. crypto_int64 s23 = (load_4(s + 60) >> 3);
  56. crypto_int64 carry0;
  57. crypto_int64 carry1;
  58. crypto_int64 carry2;
  59. crypto_int64 carry3;
  60. crypto_int64 carry4;
  61. crypto_int64 carry5;
  62. crypto_int64 carry6;
  63. crypto_int64 carry7;
  64. crypto_int64 carry8;
  65. crypto_int64 carry9;
  66. crypto_int64 carry10;
  67. crypto_int64 carry11;
  68. crypto_int64 carry12;
  69. crypto_int64 carry13;
  70. crypto_int64 carry14;
  71. crypto_int64 carry15;
  72. crypto_int64 carry16;
  73. s11 += s23 * 666643;
  74. s12 += s23 * 470296;
  75. s13 += s23 * 654183;
  76. s14 -= s23 * 997805;
  77. s15 += s23 * 136657;
  78. s16 -= s23 * 683901;
  79. s23 = 0;
  80. s10 += s22 * 666643;
  81. s11 += s22 * 470296;
  82. s12 += s22 * 654183;
  83. s13 -= s22 * 997805;
  84. s14 += s22 * 136657;
  85. s15 -= s22 * 683901;
  86. s22 = 0;
  87. s9 += s21 * 666643;
  88. s10 += s21 * 470296;
  89. s11 += s21 * 654183;
  90. s12 -= s21 * 997805;
  91. s13 += s21 * 136657;
  92. s14 -= s21 * 683901;
  93. s21 = 0;
  94. s8 += s20 * 666643;
  95. s9 += s20 * 470296;
  96. s10 += s20 * 654183;
  97. s11 -= s20 * 997805;
  98. s12 += s20 * 136657;
  99. s13 -= s20 * 683901;
  100. s20 = 0;
  101. s7 += s19 * 666643;
  102. s8 += s19 * 470296;
  103. s9 += s19 * 654183;
  104. s10 -= s19 * 997805;
  105. s11 += s19 * 136657;
  106. s12 -= s19 * 683901;
  107. s19 = 0;
  108. s6 += s18 * 666643;
  109. s7 += s18 * 470296;
  110. s8 += s18 * 654183;
  111. s9 -= s18 * 997805;
  112. s10 += s18 * 136657;
  113. s11 -= s18 * 683901;
  114. s18 = 0;
  115. carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
  116. carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
  117. carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
  118. carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= SHL64(carry12,21);
  119. carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= SHL64(carry14,21);
  120. carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= SHL64(carry16,21);
  121. carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
  122. carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
  123. carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
  124. carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= SHL64(carry13,21);
  125. carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= SHL64(carry15,21);
  126. s5 += s17 * 666643;
  127. s6 += s17 * 470296;
  128. s7 += s17 * 654183;
  129. s8 -= s17 * 997805;
  130. s9 += s17 * 136657;
  131. s10 -= s17 * 683901;
  132. s17 = 0;
  133. s4 += s16 * 666643;
  134. s5 += s16 * 470296;
  135. s6 += s16 * 654183;
  136. s7 -= s16 * 997805;
  137. s8 += s16 * 136657;
  138. s9 -= s16 * 683901;
  139. s16 = 0;
  140. s3 += s15 * 666643;
  141. s4 += s15 * 470296;
  142. s5 += s15 * 654183;
  143. s6 -= s15 * 997805;
  144. s7 += s15 * 136657;
  145. s8 -= s15 * 683901;
  146. s15 = 0;
  147. s2 += s14 * 666643;
  148. s3 += s14 * 470296;
  149. s4 += s14 * 654183;
  150. s5 -= s14 * 997805;
  151. s6 += s14 * 136657;
  152. s7 -= s14 * 683901;
  153. s14 = 0;
  154. s1 += s13 * 666643;
  155. s2 += s13 * 470296;
  156. s3 += s13 * 654183;
  157. s4 -= s13 * 997805;
  158. s5 += s13 * 136657;
  159. s6 -= s13 * 683901;
  160. s13 = 0;
  161. s0 += s12 * 666643;
  162. s1 += s12 * 470296;
  163. s2 += s12 * 654183;
  164. s3 -= s12 * 997805;
  165. s4 += s12 * 136657;
  166. s5 -= s12 * 683901;
  167. s12 = 0;
  168. carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
  169. carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
  170. carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
  171. carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
  172. carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
  173. carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
  174. carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
  175. carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
  176. carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
  177. carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
  178. carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
  179. carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
  180. s0 += s12 * 666643;
  181. s1 += s12 * 470296;
  182. s2 += s12 * 654183;
  183. s3 -= s12 * 997805;
  184. s4 += s12 * 136657;
  185. s5 -= s12 * 683901;
  186. s12 = 0;
  187. carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
  188. carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
  189. carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
  190. carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
  191. carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
  192. carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
  193. carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
  194. carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
  195. carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
  196. carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
  197. carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
  198. carry11 = s11 >> 21; s12 += carry11; s11 -= SHL64(carry11,21);
  199. s0 += s12 * 666643;
  200. s1 += s12 * 470296;
  201. s2 += s12 * 654183;
  202. s3 -= s12 * 997805;
  203. s4 += s12 * 136657;
  204. s5 -= s12 * 683901;
  205. s12 = 0;
  206. carry0 = s0 >> 21; s1 += carry0; s0 -= SHL64(carry0,21);
  207. carry1 = s1 >> 21; s2 += carry1; s1 -= SHL64(carry1,21);
  208. carry2 = s2 >> 21; s3 += carry2; s2 -= SHL64(carry2,21);
  209. carry3 = s3 >> 21; s4 += carry3; s3 -= SHL64(carry3,21);
  210. carry4 = s4 >> 21; s5 += carry4; s4 -= SHL64(carry4,21);
  211. carry5 = s5 >> 21; s6 += carry5; s5 -= SHL64(carry5,21);
  212. carry6 = s6 >> 21; s7 += carry6; s6 -= SHL64(carry6,21);
  213. carry7 = s7 >> 21; s8 += carry7; s7 -= SHL64(carry7,21);
  214. carry8 = s8 >> 21; s9 += carry8; s8 -= SHL64(carry8,21);
  215. carry9 = s9 >> 21; s10 += carry9; s9 -= SHL64(carry9,21);
  216. carry10 = s10 >> 21; s11 += carry10; s10 -= SHL64(carry10,21);
  217. s[0] = s0 >> 0;
  218. s[1] = s0 >> 8;
  219. s[2] = (s0 >> 16) | SHL64(s1,5);
  220. s[3] = s1 >> 3;
  221. s[4] = s1 >> 11;
  222. s[5] = (s1 >> 19) | SHL64(s2,2);
  223. s[6] = s2 >> 6;
  224. s[7] = (s2 >> 14) | SHL64(s3,7);
  225. s[8] = s3 >> 1;
  226. s[9] = s3 >> 9;
  227. s[10] = (s3 >> 17) | SHL64(s4,4);
  228. s[11] = s4 >> 4;
  229. s[12] = s4 >> 12;
  230. s[13] = (s4 >> 20) | SHL64(s5,1);
  231. s[14] = s5 >> 7;
  232. s[15] = (s5 >> 15) | SHL64(s6,6);
  233. s[16] = s6 >> 2;
  234. s[17] = s6 >> 10;
  235. s[18] = (s6 >> 18) | SHL64(s7,3);
  236. s[19] = s7 >> 5;
  237. s[20] = s7 >> 13;
  238. s[21] = s8 >> 0;
  239. s[22] = s8 >> 8;
  240. s[23] = (s8 >> 16) | SHL64(s9,5);
  241. s[24] = s9 >> 3;
  242. s[25] = s9 >> 11;
  243. s[26] = (s9 >> 19) | SHL64(s10,2);
  244. s[27] = s10 >> 6;
  245. s[28] = (s10 >> 14) | SHL64(s11,7);
  246. s[29] = s11 >> 1;
  247. s[30] = s11 >> 9;
  248. s[31] = s11 >> 17;
  249. }