Merge branch 'add_operator_docs' of github.com:petscheit/ZoKrates into develop
This commit is contained in:
commit
d7ce5d2f58
12 changed files with 128 additions and 0 deletions
85
zokrates_book/src/concepts/operators.md
Normal file
85
zokrates_book/src/concepts/operators.md
Normal 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 |
|
6
zokrates_cli/examples/book/add.zok
Normal file
6
zokrates_cli/examples/book/add.zok
Normal 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
|
5
zokrates_cli/examples/book/and.zok
Normal file
5
zokrates_cli/examples/book/and.zok
Normal 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
|
2
zokrates_cli/examples/book/div.zok
Normal file
2
zokrates_cli/examples/book/div.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def main(field a, field b, field c) -> (field):
|
||||
return a / b / c
|
2
zokrates_cli/examples/book/equal.zok
Normal file
2
zokrates_cli/examples/book/equal.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def main(field a, field b) -> (bool):
|
||||
return a == b
|
7
zokrates_cli/examples/book/gtl.zok
Normal file
7
zokrates_cli/examples/book/gtl.zok
Normal 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
|
2
zokrates_cli/examples/book/mul.zok
Normal file
2
zokrates_cli/examples/book/mul.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def main(field a, field b, field c) -> (field):
|
||||
return a * b * c
|
2
zokrates_cli/examples/book/not_equal.zok
Normal file
2
zokrates_cli/examples/book/not_equal.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def main(field a, field b) -> (bool):
|
||||
return !(a == b)
|
4
zokrates_cli/examples/book/or.zok
Normal file
4
zokrates_cli/examples/book/or.zok
Normal 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
|
2
zokrates_cli/examples/book/pow.zok
Normal file
2
zokrates_cli/examples/book/pow.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def main() -> (field):
|
||||
return 5**5
|
7
zokrates_cli/examples/book/sub.zok
Normal file
7
zokrates_cli/examples/book/sub.zok
Normal 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
|
4
zokrates_cli/examples/book/xor.zok
Normal file
4
zokrates_cli/examples/book/xor.zok
Normal file
|
@ -0,0 +1,4 @@
|
|||
import "utils/binary/xor" as XOR
|
||||
|
||||
def main(field a, field b) -> (field):
|
||||
return XOR(a, b)
|
Loading…
Reference in a new issue