smult.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. version 20081011
  3. Matthew Dempsky
  4. Public domain.
  5. Derived from public domain code by D. J. Bernstein.
  6. 20140216 tweak: Mask top bit of point input.
  7. */
  8. #include "crypto_scalarmult.h"
  9. static void add(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  10. {
  11. unsigned int j;
  12. unsigned int u;
  13. u = 0;
  14. for (j = 0;j < 31;++j) { u += a[j] + b[j]; out[j] = u & 255; u >>= 8; }
  15. u += a[31] + b[31]; out[31] = u;
  16. }
  17. static void sub(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  18. {
  19. unsigned int j;
  20. unsigned int u;
  21. u = 218;
  22. for (j = 0;j < 31;++j) {
  23. u += a[j] + 65280 - b[j];
  24. out[j] = u & 255;
  25. u >>= 8;
  26. }
  27. u += a[31] - b[31];
  28. out[31] = u;
  29. }
  30. static void squeeze(unsigned int a[32])
  31. {
  32. unsigned int j;
  33. unsigned int u;
  34. u = 0;
  35. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  36. u += a[31]; a[31] = u & 127;
  37. u = 19 * (u >> 7);
  38. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  39. u += a[31]; a[31] = u;
  40. }
  41. static const unsigned int minusp[32] = {
  42. 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128
  43. } ;
  44. static void freeze(unsigned int a[32])
  45. {
  46. unsigned int aorig[32];
  47. unsigned int j;
  48. unsigned int negative;
  49. for (j = 0;j < 32;++j) aorig[j] = a[j];
  50. add(a,a,minusp);
  51. negative = -((a[31] >> 7) & 1);
  52. for (j = 0;j < 32;++j) a[j] ^= negative & (aorig[j] ^ a[j]);
  53. }
  54. static void mult(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  55. {
  56. unsigned int i;
  57. unsigned int j;
  58. unsigned int u;
  59. for (i = 0;i < 32;++i) {
  60. u = 0;
  61. for (j = 0;j <= i;++j) u += a[j] * b[i - j];
  62. for (j = i + 1;j < 32;++j) u += 38 * a[j] * b[i + 32 - j];
  63. out[i] = u;
  64. }
  65. squeeze(out);
  66. }
  67. static void mult121665(unsigned int out[32],const unsigned int a[32])
  68. {
  69. unsigned int j;
  70. unsigned int u;
  71. u = 0;
  72. for (j = 0;j < 31;++j) { u += 121665 * a[j]; out[j] = u & 255; u >>= 8; }
  73. u += 121665 * a[31]; out[31] = u & 127;
  74. u = 19 * (u >> 7);
  75. for (j = 0;j < 31;++j) { u += out[j]; out[j] = u & 255; u >>= 8; }
  76. u += out[j]; out[j] = u;
  77. }
  78. static void square(unsigned int out[32],const unsigned int a[32])
  79. {
  80. unsigned int i;
  81. unsigned int j;
  82. unsigned int u;
  83. for (i = 0;i < 32;++i) {
  84. u = 0;
  85. for (j = 0;j < i - j;++j) u += a[j] * a[i - j];
  86. for (j = i + 1;j < i + 32 - j;++j) u += 38 * a[j] * a[i + 32 - j];
  87. u *= 2;
  88. if ((i & 1) == 0) {
  89. u += a[i / 2] * a[i / 2];
  90. u += 38 * a[i / 2 + 16] * a[i / 2 + 16];
  91. }
  92. out[i] = u;
  93. }
  94. squeeze(out);
  95. }
  96. static void select(unsigned int p[64],unsigned int q[64],const unsigned int r[64],const unsigned int s[64],unsigned int b)
  97. {
  98. unsigned int j;
  99. unsigned int t;
  100. unsigned int bminus1;
  101. bminus1 = b - 1;
  102. for (j = 0;j < 64;++j) {
  103. t = bminus1 & (r[j] ^ s[j]);
  104. p[j] = s[j] ^ t;
  105. q[j] = r[j] ^ t;
  106. }
  107. }
  108. static void mainloop(unsigned int work[64],const unsigned char e[32])
  109. {
  110. unsigned int xzm1[64];
  111. unsigned int xzm[64];
  112. unsigned int xzmb[64];
  113. unsigned int xzm1b[64];
  114. unsigned int xznb[64];
  115. unsigned int xzn1b[64];
  116. unsigned int a0[64];
  117. unsigned int a1[64];
  118. unsigned int b0[64];
  119. unsigned int b1[64];
  120. unsigned int c1[64];
  121. unsigned int r[32];
  122. unsigned int s[32];
  123. unsigned int t[32];
  124. unsigned int u[32];
  125. unsigned int i;
  126. unsigned int j;
  127. unsigned int b;
  128. int pos;
  129. for (j = 0;j < 32;++j) xzm1[j] = work[j];
  130. xzm1[32] = 1;
  131. for (j = 33;j < 64;++j) xzm1[j] = 0;
  132. xzm[0] = 1;
  133. for (j = 1;j < 64;++j) xzm[j] = 0;
  134. for (pos = 254;pos >= 0;--pos) {
  135. b = e[pos / 8] >> (pos & 7);
  136. b &= 1;
  137. select(xzmb,xzm1b,xzm,xzm1,b);
  138. add(a0,xzmb,xzmb + 32);
  139. sub(a0 + 32,xzmb,xzmb + 32);
  140. add(a1,xzm1b,xzm1b + 32);
  141. sub(a1 + 32,xzm1b,xzm1b + 32);
  142. square(b0,a0);
  143. square(b0 + 32,a0 + 32);
  144. mult(b1,a1,a0 + 32);
  145. mult(b1 + 32,a1 + 32,a0);
  146. add(c1,b1,b1 + 32);
  147. sub(c1 + 32,b1,b1 + 32);
  148. square(r,c1 + 32);
  149. sub(s,b0,b0 + 32);
  150. mult121665(t,s);
  151. add(u,t,b0);
  152. mult(xznb,b0,b0 + 32);
  153. mult(xznb + 32,s,u);
  154. square(xzn1b,c1);
  155. mult(xzn1b + 32,r,work);
  156. select(xzm,xzm1,xznb,xzn1b,b);
  157. }
  158. for (j = 0;j < 64;++j) work[j] = xzm[j];
  159. }
  160. static void recip(unsigned int out[32],const unsigned int z[32])
  161. {
  162. unsigned int z2[32];
  163. unsigned int z9[32];
  164. unsigned int z11[32];
  165. unsigned int z2_5_0[32];
  166. unsigned int z2_10_0[32];
  167. unsigned int z2_20_0[32];
  168. unsigned int z2_50_0[32];
  169. unsigned int z2_100_0[32];
  170. unsigned int t0[32];
  171. unsigned int t1[32];
  172. int i;
  173. /* 2 */ square(z2,z);
  174. /* 4 */ square(t1,z2);
  175. /* 8 */ square(t0,t1);
  176. /* 9 */ mult(z9,t0,z);
  177. /* 11 */ mult(z11,z9,z2);
  178. /* 22 */ square(t0,z11);
  179. /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
  180. /* 2^6 - 2^1 */ square(t0,z2_5_0);
  181. /* 2^7 - 2^2 */ square(t1,t0);
  182. /* 2^8 - 2^3 */ square(t0,t1);
  183. /* 2^9 - 2^4 */ square(t1,t0);
  184. /* 2^10 - 2^5 */ square(t0,t1);
  185. /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
  186. /* 2^11 - 2^1 */ square(t0,z2_10_0);
  187. /* 2^12 - 2^2 */ square(t1,t0);
  188. /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
  189. /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
  190. /* 2^21 - 2^1 */ square(t0,z2_20_0);
  191. /* 2^22 - 2^2 */ square(t1,t0);
  192. /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
  193. /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
  194. /* 2^41 - 2^1 */ square(t1,t0);
  195. /* 2^42 - 2^2 */ square(t0,t1);
  196. /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
  197. /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
  198. /* 2^51 - 2^1 */ square(t0,z2_50_0);
  199. /* 2^52 - 2^2 */ square(t1,t0);
  200. /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  201. /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
  202. /* 2^101 - 2^1 */ square(t1,z2_100_0);
  203. /* 2^102 - 2^2 */ square(t0,t1);
  204. /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
  205. /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
  206. /* 2^201 - 2^1 */ square(t0,t1);
  207. /* 2^202 - 2^2 */ square(t1,t0);
  208. /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  209. /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
  210. /* 2^251 - 2^1 */ square(t1,t0);
  211. /* 2^252 - 2^2 */ square(t0,t1);
  212. /* 2^253 - 2^3 */ square(t1,t0);
  213. /* 2^254 - 2^4 */ square(t0,t1);
  214. /* 2^255 - 2^5 */ square(t1,t0);
  215. /* 2^255 - 21 */ mult(out,t1,z11);
  216. }
  217. int crypto_scalarmult(unsigned char *q,
  218. const unsigned char *n,
  219. const unsigned char *p)
  220. {
  221. unsigned int work[96];
  222. unsigned char e[32];
  223. unsigned int i;
  224. for (i = 0;i < 32;++i) e[i] = n[i];
  225. e[0] &= 248;
  226. e[31] &= 127;
  227. e[31] |= 64;
  228. for (i = 0;i < 32;++i) work[i] = p[i];
  229. work[31] &= 127;
  230. mainloop(work,e);
  231. recip(work + 32,work + 32);
  232. mult(work + 64,work,work + 32);
  233. freeze(work + 64);
  234. for (i = 0;i < 32;++i) q[i] = work[64 + i];
  235. return 0;
  236. }