Dash Core  0.12.2.1
P2P Digital Currency
eckey_impl.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille *
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_ECKEY_IMPL_H_
8 #define _SECP256K1_ECKEY_IMPL_H_
9 
10 #include "eckey.h"
11 
12 #include "scalar.h"
13 #include "field.h"
14 #include "group.h"
15 #include "ecmult_gen.h"
16 
17 static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
18  if (size == 33 && (pub[0] == 0x02 || pub[0] == 0x03)) {
19  secp256k1_fe x;
20  return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == 0x03);
21  } else if (size == 65 && (pub[0] == 0x04 || pub[0] == 0x06 || pub[0] == 0x07)) {
22  secp256k1_fe x, y;
23  if (!secp256k1_fe_set_b32(&x, pub+1) || !secp256k1_fe_set_b32(&y, pub+33)) {
24  return 0;
25  }
26  secp256k1_ge_set_xy(elem, &x, &y);
27  if ((pub[0] == 0x06 || pub[0] == 0x07) && secp256k1_fe_is_odd(&y) != (pub[0] == 0x07)) {
28  return 0;
29  }
30  return secp256k1_ge_is_valid_var(elem);
31  } else {
32  return 0;
33  }
34 }
35 
36 static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed) {
37  if (secp256k1_ge_is_infinity(elem)) {
38  return 0;
39  }
42  secp256k1_fe_get_b32(&pub[1], &elem->x);
43  if (compressed) {
44  *size = 33;
45  pub[0] = 0x02 | (secp256k1_fe_is_odd(&elem->y) ? 0x01 : 0x00);
46  } else {
47  *size = 65;
48  pub[0] = 0x04;
49  secp256k1_fe_get_b32(&pub[33], &elem->y);
50  }
51  return 1;
52 }
53 
55  secp256k1_scalar_add(key, key, tweak);
57  return 0;
58  }
59  return 1;
60 }
61 
63  secp256k1_gej pt;
64  secp256k1_scalar one;
66  secp256k1_scalar_set_int(&one, 1);
67  secp256k1_ecmult(ctx, &pt, &pt, &one, tweak);
68 
69  if (secp256k1_gej_is_infinity(&pt)) {
70  return 0;
71  }
73  return 1;
74 }
75 
77  if (secp256k1_scalar_is_zero(tweak)) {
78  return 0;
79  }
80 
81  secp256k1_scalar_mul(key, key, tweak);
82  return 1;
83 }
84 
86  secp256k1_scalar zero;
87  secp256k1_gej pt;
88  if (secp256k1_scalar_is_zero(tweak)) {
89  return 0;
90  }
91 
92  secp256k1_scalar_set_int(&zero, 0);
94  secp256k1_ecmult(ctx, &pt, &pt, tweak, &zero);
96  return 1;
97 }
98 
99 #endif
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed)
Definition: eckey_impl.h:36
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
static int secp256k1_eckey_pubkey_tweak_mul(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak)
Definition: eckey_impl.h:85
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
static int secp256k1_eckey_pubkey_tweak_add(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak)
Definition: eckey_impl.h:62
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak)
Definition: eckey_impl.h:54
static secp256k1_context * ctx
Definition: tests.c:42
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
secp256k1_fe x
Definition: group.h:15
static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak)
Definition: eckey_impl.h:76
static int secp256k1_ge_set_xo_var(secp256k1_ge *r, const secp256k1_fe *x, int odd)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static void secp256k1_ge_set_xy(secp256k1_ge *r, const secp256k1_fe *x, const secp256k1_fe *y)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
Definition: eckey_impl.h:17
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
static int secp256k1_ge_is_valid_var(const secp256k1_ge *a)
secp256k1_fe y
Definition: group.h:16