Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays of anything #434

Merged
merged 44 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
be37507
generalize arrays. wip, basic boolean array mvp
Schaeff Jun 21, 2019
8b431f0
Merge branch 'add-boolean-constants' of github.com:Zokrates/ZoKrates …
Schaeff Jun 22, 2019
dfabfa0
Merge branch 'add-boolean-propagation' of github.com:Zokrates/ZoKrate…
Schaeff Jun 22, 2019
17f9473
make arrays generic over types. wip
Schaeff Jun 22, 2019
c7c20c4
Merge branch 'optimize-zero-quadratic-comb' into generalize-arrays
Schaeff Jun 22, 2019
bd25cac
add example
Schaeff Jun 22, 2019
efc46e7
make tests pass
Schaeff Jun 25, 2019
3ba608c
create optimizer structure
Schaeff Jun 25, 2019
ecac773
implement deduplication
Schaeff Jun 25, 2019
011ae45
Merge branch 'develop' of github.com:Zokrates/ZoKrates into duplicate…
Schaeff Jun 25, 2019
3c093fb
Merge branch 'duplicate-optimizer' of github.com:Zokrates/ZoKrates in…
Schaeff Jun 25, 2019
003bc5e
remove core lib, reimplement if_else manually in flattening
Schaeff Jul 1, 2019
51a8cf1
revert example
Schaeff Jul 1, 2019
8291612
Merge branch 'develop' of github.com:Zokrates/ZoKrates into remove-co…
Schaeff Jul 1, 2019
0f74b49
Merge branch 'develop' of github.com:Zokrates/ZoKrates into generaliz…
Schaeff Jul 10, 2019
84b35ce
deduplicate code using Flatten trait
Schaeff Jul 10, 2019
574f1f1
fix bound
Schaeff Jul 10, 2019
f6bae35
remove unused
Schaeff Jul 10, 2019
2cd3943
remove unimplemented
Schaeff Jul 10, 2019
6e6d90f
make flatten_array_expr generic over inner type
Schaeff Jul 11, 2019
e89d60b
make if_else generic
Schaeff Jul 11, 2019
34f41dc
merge modules
Schaeff Jul 15, 2019
89917c0
fully recursive arrays
Schaeff Jul 16, 2019
b2fcb4c
fully recursive arrays
Schaeff Jul 16, 2019
3e6fb14
Merge branch 'rec-arrays' of github.com:Zokrates/ZoKrates into rec-ar…
Schaeff Jul 16, 2019
5be2d2c
remove example
Schaeff Jul 16, 2019
3fc3408
fix equality for wasm solvers
Schaeff Jul 16, 2019
22619be
Merge branch 'generalize-arrays' of github.com:Zokrates/ZoKrates into…
Schaeff Jul 16, 2019
2e17229
add tests
Schaeff Jul 16, 2019
5a2860c
merge dev
Schaeff Aug 26, 2019
acbb0d6
implement spreads for any type
Schaeff Aug 26, 2019
b901055
introduce annotate function to restrict access to Array fields
Schaeff Aug 27, 2019
a634b1c
simplify pest ast, add tests for from_ast
Schaeff Aug 28, 2019
52f33f0
implement unimplemented rec array update and boolean ifelse
Schaeff Aug 29, 2019
7ad2e89
add unroll test for rec array, extract IfElse and Select from Flatten…
Schaeff Aug 29, 2019
05b5939
remove unreachable parts of flattening, make sure we crash on multidi…
Schaeff Aug 29, 2019
ca6d92b
implement multidim updates
Schaeff Sep 9, 2019
5b4f581
add integration test
Schaeff Sep 9, 2019
c8080e9
small tweaks, simplify and improve propagation
Schaeff Sep 9, 2019
3b22838
add comments
Schaeff Sep 11, 2019
b17f34e
Documentation on extended array types
JacobEberhardt Sep 17, 2019
c49a97c
reverse array declaration dimensions, fix tests
Schaeff Sep 18, 2019
57afbb0
Merge branch 'develop' of github.com:Zokrates/ZoKrates into rec-arrays
Schaeff Sep 23, 2019
805b429
address review comments
Schaeff Sep 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions zokrates_book/src/concepts/types.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Types

ZoKrates currently exposes three types:
ZoKrates currently exposes two primitive types and a complex array type:

### `field`
### Primitive Types

#### `field`

This is the most basic type in ZoKrates, and it represents a positive integer in `[0, p - 1]` where `p` is a (large) prime number.

