Skip to content

Commit 4187a46

Browse files
committed
Merge #1492: tests: Add Wycheproof ECDH vectors
e266ba1 tests: Add Wycheproof ECDH vectors (RandomLattice) Pull request description: ACKs for top commit: jonasnick: ACK e266ba1 Tree-SHA512: a5cc59886595b134dadcc50e6cd6f03ce036c2857cdd848f138f0c49d4bd742ae5eb5ebca7840ec8666b5d43fa9c4f67cde4d0fb2245b1cf56b079ca3f7c7f8e
2 parents 13906b7 + e266ba1 commit 4187a46

10 files changed

+10695
-11
lines changed

Makefile.am

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

253+
if ENABLE_MODULE_ECDH
254+
TESTVECTORS += src/wycheproof/ecdh_secp256k1_test.h
255+
endif
256+
253257
src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h:
254258
mkdir -p $(@D)
255-
python3 $(top_srcdir)/tools/tests_wycheproof_generate.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
259+
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdsa.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
260+
261+
src/wycheproof/ecdh_secp256k1_test.h:
262+
mkdir -p $(@D)
263+
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdh.py $(top_srcdir)/src/wycheproof/ecdh_secp256k1_test.json > $@
256264

257265
testvectors: $(TESTVECTORS)
258266

@@ -276,7 +284,9 @@ EXTRA_DIST += sage/secp256k1_params.sage
276284
EXTRA_DIST += sage/weierstrass_prover.sage
277285
EXTRA_DIST += src/wycheproof/WYCHEPROOF_COPYING
278286
EXTRA_DIST += src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json
279-
EXTRA_DIST += tools/tests_wycheproof_generate.py
287+
EXTRA_DIST += src/wycheproof/ecdh_secp256k1_test.json
288+
EXTRA_DIST += tools/tests_wycheproof_generate_ecdsa.py
289+
EXTRA_DIST += tools/tests_wycheproof_generate_ecdh.py
280290

281291
if ENABLE_MODULE_ECDH
282292
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)