Browse Source

add folding logic test

main
arnaucube 3 months ago
parent
commit
301c23f6a9
3 changed files with 88 additions and 5 deletions
  1. +3
    -4
      src/fcircuit.rs
  2. +84
    -0
      src/fold_ethdos.rs
  3. +1
    -1
      src/signature.rs

+ 3
- 4
src/fcircuit.rs

@ -70,12 +70,11 @@ where
.ok_or(ark_relations::r1cs::SynthesisError::Unsatisfiable)?; .ok_or(ark_relations::r1cs::SynthesisError::Unsatisfiable)?;
// check that the last signer is signed by the new signer // check that the last signer is signed by the new signer
let ei: SigPkVar<C, GC> = external_inputs.into();
let res = verify::<C, GC>( let res = verify::<C, GC>(
cs.clone(), cs.clone(),
self.config.clone(), self.config.clone(),
ei.pk.clone(),
(ei.sig_r, ei.sig_s),
external_inputs.pk.clone(),
(external_inputs.sig_r, external_inputs.sig_s),
msg.clone(), msg.clone(),
)?; )?;
res.enforce_equal(&Boolean::<F>::TRUE)?; res.enforce_equal(&Boolean::<F>::TRUE)?;
@ -83,7 +82,7 @@ where
// increment the degree // increment the degree
degree = degree.clone() + FpVar::<F>::one(); degree = degree.clone() + FpVar::<F>::one();
let pk_i1_xy = ei.pk.to_constraint_field()?;
let pk_i1_xy = external_inputs.pk.to_constraint_field()?;
Ok(vec![vec![pk_0_x, pk_0_y], pk_i1_xy, vec![degree]].concat()) Ok(vec![vec![pk_0_x, pk_0_y], pk_i1_xy, vec![degree]].concat())
} }
} }

+ 84
- 0
src/fold_ethdos.rs

@ -0,0 +1,84 @@
#[cfg(test)]
mod tests {
use ark_bn254::{Fr, G1Projective as G1};
use ark_ec::AffineRepr;
use ark_grumpkin::Projective as G2;
use ark_std::Zero;
use rand::rngs::OsRng;
use arkeddsa::ed_on_bn254_twist::{constraints::EdwardsVar, EdwardsProjective};
use folding_schemes::{
commitment::pedersen::Pedersen,
folding::nova::{Nova, PreprocessorParam},
frontend::FCircuit,
transcript::poseidon::poseidon_canonical_config,
FoldingScheme,
};
use crate::{
fcircuit::EthDosCircuit,
signature::gen_signatures,
utils::{dbg, elapsed, get_time},
};
#[test]
fn full_flow() {
// set how many steps of folding we want to compute
const N_STEPS: usize = 10;
dbg(format!(
"running Nova folding scheme on EthDosCircuit, with N_STEPS={}",
N_STEPS
));
let mut rng = OsRng;
let poseidon_config = poseidon_canonical_config::<Fr>();
let pks_sigs =
gen_signatures::<OsRng, EdwardsProjective>(&mut rng, &poseidon_config, N_STEPS);
// set the initial state
let xy = pks_sigs[0].pk.0.xy().unwrap();
let pk0 = vec![xy.0, xy.1];
let z_0: Vec<Fr> = vec![pk0.clone(), pk0, vec![Fr::zero()]].concat();
type FC = EthDosCircuit<Fr, EdwardsProjective, EdwardsVar>;
let f_circuit = FC::new(poseidon_config.clone()).unwrap();
// define type aliases for the FoldingScheme (FS) and Decider (D), to avoid writting the
// whole type each time
pub type FS = Nova<G1, G2, FC, Pedersen<G1>, Pedersen<G2>, false>;
// prepare the Nova prover & verifier params
let nova_preprocess_params =
PreprocessorParam::new(poseidon_config.clone(), f_circuit.clone());
let start = get_time();
let nova_params = FS::preprocess(&mut rng, &nova_preprocess_params).unwrap();
dbg(format!("Nova params generated: {:?}", elapsed(start)));
// initialize the folding scheme engine, in our case we use Nova
let mut nova = FS::init(&nova_params, f_circuit, z_0.clone()).unwrap();
// run n steps of the folding iteration
let start_full = get_time();
for i in 0..N_STEPS {
let start = get_time();
nova.prove_step(rng, pks_sigs[i].clone(), None).unwrap();
dbg(format!("Nova::prove_step {}: {:?}", nova.i, elapsed(start)));
}
dbg(format!(
"Nova's all {} steps time: {:?}",
N_STEPS,
elapsed(start_full)
));
// verify the last IVC proof
let ivc_proof = nova.ivc_proof();
dbg!(&ivc_proof.z_i);
FS::verify(
nova_params.1.clone(), // Nova's verifier params
ivc_proof,
)
.unwrap();
}
}

+ 1
- 1
src/signature.rs

@ -8,7 +8,7 @@ use ark_r1cs_std::alloc::{AllocVar, AllocationMode};
use ark_r1cs_std::boolean::Boolean; use ark_r1cs_std::boolean::Boolean;
use ark_r1cs_std::prelude::CurveVar; use ark_r1cs_std::prelude::CurveVar;
use ark_relations::r1cs::{Namespace, SynthesisError}; use ark_relations::r1cs::{Namespace, SynthesisError};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_serialize::CanonicalSerialize;
use ark_std::{rand::Rng, Zero}; use ark_std::{rand::Rng, Zero};
use core::borrow::Borrow; use core::borrow::Borrow;
use rand_core::CryptoRngCore; use rand_core::CryptoRngCore;

Loading…
Cancel
Save