Skip to content

Commit d822b29

Browse files
committed
test: split monolithic ellswift test into independent cases
No behavior changes. Refactors the previously monolithic ElligatorSwift test into isolated, independent test cases. Doing so allows the test suite to execute these cases in parallel rather than sequentially. Overall, seen 35-40% tests time reduction locally. This is quite useful for the Debug build with no optimizations, which is noticeably slow. #### Local Debug-build Results (7 jobs): - master: 138.0 seconds. - this PR: 89.3 seconds. (~1.55× speedup, ~35% reduction) #### Local Release-build Results (7 jobs): - master: 9.5 seconds. - this PR: 5.9 seconds. (~1.61× speedup, ~38% reduction)
1 parent 5c75183 commit d822b29

1 file changed

Lines changed: 65 additions & 33 deletions

File tree

src/modules/ellswift/tests_impl.h

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ static int ellswift_xdh_hash_x32(unsigned char *output, const unsigned char *x32
177177
return 1;
178178
}
179179

180-
void run_ellswift_tests(void) {
181-
int i = 0;
182-
/* Test vectors. */
180+
/* Run the test vectors for ellswift encoding */
181+
void ellswift_encoding_test_vectors_tests(void) {
182+
int i;
183183
for (i = 0; (unsigned)i < sizeof(ellswift_xswiftec_inv_tests) / sizeof(ellswift_xswiftec_inv_tests[0]); ++i) {
184184
const struct ellswift_xswiftec_inv_test *testcase = &ellswift_xswiftec_inv_tests[i];
185185
int c;
@@ -195,6 +195,11 @@ void run_ellswift_tests(void) {
195195
}
196196
}
197197
}
198+
}
199+
200+
/* Run the test vectors for ellswift decoding */
201+
void ellswift_decoding_test_vectors_tests(void) {
202+
int i;
198203
for (i = 0; (unsigned)i < sizeof(ellswift_decode_tests) / sizeof(ellswift_decode_tests[0]); ++i) {
199204
const struct ellswift_decode_test *testcase = &ellswift_decode_tests[i];
200205
secp256k1_pubkey pubkey;
@@ -207,6 +212,11 @@ void run_ellswift_tests(void) {
207212
CHECK(fe_equal(&testcase->x, &ge.x));
208213
CHECK(secp256k1_fe_is_odd(&ge.y) == testcase->odd_y);
209214
}
215+
}
216+
217+
/* Run the test vectors for ellswift expected xdh BIP324 shared secrets */
218+
void ellswift_xdh_test_vectors_tests(void) {
219+
int i;
210220
for (i = 0; (unsigned)i < sizeof(ellswift_xdh_tests_bip324) / sizeof(ellswift_xdh_tests_bip324[0]); ++i) {
211221
const struct ellswift_xdh_test *test = &ellswift_xdh_tests_bip324[i];
212222
unsigned char shared_secret[32];
@@ -223,7 +233,11 @@ void run_ellswift_tests(void) {
223233
CHECK(ret);
224234
CHECK(secp256k1_memcmp_var(shared_secret, test->shared_secret, 32) == 0);
225235
}
226-
/* Verify that secp256k1_ellswift_encode + decode roundtrips. */
236+
}
237+
238+
/* Verify that secp256k1_ellswift_encode + decode roundtrips */
239+
void ellswift_encode_decode_roundtrip_tests(void) {
240+
int i;
227241
for (i = 0; i < 1000 * COUNT; i++) {
228242
unsigned char rnd32[32];
229243
unsigned char ell64[64];
@@ -240,7 +254,11 @@ void run_ellswift_tests(void) {
240254
/* Compare with original. */
241255
CHECK(secp256k1_ge_eq_var(&g, &g2));
242256
}
243-
/* Verify the behavior of secp256k1_ellswift_create */
257+
}
258+
259+
/* Verify the behavior of secp256k1_ellswift_create */
260+
void ellswift_create_tests(void) {
261+
int i;
244262
for (i = 0; i < 400 * COUNT; i++) {
245263
unsigned char auxrnd32[32], sec32[32];
246264
secp256k1_scalar sec;
@@ -262,7 +280,11 @@ void run_ellswift_tests(void) {
262280
secp256k1_ecmult(&res, NULL, &secp256k1_scalar_zero, &sec);
263281
CHECK(secp256k1_gej_eq_ge_var(&res, &dec));
264282
}
265-
/* Verify that secp256k1_ellswift_xdh computes the right shared X coordinate. */
283+
}
284+
285+
/* Verify that secp256k1_ellswift_xdh computes the right shared X coordinate */
286+
void ellswift_compute_shared_secret_tests(void) {
287+
int i;
266288
for (i = 0; i < 800 * COUNT; i++) {
267289
unsigned char ell64[64], sec32[32], share32[32];
268290
secp256k1_scalar sec;
@@ -293,6 +315,10 @@ void run_ellswift_tests(void) {
293315
/* Compare. */
294316
CHECK(fe_equal(&res.x, &share_x));
295317
}
318+
}
319+
320+
void ellswift_xdh_correctness_tests(void) {
321+
int i;
296322
/* Verify the joint behavior of secp256k1_ellswift_xdh */
297323
for (i = 0; i < 200 * COUNT; i++) {
298324
unsigned char auxrnd32a[32], auxrnd32b[32], auxrnd32a_bad[32], auxrnd32b_bad[32];
@@ -403,41 +429,47 @@ void run_ellswift_tests(void) {
403429
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
404430
}
405431
}
432+
}
406433

407-
/* Test hash initializers. */
408-
{
409-
secp256k1_sha256 sha_optimized;
410-
/* "secp256k1_ellswift_encode" */
411-
static const unsigned char encode_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'e', 'n', 'c', 'o', 'd', 'e'};
412-
/* "secp256k1_ellswift_create" */
413-
static const unsigned char create_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'c', 'r', 'e', 'a', 't', 'e'};
414-
/* "bip324_ellswift_xonly_ecdh" */
415-
static const unsigned char bip324_tag[] = {'b', 'i', 'p', '3', '2', '4', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'x', 'o', 'n', 'l', 'y', '_', 'e', 'c', 'd', 'h'};
434+
/* Test hash initializers */
435+
void ellswift_hash_init_tests(void) {
436+
secp256k1_sha256 sha_optimized;
437+
/* "secp256k1_ellswift_encode" */
438+
static const unsigned char encode_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'e', 'n', 'c', 'o', 'd', 'e'};
439+
/* "secp256k1_ellswift_create" */
440+
static const unsigned char create_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'c', 'r', 'e', 'a', 't', 'e'};
441+
/* "bip324_ellswift_xonly_ecdh" */
442+
static const unsigned char bip324_tag[] = {'b', 'i', 'p', '3', '2', '4', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'x', 'o', 'n', 'l', 'y', '_', 'e', 'c', 'd', 'h'};
416443

417-
/* Check that hash initialized by
418-
* secp256k1_ellswift_sha256_init_encode has the expected
419-
* state. */
420-
secp256k1_ellswift_sha256_init_encode(&sha_optimized);
421-
test_sha256_tag_midstate(&sha_optimized, encode_tag, sizeof(encode_tag));
444+
/* Check that hash initialized by
445+
* secp256k1_ellswift_sha256_init_encode has the expected
446+
* state. */
447+
secp256k1_ellswift_sha256_init_encode(&sha_optimized);
448+
test_sha256_tag_midstate(&sha_optimized, encode_tag, sizeof(encode_tag));
422449

423-
/* Check that hash initialized by
424-
* secp256k1_ellswift_sha256_init_create has the expected
425-
* state. */
426-
secp256k1_ellswift_sha256_init_create(&sha_optimized);
427-
test_sha256_tag_midstate(&sha_optimized, create_tag, sizeof(create_tag));
450+
/* Check that hash initialized by
451+
* secp256k1_ellswift_sha256_init_create has the expected
452+
* state. */
453+
secp256k1_ellswift_sha256_init_create(&sha_optimized);
454+
test_sha256_tag_midstate(&sha_optimized, create_tag, sizeof(create_tag));
428455

429-
/* Check that hash initialized by
430-
* secp256k1_ellswift_sha256_init_bip324 has the expected
431-
* state. */
432-
secp256k1_ellswift_sha256_init_bip324(&sha_optimized);
433-
test_sha256_tag_midstate(&sha_optimized, bip324_tag, sizeof(bip324_tag));
434-
}
456+
/* Check that hash initialized by
457+
* secp256k1_ellswift_sha256_init_bip324 has the expected
458+
* state. */
459+
secp256k1_ellswift_sha256_init_bip324(&sha_optimized);
460+
test_sha256_tag_midstate(&sha_optimized, bip324_tag, sizeof(bip324_tag));
435461
}
436462

437463
/* --- Test registry --- */
438-
/* TODO: subdivide test in cases */
439464
static const struct tf_test_entry tests_ellswift[] = {
440-
CASE(ellswift_tests),
465+
CASE1(ellswift_encoding_test_vectors_tests),
466+
CASE1(ellswift_decoding_test_vectors_tests),
467+
CASE1(ellswift_xdh_test_vectors_tests),
468+
CASE1(ellswift_encode_decode_roundtrip_tests),
469+
CASE1(ellswift_create_tests),
470+
CASE1(ellswift_compute_shared_secret_tests),
471+
CASE1(ellswift_xdh_correctness_tests),
472+
CASE1(ellswift_hash_init_tests),
441473
};
442474

443475
#endif

0 commit comments

Comments
 (0)