Skip to content

Commit

Permalink
ecdh: Add test computing shared_secret=basepoint with random inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Dec 3, 2021
1 parent 49f608d commit 18fe75b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/modules/ecdh/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,40 @@ void test_bad_scalar(void) {
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, ecdh_hash_function_test_fail, NULL) == 0);
}

void test_result_basepoint(void) {
secp256k1_pubkey point;
secp256k1_scalar rand;
unsigned char s[32];
unsigned char s_inv[32];
unsigned char out[32];
unsigned char out_eq[32];
int i;

for (i = 0; i < 64; i++) {
random_scalar_order(&rand);
secp256k1_scalar_get_b32(s, &rand);
secp256k1_scalar_inverse(&rand, &rand);
secp256k1_scalar_get_b32(s_inv, &rand);

CHECK(secp256k1_ec_pubkey_create(ctx, &point, s) == 1);
CHECK(secp256k1_ecdh(ctx, out, &point, s_inv, NULL, NULL) == 1);

if (i > 0) {
CHECK(secp256k1_memcmp_var(out, out_eq, 32) == 0);
}

CHECK(secp256k1_ec_pubkey_create(ctx, &point, s_inv) == 1);
CHECK(secp256k1_ecdh(ctx, out_eq, &point, s, NULL, NULL) == 1);

CHECK(secp256k1_memcmp_var(out, out_eq, 32) == 0);
}
}

void run_ecdh_tests(void) {
test_ecdh_api();
test_ecdh_generator_basepoint();
test_bad_scalar();
test_result_basepoint();
}

#endif /* SECP256K1_MODULE_ECDH_TESTS_H */

0 comments on commit 18fe75b

Please sign in to comment.