diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d1f765e0..8ccf46730 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,6 +40,7 @@ env: MUSIG: 'no' ECDSAADAPTOR: 'no' BPPP: 'no' + SCHNORRADAPTOR: 'no' ### test options SECP256K1_TEST_ITERS: BENCH: 'yes' @@ -78,14 +79,14 @@ jobs: matrix: configuration: - env_vars: { WIDEMUL: 'int64', RECOVERY: 'yes' } - - 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' } + - 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', SCHNORRADAPTOR: 'yes'} - env_vars: { WIDEMUL: 'int128' } - env_vars: { WIDEMUL: 'int128_struct', ELLSWIFT: 'yes' } - env_vars: { WIDEMUL: 'int128', RECOVERY: 'yes', SCHNORRSIG: 'yes', ELLSWIFT: 'yes' } - - 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'} + - 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', SCHNORRADAPTOR: 'yes'} - env_vars: { WIDEMUL: 'int128', ASM: 'x86_64', ELLSWIFT: 'yes' } - - env_vars: { RECOVERY: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes'} - - 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', CPPFLAGS: '-DVERIFY' } + - env_vars: { RECOVERY: 'yes', SCHNORRSIG: 'yes', EXPERIMENTAL: 'yes', ECDSA_S2C: 'yes', RANGEPROOF: 'yes', WHITELIST: 'yes', GENERATOR: 'yes', MUSIG: 'yes', ECDSAADAPTOR: 'yes', BPPP: 'yes', SCHNORRADAPTOR: 'yes'} + - 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', SCHNORRADAPTOR: 'yes', CPPFLAGS: '-DVERIFY' } - env_vars: { BUILD: 'distcheck', WITH_VALGRIND: 'no', CTIMETESTS: 'no', BENCH: 'no' } - env_vars: { CPPFLAGS: '-DDETERMINISTIC' } - env_vars: { CFLAGS: '-O0', CTIMETESTS: 'no' } @@ -156,6 +157,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CC: ${{ matrix.cc }} steps: @@ -208,6 +210,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' steps: @@ -267,6 +270,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' steps: @@ -320,6 +324,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' strategy: @@ -383,6 +388,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' steps: @@ -443,6 +449,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' SECP256K1_TEST_ITERS: 2 @@ -502,6 +509,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' CFLAGS: '-fsanitize=undefined,address -g' UBSAN_OPTIONS: 'print_stacktrace=1:halt_on_error=1' @@ -567,6 +575,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'yes' CC: 'clang' SECP256K1_TEST_ITERS: 32 @@ -622,6 +631,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' CTIMETESTS: 'no' strategy: @@ -678,15 +688,15 @@ jobs: fail-fast: false matrix: env_vars: - - { 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' } + - { 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', SCHNORRADAPTOR: 'yes' } - { WIDEMUL: 'int128_struct', ECMULTGENPRECISION: 2, ECMULTWINDOW: 4 } - - { 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' } + - { 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', SCHNORRADAPTOR: 'yes' } - { WIDEMUL: 'int128', RECOVERY: 'yes' } - - { 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' } - - { 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', CC: 'gcc' } - - { 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', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 } - - { 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', CC: 'gcc', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 } - - { 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', CPPFLAGS: '-DVERIFY', CTIMETESTS: 'no' } + - { 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', SCHNORRADAPTOR: 'yes' } + - { 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', SCHNORRADAPTOR: 'yes', CC: 'gcc' } + - { 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', SCHNORRADAPTOR: 'yes', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 } + - { 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', SCHNORRADAPTOR: 'yes', CC: 'gcc', WRAPPER_CMD: 'valgrind --error-exitcode=42', SECP256K1_TEST_ITERS: 2 } + - { 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', SCHNORRADAPTOR: 'yes', CPPFLAGS: '-DVERIFY', CTIMETESTS: 'no' } - BUILD: 'distcheck' steps: @@ -805,6 +815,7 @@ jobs: MUSIG: 'yes' ECDSAADAPTOR: 'yes' BPPP: 'yes' + SCHNORRADAPTOR: 'yes' steps: - name: Checkout diff --git a/Makefile.am b/Makefile.am index c96685623..22ef48b77 100644 --- a/Makefile.am +++ b/Makefile.am @@ -317,3 +317,7 @@ endif if ENABLE_MODULE_ECDSA_ADAPTOR include src/modules/ecdsa_adaptor/Makefile.am.include endif + +if ENABLE_MODULE_SCHNORR_ADAPTOR +include src/modules/schnorr_adaptor/Makefile.am.include +endif \ No newline at end of file diff --git a/ci/ci.sh b/ci/ci.sh index b4b67316d..81c17825d 100755 --- a/ci/ci.sh +++ b/ci/ci.sh @@ -82,6 +82,7 @@ esac --enable-module-rangeproof="$RANGEPROOF" --enable-module-whitelist="$WHITELIST" --enable-module-generator="$GENERATOR" \ --enable-module-schnorrsig="$SCHNORRSIG" --enable-module-musig="$MUSIG" --enable-module-ecdsa-adaptor="$ECDSAADAPTOR" \ --enable-module-schnorrsig="$SCHNORRSIG" \ + --enable-module-schnorr-adaptor="$SCHNORRADAPTOR" \ --enable-examples="$EXAMPLES" \ --enable-ctime-tests="$CTIMETESTS" \ --with-valgrind="$WITH_VALGRIND" \ diff --git a/configure.ac b/configure.ac index d4934d32d..fe512c82f 100644 --- a/configure.ac +++ b/configure.ac @@ -213,6 +213,10 @@ AC_ARG_ENABLE(module_ellswift, AS_HELP_STRING([--enable-module-ellswift],[enable ElligatorSwift module [default=yes]]), [], [SECP_SET_DEFAULT([enable_module_ellswift], [yes], [yes])]) +AC_ARG_ENABLE(module_schnorr_adaptor, + AS_HELP_STRING([--enable-module-schnorr-adaptor],[enable schnorr adaptor module [default=no]]), [], + [SECP_SET_DEFAULT([enable_module_schnorr_adaptor], [no], [yes])]) + AC_ARG_ENABLE(module_ecdsa_s2c, AS_HELP_STRING([--enable-module-ecdsa-s2c],[enable ECDSA sign-to-contract module [default=no]]), [], @@ -490,6 +494,12 @@ if test x"$enable_module_schnorrsig" = x"yes"; then enable_module_extrakeys=yes fi +if test x"$enable_module_schnorr_adaptor" = x"yes"; then + AC_DEFINE(ENABLE_MODULE_SCHNORR_ADAPTOR, 1, [Define thsi symbol to enable the Schnorr adaptor module]) + enable_module_extrakeys=yes + enable_module_schnorrsig=yes +fi + if test x"$enable_module_ellswift" = x"yes"; then SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DENABLE_MODULE_ELLSWIFT=1" fi @@ -557,6 +567,9 @@ else if test x"$set_asm" = x"arm32"; then AC_MSG_ERROR([ARM32 assembly optimization is experimental. Use --enable-experimental to allow.]) fi + if test x"$enable_module_schnorr_adaptor" = x"yes"; then + AC_MSG_ERROR([Schnorr adaptor signatures module is experimental. Use --enable-experimental to allow.]) + fi fi ### @@ -581,6 +594,7 @@ AM_CONDITIONAL([ENABLE_MODULE_RANGEPROOF], [test x"$enable_module_rangeproof" = AM_CONDITIONAL([ENABLE_MODULE_WHITELIST], [test x"$enable_module_whitelist" = x"yes"]) AM_CONDITIONAL([ENABLE_MODULE_EXTRAKEYS], [test x"$enable_module_extrakeys" = x"yes"]) AM_CONDITIONAL([ENABLE_MODULE_SCHNORRSIG], [test x"$enable_module_schnorrsig" = x"yes"]) +AM_CONDITIONAL([ENABLE_MODULE_SCHNORR_ADAPTOR], [test x"$enable_module_schnorr_adaptor" = x"yes"]) AM_CONDITIONAL([ENABLE_MODULE_ELLSWIFT], [test x"$enable_module_ellswift" = x"yes"]) AM_CONDITIONAL([ENABLE_MODULE_ECDSA_S2C], [test x"$enable_module_ecdsa_s2c" = x"yes"]) AM_CONDITIONAL([ENABLE_MODULE_ECDSA_ADAPTOR], [test x"$enable_module_ecdsa_adaptor" = x"yes"]) @@ -607,6 +621,7 @@ echo " module ecdh = $enable_module_ecdh" echo " module recovery = $enable_module_recovery" echo " module extrakeys = $enable_module_extrakeys" echo " module schnorrsig = $enable_module_schnorrsig" +echo " module schnorr-adaptor = $enable_module_schnorr_adaptor" echo " module ellswift = $enable_module_ellswift" echo " module generator = $enable_module_generator" echo " module rangeproof = $enable_module_rangeproof" diff --git a/include/secp256k1_schnorr_adaptor.h b/include/secp256k1_schnorr_adaptor.h new file mode 100644 index 000000000..525e14bff --- /dev/null +++ b/include/secp256k1_schnorr_adaptor.h @@ -0,0 +1,127 @@ +#ifndef SECP256K1_SCHNORR_ADAPTOR_H +#define SECP256K1_SCHNORR_ADAPTOR_H + +#include "secp256k1.h" +#include "secp256k1_extrakeys.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** A pointer to a function to deterministically generate a nonce. + * + * Same as secp256k1_schnorrsig_nonce function with the exception of accepting an + * additional adaptor point argument. + * + * Returns: 1 if a nonce was successfully generated. 0 will cause signing to + * return an error. + * Out: nonce32: pointer to a 32-byte array to be filled by the function + * In: msg32: the 32-byte message being verified (will not be NULL) + * key32: pointer to a 32-byte secret key (will not be NULL) + * adaptor33: the 33-byte serialized adaptor point (will not be NULL) + * xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32 + * (will not be NULL) + * algo: pointer to an array describing the signature + * algorithm (will not be NULL) + * algolen: the length of the algo array + * data: arbitrary data pointer that is passed through + * + * Except for test cases, this function should compute some cryptographic hash of + * the message, the key, the adaptor point, the pubkey, the algorithm description, and data. + */ +typedef int (*secp256k1_adaptor_nonce_function_hardened)( + unsigned char *nonce32, + const unsigned char *msg32, + const unsigned char *key32, + const unsigned char *adaptor33, + const unsigned char *xonly_pk32, + const unsigned char *algo, + size_t algolen, + void *data +); + +/** A Schnorr Adaptor nonce generation function. */ +SECP256K1_API const secp256k1_adaptor_nonce_function_hardened secp256k1_nonce_function_schnorr_adaptor; + +/** Create a Schnorr adaptor signature. + * + * This function only signs 32-byte messages. If you have messages of a + * different size (or the same size but without a context-specific tag + * prefix), it is recommended to create a 32-byte message hash with + * secp256k1_tagged_sha256 and then sign the hash. Tagged hashing allows + * providing an context-specific tag for domain separation. This prevents + * signatures from being valid in multiple contexts by accident. + * + * Returns 1 on success, 0 on failure. + * Args: ctx: pointer to a context object (not secp256k1_context_static). + * Out: presig65: pointer to a 65-byte array to store the adaptor signature. + * In: msg32: the 32-byte message being signed. + * keypair: pointer to an initialized keypair. + * adaptor: pointer to an adaptor point. + * aux_rand32: 32 bytes of fresh randomness. While recommended to provide + * this, it is only supplemental to security and can be NULL. A + * NULL argument is treated the same as an all-zero one. See + * BIP-340 "Default Signing" for a full explanation of this + * argument and for guidance if randomness is expensive. + */ +SECP256K1_API int secp256k1_schnorr_adaptor_presign( + const secp256k1_context *ctx, + unsigned char *presig65, + const unsigned char *msg32, + const secp256k1_keypair *keypair, + const secp256k1_pubkey *adaptor, + const unsigned char *aux_rand32 +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); + +/** Extract an adaptor point from the signature. + * + * Returns 1 on success, 0 on failure. + * Args: ctx: pointer to a context object. + * Out: adaptor33: pointer to an adaptor point. + * In: presig65: pointer to a 65-byte adaptor signature. + * msg32: the 32-byte message being signed. + * pubkey: pointer to an x-only public key to verify with + */ +SECP256K1_API int secp256k1_schnorr_adaptor_extract( + const secp256k1_context *ctx, + secp256k1_pubkey *adaptor, + const unsigned char *presig65, + const unsigned char *msg32, + const secp256k1_xonly_pubkey *pubkey +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5); + +/** Adapt an adaptor signature to result in a Schnorr signature. + * + * Returns 1 on success, 0 on failure. + * Args: ctx: pointer to a context object. + * Out: sig64: pointer to a 64-byte array to store the adapted Schnorr signature. + * In: presig65: pointer to a 65-byte adaptor signature. + * secadaptor: pointer to a 32-byte secadaptor. + */ +SECP256K1_API int secp256k1_schnorr_adaptor_adapt( + const secp256k1_context *ctx, + unsigned char *sig64, + const unsigned char *presig65, + const unsigned char *secadaptor +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); + +/** Extract the secadaptor from a valid adaptor signature and a Schnorr signature. + * + * Returns 1 on success, 0 on failure. + * Args: ctx: pointer to a context object. + * Out: secadaptor: pointer to a 32-byte array to store the secadaptor. + * In: presig65: pointer to a 65-byte adaptor signature. + * sig64: pointer to a 64-byte adapted Schnorr signature. + */ +SECP256K1_API int secp256k1_schnorr_adaptor_extract_sec( + const secp256k1_context *ctx, + unsigned char *secadaptor, + const unsigned char *presig65, + const unsigned char *sig64 +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4); + +#ifdef __cplusplus +} +#endif + +#endif /* SECP256K1_SCHNORR_ADAPTOR_H */ diff --git a/src/ctime_tests.c b/src/ctime_tests.c index 407d2cc6a..de607ad5b 100644 --- a/src/ctime_tests.c +++ b/src/ctime_tests.c @@ -31,6 +31,10 @@ #include "../include/secp256k1_schnorrsig.h" #endif +#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR +#include "../include/secp256k1_schnorr_adaptor.h" +#endif + #ifdef ENABLE_MODULE_ELLSWIFT #include "../include/secp256k1_ellswift.h" #endif @@ -193,6 +197,24 @@ static void run_tests(secp256k1_context *ctx, unsigned char *key) { CHECK(ret == 1); #endif +#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR + { + unsigned char t[33]; + + for (i = 0; i < 33; i++) { + t[i] = i + 2; + } + + SECP256K1_CHECKMEM_UNDEFINE(key, 32); + ret = secp256k1_keypair_create(ctx, &keypair, key); + SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret)); + CHECK(ret == 1); + ret = secp256k1_schnorr_adaptor_presign(ctx, sig, msg, &keypair, t, NULL); + SECP256K1_CHECKMEM_DEFINE(&ret, sizeof(ret)); + CHECK(ret == 1); + } +#endif + #ifdef ENABLE_MODULE_ELLSWIFT SECP256K1_CHECKMEM_UNDEFINE(key, 32); ret = secp256k1_ellswift_create(ctx, ellswift, key, NULL); diff --git a/src/modules/schnorr_adaptor/Makefile.am.include b/src/modules/schnorr_adaptor/Makefile.am.include new file mode 100644 index 000000000..7ef16361e --- /dev/null +++ b/src/modules/schnorr_adaptor/Makefile.am.include @@ -0,0 +1,3 @@ +include_HEADERS += include/secp256k1_schnorr_adaptor.h +noinst_HEADERS += src/modules/schnorr_adaptor/main_impl.h +noinst_HEADERS += src/modules/schnorr_adaptor/tests_impl.h \ No newline at end of file diff --git a/src/modules/schnorr_adaptor/main_impl.h b/src/modules/schnorr_adaptor/main_impl.h new file mode 100644 index 000000000..4d8ddc07b --- /dev/null +++ b/src/modules/schnorr_adaptor/main_impl.h @@ -0,0 +1,331 @@ +/********************************************************************** + * Copyright (c) 2023 Zhe Pang * + * Distributed under the MIT software license, see the accompanying * + * file COPYING or http://www.opensource.org/licenses/mit-license.php.* + **********************************************************************/ + +#ifndef SECP256K1_MODULE_SCHNORR_ADAPTOR_MAIN_H +#define SECP256K1_MODULE_SCHNORR_ADAPTOR_MAIN_H + +#include "../../../include/secp256k1.h" +#include "../../../include/secp256k1_schnorr_adaptor.h" +#include "../../hash.h" + +/* Initializes SHA256 with fixed midstate. This midstate was computed by applying + * SHA256 to SHA256("SchnorrAdaptor/nonce")||SHA256("SchnorrAdaptor/nonce"). */ +static void secp256k1_nonce_function_schnorr_adaptor_sha256_tagged(secp256k1_sha256 *sha) { + secp256k1_sha256_initialize(sha); + sha->s[0] = 0xe268ac2aul; + sha->s[1] = 0x3a221b84ul; + sha->s[2] = 0x69612afdul; + sha->s[3] = 0x92ce3040ul; + sha->s[4] = 0xc83ca35ful; + sha->s[5] = 0xec2ee152ul; + sha->s[6] = 0xba136ab7ul; + sha->s[7] = 0x3bf6ec7ful; + + sha->bytes = 64; +} + +/* Initializes SHA256 with fixed midstate. This midstate was computed by applying + * SHA256 to SHA256("SchnorrAdaptor/aux")||SHA256("SchnorrAdaptor/aux"). */ +static void secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux(secp256k1_sha256 *sha) { + secp256k1_sha256_initialize(sha); + sha->s[0] = 0x50685e98ul; + sha->s[1] = 0x6313905eul; + sha->s[2] = 0x6db24fa0ul; + sha->s[3] = 0xc8b15c48ul; + sha->s[4] = 0x6b318921ul; + sha->s[5] = 0x441d8ff3ul; + sha->s[6] = 0xa7033a66ul; + sha->s[7] = 0xc3545cddul; + + sha->bytes = 64; +} + +/* algo argument for adaptor_nonce_function_bip340 to derive the nonce of Schnorr adaptor signature + * by using the correct tagged hash function. */ +static const unsigned char adaptor_bip340_algo[20] = "SchnorrAdaptor/nonce"; + +static int nonce_function_schnorr_adaptor(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *adaptor33, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) { + secp256k1_sha256 sha; + unsigned char masked_key[32]; + int i; + + if (algo == NULL) { + return 0; + } + + if (data != NULL) { + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux(&sha); + secp256k1_sha256_write(&sha, data, 32); + secp256k1_sha256_finalize(&sha, masked_key); + for (i = 0; i < 32; i++) { + masked_key[i] ^= key32[i]; + } + } else { + /* Precomputed TaggedHash("SchnorrAdaptor/aux", 0x0000...00); */ + static const unsigned char ZERO_MASK[32] = { + 65, 206, 231, 5, 44, 99, 30, 162, + 119, 101, 143, 108, 176, 134, 217, 23, + 54, 150, 157, 221, 198, 161, 164, 85, + 235, 82, 28, 56, 164, 220, 113, 53 + }; + for (i = 0; i < 32; i++) { + masked_key[i] = key32[i] ^ ZERO_MASK[i]; + } + } + + /* Tag the hash with algo which is important to avoid nonce reuse across + * algorithms. */ + if (algolen == sizeof(adaptor_bip340_algo) + && secp256k1_memcmp_var(algo, adaptor_bip340_algo, algolen) == 0) { + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged(&sha); + } else { + secp256k1_sha256_initialize_tagged(&sha, algo, algolen); + } + + /* Hash masked-key||adaptor33||msg using the tagged hash */ + secp256k1_sha256_write(&sha, masked_key, 32); + secp256k1_sha256_write(&sha, adaptor33, 33); + secp256k1_sha256_write(&sha, xonly_pk32, 32); + secp256k1_sha256_write(&sha, msg32, 32); + secp256k1_sha256_finalize(&sha, nonce32); + return 1; +} + +const secp256k1_adaptor_nonce_function_hardened secp256k1_nonce_function_schnorr_adaptor = nonce_function_schnorr_adaptor; + +static int secp256k1_schnorr_adaptor_presign_internal(const secp256k1_context *ctx, unsigned char *presig65, const unsigned char *msg32, const secp256k1_keypair *keypair, secp256k1_adaptor_nonce_function_hardened noncefp, const secp256k1_pubkey *adaptor, void *ndata) { + secp256k1_scalar sk; + secp256k1_scalar e; + secp256k1_scalar k; + secp256k1_gej rj; + secp256k1_gej r0j; + secp256k1_ge pk; + secp256k1_ge r; + secp256k1_ge r0; + secp256k1_ge t; + unsigned char nonce32[32] = {0}; + unsigned char pk_buf[32]; + unsigned char seckey[32]; + unsigned char adaptor33_buff[33]; + size_t size = 33; + size_t msglen = 32; + int ret = 1; + + VERIFY_CHECK(ctx != NULL); + ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); + ARG_CHECK(presig65 != NULL); + ARG_CHECK(msg32 != NULL); + ARG_CHECK(keypair != NULL); + ARG_CHECK(adaptor != NULL); + + if (noncefp == NULL) { + noncefp = secp256k1_nonce_function_schnorr_adaptor; + } + + ret &= secp256k1_keypair_load(ctx, &sk, &pk, keypair); + + if (secp256k1_fe_is_odd(&pk.y)) { + secp256k1_scalar_negate(&sk, &sk); + } + + /* d */ + secp256k1_scalar_get_b32(seckey, &sk); + /* bytes_from_point(P) */ + secp256k1_fe_get_b32(pk_buf, &pk.x); + + /* T = cpoint(T) */ + ret &= secp256k1_pubkey_load(ctx, &t, adaptor); + ret &= secp256k1_eckey_pubkey_serialize(&t, adaptor33_buff, &size, 1); + + ret &= !!noncefp(nonce32, msg32, seckey, adaptor33_buff, pk_buf, adaptor_bip340_algo, sizeof(adaptor_bip340_algo), ndata); + /* k0 */ + secp256k1_scalar_set_b32(&k, nonce32, NULL); + ret &= !secp256k1_scalar_is_zero(&k); + secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); + + /* R = k0*G */ + secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &rj, &k); + secp256k1_ge_set_gej(&r, &rj); + + /* R' = k*G + T, can use gej_add_ge_var since r and t aren't secret */ + secp256k1_gej_add_ge_var(&r0j, &rj, &t, NULL); + secp256k1_ge_set_gej(&r0, &r0j); + + /* We declassify R' to allow using it as a branch point. This is fine + * because R' is not a secret. */ + secp256k1_declassify(ctx, &r0, sizeof(r0)); + secp256k1_fe_normalize_var(&r0.y); + if (secp256k1_fe_is_odd(&r0.y)) { + secp256k1_scalar_negate(&k, &k); + } + + ret &= secp256k1_eckey_pubkey_serialize(&r0, presig65, &size, 1); + + secp256k1_schnorrsig_challenge(&e, &presig65[1], msg32, msglen, pk_buf); + secp256k1_scalar_mul(&e, &e, &sk); + /* k + e * d */ + secp256k1_scalar_add(&e, &e, &k); + secp256k1_scalar_get_b32(&presig65[33], &e); + + secp256k1_memczero(presig65, 65, !ret); + secp256k1_scalar_clear(&k); + secp256k1_scalar_clear(&sk); + memset(seckey, 0, sizeof(seckey)); + + return ret; +} + +int secp256k1_schnorr_adaptor_presign(const secp256k1_context *ctx, unsigned char *presig65, const unsigned char *msg32, const secp256k1_keypair *keypair, const secp256k1_pubkey *adaptor, const unsigned char *aux_rand32) { + /* We cast away const from the passed aux_rand32 argument since we know the default nonce function does not modify it. */ + return secp256k1_schnorr_adaptor_presign_internal(ctx, presig65, msg32, keypair, secp256k1_nonce_function_schnorr_adaptor, adaptor, (unsigned char*)aux_rand32); +} + +int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pubkey *adaptor, const unsigned char *presig65, const unsigned char *msg32, const secp256k1_xonly_pubkey *pubkey) { + secp256k1_scalar s0; + secp256k1_scalar e; + secp256k1_gej rj; + secp256k1_ge pk; + secp256k1_gej pkj; + secp256k1_ge r0; + secp256k1_ge t; + secp256k1_gej tj; + secp256k1_xonly_pubkey pkr0; + unsigned char buf[32]; + size_t size = 33; + size_t msglen = 32; + int overflow; + + VERIFY_CHECK(ctx != NULL); + ARG_CHECK(adaptor != NULL); + ARG_CHECK(presig65 != NULL); + ARG_CHECK(msg32 != NULL); + ARG_CHECK(pubkey != NULL); + + /* P */ + if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) { + return 0; + } + + /* s0 */ + secp256k1_scalar_set_b32(&s0, &presig65[33], &overflow); + if (overflow) { + return 0; + } + + /* R0 */ + if (!secp256k1_xonly_pubkey_parse(ctx, &pkr0, &presig65[1])) { + return 0; + } + if (!secp256k1_xonly_pubkey_load(ctx, &r0, &pkr0)) { + return 0; + } + + /* Compute e */ + secp256k1_fe_get_b32(buf, &pk.x); + secp256k1_schnorrsig_challenge(&e, &presig65[1], msg32, msglen, buf); + + /* Compute rj = s0*G + (-e) * pkj */ + secp256k1_scalar_negate(&e, &e); + secp256k1_gej_set_ge(&pkj, &pk); + secp256k1_ecmult(&rj, &pkj, &e, &s0); + + /* R */ + if (secp256k1_gej_is_infinity(&rj)) { + return 0; + } + + /* T = R0 + (- R) */ + secp256k1_gej_neg(&rj, &rj); + secp256k1_gej_add_ge_var(&tj, &rj, &r0, NULL); + if (presig65[0] == SECP256K1_TAG_PUBKEY_EVEN) { + ; + } else if (presig65[0] == SECP256K1_TAG_PUBKEY_ODD) { + secp256k1_gej_neg(&tj, &tj); + } else { + return 0; + } + secp256k1_ge_set_gej(&t, &tj); + secp256k1_pubkey_save(adaptor, &t); + + return 1; +} + +int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *presig65, const unsigned char *secadaptor) { + secp256k1_scalar s0; + secp256k1_scalar s; + secp256k1_scalar t; + int overflow; + + VERIFY_CHECK(ctx != NULL); + ARG_CHECK(sig64 != NULL); + ARG_CHECK(presig65 != NULL); + ARG_CHECK(presig65[0] == SECP256K1_TAG_PUBKEY_EVEN || presig65[0] == SECP256K1_TAG_PUBKEY_ODD); + ARG_CHECK(secadaptor != NULL); + + /* s0 */ + secp256k1_scalar_set_b32(&s0, &presig65[33], &overflow); + if (overflow) { + return 0; + } + + /* t */ + secp256k1_scalar_set_b32(&t, secadaptor, &overflow); + if (overflow) { + return 0; + } + + if (presig65[0] == SECP256K1_TAG_PUBKEY_ODD) { + secp256k1_scalar_negate(&t, &t); + } + secp256k1_scalar_add(&s, &s0, &t); + + memcpy(sig64, &presig65[1], 32); + secp256k1_scalar_get_b32(&sig64[32], &s); + secp256k1_scalar_clear(&s); + secp256k1_scalar_clear(&t); + + return 1; +} + +int secp256k1_schnorr_adaptor_extract_sec(const secp256k1_context *ctx, unsigned char *secadaptor, const unsigned char *presig65, const unsigned char *sig64) { + secp256k1_scalar s0; + secp256k1_scalar s; + secp256k1_scalar t; + int overflow; + + VERIFY_CHECK(ctx != NULL); + ARG_CHECK(secadaptor != NULL); + ARG_CHECK(presig65 != NULL); + ARG_CHECK(presig65[0] == SECP256K1_TAG_PUBKEY_EVEN || presig65[0] == SECP256K1_TAG_PUBKEY_ODD); + ARG_CHECK(sig64 != NULL); + + /* s0 */ + secp256k1_scalar_set_b32(&s0, &presig65[33], &overflow); + if (overflow) { + return 0; + } + + /* s */ + secp256k1_scalar_set_b32(&s, &sig64[32], &overflow); + if (overflow) { + return 0; + } + + if (presig65[0] == SECP256K1_TAG_PUBKEY_EVEN) { + secp256k1_scalar_negate(&s0, &s0); + } else if (presig65[0] == SECP256K1_TAG_PUBKEY_ODD) { + secp256k1_scalar_negate(&s, &s); + } + secp256k1_scalar_add(&t, &s0, &s); + + secp256k1_scalar_get_b32(secadaptor, &t); + secp256k1_scalar_clear(&s); + secp256k1_scalar_clear(&t); + + return 1; +} + +#endif diff --git a/src/modules/schnorr_adaptor/tests_impl.h b/src/modules/schnorr_adaptor/tests_impl.h new file mode 100644 index 000000000..239cefb19 --- /dev/null +++ b/src/modules/schnorr_adaptor/tests_impl.h @@ -0,0 +1,962 @@ +/********************************************************************** + * Copyright (c) 2023 Zhe Pang * + * Distributed under the MIT software license, see the accompanying * + * file COPYING or http://www.opensource.org/licenses/mit-license.php.* + **********************************************************************/ + +#ifndef SECP256K1_MODULE_SCHNORR_ADAPTOR_TESTS_H +#define SECP256K1_MODULE_SCHNORR_ADAPTOR_TESTS_H + +#include "../../../include/secp256k1_schnorr_adaptor.h" + +/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many + * bytes) changes the hash function + */ +static void nonce_function_schnorr_adaptor_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes, size_t algolen) { + unsigned char nonces[2][32]; + CHECK(nonce_function_schnorr_adaptor(nonces[0], args[0], args[1], args[2], args[3], args[4], algolen, args[5]) == 1); + secp256k1_testrand_flip(args[n_flip], n_bytes); + CHECK(nonce_function_schnorr_adaptor(nonces[1], args[0], args[1], args[2], args[3], args[4], algolen, args[5]) == 1); + CHECK(secp256k1_memcmp_var(nonces[0], nonces[1], 32) != 0); +} + +static void run_nonce_function_schnorr_adaptor_tests(void) { + unsigned char tag[20] = "SchnorrAdaptor/nonce"; + unsigned char aux_tag[18] = "SchnorrAdaptor/aux"; + unsigned char algo[20] = "SchnorrAdaptor/nonce"; + size_t algolen = sizeof(algo); + secp256k1_sha256 sha; + secp256k1_sha256 sha_optimized; + secp256k1_scalar adaptor_scalar; + secp256k1_gej tj; + secp256k1_ge tg; + unsigned char nonce[32], nonce_z[32]; + unsigned char msg[32]; + unsigned char key[32]; + unsigned char t[32]; + unsigned char adaptor33[33]; + unsigned char pk[32]; + unsigned char aux_rand[32]; + unsigned char *args[6]; + int i; + size_t size = 33; + + /* Check that hash initialized by + * secp256k1_nonce_function_schnorr_adaptor_sha256_tagged has the expected + * state. */ + secp256k1_sha256_initialize_tagged(&sha, tag, sizeof(tag)); + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged(&sha_optimized); + test_sha256_eq(&sha, &sha_optimized); + + /* Check that hash initialized by + * secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux has the expected + * state. */ + secp256k1_sha256_initialize_tagged(&sha, aux_tag, sizeof(aux_tag)); + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux(&sha_optimized); + test_sha256_eq(&sha, &sha_optimized); + + secp256k1_testrand256(msg); + secp256k1_testrand256(key); + secp256k1_testrand256(t); + secp256k1_testrand256(pk); + secp256k1_testrand256(aux_rand); + + secp256k1_scalar_set_b32(&adaptor_scalar, t, NULL); + secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &tj, &adaptor_scalar); + secp256k1_ge_set_gej(&tg, &tj); + CHECK(secp256k1_eckey_pubkey_serialize(&tg, adaptor33, &size, 1) == 1); + + /* Check that a bitflip in an argument results in different nonces. */ + args[0] = msg; + args[1] = key; + args[2] = adaptor33; + args[3] = pk; + args[4] = algo; + args[5] = aux_rand; + for (i = 0; i < COUNT; i++) { + nonce_function_schnorr_adaptor_bitflip(args, 0, 32, algolen); + nonce_function_schnorr_adaptor_bitflip(args, 1, 32, algolen); + nonce_function_schnorr_adaptor_bitflip(args, 2, 33, algolen); + nonce_function_schnorr_adaptor_bitflip(args, 3, 32, algolen); + /* Flip algo special case "SchnorrAdaptor/nonce" */ + nonce_function_schnorr_adaptor_bitflip(args, 4, algolen, algolen); + /* Flip algo again */ + nonce_function_schnorr_adaptor_bitflip(args, 4, algolen, algolen); + nonce_function_schnorr_adaptor_bitflip(args, 5, 32, algolen); + } + + /* NULL algo is disallowed */ + CHECK(nonce_function_schnorr_adaptor(nonce, msg, key, t, pk, NULL, 0, NULL) == 0); + CHECK(nonce_function_schnorr_adaptor(nonce, msg, key, t, pk, algo, algolen, NULL) == 1); + /* Other algo is fine */ + secp256k1_testrand_bytes_test(algo, algolen); + CHECK(nonce_function_schnorr_adaptor(nonce, msg, key, t, pk, algo, algolen, NULL) == 1); + + for (i = 0; i < COUNT; i++) { + unsigned char nonce2[32]; + size_t algolen_tmp; + + /* Different algolen gives different nonce */ + uint32_t offset = secp256k1_testrand_int(algolen - 1); + algolen_tmp = (algolen + offset) % algolen; + CHECK(nonce_function_schnorr_adaptor(nonce2, msg, key, t, pk, algo, algolen_tmp, NULL) == 1); + CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0); + } + + /* NULL aux_rand argument is allowed, and identical to passing all zero aux_rand. */ + memset(aux_rand, 0, 32); + CHECK(nonce_function_schnorr_adaptor(nonce_z, msg, key, t, pk, algo, algolen, &aux_rand) == 1); + CHECK(nonce_function_schnorr_adaptor(nonce, msg, key, t, pk, algo, algolen, NULL) == 1); + CHECK(secp256k1_memcmp_var(nonce_z, nonce, 32) == 0); +} + +static void test_schnorr_adaptor_api(void) { + unsigned char sk[32]; + unsigned char msg[32]; + unsigned char secadaptor[32]; + unsigned char adaptor33[33] = { + 0x02, 0xC6, 0x04, 0x7F, 0x94, 0x41, 0xED, 0x7D, + 0x6D, 0x30, 0x45, 0x40, 0x6E, 0x95, 0xC0, 0x7C, + 0xD8, 0x5C, 0x77, 0x8E, 0x4B, 0x8C, 0xEF, 0x3C, + 0xA7, 0xAB, 0xAC, 0x09, 0xB9, 0x5C, 0x70, 0x9E, 0xE5 + }; + secp256k1_keypair keypair; + secp256k1_keypair invalid_keypair = {{ 0 }}; + secp256k1_xonly_pubkey pk; + secp256k1_xonly_pubkey zero_pk; + unsigned char sig[65]; + unsigned char sig64[64]; + secp256k1_pubkey t; + secp256k1_pubkey t2; + unsigned char extracted_secadaptor[32]; + + /** setup **/ + + secp256k1_testrand256(sk); + secp256k1_testrand256(msg); + secp256k1_testrand256(secadaptor); + CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1); + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair) == 1); + memset(&zero_pk, 0, sizeof(zero_pk)); + secp256k1_ec_pubkey_parse(CTX, &t, adaptor33, 33); + + /** main test body **/ + CHECK_ILLEGAL(STATIC_CTX, secp256k1_schnorr_adaptor_presign(STATIC_CTX, sig, msg, &keypair, &t, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_presign(CTX, NULL, msg, &keypair, &t, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_presign(CTX, sig, NULL, &keypair, &t, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_presign(CTX, sig, msg, NULL, &t, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_presign(CTX, sig, msg, &keypair, NULL, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_presign(CTX, sig, msg, &invalid_keypair, &t, NULL)); + + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig, msg, &keypair, &t, NULL) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t2, sig, msg, &pk) == 1); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract(CTX, NULL, sig, msg, &pk)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract(CTX, &t2, NULL, msg, &pk)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract(CTX, &t2, sig, NULL, &pk)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract(CTX, &t2, sig, msg, NULL)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract(CTX, &t2, sig, msg, &zero_pk)); + + CHECK(secp256k1_schnorr_adaptor_adapt(CTX, sig64, sig, secadaptor) == 1); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_adapt(CTX, NULL, sig, secadaptor)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_adapt(CTX, sig64, NULL, secadaptor)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_adapt(CTX, sig64, sig, NULL)); + + CHECK(secp256k1_schnorr_adaptor_adapt(CTX, sig64, sig, secadaptor) == 1); + CHECK(secp256k1_schnorr_adaptor_extract_sec(CTX, extracted_secadaptor, sig, sig64) == 1); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract_sec(CTX, NULL, sig, sig64)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract_sec(CTX, extracted_secadaptor, NULL, sig64)); + CHECK_ILLEGAL(CTX, secp256k1_schnorr_adaptor_extract_sec(CTX, extracted_secadaptor, sig, NULL)); + +} + +/* Helper function for schnorr_adaptor_vectors + * Signs the message and checks that it's the same as expected_sig. */ +static void test_schnorr_adaptor_vectors_check_presigning(const unsigned char *sk, const unsigned char *pk_serialized, const unsigned char *aux_rand, const unsigned char *msg32, const unsigned char *adaptor33, const unsigned char *expected_sig) { + unsigned char sig[65]; + secp256k1_pubkey t; + secp256k1_keypair keypair; + secp256k1_xonly_pubkey pk, pk_expected; + secp256k1_pubkey adaptor; + secp256k1_ec_pubkey_parse(CTX, &adaptor, adaptor33, 33); + + CHECK(secp256k1_keypair_create(CTX, &keypair, sk)); + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig, msg32, &keypair, &adaptor, aux_rand)); + CHECK(secp256k1_memcmp_var(sig, expected_sig, 65) == 0); + + CHECK(secp256k1_xonly_pubkey_parse(CTX, &pk_expected, pk_serialized)); + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair)); + CHECK(secp256k1_memcmp_var(&pk, &pk_expected, sizeof(pk)) == 0); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig, msg32, &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor) == 0); +} + +/* Helper function for schnorr_adaptor_vectors + * Extracts the adaptor point and checks if it returns the same value as expected. */ +static void test_schnorr_adaptor_vectors_check_extract(const unsigned char *pk_serialized, const unsigned char *msg32, const unsigned char *sig, const unsigned char *expected_t, int expected) { + secp256k1_xonly_pubkey pk; + secp256k1_pubkey t; + unsigned char t33[33]; + secp256k1_ge adaptor; + size_t size = 33; + + CHECK(secp256k1_xonly_pubkey_parse(CTX, &pk, pk_serialized)); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig, msg32, &pk)); + secp256k1_pubkey_load(CTX, &adaptor, &t); + secp256k1_eckey_pubkey_serialize(&adaptor, t33, &size, 1); + CHECK(expected == (secp256k1_memcmp_var(t33, expected_t, 33) == 0)); + } + +/* Helper function for schnorr_adaptor_vectors + * Adapt a Schnorr adptor signature to a valid Schnorr signature + * and checks if it is the same as expected_sig64. */ +static void test_schnorr_adaptor_vectors_check_adapt(const unsigned char *sig, const unsigned char *secadaptor, const unsigned char *expected_sig64, int expected) { + unsigned char sig64[64]; + + CHECK(secp256k1_schnorr_adaptor_adapt(CTX, sig64, sig, secadaptor)); + CHECK(expected == (secp256k1_memcmp_var(sig64, expected_sig64, 64) == 0)); +} + +/* Helper function for schnorr_adaptor_vectors + * Extract adaptor from a Schnorr adptor signature and a Schnorr signature + * and checks if it is the same as expected_secadaptor. */ +static void test_schnorr_adaptor_vectors_check_extract_sec(const unsigned char *sig, const unsigned char *sig64, const unsigned char *expected_secadaptor, int expected) { + unsigned char secadaptor[32]; + + CHECK(secp256k1_schnorr_adaptor_extract_sec(CTX, secadaptor, sig, sig64)); + CHECK(expected == (secp256k1_memcmp_var(secadaptor, expected_secadaptor, 32) == 0)); +} + +/* Test vectors, see https://github.com/ZhePang/Python_Specification_for_Schnorr_Adaptor */ +static void test_schnorr_adaptor_vectors(void) { + { + /* Test vector 0 */ + const unsigned char sk[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03 + }; + const unsigned char pk[32] = { + 0xF9, 0x30, 0x8A, 0x01, 0x92, 0x58, 0xC3, 0x10, + 0x49, 0x34, 0x4F, 0x85, 0xF8, 0x9D, 0x52, 0x29, + 0xB5, 0x31, 0xC8, 0x45, 0x83, 0x6F, 0x99, 0xB0, + 0x86, 0x01, 0xF1, 0x13, 0xBC, 0xE0, 0x36, 0xF9 + }; + unsigned char aux_rand[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + const unsigned char msg[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + const unsigned char t[33] = { + 0x02, 0xC6, 0x04, 0x7F, 0x94, 0x41, 0xED, 0x7D, + 0x6D, 0x30, 0x45, 0x40, 0x6E, 0x95, 0xC0, 0x7C, + 0xD8, 0x5C, 0x77, 0x8E, 0x4B, 0x8C, 0xEF, 0x3C, + 0xA7, 0xAB, 0xAC, 0x09, 0xB9, 0x5C, 0x70, 0x9E, 0xE5 + }; + const unsigned char t32[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 + }; + const unsigned char sig[65] = { + 0x03, 0x61, 0x79, 0xDB, 0xF3, 0xE1, 0x32, 0x07, + 0x85, 0x3F, 0x88, 0x0C, 0x7A, 0x7A, 0x85, 0xEC, + 0x67, 0x8B, 0xAD, 0x64, 0xB8, 0x97, 0xF1, 0x08, + 0xD4, 0x76, 0x43, 0x8A, 0xC4, 0xA9, 0x32, 0xEE, + 0x94, 0x97, 0xCC, 0x73, 0xB8, 0xC3, 0x51, 0xF1, + 0x89, 0xB9, 0xD4, 0xFD, 0xE8, 0x93, 0xE3, 0x82, + 0x0D, 0x4B, 0xFF, 0x7F, 0x49, 0xD4, 0xBE, 0x1F, + 0x8B, 0x02, 0xCB, 0x80, 0x8C, 0xD3, 0x19, 0x23, 0xA0 + }; + const unsigned char sig64[64] = { + 0x61, 0x79, 0xDB, 0xF3, 0xE1, 0x32, 0x07, 0x85, + 0x3F, 0x88, 0x0C, 0x7A, 0x7A, 0x85, 0xEC, 0x67, + 0x8B, 0xAD, 0x64, 0xB8, 0x97, 0xF1, 0x08, 0xD4, + 0x76, 0x43, 0x8A, 0xC4, 0xA9, 0x32, 0xEE, 0x94, + 0x97, 0xCC, 0x73, 0xB8, 0xC3, 0x51, 0xF1, 0x89, + 0xB9, 0xD4, 0xFD, 0xE8, 0x93, 0xE3, 0x82, 0x0D, + 0x4B, 0xFF, 0x7F, 0x49, 0xD4, 0xBE, 0x1F, 0x8B, + 0x02, 0xCB, 0x80, 0x8C, 0xD3, 0x19, 0x23, 0x9E + }; + test_schnorr_adaptor_vectors_check_presigning(sk, pk, aux_rand, msg, t, sig); + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 1); + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 1 */ + const unsigned char sk[32] = { + 0xB7, 0xE1, 0x51, 0x62, 0x8A, 0xED, 0x2A, 0x6A, + 0xBF, 0x71, 0x58, 0x80, 0x9C, 0xF4, 0xF3, 0xC7, + 0x62, 0xE7, 0x16, 0x0F, 0x38, 0xB4, 0xDA, 0x56, + 0xA7, 0x84, 0xD9, 0x04, 0x51, 0x90, 0xCF, 0xEF + }; + const unsigned char pk[32] = { + 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F, + 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE, + 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8, + 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59 + }; + unsigned char aux_rand[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 + }; + const unsigned char msg[32] = { + 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3, + 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44, + 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0, + 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89 + }; + const unsigned char t[33] = { + 0x02, 0xE4, 0x93, 0xDB, 0xF1, 0xC1, 0x0D, 0x80, + 0xF3, 0x58, 0x1E, 0x49, 0x04, 0x93, 0x0B, 0x14, + 0x04, 0xCC, 0x6C, 0x13, 0x90, 0x0E, 0xE0, 0x75, + 0x84, 0x74, 0xFA, 0x94, 0xAB, 0xE8, 0xC4, 0xCD, 0x13 + }; + const unsigned char t32[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 + }; + const unsigned char sig[65] = { + 0x02, 0x42, 0x48, 0xD2, 0xD5, 0xD2, 0xF0, 0x31, + 0xA1, 0x88, 0xD0, 0x3F, 0x1D, 0x54, 0x3F, 0x6D, + 0x73, 0x74, 0x5E, 0xB8, 0xEF, 0x20, 0x5F, 0x20, + 0xC5, 0x23, 0x4C, 0x15, 0xD0, 0x21, 0x3A, 0xD9, + 0x5E, 0xC9, 0x63, 0x5F, 0x29, 0xE7, 0x81, 0x32, + 0xEF, 0x2A, 0xA6, 0xD2, 0xFC, 0x8B, 0x4E, 0x6F, + 0x67, 0x3A, 0x2F, 0x6D, 0x52, 0x85, 0xD3, 0x31, + 0xE7, 0x99, 0x4C, 0x23, 0x24, 0xB0, 0x9C, 0xEB, 0x1D + }; + const unsigned char sig64[64] = { + 0x42, 0x48, 0xD2, 0xD5, 0xD2, 0xF0, 0x31, 0xA1, + 0x88, 0xD0, 0x3F, 0x1D, 0x54, 0x3F, 0x6D, 0x73, + 0x74, 0x5E, 0xB8, 0xEF, 0x20, 0x5F, 0x20, 0xC5, + 0x23, 0x4C, 0x15, 0xD0, 0x21, 0x3A, 0xD9, 0x5E, + 0xC9, 0x63, 0x5F, 0x29, 0xE7, 0x81, 0x32, 0xEF, + 0x2A, 0xA6, 0xD2, 0xFC, 0x8B, 0x4E, 0x6F, 0x67, + 0x3A, 0x2F, 0x6D, 0x52, 0x85, 0xD3, 0x31, 0xE7, + 0x99, 0x4C, 0x23, 0x24, 0xB0, 0x9C, 0xEB, 0x21 + }; + test_schnorr_adaptor_vectors_check_presigning(sk, pk, aux_rand, msg, t, sig); + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 1); + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 2 */ + const unsigned char sk[32] = { + 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, + 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, + 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, + 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x14, 0xE5, 0xC9 + }; + const unsigned char pk[32] = { + 0xDD, 0x30, 0x8A, 0xFE, 0xC5, 0x77, 0x7E, 0x13, + 0x12, 0x1F, 0xA7, 0x2B, 0x9C, 0xC1, 0xB7, 0xCC, + 0x01, 0x39, 0x71, 0x53, 0x09, 0xB0, 0x86, 0xC9, + 0x60, 0xE1, 0x8F, 0xD9, 0x69, 0x77, 0x4E, 0xB8 + }; + unsigned char aux_rand[32] = { + 0xC8, 0x7A, 0xA5, 0x38, 0x24, 0xB4, 0xD7, 0xAE, + 0x2E, 0xB0, 0x35, 0xA2, 0xB5, 0xBB, 0xBC, 0xCC, + 0x08, 0x0E, 0x76, 0xCD, 0xC6, 0xD1, 0x69, 0x2C, + 0x4B, 0x0B, 0x62, 0xD7, 0x98, 0xE6, 0xD9, 0x04 + }; + const unsigned char msg[32] = { + 0x7E, 0x2D, 0x58, 0xD8, 0xB3, 0xBC, 0xDF, 0x1A, + 0xBA, 0xDE, 0xC7, 0x82, 0x90, 0x54, 0xF9, 0x0D, + 0xDA, 0x98, 0x05, 0xAA, 0xB5, 0x6C, 0x77, 0x33, + 0x30, 0x24, 0xB9, 0xD0, 0xA5, 0x08, 0xB7, 0x5C + }; + const unsigned char t[33] = { + 0x03, 0xFF, 0xF9, 0x7B, 0xD5, 0x75, 0x5E, 0xEE, + 0xA4, 0x20, 0x45, 0x3A, 0x14, 0x35, 0x52, 0x35, + 0xD3, 0x82, 0xF6, 0x47, 0x2F, 0x85, 0x68, 0xA1, + 0x8B, 0x2F, 0x05, 0x7A, 0x14, 0x60, 0x29, 0x75, 0x56 + }; + const unsigned char t32[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06 + }; + const unsigned char sig[65] = { + 0x03, 0xFC, 0xED, 0xA2, 0x9D, 0x85, 0x32, 0xD2, + 0xC1, 0x99, 0x70, 0xCC, 0x3F, 0x8C, 0x67, 0x91, + 0xCC, 0x41, 0xE3, 0x05, 0x59, 0xC6, 0xF6, 0x76, + 0x44, 0x46, 0x3B, 0x5A, 0x71, 0xC8, 0xE9, 0x9A, + 0x90, 0x5B, 0xA4, 0xDF, 0x9F, 0xA8, 0x76, 0xCC, + 0x4D, 0x1F, 0x8E, 0x5D, 0x74, 0x72, 0xED, 0x3D, + 0xB3, 0x59, 0x44, 0xD1, 0xC1, 0x6E, 0x92, 0x12, + 0xB4, 0xC4, 0x74, 0xCB, 0xDA, 0x9C, 0x1A, 0xA3, 0x22 + }; + const unsigned char sig64[64] = { + 0xFC, 0xED, 0xA2, 0x9D, 0x85, 0x32, 0xD2, 0xC1, + 0x99, 0x70, 0xCC, 0x3F, 0x8C, 0x67, 0x91, 0xCC, + 0x41, 0xE3, 0x05, 0x59, 0xC6, 0xF6, 0x76, 0x44, + 0x46, 0x3B, 0x5A, 0x71, 0xC8, 0xE9, 0x9A, 0x90, + 0x5B, 0xA4, 0xDF, 0x9F, 0xA8, 0x76, 0xCC, 0x4D, + 0x1F, 0x8E, 0x5D, 0x74, 0x72, 0xED, 0x3D, 0xB3, + 0x59, 0x44, 0xD1, 0xC1, 0x6E, 0x92, 0x12, 0xB4, + 0xC4, 0x74, 0xCB, 0xDA, 0x9C, 0x1A, 0xA3, 0x1C + }; + test_schnorr_adaptor_vectors_check_presigning(sk, pk, aux_rand, msg, t, sig); + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 1); + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 3 */ + const unsigned char sk[32] = { + 0x0B, 0x43, 0x2B, 0x26, 0x77, 0x93, 0x73, 0x81, + 0xAE, 0xF0, 0x5B, 0xB0, 0x2A, 0x66, 0xEC, 0xD0, + 0x12, 0x77, 0x30, 0x62, 0xCF, 0x3F, 0xA2, 0x54, + 0x9E, 0x44, 0xF5, 0x8E, 0xD2, 0x40, 0x17, 0x10 + }; + const unsigned char pk[32] = { + 0x25, 0xD1, 0xDF, 0xF9, 0x51, 0x05, 0xF5, 0x25, + 0x3C, 0x40, 0x22, 0xF6, 0x28, 0xA9, 0x96, 0xAD, + 0x3A, 0x0D, 0x95, 0xFB, 0xF2, 0x1D, 0x46, 0x8A, + 0x1B, 0x33, 0xF8, 0xC1, 0x60, 0xD8, 0xF5, 0x17 + }; + unsigned char aux_rand[32] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + const unsigned char msg[32] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF + }; + const unsigned char t[33] = { + 0x02, 0xC6, 0x04, 0x7F, 0x94, 0x41, 0xED, 0x7D, + 0x6D, 0x30, 0x45, 0x40, 0x6E, 0x95, 0xC0, 0x7C, + 0xD8, 0x5C, 0x77, 0x8E, 0x4B, 0x8C, 0xEF, 0x3C, + 0xA7, 0xAB, 0xAC, 0x09, 0xB9, 0x5C, 0x70, 0x9E, 0xE5 + }; + const unsigned char t32[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 + }; + const unsigned char sig[65] = { + 0x02, 0xA3, 0x96, 0xB0, 0x57, 0x1A, 0xD6, 0xC1, + 0xBC, 0x0A, 0x06, 0xA2, 0xDA, 0xFF, 0x1F, 0xB5, + 0xAB, 0x30, 0x60, 0x11, 0x86, 0xAA, 0x70, 0xE7, + 0x50, 0x8C, 0x84, 0xC2, 0xD6, 0x5E, 0xA6, 0x91, + 0x84, 0x05, 0x86, 0x63, 0x09, 0xF7, 0x63, 0x0F, + 0xA7, 0x61, 0x5D, 0x4E, 0xD0, 0xFB, 0x44, 0x20, + 0x8F, 0x35, 0x57, 0x02, 0x53, 0x91, 0xBD, 0xB7, + 0x22, 0x99, 0x2E, 0xD5, 0x62, 0xB2, 0x72, 0xDD, 0x25 + }; + const unsigned char sig64[64] = { + 0xA3, 0x96, 0xB0, 0x57, 0x1A, 0xD6, 0xC1, 0xBC, + 0x0A, 0x06, 0xA2, 0xDA, 0xFF, 0x1F, 0xB5, 0xAB, + 0x30, 0x60, 0x11, 0x86, 0xAA, 0x70, 0xE7, 0x50, + 0x8C, 0x84, 0xC2, 0xD6, 0x5E, 0xA6, 0x91, 0x84, + 0x05, 0x86, 0x63, 0x09, 0xF7, 0x63, 0x0F, 0xA7, + 0x61, 0x5D, 0x4E, 0xD0, 0xFB, 0x44, 0x20, 0x8F, + 0x35, 0x57, 0x02, 0x53, 0x91, 0xBD, 0xB7, 0x22, + 0x99, 0x2E, 0xD5, 0x62, 0xB2, 0x72, 0xDD, 0x27 + }; + test_schnorr_adaptor_vectors_check_presigning(sk, pk, aux_rand, msg, t, sig); + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 1); + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 4 */ + const unsigned char sk[32] = { + 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, + 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, + 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, + 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x14, 0xE5, 0xC9 + }; + const unsigned char pk[32] = { + 0xDD, 0x30, 0x8A, 0xFE, 0xC5, 0x77, 0x7E, 0x13, + 0x12, 0x1F, 0xA7, 0x2B, 0x9C, 0xC1, 0xB7, 0xCC, + 0x01, 0x39, 0x71, 0x53, 0x09, 0xB0, 0x86, 0xC9, + 0x60, 0xE1, 0x8F, 0xD9, 0x69, 0x77, 0x4E, 0xB8 + }; + unsigned char aux_rand[32] = { + 0xC8, 0x7A, 0xA5, 0x38, 0x24, 0xB4, 0xD7, 0xAE, + 0x2E, 0xB0, 0x35, 0xA2, 0xB5, 0xBB, 0xBC, 0xCC, + 0x08, 0x0E, 0x76, 0xCD, 0xC6, 0xD1, 0x69, 0x2C, + 0x4B, 0x0B, 0x62, 0xD7, 0x98, 0xE6, 0xD9, 0x04 + }; + const unsigned char msg[32] = { + 0x7E, 0x2D, 0x58, 0xD8, 0xB3, 0xBC, 0xDF, 0x1A, + 0xBA, 0xDE, 0xC7, 0x82, 0x90, 0x54, 0xF9, 0x0D, + 0xDA, 0x98, 0x05, 0xAA, 0xB5, 0x6C, 0x77, 0x33, + 0x30, 0x24, 0xB9, 0xD0, 0xA5, 0x08, 0xB7, 0x5C + }; + const unsigned char t[33] = { + 0x03, 0x5C, 0xBD, 0xF0, 0x64, 0x6E, 0x5D, 0xB4, + 0xEA, 0xA3, 0x98, 0xF3, 0x65, 0xF2, 0xEA, 0x7A, + 0x0E, 0x3D, 0x41, 0x9B, 0x7E, 0x03, 0x30, 0xE3, + 0x9C, 0xE9, 0x2B, 0xDD, 0xED, 0xCA, 0xC4, 0xF9, 0xBC + }; + const unsigned char t32[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07 + }; + const unsigned char sig[65] = { + 0x03, 0xA7, 0xFC, 0x93, 0x12, 0xDD, 0xAF, 0xF8, + 0xC9, 0x53, 0x3F, 0x9F, 0x73, 0x50, 0x04, 0x21, + 0xD4, 0xEB, 0xB7, 0x36, 0x3E, 0xFE, 0xA9, 0xE2, + 0x17, 0xCA, 0x70, 0xE5, 0xFE, 0xA1, 0xDC, 0xE4, + 0x3D, 0xC9, 0x51, 0xFE, 0x64, 0x5F, 0x33, 0x9C, + 0xC5, 0x80, 0x02, 0x0F, 0x7E, 0xFA, 0x3D, 0x04, + 0xAB, 0xBE, 0x63, 0xC9, 0x13, 0xF7, 0xD1, 0x59, + 0x66, 0x21, 0x4F, 0x63, 0x4A, 0xE0, 0x64, 0xE2, 0x21 + }; + const unsigned char sig64[64] = { + 0xA7, 0xFC, 0x93, 0x12, 0xDD, 0xAF, 0xF8, 0xC9, + 0x53, 0x3F, 0x9F, 0x73, 0x50, 0x04, 0x21, 0xD4, + 0xEB, 0xB7, 0x36, 0x3E, 0xFE, 0xA9, 0xE2, 0x17, + 0xCA, 0x70, 0xE5, 0xFE, 0xA1, 0xDC, 0xE4, 0x3D, + 0xC9, 0x51, 0xFE, 0x64, 0x5F, 0x33, 0x9C, 0xC5, + 0x80, 0x02, 0x0F, 0x7E, 0xFA, 0x3D, 0x04, 0xAB, + 0xBE, 0x63, 0xC9, 0x13, 0xF7, 0xD1, 0x59, 0x66, + 0x21, 0x4F, 0x63, 0x4A, 0xE0, 0x64, 0xE2, 0x1A + }; + test_schnorr_adaptor_vectors_check_presigning(sk, pk, aux_rand, msg, t, sig); + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 1); + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 5 */ + /* Verification test */ + const unsigned char pk[32] = { + 0xEE, 0xFD, 0xEA, 0x4C, 0xDB, 0x67, 0x77, 0x50, + 0xA4, 0x20, 0xFE, 0xE8, 0x07, 0xEA, 0xCF, 0x21, + 0xEB, 0x98, 0x98, 0xAE, 0x79, 0xB9, 0x76, 0x87, + 0x66, 0xE4, 0xFA, 0xA0, 0x4A, 0x2D, 0x4A, 0x34 + }; + secp256k1_xonly_pubkey pk_parsed; + CHECK(!secp256k1_xonly_pubkey_parse(CTX, &pk_parsed, pk)); + }; + { + /* Test vector 6 */ + /* Verification test */ + const unsigned char pk[32] = { + 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F, + 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE, + 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8, + 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59 + }; + const unsigned char msg[32] = { + 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3, + 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44, + 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0, + 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89 + }; + const unsigned char t[33] = { + 0x02, 0x2F, 0x8B, 0xDE, 0x4D, 0x1A, 0x07, 0x20, + 0x93, 0x55, 0xB4, 0xA7, 0x25, 0x0A, 0x5C, 0x51, + 0x28, 0xE8, 0x8B, 0x84, 0xBD, 0xDC, 0x61, 0x9A, + 0xB7, 0xCB, 0xA8, 0xD5, 0x69, 0xB2, 0x40, 0xEF, 0xE4 + }; + const unsigned char sig[65] = { + 0x02, 0x36, 0xF7, 0xE1, 0x79, 0x5A, 0x04, 0x98, + 0x76, 0x54, 0x4B, 0x25, 0x4C, 0xEA, 0x10, 0xD1, + 0x8E, 0xDE, 0x69, 0x67, 0xD1, 0x4A, 0x9F, 0x36, + 0x5D, 0x84, 0xFD, 0xF4, 0x38, 0x4C, 0x97, 0x7C, + 0x6A, 0x19, 0xC6, 0x05, 0x0F, 0x5C, 0x5A, 0xF5, + 0x61, 0x72, 0x0D, 0x2B, 0x0B, 0xA0, 0x6F, 0x5E, + 0xD8, 0x36, 0xE7, 0x15, 0xF2, 0xD5, 0xDA, 0xE8, + 0x96, 0x4C, 0x7F, 0x9F, 0x17, 0xFD, 0x0C, 0xF0, 0x90 + }; + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 0); + }; + { + /* Test vector 7 */ + /* Verification test */ + const unsigned char pk[32] = { + 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F, + 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE, + 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8, + 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59 + }; + const unsigned char msg[32] = { + 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3, + 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44, + 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0, + 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89 + }; + const unsigned char t[33] = { + 0x02, 0xE4, 0x93, 0xDB, 0xF1, 0xC1, 0x0D, 0x80, + 0xF3, 0x58, 0x1E, 0x49, 0x04, 0x93, 0x0B, 0x14, + 0x04, 0xCC, 0x6C, 0x13, 0x90, 0x0E, 0xE0, 0x75, + 0x84, 0x74, 0xFA, 0x94, 0xAB, 0xE8, 0xC4, 0xCD, 0x13 + }; + const unsigned char sig[65] = { + 0x03, 0x40, 0x66, 0xF5, 0xBE, 0x7E, 0xD4, 0xB6, + 0xE0, 0x2E, 0xB3, 0x8B, 0x26, 0xFC, 0x0A, 0x8C, + 0xA8, 0x49, 0xA3, 0x2D, 0x7E, 0xCD, 0x64, 0x5B, + 0x49, 0xE4, 0x50, 0xFD, 0x5E, 0x06, 0x5A, 0x5E, + 0x77, 0xE4, 0x9E, 0xBB, 0x07, 0xC6, 0x37, 0xF9, + 0x99, 0x1C, 0xB2, 0x5A, 0x07, 0x7E, 0xE8, 0x46, + 0xE8, 0xA4, 0x50, 0xF7, 0xC7, 0x07, 0x0A, 0x86, + 0xC8, 0x58, 0xFF, 0xE6, 0x26, 0x9A, 0xF3, 0x53, 0xE1 + }; + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 0); + }; + { + /* Test vector 8 */ + /* Verification test */ + const unsigned char pk[32] = { + 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F, + 0x36, 0x18, 0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE, + 0x58, 0xFE, 0xAE, 0x1D, 0xA2, 0xDE, 0xCE, 0xD8, + 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59 + }; + const unsigned char msg[32] = { + 0x24, 0x3F, 0x6A, 0x88, 0x85, 0xA3, 0x08, 0xD3, + 0x13, 0x19, 0x8A, 0x2E, 0x03, 0x70, 0x73, 0x44, + 0xA4, 0x09, 0x38, 0x22, 0x29, 0x9F, 0x31, 0xD0, + 0x08, 0x2E, 0xFA, 0x98, 0xEC, 0x4E, 0x6C, 0x89 + }; + const unsigned char t[33] = { + 0x02, 0x2F, 0x8B, 0xDE, 0x4D, 0x1A, 0x07, 0x20, + 0x93, 0x55, 0xB4, 0xA7, 0x25, 0x0A, 0x5C, 0x51, + 0x28, 0xE8, 0x8B, 0x84, 0xBD, 0xDC, 0x61, 0x9A, + 0xB7, 0xCB, 0xA8, 0xD5, 0x69, 0xB2, 0x40, 0xEF, 0xE4 + }; + const unsigned char sig[65] = { + 0x03, 0x14, 0xDE, 0x03, 0xC2, 0x84, 0x36, 0xAB, + 0x9A, 0x24, 0x4F, 0xE3, 0x0F, 0x60, 0x92, 0x12, + 0x9B, 0xB1, 0xFF, 0x99, 0x73, 0x0F, 0xB7, 0xE4, + 0xEA, 0x59, 0x2B, 0x5B, 0xB2, 0xC5, 0xDD, 0xE4, + 0xF9, 0xED, 0x14, 0x0B, 0xA5, 0xE3, 0x2F, 0x25, + 0x0A, 0xE1, 0xCE, 0xF8, 0xE2, 0xC5, 0xD9, 0x75, + 0x63, 0xDC, 0x77, 0x66, 0x35, 0x83, 0xE5, 0xEC, + 0xA4, 0x0F, 0xFE, 0xC9, 0x79, 0xF5, 0xF3, 0x73, 0x8E + }; + test_schnorr_adaptor_vectors_check_extract(pk, msg, sig, t, 0); + }; + { + /* Test vector 9 */ + /* Verification test */ + const unsigned char sig[65] = { + 0x02, 0xEE, 0xFD, 0xEA, 0x4C, 0xDB, 0x67, 0x77, + 0x50, 0xA4, 0x20, 0xFE, 0xE8, 0x07, 0xEA, 0xCF, + 0x21, 0xEB, 0x98, 0x98, 0xAE, 0x79, 0xB9, 0x76, + 0x87, 0x66, 0xE4, 0xFA, 0xA0, 0x4A, 0x2D, 0x4A, + 0x34, 0xED, 0x14, 0x0B, 0xA5, 0xE3, 0x2F, 0x25, + 0x0A, 0xE1, 0xCE, 0xF8, 0xE2, 0xC5, 0xD9, 0x75, + 0x63, 0xDC, 0x77, 0x66, 0x35, 0x83, 0xE5, 0xEC, + 0xA4, 0x0F, 0xFE, 0xC9, 0x79, 0xF5, 0xF3, 0x73, 0x8E + }; + secp256k1_ge r0; + CHECK(!secp256k1_eckey_pubkey_parse(&r0, sig, 33)); + }; + { + /* Test vector 10 */ + /* Adaptor Extraction and Adapting test */ + const unsigned char t32[32] = { + 0xA7, 0xA2, 0x48, 0x01, 0x1E, 0x72, 0x03, 0x81, + 0x68, 0xFA, 0xAB, 0x16, 0x76, 0x1A, 0xA4, 0x7E, + 0x54, 0x04, 0x0A, 0xF2, 0x0E, 0x3F, 0x36, 0xC1, + 0x6C, 0xE2, 0x73, 0x53, 0xCE, 0xAD, 0xF5, 0x11 + }; + const unsigned char sig[65] = { + 0x02, 0xFA, 0x88, 0x46, 0x5F, 0xAF, 0x40, 0x88, + 0x31, 0x04, 0xFA, 0x0B, 0x77, 0xC1, 0x99, 0x0C, + 0xC1, 0xB2, 0xD7, 0x74, 0x56, 0xD1, 0x51, 0x86, + 0xCE, 0xF2, 0x40, 0xF6, 0x19, 0xE0, 0xFD, 0xED, + 0x82, 0xAA, 0x34, 0x62, 0xF8, 0x66, 0x63, 0x2C, + 0xB5, 0xAA, 0xDA, 0x9C, 0xB0, 0x43, 0x35, 0x93, + 0x75, 0xEC, 0x5C, 0xF2, 0x66, 0x6B, 0xFA, 0x71, + 0xDE, 0x84, 0xA5, 0x46, 0x83, 0x59, 0x9C, 0xD1, 0x5D + }; + const unsigned char sig64[64] = { + 0xFA, 0x88, 0x46, 0x5F, 0xAF, 0x40, 0x88, 0x31, + 0x04, 0xFA, 0x0B, 0x77, 0xC1, 0x99, 0x0C, 0xC1, + 0xB2, 0xD7, 0x74, 0x56, 0xD1, 0x51, 0x86, 0xCE, + 0xF2, 0x40, 0xF6, 0x19, 0xE0, 0xFD, 0xED, 0x82, + 0x51, 0xD6, 0xAA, 0xF9, 0x84, 0xD5, 0x30, 0x37, + 0x13, 0xD5, 0x47, 0xC6, 0xB9, 0x50, 0x37, 0xF5, + 0x85, 0xB2, 0x20, 0x71, 0xCA, 0xF1, 0x08, 0x64, + 0x31, 0xB5, 0x5B, 0x4A, 0x58, 0x14, 0x85, 0x2D + }; + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 1); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 1); + }; + { + /* Test vector 11 */ + /* Adaptor Extraction and Adapting test */ + const unsigned char t32[32] = { + 0xE3, 0x23, 0x95, 0xDA, 0x56, 0xFB, 0x14, 0x01, + 0xB9, 0x67, 0x0B, 0x61, 0xAE, 0x56, 0x40, 0x7B, + 0x7E, 0x22, 0xEF, 0x7F, 0xAE, 0x0A, 0xE4, 0xE2, + 0x6B, 0x38, 0x80, 0xB5, 0xCC, 0xFB, 0x3C, 0x97 + }; + const unsigned char sig[65] = { + 0x02, 0x2D, 0xB0, 0x2D, 0x30, 0x37, 0x63, 0xF5, + 0x12, 0xCB, 0x83, 0x02, 0x5E, 0x25, 0x16, 0x75, + 0xAB, 0xB8, 0x08, 0x82, 0x94, 0xFA, 0x06, 0x4D, + 0xE9, 0xB4, 0x63, 0x80, 0x69, 0x98, 0x2E, 0xC3, + 0x02, 0xD5, 0xC4, 0x2F, 0xB6, 0x91, 0x07, 0x17, + 0x38, 0xB1, 0x10, 0xDF, 0xB7, 0xDE, 0xBF, 0xAF, + 0x58, 0x8B, 0xC4, 0xB9, 0xDE, 0x93, 0x84, 0xCD, + 0xC4, 0x00, 0xD9, 0xDD, 0x78, 0x8E, 0xDD, 0xD0, 0xF4 + }; + const unsigned char sig64[64] = { + 0x2D, 0xB0, 0x2D, 0x30, 0x37, 0x63, 0xF5, 0x12, + 0xCB, 0x83, 0x02, 0x5E, 0x25, 0x16, 0x75, 0xAB, + 0xB8, 0x08, 0x82, 0x94, 0xFA, 0x06, 0x4D, 0xE9, + 0xB4, 0x63, 0x80, 0x69, 0x98, 0x2E, 0xC3, 0x02, + 0x0D, 0x5F, 0x66, 0x23, 0xC5, 0xF3, 0xFC, 0xC9, + 0x08, 0x56, 0x2B, 0xA9, 0xCF, 0x96, 0x91, 0x22, + 0xF2, 0x5E, 0x35, 0xA1, 0x1A, 0x86, 0x17, 0x1E, + 0x6A, 0x5E, 0xA3, 0x3D, 0x3E, 0x1D, 0x6B, 0xA3 + }; + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 0); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 0); + }; + { + /* Test vector 12 */ + /* Adaptor Extraction and Adapting test */ + const unsigned char t32[32] = { + 0xE3, 0x23, 0x95, 0xDA, 0x56, 0xFB, 0x14, 0x01, + 0xB9, 0x67, 0x0B, 0x61, 0xAE, 0x56, 0x40, 0x7B, + 0x7E, 0x22, 0xEF, 0x7F, 0xAE, 0x0A, 0xE4, 0xE2, + 0x6B, 0x38, 0x80, 0xB5, 0xCC, 0xFB, 0x3C, 0x97 + }; + const unsigned char sig[65] = { + 0x03, 0x2D, 0xB0, 0x2D, 0x30, 0x37, 0x63, 0xF5, + 0x12, 0xCB, 0x83, 0x02, 0x5E, 0x25, 0x16, 0x75, + 0xAB, 0xB8, 0x08, 0x82, 0x94, 0xFA, 0x06, 0x4D, + 0xE9, 0xB4, 0x63, 0x80, 0x69, 0x98, 0x2E, 0xC3, + 0x02, 0x2A, 0x3B, 0xD0, 0x49, 0x6E, 0xF8, 0xE8, + 0xC7, 0x4E, 0xEF, 0x20, 0x48, 0x21, 0x40, 0x50, + 0xA6, 0x2E, 0xEA, 0x23, 0x08, 0x1B, 0xC3, 0xD2, + 0x77, 0xBE, 0xF8, 0x81, 0x14, 0x41, 0x58, 0x70, 0x4D + }; + const unsigned char sig64[64] = { + 0x2D, 0xB0, 0x2D, 0x30, 0x37, 0x63, 0xF5, 0x12, + 0xCB, 0x83, 0x02, 0x5E, 0x25, 0x16, 0x75, 0xAB, + 0xB8, 0x08, 0x82, 0x94, 0xFA, 0x06, 0x4D, 0xE9, + 0xB4, 0x63, 0x80, 0x69, 0x98, 0x2E, 0xC3, 0x02, + 0x0D, 0x5F, 0x66, 0x23, 0xC5, 0xF3, 0xFC, 0xC9, + 0x08, 0x56, 0x2B, 0xA9, 0xCF, 0x96, 0x91, 0x22, + 0xF2, 0x5E, 0x35, 0xA1, 0x1A, 0x86, 0x17, 0x1E, + 0x6A, 0x5E, 0xA3, 0x3D, 0x3E, 0x1D, 0x6B, 0xA3 + }; + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 0); + test_schnorr_adaptor_vectors_check_extract_sec(sig, sig64, t32, 0); + }; + { + /* Test vector 13 */ + /* Adapting test */ + const unsigned char t32[32] = { + 0xE3, 0x23, 0x95, 0xDA, 0x56, 0xFB, 0x14, 0x01, + 0xB9, 0x67, 0x0B, 0x61, 0xAE, 0x56, 0x40, 0x7B, + 0x7E, 0x22, 0xEF, 0x7F, 0xAE, 0x0A, 0xE4, 0xE2, + 0x6B, 0x38, 0x80, 0xB5, 0xCC, 0xFB, 0x3C, 0x97 + }; + const unsigned char sig[65] = { + 0x02, 0x2D, 0xB0, 0x2D, 0x30, 0x37, 0x63, 0xF5, + 0x12, 0xCB, 0x83, 0x02, 0x5E, 0x25, 0x16, 0x75, + 0xAB, 0xB8, 0x08, 0x82, 0x94, 0xFA, 0x06, 0x4D, + 0xE9, 0xB4, 0x63, 0x80, 0x69, 0x98, 0x2E, 0xC3, + 0x02, 0x2A, 0x3B, 0xD0, 0x49, 0x6E, 0xF8, 0xE8, + 0xC7, 0x4E, 0xEF, 0x20, 0x48, 0x21, 0x40, 0x50, + 0xA6, 0x2E, 0xEA, 0x23, 0x08, 0x1B, 0xC3, 0xD2, + 0x77, 0xBE, 0xF8, 0x81, 0x14, 0x41, 0x58, 0x70, 0x4D + }; + const unsigned char sig64[64] = { + 0x9F, 0x08, 0xBE, 0x86, 0x5F, 0x69, 0x3B, 0xDB, + 0xC1, 0xAE, 0x97, 0x6D, 0xFF, 0xB8, 0xBC, 0x88, + 0x4C, 0x37, 0x20, 0x52, 0xB0, 0x92, 0x6E, 0x49, + 0xE0, 0x29, 0x8A, 0x9F, 0x2D, 0x7B, 0x48, 0x60, + 0x0D, 0x5F, 0x66, 0x23, 0xC5, 0xF3, 0xFC, 0xC9, + 0x08, 0x56, 0x2B, 0xA9, 0xCF, 0x96, 0x91, 0x22, + 0xF2, 0x5E, 0x35, 0xA1, 0x1A, 0x86, 0x17, 0x1E, + 0x6A, 0x5E, 0xA3, 0x3D, 0x3E, 0x1D, 0x6B, 0xA3 + }; + test_schnorr_adaptor_vectors_check_adapt(sig, t32, sig64, 0); + }; +} + +static void test_schnorr_adaptor_presign(void) { + unsigned char sk[32]; + secp256k1_xonly_pubkey pk; + secp256k1_keypair keypair; + secp256k1_scalar adaptor_scalar; + secp256k1_gej tj; + secp256k1_ge tg; + const unsigned char msg[32] = "this is for the schnorr adaptor."; + unsigned char sig[65]; + unsigned char sig2[65]; + unsigned char secadaptor[32]; + unsigned char aux_rand[32]; + unsigned char adaptor33[33]; + secp256k1_pubkey t; + secp256k1_pubkey adaptor; + size_t size = 33; + + secp256k1_testrand256(sk); + secp256k1_testrand256(secadaptor); + secp256k1_testrand256(aux_rand); + secp256k1_scalar_set_b32(&adaptor_scalar, secadaptor, NULL); + secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &tj, &adaptor_scalar); + secp256k1_ge_set_gej(&tg, &tj); + CHECK(secp256k1_eckey_pubkey_serialize(&tg, adaptor33, &size, 1) == 1); + CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1); + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair) == 1); + CHECK(secp256k1_ec_pubkey_parse(CTX, &adaptor, adaptor33, 33) == 1); + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig, msg, &keypair, &adaptor, NULL) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig, msg, &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor) == 0); + /* Test with aux_rand */ + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig2, msg, &keypair, &adaptor, aux_rand) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig, msg, &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor) == 0); +} + +#define N_SIGS 3 +/* Creates N_SIGS valid signatures and verifies them with extract + * Then flips some bits and checks that extract now fails to get the right + * adaptor point. */ +static void test_schnorr_adaptor_extract(void) { + unsigned char sk[32]; + secp256k1_xonly_pubkey pk; + secp256k1_keypair keypair; + secp256k1_scalar adaptor_scalar; + secp256k1_gej tj; + secp256k1_ge tg; + secp256k1_scalar s; + unsigned char msg[N_SIGS][32]; + unsigned char sig[N_SIGS][65]; + unsigned char secadaptor[N_SIGS][32]; + unsigned char adaptor33[N_SIGS][33]; + secp256k1_pubkey t; + secp256k1_pubkey adaptor[N_SIGS]; + size_t size = 33; + size_t i; + + secp256k1_testrand256(sk); + CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1); + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair) == 1); + + for (i = 0; i < N_SIGS; i++) { + secp256k1_testrand256(msg[i]); + secp256k1_testrand256(secadaptor[i]); + secp256k1_scalar_set_b32(&adaptor_scalar, secadaptor[i], NULL); + secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &tj, &adaptor_scalar); + secp256k1_ge_set_gej(&tg, &tj); + CHECK(secp256k1_eckey_pubkey_serialize(&tg, adaptor33[i], &size, 1) == 1); + CHECK(secp256k1_ec_pubkey_parse(CTX, &adaptor[i], adaptor33[i], 33) == 1); + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig[i], msg[i], &keypair, &adaptor[i], NULL) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[i], msg[i], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[i]) == 0); + } + + { + /* Flip some bits in the signature and check that extract fails to + * extract the correct adaptor point */ + size_t sig_idx = secp256k1_testrand_int(N_SIGS); + size_t byte_idx = secp256k1_testrand_bits(5); + unsigned char xorbyte = secp256k1_testrand_int(254)+1; + sig[sig_idx][33 + byte_idx] ^= xorbyte; + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[sig_idx], msg[sig_idx], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[sig_idx]) != 0); + sig[sig_idx][33 + byte_idx] ^= xorbyte; + + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[sig_idx], msg[sig_idx], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[sig_idx]) == 0); + } + + /* Test overflowing s */ + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig[0], msg[0], &keypair, &adaptor[0], NULL) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[0], msg[0], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[0]) == 0); + memset(&sig[0][33], 0xFF, 32); + CHECK(!secp256k1_schnorr_adaptor_extract(CTX, &t, sig[0], msg[0], &pk)); + + /* Test negative s */ + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig[0], msg[0], &keypair, &adaptor[0], NULL) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[0], msg[0], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[0]) == 0); + secp256k1_scalar_set_b32(&s, &sig[0][33], NULL); + secp256k1_scalar_negate(&s, &s); + secp256k1_scalar_get_b32(&sig[0][33], &s); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig[0], msg[0], &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor[0]) != 0); +} +#undef N_SIGS + +static void test_schnorr_adaptor_adapt_extract_sec(void) { + unsigned char sk[32]; + secp256k1_xonly_pubkey pk; + secp256k1_keypair keypair; + secp256k1_scalar adaptor_scalar; + secp256k1_gej tj; + secp256k1_ge tg; + unsigned char msg[32]; + unsigned char sig[65]; + unsigned char sig64[64]; + unsigned char secadaptor[32]; + unsigned char aux_rand[32]; + unsigned char adaptor33[33]; + secp256k1_pubkey t; + unsigned char t2[32]; + secp256k1_pubkey adaptor; + size_t size = 33; + + secp256k1_testrand256(sk); + secp256k1_testrand256(msg); + secp256k1_testrand256(secadaptor); + secp256k1_testrand256(aux_rand); + secp256k1_scalar_set_b32(&adaptor_scalar, secadaptor, NULL); + secp256k1_ecmult_gen(&CTX->ecmult_gen_ctx, &tj, &adaptor_scalar); + secp256k1_ge_set_gej(&tg, &tj); + CHECK(secp256k1_eckey_pubkey_serialize(&tg, adaptor33, &size, 1) == 1); + CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1); + CHECK(secp256k1_keypair_xonly_pub(CTX, &pk, NULL, &keypair) == 1); + CHECK(secp256k1_ec_pubkey_parse(CTX, &adaptor, adaptor33, 33) == 1); + CHECK(secp256k1_schnorr_adaptor_presign(CTX, sig, msg, &keypair, &adaptor, aux_rand) == 1); + CHECK(secp256k1_schnorr_adaptor_extract(CTX, &t, sig, msg, &pk)); + CHECK(secp256k1_ec_pubkey_cmp(CTX, &t, &adaptor) == 0); + CHECK(secp256k1_schnorr_adaptor_adapt(CTX, sig64, sig, secadaptor) == 1); + CHECK(secp256k1_schnorrsig_verify(CTX, sig64, msg, sizeof(msg), &pk) == 1); + CHECK(secp256k1_schnorr_adaptor_extract_sec(CTX, t2, sig, sig64) == 1); + CHECK(secp256k1_memcmp_var(t2, secadaptor, 32) == 0); +} + +static void run_schnorr_adaptor_tests(void) { + int i; + run_nonce_function_schnorr_adaptor_tests(); + + test_schnorr_adaptor_api(); + test_schnorrsig_sha256_tagged(); + test_schnorr_adaptor_vectors(); + for (i = 0; i < COUNT; i++) { + test_schnorr_adaptor_presign(); + test_schnorr_adaptor_extract(); + test_schnorr_adaptor_adapt_extract_sec(); + } +} + +#endif diff --git a/src/secp256k1.c b/src/secp256k1.c index 701b436b2..71ecb6e55 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -892,6 +892,10 @@ static int secp256k1_ge_parse_ext(secp256k1_ge* ge, const unsigned char *in33) { # include "modules/schnorrsig/main_impl.h" #endif +#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR +# include "modules/schnorr_adaptor/main_impl.h" +#endif + #ifdef ENABLE_MODULE_ELLSWIFT # include "modules/ellswift/main_impl.h" #endif diff --git a/src/tests.c b/src/tests.c index d092dd69a..409195ab4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -7470,6 +7470,10 @@ static void run_ecdsa_wycheproof(void) { # include "modules/schnorrsig/tests_impl.h" #endif +#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR +# include "modules/schnorr_adaptor/tests_impl.h" +#endif + #ifdef ENABLE_MODULE_ELLSWIFT # include "modules/ellswift/tests_impl.h" #endif @@ -7855,6 +7859,10 @@ int main(int argc, char **argv) { run_schnorrsig_tests(); #endif +#ifdef ENABLE_MODULE_SCHNORR_ADAPTOR + run_schnorr_adaptor_tests(); +#endif + #ifdef ENABLE_MODULE_ELLSWIFT run_ellswift_tests(); #endif