From 4b5f149d904f9736bca4f91e0e0a633ba5bb627b Mon Sep 17 00:00:00 2001 From: Nick Dimitriou Date: Wed, 2 Apr 2025 13:24:44 +0100 Subject: [PATCH] memory test --- examples/mova_matrix_memory.rs | 111 +++++++++++++++++++++++++++++++++ folding-schemes/Cargo.toml | 6 ++ 2 files changed, 117 insertions(+) create mode 100644 examples/mova_matrix_memory.rs diff --git a/examples/mova_matrix_memory.rs b/examples/mova_matrix_memory.rs new file mode 100644 index 000000000..292324dbe --- /dev/null +++ b/examples/mova_matrix_memory.rs @@ -0,0 +1,111 @@ +use ark_crypto_primitives::sponge::poseidon::PoseidonSponge; +use ark_crypto_primitives::sponge::CryptographicSponge; +use std::alloc::{GlobalAlloc}; +use ark_pallas::{Fr, Projective}; +use ark_std::{log2, UniformRand}; +use folding_schemes::commitment::pedersen::Pedersen; +use folding_schemes::commitment::{CommitmentScheme}; +use folding_schemes::folding::nova::nifs::mova_matrix::{RelaxedCommittedRelation, Witness, NIFS}; +use folding_schemes::transcript::poseidon::poseidon_canonical_config; +use folding_schemes::{Curve}; +use matrex::{Matrix}; +use rand::{Rng, RngCore}; +use std::time::{Duration, Instant}; + +const NUM_OF_PRECONDITION_FOLDS: &[usize] = &[1]; + +fn random_sparse_matrix(n: usize, rng: &mut impl RngCore) -> Matrix { + let elements = (0..n) + .map(|row| { + ( + row * n + rand::thread_rng().gen_range(0..n), + C::ScalarField::rand(rng), + ) + }) + .collect(); + Matrix::sparse_from_vec(elements, n, n).unwrap() +} + +// Helper functions +fn get_instances>( + num: usize, + n: usize, + rng: &mut impl RngCore, + params: &CS::ProverParams, +) -> Vec<(Witness, RelaxedCommittedRelation)> { + (0..num) + .map(|_| -> (Witness, RelaxedCommittedRelation) { + // A matrix + let a = random_sparse_matrix::(n, rng); + // B matrix + let b = random_sparse_matrix::(n, rng); + // C = A * B matrix + let c = (&a * &b).unwrap(); + // Error matrix initialized to 0s + let e = Matrix::zero(n, n); + + // Random challenge + let rE = (0..2 * log2(n)) + .map(|_| C::ScalarField::rand(rng)) + .collect(); + // Witness + let witness = Witness::new::(a, b, c, e); + let instance = witness.commit::(params, rE).unwrap(); + (witness, instance) + }) + .collect() +} + +fn bench_mova_matrix() { + let mut rng = ark_std::test_rng(); + let mat_dim = 1 << 12; // 4x4 matrices + println!("mat_dim {}", mat_dim); + + for count in NUM_OF_PRECONDITION_FOLDS { + println!("Starting with pedersen setup"); + + let (pedersen_params, _) = Pedersen::::setup(&mut rng, mat_dim * mat_dim).unwrap(); + let poseidon_config = poseidon_canonical_config::(); + let pp_hash = Fr::rand(&mut rng); + + let mut total_duration = Duration::ZERO; + println!("Starting with gen instances"); + + let mut instances: Vec<(Witness, RelaxedCommittedRelation)> = + get_instances::>( + count + 1, // we want the number of folds plus one for the acc_instance + mat_dim, + &mut rng, + &pedersen_params, + ); + let mut transcript_p = PoseidonSponge::::new(&poseidon_config); + let mut acc = instances.pop().unwrap(); + + for _ in 0..*count { + let mut next = instances.pop().unwrap(); + total_duration += { + let timer = Instant::now(); + println!("Starting with prove"); + + let (wit_acc, inst_acc, _) = + NIFS::, PoseidonSponge>::prove( + &mut transcript_p, + pp_hash, + &mut next.0, + &next.1, + &acc.0, + &acc.1, + ) + .unwrap(); + let time = timer.elapsed(); + acc = (wit_acc, inst_acc); + time + }; + } + println!("Ending"); + } +} + +fn main() { + bench_mova_matrix(); +} diff --git a/folding-schemes/Cargo.toml b/folding-schemes/Cargo.toml index d5827c11f..c68969a28 100644 --- a/folding-schemes/Cargo.toml +++ b/folding-schemes/Cargo.toml @@ -86,3 +86,9 @@ path = "../examples/multi_inputs.rs" [[example]] name = "external_inputs" path = "../examples/external_inputs.rs" + + +[[example]] +name = "mova_matrix" +path = "../examples/mova_matrix_memory.rs" +