Skip to content

Commit

Permalink
chore: move param_witnesses to be stored within ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 15, 2023
1 parent 73a083e commit 123f09d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
24 changes: 11 additions & 13 deletions crates/nargo/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clap::ArgMatches;
use iter_extended::{try_btree_map, vecmap};
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::{Format, InputValue};
use noirc_abi::{decode_value, encode_value, AbiParameter, MAIN_RETURN_NAME};
use noirc_abi::{decode_value, encode_value, Abi, AbiParameter, MAIN_RETURN_NAME};
use noirc_driver::CompiledProgram;

use super::{create_named_dir, read_inputs_from_file, write_to_file, InputMap, WitnessMap};
Expand Down Expand Up @@ -67,7 +67,8 @@ pub(crate) fn execute_program(
// Solve the remaining witnesses
let solved_witness = solve_witness(compiled_program, inputs_map)?;

let public_inputs = extract_public_inputs(compiled_program, &solved_witness)?;
let public_abi = compiled_program.abi.as_ref().unwrap().clone().public_abi();
let public_inputs = extract_public_inputs(&public_abi, &solved_witness)?;
let return_value = public_inputs.get(MAIN_RETURN_NAME).cloned();

Ok((return_value, solved_witness))
Expand All @@ -77,8 +78,10 @@ pub(crate) fn solve_witness(
compiled_program: &CompiledProgram,
input_map: &InputMap,
) -> Result<WitnessMap, CliError> {
let mut solved_witness = input_map_to_witness_map(input_map, &compiled_program.param_witnesses)
.map_err(|error| match error {
let abi = compiled_program.abi.as_ref().unwrap();

let mut solved_witness =
input_map_to_witness_map(abi, input_map).map_err(|error| match error {
AbiError::UndefinedInput(_) => {
CliError::Generic(format!("{error} in the {PROVER_INPUT_FILE}.toml file."))
}
Expand All @@ -95,10 +98,7 @@ pub(crate) fn solve_witness(
///
/// In particular, this method shows one how to associate values in a Toml/JSON
/// file with witness indices
fn input_map_to_witness_map(
input_map: &InputMap,
abi_witness_map: &BTreeMap<String, Vec<Witness>>,
) -> Result<WitnessMap, AbiError> {
fn input_map_to_witness_map(abi: &Abi, input_map: &InputMap) -> Result<WitnessMap, AbiError> {
// First encode each input separately
let encoded_input_map: BTreeMap<String, Vec<FieldElement>> =
try_btree_map(input_map, |(key, value)| {
Expand All @@ -109,7 +109,7 @@ fn input_map_to_witness_map(
let witness_map = encoded_input_map
.iter()
.flat_map(|(param_name, encoded_param_fields)| {
let param_witness_indices = &abi_witness_map[param_name];
let param_witness_indices = &abi.param_witnesses[param_name];
param_witness_indices
.iter()
.zip(encoded_param_fields.iter())
Expand All @@ -121,17 +121,15 @@ fn input_map_to_witness_map(
}

pub(crate) fn extract_public_inputs(
compiled_program: &CompiledProgram,
public_abi: &Abi,
solved_witness: &WitnessMap,
) -> Result<InputMap, AbiError> {
let public_abi = compiled_program.abi.as_ref().unwrap().clone().public_abi();

let public_inputs_map = public_abi
.parameters
.iter()
.map(|AbiParameter { name, typ, .. }| {
let param_witness_values =
vecmap(compiled_program.param_witnesses[name].clone(), |witness_index| {
vecmap(public_abi.param_witnesses[name].clone(), |witness_index| {
solved_witness[&witness_index]
});

Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub fn prove_with_path<P: AsRef<Path>>(
let (_, solved_witness) = execute_program(&compiled_program, &inputs_map)?;

// Write public inputs into Verifier.toml
let public_inputs = extract_public_inputs(&compiled_program, &solved_witness)?;
let public_abi = compiled_program.abi.as_ref().unwrap().clone().public_abi();
let public_inputs = extract_public_inputs(&public_abi, &solved_witness)?;
write_inputs_to_file(&public_inputs, &program_dir, VERIFIER_INPUT_FILE, Format::Toml)?;

// Since the public outputs are added onto the public inputs list, there can be duplicates.
Expand Down
10 changes: 8 additions & 2 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![forbid(unsafe_code)]
use std::{collections::BTreeMap, convert::TryInto, str};

use acvm::FieldElement;
use acvm::{acir::native_types::Witness, FieldElement};
use errors::AbiError;
use input_parser::InputValue;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -120,6 +120,7 @@ impl AbiParameter {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Abi {
pub parameters: Vec<AbiParameter>,
pub param_witnesses: BTreeMap<String, Vec<Witness>>,
}

impl Abi {
Expand Down Expand Up @@ -149,7 +150,12 @@ impl Abi {
pub fn public_abi(self) -> Abi {
let parameters: Vec<_> =
self.parameters.into_iter().filter(|param| param.is_public()).collect();
Abi { parameters }
let param_witnesses = self
.param_witnesses
.into_iter()
.filter(|(param_name, _)| parameters.iter().any(|param| &param.name == param_name))
.collect();
Abi { parameters, param_witnesses }
}

/// Encode a set of inputs as described in the ABI into a vector of `FieldElement`s.
Expand Down
10 changes: 5 additions & 5 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![forbid(unsafe_code)]
use acvm::acir::circuit::Circuit;

use acvm::acir::native_types::Witness;
use acvm::Language;
use fm::FileType;
use noirc_abi::Abi;
Expand All @@ -13,7 +12,6 @@ use noirc_frontend::hir::Context;
use noirc_frontend::monomorphization::monomorphize;
use noirc_frontend::node_interner::FuncId;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

pub struct Driver {
Expand All @@ -24,7 +22,6 @@ pub struct Driver {
pub struct CompiledProgram {
pub circuit: Circuit,
pub abi: Option<noirc_abi::Abi>,
pub param_witnesses: BTreeMap<String, Vec<Witness>>,
}

impl Driver {
Expand Down Expand Up @@ -185,7 +182,7 @@ impl Driver {

// Create ABI for main function
let func_meta = self.context.def_interner.function_meta(&main_function);
let abi = func_meta.into_abi(&self.context.def_interner);
let mut abi = func_meta.into_abi(&self.context.def_interner);

let program = monomorphize(main_function, &self.context.def_interner);

Expand All @@ -204,7 +201,10 @@ impl Driver {
}
};

Ok(CompiledProgram { circuit, abi: Some(abi), param_witnesses })
// TODO: Try to avoid this hack.
abi.param_witnesses = param_witnesses;

Ok(CompiledProgram { circuit, abi: Some(abi) })
}

/// Returns a list of all functions in the current crate marked with #[test]
Expand Down
4 changes: 3 additions & 1 deletion crates/noirc_frontend/src/hir_def/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use iter_extended::vecmap;
use noirc_abi::{Abi, AbiParameter, AbiVisibility, MAIN_RETURN_NAME};
use noirc_errors::{Location, Span};
Expand Down Expand Up @@ -61,7 +63,7 @@ impl Parameters {
let as_abi = param.1.as_abi_type();
AbiParameter { name: param_name, typ: as_abi, visibility: param.2 }
});
noirc_abi::Abi { parameters }
noirc_abi::Abi { parameters, param_witnesses: BTreeMap::new() }
}

pub fn span(&self) -> Span {
Expand Down

0 comments on commit 123f09d

Please sign in to comment.