From c27339ceec36515810504fd09b46be6fe366ad60 Mon Sep 17 00:00:00 2001 From: Dastan <88332432+dastansam@users.noreply.github.com> Date: Mon, 22 Dec 2025 01:08:03 +0600 Subject: [PATCH] Add generic hash storage --- Cargo.lock | 94 +++++++++++++++++++---------------- Cargo.toml | 20 +++----- pallets/wormhole/Cargo.toml | 2 +- pallets/wormhole/src/lib.rs | 52 +++++++++++++------ pallets/wormhole/src/tests.rs | 4 +- 5 files changed, 98 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eae87855..2dad13f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7234,7 +7234,7 @@ dependencies = [ "pallet-timestamp", "parity-scale-codec", "primitive-types 0.13.1", - "qp-poseidon-core 1.0.2", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qpow-math", "scale-info", "sp-arithmetic", @@ -7616,8 +7616,8 @@ dependencies = [ "qp-dilithium-crypto", "qp-header", "qp-plonky2", - "qp-poseidon", - "qp-poseidon-core 1.0.2", + "qp-poseidon 1.0.5", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qp-wormhole", "qp-wormhole-circuit", "qp-wormhole-circuit-builder", @@ -8842,7 +8842,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf" dependencies = [ "heck 0.5.0", - "itertools 0.10.5", + "itertools 0.14.0", "log", "multimap", "once_cell", @@ -8875,7 +8875,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.14.0", "proc-macro2", "quote", "syn 2.0.110", @@ -8907,7 +8907,7 @@ dependencies = [ "env_logger 0.11.8", "log", "parity-scale-codec", - "qp-poseidon", + "qp-poseidon 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qp-rusty-crystals-dilithium", "qp-rusty-crystals-hdwallet", "scale-info", @@ -8926,8 +8926,8 @@ dependencies = [ "p3-field", "p3-goldilocks", "parity-scale-codec", - "qp-poseidon", - "qp-poseidon-core 1.0.2", + "qp-poseidon 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "scale-info", "serde", "serde_json", @@ -8956,7 +8956,7 @@ dependencies = [ "plonky2_maybe_rayon", "plonky2_util", "qp-plonky2-field", - "qp-poseidon-constants 1.0.1", + "qp-poseidon-constants", "rand 0.8.5", "rand_chacha 0.3.1", "serde", @@ -8984,14 +8984,13 @@ dependencies = [ [[package]] name = "qp-poseidon" -version = "1.0.2" -source = "git+https://github.com/Quantus-Network/qp-poseidon?branch=feat%2Ftransfer-proof-asset-id#9ae96dea18464d6ea43de5c78a066f9d1f17b801" +version = "1.0.5" dependencies = [ "log", "p3-field", "p3-goldilocks", "parity-scale-codec", - "qp-poseidon-core 1.0.2", + "qp-poseidon-core 1.0.5", "scale-info", "serde", "sp-core", @@ -9001,22 +9000,29 @@ dependencies = [ ] [[package]] -name = "qp-poseidon-constants" -version = "1.0.1" +name = "qp-poseidon" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d56b56652e9f44a43de9593e75d7c3e0c3a352e10675cf3024e5b3175711cd3" +checksum = "5029d1c8223c0312a0247ebc745c5b09622dcbebe104f0fdb9de358b87ef032a" dependencies = [ + "log", "p3-field", "p3-goldilocks", - "p3-poseidon2", - "rand 0.9.2", - "rand_chacha 0.9.0", + "parity-scale-codec", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "scale-info", + "serde", + "sp-core", + "sp-runtime", + "sp-storage", + "sp-trie", ] [[package]] name = "qp-poseidon-constants" -version = "1.0.2" -source = "git+https://github.com/Quantus-Network/qp-poseidon?branch=feat%2Ftransfer-proof-asset-id#9ae96dea18464d6ea43de5c78a066f9d1f17b801" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d56b56652e9f44a43de9593e75d7c3e0c3a352e10675cf3024e5b3175711cd3" dependencies = [ "p3-field", "p3-goldilocks", @@ -9027,29 +9033,28 @@ dependencies = [ [[package]] name = "qp-poseidon-core" -version = "1.0.2" -source = "git+https://github.com/Quantus-Network/qp-poseidon?branch=feat%2Ftransfer-proof-asset-id#9ae96dea18464d6ea43de5c78a066f9d1f17b801" +version = "1.0.5" dependencies = [ "p3-field", "p3-goldilocks", "p3-poseidon2", "p3-symmetric", - "qp-plonky2", - "qp-poseidon-constants 1.0.2", + "qp-poseidon-constants", "rand_chacha 0.9.0", ] [[package]] name = "qp-poseidon-core" -version = "1.0.3" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f52c70df221c356b3ce63afabfae623aae632c27d3e078cd20eec4348096c2d7" +checksum = "71dd1bf5d2997abf70247fcd23c8f04d7093b1faf33b775a42fb00c07e0a0e05" dependencies = [ "p3-field", "p3-goldilocks", "p3-poseidon2", "p3-symmetric", - "qp-poseidon-constants 1.0.1", + "qp-plonky2", + "qp-poseidon-constants", "rand_chacha 0.9.0", ] @@ -9075,7 +9080,7 @@ dependencies = [ "hex", "hex-literal", "nam-tiny-hderive", - "qp-poseidon-core 1.0.3", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qp-rusty-crystals-dilithium", "rand_chacha 0.9.0", "rand_core 0.9.3", @@ -9100,20 +9105,22 @@ version = "0.1.0" [[package]] name = "qp-wormhole-circuit" -version = "0.1.4" -source = "git+https://github.com/Quantus-Network/zk-circuits?branch=feat%2Fasset-id-wormhole#27fec229513e541237ec39497c6fd8bec69358cb" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcbccee20e314a1c52f36d8e78b1fa8205da46050c18da60d4964f41875bd4f6" dependencies = [ "anyhow", "hex", "qp-plonky2", - "qp-poseidon-core 1.0.2", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qp-zk-circuits-common", ] [[package]] name = "qp-wormhole-circuit-builder" -version = "0.1.4" -source = "git+https://github.com/Quantus-Network/zk-circuits?branch=feat%2Fasset-id-wormhole#27fec229513e541237ec39497c6fd8bec69358cb" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aec473c32d69e2e0941d192ab8cf8712761ee2e84d3829fc211087f53be15bf3" dependencies = [ "anyhow", "qp-plonky2", @@ -9123,8 +9130,9 @@ dependencies = [ [[package]] name = "qp-wormhole-prover" -version = "0.1.4" -source = "git+https://github.com/Quantus-Network/zk-circuits?branch=feat%2Fasset-id-wormhole#27fec229513e541237ec39497c6fd8bec69358cb" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fd377a2fa936e6edb069ffecbf41afe10803b342e5cda8ff3aab199f77fde5d" dependencies = [ "anyhow", "qp-plonky2", @@ -9134,8 +9142,9 @@ dependencies = [ [[package]] name = "qp-wormhole-verifier" -version = "0.1.4" -source = "git+https://github.com/Quantus-Network/zk-circuits?branch=feat%2Fasset-id-wormhole#27fec229513e541237ec39497c6fd8bec69358cb" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08e114bc7ad7a7589c502960abc570685b726993ccce05443577a9ddd8dc4ee1" dependencies = [ "anyhow", "qp-plonky2", @@ -9145,13 +9154,14 @@ dependencies = [ [[package]] name = "qp-zk-circuits-common" -version = "0.1.4" -source = "git+https://github.com/Quantus-Network/zk-circuits?branch=feat%2Fasset-id-wormhole#27fec229513e541237ec39497c6fd8bec69358cb" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445cc21c39959d1b553c4d8ea94d058ceab84cd70e5d47ec82b11535494cec46" dependencies = [ "anyhow", "hex", "qp-plonky2", - "qp-poseidon-core 1.0.2", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde", ] @@ -9164,7 +9174,7 @@ dependencies = [ "num-bigint", "num-traits", "primitive-types 0.13.1", - "qp-poseidon-core 1.0.2", + "qp-poseidon-core 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -9287,7 +9297,7 @@ dependencies = [ "primitive-types 0.13.1", "qp-dilithium-crypto", "qp-header", - "qp-poseidon", + "qp-poseidon 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "qp-scheduler", "qp-wormhole", "qp-wormhole-circuit", diff --git a/Cargo.toml b/Cargo.toml index 9bb0aabb..b0ae24b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -149,21 +149,15 @@ sp-consensus-qpow = { path = "./primitives/consensus/qpow", default-features = f # Quantus network dependencies qp-plonky2 = { version = "1.1.3", default-features = false } -qp-poseidon = { git = "https://github.com/Quantus-Network/qp-poseidon", branch = "feat/transfer-proof-asset-id", default-features = false } -qp-poseidon-core = { git = "https://github.com/Quantus-Network/qp-poseidon", branch = "feat/transfer-proof-asset-id", default-features = false, features = ["p2", "p3"] } +qp-poseidon = { version = "1.0.5", default-features = false } +qp-poseidon-core = { version = "1.0.5", default-features = false, features = ["p2", "p3"] } qp-rusty-crystals-dilithium = { version = "2.0.0", default-features = false } qp-rusty-crystals-hdwallet = { version = "1.0.0" } -qp-wormhole-circuit = { git = "https://github.com/Quantus-Network/zk-circuits", branch = "feat/asset-id-wormhole", default-features = false } -qp-wormhole-circuit-builder = { git = "https://github.com/Quantus-Network/zk-circuits", branch = "feat/asset-id-wormhole", default-features = false } -qp-wormhole-prover = { git = "https://github.com/Quantus-Network/zk-circuits", branch = "feat/asset-id-wormhole", default-features = false, features = [ - "no_random", -] } -qp-wormhole-verifier = { git = "https://github.com/Quantus-Network/zk-circuits", branch = "feat/asset-id-wormhole", default-features = false, features = [ - "no_random", -] } -qp-zk-circuits-common = { git = "https://github.com/Quantus-Network/zk-circuits", branch = "feat/asset-id-wormhole", default-features = false, features = [ - "no_random", -] } +qp-wormhole-circuit = { version = "0.1.5", default-features = false } +qp-wormhole-circuit-builder = { version = "0.1.5", default-features = false } +qp-wormhole-prover = { version = "0.1.5", default-features = false } +qp-wormhole-verifier = { version = "0.1.5", default-features = false } +qp-zk-circuits-common = { version = "0.1.5", default-features = false } # polkadot-sdk dependencies frame-benchmarking = { version = "41.0.0", default-features = false } diff --git a/pallets/wormhole/Cargo.toml b/pallets/wormhole/Cargo.toml index a1a1d064..c98ed518 100644 --- a/pallets/wormhole/Cargo.toml +++ b/pallets/wormhole/Cargo.toml @@ -17,7 +17,7 @@ lazy_static.workspace = true log.workspace = true pallet-balances.workspace = true qp-header = { workspace = true, features = ["serde"] } -qp-poseidon.workspace = true +qp-poseidon = { path = "../../../qp-poseidon/substrate", default-features = false } qp-wormhole.workspace = true qp-wormhole-circuit = { workspace = true, default-features = false } qp-wormhole-verifier = { workspace = true, default-features = false } diff --git a/pallets/wormhole/src/lib.rs b/pallets/wormhole/src/lib.rs index ec635407..b399b2f9 100644 --- a/pallets/wormhole/src/lib.rs +++ b/pallets/wormhole/src/lib.rs @@ -4,11 +4,11 @@ extern crate alloc; use core::marker::PhantomData; -use codec::{Decode, Encode, MaxEncodedLen}; +use codec::{Decode, MaxEncodedLen}; use frame_support::StorageHasher; use lazy_static::lazy_static; pub use pallet::*; -use qp_poseidon::PoseidonHasher as PoseidonCore; +pub use qp_poseidon::{PoseidonHasher as PoseidonCore, ToFelts}; use qp_wormhole_verifier::WormholeVerifier; #[cfg(test)] @@ -35,17 +35,16 @@ pub fn get_wormhole_verifier() -> Result<&'static WormholeVerifier, &'static str WORMHOLE_VERIFIER.as_ref().ok_or("Wormhole verifier not available") } -pub struct PoseidonStorageHasher(PhantomData); +// We use a generic struct so we can pass the specific Key type to the hasher +pub struct PoseidonStorageHasher(PhantomData); -impl StorageHasher - for PoseidonStorageHasher -{ +impl StorageHasher for PoseidonStorageHasher { // We are lying here, but maybe it's ok because it's just metadata const METADATA: StorageHasherIR = StorageHasherIR::Identity; type Output = [u8; 32]; fn hash(x: &[u8]) -> Self::Output { - PoseidonCore::hash_storage::(x) + PoseidonCore::hash_storage::(x) } fn max_len() -> usize { @@ -55,7 +54,7 @@ impl StorageHasher #[frame_support::pallet] pub mod pallet { - use crate::{PoseidonStorageHasher, WeightInfo}; + use crate::{PoseidonStorageHasher, ToFelts, WeightInfo}; use alloc::vec::Vec; use codec::Decode; use frame_support::{ @@ -87,15 +86,24 @@ pub mod pallet { >>::Balance; pub type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; + pub type TransferProofKey = ( + AssetIdOf, + ::TransferCount, + ::AccountId, + ::AccountId, + BalanceOf, + ); + #[pallet::pallet] pub struct Pallet(_); #[pallet::config] pub trait Config: frame_system::Config where - AssetIdOf: Default + From + Clone, - BalanceOf: Default, + AssetIdOf: Default + From + Clone + ToFelts, + BalanceOf: Default + ToFelts, AssetBalanceOf: Into> + From>, + ::AccountId: ToFelts, { /// Currency type used for native token transfers and minting type Currency: Mutate> @@ -113,7 +121,8 @@ pub mod pallet { + Default + Saturating + Copy - + sp_runtime::traits::One; + + sp_runtime::traits::One + + ToFelts; /// Account ID used as the "from" account when creating transfer proofs for minted tokens #[pallet::constant] @@ -135,8 +144,8 @@ pub mod pallet { #[pallet::getter(fn transfer_proof)] pub type TransferProof = StorageMap< _, - PoseidonStorageHasher, - (AssetIdOf, T::TransferCount, T::AccountId, T::AccountId, BalanceOf), /* (asset_id, tx_count, from, to, amount) */ + PoseidonStorageHasher>, + TransferProofKey, (), OptionQuery, >; @@ -148,7 +157,10 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { + pub enum Event + where + T::AccountId: ToFelts, + { ProofVerified { exit_amount: BalanceOf, }, @@ -184,7 +196,10 @@ pub mod pallet { } #[pallet::call] - impl Pallet { + impl Pallet + where + ::AccountId: ToFelts, + { #[pallet::call_index(0)] #[pallet::weight(::WeightInfo::verify_wormhole_proof())] pub fn verify_wormhole_proof(origin: OriginFor, proof_bytes: Vec) -> DispatchResult { @@ -374,7 +389,10 @@ pub mod pallet { } // Helper functions for recording transfer proofs - impl Pallet { + impl Pallet + where + ::AccountId: ToFelts, + { /// Record a transfer proof /// This should be called by transaction extensions or other runtime components pub fn record_transfer( @@ -397,6 +415,8 @@ pub mod pallet { // Implement the TransferProofRecorder trait for other pallets to use impl qp_wormhole::TransferProofRecorder, BalanceOf> for Pallet + where + T::AccountId: ToFelts, { type Error = DispatchError; diff --git a/pallets/wormhole/src/tests.rs b/pallets/wormhole/src/tests.rs index 74054d08..7e97b93c 100644 --- a/pallets/wormhole/src/tests.rs +++ b/pallets/wormhole/src/tests.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod wormhole_tests { - use crate::{get_wormhole_verifier, mock::*}; + use crate::{get_wormhole_verifier, mock::*, TransferProofKey}; use codec::Encode; use frame_support::{ assert_ok, @@ -73,7 +73,7 @@ mod wormhole_tests { let event_transfer_count = 0u64; - let leaf_hash = PoseidonHasher::hash_storage::( + let leaf_hash = PoseidonHasher::hash_storage::>( &( 0u32, event_transfer_count,