diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-12-15 17:40:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-15 17:40:09 +0800 |
commit | c6069a627c42c21fc02d0770d39db9a9be45b180 (patch) | |
tree | 45b43eef0ead6f3bf83e0f0bec1c48b02eefc374 /crypto/secp256k1/ext.h | |
parent | 1f2176dedc369f31f77927c2743b64868bf26b3e (diff) | |
download | dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.gz dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.bz2 dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.lz dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.xz dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.tar.zst dexon-c6069a627c42c21fc02d0770d39db9a9be45b180.zip |
crypto, crypto/secp256k1: add CompressPubkey (#15626)
This adds the inverse to DecompressPubkey and improves a few minor
details in crypto/secp256k1.
Diffstat (limited to 'crypto/secp256k1/ext.h')
-rw-r--r-- | crypto/secp256k1/ext.h | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/crypto/secp256k1/ext.h b/crypto/secp256k1/ext.h index b0f30b73c..9b043c724 100644 --- a/crypto/secp256k1/ext.h +++ b/crypto/secp256k1/ext.h @@ -19,7 +19,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() { return secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); } -// secp256k1_ecdsa_recover_pubkey recovers the public key of an encoded compact signature. +// secp256k1_ext_ecdsa_recover recovers the public key of an encoded compact signature. // // Returns: 1: recovery was successful // 0: recovery was not successful @@ -27,7 +27,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() { // Out: pubkey_out: the serialized 65-byte public key of the signer (cannot be NULL) // In: sigdata: pointer to a 65-byte signature with the recovery id at the end (cannot be NULL) // msgdata: pointer to a 32-byte message (cannot be NULL) -static int secp256k1_ecdsa_recover_pubkey( +static int secp256k1_ext_ecdsa_recover( const secp256k1_context* ctx, unsigned char *pubkey_out, const unsigned char *sigdata, @@ -46,7 +46,7 @@ static int secp256k1_ecdsa_recover_pubkey( return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED); } -// secp256k1_ecdsa_verify_enc verifies an encoded compact signature. +// secp256k1_ext_ecdsa_verify verifies an encoded compact signature. // // Returns: 1: signature is valid // 0: signature is invalid @@ -55,7 +55,7 @@ static int secp256k1_ecdsa_recover_pubkey( // msgdata: pointer to a 32-byte message (cannot be NULL) // pubkeydata: pointer to public key data (cannot be NULL) // pubkeylen: length of pubkeydata -static int secp256k1_ecdsa_verify_enc( +static int secp256k1_ext_ecdsa_verify( const secp256k1_context* ctx, const unsigned char *sigdata, const unsigned char *msgdata, @@ -74,28 +74,34 @@ static int secp256k1_ecdsa_verify_enc( return secp256k1_ecdsa_verify(ctx, &sig, msgdata, &pubkey); } -// secp256k1_decompress_pubkey decompresses a public key. +// secp256k1_ext_reencode_pubkey decodes then encodes a public key. It can be used to +// convert between public key formats. The input/output formats are chosen depending on the +// length of the input/output buffers. // -// Returns: 1: public key is valid -// 0: public key is invalid +// Returns: 1: conversion successful +// 0: conversion unsuccessful // Args: ctx: pointer to a context object (cannot be NULL) -// Out: pubkey_out: the serialized 65-byte public key (cannot be NULL) -// In: pubkeydata: pointer to 33 bytes of compressed public key data (cannot be NULL) -static int secp256k1_decompress_pubkey( +// Out: out: output buffer that will contain the reencoded key (cannot be NULL) +// In: outlen: length of out (33 for compressed keys, 65 for uncompressed keys) +// pubkeydata: the input public key (cannot be NULL) +// pubkeylen: length of pubkeydata +static int secp256k1_ext_reencode_pubkey( const secp256k1_context* ctx, - unsigned char *pubkey_out, - const unsigned char *pubkeydata + unsigned char *out, + size_t outlen, + const unsigned char *pubkeydata, + size_t pubkeylen ) { secp256k1_pubkey pubkey; - if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, 33)) { + if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) { return 0; } - size_t outputlen = 65; - return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED); + unsigned int flag = (outlen == 33) ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED; + return secp256k1_ec_pubkey_serialize(ctx, out, &outlen, &pubkey, flag); } -// secp256k1_pubkey_scalar_mul multiplies a point by a scalar in constant time. +// secp256k1_ext_scalar_mul multiplies a point by a scalar in constant time. // // Returns: 1: multiplication was successful // 0: scalar was invalid (zero or overflow) @@ -104,7 +110,7 @@ static int secp256k1_decompress_pubkey( // In: point: pointer to a 64-byte public point, // encoded as two 256bit big-endian numbers. // scalar: a 32-byte scalar with which to multiply the point -int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) { +int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) { int ret = 0; int overflow = 0; secp256k1_fe feX, feY; |