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/// Always active on regtest (block 0)
210pub const BIP34_ACTIVATION_REGTEST: u64 = 0;
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/// Always active on regtest (block 0)
228pub const BIP66_ACTIVATION_REGTEST: u64 = 0;
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/// BIP112/BIP113: CHECKSEQUENCEVERIFY (CSV) - Mainnet activation height
242///
243/// `SCRIPT_VERIFY_CHECKSEQUENCEVERIFY` (0x400). **Not** the same as BIP147 (NULLDUMMY).
244/// Reference: Bitcoin Core `consensus.nCSVEnabled` = 419328 on mainnet.
245pub const BIP112_CSV_ACTIVATION_MAINNET: u64 = 419_328;
246
247/// BIP112/BIP113: CSV - Testnet activation height (Bitcoin Core testnet3)
248///
249/// Reference: Bitcoin Core `consensus.CSVHeight` for `CTestNetParams` (`kernel/chainparams.cpp`).
250/// Testnet CSV activates later than mainnet (770112), unlike some other deployments.
251pub const BIP112_CSV_ACTIVATION_TESTNET: u64 = 770_112;
252
253/// BIP112/BIP113: CSV - Regtest (active from genesis for typical regtest chains)
254pub const BIP112_CSV_ACTIVATION_REGTEST: u64 = 0;
255
256/// BIP147: NULLDUMMY Enforcement - Mainnet activation height
257///
258/// Starting at this block, dummy stack elements must be empty (OP_0).
259/// Reference: BIP147, consensus activation at block 481,824
260pub const BIP147_ACTIVATION_MAINNET: u64 = 481_824;
261
262/// BIP147: NULLDUMMY Enforcement - Testnet activation height
263///
264/// Reference: BIP147, consensus activation at block 834,624
265pub const BIP147_ACTIVATION_TESTNET: u64 = 834_624;
266
267/// SegWit (BIP141) - Mainnet activation height
268///
269/// Starting at this block, Segregated Witness is active.
270/// Reference: BIP141, consensus activation at block 481,824
271pub const SEGWIT_ACTIVATION_MAINNET: u64 = 481_824;
272
273/// Taproot (BIP341) - Mainnet activation height
274///
275/// Starting at this block, Taproot is active.
276/// Reference: BIP341, consensus activation at block 709,632
277pub const TAPROOT_ACTIVATION_MAINNET: u64 = 709_632;
278
279/// SegWit (BIP141) - Testnet activation height (Bitcoin Core testnet3 `consensus.SegwitHeight`)
280///
281/// Reference: Bitcoin Core `kernel/chainparams.cpp` (`CTestNetParams`).
282/// Same height as BIP147 (NULLDUMMY) on testnet3.
283pub const SEGWIT_ACTIVATION_TESTNET: u64 = 834_624;
284
285/// Taproot (BIP341) - Testnet activation height (Bitcoin Core testnet3)
286///
287/// Reference: Taproot buried height implied by `MinBIP9WarningHeight - 2016` in `CTestNetParams`
288/// (`kernel/chainparams.cpp`).
289pub const TAPROOT_ACTIVATION_TESTNET: u64 = 2_011_968;
290
291/// CTV (BIP119) - Mainnet activation height
292///
293/// Starting at this block, OP_CHECKTEMPLATEVERIFY is active.
294/// Note: CTV is not yet activated on mainnet. Set to 0 to disable.
295/// Reference: BIP119 (proposed soft fork, not yet activated)
296pub const CTV_ACTIVATION_MAINNET: u64 = 0;
297
298/// CTV (BIP119) - Testnet activation height
299///
300/// Note: CTV is not yet activated on testnet. Set to 0 to disable.
301pub const CTV_ACTIVATION_TESTNET: u64 = 0;
302
303/// CTV (BIP119) - Regtest activation height
304///
305/// Always active on regtest (block 0) when feature is enabled.
306pub const CTV_ACTIVATION_REGTEST: u64 = 0;
307
308/// CSFS (BIP348) - Mainnet activation height
309///
310/// Starting at this block, OP_CHECKSIGFROMSTACK is active in Tapscript.
311/// Note: CSFS is not yet activated on mainnet. Set to 0 to disable.
312/// Reference: BIP348 (proposed soft fork, not yet activated)
313pub const CSFS_ACTIVATION_MAINNET: u64 = 0;
314
315/// CSFS (BIP348) - Testnet activation height
316///
317/// Note: CSFS is not yet activated on testnet. Set to 0 to disable.
318pub const CSFS_ACTIVATION_TESTNET: u64 = 0;
319
320/// CSFS (BIP348) - Regtest activation height
321///
322/// Always active on regtest (block 0) when feature is enabled.
323pub const CSFS_ACTIVATION_REGTEST: u64 = 0;
324
325/// BIP54: Consensus Cleanup - Mainnet activation height
326///
327/// BIP54 specifies "for all blocks **after activation**" the new rules apply. We must not
328/// enforce before that height or we would reject valid pre-activation blocks (e.g. coinbase
329/// with lock_time=0, 64-byte txs). Signalling/miner compatibility is about *when* activation
330/// locks in; the activation height is the first block from which rules apply. Set to the
331/// network-agreed height once known (or make configurable so operators can set without a new binary).
332pub const BIP54_ACTIVATION_MAINNET: u64 = u64::MAX;
333
334/// BIP54: Consensus Cleanup - Testnet activation height
335pub const BIP54_ACTIVATION_TESTNET: u64 = u64::MAX;
336
337/// BIP54: Consensus Cleanup - Regtest activation height
338///
339/// Set to u64::MAX so regtest matches mainnet/testnet (BIP54 off by default).
340/// To test BIP54 on regtest, use a lower activation (e.g. 0) via config or override.
341pub const BIP54_ACTIVATION_REGTEST: u64 = u64::MAX;
342
343// ============================================================================
344// GENESIS BLOCK CONSTANTS
345// ============================================================================
346// The genesis block is the foundation of Bitcoin. Any change to these values
347// would create a different blockchain. These are locked via formal proofs.
348
349/// Genesis block hash (mainnet)
350///
351/// The hash of the first Bitcoin block. This is the root of the blockchain.
352/// Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
353pub const GENESIS_BLOCK_HASH: [u8; 32] = [
354 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xd6, 0x68, 0x9c, 0x08, 0x5a, 0xe1, 0x65, 0x83, 0x1e, 0x93,
355 0x4f, 0xf7, 0x63, 0xae, 0x46, 0xa2, 0xa6, 0xc1, 0x72, 0xb3, 0xf1, 0xb6, 0x0a, 0x8c, 0xe2, 0x6f,
356];
357
358const fn reverse_hash32(h: [u8; 32]) -> [u8; 32] {
359 let mut out = [0u8; 32];
360 let mut i = 0usize;
361 while i < 32 {
362 out[i] = h[31 - i];
363 i += 1;
364 }
365 out
366}
367
368/// Genesis block hash in **internal / wire** byte order.
369///
370/// Bitcoin displays block hashes with the leading zero bytes first ([`GENESIS_BLOCK_HASH`]).
371/// P2P messages (`GetHeaders` locators), block header `prev_block_hash`, and typical
372/// `double_sha256(header)` output use this reversed layout.
373pub const GENESIS_BLOCK_HASH_INTERNAL: [u8; 32] = reverse_hash32(GENESIS_BLOCK_HASH);
374
375/// Genesis block timestamp (Unix timestamp)
376///
377/// The timestamp of the genesis block: January 3, 2009, 18:15:05 UTC
378/// This is the birth of Bitcoin.
379pub const GENESIS_BLOCK_TIMESTAMP: u32 = 1231006505;
380
381/// Genesis block merkle root
382///
383/// The merkle root of the genesis block's coinbase transaction.
384/// This is the root of the first transaction tree.
385pub const GENESIS_BLOCK_MERKLE_ROOT: [u8; 32] = [
386 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
387 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a,
388];
389
390/// Genesis block nonce
391///
392/// The nonce used in the genesis block's proof of work.
393pub const GENESIS_BLOCK_NONCE: u32 = 2083236893;
394
395// ============================================================================
396// Orange Paper Section 4 symbols (C, M_MAX, H, etc.)
397// ============================================================================
398// Auto-generated from blvm-spec/THE_ORANGE_PAPER.md via cargo spec-lock extract-constants
399
400/// satoshis per BTC (Orange Paper C)
401pub const C: u64 = 100_000_000;
402
403/// maximum money supply (Orange Paper M_MAX)
404pub const M_MAX: i64 = (21_000_000 * C) as i64;
405
406/// halving interval (Orange Paper H)
407pub const H: u64 = 210_000;
408
409/// maximum block weight (Orange Paper W_MAX)
410pub const W_MAX: u64 = 4_000_000;
411
412/// maximum sigops per block (Orange Paper S_MAX)
413pub const S_MAX: u64 = 80_000;
414
415/// coinbase maturity (Orange Paper R)
416pub const R: u64 = 100;
417
418/// maximum script length (Orange Paper L_SCRIPT)
419pub const L_SCRIPT: u64 = 10_000;
420
421/// maximum stack size (Orange Paper L_STACK)
422pub const L_STACK: u64 = 1_000;
423
424/// maximum operations per script (Orange Paper L_OPS)
425pub const L_OPS: u64 = 201;
426
427/// maximum element size (Orange Paper L_ELEMENT)
428pub const L_ELEMENT: u64 = 520;
429
430// ============================================================================
431// FORMAL VERIFICATION: Orange Paper Constant Locking
432// ============================================================================
433// These proofs directly verify that constant values match the Orange Paper
434// Section 4 (Consensus Constants) specification. This creates a cryptographic
435// lock between the Orange Paper (IR) and blvm-consensus implementation.
436
437#[cfg(test)]
438mod genesis_hash_byte_order_tests {
439 use super::{GENESIS_BLOCK_HASH, GENESIS_BLOCK_HASH_INTERNAL};
440
441 #[test]
442 fn genesis_internal_is_reversed_display_form() {
443 let mut rev = GENESIS_BLOCK_HASH;
444 rev.reverse();
445 assert_eq!(rev, GENESIS_BLOCK_HASH_INTERNAL);
446 }
447}
448
449#[cfg(test)]
450mod max_tx_per_block_tests {
451 use super::MAX_TRANSACTIONS_PER_BLOCK;
452
453 /// Regression: mainnet includes blocks with 12k+ txs (e.g. 2015–2016 spam waves).
454 /// A 10k cap panicked in parallel `compute_block_tx_ids` during IBD.
455 #[test]
456 fn max_transactions_per_block_covers_observed_mainnet_dense_blocks() {
457 assert!(
458 MAX_TRANSACTIONS_PER_BLOCK >= 13_000,
459 "bound {} too low for known mainnet blocks",
460 MAX_TRANSACTIONS_PER_BLOCK
461 );
462 }
463}