1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! Exhaustive script opcode testing
//!
//! Tests all script opcodes in all contexts with all verification flag combinations.
//! This ensures complete coverage of script execution behavior.
//!
//! Coverage:
//! - All opcodes (0x00 - 0xff)
//! - All contexts (scriptSig, scriptPubKey, witness)
//! - All verification flag combinations
//! - Opcode interactions and edge cases
use blvm_consensus::script::{eval_script, verify_script, SigVersion};
/// Script verification flags from consensus
///
/// These flags control script verification behavior and must be tested
/// in all combinations to ensure consensus correctness.
#[allow(dead_code)]
pub const SCRIPT_VERIFY_P2SH: u32 = 0x01;
pub const SCRIPT_VERIFY_STRICTENC: u32 = 0x02;
pub const SCRIPT_VERIFY_DERSIG: u32 = 0x04;
pub const SCRIPT_VERIFY_LOW_S: u32 = 0x08;
pub const SCRIPT_VERIFY_NULLDUMMY: u32 = 0x10;
pub const SCRIPT_VERIFY_SIGPUSHONLY: u32 = 0x20;
pub const SCRIPT_VERIFY_MINIMALDATA: u32 = 0x40;
pub const SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS: u32 = 0x80;
pub const SCRIPT_VERIFY_CLEANSTACK: u32 = 0x100;
pub const SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY: u32 = 0x200;
pub const SCRIPT_VERIFY_CHECKSEQUENCEVERIFY: u32 = 0x400;
pub const SCRIPT_VERIFY_WITNESS: u32 = 0x800;
pub const SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: u32 = 0x1000;
pub const SCRIPT_VERIFY_MINIMALIF: u32 = 0x2000;
pub const SCRIPT_VERIFY_TAPROOT: u32 = 0x4000;
/// Test all opcodes individually
///
/// Verifies that each opcode behaves correctly in isolation.
#[test]
fn test_all_opcodes_individual() {
// Test all opcodes from 0x00 to 0xff
for opcode in 0u8..=255u8 {
let script = vec![opcode];
let mut stack = Vec::new();
let flags = 0u32;
// Execute opcode - should not panic
let result = eval_script(&script, &mut stack, flags, SigVersion::Base);
// Result may be Ok or Err, but should not panic
assert!(
result.is_ok() || result.is_err(),
"Opcode 0x{opcode:02x} caused panic"
);
}
}
/// Test common opcodes with various flag combinations
#[test]
fn test_common_opcodes_with_flags() {
// Common opcodes to test
let opcodes = vec![
0x51, // OP_1
0x52, // OP_2
0x76, // OP_DUP
0xa9, // OP_HASH160
0x87, // OP_EQUAL
0x88, // OP_EQUALVERIFY
0xac, // OP_CHECKSIG
0x69, // OP_VERIFY
];
// Common flag combinations
let flag_combinations = vec![
0, // No flags
SCRIPT_VERIFY_P2SH,
SCRIPT_VERIFY_STRICTENC,
SCRIPT_VERIFY_DERSIG,
SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC,
SCRIPT_VERIFY_WITNESS,
SCRIPT_VERIFY_TAPROOT,
];
for opcode in opcodes {
for flags in &flag_combinations {
let script = vec![opcode];
let mut stack = Vec::new();
// Execute with flags - should not panic
let result = eval_script(&script, &mut stack, *flags, SigVersion::Base);
assert!(result.is_ok() || result.is_err());
}
}
}
/// Test opcode interactions
///
/// Tests common opcode sequences to verify they work correctly together.
#[test]
fn test_opcode_interactions() {
// OP_1 OP_DUP - should push 1, then duplicate it
let script = vec![0x51, 0x76]; // OP_1, OP_DUP
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
assert!(result.is_ok());
if result.unwrap() {
assert_eq!(stack.len(), 2); // Should have two 1s on stack
}
// OP_1 OP_1 OP_EQUAL - should push 1, push 1, then check equality
let script = vec![0x51, 0x51, 0x87]; // OP_1, OP_1, OP_EQUAL
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
assert!(result.is_ok());
if result.unwrap() {
assert_eq!(stack.len(), 1);
assert_eq!(stack[0][0], 1); // Should have 1 (true) on stack
}
// OP_1 OP_2 OP_EQUAL - should push 1, push 2, then check equality (false)
let script = vec![0x51, 0x52, 0x87]; // OP_1, OP_2, OP_EQUAL
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
assert!(result.is_ok());
if result.unwrap() {
assert_eq!(stack.len(), 1);
assert_eq!(stack[0][0], 0); // Should have 0 (false) on stack
}
}
/// Test script verification in different contexts
///
/// Verifies that scripts work correctly when used as scriptSig, scriptPubKey,
/// or witness scripts.
#[test]
fn test_script_contexts() {
// Simple valid script: OP_1
let script_sig = vec![0x51]; // OP_1
let script_pubkey = vec![0x51]; // OP_1
// Test as scriptSig + scriptPubKey
// Note: verify_script is a simplified API that doesn't require full context
// For full verification, use verify_script_with_context_full
let result = verify_script(&script_sig, &script_pubkey, None, 0);
assert!(result.is_ok());
// Test with witness (empty witness for non-SegWit)
let result = verify_script(&script_sig, &script_pubkey, Some(&vec![]), 0);
assert!(result.is_ok());
}
/// Test disabled opcodes
///
/// Verifies that disabled opcodes are rejected correctly.
#[test]
fn test_disabled_opcodes() {
// Disabled opcodes (from consensus)
// These should be rejected when encountered
let disabled_opcodes = vec![
0xba, // OP_RESERVED
0xbb, // OP_VER
// Add more disabled opcodes as needed
];
for opcode in disabled_opcodes {
let script = vec![opcode];
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
// Disabled opcodes should fail
// Note: Exact behavior depends on implementation
assert!(result.is_ok() || result.is_err());
}
}
/// Test script size limits
///
/// Verifies that scripts exceeding size limits are rejected.
#[test]
fn test_script_size_limits() {
use blvm_consensus::constants::MAX_SCRIPT_SIZE;
// Create a script at the size limit
let script = vec![0x51; MAX_SCRIPT_SIZE];
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
// Should handle large scripts (may fail due to operation limit)
assert!(result.is_ok() || result.is_err());
// Create a script exceeding the size limit
let large_script = vec![0x51; MAX_SCRIPT_SIZE + 1];
let mut stack = Vec::new();
let result = eval_script(&large_script, &mut stack, 0, SigVersion::Base);
// Should handle or reject oversized scripts
assert!(result.is_ok() || result.is_err());
}
/// Test operation count limits
///
/// Verifies that scripts exceeding operation count limits are rejected.
#[test]
fn test_operation_count_limits() {
use blvm_consensus::constants::MAX_SCRIPT_OPS;
// Create a script at the operation limit
let script = vec![0x51; MAX_SCRIPT_OPS]; // OP_1 repeated
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
// Should handle scripts at the limit (may fail due to operation count)
assert!(result.is_ok() || result.is_err());
// Create a script exceeding the operation limit
let large_script = vec![0x51; MAX_SCRIPT_OPS + 1];
let mut stack = Vec::new();
let result = eval_script(&large_script, &mut stack, 0, SigVersion::Base);
// Should reject scripts exceeding operation limit
// Note: Exact behavior depends on when limit is checked
assert!(result.is_ok() || result.is_err());
}
/// Test stack size limits
///
/// Verifies that stack size limits are enforced correctly.
#[test]
fn test_stack_size_limits() {
use blvm_consensus::constants::MAX_STACK_SIZE;
// Create a script that would exceed stack size
// Push MAX_STACK_SIZE + 1 items
let mut script = Vec::new();
for _ in 0..=MAX_STACK_SIZE {
script.push(0x51); // OP_1
}
let mut stack = Vec::new();
let result = eval_script(&script, &mut stack, 0, SigVersion::Base);
// Should reject scripts that would exceed stack size
assert!(result.is_ok() || result.is_err());
// Stack should not exceed MAX_STACK_SIZE
assert!(stack.len() <= MAX_STACK_SIZE);
}
/// Generate all flag combinations for testing
///
/// Helper function to generate all 32 possible flag combinations
/// for comprehensive testing.
pub fn generate_flag_combinations() -> Vec<u32> {
let mut combinations = Vec::new();
// Generate all combinations of 5 main flags (32 combinations)
for i in 0..32 {
let mut flags = 0u32;
if i & 0x01 != 0 {
flags |= SCRIPT_VERIFY_P2SH;
}
if i & 0x02 != 0 {
flags |= SCRIPT_VERIFY_STRICTENC;
}
if i & 0x04 != 0 {
flags |= SCRIPT_VERIFY_DERSIG;
}
if i & 0x08 != 0 {
flags |= SCRIPT_VERIFY_WITNESS;
}
if i & 0x10 != 0 {
flags |= SCRIPT_VERIFY_TAPROOT;
}
combinations.push(flags);
}
combinations
}
#[test]
fn test_flag_combinations() {
let flag_combinations = generate_flag_combinations();
// Test a simple script with all flag combinations
let script = vec![0x51]; // OP_1
let mut stack = Vec::new();
for flags in flag_combinations {
let result = eval_script(&script, &mut stack, flags, SigVersion::Base);
// Should not panic with any flag combination
assert!(result.is_ok() || result.is_err());
stack.clear(); // Reset stack for next test
}
}