Skip to content

Commit 060dad2

Browse files
committed
schnorr_adaptor: add tests
This commit adds test coverage, modifying the ci.yml file, Valgrind constant time tests for secret data, API tests, nonce function tests, and test vectors from the spec.
1 parent 808dc8a commit 060dad2

File tree

8 files changed

+1347
-17
lines changed

8 files changed

+1347
-17
lines changed

.github/workflows/ci.yml

+22-11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ env:
4141
ECDSAADAPTOR: 'no'
4242
BPPP: 'no'
4343
SCHNORRSIG_HALFAGG: 'no'
44+
SCHNORRADAPTOR: 'no'
4445
### test options
4546
SECP256K1_TEST_ITERS:
4647
BENCH: 'yes'
@@ -79,14 +80,14 @@ jobs:
7980
matrix:
8081
configuration:
8182
- env_vars: { WIDEMUL: 'int64', RECOVERY: 'yes' }
82-
- env_vars: { WIDEMUL: 'int64', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes'}
83+
- env_vars: { WIDEMUL: 'int64', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes'}
8384
- env_vars: { WIDEMUL: 'int128' }
8485
- env_vars: { WIDEMUL: 'int128_struct', ELLSWIFT: 'yes' }
8586
- env_vars: { WIDEMUL: 'int128', RECOVERY: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes' }
86-
- env_vars: { WIDEMUL: 'int128', ECDH: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes'}
87+
- env_vars: { WIDEMUL: 'int128', ECDH: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes'}
8788
- env_vars: { WIDEMUL: 'int128', ASM: 'x86_64', ELLSWIFT: 'yes' }
88-
- env_vars: { RECOVERY: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes'}
89-
- env_vars: { CTIMETESTS: 'no', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', CPPFLAGS: '-DVERIFY' }
89+
- env_vars: { RECOVERY: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes'}
90+
- env_vars: { CTIMETESTS: 'no', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes', CPPFLAGS: '-DVERIFY' }
9091
- env_vars: { BUILD: 'distcheck', WITH_VALGRIND: 'no', CTIMETESTS: 'no', BENCH: 'no' }
9192
- env_vars: { CPPFLAGS: '-DDETERMINISTIC' }
9293
- env_vars: { CFLAGS: '-O0', CTIMETESTS: 'no' }
@@ -158,6 +159,7 @@ jobs:
158159
ECDSAADAPTOR: 'yes'
159160
BPPP: 'yes'
160161
SCHNORRSIG_HALFAGG: 'yes'
162+
SCHNORRADAPTOR: 'yes'
161163
CC: ${{ matrix.cc }}
162164

163165
steps:
@@ -211,6 +213,7 @@ jobs:
211213
ECDSAADAPTOR: 'yes'
212214
BPPP: 'yes'
213215
SCHNORRSIG_HALFAGG: 'yes'
216+
SCHNORRADAPTOR: 'yes'
214217
CTIMETESTS: 'no'
215218

216219
steps:
@@ -271,6 +274,7 @@ jobs:
271274
ECDSAADAPTOR: 'yes'
272275
BPPP: 'yes'
273276
SCHNORRSIG_HALFAGG: 'yes'
277+
SCHNORRADAPTOR: 'yes'
274278
CTIMETESTS: 'no'
275279

276280
steps:
@@ -325,6 +329,7 @@ jobs:
325329
ECDSAADAPTOR: 'yes'
326330
BPPP: 'yes'
327331
SCHNORRSIG_HALFAGG: 'yes'
332+
SCHNORRADAPTOR: 'yes'
328333
CTIMETESTS: 'no'
329334

330335
strategy:
@@ -389,6 +394,7 @@ jobs:
389394
ECDSAADAPTOR: 'yes'
390395
BPPP: 'yes'
391396
SCHNORRSIG_HALFAGG: 'yes'
397+
SCHNORRADAPTOR: 'yes'
392398
CTIMETESTS: 'no'
393399

394400
steps:
@@ -450,6 +456,7 @@ jobs:
450456
ECDSAADAPTOR: 'yes'
451457
BPPP: 'yes'
452458
SCHNORRSIG_HALFAGG: 'yes'
459+
SCHNORRADAPTOR: 'yes'
453460
CTIMETESTS: 'no'
454461
SECP256K1_TEST_ITERS: 2
455462

@@ -510,6 +517,7 @@ jobs:
510517
ECDSAADAPTOR: 'yes'
511518
BPPP: 'yes'
512519
SCHNORRSIG_HALFAGG: 'yes'
520+
SCHNORRADAPTOR: 'yes'
513521
CTIMETESTS: 'no'
514522
CFLAGS: '-fsanitize=undefined,address -g'
515523
UBSAN_OPTIONS: 'print_stacktrace=1:halt_on_error=1'
@@ -576,6 +584,7 @@ jobs:
576584
ECDSAADAPTOR: 'yes'
577585
BPPP: 'yes'
578586
SCHNORRSIG_HALFAGG: 'yes'
587+
SCHNORRADAPTOR: 'yes'
579588
CTIMETESTS: 'yes'
580589
CC: 'clang'
581590
SECP256K1_TEST_ITERS: 32
@@ -632,6 +641,7 @@ jobs:
632641
ECDSAADAPTOR: 'yes'
633642
BPPP: 'yes'
634643
SCHNORRSIG_HALFAGG: 'yes'
644+
SCHNORRADAPTOR: 'yes'
635645
CTIMETESTS: 'no'
636646

637647
strategy:
@@ -688,15 +698,15 @@ jobs:
688698
fail-fast: false
689699
matrix:
690700
env_vars:
691-
- { WIDEMUL: 'int64', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes' }
701+
- { WIDEMUL: 'int64', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes' }
692702
- { WIDEMUL: 'int128_struct', ECMULTGENPRECISION: 2, ECMULTWINDOW: 4 }
693-
- { WIDEMUL: 'int128', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes' }
703+
- { WIDEMUL: 'int128', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes' }
694704
- { WIDEMUL: 'int128', RECOVERY: 'yes' }
695-
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes' }
696-
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', CC: 'gcc' }
697-
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 }
698-
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', CC: 'gcc', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 }
699-
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', CPPFLAGS: '-DVERIFY', CTIMETESTS: 'no' }
705+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes' }
706+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes', CC: 'gcc' }
707+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 }
708+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes', CC: 'gcc', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 }
709+
- { WIDEMUL: 'int128', RECOVERY: 'yes', ECDH: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRSIG_HALFAGG: 'yes', SCHNORRADAPTOR: 'yes', CPPFLAGS: '-DVERIFY', CTIMETESTS: 'no' }
700710
- BUILD: 'distcheck'
701711

702712
steps:
@@ -816,6 +826,7 @@ jobs:
816826
ECDSAADAPTOR: 'yes'
817827
BPPP: 'yes'
818828
SCHNORRSIG_HALFAGG: 'yes'
829+
SCHNORRADAPTOR: 'yes'
819830

820831
steps:
821832
- name: Checkout

ci/ci.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ print_environment() {
1313
# does not rely on bash.
1414
for var in WERROR_CFLAGS MAKEFLAGS BUILD \
1515
ECMULTWINDOW ECMULTGENPRECISION ASM WIDEMUL WITH_VALGRIND EXTRAFLAGS \
16-
EXPERIMENTAL ECDH RECOVERY SCHNORRSIG SCHNORRSIG_HALFAGG ELLSWIFT \
17-
ECDSA_S2C GENERATOR RANGEPROOF WHITELIST MUSIG ECDSAADAPTOR BPPP \
16+
EXPERIMENTAL ECDH RECOVERY SCHNORRSIG SCHNORRSIG_HALFAGG SCHNORRADAPTOR \
17+
ELLSWIFT ECDSA_S2C GENERATOR RANGEPROOF WHITELIST MUSIG ECDSAADAPTOR BPPP \
1818
SECP256K1_TEST_ITERS BENCH SECP256K1_BENCH_ITERS CTIMETESTS\
1919
EXAMPLES \
2020
HOST WRAPPER_CMD \
@@ -83,6 +83,7 @@ esac
8383
--enable-module-schnorrsig="$SCHNORRSIG" --enable-module-musig="$MUSIG" --enable-module-ecdsa-adaptor="$ECDSAADAPTOR" \
8484
--enable-module-schnorrsig="$SCHNORRSIG" \
8585
--enable-module-schnorrsig-halfagg="$SCHNORRSIG_HALFAGG" \
86+
--enable-module-schnorr-adaptor="$SCHNORRADAPTOR" \
8687
--enable-examples="$EXAMPLES" \
8788
--enable-ctime-tests="$CTIMETESTS" \
8889
--with-valgrind="$WITH_VALGRIND" \

src/ctime_tests.c

+45
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "../include/secp256k1_schnorrsig.h"
3232
#endif
3333

34+
#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR
35+
#include "../include/secp256k1_schnorr_adaptor.h"
36+
#endif
37+
3438
#ifdef ENABLE_MODULE_ELLSWIFT
3539
#include "../include/secp256k1_ellswift.h"
3640
#endif
@@ -193,6 +197,47 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) {
193197
CHECK(ret == 1);
194198
#endif
195199

200+
#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR
201+
{
202+
unsigned char pre_sig[65];
203+
unsigned char bip340_sig[64];
204+
unsigned char sec_adaptor[32];
205+
unsigned char extracted_sec_adaptor[32];
206+
secp256k1_pubkey adaptor_pk;
207+
208+
for (i = 0; i < 32; i++) {
209+
sec_adaptor[i] = i + 2;
210+
}
211+
ret = secp256k1_ec_pubkey_create(ctx, &adaptor_pk, sec_adaptor);
212+
CHECK(ret == 1);
213+
214+
SECP256K1_CHECKMEM_UNDEFINE(key, 32);
215+
ret = secp256k1_keypair_create(ctx, &keypair, key);
216+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
217+
CHECK(ret == 1);
218+
ret = secp256k1_schnorr_adaptor_presign(ctx, pre_sig, msg, &keypair, &adaptor_pk, NULL);
219+
SECP256K1_CHECKMEM_DEFINE(pre_sig, sizeof(pre_sig));
220+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
221+
CHECK(ret == 1);
222+
223+
SECP256K1_CHECKMEM_UNDEFINE(sec_adaptor, sizeof(sec_adaptor));
224+
ret = secp256k1_schnorr_adaptor_adapt(ctx, bip340_sig, pre_sig, sec_adaptor);
225+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
226+
CHECK(ret == 1);
227+
228+
SECP256K1_CHECKMEM_UNDEFINE(bip340_sig, sizeof(bip340_sig));
229+
ret = secp256k1_schnorr_adaptor_extract_sec(ctx, extracted_sec_adaptor, pre_sig, bip340_sig);
230+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
231+
CHECK(ret == 1);
232+
233+
SECP256K1_CHECKMEM_DEFINE(sec_adaptor, sizeof(sec_adaptor));
234+
SECP256K1_CHECKMEM_DEFINE(extracted_sec_adaptor, sizeof(extracted_sec_adaptor));
235+
ret = secp256k1_memcmp_var(sec_adaptor, extracted_sec_adaptor, sizeof(sec_adaptor));
236+
SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret));
237+
CHECK(ret == 0);
238+
}
239+
#endif
240+
196241
#ifdef ENABLE_MODULE_ELLSWIFT
197242
SECP256K1_CHECKMEM_UNDEFINE(key, 32);
198243
ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL);

src/modules/ecdsa_adaptor/tests_impl.h

-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ static void dleq_tests(void) {
100100
CHECK(secp256k1_dleq_nonce(&k, sk32, gen2_33, p1_33, p2_33, NULL, NULL) == 1);
101101
}
102102

103-
static void rand_flip_bit(unsigned char *array, size_t n) {
104-
array[secp256k1_testrand_int(n)] ^= 1 << secp256k1_testrand_int(8);
105-
}
106-
107103
/* Helper function for test_ecdsa_adaptor_spec_vectors
108104
* Checks that the adaptor signature is valid for the public and encryption keys. */
109105
static void test_ecdsa_adaptor_spec_vectors_check_verify(const unsigned char *adaptor_sig162, const unsigned char *msg32, const unsigned char *pubkey33, const unsigned char *encryption_key33, int expected) {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include_HEADERS += include/secp256k1_schnorr_adaptor.h
22
noinst_HEADERS += src/modules/schnorr_adaptor/main_impl.h
3+
noinst_HEADERS += src/modules/schnorr_adaptor/tests_impl.h

0 commit comments

Comments
 (0)