mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-12 23:13:25 +02:00
7262adb4b4 Merge bitcoin-core/secp256k1#1841: gha: Bump deprecated GHA workflow dependencies c5cd9d6d9a gha: Bump deprecated GHA workflow dependencies 95b702de34 Merge bitcoin-core/secp256k1#1839: ecdsa: VERIFY_CHECK result of _fe_set_b32_limit 634215f3fc Merge bitcoin-core/secp256k1#1837: tests: Fix function pointer initialization C89 error in ellswift tests 43fca0ff55 ecdsa: VERIFY_CHECK result of _fe_set_b32_limit b84635ed3b tests: Fix C89 function pointer initialization in ellswift tests ffc25a2731 Merge bitcoin-core/secp256k1#1834: ecmult: Document and test ng=NULL in ecmult 3a403639dc eckey: Call ecmult with NULL instead of zero scalar 7e68c0c88b ecmult: Document and test ng=NULL in ecmult 1aafe15139 Merge bitcoin-core/secp256k1#1777: Make SHA256 compression runtime pluggable b9cb1cbfd7 Merge bitcoin-core/secp256k1#1824: util: introduce and use `ARRAY_SIZE` macro 4d92a083bc sha256: speed up writes using multi-block compression 0753f8b909 Add API to override SHA256 compression at runtime fdb6a91a5e Introduce hash context to support pluggable SHA256 compression c0a2aba088 Merge bitcoin-core/secp256k1#1811: bench: Update help functions in bench and bench_internal 10f546a2c0 Merge bitcoin-core/secp256k1#1832: testrand: Remove testrand_finish 8d0eda07e9 testrand: Remove testrand_finish 95e6815843 Merge bitcoin-core/secp256k1#1825: hash: remove redundant `secp256k1_sha256_initialize` in tagged hash midstate functions f48b1bfa5d hash: add midstate initializer and use it for tagged hashes 3019186a6d Merge bitcoin-core/secp256k1#1829: ci: Fix leftover use of old ECMULTGENPRECISION 79e9f25237 ci: Fix leftover use of old ECMULTGENPRECISION dfe042feb2 Merge bitcoin-core/secp256k1#1828: Revert "ci, docker: Fix LLVM repository signature failure" 76e92cfeea Revert "ci, docker: Fix LLVM repository signature failure" ac561601b8 Merge bitcoin-core/secp256k1#1760: cmake: Add dynamic test discovery to improve parallelism c7a7f732bd Merge bitcoin-core/secp256k1#1821: ellswift: fix overflow flag handling in secp256k1_ellswift_xdh 921b9711ea util: introduce and use `ARRAY_SIZE` macro b99a94c382 Add tests for bad scalar inputs in ellswift XDH 307b49f1b9 ellswift: fix overflow flag handling in secp256k1_ellswift_xdh 322d0a4358 Merge bitcoin-core/secp256k1#1823: ci: Load Docker image by ID from builder step ed02466d3f ci: Load Docker image by ID from builder step c49c9be504 bench: Update help functions in bench and bench_internal 1d146ac3ed Merge bitcoin-core/secp256k1#1819: tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812) f47bbc07f0 test: add unit tests for secp256k1_scalar_check_overflow d071aa56d5 Merge bitcoin-core/secp256k1#1815: refactor: remove unnecessary `malloc` result casts 99ab4a105e Merge bitcoin-core/secp256k1#1817: ci: Disable Docker build summary generation c5da3bde9c Merge bitcoin-core/secp256k1#1818: ci: Enforce base-10 evaluation 97de5120cf Merge bitcoin-core/secp256k1#1804: test: show both CMake and Autotools usage for ctime_tests 4fb7ccf5d4 ci: Enforce base-10 evaluation 3ae72e7867 ci: Disable Docker build summary generation 97b3c47849 refactor: remove unnecessary `malloc` result casts 1bc74a22f8 test: show both Autotools and CMake usage for ctime_tests 8354618e02 cmake: Set `LABELS` property for tests 29f26ec3cf cmake: Integrate DiscoverTests and normalize test names f95b263f23 cmake: Add DiscoverTests module 4ac651144b cmake, refactor: Deduplicate test-related code git-subtree-dir: src/secp256k1 git-subtree-split: 7262adb4b40074201fb30847035a82b8d742f350
230 lines
5.8 KiB
C++
230 lines
5.8 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_HASH_H
|
|
#define BITCOIN_HASH_H
|
|
|
|
#include <attributes.h>
|
|
#include <crypto/common.h>
|
|
#include <crypto/ripemd160.h>
|
|
#include <crypto/sha256.h>
|
|
#include <prevector.h>
|
|
#include <serialize.h>
|
|
#include <span.h>
|
|
#include <uint256.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
typedef uint256 ChainCode;
|
|
|
|
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
|
|
class CHash256 {
|
|
private:
|
|
CSHA256 sha;
|
|
public:
|
|
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
|
|
|
|
void Finalize(std::span<unsigned char> output) {
|
|
assert(output.size() == OUTPUT_SIZE);
|
|
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
|
sha.Finalize(buf);
|
|
sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
|
|
}
|
|
|
|
CHash256& Write(std::span<const unsigned char> input) {
|
|
sha.Write(input.data(), input.size());
|
|
return *this;
|
|
}
|
|
|
|
CHash256& Reset() {
|
|
sha.Reset();
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
|
|
class CHash160 {
|
|
private:
|
|
CSHA256 sha;
|
|
public:
|
|
static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
|
|
|
|
void Finalize(std::span<unsigned char> output) {
|
|
assert(output.size() == OUTPUT_SIZE);
|
|
unsigned char buf[CSHA256::OUTPUT_SIZE];
|
|
sha.Finalize(buf);
|
|
CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
|
|
}
|
|
|
|
CHash160& Write(std::span<const unsigned char> input) {
|
|
sha.Write(input.data(), input.size());
|
|
return *this;
|
|
}
|
|
|
|
CHash160& Reset() {
|
|
sha.Reset();
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/** Compute the 256-bit hash of an object. */
|
|
template<typename T>
|
|
inline uint256 Hash(const T& in1)
|
|
{
|
|
uint256 result;
|
|
CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
|
|
return result;
|
|
}
|
|
|
|
/** Compute the 256-bit hash of the concatenation of two objects. */
|
|
template<typename T1, typename T2>
|
|
inline uint256 Hash(const T1& in1, const T2& in2) {
|
|
uint256 result;
|
|
CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
|
|
return result;
|
|
}
|
|
|
|
/** Compute the 160-bit hash an object. */
|
|
template<typename T1>
|
|
inline uint160 Hash160(const T1& in1)
|
|
{
|
|
uint160 result;
|
|
CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
|
|
return result;
|
|
}
|
|
|
|
/** A writer stream (for serialization) that computes a 256-bit hash. */
|
|
class HashWriter
|
|
{
|
|
private:
|
|
CSHA256 ctx;
|
|
|
|
public:
|
|
void write(std::span<const std::byte> src)
|
|
{
|
|
ctx.Write(UCharCast(src.data()), src.size());
|
|
}
|
|
|
|
/** Compute the double-SHA256 hash of all data written to this object.
|
|
*
|
|
* Invalidates this object.
|
|
*/
|
|
uint256 GetHash() {
|
|
uint256 result;
|
|
ctx.Finalize(result.begin());
|
|
ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
|
|
return result;
|
|
}
|
|
|
|
/** Compute the SHA256 hash of all data written to this object.
|
|
*
|
|
* Invalidates this object.
|
|
*/
|
|
uint256 GetSHA256() {
|
|
uint256 result;
|
|
ctx.Finalize(result.begin());
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Returns the first 64 bits from the resulting hash.
|
|
*/
|
|
inline uint64_t GetCheapHash() {
|
|
uint256 result = GetHash();
|
|
return ReadLE64(result.begin());
|
|
}
|
|
|
|
template <typename T>
|
|
HashWriter& operator<<(const T& obj)
|
|
{
|
|
::Serialize(*this, obj);
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/** Reads data from an underlying stream, while hashing the read data. */
|
|
template <typename Source>
|
|
class HashVerifier : public HashWriter
|
|
{
|
|
private:
|
|
Source& m_source;
|
|
|
|
public:
|
|
explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
|
|
|
|
void read(std::span<std::byte> dst)
|
|
{
|
|
m_source.read(dst);
|
|
this->write(dst);
|
|
}
|
|
|
|
void ignore(size_t num_bytes)
|
|
{
|
|
std::byte data[1024];
|
|
while (num_bytes > 0) {
|
|
size_t now = std::min<size_t>(num_bytes, 1024);
|
|
read({data, now});
|
|
num_bytes -= now;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
HashVerifier<Source>& operator>>(T&& obj)
|
|
{
|
|
::Unserialize(*this, obj);
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/** Writes data to an underlying source stream, while hashing the written data. */
|
|
template <typename Source>
|
|
class HashedSourceWriter : public HashWriter
|
|
{
|
|
private:
|
|
Source& m_source;
|
|
|
|
public:
|
|
explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
|
|
|
|
void write(std::span<const std::byte> src)
|
|
{
|
|
m_source.write(src);
|
|
HashWriter::write(src);
|
|
}
|
|
|
|
template <typename T>
|
|
HashedSourceWriter& operator<<(const T& obj)
|
|
{
|
|
::Serialize(*this, obj);
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
/** Single-SHA256 a 32-byte input (represented as uint256). */
|
|
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
|
|
|
|
unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash);
|
|
|
|
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
|
|
|
|
/** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
|
|
*
|
|
* The returned object will have SHA256(tag) written to it twice (= 64 bytes).
|
|
* A tagged hash can be computed by feeding the message into this object, and
|
|
* then calling HashWriter::GetSHA256().
|
|
*/
|
|
HashWriter TaggedHash(const std::string& tag);
|
|
|
|
/** Compute the 160-bit RIPEMD-160 hash of an array. */
|
|
inline uint160 RIPEMD160(std::span<const unsigned char> data)
|
|
{
|
|
uint160 result;
|
|
CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
|
|
return result;
|
|
}
|
|
|
|
#endif // BITCOIN_HASH_H
|