Skip to content

Commit

Permalink
Use keccak256 (availproject#172)
Browse files Browse the repository at this point in the history
* Use keccak256 as a hashing algorithm.

* Use keccak256.

* Revert deps.

* Use keccak256 when using rs_merkle.

* fmt.

* Deps update.

* Update deps for da-primitives.
  • Loading branch information
0xSasaPrsic authored Jun 26, 2023
1 parent 575afbb commit 74b5f61
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 47 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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",
]


Expand All @@ -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" }
Expand Down Expand Up @@ -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" }
Expand All @@ -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" }
Expand Down
37 changes: 28 additions & 9 deletions pallets/system/src/submitted_data.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -17,6 +18,7 @@ pub struct Metrics {
/// Total number of analysed extrinsic.
pub total_extrinsics: u32,
}

pub type RcMetrics = Rc<RefCell<Metrics>>;

impl Metrics {
Expand Down Expand Up @@ -94,7 +96,7 @@ where
/// In case an empty list of leaves is passed the function returns a 0-filled hash.
fn root<I: Iterator<Item = Vec<u8>>>(submitted_data: I, metrics: RcMetrics) -> H256 {
#[cfg(not(feature = "force-rs-merkle"))]
let root = merkle_root::<ShaTwo256, _>(submitted_data);
let root = merkle_root::<Keccak256, _>(submitted_data);
#[cfg(feature = "force-rs-merkle")]
let root = rs_merkle_root(submitted_data).into();
log::debug!(
Expand All @@ -107,17 +109,26 @@ fn root<I: Iterator<Item = Vec<u8>>>(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<I>(leaves: I) -> H256
where
I: Iterator<Item = Vec<u8>>,
{
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::<Sha256>::new();
let mut tree = MerkleTree::<Keccak256Algorithm>::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);
});

Expand Down Expand Up @@ -188,14 +199,21 @@ fn proof(
return None;
}

let proof = merkle_proof::<ShaTwo256, _, _>(submitted_data, data_index);
let proof = merkle_proof::<Keccak256, _, _>(submitted_data, data_index);
log::debug!(
target: LOG_TARGET,
"Build submitted data proof of index {data_index}: {:?} metrics: {:?}",
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)
}

Expand All @@ -217,7 +235,7 @@ where
I: IntoIterator<Item = H256>,
{
let leaf = Leaf::Hash(data_hash);
verify_proof::<ShaTwo256, _, _>(
verify_proof::<Keccak256, _, _>(
&root,
proof.into_iter(),
number_of_submitted_data as usize,
Expand Down Expand Up @@ -281,6 +299,7 @@ mod test {
}

const EXTRINSIC_VERSION: u8 = 4;

impl Decode for AvailExtrinsic {
fn decode<I: Input>(input: &mut I) -> Result<AvailExtrinsic, DecodeError> {
// This is a little more complicated than usual since the binary format must be compatible
Expand Down Expand Up @@ -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"),
]);
}

Expand Down
20 changes: 10 additions & 10 deletions rpc/kate-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rpc/kate-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/data_root_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn submit_call() -> Vec<u8> {
fn submit_call_expected() -> H256 {
// hex!("ddf368647a902a6f6ab9f53b32245be28edc99e92f43f0004bbc2cb359814b2a").into()
// hex!("9c6cf805b377632c6a224e1ca035f8f6975932529a5e492e73742e4f861ba89d").into()
hex!("66dde8b32cbd3e6c3ae02f570a23202413d67870b15354c17cc12c4c49894c55").into()
hex!("db45128913020d152dbee4d00a1dffebdb703425c44adbd7d7dfc7ae93d836bc").into()
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 74b5f61

Please sign in to comment.