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

Commit

Permalink
feat(acvm)!: Remove OpcodeResolution enum (#400)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
TomAFrench and kevaundray authored Jul 26, 2023
1 parent 6a03950 commit d0ce48c
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 126 deletions.
2 changes: 1 addition & 1 deletion acvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use acir::{
};
pub use blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError};
use core::fmt::Debug;
use pwg::{OpcodeResolution, OpcodeResolutionError};
use pwg::OpcodeResolutionError;

// We re-export async-trait so consumers can attach it to their impl
pub use async_trait::async_trait;
Expand Down
34 changes: 17 additions & 17 deletions acvm/src/pwg/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acir::{
FieldElement,
};

use super::{insert_value, OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};
use super::{insert_value, OpcodeNotSolvable, OpcodeResolutionError};

/// An Arithmetic solver will take a Circuit's arithmetic gates with witness assignments
/// and create the other witness variables
Expand All @@ -28,7 +28,7 @@ impl ArithmeticSolver {
pub(super) fn solve(
initial_witness: &mut WitnessMap,
gate: &Expression,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let gate = &ArithmeticSolver::evaluate(gate, initial_witness);
// Evaluate multiplication term
let mul_result = ArithmeticSolver::solve_mul_term(gate, initial_witness);
Expand All @@ -37,9 +37,9 @@ impl ArithmeticSolver {

match (mul_result, gate_status) {
(MulTerm::TooManyUnknowns, _) | (_, GateStatus::GateUnsolvable) => {
Ok(OpcodeResolution::Stalled(OpcodeNotSolvable::ExpressionHasTooManyUnknowns(
gate.clone(),
)))
Err(OpcodeResolutionError::OpcodeNotSolvable(
OpcodeNotSolvable::ExpressionHasTooManyUnknowns(gate.clone()),
))
}
(MulTerm::OneUnknown(q, w1), GateStatus::GateSolvable(a, (b, w2))) => {
if w1 == w2 {
Expand All @@ -51,19 +51,19 @@ impl ArithmeticSolver {
opcode_label: OpcodeLabel::Unresolved,
})
} else {
Ok(OpcodeResolution::Solved)
Ok(())
}
} else {
let assignment = -total_sum / (q + b);
// Add this into the witness assignments
insert_value(&w1, assignment, initial_witness)?;
Ok(OpcodeResolution::Solved)
Ok(())
}
} else {
// TODO: can we be more specific with this error?
Ok(OpcodeResolution::Stalled(OpcodeNotSolvable::ExpressionHasTooManyUnknowns(
gate.clone(),
)))
Err(OpcodeResolutionError::OpcodeNotSolvable(
OpcodeNotSolvable::ExpressionHasTooManyUnknowns(gate.clone()),
))
}
}
(MulTerm::OneUnknown(partial_prod, unknown_var), GateStatus::GateSatisfied(sum)) => {
Expand All @@ -78,13 +78,13 @@ impl ArithmeticSolver {
opcode_label: OpcodeLabel::Unresolved,
})
} else {
Ok(OpcodeResolution::Solved)
Ok(())
}
} else {
let assignment = -(total_sum / partial_prod);
// Add this into the witness assignments
insert_value(&unknown_var, assignment, initial_witness)?;
Ok(OpcodeResolution::Solved)
Ok(())
}
}
(MulTerm::Solved(a), GateStatus::GateSatisfied(b)) => {
Expand All @@ -95,7 +95,7 @@ impl ArithmeticSolver {
opcode_label: OpcodeLabel::Unresolved,
})
} else {
Ok(OpcodeResolution::Solved)
Ok(())
}
}
(
Expand All @@ -112,13 +112,13 @@ impl ArithmeticSolver {
opcode_label: OpcodeLabel::Unresolved,
})
} else {
Ok(OpcodeResolution::Solved)
Ok(())
}
} else {
let assignment = -(total_sum / coeff);
// Add this into the witness assignments
insert_value(&unknown_var, assignment, initial_witness)?;
Ok(OpcodeResolution::Solved)
Ok(())
}
}
}
Expand Down Expand Up @@ -272,8 +272,8 @@ fn arithmetic_smoke_test() {
values.insert(c, FieldElement::from(1_i128));
values.insert(d, FieldElement::from(1_i128));

assert_eq!(ArithmeticSolver::solve(&mut values, &gate_a), Ok(OpcodeResolution::Solved));
assert_eq!(ArithmeticSolver::solve(&mut values, &gate_b), Ok(OpcodeResolution::Solved));
assert_eq!(ArithmeticSolver::solve(&mut values, &gate_a), Ok(()));
assert_eq!(ArithmeticSolver::solve(&mut values, &gate_b), Ok(()));

assert_eq!(values.get(&a).unwrap(), &FieldElement::from(4_i128));
}
6 changes: 3 additions & 3 deletions acvm/src/pwg/blackbox/fixed_base_scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acir::{
};

