Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat(acvm)!: update ACVM to support stepwise execution
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jul 17, 2023
1 parent 4097d24 commit 04599d4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 227 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ brillig = { version = "0.19.0", path = "brillig", default-features = false }
blackbox_solver = { package = "acvm_blackbox_solver", version = "0.19.0", path = "blackbox_solver", default-features = false }

bincode = "1.3.3"
rmp-serde = "1.1.0"

num-bigint = "0.4"
num-traits = "0.2"
Expand Down
1 change: 0 additions & 1 deletion acvm/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ pub fn compile(
new_gates.push(intermediate_gate);
}
new_gates.push(arith_expr);
new_gates.sort();
for gate in new_gates {
new_opcode_labels.push(opcode_label[index]);
transformed_gates.push(Opcode::Arithmetic(gate));
Expand Down
36 changes: 11 additions & 25 deletions acvm/src/pwg/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,9 @@ use super::{OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};
/// block_value is the value of the Block at the solved_operations step
/// solved_operations is the number of solved elements in the block
#[derive(Default)]
pub(super) struct BlockSolver {
block_value: HashMap<u32, FieldElement>,
solved_operations: usize,
}
pub(super) struct BlockSolver;

impl BlockSolver {
fn insert_value(&mut self, index: u32, value: FieldElement) {
self.block_value.insert(index, value);
}

fn get_value(&self, index: u32) -> Option<FieldElement> {
self.block_value.get(&index).copied()
}

// Helper function which tries to solve a Block opcode
// As long as operations are resolved, we update/read from the block_value
// We stop when an operation cannot be resolved
Expand All @@ -44,7 +33,9 @@ impl BlockSolver {
))
};

for block_op in trace.iter().skip(self.solved_operations) {
let mut block_value: HashMap<u32, FieldElement> = HashMap::new();

for block_op in trace.iter() {
let op_expr = ArithmeticSolver::evaluate(&block_op.operation, initial_witness);
let operation = op_expr.to_const().ok_or_else(|| {
missing_assignment(ArithmeticSolver::any_witness_from_expression(&op_expr))
Expand All @@ -57,23 +48,24 @@ impl BlockSolver {
let value = ArithmeticSolver::evaluate(&block_op.value, initial_witness);
let value_witness = ArithmeticSolver::any_witness_from_expression(&value);
if value.is_const() {
self.insert_value(index, value.q_c);
block_value.insert(index, value.q_c);
} else if operation.is_zero() && value.is_linear() {
match ArithmeticSolver::solve_fan_in_term(&value, initial_witness) {
GateStatus::GateUnsolvable => return Err(missing_assignment(value_witness)),
GateStatus::GateSolvable(sum, (coef, w)) => {
let map_value =
self.get_value(index).ok_or_else(|| missing_assignment(Some(w)))?;
let map_value = block_value
.get(&index)
.copied()
.ok_or_else(|| missing_assignment(Some(w)))?;
insert_value(&w, (map_value - sum - value.q_c) / coef, initial_witness)?;
}
GateStatus::GateSatisfied(sum) => {
self.insert_value(index, sum + value.q_c);
block_value.insert(index, sum + value.q_c);
}
}
} else {
return Err(missing_assignment(value_witness));
}
self.solved_operations += 1;
}
Ok(())
}
Expand All @@ -86,16 +78,10 @@ impl BlockSolver {
initial_witness: &mut WitnessMap,
trace: &[MemOp],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
let initial_solved_operations = self.solved_operations;

match self.solve_helper(initial_witness, trace) {
Ok(()) => Ok(OpcodeResolution::Solved),
Err(OpcodeResolutionError::OpcodeNotSolvable(err)) => {
if self.solved_operations > initial_solved_operations {
Ok(OpcodeResolution::InProgress)
} else {
Ok(OpcodeResolution::Stalled(err))
}
Ok(OpcodeResolution::Stalled(err))
}
Err(err) => Err(err),
}
Expand Down
Loading

0 comments on commit 04599d4

Please sign in to comment.