Skip to content

Commit d789ab2

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

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ 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+
return ((((iter * 0xe7037ed1a0b428dbULL) >> 32) * num_cores) >> 32) != this_core;
73+
}
74+
6775
int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
6876
const unsigned char *key32, const unsigned char *algo16,
6977
void *data, unsigned int attempt) {
@@ -109,6 +117,7 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
109117
/* Check all addition formulae */
110118
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
111119
secp256k1_fe fe_inv;
120+
if (skip_section(j)) continue;
112121
secp256k1_fe_inv(&fe_inv, &groupj[j].z);
113122
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
114123
secp256k1_ge zless_gej;
@@ -155,8 +164,10 @@ void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *gr
155164

156165
void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj) {
157166
int i, j, r_log;
167+
uint64_t iter = 0;
158168
for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
159169
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
170+
if (skip_section(iter++)) continue;
160171
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
161172
secp256k1_gej tmp;
162173
secp256k1_scalar na, ng;
@@ -189,11 +200,13 @@ static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t
189200

190201
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group) {
191202
int i, j, k, x, y;
203+
uint64_t iter = 0;
192204
secp256k1_scratch *scratch = secp256k1_scratch_create(&ctx->error_callback, 4096);
193205
for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
194206
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
195207
for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
196208
for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
209+
if (skip_section(iter++)) continue;
197210
for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
198211
secp256k1_gej tmp;
199212
secp256k1_scalar g_sc;
@@ -227,6 +240,7 @@ void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overfl
227240

228241
void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
229242
int s, r, msg, key;
243+
uint64_t iter = 0;
230244
for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
231245
for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
232246
for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
@@ -239,6 +253,8 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
239253
int k, should_verify;
240254
unsigned char msg32[32];
241255

256+
if (skip_section(iter++)) continue;
257+
242258
secp256k1_scalar_set_int(&s_s, s);
243259
secp256k1_scalar_set_int(&r_s, r);
244260
secp256k1_scalar_set_int(&msg_s, msg);
@@ -277,10 +293,12 @@ void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *gr
277293

278294
void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
279295
int i, j, k;
296+
uint64_t iter = 0;
280297

281298
/* Loop */
282299
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
283300
for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
301+
if (skip_section(iter++)) continue;
284302
for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
285303
const int starting_k = k;
286304
secp256k1_ecdsa_signature sig;
@@ -341,6 +359,17 @@ int main(int argc, char** argv) {
341359
/* find random seed */
342360
secp256k1_rand_init(argc > 2 ? argv[2] : NULL);
343361

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

0 commit comments

Comments
 (0)