From 74b5f612dc993376fdb11ce45130f6792c678db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C5=A1a=20Pr=C5=A1i=C4=87?= <93726535+0xSasaPrsic@users.noreply.github.com> Date: Mon, 26 Jun 2023 15:37:39 +0200 Subject: [PATCH] Use keccak256 (#172) * Use keccak256 as a hashing algorithm. * Use keccak256. * Revert deps. * Use keccak256 when using rs_merkle. * fmt. * Deps update. * Update deps for da-primitives. --- Cargo.lock | 14 +++++----- Cargo.toml | 38 ++++++++++++++-------------- pallets/system/src/submitted_data.rs | 37 ++++++++++++++++++++------- rpc/kate-rpc/Cargo.toml | 20 +++++++-------- rpc/kate-rpc/src/lib.rs | 1 + runtime/src/data_root_tests.rs | 2 +- runtime/src/lib.rs | 2 +- 7 files changed, 67 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 095c0d357..88946e275 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1560,7 +1560,7 @@ dependencies = [ [[package]] name = "da-primitives" version = "0.4.6" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "beefy-merkle-tree", "derive_more", @@ -3833,7 +3833,7 @@ dependencies = [ [[package]] name = "kate" version = "0.7.1" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "da-primitives", "derive_more", @@ -3860,7 +3860,7 @@ dependencies = [ [[package]] name = "kate-recovery" version = "0.8.1" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "dusk-bytes", "dusk-plonk", @@ -4990,7 +4990,7 @@ dependencies = [ [[package]] name = "nomad-base" version = "0.1.3" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "ethers-signers", "frame-support", @@ -5009,7 +5009,7 @@ dependencies = [ [[package]] name = "nomad-core" version = "0.1.3" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "ethers-core", "ethers-signers", @@ -5085,7 +5085,7 @@ dependencies = [ [[package]] name = "nomad-merkle" version = "0.1.1" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "frame-support", "hex-literal", @@ -5106,7 +5106,7 @@ dependencies = [ [[package]] name = "nomad-signature" version = "0.1.1" -source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.6#6468206d95e55bad150d66f446bc3404488e460c" +source = "git+https://github.com/availproject/avail-core?tag=da-primitives/v0.4.7#a9d0f1796d0c02a32df5091e576702d8f976cfa2" dependencies = [ "elliptic-curve", "ethers-core", diff --git a/Cargo.toml b/Cargo.toml index 7b1246a25..9fb9a58c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [workspace] members = [ - "pallets/system", - "pallets/dactr", - "pallets/bridges/nomad/updater-manager", - "pallets/bridges/nomad/home", - "pallets/bridges/nomad/da-bridge", - "runtime", - "node", + "pallets/system", + "pallets/dactr", + "pallets/bridges/nomad/updater-manager", + "pallets/bridges/nomad/home", + "pallets/bridges/nomad/da-bridge", + "runtime", + "node", ] @@ -22,19 +22,19 @@ frame-system-rpc-runtime-api = { path = "pallets/system/rpc/runtime-api" } frame-system-benchmarking = { path = "pallets/system/benchmarking" } # DA Primitives -da-primitives = { version = "0.4", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } -kate = { version = "0.7", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } -kate-recovery = { version = "0.8", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } +da-primitives = { version = "0.4", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } +kate = { version = "0.7", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } +kate-recovery = { version = "0.8", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } # Nomad -nomad-signature = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } -nomad-merkle = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } -nomad-base = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } -nomad-core = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.6" } +nomad-signature = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } +nomad-merkle = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } +nomad-base = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } +nomad-core = { version = "0.1", git="https://github.com/availproject/avail-core", tag = "da-primitives/v0.4.7" } # Other stuff -uint = { git = "https://github.com/paritytech/parity-common.git", tag="rlp-v0.5.2" } -rlp = { git = "https://github.com/paritytech/parity-common.git", tag="rlp-v0.5.2" } +uint = { git = "https://github.com/paritytech/parity-common.git", tag = "rlp-v0.5.2" } +rlp = { git = "https://github.com/paritytech/parity-common.git", tag = "rlp-v0.5.2" } # Substrate (polkadot-v0.9.37). beefy-merkle-tree = { git = "https://github.com/paritytech/substrate.git/", branch = "polkadot-v0.9.37" } @@ -81,7 +81,7 @@ sc-consensus-babe-rpc = { git = "https://github.com/paritytech/substrate.git", b sc-network = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } sc-network-common = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } sc-chain-spec = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } -sc-consensus-slots = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } +sc-consensus-slots = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } sc-consensus-uncles = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } sp-authorship = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } sp-keyring = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } @@ -102,14 +102,14 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate.gi pallet-staking = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-utility = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } -pallet-scheduler = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } +pallet-scheduler = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-babe = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-authorship = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-indices = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-offences = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-treasury = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-collective = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } -pallet-bounties = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } +pallet-bounties = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-sudo = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-authority-discovery = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } pallet-tips = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } diff --git a/pallets/system/src/submitted_data.rs b/pallets/system/src/submitted_data.rs index c22e8d856..983314aee 100644 --- a/pallets/system/src/submitted_data.rs +++ b/pallets/system/src/submitted_data.rs @@ -1,8 +1,9 @@ use core::fmt::Debug; use beefy_merkle_tree::{merkle_proof, merkle_root, verify_proof, Leaf, MerkleProof}; -use da_primitives::{OpaqueExtrinsic, ShaTwo256}; +use da_primitives::OpaqueExtrinsic; use sp_core::H256; +use sp_runtime::traits::Keccak256; use sp_std::{cell::RefCell, rc::Rc, vec::Vec}; const LOG_TARGET: &str = "runtime::system::submitted_data"; @@ -17,6 +18,7 @@ pub struct Metrics { /// Total number of analysed extrinsic. pub total_extrinsics: u32, } + pub type RcMetrics = Rc>; impl Metrics { @@ -94,7 +96,7 @@ where /// In case an empty list of leaves is passed the function returns a 0-filled hash. fn root>>(submitted_data: I, metrics: RcMetrics) -> H256 { #[cfg(not(feature = "force-rs-merkle"))] - let root = merkle_root::(submitted_data); + let root = merkle_root::(submitted_data); #[cfg(feature = "force-rs-merkle")] let root = rs_merkle_root(submitted_data).into(); log::debug!( @@ -107,17 +109,26 @@ fn root>>(submitted_data: I, metrics: RcMetrics) -> H root } -/// Calculates the merkle root using `Sha256` and `rs_merkle` crate. +/// Calculates the merkle root using `Keccak256` and `rs_merkle` crate. #[cfg(feature = "force-rs-merkle")] fn rs_merkle_root(leaves: I) -> H256 where I: Iterator>, { - use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree}; + use rs_merkle::{Hasher, MerkleTree}; + + #[derive(Clone)] + pub struct Keccak256Algorithm {} + + impl Hasher for Keccak256Algorithm { + type Hash = [u8; 32]; + + fn hash(data: &[u8]) -> [u8; 32] { sp_io::hashing::keccak_256(data).into() } + } - let mut tree = MerkleTree::::new(); + let mut tree = MerkleTree::::new(); leaves.for_each(|leave| { - let leave_hash = Sha256::hash(leave.as_slice()); + let leave_hash = Keccak256Algorithm::hash(leave.as_slice()); tree.insert(leave_hash); }); @@ -188,7 +199,7 @@ fn proof( return None; } - let proof = merkle_proof::(submitted_data, data_index); + let proof = merkle_proof::(submitted_data, data_index); log::debug!( target: LOG_TARGET, "Build submitted data proof of index {data_index}: {:?} metrics: {:?}", @@ -196,6 +207,13 @@ fn proof( metrics ); + // let leaf = str::from_utf8(proof.leaf.as_slice()).unwrap(); //str::from_utf8(proof.leaf.as_slice()).unwrap(); + log::debug!("Leaf generated size: {}", proof.leaf.len()); + + for l in &proof.leaf { + log::debug!(target: LOG_TARGET, "Leaf generated {}", l); + } + Some(proof) } @@ -217,7 +235,7 @@ where I: IntoIterator, { let leaf = Leaf::Hash(data_hash); - verify_proof::( + verify_proof::( &root, proof.into_iter(), number_of_submitted_data as usize, @@ -281,6 +299,7 @@ mod test { } const EXTRINSIC_VERSION: u8 = 4; + impl Decode for AvailExtrinsic { fn decode(input: &mut I) -> Result { // This is a little more complicated than usual since the binary format must be compatible @@ -382,7 +401,7 @@ mod test { let root_proof = proof.proof_hashes().to_vec(); assert_eq!(root_proof, vec![ hex!("754B9412E0ED7907BDF4B7CA5D2A22F5E129A03DEB1F4E1C1FE42D322FDEE90E"), - hex!("8D6E30E494D17D7675A94C3C614467FF8CCE35201C1056751A6E9A100515DAF9") + hex!("8D6E30E494D17D7675A94C3C614467FF8CCE35201C1056751A6E9A100515DAF9"), ]); } diff --git a/rpc/kate-rpc/Cargo.toml b/rpc/kate-rpc/Cargo.toml index 3e934ecd9..b1669f9c1 100644 --- a/rpc/kate-rpc/Cargo.toml +++ b/rpc/kate-rpc/Cargo.toml @@ -29,16 +29,16 @@ sp-runtime = { version = "7.0.0", default-features = false } [features] default = ["std"] std = [ - "avail-base/std", - "da-runtime/std", - "da-primitives/std", - "kate/std", - "kate-recovery/std", - "frame-support/std", - "frame-system/std", - "codec/std", - "sp-api/std", - "sp-runtime/std", + "avail-base/std", + "da-runtime/std", + "da-primitives/std", + "kate/std", + "kate-recovery/std", + "frame-support/std", + "frame-system/std", + "codec/std", + "sp-api/std", + "sp-runtime/std", ] # Enables secure seed generation using for padding fill during the matrix diff --git a/rpc/kate-rpc/src/lib.rs b/rpc/kate-rpc/src/lib.rs index 72251f434..2371d8afa 100644 --- a/rpc/kate-rpc/src/lib.rs +++ b/rpc/kate-rpc/src/lib.rs @@ -425,6 +425,7 @@ where at ) })?; + DataProof::try_from(&merkle_proof) .map_err(|e| internal_err!("Data proof cannot be loaded from merkle root: {:?}", e)) } diff --git a/runtime/src/data_root_tests.rs b/runtime/src/data_root_tests.rs index 8c0ea6b31..eb44ed16b 100644 --- a/runtime/src/data_root_tests.rs +++ b/runtime/src/data_root_tests.rs @@ -17,7 +17,7 @@ fn submit_call() -> Vec { fn submit_call_expected() -> H256 { // hex!("ddf368647a902a6f6ab9f53b32245be28edc99e92f43f0004bbc2cb359814b2a").into() // hex!("9c6cf805b377632c6a224e1ca035f8f6975932529a5e492e73742e4f861ba89d").into() - hex!("66dde8b32cbd3e6c3ae02f570a23202413d67870b15354c17cc12c4c49894c55").into() + hex!("db45128913020d152dbee4d00a1dffebdb703425c44adbd7d7dfc7ae93d836bc").into() } #[test] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 499dba394..a8d9c04e9 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -123,7 +123,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 9, + spec_version: 10, impl_version: 0, apis: apis::runtime_api_versions(), transaction_version: 1,