Skip to content

Commit d1be60a

Browse files
committed
feat: circom example circuits
1 parent a6696d0 commit d1be60a

File tree

7 files changed

+207
-0
lines changed

7 files changed

+207
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pragma circom 2.0.0;
2+
3+
template Multiplier(n) {
4+
signal input a;
5+
signal input b;
6+
signal output c;
7+
8+
signal int[n];
9+
10+
int[0] <== a*a + b;
11+
for (var i=1; i<n; i++) {
12+
int[i] <== int[i-1]*int[i-1] + b;
13+
}
14+
15+
c <== int[n-1];
16+
}
17+
18+
component main {public [d]} = Multiplier(1000);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Reference: https://github.com/iden3/snarkjs?tab=readme-ov-file#10-compile-the-circuit
4+
5+
# 10. Compile the circuit
6+
circom --r1cs --wasm --c --sym --inspect circuit.circom
7+
# 11. View information about the circuit
8+
snarkjs r1cs info circuit.r1cs
9+
# 12. Print the constraints [omitted]
10+
# 13. Export r1cs to json
11+
snarkjs r1cs export json circuit.r1cs circuit.r1cs.json
12+
# 14. Calculate the witness
13+
snarkjs wtns calculate circuit_js/circuit.wasm input.json witness.wtns
14+
# 15. Setup. This generates the reference zkey without phase 2 contributions.
15+
#IMPORTANT: Do not use this zkey in production, as it's not safe. It requires at least one contribution.
16+
snarkjs groth16 setup circuit.r1cs pot14_final.ptau circuit_0000.zkey
17+
# 16. Contribute to the phase 2 ceremony
18+
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor Name" -v -e="Random entropy"
19+
# 17. Provide a second contribution
20+
snarkjs zkey contribute circuit_0001.zkey circuit_0002.zkey --name="Second contribution Name" -v -e="Another random entropy"
21+
# 18. Provide a third contribution using third-party software
22+
snarkjs zkey export bellman circuit_0002.zkey challenge_phase2_0003
23+
snarkjs zkey bellman contribute bn128 challenge_phase2_0003 response_phase2_0003 -e="some random text"
24+
snarkjs zkey import bellman circuit_0002.zkey response_phase2_0003 circuit_0003.zkey -n="Third contribution name"
25+
# 19. Verify the latest zkey. Expected output: [INFO] snarkJS: ZKey Ok!
26+
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_0003.zkey
27+
# 20. Apply a random beacon
28+
snarkjs zkey beacon circuit_0003.zkey circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
29+
# 21. Verify the final zkey. Expected output: [INFO] snarkJS: ZKey Ok!
30+
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_final.zkey
31+
# 22. Export the verification key
32+
snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
33+
# 23a. Calculate the witness and generate the proof in one step
34+
snarkjs groth16 fullprove input.json circuit_js/circuit.wasm circuit_final.zkey proof.json public.json
35+
# 24. Verify the proof
36+
snarkjs groth16 verify verification_key.json public.json proof.json
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Reference: https://github.com/iden3/snarkjs?tab=readme-ov-file#guide
4+
5+
# 1. Start a new powers of tau ceremony
6+
snarkjs powersoftau new bn128 14 pot14_0000.ptau -v
7+
# 2. Contribute to the ceremony
8+
snarkjs powersoftau contribute pot14_0000.ptau pot14_0001.ptau --name="First contribution" -v -e="random text"
9+
# 3. Provide a second contribution
10+
snarkjs powersoftau contribute pot14_0001.ptau pot14_0002.ptau --name="Second contribution" -v -e="some random text"
11+
# 4. Provide a third contribution using third-party software
12+
snarkjs powersoftau export challenge pot14_0002.ptau challenge_0003
13+
snarkjs powersoftau challenge contribute bn128 challenge_0003 response_0003 -e="some random text"
14+
snarkjs powersoftau import response pot14_0002.ptau response_0003 pot14_0003.ptau -n="Third contribution name"
15+
# 5. Verify the protocol so far. Expected output: [INFO] snarkJS: Powers Of tau file OK!
16+
snarkjs powersoftau verify pot14_0003.ptau
17+
# 6. Apply a random beacon
18+
snarkjs powersoftau beacon pot14_0003.ptau pot14_beacon.ptau 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon"
19+
# 7. Prepare phase 2
20+
snarkjs powersoftau prepare phase2 pot14_beacon.ptau pot14_final.ptau -v
21+
# 8. Verify the final ptau. Expected output: [INFO] snarkJS: Powers Of tau file OK!
22+
snarkjs powersoftau verify pot14_final.ptau
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": "3", "b": "11" }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"pi_a": [
3+
"16722573656943464333860124659786065872289619391098705842213648474331604159297",
4+
"13790640306822955647346253672680911890378915127950076115856346225363265763183",
5+
"1"
6+
],
7+
"pi_b": [
8+
[
9+
"93138861937445328169175979420211514695928597655489281366976893707939341752",
10+
"3224312611460037886945348472980480769019622577307510050916157765384730753073"
11+
],
12+
[
13+
"1776487012873561182566306494781430471767574663530402466604493648041677636385",
14+
"18218468117142115350457896979724484266508681772760755647544879184033721047170"
15+
],
16+
[
17+
"1",
18+
"0"
19+
]
20+
],
21+
"pi_c": [
22+
"8087254025918678094012939564591378430707391653016930146495341773896523279908",
23+
"8520099736927811710367498212633391432973805490157245760079769483387556529963",
24+
"1"
25+
],
26+
"protocol": "groth16",
27+
"curve": "bn128"
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"7713112592372404476342535432037683616424591277138491596200192981572885523208"
3+
]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"protocol": "groth16",
3+
"curve": "bn128",
4+
"nPublic": 2,
5+
"vk_alpha_1": [
6+
"11979537797352601789418129132512619370824465834135308733610083091376446810278",
7+
"10843325232858927436674816085333342966455784247882376825352784573722249290167",
8+
"1"
9+
],
10+
"vk_beta_2": [
11+
[
12+
"10599693880755786334069380102303222433996951919233568030030764463733362570899",
13+
"15042346062760545077514600966723822955342063689442050010692978775375051492737"
14+
],
15+
[
16+
"10547164923918579885933645981699410126244703770133552136775609674882385446602",
17+
"195797092158399244339884107816073252267042703717507095490077911729212481033"
18+
],
19+
[
20+
"1",
21+
"0"
22+
]
23+
],
24+
"vk_gamma_2": [
25+
[
26+
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
27+
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
28+
],
29+
[
30+
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
31+
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
32+
],
33+
[
34+
"1",
35+
"0"
36+
]
37+
],
38+
"vk_delta_2": [
39+
[
40+
"7491761477267373988056924362094133502772122140175430139014328627729119354576",
41+
"3245857749429578401515349745724762757877603836353345132598110932217324772285"
42+
],
43+
[
44+
"22919757621085021054330885561779403532516949645430162648639371946363441611",
45+
"6040033893960196523609594568354994339293676639153897589715849154590852743300"
46+
],
47+
[
48+
"1",
49+
"0"
50+
]
51+
],
52+
"vk_alphabeta_12": [
53+
[
54+
[
55+
"9075678340561424990277844593919960391672253853932946482664811778834287519100",
56+
"3040240318098647491121106260794412622567935471341829963554034196149728455716"
57+
],
58+
[
59+
"12036831181490782037905463719809730818852265058651117168143630531115014491531",
60+
"13751976382939702529667058623314873452032280076585017089464807757849149307126"
61+
],
62+
[
63+
"15639216501934325742764969740603585496069839733069323227453907003749668890912",
64+
"12896495719043583023354357129518586275808776006875626287891998453593362088181"
65+
]
66+
],
67+
[
68+
[
69+
"9153954881517839006532730553709327108015765836295304157243424473036091833935",
70+
"20394682900624945116941887925696925183137532287164401522515969938497465376916"
71+
],
72+
[
73+
"16775111153018513650684625819839092992658733862286785874746437149073741244261",
74+
"6726338845658414968603586126224253309944548712756302882095870181654667523321"
75+
],
76+
[
77+
"11877710049002158412590916351929627471765211851639610239632165778883289023106",
78+
"14887355832791045461307215285131371622242503109457084253649549904747979632792"
79+
]
80+
]
81+
],
82+
"IC": [
83+
[
84+
"100120723054092704620506719968910072979237999062852756544445218524958526679",
85+
"1744944903997407449414143230225893492269031779263425240251923956832350622849",
86+
"1"
87+
],
88+
[
89+
"16937039055696066876960308939798844573005561469463569088463454952313096768648",
90+
"13839573833663966986514793303073233505285318033651011302487328002816678745885",
91+
"1"
92+
],
93+
[
94+
"13096986189699889469454836523563065907624386782951767748624263342085237338703",
95+
"15930466161328467860037395610308276248697320293853169643218136846371441467666",
96+
"1"
97+
]
98+
]
99+
}

0 commit comments

Comments
 (0)