1
0

sc_muladd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. a[0]+256*a[1]+...+256^31*a[31] = a
  25. b[0]+256*b[1]+...+256^31*b[31] = b
  26. c[0]+256*c[1]+...+256^31*c[31] = c
  27. Output:
  28. s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l
  29. where l = 2^252 + 27742317777372353535851937790883648493.
  30. */
  31. void sc_muladd(unsigned char *s,const unsigned char *a,const unsigned char *b,const unsigned char *c)
  32. {
  33. crypto_int64 a0 = 2097151 & load_3(a);
  34. crypto_int64 a1 = 2097151 & (load_4(a + 2) >> 5);
  35. crypto_int64 a2 = 2097151 & (load_3(a + 5) >> 2);
  36. crypto_int64 a3 = 2097151 & (load_4(a + 7) >> 7);
  37. crypto_int64 a4 = 2097151 & (load_4(a + 10) >> 4);
  38. crypto_int64 a5 = 2097151 & (load_3(a + 13) >> 1);
  39. crypto_int64 a6 = 2097151 & (load_4(a + 15) >> 6);
  40. crypto_int64 a7 = 2097151 & (load_3(a + 18) >> 3);
  41. crypto_int64 a8 = 2097151 & load_3(a + 21);
  42. crypto_int64 a9 = 2097151 & (load_4(a + 23) >> 5);
  43. crypto_int64 a10 = 2097151 & (load_3(a + 26) >> 2);
  44. crypto_int64 a11 = (load_4(a + 28) >> 7);
  45. crypto_int64 b0 = 2097151 & load_3(b);
  46. crypto_int64 b1 = 2097151 & (load_4(b + 2) >> 5);
  47. crypto_int64 b2 = 2097151 & (load_3(b + 5) >> 2);
  48. crypto_int64 b3 = 2097151 & (load_4(b + 7) >> 7);
  49. crypto_int64 b4 = 2097151 & (load_4(b + 10) >> 4);
  50. crypto_int64 b5 = 2097151 & (load_3(b + 13) >> 1);
  51. crypto_int64 b6 = 2097151 & (load_4(b + 15) >> 6);
  52. crypto_int64 b7 = 2097151 & (load_3(b + 18) >> 3);
  53. crypto_int64 b8 = 2097151 & load_3(b + 21);
  54. crypto_int64 b9 = 2097151 & (load_4(b + 23) >> 5);
  55. crypto_int64 b10 = 2097151 & (load_3(b + 26) >> 2);
  56. crypto_int64 b11 = (load_4(b + 28) >> 7);
  57. crypto_int64 c0 = 2097151 & load_3(c);
  58. crypto_int64 c1 = 2097151 & (load_4(c + 2) >> 5);
  59. crypto_int64 c2 = 2097151 & (load_3(c + 5) >> 2);
  60. crypto_int64 c3 = 2097151 & (load_4(c + 7) >> 7);
  61. crypto_int64 c4 = 2097151 & (load_4(c + 10) >> 4);
  62. crypto_int64 c5 = 2097151 & (load_3(c + 13) >> 1);
  63. crypto_int64 c6 = 2097151 & (load_4(c + 15) >> 6);
  64. crypto_int64 c7 = 2097151 & (load_3(c + 18) >> 3);
  65. crypto_int64 c8 = 2097151 & load_3(c + 21);
  66. crypto_int64 c9 = 2097151 & (load_4(c + 23) >> 5);
  67. crypto_int64 c10 = 2097151 & (load_3(c + 26) >> 2);
  68. crypto_int64 c11 = (load_4(c + 28) >> 7);
  69. crypto_int64 s0;
  70. crypto_int64 s1;
  71. crypto_int64 s2;
  72. crypto_int64 s3;
  73. crypto_int64 s4;
  74. crypto_int64 s5;
  75. crypto_int64 s6;
  76. crypto_int64 s7;
  77. crypto_int64 s8;
  78. crypto_int64 s9;
  79. crypto_int64 s10;
  80. crypto_int64 s11;
  81. crypto_int64 s12;
  82. crypto_int64 s13;
  83. crypto_int64 s14;
  84. crypto_int64 s15;
  85. crypto_int64 s16;
  86. crypto_int64 s17;
  87. crypto_int64 s18;
  88. crypto_int64 s19;
  89. crypto_int64 s20;
  90. crypto_int64 s21;
  91. crypto_int64 s22;
  92. crypto_int64 s23;
  93. crypto_int64 carry0;
  94. crypto_int64 carry1;
  95. crypto_int64 carry2;
  96. crypto_int64 carry3;
  97. crypto_int64 carry4;
  98. crypto_int64 carry5;
  99. crypto_int64 carry6;
  100. crypto_int64 carry7;
  101. crypto_int64 carry8;
  102. crypto_int64 carry9;
  103. crypto_int64 carry10;
  104. crypto_int64 carry11;
  105. crypto_int64 carry12;
  106. crypto_int64 carry13;
  107. crypto_int64 carry14;
  108. crypto_int64 carry15;
  109. crypto_int64 carry16;
  110. crypto_int64 carry17;
  111. crypto_int64 carry18;
  112. crypto_int64 carry19;
  113. crypto_int64 carry20;
  114. crypto_int64 carry21;
  115. crypto_int64 carry22;
  116. s0 = c0 + a0*b0;
  117. s1 = c1 + a0*b1 + a1*b0;
  118. s2 = c2 + a0*b2 + a1*b1 + a2*b0;
  119. s3 = c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0;
  120. s4 = c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0;
  121. s5 = c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0;
  122. s6 = c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0;
  123. s7 = c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0;
  124. s8 = c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0;
  125. s9 = c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0;
  126. s10 = c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0;
  127. 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;
  128. s12 = a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1;
  129. s13 = a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2;
  130. s14 = a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3;
  131. s15 = a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4;
  132. s16 = a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5;
  133. s17 = a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6;
  134. s18 = a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7;
  135. s19 = a8*b11 + a9*b10 + a10*b9 + a11*b8;
  136. s20 = a9*b11 + a10*b10 + a11*b9;
  137. s21 = a10*b11 + a11*b10;
  138. s22 = a11*b11;
  139. s23 = 0;
  140. carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
  141. carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
  142. carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
  143. carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
  144. carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
  145. carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
  146. carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
  147. carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
  148. carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
  149. carry18 = (s18 + (1<<20)) >> 21; s19 += carry18; s18 -= carry18 << 21;
  150. carry20 = (s20 + (1<<20)) >> 21; s21 += carry20; s20 -= carry20 << 21;
  151. carry22 = (s22 + (1<<20)) >> 21; s23 += carry22; s22 -= carry22 << 21;
  152. carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
  153. carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
  154. carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
  155. carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
  156. carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
  157. carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
  158. carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
  159. carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
  160. carry17 = (s17 + (1<<20)) >> 21; s18 += carry17; s17 -= carry17 << 21;
  161. carry19 = (s19 + (1<<20)) >> 21; s20 += carry19; s19 -= carry19 << 21;
  162. carry21 = (s21 + (1<<20)) >> 21; s22 += carry21; s21 -= carry21 << 21;
  163. s11 += s23 * 666643;
  164. s12 += s23 * 470296;
  165. s13 += s23 * 654183;
  166. s14 -= s23 * 997805;
  167. s15 += s23 * 136657;
  168. s16 -= s23 * 683901;
  169. s23 = 0;
  170. s10 += s22 * 666643;
  171. s11 += s22 * 470296;
  172. s12 += s22 * 654183;
  173. s13 -= s22 * 997805;
  174. s14 += s22 * 136657;
  175. s15 -= s22 * 683901;
  176. s22 = 0;
  177. s9 += s21 * 666643;
  178. s10 += s21 * 470296;
  179. s11 += s21 * 654183;
  180. s12 -= s21 * 997805;
  181. s13 += s21 * 136657;
  182. s14 -= s21 * 683901;
  183. s21 = 0;
  184. s8 += s20 * 666643;
  185. s9 += s20 * 470296;
  186. s10 += s20 * 654183;
  187. s11 -= s20 * 997805;
  188. s12 += s20 * 136657;
  189. s13 -= s20 * 683901;
  190. s20 = 0;
  191. s7 += s19 * 666643;
  192. s8 += s19 * 470296;
  193. s9 += s19 * 654183;
  194. s10 -= s19 * 997805;
  195. s11 += s19 * 136657;
  196. s12 -= s19 * 683901;
  197. s19 = 0;
  198. s6 += s18 * 666643;
  199. s7 += s18 * 470296;
  200. s8 += s18 * 654183;
  201. s9 -= s18 * 997805;
  202. s10 += s18 * 136657;
  203. s11 -= s18 * 683901;
  204. s18 = 0;
  205. carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
  206. carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
  207. carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
  208. carry12 = (s12 + (1<<20)) >> 21; s13 += carry12; s12 -= carry12 << 21;
  209. carry14 = (s14 + (1<<20)) >> 21; s15 += carry14; s14 -= carry14 << 21;
  210. carry16 = (s16 + (1<<20)) >> 21; s17 += carry16; s16 -= carry16 << 21;
  211. carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
  212. carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
  213. carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
  214. carry13 = (s13 + (1<<20)) >> 21; s14 += carry13; s13 -= carry13 << 21;
  215. carry15 = (s15 + (1<<20)) >> 21; s16 += carry15; s15 -= carry15 << 21;
  216. s5 += s17 * 666643;
  217. s6 += s17 * 470296;
  218. s7 += s17 * 654183;
  219. s8 -= s17 * 997805;
  220. s9 += s17 * 136657;
  221. s10 -= s17 * 683901;
  222. s17 = 0;
  223. s4 += s16 * 666643;
  224. s5 += s16 * 470296;
  225. s6 += s16 * 654183;
  226. s7 -= s16 * 997805;
  227. s8 += s16 * 136657;
  228. s9 -= s16 * 683901;
  229. s16 = 0;
  230. s3 += s15 * 666643;
  231. s4 += s15 * 470296;
  232. s5 += s15 * 654183;
  233. s6 -= s15 * 997805;
  234. s7 += s15 * 136657;
  235. s8 -= s15 * 683901;
  236. s15 = 0;
  237. s2 += s14 * 666643;
  238. s3 += s14 * 470296;
  239. s4 += s14 * 654183;
  240. s5 -= s14 * 997805;
  241. s6 += s14 * 136657;
  242. s7 -= s14 * 683901;
  243. s14 = 0;
  244. s1 += s13 * 666643;
  245. s2 += s13 * 470296;
  246. s3 += s13 * 654183;
  247. s4 -= s13 * 997805;
  248. s5 += s13 * 136657;
  249. s6 -= s13 * 683901;
  250. s13 = 0;
  251. s0 += s12 * 666643;
  252. s1 += s12 * 470296;
  253. s2 += s12 * 654183;
  254. s3 -= s12 * 997805;
  255. s4 += s12 * 136657;
  256. s5 -= s12 * 683901;
  257. s12 = 0;
  258. carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
  259. carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
  260. carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
  261. carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
  262. carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
  263. carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
  264. carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
  265. carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
  266. carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
  267. carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
  268. carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
  269. carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
  270. s0 += s12 * 666643;
  271. s1 += s12 * 470296;
  272. s2 += s12 * 654183;
  273. s3 -= s12 * 997805;
  274. s4 += s12 * 136657;
  275. s5 -= s12 * 683901;
  276. s12 = 0;
  277. carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
  278. carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
  279. carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
  280. carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
  281. carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
  282. carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
  283. carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
  284. carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
  285. carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
  286. carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
  287. carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
  288. carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
  289. s0 += s12 * 666643;
  290. s1 += s12 * 470296;
  291. s2 += s12 * 654183;
  292. s3 -= s12 * 997805;
  293. s4 += s12 * 136657;
  294. s5 -= s12 * 683901;
  295. s12 = 0;
  296. carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
  297. carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
  298. carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
  299. carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
  300. carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
  301. carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
  302. carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
  303. carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
  304. carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
  305. carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
  306. carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
  307. s[0] = s0 >> 0;
  308. s[1] = s0 >> 8;
  309. s[2] = (s0 >> 16) | (s1 << 5);
  310. s[3] = s1 >> 3;
  311. s[4] = s1 >> 11;
  312. s[5] = (s1 >> 19) | (s2 << 2);
  313. s[6] = s2 >> 6;
  314. s[7] = (s2 >> 14) | (s3 << 7);
  315. s[8] = s3 >> 1;
  316. s[9] = s3 >> 9;
  317. s[10] = (s3 >> 17) | (s4 << 4);
  318. s[11] = s4 >> 4;
  319. s[12] = s4 >> 12;
  320. s[13] = (s4 >> 20) | (s5 << 1);
  321. s[14] = s5 >> 7;
  322. s[15] = (s5 >> 15) | (s6 << 6);
  323. s[16] = s6 >> 2;
  324. s[17] = s6 >> 10;
  325. s[18] = (s6 >> 18) | (s7 << 3);
  326. s[19] = s7 >> 5;
  327. s[20] = s7 >> 13;
  328. s[21] = s8 >> 0;
  329. s[22] = s8 >> 8;
  330. s[23] = (s8 >> 16) | (s9 << 5);
  331. s[24] = s9 >> 3;
  332. s[25] = s9 >> 11;
  333. s[26] = (s9 >> 19) | (s10 << 2);
  334. s[27] = s10 >> 6;
  335. s[28] = (s10 >> 14) | (s11 << 7);
  336. s[29] = s11 >> 1;
  337. s[30] = s11 >> 9;
  338. s[31] = s11 >> 17;
  339. }