PRCYCoin  2.0.0.7rc1
P2P Digital Currency
ecmult_impl.h
Go to the documentation of this 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 
7 #ifndef _SECP256K1_ECMULT_IMPL_H_
8 #define _SECP256K1_ECMULT_IMPL_H_
9 
10 #include "group.h"
11 #include "scalar.h"
12 #include "ecmult.h"
13 
14 /* optimal for 128-bit and 256-bit exponents. */
15 #define WINDOW_A 5
16 
19 #ifdef USE_ENDOMORPHISM
20 #define WINDOW_G 14
21 #else
22 #define WINDOW_G 15
23 #endif
24 
37 static void secp256k1_ecmult_table_precomp_gej_var(secp256k1_gej_t *pre, const secp256k1_gej_t *a, int w) {
38  pre[0] = *a;
39  secp256k1_gej_t d; secp256k1_gej_double_var(&d, &pre[0]);
40  for (int i=1; i<(1 << (w-2)); i++)
41  secp256k1_gej_add_var(&pre[i], &d, &pre[i-1]);
42 }
43 
44 static void secp256k1_ecmult_table_precomp_ge_var(secp256k1_ge_t *pre, const secp256k1_gej_t *a, int w) {
45  const int table_size = 1 << (w-2);
46  secp256k1_gej_t prej[table_size];
47  prej[0] = *a;
48  secp256k1_gej_t d; secp256k1_gej_double_var(&d, a);
49  for (int i=1; i<table_size; i++) {
50  secp256k1_gej_add_var(&prej[i], &d, &prej[i-1]);
51  }
52  secp256k1_ge_set_all_gej_var(table_size, pre, prej);
53 }
54 
56 #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
57 
60 #define ECMULT_TABLE_GET(r,pre,n,w,neg) do { \
61  VERIFY_CHECK(((n) & 1) == 1); \
62  VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
63  VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
64  if ((n) > 0) \
65  *(r) = (pre)[((n)-1)/2]; \
66  else \
67  (neg)((r), &(pre)[(-(n)-1)/2]); \
68 } while(0)
69 
70 #define ECMULT_TABLE_GET_GEJ(r,pre,n,w) ECMULT_TABLE_GET((r),(pre),(n),(w),secp256k1_gej_neg)
71 #define ECMULT_TABLE_GET_GE(r,pre,n,w) ECMULT_TABLE_GET((r),(pre),(n),(w),secp256k1_ge_neg)
72 
73 typedef struct {
74  /* For accelerating the computation of a*P + b*G: */
75  secp256k1_ge_t pre_g[ECMULT_TABLE_SIZE(WINDOW_G)]; /* odd multiples of the generator */
76 #ifdef USE_ENDOMORPHISM
77  secp256k1_ge_t pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)]; /* odd multiples of 2^128*generator */
78 #endif
80 
81 static const secp256k1_ecmult_consts_t *secp256k1_ecmult_consts = NULL;
82 
83 static void secp256k1_ecmult_start(void) {
84  if (secp256k1_ecmult_consts != NULL)
85  return;
86  /* Allocate the precomputation table. */
88  /* get the generator */
89  const secp256k1_ge_t *g = &secp256k1_ge_consts->g;
90  secp256k1_gej_t gj; secp256k1_gej_set_ge(&gj, g);
91 
92 #ifdef USE_ENDOMORPHISM
93  /* calculate 2^128*generator */
94  secp256k1_gej_t g_128j = gj;
95  for (int i=0; i<128; i++)
96  secp256k1_gej_double_var(&g_128j, &g_128j);
97 #endif
98  /* precompute the tables with odd multiples */
99  secp256k1_ecmult_table_precomp_ge_var(ret->pre_g, &gj, WINDOW_G);
100 
101 #ifdef USE_ENDOMORPHISM
102  secp256k1_ecmult_table_precomp_ge_var(ret->pre_g_128, &g_128j, WINDOW_G);
103 #endif
104  /* Set the global pointer to the precomputation table. */
105  secp256k1_ecmult_consts = ret;
106 }
107 
108 static void secp256k1_ecmult_stop(void) {
109  if (secp256k1_ecmult_consts == NULL)
110  return;
111 
112  secp256k1_ecmult_consts_t *c = (secp256k1_ecmult_consts_t*)secp256k1_ecmult_consts;
113  secp256k1_ecmult_consts = NULL;
114  free(c);
115  }
116 
124 static int secp256k1_ecmult_wnaf(int *wnaf, const secp256k1_scalar_t *a, int w) {
125  secp256k1_scalar_t s = *a;
126 
127  int sign = 1;
128  if (secp256k1_scalar_get_bits(&s, 255, 1)) {
129  secp256k1_scalar_negate(&s, &s);
130  sign = -1;
131  }
132 
133  int set_bits = 0;
134  int bit = 0;
135  while (bit < 256) {
136  if (secp256k1_scalar_get_bits(&s, bit, 1) == 0) {
137  bit++;
138  continue;
139  }
140  while (set_bits < bit) {
141  wnaf[set_bits++] = 0;
142  }
143  int now = w;
144  if (bit + now > 256) {
145  now = 256 - bit;
146  }
147  int word = secp256k1_scalar_get_bits_var(&s, bit, now);
148  if (word & (1 << (w-1))) {
149  secp256k1_scalar_add_bit(&s, bit + w);
150  wnaf[set_bits++] = sign * (word - (1 << w));
151  } else {
152  wnaf[set_bits++] = sign * word;
153  }
154  bit += now;
155  }
156  return set_bits;
157 }
158 
159 static void secp256k1_ecmult(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_scalar_t *na, const secp256k1_scalar_t *ng) {
160  const secp256k1_ecmult_consts_t *c = secp256k1_ecmult_consts;
161 
162 #ifdef USE_ENDOMORPHISM
163  secp256k1_scalar_t na_1, na_lam;
164  /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
165  secp256k1_scalar_split_lambda_var(&na_1, &na_lam, na);
166 
167  /* build wnaf representation for na_1 and na_lam. */
168  int wnaf_na_1[130]; int bits_na_1 = secp256k1_ecmult_wnaf(wnaf_na_1, &na_1, WINDOW_A);
169  int wnaf_na_lam[130]; int bits_na_lam = secp256k1_ecmult_wnaf(wnaf_na_lam, &na_lam, WINDOW_A);
170  VERIFY_CHECK(bits_na_1 <= 130);
171  VERIFY_CHECK(bits_na_lam <= 130);
172  int bits = bits_na_1;
173  if (bits_na_lam > bits) bits = bits_na_lam;
174 #else
175  /* build wnaf representation for na. */
176  int wnaf_na[256]; int bits_na = secp256k1_ecmult_wnaf(wnaf_na, na, WINDOW_A);
177  int bits = bits_na;
178 #endif
179 
180  /* calculate odd multiples of a */
182  secp256k1_ecmult_table_precomp_gej_var(pre_a, a, WINDOW_A);
183 
184 #ifdef USE_ENDOMORPHISM
186  for (int i=0; i<ECMULT_TABLE_SIZE(WINDOW_A); i++)
187  secp256k1_gej_mul_lambda(&pre_a_lam[i], &pre_a[i]);
188 
189  /* Splitted G factors. */
190  secp256k1_scalar_t ng_1, ng_128;
191 
192  /* 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) */
193  secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
194 
195  /* Build wnaf representation for ng_1 and ng_128 */
196  int wnaf_ng_1[129]; int bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, &ng_1, WINDOW_G);
197  int wnaf_ng_128[129]; int bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, &ng_128, WINDOW_G);
198  if (bits_ng_1 > bits) bits = bits_ng_1;
199  if (bits_ng_128 > bits) bits = bits_ng_128;
200 #else
201  int wnaf_ng[257]; int bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, ng, WINDOW_G);
202  if (bits_ng > bits) bits = bits_ng;
203 #endif
204 
205  secp256k1_gej_set_infinity(r);
206  secp256k1_gej_t tmpj;
207  secp256k1_ge_t tmpa;
208 
209  for (int i=bits-1; i>=0; i--) {
210  secp256k1_gej_double_var(r, r);
211  int n;
212 #ifdef USE_ENDOMORPHISM
213  if (i < bits_na_1 && (n = wnaf_na_1[i])) {
214  ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
215  secp256k1_gej_add_var(r, r, &tmpj);
216  }
217  if (i < bits_na_lam && (n = wnaf_na_lam[i])) {
218  ECMULT_TABLE_GET_GEJ(&tmpj, pre_a_lam, n, WINDOW_A);
219  secp256k1_gej_add_var(r, r, &tmpj);
220  }
221  if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
222  ECMULT_TABLE_GET_GE(&tmpa, c->pre_g, n, WINDOW_G);
223  secp256k1_gej_add_ge_var(r, r, &tmpa);
224  }
225  if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
226  ECMULT_TABLE_GET_GE(&tmpa, c->pre_g_128, n, WINDOW_G);
227  secp256k1_gej_add_ge_var(r, r, &tmpa);
228  }
229 #else
230  if (i < bits_na && (n = wnaf_na[i])) {
231  ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
232  secp256k1_gej_add_var(r, r, &tmpj);
233  }
234  if (i < bits_ng && (n = wnaf_ng[i])) {
235  ECMULT_TABLE_GET_GE(&tmpa, c->pre_g, n, WINDOW_G);
236  secp256k1_gej_add_ge_var(r, r, &tmpa);
237  }
238 #endif
239  }
240 }
241 
242 #endif
VERIFY_CHECK
#define VERIFY_CHECK(cond)
Definition: util.h:61
WINDOW_A
#define WINDOW_A
Definition: ecmult_impl.h:15
secp256k1_gej_t
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:21
secp256k1_ecmult_consts_t::pre_g
secp256k1_ge_t pre_g[ECMULT_TABLE_SIZE(WINDOW_G)]
Definition: ecmult_impl.h:75
ecmult.h
secp256k1_ecmult_consts_t
Definition: ecmult_impl.h:73
WINDOW_G
#define WINDOW_G
larger numbers may result in slightly better performance, at the cost of exponentially larger precomp...
Definition: ecmult_impl.h:22
scalar.h
r
void const uint64_t uint64_t * r
Definition: field_5x52_asm_impl.h:10
group.h
ECMULT_TABLE_GET_GE
#define ECMULT_TABLE_GET_GE(r, pre, n, w)
Definition: ecmult_impl.h:71
ECMULT_TABLE_SIZE
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult_impl.h:56
secp256k1_scalar_t
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
secp256k1_ge_t
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:14
ECMULT_TABLE_GET_GEJ
#define ECMULT_TABLE_GET_GEJ(r, pre, n, w)
Definition: ecmult_impl.h:70