Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
secp256k1
src
modules
ecdh
tests_exhaustive_impl.h
Go to the documentation of this file.
1
/***********************************************************************
2
* Distributed under the MIT software license, see the accompanying *
3
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4
***********************************************************************/
5
6
#ifndef SECP256K1_MODULE_ECDH_TESTS_EXHAUSTIVE_H
7
#define SECP256K1_MODULE_ECDH_TESTS_EXHAUSTIVE_H
8
9
#include "
../../../include/secp256k1_ecdh.h
"
10
#include "
main_impl.h
"
11
12
static
void
test_exhaustive_ecdh
(
const
secp256k1_context
*ctx,
const
secp256k1_ge
*group) {
13
int
i, j;
14
unsigned
char
seckeys[
EXHAUSTIVE_TEST_ORDER
- 1][32];
15
secp256k1_pubkey
pubkeys[
EXHAUSTIVE_TEST_ORDER
- 1];
16
17
/* Construct key pairs (32-byte secret key, public key object) for the entire group. */
18
for
(i = 1; i <
EXHAUSTIVE_TEST_ORDER
; i++) {
19
secp256k1_scalar
scalar;
20
secp256k1_scalar_set_int(&scalar, i);
21
secp256k1_scalar_get_b32(seckeys[i - 1], &scalar);
22
CHECK
(
secp256k1_ec_pubkey_create
(ctx, &pubkeys[i - 1], seckeys[i - 1]));
23
}
24
25
/* Loop over key combinations. */
26
for
(i = 1; i <
EXHAUSTIVE_TEST_ORDER
; i++) {
27
for
(j = 1; j <
EXHAUSTIVE_TEST_ORDER
; j++) {
28
unsigned
char
ecdh_result_ij[32];
29
unsigned
char
ecdh_result_ji[32];
30
31
/* Calculate ECDH(i*G, j) and ECDH(j*G, i) using API function and verify that the results match. */
32
CHECK
(
secp256k1_ecdh
(ctx, ecdh_result_ij, &pubkeys[i - 1], seckeys[j - 1], NULL, NULL));
33
CHECK
(
secp256k1_ecdh
(ctx, ecdh_result_ji, &pubkeys[j - 1], seckeys[i - 1], NULL, NULL));
34
CHECK
(
secp256k1_memcmp_var
(ecdh_result_ij, ecdh_result_ji, 32) == 0);
35
36
/* Recalculate the expected ECDH result manually by invoking the default ECDH hash
37
* function on the precomputed group element (group[i * j]) coordinates, and verify
38
* that it matches the previously calculated public API results. */
39
{
40
secp256k1_ge
ecdh_ge_expected = group[(i * j) %
EXHAUSTIVE_TEST_ORDER
];
41
unsigned
char
ecdh_result_expected[32];
42
unsigned
char
x[32];
43
unsigned
char
y[32];
44
45
secp256k1_fe_normalize_var
(&ecdh_ge_expected.
x
);
46
secp256k1_fe_normalize_var
(&ecdh_ge_expected.
y
);
47
secp256k1_fe_get_b32
(x, &ecdh_ge_expected.
x
);
48
secp256k1_fe_get_b32
(y, &ecdh_ge_expected.
y
);
49
CHECK
(
secp256k1_ecdh_hash_function_default
(ecdh_result_expected, x, y, NULL));
50
CHECK
(
secp256k1_memcmp_var
(ecdh_result_ij, ecdh_result_expected, 32) == 0);
51
}
52
}
53
}
54
}
55
56
#endif
main_impl.h
test_exhaustive_ecdh
static void test_exhaustive_ecdh(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition
tests_exhaustive_impl.h:12
secp256k1_fe_normalize_var
#define secp256k1_fe_normalize_var
Definition
field.h:80
secp256k1_fe_get_b32
#define secp256k1_fe_get_b32
Definition
field.h:89
secp256k1_context
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
Definition
key.h:22
CHECK
#define CHECK(cond)
Unconditional failure on condition failure.
Definition
util.h:49
secp256k1_memcmp_var
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition
util.h:281
secp256k1_ec_pubkey_create
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition
secp256k1.c:632
secp256k1_ecdh.h
secp256k1_ecdh
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Definition
main_impl.h:34
secp256k1_ecdh_hash_function_default
SECP256K1_API const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default
A default ECDH hash function (currently equal to secp256k1_ecdh_hash_function_sha256).
Definition
secp256k1_ecdh.h:34
secp256k1_ge
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition
group.h:16
secp256k1_ge::x
secp256k1_fe x
Definition
group.h:17
secp256k1_ge::y
secp256k1_fe y
Definition
group.h:18
secp256k1_pubkey
Opaque data structure that holds a parsed and valid public key.
Definition
secp256k1.h:62
secp256k1_scalar
A scalar modulo the group order of the secp256k1 curve.
Definition
scalar_4x64.h:13
EXHAUSTIVE_TEST_ORDER
#define EXHAUSTIVE_TEST_ORDER
Definition
tests_exhaustive.c:13
Generated on
for Bitcoin Core by
1.18.0