Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
main_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2015 Andrew Poelstra *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#ifndef SECP256K1_MODULE_ECDH_MAIN_H
8#define SECP256K1_MODULE_ECDH_MAIN_H
9
12
13static int ecdh_hash_function_sha256_impl(const secp256k1_hash_ctx *hash_ctx, unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) {
14 unsigned char version = (y32[31] & 0x01) | 0x02;
16 (void)data;
17
19 secp256k1_sha256_write(hash_ctx, &sha, &version, 1);
20 secp256k1_sha256_write(hash_ctx, &sha, x32, 32);
21 secp256k1_sha256_finalize(hash_ctx, &sha, output);
23
24 return 1;
25}
26
27static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data) {
29}
30
33
34int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *output, const secp256k1_pubkey *point, const unsigned char *scalar, secp256k1_ecdh_hash_function hashfp, void *data) {
35 int ret = 0;
36 int is_sec_valid;
37 secp256k1_gej res;
38 secp256k1_ge pt;
40 unsigned char x[32];
41 unsigned char y[32];
42
43 VERIFY_CHECK(ctx != NULL);
44 ARG_CHECK(output != NULL);
45 ARG_CHECK(point != NULL);
46 ARG_CHECK(scalar != NULL);
47
48 secp256k1_pubkey_load(ctx, &pt, point);
49 is_sec_valid = secp256k1_scalar_set_b32_seckey(&s, scalar);
50 secp256k1_scalar_cmov(&s, &secp256k1_scalar_one, !is_sec_valid);
51
52 secp256k1_ecmult_const(&res, &pt, &s);
53 secp256k1_ge_set_gej(&pt, &res);
54
55 /* Compute a hash of the point */
58 secp256k1_fe_get_b32(x, &pt.x);
59 secp256k1_fe_get_b32(y, &pt.y);
60
61 if (hashfp == NULL || hashfp == secp256k1_ecdh_hash_function_sha256) {
62 /* Use ctx-aware function by default */
63 ret = ecdh_hash_function_sha256_impl(&ctx->hash_ctx, output, x, y, data);
64 } else {
65 ret = hashfp(output, x, y, data);
66 }
67
68 secp256k1_memclear_explicit(x, sizeof(x));
69 secp256k1_memclear_explicit(y, sizeof(y));
70 secp256k1_scalar_clear(&s);
71 secp256k1_ge_clear(&pt);
72 secp256k1_gej_clear(&res);
73
74 return (!!ret) & is_sec_valid;
75}
76
77#endif /* SECP256K1_MODULE_ECDH_MAIN_H */
int ret
static int ecdh_hash_function_sha256_impl(const secp256k1_hash_ctx *hash_ctx, unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data)
Definition main_impl.h:13
static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data)
Definition main_impl.h:27
#define secp256k1_fe_get_b32
Definition field.h:89
#define secp256k1_fe_normalize
Definition field.h:78
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
Definition key.h:22
static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
Definition util.h:268
#define VERIFY_CHECK(cond)
Definition util.h:169
#define ARG_CHECK(cond)
Definition secp256k1.c:45
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition secp256k1.h:237
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
int(*) secp256k1_ecdh_hash_function(unsigned char *output, const unsigned char *x32, const unsigned char *y32, void *data)
A pointer to a function that hashes an EC point to obtain an ECDH secret.
SECP256K1_API const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256
An implementation of SHA256 hash function that applies to compressed public key.
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).
secp256k1_hash_ctx hash_ctx
Definition secp256k1.c:63
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition group.h:16
secp256k1_fe x
Definition group.h:17
secp256k1_fe y
Definition group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition group.h:28
Opaque data structure that holds a parsed and valid public key.
Definition secp256k1.h:62
A scalar modulo the group order of the secp256k1 curve.
Definition scalar_4x64.h:13