1 #include "secp256k1/include/secp256k1.h"
15 secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
31 secp256k1_context_destroy(ctx);
38 secp256k1_pubkey pubkey;
40 assert((
int)*in_outlen == (compressed ? 33 : 65));
41 memset(public_key, 0, *in_outlen);
43 if (!secp256k1_ec_pubkey_create(
secp256k1_ctx, &pubkey, (
const unsigned char*)private_key)) {
47 if (!secp256k1_ec_pubkey_serialize(
secp256k1_ctx, public_key, in_outlen, &pubkey, compressed)) {
57 return secp256k1_ec_privkey_tweak_add(
secp256k1_ctx, (
unsigned char*)private_key, (
const unsigned char*)tweak);
63 secp256k1_pubkey pubkey;
66 if (!secp256k1_ec_pubkey_parse(
secp256k1_ctx, &pubkey, public_key_inout, 33))
69 if (!secp256k1_ec_pubkey_tweak_add(
secp256k1_ctx, &pubkey, (
const unsigned char*)tweak))
72 if (!secp256k1_ec_pubkey_serialize(
secp256k1_ctx, public_key_inout, &out, &pubkey, SECP256K1_EC_COMPRESSED))
82 return secp256k1_ec_seckey_verify(
secp256k1_ctx, (
const unsigned char*)private_key);
87 secp256k1_pubkey pubkey;
90 if (!secp256k1_ec_pubkey_parse(
secp256k1_ctx, &pubkey, public_key, compressed ? 33 : 65)) {
91 memset(&pubkey, 0,
sizeof(pubkey));
95 memset(&pubkey, 0,
sizeof(pubkey));
99 btc_bool ecc_sign(
const uint8_t* private_key,
const uint8_t* hash,
unsigned char* sigder,
size_t* outlen)
103 secp256k1_ecdsa_signature sig;
104 if (!secp256k1_ecdsa_sign(
secp256k1_ctx, &sig, hash, private_key, secp256k1_nonce_function_rfc6979, NULL))
107 if (!secp256k1_ecdsa_signature_serialize_der(
secp256k1_ctx, sigder, outlen, &sig))
117 secp256k1_ecdsa_signature sig;
118 secp256k1_pubkey pubkey;
120 if (!secp256k1_ec_pubkey_parse(
secp256k1_ctx, &pubkey, public_key, compressed ? 33 : 65))
123 if (!secp256k1_ecdsa_signature_parse_der(
secp256k1_ctx, &sig, sigder, siglen))
126 return secp256k1_ecdsa_verify(
secp256k1_ctx, &sig, hash, &pubkey);
void ecc_get_pubkey(const uint8_t *private_key, uint8_t *public_key, size_t *in_outlen, btc_bool compressed)
get public key from given private key
void ecc_start(void)
init static ecc context
btc_bool random_bytes(uint8_t *buf, uint32_t len, const uint8_t update_seed)
static secp256k1_context * secp256k1_ctx
btc_bool ecc_verify_pubkey(const uint8_t *public_key, btc_bool compressed)
verifies a given public key (compressed[33] or uncompressed[65] bytes)
btc_bool ecc_public_key_tweak_add(uint8_t *public_key_inout, const uint8_t *tweak)
ec mul tweak on given public key
btc_bool ecc_private_key_tweak_add(uint8_t *private_key, const uint8_t *tweak)
ec mul tweak on given private key
btc_bool ecc_sign(const uint8_t *private_key, const uint8_t *hash, unsigned char *sigder, size_t *outlen)
btc_bool ecc_verify_privatekey(const uint8_t *private_key)
verifies a given 32byte key
void ecc_stop(void)
destroys the static ecc context
btc_bool ecc_verify_sig(const uint8_t *public_key, btc_bool compressed, const uint8_t *hash, unsigned char *sigder, size_t siglen)