forked from tommymsz006/zkbattleship-circuit
/
circuits.sh
executable file
·65 lines (52 loc) · 3.35 KB
/
circuits.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
set -e
# --------------------------------------------------------------------------------
# Phase 2
# ... circuit-specific stuff
# if zk/zkey does not exist, make folder
[ -d zk/zkey ] || mkdir zk/zkey
# Compile circuits
circom zk/circuits/board.circom -o zk/ --r1cs --wasm
circom zk/circuits/shot.circom -o zk/ --r1cs --wasm
#Setup
yarn snarkjs groth16 setup zk/board.r1cs zk/ptau/pot15_final.ptau zk/zkey/board_final.zkey
yarn snarkjs groth16 setup zk/shot.r1cs zk/ptau/pot15_final.ptau zk/zkey/shot_final.zkey
# # Generate reference zkey
yarn snarkjs zkey new zk/board.r1cs zk/ptau/pot15_final.ptau zk/zkey/board_0000.zkey
yarn snarkjs zkey new zk/shot.r1cs zk/ptau/pot15_final.ptau zk/zkey/shot_0000.zkey
# # Ceremony just like before but for zkey this time
yarn snarkjs zkey contribute zk/zkey/board_0000.zkey zk/zkey/board_0001.zkey \
--name="First board contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
yarn snarkjs zkey contribute zk/zkey/shot_0000.zkey zk/zkey/shot_0001.zkey \
--name="First shot contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
yarn snarkjs zkey contribute zk/zkey/board_0001.zkey zk/zkey/board_0002.zkey \
--name="Second board contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
yarn snarkjs zkey contribute zk/zkey/shot_0001.zkey zk/zkey/shot_0002.zkey \
--name="Second shot contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
yarn snarkjs zkey contribute zk/zkey/board_0002.zkey zk/zkey/board_0003.zkey \
--name="Third board contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
yarn snarkjs zkey contribute zk/zkey/shot_0002.zkey zk/zkey/shot_0003.zkey \
--name="Third shot contribution" -v -e="$(head -n 4096 /dev/urandom | openssl sha1)"
# # Verify zkey
yarn snarkjs zkey verify zk/board.r1cs zk/ptau/pot15_final.ptau zk/zkey/board_0003.zkey
yarn snarkjs zkey verify zk/shot.r1cs zk/ptau/pot15_final.ptau zk/zkey/shot_0003.zkey
# # Apply random beacon as before
yarn snarkjs zkey beacon zk/zkey/board_0003.zkey zk/zkey/board_final.zkey \
0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Board FinalBeacon phase2"
yarn snarkjs zkey beacon zk/zkey/shot_0003.zkey zk/zkey/shot_final.zkey \
0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Shot Final Beacon phase2"
# # Optional: verify final zkey
yarn snarkjs zkey verify zk/board.r1cs zk/ptau/pot15_final.ptau zk/zkey/board_final.zkey
yarn snarkjs zkey verify zk/shot.r1cs zk/ptau/pot15_final.ptau zk/zkey/shot_final.zkey
# # Export verification key
yarn snarkjs zkey export verificationkey zk/zkey/board_final.zkey zk/board_verification_key.json
yarn snarkjs zkey export verificationkey zk/zkey/shot_final.zkey zk/shot_verification_key.json
# Export board verifier with updated name and solidity version
yarn snarkjs zkey export solidityverifier zk/zkey/board_final.zkey contracts/BoardVerifier.sol
# sed -i'.bak' 's/0.6.11;/0.8.11;/g' contracts/BoardVerifier.sol
sed -i'.bak' 's/contract Verifier/contract BoardVerifier/g' contracts/BoardVerifier.sol
# Export shot verifier with updated name and solidity version
yarn snarkjs zkey export solidityverifier zk/zkey/shot_final.zkey contracts/ShotVerifier.sol
# sed -i'.bak' 's/0.6.11;/0.8.11;/g' contracts/ShotVerifier.sol
sed -i'.bak' 's/contract Verifier/contract ShotVerifier/g' contracts/ShotVerifier.sol
rm contracts/*.bak