Dash Core  0.12.2.1
P2P Digital Currency
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 http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #ifndef _SECP256K1_MODULE_ECDH_MAIN_
8 #define _SECP256K1_MODULE_ECDH_MAIN_
9 
10 #include "include/secp256k1_ecdh.h"
11 #include "ecmult_const_impl.h"
12 
13 int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *result, const secp256k1_pubkey *point, const unsigned char *scalar) {
14  int ret = 0;
15  int overflow = 0;
16  secp256k1_gej res;
17  secp256k1_ge pt;
19  ARG_CHECK(result != NULL);
20  ARG_CHECK(point != NULL);
21  ARG_CHECK(scalar != NULL);
22  (void)ctx;
23 
24  secp256k1_pubkey_load(ctx, &pt, point);
25  secp256k1_scalar_set_b32(&s, scalar, &overflow);
26  if (overflow || secp256k1_scalar_is_zero(&s)) {
27  ret = 0;
28  } else {
29  unsigned char x[32];
30  unsigned char y[1];
32 
33  secp256k1_ecmult_const(&res, &pt, &s);
34  secp256k1_ge_set_gej(&pt, &res);
35  /* Compute a hash of the point in compressed form
36  * Note we cannot use secp256k1_eckey_pubkey_serialize here since it does not
37  * expect its output to be secret and has a timing sidechannel. */
40  secp256k1_fe_get_b32(x, &pt.x);
41  y[0] = 0x02 | secp256k1_fe_is_odd(&pt.y);
42 
44  secp256k1_sha256_write(&sha, y, sizeof(y));
45  secp256k1_sha256_write(&sha, x, sizeof(x));
47  ret = 1;
48  }
49 
51  return ret;
52 }
53 
54 #endif
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q)
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
static void secp256k1_sha256_finalize(secp256k1_sha256_t *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256_t *hash, const unsigned char *data, size_t size)
#define ARG_CHECK(cond)
Definition: secp256k1.c:23
static secp256k1_context * ctx
Definition: tests.c:42
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
static void secp256k1_sha256_initialize(secp256k1_sha256_t *hash)
static void secp256k1_scalar_clear(secp256k1_scalar *r)
secp256k1_fe x
Definition: group.h:15
int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *result, const secp256k1_pubkey *point, const unsigned char *scalar)
Definition: main_impl.h:13
static void secp256k1_fe_normalize(secp256k1_fe *r)
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:119
secp256k1_fe y
Definition: group.h:16
result
Definition: rpcuser.py:37