github.com/guiltylotus/go-ethereum@v1.9.7/crypto/secp256k1/libsecp256k1/src/eckey_impl.h (about) 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 } 40 secp256k1_fe_normalize_var(&elem->x); 41 secp256k1_fe_normalize_var(&elem->y); 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 54 static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak) { 55 secp256k1_scalar_add(key, key, tweak); 56 if (secp256k1_scalar_is_zero(key)) { 57 return 0; 58 } 59 return 1; 60 } 61 62 static int secp256k1_eckey_pubkey_tweak_add(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak) { 63 secp256k1_gej pt; 64 secp256k1_scalar one; 65 secp256k1_gej_set_ge(&pt, key); 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 } 72 secp256k1_ge_set_gej(key, &pt); 73 return 1; 74 } 75 76 static int secp256k1_eckey_privkey_tweak_mul(secp256k1_scalar *key, const secp256k1_scalar *tweak) { 77 if (secp256k1_scalar_is_zero(tweak)) { 78 return 0; 79 } 80 81 secp256k1_scalar_mul(key, key, tweak); 82 return 1; 83 } 84 85 static int secp256k1_eckey_pubkey_tweak_mul(const secp256k1_ecmult_context *ctx, secp256k1_ge *key, const secp256k1_scalar *tweak) { 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); 93 secp256k1_gej_set_ge(&pt, key); 94 secp256k1_ecmult(ctx, &pt, &pt, tweak, &zero); 95 secp256k1_ge_set_gej(key, &pt); 96 return 1; 97 } 98 99 #endif