Skip to content

Commit 59da697

Browse files
committed
release
1 parent bb59d61 commit 59da697

File tree

4 files changed

+250
-5
lines changed

4 files changed

+250
-5
lines changed

LICENSE.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Portions of this code are copyright 2019 Meilof Veeningen and have the
2+
following license:
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is furnished
9+
to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
22+
Portions of this code are copyright (c) 2016-2018 Koninklijke Philips N.V.
23+
All rights reserved. A copyright license for redistribution and use in source
24+
and binary forms, with or without modification, is hereby granted for
25+
non-commercial, experimental and research purposes, provided that the following
26+
conditions are met:
27+
28+
* Redistributions of source code must retain the above copyright notice,
29+
this list of conditions and the following disclaimers.
30+
* Redistributions in binary form must reproduce the above copyright notice,
31+
this list of conditions and the following disclaimers in the
32+
documentation and/or other materials provided with the distribution. If
33+
you wish to use this software commercially, kindly contact
34+
[email protected] to obtain a commercial license.
35+
36+
This license extends only to copyright and does not include or grant any
37+
patent license or other license whatsoever.
38+
39+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
40+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
43+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49+
POSSIBILITY OF SUCH DAMAGE.
50+
51+
The file `pysnark/contracts/Pairing.sol` is copyright by Christian Reitwiessner, see the file itself for its license.
52+
53+
The file `examples/DRBG.py` is copyright by David Lazar, see the file itself for its license.
54+
55+
PySNARK uses libsnark, which is covered by [this license](https://github.com/meilof/libsnark/blob/master/LICENSE).

README.md

+188-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,191 @@
1-
Mac OS magic command:
1+
# PySNARK
2+
3+
*(This repository is for the libsnark-based version of PySNARK. See [here](https://github.com/Charterhouse/pysnark) for the original libqap-based version.)*
4+
5+
PySNARK lets you program zk-SNARKs (aka verifiable computations) directly in Python 3. For example, the following code runs a SNARK program to compute a cube of a number, generates key material, generates a proof, and verifies it:
6+
7+
```
8+
import sys
9+
10+
from pysnark.runtime import snark
11+
12+
@snark
13+
def cube(x):
14+
return x*x*x
15+
16+
print("The cube of", sys.argv[1], "is", cube(int(sys.argv[1])))
17+
```
18+
19+
PySNARK can use [qaptools](https://github.com/Charterhouse/qaptools) or [libsnark](https://github.com/scipr-lab/libsnark) as backend. For any computations performed using the PubVal datatype provided by pysnark (or using the `@snark` decorator), the library keeps track of the Rank-1 constraint system of the computation. When the computation finishes, key material for the computation is generated (or re-used) and a SNARK proof is generated.
20+
21+
The [previous PySNARK](https://github.com/Charterhouse/pysnark) also inclded functionality to automatically turn the zk-SNARK into a Solidity smart contracts for use on the Ethereum blockchain. This functionality is not available in the current version yet.
22+
23+
PySNARK may be used for non-commercial, experimental and research purposes; see `LICENSE.md` for details.
24+
PySNARK is experimental and **not fit for production environment**.
25+
26+
## Installation
27+
28+
PySNARK requires Python 3.*.
29+
30+
### Requirements for libsnark backend
31+
32+
The libsnark module requires SWIG, a C++ compiler, Python3 header files, CMake, and the GNU MP library. See [here](https://github.com/scipr-lab/libsnark) for details. On Linux, the following has been found to work to satisfy the requirements:
33+
34+
```
35+
sudo apt-get install pkg-config build-essential cmake git libgmp3-dev libprocps-dev python-markdown libboost-all-dev libssl-dev
36+
```
37+
38+
### Requirements for qaptools backend
39+
40+
To compile the qaptools backend, a C++ compiler is needed. For Windows, qaptools binaries can be downloaded [here](https://github.com/Charterhouse/qaptools).
41+
42+
### Building
43+
44+
Download PySNARK including submodules:
45+
46+
```
47+
git clone --recursive https://github.com/meilof/pysnark.git
48+
```
49+
50+
Build and install PySNARK (assuming `python` is Python 3):
51+
52+
```
53+
python setup.py install
54+
```
55+
56+
To disable the qaptools backend, use `--disable-qaptools`. To disable the libsnark backend, use `--disable-libsnark`. To specify locations of precompiled qaptools binaries (e.g., for Windows), use `--qaptools=bin=<dir>`. Any CMake arguments (`-D...`), for example, for libsnark, can be given direcly on the above command line. For example, on Mac OS X I use `python3 setup.py install -DCMAKE_PREFIX_PATH=/usr/local/Cellar/openssl/1.0.2s -DCMAKE_SHARED_LINKER_FLAGS=-L/usr/local/Cellar/openssl/1.0.2s/lib -DWITH_PROCPS=OFF -DWITH_SUPERCOP=OFF -DOPT_FLAGS=-std=c++11`.
57+
58+
59+
## Using PySNARK (libsnark backend)
60+
61+
To try out PySNARK, do the following:
62+
63+
```
64+
cd examples
65+
python cube.py 3
66+
```
67+
68+
This will execute a SNARK computation to compute the cube of the input value, `3`.
69+
As the comptation prorgresses, a constraint system of the computation is kept.
70+
71+
By default, if available, the libsnark backend will be used. In this case, the following files will be generated:
72+
73+
* `pysnark_ek`: key material to generate proofs for this computation (if the same computation is performed later, this file will be re-used; if another computation is performed, it is rebuilt)
74+
* `pysnark_vk`: key material to verify proofs for this computation
75+
* `pysnark_log`: computation log that can be verified with the `pysnark_vk` key: number of inputs/outputs, followed by the inputs/outputs themselves, followed by a proof that the input/outputs were correctly computed
76+
77+
78+
## Using PySNARK (qaptools backend)
79+
80+
We discuss the usage of the PySNARK toolchain based on running one of the provided examples acting as each
81+
of the different types of parties in a verifiable computation: trusted party, prover, or verifier.
82+
83+
### As trusted party
84+
85+
To try out running PySNARK as trusted party performing key generation, do the following:
86+
87+
```
88+
cd examples
89+
python cube.py 3
90+
```
91+
92+
If PySNARK has been correctly installed, this will perform a verifiable computation that will compute the cube of the input value, `3`.
93+
At the same time, it will generate all key material needed to verifiably perform the computation in the script.
94+
(Performing an example computation is the only way to generate this key material.)
95+
PySNARK produces the following files:
96+
97+
* Files that should be kept secret by the trusted party generating the key material:
98+
* `pysnark_mastersk`: zk-SNARK master secret key
99+
* Files that the trusted party should distribute to provers along with the Python script (i.e., `cube.py` in this case):
100+
* `pysnark_schedule`: schedule of functions called in the computation
101+
* `pysnark_masterek`: master evaluation key
102+
* `pysnark_ek_main`: zk-SNARK evaluation
103+
key for the main function of the computation
104+
* `pysnark_eqs_main`: equations for the main function of the computation
105+
* Files that the trusted party should distribute to verifiers:
106+
* `pysnark_schedule`: schedule of functions called in the computation
107+
* `pysnark_masterpk`: master public key
108+
* `pysnark_vk_main`: verificaiton key for the main function
109+
* Files that the prover should distribute to verifiers:
110+
* `pysnark_proof`: proof that the particular computation was performed correctly
111+
* `pysnark_values`: input/output values of the computation
112+
* Files that are not needed anymore after the execution:
113+
* `pysnark_eqs`: equations for the zk-SNARK
114+
* `pysnark_wires`: wire values of the computation
115+
116+
### As prover
117+
118+
To try out running PySNARK as a prover, put the files discussed above (i.e., `pysnark_schedule`, `pysnark_masterek`, `pysnark_ek_main`, and `pysnark_eqs_main`) together with `cube.py` in a directory and run the same command:
119+
120+
```
121+
cd examples
122+
python cube.py 3
123+
```
124+
125+
This will perform a verifiable computation based on the previously generated key material.
126+
127+
### As verifier
128+
129+
To try out running PySNARK as a verifier, put the files discussed above (i.e., `pysnark_schedule`, `pysnark_masterpk` and `pysnark_vk_main` received from the trusted party, and `pysnark_proof` and `pysnark_values` received from the prover) in a folder and run
130+
131+
```
132+
python -m pysnark.qaptools.runqapver
133+
```
134+
135+
This will verify the computation proof with respect to the input/output values from the `pysnark_values` file, e.g,:
2136

3137
```
4-
python3 setup.py install -DCMAKE_PREFIX_PATH=/usr/local/Cellar/openssl/1.0.2s -DCMAKE_SHARED_LINKER_FLAGS=-L/usr/local/Cellar/openssl/1.0.2s/lib -DWITH_PROCPS=OFF -DWITH_SUPERCOP=OFF -DOPT_FLAGS=-std=c++11
138+
# PySNARK i/o
139+
main/o_in: 21
140+
main/o_out: 9261
5141
```
142+
143+
In this case, we have verifiably computed the fact that the cube of 21 is 9261. See the `examples` folder for additional examples.
144+
145+
146+
### Using commitments
147+
148+
PySNARK allows proofs to refer to committed data using [Geppetri](https://eprint.iacr.org/2017/013).
149+
This has three applications:
150+
- it allows proofs to refer to external private inputs from parties other than the trusted third party;
151+
- it allows different verifiable computations to share secret data with each other; and
152+
- it allows to divide a verifiable computation into multiple subcomputations, each with their own evaluation and verification keys (but all based on the same master secret key)
153+
154+
All computations sharing committe data should use the same master secret key.
155+
156+
See `examples/testcomm.py` for examples.
157+
158+
#### External secret inputs
159+
160+
To commit to data, use `pysnark.qaptools.runqapinput`, e.g., to commit to values 1, 2, and 3 using a commitment named `test`, use:
161+
162+
```python -m pysnark.qaptools.runqapinput test 1 2 3```
163+
164+
Alternatively, use `pysnark.qaptools.runqapinput.gencomm` from a Python script.
165+
Share `pysnark_wires_test` with any prover who wants to perform a computation with respect to this committed data, and `pysnark_comm_test` to any verifier.
166+
167+
Import this data into the verifiable computation with
168+
169+
```[one,two,three] = pysnark.qaptools.backend.importcomm("test")```
170+
171+
#### Sharing data between verifiable computations
172+
173+
In the first computation, do
174+
175+
```pysnark.qaptools.backend.exportcomm([Var(1),Var(2),Var(3)], "test")```
176+
177+
and share `pysnark_wires_test` and `pysnark_comm_test` with the other prover and the verifier, respectively.
178+
179+
In the second verifiable computation, do
180+
181+
```[one,two,three] = pysnark.qaptools.backend.importcomm("test")```
182+
183+
#### Sharing data between different parts of a verifiable computation
184+
185+
This is implicitly used whenever a function is called that is decorated with `@pysnark.qaptools.backend.subqap`.
186+
When a particular functon is used multiple times in a verifiable computation, using `@pysnark.qaptools.backend.subqap` prevents the circuit for the function to be replicated, resulting in smaller key material (but slower verification).
187+
188+
## Acknowledgements
189+
190+
Part of this work on this software was carried out as part of the [SODA](https://www.soda-project.eu/) project that has received funding from the European Union’s Horizon 2020 research and innovation programme under grant agreement No 731583.
191+

TODO

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
http://mingw-w64.org/doku.php/download
55
* does making a source distribution work?
66
* windows cross-compiling?
7+
* benchmarking: @guarded_and_unguarded
8+
* libsnark: automatically load key given by hash?
79
* energy saving branches
8-
- bits: use 0
10+
- bits: use 0 (and other ValueError computations)
911
- use guard as ONE
1012
- check for other computations
1113
(could be beneficial to have safe instead of unsafe, document this
@@ -30,4 +32,6 @@
3032
* do sudoku example
3133
* rename / operator because it's too dangerous
3234
* make ggh hashing more transparent: one function, conditonal % inside
33-
* check where .value is used: can we avoid all use outside of runtime?
35+
* check where .value is used: can we avoid all use outside of runtime?
36+
37+
* jupyter, mybinder.org, https://aws.amazon.com/getting-started/projects/build-serverless-web-app-lambda-apigateway-s3-dynamodb-cognito/

examples/testcomm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2727
# POSSIBILITY OF SUCH DAMAGE.
2828

29-
from pysnark.useqaptools import importcomm, exportcomm, subqap
29+
from pysnark.qaptools.backend import importcomm, exportcomm, subqap
3030
from pysnark.qaptools.runqapinput import gencomm
3131

3232
from pysnark.runtime import PrivVal

0 commit comments

Comments
 (0)