Skip to content

Commit afd7eb4

Browse files
Merge bitcoin-core/secp256k1#1371: Add exhaustive tests for ellswift (with create+decode roundtrip)
2792119 Add exhaustive test for ellswift (create+decode roundtrip) (Sebastian Falbesoner) Pull request description: This PR adds the basic structure for ellswift exhaustive tests. Right now only a `secp256k1_ellswift_create` + `secp256k1_ellswift_decode` indirect roundtrip (exhaustive loop scalar -> ellswift pubkey -> decoded pubkey -> decoded group element, compared with exhaustive precomputed group element) is included. The exhaustive tests passes locally with all currently supported orders (n=13 [default] and n=199). Note that for n=7, the test is skipped, as the used curve in this case is even-ordered and ellswift only supports odd-ordered curves. ACKs for top commit: sipa: utACK 2792119 real-or-random: utACK 2792119 Tree-SHA512: c51d3d99e9839793b3c15d75b9a29f01080db160ab8819973abd877288f9f0af972ea4264290220ab1cd035fdebcfac7767436aa39154d924ef0bf6a5733a55d
2 parents 332af31 + 2792119 commit afd7eb4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#define EXHAUSTIVE_TEST_ORDER 13
1414
#endif
1515

16+
/* These values of B are all values in [1, 8] that result in a curve with even order. */
17+
#define EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER (SECP256K1_B == 1 || SECP256K1_B == 6 || SECP256K1_B == 8)
18+
1619
#ifdef USE_EXTERNAL_DEFAULT_CALLBACKS
1720
#pragma message("Ignoring USE_EXTERNAL_CALLBACKS in exhaustive_tests.")
1821
#undef USE_EXTERNAL_DEFAULT_CALLBACKS
@@ -395,6 +398,10 @@ static void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_g
395398
#include "modules/schnorrsig/tests_exhaustive_impl.h"
396399
#endif
397400

401+
#ifdef ENABLE_MODULE_ELLSWIFT
402+
#include "modules/ellswift/tests_exhaustive_impl.h"
403+
#endif
404+
398405
int main(int argc, char** argv) {
399406
int i;
400407
secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER];
@@ -490,6 +497,15 @@ int main(int argc, char** argv) {
490497
#ifdef ENABLE_MODULE_SCHNORRSIG
491498
test_exhaustive_schnorrsig(ctx);
492499
#endif
500+
#ifdef ENABLE_MODULE_ELLSWIFT
501+
/* The ellswift algorithm does have additional edge cases when operating on
502+
* curves of even order, which are not included in the code as secp256k1 is
503+
* of odd order. Skip the ellswift tests if the used exhaustive tests curve
504+
* is even-ordered accordingly. */
505+
#if !EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER
506+
test_exhaustive_ellswift(ctx, group);
507+
#endif
508+
#endif
493509

494510
secp256k1_context_destroy(ctx);
495511
}

0 commit comments

Comments
 (0)