UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
tagged_hash.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_TAGGED_HASH_HPP
2#define SECP256K1_TAGGED_HASH_HPP
3
4// ============================================================================
5// BIP-340 Tagged Hash -- Shared Utilities
6// ============================================================================
7// Provides cached tagged-hash midstates for BIP-340 (Schnorr) operations.
8// Used by both schnorr.cpp (fast path) and ct_sign.cpp (CT path).
9//
10// Eliminates duplication of make_tag_midstate / cached_tagged_hash / midstate
11// constants between the two translation units.
12// ============================================================================
13
14#include "secp256k1/sha256.hpp"
15#include <array>
16#include <cstddef>
17#include <cstdint>
18#include <string_view>
19
20namespace secp256k1::detail {
21
22// Build a SHA256 midstate from a BIP-340 tag string.
23// The midstate captures H(SHA256(tag) || SHA256(tag)) ready for further data.
24inline SHA256 make_tag_midstate(std::string_view tag) {
25 auto tag_hash = SHA256::hash(tag.data(), tag.size());
26 SHA256 ctx;
27 ctx.update(tag_hash.data(), 32);
28 ctx.update(tag_hash.data(), 32);
29 return ctx;
30}
31
32// Pre-computed BIP-340 midstates (constructed once, shared across TUs).
33inline const SHA256 g_aux_midstate = make_tag_midstate("BIP0340/aux");
34inline const SHA256 g_nonce_midstate = make_tag_midstate("BIP0340/nonce");
35inline const SHA256 g_challenge_midstate = make_tag_midstate("BIP0340/challenge");
36
37// Fast tagged hash using a cached midstate (avoids re-computing tag prefix).
38#if defined(__GNUC__) && !defined(__clang__)
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Warray-bounds"
41#endif
42inline std::array<uint8_t, 32> cached_tagged_hash(const SHA256& midstate,
43 const void* data,
44 std::size_t len) {
45 SHA256 ctx = midstate;
46 ctx.update(data, len);
47 return ctx.finalize();
48}
49#if defined(__GNUC__) && !defined(__clang__)
50#pragma GCC diagnostic pop
51#endif
52
53} // namespace secp256k1::detail
54
55#endif // SECP256K1_TAGGED_HASH_HPP
static digest_type hash(const void *data, std::size_t len) noexcept
Definition sha256.hpp:120
digest_type finalize() noexcept
Definition sha256.hpp:73
void update(const void *data, std::size_t len) noexcept
Definition sha256.hpp:43
const SHA256 g_nonce_midstate
std::array< uint8_t, 32 > cached_tagged_hash(const SHA256 &midstate, const void *data, std::size_t len)
const SHA256 g_aux_midstate
const SHA256 g_challenge_midstate
SHA256 make_tag_midstate(std::string_view tag)