bool elliptic_curve_key::verify(hash_digest hash, const data_chunk& signature) { BITCOIN_ASSERT(key_ != nullptr); // SSL likes a reversed hash std::reverse(hash.begin(), hash.end()); // -1 = error, 0 = bad sig, 1 = good if (ECDSA_verify(0, hash.data(), hash.size(), signature.data(), signature.size(), key_) == 1) return true; return false; }
bool verify_signature(const ec_point& public_key, hash_digest hash, const data_chunk& signature) { std::reverse(hash.begin(), hash.end()); init.init(); return 1 == secp256k1_ecdsa_verify( hash.data(), hash.size(), signature.data(), signature.size(), public_key.data(), public_key.size() ); }
data_chunk elliptic_curve_key::sign(hash_digest hash) const { BITCOIN_ASSERT(key_ != nullptr); // SSL likes a reversed hash std::reverse(hash.begin(), hash.end()); data_chunk signature(ECDSA_size(key_)); unsigned int signature_length = signature.size(); if (!ECDSA_sign(0, hash.data(), hash.size(), signature.data(), &signature_length, key_)) return data_chunk(); signature.resize(signature_length); return signature; }
data_chunk sign(ec_secret secret, hash_digest hash, ec_secret nonce) { std::reverse(hash.begin(), hash.end()); init.init(); int out_size = 72; data_chunk signature(out_size); if (!verify_private_key(nonce)) // Needed because of upstream bug return data_chunk(); bool valid = secp256k1_ecdsa_sign(hash.data(), hash.size(), signature.data(), &out_size, secret.data(), nonce.data()) == 1; if (!valid) return data_chunk(); signature.resize(out_size); return signature; }
BC_API ec_secret create_nonce(ec_secret secret, hash_digest hash) { std::reverse(hash.begin(), hash.end()); init.init(); hash_digest K {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}; hash_digest V {{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }}; K = hmac_sha256_hash(V + byte_array<1>{{0x00}} + secret + hash, K); V = hmac_sha256_hash(V, K); K = hmac_sha256_hash(V + byte_array<1>{{0x01}} + secret + hash, K); V = hmac_sha256_hash(V, K); while (true) { V = hmac_sha256_hash(V, K); if (verify_private_key(V)) return V; K = hmac_sha256_hash(V + byte_array<1>{{0x00}}, K); V = hmac_sha256_hash(V, K); } }
static std::string pack_hash(hash_digest in) { return std::string(in.begin(), in.end()); }
void hash_number::set_hash(const hash_digest& hash) { std::copy(hash.begin(), hash.end(), hash_.begin()); }
void append_hash(czmqpp::message& message, const hash_digest& hash) { message.append(data_chunk(hash.begin(), hash.end())); }
// Bitcoin hash format (these are all reversed): std::string encode_hash(hash_digest hash) { std::reverse(hash.begin(), hash.end()); return encode_base16(hash); }