|
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,: |
2 | 136 |
|
3 | 137 | ```
|
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 |
5 | 141 | ```
|
| 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 | + |
0 commit comments