-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzkverify-v2.sh
More file actions
135 lines (104 loc) Β· 4.06 KB
/
Copy pathzkverify-v2.sh
File metadata and controls
135 lines (104 loc) Β· 4.06 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
set -e
# Ask for API key and number of submissions
read -p "π Enter your API key: " API_KEY
read -p "π’ How many submissions to generate? " N
if [[ -z "$API_KEY" || -z "$N" ]]; then
echo "β Missing API key or submission count!"
exit 1
fi
# ========== STEP 1: ENV SETUP ==========
echo "β
Installing dependencies..."
sudo apt update && sudo apt install -y curl git jq
npm install snarkjs circom circomlib
# ========== STEP 2: COMPILE CIRCUIT ==========
echo "β
Compiling circuit..."
mkdir -p zkverify/{circuits,keys,proofs,input,witness,scripts}
cd zkverify
cat > circuits/add_and_multiply.circom <<EOF
include "../node_modules/circomlib/circuits/sha256.circom";
template QuizV3() {
signal input a;
signal input b;
signal input c;
signal input result;
signal temp1;
signal temp2;
temp1 <== (a + b) * c;
temp2 <== temp1 + 7;
result === temp2;
}
component main = QuizV3();
EOF
npx circom circuits/add_and_multiply.circom --r1cs --wasm --sym -o circuits/
mv add_and_multiply.* circuits/
# ========== STEP 3: PTAU & ZKEY ==========
echo "β
Preparing powers of tau and zkey..."
npx snarkjs powersoftau new bn128 12 keys/pot12_0000.ptau -v
npx snarkjs powersoftau contribute keys/pot12_0000.ptau keys/pot12_final.ptau --name="zkverify" -v -e="zkverify-challenge"
npx snarkjs powersoftau prepare phase2 keys/pot12_final.ptau keys/pot12_final_prepared.ptau
npx snarkjs groth16 setup circuits/add_and_multiply.r1cs keys/pot12_final_prepared.ptau keys/add_and_multiply_0000.zkey
npx snarkjs zkey contribute keys/add_and_multiply_0000.zkey keys/add_and_multiply_final.zkey --name="zkverify" -v -e="zkverify-contrib"
npx snarkjs zkey export verificationkey keys/add_and_multiply_final.zkey keys/verification_key.json
# ========== STEP 4: CREATE generatePayload.js ==========
echo "β
Creating generatePayload.js..."
cat > scripts/generatePayload.js <<'EOF'
const fs = require("fs");
const proof = JSON.parse(fs.readFileSync("proofs/proof.json", "utf8"));
const publicSignals = JSON.parse(fs.readFileSync("proofs/public.json", "utf8"));
const vk = JSON.parse(fs.readFileSync("keys/verification_key.json", "utf8"));
const payload = {
proofType: "groth16",
vkRegistered: true,
proofOptions: {
library: "snarkjs",
curve: "bn128"
},
proofData: {
proof: {
pi_a: proof.pi_a.map(String),
pi_b: proof.pi_b.map(pair => pair.map(String)),
pi_c: proof.pi_c.map(String)
},
publicSignals: publicSignals.map(String),
vk
}
};
fs.writeFileSync("payload.json", JSON.stringify(payload, null, 2));
EOF
# ========== STEP 5β7: LOOP PROOFS ==========
echo "β
Starting loop to create and submit payloads..."
> submit.log
for i in $(seq 1 $N); do
echo "π [$i/$N] Generating input..."
A=$(( RANDOM % 50 + 1 ))
B=$(( RANDOM % 50 + 1 ))
C=$(( RANDOM % 50 + 1 ))
RESULT=$(( ( (A + B) * C ) + 7 ))
echo "{ \"a\": $A, \"b\": $B, \"c\": $C, \"result\": $RESULT }" > input/input.json
npx snarkjs wtns calculate circuits/add_and_multiply.wasm input/input.json witness/witness.wtns
npx snarkjs groth16 prove keys/add_and_multiply_final.zkey witness/witness.wtns proofs/proof.json proofs/public.json
node scripts/generatePayload.js
SUCCESS=false
ATTEMPT=1
while [ $SUCCESS = false ] && [ $ATTEMPT -le 5 ]; do
echo "π Submitting payload #$i (Attempt $ATTEMPT)..."
RESPONSE=$(curl -s -X POST https://relayer-api.horizenlabs.io/api/v1/submit-proof/$API_KEY \
-H "Content-Type: application/json" \
-d @payload.json)
echo "[$i][$ATTEMPT] $RESPONSE" | tee -a submit.log
if [[ $RESPONSE == *"Too Many Requests"* ]]; then
echo "β³ Too Many Requests. Waiting before retry..."
RETRY_DELAY=$(( RANDOM % 101 + 100 )) # Wait 100-200s
echo "β³ Waiting $RETRY_DELAY seconds before retry..."
sleep $RETRY_DELAY
((ATTEMPT++))
else
SUCCESS=true
fi
done
DELAY=$(( RANDOM % 101 + 100 )) # Wait 100-200s before next submission
echo "β³ Waiting $DELAY seconds before next submission... "
sleep $DELAY
done
echo "π Done. All results saved in submit.log"