diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr b/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr index d84be844d8e..5bf3f7e0975 100644 --- a/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr +++ b/crates/nargo_cli/tests/execution_success/distinct_keyword/src/main.nr @@ -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] } diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json index ad8ebce2475..77dc3d9fe1c 100644 --- a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json +++ b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/distinct_keyword.json @@ -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} \ No newline at end of file +{"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} \ No newline at end of file diff --git a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr index d79dfba9359..57b9214a4e1 100644 Binary files a/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr and b/crates/nargo_cli/tests/execution_success/distinct_keyword/target/witness.tr differ diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs index aca809a85fa..a9bdcd7b43b 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs @@ -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}; @@ -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 = + 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) @@ -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::{