Skip to content

Commit 38a3c5d

Browse files
theStacksipa
andcommitted
Add exhaustive test for ellswift (create+decode roundtrip)
Co-authored-by: Pieter Wuille <[email protected]>
1 parent 332af31 commit 38a3c5d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/modules/ellswift/Makefile.am.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ include_HEADERS += include/secp256k1_ellswift.h
22
noinst_HEADERS += src/modules/ellswift/bench_impl.h
33
noinst_HEADERS += src/modules/ellswift/main_impl.h
44
noinst_HEADERS += src/modules/ellswift/tests_impl.h
5+
noinst_HEADERS += src/modules/ellswift/tests_exhaustive_impl.h
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/***********************************************************************
2+
* Distributed under the MIT software license, see the accompanying *
3+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4+
***********************************************************************/
5+
6+
#ifndef SECP256K1_MODULE_ELLSWIFT_TESTS_EXHAUSTIVE_H
7+
#define SECP256K1_MODULE_ELLSWIFT_TESTS_EXHAUSTIVE_H
8+
9+
#include "../../../include/secp256k1_ellswift.h"
10+
#include "main_impl.h"
11+
12+
static void test_exhaustive_ellswift(const secp256k1_context *ctx, const secp256k1_ge *group) {
13+
int i;
14+
15+
/* Note that SwiftEC/ElligatorSwift are inherently curve operations, not
16+
* group operations, and this test only checks the curve points which are in
17+
* a tiny subgroup. In that sense it can't be really seen as exhaustive as
18+
* it doesn't (and for computational reasons obviously cannot) test the
19+
* entire domain ellswift operates under. */
20+
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
21+
secp256k1_scalar scalar_i;
22+
unsigned char sec32[32];
23+
unsigned char ell64[64];
24+
secp256k1_pubkey pub_decoded;
25+
secp256k1_ge ge_decoded;
26+
27+
/* Construct ellswift pubkey from exhaustive loop scalar i. */
28+
secp256k1_scalar_set_int(&scalar_i, i);
29+
secp256k1_scalar_get_b32(sec32, &scalar_i);
30+
CHECK(secp256k1_ellswift_create(ctx, ell64, sec32, NULL));
31+
32+
/* Decode ellswift pubkey and check that it matches the precomputed group element. */
33+
secp256k1_ellswift_decode(ctx, &pub_decoded, ell64);
34+
secp256k1_pubkey_load(ctx, &ge_decoded, &pub_decoded);
35+
ge_equals_ge(&ge_decoded, &group[i]);
36+
}
37+
}
38+
39+
#endif

src/tests_exhaustive.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ static void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_g
395395
#include "modules/schnorrsig/tests_exhaustive_impl.h"
396396
#endif
397397

398+
#ifdef ENABLE_MODULE_ELLSWIFT
399+
#include "modules/ellswift/tests_exhaustive_impl.h"
400+
#endif
401+
398402
int main(int argc, char** argv) {
399403
int i;
400404
secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER];
@@ -490,6 +494,15 @@ int main(int argc, char** argv) {
490494
#ifdef ENABLE_MODULE_SCHNORRSIG
491495
test_exhaustive_schnorrsig(ctx);
492496
#endif
497+
#ifdef ENABLE_MODULE_ELLSWIFT
498+
/* The ellswift algorithm does have additional edge cases when operating on
499+
* curves of even order, which are not included in the code as secp256k1 is
500+
* of odd order. The n=7 exhaustive group is on an even-ordered curve, so
501+
* skip the tests for it. */
502+
#if EXHAUSTIVE_TEST_ORDER != 7
503+
test_exhaustive_ellswift(ctx, group);
504+
#endif
505+
#endif
493506

494507
secp256k1_context_destroy(ctx);
495508
}

0 commit comments

Comments
 (0)