Expand All @@ -14,18 +16,72 @@ While `field` values mostly behave like unsigned integers, one should keep in mi
{{#include ../../../zokrates_cli/examples/book/field_overflow.code}}
```

### `bool`
#### `bool`

ZoKrates has limited support for booleans, to the extent that they can only be used as the condition in `if ... else ... endif` expressions.

You can use them for equality checks, inequality checks and inequality checks between `field` values.

Note that while equality checks are cheap, inequality checks should be use wisely as they are orders of magnitude more expensive.

### `field[n]`
### Complex Types

#### Arrays

ZoKrates supports static arrays, i.e., their length needs to be known at compile time.
Arrays can contain elements of any type and have arbitrary dimensions.

Static arrays of `field` can be instantiated with a constant size, and their elements can be accessed and updated:
The following examples code shows examples of how to use arrays:

```zokrates
{{#include ../../../zokrates_cli/examples/book/array.code}}
```

##### Declaration and Initialization
An array is defined by appending `[]` to a type literal representing the type of the array's elements.

Initialization always needs to happen in the same statement than declaration, unless the array is declared within a function's signature.

For initialization, a list of comma-separated values is provided within brackets `[]`.

ZoKrates offers a special shorthand syntax to initialize an array with a constant value:
`[value;repetitions]`


The following code provides examples for declaration and initialization:
```zokrates
field[3] a = [1, 2, 3] // initialize a field array with field values
bool[13] b = [false; 13] // initialize a bool array with value false
```

##### Multidimensional Arrays

As an array can contain any type of elements, it can contain arrays again.
There is a special syntax to declare such multi-dimensional arrays, i.e., arrays of arrays.
To declare an array of an inner array, i.e., and array of elements of a type, prepend brackets `[size]` to the declaration of the inner array.
In summary, this leads to the following scheme for array declarations:
`data_type[size of 1st dimension][size of 2nd dimension]`.
Consider the following example:

```zokrates
{{#include ../../../zokrates_cli/examples/book/multidim_array.code}}
```

##### Spreads and Slices
ZoKrates provides some syntactic sugar to retrieve subsets of arrays.

###### Spreads
The spread operator `...` applied to an copies the elements of an existing array.
This can be used to conveniently compose new arrays, as shown in the following example:
```
field[3] = [1, 2, 3]
field[4] c = [...a, 4] // initialize an array copying values from `a`, followed by 4
```

###### Slices
An array can also be assigned to by creating a copy of a subset of an existing array.
This operation is called slicing, and the following example shows how to slice in ZoKrates:
```
field[3] a = [1, 2, 3]
field[2] b = a[1..3] // initialize an array copying a slice from `a`
```
9 changes: 9 additions & 0 deletions zokrates_cli/examples/arrays/boolean_array.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def main(bool[3] a) -> (field[3]):
bool[3] c = [true, true || false, true]
a[1] = true || a[2]
a[2] = a[0]
field[3] result = [0; 3]
for field i in 0..3 do
result[i] = if a[i] then 33 else 0 fi
endfor
return result
12 changes: 12 additions & 0 deletions zokrates_cli/examples/arrays/cube.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def main(field[2][2][2] cube) -> (field):
field res = 0

for field i in 0..2 do
for field j in 0..2 do
for field k in 0..2 do
res = res + cube[i][j][k]
endfor
endfor
endfor

return res
4 changes: 4 additions & 0 deletions zokrates_cli/examples/arrays/multidim_update.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def main(field[10][10][10] a, field i, field j, field k) -> (field[3]):
a[i][j][k] = 42
field[3][3] b = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
return b[0]
10 changes: 10 additions & 0 deletions zokrates_cli/examples/arrays/value.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def main() -> ():
field[3] a = [1, 2, 3]
bool[3] b = [true, true, false]
field[3][2] c = [[1, 2], [3, 4], [5, 6]]

field[3] aa = [...a]
bool[3] bb = [...b]
field[3][2] cc = [...c]

return
3 changes: 2 additions & 1 deletion zokrates_cli/examples/book/array.code
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
def main() -> (field):
field[3] a = [1, 2, 3] // initialize an array with values
field[3] a = [1, 2, 3] // initialize a field array with field values
a[2] = 4 // set a member to a value
field[4] b = [42; 4] // initialize an array of 4 values all equal to 42
field[4] c = [...a, 4] // initialize an array copying values from `a`, followed by 4
field[2] d = a[1..3] // initialize an array copying a slice from `a`
bool[3] e = [true, true || false, true] // initialize a boolean array
return a[0] + b[1] + c[2]
9 changes: 9 additions & 0 deletions zokrates_cli/examples/book/multidim_array.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def main() -> (field):

// Array of two elements of array of 3 elements
field[2][3] a = [[1, 2, 3],[4, 5, 6]]

field[3] b = a[0] // should be [1, 2, 3]

// allowed access [0..2][0..3]
return a[1][2]
6 changes: 6 additions & 0 deletions zokrates_cli/tests/code/multidim_update.arguments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
0,
0,
0,
0
]
3 changes: 3 additions & 0 deletions zokrates_cli/tests/code/multidim_update.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main(field[2][2] a) -> (field[2][2]):
a[1][1] = 42
return a
4 changes: 4 additions & 0 deletions zokrates_cli/tests/code/multidim_update.expected.witness
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
~out_0 0
~out_1 0
~out_2 0
~out_3 42
Loading