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 2 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
35 changes: 31 additions & 4 deletions crates/nargo/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use acvm::ProofSystemCompiler;
use clap::ArgMatches;
use noirc_abi::errors::AbiError;
use noirc_abi::input_parser::{Format, InputValue};
use noirc_abi::Abi;
use std::path::Path;

use super::{create_named_dir, read_inputs_from_file, write_inputs_to_file, write_to_file};
Expand All @@ -27,6 +28,14 @@ pub(crate) fn run(args: ArgMatches) -> Result<(), CliError> {
/// So when we add witness values, their index start from 1.
const WITNESS_OFFSET: u32 = 1;

// A map from the fields in an TOML/JSON file
// which correspond to some ABI to their values
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
pub type AbiMap = BTreeMap<String, InputValue>;
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

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

fn prove(proof_name: &str, show_ssa: bool, allow_warnings: bool) -> Result<(), CliError> {
let curr_dir = std::env::current_dir().unwrap();
let mut proof_path = PathBuf::new();
Expand All @@ -42,7 +51,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 compiled_program = super::compile_cmd::compile_circuit(
program_dir.as_ref(),
show_ssa,
Expand All @@ -55,7 +64,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 @@ -85,8 +94,8 @@ 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> {
witness_map: &AbiMap,
) -> Result<WitnessMap, 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(_) => {
Expand All @@ -112,6 +121,24 @@ fn solve_witness(
Ok(solved_witness)
}

// Given an AbiMap 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 abi_map_to_witness_map(abi: &Abi, abi_map: &AbiMap) -> Result<WitnessMap, AbiError> {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
// The ABI map is first encoded as a vector of field elements
let encoded_inputs = abi.clone().encode(abi_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())
}

pub fn prove_with_path<P: AsRef<Path>>(
proof_name: &str,
program_dir: P,
Expand Down
10 changes: 5 additions & 5 deletions crates/nargo/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -42,7 +42,7 @@ 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_abi_map: AbiMap = BTreeMap::new();

// Load public inputs (if any) from `VERIFIER_INPUT_FILE`.
let public_abi = compiled_program.abi.clone().unwrap().public_abi();
Expand All @@ -53,18 +53,18 @@ pub fn verify_with_path<P: AsRef<Path>>(
read_inputs_from_file(curr_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_abi_map, load_proof(proof_path)?)?;

Ok(valid_proof)
}

fn verify_proof(
compiled_program: CompiledProgram,
public_inputs: BTreeMap<String, InputValue>,
public_abi_map: AbiMap,
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 {
let public_inputs = public_abi.encode(&public_abi_map, false).map_err(|error| match error {
AbiError::UndefinedInput(_) => {
CliError::Generic(format!("{error} in the {VERIFIER_INPUT_FILE}.toml file."))
}
Expand Down