Add TaggedHash function (BIP 340)

This adds the TaggedHash function as defined by BIP340 to the hash module, which
is used in BIP340 and BIP341 to produce domain-separated hashes.
This commit is contained in:
Pieter Wuille
2020-09-11 14:32:50 -07:00
parent 450d2b2371
commit 9eb590894f
2 changed files with 19 additions and 0 deletions

View File

@@ -6,6 +6,7 @@
#include <crypto/common.h>
#include <crypto/hmac_sha512.h>
#include <string>
inline uint32_t ROTL32(uint32_t x, int8_t r)
{
@@ -84,3 +85,12 @@ uint256 SHA256Uint256(const uint256& input)
CSHA256().Write(input.begin(), 32).Finalize(result.begin());
return result;
}
CHashWriter TaggedHash(const std::string& tag)
{
CHashWriter writer(SER_GETHASH, 0);
uint256 taghash;
CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
writer << taghash << taghash;
return writer;
}