diff --git a/circuits/circuits/disclose.circom b/circuits/circuits/disclose.circom index 99ddcfe83..820474590 100644 --- a/circuits/circuits/disclose.circom +++ b/circuits/circuits/disclose.circom @@ -4,7 +4,7 @@ include "circomlib/circuits/poseidon.circom"; include "@zk-email/circuits/helpers/extract.circom"; include "./utils/isOlderThan.circom"; include "./utils/isValid.circom"; -include "./utils/binary-merkle-root.circom"; +include "binary-merkle-root.circom"; template Disclose(nLevels) { signal input secret; diff --git a/circuits/circuits/register_sha256WithRSAEncryption_65537.circom b/circuits/circuits/register_sha256WithRSAEncryption_65537.circom index 5ba989cc8..54ebc0232 100644 --- a/circuits/circuits/register_sha256WithRSAEncryption_65537.circom +++ b/circuits/circuits/register_sha256WithRSAEncryption_65537.circom @@ -5,7 +5,7 @@ include "@zk-email/circuits/helpers/extract.circom"; include "./passport_verifier_sha256WithRSAEncryption_65537.circom"; include "./utils/chunk_data.circom"; include "./utils/compute_pubkey_leaf.circom"; -include "./utils/binary-merkle-root.circom"; +include "binary-merkle-root.circom"; template Register_sha256WithRSAEncryption_65537(n, k, max_datahashes_bytes, nLevels, signatureAlgorithm) { signal input secret; diff --git a/circuits/circuits/utils/binary-merkle-root.circom b/circuits/circuits/utils/binary-merkle-root.circom deleted file mode 100644 index e169d3d52..000000000 --- a/circuits/circuits/utils/binary-merkle-root.circom +++ /dev/null @@ -1,43 +0,0 @@ -pragma circom 2.1.5; - -include "circomlib/circuits/poseidon.circom"; -include "circomlib/circuits/mux1.circom"; -include "circomlib/circuits/comparators.circom"; - -// This circuit is designed to calculate the root of a binary Merkle -// tree given a leaf, its depth, and the necessary sibling -// information (aka proof of membership). -// A circuit is designed without the capability to iterate through -// a dynamic array. To address this, a parameter with the static maximum -// tree depth is defined (i.e. 'MAX_DEPTH'). And additionally, the circuit -// receives a dynamic depth as an input, which is utilized in calculating the -// true root of the Merkle tree. The actual depth of the Merkle tree -// may be equal to or less than the static maximum depth. -template BinaryMerkleRoot(MAX_DEPTH) { - signal input leaf, depth, indices[MAX_DEPTH], siblings[MAX_DEPTH]; - - signal output out; - - signal nodes[MAX_DEPTH + 1]; - nodes[0] <== leaf; - - signal roots[MAX_DEPTH]; - var root = 0; - - for (var i = 0; i < MAX_DEPTH; i++) { - var isDepth = IsEqual()([depth, i]); - - roots[i] <== isDepth * nodes[i]; - - root += roots[i]; - - var c[2][2] = [ [nodes[i], siblings[i]], [siblings[i], nodes[i]] ]; - var childNodes[2] = MultiMux1(2)(c, indices[i]); - - nodes[i + 1] <== Poseidon(2)(childNodes); - } - - var isDepth = IsEqual()([depth, MAX_DEPTH]); - - out <== root + isDepth * nodes[MAX_DEPTH]; -} diff --git a/circuits/package.json b/circuits/package.json index c3f3bd942..3df13c5ef 100644 --- a/circuits/package.json +++ b/circuits/package.json @@ -13,6 +13,7 @@ "@zk-email/circuits": "^3.2.2", "@zk-email/helpers": "^3.1.3", "@zk-email/zk-regex-circom": "^1.2.1", + "@zk-kit/binary-merkle-root.circom": "^1.0.0-beta", "@zk-kit/circuits": "^1.0.0-beta", "@zk-kit/imt": "https://gitpkg.now.sh/0xturboblitz/zk-kit/packages/imt?6d417675", "chai-as-promised": "^7.1.1", @@ -34,4 +35,4 @@ "ts-mocha": "^10.0.0", "ts-node": "^10.9.2" } -} \ No newline at end of file +} diff --git a/circuits/scripts/build_disclose_circuit.sh b/circuits/scripts/build_disclose_circuit.sh index 20791b62d..e266804f7 100755 --- a/circuits/scripts/build_disclose_circuit.sh +++ b/circuits/scripts/build_disclose_circuit.sh @@ -23,7 +23,7 @@ fi cd .. echo "compiling circuit" -circom circuits/disclose.circom -l node_modules --r1cs --O1 --wasm -c --output build +circom circuits/disclose.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build echo "building zkey" yarn snarkjs groth16 setup build/disclose.r1cs build/powersOfTau28_hez_final_20.ptau build/disclose.zkey diff --git a/circuits/scripts/build_merkle_circuit.sh b/circuits/scripts/build_merkle_circuit.sh deleted file mode 100755 index 6d495ecaa..000000000 --- a/circuits/scripts/build_merkle_circuit.sh +++ /dev/null @@ -1,21 +0,0 @@ - -mkdir -p build -cd build -if [ ! -f powersOfTau28_hez_final_20.ptau ]; then - echo "Download power of tau...." - wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_20.ptau - echo "Finished download!" -else - echo "Powers of tau file already downloaded... Skip download action!" -fi -cd .. - -echo "compiling circuit" -circom circuits/merkle_tree/only_tree.circom --r1cs --sym --wasm --output build - -echo "building zkey" -yarn snarkjs groth16 setup build/only_tree.r1cs build/powersOfTau28_hez_final_20.ptau build/only_tree.zkey - -echo "building vkey" -echo "test random" | yarn snarkjs zkey contribute build/only_tree.zkey build/only_tree_final.zkey -yarn snarkjs zkey export verificationkey build/only_tree_final.zkey build/only_tree_verification_key.json \ No newline at end of file diff --git a/circuits/scripts/build_register_circuit.sh b/circuits/scripts/build_register_circuit.sh index c54cfb83b..c318f6fd5 100755 --- a/circuits/scripts/build_register_circuit.sh +++ b/circuits/scripts/build_register_circuit.sh @@ -23,7 +23,7 @@ fi cd .. echo "compiling circuit" -circom circuits/register_sha256WithRSAEncryption_65537.circom -l node_modules --r1cs --O1 --wasm -c --output build +circom circuits/register_sha256WithRSAEncryption_65537.circom -l node_modules -l ./node_modules/@zk-kit/binary-merkle-root.circom/src -l ./node_modules/circomlib/circuits --r1cs --O1 --wasm -c --output build echo "building zkey" yarn snarkjs groth16 setup build/register_sha256WithRSAEncryption_65537.r1cs build/powersOfTau28_hez_final_20.ptau build/register_sha256WithRSAEncryption_65537.zkey diff --git a/circuits/yarn.lock b/circuits/yarn.lock index 72d6c0832..85907f9f9 100644 --- a/circuits/yarn.lock +++ b/circuits/yarn.lock @@ -516,6 +516,13 @@ commander "^11.0.0" snarkjs "^0.7.0" +"@zk-kit/binary-merkle-root.circom@^1.0.0-beta": + version "1.0.0-beta" + resolved "https://registry.yarnpkg.com/@zk-kit/binary-merkle-root.circom/-/binary-merkle-root.circom-1.0.0-beta.tgz#1124840ff3d0af8c28ad4d9ee5004d41f6768978" + integrity sha512-yj8bPpYWNjmk3DLIv9zyIyZ0WxTWKuYs1BxEDOCNKBmk6Gw4FtTcAsppXAF4gkqkvU29hhG/naaOuz0SGQSCFg== + dependencies: + circomlib "^2.0.5" + "@zk-kit/circuits@^1.0.0-beta": version "1.0.0-beta" resolved "https://registry.yarnpkg.com/@zk-kit/circuits/-/circuits-1.0.0-beta.tgz#4f41315839855762dac11b2ba2ce5e58fd8ad1e9"