Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Messages sealed as one type must be unsealed using the corresponding method, oth
- `RpoRandomCoin`: a struct implementing `FeltRng` as well as the [`RandomCoin`](https://github.com/facebook/winterfell/blob/main/crypto/src/random/mod.rs) trait using RPO hash function.
- `RpxRandomCoin`: a struct implementing `FeltRng` as well as the [`RandomCoin`](https://github.com/facebook/winterfell/blob/main/crypto/src/random/mod.rs) trait using RPX hash function.

## STARK proving system

The STARK module exports foundational components for the STARK proving system. It primarily consists of re-exports from the [Plonky3](https://github.com/Plonky3/Plonky3) project with some Miden-specific [adaptations](https://github.com/0xMiden/p3-miden).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should expand this quite a bit in the future. Let's add it to the list of follow-ups.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few possible additional re-exports were mentioned here:
#720 (comment)

But the re-exports we have as of 7ba721b are rather extensive.


## Make commands

We use `make` to automate building, testing, and other processes. In most cases, `make` commands are wrappers around `cargo` commands with specific arguments. You can view the list of available commands in the [Makefile](Makefile), or run the following command:
Expand Down
6 changes: 3 additions & 3 deletions miden-crypto/benches/common/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::iter;

use miden_crypto::{
Felt, ONE, Word,
test_utils::{prng_array, rand_value},
rand::test_utils::{prng_array, rand_value},
};

// === Byte Array Generation ===
Expand Down Expand Up @@ -81,11 +81,11 @@ pub fn generate_word(seed: &mut [u8; 32]) -> Word {
}

/// Generate a generic value from seed using PRNG
pub fn generate_value<T: miden_crypto::utils::Randomizable + std::fmt::Debug + Clone>(
pub fn generate_value<T: miden_crypto::rand::Randomizable + std::fmt::Debug + Clone>(
seed: &mut [u8; 32],
) -> T {
*seed = prng_array(*seed);
let value: [T; 1] = miden_crypto::test_utils::prng_array(*seed);
let value: [T; 1] = miden_crypto::rand::test_utils::prng_array(*seed);
value[0].clone()
}

Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/benches/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use miden_crypto::{
smt::{LeafIndex, SMT_MAX_DEPTH, SimpleSmt},
store::MerkleStore,
},
test_utils::{rand_array, rand_value},
rand::test_utils::{rand_array, rand_value},
};

/// Since MerkleTree can only be created when a power-of-two number of elements is used, the sample
Expand Down
5 changes: 3 additions & 2 deletions miden-crypto/src/dsa/falcon512_rpo/math/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,12 @@ impl<F: Zeroize> ZeroizeOnDrop for Polynomial<F> {}
#[cfg(all(test, feature = "std"))]
mod tests {
use super::{FalconFelt, N, Polynomial};
use crate::rand::test_utils::rand_array;

#[test]
fn test_negacyclic_reduction() {
let coef1: [u8; N] = crate::test_utils::rand_array();
let coef2: [u8; N] = crate::test_utils::rand_array();
let coef1: [u8; N] = rand_array();
let coef2: [u8; N] = rand_array();

let poly1 = Polynomial::new(coef1.iter().map(|&a| FalconFelt::new(a as i16)).collect());
let poly2 = Polynomial::new(coef2.iter().map(|&a| FalconFelt::new(a as i16)).collect());
Expand Down
12 changes: 0 additions & 12 deletions miden-crypto/src/hash/algebraic_sponge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ use crate::{BasedVectorSpace, PrimeField64};
pub(crate) mod poseidon2;
pub(crate) mod rescue;

// Re-export the main hash function types
pub use poseidon2::Poseidon2;
// Re-export P3 integration types for public API
pub use poseidon2::{
Poseidon2Challenger, Poseidon2Compression, Poseidon2Hasher, Poseidon2Permutation256,
};
pub use rescue::{
Rpo256, Rpx256,
rpo::{RpoChallenger, RpoCompression, RpoHasher, RpoPermutation256},
rpx::{RpxChallenger, RpxCompression, RpxHasher, RpxPermutation256},
};

// CONSTANTS
// ================================================================================================

Expand Down
3 changes: 0 additions & 3 deletions miden-crypto/src/hash/algebraic_sponge/rescue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ mod mds;
use mds::{MDS, apply_mds};

pub(crate) mod rpo;
pub use rpo::Rpo256;

pub(crate) mod rpx;
pub use rpx::Rpx256;

#[cfg(test)]
mod tests;
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/hash/algebraic_sponge/rescue/rpo/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
use crate::{
ONE, PrimeCharacteristicRing, PrimeField64, Word, ZERO,
hash::algebraic_sponge::{AlgebraicSponge, BINARY_CHUNK_SIZE, CAPACITY_RANGE, RATE_WIDTH},
test_utils::rand_value,
rand::test_utils::rand_value,
};

#[test]
Expand Down
4 changes: 3 additions & 1 deletion miden-crypto/src/hash/algebraic_sponge/rescue/rpx/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use p3_field::PrimeField64;
use proptest::prelude::*;

use super::{Felt, Rpx256};
use crate::{ONE, Word, ZERO, hash::algebraic_sponge::AlgebraicSponge, test_utils::rand_value};
use crate::{
ONE, Word, ZERO, hash::algebraic_sponge::AlgebraicSponge, rand::test_utils::rand_value,
};

// The number of iterations to run the `ext_round_matches_reference_many` test.
#[cfg(all(
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/hash/algebraic_sponge/rescue/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use p3_field::PrimeCharacteristicRing;

use super::{ALPHA, Felt, INV_ALPHA};
use crate::test_utils::rand_value;
use crate::rand::test_utils::rand_value;

#[test]
fn test_alphas() {
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/hash/blake/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use p3_miden_goldilocks::Goldilocks as Felt;
use proptest::prelude::*;

use super::*;
use crate::test_utils::rand_vector;
use crate::rand::test_utils::rand_vector;

#[test]
fn blake3_hash_elements() {
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/hash/keccak/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;
use proptest::prelude::*;

use super::*;
use crate::test_utils::rand_vector;
use crate::rand::test_utils::rand_vector;

#[test]
fn keccak256_hash_elements() {
Expand Down
17 changes: 13 additions & 4 deletions miden-crypto/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ pub mod sha2;

/// Poseidon2 hash function.
pub mod poseidon2 {
pub use super::algebraic_sponge::poseidon2::Poseidon2;
pub use p3_miden_goldilocks::Poseidon2Goldilocks;

pub use super::algebraic_sponge::poseidon2::{
Poseidon2, Poseidon2Challenger, Poseidon2Compression, Poseidon2Hasher,
Poseidon2Permutation256,
};
}

/// Rescue Prime Optimized (RPO) hash function.
pub mod rpo {
pub use super::algebraic_sponge::rescue::Rpo256;
pub use super::algebraic_sponge::rescue::rpo::{
Rpo256, RpoChallenger, RpoCompression, RpoHasher,
};
}

/// Rescue Prime Extended (RPX) hash function.
pub mod rpx {
pub use super::algebraic_sponge::rescue::Rpx256;
pub use super::algebraic_sponge::rescue::rpx::{
Rpx256, RpxChallenger, RpxCompression, RpxHasher,
};
}

pub mod algebraic_sponge;
mod algebraic_sponge;

// TRAITS
// ================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/hash/sha2/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;
use proptest::prelude::*;

use super::*;
use crate::test_utils::rand_vector;
use crate::rand::test_utils::rand_vector;

// SHA-256 TESTS
// ================================================================================================
Expand Down
50 changes: 29 additions & 21 deletions miden-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,44 @@ pub mod rand;
pub mod utils;
pub mod word;

// Test utilities for generating random data (used in tests and benchmarks)
#[cfg(any(test, feature = "std"))]
pub mod test_utils;

// RE-EXPORTS
// ================================================================================================

pub use k256::elliptic_curve::zeroize;
pub use p3_air::{
Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, BaseAirWithPublicValues,
ExtensionBuilder, FilteredAirBuilder, PairBuilder, PairCol, PermutationAirBuilder,
VirtualPairCol,
};
pub use p3_field::{
BasedVectorSpace, ExtensionField, Field, PrimeCharacteristicRing, PrimeField64,
batch_multiplicative_inverse, extension::BinomialExtensionField, integers::QuotientMap,
};
pub use p3_miden_air::{BaseAirWithAuxTrace, FilteredMidenAirBuilder, MidenAir, MidenAirBuilder};
pub use p3_miden_goldilocks::{Goldilocks as Felt, Poseidon2Goldilocks};
pub use p3_miden_prover::{
Commitments, Domain, Entry, OpenedValues, PackedChallenge, PackedVal, PcsError, Proof,
ProverConstraintFolder, StarkConfig, StarkGenericConfig, SymbolicAirBuilder,
SymbolicExpression, SymbolicVariable, Val, VerificationError, VerifierConstraintFolder,
generate_logup_trace, get_log_quotient_degree, get_max_constraint_degree,
get_symbolic_constraints, prove, quotient_values, recompose_quotient_from_chunks, verify,
verify_constraints,
extension::BinomialExtensionField, integers::QuotientMap,
};
pub use p3_miden_goldilocks::Goldilocks as Felt;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the longer run, I wonder if we should create a newtype for this - i.e.:

pub struct Felt(p3_miden_goldilocks::Goldilocks);

And implement a bunch of "pass-through" methods in it directly so that we don't have to import various traits when we need to get specific functionality.

pub use word::{Word, WordError};

pub use crate::rand::{Randomizable, RpoRandomCoin, RpxRandomCoin};
pub mod stark {
//! Foundational components for the STARK proving system based on Plonky3.
//!
//! This module contains components needed to build a STARK prover/verifier and define
//! Algebraic Intermediate Representation (AIR) for the Miden VM and other components.
//! It primarily consists of re-exports from the Plonky3 project with some Miden-specific
//! adaptations.
pub use p3_miden_prover::{
Commitments, Domain, Entry, OpenedValues, PackedChallenge, PackedVal, PcsError, Proof,
ProverConstraintFolder, StarkConfig, StarkGenericConfig, SymbolicAirBuilder,
SymbolicExpression, SymbolicVariable, Val, VerificationError, VerifierConstraintFolder,
generate_logup_trace, get_log_quotient_degree, get_max_constraint_degree,
get_symbolic_constraints, prove, quotient_values, recompose_quotient_from_chunks, verify,
verify_constraints,
};

pub mod air {
pub use p3_air::{
Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, BaseAirWithPublicValues,
ExtensionBuilder, FilteredAirBuilder, PairBuilder, PairCol, PermutationAirBuilder,
VirtualPairCol,
};
pub use p3_miden_air::{
BaseAirWithAuxTrace, FilteredMidenAirBuilder, MidenAir, MidenAirBuilder,
};
}
}
Comment on lines +35 to +61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my stab at how I think the module should be organized, but we can change this later in case another structure is more logical.

Also, same as last comment: we should expand this in the future to provide a more comprehensive explanation of what this module is and how the provided functionality is different from stock Plonky3. This would include adding comments for the air submodule and to many structs/functions here that are currently missing comments. Let's note this in the same issue.


// TYPE ALIASES
// ================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use miden_crypto::{
EMPTY_WORD, Felt, ONE, Word,
hash::rpo::Rpo256,
merkle::smt::{LargeSmt, LargeSmtError, MemoryStorage, SmtStorage},
test_utils::rand_value,
rand::test_utils::rand_value,
};
use rand::{Rng, prelude::IteratorRandom, rng};

Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/merkle/smt/large_forest/history/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{CompactLeaf, History, LeafChanges, NodeChanges, error::Result};
use crate::{
Felt, Word,
merkle::{NodeIndex, smt::LeafIndex},
test_utils::rand_value,
rand::test_utils::rand_value,
};

// TESTS
Expand Down
2 changes: 1 addition & 1 deletion miden-crypto/src/merkle/smt/partial/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use assert_matches::assert_matches;

use super::{PartialSmt, SMT_DEPTH};
#[cfg(any(test, feature = "std"))]
use crate::test_utils::{rand_array, rand_value};
use crate::rand::test_utils::{rand_array, rand_value};
use crate::{
EMPTY_WORD, Felt, ONE, Word, ZERO,
merkle::{
Expand Down
4 changes: 4 additions & 0 deletions miden-crypto/src/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub use rpo::RpoRandomCoin;
mod rpx;
pub use rpx::RpxRandomCoin;

// Test utilities for generating random data (used in tests and benchmarks)
#[cfg(any(test, feature = "std"))]
pub mod test_utils;

// RANDOMNESS (ported from Winterfell's winter-utils)
// ================================================================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use alloc::vec::Vec;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;

use crate::utils::Randomizable;
use crate::rand::Randomizable;

/// Generates a random value of type T using the thread-local random number generator.
///
/// # Examples
/// ```
/// # use miden_crypto::test_utils::rand_value;
/// # use miden_crypto::rand::test_utils::rand_value;
/// let x: u64 = rand_value();
/// let y: u128 = rand_value();
/// ```
Expand All @@ -31,7 +31,7 @@ pub fn rand_value<T: Randomizable>() -> T {
///
/// # Examples
/// ```
/// # use miden_crypto::test_utils::rand_array;
/// # use miden_crypto::rand::test_utils::rand_array;
/// let arr: [u64; 4] = rand_array();
/// ```
#[cfg(feature = "std")]
Expand All @@ -43,7 +43,7 @@ pub fn rand_array<T: Randomizable, const N: usize>() -> [T; N] {
///
/// # Examples
/// ```
/// # use miden_crypto::test_utils::rand_vector;
/// # use miden_crypto::rand::test_utils::rand_vector;
/// let vec: Vec<u64> = rand_vector(100);
/// ```
#[cfg(feature = "std")]
Expand All @@ -58,7 +58,7 @@ pub fn rand_vector<T: Randomizable>(length: usize) -> Vec<T> {
///
/// # Examples
/// ```
/// # use miden_crypto::test_utils::prng_array;
/// # use miden_crypto::rand::test_utils::prng_array;
/// let seed = [0u8; 32];
/// let arr: [u64; 4] = prng_array(seed);
/// ```
Expand All @@ -75,7 +75,7 @@ pub fn prng_array<T: Randomizable, const N: usize>(seed: [u8; 32]) -> [T; N] {
///
/// # Examples
/// ```
/// # use miden_crypto::test_utils::prng_vector;
/// # use miden_crypto::rand::test_utils::prng_vector;
/// let seed = [0u8; 32];
/// let vec: Vec<u64> = prng_vector(seed, 100);
/// ```
Expand Down
10 changes: 5 additions & 5 deletions miden-crypto/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ pub fn hex_to_bytes<const N: usize>(value: &str) -> Result<[u8; N], HexParseErro
Ok(decoded)
}

// MATH UTILITIES
// ================================================================================================

pub use p3_field::batch_multiplicative_inverse;

// CONVERSIONS BETWEEN BYTES AND ELEMENTS
// ================================================================================================

Expand Down Expand Up @@ -354,8 +359,3 @@ pub fn transpose_slice<T: Copy + Send + Sync, const N: usize>(source: &[T]) -> V
});
result
}

// RANDOMNESS (ported from Winterfell's winter-utils)
// ================================================================================================

pub use crate::rand::Randomizable;
2 changes: 1 addition & 1 deletion miden-crypto/src/word/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::string::String;
use p3_field::PrimeCharacteristicRing;

use super::{Deserializable, Felt, Serializable, WORD_SIZE_BYTES, WORD_SIZE_FELT, Word};
use crate::{test_utils::rand_value, utils::SliceReader, word};
use crate::{rand::test_utils::rand_value, utils::SliceReader, word};

// TESTS
// ================================================================================================
Expand Down
Loading