From 9e20a08e53561bd8b7fed33ccd32a1d16642bbb8 Mon Sep 17 00:00:00 2001 From: Nick Dimitriou Date: Fri, 26 Jul 2024 18:13:52 +0300 Subject: [PATCH] Removed homogenization and added pointvsline --- examples/folding_benchmarks/bench_utils.rs | 16 +++-- examples/folding_benchmarks/hypernova.rs | 36 +++++------- examples/folding_benchmarks/mova.rs | 33 +++++------ examples/folding_benchmarks/nova.rs | 39 ++++++------- folding-schemes/src/folding/mova/mod.rs | 2 +- folding-schemes/src/folding/mova/nifs.rs | 58 ++++++------------- .../{homogenization.rs => pointvsline.rs} | 48 ++++----------- 7 files changed, 87 insertions(+), 145 deletions(-) rename folding-schemes/src/folding/mova/{homogenization.rs => pointvsline.rs} (86%) diff --git a/examples/folding_benchmarks/bench_utils.rs b/examples/folding_benchmarks/bench_utils.rs index f6c05861..c066fcc2 100644 --- a/examples/folding_benchmarks/bench_utils.rs +++ b/examples/folding_benchmarks/bench_utils.rs @@ -83,16 +83,20 @@ pub fn to_F_vec(z: Vec) -> Vec { } pub fn write_to_csv(pows: &[usize], prove_times: &[Duration], file_path: String) -> Result<(), Box> { - let path = env::current_dir()?.join("").join(file_path); + let path = env::current_dir()?.join("examples/folding_benchmarks").join(file_path); let mut writer = Writer::from_path(path)?; writer.write_record(&["pow", "prove_time"])?; - for (pow, prove_time) in pows.iter().zip(prove_times) { - writer.write_record(&[ - pow.to_string(), - prove_time.as_micros().to_string(), - ])?; + let mut pows_cycle = pows.iter().cycle(); + + for prove_time in prove_times { + if let Some(pow) = pows_cycle.next() { + writer.write_record(&[ + pow.to_string(), + prove_time.as_micros().to_string(), + ])?; + } } writer.flush()?; diff --git a/examples/folding_benchmarks/hypernova.rs b/examples/folding_benchmarks/hypernova.rs index 83725439..dadc84e6 100644 --- a/examples/folding_benchmarks/hypernova.rs +++ b/examples/folding_benchmarks/hypernova.rs @@ -44,7 +44,6 @@ fn hypernova_benchmarks(power: usize, prove_times: &mut Vec) { let poseidon_config = poseidon_canonical_config::(); let mut transcript_p: PoseidonSponge = PoseidonSponge::::new(&poseidon_config); - transcript_p.absorb(&Fr::from_le_bytes_mod_order(b"init init")); let start = Instant::now(); @@ -90,32 +89,29 @@ fn hypernova_benchmarks(power: usize, prove_times: &mut Vec) { fn main() { - println!("starting"); - + // let pows: Vec = (10..24).collect(); let pows: Vec = vec![16, 20]; - println!("{:?}", pows); + let iter = 10; + let mut prove_times: Vec = Vec::with_capacity(pows.len() * iter); + for i in 0..iter { + println!("starting {:}", i); - let mut prove_times: Vec = Vec::with_capacity(pows.len()); - for pow in &pows { - println!("{}", pow); - hypernova_benchmarks(*pow, &mut prove_times); - } - println!("Powers {:?}", pows); + println!("{:?}", pows); - println!("Prove times {:?}", prove_times); - println!( - "| {0: <10} | {1: <10} |", - "2^pow", "prove time" - ); - println!("| {0: <10} | {1: <10} |", "2^pow", "prove time"); - for (pow, prove_time) in pows.iter().zip(prove_times.iter()) { - println!("| {0: <10} | {1:?} |", pow, prove_time); - } - if let Err(e) = write_to_csv(&pows, &prove_times, String::from("hypernova_prove_times.csv")) { + for pow in &pows { + println!("{}", pow); + hypernova_benchmarks(*pow, &mut prove_times); + } + + println!("Powers {:?}", pows); + println!("Prove times {:?}", prove_times); + + } + if let Err(e) = write_to_csv(&pows, &prove_times, format!("hypernova_prove_times.csv")) { eprintln!("Failed to write to CSV: {}", e); } else { println!("CSV file has been successfully written."); diff --git a/examples/folding_benchmarks/mova.rs b/examples/folding_benchmarks/mova.rs index 87ccc33c..46530cb2 100644 --- a/examples/folding_benchmarks/mova.rs +++ b/examples/folding_benchmarks/mova.rs @@ -4,7 +4,6 @@ use ark_std::log2; use ark_std::UniformRand; use folding_schemes::commitment::pedersen::Pedersen; use folding_schemes::commitment::CommitmentScheme; -use folding_schemes::folding::mova::homogenization::{Homogenization, PointVsLineHomogenization}; use folding_schemes::folding::mova::nifs::NIFS; use folding_schemes::folding::mova::Witness; use folding_schemes::transcript::poseidon::{poseidon_canonical_config}; @@ -78,7 +77,6 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec) { Projective, Pedersen, PoseidonSponge, - PointVsLineHomogenization> >::prove( &pedersen_params, &r1cs, @@ -107,7 +105,6 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec) { Projective, Pedersen, PoseidonSponge, - PointVsLineHomogenization> >::verify( &mut transcript_v, &committed_instance_1, @@ -125,30 +122,28 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec) { fn main() { - - - println!("starting"); - // let pows: Vec = (10..24).collect(); let pows: Vec = vec![16, 20]; + let iter = 10; + let mut prove_times: Vec = Vec::with_capacity(pows.len() * iter); + for i in 0..iter { + println!("starting {:}", i); - println!("{:?}", pows); - let mut prove_times: Vec = Vec::with_capacity(pows.len()); - for pow in &pows { - println!("{}", pow); - mova_benchmark(*pow, &mut prove_times); - } + println!("{:?}", pows); - println!("Powers {:?}", pows); - println!("Prove times {:?}", prove_times); - println!("| {0: <10} | {1: <10} |", "2^pow", "prove time"); - for (pow, prove_time) in pows.iter().zip(prove_times.iter()) { - println!("| {0: <10} | {1:?} |", pow, prove_time); - } + for pow in &pows { + println!("{}", pow); + mova_benchmark(*pow, &mut prove_times); + } + + println!("Powers {:?}", pows); + println!("Prove times {:?}", prove_times); + + } if let Err(e) = write_to_csv(&pows, &prove_times, format!("mova_prove_times.csv")) { eprintln!("Failed to write to CSV: {}", e); } else { diff --git a/examples/folding_benchmarks/nova.rs b/examples/folding_benchmarks/nova.rs index 630d5688..c7b640e4 100644 --- a/examples/folding_benchmarks/nova.rs +++ b/examples/folding_benchmarks/nova.rs @@ -45,8 +45,8 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec) { .commit::>(&pedersen_params, x) .unwrap(); - let poseidon_config = poseidon_canonical_config::(); - let mut transcript_p = PoseidonTranscript::::new(&poseidon_config); + let poseidon_config = poseidon_canonical_config::(); + let mut transcript_p: PoseidonSponge = PoseidonSponge::::new(&poseidon_config); let vector = vec![1; size]; // witness_1.E = vector.into_iter().map(|x| Fr::from(x)).collect(); @@ -78,7 +78,7 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec) { let elapsed = start.elapsed(); println!("Time before starting folding {:?}", elapsed); - let result = NIFS::>::fold_instances( + let result = NIFS::>::fold_instances( r, &witness_1, &running_committed_instance, @@ -114,33 +114,28 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec) { fn main() { - println!("starting"); - // let pows: Vec = (10..24).collect(); let pows: Vec = vec![16, 20]; - println!("{:?}", pows); + let iter = 10; + let mut prove_times: Vec = Vec::with_capacity(pows.len() * iter); + for i in 0..iter { + println!("starting {:}", i); - let mut prove_times: Vec = Vec::with_capacity(pows.len()); - for pow in &pows { - println!("{}", pow); - nova_benchmark(*pow, &mut prove_times); - } - println!("Powers {:?}", pows); + println!("{:?}", pows); - println!("Prove times {:?}", prove_times); - println!( - "| {0: <10} | {1: <10} |", - "2^pow", "prove time" - ); - println!("| {0: <10} | {1: <10} |", "2^pow", "prove time"); - for (pow, prove_time) in pows.iter().zip(prove_times.iter()) { - println!("| {0: <10} | {1:?} |", pow, prove_time); - } - if let Err(e) = write_to_csv(&pows, &prove_times, String::from("nova_prove_times.csv")) { + for pow in &pows { + println!("{}", pow); + nova_benchmark(*pow, &mut prove_times); + } + + println!("Powers {:?}", pows); + println!("Prove times {:?}", prove_times); + } + if let Err(e) = write_to_csv(&pows, &prove_times, format!("nova_prove_times.csv")) { eprintln!("Failed to write to CSV: {}", e); } else { println!("CSV file has been successfully written."); diff --git a/folding-schemes/src/folding/mova/mod.rs b/folding-schemes/src/folding/mova/mod.rs index 796cfa10..2e433636 100644 --- a/folding-schemes/src/folding/mova/mod.rs +++ b/folding-schemes/src/folding/mova/mod.rs @@ -22,7 +22,7 @@ use crate::transcript::{AbsorbNonNative, Transcript}; use crate::utils::mle::dense_vec_to_dense_mle; -pub mod homogenization; +pub mod pointvsline; pub mod nifs; pub mod traits; pub mod utils; diff --git a/folding-schemes/src/folding/mova/nifs.rs b/folding-schemes/src/folding/mova/nifs.rs index 39afc037..072a3ba3 100644 --- a/folding-schemes/src/folding/mova/nifs.rs +++ b/folding-schemes/src/folding/mova/nifs.rs @@ -7,8 +7,6 @@ use ark_std::{log2, Zero}; use std::marker::PhantomData; use std::time::Instant; -use super::homogenization::{HomogeneousEvaluationClaim, Homogenization}; - use super::{CommittedInstance, InstanceWitness, Witness}; use crate::arith::r1cs::R1CS; use crate::commitment::CommitmentScheme; @@ -19,11 +17,11 @@ use crate::utils::mle::dense_vec_to_dense_mle; use crate::utils::vec::{hadamard, mat_vec_mul, vec_add, vec_scalar_mul, vec_sub}; use crate::Error; +use crate::folding::mova::pointvsline::{PointVsLine, PointvsLineEvaluationClaim, PointVsLineProof}; /// Proof defines a multifolding proof -#[derive(Clone, Debug)] -pub struct Proof, H: Homogenization> { - pub hg_proof: H::Proof, +pub struct Proof { + pub hg_proof: PointVsLineProof, pub mleE1_prime: C::ScalarField, pub mleE2_prime: C::ScalarField, pub mleT: C::ScalarField, @@ -31,15 +29,14 @@ pub struct Proof, H: Homogenization /// Implements the Non-Interactive Folding Scheme described in section 4 of /// [Nova](https://eprint.iacr.org/2021/370.pdf) -pub struct NIFS, T: Transcript, H: Homogenization> { +pub struct NIFS, T: Transcript, > { _c: PhantomData, _cp: PhantomData, _ct: PhantomData, - _ch: PhantomData, } -impl, T: Transcript, H: Homogenization> -NIFS +impl, T: Transcript, > +NIFS where ::ScalarField: Absorb, { @@ -86,7 +83,7 @@ NIFS Ok(Witness:: { E, W, rW }) } - pub fn fold_homogenized_committed_instance( + pub fn fold_committed_instance( rho: C::ScalarField, ci1: &CommittedInstance, // U_i ci2: &CommittedInstance, // u_i @@ -115,27 +112,6 @@ NIFS }) } - /// NIFS.P is the consecutive combination of compute_cmT with fold_instances - - /// compute_cmT is part of the NIFS.P logic - pub fn compute_cmT( - cs_prover_params: &CS::ProverParams, - r1cs: &R1CS, - w1: &Witness, - ci1: &CommittedInstance, - w2: &Witness, - ci2: &CommittedInstance, - ) -> Result<(Vec, C), Error> { - let z1: Vec = [vec![ci1.u], ci1.x.to_vec(), w1.W.to_vec()].concat(); - let z2: Vec = [vec![ci2.u], ci2.x.to_vec(), w2.W.to_vec()].concat(); - - // compute cross terms - let T = Self::compute_T(r1cs, ci1.u, ci2.u, &z1, &z2)?; - // use r_T=0 since we don't need hiding property for cm(T) - let cmT = CS::commit(cs_prover_params, &T, &C::ScalarField::zero())?; - Ok((T, cmT)) - } - #[allow(clippy::type_complexity)] pub fn prove( _cs_prover_params: &CS::ProverParams, @@ -145,18 +121,18 @@ NIFS ci2: &CommittedInstance, w1: &Witness, w2: &Witness, - ) -> Result<(Proof, InstanceWitness), Error> { + ) -> Result<(Proof, InstanceWitness), Error> { let start = Instant::now(); let elapsed = start.elapsed(); println!("Time before homogenization point-vs-line {:?}", elapsed); let ( hg_proof, - HomogeneousEvaluationClaim { + PointvsLineEvaluationClaim { mleE1_prime, mleE2_prime, rE_prime, }, - ) = H::prove(transcript, ci1, ci2, w1, w2)?; + ) = PointVsLine::::prove(transcript, ci1, ci2, w1, w2)?; let elapsed = start.elapsed(); println!("Time after homogenization point-vs-line {:?}", elapsed); @@ -199,15 +175,15 @@ NIFS let elapsed = start.elapsed(); println!("Time before start folding {:?}", elapsed); - let temp = Ok(( - Proof { + let fold = Ok(( + Proof:: { hg_proof, mleE1_prime, mleE2_prime, mleT: mleT_evaluated, }, InstanceWitness { - ci: Self::fold_homogenized_committed_instance( + ci: Self::fold_committed_instance( rho, ci1, ci2, @@ -221,7 +197,7 @@ NIFS )); let elapsed = start.elapsed(); println!("Time after folding {:?}", elapsed); - temp + fold } /// verify implements NIFS.V logic described in [Nova](https://eprint.iacr.org/2021/370.pdf)'s @@ -230,9 +206,9 @@ NIFS transcript: &mut impl Transcript, ci1: &CommittedInstance, ci2: &CommittedInstance, - proof: &Proof, + proof: &Proof ) -> Result, Error> { - let rE_prime = H::verify( + let rE_prime = PointVsLine::::verify( transcript, ci1, ci2, @@ -249,7 +225,7 @@ NIFS transcript.absorb(&rho_scalar); let rho: C::ScalarField = transcript.get_challenge(); - NIFS::::fold_homogenized_committed_instance( + NIFS::::fold_committed_instance( rho, ci1, ci2, diff --git a/folding-schemes/src/folding/mova/homogenization.rs b/folding-schemes/src/folding/mova/pointvsline.rs similarity index 86% rename from folding-schemes/src/folding/mova/homogenization.rs rename to folding-schemes/src/folding/mova/pointvsline.rs index 2ce5fe95..eea147b7 100644 --- a/folding-schemes/src/folding/mova/homogenization.rs +++ b/folding-schemes/src/folding/mova/pointvsline.rs @@ -26,36 +26,13 @@ use crate::utils::sum_check::SumCheck; use crate::utils::virtual_polynomial::VPAuxInfo; use crate::Error; -pub struct HomogeneousEvaluationClaim { + +pub struct PointvsLineEvaluationClaim { pub mleE1_prime: C::ScalarField, pub mleE2_prime: C::ScalarField, pub rE_prime: Vec, } -pub trait Homogenization> { - type Proof: Clone + Debug; - - fn prove( - transcript: &mut impl Transcript, - ci1: &CommittedInstance, - ci2: &CommittedInstance, - w1: &Witness, - w2: &Witness, - ) -> Result<(Self::Proof, HomogeneousEvaluationClaim), Error>; - - fn verify( - transcript: &mut impl Transcript, - ci1: &CommittedInstance, - ci2: &CommittedInstance, - proof: &Self::Proof, - mleE1_prime: &C::ScalarField, - mleE2_prime: &C::ScalarField, - ) -> Result< - Vec, // rE=rE1'=rE2'. - Error, - >; -} - #[derive(Clone, Debug)] pub struct PointVsLineProof { pub h1: DensePolynomial, @@ -63,24 +40,22 @@ pub struct PointVsLineProof { } #[derive(Clone, Debug, Default)] -pub struct PointVsLineHomogenization> { +pub struct PointVsLine> { _phantom_C: std::marker::PhantomData, _phantom_T: std::marker::PhantomData, } -impl> Homogenization for PointVsLineHomogenization -where +impl> PointVsLine + where ::ScalarField: Absorb, { - type Proof = PointVsLineProof; - - fn prove( + pub fn prove( transcript: &mut impl Transcript, ci1: &CommittedInstance, ci2: &CommittedInstance, w1: &Witness, w2: &Witness, - ) -> Result<(Self::Proof, HomogeneousEvaluationClaim), Error> { + ) -> Result<(PointVsLineProof, PointvsLineEvaluationClaim), Error> { let vars = log2(w1.E.len()) as usize; let mleE1 = dense_vec_to_dense_mle(vars, &w1.E); @@ -112,8 +87,8 @@ where let rE_prime = compute_l(&ci1.rE, &ci2.rE, beta)?; Ok(( - Self::Proof { h1, h2 }, - HomogeneousEvaluationClaim { + PointVsLineProof { h1, h2 }, + PointvsLineEvaluationClaim { mleE1_prime, mleE2_prime, rE_prime, @@ -121,11 +96,11 @@ where )) } - fn verify( + pub fn verify( transcript: &mut impl Transcript, ci1: &CommittedInstance, ci2: &CommittedInstance, - proof: &Self::Proof, + proof: &PointVsLineProof, mleE1_prime: &::ScalarField, mleE2_prime: &::ScalarField, ) -> Result< @@ -159,6 +134,7 @@ where Ok(rE_prime) } + } // TODO: Test this.