From b799c8aa4491f4f17e248a50a154386803b6d712 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 19 Apr 2023 18:04:33 +0100 Subject: [PATCH] fix: maintain ordering of return value witnesses when constructing ABI (#1177) fix: maintain ordering of return value witnesses --- crates/noirc_evaluator/src/lib.rs | 10 ++++++---- .../src/ssa/acir_gen/operations/return.rs | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/noirc_evaluator/src/lib.rs b/crates/noirc_evaluator/src/lib.rs index 4c1b05381f5..722ecc12397 100644 --- a/crates/noirc_evaluator/src/lib.rs +++ b/crates/noirc_evaluator/src/lib.rs @@ -43,7 +43,10 @@ pub struct Evaluator { // Witnesses below `num_witnesses_abi_len` and not included in this set // correspond to private parameters and must not be made public. public_parameters: BTreeSet, - return_values: BTreeSet, + // The witness indices for return values are not guaranteed to be contiguous + // and increasing as for `public_parameters`. We then use a `Vec` rather + // than a `BTreeSet` to preserve this order for the ABI. + return_values: Vec, opcodes: Vec, } @@ -78,7 +81,7 @@ pub fn create_circuit( current_witness_index, opcodes, public_parameters: PublicInputs(public_parameters), - return_values: PublicInputs(return_values.clone()), + return_values: PublicInputs(return_values.iter().copied().collect()), }, np_language, is_opcode_supported, @@ -86,8 +89,7 @@ pub fn create_circuit( .map_err(|_| RuntimeErrorKind::Spanless(String::from("produced an acvm compile error")))?; let (parameters, return_type) = program.main_function_signature; - let return_witnesses: Vec = return_values.into_iter().collect(); - let abi = Abi { parameters, param_witnesses, return_type, return_witnesses }; + let abi = Abi { parameters, param_witnesses, return_type, return_witnesses: return_values }; Ok((optimized_circuit, abi)) } diff --git a/crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs b/crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs index 1e1183f13ab..3269af06d16 100644 --- a/crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs @@ -46,7 +46,7 @@ pub(crate) fn evaluate( "we do not allow private ABI inputs to be returned as public outputs", ))); } - evaluator.return_values.insert(witness); + evaluator.return_values.push(witness); } }