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

Merge branch 'add_operator_docs' of github.com:petscheit/ZoKrates into develop

This commit is contained in:
schaeff 2020-07-16 22:19:33 +02:00
commit d7ce5d2f58
12 changed files with 128 additions and 0 deletions

View file

@ -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 | ** <br> | Power | Left |
| 2 | * <br> /<br> | Multiplication <br> Division <br> | Left <br> Left |
| 3 | + <br> - <br> | Addition <br> Subtraction <br> | Left <br> Left |
| 4 | >= <br> > <br> <= <br> < | gte <br> gt <br> lte <br> lt | Left <br> Left <br> Left <br> Left|
| 5 | == <br> != <br> | Equal <br> Not Equal <br> | Left <br> Left |
| 6 | && | And | Left |
| 7 | XOR(a,b) - import from stdlib | Xor | Left |
| 8 | \|\| | Or | Left |

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,2 @@
def main(field a, field b, field c) -> (field):
return a / b / c

View file

@ -0,0 +1,2 @@
def main(field a, field b) -> (bool):
return a == b

View file

@ -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

View file

@ -0,0 +1,2 @@
def main(field a, field b, field c) -> (field):
return a * b * c

View file

@ -0,0 +1,2 @@
def main(field a, field b) -> (bool):
return !(a == b)

View file

@ -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

View file

@ -0,0 +1,2 @@
def main() -> (field):
return 5**5

View file

@ -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

View file

@ -0,0 +1,4 @@
import "utils/binary/xor" as XOR
def main(field a, field b) -> (field):
return XOR(a, b)