Skip to content

Commit ea373be

Browse files
committed
interface with snarkjs
1 parent f9a221e commit ea373be

File tree

6 files changed

+84
-5
lines changed

6 files changed

+84
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ examples/pysnark_*
1010
examples/out
1111
MANIFEST.in
1212
qaptools/*
13+
examples/*.json
1314
examples/contracts/*
1415
examples/test/*
1516
examples/migrations/*

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Features:
2222

2323
* Support Unix platforms (Linux, Mac OS X, ...) and Windows
2424
* Automatically produce Solidity smart contracts
25+
* Automatically produce snarkjs verification key/proof/public values
2526
* Support for [integer arithmetic](https://github.com/meilof/pysnark/blob/master/pysnark/runtime.py#L179), [linear algebra](https://github.com/meilof/pysnark/blob/master/pysnark/linalg.py#L3), [arrays with conditional indexing](https://github.com/meilof/pysnark/blob/master/pysnark/array.py#L36), [if statements](https://github.com/meilof/pysnark/blob/master/pysnark/branching.py#L10) and [branching](https://github.com/meilof/pysnark/blob/master/pysnark/branching.py#L132), and [hashing](https://github.com/meilof/pysnark/blob/master/pysnark/hash.py#L61); see provided [examples](https://github.com/meilof/pysnark/tree/master/examples)
2627

2728
PySNARK may be used for non-commercial, experimental and research purposes; see `LICENSE.md` for details.
@@ -88,6 +89,22 @@ By default, if available, the libsnark backend will be used. In this case, the f
8889
* `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
8990

9091

92+
### Combining with snarkjs
93+
94+
PySNARK with the libsnark backend can automatically produce snarkjs `public.json`, `proof.json` and `verification_key.json` files for the performed verifiable computation:
95+
96+
```
97+
meilofs-air:examples meilof$ python3 cube.py 33
98+
The cube of 33 is 35937
99+
*** Trying to read pysnark_ek
100+
*** PySNARK: generating proof pysnark_log (sat=True, #io=2, #witness=2, #constraint=3)
101+
*** Public inputs: 33 35937
102+
*** Verification status: True
103+
meilofs-air:examples meilof$ python3 -m pysnark.libsnark.tosnarkjs
104+
meilofs-air:examples meilof$ snarkjs verify
105+
OK
106+
```
107+
91108
## Using PySNARK (qaptools backend)
92109

93110
We discuss the usage of the PySNARK toolchain based on running one of the provided examples acting as each

TODO

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
* write_proof: do non-binary output; check: should be montgomery or not?
2+
* write_proof does not actually work; convert to public.json, proof.json,
3+
verification_key.json; output human readable ver key (see ~/temp/factor/)
4+
15
* documenteren
2-
* port smart contracts
3-
* source release
46
* setup.py: detect which depends/ are available, also allow to use exes, even .exe windows...
57
also: put in version
68

@@ -30,7 +32,7 @@
3032
- remove it? produces counter-intuitive results...
3133
- or check why it does not help that much
3234
* dealing with huge values, e.g., compute 2^10000 gives segfault
33-
time python3 exp.py 2 10000
35+
time python3 exp.py 2 10000; do lincomb.reduce() to -p/2,p/2; let nobackend rerurn 2^64 as modulus?
3436
* TypeError instead of ValueError?
3537
* remove gtest
3638
* solve double dependency thing?

depends/python-libsnark

pysnark/libsnark/backend.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def prove():
5656
")")
5757

5858
proof=libsnark.zks_prover(keypair.pk, pubvals, privvals);
59-
verified=libsnark.zks_verifier_strong_IC(keypair.vk, pubvals, proof);
59+
verified=libsnark.zks_verifier_strong_IC(keypair.vk, pubvals, proof)
60+
libsnark.write_proof(proof, pubvals, "pysnark_log")
6061

6162
print("*** Public inputs: " + " ".join([str(pubvals.at(i)) for i in range(pubvals.size())]))
6263
print("*** Verification status:", verified)

pysnark/libsnark/tosnarkjs.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
vkin = open("pysnark_vk", "r")
2+
vkout = open("verification_key.json", "w")
3+
4+
print('{', file=vkout)
5+
print(' "protocol": "original",', file=vkout)
6+
7+
def process_g2(nm, fin, fout):
8+
a1=list(map(lambda x:x.strip(),next(fin).split(" ")))
9+
a2=list(map(lambda x:x.strip(),next(fin).split(" ")))
10+
print(' "' + nm + '": [', file=fout)
11+
print(' [ "' + a1[0] + '", "' + a1[1] + '"],', file=fout)
12+
print(' [ "' + a2[0] + '", "' + a2[1] + '"],', file=fout)
13+
print(' [ "1", "0" ] ],', file=fout)
14+
15+
def process_g1(nm, fin, fout):
16+
b=[next(fin).strip(), next(fin).strip()]
17+
print(' "' + nm + '": ["' + b[0] + '", "' + b[1] + '", "1" ],', file=fout)
18+
19+
process_g2("vk_a", vkin, vkout)
20+
process_g1("vk_b", vkin, vkout)
21+
process_g2("vk_c", vkin, vkout)
22+
process_g2("vk_g", vkin, vkout)
23+
process_g1("vk_gb_1", vkin, vkout)
24+
process_g2("vk_gb_2", vkin, vkout)
25+
process_g2("vk_z", vkin, vkout)
26+
27+
nin = int(next(vkin).strip())
28+
print(' "nPublic": ' + str(nin-1) + ',', file=vkout)
29+
30+
print(' "IC": [', file=vkout)
31+
for i in range(nin):
32+
b=[next(vkin).strip(), next(vkin).strip()]
33+
print(' ["' + b[0] + '", "' + b[1] + '", "1"]' + (',' if i!=nin-1 else ''), file=vkout)
34+
print(' ]', file=vkout)
35+
print('}', file=vkout)
36+
37+
login = open("pysnark_log", "r")
38+
pubout = open("public.json", "w")
39+
proofout = open("proof.json", "w")
40+
41+
npub = int(next(login).strip())
42+
43+
print('[', file=pubout)
44+
for i in range(npub):
45+
print(' "' + next(login).strip() + '"' + (',' if i!=npub-1 else ''), file=pubout)
46+
print(']', file=pubout)
47+
48+
print('{', file=proofout)
49+
process_g1("pi_a", login, proofout)
50+
process_g1("pi_ap", login, proofout)
51+
process_g2("pi_b", login, proofout)
52+
process_g1("pi_bp", login, proofout)
53+
process_g1("pi_c", login, proofout)
54+
process_g1("pi_cp", login, proofout)
55+
process_g1("pi_h", login, proofout)
56+
process_g1("pi_kp", login, proofout)
57+
print(' "protocol": "original"', file=proofout)
58+
print('}', file=proofout)

0 commit comments

Comments
 (0)