Skip to content

Commit 0585b8b

Browse files
Merge dashpay#718: Clarify that a secp256k1_ecdh_hash_function must return 0 or 1
eb45ef3 Clarify that a secp256k1_ecdh_hash_function must return 0 or 1 (Tim Ruffing) Pull request description: and improve style of the ECDH docs. ACKs for top commit: sipa: utACK eb45ef3 jonasnick: ACK eb45ef3 elichai: ACK eb45ef3 apoelstra: utACK bitcoin-core/secp256k1@eb45ef3 Tree-SHA512: fa1e34fbbe2fd53b633c48c70fbd9d6eec4be1303b660ff87945d49333264ef5c28a4db9407161907697f37ca657a1ee7b50e58861689de526ad4d685dedeae6
2 parents 856a01d + eb45ef3 commit 0585b8b

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

include/secp256k1_ecdh.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,44 @@
77
extern "C" {
88
#endif
99

10-
/** A pointer to a function that applies hash function to a point
10+
/** A pointer to a function that hashes an EC point to obtain an ECDH secret
1111
*
12-
* Returns: 1 if a point was successfully hashed. 0 will cause ecdh to fail
13-
* Out: output: pointer to an array to be filled by the function
14-
* In: x: pointer to a 32-byte x coordinate
15-
* y: pointer to a 32-byte y coordinate
16-
* data: Arbitrary data pointer that is passed through
12+
* Returns: 1 if the point was successfully hashed.
13+
* 0 will cause secp256k1_ecdh to fail and return 0.
14+
* Other return values are not allowed, and the behaviour of
15+
* secp256k1_ecdh is undefined for other return values.
16+
* Out: output: pointer to an array to be filled by the function
17+
* In: x32: pointer to a 32-byte x coordinate
18+
* y32: pointer to a 32-byte y coordinate
19+
* data: arbitrary data pointer that is passed through
1720
*/
1821
typedef int (*secp256k1_ecdh_hash_function)(
1922
unsigned char *output,
20-
const unsigned char *x,
21-
const unsigned char *y,
23+
const unsigned char *x32,
24+
const unsigned char *y32,
2225
void *data
2326
);
2427

2528
/** An implementation of SHA256 hash function that applies to compressed public key.
2629
* Populates the output parameter with 32 bytes. */
2730
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256;
2831

29-
/** A default ecdh hash function (currently equal to secp256k1_ecdh_hash_function_sha256).
32+
/** A default ECDH hash function (currently equal to secp256k1_ecdh_hash_function_sha256).
3033
* Populates the output parameter with 32 bytes. */
3134
SECP256K1_API extern const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default;
3235

3336
/** Compute an EC Diffie-Hellman secret in constant time
37+
*
3438
* Returns: 1: exponentiation was successful
35-
* 0: scalar was invalid (zero or overflow)
39+
* 0: scalar was invalid (zero or overflow) or hashfp returned 0
3640
* Args: ctx: pointer to a context object (cannot be NULL)
3741
* Out: output: pointer to an array to be filled by hashfp
3842
* In: pubkey: a pointer to a secp256k1_pubkey containing an
3943
* initialized public key
4044
* privkey: a 32-byte scalar with which to multiply the point
4145
* hashfp: pointer to a hash function. If NULL, secp256k1_ecdh_hash_function_sha256 is used
4246
* (in which case, 32 bytes will be written to output)
43-
* data: Arbitrary data pointer that is passed through to hashfp
47+
* data: arbitrary data pointer that is passed through to hashfp
4448
*/
4549
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(
4650
const secp256k1_context* ctx,

src/modules/ecdh/main_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#include "include/secp256k1_ecdh.h"
1111
#include "ecmult_const_impl.h"
1212

13-
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
14-
unsigned char version = (y[31] & 0x01) | 0x02;
13+
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) {
14+
unsigned char version = (y32[31] & 0x01) | 0x02;
1515
secp256k1_sha256 sha;
1616
(void)data;
1717

1818
secp256k1_sha256_initialize(&sha);
1919
secp256k1_sha256_write(&sha, &version, 1);
20-
secp256k1_sha256_write(&sha, x, 32);
20+
secp256k1_sha256_write(&sha, x32, 32);
2121
secp256k1_sha256_finalize(&sha, output);
2222

2323
return 1;

0 commit comments

Comments
 (0)