Browse Source

Merge pull request #70 from 0xPolygonMiden/next

v0.1.3 release
al-gkr-basic-workflow v0.1.3
Bobbin Threadbare 2 years ago
committed by GitHub
parent
commit
835142d432
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 24 deletions
  1. +4
    -0
      CHANGELOG.md
  2. +4
    -4
      Cargo.toml
  3. +11
    -2
      src/hash/blake/mod.rs
  4. +1
    -1
      src/hash/rpo/digest.rs
  5. +9
    -0
      src/hash/rpo/mod.rs
  6. +34
    -17
      src/merkle/path_set.rs

+ 4
- 0
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)

+ 4
- 4
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"] }

+ 11
- 2
src/hash/blake/mod.rs

@ -56,13 +56,13 @@ impl From<[u8; N]> for Blake3Digest {
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 @@ 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 {

+ 1
- 1
src/hash/rpo/digest.rs

@ -46,7 +46,7 @@ impl Digest for RpoDigest {
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());
}
}

+ 9
- 0
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 {

+ 34
- 17
src/merkle/path_set.rs

@ -16,15 +16,30 @@ impl MerklePathSet {
// --------------------------------------------------------------------------------------------
/// 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 @@ 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);

Loading…
Cancel
Save