Skip to content

Commit

Permalink
chore(fflonk): allocator feature
Browse files Browse the repository at this point in the history
  • Loading branch information
saitima committed Jan 11, 2025
1 parent 64923f3 commit eab05b8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 44 deletions.
5 changes: 3 additions & 2 deletions crates/fflonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "CUDA implementation of the fflonk prover"
exclude = ["/data"]

[dependencies]
fflonk-cpu = {workspace = true}
fflonk-cpu = {workspace = true, optional = true}
circuit_definitions.workspace = true
gpu-ffi.workspace = true
rand = "0.4"
Expand All @@ -24,5 +24,6 @@ serde_json = "1"
serde_derive = "1"

[features]
default = []
default = ["fflonk-cpu"]
sanity = []
allocator = ["fflonk-cpu/allocator"]
9 changes: 4 additions & 5 deletions crates/fflonk/src/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use bellman::{
bn256::{Bn256, Fr},
kate_commitment::{Crs, CrsForMonomialForm},
plonk::{
better_better_cs::cs::{
Circuit, SynthesisModeGenerateSetup, SynthesisModeProve, SynthesisModeTesting,
},
better_better_cs::cs::{Circuit, SynthesisModeProve, SynthesisModeTesting},
commitments::transcript::keccak_transcript::RollingKeccakTranscript,
},
};
Expand All @@ -14,7 +12,7 @@ use circuit_definitions::circuit_definitions::aux_layer::{
};
use fflonk::{FflonkAssembly, L1_VERIFIER_DOMAIN_SIZE_LOG};

pub type FflonkSnarkVerifierCircuitDeviceSetup<A> =
pub type FflonkSnarkVerifierCircuitDeviceSetup<A: HostAllocator = GlobalHost> =
FflonkDeviceSetup<Bn256, FflonkSnarkVerifierCircuit, A>;

