1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

Merge pull request #1032 from Zokrates/fail-early-on-complex-value-inequality

Fail at compile time on unsatisfied equality of complex values
This commit is contained in:
Thibaut Schaeffer 2021-10-15 13:01:52 +03:00 committed by GitHub
commit f780b49aa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 2 deletions

View file

@ -0,0 +1 @@
Fail at compile time when complex types are known not to be equal

View file

@ -0,0 +1,3 @@
def main():
assert([1f] == [2f])
return

View file

@ -2,8 +2,8 @@ use crate::zir::result_folder::fold_statement;
use crate::zir::result_folder::ResultFolder;
use crate::zir::types::UBitwidth;
use crate::zir::{
BooleanExpression, FieldElementExpression, Identifier, UExpression, UExpressionInner,
ZirExpression, ZirProgram, ZirStatement,
BooleanExpression, FieldElementExpression, Identifier, RuntimeError, UExpression,
UExpressionInner, ZirExpression, ZirProgram, ZirStatement,
};
use std::collections::HashMap;
use std::fmt;
@ -15,6 +15,7 @@ type Constants<'ast, T> = HashMap<Identifier<'ast>, ZirExpression<'ast, T>>;
pub enum Error {
OutOfBounds(u128, u128),
DivisionByZero,
AssertionFailed(RuntimeError),
}
impl fmt::Display for Error {
@ -28,6 +29,7 @@ impl fmt::Display for Error {
Error::DivisionByZero => {
write!(f, "Division by zero detected in zir during static analysis",)
}
Error::AssertionFailed(err) => write!(f, "{}", err),
}
}
}
@ -53,6 +55,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ZirPropagator<'ast, T> {
match s {
ZirStatement::Assertion(e, error) => match self.fold_boolean_expression(e)? {
BooleanExpression::Value(true) => Ok(vec![]),
BooleanExpression::Value(false) => Err(Error::AssertionFailed(error)),
e => Ok(vec![ZirStatement::Assertion(e, error)]),
},
ZirStatement::Definition(a, e) => {

View file

@ -0,0 +1,32 @@
{
"entry_point": "./tests/tests/assert_array_equality.zok",
"curves": ["Bn128", "Bls12_381", "Bls12_377", "Bw6_761"],
"tests": [
{
"input": {
"values": ["1", "2"]
},
"output": {
"Ok": {
"values": []
}
}
},
{
"input": {
"values": ["1", "1"]
},
"output": {
"Err": {
"UnsatisfiedConstraint": {
"left": "0",
"right": "1",
"error": {
"SourceAssertion": "Assertion failed at ./tests/tests/assert_array_equality.zok:2:2"
}
}
}
}
}
]
}

View file

@ -0,0 +1,3 @@
def main(field[2] a):
assert(a == [1, 2])
return