Skip to content

Commit 8c5dfdf

Browse files
committed
Support splitting exhaustive tests across cores
1 parent e4fde3b commit 8c5dfdf

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-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
@@ -110,7 +110,7 @@ static void secp256k1_rand256_test(unsigned char *b32) {
110110

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

src/tests_exhaustive.c

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

67+
static uint32_t num_cores = 1;
68+
static uint32_t this_core = 0;
69+
70+
SECP256K1_INLINE static int skip_section(uint64_t iter) {
71+
if (num_cores == 1) return 0;
72+
iter *= 0xe7037ed1a0b428dbULL;
73+
return ((((uint32_t)iter ^ (iter >> 32)) * num_cores) >> 32) != this_core;
74+
}
75+
6776
int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
6877
const unsigned char *key32, const unsigned char *algo16,
6978
void *data, unsigned int attempt) {
@@ -109,6 +118,7 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
109118
/* Check all addition formulae */
110119
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
111120
secp256k1_fe fe_inv;
121+
if (skip_section(j)) continue;
112122
secp256k1_fe_inv(&fe_inv, &groupj[j].z);
113123
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
114124
secp256k1_ge zless_gej;
@@ -155,8 +165,10 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
155165

156166
void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj) {
157167
int i, j, r_log;
168+
uint64_t iter = 0;
158169
for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
159170
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
171+
if (skip_section(iter++)) continue;
160172
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
161173
secp256k1_gej tmp;
162174
secp256k1_scalar na, ng;
@@ -189,11 +201,13 @@ static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t
189201

190202
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group) {
191203
int i, j, k, x, y;
204+
uint64_t iter = 0;
192205
secp256k1_scratch *scratch = secp256k1_scratch_create(&ctx->error_callback, 4096);
193206
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
194207
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
195208
for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
196209
for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
210+
if (skip_section(iter++)) continue;
197211
for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
198212
secp256k1_gej tmp;
199213
secp256k1_scalar g_sc;
@@ -227,6 +241,7 @@ void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overfl
227241

228242
void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
229243
int s, r, msg, key;
244+
uint64_t iter = 0;
230245
for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
231246
for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
232247
for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
@@ -239,6 +254,8 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
239254
int k, should_verify;
240255
unsigned char msg32[32];
241256

257+
if (skip_section(iter++)) continue;
258+
242259
secp256k1_scalar_set_int(&s_s, s);
243260
secp256k1_scalar_set_int(&r_s, r);
244261
secp256k1_scalar_set_int(&msg_s, msg);
@@ -277,10 +294,12 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
277294

278295
void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
279296
int i, j, k;
297+
uint64_t iter = 0;
280298

281299
/* Loop */
282300
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
283301
for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
302+
if (skip_section(iter++)) continue;
284303
for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
285304
const int starting_k = k;
286305
secp256k1_ecdsa_signature sig;
@@ -341,6 +360,17 @@ int main(int argc, char** argv) {
341360
/* find random seed */
342361
secp256k1_rand_init(argc > 2 ? argv[2] : NULL);
343362

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

0 commit comments

Comments
 (0)