From 77381ba344f7c2964be76dded55ee13053c3bc4c Mon Sep 17 00:00:00 2001 From: drewstone Date: Thu, 8 Jul 2021 11:26:36 -0400 Subject: [PATCH] Develop to master: BN254 & more poseidon fns (#166) * Add EVM webb anchor contracts w/ CHAIN_ID (#123) * Adds merkle extension directory (#124) * Add EVM webb anchor contracts w/ CHAIN_ID * zkutil script * Add old evm files and wasm project for merkle extension * update toml * Add yarn lock * Upload to crates (#120) * Added orml pallets * Update config and types * Added native currency id to config * Runtime config, benchmark errors * Testing mixer with non-native currency * Updated deps, remove wasm-utils * Using merkle from crates * Return back to local merkle dep * Improve events (#122) Co-authored-by: Shady Khalifa * Multiasset integration (#119) * Added orml pallets * Update config and types * Added native currency id to config * Runtime config, benchmark errors * Testing mixer with non-native currency * Making create_new non-dispatchable Co-authored-by: Drew Stone * Remove chain id * Renames group to tree (#126) * Renames group to tree * Rename more * Fix naming err * Update pallets/mixer/src/lib.rs Co-authored-by: Filip Lazovic Co-authored-by: Filip Lazovic * Update rust * Give basic weight to create_new * Add RPC `merkle.treeLeaves` to `Merkle` pallet (#129) * remove yarn.lock * add rpc crate * add merkle rpc impl * fix some tests * Update runtime/src/lib.rs * Update pallets/merkle/src/lib.rs * Update pallets/merkle/src/lib.rs * Update pallets/merkle/rpc/src/lib.rs Co-authored-by: Drew Stone * Chain id removal * New Github actions and Dockerfile (#131) * github actions and Dockerfile * dockerignore * fmt * cache lints * disable clippy for now * add build to the CI * remove old actions * no-override * Fix docker build (#132) * fix image name * New token pallet (#134) * Updates * Updates * Merge pallet_assets w/ orml_tokens * Pallets compile, node doesn't * [wip] comment out frontier for now * Comment out EVM/Frontier, get node compiling * remove frontier service file from git * Newlines * ups (#133) * Test updates * Fix merge account * Fix no_op test * Updates, dust test not working * Fix all tests Co-authored-by: Shady Khalifa * Adds frontier back (#135) * Updates * Updates * Merge pallet_assets w/ orml_tokens * Pallets compile, node doesn't * [wip] comment out frontier for now * Comment out EVM/Frontier, get node compiling * remove frontier service file from git * Newlines * ups (#133) * Test updates * Fix merge account * Fix no_op test * Updates, dust test not working * Fix all tests * Toml updates * Adds frontier, mocks orml for as new local packages * Add all rpc methods back, fix service * Change to webb node from template * Organising * Optional rpc in toml * Fix conflict * Update tests and mocks * Reverts Co-authored-by: Shady Khalifa * fix failing tests * Update contracts, add mocks * Add ts-tests from edgeware/frontier for local anon testing * Tsconfig, package.json update * ups * Add edgeware types back * Add truffle config * Update to master substrate (#137) * Update to master substrate * Fix benchmarks and tests * Formatting/lint * switch to ubuntu as base docker image * Add anchor (#140) * Update README.md * Update README.md (#125) * Update build.sh * Develop to master merge (#136) * Add EVM webb anchor contracts w/ CHAIN_ID (#123) * Adds merkle extension directory (#124) * Add EVM webb anchor contracts w/ CHAIN_ID * zkutil script * Add old evm files and wasm project for merkle extension * update toml * Add yarn lock * Upload to crates (#120) * Added orml pallets * Update config and types * Added native currency id to config * Runtime config, benchmark errors * Testing mixer with non-native currency * Updated deps, remove wasm-utils * Using merkle from crates * Return back to local merkle dep * Improve events (#122) Co-authored-by: Shady Khalifa * Multiasset integration (#119) * Added orml pallets * Update config and types * Added native currency id to config * Runtime config, benchmark errors * Testing mixer with non-native currency * Making create_new non-dispatchable Co-authored-by: Drew Stone * Remove chain id * Renames group to tree (#126) * Renames group to tree * Rename more * Fix naming err * Update pallets/mixer/src/lib.rs Co-authored-by: Filip Lazovic Co-authored-by: Filip Lazovic * Update rust * Give basic weight to create_new * Add RPC `merkle.treeLeaves` to `Merkle` pallet (#129) * remove yarn.lock * add rpc crate * add merkle rpc impl * fix some tests * Update runtime/src/lib.rs * Update pallets/merkle/src/lib.rs * Update pallets/merkle/src/lib.rs * Update pallets/merkle/rpc/src/lib.rs Co-authored-by: Drew Stone * Chain id removal * New Github actions and Dockerfile (#131) * github actions and Dockerfile * dockerignore * fmt * cache lints * disable clippy for now * add build to the CI * remove old actions * no-override * Fix docker build (#132) * fix image name * New token pallet (#134) * Updates * Updates * Merge pallet_assets w/ orml_tokens * Pallets compile, node doesn't * [wip] comment out frontier for now * Comment out EVM/Frontier, get node compiling * remove frontier service file from git * Newlines * ups (#133) * Test updates * Fix merge account * Fix no_op test * Updates, dust test not working * Fix all tests Co-authored-by: Shady Khalifa * Adds frontier back (#135) * Updates * Updates * Merge pallet_assets w/ orml_tokens * Pallets compile, node doesn't * [wip] comment out frontier for now * Comment out EVM/Frontier, get node compiling * remove frontier service file from git * Newlines * ups (#133) * Test updates * Fix merge account * Fix no_op test * Updates, dust test not working * Fix all tests * Toml updates * Adds frontier, mocks orml for as new local packages * Add all rpc methods back, fix service * Change to webb node from template * Organising * Optional rpc in toml * Fix conflict * Update tests and mocks * Reverts Co-authored-by: Shady Khalifa * fix failing tests * Update contracts, add mocks * Add ts-tests from edgeware/frontier for local anon testing * Tsconfig, package.json update * ups * Add edgeware types back * Add truffle config * Update to master substrate (#137) * Update to master substrate * Fix benchmarks and tests * Formatting/lint Co-authored-by: Filip Lazovic Co-authored-by: Shady Khalifa * Update README.md * Update README.md * Add anchor * Remove old anchor, replace with new submodule * Update submodule * Attempting to pass test for circom on locally generated keys * Get evm tests passing with solidity 0.7.6 on ganache * Change evm contracts readme * Link git submodule for anchor contracts and types to anon repo branch add-anchor * Add contract ABIs for a locally generated circuit * Update contracts ABI from build folder, still ignore build/circuits * Remove random seed * Update README.md * Update README.md (#125) * Update build.sh * Update README.md * Update README.md * Add anchor * Remove old anchor, replace with new submodule * Update submodule * Attempting to pass test for circom on locally generated keys * Get evm tests passing with solidity 0.7.6 on ganache * Change evm contracts readme * Link git submodule for anchor contracts and types to anon repo branch add-anchor * Add contract ABIs for a locally generated circuit * Update contracts ABI from build folder, still ignore build/circuits * Remove random seed * Fix service Co-authored-by: Filip Lazovic Co-authored-by: Shady Khalifa Co-authored-by: Nathan Barnavon * Update * Update toml/lock * Ups frontier * Up lock * Fix readme * rename scripts to use webb-node (#141) * Merkle bytes interface (#144) * Moving zk logic outside the pallet * Moving bulletproofs verification outside the pallet * Using bytes vec for inputs * Updated mixer with new types * Fixed tests * Updated job hooks, ran cargo fmt * Fix compile errors * Removed unused hasher * Bring back hooks * Merkle arkworks support (#145) * Updated job hooks, ran cargo fmt * Bring back hooks * Compiling to no_std * Added arkworks hashing * Error handling improvements * Cargo fmt * Updated cryoto-primitives * Compiled * Update zero tree gen, types * Updated verify function * Passing test * Fixed mixer * Moved to mixer gadget * Using pallet Error enum * Updates after review part 1 * Update after review part 2 * Rename args, hasher.rs -> setup.rs * Added more tests * Remove ts-tests * Bridge pallet stubbed implementation (#138) * Update traits for bridge, add bridge wrap fn * stub deposit * Unwrap fn for webb wrapped assets on bridge * More updates * Stub out more functions * Tidy up mock * Change mixer to anchor in bridge docs * Update branch in bridge toml * use webb-tools/substrate fork * Updates frontier dependency (#146) * Update and add new inherent * Update * Update lock * Ups * Merge develop * Updates * Fixes Co-authored-by: Shady Khalifa * Verify guard (#147) * Added verification guard * Updated verifying key storage * Update verification key storage (#149) * Fix bridge mock * Update verification key flow * Add pallet functions * Function comment update * Key storage - Storing verifying key data in runtime using 2-step initialization. (#157) * Storing keys in runtime storage, [wip] * Fix tests, get_mixer requires initialized trees * Update initialization with new pallet function * Updates to key storage - master merge (#159) * Merge master * Remove contracts * Ups * Upds * Fmt * Remove warnings for bridge until implemented * Remove options * Fmt * Bulletproofs precompiles (#148) * Added precompiles * Using chacha * cargo fmt * Added tests for invalid casees * Add precompile test * Added verify directly test * Fix comments * Update frontier fork, substrate deps, rebuild/receive same error * Toml updates * More attempts * Compiles * Merge dev * fmt Co-authored-by: Filip Lazovic * Fixes to the CI / benchmarking & cleanup of unused params (#162) * Fix compile issues * Ups * Build benchmarks works * Build mixer benchmarks * Fmt * Fix test script * Add pallet-assets support for a mixer (#163) * Add pallet-assets support for a mixer * Add pallet * Fmt * Fix * Add bn254 support (#165) * Add bn254 * fmt * fix tests * Fmt * lock update Co-authored-by: Filip Lazovic Co-authored-by: Shady Khalifa Co-authored-by: Nathan Barnavon Co-authored-by: nepoche --- Cargo.lock | 137 +++++++++++++---------------- pallets/merkle/Cargo.toml | 14 ++- pallets/merkle/src/tests.rs | 69 ++++++++------- pallets/merkle/src/utils/mod.rs | 2 + pallets/merkle/src/utils/setup.rs | 139 +++++++++++++++++++++++++++--- 5 files changed, 242 insertions(+), 119 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ae2401..344b1aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,9 +142,9 @@ dependencies = [ [[package]] name = "ark-bls12-381" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70e1c2ad76c4f725520440b981df3ce2d635f2baa1122750c757c0cf0f3d4b74" +checksum = "65be532f9dd1e98ad0150b037276cde464c6f371059e6dd02c0222395761f6aa" dependencies = [ "ark-ec", "ark-ff", @@ -153,9 +153,9 @@ dependencies = [ [[package]] name = "ark-bn254" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ead066869de5e8cb2938123204d1572f09496b629e146a6f80fa8ec508446ba" +checksum = "ea691771ebbb28aea556c044e2e5c5227398d840cee0c34d4d20fa8eb2689e8c" dependencies = [ "ark-ec", "ark-ff", @@ -164,42 +164,29 @@ dependencies = [ [[package]] name = "ark-crypto-primitives" -version = "0.2.0" -source = "git+https://github.com/webb-tools/crypto-primitives?rev=e8cd8bf#e8cd8bfe0ac13d4d308b5ea94833bcdad1ad1a9e" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff773c0ef8c655c98071d3026a63950798a66b2f45baef22d8334c1756f1bd18" dependencies = [ "ark-ec", "ark-ff", "ark-nonnative-field", "ark-r1cs-std", "ark-relations", + "ark-serialize", "ark-snark", "ark-std", "blake2", "derivative", "digest 0.9.0", -] - -[[package]] -name = "ark-crypto-primitives" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74b83a7e125e5c611e4a997123effb2f02e3fbc66531dd77751d3016ee920741" -dependencies = [ - "ark-ec", - "ark-ff", - "ark-relations", - "ark-snark", - "ark-std", - "blake2", - "derivative", - "digest 0.9.0", + "tracing", ] [[package]] name = "ark-ec" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c56006994f509d76fbce6f6ffe3108f7191b4f3754ecd00bbae7cac20ec05020" +checksum = "dea978406c4b1ca13c2db2373b05cc55429c3575b8b21f1b9ee859aa5b03dd42" dependencies = [ "ark-ff", "ark-serialize", @@ -211,9 +198,9 @@ dependencies = [ [[package]] name = "ark-ed-on-bls12-381" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08d948a004fc5d81d3b5cf2ac5e5a7ca106255ff222faec16e34d291fd79d86" +checksum = "43b7ada17db3854f5994e74e60b18e10e818594935ee7e1d329800c117b32970" dependencies = [ "ark-bls12-381", "ark-ec", @@ -224,9 +211,9 @@ dependencies = [ [[package]] name = "ark-ed-on-bn254" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4788967dece3adba9adec5dcc675c711a39418dd4aaf197b0eecc6c810a0bf18" +checksum = "7fdc786b806fdbff4abebb08ec2fcb50cfe3941918e57120ab121228452903fd" dependencies = [ "ark-bn254", "ark-ec", @@ -237,25 +224,27 @@ dependencies = [ [[package]] name = "ark-ff" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4d8802d40fce9212c5c09be08f75c4b3becc0c488e87f60fff787b01250ce33" +checksum = "6b3235cc41ee7a12aaaf2c575a2ad7b46713a8a50bda2fc3b003a04845c05dd6" dependencies = [ "ark-ff-asm", "ark-ff-macros", "ark-serialize", "ark-std", "derivative", + "num-bigint 0.4.0", "num-traits", + "paste", "rustc_version 0.3.3", "zeroize", ] [[package]] name = "ark-ff-asm" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e8cb28c2137af1ef058aa59616db3f7df67dbb70bf2be4ee6920008cc30d98c" +checksum = "db02d390bf6643fb404d3d22d31aee1c4bc4459600aef9113833d17e786c6e44" dependencies = [ "quote", "syn", @@ -263,9 +252,9 @@ dependencies = [ [[package]] name = "ark-ff-macros" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9c256a93a10ed9708c16a517d6dcfaba3d215c0d7fab44d29a9affefb5eeb8" +checksum = "db2fd794a08ccb318058009eefdf15bcaaaaf6f8161eb3345f907222bac38b20" dependencies = [ "num-bigint 0.4.0", "num-traits", @@ -275,11 +264,11 @@ dependencies = [ [[package]] name = "ark-groth16" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba848e856c96bc15eb3f68359cd8ea92747eb2be35a177a25e780d307069f38" +checksum = "38f8fff7468e947130b5caf9bdd27de8b913cf30e15104b4f0cd301726b3d897" dependencies = [ - "ark-crypto-primitives 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ark-crypto-primitives", "ark-ec", "ark-ff", "ark-poly", @@ -290,9 +279,9 @@ dependencies = [ [[package]] name = "ark-marlin" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eea366fcce1487bd8f1bee281fbc9de1d7bce353a89ace8eee6ebc9d65e2d2" +checksum = "caa8510faa8e64f0a6841ee4b58efe2d56f7a80d86fa0ce9891bbb3aa20166d9" dependencies = [ "ark-ff", "ark-poly", @@ -302,14 +291,14 @@ dependencies = [ "ark-std", "derivative", "digest 0.9.0", - "rand_chacha 0.2.2", + "rand_chacha 0.3.1", ] [[package]] name = "ark-nonnative-field" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17887af156e9911d1dba5b30d49256d508f82f6a4f765a6fad8b5c637b700353" +checksum = "440ad4569974910adbeb84422b7e622b79e08d27142afd113785b7fcfb446186" dependencies = [ "ark-ec", "ark-ff", @@ -325,9 +314,9 @@ dependencies = [ [[package]] name = "ark-poly" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72d6683d21645a2abb94034f6a14e708405e55d9597687952d54b2269922857a" +checksum = "7b0f78f47537c2f15706db7e98fe64cc1711dbf9def81218194e17239e53e5aa" dependencies = [ "ark-ff", "ark-serialize", @@ -338,9 +327,9 @@ dependencies = [ [[package]] name = "ark-poly-commit" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad7df2d50062d718e5914d013ee2f30dc203324e08070d16b241e869b8717ed6" +checksum = "a71ddfa72bad1446cab7bbecb6018dbbdc9abcbc3a0065483ae5186ad2a64dcd" dependencies = [ "ark-ec", "ark-ff", @@ -354,9 +343,9 @@ dependencies = [ [[package]] name = "ark-r1cs-std" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a90fea2b84ae4443983d56540360ea004cab952292b7a6535798b6b9dcb7f41" +checksum = "22e8fdacb1931f238a0d866ced1e916a49d36de832fd8b83dc916b718ae72893" dependencies = [ "ark-ec", "ark-ff", @@ -370,9 +359,9 @@ dependencies = [ [[package]] name = "ark-relations" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a42f124f8dfff2b0561143c0c7ea48d7f7dc8d2c4c1e87eca14a27430c653c0b" +checksum = "4cba4c1c99792a6834bd97f7fd76578ec2cd58d2afc5139a17e1d1bec65b38f6" dependencies = [ "ark-ff", "ark-std", @@ -381,19 +370,20 @@ dependencies = [ [[package]] name = "ark-serialize" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3e9b59329dc9b92086b3dc619f31cef4a0c802f10829b575a3666d48a48387d" +checksum = "1d6c2b318ee6e10f8c2853e73a83adc0ccb88995aa978d8a3408d492ab2ee671" dependencies = [ "ark-serialize-derive", "ark-std", + "digest 0.9.0", ] [[package]] name = "ark-serialize-derive" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ac3d78c750b01f5df5b2e76d106ed31487a93b3868f14a7f0eb3a74f45e1d8a" +checksum = "8dd4e5f0bf8285d5ed538d27fab7411f3e297908fd93c62195de8bee3f199e82" dependencies = [ "proc-macro2", "quote", @@ -402,9 +392,9 @@ dependencies = [ [[package]] name = "ark-snark" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39da26432fe584b0010741299820145ec69180fe9ea18ddf96946932763624a1" +checksum = "0dc3dff1a5f67a9c0b34df32b079752d8dd17f1e9d06253da0453db6c1b7cc8a" dependencies = [ "ark-ff", "ark-relations", @@ -413,21 +403,23 @@ dependencies = [ [[package]] name = "ark-std" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5b856a29bea7b810858116a596beee3d20fc4c5aeb240e8e5a8bca4845a470" +checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" dependencies = [ - "rand 0.7.3", - "rand_xorshift", + "num-traits", + "rand 0.8.4", ] [[package]] name = "arkworks-gadgets" -version = "0.1.0" -source = "git+https://github.com/webb-tools/arkworks-gadgets?rev=ed39192#ed39192d258b027d5623c695325ac5988a943442" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29eb1c5dfacecbbb1580f37c1bc4541e5256b5552d7c54ee3c39ddd9af74f98c" dependencies = [ "ark-bls12-381", - "ark-crypto-primitives 0.2.0 (git+https://github.com/webb-tools/crypto-primitives?rev=e8cd8bf)", + "ark-bn254", + "ark-crypto-primitives", "ark-ec", "ark-ed-on-bls12-381", "ark-ed-on-bn254", @@ -439,6 +431,7 @@ dependencies = [ "ark-r1cs-std", "ark-relations", "ark-serialize", + "ark-snark", "ark-std", "blake2", "digest 0.9.0", @@ -3425,9 +3418,9 @@ checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" [[package]] name = "libc" -version = "0.2.97" +version = "0.2.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" [[package]] name = "libloading" @@ -4863,6 +4856,7 @@ dependencies = [ name = "pallet-merkle" version = "3.0.0" dependencies = [ + "ark-crypto-primitives", "ark-groth16", "ark-serialize", "arkworks-gadgets", @@ -5520,9 +5514,9 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "primitive-types" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2415937401cb030a2a0a4d922483f945fa068f52a7dbb22ce0fe5f2b6f6adace" +checksum = "06345ee39fbccfb06ab45f3a1a5798d9dafa04cb8921a76d227040003a234b0e" dependencies = [ "fixed-hash", "impl-codec", @@ -5863,15 +5857,6 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "rand_xorshift" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77d416b86801d23dde1aa643023b775c3a462efc0ed96443add11546cdf1dca8" -dependencies = [ - "rand_core 0.5.1", -] - [[package]] name = "rawpointer" version = "0.2.1" diff --git a/pallets/merkle/Cargo.toml b/pallets/merkle/Cargo.toml index 58812e6..a10c984 100644 --- a/pallets/merkle/Cargo.toml +++ b/pallets/merkle/Cargo.toml @@ -27,9 +27,17 @@ sp-io = { default-features = false, version = "3.0.0", git = "https://github.com frame-benchmarking = { default-features = false, version = "3.0.0", optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.6" } lazy_static = { version = "1.4.0", features = ["spin_no_std"] } -arkworks-gadgets = { git = "https://github.com/webb-tools/arkworks-gadgets", rev = "ed39192", features = ["r1cs"], default-features = false } -ark-groth16 = {version = "^0.2.0", default-features = false } -ark-serialize = {version = "^0.2.0", default-features = false } +ark-crypto-primitives = { version = "^0.3.0", features = ["r1cs"], default-features = false } +ark-groth16 = {version = "^0.3.0", default-features = false } +ark-serialize = {version = "^0.3.0", default-features = false } + +[dependencies.arkworks-gadgets] +version = "0.2.5" +default-features = false +features = [ + "r1cs", + "default_poseidon", +] [dependencies.curve25519-dalek] version = "3.0.0" diff --git a/pallets/merkle/src/tests.rs b/pallets/merkle/src/tests.rs index 9daef02..6ed2ff6 100644 --- a/pallets/merkle/src/tests.rs +++ b/pallets/merkle/src/tests.rs @@ -8,8 +8,12 @@ use crate::{ }; use ark_serialize::CanonicalSerialize; use arkworks_gadgets::{ - prelude::{ark_bls12_381::Fr as Bls381, ark_ff::to_bytes}, - setup::mixer::{prove_groth16, setup_circuit, setup_random_groth16}, + ark_std::test_rng, + prelude::{ + ark_bls12_381::{Bls12_381, Fr as Bls381}, + ark_ff::to_bytes, + }, + setup::mixer::{prove_groth16_x5, setup_circuit_x5, setup_random_groth16_x5}, }; use bulletproofs::{r1cs::Prover, BulletproofGens, PedersenGens}; use bulletproofs_gadgets::{ @@ -24,8 +28,7 @@ use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar}; use frame_support::{assert_err, assert_ok, traits::UnfilteredDispatchable}; use frame_system::RawOrigin; use merlin::Transcript; -use rand_chacha::ChaChaRng; -use rand_core::OsRng; +use rand_chacha::rand_core::OsRng; use sp_runtime::traits::BadOrigin; fn key_bytes(x: u8) -> [u8; 32] { @@ -1147,11 +1150,12 @@ fn should_verify_large_zk_proof_of_membership() { #[test] fn should_verify_simple_zk_proof_of_membership_arkworks() { new_test_ext().execute_with(|| { - let mut rng = OsRng::default(); + let mut rng = test_rng(); + let curve = arkworks_gadgets::setup::common::Curve::Bls381; let recipient = Bls381::from(0u8); let relayer = Bls381::from(0u8); let leaves = Vec::new(); - let (circuit, leaf, nullifier, root, _) = setup_circuit(&leaves, 0, recipient, relayer, &mut rng); + let (circuit, leaf, nullifier, root, _) = setup_circuit_x5(&leaves, 0, recipient, relayer, &mut rng, curve); let leaf_bytes = to_bytes![leaf].unwrap(); let hasher = HashFunction::PoseidonDefault; @@ -1164,7 +1168,7 @@ fn should_verify_simple_zk_proof_of_membership_arkworks() { Some(30), )); - let (pk, vk) = setup_random_groth16(&mut rng); + let (pk, vk) = setup_random_groth16_x5::<_, Bls12_381>(&mut rng, curve); let mut vk_bytes = Vec::new(); vk.serialize(&mut vk_bytes).unwrap(); @@ -1182,7 +1186,7 @@ fn should_verify_simple_zk_proof_of_membership_arkworks() { let relayer_bytes = to_bytes![relayer].unwrap(); let nullifier_bytes = to_bytes![nullifier].unwrap(); - let proof = prove_groth16(&pk, circuit.clone(), &mut rng); + let proof = prove_groth16_x5(&pk, circuit.clone(), &mut rng); let mut proof_bytes = vec![0u8; proof.serialized_size()]; proof.serialize(&mut proof_bytes[..]).unwrap(); @@ -1204,11 +1208,12 @@ fn should_verify_simple_zk_proof_of_membership_arkworks() { #[test] fn should_fail_to_verify_empty_public_inputs_arkworks() { new_test_ext().execute_with(|| { - let mut rng = OsRng::default(); + let mut rng = test_rng(); + let curve = arkworks_gadgets::setup::common::Curve::Bls381; let recipient = Bls381::from(0u8); let relayer = Bls381::from(0u8); let leaves = Vec::new(); - let (circuit, leaf, nullifier, root, _) = setup_circuit(&leaves, 0, recipient, relayer, &mut rng); + let (circuit, leaf, nullifier, root, _) = setup_circuit_x5(&leaves, 0, recipient, relayer, &mut rng, curve); let leaf_bytes = to_bytes![leaf].unwrap(); let hasher = HashFunction::PoseidonDefault; @@ -1221,7 +1226,7 @@ fn should_fail_to_verify_empty_public_inputs_arkworks() { Some(30), )); - let (pk, vk) = setup_random_groth16(&mut rng); + let (pk, vk) = setup_random_groth16_x5::<_, Bls12_381>(&mut rng, curve); let mut vk_bytes = Vec::new(); vk.serialize(&mut vk_bytes).unwrap(); @@ -1239,7 +1244,7 @@ fn should_fail_to_verify_empty_public_inputs_arkworks() { let relayer_bytes = to_bytes![relayer].unwrap(); let nullifier_bytes = to_bytes![nullifier].unwrap(); - let proof = prove_groth16(&pk, circuit.clone(), &mut rng); + let proof = prove_groth16_x5(&pk, circuit.clone(), &mut rng); let mut proof_bytes = vec![0u8; proof.serialized_size()]; proof.serialize(&mut proof_bytes[..]).unwrap(); @@ -1297,13 +1302,14 @@ fn should_fail_to_verify_empty_public_inputs_arkworks() { } #[test] -fn should_fail_to_verify_invalid_public_inputs_arkworks() { +fn should_fail_to_verify_arkworks() { new_test_ext().execute_with(|| { - let mut rng = OsRng::default(); + let mut rng = test_rng(); + let curve = arkworks_gadgets::setup::common::Curve::Bls381; let recipient = Bls381::from(0u8); let relayer = Bls381::from(0u8); let leaves = Vec::new(); - let (circuit, leaf, nullifier, root, _) = setup_circuit(&leaves, 0, recipient, relayer, &mut rng); + let (circuit, leaf, nullifier, root, _) = setup_circuit_x5(&leaves, 0, recipient, relayer, &mut rng, curve); let leaf_bytes = to_bytes![leaf].unwrap(); let hasher = HashFunction::PoseidonDefault; @@ -1316,7 +1322,7 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { Some(30), )); - let (pk, vk) = setup_random_groth16(&mut rng); + let (pk, vk) = setup_random_groth16_x5::<_, Bls12_381>(&mut rng, curve); let mut vk_bytes = Vec::new(); vk.serialize(&mut vk_bytes).unwrap(); @@ -1334,12 +1340,13 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { let relayer_bytes = to_bytes![relayer].unwrap(); let nullifier_bytes = to_bytes![nullifier].unwrap(); - let proof = prove_groth16(&pk, circuit.clone(), &mut rng); + let proof = prove_groth16_x5(&pk, circuit.clone(), &mut rng); let mut proof_bytes = vec![0u8; proof.serialized_size()]; proof.serialize(&mut proof_bytes[..]).unwrap(); let mut invalid_nullifier = nullifier_bytes.clone(); - invalid_nullifier.push(0u8); + invalid_nullifier[0] = 1u8; + invalid_nullifier.push(1u8); assert_err!( MerkleTrees::verify_zk( 0, @@ -1354,11 +1361,12 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { recipient_bytes.clone(), relayer_bytes.clone(), ), - Error::::InvalidPublicInputs + Error::::ZkVerificationFailed ); let mut invalid_recipient = recipient_bytes.clone(); - invalid_recipient.push(0u8); + invalid_recipient[0] = (1u8); + invalid_recipient.push(1u8); assert_err!( MerkleTrees::verify_zk( 0, @@ -1373,11 +1381,12 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { invalid_recipient, relayer_bytes.clone(), ), - Error::::InvalidPublicInputs + Error::::ZkVerificationFailed ); let mut invalid_relayer = recipient_bytes.clone(); - invalid_relayer.push(0u8); + invalid_relayer[0] = 1u8; + invalid_relayer.push(1u8); assert_err!( MerkleTrees::verify_zk( 0, @@ -1392,7 +1401,7 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { // Invalid replayer bytes invalid_relayer, ), - Error::::InvalidPublicInputs + Error::::ZkVerificationFailed ); }); } @@ -1400,11 +1409,12 @@ fn should_fail_to_verify_invalid_public_inputs_arkworks() { #[test] fn should_fail_to_add_leaf_without_a_key_arkworks() { new_test_ext().execute_with(|| { - let mut rng = OsRng::default(); + let mut rng = test_rng(); + let curve = arkworks_gadgets::setup::common::Curve::Bls381; let recipient = Bls381::from(0u8); let relayer = Bls381::from(0u8); let leaves = Vec::new(); - let (_, leaf, ..) = setup_circuit(&leaves, 0, recipient, relayer, &mut rng); + let (_, leaf, ..) = setup_circuit_x5(&leaves, 0, recipient, relayer, &mut rng, curve); let leaf_bytes = to_bytes![leaf].unwrap(); let hasher = HashFunction::PoseidonDefault; @@ -1427,11 +1437,12 @@ fn should_fail_to_add_leaf_without_a_key_arkworks() { #[test] fn should_fail_to_verify_with_invalid_key_arkworks() { new_test_ext().execute_with(|| { - let mut rng = OsRng::default(); + let mut rng = test_rng(); + let curve = arkworks_gadgets::setup::common::Curve::Bls381; let recipient = Bls381::from(0u8); let relayer = Bls381::from(0u8); let leaves = Vec::new(); - let (circuit, leaf, nullifier, root, _) = setup_circuit(&leaves, 0, recipient, relayer, &mut rng); + let (circuit, leaf, nullifier, root, _) = setup_circuit_x5(&leaves, 0, recipient, relayer, &mut rng, curve); let leaf_bytes = to_bytes![leaf].unwrap(); let hasher = HashFunction::PoseidonDefault; @@ -1444,7 +1455,7 @@ fn should_fail_to_verify_with_invalid_key_arkworks() { Some(30), )); - let (pk, vk) = setup_random_groth16(&mut rng); + let (pk, vk) = setup_random_groth16_x5::<_, Bls12_381>(&mut rng, curve); let mut vk_bytes = Vec::new(); vk.serialize(&mut vk_bytes).unwrap(); // pushing invalid byte @@ -1463,7 +1474,7 @@ fn should_fail_to_verify_with_invalid_key_arkworks() { let relayer_bytes = to_bytes![relayer].unwrap(); let nullifier_bytes = to_bytes![nullifier].unwrap(); - let proof = prove_groth16(&pk, circuit.clone(), &mut rng); + let proof = prove_groth16_x5(&pk, circuit.clone(), &mut rng); let mut proof_bytes = vec![0u8; proof.serialized_size()]; proof.serialize(&mut proof_bytes[..]).unwrap(); diff --git a/pallets/merkle/src/utils/mod.rs b/pallets/merkle/src/utils/mod.rs index 73f45f2..dc1fccd 100644 --- a/pallets/merkle/src/utils/mod.rs +++ b/pallets/merkle/src/utils/mod.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + //! Utility functions and data type definitions pub mod keys; diff --git a/pallets/merkle/src/utils/setup.rs b/pallets/merkle/src/utils/setup.rs index ceca7e1..b22a533 100644 --- a/pallets/merkle/src/utils/setup.rs +++ b/pallets/merkle/src/utils/setup.rs @@ -2,22 +2,25 @@ use crate::{ utils::keys::{from_bytes_to_bp_gens, slice_to_bytes_32, ScalarBytes}, Config, Error, }; +use ark_crypto_primitives::CRH as CRHTrait; use ark_groth16::{Proof, VerifyingKey}; use ark_serialize::CanonicalDeserialize; use arkworks_gadgets::{ merkle_tree::gen_empty_hashes, + poseidon::PoseidonParameters, prelude::{ ark_bls12_381::{Bls12_381, Fr as Bls381}, + ark_bn254::{Bn254, Fr as Bn254Fr}, ark_ff::to_bytes, - webb_crypto_primitives::{ - crh::{poseidon::PoseidonParameters, CRH}, - to_field_elements, - }, }, setup::{ - common::{setup_params_3, verify_groth16, PoseidonCRH3, TreeConfig}, + common::{ + setup_params_x17_3, setup_params_x5_3, verify_groth16, Curve as CurveEnum, PoseidonCRH_x17_3, + PoseidonCRH_x5_3, TreeConfig_x17, TreeConfig_x5, + }, mixer::get_public_inputs, }, + utils::to_field_elements, }; use bulletproofs::{ r1cs::{R1CSProof, Verifier}, @@ -42,7 +45,14 @@ use rand_chacha::{rand_core::SeedableRng, ChaChaRng}; use sp_std::prelude::*; lazy_static! { - static ref DEFAULT_ARKWORKS_POSEIDON_PARAMETERS: PoseidonParameters = setup_params_3::(); + static ref DEFAULT_BLS381_ARKWORKS_POSEIDON_PARAMETERS: PoseidonParameters = + setup_params_x5_3::(CurveEnum::Bls381); + static ref DEFAULT_BN254_ARKWORKS_POSEIDON_PARAMETERS: PoseidonParameters = + setup_params_x5_3::(CurveEnum::Bn254); + static ref BLS381_X17_ARKWORKS_POSEIDON_PARAMETERS: PoseidonParameters = + setup_params_x17_3::(CurveEnum::Bls381); + static ref BN254_X17_ARKWORKS_POSEIDON_PARAMETERS: PoseidonParameters = + setup_params_x17_3::(CurveEnum::Bn254); } /// Default hasher instance used to construct the tree @@ -115,11 +125,56 @@ impl Setup { _ => Err(Error::::Unimplemented), }, Backend::Arkworks(Curve::Bls381, _) => match self.hasher { - HashFunction::PoseidonDefault => { + HashFunction::PoseidonDefault | HashFunction::Poseidon(3, 5) => { + let mut bytes = Vec::new(); + bytes.extend(xl); + bytes.extend(xr); + let res = + PoseidonCRH_x5_3::::evaluate(&DEFAULT_BLS381_ARKWORKS_POSEIDON_PARAMETERS, &bytes) + .unwrap(); + let bytes_res = to_bytes![res]; + let bytes = match bytes_res { + Ok(bytes) => bytes, + Err(_) => return Err(Error::::HashingFailed), + }; + Ok(bytes) + } + HashFunction::Poseidon(3, 17) => { let mut bytes = Vec::new(); bytes.extend(xl); bytes.extend(xr); - let res = PoseidonCRH3::evaluate(&DEFAULT_ARKWORKS_POSEIDON_PARAMETERS, &bytes).unwrap(); + let res = PoseidonCRH_x17_3::::evaluate(&BLS381_X17_ARKWORKS_POSEIDON_PARAMETERS, &bytes) + .unwrap(); + let bytes_res = to_bytes![res]; + let bytes = match bytes_res { + Ok(bytes) => bytes, + Err(_) => return Err(Error::::HashingFailed), + }; + Ok(bytes) + } + _ => Err(Error::::Unimplemented), + }, + Backend::Arkworks(Curve::Bn254, _) => match self.hasher { + HashFunction::PoseidonDefault | HashFunction::Poseidon(3, 5) => { + let mut bytes = Vec::new(); + bytes.extend(xl); + bytes.extend(xr); + let res = + PoseidonCRH_x5_3::::evaluate(&DEFAULT_BN254_ARKWORKS_POSEIDON_PARAMETERS, &bytes) + .unwrap(); + let bytes_res = to_bytes![res]; + let bytes = match bytes_res { + Ok(bytes) => bytes, + Err(_) => return Err(Error::::HashingFailed), + }; + Ok(bytes) + } + HashFunction::Poseidon(3, 17) => { + let mut bytes = Vec::new(); + bytes.extend(xl); + bytes.extend(xr); + let res = PoseidonCRH_x17_3::::evaluate(&BN254_X17_ARKWORKS_POSEIDON_PARAMETERS, &bytes) + .unwrap(); let bytes_res = to_bytes![res]; let bytes = match bytes_res { Ok(bytes) => bytes, @@ -153,7 +208,39 @@ impl Setup { }, Backend::Arkworks(Curve::Bls381, _) => match self.hasher { HashFunction::PoseidonDefault => { - let res = gen_empty_hashes::(&(), &DEFAULT_ARKWORKS_POSEIDON_PARAMETERS) + let res = + gen_empty_hashes::>(&(), &DEFAULT_BLS381_ARKWORKS_POSEIDON_PARAMETERS) + .map_err(|_| Error::::ZeroTreeGenFailed)?; + let zero_tree: Vec = res + .iter() + .map(|val| to_bytes![val].map_err(|_| Error::::ZeroTreeGenFailed)) + .collect::, _>>()?; + Ok((zero_tree[0..depth].to_vec(), zero_tree[depth].clone())) + } + HashFunction::Poseidon(3, 17) => { + let res = gen_empty_hashes::>(&(), &BLS381_X17_ARKWORKS_POSEIDON_PARAMETERS) + .map_err(|_| Error::::ZeroTreeGenFailed)?; + let zero_tree: Vec = res + .iter() + .map(|val| to_bytes![val].map_err(|_| Error::::ZeroTreeGenFailed)) + .collect::, _>>()?; + Ok((zero_tree[0..depth].to_vec(), zero_tree[depth].clone())) + } + _ => Err(Error::::Unimplemented), + }, + Backend::Arkworks(Curve::Bn254, _) => match self.hasher { + HashFunction::PoseidonDefault => { + let res = + gen_empty_hashes::>(&(), &DEFAULT_BN254_ARKWORKS_POSEIDON_PARAMETERS) + .map_err(|_| Error::::ZeroTreeGenFailed)?; + let zero_tree: Vec = res + .iter() + .map(|val| to_bytes![val].map_err(|_| Error::::ZeroTreeGenFailed)) + .collect::, _>>()?; + Ok((zero_tree[0..depth].to_vec(), zero_tree[depth].clone())) + } + HashFunction::Poseidon(3, 17) => { + let res = gen_empty_hashes::>(&(), &BN254_X17_ARKWORKS_POSEIDON_PARAMETERS) .map_err(|_| Error::::ZeroTreeGenFailed)?; let zero_tree: Vec = res .iter() @@ -238,10 +325,40 @@ impl Setup { let vk = VerifyingKey::::deserialize(&verifier_key.unwrap()[..]) .map_err(|_| Error::::InvalidVerifierKey)?; - let public_inputs = get_public_inputs(*nullifier, *root, *recipient, *relayer); + let public_inputs = get_public_inputs::(*nullifier, *root, *recipient, *relayer); let proof = Proof::::deserialize(&proof_bytes[..]).map_err(|_| Error::::InvalidZkProof)?; - let res = verify_groth16(&vk, &public_inputs, &proof); + let res = verify_groth16::(&vk, &public_inputs, &proof); + if !res { + return Err(Error::::ZkVerificationFailed); + } + + Ok(()) + } + Backend::Arkworks(Curve::Bn254, Snark::Groth16) => { + let nullifier_elts = + to_field_elements::(&nullifier_hash_bytes).map_err(|_| Error::::InvalidPublicInputs)?; + let root_elts = + to_field_elements::(&root_bytes).map_err(|_| Error::::InvalidPublicInputs)?; + let recipient_elts = + to_field_elements::(&recipient_bytes).map_err(|_| Error::::InvalidPublicInputs)?; + let relayer_elts = + to_field_elements::(&relayer_bytes).map_err(|_| Error::::InvalidPublicInputs)?; + + let nullifier = nullifier_elts.get(0).ok_or(Error::::InvalidPublicInputs)?; + let root = root_elts.get(0).ok_or(Error::::InvalidPublicInputs)?; + let recipient = recipient_elts.get(0).ok_or(Error::::InvalidPublicInputs)?; + let relayer = relayer_elts.get(0).ok_or(Error::::InvalidPublicInputs)?; + + if verifier_key.is_none() { + return Err(Error::::InvalidVerifierKey); + } + + let vk = VerifyingKey::::deserialize(&verifier_key.unwrap()[..]) + .map_err(|_| Error::::InvalidVerifierKey)?; + let public_inputs = get_public_inputs::(*nullifier, *root, *recipient, *relayer); + let proof = Proof::::deserialize(&proof_bytes[..]).map_err(|_| Error::::InvalidZkProof)?; + let res = verify_groth16::(&vk, &public_inputs, &proof); if !res { return Err(Error::::ZkVerificationFailed); }