use crate::{
pwg::{insert_value, witness_to_value, OpcodeResolution, OpcodeResolutionError},
pwg::{insert_value, witness_to_value, OpcodeResolutionError},
BlackBoxFunctionSolver,
};

Expand All @@ -13,13 +13,13 @@ pub(super) fn fixed_base_scalar_mul(
initial_witness: &mut WitnessMap,
input: FunctionInput,
outputs: (Witness, Witness),
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let scalar = witness_to_value(initial_witness, input.witness)?;

let (pub_x, pub_y) = backend.fixed_base_scalar_mul(scalar)?;

insert_value(&outputs.0, pub_x, initial_witness)?;
insert_value(&outputs.1, pub_y, initial_witness)?;

Ok(OpcodeResolution::Solved)
Ok(())
}
10 changes: 5 additions & 5 deletions acvm/src/pwg/blackbox/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use acir::{
use blackbox_solver::{hash_to_field_128_security, BlackBoxResolutionError};

use crate::pwg::{insert_value, witness_to_value};
use crate::{pwg::OpcodeResolution, OpcodeResolutionError};
use crate::OpcodeResolutionError;

/// Attempts to solve a `HashToField128Security` opcode
/// If successful, `initial_witness` will be mutated to contain the new witness assignment.
pub(super) fn solve_hash_to_field(
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let message_input = get_hash_input(initial_witness, inputs, None)?;
let field = hash_to_field_128_security(&message_input)?;

insert_value(output, field, initial_witness)?;

Ok(OpcodeResolution::Solved)
Ok(())
}

/// Attempts to solve a 256 bit hash function opcode.
Expand All @@ -32,7 +32,7 @@ pub(super) fn solve_generic_256_hash_opcode(
outputs: &[Witness],
hash_function: fn(data: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError>,
black_box_func: BlackBoxFunc,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let message_input = get_hash_input(initial_witness, inputs, var_message_size)?;
let digest: [u8; 32] = hash_function(&message_input)?;

Expand All @@ -44,7 +44,7 @@ pub(super) fn solve_generic_256_hash_opcode(
})?;
write_digest_to_outputs(initial_witness, outputs, digest)?;

Ok(OpcodeResolution::Solved)
Ok(())
}

/// Reads the hash function input from a [`WitnessMap`].
Expand Down
11 changes: 5 additions & 6 deletions acvm/src/pwg/blackbox/logic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::pwg::{insert_value, witness_to_value};
use crate::{pwg::OpcodeResolution, OpcodeResolutionError};
use crate::OpcodeResolutionError;
use acir::{
circuit::opcodes::FunctionInput,
native_types::{Witness, WitnessMap},
Expand All @@ -13,7 +13,7 @@ pub(super) fn and(
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
assert_eq!(
lhs.num_bits, rhs.num_bits,
"number of bits specified for each input must be the same"
Expand All @@ -30,7 +30,7 @@ pub(super) fn xor(
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
assert_eq!(
lhs.num_bits, rhs.num_bits,
"number of bits specified for each input must be the same"
Expand All @@ -47,11 +47,10 @@ fn solve_logic_gate(
b: &Witness,
result: Witness,
logic_op: impl Fn(&FieldElement, &FieldElement) -> FieldElement,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let w_l_value = witness_to_value(initial_witness, *a)?;
let w_r_value = witness_to_value(initial_witness, *b)?;
let assignment = logic_op(w_l_value, w_r_value);

insert_value(&result, assignment, initial_witness)?;
Ok(OpcodeResolution::Solved)
insert_value(&result, assignment, initial_witness)
}
12 changes: 6 additions & 6 deletions acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acir::{
};
use blackbox_solver::{blake2s, keccak256, sha256};

use super::{OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};
use super::{OpcodeNotSolvable, OpcodeResolutionError};
use crate::BlackBoxFunctionSolver;

mod fixed_base_scalar_mul;
Expand Down Expand Up @@ -50,14 +50,14 @@ pub(crate) fn solve(
backend: &impl BlackBoxFunctionSolver,
initial_witness: &mut WitnessMap,
bb_func: &BlackBoxFuncCall,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let inputs = bb_func.get_inputs_vec();
if !contains_all_inputs(initial_witness, &inputs) {
let unassigned_witness = first_missing_assignment(initial_witness, &inputs)
.expect("Some assignments must be missing because it does not contains all inputs");
return Ok(OpcodeResolution::Stalled(OpcodeNotSolvable::MissingAssignment(
unassigned_witness.0,
)));
return Err(OpcodeResolutionError::OpcodeNotSolvable(
OpcodeNotSolvable::MissingAssignment(unassigned_witness.0),
));
}

match bb_func {
Expand Down Expand Up @@ -150,6 +150,6 @@ pub(crate) fn solve(
BlackBoxFuncCall::FixedBaseScalarMul { input, outputs } => {
fixed_base_scalar_mul(backend, initial_witness, *input, *outputs)
}
BlackBoxFuncCall::RecursiveAggregation { .. } => Ok(OpcodeResolution::Solved),
BlackBoxFuncCall::RecursiveAggregation { .. } => Ok(()),
}
}
6 changes: 3 additions & 3 deletions acvm/src/pwg/blackbox/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use acir::{
};

use crate::{
pwg::{insert_value, witness_to_value, OpcodeResolution, OpcodeResolutionError},
pwg::{insert_value, witness_to_value, OpcodeResolutionError},
BlackBoxFunctionSolver,
};

Expand All @@ -14,7 +14,7 @@ pub(super) fn pedersen(
inputs: &[FunctionInput],
domain_separator: u32,
outputs: (Witness, Witness),
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let scalars: Result<Vec<_>, _> =
inputs.iter().map(|input| witness_to_value(initial_witness, input.witness)).collect();
let scalars: Vec<_> = scalars?.into_iter().cloned().collect();
Expand All @@ -24,5 +24,5 @@ pub(super) fn pedersen(
insert_value(&outputs.0, res_x, initial_witness)?;
insert_value(&outputs.1, res_y, initial_witness)?;

Ok(OpcodeResolution::Solved)
Ok(())
}
6 changes: 3 additions & 3 deletions acvm/src/pwg/blackbox/range.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{pwg::witness_to_value, pwg::OpcodeResolution, OpcodeResolutionError};
use crate::{pwg::witness_to_value, OpcodeResolutionError};
use acir::{
circuit::{opcodes::FunctionInput, OpcodeLabel},
native_types::WitnessMap,
Expand All @@ -7,12 +7,12 @@ use acir::{
pub(super) fn solve_range_opcode(
initial_witness: &mut WitnessMap,
input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let w_value = witness_to_value(initial_witness, input.witness)?;
if w_value.num_bits() > input.num_bits {
return Err(OpcodeResolutionError::UnsatisfiedConstrain {
opcode_label: OpcodeLabel::Unresolved,
});
}
Ok(OpcodeResolution::Solved)
Ok(())
}
13 changes: 5 additions & 8 deletions acvm/src/pwg/blackbox/signature/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use acir::{
};
use blackbox_solver::{ecdsa_secp256k1_verify, ecdsa_secp256r1_verify};

use crate::{
pwg::{insert_value, OpcodeResolution},
OpcodeResolutionError,
};
use crate::{pwg::insert_value, OpcodeResolutionError};

use super::to_u8_vec;

Expand All @@ -19,7 +16,7 @@ pub(crate) fn secp256k1_prehashed(
signature_inputs: &[FunctionInput],
hashed_message_inputs: &[FunctionInput],
output: Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let hashed_message = to_u8_vec(initial_witness, hashed_message_inputs)?;

// These errors should never be emitted in practice as they would imply malformed ACIR generation.
Expand Down Expand Up @@ -50,7 +47,7 @@ pub(crate) fn secp256k1_prehashed(
let is_valid = ecdsa_secp256k1_verify(&hashed_message, &pub_key_x, &pub_key_y, &signature)?;

insert_value(&output, FieldElement::from(is_valid), initial_witness)?;
Ok(OpcodeResolution::Solved)
Ok(())
}

pub(crate) fn secp256r1_prehashed(
Expand All @@ -60,7 +57,7 @@ pub(crate) fn secp256r1_prehashed(
signature_inputs: &[FunctionInput],
hashed_message_inputs: &[FunctionInput],
output: Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let hashed_message = to_u8_vec(initial_witness, hashed_message_inputs)?;

let pub_key_x: [u8; 32] =
Expand Down Expand Up @@ -90,5 +87,5 @@ pub(crate) fn secp256r1_prehashed(
let is_valid = ecdsa_secp256r1_verify(&hashed_message, &pub_key_x, &pub_key_y, &signature)?;

insert_value(&output, FieldElement::from(is_valid), initial_witness)?;
Ok(OpcodeResolution::Solved)
Ok(())
}
6 changes: 3 additions & 3 deletions acvm/src/pwg/blackbox/signature/schnorr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::to_u8_vec;
use crate::{
pwg::{insert_value, witness_to_value, OpcodeResolution, OpcodeResolutionError},
pwg::{insert_value, witness_to_value, OpcodeResolutionError},
BlackBoxFunctionSolver,
};
use acir::{
Expand All @@ -18,7 +18,7 @@ pub(crate) fn schnorr_verify(
signature: &[FunctionInput],
message: &[FunctionInput],
output: Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
) -> Result<(), OpcodeResolutionError> {
let public_key_x: &FieldElement = witness_to_value(initial_witness, public_key_x.witness)?;
let public_key_y: &FieldElement = witness_to_value(initial_witness, public_key_y.witness)?;

Expand All @@ -31,5 +31,5 @@ pub(crate) fn schnorr_verify(

insert_value(&output, FieldElement::from(valid_signature), initial_witness)?;

Ok(OpcodeResolution::Solved)
Ok(())
}
Loading

0 comments on commit d0ce48c

Please sign in to comment.