Skip to content

Commit acc92ab

Browse files
committed
Permit COMB_BITS < 256 for exhaustive tests
1 parent a731eae commit acc92ab

7 files changed

+69
-49
lines changed

src/ecmult_gen.h

+58-39
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
/* Configuration parameters for the signed-digit multi-comb algorithm:
1515
*
16-
* - COMB_BLOCKS is the number of blocks the input is split into. Each
17-
* has a corresponding table.
16+
* - COMB_BLOCKS is the number of blocks the input is split into. Each has a corresponding table.
1817
* - COMB_TEETH is the number of bits simultaneously covered by one table.
19-
*
20-
* The comb's spacing (COMB_SPACING), or the distance between the teeth,
21-
* is defined as ceil(256 / (COMB_BLOCKS * COMB_TEETH)). Each block covers
22-
* COMB_SPACING * COMB_TEETH consecutive bits in the input.
18+
* - COMB_SPACING is the distance between the teeth. For production purposes, the only reasonable
19+
* value is ceil(256 / (COMB_BLOCKS * COMB_TEETH)), so unless explicitly configured otherwise,
20+
* that value will be used. COMB_BLOCKS * COMB_TEETH * COMB_SPACING needs to be at least 256.
2321
*
2422
* The size of the precomputed table is COMB_BLOCKS * (1 << (COMB_TEETH - 1))
2523
* secp256k1_ge_storages.
@@ -36,65 +34,86 @@
3634
* doesn't support infinities) */
3735
# undef COMB_BLOCKS
3836
# undef COMB_TEETH
39-
# if EXHAUSTIVE_TEST_ORDER > 32
40-
# define COMB_BLOCKS 52
41-
# define COMB_TEETH 5
42-
# elif EXHAUSTIVE_TEST_ORDER > 16
43-
# define COMB_BLOCKS 64
44-
# define COMB_TEETH 4
45-
# elif EXHAUSTIVE_TEST_ORDER > 8
46-
# define COMB_BLOCKS 86
47-
# define COMB_TEETH 3
48-
# elif EXHAUSTIVE_TEST_ORDER > 4
49-
# define COMB_BLOCKS 128
37+
# undef COMB_SPACING
38+
# if EXHAUSTIVE_TEST_ORDER == 13
39+
# define COMB_RANGE 4
40+
# define COMB_BLOCKS 1
5041
# define COMB_TEETH 2
42+
# define COMB_SPACING 2
43+
# elif EXHAUSTIVE_TEST_ORDER == 199
44+
# define COMB_RANGE 8
45+
# define COMB_BLOCKS 2
46+
# define COMB_TEETH 3
47+
# define COMB_SPACING 2
5148
# else
52-
# define COMB_BLOCKS 256
53-
# define COMB_TEETH 1
49+
# error "Unknown exhaustive test order"
50+
# endif
51+
# if (COMB_RANGE >= 32) || ((EXHAUSTIVE_TEST_ORDER >> (COMB_RANGE - 1)) != 1)
52+
# error "COMB_RANGE != ceil(log2(EXHAUSTIVE_TEST_ORDER+1))"
5453
# endif
5554
#else /* !defined(EXHAUSTIVE_TEST_ORDER) */
55+
# define COMB_RANGE 256
56+
#endif /* defined(EXHAUSTIVE_TEST_ORDER) */
57+
5658
/* Use (11, 6) as default configuration, which results in a 22 kB table. */
57-
# ifndef COMB_BLOCKS
58-
# define COMB_BLOCKS 11
59-
# ifdef DEBUG_CONFIG
60-
# pragma message DEBUG_CONFIG_MSG("COMB_BLOCKS undefined, assuming default value")
61-
# endif
59+
#ifndef COMB_BLOCKS
60+
# define COMB_BLOCKS 11
61+
# ifdef DEBUG_CONFIG
62+
# pragma message DEBUG_CONFIG_MSG("COMB_BLOCKS undefined, assuming default value")
6263
# endif
63-
# ifndef COMB_TEETH
64-
# define COMB_TEETH 6
65-
# ifdef DEBUG_CONFIG
66-
# pragma message DEBUG_CONFIG_MSG("COMB_TEETH undefined, assuming default value")
67-
# endif
64+
#endif
65+
#ifndef COMB_TEETH
66+
# define COMB_TEETH 6
67+
# ifdef DEBUG_CONFIG
68+
# pragma message DEBUG_CONFIG_MSG("COMB_TEETH undefined, assuming default value")
6869
# endif
69-
#endif /* defined(EXHAUSTIVE_TEST_ORDER) */
70+
#endif
71+
/* Use ceil(COMB_RANGE / (COMB_BLOCKS * COMB_TEETH)) as default COMB_SPACING. */
72+
#ifndef COMB_SPACING
73+
# define COMB_SPACING ((COMB_RANGE + COMB_BLOCKS * COMB_TEETH) / (COMB_BLOCKS * COMB_TEETH))
74+
# ifdef DEBUG_CONFIG
75+
# pragma message DEBUG_CONFIG_MSG("COMB_SPACING undefined, assuming default value")
76+
# endif
77+
#endif
7078

