Skip to content

Commit a1cce06

Browse files
RandomLatticeandozw
andcommitted
tests: Add Wycheproof ECDSA vectors
Adds a test using the Wycheproof vectors as outlined in #1106. The vectors are pulled from the Wycheproof repo using a python script to emit C code. The script is embedded as a comment. Co-authored-by: Sean Andersen <[email protected]>
1 parent 9c8c4f4 commit a1cce06

5 files changed

+7410
-0
lines changed

Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ src/precomputed_ecmult_gen.c:
213213
PRECOMP = src/precomputed_ecmult_gen.c src/precomputed_ecmult.c
214214
precomp: $(PRECOMP)
215215

216+
src/vectors/ecdsa_secp256k1_sha256_bitcoin_test.inc: src/vectors/ecdsa_secp256k1_sha256_bitcoin_test.json
217+
python3 src/vectors/tests_wycheproof_generate.py $< > $@
218+
216219
# Ensure the prebuilt files will be build first (only if they don't exist,
217220
# e.g., after `make maintainer-clean`).
218221
BUILT_SOURCES = $(PRECOMP)

src/tests.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7306,6 +7306,40 @@ static void run_ecdsa_edge_cases(void) {
73067306
test_ecdsa_edge_cases();
73077307
}
73087308

7309+
/** Wycheproof tests
7310+
7311+
The tests check for known attacks (range checks in (r,s), arithmetic errors, malleability).
7312+
*/
7313+
static void test_ecdsa_wycheproof(void) {
7314+
#include "vectors/ecdsa_secp256k1_sha256_bitcoin_test.inc"
7315+
7316+
int t;
7317+
for (t = 0; t < SECP256K1_TEST_ECDSA_WYCHEPROOF_NUMBER_TESTS; t++) {
7318+
secp256k1_ecdsa_signature signature;
7319+
secp256k1_sha256 hasher;
7320+
secp256k1_pubkey pubkey;
7321+
unsigned char out[32] = {0};
7322+
int actual_verify = 0;
7323+
7324+
memset(&pubkey, 0, sizeof(pubkey));
7325+
CHECK(secp256k1_ec_pubkey_parse(CTX, &pubkey, testcases[t].pk, 65) == 1);
7326+
7327+
secp256k1_sha256_initialize(&hasher);
7328+
secp256k1_sha256_write(&hasher, (const unsigned char*)testcases[t].msg, testcases[t].msglen);
7329+
secp256k1_sha256_finalize(&hasher, out);
7330+
7331+
if (secp256k1_ecdsa_signature_parse_der(CTX, &signature, testcases[t].sig, testcases[t].siglen) == 1) {
7332+
actual_verify = secp256k1_ecdsa_verify(CTX, (const secp256k1_ecdsa_signature *)&signature, out, &pubkey);
7333+
}
7334+
CHECK(testcases[t].expected_verify == actual_verify);
7335+
}
7336+
}
7337+
7338+
/* Tests cases from Wycheproof test suite. */
7339+
static void run_ecdsa_wycheproof(void) {
7340+
test_ecdsa_wycheproof();
7341+
}
7342+
73097343
#ifdef ENABLE_MODULE_ECDH
73107344
# include "modules/ecdh/tests_impl.h"
73117345
#endif
@@ -7638,6 +7672,7 @@ int main(int argc, char **argv) {
76387672
run_ecdsa_sign_verify();
76397673
run_ecdsa_end_to_end();
76407674
run_ecdsa_edge_cases();
7675+
run_ecdsa_wycheproof();
76417676

76427677
#ifdef ENABLE_MODULE_RECOVERY
76437678
/* ECDSA pubkey recovery tests */

0 commit comments

Comments
 (0)