Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sha384 hash function #27

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
//!
//! [`Hasher`]: crate::Hasher
mod sha256;
mod sha384;

pub use sha256::Sha256Algorithm as Sha256;
pub use sha384::Sha384Algorithm as Sha384;
46 changes: 46 additions & 0 deletions src/algorithms/sha384.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// sha384.rs
// Author imotai <[email protected]>
//
use crate::{prelude::*, Hasher};
use sha2::{digest::FixedOutput, Digest, Sha384};

/// Sha384 implementation of the [`Hasher`] trait.
///
/// # Examples
///
/// ```
/// # use rs_merkle::{MerkleTree, MerkleProof, algorithms::Sha384, Hasher, Error, utils};
/// # use std::convert::TryFrom;
/// #
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let tree = MerkleTree::<Sha384>::new();
/// let other_tree: MerkleTree<Sha384> = MerkleTree::new();
///
/// let proof_bytes: Vec<u8> = vec![
/// 46, 125, 44, 3, 169, 80, 122, 226, 101, 236, 245, 181, 53, 104, 133, 165, 51, 147, 162,
/// 2, 157, 36, 19, 148, 153, 114, 101, 161, 162, 90, 239, 198, 37, 47, 16, 200, 54, 16,
/// 235, 202, 26, 5, 156, 11, 174, 130, 85, 235, 162, 249, 91, 228, 209, 215, 188, 250,
/// 137, 215, 36, 138, 130, 217, 241, 17, 229, 160, 31, 238, 20, 224, 237, 92, 72, 113, 79,
/// 34, 24, 15, 37, 173, 131, 101, 181, 63, 151, 121, 247, 157, 196, 163, 215, 233, 57, 99,
/// 249, 74,
/// ];
///
/// let proof_result = MerkleProof::<Sha384>::from_bytes(&proof_bytes);
/// # Ok(())
/// # }
/// ```
///
/// [`Hasher`]: crate::Hasher
#[derive(Clone)]
pub struct Sha384Algorithm {}

impl Hasher for Sha384Algorithm {
type Hash = [u8; 48];

fn hash(data: &[u8]) -> [u8; 48] {
let mut hasher = Sha384::new();
hasher.update(data);
<[u8; 48]>::from(hasher.finalize_fixed())
}
}
2 changes: 0 additions & 2 deletions src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ impl<T: Hasher> MerkleTree<T> {
/// # }
pub fn from_leaves(leaves: &[T::Hash]) -> Self {
let mut tree = Self::new();

tree.append(leaves.to_vec().as_mut());
tree.commit();

tree
}

Expand Down
26 changes: 24 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use rayon::prelude::*;
use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree};
use rs_merkle::{
algorithms::{Sha256, Sha384},
Hasher, MerkleTree,
};

pub struct TestData {
pub leaf_values: Vec<String>,
pub expected_root_hex: String,
pub leaf_hashes: Vec<[u8; 32]>,
}

pub struct TestData48 {
pub leaf_values: Vec<String>,
pub expected_root_hex: String,
pub leaf_hashes: Vec<[u8; 48]>,
}

fn combine<T: Clone>(active: Vec<T>, rest: Vec<T>, mut combinations: Vec<Vec<T>>) -> Vec<Vec<T>> {
return if rest.is_empty() {
if active.is_empty() {
Expand Down Expand Up @@ -40,14 +49,27 @@ pub fn setup() -> TestData {
.iter()
.map(|x| Sha256::hash(x.as_bytes()))
.collect();

TestData {
leaf_values: leaf_values.iter().cloned().map(String::from).collect(),
leaf_hashes,
expected_root_hex: String::from(expected_root_hex),
}
}

pub fn setup_sha384() -> TestData48 {
let leaf_values = ["a", "b", "c", "d", "e", "f"];
let expected_root_hex = "19090f8e9527d4baaddbc1b9bb1142d96ef337537cfdb4aa176a709036be05fca58412b65c630d757288aaa7d54a57ad";
let leaf_hashes = leaf_values
.iter()
.map(|x| Sha384::hash(x.as_bytes()))
.collect();
TestData48 {
leaf_values: leaf_values.iter().cloned().map(String::from).collect(),
leaf_hashes,
expected_root_hex: String::from(expected_root_hex),
}
}

#[derive(Clone)]
pub struct ProofTestCases {
pub merkle_tree: MerkleTree<Sha256>,
Expand Down
15 changes: 13 additions & 2 deletions tests/merkle_tree_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ mod common;

pub mod root {
use crate::common;
use rs_merkle::{algorithms::Sha256, MerkleTree};
use rs_merkle::{
algorithms::{Sha256, Sha384},
MerkleTree,
};

#[test]
pub fn should_return_a_correct_root() {
let test_data = common::setup();

let merkle_tree = MerkleTree::<Sha256>::from_leaves(&test_data.leaf_hashes);
assert_eq!(
merkle_tree.root_hex(),
Some(test_data.expected_root_hex.to_string())
);
}

#[test]
pub fn should_return_a_correct_root_sha384() {
let test_data = common::setup_sha384();
let merkle_tree = MerkleTree::<Sha384>::from_leaves(&test_data.leaf_hashes);
assert_eq!(
merkle_tree.root_hex(),
Some(test_data.expected_root_hex.to_string())
Expand Down