Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zkp grant phase3 #46

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/extention-contracts/lib/forge-std"]
path = packages/extention-contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
1,732 changes: 1,639 additions & 93 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"workspaces": [
"packages/circom-circuits",
"packages/contracts",
"packages/base-contracts",
"packages/extention-contracts",
"packages/halo2-circuits",
"packages/script"
],
Expand All @@ -21,10 +22,11 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@zk-kit/imt.sol": "^2.0.0-beta.3",
"circomlib": "^2.0.5",
"js-sha256": "^0.10.1",
"js-x509-utils": "^1.0.7",
"node-rsa": "^1.1.1",
"rsasign": "^1.4.2"
}
}
}
23 changes: 8 additions & 15 deletions packages/circom-circuit/.gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
build/
setup/
certs/
powersOfTau*
user-sig-setup/circuit*
user-sig-setup/witness.wtns
user-sig-setup/proof.json
user-sig-setup/public.json
user-sig-setup/verification_key.json
user-sig-build/*
gov-sig-setup/circuit*
gov-sig-setup/witness.wtns
gov-sig-setup/proof.json
gov-sig-setup/public.json
gov-sig-setup/verification_key.json
gov-sig-setup/input.json
gov-sig-build/*
build/gov_sig/*
build/main_myna_inclusion/*
build/main_myna_registration/*
build/user_sig/*
setup/gov_sig/*
setup/main_myna_inclusion/*
setup/main_myna_registration/*
setup/user_sig/*
154 changes: 103 additions & 51 deletions packages/circom-circuit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,106 @@ init:
test-circuit:
npm run test

gov-sig-build-circuit:
circom src/verify-gov-sig.circom --r1cs --wasm --sym -o ./gov-sig-build
node gov-sig-build/verify-gov-sig_js/generate_witness.js ./gov-sig-build/verify-gov-sig_js/verify-gov-sig.wasm ./gov-sig-setup/input.json ./gov-sig-setup/witness.wtns

user-sig-build-circuit:
circom src/verify-user-sig.circom --r1cs --wasm --sym -o ./user-sig-build
node user-sig-build/verify-user-sig_js/generate_witness.js ./user-sig-build/verify-user-sig_js/verify-user-sig.wasm ./user-sig-setup/input.json ./user-sig-setup/witness.wtns

gov-sig-setup-groth16:
snarkjs groth16 setup ./gov-sig-build/verify-gov-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./gov-sig-setup/circuit_0000.zkey
snarkjs zkey contribute ./gov-sig-setup/circuit_0000.zkey ./gov-sig-setup/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./gov-sig-setup/circuit_0001.zkey ./gov-sig-setup/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./gov-sig-setup/circuit_0002.zkey ./gov-sig-setup/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./gov-sig-build/verify-gov-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./gov-sig-setup/circuit_0003.zkey
snarkjs zkey beacon ./gov-sig-setup/circuit_0003.zkey ./gov-sig-setup/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./gov-sig-build/verify-gov-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./gov-sig-setup/circuit_final.zkey
snarkjs zkey export verificationkey ./gov-sig-setup/circuit_final.zkey ./gov-sig-setup/verification_key.json

user-sig-setup-groth16:
snarkjs groth16 setup ./user-sig-build/verify-user-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./user-sig-setup/circuit_0000.zkey
snarkjs zkey contribute ./user-sig-setup/circuit_0000.zkey ./user-sig-setup/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./user-sig-setup/circuit_0001.zkey ./user-sig-setup/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./user-sig-setup/circuit_0002.zkey ./user-sig-setup/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./user-sig-build/verify-user-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./user-sig-setup/circuit_0003.zkey
snarkjs zkey beacon ./user-sig-setup/circuit_0003.zkey ./user-sig-setup/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./user-sig-build/verify-user-sig.r1cs ./powersOfTau28_hez_final_20.ptau ./user-sig-setup/circuit_final.zkey
snarkjs zkey export verificationkey ./user-sig-setup/circuit_final.zkey ./user-sig-setup/verification_key.json

gov-sig-create-proof:
snarkjs groth16 prove ./gov-sig-setup/circuit_final.zkey ./gov-sig-setup/witness.wtns ./gov-sig-setup/proof.json ./gov-sig-setup/public.json

user-sig-create-proof:
snarkjs groth16 prove ./user-sig-setup/circuit_final.zkey ./user-sig-setup/witness.wtns ./user-sig-setup/proof.json ./user-sig-setup/public.json

gov-sig-verify-proof:
snarkjs groth16 verify ./gov-sig-setup/verification_key.json ./gov-sig-setup/public.json ./gov-sig-setup/proof.json

user-sig-verify-proof:
snarkjs groth16 verify ./user-sig-setup/verification_key.json ./user-sig-setup/public.json ./user-sig-setup/proof.json

gov-sig-export-verifier:
snarkjs zkey export solidityverifier ./gov-sig-setup/circuit_final.zkey ../contracts/src/circom-verifier/govSigVerifier.sol

user-sig-export-verifier:
snarkjs zkey export solidityverifier ./user-sig-setup/circuit_final.zkey ../contracts/src/circom-verifier/userSigVerifier.sol

gov-sig-generate-calldata:
snarkjs generatecall ./gov-sig-setup/public.json ./gov-sig-setup/proof.json >> ./gov-sig-build/solidity-input.json

user-sig-generate-calldata:
snarkjs generatecall ./user-sig-setup/public.json ./user-sig-setup/proof.json >> ./user-sig-build/solidity-input.json
build-main-registration-circuit:
circom src/main_myna_registration.circom --r1cs --wasm --sym -o ./build/main_myna_registration
node ./build/main_myna_registration/main_myna_registration_js/generate_witness.js ./build/main_myna_registration/main_myna_registration_js/main_myna_registration.wasm ./setup/main_myna_registration/input.json ./setup/main_myna_registration/witness.wtns

build-main-inclusion-circuit:
circom src/main_myna_inclusion.circom --r1cs --wasm --sym -o ./build/main_myna_inclusion
node ./build/main_myna_inclusion/main_myna_inclusion_js/generate_witness.js ./build/main_myna_inclusion/main_myna_inclusion_js/main_myna_inclusion.wasm ./setup/main_myna_inclusion/input.json ./setup/main_myna_inclusion/witness.wtns

build-gov-sig-circuit:
circom src/verify_gov_sig.circom --r1cs --wasm --sym -o ./build/gov_sig
node ./build/gov_sig/verify_gov_sig_js/generate_witness.js ./build/gov_sig/verify_gov_sig_js/verify_gov_sig.wasm ./setup/gov_sig/input.json ./setup/gov_sig/witness.wtns

build-user-sig-circuit:
circom src/verify_user_sig.circom --r1cs --wasm --sym -o ./build/user_sig
node ./build/user_sig/verify_user_sig_js/generate_witness.js ./build/user_sig/verify_user_sig_js/verify_user_sig.wasm ./setup/user_sig/input.json ./setup/user_sig/witness.wtns

setup-main-registration-groth16:
snarkjs groth16 setup ./build/main_myna_registration/main_myna_registration.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_registration/circuit_0000.zkey
snarkjs zkey contribute ./setup/main_myna_registration/circuit_0000.zkey ./setup/main_myna_registration/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./setup/main_myna_registration/circuit_0001.zkey ./setup/main_myna_registration/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./setup/main_myna_registration/circuit_0002.zkey ./setup/main_myna_registration/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./build/main_myna_registration/main_myna_registration.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_registration/circuit_0003.zkey
snarkjs zkey beacon ./setup/main_myna_registration/circuit_0003.zkey ./setup/main_myna_registration/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./build/main_myna_registration/main_myna_registration.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_registration/circuit_final.zkey
snarkjs zkey export verificationkey ./setup/main_myna_registration/circuit_final.zkey ./setup/main_myna_registration/verification_key.json

setup-main-inclusion-groth16:
snarkjs groth16 setup ./build/main_myna_inclusion/main_myna_inclusion.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_inclusion/circuit_0000.zkey
snarkjs zkey contribute ./setup/main_myna_inclusion/circuit_0000.zkey ./setup/main_myna_inclusion/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./setup/main_myna_inclusion/circuit_0001.zkey ./setup/main_myna_inclusion/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./setup/main_myna_inclusion/circuit_0002.zkey ./setup/main_myna_inclusion/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./build/main_myna_inclusion/main_myna_inclusion.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_inclusion/circuit_0003.zkey
snarkjs zkey beacon ./setup/main_myna_inclusion/circuit_0003.zkey ./setup/main_myna_inclusion/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./build/main_myna_inclusion/main_myna_inclusion.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/main_myna_inclusion/circuit_final.zkey
snarkjs zkey export verificationkey ./setup/main_myna_inclusion/circuit_final.zkey ./setup/main_myna_inclusion/verification_key.json

setup-gov-sig-groth16:
snarkjs groth16 setup ./build/gov_sig/verify_gov_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/gov_sig/circuit_0000.zkey
snarkjs zkey contribute ./setup/gov_sig/circuit_0000.zkey ./setup/gov_sig/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./setup/gov_sig/circuit_0001.zkey ./setup/gov_sig/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./setup/gov_sig/circuit_0002.zkey ./setup/gov_sig/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./build/gov_sig/verify_gov_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/gov_sig/circuit_0003.zkey
snarkjs zkey beacon ./setup/gov_sig/circuit_0003.zkey ./setup/gov_sig/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./build/gov_sig/verify_gov_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/gov_sig/circuit_final.zkey
snarkjs zkey export verificationkey ./setup/gov_sig/circuit_final.zkey ./setup/gov_sig/verification_key.json

setup-user-sig-groth16:
snarkjs groth16 setup ./build/user_sig/verify_user_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/user_sig/circuit_0000.zkey
snarkjs zkey contribute ./setup/user_sig/circuit_0000.zkey ./setup/user_sig/circuit_0001.zkey --name="1st Contributor Name 1" -v -e="1st random entropy"
snarkjs zkey contribute ./setup/user_sig/circuit_0001.zkey ./setup/user_sig/circuit_0002.zkey --name="2st Contributor Name 2" -v -e="2st random entropy"
snarkjs zkey contribute ./setup/user_sig/circuit_0002.zkey ./setup/user_sig/circuit_0003.zkey --name="3st Contributor Name 3" -v -e="3st random entropy"
snarkjs zkey verify ./build/user_sig/verify_user_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/user_sig/circuit_0003.zkey
snarkjs zkey beacon ./setup/user_sig/circuit_0003.zkey ./setup/user_sig/circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
snarkjs zkey verify ./build/user_sig/verify_user_sig.r1cs ./powersOfTau28_hez_final_20.ptau ./setup/user_sig/circuit_final.zkey
snarkjs zkey export verificationkey ./setup/user_sig/circuit_final.zkey ./setup/user_sig/verification_key.json

create-main-registration-proof:
snarkjs groth16 prove ./setup/main_myna_registration/circuit_final.zkey ./setup/main_myna_registration/witness.wtns ./setup/main_myna_registration/proof.json ./setup/main_myna_registration/public.json

create-main-inclusion-proof:
snarkjs groth16 prove ./setup/main_myna_inclusion/circuit_final.zkey ./setup/main_myna_inclusion/witness.wtns ./setup/main_myna_inclusion/proof.json ./setup/main_myna_inclusion/public.json

create-gov-sig-proof:
snarkjs groth16 prove ./setup/gov_sig/circuit_final.zkey ./setup/gov_sig/witness.wtns ./setup/gov_sig/proof.json ./setup/gov_sig/public.json

create-user-sig-proof:
snarkjs groth16 prove ./setup/user_sig/circuit_final.zkey ./setup/user_sig/witness.wtns ./setup/user_sig/proof.json ./setup/user_sig/public.json

verify-main-registration-proof:
snarkjs groth16 verify ./setup/main_myna_registration/verification_key.json ./setup/main_myna_registration/public.json ./setup/main_myna_registration/proof.json

verify-main-inclusion-proof:
snarkjs groth16 verify ./setup/main_myna_inclusion/verification_key.json ./setup/main_myna_inclusion/public.json ./setup/main_myna_inclusion/proof.json

verify-gov-sig-proof:
snarkjs groth16 verify ./setup/gov_sig/verification_key.json ./setup/gov_sig/public.json ./setup/gov_sig/proof.json

verify-user-sig-proof:
snarkjs groth16 verify ./setup/user_sig/verification_key.json ./setup/user_sig/public.json ./setup/user_sig/proof.json

export-main-registration-verifier:
snarkjs zkey export solidityverifier ./setup/main_myna_registration/circuit_final.zkey ../extention-contracts/src/circom-verifier/MainMynaRegistraionVerifier.sol

export-main-inclusion-verifier:
snarkjs zkey export solidityverifier ./setup/main_myna_inclusion/circuit_final.zkey ../extention-contracts/src/circom-verifier/MainMynaInclusionVerifier.sol

export-gov-sig-verifier:
snarkjs zkey export solidityverifier ./setup/gov_sig/circuit_final.zkey ../contracts/src/circom-verifier/GovSigVerifier.sol

export-user-sig-verifier:
snarkjs zkey export solidityverifier ./setup/user_sig/circuit_final.zkey ../contracts/src/circom-verifier/UserSigVerifier.sol

generate-main-registration-calldata:
snarkjs generatecall ./setup/main_myna_registration/public.json ./setup/main_myna_registration/proof.json >> ./build/main_myna_registration/solidity_input.json

generate-main-inclusion-calldata:
snarkjs generatecall ./setup/main_myna_inclusion/public.json ./setup/main_myna_inclusion/proof.json >> ./build/main_myna_inclusion/solidity_input.json

generate-gov-sig-calldata:
snarkjs generatecall ./setup/gov_sig/public.json ./setup/gov_sig/proof.json >> ./build/gov_sig/solidity_input.json

generate-user-sig-calldata:
snarkjs generatecall ./setup/user_sig/public.json ./setup/user_sig/proof.json >> ./build/user_sig/solidity_input.json
40 changes: 40 additions & 0 deletions packages/circom-circuit/src/helpers/MerkleInclusionProof.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pragma circom 2.0.0;

include "../../../../node_modules/circomlib/circuits/poseidon.circom";
include "../../../../node_modules/circomlib/circuits/mux1.circom";

template MerkleTreeInclusionProof(nLevels) {
signal input leaf;
signal input pathIndices[nLevels];
signal input siblings[nLevels];

signal output root;

component poseidons[nLevels];
component mux[nLevels];

signal hashes[nLevels + 1];
hashes[0] <== leaf;

for (var i = 0; i < nLevels; i++) {
pathIndices[i] * (1 - pathIndices[i]) === 0;

poseidons[i] = Poseidon(2);
mux[i] = MultiMux1(2);

mux[i].c[0][0] <== hashes[i];
mux[i].c[0][1] <== siblings[i];

mux[i].c[1][0] <== siblings[i];
mux[i].c[1][1] <== hashes[i];

mux[i].s <== pathIndices[i];

poseidons[i].inputs[0] <== mux[i].out[0];
poseidons[i].inputs[1] <== mux[i].out[1];

hashes[i + 1] <== poseidons[i].out;
}

root <== hashes[nLevels];
}
59 changes: 59 additions & 0 deletions packages/circom-circuit/src/main_myna_inclusion.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pragma circom 2.1.5;

include "../../../node_modules/circomlib/circuits/poseidon.circom";
include "./helpers/MerkleInclusionProof.circom";

template CalculateHash(k) {
signal input modulus[k];
signal input userSecret;
component poseidon = Poseidon(k-1);
component poseidon2 = Poseidon(3);

for (var i = 0; i < k - 1; i++) {
poseidon.inputs[i] <== modulus[i];
}
poseidon2.inputs[0] <== poseidon.out;
poseidon2.inputs[1] <== modulus[k-1];
poseidon2.inputs[2] <== userSecret;

signal output out <== poseidon2.out;
}

template MainMynaInclusion(nLevels, k) {
signal input userSecret;
signal input modulus[k];
signal input treePathIndices[nLevels];
signal input treeSiblings[nLevels];

component calculateHash = CalculateHash(k);
calculateHash.modulus <== modulus;
calculateHash.userSecret <== userSecret;
signal output identityCommitment <== calculateHash.out;

component hashUserSecret = Poseidon(1);
component hashModulus = Poseidon(k - 1);
component hashModulus2 = Poseidon(2);

hashUserSecret.inputs[0] <== userSecret;
for (var i = 0; i < k - 1; i++) {
hashModulus.inputs[i] <== modulus[i];
}
hashModulus2.inputs[0] <== hashModulus.out;
hashModulus2.inputs[1] <== modulus[k - 1];

component poseidon = Poseidon(2);
poseidon.inputs[0] <== hashModulus2.out;
poseidon.inputs[1] <== hashUserSecret.out;

signal leaf <== poseidon.out;

component mtp = MerkleTreeInclusionProof(nLevels);
mtp.leaf <== leaf;
for (var i = 0; i < nLevels; i++){
mtp.pathIndices[i] <== treePathIndices[i];
mtp.siblings[i] <== treeSiblings[i];
}
signal output root <== mtp.root;
}

component main = MainMynaInclusion(15, 17);
Loading