Skip to main content

blvm_primitives/
constants.rs

1//! Bitcoin consensus constants from Orange Paper
2
3/// Maximum money supply: 21,000,000 BTC in satoshis
4pub const MAX_MONEY: i64 = 21_000_000 * 100_000_000;
5
6/// Maximum transaction size: 1MB
7pub const MAX_TX_SIZE: usize = 1_000_000;
8
9/// Maximum block serialized size in bytes (network rule)
10/// This is the maximum size of a block when serialized without witness data
11pub const MAX_BLOCK_SERIALIZED_SIZE: usize = 4_000_000;
12
13/// Maximum block weight in weight units (network rule, BIP141)
14/// Weight = (stripped_size × 4) + witness_size
15/// This is the primary limit for SegWit blocks
16pub const MAX_BLOCK_WEIGHT: usize = 4_000_000;
17
18/// Upper bound on the number of transactions in any consensus-valid block.
19///
20/// Consensus does not fix a tx-count limit; it follows from `MAX_BLOCK_WEIGHT` (BIP141).
21/// Mainnet has included blocks with **12k+** transactions (e.g. dense 2015–2016 spam eras);
22/// a 10k cap incorrectly rejects valid blocks and panicked in `compute_block_tx_ids`.
23///
24/// Uses a loose per-tx minimum weight (~60 WU) so `MAX_BLOCK_WEIGHT / 60` safely exceeds
25/// any block that could pass weight check (real minimum per tx is higher).
26pub const MAX_TRANSACTIONS_PER_BLOCK: usize = MAX_BLOCK_WEIGHT / 60;
27
28/// Maximum block size (deprecated - use MAX_BLOCK_WEIGHT for SegWit blocks)
29/// Kept for backward compatibility
30#[deprecated(note = "Use MAX_BLOCK_WEIGHT for SegWit blocks")]
31pub const MAX_BLOCK_SIZE: usize = MAX_BLOCK_WEIGHT;
32
33/// Maximum number of inputs per transaction
34// Note: consensus has no explicit input limit - only bounded by block weight
35// Setting to a very high value to match consensus behavior
36pub const MAX_INPUTS: usize = 100_000;
37
38/// Maximum number of outputs per transaction
39// Note: consensus has no explicit output limit - only bounded by block weight
40// Setting to a very high value to match consensus behavior
41pub const MAX_OUTPUTS: usize = 100_000;
42
43/// Maximum script length
44pub const MAX_SCRIPT_SIZE: usize = 10_000;
45
46/// Maximum stack size during script execution
47pub const MAX_STACK_SIZE: usize = 1000;
48
49/// Maximum number of operations in script
50pub const MAX_SCRIPT_OPS: usize = 201;
51
52/// Maximum script element size (BIP141: witness elements can be up to 520 bytes)
53pub const MAX_SCRIPT_ELEMENT_SIZE: usize = 520;
54
55/// Halving interval: 210,000 blocks
56pub const HALVING_INTERVAL: u64 = 210_000;
57
58/// Initial block subsidy: 50 BTC
59pub const INITIAL_SUBSIDY: i64 = 50 * 100_000_000;
60
61/// Satoshis per BTC
62pub const SATOSHIS_PER_BTC: i64 = 100_000_000;
63
64/// Difficulty adjustment interval: 2016 blocks
65pub const DIFFICULTY_ADJUSTMENT_INTERVAL: u64 = 2016;
66
67/// Target time per block: 10 minutes
68pub const TARGET_TIME_PER_BLOCK: u64 = 600;
69
70/// Maximum future block time tolerance: 2 hours (7200 seconds)
71///
72/// Blocks with timestamps more than this far in the future are rejected
73/// to prevent time-warp attacks. This allows for reasonable clock skew
74/// between network nodes.
75pub const MAX_FUTURE_BLOCK_TIME: u64 = 7200;
76
77/// Maximum target (minimum difficulty)
78pub const MAX_TARGET: u32 = 0x1d00ffff;
79
80/// Minimum target (maximum difficulty) - Bitcoin's genesis target
81pub const MIN_TARGET: [u8; 32] = [
82    0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84];
85
86/// Lock time threshold: transactions with lock time < this are block height
87pub const LOCKTIME_THRESHOLD: u32 = 500_000_000;
88
89/// Sequence number for final transaction
90pub const SEQUENCE_FINAL: u32 = 0xffffffff;
91
92/// Sequence number for RBF
93pub const SEQUENCE_RBF: u32 = 0xfffffffe;
94
95/// Minimum relay fee for RBF replacement (BIP125)
96///
97/// A replacement transaction must pay at least this much more in fees
98/// than the transaction it replaces. This prevents spam replacements
99/// with minimal fee increases.
100pub const MIN_RELAY_FEE: i64 = 1000; // 1000 satoshis
101
102/// Coinbase maturity requirement: 100 blocks
103///
104/// Coinbase outputs cannot be spent until 100 blocks deep.
105/// This prevents miners from spending coinbase immediately and helps
106/// secure the network against deep reorgs.
107pub const COINBASE_MATURITY: u64 = 100;
108
109/// Maximum block sigop cost (network rule)
110///
111/// Total sigop cost for a block must not exceed this value.
112/// Sigop cost = (legacy sigops × 4) + (P2SH sigops × 4) + witness sigops
113///
114/// Reference: consensus `consensus.h` MAX_BLOCK_SIGOPS_COST = 80000
115pub const MAX_BLOCK_SIGOPS_COST: u64 = 80_000;
116
117/// BIP54: Maximum sigop count per transaction (Consensus Cleanup).
118/// Transactions with total sigop count > this are invalid after BIP54 activation.
119pub const BIP54_MAX_SIGOPS_PER_TX: u64 = 2500;
120
121/// Witness commitment hash length (BIP141)
122///
123/// The witness commitment in the coinbase transaction contains:
124/// - OP_RETURN (0x6a): 1 byte
125/// - Push opcode (0x24): 1 byte
126/// - Commitment hash: 32 bytes
127///   Total: 34 bytes
128///
129/// Reference: BIP141 - Witness commitment format
130pub const WITNESS_COMMITMENT_HASH_LENGTH: usize = 32;
131
132/// Witness commitment script length (BIP141)
133///
134/// Total length of witness commitment script:
135/// - OP_RETURN (0x6a): 1 byte
136/// - Push opcode (0x24): 1 byte  
137/// - Commitment hash: 32 bytes
138///   Total: 34 bytes
139pub const WITNESS_COMMITMENT_SCRIPT_LENGTH: usize = 34;
140
141/// Taproot script length (BIP341)
142///
143/// Taproot P2TR script format: OP_1 <32-byte-program>
144/// - OP_1 (0x51): 1 byte
145/// - Push opcode (0x20): 1 byte
146/// - Program hash: 32 bytes
147///   Total: 34 bytes
148pub const TAPROOT_SCRIPT_LENGTH: usize = 34;
149
150/// Taproot program hash length (BIP341)
151///
152/// Taproot witness program (P2TR) is 32 bytes
153pub const TAPROOT_PROGRAM_LENGTH: usize = 32;
154
155/// SegWit witness program lengths (BIP141)
156///
157/// SegWit v0 programs:
158/// - P2WPKH: 20 bytes
159/// - P2WSH: 32 bytes
160pub const SEGWIT_P2WPKH_LENGTH: usize = 20;
161pub const SEGWIT_P2WSH_LENGTH: usize = 32;
162
163// ============================================================================
164// BIP ACTIVATION HEIGHTS
165// ============================================================================
166// Consensus-critical activation heights. Any change to these values would cause
167// a chain split. These heights are locked via formal proofs to match consensus.
168
169/// BIP30 has no deactivation height. `u64::MAX` keeps `height <= deactivation` true
170/// at every height. The two historical duplicate blocks are exempt by hash inside
171/// `check_bip30`, not by turning the rule off after block 91722.
172pub const BIP30_DEACTIVATION_MAINNET: u64 = u64::MAX;
173
174/// BIP30 stays enforced on testnet. There is no deactivation height.
175pub const BIP30_DEACTIVATION_TESTNET: u64 = u64::MAX;
176
177/// BIP30 stays enforced on regtest. There is no deactivation height.
178pub const BIP30_DEACTIVATION_REGTEST: u64 = u64::MAX;
179
180/// BIP16: P2SH (Pay-to-Script-Hash) - Mainnet activation height
181///
182/// Starting at this block, P2SH scripts are valid.
183/// Reference: BIP16, activated April 1, 2012 at block 173,805
184pub const BIP16_P2SH_ACTIVATION_MAINNET: u64 = 173_805;
185
186/// BIP16: P2SH (Pay-to-Script-Hash) - Testnet activation height
187///
188/// Reference: BIP16, always active on testnet
189pub const BIP16_P2SH_ACTIVATION_TESTNET: u64 = 0;
190
191/// BIP16: P2SH (Pay-to-Script-Hash) - Regtest activation height
192///
193/// Reference: BIP16, always active on regtest
194pub const BIP16_P2SH_ACTIVATION_REGTEST: u64 = 0;
195
196/// BIP34: Block Height in Coinbase - Mainnet activation height
197///
198/// Starting at this block, coinbase scriptSig must contain the block height.
199/// Reference: Bitcoin Core `consensus.BIP34Height` (`CMainParams` in `kernel/chainparams.cpp`).
200pub const BIP34_ACTIVATION_MAINNET: u64 = 227_931;
201
202/// BIP34: Block Height in Coinbase - Testnet activation height
203///
204/// Reference: Bitcoin Core `consensus.BIP34Height` for `CTestNetParams` (`chainparams.cpp`).
205pub const BIP34_ACTIVATION_TESTNET: u64 = 21_111;
206
207/// BIP34: Block Height in Coinbase - Regtest activation height
208///
209/// Bitcoin Core activates BIP34 at height 1 on regtest, so the version-1 genesis stays valid.
210pub const BIP34_ACTIVATION_REGTEST: u64 = 1;
211
212/// BIP66: Strict DER Signatures - Mainnet activation height
213///
214/// Starting at this block, all signatures must use strict DER encoding.
215/// Reference: BIP66, consensus activation at block 363,725
216/// Note: The code checks `height < activation_height`, so 363,725 is the first
217/// block where BIP66 is enforced.
218pub const BIP66_ACTIVATION_MAINNET: u64 = 363_725;
219
220/// BIP66: Strict DER Signatures - Testnet activation height
221///
222/// Reference: BIP66, consensus activation at block 330,776
223pub const BIP66_ACTIVATION_TESTNET: u64 = 330_776;
224
225/// BIP66: Strict DER Signatures - Regtest activation height
226///
227/// Bitcoin Core activates BIP66 at height 1 on regtest.
228pub const BIP66_ACTIVATION_REGTEST: u64 = 1;
229
230/// BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Mainnet activation height
231///
232/// Starting at this block, CLTV opcode is enabled.
233/// Reference: BIP65, consensus activation at block 388,381
234pub const BIP65_ACTIVATION_MAINNET: u64 = 388_381;
235
236/// BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Testnet activation height
237///
238/// Reference: Bitcoin Core `consensus.BIP65Height` for `CTestNetParams` (`kernel/chainparams.cpp`).
239pub const BIP65_ACTIVATION_TESTNET: u64 = 581_885;
240
241/// BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Regtest activation height
242///
243/// Bitcoin Core activates BIP65 at height 1 on regtest.
244pub const BIP65_ACTIVATION_REGTEST: u64 = 1;
245
246/// BIP112/BIP113: CHECKSEQUENCEVERIFY (CSV) - Mainnet activation height
247///
248/// `SCRIPT_VERIFY_CHECKSEQUENCEVERIFY` (0x400). **Not** the same as BIP147 (NULLDUMMY).
249/// Reference: Bitcoin Core `consensus.nCSVEnabled` = 419328 on mainnet.
250pub const BIP112_CSV_ACTIVATION_MAINNET: u64 = 419_328;
251
252/// BIP112/BIP113: CSV - Testnet activation height (Bitcoin Core testnet3)
253///
254/// Reference: Bitcoin Core `consensus.CSVHeight` for `CTestNetParams` (`kernel/chainparams.cpp`).
255/// Testnet CSV activates later than mainnet (770112), unlike some other deployments.
256pub const BIP112_CSV_ACTIVATION_TESTNET: u64 = 770_112;
257
258/// BIP112/BIP113: CSV - Regtest activation height
259///
260/// Bitcoin Core activates CSV at height 1 on regtest.
261pub const BIP112_CSV_ACTIVATION_REGTEST: u64 = 1;
262
263/// BIP147: NULLDUMMY Enforcement - Mainnet activation height
264///
265/// Starting at this block, dummy stack elements must be empty (OP_0).
266/// Reference: BIP147, consensus activation at block 481,824
267pub const BIP147_ACTIVATION_MAINNET: u64 = 481_824;
268
269/// BIP147: NULLDUMMY Enforcement - Testnet activation height
270///
271/// Reference: BIP147, consensus activation at block 834,624
272pub const BIP147_ACTIVATION_TESTNET: u64 = 834_624;
273
274/// SegWit (BIP141) - Mainnet activation height
275///
276/// Starting at this block, Segregated Witness is active.
277/// Reference: BIP141, consensus activation at block 481,824
278pub const SEGWIT_ACTIVATION_MAINNET: u64 = 481_824;
279
280/// Taproot (BIP341) - Mainnet activation height
281///
282/// Starting at this block, Taproot is active.
283/// Reference: BIP341, consensus activation at block 709,632
284pub const TAPROOT_ACTIVATION_MAINNET: u64 = 709_632;
285
286/// SegWit (BIP141) - Testnet activation height (Bitcoin Core testnet3 `consensus.SegwitHeight`)
287///
288/// Reference: Bitcoin Core `kernel/chainparams.cpp` (`CTestNetParams`).
289/// Same height as BIP147 (NULLDUMMY) on testnet3.
290pub const SEGWIT_ACTIVATION_TESTNET: u64 = 834_624;
291
292/// Taproot (BIP341) - Testnet activation height (Bitcoin Core testnet3)
293///
294/// Reference: Taproot buried height implied by `MinBIP9WarningHeight - 2016` in `CTestNetParams`
295/// (`kernel/chainparams.cpp`).
296pub const TAPROOT_ACTIVATION_TESTNET: u64 = 2_011_968;
297
298/// CTV (BIP119) - Mainnet activation height
299///
300/// Starting at this block, OP_CHECKTEMPLATEVERIFY is active.
301/// Note: CTV is not yet activated on mainnet. Set to 0 to disable.
302/// Reference: BIP119 (proposed soft fork, not yet activated)
303pub const CTV_ACTIVATION_MAINNET: u64 = 0;
304
305/// CTV (BIP119) - Testnet activation height
306///
307/// Note: CTV is not yet activated on testnet. Set to 0 to disable.
308pub const CTV_ACTIVATION_TESTNET: u64 = 0;
309
310/// CTV (BIP119) - Regtest activation height
311///
312/// Always active on regtest (block 0) when feature is enabled.
313pub const CTV_ACTIVATION_REGTEST: u64 = 0;
314
315/// CSFS (BIP348) - Mainnet activation height
316///
317/// Starting at this block, OP_CHECKSIGFROMSTACK is active in Tapscript.
318/// Note: CSFS is not yet activated on mainnet. Set to 0 to disable.
319/// Reference: BIP348 (proposed soft fork, not yet activated)
320pub const CSFS_ACTIVATION_MAINNET: u64 = 0;
321
322/// CSFS (BIP348) - Testnet activation height
323///
324/// Note: CSFS is not yet activated on testnet. Set to 0 to disable.
325pub const CSFS_ACTIVATION_TESTNET: u64 = 0;
326
327/// CSFS (BIP348) - Regtest activation height
328///
329/// Always active on regtest (block 0) when feature is enabled.
330pub const CSFS_ACTIVATION_REGTEST: u64 = 0;
331
332/// BIP54: Consensus Cleanup - Mainnet activation height
333///
334/// BIP54 specifies "for all blocks **after activation**" the new rules apply. We must not
335/// enforce before that height or we would reject valid pre-activation blocks (e.g. coinbase
336/// with lock_time=0, 64-byte txs). Signalling/miner compatibility is about *when* activation
337/// locks in; the activation height is the first block from which rules apply. Set to the
338/// network-agreed height once known (or make configurable so operators can set without a new binary).
339pub const BIP54_ACTIVATION_MAINNET: u64 = u64::MAX;
340
341/// BIP54: Consensus Cleanup - Testnet activation height
342pub const BIP54_ACTIVATION_TESTNET: u64 = u64::MAX;
343
344/// BIP54: Consensus Cleanup - Regtest activation height
345///
346/// Set to u64::MAX so regtest matches mainnet/testnet (BIP54 off by default).
347/// To test BIP54 on regtest, use a lower activation (e.g. 0) via config or override.
348pub const BIP54_ACTIVATION_REGTEST: u64 = u64::MAX;
349
350// ============================================================================
351// GENESIS BLOCK CONSTANTS
352// ============================================================================
353// The genesis block is the foundation of Bitcoin. Any change to these values
354// would create a different blockchain. These are locked via formal proofs.
355
356/// Genesis block hash (mainnet)
357///
358/// The hash of the first Bitcoin block. This is the root of the blockchain.
359/// Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
360pub const GENESIS_BLOCK_HASH: [u8; 32] = [
361    0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xd6, 0x68, 0x9c, 0x08, 0x5a, 0xe1, 0x65, 0x83, 0x1e, 0x93,
362    0x4f, 0xf7, 0x63, 0xae, 0x46, 0xa2, 0xa6, 0xc1, 0x72, 0xb3, 0xf1, 0xb6, 0x0a, 0x8c, 0xe2, 0x6f,
363];
364
365const fn reverse_hash32(h: [u8; 32]) -> [u8; 32] {
366    let mut out = [0u8; 32];
367    let mut i = 0usize;
368    while i < 32 {
369        out[i] = h[31 - i];
370        i += 1;
371    }
372    out
373}
374
375/// Genesis block hash in **internal / wire** byte order.
376///
377/// Bitcoin displays block hashes with the leading zero bytes first ([`GENESIS_BLOCK_HASH`]).
378/// P2P messages (`GetHeaders` locators), block header `prev_block_hash`, and typical
379/// `double_sha256(header)` output use this reversed layout.
380pub const GENESIS_BLOCK_HASH_INTERNAL: [u8; 32] = reverse_hash32(GENESIS_BLOCK_HASH);
381
382/// Genesis block timestamp (Unix timestamp)
383///
384/// The timestamp of the genesis block: January 3, 2009, 18:15:05 UTC
385/// This is the birth of Bitcoin.
386pub const GENESIS_BLOCK_TIMESTAMP: u32 = 1231006505;
387
388/// Genesis block merkle root
389///
390/// The merkle root of the genesis block's coinbase transaction.
391/// This is the root of the first transaction tree.
392pub const GENESIS_BLOCK_MERKLE_ROOT: [u8; 32] = [
393    0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
394    0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a,
395];
396
397/// Genesis block nonce
398///
399/// The nonce used in the genesis block's proof of work.
400pub const GENESIS_BLOCK_NONCE: u32 = 2083236893;
401
402// ============================================================================
403// Orange Paper Section 4 symbols (C, M_MAX, H, etc.)
404// ============================================================================
405// Auto-generated from blvm-spec/THE_ORANGE_PAPER.md via cargo spec-lock extract-constants
406
407/// satoshis per BTC (Orange Paper C)
408pub const C: u64 = 100_000_000;
409
410/// maximum money supply (Orange Paper M_MAX)
411pub const M_MAX: i64 = (21_000_000 * C) as i64;
412
413/// halving interval (Orange Paper H)
414pub const H: u64 = 210_000;
415
416/// maximum block weight (Orange Paper W_MAX)
417pub const W_MAX: u64 = 4_000_000;
418
419/// maximum sigops per block (Orange Paper S_MAX)
420pub const S_MAX: u64 = 80_000;
421
422/// coinbase maturity (Orange Paper R)
423pub const R: u64 = 100;
424
425/// maximum script length (Orange Paper L_SCRIPT)
426pub const L_SCRIPT: u64 = 10_000;
427
428/// maximum stack size (Orange Paper L_STACK)
429pub const L_STACK: u64 = 1_000;
430
431/// maximum operations per script (Orange Paper L_OPS)
432pub const L_OPS: u64 = 201;
433
434/// maximum element size (Orange Paper L_ELEMENT)
435pub const L_ELEMENT: u64 = 520;
436
437// ============================================================================
438// FORMAL VERIFICATION: Orange Paper Constant Locking
439// ============================================================================
440// These proofs directly verify that constant values match the Orange Paper
441// Section 4 (Consensus Constants) specification. This creates a cryptographic
442// lock between the Orange Paper (IR) and blvm-consensus implementation.
443
444#[cfg(test)]
445mod genesis_hash_byte_order_tests {
446    use super::{GENESIS_BLOCK_HASH, GENESIS_BLOCK_HASH_INTERNAL};
447
448    #[test]
449    fn genesis_internal_is_reversed_display_form() {
450        let mut rev = GENESIS_BLOCK_HASH;
451        rev.reverse();
452        assert_eq!(rev, GENESIS_BLOCK_HASH_INTERNAL);
453    }
454}
455
456#[cfg(test)]
457mod max_tx_per_block_tests {
458    use super::MAX_TRANSACTIONS_PER_BLOCK;
459
460    /// Regression: mainnet includes blocks with 12k+ txs (e.g. 2015–2016 spam waves).
461    /// A 10k cap panicked in parallel `compute_block_tx_ids` during IBD.
462    #[test]
463    fn max_transactions_per_block_covers_observed_mainnet_dense_blocks() {
464        assert!(
465            MAX_TRANSACTIONS_PER_BLOCK >= 13_000,
466            "bound {} too low for known mainnet blocks",
467            MAX_TRANSACTIONS_PER_BLOCK
468        );
469    }
470}