Skip to content

Commit 692397e

Browse files
committed
Permit COMB_BITS < 256 for exhaustive tests
1 parent c722efd commit 692397e

7 files changed

+62
-44
lines changed

src/ecmult_gen.h

+51-34
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
* - COMB_BLOCKS is the number of blocks the input is split into. Each
1717
* has a corresponding table.
1818
* - COMB_TEETH is the number of bits simultaneously covered by one table.
19+
* - COMB_RANGE is the number of bits in supported scalars. For production
20+
* purposes, only 256 is reasonable, but smaller numbers are supported for
21+
* exhaustive test mode.
1922
*
2023
* 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
24+
* is defined as ceil(COMB_RANGE / (COMB_BLOCKS * COMB_TEETH)). Each block covers
2225
* COMB_SPACING * COMB_TEETH consecutive bits in the input.
2326
*
2427
* The size of the precomputed table is COMB_BLOCKS * (1 << (COMB_TEETH - 1))
@@ -36,55 +39,67 @@
3639
* doesn't support infinities) */
3740
# undef COMB_BLOCKS
3841
# 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
42+
# if EXHAUSTIVE_TEST_ORDER == 7
43+
# define COMB_RANGE 3
44+
# define COMB_BLOCKS 1
45+
# define COMB_TEETH 2
46+
# elif EXHAUSTIVE_TEST_ORDER == 13
47+
# define COMB_RANGE 4
48+
# define COMB_BLOCKS 1
5049
# define COMB_TEETH 2
50+
# elif EXHAUSTIVE_TEST_ORDER == 199
51+
# define COMB_RANGE 8
52+
# define COMB_BLOCKS 2
53+
# define COMB_TEETH 3
5154
# else
52-
# define COMB_BLOCKS 256
53-
# define COMB_TEETH 1
55+
# error "Unknown exhaustive test order"
56+
# endif
57+
# if (COMB_RANGE >= 32) || ((EXHAUSTIVE_TEST_ORDER >> (COMB_RANGE - 1)) != 1)
58+
# error "COMB_RANGE != ceil(log2(EXHAUSTIVE_TEST_ORDER+1))"
5459
# endif
5560
#else /* !defined(EXHAUSTIVE_TEST_ORDER) */
61+
# define COMB_RANGE 256
62+
#endif /* defined(EXHAUSTIVE_TEST_ORDER) */
63+
5664
/* 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
65+
#ifndef COMB_BLOCKS
66+
# define COMB_BLOCKS 11
67+
# ifdef DEBUG_CONFIG
68+
# pragma message DEBUG_CONFIG_MSG("COMB_BLOCKS undefined, assuming default value")
6269
# 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
70+
#endif
71+
#ifndef COMB_TEETH
72+
# define COMB_TEETH 6
73+
# ifdef DEBUG_CONFIG
74+
# pragma message DEBUG_CONFIG_MSG("COMB_TEETH undefined, assuming default value")
6875
# endif
69-
#endif /* defined(EXHAUSTIVE_TEST_ORDER) */
76+
#endif
77+
/* Use ceil(COMB_RANGE / (COMB_BLOCKS * COMB_TEETH)) as COMB_SPACING. */
78+
#define COMB_SPACING CEIL_DIV(COMB_RANGE, COMB_BLOCKS * COMB_TEETH)
7079

7180
/* Range checks on the parameters. */
81+
82+
/* The remaining COMB_* parameters are derived values, don't modify these. */
83+
/* - The number of bits covered by all the blocks; must be at least COMB_RANGE. */
84+
#define COMB_BITS (COMB_BLOCKS * COMB_TEETH * COMB_SPACING)
85+
/* - The number of entries per table. */
86+
#define COMB_POINTS (1 << (COMB_TEETH - 1))
87+
88+
/* Sanity checks. */
7289
#if !(1 <= COMB_BLOCKS && COMB_BLOCKS <= 256)
7390
# error "COMB_BLOCKS must be in the range [1, 256]"
7491
#endif
7592
#if !(1 <= COMB_TEETH && COMB_TEETH <= 8)
7693
# error "COMB_TEETH must be in the range [1, 8]"
7794
#endif
95+
#if COMB_BITS < COMB_RANGE
96+
# error "COMB_BLOCKS * COMB_TEETH * COMB_SPACING is too low"
97+
#endif
7898

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 CEIL_DIV(256, 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. */
99+
/* These last 2 checks are not strictly required, but prevent gratuitously inefficient
100+
* configurations. Note that they compare with 256 rather than COMB_RANGE, so they do
101+
* permit somewhat excessive values for the exhaustive test case, where testing with
102+
* suboptimal parameters may be desirable. */
88103
#if (COMB_BLOCKS - 1) * COMB_TEETH * COMB_SPACING >= 256
89104
# error "COMB_BLOCKS can be reduced"
90105
#endif
@@ -93,8 +108,10 @@
93108
#endif
94109

95110
#ifdef DEBUG_CONFIG
111+
# pragma message DEBUG_CONFIG_DEF(COMB_RANGE)
96112
# pragma message DEBUG_CONFIG_DEF(COMB_BLOCKS)
97113
# pragma message DEBUG_CONFIG_DEF(COMB_TEETH)
114+
# pragma message DEBUG_CONFIG_DEF(COMB_SPACING)
98115
#endif
99116

100117
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));
@@ -95,6 +94,7 @@ static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, cons
9594
for (block = 0; block < blocks; ++block) {
9695
size_t index;
9796
for (index = 0; index < points; ++index) {
97+
VERIFY_CHECK(!secp256k1_ge_is_infinity(&prec[block * points + index]));
9898
secp256k1_ge_to_storage(&table[block * points + index], &prec[block * points + index]);
9999
}
100100
}

src/ecmult_gen_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
109109
/* Compute the scalar d = (gn + ctx->scalar_offset). */
110110
secp256k1_scalar_add(&d, &ctx->scalar_offset, gn);
111111
/* Convert to recoded array. */
112-
for (i = 0; i < 8; ++i) {
112+
for (i = 0; i < 8 && i < ((COMB_BITS + 31) >> 5); ++i) {
113113
recoded[i] = secp256k1_scalar_get_bits_limb32(&d, 32 * i, 32);
114114
}
115115
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 = CEIL_DIV(256, 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, "#elif (COMB_BLOCKS == %d) && (COMB_TEETH == %d)\n", blocks, teeth);
35+
fprintf(fp, "#elif (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)