Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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 https://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
17static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
18 if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
20 return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
21 } else if (size == 65 && (pub[0] == SECP256K1_TAG_PUBKEY_UNCOMPRESSED || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
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);
28 secp256k1_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
29 return 0;
30 }
31 return secp256k1_ge_is_valid_var(elem);
32 } else {
33 return 0;
34 }
35}
36
37static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed) {
38 if (secp256k1_ge_is_infinity(elem)) {
39 return 0;
40 }
41 secp256k1_fe_normalize_var(&elem->x);
42 secp256k1_fe_normalize_var(&elem->y);
43 secp256k1_fe_get_b32(&pub[1], &elem->x);
44 if (compressed) {
45 *size = 33;
46 pub[0] = secp256k1_fe_is_odd(&elem->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN;
47 } else {
48 *size = 65;
50 secp256k1_fe_get_b32(&pub[33], &elem->y);
51 }
52 return 1;
53}
54
55static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
56 secp256k1_scalar_add(key, key, tweak);
57 return !secp256k1_scalar_is_zero(key);
58}
59
60static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak) {
63 secp256k1_gej_set_ge(&pt, key);
64 secp256k1_scalar_set_int(&one, 1);
65 secp256k1_ecmult(&pt, &pt, &one, tweak);
66
67 if (secp256k1_gej_is_infinity(&pt)) {
68 return 0;
69 }
70 secp256k1_ge_set_gej(key, &pt);
71 return 1;
72}
73
74static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
75 int ret;
76 ret = !secp256k1_scalar_is_zero(tweak);
77
78 secp256k1_scalar_mul(key, key, tweak);
79 return ret;
80}
81
82static int secp256k1_eckey_pubkey_tweak_mul(secp256k1_ge *key, const secp256k1_scalar *tweak) {
85 if (secp256k1_scalar_is_zero(tweak)) {
86 return 0;
87 }
88
89 secp256k1_scalar_set_int(&zero, 0);
90 secp256k1_gej_set_ge(&pt, key);
91 secp256k1_ecmult(&pt, &pt, tweak, &zero);
92 secp256k1_ge_set_gej(key, &pt);
93 return 1;
94}
95
96#endif /* SECP256K1_ECKEY_IMPL_H */
std::string one()
uint64_t y
Definition sha3.cpp:34
#define SECP256K1_TAG_PUBKEY_EVEN
Definition secp256k1.h:205
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD
Definition secp256k1.h:209
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN
Definition secp256k1.h:208
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED
Definition secp256k1.h:207
#define SECP256K1_TAG_PUBKEY_ODD
Definition secp256k1.h:206
bool pub
CK_RV ret