mirror of
https://github.com/arnaucube/sonobe-playground.git
synced 2026-01-13 17:41:28 +01:00
Nova folding
This commit is contained in:
5491
Cargo.lock
generated
5491
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
12
Cargo.toml
@@ -5,3 +5,15 @@ edition = "2021"
|
|||||||
authors = ["Piotr Mikołajczyk <piomiko41@gmail.com>"]
|
authors = ["Piotr Mikołajczyk <piomiko41@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
ark-bn254 = { version = "^0.4.0", features = ["r1cs"] }
|
||||||
|
ark-grumpkin = { version = "0.4.0", features = ["r1cs"] }
|
||||||
|
ark-groth16 = { version = "0.4.0", features = ["parallel"] }
|
||||||
|
ark-serialize = { version = "0.4.0" }
|
||||||
|
rand = { version = "0.8.5" }
|
||||||
|
sonobe = { git = "https://github.com/privacy-scaling-explorations/sonobe", rev = "f1d82418ba047cf90805f2d0505370246df24d68", package = "folding-schemes" }
|
||||||
|
num-traits = "0.2.15"
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
ark-r1cs-std = { git = "https://github.com/winderica/r1cs-std", branch = "cherry-pick" }
|
||||||
|
ark-bn254 = { git = "https://github.com/arnaucube/ark-curves-cherry-picked", branch = "cherry-pick" }
|
||||||
|
ark-grumpkin = { git = "https://github.com/arnaucube/ark-curves-cherry-picked", branch = "cherry-pick" }
|
||||||
|
|||||||
BIN
circuit/input.bin
Normal file
BIN
circuit/input.bin
Normal file
Binary file not shown.
21
src/circuit.rs
Normal file
21
src/circuit.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use std::env::current_dir;
|
||||||
|
use ark_bn254::Fr;
|
||||||
|
use sonobe::frontend::circom::CircomFCircuit;
|
||||||
|
use sonobe::frontend::FCircuit;
|
||||||
|
|
||||||
|
const IVC_STEP_WIDTH: usize = 2;
|
||||||
|
const STEP_INPUT_WIDTH: usize = 256;
|
||||||
|
|
||||||
|
pub fn create_circuit() -> CircomFCircuit<Fr> {
|
||||||
|
let root = current_dir().expect("Failed to get current directory");
|
||||||
|
let circuit_file = root.join("circuit/grayscale_step.r1cs");
|
||||||
|
let witness_generator_file = root.join("circuit/grayscale_step_js/grayscale_step.wasm");
|
||||||
|
|
||||||
|
let f_circuit_params = (
|
||||||
|
circuit_file.into(),
|
||||||
|
witness_generator_file.into(),
|
||||||
|
IVC_STEP_WIDTH,
|
||||||
|
STEP_INPUT_WIDTH,
|
||||||
|
);
|
||||||
|
CircomFCircuit::<Fr>::new(f_circuit_params).expect("Failed to create circuit")
|
||||||
|
}
|
||||||
25
src/folding.rs
Normal file
25
src/folding.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use ark_bn254::{Bn254, constraints::GVar, Fr, G1Projective as G1};
|
||||||
|
use ark_grumpkin::{constraints::GVar as GVar2, Projective as G2};
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
use sonobe::{
|
||||||
|
commitment::{kzg::KZG, pedersen::Pedersen},
|
||||||
|
folding::nova::{Nova, PreprocessorParam},
|
||||||
|
FoldingScheme,
|
||||||
|
frontend::circom::CircomFCircuit,
|
||||||
|
transcript::poseidon::poseidon_canonical_config,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type NovaFolding =
|
||||||
|
Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
|
||||||
|
|
||||||
|
pub fn prepare_folding(
|
||||||
|
circuit: &CircomFCircuit<Fr>,
|
||||||
|
start_ivc_state: Vec<Fr>,
|
||||||
|
rng: &mut OsRng,
|
||||||
|
) -> NovaFolding {
|
||||||
|
let nova_preprocess_params =
|
||||||
|
PreprocessorParam::new(poseidon_canonical_config::<Fr>(), circuit.clone());
|
||||||
|
let nova_params = NovaFolding::preprocess(&mut *rng, &nova_preprocess_params)
|
||||||
|
.expect("Failed to preprocess Nova");
|
||||||
|
NovaFolding::init(&nova_params, circuit.clone(), start_ivc_state).expect("Failed to init Nova")
|
||||||
|
}
|
||||||
14
src/input.rs
Normal file
14
src/input.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
use std::{fs::File, io::Read};
|
||||||
|
|
||||||
|
use ark_bn254::Fr;
|
||||||
|
use ark_serialize::CanonicalDeserialize;
|
||||||
|
|
||||||
|
pub fn prepare_input() -> Vec<Vec<Fr>> {
|
||||||
|
let mut file = File::open("circuit/input.bin").expect("Failed to open input file");
|
||||||
|
let mut buffer = Vec::new();
|
||||||
|
file.read_to_end(&mut buffer)
|
||||||
|
.expect("Failed to read input file");
|
||||||
|
|
||||||
|
CanonicalDeserialize::deserialize_uncompressed(&mut buffer.as_slice())
|
||||||
|
.expect("Failed to deserialize input")
|
||||||
|
}
|
||||||
38
src/main.rs
38
src/main.rs
@@ -1,3 +1,37 @@
|
|||||||
fn main() {
|
use std::time::Instant;
|
||||||
println!("Hello, world!");
|
|
||||||
|
use ark_bn254::Fr;
|
||||||
|
use num_traits::identities::Zero;
|
||||||
|
use sonobe::FoldingScheme;
|
||||||
|
|
||||||
|
use crate::{circuit::create_circuit, folding::prepare_folding, input::prepare_input};
|
||||||
|
|
||||||
|
fn measure<T, Action: FnOnce() -> T>(action_name: &str, action: Action) -> T {
|
||||||
|
let start = Instant::now();
|
||||||
|
let result = action();
|
||||||
|
println!("{action_name}: {:?}", start.elapsed());
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
mod circuit;
|
||||||
|
mod folding;
|
||||||
|
mod input;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut rng = rand::rngs::OsRng;
|
||||||
|
|
||||||
|
let circuit = measure("Prepare circuit", create_circuit);
|
||||||
|
|
||||||
|
let start_ivc_state = vec![Fr::zero(); 2];
|
||||||
|
let mut folding = measure("Prepare folding", || {
|
||||||
|
prepare_folding(&circuit, start_ivc_state, &mut rng)
|
||||||
|
});
|
||||||
|
|
||||||
|
for (i, external_inputs_at_step) in prepare_input()[..5].iter().enumerate() {
|
||||||
|
measure(&format!("Nova::prove_step {i}"), || {
|
||||||
|
folding
|
||||||
|
.prove_step(rng, external_inputs_at_step.clone(), None)
|
||||||
|
.expect("Failed to prove step")
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user