6 Commits

Author SHA1 Message Date
Bobbin Threadbare
ce9b45fe77 chore: add badges to readme 2024-03-17 13:32:46 -07:00
Bobbin Threadbare
56d014898d chore: update copyright year 2024-03-17 13:25:26 -07:00
Bobbin Threadbare
8e81ccdb68 chore: increment version to v0.8.2 and update changelog 2024-03-17 13:23:44 -07:00
Paul Schoenfelder
999a64fca6 chore: handle deprecations in winterfell 0.8.3 release 2024-03-17 16:18:23 -04:00
Bobbin Threadbare
4bc4bea0db chore: update changelog 2024-02-21 23:59:36 -05:00
Augusto Hack
dbab0e9aa9 fix: clippy warnings (#280) 2024-02-21 20:55:02 -08:00
54 changed files with 247 additions and 193 deletions

View File

@@ -1,3 +1,11 @@
# 0.8.2 (2024-03-17)
* Updated `no-std` approach to be in sync with winterfell v0.8.3 release (#290).
## 0.8.1 (2024-02-21)
* Fixed clippy warnings (#280)
## 0.8.0 (2024-02-14)
* Implemented the `PartialMmr` data structure (#195).

View File

@@ -1,12 +1,12 @@
[package]
name = "miden-crypto"
version = "0.8.0"
version = "0.8.2"
description = "Miden Cryptographic primitives"
authors = ["miden contributors"]
readme = "README.md"
license = "MIT"
repository = "https://github.com/0xPolygonMiden/crypto"
documentation = "https://docs.rs/miden-crypto/0.8.0"
documentation = "https://docs.rs/miden-crypto/0.8.2"
categories = ["cryptography", "no-std"]
keywords = ["miden", "crypto", "hash", "merkle"]
edition = "2021"
@@ -38,7 +38,6 @@ serde = ["dep:serde", "serde?/alloc", "winter_math/serde"]
std = [
"blake3/std",
"dep:cc",
"dep:libc",
"winter_crypto/std",
"winter_math/std",
"winter_utils/std",
@@ -47,9 +46,10 @@ std = [
[dependencies]
blake3 = { version = "1.5", default-features = false }
clap = { version = "4.5", features = ["derive"], optional = true }
libc = { version = "0.2", default-features = false, optional = true }
rand_utils = { version = "0.8", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", features = ["derive"], default-features = false, optional = true }
serde = { version = "1.0", features = [
"derive",
], default-features = false, optional = true }
winter_crypto = { version = "0.8", package = "winter-crypto", default-features = false }
winter_math = { version = "0.8", package = "winter-math", default-features = false }
winter_utils = { version = "0.8", package = "winter-utils", default-features = false }

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Polygon Miden
Copyright (c) 2024 Polygon Miden
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -1,4 +1,9 @@
# Miden Crypto
<a href="https://github.com/0xPolygonMiden/miden-crypto/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
<a href="https://github.com/0xPolygonMiden/crypto/actions/workflows/ci.yml"><img src="https://github.com/0xPolygonMiden/crypto/actions/workflows/ci.yml/badge.svg?branch=main"></a>
<img src="https://img.shields.io/badge/rustc-1.75+-lightgray.svg">
<a href="https://crates.io/crates/miden-crypto"><img src="https://img.shields.io/crates/v/miden-crypto"></a>
This crate contains cryptographic primitives used in Polygon Miden.
## Hash

View File

@@ -1,4 +1,5 @@
use core::mem::swap;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use miden_crypto::{
merkle::{LeafIndex, SimpleSmt},

View File

@@ -1,9 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use miden_crypto::merkle::{
DefaultMerkleStore as MerkleStore, LeafIndex, MerkleTree, NodeIndex, SimpleSmt, SMT_MAX_DEPTH,
use miden_crypto::{
hash::rpo::RpoDigest,
merkle::{
DefaultMerkleStore as MerkleStore, LeafIndex, MerkleTree, NodeIndex, SimpleSmt,
SMT_MAX_DEPTH,
},
Felt, Word,
};
use miden_crypto::Word;
use miden_crypto::{hash::rpo::RpoDigest, Felt};
use rand_utils::{rand_array, rand_value};
/// Since MerkleTree can only be created when a power-of-two number of elements is used, the sample

View File

@@ -1,6 +1,7 @@
use super::{LOG_N, MODULUS, PK_LEN};
use core::fmt;
use super::{LOG_N, MODULUS, PK_LEN};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FalconError {
KeyGenerationFailed,

View File

@@ -1,4 +1,4 @@
use libc::c_int;
use core::ffi::c_int;
// C IMPLEMENTATION INTERFACE
// ================================================================================================
@@ -77,8 +77,11 @@ extern "C" {
#[cfg(test)]
pub fn rpo128_absorb(
sc: *mut Rpo128Context,
data: *const ::std::os::raw::c_void,
len: libc::size_t,
data: *const core::ffi::c_void,
// TODO: When #![feature(c_size_t)] stabilizes, switch this to `core::ffi::size_t` to be
// more accurate. Currently, however, all Rust targets as of this writing are such that
// `core::ffi::size_t` and `usize` are the same size.
len: usize,
);
#[cfg(test)]
@@ -96,9 +99,11 @@ pub struct Rpo128Context {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloc::vec::Vec;
use rand_utils::{rand_array, rand_value, rand_vector};
use super::*;
use crate::dsa::rpo_falcon512::{NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
use rand_utils::{rand_array, rand_value, rand_vector};
#[test]
fn falcon_ffi() {

View File

@@ -2,9 +2,11 @@ use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, FalconError, Polynomial,
PublicKeyBytes, Rpo256, SecretKeyBytes, Serializable, Signature, Word,
};
#[cfg(feature = "std")]
use super::{ffi, NonceBytes, NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN};
use {
super::{ffi, NonceBytes, NONCE_LEN, PK_LEN, SIG_LEN, SK_LEN},
alloc::vec::Vec,
};
// PUBLIC KEY
// ================================================================================================
@@ -182,9 +184,10 @@ impl Deserializable for KeyPair {
#[cfg(all(test, feature = "std"))]
mod tests {
use super::{super::Felt, KeyPair, NonceBytes, Word};
use rand_utils::{rand_array, rand_vector};
use super::{super::Felt, KeyPair, NonceBytes, Word};
#[test]
fn test_falcon_verification() {
// generate random keys

View File

@@ -1,9 +1,6 @@
use crate::{
hash::rpo::Rpo256,
utils::{
collections::Vec, ByteReader, ByteWriter, Deserializable, DeserializationError,
Serializable,
},
utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
Felt, Word, ZERO,
};

View File

@@ -1,6 +1,8 @@
use super::{FalconError, Felt, Vec, LOG_N, MODULUS, MODULUS_MINUS_1_OVER_TWO, N, PK_LEN};
use alloc::vec::Vec;
use core::ops::{Add, Mul, Sub};
use super::{FalconError, Felt, LOG_N, MODULUS, MODULUS_MINUS_1_OVER_TWO, N, PK_LEN};
// FALCON POLYNOMIAL
// ================================================================================================

View File

@@ -1,10 +1,11 @@
use alloc::string::ToString;
use core::cell::OnceCell;
use super::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Felt, NonceBytes, NonceElements,
Polynomial, PublicKeyBytes, Rpo256, Serializable, SignatureBytes, Word, MODULUS, N,
SIG_L2_BOUND, ZERO,
};
use crate::utils::string::ToString;
use core::cell::OnceCell;
// FALCON SIGNATURE
// ================================================================================================
@@ -195,12 +196,13 @@ fn decode_nonce(nonce: &NonceBytes) -> NonceElements {
#[cfg(all(test, feature = "std"))]
mod tests {
use core::ffi::c_void;
use rand_utils::rand_vector;
use super::{
super::{ffi::*, Felt, KeyPair},
super::{ffi::*, KeyPair},
*,
};
use libc::c_void;
use rand_utils::rand_vector;
// Wrappers for unsafe functions
impl Rpo128Context {
@@ -234,7 +236,10 @@ mod tests {
fn test_hash_to_point() {
// Create a random message and transform it into a u8 vector
let msg_felts: Word = rand_vector::<Felt>(4).try_into().unwrap();
let msg_bytes = msg_felts.iter().flat_map(|e| e.as_int().to_le_bytes()).collect::<Vec<_>>();
let msg_bytes = msg_felts
.iter()
.flat_map(|e| e.as_int().to_le_bytes())
.collect::<alloc::vec::Vec<_>>();
// Create a nonce i.e. a [u8; 40] array and pack into a [Felt; 8] array.
let nonce: [u8; 40] = rand_vector::<u8>(40).try_into().unwrap();

View File

@@ -1,14 +1,16 @@
use super::{Digest, ElementHasher, Felt, FieldElement, Hasher};
use crate::utils::{
bytes_to_hex_string, hex_to_bytes, string::String, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
};
use alloc::string::String;
use core::{
mem::{size_of, transmute, transmute_copy},
ops::Deref,
slice::from_raw_parts,
};
use super::{Digest, ElementHasher, Felt, FieldElement, Hasher};
use crate::utils::{
bytes_to_hex_string, hex_to_bytes, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
};
#[cfg(test)]
mod tests;

View File

@@ -1,8 +1,9 @@
use super::*;
use crate::utils::collections::Vec;
use proptest::prelude::*;
use rand_utils::rand_vector;
use super::*;
use alloc::vec::Vec;
#[test]
fn blake3_hash_elements() {
// test multiple of 8

View File

@@ -1,7 +1,6 @@
#[cfg(target_feature = "sve")]
pub mod optimized {
use crate::hash::rescue::STATE_WIDTH;
use crate::Felt;
use crate::{hash::rescue::STATE_WIDTH, Felt};
mod ffi {
#[link(name = "rpo_sve", kind = "static")]
@@ -50,8 +49,10 @@ mod x86_64_avx2;
#[cfg(target_feature = "avx2")]
pub mod optimized {
use super::x86_64_avx2::{apply_inv_sbox, apply_sbox};
use crate::hash::rescue::{add_constants, STATE_WIDTH};
use crate::Felt;
use crate::{
hash::rescue::{add_constants, STATE_WIDTH},
Felt,
};
#[inline(always)]
pub fn add_constants_and_apply_sbox(
@@ -80,8 +81,7 @@ pub mod optimized {
#[cfg(not(any(target_feature = "avx2", target_feature = "sve")))]
pub mod optimized {
use crate::hash::rescue::STATE_WIDTH;
use crate::Felt;
use crate::{hash::rescue::STATE_WIDTH, Felt};
#[inline(always)]
pub fn add_constants_and_apply_sbox(

View File

@@ -157,9 +157,10 @@ const fn block3(x: [i64; 3], y: [i64; 3]) -> [i64; 3] {
#[cfg(test)]
mod tests {
use super::super::{apply_mds, Felt, MDS, ZERO};
use proptest::prelude::*;
use super::super::{apply_mds, Felt, MDS, ZERO};
const STATE_WIDTH: usize = 12;
#[inline(always)]

View File

@@ -1,7 +1,8 @@
use core::ops::Range;
use super::{
CubeExtension, Digest, ElementHasher, Felt, FieldElement, Hasher, StarkField, ONE, ZERO,
};
use core::ops::Range;
mod arch;
pub use arch::optimized::{add_constants_and_apply_inv_sbox, add_constants_and_apply_sbox};

View File

@@ -1,10 +1,14 @@
use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::utils::{
bytes_to_hex_string, hex_to_bytes, string::String, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
};
use alloc::string::String;
use core::{cmp::Ordering, fmt::Display, ops::Deref};
use winter_utils::Randomizable;
use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::{
rand::Randomizable,
utils::{
bytes_to_hex_string, hex_to_bytes, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
},
};
// DIGEST TRAIT IMPLEMENTATIONS
// ================================================================================================
@@ -320,10 +324,12 @@ impl IntoIterator for RpoDigest {
#[cfg(test)]
mod tests {
use super::{Deserializable, Felt, RpoDigest, Serializable, DIGEST_BYTES, DIGEST_SIZE};
use crate::utils::{string::String, SliceReader};
use alloc::string::String;
use rand_utils::rand_value;
use super::{Deserializable, Felt, RpoDigest, Serializable, DIGEST_BYTES, DIGEST_SIZE};
use crate::utils::SliceReader;
#[test]
fn digest_serialization() {
let e1 = Felt::new(rand_value());

View File

@@ -1,10 +1,11 @@
use core::ops::Range;
use super::{
add_constants, add_constants_and_apply_inv_sbox, add_constants_and_apply_sbox, apply_inv_sbox,
apply_mds, apply_sbox, Digest, ElementHasher, Felt, FieldElement, Hasher, StarkField, ARK1,
ARK2, BINARY_CHUNK_SIZE, CAPACITY_RANGE, DIGEST_BYTES, DIGEST_RANGE, DIGEST_SIZE, INPUT1_RANGE,
INPUT2_RANGE, MDS, NUM_ROUNDS, ONE, RATE_RANGE, RATE_WIDTH, STATE_WIDTH, ZERO,
};
use core::{convert::TryInto, ops::Range};
mod digest;
pub use digest::RpoDigest;

View File

@@ -1,14 +1,12 @@
use proptest::prelude::*;
use rand_utils::rand_value;
use super::{
super::{apply_inv_sbox, apply_sbox, ALPHA, INV_ALPHA},
Felt, FieldElement, Hasher, Rpo256, RpoDigest, StarkField, ONE, STATE_WIDTH, ZERO,
};
use crate::{
utils::collections::{BTreeSet, Vec},
Word,
};
use core::convert::TryInto;
use proptest::prelude::*;
use rand_utils::rand_value;
use crate::Word;
use alloc::{collections::BTreeSet, vec::Vec};
#[test]
fn test_sbox() {

View File

@@ -1,10 +1,14 @@
use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::utils::{
bytes_to_hex_string, hex_to_bytes, string::String, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
};
use alloc::string::String;
use core::{cmp::Ordering, fmt::Display, ops::Deref};
use winter_utils::Randomizable;
use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::{
rand::Randomizable,
utils::{
bytes_to_hex_string, hex_to_bytes, ByteReader, ByteWriter, Deserializable,
DeserializationError, HexParseError, Serializable,
},
};
// DIGEST TRAIT IMPLEMENTATIONS
// ================================================================================================
@@ -309,10 +313,12 @@ impl Deserializable for RpxDigest {
#[cfg(test)]
mod tests {
use super::{Deserializable, Felt, RpxDigest, Serializable, DIGEST_BYTES, DIGEST_SIZE};
use crate::utils::{string::String, SliceReader};
use alloc::string::String;
use rand_utils::rand_value;
use super::{Deserializable, Felt, RpxDigest, Serializable, DIGEST_BYTES, DIGEST_SIZE};
use crate::utils::SliceReader;
#[test]
fn digest_serialization() {
let e1 = Felt::new(rand_value());

View File

@@ -1,3 +1,5 @@
use core::ops::Range;
use super::{
add_constants, add_constants_and_apply_inv_sbox, add_constants_and_apply_sbox, apply_inv_sbox,
apply_mds, apply_sbox, CubeExtension, Digest, ElementHasher, Felt, FieldElement, Hasher,
@@ -5,7 +7,6 @@ use super::{
DIGEST_SIZE, INPUT1_RANGE, INPUT2_RANGE, MDS, NUM_ROUNDS, RATE_RANGE, RATE_WIDTH, STATE_WIDTH,
ZERO,
};
use core::{convert::TryInto, ops::Range};
mod digest;
pub use digest::RpxDigest;

View File

@@ -1,6 +1,7 @@
use super::{Felt, FieldElement, ALPHA, INV_ALPHA};
use rand_utils::rand_value;
use super::{Felt, FieldElement, ALPHA, INV_ALPHA};
#[test]
fn test_alphas() {
let e: Felt = Felt::new(rand_value());

View File

@@ -1,9 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#[cfg(not(feature = "std"))]
#[cfg_attr(test, macro_use)]
#[macro_use]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
pub mod dsa;
pub mod hash;
pub mod merkle;

View File

@@ -1,3 +1,5 @@
use std::time::Instant;
use clap::Parser;
use miden_crypto::{
hash::rpo::{Rpo256, RpoDigest},
@@ -5,7 +7,6 @@ use miden_crypto::{
Felt, Word, ONE,
};
use rand_utils::rand_value;
use std::time::Instant;
#[derive(Parser, Debug)]
#[clap(name = "Benchmark", about = "SMT benchmark", version, rename_all = "kebab-case")]

View File

@@ -1,6 +1,7 @@
use super::{Felt, RpoDigest, EMPTY_WORD};
use core::slice;
use super::{Felt, RpoDigest, EMPTY_WORD};
// EMPTY NODES SUBTREES
// ================================================================================================

View File

@@ -1,10 +1,7 @@
use crate::{
merkle::{MerklePath, NodeIndex, RpoDigest},
utils::collections::Vec,
};
use alloc::vec::Vec;
use core::fmt;
use super::smt::SmtLeafError;
use super::{smt::SmtLeafError, MerklePath, NodeIndex, RpoDigest};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MerkleError {

View File

@@ -1,6 +1,7 @@
use core::fmt::Display;
use super::{Felt, MerkleError, RpoDigest};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use core::fmt::Display;
// NODE INDEX
// ================================================================================================
@@ -181,9 +182,10 @@ impl Deserializable for NodeIndex {
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
use super::*;
#[test]
fn test_node_index_value_too_high() {
assert_eq!(NodeIndex::new(0, 0).unwrap(), NodeIndex { depth: 0, value: 0 });

View File

@@ -1,8 +1,11 @@
use super::{InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, Vec, Word};
use crate::utils::{string::String, uninit_vector, word_to_hex};
use alloc::{string::String, vec::Vec};
use core::{fmt, ops::Deref, slice};
use winter_math::log2;
use super::{InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, Word};
use crate::utils::{uninit_vector, word_to_hex};
// MERKLE TREE
// ================================================================================================
@@ -283,13 +286,15 @@ pub fn path_to_text(path: &MerklePath) -> Result<String, fmt::Error> {
#[cfg(test)]
mod tests {
use core::mem::size_of;
use proptest::prelude::*;
use super::*;
use crate::{
merkle::{digests_to_words, int_to_leaf, int_to_node, InnerNodeInfo},
Felt, Word, WORD_SIZE,
merkle::{digests_to_words, int_to_leaf, int_to_node},
Felt, WORD_SIZE,
};
use core::mem::size_of;
use proptest::prelude::*;
const LEAVES4: [RpoDigest; WORD_SIZE] =
[int_to_node(1), int_to_node(2), int_to_node(3), int_to_node(4)];

View File

@@ -1,4 +1,5 @@
use super::super::{RpoDigest, Vec};
use super::super::RpoDigest;
use alloc::vec::Vec;
/// Container for the update data of a [super::PartialMmr]
#[derive(Debug)]

View File

@@ -1,9 +1,9 @@
use crate::merkle::MerkleError;
use core::fmt::{Display, Formatter};
#[cfg(feature = "std")]
use std::error::Error;
use crate::merkle::MerkleError;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MmrError {
InvalidPosition(usize),

View File

@@ -11,11 +11,12 @@
//! merged, creating a new tree with depth d+1, this process is continued until the property is
//! reestablished.
use super::{
super::{InnerNodeInfo, MerklePath, Vec},
super::{InnerNodeInfo, MerklePath},
bit::TrueBitPositionIterator,
leaf_to_corresponding_tree, nodes_in_forest, MmrDelta, MmrError, MmrPeaks, MmrProof, Rpo256,
RpoDigest,
};
use alloc::vec::Vec;
// MMR
// ===============================================================================================

View File

@@ -126,9 +126,10 @@ impl From<InOrderIndex> for u64 {
#[cfg(test)]
mod test {
use super::InOrderIndex;
use proptest::prelude::*;
use super::InOrderIndex;
proptest! {
#[test]
fn proptest_inorder_index_random(count in 1..1000usize) {

View File

@@ -1,13 +1,11 @@
use super::{MmrDelta, MmrProof, Rpo256, RpoDigest};
use crate::{
merkle::{
use crate::merkle::{
mmr::{leaf_to_corresponding_tree, nodes_in_forest},
InOrderIndex, InnerNodeInfo, MerklePath, MmrError, MmrPeaks,
},
utils::{
collections::{BTreeMap, BTreeSet, Vec},
vec,
},
};
use alloc::{
collections::{BTreeMap, BTreeSet},
vec::Vec,
};
// TYPE ALIASES
@@ -616,10 +614,11 @@ fn forest_to_rightmost_index(forest: usize) -> InOrderIndex {
#[cfg(test)]
mod tests {
use super::{
forest_to_rightmost_index, forest_to_root_index, BTreeSet, InOrderIndex, MmrPeaks,
PartialMmr, RpoDigest, Vec,
forest_to_rightmost_index, forest_to_root_index, InOrderIndex, MmrPeaks, PartialMmr,
RpoDigest,
};
use crate::merkle::{int_to_node, MerkleStore, Mmr, NodeIndex};
use alloc::{collections::BTreeSet, vec::Vec};
const LEAVES: [RpoDigest; 7] = [
int_to_node(0),

View File

@@ -1,7 +1,5 @@
use super::{
super::{RpoDigest, Vec, ZERO},
Felt, MmrError, MmrProof, Rpo256, Word,
};
use super::{super::ZERO, Felt, MmrError, MmrProof, Rpo256, RpoDigest, Word};
use alloc::vec::Vec;
// MMR PEAKS
// ================================================================================================

View File

@@ -1,5 +1,5 @@
use super::{
super::{InnerNodeInfo, Rpo256, RpoDigest, Vec},
super::{InnerNodeInfo, Rpo256, RpoDigest},
bit::TrueBitPositionIterator,
full::high_bitmask,
leaf_to_corresponding_tree, nodes_in_forest, Mmr, MmrPeaks, PartialMmr,
@@ -8,6 +8,7 @@ use crate::{
merkle::{int_to_node, InOrderIndex, MerklePath, MerkleTree, MmrProof, NodeIndex},
Felt, Word,
};
use alloc::vec::Vec;
#[test]
fn test_position_equal_or_higher_than_leafs_is_never_contained() {
@@ -837,9 +838,10 @@ fn test_mmr_add_invalid_odd_leaf() {
}
mod property_tests {
use super::leaf_to_corresponding_tree;
use proptest::prelude::*;
use super::leaf_to_corresponding_tree;
proptest! {
#[test]
fn test_last_position_is_always_contained_in_the_last_tree(leaves in any::<usize>().prop_filter("cant have an empty tree", |v| *v != 0)) {

View File

@@ -2,7 +2,6 @@
use super::{
hash::rpo::{Rpo256, RpoDigest},
utils::collections::{vec, BTreeMap, BTreeSet, KvMap, RecordingMap, Vec},
Felt, Word, EMPTY_WORD, ZERO,
};
@@ -56,6 +55,6 @@ const fn int_to_leaf(value: u64) -> Word {
}
#[cfg(test)]
fn digests_to_words(digests: &[RpoDigest]) -> Vec<Word> {
fn digests_to_words(digests: &[RpoDigest]) -> alloc::vec::Vec<Word> {
digests.iter().map(|d| d.into()).collect()
}

View File

@@ -1,13 +1,18 @@
use super::{
BTreeMap, BTreeSet, InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest,
ValuePath, Vec, Word, EMPTY_WORD,
};
use crate::utils::{
format, string::String, vec, word_to_hex, ByteReader, ByteWriter, Deserializable,
DeserializationError, Serializable,
use alloc::{
collections::{BTreeMap, BTreeSet},
string::String,
vec::Vec,
};
use core::fmt;
use super::{
InnerNodeInfo, MerkleError, MerklePath, NodeIndex, Rpo256, RpoDigest, ValuePath, Word,
EMPTY_WORD,
};
use crate::utils::{
word_to_hex, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
#[cfg(test)]
mod tests;

View File

@@ -1,10 +1,11 @@
use super::{
super::{
digests_to_words, int_to_node, BTreeMap, DefaultMerkleStore as MerkleStore, MerkleTree,
NodeIndex, PartialMerkleTree,
digests_to_words, int_to_node, DefaultMerkleStore as MerkleStore, MerkleTree, NodeIndex,
PartialMerkleTree,
},
Deserializable, InnerNodeInfo, RpoDigest, Serializable, ValuePath, Vec,
Deserializable, InnerNodeInfo, RpoDigest, Serializable, ValuePath,
};
use alloc::{collections::BTreeMap, vec::Vec};
// TEST DATA
// ================================================================================================

View File

@@ -1,8 +1,11 @@
use crate::Word;
use super::{vec, InnerNodeInfo, MerkleError, NodeIndex, Rpo256, RpoDigest, Vec};
use alloc::vec::Vec;
use core::ops::{Deref, DerefMut};
use winter_utils::{ByteReader, Deserializable, DeserializationError, Serializable};
use super::{InnerNodeInfo, MerkleError, NodeIndex, Rpo256, RpoDigest};
use crate::{
utils::{ByteReader, Deserializable, DeserializationError, Serializable},
Word,
};
// MERKLE PATH
// ================================================================================================
@@ -126,7 +129,7 @@ impl FromIterator<RpoDigest> for MerklePath {
impl IntoIterator for MerklePath {
type Item = RpoDigest;
type IntoIter = vec::IntoIter<RpoDigest>;
type IntoIter = alloc::vec::IntoIter<RpoDigest>;
fn into_iter(self) -> Self::IntoIter {
self.nodes.into_iter()

View File

@@ -1,9 +1,9 @@
use alloc::vec::Vec;
use core::fmt;
use crate::{
hash::rpo::RpoDigest,
merkle::{LeafIndex, SMT_DEPTH},
utils::collections::Vec,
Word,
};

View File

@@ -1,9 +1,8 @@
use alloc::{string::ToString, vec::Vec};
use core::cmp::Ordering;
use crate::utils::{collections::Vec, string::ToString, vec};
use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use super::{Felt, LeafIndex, Rpo256, RpoDigest, SmtLeafError, Word, EMPTY_WORD, SMT_DEPTH};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

View File

@@ -1,11 +1,8 @@
use crate::hash::rpo::Rpo256;
use crate::merkle::{EmptySubtreeRoots, InnerNodeInfo};
use crate::utils::collections::{BTreeMap, BTreeSet};
use crate::{Felt, EMPTY_WORD};
use super::{
InnerNode, LeafIndex, MerkleError, MerklePath, NodeIndex, RpoDigest, SparseMerkleTree, Word,
EmptySubtreeRoots, Felt, InnerNode, InnerNodeInfo, LeafIndex, MerkleError, MerklePath,
NodeIndex, Rpo256, RpoDigest, SparseMerkleTree, Word, EMPTY_WORD,
};
use alloc::collections::{BTreeMap, BTreeSet};
mod error;
pub use error::{SmtLeafError, SmtProofError};

View File

@@ -1,7 +1,6 @@
use crate::utils::string::ToString;
use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use super::{MerklePath, RpoDigest, SmtLeaf, SmtProofError, Word, SMT_DEPTH};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use alloc::string::ToString;
/// A proof which can be used to assert membership (or non-membership) of key-value pairs in a
/// [`super::Smt`].
@@ -23,7 +22,8 @@ impl SmtProof {
/// # Errors
/// Returns an error if the path length is not [`SMT_DEPTH`].
pub fn new(path: MerklePath, leaf: SmtLeaf) -> Result<Self, SmtProofError> {
if path.len() != SMT_DEPTH.into() {
let depth: usize = SMT_DEPTH.into();
if path.len() != depth {
return Err(SmtProofError::InvalidPathLength(path.len()));
}

View File

@@ -1,11 +1,10 @@
use winter_utils::{Deserializable, Serializable};
use super::*;
use super::{Felt, LeafIndex, NodeIndex, Rpo256, RpoDigest, Smt, SmtLeaf, EMPTY_WORD, SMT_DEPTH};
use crate::{
merkle::{EmptySubtreeRoots, MerkleStore},
utils::collections::Vec,
ONE, WORD_SIZE,
utils::{Deserializable, Serializable},
Word, ONE, WORD_SIZE,
};
use alloc::vec::Vec;
// SMT
// --------------------------------------------------------------------------------------------

View File

@@ -1,9 +1,9 @@
use super::{EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex};
use crate::{
hash::rpo::{Rpo256, RpoDigest},
Word,
Felt, Word, EMPTY_WORD,
};
use super::{EmptySubtreeRoots, MerkleError, MerklePath, NodeIndex, Vec};
use alloc::vec::Vec;
mod full;
pub use full::{Smt, SmtLeaf, SmtLeafError, SmtProof, SmtProofError, SMT_DEPTH};

View File

@@ -1,13 +1,9 @@
use crate::{
merkle::{EmptySubtreeRoots, InnerNodeInfo, MerklePath, ValuePath},
EMPTY_WORD,
};
use super::{
InnerNode, LeafIndex, MerkleError, NodeIndex, RpoDigest, SparseMerkleTree, Word, SMT_MAX_DEPTH,
super::ValuePath, EmptySubtreeRoots, InnerNode, InnerNodeInfo, LeafIndex, MerkleError,
MerklePath, NodeIndex, RpoDigest, SparseMerkleTree, Word, EMPTY_WORD, SMT_MAX_DEPTH,
SMT_MIN_DEPTH,
};
use crate::utils::collections::{BTreeMap, BTreeSet};
use alloc::collections::{BTreeMap, BTreeSet};
#[cfg(test)]
mod tests;

View File

@@ -8,9 +8,9 @@ use crate::{
digests_to_words, int_to_leaf, int_to_node, smt::SparseMerkleTree, EmptySubtreeRoots,
InnerNodeInfo, LeafIndex, MerkleTree,
},
utils::collections::Vec,
Word, EMPTY_WORD,
};
use alloc::vec::Vec;
// TEST DATA
// ================================================================================================

View File

@@ -1,11 +1,15 @@
use super::{
mmr::Mmr, BTreeMap, EmptySubtreeRoots, InnerNodeInfo, KvMap, MerkleError, MerklePath,
MerkleTree, NodeIndex, PartialMerkleTree, RecordingMap, RootPath, Rpo256, RpoDigest, SimpleSmt,
Smt, ValuePath, Vec,
};
use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use alloc::{collections::BTreeMap, vec::Vec};
use core::borrow::Borrow;
use super::{
mmr::Mmr, EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, MerkleTree, NodeIndex,
PartialMerkleTree, RootPath, Rpo256, RpoDigest, SimpleSmt, Smt, ValuePath,
};
use crate::utils::{
collections::{KvMap, RecordingMap},
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
#[cfg(test)]
mod tests;

View File

@@ -1,3 +1,5 @@
use seq_macro::seq;
use super::{
DefaultMerkleStore as MerkleStore, EmptySubtreeRoots, MerkleError, MerklePath, NodeIndex,
PartialMerkleTree, RecordingMerkleStore, Rpo256, RpoDigest,
@@ -10,12 +12,11 @@ use crate::{
};
#[cfg(feature = "std")]
use super::{Deserializable, Serializable};
#[cfg(feature = "std")]
use std::error::Error;
use seq_macro::seq;
use {
super::{Deserializable, Serializable},
alloc::boxed::Box,
std::error::Error,
};
// TEST DATA
// ================================================================================================

View File

@@ -1,6 +1,7 @@
//! Pseudo-random element generation.
pub use winter_crypto::{DefaultRandomCoin as WinterRandomCoin, RandomCoin, RandomCoinError};
pub use winter_utils::Randomizable;
use crate::{Felt, FieldElement, Word, ZERO};

View File

@@ -1,12 +1,9 @@
use super::{Felt, FeltRng, FieldElement, Word, ZERO};
use super::{Felt, FeltRng, FieldElement, RandomCoin, RandomCoinError, Word, ZERO};
use crate::{
hash::rpo::{Rpo256, RpoDigest},
utils::{
collections::Vec, string::ToString, vec, ByteReader, ByteWriter, Deserializable,
DeserializationError, Serializable,
},
utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
};
pub use winter_crypto::{RandomCoin, RandomCoinError};
use alloc::{string::ToString, vec::Vec};
// CONSTANTS
// ================================================================================================

View File

@@ -1,8 +1,8 @@
use core::cell::RefCell;
use winter_utils::{
collections::{btree_map::IntoIter, BTreeMap, BTreeSet},
Box,
use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
};
use core::cell::RefCell;
// KEY-VALUE MAP TRAIT
// ================================================================================================
@@ -201,7 +201,7 @@ impl<K: Clone + Ord, V: Clone> FromIterator<(K, V)> for RecordingMap<K, V> {
impl<K: Clone + Ord, V: Clone> IntoIterator for RecordingMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
type IntoIter = alloc::collections::btree_map::IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()

View File

@@ -1,26 +1,21 @@
//! Utilities used in this crate which can also be generally useful downstream.
use super::{utils::string::String, Word};
use alloc::string::String;
use core::fmt::{self, Display, Write};
#[cfg(not(feature = "std"))]
pub use alloc::{format, vec};
#[cfg(feature = "std")]
pub use std::{format, vec};
use super::Word;
mod kv_map;
// RE-EXPORTS
// ================================================================================================
pub use winter_utils::{
string, uninit_vector, Box, ByteReader, ByteWriter, Deserializable, DeserializationError,
Serializable, SliceReader,
boxed, string, uninit_vector, Box, ByteReader, ByteWriter, Deserializable,
DeserializationError, Serializable, SliceReader,
};
pub mod collections {
pub use super::kv_map::*;
pub use winter_utils::collections::*;
}
// UTILITY FUNCTIONS
@@ -99,12 +94,11 @@ pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseErro
});
let mut decoded = [0u8; N];
#[allow(clippy::needless_range_loop)]
for pos in 0..N {
for byte in decoded.iter_mut() {
// These `unwrap` calls are okay because the length was checked above
let high: u8 = data.next().unwrap()?;
let low: u8 = data.next().unwrap()?;
decoded[pos] = (high << 4) + low;
*byte = (high << 4) + low;
}
Ok(decoded)