7179
/* Range checks on the parameters. */
80+
81+
/* The remaining COMB_* parameters are derived values, don't modify these. */
82+
/* - The number of bits covered by all the blocks; must be at least COMB_RANGE. */
83+
#define COMB_BITS (COMB_BLOCKS * COMB_TEETH * COMB_SPACING)
84+
/* - The number of entries per table. */
85+
#define COMB_POINTS (1 << (COMB_TEETH - 1))
86+
87+
/* Sanity checks. */
7288
#if !(1 <= COMB_BLOCKS && COMB_BLOCKS <= 256)
7389
# error "COMB_BLOCKS must be in the range [1, 256]"
7490
#endif
7591
#if !(1 <= COMB_TEETH && COMB_TEETH <= 8)
7692
# error "COMB_TEETH must be in the range [1, 8]"
7793
#endif
94+
#if COMB_BITS < COMB_RANGE
95+
# error "COMB_BLOCKS * COMB_TEETH * COMB_SPACING is too low"
96+
#endif
7897

79-
/* The remaining COMB_* parameters are derived values, don't modify these. */
80-
/* - The distance between the teeth of each comb. */
81-
#define COMB_SPACING ((255 + COMB_BLOCKS * COMB_TEETH) / (COMB_BLOCKS * COMB_TEETH))
82-
/* - The number of bits covered by all the blocks; must be at least 256. */
83-
#define COMB_BITS (COMB_BLOCKS * COMB_TEETH * COMB_SPACING)
84-
/* - The number of entries per table. */
85-
#define COMB_POINTS (1 << (COMB_TEETH - 1))
86-
87-
/* Additional sanity checks. */
98+
/* These last 3 checks are not strictly required, but prevent gratuitously inefficient
99+
* configurations. Note that they compare with 256 rather than COMB_RANGE, so they do
100+
* permit somewhat excessive values for the exhaustive test case, where testing with
101+
* suboptimal parameters may be desirable. */
88102
#if (COMB_BLOCKS - 1) * COMB_TEETH * COMB_SPACING >= 256
89103
# error "COMB_BLOCKS can be reduced"
90104
#endif
91105
#if COMB_BLOCKS * (COMB_TEETH - 1) * COMB_SPACING >= 256
92106
# error "COMB_TEETH can be reduced"
93107
#endif
108+
#if COMB_BLOCKS * COMB_TEETH * (COMB_SPACING - 1) >= 256
109+
# error "COMB_SPACING can be reduced"
110+
#endif
94111

95112
#ifdef DEBUG_CONFIG
113+
# pragma message DEBUG_CONFIG_DEF(COMB_RANGE)
96114
# pragma message DEBUG_CONFIG_DEF(COMB_BLOCKS)
97115
# pragma message DEBUG_CONFIG_DEF(COMB_TEETH)
116+
# pragma message DEBUG_CONFIG_DEF(COMB_SPACING)
98117
#endif
99118

