ZoKrates provide a single thread of execution with two control flow constructs.
Function calls can help make programs clearer and more modular. However, using function calls is not always zero-cost, so deep call chains should be avoided.
Arguments are passed by value.
def incr(field a) -> (field): a = a + 1 return a def main() -> (field): field x = 1 field res = incr(x) x == 1 // x has not changed return 1
An if expression allows you to branch your code depending on a condition.
def main(field x) -> (field): field y = if x + 2 == 3 then 1 else 5 fi return y
The condition supports
==, which can be combined with the boolean operators
When it comes to inequality checks, there is a caveat: when executing
a < b, both
bwill be asserted to be strictly lower than the biggest power of 2 lower than
p/2. This means that
bare both asserted to be between
2**252 - 1. The same applies to other inequality checks.
For loops are available with the following syntax:
def main() -> (field): field res = 0 for field i in 0..4 do res = res + i endfor return res
The bounds have to be known at compile time, so only constants are allowed. For-loops define their own scope.