Skip to content

Commit

Permalink
chore: handle public parameters and return values separately in evalu…
Browse files Browse the repository at this point in the history
…ator (#1062)

chore: handle public parameters and return values separately
  • Loading branch information
TomAFrench authored Mar 31, 2023
1 parent aee8368 commit 98c2cd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
46 changes: 24 additions & 22 deletions crates/noirc_evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use acvm::{
};
use errors::{RuntimeError, RuntimeErrorKind};
use iter_extended::btree_map;
use noirc_abi::{Abi, AbiType, AbiVisibility, MAIN_RETURN_NAME};
use noirc_abi::{Abi, AbiType, AbiVisibility};
use noirc_frontend::monomorphization::ast::*;
use ssa::{node::ObjectType, ssa_gen::IrGenerator};
use std::collections::{BTreeMap, BTreeSet};
Expand All @@ -38,10 +38,12 @@ pub struct Evaluator {
// creating the private/public inputs of the ABI.
num_witnesses_abi_len: usize,
param_witnesses: BTreeMap<String, Vec<Witness>>,
// This is the list of witness indices which are linked to public inputs.
// This is the list of witness indices which are linked to public parameters.
// Witnesses below `num_witnesses_abi_len` and not included in this set
// correspond to private inputs and must not be made public.
public_inputs: BTreeSet<Witness>,
// correspond to private parameters and must not be made public.
public_parameters: BTreeSet<Witness>,
return_values: BTreeSet<Witness>,

opcodes: Vec<AcirOpcode>,
}

Expand All @@ -62,30 +64,30 @@ pub fn create_circuit(
// First evaluate the main function
evaluator.evaluate_main_alt(program.clone(), enable_logging, show_output)?;

let witness_index = evaluator.current_witness_index();

let (parameters, return_type) = program.main_function_signature;

// TODO: remove return value from `param_witnesses` once we track public outputs
// see https://github.com/noir-lang/acvm/pull/56
let mut param_witnesses = evaluator.param_witnesses;
let return_witnesses = param_witnesses.remove(MAIN_RETURN_NAME).unwrap_or_default();
let return_witnesses_set: BTreeSet<Witness> = return_witnesses.iter().copied().collect();

let abi = Abi { parameters, param_witnesses, return_type, return_witnesses };

let Evaluator {
current_witness_index,
param_witnesses,
public_parameters,
return_values,
opcodes,
..
} = evaluator;
let optimized_circuit = acvm::compiler::compile(
Circuit {
current_witness_index: witness_index,
opcodes: evaluator.opcodes,
public_parameters: PublicInputs(evaluator.public_inputs),
return_values: PublicInputs(return_witnesses_set),
current_witness_index,
opcodes,
public_parameters: PublicInputs(public_parameters),
return_values: PublicInputs(return_values.clone()),
},
np_language,
is_opcode_supported,
)
.map_err(|_| RuntimeErrorKind::Spanless(String::from("produced an acvm compile error")))?;

let (parameters, return_type) = program.main_function_signature;
let return_witnesses: Vec<Witness> = return_values.into_iter().collect();
let abi = Abi { parameters, param_witnesses, return_type, return_witnesses };

Ok((optimized_circuit, abi))
}

Expand All @@ -101,7 +103,7 @@ impl Evaluator {
// an intermediate variable.
let is_intermediate_variable = witness_index.as_usize() > self.num_witnesses_abi_len;

let is_public_input = self.public_inputs.contains(&witness_index);
let is_public_input = self.public_parameters.contains(&witness_index);

!is_intermediate_variable && !is_public_input
}
Expand Down Expand Up @@ -213,7 +215,7 @@ impl Evaluator {
};

if param_visibility == &AbiVisibility::Public {
self.public_inputs.extend(witnesses.clone());
self.public_parameters.extend(witnesses.clone());
}
self.param_witnesses.insert(name.to_owned(), witnesses);

Expand Down
12 changes: 1 addition & 11 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use acvm::acir::native_types::Witness;
use noirc_abi::MAIN_RETURN_NAME;

use crate::{
errors::RuntimeErrorKind,
ssa::{
Expand Down Expand Up @@ -40,7 +37,6 @@ pub(crate) fn evaluate(
}
};

let mut witnesses: Vec<Witness> = Vec::new();
for object in objects {
let witness = var_cache.get_or_compute_witness_unwrap(object, evaluator, ctx);
// Before pushing to the public inputs, we need to check that
Expand All @@ -50,14 +46,8 @@ pub(crate) fn evaluate(
"we do not allow private ABI inputs to be returned as public outputs",
)));
}
witnesses.push(witness);
evaluator.return_values.insert(witness);
}
evaluator.public_inputs.extend(witnesses.clone());
evaluator
.param_witnesses
.entry(MAIN_RETURN_NAME.to_owned())
.or_default()
.append(&mut witnesses);
}

Ok(None)
Expand Down

0 comments on commit 98c2cd9

Please sign in to comment.