Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Move WitnessMap type into ACVM to avoid leaking BTreeMap type
Browse files Browse the repository at this point in the history
phated committed May 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b71f690 commit e1f5b61
Showing 11 changed files with 57 additions and 109 deletions.
85 changes: 18 additions & 67 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -24,8 +24,7 @@ edition = "2021"
rust-version = "1.66"

[workspace.dependencies]
#acvm = "0.11.0"
acvm = { git = "https://github.com/noir-lang/acvm", rev = "018d6865e03ddd09ed409b383f38971ec9ec3310" }
acvm = "0.11.0"
arena = { path = "crates/arena" }
fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
@@ -53,5 +52,5 @@ wasm-bindgen = { version = "0.2.83", features = ["serde-serialize"] }
wasm-bindgen-test = "0.3.33"

[patch.crates-io]
acvm = { package = "acvm", git = "https://github.com/noir-lang/acvm", rev = "a83333b9e270dfcfd40a36271896840ec0201bc4" }
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/acvm-backend-barretenberg", rev = "a97dc35affc33db015a0f84f7e0e22dd143ac558" }
acvm = { package = "acvm", git = "https://github.com/noir-lang/acvm", rev = "bbd9ab7ca5be3fb31f3e141fee2522704852f5de" }
acvm-backend-barretenberg = { git = "https://github.com/noir-lang/acvm-backend-barretenberg", rev = "4f622b58144330ebc53d8458bd9e456a8efb147c" }
3 changes: 1 addition & 2 deletions crates/nargo/src/ops/execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use acvm::pwg::{solve, PartialWitnessGeneratorStatus};
use acvm::PartialWitnessGenerator;
use acvm::{acir::circuit::Circuit, pwg::block::Blocks};
use noirc_abi::WitnessMap;
use acvm::{acir::circuit::Circuit, acir::native_types::WitnessMap, pwg::block::Blocks};

use crate::NargoError;

3 changes: 1 addition & 2 deletions crates/nargo/src/ops/prove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use acvm::acir::circuit::Circuit;
use acvm::acir::{circuit::Circuit, native_types::WitnessMap};
use acvm::ProofSystemCompiler;
use noirc_abi::WitnessMap;

