diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c362f2..2dc51ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.3 (2023-02-20) + +- Updated Winterfell dependency to v0.5.1 (#68) + ## 0.1.2 (2023-02-17) - Fixed `Rpo256::hash` pad that was panicking on input (#44) diff --git a/Cargo.toml b/Cargo.toml index bdb4702..902fe7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "miden-crypto" -version = "0.1.2" +version = "0.1.3" description="Miden Cryptographic primitives" authors = ["miden contributors"] readme="README.md" @@ -24,9 +24,9 @@ std = ["blake3/std", "winter_crypto/std", "winter_math/std", "winter_utils/std"] [dependencies] blake3 = { version = "1.0", default-features = false } -winter_crypto = { version = "0.4.1", package = "winter-crypto", default-features = false } -winter_math = { version = "0.4.1", package = "winter-math", default-features = false } -winter_utils = { version = "0.4.1", package = "winter-utils", default-features = false } +winter_crypto = { version = "0.5.1", package = "winter-crypto", default-features = false } +winter_math = { version = "0.5.1", package = "winter-math", default-features = false } +winter_utils = { version = "0.5.1", package = "winter-utils", default-features = false } [dev-dependencies] criterion = { version = "0.4", features = ["html_reports"] } diff --git a/src/hash/blake/mod.rs b/src/hash/blake/mod.rs index 488c8ba..578b86c 100644 --- a/src/hash/blake/mod.rs +++ b/src/hash/blake/mod.rs @@ -56,13 +56,13 @@ impl From<[u8; N]> for Blake3Digest { impl Serializable for Blake3Digest { fn write_into(&self, target: &mut W) { - target.write_u8_slice(&self.0); + target.write_bytes(&self.0); } } impl Deserializable for Blake3Digest { fn read_from(source: &mut R) -> Result { - source.read_u8_array().map(Self) + source.read_array().map(Self) } } @@ -81,6 +81,9 @@ impl Digest for Blake3Digest { pub struct Blake3_256; impl Hasher for Blake3_256 { + /// Blake3 collision resistance is 128-bits for 32-bytes output. + const COLLISION_RESISTANCE: u32 = 128; + type Digest = Blake3Digest<32>; fn hash(bytes: &[u8]) -> Self::Digest { @@ -141,6 +144,9 @@ impl Blake3_256 { pub struct Blake3_192; impl Hasher for Blake3_192 { + /// Blake3 collision resistance is 96-bits for 24-bytes output. + const COLLISION_RESISTANCE: u32 = 96; + type Digest = Blake3Digest<24>; fn hash(bytes: &[u8]) -> Self::Digest { @@ -201,6 +207,9 @@ impl Blake3_192 { pub struct Blake3_160; impl Hasher for Blake3_160 { + /// Blake3 collision resistance is 80-bits for 20-bytes output. + const COLLISION_RESISTANCE: u32 = 80; + type Digest = Blake3Digest<20>; fn hash(bytes: &[u8]) -> Self::Digest { diff --git a/src/hash/rpo/digest.rs b/src/hash/rpo/digest.rs index a2227fa..3f56f9c 100644 --- a/src/hash/rpo/digest.rs +++ b/src/hash/rpo/digest.rs @@ -46,7 +46,7 @@ impl Digest for RpoDigest { impl Serializable for RpoDigest { fn write_into(&self, target: &mut W) { - target.write_u8_slice(&self.as_bytes()); + target.write_bytes(&self.as_bytes()); } } diff --git a/src/hash/rpo/mod.rs b/src/hash/rpo/mod.rs index 67fc1a5..122519f 100644 --- a/src/hash/rpo/mod.rs +++ b/src/hash/rpo/mod.rs @@ -91,6 +91,15 @@ const INV_ALPHA: u64 = 10540996611094048183; pub struct Rpo256(); impl Hasher for Rpo256 { + /// Rpo256 collision resistance is the same as the security level, that is 128-bits. + /// + /// #### Collision resistance + /// + /// However, our setup of the capacity registers might drop it to 126. + /// + /// Related issue: [#69](https://github.com/0xPolygonMiden/crypto/issues/69) + const COLLISION_RESISTANCE: u32 = 128; + type Digest = RpoDigest; fn hash(bytes: &[u8]) -> Self::Digest { diff --git a/src/merkle/path_set.rs b/src/merkle/path_set.rs index 6acc12c..0b9d85c 100644 --- a/src/merkle/path_set.rs +++ b/src/merkle/path_set.rs @@ -16,15 +16,30 @@ impl MerklePathSet { // -------------------------------------------------------------------------------------------- /// Returns an empty MerklePathSet. - pub fn new(depth: u8) -> Result { + pub fn new(depth: u8) -> Self { let root = [ZERO; 4]; let paths = BTreeMap::new(); - Ok(Self { + Self { root, total_depth: depth, paths, - }) + } + } + + /// Appends the provided paths iterator into the set. + /// + /// Analogous to `[Self::add_path]`. + pub fn with_paths(self, paths: I) -> Result + where + I: IntoIterator, + { + paths + .into_iter() + .try_fold(self, |mut set, (index, value, path)| { + set.add_path(index, value, path)?; + Ok(set) + }) } // PUBLIC ACCESSORS @@ -232,9 +247,9 @@ mod tests { let root_exp = calculate_parent_hash(parent0, 0, parent1); - let mut set = super::MerklePathSet::new(3).unwrap(); - - set.add_path(0, leaf0, vec![leaf1, parent1].into()).unwrap(); + let set = super::MerklePathSet::new(3) + .with_paths([(0, leaf0, vec![leaf1, parent1].into())]) + .unwrap(); assert_eq!(set.root(), root_exp); } @@ -245,9 +260,9 @@ mod tests { let hash_6 = int_to_node(6); let index = 6_u64; let depth = 4_u8; - let mut set = super::MerklePathSet::new(depth).unwrap(); - - set.add_path(index, hash_6, path_6.clone().into()).unwrap(); + let set = super::MerklePathSet::new(depth) + .with_paths([(index, hash_6, path_6.clone().into())]) + .unwrap(); let stored_path_6 = set.get_path(NodeIndex::new(depth, index)).unwrap(); assert_eq!(path_6, *stored_path_6); @@ -260,9 +275,9 @@ mod tests { let hash_6 = int_to_node(6); let index = 6_u64; let depth = 4_u8; - let mut set = MerklePathSet::new(depth).unwrap(); - - set.add_path(index, hash_6, path_6.into()).unwrap(); + let set = MerklePathSet::new(depth) + .with_paths([(index, hash_6, path_6.into())]) + .unwrap(); assert_eq!( int_to_node(6u64), @@ -290,11 +305,13 @@ mod tests { let index_5 = 5_u64; let index_4 = 4_u64; let depth = 4_u8; - let mut set = MerklePathSet::new(depth).unwrap(); - - set.add_path(index_6, hash_6, path_6.into()).unwrap(); - set.add_path(index_5, hash_5, path_5.into()).unwrap(); - set.add_path(index_4, hash_4, path_4.into()).unwrap(); + let mut set = MerklePathSet::new(depth) + .with_paths([ + (index_6, hash_6, path_6.into()), + (index_5, hash_5, path_5.into()), + (index_4, hash_4, path_4.into()), + ]) + .unwrap(); let new_hash_6 = int_to_node(100); let new_hash_5 = int_to_node(55);