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

Commit

Permalink
feat!: Remove backend solvable methods from the interface and solve t…
Browse files Browse the repository at this point in the history
…hem in ACVM (#264)

* Use acvm to solve bb functions when possible

* use backend for aes

* code review

* format

* Remove backend solvable opcodes from the PWG trait (#289)

* reduce diff

* revert merge overwrite

---------

Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
guipublic and kevaundray authored May 18, 2023
1 parent b9d61a1 commit 69916cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 148 deletions.
52 changes: 0 additions & 52 deletions acvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,6 @@ pub trait PartialWitnessGenerator {
inputs: &[FunctionInput],
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn and(
&self,
initial_witness: &mut WitnessMap,
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn xor(
&self,
initial_witness: &mut WitnessMap,
lhs: &FunctionInput,
rhs: &FunctionInput,
output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn range(
&self,
initial_witness: &mut WitnessMap,
input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn sha256(
&self,
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn blake2s(
&self,
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn compute_merkle_root(
&self,
initial_witness: &mut WitnessMap,
Expand All @@ -134,33 +103,12 @@ pub trait PartialWitnessGenerator {
inputs: &[FunctionInput],
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn hash_to_field_128_security(
&self,
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn ecdsa_secp256k1(
&self,
initial_witness: &mut WitnessMap,
public_key_x: &[FunctionInput],
public_key_y: &[FunctionInput],
signature: &[FunctionInput],
hashed_message: &[FunctionInput],
outputs: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn fixed_base_scalar_mul(
&self,
initial_witness: &mut WitnessMap,
input: &FunctionInput,
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
fn keccak256(
&self,
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError>;
}

pub trait SmartContract {
Expand Down
68 changes: 0 additions & 68 deletions acvm/src/pwg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,47 +270,6 @@ mod test {
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn and(
&self,
_initial_witness: &mut WitnessMap,
_lhs: &FunctionInput,
_rhs: &FunctionInput,
_output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn xor(
&self,
_initial_witness: &mut WitnessMap,
_lhs: &FunctionInput,
_rhs: &FunctionInput,
_output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn range(
&self,
_initial_witness: &mut WitnessMap,
_input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn sha256(
&self,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn blake2s(
&self,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn compute_merkle_root(
&self,
_initial_witness: &mut WitnessMap,
Expand Down Expand Up @@ -340,25 +299,6 @@ mod test {
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn hash_to_field_128_security(
&self,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn ecdsa_secp256k1(
&self,
_initial_witness: &mut WitnessMap,
_public_key_x: &[FunctionInput],
_public_key_y: &[FunctionInput],
_signature: &[FunctionInput],
_hashed_message: &[FunctionInput],
_output: &Witness,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn fixed_base_scalar_mul(
&self,
_initial_witness: &mut WitnessMap,
Expand All @@ -367,14 +307,6 @@ mod test {
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn keccak256(
&self,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
}

#[test]
Expand Down
56 changes: 28 additions & 28 deletions acvm/src/pwg/blackbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use acir::{
native_types::{Witness, WitnessMap},
};

use super::{OpcodeNotSolvable, OpcodeResolution, OpcodeResolutionError};
use super::{
hash::{blake2s256, hash_to_field_128_security, keccak256, sha256},
logic::{and, xor},
range::solve_range_opcode,
signature::ecdsa::secp256k1_prehashed,
OpcodeResolution, OpcodeResolutionError,
};
use crate::pwg::OpcodeNotSolvable;
use crate::PartialWitnessGenerator;

/// Check if all of the inputs to the function have assignments
Expand Down Expand Up @@ -33,31 +40,24 @@ pub(crate) fn solve(
bb_func: &BlackBoxFuncCall,
) -> Result<OpcodeResolution, 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,
)));
}

match bb_func {
_ if !contains_all_inputs(initial_witness, &inputs) => {
if let Some(unassigned_witness) = first_missing_assignment(initial_witness, &inputs) {
Ok(OpcodeResolution::Stalled(OpcodeNotSolvable::MissingAssignment(
unassigned_witness.0,
)))
} else {
// This only exists because Rust won't let us bind in a pattern guard.
// See https://github.com/rust-lang/rust/issues/51114
unreachable!("Only reachable if the blackbox is stalled")
}
}
BlackBoxFuncCall::AES { inputs, outputs } => backend.aes(initial_witness, inputs, outputs),
BlackBoxFuncCall::AND { lhs, rhs, output } => {
backend.and(initial_witness, lhs, rhs, output)
}
BlackBoxFuncCall::XOR { lhs, rhs, output } => {
backend.xor(initial_witness, lhs, rhs, output)
}
BlackBoxFuncCall::RANGE { input } => backend.range(initial_witness, input),
BlackBoxFuncCall::SHA256 { inputs, outputs } => {
backend.sha256(initial_witness, inputs, outputs)
acir::circuit::opcodes::BlackBoxFuncCall::AND { lhs, rhs, output } => {
and(initial_witness, lhs, rhs, output)
}
BlackBoxFuncCall::XOR { lhs, rhs, output } => xor(initial_witness, lhs, rhs, output),
BlackBoxFuncCall::RANGE { input } => solve_range_opcode(initial_witness, input),
BlackBoxFuncCall::SHA256 { inputs, outputs } => sha256(initial_witness, inputs, outputs),
BlackBoxFuncCall::Blake2s { inputs, outputs } => {
backend.blake2s(initial_witness, inputs, outputs)
blake2s256(initial_witness, inputs, outputs)
}
BlackBoxFuncCall::ComputeMerkleRoot { leaf, index, hash_path, output } => {
backend.compute_merkle_root(initial_witness, leaf, index, hash_path, output)
Expand All @@ -80,27 +80,27 @@ pub(crate) fn solve(
backend.pedersen(initial_witness, inputs, outputs)
}
BlackBoxFuncCall::HashToField128Security { inputs, output } => {
backend.hash_to_field_128_security(initial_witness, inputs, output)
hash_to_field_128_security(initial_witness, inputs, output)
}
BlackBoxFuncCall::EcdsaSecp256k1 {
public_key_x,
public_key_y,
signature,
hashed_message,
hashed_message: message,
output,
} => backend.ecdsa_secp256k1(
} => secp256k1_prehashed(
initial_witness,
public_key_x,
public_key_y,
signature,
hashed_message,
output,
message,
*output,
),
BlackBoxFuncCall::FixedBaseScalarMul { input, outputs } => {
backend.fixed_base_scalar_mul(initial_witness, input, outputs)
}
BlackBoxFuncCall::Keccak256 { inputs, outputs } => {
backend.keccak256(initial_witness, inputs, outputs)
keccak256(initial_witness, inputs, outputs)
}
}
}

0 comments on commit 69916cb

Please sign in to comment.