Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(nargo): add InputMap and WitnessMap terminology #713

Merged
merged 18 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use acvm::{acir::circuit::PublicInputs, FieldElement};
use acvm::{
acir::{circuit::PublicInputs, native_types::Witness},
FieldElement,
};
pub use check_cmd::check_from_path;
use clap::{App, AppSettings, Arg};
use const_format::formatcp;
Expand Down Expand Up @@ -32,6 +35,12 @@ mod verify_cmd;
const SHORT_GIT_HASH: &str = git_version!(prefix = "git:");
const VERSION_STRING: &str = formatcp!("{} ({})", env!("CARGO_PKG_VERSION"), SHORT_GIT_HASH);

/// 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>;

pub fn start_cli() {
let allow_warnings = Arg::with_name("allow-warnings")
.long("allow-warnings")
Expand Down
58 changes: 34 additions & 24 deletions crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::path::{Path, PathBuf};

use acvm::acir::native_types::Witness;
use acvm::FieldElement;
use acvm::PartialWitnessGenerator;
use acvm::ProofSystemCompiler;
use clap::ArgMatches;
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::{Format, InputValue};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
use noirc_abi::input_parser::Format;
use noirc_abi::Abi;

use super::{create_named_dir, read_inputs_from_file, write_inputs_to_file, write_to_file};
use super::{InputMap, WitnessMap};
use crate::cli::dedup_public_input_indices;
use crate::{
constants::{PROOFS_DIR, PROOF_EXT, PROVER_INPUT_FILE, VERIFIER_INPUT_FILE},
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn compile_circuit_and_witness<P: AsRef<Path>>(
program_dir: P,
show_ssa: bool,
allow_unused_variables: bool,
) -> Result<(noirc_driver::CompiledProgram, BTreeMap<Witness, FieldElement>), CliError> {
) -> Result<(noirc_driver::CompiledProgram, WitnessMap), CliError> {
let mut compiled_program = super::compile_cmd::compile_circuit(
program_dir.as_ref(),
show_ssa,
Expand All @@ -91,7 +91,7 @@ pub fn compile_circuit_and_witness<P: AsRef<Path>>(
pub fn parse_and_solve_witness<P: AsRef<Path>>(
program_dir: P,
compiled_program: &noirc_driver::CompiledProgram,
) -> Result<BTreeMap<Witness, FieldElement>, CliError> {
) -> Result<WitnessMap, CliError> {
let abi = compiled_program.abi.as_ref().expect("compiled program is missing an abi object");
// Parse the initial witness values from Prover.toml
let witness_map =
Expand Down Expand Up @@ -121,29 +121,39 @@ pub fn parse_and_solve_witness<P: AsRef<Path>>(

fn solve_witness(
compiled_program: &noirc_driver::CompiledProgram,
witness_map: &BTreeMap<String, InputValue>,
) -> Result<BTreeMap<Witness, FieldElement>, CliError> {
let abi = compiled_program.abi.as_ref().unwrap();
let encoded_inputs = abi.clone().encode(witness_map, true).map_err(|error| match error {
AbiError::UndefinedInput(_) => {
CliError::Generic(format!("{error} in the {PROVER_INPUT_FILE}.toml file."))
}
_ => CliError::from(error),
})?;

let mut solved_witness: BTreeMap<Witness, FieldElement> = encoded_inputs
input_map: &InputMap,
) -> Result<WitnessMap, CliError> {
let abi = compiled_program.abi.as_ref().unwrap().clone();
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 {VERIFIER_INPUT_FILE}.toml file."))
}
_ => CliError::from(error),
})?;

let backend = crate::backends::ConcreteBackend;
backend.solve(&mut solved_witness, compiled_program.circuit.opcodes.clone())?;

Ok(solved_witness)
}

/// Given an InputMap and an Abi, produce a WitnessMap
///
/// In particular, this method shows one how to associate values in a Toml/JSON
/// file with witness indices
fn input_map_to_witness_map(abi: Abi, input_map: &InputMap) -> Result<WitnessMap, AbiError> {
// The ABI map is first encoded as a vector of field elements
let encoded_inputs = abi.encode(input_map, true)?;

Ok(encoded_inputs
.into_iter()
.enumerate()
.map(|(index, witness_value)| {
let witness = Witness::new(WITNESS_OFFSET + (index as u32));
(witness, witness_value)
})
.collect();

let backend = crate::backends::ConcreteBackend;
backend.solve(&mut solved_witness, compiled_program.circuit.opcodes.clone())?;

Ok(solved_witness)
.collect())
}

fn save_proof_to_dir<P: AsRef<Path>>(
Expand Down
24 changes: 13 additions & 11 deletions crates/nargo/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
compile_cmd::compile_circuit, dedup_public_input_indices_values, read_inputs_from_file,
InputMap,
};
use crate::{
constants::{PROOFS_DIR, PROOF_EXT, VERIFIER_INPUT_FILE},
Expand All @@ -8,7 +9,7 @@ use crate::{
use acvm::ProofSystemCompiler;
use clap::ArgMatches;
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::{Format, InputValue};
use noirc_abi::input_parser::Format;
use noirc_driver::CompiledProgram;
use std::{collections::BTreeMap, path::Path, path::PathBuf};

Expand Down Expand Up @@ -44,34 +45,35 @@ pub fn verify_with_path<P: AsRef<Path>>(
allow_warnings: bool,
) -> Result<bool, CliError> {
let compiled_program = compile_circuit(program_dir.as_ref(), show_ssa, allow_warnings)?;
let mut public_inputs = BTreeMap::new();
let mut public_inputs_map: InputMap = BTreeMap::new();

// Load public inputs (if any) from `VERIFIER_INPUT_FILE`.
let public_abi = compiled_program.abi.clone().unwrap().public_abi();
let num_pub_params = public_abi.num_parameters();
if num_pub_params != 0 {
let current_dir = program_dir;
public_inputs =
public_inputs_map =
read_inputs_from_file(current_dir, VERIFIER_INPUT_FILE, Format::Toml, public_abi)?;
}

kevaundray marked this conversation as resolved.
Show resolved Hide resolved
let valid_proof = verify_proof(compiled_program, public_inputs, load_proof(proof_path)?)?;
let valid_proof = verify_proof(compiled_program, public_inputs_map, load_proof(proof_path)?)?;

Ok(valid_proof)
}

fn verify_proof(
mut compiled_program: CompiledProgram,
public_inputs: BTreeMap<String, InputValue>,
public_inputs_map: InputMap,
proof: Vec<u8>,
) -> Result<bool, CliError> {
let public_abi = compiled_program.abi.unwrap().public_abi();
let public_inputs = public_abi.encode(&public_inputs, false).map_err(|error| match error {
AbiError::UndefinedInput(_) => {
CliError::Generic(format!("{error} in the {VERIFIER_INPUT_FILE}.toml file."))
}
_ => CliError::from(error),
})?;
let public_inputs =
public_abi.encode(&public_inputs_map, false).map_err(|error| match error {
AbiError::UndefinedInput(_) => {
CliError::Generic(format!("{error} in the {VERIFIER_INPUT_FILE}.toml file."))
}
_ => CliError::from(error),
})?;

// Similarly to when proving -- we must remove the duplicate public witnesses which
// can be present because a public input can also be added as a public output.
Expand Down