Skip to content

Commit

Permalink
🛠️ Add rest of EVM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 23, 2023
1 parent c3c1e3f commit 35b6479
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/mipsevm/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::Result;
///
/// The [State] by itself does not contain functionality for performing instruction steps
/// or executing the MIPS emulator. For this, use the [crate::InstrumentedState] struct.
#[derive(Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct State {
/// The [Memory] of the emulated MIPS thread context.
pub memory: Rc<RefCell<Memory>>,
Expand Down
96 changes: 95 additions & 1 deletion crates/mipsevm/src/test_utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ mod test {

#[test]
fn evm() {
// todo
let mut mips_evm = MipsEVM::new();
mips_evm.try_init().unwrap();

Expand Down Expand Up @@ -363,4 +362,99 @@ mod test {
}
}
}

#[test]
fn evm_single_step() {
let mut mips_evm = MipsEVM::new();
mips_evm.try_init().unwrap();

let cases = [
("j MSB set target", 0, 4, 0x0A_00_00_02),
(
"j non-zero PC region",
0x10_00_00_00,
0x10_00_00_04,
0x08_00_00_02,
),
("jal MSB set target", 0, 4, 0x0E_00_00_02),
(
"jal non-zero PC region",
0x10_00_00_00,
0x10_00_00_04,
0x0C_00_00_02,
),
];

for (name, pc, next_pc, instruction) in cases {
println!(" -> Running test: {name}");

let mut state = State::default();
state.pc = pc;
state.next_pc = next_pc;
state
.memory
.borrow_mut()
.set_memory(pc, instruction)
.unwrap();

let mut instrumented = InstrumentedState::new(
state,
StaticOracle::new(b"hello world".to_vec()),
io::stdout(),
io::stderr(),
);
let step_witness = instrumented.step(true).unwrap().unwrap();

let evm_post = mips_evm.step(step_witness).unwrap();
let rust_post = instrumented.state.encode_witness().unwrap();

assert_eq!(evm_post, rust_post);
}
}

#[test]
fn evm_fault() {
let mut mips_evm = MipsEVM::new();
mips_evm.try_init().unwrap();

let cases = [
("illegal instruction", 0, 0xFF_FF_FF_FFu32),
("branch in delay slot", 8, 0x11_02_00_03),
("jump in delay slot", 8, 0x0c_00_00_0c),
];

for (name, next_pc, instruction) in cases {
println!(" -> Running test: {name}");

let mut state = State::default();
state.next_pc = next_pc;
let mut initial_state = state.clone();
state
.memory
.borrow_mut()
.set_memory(0, instruction)
.unwrap();

// Set the return address ($ra) to jump to when the test completes.
state.registers[31] = END_ADDR;

let mut instrumented = InstrumentedState::new(
state,
StaticOracle::new(b"hello world".to_vec()),
io::stdout(),
io::stderr(),
);
assert!(instrumented.step(true).is_err());

let instruction_proof = initial_state.memory.borrow_mut().merkle_proof(0).unwrap();
let step_witness = StepWitness {
state: initial_state.encode_witness().unwrap(),
mem_proof: instruction_proof.to_vec(),
preimage_key: alloy_primitives::B256::ZERO,
preimage_value: Vec::default(),
preimage_offset: 0,
};
assert!(mips_evm.step(step_witness).is_err());
}
}
}

0 comments on commit 35b6479

Please sign in to comment.