Skip to content

Commit

Permalink
feat: pass brillig bytecode to VM by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 7, 2023
1 parent 03c7923 commit c8c89e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl BrilligSolver {
let mut vm = VM::new(
input_registers,
input_memory,
brillig.bytecode.clone(),
&brillig.bytecode,
brillig.foreign_call_results.clone(),
bb_solver,
);
Expand Down
65 changes: 31 additions & 34 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub enum VMStatus {

#[derive(Debug, PartialEq, Eq, Clone)]
/// VM encapsulates the state of the Brillig VM during execution.
pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> {
pub struct VM<'a, B: BlackBoxFunctionSolver> {
/// Register storage
registers: Registers,
/// Instruction pointer
Expand All @@ -72,25 +72,25 @@ pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> {
/// List is appended onto by the caller upon reaching a [VMStatus::ForeignCallWait]
foreign_call_results: Vec<ForeignCallResult>,
/// Executable opcodes
bytecode: Vec<Opcode>,
bytecode: &'a [Opcode],
/// Status of the VM
status: VMStatus,
/// Memory of the VM
memory: Memory,
/// Call stack
call_stack: Vec<Value>,
/// The solver for blackbox functions
black_box_solver: &'bb_solver B,
black_box_solver: &'a B,
}

impl<'bb_solver, B: BlackBoxFunctionSolver> VM<'bb_solver, B> {
impl<'a, B: BlackBoxFunctionSolver> VM<'a, B> {
/// Constructs a new VM instance
pub fn new(
inputs: Registers,
memory: Vec<Value>,
bytecode: Vec<Opcode>,
bytecode: &'a [Opcode],
foreign_call_results: Vec<ForeignCallResult>,
black_box_solver: &'bb_solver B,
black_box_solver: &'a B,
) -> Self {
Self {
registers: inputs,
Expand Down Expand Up @@ -443,7 +443,8 @@ mod tests {
};

// Start VM
let mut vm = VM::new(input_registers, vec![], vec![opcode], vec![], &DummyBlackBoxSolver);
let opcodes = [opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

// Process a single VM opcode
//
Expand Down Expand Up @@ -487,7 +488,7 @@ mod tests {
opcodes.push(Opcode::JumpIf { condition: RegisterIndex::from(2), location: 3 });

let mut vm =
VM::new(Registers::load(registers), vec![], opcodes, vec![], &DummyBlackBoxSolver);
VM::new(Registers::load(registers), vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -528,13 +529,9 @@ mod tests {
destination: RegisterIndex::from(2),
};

let mut vm = VM::new(
input_registers,
vec![],
vec![jump_opcode, trap_opcode, not_equal_cmp_opcode, jump_if_not_opcode, add_opcode],
vec![],
&DummyBlackBoxSolver,
);
let opcodes =
[jump_opcode, trap_opcode, not_equal_cmp_opcode, jump_if_not_opcode, add_opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -571,8 +568,8 @@ mod tests {
let mov_opcode =
Opcode::Mov { destination: RegisterIndex::from(2), source: RegisterIndex::from(0) };

let mut vm =
VM::new(input_registers, vec![], vec![mov_opcode], vec![], &DummyBlackBoxSolver);
let opcodes = &[mov_opcode];
let mut vm = VM::new(input_registers, vec![], opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::Finished);
Expand Down Expand Up @@ -629,13 +626,8 @@ mod tests {
destination: RegisterIndex::from(2),
};

let mut vm = VM::new(
input_registers,
vec![],
vec![equal_opcode, not_equal_opcode, less_than_opcode, less_than_equal_opcode],
vec![],
&DummyBlackBoxSolver,
);
let opcodes = [equal_opcode, not_equal_opcode, less_than_opcode, less_than_equal_opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -705,7 +697,9 @@ mod tests {
// if tmp != 0 goto loop_body
Opcode::JumpIf { condition: r_tmp, location: start.len() },
];
let vm = brillig_execute_and_get_vm(memory, [&start[..], &loop_body[..]].concat());

let opcodes = [&start[..], &loop_body[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.get_memory().clone()
}

Expand Down Expand Up @@ -780,7 +774,9 @@ mod tests {
// if tmp != 0 goto loop_body
Opcode::JumpIf { condition: r_tmp, location: start.len() },
];
let vm = brillig_execute_and_get_vm(memory, [&start[..], &loop_body[..]].concat());

let opcodes = [&start[..], &loop_body[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.registers.get(r_sum)
}

Expand Down Expand Up @@ -858,7 +854,8 @@ mod tests {
Opcode::Return {},
];

let vm = brillig_execute_and_get_vm(memory, [&start[..], &recursive_fn[..]].concat());
let opcodes = [&start[..], &recursive_fn[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.get_memory().clone()
}

Expand All @@ -883,8 +880,8 @@ mod tests {
/// Helper to execute brillig code
fn brillig_execute_and_get_vm(
memory: Vec<Value>,
opcodes: Vec<Opcode>,
) -> VM<'static, DummyBlackBoxSolver> {
opcodes: &[Opcode],
) -> VM<'_, DummyBlackBoxSolver> {
let mut vm = VM::new(empty_registers(), memory, opcodes, vec![], &DummyBlackBoxSolver);
brillig_execute(&mut vm);
assert_eq!(vm.call_stack, vec![]);
Expand Down Expand Up @@ -917,7 +914,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(vec![], double_program);
let mut vm = brillig_execute_and_get_vm(vec![], &double_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -978,7 +975,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), invert_program);
let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), &invert_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1051,7 +1048,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(input_string.clone(), string_double_program);
let mut vm = brillig_execute_and_get_vm(input_string.clone(), &string_double_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1113,7 +1110,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), invert_program);
let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), &invert_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1198,7 +1195,7 @@ mod tests {
];
let mut initial_memory = matrix_a.clone();
initial_memory.extend(matrix_b.clone());
let mut vm = brillig_execute_and_get_vm(initial_memory, matrix_mul_program);
let mut vm = brillig_execute_and_get_vm(initial_memory, &matrix_mul_program);

// Check that VM is waiting
assert_eq!(
Expand Down

0 comments on commit c8c89e2

Please sign in to comment.