-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: Add Wycheproof ECDH vectors #1492
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,14 @@ | |||||
#ifndef SECP256K1_MODULE_ECDH_TESTS_H | ||||||
#define SECP256K1_MODULE_ECDH_TESTS_H | ||||||
|
||||||
static int ecdh_hash_function_test_xpassthru(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) { | ||||||
(void)x; | ||||||
(void)y; | ||||||
(void)data; | ||||||
memcpy(output, x, 32); | ||||||
return 1; | ||||||
} | ||||||
|
||||||
static int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) { | ||||||
(void)output; | ||||||
(void)x; | ||||||
|
@@ -142,11 +150,53 @@ static void test_result_basepoint(void) { | |||||
} | ||||||
} | ||||||
|
||||||
static void test_ecdh_wycheproof(void) { | ||||||
#include "../../wycheproof/ecdh_secp256k1_test.h" | ||||||
int t; | ||||||
for (t = 0; t < SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS; t++) { | ||||||
int parsed_ok; | ||||||
secp256k1_pubkey point; | ||||||
const unsigned char *pk; | ||||||
const unsigned char *sk; | ||||||
const unsigned char *expected_shared_secret; | ||||||
unsigned char output_ecdh[65] = { 0 }; | ||||||
|
||||||
int expected_result; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||||||
int actual; | ||||||
|
||||||
memset(&point, 0, sizeof(point)); | ||||||
pk = &wycheproof_ecdh_public_keys[testvectors[t].pk_offset]; | ||||||
parsed_ok = secp256k1_ec_pubkey_parse(CTX, &point, pk, testvectors[t].pk_len); | ||||||
|
||||||
expected_result = testvectors[t].expected_result; | ||||||
|
||||||
/* fail if public key is valid, but doesn't parse */ | ||||||
CHECK(parsed_ok || expected_result == 0); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be tighter:
Suggested change
The missing case is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, addressed |
||||||
|
||||||
if (!parsed_ok && expected_result == 0) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
sk = &wycheproof_ecdh_private_keys[testvectors[t].sk_offset]; | ||||||
CHECK(testvectors[t].sk_len == 32); | ||||||
|
||||||
actual = secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL); | ||||||
expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset]; | ||||||
|
||||||
CHECK(actual == expected_result); | ||||||
if (expected_result == 0) { | ||||||
CHECK(testvectors[t].shared_len == 0); | ||||||
} | ||||||
CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0); | ||||||
} | ||||||
} | ||||||
|
||||||
static void run_ecdh_tests(void) { | ||||||
test_ecdh_api(); | ||||||
test_ecdh_generator_basepoint(); | ||||||
test_bad_scalar(); | ||||||
test_result_basepoint(); | ||||||
test_ecdh_wycheproof(); | ||||||
} | ||||||
|
||||||
#endif /* SECP256K1_MODULE_ECDH_TESTS_H */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,18 @@ | |
`b063b4aedae951c69df014cd25fa6d69ae9e8cb9`, see | ||
https://github.com/google/wycheproof/blob/b063b4aedae951c69df014cd25fa6d69ae9e8cb9/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json | ||
|
||
* The file `ecdh_secp256k1_test.json` in this directory | ||
comes from Google's project Wycheproof with git commit | ||
`d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d`, see | ||
https://github.com/google/wycheproof/blob/d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d/testvectors_v1/ecdh_secp256k1_test.json | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wycheproof ownership was recently moved to C2SP (https://github.com/C2SP/wycheproof community maintenance), so this should be updated to the new URL.) See @FiloSottile's talk https://archive.org/details/oscw-2024-fillippo-valsorda-cryptographic-test-vectors for background.) You could update the other URLs in a separate commit, and update the ECDSA vectors, see C2SP/wycheproof#91 (if you're willing to care of this in this PR). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, we will open a concurrent PR to update this. I think it will be cleaner. |
||
|
||
* The file `ecdsa_secp256k1_sha256_bitcoin_test.h` is generated from | ||
`ecdsa_secp256k1_sha256_bitcoin_test.json` using the script | ||
`tests_wycheproof_generate.py`. | ||
`tests_wycheproof_generate_ecdsa.py`. | ||
|
||
* The file `ecdh_secp256k1_test.h` is generated from | ||
`ecdh_secp256k1_test.json` using the script | ||
`tests_wycheproof_generate_ecdh.py`. | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You use
x
below.(just a random comment, I haven't really reviewed the code so far)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, addressed