document, add circuit diagram

This commit is contained in:
2024-12-30 10:35:34 +01:00
parent 28aca46a46
commit 2477471560
11 changed files with 156 additions and 116 deletions

View File

@@ -1,3 +1,4 @@
//! This file contains the FCircuit (Sonobe's trait) implementation for the ETHdos logic.
use ark_crypto_primitives::sponge::{
constraints::CryptographicSpongeVar,
poseidon::{constraints::PoseidonSpongeVar, PoseidonConfig},
@@ -92,6 +93,7 @@ where
Ok([vec![pk_0_x, pk_0_y], pk_i1_xy, vec![degree]].concat())
}
}
#[cfg(test)]
pub mod tests {
use super::*;

View File

@@ -1,84 +0,0 @@
#[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();
}
}

View File

@@ -1,4 +1,4 @@
//! This file contains the WASM bindings.
//! This file contains the WASM bindings, and at the bottom a test running the full flow.
//!
#![allow(non_snake_case)]
#![allow(dead_code)]
@@ -28,16 +28,15 @@ use crate::signature::{gen_signatures, SigPk};
use crate::utils::{dbg, elapsed, get_time};
mod fcircuit;
mod fold_ethdos;
mod signature;
mod utils;
use wasm_bindgen::prelude::*;
// define type aliases for the FCircuit (FC) and the FoldingScheme (FS), to avoid writting the
// whole type each time.
// define type aliases for the FCircuit (FC) and the FoldingScheme (FS), to avoid writing the whole
// type each time.
type FC = EthDosCircuit<Fr, EdwardsProjective, EdwardsVar>;
type FS = Nova<G1, G2, FC, Pedersen<G1>, Pedersen<G2>, false>;
type FS = Nova<G1, G2, FC, Pedersen<G1>, Pedersen<G2>>;
#[wasm_bindgen]
extern "C" {
@@ -207,3 +206,89 @@ pub fn verify_proof(verifier_params: String, ivc_proof: String) -> String {
.unwrap();
"verified".to_string()
}
#[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 showing a full-execution example.
#[test]
fn test_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 writing 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();
}
}

View File

@@ -1,3 +1,5 @@
//! This file is just some helper methods on top of https://github.com/kilic/arkeddsa in order to
//! use the Signature and PublicKey as ExternalInputs in the FCircuit.
use ark_crypto_primitives::sponge::{
poseidon::{PoseidonConfig, PoseidonSponge},
Absorb, CryptographicSponge,

View File

@@ -1,17 +1,6 @@
#[cfg(target_arch = "wasm32")]
use web_sys::console;
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
pub fn dbg(s: String) {
#[cfg(target_arch = "wasm32")]
console::log_1(&s.into());
@@ -49,9 +38,20 @@ pub fn elapsed(start: u64) -> u64 {
#[cfg(target_arch = "wasm32")]
fn get_wasm_time() -> u64 {
use web_sys::window;
let window = window().expect("should have a window in this context");
let window = window().expect("no window");
let performance = window
.performance()
.expect("performance should be available");
.expect("window.performance() not found");
performance.now() as u64
}
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}