Browse Source

small polishing

reduce-memory-usage
arnaucube 3 months ago
parent
commit
b77cb532cf
3 changed files with 31 additions and 29 deletions
  1. +15
    -14
      src/keccak_chain.rs
  2. +3
    -3
      src/naive_approach_sha_chain.rs
  3. +13
    -12
      src/sha_chain.rs

+ 15
- 14
src/keccak_chain.rs

@ -57,7 +57,7 @@ mod tests {
#[test] #[test]
fn full_flow() { fn full_flow() {
// set how many steps of folding we want to compute // set how many steps of folding we want to compute
let n_steps = 1000;
let n_steps = 50;
// set the initial state // set the initial state
let z_0_aux: Vec<u32> = vec![0_u32; 32 * 8]; let z_0_aux: Vec<u32> = vec![0_u32; 32 * 8];
@ -93,8 +93,9 @@ mod tests {
assert!(cs.is_satisfied().unwrap()); assert!(cs.is_satisfied().unwrap());
// ---------------- // ----------------
// define type aliases to avoid writting the whole type each time
pub type N =
// define type aliases for the FoldingScheme (FS) and Decider (D), to avoid writting the
// whole type each time
pub type FS =
Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>; Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
pub type D = DeciderEth< pub type D = DeciderEth<
G1, G1,
@ -105,7 +106,7 @@ mod tests {
KZG<'static, Bn254>, KZG<'static, Bn254>,
Pedersen<G2>, Pedersen<G2>,
Groth16<Bn254>, Groth16<Bn254>,
N,
FS,
>; >;
let poseidon_config = poseidon_canonical_config::<Fr>(); let poseidon_config = poseidon_canonical_config::<Fr>();
@ -114,16 +115,11 @@ mod tests {
// prepare the Nova prover & verifier params // prepare the Nova prover & verifier params
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone()); let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
let start = Instant::now(); let start = Instant::now();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = FS::preprocess(&mut rng, &nova_preprocess_params).unwrap();
println!("Nova params generated: {:?}", start.elapsed()); println!("Nova params generated: {:?}", start.elapsed());
// initialize the folding scheme engine, in our case we use Nova // initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit.clone(), z_0.clone()).unwrap();
// prepare the Decider prover & verifier params
let start = Instant::now();
let (decider_pp, decider_vp) = D::preprocess(&mut rng, &nova_params, nova.clone()).unwrap();
println!("Decider params generated: {:?}", start.elapsed());
let mut nova = FS::init(&nova_params, f_circuit.clone(), z_0.clone()).unwrap();
// run n steps of the folding iteration // run n steps of the folding iteration
let start_full = Instant::now(); let start_full = Instant::now();
@ -145,7 +141,7 @@ mod tests {
// perform the hash chain natively in rust (which uses a rust Keccak256 library) // perform the hash chain natively in rust (which uses a rust Keccak256 library)
let mut z_i_native = z_0.clone(); let mut z_i_native = z_0.clone();
for i in 0..n_steps { for i in 0..n_steps {
z_i_native = rust_native_step(i, z_i_native.clone(), vec![]).unwrap();
z_i_native = rust_native_step(i, z_i_native, vec![]).unwrap();
} }
// check that the value of the last folding state (nova.z_i) computed through folding, is // check that the value of the last folding state (nova.z_i) computed through folding, is
// equal to the natively computed hash using the rust_native_step method // equal to the natively computed hash using the rust_native_step method
@ -156,8 +152,8 @@ mod tests {
// The following lines contain a sanity check that checks the IVC proof (before going into // The following lines contain a sanity check that checks the IVC proof (before going into
// the zkSNARK proof) // the zkSNARK proof)
let (running_instance, incoming_instance, cyclefold_instance) = nova.instances(); let (running_instance, incoming_instance, cyclefold_instance) = nova.instances();
N::verify(
nova_params.1, // Nova's verifier params
FS::verify(
nova_params.1.clone(), // Nova's verifier params
z_0, z_0,
nova.z_i.clone(), nova.z_i.clone(),
nova.i, nova.i,
@ -168,6 +164,11 @@ mod tests {
.unwrap(); .unwrap();
// ---------------- // ----------------
// prepare the Decider prover & verifier params
let start = Instant::now();
let (decider_pp, decider_vp) = D::preprocess(&mut rng, &nova_params, nova.clone()).unwrap();
println!("Decider params generated: {:?}", start.elapsed());
let rng = rand::rngs::OsRng; let rng = rand::rngs::OsRng;
let start = Instant::now(); let start = Instant::now();
let proof = D::prove(rng, decider_pp, nova.clone()).unwrap(); let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();

+ 3
- 3
src/naive_approach_sha_chain.rs

@ -85,7 +85,7 @@ mod tests {
fn full_flow() { fn full_flow() {
// set how many iterations of the SHA256ChainCircuit circuit internal loop we want to // set how many iterations of the SHA256ChainCircuit circuit internal loop we want to
// compute // compute
const N_STEPS: usize = 50;
const N_STEPS: usize = 100;
const HASHES_PER_STEP: usize = 10; const HASHES_PER_STEP: usize = 10;
println!("running the 'naive' SHA256ChainCircuit, with N_STEPS={}, HASHES_PER_STEP={}. Total hashes = {}", N_STEPS, HASHES_PER_STEP, N_STEPS* HASHES_PER_STEP); println!("running the 'naive' SHA256ChainCircuit, with N_STEPS={}, HASHES_PER_STEP={}. Total hashes = {}", N_STEPS, HASHES_PER_STEP, N_STEPS* HASHES_PER_STEP);
@ -105,8 +105,8 @@ mod tests {
let cs = ConstraintSystem::<Fr>::new_ref(); let cs = ConstraintSystem::<Fr>::new_ref();
circuit.clone().generate_constraints(cs.clone()).unwrap(); circuit.clone().generate_constraints(cs.clone()).unwrap();
println!( println!(
"number of constraints of the (naive) SHA256ChainCircuit with N={} hash iterations: {}",
N_STEPS,
"number of constraints of the (naive) SHA256ChainCircuit with N_STEPS*HASHES_PER_STEP={} sha256 hashes in total: {}",
N_STEPS * HASHES_PER_STEP,
cs.num_constraints() cs.num_constraints()
); );

+ 13
- 12
src/sha_chain.rs

@ -145,8 +145,9 @@ mod tests {
); );
// ---------------- // ----------------
// define type aliases to avoid writting the whole type each time
pub type N = Nova<
// define type aliases for the FoldingScheme (FS) and Decider (D), to avoid writting the
// whole type each time
pub type FS = Nova<
G1, G1,
GVar, GVar,
G2, G2,
@ -165,7 +166,7 @@ mod tests {
KZG<'static, Bn254>, KZG<'static, Bn254>,
Pedersen<G2>, Pedersen<G2>,
Groth16<Bn254>, Groth16<Bn254>,
N,
FS,
>; >;
let poseidon_config = poseidon_canonical_config::<Fr>(); let poseidon_config = poseidon_canonical_config::<Fr>();
@ -174,16 +175,11 @@ mod tests {
// prepare the Nova prover & verifier params // prepare the Nova prover & verifier params
let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit); let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit);
let start = Instant::now(); let start = Instant::now();
let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
let nova_params = FS::preprocess(&mut rng, &nova_preprocess_params).unwrap();
println!("Nova params generated: {:?}", start.elapsed()); println!("Nova params generated: {:?}", start.elapsed());
// initialize the folding scheme engine, in our case we use Nova // initialize the folding scheme engine, in our case we use Nova
let mut nova = N::init(&nova_params, f_circuit, z_0.clone()).unwrap();
// prepare the Decider prover & verifier params
let start = Instant::now();
let (decider_pp, decider_vp) = D::preprocess(&mut rng, &nova_params, nova.clone()).unwrap();
println!("Decider params generated: {:?}", start.elapsed());
let mut nova = FS::init(&nova_params, f_circuit, z_0.clone()).unwrap();
// run n steps of the folding iteration // run n steps of the folding iteration
let start_full = Instant::now(); let start_full = Instant::now();
@ -207,8 +203,8 @@ mod tests {
// The following lines contain a sanity check that checks the IVC proof (before going into // The following lines contain a sanity check that checks the IVC proof (before going into
// the zkSNARK proof) // the zkSNARK proof)
let (running_instance, incoming_instance, cyclefold_instance) = nova.instances(); let (running_instance, incoming_instance, cyclefold_instance) = nova.instances();
N::verify(
nova_params.1, // Nova's verifier params
FS::verify(
nova_params.1.clone(), // Nova's verifier params
z_0, z_0,
nova.z_i.clone(), nova.z_i.clone(),
nova.i, nova.i,
@ -219,6 +215,11 @@ mod tests {
.unwrap(); .unwrap();
// ---------------- // ----------------
// prepare the Decider prover & verifier params
let start = Instant::now();
let (decider_pp, decider_vp) = D::preprocess(&mut rng, &nova_params, nova.clone()).unwrap();
println!("Decider params generated: {:?}", start.elapsed());
let rng = rand::rngs::OsRng; let rng = rand::rngs::OsRng;
let start = Instant::now(); let start = Instant::now();
let proof = D::prove(rng, decider_pp, nova.clone()).unwrap(); let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();

Loading…
Cancel
Save