Skip to content

Commit 3ef94aa

Browse files
committed
Merge bitcoin-core/secp256k1#1026: ecdh: Add test computing shared_secret=basepoint with random inputs
3531a43 ecdh: Make generator_basepoint test depend on global iteration count (Tim Ruffing) c881dd4 ecdh: Add test computing shared_secret=basepoint with random inputs (Tim Ruffing) Pull request description: ACKs for top commit: jonasnick: ACK 3531a43 Tree-SHA512: 5a2e47bad7ec5b3fd9033283fe00e54563b7b1655baf2b8ca39718deceddcc816bb8fcda0d07af6f1f8a785642da5dc69b7df52a1ddd445a3a98a5d5ecff6780
2 parents 0775283 + 3531a43 commit 3ef94aa

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/modules/ecdh/tests_impl.h

+34-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void test_ecdh_generator_basepoint(void) {
6060

6161
s_one[31] = 1;
6262
/* Check against pubkey creation when the basepoint is the generator */
63-
for (i = 0; i < 100; ++i) {
63+
for (i = 0; i < 2 * count; ++i) {
6464
secp256k1_sha256 sha;
6565
unsigned char s_b32[32];
6666
unsigned char output_ecdh[65];
@@ -123,10 +123,43 @@ void test_bad_scalar(void) {
123123
CHECK(secp256k1_ecdh(ctx, output, &point, s_overflow, ecdh_hash_function_test_fail, NULL) == 0);
124124
}
125125

126+
/** Test that ECDH(sG, 1/s) == ECDH((1/s)G, s) == ECDH(G, 1) for a few random s. */
127+
void test_result_basepoint(void) {
128+
secp256k1_pubkey point;
129+
secp256k1_scalar rand;
130+
unsigned char s[32];
131+
unsigned char s_inv[32];
132+
unsigned char out[32];
133+
unsigned char out_inv[32];
134+
unsigned char out_base[32];
135+
int i;
136+
137+
unsigned char s_one[32] = { 0 };
138+
s_one[31] = 1;
139+
CHECK(secp256k1_ec_pubkey_create(ctx, &point, s_one) == 1);
140+
CHECK(secp256k1_ecdh(ctx, out_base, &point, s_one, NULL, NULL) == 1);
141+
142+
for (i = 0; i < 2 * count; i++) {
143+
random_scalar_order(&rand);
144+
secp256k1_scalar_get_b32(s, &rand);
145+
secp256k1_scalar_inverse(&rand, &rand);
146+
secp256k1_scalar_get_b32(s_inv, &rand);
147+
148+
CHECK(secp256k1_ec_pubkey_create(ctx, &point, s) == 1);
149+
CHECK(secp256k1_ecdh(ctx, out, &point, s_inv, NULL, NULL) == 1);
150+
CHECK(secp256k1_memcmp_var(out, out_base, 32) == 0);
151+
152+
CHECK(secp256k1_ec_pubkey_create(ctx, &point, s_inv) == 1);
153+
CHECK(secp256k1_ecdh(ctx, out_inv, &point, s, NULL, NULL) == 1);
154+
CHECK(secp256k1_memcmp_var(out_inv, out_base, 32) == 0);
155+
}
156+
}
157+
126158
void run_ecdh_tests(void) {
127159
test_ecdh_api();
128160
test_ecdh_generator_basepoint();
129161
test_bad_scalar();
162+
test_result_basepoint();
130163
}
131164

132165
#endif /* SECP256K1_MODULE_ECDH_TESTS_H */

0 commit comments

Comments
 (0)