Skip to content

Commit

Permalink
chore: remove domain digest (#6436)
Browse files Browse the repository at this point in the history
Description
---
removes domain digest from MMR

Motivation and Context
---
MMR should not care what hasher you want to use.
  • Loading branch information
SWvheerden authored Jul 30, 2024
1 parent 5a33bc8 commit f9907a8
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 170 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion base_layer/mmr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ default = []
[dependencies]
tari_utilities = { version = "0.7" }
tari_crypto = { version = "0.20.3" }
tari_common = { path = "../../common", version = "1.0.0-pre.18" }
thiserror = "1.0"
borsh = "1.2"
digest = "0.10"
Expand Down
5 changes: 2 additions & 3 deletions base_layer/mmr/src/balanced_binary_merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use std::{
use borsh::{BorshDeserialize, BorshSerialize};
use digest::Digest;
use serde::{Deserialize, Serialize};
use tari_common::DomainDigest;
use thiserror::Error;

use crate::{common::hash_together, BalancedBinaryMerkleTree, Hash};
Expand All @@ -47,7 +46,7 @@ pub struct BalancedBinaryMerkleProof<D> {
}

impl<D> BalancedBinaryMerkleProof<D>
where D: Digest + DomainDigest
where D: Digest
{
#[must_use = "Must use the result of the proof verification"]
pub fn verify(&self, root: &Hash, leaf_hash: Hash) -> bool {
Expand Down Expand Up @@ -137,7 +136,7 @@ pub struct MergedBalancedBinaryMerkleProof<D> {
}

impl<D> MergedBalancedBinaryMerkleProof<D>
where D: Digest + DomainDigest
where D: Digest
{
pub fn create_from_proofs(proofs: &[BalancedBinaryMerkleProof<D>]) -> Result<Self, BalancedBinaryMerkleProofError> {
let heights = proofs
Expand Down
3 changes: 1 addition & 2 deletions base_layer/mmr/src/balanced_binary_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::{convert::TryFrom, marker::PhantomData};

use digest::Digest;
use serde::{Deserialize, Serialize};
use tari_common::DomainDigest;
use thiserror::Error;

use crate::{common::hash_together, Hash};
Expand Down Expand Up @@ -54,7 +53,7 @@ pub struct BalancedBinaryMerkleTree<D> {
}

impl<D> BalancedBinaryMerkleTree<D>
where D: Digest + DomainDigest
where D: Digest
{
// There is no push method for this tree. This tree is created at once and no modifications are allowed.
pub fn create(leaves: Vec<Hash>) -> Self {
Expand Down
3 changes: 1 addition & 2 deletions base_layer/mmr/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use std::convert::TryInto;

use digest::Digest;
use tari_common::DomainDigest;

use crate::{error::MerkleMountainRangeError, Hash};

Expand Down Expand Up @@ -187,7 +186,7 @@ pub fn is_left_sibling(pos: usize) -> bool {
(peak_map & peak) == 0
}

pub fn hash_together<D: Digest + DomainDigest>(left: &[u8], right: &[u8]) -> Hash {
pub fn hash_together<D: Digest>(left: &[u8], right: &[u8]) -> Hash {
D::new().chain_update(left).chain_update(right).finalize().to_vec()
}

Expand Down
5 changes: 2 additions & 3 deletions base_layer/mmr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use std::{convert::TryFrom, marker::PhantomData};

use digest::Digest;
use tari_common::DomainDigest;

use crate::{error::MerkleMountainRangeError, pruned_hashset::PrunedHashSet, ArrayLike, Hash, MerkleMountainRange};

Expand All @@ -36,7 +35,7 @@ pub type PrunedMmr<D> = MerkleMountainRange<D, PrunedHashSet>;
/// `validate` will throw an error.
pub fn prune_mmr<D, B>(mmr: &MerkleMountainRange<D, B>) -> Result<PrunedMmr<D>, MerkleMountainRangeError>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
let backend = PrunedHashSet::try_from(mmr)?;
Expand All @@ -51,7 +50,7 @@ pub fn calculate_mmr_root<D, B>(
additions: Vec<Hash>,
) -> Result<Hash, MerkleMountainRangeError>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
let mut mmr = prune_mmr(src)?;
Expand Down
5 changes: 2 additions & 3 deletions base_layer/mmr/src/merkle_mountain_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use std::{
};

use digest::Digest;
use tari_common::DomainDigest;

use crate::{
backend::ArrayLike,
Expand Down Expand Up @@ -62,7 +61,7 @@ pub struct MerkleMountainRange<D, B> {

impl<D, B> MerkleMountainRange<D, B>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
/// Create a new Merkle mountain range using the given backend for storage
Expand Down Expand Up @@ -282,7 +281,7 @@ where

impl<D, B, B2> PartialEq<MerkleMountainRange<D, B2>> for MerkleMountainRange<D, B>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
B2: ArrayLike<Value = Hash>,
{
Expand Down
25 changes: 7 additions & 18 deletions base_layer/mmr/src/merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::fmt::{self, Display, Formatter};
use digest::Digest;
use log::error;
use serde::{Deserialize, Serialize};
use tari_common::DomainDigest;
use tari_utilities::hex::Hex;
use thiserror::Error;

Expand Down Expand Up @@ -81,7 +80,7 @@ impl MerkleProof {
leaf_index: LeafIndex,
) -> Result<MerkleProof, MerkleProofError>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
let pos = node_index(leaf_index);
Expand All @@ -100,7 +99,7 @@ impl MerkleProof {
/// other MMR implementations work).
pub fn for_node<D, B>(mmr: &MerkleMountainRange<D, B>, pos: usize) -> Result<MerkleProof, MerkleProofError>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
// check this pos is actually a leaf in the MMR
Expand All @@ -113,7 +112,7 @@ impl MerkleProof {

fn generate_proof<D, B>(mmr: &MerkleMountainRange<D, B>, pos: usize) -> Result<MerkleProof, MerkleProofError>
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
// check we actually have a hash in the MMR at this pos
Expand Down Expand Up @@ -155,7 +154,7 @@ impl MerkleProof {
})
}

pub fn verify_leaf<D: Digest + DomainDigest>(
pub fn verify_leaf<D: Digest>(
&self,
root: &HashSlice,
hash: &HashSlice,
Expand All @@ -166,12 +165,7 @@ impl MerkleProof {
}

/// Verifies the Merkle proof against the provided root hash, element and position in the MMR.
pub fn verify<D: Digest + DomainDigest>(
&self,
root: &HashSlice,
hash: &HashSlice,
pos: usize,
) -> Result<(), MerkleProofError> {
pub fn verify<D: Digest>(&self, root: &HashSlice, hash: &HashSlice, pos: usize) -> Result<(), MerkleProofError> {
let mut proof = self.clone();
// calculate the peaks once as these are based on overall MMR size (and will not change)
let peaks = find_peaks(self.mmr_size).ok_or(MerkleMountainRangeError::InvalidMmrSize)?;
Expand All @@ -191,12 +185,7 @@ impl MerkleProof {
///
/// After running [verify_consume], we'll know the hash of 6 and it's position (the local root), and so we'll also
/// know where to insert the hash in the peak list.
fn check_root<D: Digest + DomainDigest>(
&self,
hash: &HashSlice,
pos: usize,
peaks: &[usize],
) -> Result<Hash, MerkleProofError> {
fn check_root<D: Digest>(&self, hash: &HashSlice, pos: usize, peaks: &[usize]) -> Result<Hash, MerkleProofError> {
// The peak hash list provided in the proof does not include the local peak determined from the candidate
// node, so len(peak) must be len(self.peaks) + 1.
if peaks.len() != self.peaks.len() + 1 {
Expand Down Expand Up @@ -230,7 +219,7 @@ impl MerkleProof {
/// calculating the parent hash, and then calling `verify_consume` again using the parent hash and position.
/// Once `self.path` is empty, we have the local root and position, this data is used to hash all the peaks
/// together in `check_root` to calculate the final merkle root.
fn verify_consume<D: Digest + DomainDigest>(
fn verify_consume<D: Digest>(
&mut self,
root: &HashSlice,
hash: &HashSlice,
Expand Down
3 changes: 1 addition & 2 deletions base_layer/mmr/src/pruned_hashset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::convert::TryFrom;

use digest::Digest;
use serde::{Deserialize, Serialize};
use tari_common::DomainDigest;

use crate::{common::find_peaks, error::MerkleMountainRangeError, ArrayLike, Hash, MerkleMountainRange};

Expand All @@ -51,7 +50,7 @@ pub struct PrunedHashSet {

impl<D, B> TryFrom<&MerkleMountainRange<D, B>> for PrunedHashSet
where
D: Digest + DomainDigest,
D: Digest,
B: ArrayLike<Value = Hash>,
{
type Error = MerkleMountainRangeError;
Expand Down
2 changes: 0 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ build = ["toml", "prost-build"]
static-application-info = ["git2"]

[dependencies]
tari_crypto = { version = "0.20.3" }
tari_features = { path = "./tari_features", version = "1.0.0-pre.18"}

anyhow = "1.0.53"
blake2 = "0.10"
config = { version = "0.14.0", default-features = false, features = ["toml"] }
dirs-next = "1.0.2"
git2 = { version = "0.18", default-features = false, optional = true }
Expand Down
128 changes: 0 additions & 128 deletions common/src/hashing.rs

This file was deleted.

3 changes: 0 additions & 3 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ pub use configuration::{
pub mod dir_utils;
pub use logging::initialize_logging;

mod hashing;
pub use hashing::{mac_domain_hasher, DomainDigest};

pub const DEFAULT_CONFIG: &str = "config/config.toml";
pub const DEFAULT_BASE_NODE_LOG_CONFIG: &str = "config/log4rs_base_node.yml";
pub const DEFAULT_WALLET_LOG_CONFIG: &str = "config/log4rs_console_wallet.yml";
Expand Down

0 comments on commit f9907a8

Please sign in to comment.