summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml8
-rw-r--r--src/hasher.rs77
-rw-r--r--src/merkletree.rs3
-rw-r--r--src/proof.rs1
4 files changed, 40 insertions, 49 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e28f8ce..d699a86 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,9 +5,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
-hex = { version = "0.4.3", optional = true }
-sha2 = { version = "0.10.9", optional = true }
-
-[features]
-default = []
-sha256 = ["sha2", "hex"]
+hex = { version = "0.4.3" }
+sha2 = { version = "0.10.9" }
diff --git a/src/hasher.rs b/src/hasher.rs
index d27a7e5..088b16a 100644
--- a/src/hasher.rs
+++ b/src/hasher.rs
@@ -1,5 +1,7 @@
//! Provides hashing abstractions and implementations including SHA256 and a default dummy hasher.
+use sha2::{Digest, Sha256};
+
/// A trait representing a generic hash function.
///
/// This allows the Merkle tree to use any hash function that conforms to this interface.
@@ -21,52 +23,49 @@ impl Hasher for DummyHasher {
}
}
-#[cfg(feature = "sha256")]
-mod hasher_sha256 {
- use super::*;
- use sha2::{Digest, Sha256};
+#[derive(Clone)]
+/// A hasher implementation using the SHA-256 cryptographic hash function.
+pub struct SHA256Hasher;
- #[derive(Clone)]
- /// A hasher implementation using the SHA-256 cryptographic hash function.
- pub struct SHA256Hasher;
+impl Default for SHA256Hasher {
+ fn default() -> Self {
+ Self::new()
+ }
+}
- impl SHA256Hasher {
- pub fn new() -> Self {
- Self {}
- }
+impl SHA256Hasher {
+ pub fn new() -> Self {
+ Self {}
}
+}
- impl Hasher for SHA256Hasher {
- fn hash(&self, input: &[u8]) -> String {
- let mut hasher = Sha256::new();
- hasher.update(input);
- hex::encode(hasher.finalize())
- }
+impl Hasher for SHA256Hasher {
+ fn hash(&self, input: &[u8]) -> String {
+ let mut hasher = Sha256::new();
+ hasher.update(input);
+ hex::encode(hasher.finalize())
}
+}
- #[cfg(test)]
- mod tests {
- use super::*;
+#[cfg(test)]
+mod tests {
+ use super::*;
- #[test]
- fn test_sha256_hasher_with_known_input() {
- let hasher = SHA256Hasher;
- let input = "hello".as_bytes();
- let expected_hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
- let actual_hash = hasher.hash(input);
- assert_eq!(actual_hash, expected_hash);
- }
+ #[test]
+ fn test_sha256_hasher_with_known_input() {
+ let hasher = SHA256Hasher;
+ let input = "hello".as_bytes();
+ let expected_hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
+ let actual_hash = hasher.hash(input);
+ assert_eq!(actual_hash, expected_hash);
+ }
- #[test]
- fn test_sha256_hasher_empty_string() {
- let hasher = SHA256Hasher;
- let input = &[];
- let expected_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
- let actual_hash = hasher.hash(input);
- assert_eq!(actual_hash, expected_hash);
- }
+ #[test]
+ fn test_sha256_hasher_empty_string() {
+ let hasher = SHA256Hasher;
+ let input = &[];
+ let expected_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+ let actual_hash = hasher.hash(input);
+ assert_eq!(actual_hash, expected_hash);
}
}
-
-#[cfg(feature = "sha256")]
-pub use hasher_sha256::*;
diff --git a/src/merkletree.rs b/src/merkletree.rs
index 0d5186b..84d59d2 100644
--- a/src/merkletree.rs
+++ b/src/merkletree.rs
@@ -137,7 +137,6 @@ mod tests {
}
#[test]
- #[cfg(feature = "sha256")]
fn test_merkle_tree_hashing() {
let data = &["hello".as_bytes(), "world".as_bytes()];
let tree = MerkleTree::new(SHA256Hasher::new(), data);
@@ -150,7 +149,6 @@ mod tests {
}
#[test]
- #[cfg(feature = "sha256")]
fn test_merkle_tree_single_leaf() {
let data = &["hello".as_bytes()];
let tree = MerkleTree::new(SHA256Hasher::new(), data);
@@ -164,7 +162,6 @@ mod tests {
}
#[test]
- #[cfg(feature = "sha256")]
fn test_merkle_tree_with_10_elements() {
let inputs = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
let data: Vec<&[u8]> = inputs.iter().map(|s| s.as_bytes()).collect();
diff --git a/src/proof.rs b/src/proof.rs
index d2d1c0f..10eb825 100644
--- a/src/proof.rs
+++ b/src/proof.rs
@@ -167,7 +167,6 @@ mod tests {
}
#[test]
- #[cfg(feature = "sha256")]
fn test_proof_generation_and_verification_sha256() {
let hasher = SHA256Hasher::new();
let data = vec!["a", "b", "c", "d"];