Skip to content

Commit f26c323

Browse files
RandomLatticeandozw
andcommitted
tests: Add Wycheproof ECDH vectors
Adds a test for the ECDH module using the Wycheproof vectors. We use a python script to convert the JSON-formatted vectors into C code, in the same spirit as bitcoin-core#1245 Co-authored-by: Sean Andersen <6730974+andozw@users.noreply.github.com>
1 parent 0653a25 commit f26c323

10 files changed

+10615
-11
lines changed

Makefile.am

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,17 @@ maintainer-clean-local: clean-precomp
225225
### (see the comments in the previous section for detailed rationale)
226226
TESTVECTORS = src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h
227227

228+
if ENABLE_MODULE_ECDH
229+
TESTVECTORS += src/wycheproof/ecdh_secp256k1_test.h
230+
endif
231+
228232
src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h:
229233
mkdir -p $(@D)
230-
python3 $(top_srcdir)/tools/tests_wycheproof_generate.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
234+
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdsa.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
235+
236+
src/wycheproof/ecdh_secp256k1_test.h:
237+
mkdir -p $(@D)
238+
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdh.py $(top_srcdir)/src/wycheproof/ecdh_secp256k1_test.json > $@
231239

232240
testvectors: $(TESTVECTORS)
233241

@@ -250,7 +258,9 @@ EXTRA_DIST += sage/secp256k1_params.sage
250258
EXTRA_DIST += sage/weierstrass_prover.sage
251259
EXTRA_DIST += src/wycheproof/WYCHEPROOF_COPYING
252260
EXTRA_DIST += src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json
253-
EXTRA_DIST += tools/tests_wycheproof_generate.py
261+
EXTRA_DIST += src/wycheproof/ecdh_secp256k1_test.json
262+
EXTRA_DIST += tools/tests_wycheproof_generate_ecdsa.py
263+
EXTRA_DIST += tools/tests_wycheproof_generate_ecdh.py
254264

255265
if ENABLE_MODULE_ECDH
256266
include src/modules/ecdh/Makefile.am.include

src/modules/ecdh/Makefile.am.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ include_HEADERS += include/secp256k1_ecdh.h
22
noinst_HEADERS += src/modules/ecdh/main_impl.h
33
noinst_HEADERS += src/modules/ecdh/tests_impl.h
44
noinst_HEADERS += src/modules/ecdh/bench_impl.h
5+
noinst_HEADERS += src/wycheproof/ecdh_secp256k1_test.h

src/modules/ecdh/tests_impl.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
#ifndef SECP256K1_MODULE_ECDH_TESTS_H
88
#define SECP256K1_MODULE_ECDH_TESTS_H
99

10+
static int ecdh_hash_function_test_xpassthru(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
11+
(void)x;
12+
(void)y;
13+
(void)data;
14+
memcpy(output, x, 32);
15+
return 1;
16+
}
17+
1018
static int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
1119
(void)output;
1220
(void)x;
@@ -142,11 +150,53 @@ static void test_result_basepoint(void) {
142150
}
143151
}
144152

153+
static void test_ecdh_wycheproof(void) {
154+
#include "../../wycheproof/ecdh_secp256k1_test.h"
155+
int t;
156+
for (t = 0; t < SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS; t++) {
157+
int parsed_ok;
158+
secp256k1_pubkey point;
159+
const unsigned char *pk;
160+
const unsigned char *sk;
161+
const unsigned char *expected_shared_secret;
162+
unsigned char output_ecdh[65] = { 0 };
163+
164+
int expected_result;
165+
int actual;
166+
167+
memset(&point, 0, sizeof(point));
168+
pk = &wycheproof_ecdh_public_keys[testvectors[t].pk_offset];
169+
parsed_ok = secp256k1_ec_pubkey_parse(CTX, &point, pk, testvectors[t].pk_len);
170+
171+
expected_result = testvectors[t].expected_result;
172+
173+
/* fail if public key is valid, but doesn't parse */
174+
CHECK(parsed_ok || expected_result == 0);
175+
176+
if (!parsed_ok && expected_result == 0) {
177+
continue;
178+
}
179+
180+
sk = &wycheproof_ecdh_private_keys[testvectors[t].sk_offset];
181+
CHECK(testvectors[t].sk_len == 32);
182+
183+
actual = secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL);
184+
expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset];
185+
186+
CHECK(actual == expected_result);
187+
if (expected_result == 0) {
188+
CHECK(testvectors[t].shared_len == 0);
189+
}
190+
CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0);
191+
}
192+
}
193+
145194
static void run_ecdh_tests(void) {
146195
test_ecdh_api();
147196
test_ecdh_generator_basepoint();
148197
test_bad_scalar();
149198
test_result_basepoint();
199+
test_ecdh_wycheproof();
150200
}
151201

152202
#endif /* SECP256K1_MODULE_ECDH_TESTS_H */

src/wycheproof/WYCHEPROOF_COPYING

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
`b063b4aedae951c69df014cd25fa6d69ae9e8cb9`, see
44
https://github.com/google/wycheproof/blob/b063b4aedae951c69df014cd25fa6d69ae9e8cb9/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json
55

6+
* The file `ecdh_secp256k1_test.json` in this directory
7+
comes from Google's project Wycheproof with git commit
8+
`d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d`, see
9+
https://github.com/google/wycheproof/blob/d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d/testvectors_v1/ecdh_secp256k1_test.json
10+
611
* The file `ecdsa_secp256k1_sha256_bitcoin_test.h` is generated from
712
`ecdsa_secp256k1_sha256_bitcoin_test.json` using the script
8-
`tests_wycheproof_generate.py`.
13+
`tests_wycheproof_generate_ecdsa.py`.
14+
15+
* The file `ecdh_secp256k1_test.h` is generated from
16+
`ecdh_secp256k1_test.json` using the script
17+
`tests_wycheproof_generate_ecdh.py`.
918

1019
-------------------------------------------------------------------------------
1120

0 commit comments

Comments
 (0)