pub fn prove_execution<B: ProofSystemCompiler>(
backend: &B,
3 changes: 1 addition & 2 deletions crates/nargo/src/ops/verify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use acvm::acir::circuit::Circuit;
use acvm::acir::{circuit::Circuit, native_types::WitnessMap};
use acvm::ProofSystemCompiler;
use noirc_abi::WitnessMap;

pub fn verify_proof<B: ProofSystemCompiler>(
backend: &B,
4 changes: 2 additions & 2 deletions crates/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::Path;

use acvm::acir::circuit::Circuit;
use acvm::acir::{circuit::Circuit, native_types::WitnessMap};
use acvm::Backend;
use clap::Args;
use noirc_abi::input_parser::{Format, InputValue};
use noirc_abi::{Abi, InputMap, WitnessMap};
use noirc_abi::{Abi, InputMap};
use noirc_driver::{CompileOptions, CompiledProgram};

use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
5 changes: 2 additions & 3 deletions crates/nargo_cli/src/cli/fs/witness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::{Path, PathBuf};

use acvm::acir::native_types::Witness;
use noirc_abi::WitnessMap;
use acvm::acir::native_types::WitnessMap;

use super::{create_named_dir, write_to_file};
use crate::{constants::WITNESS_EXT, errors::FilesystemError};
@@ -14,7 +13,7 @@ pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
create_named_dir(witness_dir.as_ref(), "witness");
let witness_path = witness_dir.as_ref().join(witness_name).with_extension(WITNESS_EXT);

let buf = Witness::to_bytes(&witness);
let buf: Vec<u8> = witness.try_into()?;

write_to_file(buf.as_slice(), &witness_path);

6 changes: 3 additions & 3 deletions crates/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, io::Write, path::Path};
use std::{io::Write, path::Path};

use acvm::Backend;
use acvm::{acir::native_types::WitnessMap, Backend};
use clap::Args;
use nargo::ops::execute_circuit;
use noirc_driver::{CompileOptions, Driver};
@@ -89,7 +89,7 @@ fn run_test<B: Backend>(

// Run the backend to ensure the PWG evaluates functions like std::hash::pedersen,
// otherwise constraints involving these expressions will not error.
match execute_circuit(backend, program.circuit, BTreeMap::new()) {
match execute_circuit(backend, program.circuit, WitnessMap::new()) {
Ok(_) => Ok(()),
Err(error) => {
let writer = StandardStream::stderr(ColorChoice::Always);
6 changes: 5 additions & 1 deletion crates/nargo_cli/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use acvm::{Backend, ProofSystemCompiler, SmartContract};
use acvm::{acir::native_types::WitnessMapError, Backend, ProofSystemCompiler, SmartContract};
use hex::FromHexError;
use nargo::NargoError;
use noirc_abi::errors::{AbiError, InputParserError};
@@ -21,6 +21,10 @@ pub(crate) enum FilesystemError {
/// Input parsing error
#[error(transparent)]
InputParserError(#[from] InputParserError),

/// WitnessMap serialization error
#[error(transparent)]
WitnessMapSerialization(#[from] WitnessMapError),
}

#[derive(Debug, Error)]
12 changes: 6 additions & 6 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,10 @@

use std::{collections::BTreeMap, str};

use acvm::{acir::native_types::Witness, FieldElement};
use acvm::{
acir::native_types::{Witness, WitnessMap},
FieldElement,
};
use errors::AbiError;
use input_parser::InputValue;
use iter_extended::{try_btree_map, try_vecmap, vecmap};
@@ -22,9 +25,6 @@ mod serialization;
/// A map from the fields in an TOML/JSON file which correspond to some ABI to their values
pub type InputMap = BTreeMap<String, InputValue>;

/// A map from the witnesses in a constraint system to the field element values
pub type WitnessMap = BTreeMap<Witness, FieldElement>;

/// A tuple of the arguments to a function along with its return value.
pub type FunctionSignature = (Vec<AbiParameter>, Option<AbiType>);

@@ -254,7 +254,7 @@ impl Abi {
.collect::<Result<_, _>>()?;

// Write input field elements into witness indices specified in `self.param_witnesses`.
let mut witness_map: WitnessMap = encoded_input_map
let mut witness_map: BTreeMap<Witness, FieldElement> = encoded_input_map
.iter()
.flat_map(|(param_name, encoded_param_fields)| {
let param_witness_indices = &self.param_witnesses[param_name];
@@ -297,7 +297,7 @@ impl Abi {
(_, None) => {}
}

Ok(witness_map)
Ok(witness_map.into())
}

fn encode_value(value: InputValue) -> Result<Vec<FieldElement>, AbiError> {
32 changes: 15 additions & 17 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/sort.rs
Original file line number Diff line number Diff line change
@@ -113,10 +113,8 @@ fn permutation_layer(

#[cfg(test)]
mod test {
use std::collections::BTreeMap;

use acvm::{
acir::{circuit::opcodes::FunctionInput, native_types::Witness},
acir::{circuit::opcodes::FunctionInput, native_types::Witness, native_types::WitnessMap},
pwg::{block::Blocks, solve, OpcodeResolution, PartialWitnessGeneratorStatus},
FieldElement, OpcodeResolutionError, PartialWitnessGenerator,
};
@@ -131,15 +129,15 @@ mod test {
impl PartialWitnessGenerator for MockBackend {
fn aes(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn and(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_lhs: &FunctionInput,
_rhs: &FunctionInput,
_output: &Witness,
@@ -148,7 +146,7 @@ mod test {
}
fn xor(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_lhs: &FunctionInput,
_rhs: &FunctionInput,
_output: &Witness,
@@ -157,30 +155,30 @@ mod test {
}
fn range(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_input: &FunctionInput,
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn sha256(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn blake2s(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_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 BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_leaf: &FunctionInput,
_index: &FunctionInput,
_hash_path: &[FunctionInput],
@@ -190,7 +188,7 @@ mod test {
}
fn schnorr_verify(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_public_key_x: &FunctionInput,
_public_key_y: &FunctionInput,
_signature: &[FunctionInput],
@@ -201,23 +199,23 @@ mod test {
}
fn pedersen(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn hash_to_field_128_security(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_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 BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_public_key_x: &[FunctionInput],
_public_key_y: &[FunctionInput],
_signature: &[FunctionInput],
@@ -228,15 +226,15 @@ mod test {
}
fn fixed_base_scalar_mul(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_input: &FunctionInput,
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
panic!("Path not trodden by this test")
}
fn keccak256(
&self,
_initial_witness: &mut BTreeMap<Witness, FieldElement>,
_initial_witness: &mut WitnessMap,
_inputs: &[FunctionInput],
_outputs: &[Witness],
) -> Result<OpcodeResolution, OpcodeResolutionError> {
@@ -255,7 +253,7 @@ mod test {
let mut input = Vec::new();
let mut a_val = Vec::new();
let mut b_wit = Vec::new();
let mut solved_witness: BTreeMap<Witness, FieldElement> = BTreeMap::new();
let mut solved_witness = WitnessMap::new();
for i in 0..n {
let w = eval.add_witness_to_cs();
input.push(w.into());

0 comments on commit e1f5b61

Please sign in to comment.