UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
bip143.hpp
Go to the documentation of this file.
1#ifndef SECP256K1_BIP143_HPP
2#define SECP256K1_BIP143_HPP
3
4// ============================================================================
5// BIP-143: Transaction Signature Verification for Version 0 Witness Program
6// ============================================================================
7// Implements the SegWit v0 sighash algorithm as specified in BIP-143.
8// This defines how transaction digests are computed for signing SegWit v0
9// inputs (P2WPKH and P2WSH), replacing the legacy sighash algorithm.
10//
11// The BIP-143 digest commits to:
12// - nVersion, hashPrevouts, hashSequence
13// - outpoint (txid + vout)
14// - scriptCode, value
15// - nSequence, hashOutputs
16// - nLockTime, nHashType
17//
18// This prevents the quadratic hashing problem of legacy transactions.
19//
20// Reference: BIP-143, https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki
21// ============================================================================
22
23#include <array>
24#include <cstdint>
25#include <cstddef>
26#include <vector>
27
28namespace secp256k1 {
29
30// Sighash types (same as legacy, used in witness sighash)
31enum class SighashType : std::uint32_t {
32 ALL = 0x01,
33 NONE = 0x02,
34 SINGLE = 0x03,
35 ANYONECANPAY = 0x80,
36};
37
38// A single transaction outpoint (txid + output index)
39struct Outpoint {
40 std::array<std::uint8_t, 32> txid; // LE txid
41 std::uint32_t vout;
42};
43
44// A single transaction output (value + scriptPubKey)
45struct TxOutput {
46 std::uint64_t value;
47 std::vector<std::uint8_t> script_pubkey;
48};
49
50// Precomputed hash components for BIP-143 sighash.
51// Reusable across multiple inputs of the same transaction.
53 std::uint32_t version;
54 std::array<std::uint8_t, 32> hash_prevouts; // dSHA256 of all outpoints
55 std::array<std::uint8_t, 32> hash_sequence; // dSHA256 of all sequences
56 std::array<std::uint8_t, 32> hash_outputs; // dSHA256 of all outputs
57 std::uint32_t locktime;
58};
59
60// Compute hashPrevouts: double-SHA256 of all outpoints concatenated.
61std::array<std::uint8_t, 32> bip143_hash_prevouts(
62 const Outpoint* outpoints, std::size_t count) noexcept;
63
64// Compute hashSequence: double-SHA256 of all nSequence values concatenated.
65std::array<std::uint8_t, 32> bip143_hash_sequence(
66 const std::uint32_t* sequences, std::size_t count) noexcept;
67
68// Compute hashOutputs: double-SHA256 of all outputs serialized.
69std::array<std::uint8_t, 32> bip143_hash_outputs(
70 const TxOutput* outputs, std::size_t count) noexcept;
71
72// Build reusable preimage components from transaction data.
74 std::uint32_t version,
75 const Outpoint* outpoints, std::size_t input_count,
76 const std::uint32_t* sequences,
77 const TxOutput* outputs, std::size_t output_count,
78 std::uint32_t locktime) noexcept;
79
80// Compute BIP-143 sighash for a specific input.
81// script_code: the scriptCode for this input (P2WPKH: OP_DUP OP_HASH160...;
82// P2WSH: the witness script).
83// value: the value in satoshis of the output being spent.
84// sighash_type: SIGHASH_ALL, SIGHASH_NONE, SIGHASH_SINGLE, |ANYONECANPAY
85//
86// For ANYONECANPAY: hashPrevouts and hashSequence are zeroed.
87// For NONE: hashOutputs is zeroed.
88// For SINGLE: hashOutputs is hash of the output at same index (or zeros if
89// the index exceeds output count).
90std::array<std::uint8_t, 32> bip143_sighash(
91 const Bip143Preimage& preimage,
92 const Outpoint& outpoint,
93 const std::uint8_t* script_code, std::size_t script_code_len,
94 std::uint64_t value,
95 std::uint32_t sequence,
96 std::uint32_t sighash_type) noexcept;
97
98// Convenience: build P2WPKH scriptCode from a 20-byte pubkey hash.
99// Returns: OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY OP_CHECKSIG (25 bytes)
100std::array<std::uint8_t, 25> bip143_p2wpkh_script_code(
101 const std::uint8_t pubkey_hash[20]) noexcept;
102
103} // namespace secp256k1
104
105#endif // SECP256K1_BIP143_HPP
std::array< std::uint8_t, 32 > bip143_hash_prevouts(const Outpoint *outpoints, std::size_t count) noexcept
std::array< std::uint8_t, 25 > bip143_p2wpkh_script_code(const std::uint8_t pubkey_hash[20]) noexcept
std::array< std::uint8_t, 32 > bip143_hash_sequence(const std::uint32_t *sequences, std::size_t count) noexcept
std::array< std::uint8_t, 32 > bip143_sighash(const Bip143Preimage &preimage, const Outpoint &outpoint, const std::uint8_t *script_code, std::size_t script_code_len, std::uint64_t value, std::uint32_t sequence, std::uint32_t sighash_type) noexcept
Bip143Preimage bip143_build_preimage(std::uint32_t version, const Outpoint *outpoints, std::size_t input_count, const std::uint32_t *sequences, const TxOutput *outputs, std::size_t output_count, std::uint32_t locktime) noexcept
std::array< std::uint8_t, 32 > bip143_hash_outputs(const TxOutput *outputs, std::size_t count) noexcept
std::uint32_t locktime
Definition bip143.hpp:57
std::array< std::uint8_t, 32 > hash_prevouts
Definition bip143.hpp:54
std::array< std::uint8_t, 32 > hash_outputs
Definition bip143.hpp:56
std::array< std::uint8_t, 32 > hash_sequence
Definition bip143.hpp:55
std::uint32_t version
Definition bip143.hpp:53
std::uint32_t vout
Definition bip143.hpp:41
std::array< std::uint8_t, 32 > txid
Definition bip143.hpp:40
std::vector< std::uint8_t > script_pubkey
Definition bip143.hpp:47
std::uint64_t value
Definition bip143.hpp:46