Skip to content

Commit e266ba1

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 <[email protected]>
1 parent 0653a25 commit e266ba1

10 files changed

+10695
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
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)y;
12+
(void)data;
13+
memcpy(output, x, 32);
14+
return 1;
15+
}
16+
1017
static int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
1118
(void)output;
1219
(void)x;
@@ -142,11 +149,45 @@ static void test_result_basepoint(void) {
142149
}
143150
}
144151

152+
static void test_ecdh_wycheproof(void) {
153+
#include "../../wycheproof/ecdh_secp256k1_test.h"
154+
int t;
155+
for (t = 0; t < SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS; t++) {
156+
int parsed_ok;
157+
secp256k1_pubkey point;
158+
const unsigned char *pk;
159+
const unsigned char *sk;
160+
const unsigned char *expected_shared_secret;
161+
unsigned char output_ecdh[65] = { 0 };
162+
163+
int expected_result;
164+
165+
memset(&point, 0, sizeof(point));
166+
pk = &wycheproof_ecdh_public_keys[testvectors[t].pk_offset];
167+
parsed_ok = secp256k1_ec_pubkey_parse(CTX, &point, pk, testvectors[t].pk_len);
168+
169+
expected_result = testvectors[t].expected_result;
170+
CHECK(parsed_ok == expected_result);
171+
if (!parsed_ok) {
172+
continue;
173+
}
174+
175+
sk = &wycheproof_ecdh_private_keys[testvectors[t].sk_offset];
176+
CHECK(testvectors[t].sk_len == 32);
177+
178+
CHECK(secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL) == 1);
179+
expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset];
180+
181+
CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0);
182+
}
183+
}
184+
145185
static void run_ecdh_tests(void) {
146186
test_ecdh_api();
147187
test_ecdh_generator_basepoint();
148188
test_bad_scalar();
149189
test_result_basepoint();
190+
test_ecdh_wycheproof();
150191
}
151192

152193
#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

src/wycheproof/ecdh_secp256k1_test.h

Lines changed: 2008 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)