From a98701309d4ca557682e96963bd011aa3e0b7b13 Mon Sep 17 00:00:00 2001 From: schaeff Date: Sun, 25 Jul 2021 18:31:22 +0200 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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