1 /**********************************************************************
2 * Copyright (c) 2015 Andrew Poelstra *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef SECP256K1_MODULE_ECDH_MAIN_H
8 #define SECP256K1_MODULE_ECDH_MAIN_H
10 #include "include/secp256k1_ecdh.h"
11 #include "ecmult_const_impl.h"
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;
18 secp256k1_sha256_initialize(&sha);
19 secp256k1_sha256_write(&sha, &version, 1);
20 secp256k1_sha256_write(&sha, x, 32);
21 secp256k1_sha256_finalize(&sha, output);
26 const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_sha256 = ecdh_hash_function_sha256;
27 const secp256k1_ecdh_hash_function secp256k1_ecdh_hash_function_default = ecdh_hash_function_sha256;
29 int 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 VERIFY_CHECK(ctx != NULL);
36 ARG_CHECK(output != NULL);
37 ARG_CHECK(point != NULL);
38 ARG_CHECK(scalar != NULL);
40 hashfp = secp256k1_ecdh_hash_function_default;
43 secp256k1_pubkey_load(ctx, &pt, point);
44 secp256k1_scalar_set_b32(&s, scalar, &overflow);
45 if (overflow || secp256k1_scalar_is_zero(&s)) {
51 secp256k1_ecmult_const(&res, &pt, &s, 256);
52 secp256k1_ge_set_gej(&pt, &res);
54 /* Compute a hash of the point */
55 secp256k1_fe_normalize(&pt.x);
56 secp256k1_fe_normalize(&pt.y);
57 secp256k1_fe_get_b32(x, &pt.x);
58 secp256k1_fe_get_b32(y, &pt.y);
60 ret = hashfp(output, x, y, data);
63 secp256k1_scalar_clear(&s);
67 #endif /* SECP256K1_MODULE_ECDH_MAIN_H */