Skip to content

Commit

Permalink
feat: Only create new witnesses for distinctiveness when duplicates e…
Browse files Browse the repository at this point in the history
…xist (#2191)

* feat: reuse witnesses where possible while guaranteeing distinctiveness

* Update crates/noirc_evaluator/src/ssa/acir_gen/mod.rs
  • Loading branch information
TomAFrench authored Aug 7, 2023
1 parent aad82c3 commit 14cbdbc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Example that uses the distinct keyword
fn main(x: pub Field) -> distinct pub [Field;2] {
[x+1, x]
fn main(x: pub Field) -> distinct pub [Field; 3] {
[x+1, x, x]
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1]},"return_type":{"kind":"array","length":2,"type":{"kind":"field"}},"return_witnesses":[3,4]},"bytecode":"H4sIAAAAAAAA/9WRQQ6AIAwEEfU/LW2lvfkVifj/JxgSTEg8Cgf3srftTnd1zs3uLV99rw7fhFOTRbAx5xgyEh4QLKkAS9oUFUXlDEqUlTVasgiGTBkvMbpqmO/YaySz78g89+sFf9l5GcA8Nf6wl9+WWzcyyYCuDAMAAA==","proving_key":null,"verification_key":null}
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"x":[1]},"return_type":{"kind":"array","length":3,"type":{"kind":"field"}},"return_witnesses":[2,1,3]},"bytecode":"H4sIAAAAAAAA/9VPOQ7AIAzj6IMSkkCy9StFhf8/oQtISB3LUi8+Bss+nHPBvTGzczB8A/qliyAzt5IaEl6QrKoAS82KiqJyJyVqylqsWgFDpoZdjPooCxt3/eVz3LcL5l+/cFx0GP4Bi8kGuhwCAAA=","proving_key":null,"verification_key":null}
Binary file not shown.
32 changes: 19 additions & 13 deletions crates/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use crate::brillig::{brillig_gen::brillig_fn::FunctionContext as BrilligFunction
use crate::errors::{InternalError, RuntimeError};
pub(crate) use acir_ir::generated_acir::GeneratedAcir;
use acvm::{
acir::{brillig::Opcode, circuit::opcodes::BlockId, native_types::Expression},
acir::{
brillig::Opcode,
circuit::opcodes::BlockId,
native_types::{Expression, Witness},
},
FieldElement,
};
use iter_extended::{try_vecmap, vecmap};
Expand Down Expand Up @@ -119,17 +123,19 @@ impl Ssa {

match abi_distinctness {
AbiDistinctness::Distinct => {
// Create a witness for each return witness we have
// to guarantee that the return witnesses are distinct
let distinct_return_witness: Vec<_> = generated_acir
.return_witnesses
.clone()
.into_iter()
.map(|return_witness| {
generated_acir
.create_witness_for_expression(&Expression::from(return_witness))
})
.collect();
let mut distinct_return_witness: Vec<Witness> =
Vec::with_capacity(generated_acir.return_witnesses.len());

for mut return_witness in generated_acir.return_witnesses.clone() {
// If witness has already been used then create a new one
// to guarantee that the return witnesses are distinct
if distinct_return_witness.contains(&return_witness) {
return_witness = generated_acir
.create_witness_for_expression(&Expression::from(return_witness));
}

distinct_return_witness.push(return_witness);
}

generated_acir.return_witnesses = distinct_return_witness;
Ok(generated_acir)
Expand Down Expand Up @@ -1121,7 +1127,7 @@ impl Context {

#[cfg(test)]
mod tests {
use std::{rc::Rc, collections::HashMap};
use std::{collections::HashMap, rc::Rc};

use acvm::{
acir::{
Expand Down

0 comments on commit 14cbdbc

Please sign in to comment.