Skip to content

Commit

Permalink
add rem test
Browse files Browse the repository at this point in the history
  • Loading branch information
dark64 committed Nov 15, 2023
1 parent a11c1ba commit fafcb6f
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ members = [
]

[profile.dev]
opt-level = 1
# opt-level = 1
38 changes: 38 additions & 0 deletions zokrates_analysis/src/panic_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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),
}
}
Expand Down
70 changes: 70 additions & 0 deletions zokrates_core_test/tests/tests/rem.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
3 changes: 3 additions & 0 deletions zokrates_core_test/tests/tests/rem.zok
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main(field x, field y) -> field {
return x % y;
}
12 changes: 12 additions & 0 deletions zokrates_core_test/tests/tests/uint/u16/rem.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
12 changes: 12 additions & 0 deletions zokrates_core_test/tests/tests/uint/u32/rem.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
12 changes: 12 additions & 0 deletions zokrates_core_test/tests/tests/uint/u64/rem.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
12 changes: 12 additions & 0 deletions zokrates_core_test/tests/tests/uint/u8/rem.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit fafcb6f

Please sign in to comment.