Squashed 'src/secp256k1/' changes from 84973d393..0b7024185
[bitcoinplatinum.git] / src / eckey_impl.h
blob1ab9a68ec048c55a72e79674073e805e92487d63
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 **********************************************************************/
7 #ifndef SECP256K1_ECKEY_IMPL_H
8 #define SECP256K1_ECKEY_IMPL_H
10 #include "eckey.h"
12 #include "scalar.h"
13 #include "field.h"
14 #include "group.h"
15 #include "ecmult_gen.h"
17 static 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)) {
19 secp256k1_fe x;
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] == 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;
26 secp256k1_ge_set_xy(elem, &x, &y);
27 if ((pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD) &&
28 secp256k1_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
29 return 0;
31 return secp256k1_ge_is_valid_var(elem);
32 } else {
33 return 0;
37 static 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;
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;
49 pub[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED;
50 secp256k1_fe_get_b32(&pub[33], &elem->y);
52 return 1;
55 static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
56 secp256k1_scalar_add(key, key, tweak);
57 if (secp256k1_scalar_is_zero(key)) {
58 return 0;
60 return 1;
63 static int secp256k1_eckey_pubkey_tweak_add(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak) {
64 secp256k1_gej pt;
65 secp256k1_scalar one;
66 secp256k1_gej_set_ge(&pt, key);
67 secp256k1_scalar_set_int(&one, 1);
68 secp256k1_ecmult(ctx, &pt, &pt, &one, tweak);
70 if (secp256k1_gej_is_infinity(&pt)) {
71 return 0;
73 secp256k1_ge_set_gej(key, &pt);
74 return 1;
77 static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
78 if (secp256k1_scalar_is_zero(tweak)) {
79 return 0;
82 secp256k1_scalar_mul(key, key, tweak);
83 return 1;
86 static int secp256k1_eckey_pubkey_tweak_mul(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak) {
87 secp256k1_scalar zero;
88 secp256k1_gej pt;
89 if (secp256k1_scalar_is_zero(tweak)) {
90 return 0;
93 secp256k1_scalar_set_int(&zero, 0);
94 secp256k1_gej_set_ge(&pt, key);
95 secp256k1_ecmult(ctx, &pt, &pt, tweak, &zero);
96 secp256k1_ge_set_gej(key, &pt);
97 return 1;
100 #endif /* SECP256K1_ECKEY_IMPL_H */