Skip to content

Commit 39f67dd

Browse files
committed
Support splitting exhaustive tests across cores
1 parent e99b26f commit 39f67dd

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/modules/recovery/tests_exhaustive_impl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
void test_exhaustive_recovery_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
1414
int i, j, k;
15+
uint64_t iter = 0;
1516

1617
/* Loop */
1718
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
1819
for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
20+
if (skip_section(&iter)) continue;
1921
for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
2022
const int starting_k = k;
2123
secp256k1_fe r_dot_y_normalized;
@@ -80,6 +82,7 @@ void test_exhaustive_recovery_sign(const secp256k1_context *ctx, const secp256k1
8082
void test_exhaustive_recovery_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
8183
/* This is essentially a copy of test_exhaustive_verify, with recovery added */
8284
int s, r, msg, key;
85+
uint64_t iter = 0;
8386
for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
8487
for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
8588
for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
@@ -94,6 +97,8 @@ void test_exhaustive_recovery_verify(const secp256k1_context *ctx, const secp256
9497
int k, should_verify;
9598
unsigned char msg32[32];
9699

100+
if (skip_section(&iter)) continue;
101+
97102
secp256k1_scalar_set_int(&s_s, s);
98103
secp256k1_scalar_set_int(&r_s, r);
99104
secp256k1_scalar_set_int(&msg_s, msg);

src/testrand_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void secp256k1_rand_flip(unsigned char *b, size_t len) {
114114

115115
static void secp256k1_rand_init(const char* hexseed) {
116116
unsigned char seed16[16] = {0};
117-
if (hexseed) {
117+
if (hexseed && strlen(hexseed) != 0) {
118118
int pos = 0;
119119
while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
120120
unsigned short sh;

src/tests_exhaustive.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ void random_fe(secp256k1_fe *x) {
6666
}
6767
/** END stolen from tests.c */
6868

69+
static uint32_t num_cores = 1;
70+
static uint32_t this_core = 0;
71+
72+
SECP256K1_INLINE static int skip_section(uint64_t* iter) {
73+
if (num_cores == 1) return 0;
74+
*iter += 0xe7037ed1a0b428dbULL;
75+
return ((((uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
76+
}
77+
6978
int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
7079
const unsigned char *key32, const unsigned char *algo16,
7180
void *data, unsigned int attempt) {
@@ -99,6 +108,7 @@ void test_exhaustive_endomorphism(const secp256k1_ge *group) {
99108

100109
void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj) {
101110
int i, j;
111+
uint64_t iter = 0;
102112

103113
/* Sanity-check (and check infinity functions) */
104114
CHECK(secp256k1_ge_is_infinity(&group[0]));
@@ -111,6 +121,7 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
111121
/* Check all addition formulae */
112122
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
113123
secp256k1_fe fe_inv;
124+
if (skip_section(&iter)) continue;
114125
secp256k1_fe_inv(&fe_inv, &groupj[j].z);
115126
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
116127
secp256k1_ge zless_gej;
@@ -157,8 +168,10 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
157168

158169
void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj) {
159170
int i, j, r_log;
171+
uint64_t iter = 0;
160172
for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
161173
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
174+
if (skip_section(&iter)) continue;
162175
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
163176
secp256k1_gej tmp;
164177
secp256k1_scalar na, ng;
@@ -191,11 +204,13 @@ static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t
191204

192205
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group) {
193206
int i, j, k, x, y;
207+
uint64_t iter = 0;
194208
secp256k1_scratch *scratch = secp256k1_scratch_create(&ctx->error_callback, 4096);
195209
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
196210
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
197211
for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
198212
for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
213+
if (skip_section(&iter)) continue;
199214
for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
200215
secp256k1_gej tmp;
201216
secp256k1_scalar g_sc;
@@ -229,6 +244,7 @@ void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overfl
229244

230245
void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
231246
int s, r, msg, key;
247+
uint64_t iter = 0;
232248
for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
233249
for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
234250
for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
@@ -241,6 +257,8 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
241257
int k, should_verify;
242258
unsigned char msg32[32];
243259

260+
if (skip_section(&iter)) continue;
261+
244262
secp256k1_scalar_set_int(&s_s, s);
245263
secp256k1_scalar_set_int(&r_s, r);
246264
secp256k1_scalar_set_int(&msg_s, msg);
@@ -279,10 +297,12 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
279297

280298
void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
281299
int i, j, k;
300+
uint64_t iter = 0;
282301

283302
/* Loop */
284303
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
285304
for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
305+
if (skip_section(&iter)) continue;
286306
for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
287307
const int starting_k = k;
288308
secp256k1_ecdsa_signature sig;
@@ -344,6 +364,17 @@ int main(int argc, char** argv) {
344364
/* find random seed */
345365
secp256k1_rand_init(argc > 2 ? argv[2] : NULL);
346366

367+
/* set up split processing */
368+
if (argc > 4) {
369+
num_cores = strtol(argv[3], NULL, 0);
370+
this_core = strtol(argv[4], NULL, 0);
371+
if (num_cores < 1 || this_core >= num_cores) {
372+
fprintf(stderr, "Usage: %s [count] [seed] [numcores] [thiscore]\n", argv[0]);
373+
return 1;
374+
}
375+
printf("running tests for core %lu (out of [0..%lu])\n", (unsigned long)this_core, (unsigned long)num_cores - 1);
376+
}
377+
347378
while (count--) {
348379
/* Build context */
349380
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

0 commit comments

Comments
 (0)