100119
typedef struct {

src/ecmult_gen_compute_table.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#include "ecmult_gen.h"
1111

12-
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int blocks, int teeth);
12+
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int blocks, int teeth, int spacing);
1313

1414
#endif /* SECP256K1_ECMULT_GEN_COMPUTE_TABLE_H */

src/ecmult_gen_compute_table_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
#include "ecmult_gen.h"
1515
#include "util.h"
1616

17-
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int blocks, int teeth) {
17+
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int blocks, int teeth, int spacing) {
1818
size_t points = ((size_t)1) << (teeth - 1);
1919
size_t points_total = points * blocks;
20-
int spacing = (256 + blocks * teeth - 1) / (blocks * teeth);
2120
secp256k1_ge* prec = checked_malloc(&default_error_callback, points_total * sizeof(*prec));
2221
secp256k1_gej* ds = checked_malloc(&default_error_callback, teeth * sizeof(*ds));
2322
secp256k1_gej* vs = checked_malloc(&default_error_callback, points_total * sizeof(*vs));
@@ -93,6 +92,7 @@ static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, cons
9392
for (block = 0; block < blocks; ++block) {
9493
size_t index;
9594
for (index = 0; index < points; ++index) {
95+
VERIFY_CHECK(!secp256k1_ge_is_infinity(&prec[block * points + index]));
9696
secp256k1_ge_to_storage(&table[block * points + index], &prec[block * points + index]);
9797
}
9898
}

src/ecmult_gen_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
108108
/* Compute the scalar d = (gn + ctx->scalar_offset). */
109109
secp256k1_scalar_add(&d, &ctx->scalar_offset, gn);
110110
/* Convert to recoded array. */
111-
for (i = 0; i < 8; ++i) {
111+
for (i = 0; i < 8 && i < ((COMB_BITS + 31) >> 5); ++i) {
112112
recoded[i] = secp256k1_scalar_get_bits_limb32(&d, 32 * i, 32);
113113
}
114114
secp256k1_scalar_clear(&d);

src/precompute_ecmult_gen.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ static const int CONFIGS[][2] = {
2424
};
2525

2626
static void print_table(FILE* fp, int blocks, int teeth) {
27+
int spacing = (255 + blocks * teeth) / (blocks * teeth);
2728
size_t points = ((size_t)1) << (teeth - 1);
2829
int outer;
2930
size_t inner;
3031

3132
secp256k1_ge_storage* table = checked_malloc(&default_error_callback, blocks * points * sizeof(secp256k1_ge_storage));
32-
secp256k1_ecmult_gen_compute_table(table, &secp256k1_ge_const_g, blocks, teeth);
33+
secp256k1_ecmult_gen_compute_table(table, &secp256k1_ge_const_g, blocks, teeth, spacing);
3334

34-
fprintf(fp, "#if (COMB_BLOCKS == %d) && (COMB_TEETH == %d)\n", blocks, teeth);
35+
fprintf(fp, "#if (COMB_BLOCKS == %d) && (COMB_TEETH == %d) && (COMB_SPACING == %d)\n", blocks, teeth, spacing);
3536
for (outer = 0; outer != blocks; outer++) {
3637
fprintf(fp,"{");
3738
for (inner = 0; inner != points; inner++) {

src/precomputed_ecmult_gen.c

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests_exhaustive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int main(int argc, char** argv) {
389389
}
390390

391391
/* Recreate the ecmult{,_gen} tables using the right generator (as selected via EXHAUSTIVE_TEST_ORDER) */
392-
secp256k1_ecmult_gen_compute_table(&secp256k1_ecmult_gen_prec_table[0][0], &secp256k1_ge_const_g, COMB_BLOCKS, COMB_TEETH);
392+
secp256k1_ecmult_gen_compute_table(&secp256k1_ecmult_gen_prec_table[0][0], &secp256k1_ge_const_g, COMB_BLOCKS, COMB_TEETH, COMB_SPACING);
393393
secp256k1_ecmult_compute_two_tables(secp256k1_pre_g, secp256k1_pre_g_128, WINDOW_G, &secp256k1_ge_const_g);
394394

395395
while (count--) {

0 commit comments

Comments
 (0)