Expand description
§Consensus-Proof
Direct mathematical implementation of Bitcoin consensus rules from the Orange Paper.
This crate provides pure, side-effect-free functions that implement the mathematical specifications defined in the Orange Paper. It serves as the mathematical foundation for Bitcoin consensus validation.
§Architecture
The system follows a layered architecture:
- Orange Paper (mathematical specifications)
- Consensus Proof (this crate - direct implementation)
- Reference Node (minimal Bitcoin implementation)
- Developer SDK (developer-friendly interface)
§Design Principles
- Pure Functions: All functions are deterministic and side-effect-free
- Mathematical Accuracy: Direct implementation of Orange Paper specifications
- Exact Version Pinning: All consensus-critical dependencies pinned to exact versions
- No Consensus Rule Interpretation: Only mathematical implementation
§Usage
use blvm_consensus::transaction::check_transaction;
use blvm_consensus::types::*;
let transaction = Transaction {
version: 1,
inputs: vec![].into(),
outputs: vec![TransactionOutput {
value: 1000,
script_pubkey: vec![0x51],
}]
.into(),
lock_time: 0,
};
let result = check_transaction(&transaction).unwrap();Re-exports§
pub use config::reset_assume_valid_height;pub use config::set_assume_valid_height;pub use script::batch_verify_signatures;pub use script::clear_all_caches;pub use script::clear_hash_cache;pub use script::clear_script_cache;pub use script::clear_stack_pool;pub use script::disable_caching;pub use script::reset_benchmarking_state;pub use transaction_hash::clear_sighash_templates;
Modules§
- activation
- Unified fork activation: table and trait for “is fork X active at height H?”.
- bip113
- BIP113: Median Time-Past
- bip119
- BIP119: OP_CHECKTEMPLATEVERIFY (CTV)
- bip348
- BIP348: OP_CHECKSIGFROMSTACK (CSFS)
- bip_
validation - BIP Validation Rules
- block
- Block validation functions from Orange Paper Section 5.3 Section 5.3
- checkqueue
- Script verification queue: dedicated N-1 worker threads plus master as Nth.
- config
- Configuration for blvm-consensus
- constants
- Bitcoin consensus constants from Orange Paper
- crypto
- Cryptographic hash functions with CPU feature detection and optimizations
- economic
- Economic model functions from Orange Paper Section 7 Section 6
- error
- Error types for consensus validation
- locktime
- Shared locktime validation logic for BIP65 (CLTV) and BIP112 (CSV)
- mempool
- Mempool validation functions from Orange Paper Section 9
- mining
- Mining and block creation functions from Orange Paper Section 10.1
- opcodes
- Bitcoin Script Opcode Constants
- optimizations
- BLVM Runtime Optimization Passes
- orange_
paper_ constants - Orange Paper Section 4 symbols (C, H, M_MAX, etc.) — re-export from primitives constants.
- pow
- Proof of Work functions from Orange Paper Section 8 Section 7
- profile_
log - Non-blocking profile logging for IBD and consensus hot paths.
- reorganization
- Chain reorganization functions from Orange Paper Section 10.3
- script
- Script execution engine from Orange Paper Section 5.2
- script_
profile - Script sub-timing for IBD profiling (sighash, interpreter, multisig, P2PKH fast path). Used by block.rs to extend [PERF] with breakdown when profile feature is enabled.
- secp256k1_
backend - Secp256k1 backend abstraction.
- segwit
- Segregated Witness (SegWit) functions from Orange Paper Section 11.1
- sequence_
locks - Sequence lock calculation functions (BIP68)
- serialization
- Bitcoin wire format serialization/deserialization
- sigop
- Signature operation counting functions
- smallvec
- Small vectors in various sizes. These store a certain number of elements inline, and fall back to the heap for larger allocations. This can be a useful optimization for improving cache locality and reducing allocator traffic for workloads that fit within the inline buffer.
- taproot
- Taproot functions from Orange Paper Section 11.2
- test_
utils - Property test strategies and shared test fixtures.
- transaction
- Transaction validation functions from Orange Paper Section 5.1
- transaction_
hash - Transaction hash calculation for signature verification
- types
- Essential Bitcoin types for consensus validation
- utxo_
overlay - Zero-Copy UTXO Overlay
- version_
bits - BIP9-style version bits activation.
- witness
- Unified witness validation framework for SegWit (BIP141) and Taproot (BIP340/341/342)
Macros§
- hot_
inline - Inlining hints for hot functions
- profile_
log - Log a profile message without blocking. Drops the message if the channel is full.
- tx_
inputs - Helper macro to create Transaction inputs/outputs that works with both Vec and SmallVec
- tx_
outputs
Structs§
- Bip54
Boundary Timestamps - BIP54 timewarp: timestamps of boundary blocks for period-boundary checks.
- Block
- Block: ℬ = ℋ × 𝒯𝒳*
- Block
Context - Block validation context
- Block
Hash - Block hash: newtype wrapper for type safety
- Block
Header - Block Header: ℋ = ℤ × ℍ × ℍ × ℕ × ℕ × ℕ
- Block
Height - Block height: newtype wrapper for type safety
- Consensus
Proof - Consensus Proof - wrapper struct for consensus validation functions
- OutPoint
- OutPoint: 𝒪 = ℍ × ℕ
- Script
Context - Script execution context
- Shared
Byte String - Shareable script_pubkey for UTXO: small scripts use inline storage; longer use
Arc<[u8]>. Clone is cheap (inline copies up to 64 bytes, shared isArc::clone). Serde matchesByteString. - Time
Context - Time context for consensus validation
- Transaction
- Transaction: 𝒯𝒳 = ℕ × ℐ* × 𝒯* × ℕ
- Transaction
Input - Transaction Input: ℐ = 𝒪 × 𝕊 × ℕ
- Transaction
Output - Transaction Output: 𝒯 = ℤ × 𝕊
- UTXO
- UTXO: 𝒰 = ℤ × 𝕊 × ℕ
Enums§
- Consensus
Error - ForkId
- Stable identifier for each consensus-affecting fork (BIP or soft-fork bundle).
- Network
- Network type for consensus validation
- Validation
Result - Validation result
Constants§
- BIP16_
P2SH_ ACTIVATION_ MAINNET - BIP16: P2SH (Pay-to-Script-Hash) - Mainnet activation height
- BIP16_
P2SH_ ACTIVATION_ REGTEST - BIP16: P2SH (Pay-to-Script-Hash) - Regtest activation height
- BIP16_
P2SH_ ACTIVATION_ TESTNET - BIP16: P2SH (Pay-to-Script-Hash) - Testnet activation height
- BIP30_
DEACTIVATION_ MAINNET - BIP30: Duplicate Coinbase Prevention - Mainnet deactivation height
- BIP30_
DEACTIVATION_ REGTEST - BIP30: Duplicate Coinbase Prevention - Regtest deactivation height
- BIP30_
DEACTIVATION_ TESTNET - BIP30: Duplicate Coinbase Prevention - Testnet deactivation height
- BIP34_
ACTIVATION_ MAINNET - BIP34: Block Height in Coinbase - Mainnet activation height
- BIP34_
ACTIVATION_ REGTEST - BIP34: Block Height in Coinbase - Regtest activation height
- BIP34_
ACTIVATION_ TESTNET - BIP34: Block Height in Coinbase - Testnet activation height
- BIP54_
ACTIVATION_ MAINNET - BIP54: Consensus Cleanup - Mainnet activation height
- BIP54_
ACTIVATION_ REGTEST - BIP54: Consensus Cleanup - Regtest activation height
- BIP54_
ACTIVATION_ TESTNET - BIP54: Consensus Cleanup - Testnet activation height
- BIP54_
MAX_ SIGOPS_ PER_ TX - BIP54: Maximum sigop count per transaction (Consensus Cleanup). Transactions with total sigop count > this are invalid after BIP54 activation.
- BIP65_
ACTIVATION_ MAINNET - BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Mainnet activation height
- BIP65_
ACTIVATION_ TESTNET - BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Testnet activation height
- BIP66_
ACTIVATION_ MAINNET - BIP66: Strict DER Signatures - Mainnet activation height
- BIP66_
ACTIVATION_ REGTEST - BIP66: Strict DER Signatures - Regtest activation height
- BIP66_
ACTIVATION_ TESTNET - BIP66: Strict DER Signatures - Testnet activation height
- BIP112_
CSV_ ACTIVATION_ MAINNET - BIP112/BIP113: CHECKSEQUENCEVERIFY (CSV) - Mainnet activation height
- BIP112_
CSV_ ACTIVATION_ REGTEST - BIP112/BIP113: CSV - Regtest (active from genesis for typical regtest chains)
- BIP112_
CSV_ ACTIVATION_ TESTNET - BIP112/BIP113: CSV - Testnet activation height (Bitcoin Core testnet3)
- BIP147_
ACTIVATION_ MAINNET - BIP147: NULLDUMMY Enforcement - Mainnet activation height
- BIP147_
ACTIVATION_ TESTNET - BIP147: NULLDUMMY Enforcement - Testnet activation height
- C
- satoshis per BTC (Orange Paper C)
- COINBASE_
MATURITY - Coinbase maturity requirement: 100 blocks
- CSFS_
ACTIVATION_ MAINNET - CSFS (BIP348) - Mainnet activation height
- CSFS_
ACTIVATION_ REGTEST - CSFS (BIP348) - Regtest activation height
- CSFS_
ACTIVATION_ TESTNET - CSFS (BIP348) - Testnet activation height
- CTV_
ACTIVATION_ MAINNET - CTV (BIP119) - Mainnet activation height
- CTV_
ACTIVATION_ REGTEST - CTV (BIP119) - Regtest activation height
- CTV_
ACTIVATION_ TESTNET - CTV (BIP119) - Testnet activation height
- DIFFICULTY_
ADJUSTMENT_ INTERVAL - Difficulty adjustment interval: 2016 blocks
- GENESIS_
BLOCK_ HASH - Genesis block hash (mainnet)
- GENESIS_
BLOCK_ HASH_ INTERNAL - Genesis block hash in internal / wire byte order.
- GENESIS_
BLOCK_ MERKLE_ ROOT - Genesis block merkle root
- GENESIS_
BLOCK_ NONCE - Genesis block nonce
- GENESIS_
BLOCK_ TIMESTAMP - Genesis block timestamp (Unix timestamp)
- H
- halving interval (Orange Paper H)
- HALVING_
INTERVAL - Halving interval: 210,000 blocks
- INITIAL_
SUBSIDY - Initial block subsidy: 50 BTC
- LOCKTIME_
THRESHOLD - Lock time threshold: transactions with lock time < this are block height
- L_
ELEMENT - maximum element size (Orange Paper L_ELEMENT)
- L_OPS
- maximum operations per script (Orange Paper L_OPS)
- L_
SCRIPT - maximum script length (Orange Paper L_SCRIPT)
- L_STACK
- maximum stack size (Orange Paper L_STACK)
- MAX_
BLOCK_ SERIALIZED_ SIZE - Maximum block serialized size in bytes (network rule) This is the maximum size of a block when serialized without witness data
- MAX_
BLOCK_ SIGOPS_ COST - Maximum block sigop cost (network rule)
- MAX_
BLOCK_ SIZE Deprecated - Maximum block size (deprecated - use MAX_BLOCK_WEIGHT for SegWit blocks) Kept for backward compatibility
- MAX_
BLOCK_ WEIGHT - Maximum block weight in weight units (network rule, BIP141) Weight = (stripped_size × 4) + witness_size This is the primary limit for SegWit blocks
- MAX_
FUTURE_ BLOCK_ TIME - Maximum future block time tolerance: 2 hours (7200 seconds)
- MAX_
INPUTS - Maximum number of inputs per transaction
- MAX_
MONEY - Maximum money supply: 21,000,000 BTC in satoshis
- MAX_
OUTPUTS - Maximum number of outputs per transaction
- MAX_
SCRIPT_ ELEMENT_ SIZE - Maximum script element size (BIP141: witness elements can be up to 520 bytes)
- MAX_
SCRIPT_ OPS - Maximum number of operations in script
- MAX_
SCRIPT_ SIZE - Maximum script length
- MAX_
STACK_ SIZE - Maximum stack size during script execution
- MAX_
TARGET - Maximum target (minimum difficulty)
- MAX_
TRANSACTIONS_ PER_ BLOCK - Upper bound on the number of transactions in any consensus-valid block.
- MAX_
TX_ SIZE - Maximum transaction size: 1MB
- MIN_
RELAY_ FEE - Minimum relay fee for RBF replacement (BIP125)
- MIN_
TARGET - Minimum target (maximum difficulty) - Bitcoin’s genesis target
- M_MAX
- maximum money supply (Orange Paper M_MAX)
- R
- coinbase maturity (Orange Paper R)
- SATOSHIS_
PER_ BTC - Satoshis per BTC
- SEGWIT_
ACTIVATION_ MAINNET - SegWit (BIP141) - Mainnet activation height
- SEGWIT_
ACTIVATION_ TESTNET - SegWit (BIP141) - Testnet activation height (Bitcoin Core testnet3
consensus.SegwitHeight) - SEGWIT_
P2WPKH_ LENGTH - SegWit witness program lengths (BIP141)
- SEGWIT_
P2WSH_ LENGTH - SEQUENCE_
FINAL - Sequence number for final transaction
- SEQUENCE_
RBF - Sequence number for RBF
- S_MAX
- maximum sigops per block (Orange Paper S_MAX)
- TAPROOT_
ACTIVATION_ MAINNET - Taproot (BIP341) - Mainnet activation height
- TAPROOT_
ACTIVATION_ TESTNET - Taproot (BIP341) - Testnet activation height (Bitcoin Core testnet3)
- TAPROOT_
PROGRAM_ LENGTH - Taproot program hash length (BIP341)
- TAPROOT_
SCRIPT_ LENGTH - Taproot script length (BIP341)
- TARGET_
TIME_ PER_ BLOCK - Target time per block: 10 minutes
- WITNESS_
COMMITMENT_ HASH_ LENGTH - Witness commitment hash length (BIP141)
- WITNESS_
COMMITMENT_ SCRIPT_ LENGTH - Witness commitment script length (BIP141)
- W_MAX
- maximum block weight (Orange Paper W_MAX)
Functions§
- utxo_
set_ insert - Insert owned UTXO into UtxoSet (wraps in Arc). Convenience for tests and one-off inserts.
- utxo_
set_ with_ capacity - Pre-allocate a UtxoSet for
nentries. Avoids costly reallocation spikes when loading large checkpoints (at 50M entries the HashMap table alone is ~2.5 GB; a growth-triggered realloc temporarily doubles that).