ecmult_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /** Fill a table 'pre' with precomputed odd multiples of a. W determines the size of the table.
  23. * pre will contains the values [1*a,3*a,5*a,...,(2^(w-1)-1)*a], so it needs place for
  24. * 2^(w-2) entries.
  25. *
  26. * There are two versions of this function:
  27. * - secp256k1_ecmult_precomp_wnaf_gej, which operates on group elements in jacobian notation,
  28. * fast to precompute, but slower to use in later additions.
  29. * - secp256k1_ecmult_precomp_wnaf_ge, which operates on group elements in affine notations,
  30. * (much) slower to precompute, but a bit faster to use in later additions.
  31. * To compute a*P + b*G, we use the jacobian version for P, and the affine version for G, as
  32. * G is constant, so it only needs to be done once in advance.
  33. */
  34. static void secp256k1_ecmult_table_precomp_gej_var(secp256k1_gej_t *pre, const secp256k1_gej_t *a, int w) {
  35. secp256k1_gej_t d;
  36. int i;
  37. pre[0] = *a;
  38. secp256k1_gej_double_var(&d, &pre[0]);
  39. for (i = 1; i < (1 << (w-2)); i++) {
  40. secp256k1_gej_add_var(&pre[i], &d, &pre[i-1]);
  41. }
  42. }
  43. static void secp256k1_ecmult_table_precomp_ge_storage_var(secp256k1_ge_storage_t *pre, const secp256k1_gej_t *a, int w) {
  44. secp256k1_gej_t d;
  45. int i;
  46. const int table_size = 1 << (w-2);
  47. secp256k1_gej_t *prej = (secp256k1_gej_t *)checked_malloc(sizeof(secp256k1_gej_t) * table_size);
  48. secp256k1_ge_t *prea = (secp256k1_ge_t *)checked_malloc(sizeof(secp256k1_ge_t) * table_size);
  49. prej[0] = *a;
  50. secp256k1_gej_double_var(&d, a);
  51. for (i = 1; i < table_size; i++) {
  52. secp256k1_gej_add_var(&prej[i], &d, &prej[i-1]);
  53. }
  54. secp256k1_ge_set_all_gej_var(table_size, prea, prej);
  55. for (i = 0; i < table_size; i++) {
  56. secp256k1_ge_to_storage(&pre[i], &prea[i]);
  57. }
  58. free(prej);
  59. free(prea);
  60. }
  61. /** The number of entries a table with precomputed multiples needs to have. */
  62. #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
  63. /** The following two macro retrieves a particular odd multiple from a table
  64. * of precomputed multiples. */
  65. #define ECMULT_TABLE_GET_GEJ(r,pre,n,w) do { \
  66. VERIFY_CHECK(((n) & 1) == 1); \
  67. VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
  68. VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
  69. if ((n) > 0) { \
  70. *(r) = (pre)[((n)-1)/2]; \
  71. } else { \
  72. secp256k1_gej_neg((r), &(pre)[(-(n)-1)/2]); \
  73. } \
  74. } while(0)
  75. #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
  76. VERIFY_CHECK(((n) & 1) == 1); \
  77. VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
  78. VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
  79. if ((n) > 0) { \
  80. secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
  81. } else { \
  82. secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
  83. secp256k1_ge_neg((r), (r)); \
  84. } \
  85. } while(0)
  86. static void secp256k1_ecmult_context_init(secp256k1_ecmult_context_t *ctx) {
  87. ctx->pre_g = NULL;
  88. #ifdef USE_ENDOMORPHISM
  89. ctx->pre_g_128 = NULL;
  90. #endif
  91. }
  92. static void secp256k1_ecmult_context_build(secp256k1_ecmult_context_t *ctx) {
  93. secp256k1_gej_t gj;
  94. if (ctx->pre_g != NULL) {
  95. return;
  96. }
  97. /* get the generator */
  98. secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
  99. ctx->pre_g = (secp256k1_ge_storage_t (*)[])checked_malloc(sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
  100. /* precompute the tables with odd multiples */
  101. secp256k1_ecmult_table_precomp_ge_storage_var(*ctx->pre_g, &gj, WINDOW_G);
  102. #ifdef USE_ENDOMORPHISM
  103. {
  104. secp256k1_gej_t g_128j;
  105. int i;
  106. ctx->pre_g_128 = (secp256k1_ge_storage_t (*)[])checked_malloc(sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
  107. /* calculate 2^128*generator */
  108. g_128j = gj;
  109. for (i = 0; i < 128; i++) {
  110. secp256k1_gej_double_var(&g_128j, &g_128j);
  111. }
  112. secp256k1_ecmult_table_precomp_ge_storage_var(*ctx->pre_g_128, &g_128j, WINDOW_G);
  113. }
  114. #endif
  115. }
  116. static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context_t *dst,
  117. const secp256k1_ecmult_context_t *src) {
  118. if (src->pre_g == NULL) {
  119. dst->pre_g = NULL;
  120. } else {
  121. size_t size = sizeof((*dst->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
  122. dst->pre_g = (secp256k1_ge_storage_t (*)[])checked_malloc(size);
  123. memcpy(dst->pre_g, src->pre_g, size);
  124. }
  125. #ifdef USE_ENDOMORPHISM
  126. if (src->pre_g_128 == NULL) {
  127. dst->pre_g_128 = NULL;
  128. } else {
  129. size_t size = sizeof((*dst->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
  130. dst->pre_g_128 = (secp256k1_ge_storage_t (*)[])checked_malloc(size);
  131. memcpy(dst->pre_g_128, src->pre_g_128, size);
  132. }
  133. #endif
  134. }
  135. static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context_t *ctx) {
  136. return ctx->pre_g != NULL;
  137. }
  138. static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context_t *ctx) {
  139. free(ctx->pre_g);
  140. #ifdef USE_ENDOMORPHISM
  141. free(ctx->pre_g_128);
  142. #endif
  143. secp256k1_ecmult_context_init(ctx);
  144. }
  145. /** Convert a number to WNAF notation. The number becomes represented by sum(2^i * wnaf[i], i=0..bits),
  146. * with the following guarantees:
  147. * - each wnaf[i] is either 0, or an odd integer between -(1<<(w-1) - 1) and (1<<(w-1) - 1)
  148. * - two non-zero entries in wnaf are separated by at least w-1 zeroes.
  149. * - the number of set values in wnaf is returned. This number is at most 256, and at most one more
  150. * - than the number of bits in the (absolute value) of the input.
  151. */
  152. static int secp256k1_ecmult_wnaf(int *wnaf, const secp256k1_scalar_t *a, int w) {
  153. secp256k1_scalar_t s = *a;
  154. int set_bits = 0;
  155. int bit = 0;
  156. int sign = 1;
  157. if (secp256k1_scalar_get_bits(&s, 255, 1)) {
  158. secp256k1_scalar_negate(&s, &s);
  159. sign = -1;
  160. }
  161. while (bit < 256) {
  162. int now;
  163. int word;
  164. if (secp256k1_scalar_get_bits(&s, bit, 1) == 0) {
  165. bit++;
  166. continue;
  167. }
  168. while (set_bits < bit) {
  169. wnaf[set_bits++] = 0;
  170. }
  171. now = w;
  172. if (bit + now > 256) {
  173. now = 256 - bit;
  174. }
  175. word = secp256k1_scalar_get_bits_var(&s, bit, now);
  176. if (word & (1 << (w-1))) {
  177. secp256k1_scalar_add_bit(&s, bit + w);
  178. wnaf[set_bits++] = sign * (word - (1 << w));
  179. } else {
  180. wnaf[set_bits++] = sign * word;
  181. }
  182. bit += now;
  183. }
  184. return set_bits;
  185. }
  186. static void secp256k1_ecmult(const secp256k1_ecmult_context_t *ctx, secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_scalar_t *na, const secp256k1_scalar_t *ng) {
  187. secp256k1_gej_t tmpj;
  188. secp256k1_gej_t pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
  189. secp256k1_ge_t tmpa;
  190. #ifdef USE_ENDOMORPHISM
  191. secp256k1_gej_t pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
  192. secp256k1_scalar_t na_1, na_lam;
  193. /* Splitted G factors. */
  194. secp256k1_scalar_t ng_1, ng_128;
  195. int wnaf_na_1[130];
  196. int wnaf_na_lam[130];
  197. int bits_na_1;
  198. int bits_na_lam;
  199. int wnaf_ng_1[129];
  200. int bits_ng_1;
  201. int wnaf_ng_128[129];
  202. int bits_ng_128;
  203. #else
  204. int wnaf_na[256];
  205. int bits_na;
  206. int wnaf_ng[257];
  207. int bits_ng;
  208. #endif
  209. int i;
  210. int bits;
  211. #ifdef USE_ENDOMORPHISM
  212. /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
  213. secp256k1_scalar_split_lambda_var(&na_1, &na_lam, na);
  214. /* build wnaf representation for na_1 and na_lam. */
  215. bits_na_1 = secp256k1_ecmult_wnaf(wnaf_na_1, &na_1, WINDOW_A);
  216. bits_na_lam = secp256k1_ecmult_wnaf(wnaf_na_lam, &na_lam, WINDOW_A);
  217. VERIFY_CHECK(bits_na_1 <= 130);
  218. VERIFY_CHECK(bits_na_lam <= 130);
  219. bits = bits_na_1;
  220. if (bits_na_lam > bits) {
  221. bits = bits_na_lam;
  222. }
  223. #else
  224. /* build wnaf representation for na. */
  225. bits_na = secp256k1_ecmult_wnaf(wnaf_na, na, WINDOW_A);
  226. bits = bits_na;
  227. #endif
  228. /* calculate odd multiples of a */
  229. secp256k1_ecmult_table_precomp_gej_var(pre_a, a, WINDOW_A);
  230. #ifdef USE_ENDOMORPHISM
  231. for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
  232. secp256k1_gej_mul_lambda(&pre_a_lam[i], &pre_a[i]);
  233. }
  234. /* 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) */
  235. secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
  236. /* Build wnaf representation for ng_1 and ng_128 */
  237. bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, &ng_1, WINDOW_G);
  238. bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, &ng_128, WINDOW_G);
  239. if (bits_ng_1 > bits) {
  240. bits = bits_ng_1;
  241. }
  242. if (bits_ng_128 > bits) {
  243. bits = bits_ng_128;
  244. }
  245. #else
  246. bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, ng, WINDOW_G);
  247. if (bits_ng > bits) {
  248. bits = bits_ng;
  249. }
  250. #endif
  251. secp256k1_gej_set_infinity(r);
  252. for (i = bits-1; i >= 0; i--) {
  253. int n;
  254. secp256k1_gej_double_var(r, r);
  255. #ifdef USE_ENDOMORPHISM
  256. if (i < bits_na_1 && (n = wnaf_na_1[i])) {
  257. ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
  258. secp256k1_gej_add_var(r, r, &tmpj);
  259. }
  260. if (i < bits_na_lam && (n = wnaf_na_lam[i])) {
  261. ECMULT_TABLE_GET_GEJ(&tmpj, pre_a_lam, n, WINDOW_A);
  262. secp256k1_gej_add_var(r, r, &tmpj);
  263. }
  264. if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
  265. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
  266. secp256k1_gej_add_ge_var(r, r, &tmpa);
  267. }
  268. if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
  269. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g_128, n, WINDOW_G);
  270. secp256k1_gej_add_ge_var(r, r, &tmpa);
  271. }
  272. #else
  273. if (i < bits_na && (n = wnaf_na[i])) {
  274. ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
  275. secp256k1_gej_add_var(r, r, &tmpj);
  276. }
  277. if (i < bits_ng && (n = wnaf_ng[i])) {
  278. ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
  279. secp256k1_gej_add_ge_var(r, r, &tmpa);
  280. }
  281. #endif
  282. }
  283. }
  284. #endif