Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrause98 committed Jun 12, 2024
1 parent 95544b0 commit ba84b72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
25 changes: 14 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VMState>) -> (U256, VMState) {
pub fn run_program_with_custom_state(
bin_path: &str,
custom_state: &mut Option<VMState>,
) -> (U256, VMState) {
let opcode_table = synthesize_opcode_decoding_tables(11, ISAVersion(2));

let program = std::fs::read(bin_path).unwrap();
Expand All @@ -33,14 +38,13 @@ pub fn run_program_with_custom_state(bin_path: &str, custom_state: &mut Option<V
program_code.push(raw_opcode_u256);
}

let mut vm =
match custom_state {
Some(state) => {
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);
Expand Down Expand Up @@ -82,7 +86,6 @@ pub fn run_program_with_custom_state(bin_path: &str, custom_state: &mut Option<V
}
Variant::UMA(_) => todo!(),
}

}

vm.current_frame.pc += 1;
Expand Down
28 changes: 14 additions & 14 deletions src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ 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 {
registers,
flag_lt,
flag_gt,
flag_eq,
current_frame
current_frame,
}
}

Expand Down
18 changes: 12 additions & 6 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,46 +62,52 @@ 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());
}

#[test]
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());
}

#[test]
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());
}

#[test]
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());
}

#[test]
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());
}

#[test]
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());
}

0 comments on commit ba84b72

Please sign in to comment.