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: Duplicate Coinbase Prevention - Mainnet deactivation height
170///
171/// BIP30 was disabled after this block to allow duplicate coinbases in blocks 91842 and 91880.
172/// Reference: consensus disabled BIP30 after block 91722
173pub const BIP30_DEACTIVATION_MAINNET: u64 = 91722;
174
175/// BIP30: Duplicate Coinbase Prevention - Testnet deactivation height
176///
177/// BIP30 was disabled after this block on testnet.
178pub const BIP30_DEACTIVATION_TESTNET: u64 = 0; // BIP30 never enforced on testnet
179
180/// BIP30: Duplicate Coinbase Prevention - Regtest deactivation height
181///
182/// BIP30 is never enforced on regtest.
183pub const BIP30_DEACTIVATION_REGTEST: u64 = 0;
184
185/// BIP16: P2SH (Pay-to-Script-Hash) - Mainnet activation height
186///
187/// Starting at this block, P2SH scripts are valid.
188/// Reference: BIP16, activated April 1, 2012 at block 173,805
189pub const BIP16_P2SH_ACTIVATION_MAINNET: u64 = 173_805;
190
191/// BIP16: P2SH (Pay-to-Script-Hash) - Testnet activation height
192///
193/// Reference: BIP16, always active on testnet
194pub const BIP16_P2SH_ACTIVATION_TESTNET: u64 = 0;
195
196/// BIP16: P2SH (Pay-to-Script-Hash) - Regtest activation height
197///
198/// Reference: BIP16, always active on regtest
199pub const BIP16_P2SH_ACTIVATION_REGTEST: u64 = 0;
200
201/// BIP34: Block Height in Coinbase - Mainnet activation height
202///
203/// Starting at this block, coinbase scriptSig must contain the block height.
204/// Reference: Bitcoin Core `consensus.BIP34Height` (`CMainParams` in `kernel/chainparams.cpp`).
205pub const BIP34_ACTIVATION_MAINNET: u64 = 227_931;
206
207/// BIP34: Block Height in Coinbase - Testnet activation height
208///
209/// Reference: Bitcoin Core `consensus.BIP34Height` for `CTestNetParams` (`chainparams.cpp`).
210pub const BIP34_ACTIVATION_TESTNET: u64 = 21_111;
211
212/// BIP34: Block Height in Coinbase - Regtest activation height
213///
214/// Always active on regtest (block 0)
215pub const BIP34_ACTIVATION_REGTEST: u64 = 0;
216
217/// BIP66: Strict DER Signatures - Mainnet activation height
218///
219/// Starting at this block, all signatures must use strict DER encoding.
220/// Reference: BIP66, consensus activation at block 363,725
221/// Note: The code checks `height < activation_height`, so 363,725 is the first
222/// block where BIP66 is enforced.
223pub const BIP66_ACTIVATION_MAINNET: u64 = 363_725;
224
225/// BIP66: Strict DER Signatures - Testnet activation height
226///
227/// Reference: BIP66, consensus activation at block 330,776
228pub const BIP66_ACTIVATION_TESTNET: u64 = 330_776;
229
230/// BIP66: Strict DER Signatures - Regtest activation height
231///
232/// Always active on regtest (block 0)
233pub const BIP66_ACTIVATION_REGTEST: u64 = 0;
234
235/// BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Mainnet activation height
236///
237/// Starting at this block, CLTV opcode is enabled.
238/// Reference: BIP65, consensus activation at block 388,381
239pub const BIP65_ACTIVATION_MAINNET: u64 = 388_381;
240
241/// BIP65: OP_CHECKLOCKTIMEVERIFY (CLTV) - Testnet activation height
242///
243/// Reference: Bitcoin Core `consensus.BIP65Height` for `CTestNetParams` (`kernel/chainparams.cpp`).
244pub const BIP65_ACTIVATION_TESTNET: u64 = 581_885;
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 (active from genesis for typical regtest chains)
259pub const BIP112_CSV_ACTIVATION_REGTEST: u64 = 0;
260
261/// BIP147: NULLDUMMY Enforcement - Mainnet activation height
262///
263/// Starting at this block, dummy stack elements must be empty (OP_0).
264/// Reference: BIP147, consensus activation at block 481,824
265pub const BIP147_ACTIVATION_MAINNET: u64 = 481_824;
266
267/// BIP147: NULLDUMMY Enforcement - Testnet activation height
268///
269/// Reference: BIP147, consensus activation at block 834,624
270pub const BIP147_ACTIVATION_TESTNET: u64 = 834_624;
271
272/// SegWit (BIP141) - Mainnet activation height
273///
274/// Starting at this block, Segregated Witness is active.
275/// Reference: BIP141, consensus activation at block 481,824
276pub const SEGWIT_ACTIVATION_MAINNET: u64 = 481_824;
277
278/// Taproot (BIP341) - Mainnet activation height
279///
280/// Starting at this block, Taproot is active.
281/// Reference: BIP341, consensus activation at block 709,632
282pub const TAPROOT_ACTIVATION_MAINNET: u64 = 709_632;
283
284/// SegWit (BIP141) - Testnet activation height (Bitcoin Core testnet3 `consensus.SegwitHeight`)
285///
286/// Reference: Bitcoin Core `kernel/chainparams.cpp` (`CTestNetParams`).
287/// Same height as BIP147 (NULLDUMMY) on testnet3.
288pub const SEGWIT_ACTIVATION_TESTNET: u64 = 834_624;
289
290/// Taproot (BIP341) - Testnet activation height (Bitcoin Core testnet3)
291///
292/// Reference: Taproot buried height implied by `MinBIP9WarningHeight - 2016` in `CTestNetParams`
293/// (`kernel/chainparams.cpp`).
294pub const TAPROOT_ACTIVATION_TESTNET: u64 = 2_011_968;
295
296/// CTV (BIP119) - Mainnet activation height
297///
298/// Starting at this block, OP_CHECKTEMPLATEVERIFY is active.
299/// Note: CTV is not yet activated on mainnet. Set to 0 to disable.
300/// Reference: BIP119 (proposed soft fork, not yet activated)
301pub const CTV_ACTIVATION_MAINNET: u64 = 0;
302
303/// CTV (BIP119) - Testnet activation height
304///
305/// Note: CTV is not yet activated on testnet. Set to 0 to disable.
306pub const CTV_ACTIVATION_TESTNET: u64 = 0;
307
308/// CTV (BIP119) - Regtest activation height
309///
310/// Always active on regtest (block 0) when feature is enabled.
311pub const CTV_ACTIVATION_REGTEST: u64 = 0;
312
313/// CSFS (BIP348) - Mainnet activation height
314///
315/// Starting at this block, OP_CHECKSIGFROMSTACK is active in Tapscript.
316/// Note: CSFS is not yet activated on mainnet. Set to 0 to disable.
317/// Reference: BIP348 (proposed soft fork, not yet activated)
318pub const CSFS_ACTIVATION_MAINNET: u64 = 0;
319
320/// CSFS (BIP348) - Testnet activation height
321///
322/// Note: CSFS is not yet activated on testnet. Set to 0 to disable.
323pub const CSFS_ACTIVATION_TESTNET: u64 = 0;
324
325/// CSFS (BIP348) - Regtest activation height
326///
327/// Always active on regtest (block 0) when feature is enabled.
328pub const CSFS_ACTIVATION_REGTEST: u64 = 0;
329
330/// BIP54: Consensus Cleanup - Mainnet activation height
331///
332/// BIP54 specifies "for all blocks **after activation**" the new rules apply. We must not
333/// enforce before that height or we would reject valid pre-activation blocks (e.g. coinbase
334/// with lock_time=0, 64-byte txs). Signalling/miner compatibility is about *when* activation
335/// locks in; the activation height is the first block from which rules apply. Set to the
336/// network-agreed height once known (or make configurable so operators can set without a new binary).
337pub const BIP54_ACTIVATION_MAINNET: u64 = u64::MAX;
338
339/// BIP54: Consensus Cleanup - Testnet activation height
340pub const BIP54_ACTIVATION_TESTNET: u64 = u64::MAX;
341
342/// BIP54: Consensus Cleanup - Regtest activation height
343///
344/// Set to u64::MAX so regtest matches mainnet/testnet (BIP54 off by default).
345/// To test BIP54 on regtest, use a lower activation (e.g. 0) via config or override.
346pub const BIP54_ACTIVATION_REGTEST: u64 = u64::MAX;
347
348// ============================================================================
349// GENESIS BLOCK CONSTANTS
350// ============================================================================
351// The genesis block is the foundation of Bitcoin. Any change to these values
352// would create a different blockchain. These are locked via formal proofs.
353
354/// Genesis block hash (mainnet)
355///
356/// The hash of the first Bitcoin block. This is the root of the blockchain.
357/// Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
358pub const GENESIS_BLOCK_HASH: [u8; 32] = [
359 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xd6, 0x68, 0x9c, 0x08, 0x5a, 0xe1, 0x65, 0x83, 0x1e, 0x93,
360 0x4f, 0xf7, 0x63, 0xae, 0x46, 0xa2, 0xa6, 0xc1, 0x72, 0xb3, 0xf1, 0xb6, 0x0a, 0x8c, 0xe2, 0x6f,
361];
362
363const fn reverse_hash32(h: [u8; 32]) -> [u8; 32] {
364 let mut out = [0u8; 32];
365 let mut i = 0usize;
366 while i < 32 {
367 out[i] = h[31 - i];
368 i += 1;
369 }
370 out
371}
372
373/// Genesis block hash in **internal / wire** byte order.
374///
375/// Bitcoin displays block hashes with the leading zero bytes first ([`GENESIS_BLOCK_HASH`]).
376/// P2P messages (`GetHeaders` locators), block header `prev_block_hash`, and typical
377/// `double_sha256(header)` output use this reversed layout.
378pub const GENESIS_BLOCK_HASH_INTERNAL: [u8; 32] = reverse_hash32(GENESIS_BLOCK_HASH);
379
380/// Genesis block timestamp (Unix timestamp)
381///
382/// The timestamp of the genesis block: January 3, 2009, 18:15:05 UTC
383/// This is the birth of Bitcoin.
384pub const GENESIS_BLOCK_TIMESTAMP: u32 = 1231006505;
385
386/// Genesis block merkle root
387///
388/// The merkle root of the genesis block's coinbase transaction.
389/// This is the root of the first transaction tree.
390pub const GENESIS_BLOCK_MERKLE_ROOT: [u8; 32] = [
391 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
392 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a,
393];
394
395/// Genesis block nonce
396///
397/// The nonce used in the genesis block's proof of work.
398pub const GENESIS_BLOCK_NONCE: u32 = 2083236893;
399
400// ============================================================================
401// Orange Paper Section 4 symbols (C, M_MAX, H, etc.)
402// ============================================================================
403// Auto-generated from blvm-spec/THE_ORANGE_PAPER.md via cargo spec-lock extract-constants
404
405/// satoshis per BTC (Orange Paper C)
406pub const C: u64 = 100_000_000;
407
408/// maximum money supply (Orange Paper M_MAX)
409pub const M_MAX: i64 = (21_000_000 * C) as i64;
410
411/// halving interval (Orange Paper H)
412pub const H: u64 = 210_000;
413
414/// maximum block weight (Orange Paper W_MAX)
415pub const W_MAX: u64 = 4_000_000;
416
417/// maximum sigops per block (Orange Paper S_MAX)
418pub const S_MAX: u64 = 80_000;
419
420/// coinbase maturity (Orange Paper R)
421pub const R: u64 = 100;
422
423/// maximum script length (Orange Paper L_SCRIPT)
424pub const L_SCRIPT: u64 = 10_000;
425
426/// maximum stack size (Orange Paper L_STACK)
427pub const L_STACK: u64 = 1_000;
428
429/// maximum operations per script (Orange Paper L_OPS)
430pub const L_OPS: u64 = 201;
431
432/// maximum element size (Orange Paper L_ELEMENT)
433pub const L_ELEMENT: u64 = 520;
434
435// ============================================================================
436// FORMAL VERIFICATION: Orange Paper Constant Locking
437// ============================================================================
438// These proofs directly verify that constant values match the Orange Paper
439// Section 4 (Consensus Constants) specification. This creates a cryptographic
440// lock between the Orange Paper (IR) and blvm-consensus implementation.
441
442#[cfg(test)]
443mod genesis_hash_byte_order_tests {
444 use super::{GENESIS_BLOCK_HASH, GENESIS_BLOCK_HASH_INTERNAL};
445
446 #[test]
447 fn genesis_internal_is_reversed_display_form() {
448 let mut rev = GENESIS_BLOCK_HASH;
449 rev.reverse();
450 assert_eq!(rev, GENESIS_BLOCK_HASH_INTERNAL);
451 }
452}
453
454#[cfg(test)]
455mod max_tx_per_block_tests {
456 use super::MAX_TRANSACTIONS_PER_BLOCK;
457
458 /// Regression: mainnet includes blocks with 12k+ txs (e.g. 2015–2016 spam waves).
459 /// A 10k cap panicked in parallel `compute_block_tx_ids` during IBD.
460 #[test]
461 fn max_transactions_per_block_covers_observed_mainnet_dense_blocks() {
462 assert!(
463 MAX_TRANSACTIONS_PER_BLOCK >= 13_000,
464 "bound {} too low for known mainnet blocks",
465 MAX_TRANSACTIONS_PER_BLOCK
466 );
467 }
468}