diff --git a/zokrates_book/src/concepts/operators.md b/zokrates_book/src/concepts/operators.md new file mode 100644 index 00000000..38765d5a --- /dev/null +++ b/zokrates_book/src/concepts/operators.md @@ -0,0 +1,85 @@ +## Operators + +The ZoKrates programming language constists of different operands that can be used on variables + +### Available Operators + +#### Or + +```zokrates +{{#include ../../../zokrates_cli/examples/book/or.zok}} +``` + +#### Xor + +```zokrates +{{#include ../../../zokrates_cli/examples/book/xor.zok}} +``` + +#### And + +```zokrates +{{#include ../../../zokrates_cli/examples/book/and.zok}} +``` + +#### Equal + +```zokrates +{{#include ../../../zokrates_cli/examples/book/equal.zok}} +``` + +#### Not Equal + +```zokrates +{{#include ../../../zokrates_cli/examples/book/not_equal.zok}} +``` + +#### Greater/Lower + +```zokrates +{{#include ../../../zokrates_cli/examples/book/gtl.zok}} +``` + +#### Add + +```zokrates +{{#include ../../../zokrates_cli/examples/book/add.zok}} +``` + +#### Subtract + +```zokrates +{{#include ../../../zokrates_cli/examples/book/subtract.zok}} +``` + +#### Multiply + +```zokrates +{{#include ../../../zokrates_cli/examples/book/mul.zok}} +``` + +#### Divide + +```zokrates +{{#include ../../../zokrates_cli/examples/book/div.zok}} +``` + +#### Power + +```zokrates +{{#include ../../../zokrates_cli/examples/book/pow.zok}} +``` + +### Operator Precedence +The following table lists the precedence and associativity of all available operators. Operators are listed top to bottom, in descending precedence. + +| Precedence | Operator | Description | Associativity | +|------------|----------------------------------------|------------------------------------|-----------------------------------| +| 1 | **
| Power | Left | +| 2 | *
/
| Multiplication
Division
| Left
Left | +| 3 | +
-
| Addition
Subtraction
| Left
Left | +| 4 | >=
>
<=
< | gte
gt
lte
lt | Left
Left
Left
Left| +| 5 | ==
!=
| Equal
Not Equal
| Left
Left | +| 6 | && | And | Left | +| 7 | XOR(a,b) - import from stdlib | Xor | Left | +| 8 | \|\| | Or | Left | \ No newline at end of file diff --git a/zokrates_cli/examples/book/add.zok b/zokrates_cli/examples/book/add.zok new file mode 100644 index 00000000..8321dc10 --- /dev/null +++ b/zokrates_cli/examples/book/add.zok @@ -0,0 +1,6 @@ +// only using add, no need to flatten +def main(field a) -> (field): + field b = a + 5 + field c = a + b + a + 4 + field d = a + c + a + b + return b + c + d \ No newline at end of file diff --git a/zokrates_cli/examples/book/and.zok b/zokrates_cli/examples/book/and.zok new file mode 100644 index 00000000..e63da463 --- /dev/null +++ b/zokrates_cli/examples/book/and.zok @@ -0,0 +1,5 @@ +// example using if-then-else-fi with && + +def main(field a, field b) -> (field): + field y = if a + 2 == 3 && a * 2 == 2 then 1 else 0 fi + return y \ No newline at end of file diff --git a/zokrates_cli/examples/book/div.zok b/zokrates_cli/examples/book/div.zok new file mode 100644 index 00000000..6a223445 --- /dev/null +++ b/zokrates_cli/examples/book/div.zok @@ -0,0 +1,2 @@ +def main(field a, field b, field c) -> (field): + return a / b / c \ No newline at end of file diff --git a/zokrates_cli/examples/book/equal.zok b/zokrates_cli/examples/book/equal.zok new file mode 100644 index 00000000..9037d5e2 --- /dev/null +++ b/zokrates_cli/examples/book/equal.zok @@ -0,0 +1,2 @@ +def main(field a, field b) -> (bool): + return a == b \ No newline at end of file diff --git a/zokrates_cli/examples/book/gtl.zok b/zokrates_cli/examples/book/gtl.zok new file mode 100644 index 00000000..ac835b0b --- /dev/null +++ b/zokrates_cli/examples/book/gtl.zok @@ -0,0 +1,7 @@ +def main() -> (bool): + field a = if 10 > 9 then 1 else 0 fi + field b = if 10 >= 9 then 1 else 0 fi + field c = if 10 <= 10 then 1 else 0 fi + field d = if 10 < 9 then 1 else 0 fi + + return a == b && c == d \ No newline at end of file diff --git a/zokrates_cli/examples/book/mul.zok b/zokrates_cli/examples/book/mul.zok new file mode 100644 index 00000000..0651e7d2 --- /dev/null +++ b/zokrates_cli/examples/book/mul.zok @@ -0,0 +1,2 @@ +def main(field a, field b, field c) -> (field): + return a * b * c \ No newline at end of file diff --git a/zokrates_cli/examples/book/not_equal.zok b/zokrates_cli/examples/book/not_equal.zok new file mode 100644 index 00000000..d00d3203 --- /dev/null +++ b/zokrates_cli/examples/book/not_equal.zok @@ -0,0 +1,2 @@ +def main(field a, field b) -> (bool): + return !(a == b) \ No newline at end of file diff --git a/zokrates_cli/examples/book/or.zok b/zokrates_cli/examples/book/or.zok new file mode 100644 index 00000000..80860190 --- /dev/null +++ b/zokrates_cli/examples/book/or.zok @@ -0,0 +1,4 @@ +// example using if-then-else-fi with || +def main(field a, field b) -> (field): + field y = if a + 2 == 4 || b * 2 == 2 then 1 else 0 fi + return y \ No newline at end of file diff --git a/zokrates_cli/examples/book/pow.zok b/zokrates_cli/examples/book/pow.zok new file mode 100644 index 00000000..d5f400ab --- /dev/null +++ b/zokrates_cli/examples/book/pow.zok @@ -0,0 +1,2 @@ +def main() -> (field): + return 5**5 \ No newline at end of file diff --git a/zokrates_cli/examples/book/sub.zok b/zokrates_cli/examples/book/sub.zok new file mode 100644 index 00000000..bc891f8b --- /dev/null +++ b/zokrates_cli/examples/book/sub.zok @@ -0,0 +1,7 @@ +// only using sub, no need to flatten +def main(field a) -> (field): + field b = a + 100 + field c = b + a + b + field d = b - a - 3 - a + field e = d + 3 - a + return d + c + e \ No newline at end of file diff --git a/zokrates_cli/examples/book/xor.zok b/zokrates_cli/examples/book/xor.zok new file mode 100644 index 00000000..91e53edf --- /dev/null +++ b/zokrates_cli/examples/book/xor.zok @@ -0,0 +1,4 @@ +import "utils/binary/xor" as XOR + +def main(field a, field b) -> (field): + return XOR(a, b) \ No newline at end of file