From eedc4dbe61d887b6509acfa4f50612a0edc2e168 Mon Sep 17 00:00:00 2001 From: schaeff Date: Sun, 25 Jul 2021 15:46:30 +0200 Subject: [PATCH 1/6] use vec instead of map --- zokrates_abi/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/zokrates_abi/src/lib.rs b/zokrates_abi/src/lib.rs index f828825e..4102a428 100644 --- a/zokrates_abi/src/lib.rs +++ b/zokrates_abi/src/lib.rs @@ -14,14 +14,11 @@ impl> Encode for Inputs { } } -use std::collections::BTreeMap; use std::fmt; use zokrates_core::typed_absy::types::{ConcreteType, UBitwidth}; use zokrates_field::Field; -type Map = BTreeMap; - #[derive(Debug, PartialEq)] pub enum Error { Json(String), @@ -48,7 +45,7 @@ pub enum Value { Field(T), Boolean(bool), Array(Vec>), - Struct(Map>), + Struct(Vec<(String, Value)>), } #[derive(PartialEq, Debug)] From 84f717cd32f03b9328bdc34fef768a87b7dda964 Mon Sep 17 00:00:00 2001 From: schaeff Date: Sun, 25 Jul 2021 15:51:45 +0200 Subject: [PATCH 2/6] add changelog --- changelogs/unreleased/947-schaeff | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelogs/unreleased/947-schaeff diff --git a/changelogs/unreleased/947-schaeff b/changelogs/unreleased/947-schaeff new file mode 100644 index 00000000..07e3af35 --- /dev/null +++ b/changelogs/unreleased/947-schaeff @@ -0,0 +1 @@ +Fix abi encoder bug for struct values where the members are encoded in the wrong order \ No newline at end of file From d3621897ed00e74ea29e8d17536735273d98713e Mon Sep 17 00:00:00 2001 From: schaeff Date: Sun, 25 Jul 2021 16:37:36 +0200 Subject: [PATCH 3/6] fix test --- zokrates_abi/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/zokrates_abi/src/lib.rs b/zokrates_abi/src/lib.rs index 4102a428..b3d13e04 100644 --- a/zokrates_abi/src/lib.rs +++ b/zokrates_abi/src/lib.rs @@ -325,7 +325,6 @@ pub fn parse_strict(s: &str, types: Vec) -> Result Date: Sun, 25 Jul 2021 18:31:22 +0200 Subject: [PATCH 4/6] implement fold_program to avoid calling fold_module more than expected --- changelogs/unreleased/949-schaeff | 1 + zokrates_core/src/static_analysis/constant_inliner.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 changelogs/unreleased/949-schaeff diff --git a/changelogs/unreleased/949-schaeff b/changelogs/unreleased/949-schaeff new file mode 100644 index 00000000..146b5923 --- /dev/null +++ b/changelogs/unreleased/949-schaeff @@ -0,0 +1 @@ +Fix state corruption in the constant inliner \ No newline at end of file diff --git a/zokrates_core/src/static_analysis/constant_inliner.rs b/zokrates_core/src/static_analysis/constant_inliner.rs index ad00d292..26920dcc 100644 --- a/zokrates_core/src/static_analysis/constant_inliner.rs +++ b/zokrates_core/src/static_analysis/constant_inliner.rs @@ -67,6 +67,17 @@ impl<'ast, 'a, T: Field> ConstantInliner<'ast, T> { } impl<'ast, T: Field> Folder<'ast, T> for ConstantInliner<'ast, T> { + fn fold_program(&mut self, p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> { + for (id, _) in p.modules { + self.fold_module_id(id); + } + + TypedProgram { + modules: std::mem::take(&mut self.modules), + ..p + } + } + fn fold_module_id(&mut self, id: OwnedTypedModuleId) -> OwnedTypedModuleId { // anytime we encounter a module id, visit the corresponding module if it hasn't been done yet if !self.treated(&id) { From c9e4cf1e48019b882697914d0e841fa8fd2e8c4f Mon Sep 17 00:00:00 2001 From: schaeff Date: Mon, 26 Jul 2021 14:42:13 +0200 Subject: [PATCH 5/6] inline constants starting from main --- zokrates_core/src/static_analysis/constant_inliner.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/zokrates_core/src/static_analysis/constant_inliner.rs b/zokrates_core/src/static_analysis/constant_inliner.rs index 26920dcc..fcee3186 100644 --- a/zokrates_core/src/static_analysis/constant_inliner.rs +++ b/zokrates_core/src/static_analysis/constant_inliner.rs @@ -68,9 +68,7 @@ impl<'ast, 'a, T: Field> ConstantInliner<'ast, T> { impl<'ast, T: Field> Folder<'ast, T> for ConstantInliner<'ast, T> { fn fold_program(&mut self, p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> { - for (id, _) in p.modules { - self.fold_module_id(id); - } + self.fold_module_id(p.main.clone()); TypedProgram { modules: std::mem::take(&mut self.modules), From ecb105fb56d785b994bbc94db7480dde00bdfbc8 Mon Sep 17 00:00:00 2001 From: schaeff Date: Mon, 26 Jul 2021 15:02:37 +0200 Subject: [PATCH 6/6] add test --- zokrates_cli/examples/imports/inliner_state.zok | 12 ++++++++++++ zokrates_cli/examples/imports/inliner_state_aux.zok | 5 +++++ 2 files changed, 17 insertions(+) create mode 100644 zokrates_cli/examples/imports/inliner_state.zok create mode 100644 zokrates_cli/examples/imports/inliner_state_aux.zok diff --git a/zokrates_cli/examples/imports/inliner_state.zok b/zokrates_cli/examples/imports/inliner_state.zok new file mode 100644 index 00000000..321ee5a0 --- /dev/null +++ b/zokrates_cli/examples/imports/inliner_state.zok @@ -0,0 +1,12 @@ +from "./inliner_state_aux" import main as test + +// we check that the constant inliner does not get corrupted +// notice that the constant generic has the same name as the constant `K` +// defined in the `test1.zok` module +def tmp(): + u32 junk = test() + K + return + +// dummy main +def main(): + return \ No newline at end of file diff --git a/zokrates_cli/examples/imports/inliner_state_aux.zok b/zokrates_cli/examples/imports/inliner_state_aux.zok new file mode 100644 index 00000000..f828089d --- /dev/null +++ b/zokrates_cli/examples/imports/inliner_state_aux.zok @@ -0,0 +1,5 @@ +// the issue only shows if `K` is an array, renaming this constant to something else works +const u32[1] K = [1] + +def main() -> u32: + return K[0] \ No newline at end of file