diff --git a/Cargo.toml b/Cargo.toml index 81d2c241e..d866d5e73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,4 @@ members = [ ] [profile.dev] -opt-level = 1 \ No newline at end of file +# opt-level = 1 \ No newline at end of file diff --git a/zokrates_analysis/src/panic_extractor.rs b/zokrates_analysis/src/panic_extractor.rs index db157af18..bcf31a400 100644 --- a/zokrates_analysis/src/panic_extractor.rs +++ b/zokrates_analysis/src/panic_extractor.rs @@ -108,6 +108,25 @@ impl<'ast, T: Field> Folder<'ast, T> for PanicExtractor<'ast, T> { ); FieldElementExpression::idiv(n, d) } + FieldElementExpression::Rem(e) => { + let n = self.fold_field_expression(*e.left); + let d = self.fold_field_expression(*e.right); + self.panic_buffer.push( + ZirStatement::assertion( + BooleanExpression::not( + BooleanExpression::field_eq( + d.clone().span(span), + FieldElementExpression::value(T::zero()).span(span), + ) + .span(span), + ) + .span(span), + RuntimeError::DivisionByZero, + ) + .span(span), + ); + FieldElementExpression::rem(n, d) + } e => fold_field_expression_cases(self, e), } } @@ -169,6 +188,25 @@ impl<'ast, T: Field> Folder<'ast, T> for PanicExtractor<'ast, T> { ); UExpression::div(n, d).into_inner() } + UExpressionInner::Rem(e) => { + let n = self.fold_uint_expression(*e.left); + let d = self.fold_uint_expression(*e.right); + self.panic_buffer.push( + ZirStatement::assertion( + BooleanExpression::not( + BooleanExpression::uint_eq( + d.clone().span(span), + UExpression::value(0).annotate(b).span(span), + ) + .span(span), + ) + .span(span), + RuntimeError::DivisionByZero, + ) + .span(span), + ); + UExpression::rem(n, d).into_inner() + } e => fold_uint_expression_cases(self, b, e), } } diff --git a/zokrates_core_test/tests/tests/rem.json b/zokrates_core_test/tests/tests/rem.json new file mode 100644 index 000000000..f43813da1 --- /dev/null +++ b/zokrates_core_test/tests/tests/rem.json @@ -0,0 +1,70 @@ +{ + "max_constraint_count": 1131, + "curves": ["Bn128", "Bls12_381", "Bls12_377", "Bw6_761"], + "tests": [ + { + "input": { + "values": ["0", "0"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, + { + "input": { + "values": ["1", "0"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, + { + "input": { + "values": ["0", "1"] + }, + "output": { + "Ok": { + "value": "0" + } + } + }, + { + "input": { + "values": ["2", "2"] + }, + "output": { + "Ok": { + "value": "0" + } + } + }, + { + "input": { + "values": ["4", "2"] + }, + "output": { + "Ok": { + "value": "0" + } + } + }, + { + "input": { + "values": ["5", "2"] + }, + "output": { + "Ok": { + "value": "1" + } + } + } + ] +} diff --git a/zokrates_core_test/tests/tests/rem.zok b/zokrates_core_test/tests/tests/rem.zok new file mode 100644 index 000000000..1d8b95e29 --- /dev/null +++ b/zokrates_core_test/tests/tests/rem.zok @@ -0,0 +1,3 @@ +def main(field x, field y) -> field { + return x % y; +} \ No newline at end of file diff --git a/zokrates_core_test/tests/tests/uint/u16/rem.json b/zokrates_core_test/tests/tests/uint/u16/rem.json index afebdba57..61cf2701d 100644 --- a/zokrates_core_test/tests/tests/uint/u16/rem.json +++ b/zokrates_core_test/tests/tests/uint/u16/rem.json @@ -2,6 +2,18 @@ "entry_point": "./tests/tests/uint/u16/rem.zok", "max_constraint_count": 88, "tests": [ + { + "input": { + "values": ["0x0001", "0x0000"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, { "input": { "values": ["0x0002", "0x0004"] diff --git a/zokrates_core_test/tests/tests/uint/u32/rem.json b/zokrates_core_test/tests/tests/uint/u32/rem.json index 4b24f228a..5d68dff29 100644 --- a/zokrates_core_test/tests/tests/uint/u32/rem.json +++ b/zokrates_core_test/tests/tests/uint/u32/rem.json @@ -2,6 +2,18 @@ "entry_point": "./tests/tests/uint/u32/rem.zok", "max_constraint_count": 168, "tests": [ + { + "input": { + "values": ["0x00000001", "0x00000000"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, { "input": { "values": ["0x00000002", "0x00000004"] diff --git a/zokrates_core_test/tests/tests/uint/u64/rem.json b/zokrates_core_test/tests/tests/uint/u64/rem.json index c10e559b6..80a13a596 100644 --- a/zokrates_core_test/tests/tests/uint/u64/rem.json +++ b/zokrates_core_test/tests/tests/uint/u64/rem.json @@ -2,6 +2,18 @@ "entry_point": "./tests/tests/uint/u64/rem.zok", "max_constraint_count": 328, "tests": [ + { + "input": { + "values": ["0x0000000000000001", "0x0000000000000000"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, { "input": { "values": ["0x0000000000000002", "0x0000000000000004"] diff --git a/zokrates_core_test/tests/tests/uint/u8/rem.json b/zokrates_core_test/tests/tests/uint/u8/rem.json index 587194ab6..a22bbc3c5 100644 --- a/zokrates_core_test/tests/tests/uint/u8/rem.json +++ b/zokrates_core_test/tests/tests/uint/u8/rem.json @@ -2,6 +2,18 @@ "entry_point": "./tests/tests/uint/u8/rem.zok", "max_constraint_count": 48, "tests": [ + { + "input": { + "values": ["0x01", "0x00"] + }, + "output": { + "Err": { + "UnsatisfiedConstraint": { + "error": "Inverse" + } + } + } + }, { "input": { "values": ["0x02", "0x04"]