fn factorial_example() {
use neo_vm_core::{NeoVM, StackItem, VMState};
let script = vec![
0x15, 0x14, 0xA0, 0x13, 0xA0, 0x12, 0xA0, 0x11, 0xA0, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
assert_eq!(result, StackItem::Integer(120));
println!("5! = {}", 120);
}
#[allow(dead_code)]
fn fibonacci_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x10, 0x11, 0x13, 0xC3, 0x4C, 0x00, 0x4C, 0x01, 0x9E, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
println!("Fibonacci example completed");
}
fn hash_example() {
use neo_vm_core::{NeoVM, StackItem, VMState};
let script = vec![
0x0C, 0x05, b'h', b'e', b'l', b'l', b'o', 0xF0, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
if let Some(StackItem::ByteString(hash)) = vm.eval_stack.pop() {
println!("SHA256('hello') = {}", hex::encode(&hash));
println!("Hash length: {} bytes", hash.len());
}
}
fn array_example() {
use neo_vm_core::{NeoVM, StackItem, VMState};
let script = vec![
0x16, 0xC3, 0xCA, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
assert_eq!(result, StackItem::Integer(6));
println!("Array size: 6");
}
fn conditional_example() {
use neo_vm_core::{NeoVM, StackItem, VMState};
let script = vec![
0x1A, 0x15, 0xB7, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
assert_eq!(result, StackItem::Boolean(true));
println!("26 > 5 is true");
}
fn error_handling_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x15, 0x10, 0xA1, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
assert!(matches!(vm.state, VMState::Fault));
println!("Division by zero correctly detected as error");
}
fn bitwise_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x1F, 0x18, 0x92, 0x17, 0x91, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
println!("Bitwise result: {:?}", result);
}
fn loop_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x10, 0x22, 0xFE, 0x40, ];
let mut vm = NeoVM::new(100); vm.load_script(script).unwrap();
let start_gas = vm.gas_consumed;
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
assert!(matches!(vm.state, VMState::Fault));
println!("Loop terminated after gas exhaustion");
println!("Gas consumed: {}", vm.gas_consumed - start_gas);
}
fn minmax_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x0B, 0x13, 0x15, 0xB9, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
println!("min(3, 5) = {:?}", result);
}
#[allow(dead_code)]
fn sign_example() {
use neo_vm_core::{NeoVM, VMState};
let script = vec![
0x0C, 0x0A, 0x10, 0x9B, 0x40, ];
let mut vm = NeoVM::new(1_000_000);
vm.load_script(script).unwrap();
while !matches!(vm.state, VMState::Halt | VMState::Fault) {
vm.execute_next().unwrap();
}
let result = vm.eval_stack.pop().unwrap();
println!("Sign example result: {:?}", result);
}
fn main() {
println!("=== Neo zkVM Examples ===\n");
println!("1. Factorial:");
factorial_example();
println!();
println!("2. Hash Computation:");
hash_example();
println!();
println!("3. Array Operations:");
array_example();
println!();
println!("4. Conditional Execution:");
conditional_example();
println!();
println!("5. Error Handling:");
error_handling_example();
println!();
println!("6. Bitwise Operations:");
bitwise_example();
println!();
println!("7. Loop with Gas Limit:");
loop_example();
println!();
println!("8. Min/Max Operations:");
minmax_example();
println!();
println!("All examples completed!");
}