Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exhaustive tests for ellswift (with create+decode roundtrip) #1371

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/ellswift/Makefile.am.include
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include_HEADERS += include/secp256k1_ellswift.h
noinst_HEADERS += src/modules/ellswift/bench_impl.h
noinst_HEADERS += src/modules/ellswift/main_impl.h
noinst_HEADERS += src/modules/ellswift/tests_impl.h
noinst_HEADERS += src/modules/ellswift/tests_exhaustive_impl.h
39 changes: 39 additions & 0 deletions src/modules/ellswift/tests_exhaustive_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***********************************************************************
* Distributed under the MIT software license, see the accompanying *
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/

#ifndef SECP256K1_MODULE_ELLSWIFT_TESTS_EXHAUSTIVE_H
#define SECP256K1_MODULE_ELLSWIFT_TESTS_EXHAUSTIVE_H

#include "../../../include/secp256k1_ellswift.h"
#include "main_impl.h"

static void test_exhaustive_ellswift(const secp256k1_context *ctx, const secp256k1_ge *group) {
int i;

/* Note that SwiftEC/ElligatorSwift are inherently curve operations, not
* group operations, and this test only checks the curve points which are in
* a tiny subgroup. In that sense it can't be really seen as exhaustive as
* it doesn't (and for computational reasons obviously cannot) test the
* entire domain ellswift operates under. */
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
secp256k1_scalar scalar_i;
unsigned char sec32[32];
unsigned char ell64[64];
secp256k1_pubkey pub_decoded;
secp256k1_ge ge_decoded;

/* Construct ellswift pubkey from exhaustive loop scalar i. */
secp256k1_scalar_set_int(&scalar_i, i);
secp256k1_scalar_get_b32(sec32, &scalar_i);
CHECK(secp256k1_ellswift_create(ctx, ell64, sec32, NULL));

/* Decode ellswift pubkey and check that it matches the precomputed group element. */
secp256k1_ellswift_decode(ctx, &pub_decoded, ell64);
secp256k1_pubkey_load(ctx, &ge_decoded, &pub_decoded);
ge_equals_ge(&ge_decoded, &group[i]);
}
}

#endif
16 changes: 16 additions & 0 deletions src/tests_exhaustive.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#define EXHAUSTIVE_TEST_ORDER 13
#endif

/* These values of B are all values in [1, 8] that result in a curve with even order. */
#define EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER (SECP256K1_B == 1 || SECP256K1_B == 6 || SECP256K1_B == 8)

#ifdef USE_EXTERNAL_DEFAULT_CALLBACKS
#pragma message("Ignoring USE_EXTERNAL_CALLBACKS in exhaustive_tests.")
#undef USE_EXTERNAL_DEFAULT_CALLBACKS
Expand Down Expand Up @@ -395,6 +398,10 @@ static void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_g
#include "modules/schnorrsig/tests_exhaustive_impl.h"
#endif

#ifdef ENABLE_MODULE_ELLSWIFT
#include "modules/ellswift/tests_exhaustive_impl.h"
#endif

int main(int argc, char** argv) {
int i;
secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER];
Expand Down Expand Up @@ -490,6 +497,15 @@ int main(int argc, char** argv) {
#ifdef ENABLE_MODULE_SCHNORRSIG
test_exhaustive_schnorrsig(ctx);
#endif
#ifdef ENABLE_MODULE_ELLSWIFT
/* The ellswift algorithm does have additional edge cases when operating on
* curves of even order, which are not included in the code as secp256k1 is
* of odd order. Skip the ellswift tests if the used exhaustive tests curve
* is even-ordered accordingly. */
#if !EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER
test_exhaustive_ellswift(ctx, group);
#endif
#endif

secp256k1_context_destroy(ctx);
}
Expand Down