commit
835142d432
6 changed files with 63 additions and 24 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
## 0.1.3 (2023-02-20)
|
||||||
|
|
||||||
|
- Updated Winterfell dependency to v0.5.1 (#68)
|
||||||
|
|
||||||
## 0.1.2 (2023-02-17)
|
## 0.1.2 (2023-02-17)
|
||||||
|
|
||||||
- Fixed `Rpo256::hash` pad that was panicking on input (#44)
|
- Fixed `Rpo256::hash` pad that was panicking on input (#44)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "miden-crypto"
|
name = "miden-crypto"
|
||||||
version = "0.1.2"
|
version = "0.1.3"
|
||||||
description="Miden Cryptographic primitives"
|
description="Miden Cryptographic primitives"
|
||||||
authors = ["miden contributors"]
|
authors = ["miden contributors"]
|
||||||
readme="README.md"
|
readme="README.md"
|
||||||
|
@ -24,9 +24,9 @@ std = ["blake3/std", "winter_crypto/std", "winter_math/std", "winter_utils/std"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
blake3 = { version = "1.0", default-features = false }
|
blake3 = { version = "1.0", default-features = false }
|
||||||
winter_crypto = { version = "0.4.1", package = "winter-crypto", default-features = false }
|
winter_crypto = { version = "0.5.1", package = "winter-crypto", default-features = false }
|
||||||
winter_math = { version = "0.4.1", package = "winter-math", default-features = false }
|
winter_math = { version = "0.5.1", package = "winter-math", default-features = false }
|
||||||
winter_utils = { version = "0.4.1", package = "winter-utils", default-features = false }
|
winter_utils = { version = "0.5.1", package = "winter-utils", default-features = false }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = { version = "0.4", features = ["html_reports"] }
|
criterion = { version = "0.4", features = ["html_reports"] }
|
||||||
|
|
|
@ -56,13 +56,13 @@ impl<const N: usize> From<[u8; N]> for Blake3Digest<N> {
|
||||||
|
|
||||||
impl<const N: usize> Serializable for Blake3Digest<N> {
|
impl<const N: usize> Serializable for Blake3Digest<N> {
|
||||||
fn write_into<W: ByteWriter>(&self, target: &mut W) {
|
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> {
|
impl<const N: usize> Deserializable for Blake3Digest<N> {
|
||||||
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
|
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<const N: usize> Digest for Blake3Digest<N> {
|
||||||
pub struct Blake3_256;
|
pub struct Blake3_256;
|
||||||
|
|
||||||
impl Hasher for 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>;
|
type Digest = Blake3Digest<32>;
|
||||||
|
|
||||||
fn hash(bytes: &[u8]) -> Self::Digest {
|
fn hash(bytes: &[u8]) -> Self::Digest {
|
||||||
|
@ -141,6 +144,9 @@ impl Blake3_256 {
|
||||||
pub struct Blake3_192;
|
pub struct Blake3_192;
|
||||||
|
|
||||||
impl Hasher for 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>;
|
type Digest = Blake3Digest<24>;
|
||||||
|
|
||||||
fn hash(bytes: &[u8]) -> Self::Digest {
|
fn hash(bytes: &[u8]) -> Self::Digest {
|
||||||
|
@ -201,6 +207,9 @@ impl Blake3_192 {
|
||||||
pub struct Blake3_160;
|
pub struct Blake3_160;
|
||||||
|
|
||||||
impl Hasher for 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>;
|
type Digest = Blake3Digest<20>;
|
||||||
|
|
||||||
fn hash(bytes: &[u8]) -> Self::Digest {
|
fn hash(bytes: &[u8]) -> Self::Digest {
|
||||||
|
|
|
@ -46,7 +46,7 @@ impl Digest for RpoDigest {
|
||||||
|
|
||||||
impl Serializable for RpoDigest {
|
impl Serializable for RpoDigest {
|
||||||
fn write_into<W: ByteWriter>(&self, target: &mut W) {
|
fn write_into<W: ByteWriter>(&self, target: &mut W) {
|
||||||
target.write_u8_slice(&self.as_bytes());
|
target.write_bytes(&self.as_bytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,15 @@ const INV_ALPHA: u64 = 10540996611094048183;
|
||||||
pub struct Rpo256();
|
pub struct Rpo256();
|
||||||
|
|
||||||
impl Hasher for 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;
|
type Digest = RpoDigest;
|
||||||
|
|
||||||
fn hash(bytes: &[u8]) -> Self::Digest {
|
fn hash(bytes: &[u8]) -> Self::Digest {
|
||||||
|
|
|
@ -16,14 +16,29 @@ impl MerklePathSet {
|
||||||
// --------------------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Returns an empty MerklePathSet.
|
/// Returns an empty MerklePathSet.
|
||||||
pub fn new(depth: u8) -> Result<Self, MerkleError> {
|
pub fn new(depth: u8) -> Self {
|
||||||
let root = [ZERO; 4];
|
let root = [ZERO; 4];
|
||||||
let paths = BTreeMap::new();
|
let paths = BTreeMap::new();
|
||||||
|
|
||||||
Ok(Self {
|
Self {
|
||||||
root,
|
root,
|
||||||
total_depth: depth,
|
total_depth: depth,
|
||||||
paths,
|
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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,9 +247,9 @@ mod tests {
|
||||||
|
|
||||||
let root_exp = calculate_parent_hash(parent0, 0, parent1);
|
let root_exp = calculate_parent_hash(parent0, 0, parent1);
|
||||||
|
|
||||||
let mut set = super::MerklePathSet::new(3).unwrap();
|
let set = super::MerklePathSet::new(3)
|
||||||
|
.with_paths([(0, leaf0, vec![leaf1, parent1].into())])
|
||||||
set.add_path(0, leaf0, vec![leaf1, parent1].into()).unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(set.root(), root_exp);
|
assert_eq!(set.root(), root_exp);
|
||||||
}
|
}
|
||||||
|
@ -245,9 +260,9 @@ mod tests {
|
||||||
let hash_6 = int_to_node(6);
|
let hash_6 = int_to_node(6);
|
||||||
let index = 6_u64;
|
let index = 6_u64;
|
||||||
let depth = 4_u8;
|
let depth = 4_u8;
|
||||||
let mut set = super::MerklePathSet::new(depth).unwrap();
|
let set = super::MerklePathSet::new(depth)
|
||||||
|
.with_paths([(index, hash_6, path_6.clone().into())])
|
||||||
set.add_path(index, hash_6, path_6.clone().into()).unwrap();
|
.unwrap();
|
||||||
let stored_path_6 = set.get_path(NodeIndex::new(depth, index)).unwrap();
|
let stored_path_6 = set.get_path(NodeIndex::new(depth, index)).unwrap();
|
||||||
|
|
||||||
assert_eq!(path_6, *stored_path_6);
|
assert_eq!(path_6, *stored_path_6);
|
||||||
|
@ -260,9 +275,9 @@ mod tests {
|
||||||
let hash_6 = int_to_node(6);
|
let hash_6 = int_to_node(6);
|
||||||
let index = 6_u64;
|
let index = 6_u64;
|
||||||
let depth = 4_u8;
|
let depth = 4_u8;
|
||||||
let mut set = MerklePathSet::new(depth).unwrap();
|
let set = MerklePathSet::new(depth)
|
||||||
|
.with_paths([(index, hash_6, path_6.into())])
|
||||||
set.add_path(index, hash_6, path_6.into()).unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
int_to_node(6u64),
|
int_to_node(6u64),
|
||||||
|
@ -290,11 +305,13 @@ mod tests {
|
||||||
let index_5 = 5_u64;
|
let index_5 = 5_u64;
|
||||||
let index_4 = 4_u64;
|
let index_4 = 4_u64;
|
||||||
let depth = 4_u8;
|
let depth = 4_u8;
|
||||||
let mut set = MerklePathSet::new(depth).unwrap();
|
let mut set = MerklePathSet::new(depth)
|
||||||
|
.with_paths([
|
||||||
set.add_path(index_6, hash_6, path_6.into()).unwrap();
|
(index_6, hash_6, path_6.into()),
|
||||||
set.add_path(index_5, hash_5, path_5.into()).unwrap();
|
(index_5, hash_5, path_5.into()),
|
||||||
set.add_path(index_4, hash_4, path_4.into()).unwrap();
|
(index_4, hash_4, path_4.into()),
|
||||||
|
])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let new_hash_6 = int_to_node(100);
|
let new_hash_6 = int_to_node(100);
|
||||||
let new_hash_5 = int_to_node(55);
|
let new_hash_5 = int_to_node(55);
|
||||||
|
|
Loading…
Add table
Reference in a new issue