Skip to content

Commit 3e10eb4

Browse files
peterdettmansipa
authored andcommitted
Make use of negation optional via COMB_NEGATION
1 parent 704dacb commit 3e10eb4

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/ecmult_gen.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@
3737
# endif
3838

3939
# define COMB_SPACING 1
40+
# define COMB_NEGATION 1
4041

4142
#else
4243

4344
/* COMB_BLOCKS, COMB_TEETH, COMB_SPACING must all be positive and the product of the three (COMB_BITS)
44-
* must evaluate to a value in the range [256, 288]. The resulting memory usage for precomputation
45-
* will be COMB_POINTS_TOTAL * sizeof(secp256k1_ge_storage). */
45+
* must evaluate to a value in the range [256, 288]. The COMB_NEGATION boolean controls whether the
46+
* comb will use negations so that only negative multiples need be precomputed. The resulting memory
47+
* usage for precomputation will be COMB_POINTS_TOTAL * sizeof(secp256k1_ge_storage).
48+
*/
4649
#define COMB_BLOCKS 4
4750
#define COMB_TEETH 5
4851
#define COMB_SPACING 13
52+
#define COMB_NEGATION 1
4953

5054
#endif
5155

@@ -58,12 +62,15 @@
5862
#if !(1 <= COMB_SPACING && COMB_SPACING <= 256)
5963
# error "COMB_SPACING must be in the range [1, 256]"
6064
#endif
65+
#if !(0 <= COMB_NEGATION && COMB_NEGATION <= 1)
66+
# error "COMB_NEGATION must be in the range [0, 1]"
67+
#endif
6168

6269
/* The remaining COMB_* parameters are derived values, don't modify these. */
6370
#define COMB_BITS (COMB_BLOCKS * COMB_TEETH * COMB_SPACING)
6471
#define COMB_GROUPED ((COMB_SPACING == 1) && ((32 % COMB_TEETH) == 0))
6572
#define COMB_OFFSET (COMB_BITS == 256)
66-
#define COMB_POINTS (1 << (COMB_TEETH - 1))
73+
#define COMB_POINTS (1 << (COMB_TEETH - COMB_NEGATION))
6774
#define COMB_POINTS_TOTAL (COMB_BLOCKS * COMB_POINTS)
6875
#define COMB_MASK (COMB_POINTS - 1)
6976

src/ecmult_gen_impl.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx
7171
}
7272
}
7373
secp256k1_gej_neg(&vs[vs_pos++], &sum);
74-
for (tooth = 0; tooth < (COMB_TEETH - 1); ++tooth) {
74+
for (tooth = 0; tooth < (COMB_TEETH - COMB_NEGATION); ++tooth) {
7575
stride = 1 << tooth;
7676
for (index = 0; index < stride; ++index, ++vs_pos) {
7777
secp256k1_gej_add_var(&vs[vs_pos], &vs[vs_pos - stride], &ds[tooth], NULL);
@@ -207,12 +207,15 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
207207

208208
#if USE_COMB
209209

210-
int abs, bit_pos, block, comb_off, index, sign;
210+
#if COMB_NEGATION
211+
secp256k1_fe neg;
212+
int sign;
213+
#endif
214+
int abs, bit_pos, block, comb_off, index;
211215
#if !COMB_GROUPED
212216
int bit, tooth;
213217
#endif
214218
uint32_t recoded[9];
215-
secp256k1_fe neg;
216219

217220
memset(&adds, 0, sizeof(adds));
218221
*r = ctx->initial;
@@ -238,19 +241,25 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
238241
}
239242
#endif
240243

244+
#if COMB_NEGATION
241245
sign = (bits >> (COMB_TEETH - 1)) & 1;
242-
abs = (bits ^ -sign) & COMB_MASK;
243-
244246
VERIFY_CHECK(sign == 0 || sign == 1);
247+
248+
bits ^= -sign;
249+
#endif
250+
251+
abs = bits & COMB_MASK;
245252
VERIFY_CHECK(0 <= abs && abs < COMB_POINTS);
246253

247254
for (index = 0; index < COMB_POINTS; ++index) {
248255
secp256k1_ge_storage_cmov(&adds, &(*ctx->prec)[block][index], index == abs);
249256
}
250257

251258
secp256k1_ge_from_storage(&add, &adds);
259+
#if COMB_NEGATION
252260
secp256k1_fe_negate(&neg, &add.y, 1);
253261
secp256k1_fe_cmov(&add.y, &neg, sign);
262+
#endif
254263

255264
secp256k1_gej_add_ge(r, r, &add);
256265
}
@@ -262,10 +271,12 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
262271
secp256k1_gej_double(r, r);
263272
}
264273

274+
#if COMB_NEGATION
265275
secp256k1_fe_clear(&neg);
276+
sign = 0;
277+
#endif
266278
memset(recoded, 0, sizeof(recoded));
267279
abs = 0;
268-
sign = 0;
269280

270281
#else
271282
int i, j;

src/gen_context.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ int main(int argc, char **argv) {
6666
fprintf(fp, " #error configuration mismatch, invalid USE_COMB. Try deleting ecmult_static_context.h before the build.\n");
6767
fprintf(fp, "#endif\n");
6868
#if USE_COMB
69-
fprintf(fp, "#if COMB_BLOCKS != %i || COMB_TEETH != %i || COMB_SPACING != %i\n", COMB_BLOCKS, COMB_TEETH, COMB_SPACING);
70-
fprintf(fp, " #error configuration mismatch, invalid COMB_BLOCKS, COMB_TEETH, or COMB_SPACING. Try deleting ecmult_static_context.h before the build.\n");
69+
fprintf(fp, "#if COMB_BLOCKS != %i || COMB_TEETH != %i || COMB_SPACING != %i || COMB_NEGATION != %i\n", COMB_BLOCKS, COMB_TEETH, COMB_SPACING, COMB_NEGATION);
70+
fprintf(fp, " #error configuration mismatch, invalid COMB_BLOCKS, COMB_TEETH, COMB_SPACING, or COMB_NEGATION. Try deleting ecmult_static_context.h before the build.\n");
7171
fprintf(fp, "#endif\n");
7272
#else
7373
fprintf(fp, "#if ECMULT_GEN_PREC_N != %d || ECMULT_GEN_PREC_G != %d\n", ECMULT_GEN_PREC_N, ECMULT_GEN_PREC_G);

0 commit comments

Comments
 (0)