field_5x52_int128_impl.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**********************************************************************
  2. * Copyright (c) 2013, 2014 Pieter Wuille *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. #ifndef _SECP256K1_FIELD_INNER5X52_IMPL_H_
  7. #define _SECP256K1_FIELD_INNER5X52_IMPL_H_
  8. #include <stdint.h>
  9. #ifdef VERIFY
  10. #define VERIFY_BITS(x, n) VERIFY_CHECK(((x) >> (n)) == 0)
  11. #else
  12. #define VERIFY_BITS(x, n) do { } while(0)
  13. #endif
  14. SECP256K1_INLINE static void secp256k1_fe_mul_inner(uint64_t *r, const uint64_t *a, const uint64_t * SECP256K1_RESTRICT b) {
  15. uint128_t c, d;
  16. uint64_t t3, t4, tx, u0;
  17. uint64_t a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4];
  18. const uint64_t M = 0xFFFFFFFFFFFFFULL, R = 0x1000003D10ULL;
  19. VERIFY_BITS(a[0], 56);
  20. VERIFY_BITS(a[1], 56);
  21. VERIFY_BITS(a[2], 56);
  22. VERIFY_BITS(a[3], 56);
  23. VERIFY_BITS(a[4], 52);
  24. VERIFY_BITS(b[0], 56);
  25. VERIFY_BITS(b[1], 56);
  26. VERIFY_BITS(b[2], 56);
  27. VERIFY_BITS(b[3], 56);
  28. VERIFY_BITS(b[4], 52);
  29. VERIFY_CHECK(r != b);
  30. /* [... a b c] is a shorthand for ... + a<<104 + b<<52 + c<<0 mod n.
  31. * px is a shorthand for sum(a[i]*b[x-i], i=0..x).
  32. * Note that [x 0 0 0 0 0] = [x*R].
  33. */
  34. d = (uint128_t)a0 * b[3]
  35. + (uint128_t)a1 * b[2]
  36. + (uint128_t)a2 * b[1]
  37. + (uint128_t)a3 * b[0];
  38. VERIFY_BITS(d, 114);
  39. /* [d 0 0 0] = [p3 0 0 0] */
  40. c = (uint128_t)a4 * b[4];
  41. VERIFY_BITS(c, 112);
  42. /* [c 0 0 0 0 d 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  43. d += (c & M) * R; c >>= 52;
  44. VERIFY_BITS(d, 115);
  45. VERIFY_BITS(c, 60);
  46. /* [c 0 0 0 0 0 d 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  47. t3 = d & M; d >>= 52;
  48. VERIFY_BITS(t3, 52);
  49. VERIFY_BITS(d, 63);
  50. /* [c 0 0 0 0 d t3 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  51. d += (uint128_t)a0 * b[4]
  52. + (uint128_t)a1 * b[3]
  53. + (uint128_t)a2 * b[2]
  54. + (uint128_t)a3 * b[1]
  55. + (uint128_t)a4 * b[0];
  56. VERIFY_BITS(d, 115);
  57. /* [c 0 0 0 0 d t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  58. d += c * R;
  59. VERIFY_BITS(d, 116);
  60. /* [d t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  61. t4 = d & M; d >>= 52;
  62. VERIFY_BITS(t4, 52);
  63. VERIFY_BITS(d, 64);
  64. /* [d t4 t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  65. tx = (t4 >> 48); t4 &= (M >> 4);
  66. VERIFY_BITS(tx, 4);
  67. VERIFY_BITS(t4, 48);
  68. /* [d t4+(tx<<48) t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  69. c = (uint128_t)a0 * b[0];
  70. VERIFY_BITS(c, 112);
  71. /* [d t4+(tx<<48) t3 0 0 c] = [p8 0 0 0 p4 p3 0 0 p0] */
  72. d += (uint128_t)a1 * b[4]
  73. + (uint128_t)a2 * b[3]
  74. + (uint128_t)a3 * b[2]
  75. + (uint128_t)a4 * b[1];
  76. VERIFY_BITS(d, 115);
  77. /* [d t4+(tx<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  78. u0 = d & M; d >>= 52;
  79. VERIFY_BITS(u0, 52);
  80. VERIFY_BITS(d, 63);
  81. /* [d u0 t4+(tx<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  82. /* [d 0 t4+(tx<<48)+(u0<<52) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  83. u0 = (u0 << 4) | tx;
  84. VERIFY_BITS(u0, 56);
  85. /* [d 0 t4+(u0<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  86. c += (uint128_t)u0 * (R >> 4);
  87. VERIFY_BITS(c, 115);
  88. /* [d 0 t4 t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  89. r[0] = c & M; c >>= 52;
  90. VERIFY_BITS(r[0], 52);
  91. VERIFY_BITS(c, 61);
  92. /* [d 0 t4 t3 0 c r0] = [p8 0 0 p5 p4 p3 0 0 p0] */
  93. c += (uint128_t)a0 * b[1]
  94. + (uint128_t)a1 * b[0];
  95. VERIFY_BITS(c, 114);
  96. /* [d 0 t4 t3 0 c r0] = [p8 0 0 p5 p4 p3 0 p1 p0] */
  97. d += (uint128_t)a2 * b[4]
  98. + (uint128_t)a3 * b[3]
  99. + (uint128_t)a4 * b[2];
  100. VERIFY_BITS(d, 114);
  101. /* [d 0 t4 t3 0 c r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  102. c += (d & M) * R; d >>= 52;
  103. VERIFY_BITS(c, 115);
  104. VERIFY_BITS(d, 62);
  105. /* [d 0 0 t4 t3 0 c r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  106. r[1] = c & M; c >>= 52;
  107. VERIFY_BITS(r[1], 52);
  108. VERIFY_BITS(c, 63);
  109. /* [d 0 0 t4 t3 c r1 r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  110. c += (uint128_t)a0 * b[2]
  111. + (uint128_t)a1 * b[1]
  112. + (uint128_t)a2 * b[0];
  113. VERIFY_BITS(c, 114);
  114. /* [d 0 0 t4 t3 c r1 r0] = [p8 0 p6 p5 p4 p3 p2 p1 p0] */
  115. d += (uint128_t)a3 * b[4]
  116. + (uint128_t)a4 * b[3];
  117. VERIFY_BITS(d, 114);
  118. /* [d 0 0 t4 t3 c t1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  119. c += (d & M) * R; d >>= 52;
  120. VERIFY_BITS(c, 115);
  121. VERIFY_BITS(d, 62);
  122. /* [d 0 0 0 t4 t3 c r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  123. /* [d 0 0 0 t4 t3 c r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  124. r[2] = c & M; c >>= 52;
  125. VERIFY_BITS(r[2], 52);
  126. VERIFY_BITS(c, 63);
  127. /* [d 0 0 0 t4 t3+c r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  128. c += d * R + t3;;
  129. VERIFY_BITS(c, 100);
  130. /* [t4 c r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  131. r[3] = c & M; c >>= 52;
  132. VERIFY_BITS(r[3], 52);
  133. VERIFY_BITS(c, 48);
  134. /* [t4+c r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  135. c += t4;
  136. VERIFY_BITS(c, 49);
  137. /* [c r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  138. r[4] = c;
  139. VERIFY_BITS(r[4], 49);
  140. /* [r4 r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  141. }
  142. SECP256K1_INLINE static void secp256k1_fe_sqr_inner(uint64_t *r, const uint64_t *a) {
  143. uint128_t c, d;
  144. uint64_t a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4];
  145. int64_t t3, t4, tx, u0;
  146. const uint64_t M = 0xFFFFFFFFFFFFFULL, R = 0x1000003D10ULL;
  147. VERIFY_BITS(a[0], 56);
  148. VERIFY_BITS(a[1], 56);
  149. VERIFY_BITS(a[2], 56);
  150. VERIFY_BITS(a[3], 56);
  151. VERIFY_BITS(a[4], 52);
  152. /** [... a b c] is a shorthand for ... + a<<104 + b<<52 + c<<0 mod n.
  153. * px is a shorthand for sum(a[i]*a[x-i], i=0..x).
  154. * Note that [x 0 0 0 0 0] = [x*R].
  155. */
  156. d = (uint128_t)(a0*2) * a3
  157. + (uint128_t)(a1*2) * a2;
  158. VERIFY_BITS(d, 114);
  159. /* [d 0 0 0] = [p3 0 0 0] */
  160. c = (uint128_t)a4 * a4;
  161. VERIFY_BITS(c, 112);
  162. /* [c 0 0 0 0 d 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  163. d += (c & M) * R; c >>= 52;
  164. VERIFY_BITS(d, 115);
  165. VERIFY_BITS(c, 60);
  166. /* [c 0 0 0 0 0 d 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  167. t3 = d & M; d >>= 52;
  168. VERIFY_BITS(t3, 52);
  169. VERIFY_BITS(d, 63);
  170. /* [c 0 0 0 0 d t3 0 0 0] = [p8 0 0 0 0 p3 0 0 0] */
  171. a4 *= 2;
  172. d += (uint128_t)a0 * a4
  173. + (uint128_t)(a1*2) * a3
  174. + (uint128_t)a2 * a2;
  175. VERIFY_BITS(d, 115);
  176. /* [c 0 0 0 0 d t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  177. d += c * R;
  178. VERIFY_BITS(d, 116);
  179. /* [d t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  180. t4 = d & M; d >>= 52;
  181. VERIFY_BITS(t4, 52);
  182. VERIFY_BITS(d, 64);
  183. /* [d t4 t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  184. tx = (t4 >> 48); t4 &= (M >> 4);
  185. VERIFY_BITS(tx, 4);
  186. VERIFY_BITS(t4, 48);
  187. /* [d t4+(tx<<48) t3 0 0 0] = [p8 0 0 0 p4 p3 0 0 0] */
  188. c = (uint128_t)a0 * a0;
  189. VERIFY_BITS(c, 112);
  190. /* [d t4+(tx<<48) t3 0 0 c] = [p8 0 0 0 p4 p3 0 0 p0] */
  191. d += (uint128_t)a1 * a4
  192. + (uint128_t)(a2*2) * a3;
  193. VERIFY_BITS(d, 114);
  194. /* [d t4+(tx<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  195. u0 = d & M; d >>= 52;
  196. VERIFY_BITS(u0, 52);
  197. VERIFY_BITS(d, 62);
  198. /* [d u0 t4+(tx<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  199. /* [d 0 t4+(tx<<48)+(u0<<52) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  200. u0 = (u0 << 4) | tx;
  201. VERIFY_BITS(u0, 56);
  202. /* [d 0 t4+(u0<<48) t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  203. c += (uint128_t)u0 * (R >> 4);
  204. VERIFY_BITS(c, 113);
  205. /* [d 0 t4 t3 0 0 c] = [p8 0 0 p5 p4 p3 0 0 p0] */
  206. r[0] = c & M; c >>= 52;
  207. VERIFY_BITS(r[0], 52);
  208. VERIFY_BITS(c, 61);
  209. /* [d 0 t4 t3 0 c r0] = [p8 0 0 p5 p4 p3 0 0 p0] */
  210. a0 *= 2;
  211. c += (uint128_t)a0 * a1;
  212. VERIFY_BITS(c, 114);
  213. /* [d 0 t4 t3 0 c r0] = [p8 0 0 p5 p4 p3 0 p1 p0] */
  214. d += (uint128_t)a2 * a4
  215. + (uint128_t)a3 * a3;
  216. VERIFY_BITS(d, 114);
  217. /* [d 0 t4 t3 0 c r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  218. c += (d & M) * R; d >>= 52;
  219. VERIFY_BITS(c, 115);
  220. VERIFY_BITS(d, 62);
  221. /* [d 0 0 t4 t3 0 c r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  222. r[1] = c & M; c >>= 52;
  223. VERIFY_BITS(r[1], 52);
  224. VERIFY_BITS(c, 63);
  225. /* [d 0 0 t4 t3 c r1 r0] = [p8 0 p6 p5 p4 p3 0 p1 p0] */
  226. c += (uint128_t)a0 * a2
  227. + (uint128_t)a1 * a1;
  228. VERIFY_BITS(c, 114);
  229. /* [d 0 0 t4 t3 c r1 r0] = [p8 0 p6 p5 p4 p3 p2 p1 p0] */
  230. d += (uint128_t)a3 * a4;
  231. VERIFY_BITS(d, 114);
  232. /* [d 0 0 t4 t3 c r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  233. c += (d & M) * R; d >>= 52;
  234. VERIFY_BITS(c, 115);
  235. VERIFY_BITS(d, 62);
  236. /* [d 0 0 0 t4 t3 c r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  237. r[2] = c & M; c >>= 52;
  238. VERIFY_BITS(r[2], 52);
  239. VERIFY_BITS(c, 63);
  240. /* [d 0 0 0 t4 t3+c r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  241. c += d * R + t3;;
  242. VERIFY_BITS(c, 100);
  243. /* [t4 c r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  244. r[3] = c & M; c >>= 52;
  245. VERIFY_BITS(r[3], 52);
  246. VERIFY_BITS(c, 48);
  247. /* [t4+c r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  248. c += t4;
  249. VERIFY_BITS(c, 49);
  250. /* [c r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  251. r[4] = c;
  252. VERIFY_BITS(r[4], 49);
  253. /* [r4 r3 r2 r1 r0] = [p8 p7 p6 p5 p4 p3 p2 p1 p0] */
  254. }
  255. #endif