diff --git a/zokrates_core/src/static_analysis/propagation.rs b/zokrates_core/src/static_analysis/propagation.rs index f1c410a8..50601552 100644 --- a/zokrates_core/src/static_analysis/propagation.rs +++ b/zokrates_core/src/static_analysis/propagation.rs @@ -1,5 +1,11 @@ //! Module containing constant propagation for the typed AST //! +//! On top of the usual behavior of removing statements which assign a constant to a variable (as the variable can simply be +//! substituted for the constant whenever used), we provide a `verbose` mode which does not remove such statements. This is done +//! as for partial passes which do not visit the whole program, the variables being defined may be be used in parts of the program +//! that are not visited. Keeping the statements is semantically equivalent and enables rebuilding the set of constants at the +//! next pass. +//! //! @file propagation.rs //! @author Thibaut Schaeffer //! @date 2018 @@ -16,7 +22,7 @@ pub struct Propagator<'ast, T: Field> { // we currently do not support partially constant expressions: `field [x, 1][1]` is not considered constant, `field [0, 1][1]` is constants: HashMap, TypedExpression<'ast, T>>, // the verbose mode doesn't remove statements which assign constants to variables - // it required when using propagation in combination with unrolling + // it's required when using propagation in combination with unrolling verbose: bool, } diff --git a/zokrates_core_test/tests/tests/nested_loop.json b/zokrates_core_test/tests/tests/nested_loop.json index 82b19745..c71202c9 100644 --- a/zokrates_core_test/tests/tests/nested_loop.json +++ b/zokrates_core_test/tests/tests/nested_loop.json @@ -7,7 +7,7 @@ }, "output": { "Ok": { - "values": ["4838400", "10"] + "values": ["4838400", "10", "25"] } } }, @@ -17,7 +17,7 @@ }, "output": { "Ok": { - "values": ["0", "10"] + "values": ["0", "10", "25"] } } } diff --git a/zokrates_core_test/tests/tests/nested_loop.zok b/zokrates_core_test/tests/tests/nested_loop.zok index 581571b3..2d00f1a0 100644 --- a/zokrates_core_test/tests/tests/nested_loop.zok +++ b/zokrates_core_test/tests/tests/nested_loop.zok @@ -1,4 +1,4 @@ -def main(field[4] values) -> (field, field): +def main(field[4] values) -> (field, field, field): field res0 = 1 field res1 = 0 @@ -15,4 +15,13 @@ def main(field[4] values) -> (field, field): res1 = res1 + 1 endfor - return res0, res1 \ No newline at end of file + field res2 = 0 + field i = 0 + for field i in i..5 do + i = 5 + for field i in 0..i do + res2 = res2 + 1 + endfor + endfor + + return res0, res1, res2 \ No newline at end of file