arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
feat: upgrade to winterfell 0.5
Victor Lopez victor@codx.io
Mon, 20 Feb 2023 17:31:52 +0100
commit

0af45b75f443349c77b5ff9f1dc1fe11a8a48c00

parent

3c9a5235a090a7a7ba26baf9ce5358e199b56920

5 files changed, 59 insertions(+), 24 deletions(-)

jump to
M Cargo.toml → 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"] }
M src/hash/blake/mod.rs → src/hash/blake/mod.rs

@@ -56,13 +56,13 @@ }

impl<const N: usize> Serializable for Blake3Digest<N> { fn write_into<W: ByteWriter>(&self, target: &mut W) { - target.write_u8_slice(&self.0); + target.write_bytes(&self.0); } } impl<const N: usize> Deserializable for Blake3Digest<N> { fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> { - source.read_u8_array().map(Self) + source.read_array().map(Self) } }

@@ -81,6 +81,9 @@ /// 256-bit output blake3 hasher.

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 @@ /// 192-bit output blake3 hasher.

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 @@ /// 160-bit output blake3 hasher.

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 {
M src/hash/rpo/digest.rs → src/hash/rpo/digest.rs

@@ -46,7 +46,7 @@ }

impl Serializable for RpoDigest { fn write_into<W: ByteWriter>(&self, target: &mut W) { - target.write_u8_slice(&self.as_bytes()); + target.write_bytes(&self.as_bytes()); } }
M src/hash/rpo/mod.rs → src/hash/rpo/mod.rs

@@ -91,6 +91,15 @@ /// using [hash()](Rpo256::hash) function.

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 {
M src/merkle/path_set.rs → src/merkle/path_set.rs

@@ -16,15 +16,30 @@ // CONSTRUCTOR

// -------------------------------------------------------------------------------------------- /// Returns an empty MerklePathSet. - pub fn new(depth: u8) -> Result<Self, MerkleError> { + 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<I>(self, paths: I) -> Result<Self, MerkleError> + where + I: IntoIterator<Item = (u64, Word, MerklePath)>, + { + 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 @@ let parent1 = calculate_parent_hash(leaf2, 2, leaf3);

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 @@ let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];

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 @@ let path_6 = vec![int_to_node(7), int_to_node(45), int_to_node(123)];

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 @@ let index_6 = 6_u64;

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);