diff --git a/src/lib.rs b/src/lib.rs index 1d4fa654..6ae6e212 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,9 +14,14 @@ use zkevm_opcode_defs::ISAVersion; use zkevm_opcode_defs::LogOpcode; use zkevm_opcode_defs::Opcode as Variant; -pub fn run_program(bin_path: &str) -> (U256, VMState) { run_program_with_custom_state(bin_path, &mut None) } +pub fn run_program(bin_path: &str) -> (U256, VMState) { + run_program_with_custom_state(bin_path, &mut None) +} -pub fn run_program_with_custom_state(bin_path: &str, custom_state: &mut Option) -> (U256, VMState) { +pub fn run_program_with_custom_state( + bin_path: &str, + custom_state: &mut Option, +) -> (U256, VMState) { let opcode_table = synthesize_opcode_decoding_tables(11, ISAVersion(2)); let program = std::fs::read(bin_path).unwrap(); @@ -33,14 +38,13 @@ pub fn run_program_with_custom_state(bin_path: &str, custom_state: &mut Option { - state.load_program(program_code); - state - } - None => &mut VMState::new(program_code) - }; + let mut vm = match custom_state { + Some(state) => { + state.load_program(program_code); + state + } + None => &mut VMState::new(program_code), + }; loop { let opcode = vm.get_opcode(&opcode_table); @@ -82,7 +86,6 @@ pub fn run_program_with_custom_state(bin_path: &str, custom_state: &mut Option todo!(), } - } vm.current_frame.pc += 1; diff --git a/src/opcode.rs b/src/opcode.rs index 28b5b76f..84b3aac9 100644 --- a/src/opcode.rs +++ b/src/opcode.rs @@ -60,20 +60,20 @@ impl Opcode { let imm0: u16 = ((raw_op & 0xffff00000000) >> 32) as u16; let imm1: u16 = ((raw_op & 0xffff000000000000) >> 48) as u16; - Self { - variant: opcode_zksync.opcode, - src0_operand_type: opcode_zksync.src0_operand_type, - dst0_operand_type: opcode_zksync.dst0_operand_type, - predicate: Predicate::from(predicate_u8), - alters_vm_flags, - swap_flag, - src0_index: first_four_bits(src0_and_1_index), - src1_index: second_four_bits(src0_and_1_index), - dst0_index: first_four_bits(dst0_and_1_index), - dst1_index: second_four_bits(dst0_and_1_index), - imm0, - imm1, - } + Self { + variant: opcode_zksync.opcode, + src0_operand_type: opcode_zksync.src0_operand_type, + dst0_operand_type: opcode_zksync.dst0_operand_type, + predicate: Predicate::from(predicate_u8), + alters_vm_flags, + swap_flag, + src0_index: first_four_bits(src0_and_1_index), + src1_index: second_four_bits(src0_and_1_index), + dst0_index: first_four_bits(dst0_and_1_index), + dst1_index: second_four_bits(dst0_and_1_index), + imm0, + imm1, + } } } diff --git a/src/state.rs b/src/state.rs index 9103fc57..4d6a131c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -42,11 +42,7 @@ impl VMState { } } - pub fn new_with_flag_state( - flag_lt: bool, - flag_eq: bool, - flag_gt: bool, - ) -> Self { + pub fn new_with_flag_state(flag_lt: bool, flag_eq: bool, flag_gt: bool) -> Self { let registers = [U256::zero(); 15]; let current_frame = CallFrame::new(vec![]); Self { @@ -54,7 +50,7 @@ impl VMState { flag_lt, flag_gt, flag_eq, - current_frame + current_frame, } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 36adc8f9..0883eb75 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -62,7 +62,8 @@ fn test_add_does_not_run_if_eq_is_not_set() { fn test_add_runs_if_eq_is_set() { let bin_path = make_bin_path_asm("add_conditional_eq"); let vm_with_custom_flags = VMState::new_with_flag_state(false, true, false); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("10").unwrap()); } @@ -70,7 +71,8 @@ fn test_add_runs_if_eq_is_set() { fn test_add_does_run_if_lt_is_set() { let bin_path = make_bin_path_asm("add_conditional_lt"); let vm_with_custom_flags = VMState::new_with_flag_state(true, false, true); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("10").unwrap()); } @@ -78,7 +80,8 @@ fn test_add_does_run_if_lt_is_set() { fn test_add_does_not_run_if_lt_is_not_set() { let bin_path = make_bin_path_asm("add_conditional_not_lt"); let vm_with_custom_flags = VMState::new_with_flag_state(true, false, true); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("10").unwrap()); } @@ -86,7 +89,8 @@ fn test_add_does_not_run_if_lt_is_not_set() { fn test_add_does_run_if_gt_is_set() { let bin_path = make_bin_path_asm("add_conditional_gt"); let vm_with_custom_flags = VMState::new_with_flag_state(true, false, true); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("20").unwrap()); } @@ -94,7 +98,8 @@ fn test_add_does_run_if_gt_is_set() { fn test_add_does_not_run_if_gt_is_not_set() { let bin_path = make_bin_path_asm("add_conditional_not_gt"); let vm_with_custom_flags = VMState::new_with_flag_state(false, false, false); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("0").unwrap()); } @@ -102,6 +107,7 @@ fn test_add_does_not_run_if_gt_is_not_set() { fn test_more_complex_program_with_conditionals() { let bin_path = make_bin_path_asm("add_and_sub_with_conditionals"); let vm_with_custom_flags = VMState::new_with_flag_state(false, true, false); - let (result, final_vm_state) = run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); + let (result, final_vm_state) = + run_program_with_custom_state(&bin_path, &mut Some(vm_with_custom_flags)); assert_eq!(result, U256::from_dec_str("10").unwrap()); }