Skip to content

Commit

Permalink
Removed homogenization and added pointvsline
Browse files Browse the repository at this point in the history
  • Loading branch information
NiDimi committed Jul 26, 2024
1 parent 64db0cb commit 9e20a08
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 145 deletions.
16 changes: 10 additions & 6 deletions examples/folding_benchmarks/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ pub fn to_F_vec<F: PrimeField>(z: Vec<BigUint>) -> Vec<F> {
}

pub fn write_to_csv(pows: &[usize], prove_times: &[Duration], file_path: String) -> Result<(), Box<dyn Error>> {
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()?;
Expand Down
36 changes: 16 additions & 20 deletions examples/folding_benchmarks/hypernova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ fn hypernova_benchmarks(power: usize, prove_times: &mut Vec<Duration>) {
let poseidon_config = poseidon_canonical_config::<Fr>();

let mut transcript_p: PoseidonSponge<Fr> = PoseidonSponge::<Fr>::new(&poseidon_config);
transcript_p.absorb(&Fr::from_le_bytes_mod_order(b"init init"));

let start = Instant::now();

Expand Down Expand Up @@ -90,32 +89,29 @@ fn hypernova_benchmarks(power: usize, prove_times: &mut Vec<Duration>) {


fn main() {
println!("starting");

// let pows: Vec<usize> = (10..24).collect();
let pows: Vec<usize> = vec![16, 20];
println!("{:?}", pows);
let iter = 10;
let mut prove_times: Vec<Duration> = Vec::with_capacity(pows.len() * iter);
for i in 0..iter {
println!("starting {:}", i);

let mut prove_times: Vec<Duration> = 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.");
Expand Down
33 changes: 14 additions & 19 deletions examples/folding_benchmarks/mova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -78,7 +77,6 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {
Projective,
Pedersen<Projective>,
PoseidonSponge<Fr>,
PointVsLineHomogenization<Projective, PoseidonSponge<Fr>>
>::prove(
&pedersen_params,
&r1cs,
Expand Down Expand Up @@ -107,7 +105,6 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {
Projective,
Pedersen<Projective>,
PoseidonSponge<Fr>,
PointVsLineHomogenization<Projective, PoseidonSponge<Fr>>
>::verify(
&mut transcript_v,
&committed_instance_1,
Expand All @@ -125,30 +122,28 @@ fn mova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {


fn main() {


println!("starting");

// let pows: Vec<usize> = (10..24).collect();
let pows: Vec<usize> = vec![16, 20];
let iter = 10;
let mut prove_times: Vec<Duration> = Vec::with_capacity(pows.len() * iter);
for i in 0..iter {
println!("starting {:}", i);

println!("{:?}", pows);

let mut prove_times: Vec<Duration> = 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 {
Expand Down
39 changes: 17 additions & 22 deletions examples/folding_benchmarks/nova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {
.commit::<Pedersen<Projective>>(&pedersen_params, x)
.unwrap();

let poseidon_config = poseidon_canonical_config::<ark_pallas::Fr>();
let mut transcript_p = PoseidonTranscript::<Projective>::new(&poseidon_config);
let poseidon_config = poseidon_canonical_config::<Fr>();
let mut transcript_p: PoseidonSponge<Fr> = PoseidonSponge::<Fr>::new(&poseidon_config);
let vector = vec![1; size];
//
witness_1.E = vector.into_iter().map(|x| Fr::from(x)).collect();
Expand Down Expand Up @@ -78,7 +78,7 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {
let elapsed = start.elapsed();
println!("Time before starting folding {:?}", elapsed);

let result = NIFS::<G1, Pedersen<G1>>::fold_instances(
let result = NIFS::<Projective, Pedersen<Projective>>::fold_instances(
r,
&witness_1,
&running_committed_instance,
Expand Down Expand Up @@ -114,33 +114,28 @@ fn nova_benchmark(power: usize, prove_times: &mut Vec<Duration>) {


fn main() {
println!("starting");

// let pows: Vec<usize> = (10..24).collect();
let pows: Vec<usize> = vec![16, 20];
println!("{:?}", pows);
let iter = 10;
let mut prove_times: Vec<Duration> = Vec::with_capacity(pows.len() * iter);
for i in 0..iter {
println!("starting {:}", i);

let mut prove_times: Vec<Duration> = 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.");
Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/mova/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 17 additions & 41 deletions folding-schemes/src/folding/mova/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,27 +17,26 @@ 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<C: CurveGroup, T: Transcript<C::ScalarField>, H: Homogenization<C, T>> {
pub hg_proof: H::Proof,
pub struct Proof<C: CurveGroup, > {
pub hg_proof: PointVsLineProof<C>,
pub mleE1_prime: C::ScalarField,
pub mleE2_prime: C::ScalarField,
pub mleT: C::ScalarField,
}

/// Implements the Non-Interactive Folding Scheme described in section 4 of
/// [Nova](https://eprint.iacr.org/2021/370.pdf)
pub struct NIFS<C: CurveGroup, CS: CommitmentScheme<C>, T: Transcript<C::ScalarField>, H: Homogenization<C, T>> {
pub struct NIFS<C: CurveGroup, CS: CommitmentScheme<C>, T: Transcript<C::ScalarField>, > {
_c: PhantomData<C>,
_cp: PhantomData<CS>,
_ct: PhantomData<T>,
_ch: PhantomData<H>,
}

impl<C: CurveGroup, CS: CommitmentScheme<C>, T: Transcript<C::ScalarField>, H: Homogenization<C, T>>
NIFS<C, CS, T, H>
impl<C: CurveGroup, CS: CommitmentScheme<C>, T: Transcript<C::ScalarField>, >
NIFS<C, CS, T>
where
<C as Group>::ScalarField: Absorb,
{
Expand Down Expand Up @@ -86,7 +83,7 @@ NIFS<C, CS, T, H>
Ok(Witness::<C> { E, W, rW })
}

pub fn fold_homogenized_committed_instance(
pub fn fold_committed_instance(
rho: C::ScalarField,
ci1: &CommittedInstance<C>, // U_i
ci2: &CommittedInstance<C>, // u_i
Expand Down Expand Up @@ -115,27 +112,6 @@ NIFS<C, CS, T, H>
})
}

/// 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<C::ScalarField>,
w1: &Witness<C>,
ci1: &CommittedInstance<C>,
w2: &Witness<C>,
ci2: &CommittedInstance<C>,
) -> Result<(Vec<C::ScalarField>, C), Error> {
let z1: Vec<C::ScalarField> = [vec![ci1.u], ci1.x.to_vec(), w1.W.to_vec()].concat();
let z2: Vec<C::ScalarField> = [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,
Expand All @@ -145,18 +121,18 @@ NIFS<C, CS, T, H>
ci2: &CommittedInstance<C>,
w1: &Witness<C>,
w2: &Witness<C>,
) -> Result<(Proof<C, T, H>, InstanceWitness<C>), Error> {
) -> Result<(Proof<C>, InstanceWitness<C>), 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::<C, T>::prove(transcript, ci1, ci2, w1, w2)?;
let elapsed = start.elapsed();
println!("Time after homogenization point-vs-line {:?}", elapsed);

Expand Down Expand Up @@ -199,15 +175,15 @@ NIFS<C, CS, T, H>

let elapsed = start.elapsed();
println!("Time before start folding {:?}", elapsed);
let temp = Ok((
Proof {
let fold = Ok((
Proof::<C> {
hg_proof,
mleE1_prime,
mleE2_prime,
mleT: mleT_evaluated,
},
InstanceWitness {
ci: Self::fold_homogenized_committed_instance(
ci: Self::fold_committed_instance(
rho,
ci1,
ci2,
Expand All @@ -221,7 +197,7 @@ NIFS<C, CS, T, H>
));
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
Expand All @@ -230,9 +206,9 @@ NIFS<C, CS, T, H>
transcript: &mut impl Transcript<C::ScalarField>,
ci1: &CommittedInstance<C>,
ci2: &CommittedInstance<C>,
proof: &Proof<C, T, H>,
proof: &Proof<C,>
) -> Result<CommittedInstance<C>, Error> {
let rE_prime = H::verify(
let rE_prime = PointVsLine::<C, T>::verify(
transcript,
ci1,
ci2,
Expand All @@ -249,7 +225,7 @@ NIFS<C, CS, T, H>
transcript.absorb(&rho_scalar);
let rho: C::ScalarField = transcript.get_challenge();

NIFS::<C, CS, T, H>::fold_homogenized_committed_instance(
NIFS::<C, CS, T>::fold_committed_instance(
rho,
ci1,
ci2,
Expand Down
Loading

0 comments on commit 9e20a08

Please sign in to comment.