PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/crypto/secp256k1/libsecp256k1/src/ecmult_impl.h

https://gitlab.com/akomba/ether-bot-wallet
C Header | 389 lines | 265 code | 52 blank | 72 comment | 49 complexity | bb2bc6f274479fcd11a0744db5c3a413 MD5 | raw file
  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_ECMULT_IMPL_H_
  7. #define _SECP256K1_ECMULT_IMPL_H_
  8. #include "group.h"
  9. #include "scalar.h"
  10. #include "ecmult.h"
  11. /* optimal for 128-bit and 256-bit exponents. */
  12. #define WINDOW_A 5
  13. /** larger numbers may result in slightly better performance, at the cost of
  14. exponentially larger precomputed tables. */
  15. #ifdef USE_ENDOMORPHISM
  16. /** Two tables for window size 15: 1.375 MiB. */
  17. #define WINDOW_G 15
  18. #else
  19. /** One table for window size 16: 1.375 MiB. */
  20. #define WINDOW_G 16
  21. #endif
  22. /** The number of entries a table with precomputed multiples needs to have. */
  23. #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
  24. /** Fill a table 'prej' with precomputed odd multiples of a. Prej will contain
  25. * the values [1*a,3*a,...,(2*n-1)*a], so it space for n values. zr[0] will
  26. * contain prej[0].z / a.z. The other zr[i] values = prej[i].z / prej[i-1].z.
  27. * Prej's Z values are undefined, except for the last value.
  28. */
  29. static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a) {
  30. secp256k1_gej d;
  31. secp256k1_ge a_ge, d_ge;
  32. int i;
  33. VERIFY_CHECK(!a->infinity);
  34. secp256k1_gej_double_var(&d, a, NULL);
  35. /*
  36. * Perform the additions on an isomorphism where 'd' is affine: drop the z coordinate
  37. * of 'd', and scale the 1P starting value's x/y coordinates without changing its z.
  38. */
  39. d_ge.x = d.x;
  40. d_ge.y = d.y;
  41. d_ge.infinity = 0;
  42. secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
  43. prej[0].x = a_ge.x;
  44. prej[0].y = a_ge.y;
  45. prej[0].z = a->z;
  46. prej[0].infinity = 0;
  47. zr[0] = d.z;
  48. for (i = 1; i < n; i++) {
  49. secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
  50. }
  51. /*
  52. * Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
  53. * the final point's z coordinate is actually used though, so just update that.
  54. */
  55. secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
  56. }
  57. /** Fill a table 'pre' with precomputed odd multiples of a.
  58. *
  59. * There are two versions of this function:
  60. * - secp256k1_ecmult_odd_multiples_table_globalz_windowa which brings its
  61. * resulting point set to a single constant Z denominator, stores the X and Y
  62. * coordinates as ge_storage points in pre, and stores the global Z in rz.
  63. * It only operates on tables sized for WINDOW_A wnaf multiples.
  64. * - secp256k1_ecmult_odd_multiples_table_storage_var, which converts its
  65. * resulting point set to actually affine points, and stores those in pre.
  66. * It operates on tables of any size, but uses heap-allocated temporaries.
  67. *
  68. * To compute a*P + b*G, we compute a table for P using the first function,
  69. * and for G using the second (which requires an inverse, but it only needs to
  70. * happen once).
  71. */
  72. static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) {
  73. secp256k1_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];
  74. secp256k1_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)];
  75. /* Compute the odd multiples in Jacobian form. */
  76. secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
  77. /* Bring them to the same Z denominator. */
  78. secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
  79. }
  80. static void secp256k1_ecmult_odd_multiples_table_storage_var(int n, secp256k1_ge_storage *pre, const secp256k1_gej *a, const secp256k1_callback *cb) {
  81. secp256k1_gej *prej = (secp256k1_gej*)checked_malloc(cb, sizeof(secp256k1_gej) * n);
  82. secp256k1_ge *prea = (secp256k1_ge*)checked_malloc(cb, sizeof(secp256k1_ge) * n);
  83. secp256k1_fe *zr = (secp256k1_fe*)checked_malloc(cb, sizeof(secp256k1_fe) * n);
  84. int i;
  85. /* Compute the odd multiples in Jacobian form. */
  86. secp256k1_ecmult_odd_multiples_table(n, prej, zr, a);
  87. /* Convert them in batch to affine coordinates. */
  88. secp256k1_ge_set_table_gej_var(n, prea, prej, zr);
  89. /* Convert them to compact storage form. */
  90. for (i = 0; i < n; i++) {
  91. secp256k1_ge_to_storage(&pre[i], &prea[i]);
  92. }
  93. free(prea);
  94. free(prej);
  95. free(zr);
  96. }
  97. /** The following two macro retrieves a particular odd multiple from a table
  98. * of precomputed multiples. */
  99. #define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \
  100. VERIFY_CHECK(((n) & 1) == 1); \
  101. VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
  102. VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
  103. if ((n) > 0) { \
  104. *(r) = (pre)[((n)-1)/2]; \
  105. } else { \
  106. secp256k1_ge_neg((r), &(pre)[(-(n)-1)/2]); \
  107. } \
  108. } while(0)
  109. #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
  110. VERIFY_CHECK(((n) & 1) == 1); \
  111. VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
  112. VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
  113. if ((n) > 0) { \
  114. secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
  115. } else { \
  116. secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
  117. secp256k1_ge_neg((r), (r)); \
  118. } \
  119. } while(0)
  120. static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) {
  121. ctx->pre_g = NULL;
  122. #ifdef USE_ENDOMORPHISM
  123. ctx->pre_g_128 = NULL;
  124. #endif
  125. }
  126. static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb) {
  127. secp256k1_gej gj;
  128. if (ctx->pre_g != NULL) {
  129. return;
  130. }
  131. /* get the generator */
  132. secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
  133. ctx->pre_g = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
  134. /* precompute the tables with odd multiples */
  135. secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj, cb);
  136. #ifdef USE_ENDOMORPHISM
  137. {
  138. secp256k1_gej g_128j;
  139. int i;
  140. ctx->pre_g_128 = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
  141. /* calculate 2^128*generator */
  142. g_128j = gj;
  143. for (i = 0; i < 128; i++) {
  144. secp256k1_gej_double_var(&g_128j, &g_128j, NULL);
  145. }
  146. secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g_128, &g_128j, cb);
  147. }
  148. #endif
  149. }
  150. static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
  151. const secp256k1_ecmult_context *src, const secp256k1_callback *cb) {
  152. if (src->pre_g == NULL) {
  153. dst->pre_g = NULL;
  154. } else {
  155. size_t size = sizeof((*dst->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
  156. dst->pre_g = (secp256k1_ge_storage (*)[])checked_malloc(cb, size);
  157. memcpy(dst->pre_g, src->pre_g, size);
  158. }
  159. #ifdef USE_ENDOMORPHISM
  160. if (src->pre_g_128 == NULL) {
  161. dst->pre_g_128 = NULL;
  162. } else {
  163. size_t size = sizeof((*dst->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
  164. dst->pre_g_128 = (secp256k1_ge_storage (*)[])checked_malloc(cb, size);
  165. memcpy(dst->pre_g_128, src->pre_g_128, size);
  166. }
  167. #endif
  168. }
  169. static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx) {
  170. return ctx->pre_g != NULL;
  171. }
  172. static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx) {
  173. free(ctx->pre_g);
  174. #ifdef USE_ENDOMORPHISM
  175. free(ctx->pre_g_128);
  176. #endif
  177. secp256k1_ecmult_context_init(ctx);
  178. }
  179. /** Convert a number to WNAF notation. The number becomes represented by sum(2^i * wnaf[i], i=0..bits),
  180. * with the following guarantees:
  181. * - each wnaf[i] is either 0, or an odd integer between -(1<<(w-1) - 1) and (1<<(w-1) - 1)
  182. * - two non-zero entries in wnaf are separated by at least w-1 zeroes.
  183. * - the number of set values in wnaf is returned. This number is at most 256, and at most one more
  184. * than the number of bits in the (absolute value) of the input.
  185. */
  186. static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w) {
  187. secp256k1_scalar s = *a;
  188. int last_set_bit = -1;
  189. int bit = 0;
  190. int sign = 1;
  191. int carry = 0;
  192. VERIFY_CHECK(wnaf != NULL);
  193. VERIFY_CHECK(0 <= len && len <= 256);
  194. VERIFY_CHECK(a != NULL);
  195. VERIFY_CHECK(2 <= w && w <= 31);
  196. memset(wnaf, 0, len * sizeof(wnaf[0]));
  197. if (secp256k1_scalar_get_bits(&s, 255, 1)) {
  198. secp256k1_scalar_negate(&s, &s);
  199. sign = -1;
  200. }
  201. while (bit < len) {
  202. int now;
  203. int word;
  204. if (secp256k1_scalar_get_bits(&s, bit, 1) == (unsigned int)carry) {
  205. bit++;
  206. continue;
  207. }
  208. now = w;
  209. if (now > len - bit) {
  210. now = len - bit;
  211. }
  212. word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
  213. carry = (word >> (w-1)) & 1;
  214. word -= carry << w;
  215. wnaf[bit] = sign * word;
  216. last_set_bit = bit;
  217. bit += now;
  218. }
  219. #ifdef VERIFY
  220. CHECK(carry == 0);
  221. while (bit < 256) {
  222. CHECK(secp256k1_scalar_get_bits(&s, bit++, 1) == 0);
  223. }
  224. #endif
  225. return last_set_bit + 1;
  226. }
  227. static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
  228. secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
  229. secp256k1_ge tmpa;
  230. secp256k1_fe Z;
  231. #ifdef USE_ENDOMORPHISM
  232. secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
  233. secp256k1_scalar na_1, na_lam;
  234. /* Splitted G factors. */
  235. secp256k1_scalar ng_1, ng_128;
  236. int wnaf_na_1[130];
  237. int wnaf_na_lam[130];
  238. int bits_na_1;
  239. int bits_na_lam;
  240. int wnaf_ng_1[129];
  241. int bits_ng_1;
  242. int wnaf_ng_128[129];
  243. int bits_ng_128;
  244. #else
  245. int wnaf_na[256];
  246. int bits_na;
  247. int wnaf_ng[256];
  248. int bits_ng;
  249. #endif
  250. int i;
  251. int bits;
  252. #ifdef USE_ENDOMORPHISM
  253. /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
  254. secp256k1_scalar_split_lambda(&na_1, &na_lam, na);
  255. /* build wnaf representation for na_1 and na_lam. */
  256. bits_na_1 = secp256k1_ecmult_wnaf(wnaf_na_1, 130, &na_1, WINDOW_A);
  257. bits_na_lam = secp256k1_ecmult_wnaf(wnaf_na_lam, 130, &na_lam, WINDOW_A);
  258. VERIFY_CHECK(bits_na_1 <= 130);
  259. VERIFY_CHECK(bits_na_lam <= 130);
  260. bits = bits_na_1;
  261. if (bits_na_lam > bits) {
  262. bits = bits_na_lam;
  263. }
  264. #else
  265. /* build wnaf representation for na. */
  266. bits_na = secp256k1_ecmult_wnaf(wnaf_na, 256, na, WINDOW_A);
  267. bits = bits_na;
  268. #endif
  269. /* Calculate odd multiples of a.
  270. * All multiples are brought to the same Z 'denominator', which is stored
  271. * in Z. Due to secp256k1' isomorphism we can do all operations pretending
  272. * that the Z coordinate was 1, use affine addition formulae, and correct
  273. * the Z coordinate of the result once at the end.
  274. * The exception is the precomputed G table points, which are actually
  275. * affine. Compared to the base used for other points, they have a Z ratio
  276. * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
  277. * isomorphism to efficiently add with a known Z inverse.
  278. */
  279. secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, a);
  280. #ifdef USE_ENDOMORPHISM
  281. for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
  282. secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
  283. }
  284. /* split ng into ng_1 and ng_128 (where gn = gn_1 + gn_128*2^128, and gn_1 and gn_128 are ~128 bit) */
  285. secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
  286. /* Build wnaf representation for ng_1 and ng_128 */
  287. bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, 129, &ng_1, WINDOW_G);
  288. bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, 129, &ng_128, WINDOW_G);
  289. if (bits_ng_1 > bits) {
  290. bits = bits_ng_1;
  291. }
  292. if (bits_ng_128 > bits) {
  293. bits = bits_ng_128;
  294. }
  295. #else
  296. bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, 256, ng, WINDOW_G);
  297. if (bits_ng > bits) {
  298. bits = bits_ng;
  299. }
  300. #endif
  301. secp256k1_gej_set_infinity(r);
  302. for (i = bits - 1; i >= 0; i--) {
  303. int n;
  304. secp256k1_gej_double_var(r, r, NULL);
  305. #ifdef USE_ENDOMORPHISM
  306. if (i < bits_na_1 && (n = wnaf_na_1[i])) {
  307. ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
  308. secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
  309. }
  310. if (i < bits_na_lam && (n = wnaf_na_lam[i])) {
  311. ECMULT_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
  312. secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
  313. }
  314. if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
  315. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
  316. secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
  317. }
  318. if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
  319. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g_128, n, WINDOW_G);
  320. secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
  321. }
  322. #else
  323. if (i < bits_na && (n = wnaf_na[i])) {
  324. ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
  325. secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
  326. }
  327. if (i < bits_ng && (n = wnaf_ng[i])) {
  328. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
  329. secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
  330. }
  331. #endif
  332. }
  333. if (!r->infinity) {
  334. secp256k1_fe_mul(&r->z, &r->z, &Z);
  335. }
  336. }
  337. #endif