Skip to content

Commit

Permalink
better code style (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni authored Jan 27, 2024
1 parent cd5ba16 commit 5f4169c
Showing 1 changed file with 33 additions and 65 deletions.
98 changes: 33 additions & 65 deletions src/math/elliptic_curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,17 @@ bool ec_add(ec_secret& left, ec_secret const& right) {
right.data()) == 1;
}

bool ec_multiply(ec_compressed& point, ec_secret const& secret)
{
bool ec_multiply(ec_compressed& point, ec_secret const& secret) {
auto const context = verification.context();
return ec_multiply(context, point, secret);
}

bool ec_multiply(ec_uncompressed& point, ec_secret const& secret)
{
bool ec_multiply(ec_uncompressed& point, ec_secret const& secret) {
auto const context = verification.context();
return ec_multiply(context, point, secret);
}

bool ec_multiply(ec_secret& left, ec_secret const& right)
{
bool ec_multiply(ec_secret& left, ec_secret const& right) {
auto const context = verification.context();
return secp256k1_ec_privkey_tweak_mul(context, left.data(),
right.data()) == 1;
Expand All @@ -138,50 +135,43 @@ bool ec_multiply(ec_secret& left, ec_secret const& right)
// Convert keys
// ----------------------------------------------------------------------------

bool compress(ec_compressed& out, const ec_uncompressed& point)
{
bool compress(ec_compressed& out, const ec_uncompressed& point) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point) && serialize(context, out, pubkey);
}

bool decompress(ec_uncompressed& out, const ec_compressed& point)
{
bool decompress(ec_uncompressed& out, const ec_compressed& point) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point) && serialize(context, out, pubkey);
}

bool secret_to_public(ec_compressed& out, ec_secret const& secret)
{
bool secret_to_public(ec_compressed& out, ec_secret const& secret) {
auto const context = signing.context();
return secret_to_public(context, out, secret);
}

bool secret_to_public(ec_uncompressed& out, ec_secret const& secret)
{
bool secret_to_public(ec_uncompressed& out, ec_secret const& secret) {
auto const context = signing.context();
return secret_to_public(context, out, secret);
}

// Verify keys
// ----------------------------------------------------------------------------

bool verify(ec_secret const& secret)
{
bool verify(ec_secret const& secret) {
auto const context = verification.context();
return secp256k1_ec_seckey_verify(context, secret.data()) == 1;
}

bool verify(const ec_compressed& point)
{
bool verify(const ec_compressed& point) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point);
}

bool verify(const ec_uncompressed& point)
{
bool verify(const ec_uncompressed& point) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point);
Expand All @@ -190,66 +180,57 @@ bool verify(const ec_uncompressed& point)
// Detect public keys
// ----------------------------------------------------------------------------

bool is_compressed_key(data_slice point)
{
bool is_compressed_key(data_slice point) {
auto const size = point.size();
if (size != ec_compressed_size) {
return false;
}
}

auto const first = point.data()[0];
return first == compressed_even || first == compressed_odd;
}

bool is_uncompressed_key(data_slice point)
{
bool is_uncompressed_key(data_slice point) {
auto const size = point.size();
if (size != ec_uncompressed_size) {
return false;
}
}

auto const first = point.data()[0];
return first == uncompressed;
}

bool is_public_key(data_slice point)
{
bool is_public_key(data_slice point) {
return is_compressed_key(point) || is_uncompressed_key(point);
}

bool is_even_key(const ec_compressed& point)
{
bool is_even_key(const ec_compressed& point) {
return point.front() == ec_even_sign;
}

bool is_endorsement(const endorsement& endorsement)
{
bool is_endorsement(const endorsement& endorsement) {
auto const size = endorsement.size();
return size >= min_endorsement_size && size <= max_endorsement_size;
}

// DER parse/encode
// ----------------------------------------------------------------------------

bool parse_endorsement(uint8_t& sighash_type, der_signature& der_signature,
endorsement&& endorsement)
{
bool parse_endorsement(uint8_t& sighash_type, der_signature& der_signature, endorsement&& endorsement) {
if (endorsement.empty()) {
return false;
}
}

sighash_type = endorsement.back();
endorsement.pop_back();
der_signature = std::move(endorsement);
return true;
}

bool parse_signature(ec_signature& out, const der_signature& der_signature,
bool strict)
{
bool parse_signature(ec_signature& out, const der_signature& der_signature, bool strict) {
if (der_signature.empty()) {
return false;
}
}

bool valid;
secp256k1_ecdsa_signature parsed;
Expand All @@ -261,17 +242,16 @@ bool parse_signature(ec_signature& out, const der_signature& der_signature,
} else {
valid = ecdsa_signature_parse_der_lax(context, &parsed,
der_signature.data(), der_signature.size()) == 1;
}
}

if (valid) {
std::copy_n(std::begin(parsed.data), ec_signature_size, out.begin());
}
}

return valid;
}

bool encode_signature(der_signature& out, const ec_signature& signature)
{
bool encode_signature(der_signature& out, const ec_signature& signature) {
// Copy to avoid exposing external types.
secp256k1_ecdsa_signature sign;
std::copy_n(signature.begin(), ec_signature_size, std::begin(sign.data));
Expand All @@ -283,7 +263,7 @@ bool encode_signature(der_signature& out, const ec_signature& signature)
if (secp256k1_ecdsa_signature_serialize_der(context, out.data(), &size,
&sign) != 1) {
return false;
}
}

out.resize(size);
return true;
Expand All @@ -292,41 +272,33 @@ bool encode_signature(der_signature& out, const ec_signature& signature)
// EC sign/verify
// ----------------------------------------------------------------------------

bool sign(ec_signature& out, ec_secret const& secret, hash_digest const& hash)
{
bool sign(ec_signature& out, ec_secret const& secret, hash_digest const& hash) {
secp256k1_ecdsa_signature signature;
auto const context = signing.context();

if (secp256k1_ecdsa_sign(context, &signature, hash.data(), secret.data(),
secp256k1_nonce_function_rfc6979, nullptr) != 1) {
if (secp256k1_ecdsa_sign(context, &signature, hash.data(), secret.data(), secp256k1_nonce_function_rfc6979, nullptr) != 1) {
return false;
}
}

std::copy_n(std::begin(signature.data), out.size(), out.begin());
return true;
}

bool verify_signature(const ec_compressed& point, hash_digest const& hash,
const ec_signature& signature)
{
bool verify_signature(const ec_compressed& point, hash_digest const& hash, const ec_signature& signature) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point) &&
verify_signature(context, pubkey, hash, signature);
}

bool verify_signature(const ec_uncompressed& point, hash_digest const& hash,
const ec_signature& signature)
{
bool verify_signature(const ec_uncompressed& point, hash_digest const& hash, const ec_signature& signature) {
secp256k1_pubkey pubkey;
auto const context = verification.context();
return parse(context, pubkey, point) &&
verify_signature(context, pubkey, hash, signature);
}

bool verify_signature(data_slice point, hash_digest const& hash,
const ec_signature& signature)
{
bool verify_signature(data_slice point, hash_digest const& hash, const ec_signature& signature) {
// Copy to avoid exposing external types.
secp256k1_ecdsa_signature parsed;
std::copy_n(signature.begin(), ec_signature_size, std::begin(parsed.data));
Expand Down Expand Up @@ -367,16 +339,12 @@ bool sign_recoverable(recoverable_signature& out, ec_secret const& secret, hash_
return result;
}

bool recover_public(ec_compressed& out,
const recoverable_signature& recoverable, hash_digest const& hash)
{
bool recover_public(ec_compressed& out, const recoverable_signature& recoverable, hash_digest const& hash) {
auto const context = verification.context();
return recover_public(context, out, recoverable, hash);
}

bool recover_public(ec_uncompressed& out,
const recoverable_signature& recoverable, hash_digest const& hash)
{
bool recover_public(ec_uncompressed& out, const recoverable_signature& recoverable, hash_digest const& hash) {
auto const context = verification.context();
return recover_public(context, out, recoverable, hash);
}
Expand Down

0 comments on commit 5f4169c

Please sign in to comment.