field_5x52_impl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_REPR_IMPL_H_
  7. #define _SECP256K1_FIELD_REPR_IMPL_H_
  8. #if defined HAVE_CONFIG_H
  9. #include "libsecp256k1-config.h"
  10. #endif
  11. #include <string.h>
  12. #include "util.h"
  13. #include "num.h"
  14. #include "field.h"
  15. #if defined(USE_ASM_X86_64)
  16. #include "field_5x52_asm_impl.h"
  17. #else
  18. #include "field_5x52_int128_impl.h"
  19. #endif
  20. /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
  21. * represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
  22. * each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
  23. * is at most M*(2^53-1), except the most significant one, which is limited to M*(2^49-1). All operations
  24. * accept any input with magnitude at most M, and have different rules for propagating magnitude to their
  25. * output.
  26. */
  27. #ifdef VERIFY
  28. static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
  29. const uint64_t *d = a->n;
  30. int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
  31. /* secp256k1 'p' value defined in "Standards for Efficient Cryptography" (SEC2) 2.7.1. */
  32. r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
  33. r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
  34. r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
  35. r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
  36. r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
  37. r &= (a->magnitude >= 0);
  38. r &= (a->magnitude <= 2048);
  39. if (a->normalized) {
  40. r &= (a->magnitude <= 1);
  41. if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
  42. r &= (d[0] < 0xFFFFEFFFFFC2FULL);
  43. }
  44. }
  45. VERIFY_CHECK(r == 1);
  46. }
  47. #else
  48. static void secp256k1_fe_verify(const secp256k1_fe_t *a) {
  49. (void)a;
  50. }
  51. #endif
  52. static void secp256k1_fe_normalize(secp256k1_fe_t *r) {
  53. uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
  54. /* Reduce t4 at the start so there will be at most a single carry from the first pass */
  55. uint64_t m;
  56. uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
  57. /* The first pass ensures the magnitude is 1, ... */
  58. t0 += x * 0x1000003D1ULL;
  59. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
  60. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
  61. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
  62. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
  63. /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
  64. VERIFY_CHECK(t4 >> 49 == 0);
  65. /* At most a single final reduction is needed; check if the value is >= the field characteristic */
  66. x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
  67. & (t0 >= 0xFFFFEFFFFFC2FULL));
  68. /* Apply the final reduction (for constant-time behaviour, we do it always) */
  69. t0 += x * 0x1000003D1ULL;
  70. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
  71. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
  72. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
  73. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
  74. /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
  75. VERIFY_CHECK(t4 >> 48 == x);
  76. /* Mask off the possible multiple of 2^256 from the final reduction */
  77. t4 &= 0x0FFFFFFFFFFFFULL;
  78. r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
  79. #ifdef VERIFY
  80. r->magnitude = 1;
  81. r->normalized = 1;
  82. secp256k1_fe_verify(r);
  83. #endif
  84. }
  85. static void secp256k1_fe_normalize_weak(secp256k1_fe_t *r) {
  86. uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
  87. /* Reduce t4 at the start so there will be at most a single carry from the first pass */
  88. uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
  89. /* The first pass ensures the magnitude is 1, ... */
  90. t0 += x * 0x1000003D1ULL;
  91. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
  92. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
  93. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
  94. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
  95. /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
  96. VERIFY_CHECK(t4 >> 49 == 0);
  97. r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
  98. #ifdef VERIFY
  99. r->magnitude = 1;
  100. secp256k1_fe_verify(r);
  101. #endif
  102. }
  103. static void secp256k1_fe_normalize_var(secp256k1_fe_t *r) {
  104. uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
  105. /* Reduce t4 at the start so there will be at most a single carry from the first pass */
  106. uint64_t m;
  107. uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
  108. /* The first pass ensures the magnitude is 1, ... */
  109. t0 += x * 0x1000003D1ULL;
  110. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
  111. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
  112. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
  113. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
  114. /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
  115. VERIFY_CHECK(t4 >> 49 == 0);
  116. /* At most a single final reduction is needed; check if the value is >= the field characteristic */
  117. x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
  118. & (t0 >= 0xFFFFEFFFFFC2FULL));
  119. if (x) {
  120. t0 += 0x1000003D1ULL;
  121. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
  122. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
  123. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
  124. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
  125. /* If t4 didn't carry to bit 48 already, then it should have after any final reduction */
  126. VERIFY_CHECK(t4 >> 48 == x);
  127. /* Mask off the possible multiple of 2^256 from the final reduction */
  128. t4 &= 0x0FFFFFFFFFFFFULL;
  129. }
  130. r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
  131. #ifdef VERIFY
  132. r->magnitude = 1;
  133. r->normalized = 1;
  134. secp256k1_fe_verify(r);
  135. #endif
  136. }
  137. static int secp256k1_fe_normalizes_to_zero(secp256k1_fe_t *r) {
  138. uint64_t t0 = r->n[0], t1 = r->n[1], t2 = r->n[2], t3 = r->n[3], t4 = r->n[4];
  139. /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
  140. uint64_t z0, z1;
  141. /* Reduce t4 at the start so there will be at most a single carry from the first pass */
  142. uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
  143. /* The first pass ensures the magnitude is 1, ... */
  144. t0 += x * 0x1000003D1ULL;
  145. t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
  146. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
  147. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
  148. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
  149. z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
  150. /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
  151. VERIFY_CHECK(t4 >> 49 == 0);
  152. return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
  153. }
  154. static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe_t *r) {
  155. uint64_t t0, t1, t2, t3, t4;
  156. uint64_t z0, z1;
  157. uint64_t x;
  158. t0 = r->n[0];
  159. t4 = r->n[4];
  160. /* Reduce t4 at the start so there will be at most a single carry from the first pass */
  161. x = t4 >> 48;
  162. /* The first pass ensures the magnitude is 1, ... */
  163. t0 += x * 0x1000003D1ULL;
  164. /* z0 tracks a possible raw value of 0, z1 tracks a possible raw value of P */
  165. z0 = t0 & 0xFFFFFFFFFFFFFULL;
  166. z1 = z0 ^ 0x1000003D0ULL;
  167. /* Fast return path should catch the majority of cases */
  168. if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
  169. return 0;
  170. }
  171. t1 = r->n[1];
  172. t2 = r->n[2];
  173. t3 = r->n[3];
  174. t4 &= 0x0FFFFFFFFFFFFULL;
  175. t1 += (t0 >> 52); t0 = z0;
  176. t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
  177. t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
  178. t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
  179. z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
  180. /* ... except for a possible carry at bit 48 of t4 (i.e. bit 256 of the field element) */
  181. VERIFY_CHECK(t4 >> 49 == 0);
  182. return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
  183. }
  184. SECP256K1_INLINE static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a) {
  185. r->n[0] = a;
  186. r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
  187. #ifdef VERIFY
  188. r->magnitude = 1;
  189. r->normalized = 1;
  190. secp256k1_fe_verify(r);
  191. #endif
  192. }
  193. SECP256K1_INLINE static int secp256k1_fe_is_zero(const secp256k1_fe_t *a) {
  194. const uint64_t *t = a->n;
  195. #ifdef VERIFY
  196. VERIFY_CHECK(a->normalized);
  197. secp256k1_fe_verify(a);
  198. #endif
  199. return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
  200. }
  201. SECP256K1_INLINE static int secp256k1_fe_is_odd(const secp256k1_fe_t *a) {
  202. #ifdef VERIFY
  203. VERIFY_CHECK(a->normalized);
  204. secp256k1_fe_verify(a);
  205. #endif
  206. return a->n[0] & 1;
  207. }
  208. SECP256K1_INLINE static void secp256k1_fe_clear(secp256k1_fe_t *a) {
  209. int i;
  210. #ifdef VERIFY
  211. a->magnitude = 0;
  212. a->normalized = 1;
  213. #endif
  214. for (i=0; i<5; i++) {
  215. a->n[i] = 0;
  216. }
  217. }
  218. static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
  219. int i;
  220. #ifdef VERIFY
  221. VERIFY_CHECK(a->normalized);
  222. VERIFY_CHECK(b->normalized);
  223. secp256k1_fe_verify(a);
  224. secp256k1_fe_verify(b);
  225. #endif
  226. for (i = 4; i >= 0; i--) {
  227. if (a->n[i] > b->n[i]) {
  228. return 1;
  229. }
  230. if (a->n[i] < b->n[i]) {
  231. return -1;
  232. }
  233. }
  234. return 0;
  235. }
  236. static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a) {
  237. int i;
  238. r->n[0] = r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
  239. for (i=0; i<32; i++) {
  240. int j;
  241. for (j=0; j<2; j++) {
  242. int limb = (8*i+4*j)/52;
  243. int shift = (8*i+4*j)%52;
  244. r->n[limb] |= (uint64_t)((a[31-i] >> (4*j)) & 0xF) << shift;
  245. }
  246. }
  247. if (r->n[4] == 0x0FFFFFFFFFFFFULL && (r->n[3] & r->n[2] & r->n[1]) == 0xFFFFFFFFFFFFFULL && r->n[0] >= 0xFFFFEFFFFFC2FULL) {
  248. return 0;
  249. }
  250. #ifdef VERIFY
  251. r->magnitude = 1;
  252. r->normalized = 1;
  253. secp256k1_fe_verify(r);
  254. #endif
  255. return 1;
  256. }
  257. /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
  258. static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a) {
  259. int i;
  260. #ifdef VERIFY
  261. VERIFY_CHECK(a->normalized);
  262. secp256k1_fe_verify(a);
  263. #endif
  264. for (i=0; i<32; i++) {
  265. int j;
  266. int c = 0;
  267. for (j=0; j<2; j++) {
  268. int limb = (8*i+4*j)/52;
  269. int shift = (8*i+4*j)%52;
  270. c |= ((a->n[limb] >> shift) & 0xF) << (4 * j);
  271. }
  272. r[31-i] = c;
  273. }
  274. }
  275. SECP256K1_INLINE static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) {
  276. #ifdef VERIFY
  277. VERIFY_CHECK(a->magnitude <= m);
  278. secp256k1_fe_verify(a);
  279. #endif
  280. r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->n[0];
  281. r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[1];
  282. r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[2];
  283. r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->n[3];
  284. r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->n[4];
  285. #ifdef VERIFY
  286. r->magnitude = m + 1;
  287. r->normalized = 0;
  288. secp256k1_fe_verify(r);
  289. #endif
  290. }
  291. SECP256K1_INLINE static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) {
  292. r->n[0] *= a;
  293. r->n[1] *= a;
  294. r->n[2] *= a;
  295. r->n[3] *= a;
  296. r->n[4] *= a;
  297. #ifdef VERIFY
  298. r->magnitude *= a;
  299. r->normalized = 0;
  300. secp256k1_fe_verify(r);
  301. #endif
  302. }
  303. SECP256K1_INLINE static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
  304. #ifdef VERIFY
  305. secp256k1_fe_verify(a);
  306. #endif
  307. r->n[0] += a->n[0];
  308. r->n[1] += a->n[1];
  309. r->n[2] += a->n[2];
  310. r->n[3] += a->n[3];
  311. r->n[4] += a->n[4];
  312. #ifdef VERIFY
  313. r->magnitude += a->magnitude;
  314. r->normalized = 0;
  315. secp256k1_fe_verify(r);
  316. #endif
  317. }
  318. static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b) {
  319. #ifdef VERIFY
  320. VERIFY_CHECK(a->magnitude <= 8);
  321. VERIFY_CHECK(b->magnitude <= 8);
  322. secp256k1_fe_verify(a);
  323. secp256k1_fe_verify(b);
  324. VERIFY_CHECK(r != b);
  325. #endif
  326. secp256k1_fe_mul_inner(r->n, a->n, b->n);
  327. #ifdef VERIFY
  328. r->magnitude = 1;
  329. r->normalized = 0;
  330. secp256k1_fe_verify(r);
  331. #endif
  332. }
  333. static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
  334. #ifdef VERIFY
  335. VERIFY_CHECK(a->magnitude <= 8);
  336. secp256k1_fe_verify(a);
  337. #endif
  338. secp256k1_fe_sqr_inner(r->n, a->n);
  339. #ifdef VERIFY
  340. r->magnitude = 1;
  341. r->normalized = 0;
  342. secp256k1_fe_verify(r);
  343. #endif
  344. }
  345. static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag) {
  346. uint64_t mask0, mask1;
  347. mask0 = flag + ~((uint64_t)0);
  348. mask1 = ~mask0;
  349. r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
  350. r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
  351. r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
  352. r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
  353. r->n[4] = (r->n[4] & mask0) | (a->n[4] & mask1);
  354. #ifdef VERIFY
  355. r->magnitude = (r->magnitude & mask0) | (a->magnitude & mask1);
  356. r->normalized = (r->normalized & mask0) | (a->normalized & mask1);
  357. #endif
  358. }
  359. static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage_t *r, const secp256k1_fe_storage_t *a, int flag) {
  360. uint64_t mask0, mask1;
  361. mask0 = flag + ~((uint64_t)0);
  362. mask1 = ~mask0;
  363. r->n[0] = (r->n[0] & mask0) | (a->n[0] & mask1);
  364. r->n[1] = (r->n[1] & mask0) | (a->n[1] & mask1);
  365. r->n[2] = (r->n[2] & mask0) | (a->n[2] & mask1);
  366. r->n[3] = (r->n[3] & mask0) | (a->n[3] & mask1);
  367. }
  368. static void secp256k1_fe_to_storage(secp256k1_fe_storage_t *r, const secp256k1_fe_t *a) {
  369. #ifdef VERIFY
  370. VERIFY_CHECK(a->normalized);
  371. #endif
  372. r->n[0] = a->n[0] | a->n[1] << 52;
  373. r->n[1] = a->n[1] >> 12 | a->n[2] << 40;
  374. r->n[2] = a->n[2] >> 24 | a->n[3] << 28;
  375. r->n[3] = a->n[3] >> 36 | a->n[4] << 16;
  376. }
  377. static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe_t *r, const secp256k1_fe_storage_t *a) {
  378. r->n[0] = a->n[0] & 0xFFFFFFFFFFFFFULL;
  379. r->n[1] = a->n[0] >> 52 | ((a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
  380. r->n[2] = a->n[1] >> 40 | ((a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
  381. r->n[3] = a->n[2] >> 28 | ((a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
  382. r->n[4] = a->n[3] >> 16;
  383. #ifdef VERIFY
  384. r->magnitude = 1;
  385. r->normalized = 1;
  386. #endif
  387. }
  388. #endif