1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

Merge pull request #227 from Zokrates/test-book-snippets

Test book snippets
This commit is contained in:
Stefan 2019-01-21 20:28:46 +01:00 committed by GitHub
commit 0b99d60c08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 80 additions and 67 deletions

View file

@ -3,8 +3,5 @@
Comments can be added with double-slashes.
```zokrates
def main() -> (field):
field a = 42 // this is an end of line comment
// this is a full line comment
return a
{{#include ../../../zokrates_cli/examples/book/comments.code}}
```

View file

@ -9,15 +9,7 @@ Function calls can help make programs clearer and more modular. However, using f
Arguments are passed by value.
```zokrates
def incr(field a) -> (field)
a = a + 1
return 1
def main() -> (field):
field x = 1
field res = incr(x)
x == 1 // x has not changed
return 1
{{#include ../../../zokrates_cli/examples/book/side_effects.code}}
```
### If expressions
@ -25,9 +17,7 @@ def main() -> (field):
An if expression allows you to branch your code depending on a condition.
```zokrates
def main(field x) -> (field):
field y = if x + 2 == 3 then 1 else 5 fi
return y
{{#include ../../../zokrates_cli/examples/book/if_else.code}}
```
The condition supports `<`, `<=`, `>`, `>=`, `==`, which can be combined with the boolean operators `&&`, `||` and `!`.
@ -39,12 +29,7 @@ The condition supports `<`, `<=`, `>`, `>=`, `==`, which can be combined with th
For loops are available with the following syntax:
```zokrates
def main() -> (field):
field res = 0
for field i in 0..4 do
res = res + i
endfor
return res
{{#include ../../../zokrates_cli/examples/book/for.code}}
```
The bounds have to be known at compile time, so only constants are allowed.

View file

@ -3,19 +3,14 @@
A function has to be declared at the top level before it is called.
```zokrates
def foo() -> (field):
return 1
def bar() -> (field):
return foo()
{{#include ../../../zokrates_cli/examples/book/function_declaration.code}}
```
A function's signature has to be explicitly provided.
Functions can return many values by providing them as a comma-separated list.
```zokrates
def main() -> (field, field[3]):
return 1, [2, 3, 4]
{{#include ../../../zokrates_cli/examples/book/multi_return.code}}
```
### Inference
@ -23,24 +18,11 @@ def main() -> (field, field[3]):
When defining a variable as the return value of a function, types are optional:
```zokrates
def foo() -> (field, field):
return 21, 42
def main() -> (field):
a, b = foo()
return 1
{{#include ../../../zokrates_cli/examples/book/multi_def.code}}
```
If there is an ambiguity, providing the types of some of the assigned variables is necessary.
```zokrates
def foo() -> (field, field[3]):
return 1, [2, 3, 4]
def foo() -> (field, field):
return 1, 2
def main() -> (field):
a, field[3] b = foo()
return 1
{{#include ../../../zokrates_cli/examples/book/type_annotations.code}}
```

View file

@ -11,10 +11,7 @@ The prime `p` is set to `2188824287183927522224640574525727508854836440041603434
While `field` values mostly behave like unsigned integers, one should keep in mind that they overflow at `p` and not some power of 2 and underflow at 0, so that we have:
```zokrates
def main() -> (field):
field p_minus_one = 21888242871839275222246405745257275088548364400416034343698204186575808495616
0 - 1 == p_minus_one
return 1
{{#include ../../../zokrates_cli/examples/book/field_overflow.code}}
```
### `bool`
@ -30,8 +27,5 @@ Note that while equality checks are cheap, inequality checks should be use wisel
Static arrays of `field` can be instantiated with a constant size, and their elements can be accessed and updated:
```zokrates
def main() -> (field):
field[3] a = [1, 2, 3]
a[2] = 4
return a[0] + a[2]
{{#include ../../../zokrates_cli/examples/book/array.code}}
```

View file

@ -29,9 +29,7 @@ cd ZoKrates/target/release
First, create the text-file `root.code` and implement your program. In this example, we will prove knowledge of the square root `a` of a number `b`:
```zokrates
def main(private field a, field b) -> (field):
field result = if a * a == b then 1 else 0 fi
return result
{{#include ../../zokrates_cli/examples/book/factorize.code}}
```
Some observations:

View file

@ -16,11 +16,7 @@ We will start this tutorial by using ZoKrates to compute the hash for an arbitra
First, we create a new file named `hashexample.code` with the following content:
```zokrates
import "LIBSNARK/sha256packed"
def main(private field a, private field b, private field c, private field d) -> (field, field):
h0, h1 = sha256packed(a, b, c, d)
return h0, h1
{{#include ../../zokrates_cli/examples/book/hashexample.code}}
```
The first line imports the `sha256packed` function from the ZoKrates standard library.
@ -74,13 +70,7 @@ To make it work, the two parties have to follow their roles in the protocol:
First, Victor has to specify what hash he is interested in. Therefore, we have to adjust the zkSNARK circuit, compiled by ZoKrates, such that in addition to computing the digest, it also validates it against the digest of interest, provided by Victor. This leads to the following update for `hashexample.code`:
```zokrates
import "LIBSNARK/sha256packed"
def main(private field a, private field b, private field c, private field d) -> (field):
h0, h1 = sha256packed(a, b, c, d)
h0 == 263561599766550617289250058199814760685
h1 == 65303172752238645975888084098459749904
return 1
{{#include ../../zokrates_cli/examples/book/hashexample_updated.code}}
```
Note that we now compare the result of `sha256packed` with the hard-coded correct solution defined by Victor. The lines which we added are treated as assertions: the verifier will not accept a proof where these constraints were not satisfied. Clearly, this program only returns 1 if all of the computed bits are equal.

View file

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

View file

@ -0,0 +1,4 @@
def main() -> (field):
field a = 42 // this is an end of line comment
// this is a full line comment
return a

View file

@ -0,0 +1,3 @@
def main(private field a, field b) -> (field):
field result = if a * a == b then 1 else 0 fi
return result

View file

@ -0,0 +1,4 @@
def main() -> (field):
field pMinusOne = 21888242871839275222246405745257275088548364400416034343698204186575808495616
0 - 1 == pMinusOne
return 1

View file

@ -0,0 +1,6 @@
def main() -> (field):
field res = 0
for field i in 0..4 do
res = res + i
endfor
return res

View file

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

View file

@ -0,0 +1,5 @@
import "LIBSNARK/sha256packed"
def main(private field a, private field b, private field c, private field d) -> (field, field):
h0, h1 = sha256packed(a, b, c, d)
return h0, h1

View file

@ -0,0 +1,7 @@
import "LIBSNARK/sha256packed"
def main(private field a, private field b, private field c, private field d) -> (field):
h0, h1 = sha256packed(a, b, c, d)
h0 == 263561599766550617289250058199814760685
h1 == 65303172752238645975888084098459749904
return 1

View file

@ -0,0 +1,3 @@
def main(field x) -> (field):
field y = if x + 2 == 3 then 1 else 5 fi
return y

View file

@ -0,0 +1,6 @@
def foo() -> (field, field):
return 21, 42
def main() -> (field):
a, b = foo()
return 1

View file

@ -0,0 +1,2 @@
def main() -> (field, field[3]):
return 1, [2, 3, 4]

View file

@ -0,0 +1,9 @@
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

View file

@ -0,0 +1,9 @@
def foo() -> (field, field[3]):
return 1, [2, 3, 4]
def foo() -> (field, field):
return 1, 2
def main() -> (field):
a, field[3] b = foo()
return 1