use super::*;
Expand Down Expand Up @@ -190,7 +188,8 @@ pub fn precompute_and_save_setup_and_vk_for_fflonk_snark_circuit(

println!("Generating fflonk setup data on the device");
let device_setup =
FflonkSnarkVerifierCircuitDeviceSetup::create_setup_on_device(&circuit).unwrap();
FflonkSnarkVerifierCircuitDeviceSetup::<GlobalHost>::create_setup_on_device(&circuit)
.unwrap();
let setup_file_path = format!("{}/final_snark_device_setup.bin", path);
println!("Saving setup into file {setup_file_path}");
let device_setup_file = std::fs::File::create(&setup_file_path).unwrap();
Expand Down
43 changes: 21 additions & 22 deletions crates/fflonk/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,24 @@ impl<E: Engine, C: Circuit<E>, A: HostAllocator> FflonkDeviceSetup<E, C, A> {
assert_eq!(num_polys, 5);
let mut main_gate_selector_monomials = vec![];
for _ in 0..num_polys {
let num_values = reader.read_u64::<BigEndian>()?;
let mut coeffs = Vec::with_capacity_in(num_values as usize, A::default());
for _ in 0..num_values {
let el = read_fr(&mut reader)?;
coeffs.push(el);
}
let coeffs = read_raw_fr_vec::<_, _, A>(&mut reader)?;
main_gate_selector_monomials.push(coeffs);
}

let num_polys = reader.read_u64::<BigEndian>()?;
assert_eq!(num_polys, 3);
let mut variable_indexes = vec![];
for _ in 0..num_polys {
let num_values = reader.read_u64::<BigEndian>()?;
let mut indexes = Vec::with_capacity_in(num_values as usize, A::default());
for _ in 0..num_values {
let el = reader.read_u32::<BigEndian>()?;
indexes.push(el);
}
let num_values = reader.read_u64::<BigEndian>()? as usize;
let mut indexes = Vec::with_capacity_in(num_values, A::default());
let indexes_buf = unsafe {
indexes.set_len(num_values);
std::slice::from_raw_parts_mut(
indexes.as_mut_ptr() as *mut u8,
num_values * std::mem::size_of::<u32>(),
)
};
reader.read_exact(indexes_buf)?;
variable_indexes.push(indexes);
}

Expand All @@ -293,14 +292,18 @@ impl<E: Engine, C: Circuit<E>, A: HostAllocator> FflonkDeviceSetup<E, C, A> {
use byteorder::{BigEndian, WriteBytesExt};
writer.write_u64::<BigEndian>(self.main_gate_selector_monomials.len() as u64)?;
for mon in self.main_gate_selector_monomials.iter() {
write_fr_vec(&mon, &mut writer)?;
write_raw_fr_slice(&mon, &mut writer)?;
}
writer.write_u64::<BigEndian>(self.variable_indexes.len() as u64)?;
for col in self.variable_indexes.iter() {
writer.write_u64::<BigEndian>(col.len() as u64)?;
for el in col {
writer.write_u32::<BigEndian>(*el)?;
}
let buf = unsafe {
std::slice::from_raw_parts(
col.as_ptr() as *mut u8,
col.len() * std::mem::size_of::<u32>(),
)
};
writer.write_all(buf)?;
}
write_curve_affine(&self.c0_commitment, &mut writer)?;
write_curve_affine(&self.g2_elems[0], &mut writer)?;
Expand All @@ -320,9 +323,7 @@ pub fn read_raw_fr_vec<F: PrimeField, R: std::io::Read, A: Allocator + Default>(
values.as_mut_ptr() as *mut u8,
num_values * std::mem::size_of::<F>(),
);
// src.read_exact(buf)?;
let mut dst = &mut buf[..];
std::io::copy(&mut src, &mut dst)?;
src.read_exact(buf)?;
}

Ok(values)
Expand All @@ -341,9 +342,7 @@ pub fn write_raw_fr_slice<F: PrimeField, W: std::io::Write>(
src_values.as_ptr() as *mut u8,
num_values * std::mem::size_of::<F>(),
);
let mut reader = &buf[..];
// dst.write_all(buf)?;
std::io::copy(&mut reader, &mut dst)?;
dst.write_all(buf)?;
}

Ok(())
Expand Down
14 changes: 6 additions & 8 deletions crates/proof-compression/src/proof_system/boojum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,12 @@ where
let geometry = circuit.geometry();
let (max_trace_len, num_vars) = circuit.size_hint();

let builder_impl =
boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder::<
GoldilocksField,
GoldilocksField,
SetupCSConfig,
>::new(geometry, max_trace_len.unwrap());
let builder =
boojum::cs::cs_builder::new_builder::<_, GoldilocksField>(builder_impl);
let builder_impl = boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder::<
GoldilocksField,
GoldilocksField,
SetupCSConfig,
>::new(geometry, max_trace_len.unwrap());
let builder = boojum::cs::cs_builder::new_builder::<_, GoldilocksField>(builder_impl);

let builder = circuit.configure_builder_proxy(builder);
let mut cs = builder.build(num_vars.unwrap());
Expand Down
3 changes: 2 additions & 1 deletion crates/proof-compression/src/proof_system/fflonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use circuit_definitions::circuit_definitions::aux_layer::ZkSyncSnarkWrapperCircu
use super::*;
pub(crate) use ::fflonk::HostAllocator;
pub(crate) type FflonkSnarkVerifierCircuit = ZkSyncSnarkWrapperCircuitNoLookupCustomGate;
pub(crate) type FflonkSnarkVerifierCircuitVK = FflonkVerificationKey<Bn256, FflonkSnarkVerifierCircuit>;
pub(crate) type FflonkSnarkVerifierCircuitVK =
FflonkVerificationKey<Bn256, FflonkSnarkVerifierCircuit>;
pub(crate) type FflonkSnarkVerifierCircuitProof = FflonkProof<Bn256, FflonkSnarkVerifierCircuit>;
type FflonkAssembly<CSConfig, A> = Assembly<Bn256, PlonkCsWidth3Params, NaiveMainGate, CSConfig, A>;
pub(crate) struct FflonkSnarkWrapper;
Expand Down
3 changes: 1 addition & 2 deletions crates/proof-compression/src/proof_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ pub(crate) use interface::*;
mod plonk;
pub(crate) use plonk::*;


pub(crate) use ::fflonk::fflonk_cpu::franklin_crypto;
pub(crate) use franklin_crypto::bellman;
pub(crate) use franklin_crypto::bellman;
4 changes: 2 additions & 2 deletions crates/proof-compression/src/step/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use franklin_crypto::boojum::cs::{
oracle::TreeHasher,
};

pub(crate)trait CompressionStep: CompressionProofSystem {
pub(crate) trait CompressionStep: CompressionProofSystem {
type PreviousStepTreeHasher: TreeHasher<
GoldilocksField,
Output: serde::Serialize + serde::de::DeserializeOwned,
Expand Down Expand Up @@ -138,7 +138,7 @@ pub(crate)trait CompressionStep: CompressionProofSystem {
) -> CompressionLayerCircuit<Self>;
}

pub(crate)trait CompressionStepExt: CompressionProofSystemExt + CompressionStep {
pub(crate) trait CompressionStepExt: CompressionProofSystemExt + CompressionStep {
fn precomputae_and_store_compression_circuits<BS, CM>(blob_storage: &BS, context_manager: &CM)
where
BS: BlobStorageExt,
Expand Down
4 changes: 2 additions & 2 deletions crates/proof-compression/src/step/snark_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use franklin_crypto::boojum::cs::{

use super::*;

pub(crate)trait SnarkWrapperStep: SnarkWrapperProofSystem {
pub(crate) trait SnarkWrapperStep: SnarkWrapperProofSystem {
const IS_PLONK: bool;
const IS_FFLONK: bool;
const PREVIOUS_COMPRESSION_MODE: u8;
Expand Down Expand Up @@ -153,7 +153,7 @@ pub(crate)trait SnarkWrapperStep: SnarkWrapperProofSystem {
) -> Self::Circuit;
}

pub(crate)trait SnarkWrapperStepExt: SnarkWrapperProofSystemExt + SnarkWrapperStep {
pub(crate) trait SnarkWrapperStepExt: SnarkWrapperProofSystemExt + SnarkWrapperStep {
fn precompute_and_store_snark_wrapper_circuit<BS, CM>(
compact_raw_crs: AsyncHandler<Self::CRS>,
blob_storage: &BS,
Expand Down

0 comments on commit eab05b8

Please sign in to comment.