smult_curve25519_ref.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* $OpenBSD: smult_curve25519_ref.c,v 1.2 2013/11/02 22:02:14 markus Exp $ */
  2. /*
  3. version 20081011
  4. Matthew Dempsky
  5. Public domain.
  6. Derived from public domain code by D. J. Bernstein.
  7. */
  8. int crypto_scalarmult_curve25519(unsigned char *, const unsigned char *, const unsigned char *);
  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 j;
  126. unsigned int b;
  127. int pos;
  128. for (j = 0;j < 32;++j) xzm1[j] = work[j];
  129. xzm1[32] = 1;
  130. for (j = 33;j < 64;++j) xzm1[j] = 0;
  131. xzm[0] = 1;
  132. for (j = 1;j < 64;++j) xzm[j] = 0;
  133. for (pos = 254;pos >= 0;--pos) {
  134. b = e[pos / 8] >> (pos & 7);
  135. b &= 1;
  136. select(xzmb,xzm1b,xzm,xzm1,b);
  137. add(a0,xzmb,xzmb + 32);
  138. sub(a0 + 32,xzmb,xzmb + 32);
  139. add(a1,xzm1b,xzm1b + 32);
  140. sub(a1 + 32,xzm1b,xzm1b + 32);
  141. square(b0,a0);
  142. square(b0 + 32,a0 + 32);
  143. mult(b1,a1,a0 + 32);
  144. mult(b1 + 32,a1 + 32,a0);
  145. add(c1,b1,b1 + 32);
  146. sub(c1 + 32,b1,b1 + 32);
  147. square(r,c1 + 32);
  148. sub(s,b0,b0 + 32);
  149. mult121665(t,s);
  150. add(u,t,b0);
  151. mult(xznb,b0,b0 + 32);
  152. mult(xznb + 32,s,u);
  153. square(xzn1b,c1);
  154. mult(xzn1b + 32,r,work);
  155. select(xzm,xzm1,xznb,xzn1b,b);
  156. }
  157. for (j = 0;j < 64;++j) work[j] = xzm[j];
  158. }
  159. static void recip(unsigned int out[32],const unsigned int z[32])
  160. {
  161. unsigned int z2[32];
  162. unsigned int z9[32];
  163. unsigned int z11[32];
  164. unsigned int z2_5_0[32];
  165. unsigned int z2_10_0[32];
  166. unsigned int z2_20_0[32];
  167. unsigned int z2_50_0[32];
  168. unsigned int z2_100_0[32];
  169. unsigned int t0[32];
  170. unsigned int t1[32];
  171. int i;
  172. /* 2 */ square(z2,z);
  173. /* 4 */ square(t1,z2);
  174. /* 8 */ square(t0,t1);
  175. /* 9 */ mult(z9,t0,z);
  176. /* 11 */ mult(z11,z9,z2);
  177. /* 22 */ square(t0,z11);
  178. /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
  179. /* 2^6 - 2^1 */ square(t0,z2_5_0);
  180. /* 2^7 - 2^2 */ square(t1,t0);
  181. /* 2^8 - 2^3 */ square(t0,t1);
  182. /* 2^9 - 2^4 */ square(t1,t0);
  183. /* 2^10 - 2^5 */ square(t0,t1);
  184. /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
  185. /* 2^11 - 2^1 */ square(t0,z2_10_0);
  186. /* 2^12 - 2^2 */ square(t1,t0);
  187. /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
  188. /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
  189. /* 2^21 - 2^1 */ square(t0,z2_20_0);
  190. /* 2^22 - 2^2 */ square(t1,t0);
  191. /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
  192. /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
  193. /* 2^41 - 2^1 */ square(t1,t0);
  194. /* 2^42 - 2^2 */ square(t0,t1);
  195. /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
  196. /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
  197. /* 2^51 - 2^1 */ square(t0,z2_50_0);
  198. /* 2^52 - 2^2 */ square(t1,t0);
  199. /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  200. /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
  201. /* 2^101 - 2^1 */ square(t1,z2_100_0);
  202. /* 2^102 - 2^2 */ square(t0,t1);
  203. /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
  204. /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
  205. /* 2^201 - 2^1 */ square(t0,t1);
  206. /* 2^202 - 2^2 */ square(t1,t0);
  207. /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  208. /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
  209. /* 2^251 - 2^1 */ square(t1,t0);
  210. /* 2^252 - 2^2 */ square(t0,t1);
  211. /* 2^253 - 2^3 */ square(t1,t0);
  212. /* 2^254 - 2^4 */ square(t0,t1);
  213. /* 2^255 - 2^5 */ square(t1,t0);
  214. /* 2^255 - 21 */ mult(out,t1,z11);
  215. }
  216. int crypto_scalarmult_curve25519(unsigned char *q,
  217. const unsigned char *n,
  218. const unsigned char *p)
  219. {
  220. unsigned int work[96];
  221. unsigned char e[32];
  222. unsigned int i;
  223. for (i = 0;i < 32;++i) e[i] = n[i];
  224. e[0] &= 248;
  225. e[31] &= 127;
  226. e[31] |= 64;
  227. for (i = 0;i < 32;++i) work[i] = p[i];
  228. mainloop(work,e);
  229. recip(work + 32,work + 32);
  230. mult(work + 64,work,work + 32);
  231. freeze(work + 64);
  232. for (i = 0;i < 32;++i) q[i] = work[64 + i];
  233. return 0;
  234. }