Skip to content

Commit 84be6f9

Browse files
committed
feat(proof-compression): faster CRS loading with raw values
1 parent f5aff39 commit 84be6f9

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed

crates/proof-compression/src/proof_system.rs

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ use circuit_definitions::circuit_definitions::aux_layer::compression::{
3434

3535
use fflonk::bellman::plonk::better_better_cs::cs::Circuit;
3636
use fflonk::fflonk::L1_VERIFIER_DOMAIN_SIZE_LOG;
37-
use fflonk::{init_compact_crs, CombinedMonomialDeviceStorage, DeviceContextWithSingleDevice};
37+
use fflonk::{CombinedMonomialDeviceStorage, DeviceContextWithSingleDevice};
3838
use gpu_prover::{DeviceMemoryManager, ManagerConfigs};
3939
use shivini::gpu_proof_config::GpuProofConfig;
40-
use shivini::{cs::GpuSetup, GpuTreeHasher, ProverContext};
4140
use shivini::{
4241
CacheStrategy, CommitmentCacheStrategy, GPUPoWRunner, PolynomialsCacheStrategy,
4342
ProverContextConfig,
4443
};
44+
use shivini::{GpuTreeHasher, ProverContext};
4545
pub trait ProofSystemDefinition: Sized {
4646
type FieldElement;
4747
type ExternalWitnessData;
@@ -160,7 +160,7 @@ where
160160
{
161161
type FieldElement = GoldilocksField;
162162
type ExternalWitnessData = WitnessVec<Self::FieldElement, Self::Allocator>;
163-
type Precomputation = GpuSetup<CF::ThisLayerHasher>;
163+
type Precomputation = BoojumDeviceSetupWrapper<CF::ThisLayerHasher>;
164164
type Proof = Proof<Self::FieldElement, CF::ThisLayerHasher, GoldilocksExt2>;
165165
type VK = VerificationKey<Self::FieldElement, CF::ThisLayerHasher>;
166166
type FinalizationHint = FinalizationHintsForProver;
@@ -277,7 +277,7 @@ where
277277
commitment: CommitmentCacheStrategy::CacheCosetCaps,
278278
};
279279
let worker = Worker::new();
280-
let precomputation = precomputation.wait();
280+
let precomputation = precomputation.wait().into_inner();
281281
let ctx = ctx.wait();
282282
let gpu_proof = shivini::gpu_prove_from_external_witness_data_with_cache_strategy::<
283283
CF::ThisLayerTranscript,
@@ -338,7 +338,7 @@ where
338338
>(setup_base, vk_params, vars_hint, wits_hint, &worker)
339339
.unwrap();
340340
drop(ctx);
341-
(precomputation, vk)
341+
(BoojumDeviceSetupWrapper::from_inner(precomputation), vk)
342342
}
343343
fn synthesize_for_setup(
344344
circuit: CompressionLayerCircuit<Self>,
@@ -406,7 +406,11 @@ impl ProofSystemDefinition for PlonkSnarkWrapper {
406406
}
407407

408408
impl SnarkWrapperProofSystem for PlonkSnarkWrapper {
409-
type CRS = bellman::kate_commitment::Crs<bellman::compact_bn256::Bn256, CrsForMonomialForm>;
409+
type CRS = bellman::kate_commitment::Crs<
410+
bellman::compact_bn256::Bn256,
411+
CrsForMonomialForm,
412+
Self::Allocator,
413+
>;
410414
type Circuit = PlonkSnarkVerifierCircuit;
411415
type ContextConfig = (Vec<usize>, Vec<CompactG1Affine>);
412416
type Context = UnsafePlonkProverDeviceMemoryManagerWrapper;
@@ -508,18 +512,21 @@ impl ProofSystemDefinition for FflonkSnarkWrapper {
508512
}
509513

510514
impl SnarkWrapperProofSystem for FflonkSnarkWrapper {
511-
type CRS = bellman::kate_commitment::Crs<bellman::compact_bn256::Bn256, CrsForMonomialForm>;
515+
type CRS = bellman::kate_commitment::Crs<
516+
bellman::compact_bn256::Bn256,
517+
CrsForMonomialForm,
518+
Self::Allocator,
519+
>;
512520
type Circuit = FflonkSnarkVerifierCircuit;
513521
type ContextConfig = (usize, Self::CRS);
514522
type Context = DeviceContextWithSingleDevice;
515523

516524
fn load_crs(domain_size: Self::FinalizationHint) -> Self::CRS {
517-
// let raw_compact_crs_file_path = std::env::var("FFLONK_COMPACT_RAW_CRS_FILE").unwrap();
518-
// let raw_compact_crs_file = std::fs::File::open(raw_compact_crs_file_path).unwrap();
519-
// let num_points = domain_size * fflonk::MAX_COMBINED_DEGREE_FACTOR;
520-
// read_crs_from_raw_compact_form::<_, Self::Allocator>(raw_compact_crs_file, num_points)
521-
// .unwrap()
522-
init_compact_crs(&bellman::worker::Worker::new(), domain_size)
525+
let raw_compact_crs_file_path = std::env::var("FFLONK_COMPACT_RAW_CRS_FILE").unwrap();
526+
let raw_compact_crs_file = std::fs::File::open(raw_compact_crs_file_path).unwrap();
527+
let num_points = domain_size * fflonk::MAX_COMBINED_DEGREE_FACTOR;
528+
read_crs_from_raw_compact_form::<_, Self::Allocator>(raw_compact_crs_file, num_points)
529+
.unwrap()
523530
}
524531

525532
fn get_context_config() -> Self::ContextConfig {
@@ -531,7 +538,6 @@ impl SnarkWrapperProofSystem for FflonkSnarkWrapper {
531538
fn init_context(config: Self::ContextConfig) -> Self::Context {
532539
let (domain_size, crs) = config;
533540
let context = Self::Context::init_from_preloaded_crs(domain_size, crs).unwrap();
534-
535541
context
536542
}
537543

@@ -624,7 +630,7 @@ impl SnarkWrapperProofSystemExt for FflonkSnarkWrapper {
624630
&raw_compact_crs_file_path
625631
)));
626632
let raw_compact_crs_file = std::fs::File::create(raw_compact_crs_file_path).unwrap();
627-
write_crs_into_raw_compact_form(original_crs, raw_compact_crs_file, num_points).unwrap();
633+
write_crs_into_raw_compact_form(&original_crs, raw_compact_crs_file, num_points).unwrap();
628634
}
629635
}
630636

@@ -649,7 +655,7 @@ impl ProofSystemDefinition for MarkerProofSystem {
649655
}
650656

651657
pub fn write_crs_into_raw_compact_form<W: std::io::Write>(
652-
original_crs: Crs<bellman::bn256::Bn256, CrsForMonomialForm>,
658+
original_crs: &Crs<bellman::bn256::Bn256, CrsForMonomialForm>,
653659
mut dst_raw_compact_crs: W,
654660
num_points: usize,
655661
) -> std::io::Result<()> {
@@ -660,15 +666,16 @@ pub fn write_crs_into_raw_compact_form<W: std::io::Write>(
660666
dst_raw_compact_crs.write_u32::<BigEndian>(num_points as u32)?;
661667
for g1_base in original_crs.g1_bases.iter() {
662668
let (x, y) = g1_base.as_xy();
663-
x.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
664-
y.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
669+
x.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
670+
y.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
665671
}
672+
assert_eq!(original_crs.g2_monomial_bases.len(), 2);
666673
for g2_base in original_crs.g2_monomial_bases.iter() {
667674
let (x, y) = g2_base.as_xy();
668-
x.c0.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
669-
x.c1.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
670-
y.c0.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
671-
y.c1.into_raw_repr().write_be(&mut dst_raw_compact_crs)?;
675+
x.c0.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
676+
x.c1.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
677+
y.c0.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
678+
y.c1.into_raw_repr().write_le(&mut dst_raw_compact_crs)?;
672679
}
673680

674681
Ok(())
@@ -677,28 +684,22 @@ pub fn write_crs_into_raw_compact_form<W: std::io::Write>(
677684
// TODO: Crs doesn't allow bases located in a custom allocator
678685
pub fn read_crs_from_raw_compact_form<R: std::io::Read, A: Allocator + Default>(
679686
mut src_raw_compact_crs: R,
680-
num_points: usize,
681-
) -> std::io::Result<Crs<bellman::compact_bn256::Bn256, CrsForMonomialForm>> {
682-
println!("Reading Raw compact CRS");
687+
num_g1_points: usize,
688+
) -> std::io::Result<Crs<bellman::compact_bn256::Bn256, CrsForMonomialForm, A>> {
683689
use byteorder::{BigEndian, ReadBytesExt};
684690
let actual_num_points = src_raw_compact_crs.read_u32::<BigEndian>()? as usize;
685-
assert!(num_points <= actual_num_points as usize);
686-
use bellman::{PrimeField, PrimeFieldRepr};
687-
// let mut g1_bases = Vec::with_capacity_in(num_points, A::default());
688-
println!("Reading G1 points");
689-
let mut g1_bases = Vec::with_capacity(num_points);
691+
assert!(num_g1_points <= actual_num_points as usize);
692+
let mut g1_bases = Vec::with_capacity_in(num_g1_points, A::default());
690693
unsafe {
691-
g1_bases.set_len(num_points);
694+
g1_bases.set_len(num_g1_points);
692695
let buf = std::slice::from_raw_parts_mut(
693696
g1_bases.as_mut_ptr() as *mut u8,
694-
num_points * std::mem::size_of::<bellman::compact_bn256::G1Affine>(),
697+
num_g1_points * std::mem::size_of::<bellman::compact_bn256::G1Affine>(),
695698
);
696699
src_raw_compact_crs.read_exact(buf)?;
697700
}
698701
let num_g2_points = 2;
699-
// let mut g2_bases = Vec::with_capacity_in(num_g2_points, A::default());
700-
println!("Reading G2 points");
701-
let mut g2_bases = Vec::with_capacity(num_g2_points);
702+
let mut g2_bases = Vec::with_capacity_in(num_g2_points, A::default());
702703
unsafe {
703704
g2_bases.set_len(num_g2_points);
704705
let buf = std::slice::from_raw_parts_mut(
@@ -707,9 +708,5 @@ pub fn read_crs_from_raw_compact_form<R: std::io::Read, A: Allocator + Default>(
707708
);
708709
src_raw_compact_crs.read_exact(buf)?;
709710
}
710-
let mut compact_crs = Crs::<_, CrsForMonomialForm>::dummy_crs(1);
711-
compact_crs.g1_bases = std::sync::Arc::new(g1_bases);
712-
compact_crs.g2_monomial_bases = std::sync::Arc::new(g2_bases);
713-
714-
Ok(compact_crs)
711+
Ok(Crs::<_, CrsForMonomialForm, A>::new_in(g1_bases, g2_bases))
715712
}

crates/proof-compression/src/serialization.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@ use shivini::{
66
GpuTreeHasher,
77
};
88

9+
pub trait SerializationWrapper: Sized {
10+
type Inner;
11+
fn into_inner(self) -> Self::Inner;
12+
fn from_inner(inner: Self::Inner) -> Self;
13+
}
14+
915
use crate::PlonkSnarkVerifierCircuitDeviceSetup;
1016

1117
pub struct BoojumDeviceSetupWrapper<H: GpuTreeHasher>(GpuSetup<H>);
18+
impl<H> SerializationWrapper for BoojumDeviceSetupWrapper<H>
19+
where
20+
H: GpuTreeHasher,
21+
{
22+
type Inner = GpuSetup<H>;
23+
fn into_inner(self) -> Self::Inner {
24+
self.0
25+
}
26+
27+
fn from_inner(inner: Self::Inner) -> Self {
28+
Self(inner)
29+
}
30+
}
1231

1332
impl<H: GpuTreeHasher> boojum::cs::implementations::fast_serialization::MemcopySerializable
1433
for BoojumDeviceSetupWrapper<H>
@@ -17,7 +36,7 @@ impl<H: GpuTreeHasher> boojum::cs::implementations::fast_serialization::MemcopyS
1736
&self,
1837
dst: W,
1938
) -> Result<(), Box<dyn std::error::Error>> {
20-
Ok(bincode::serialize_into(dst, self.0).unwrap())
39+
Ok(bincode::serialize_into(dst, &self.0).unwrap())
2140
}
2241

2342
fn read_from_buffer<R: std::io::Read>(src: R) -> Result<Self, Box<dyn std::error::Error>> {
@@ -45,10 +64,15 @@ impl MemcopySerializable for PlonkSnarkVerifierCircuitDeviceSetupWrapper {
4564
}
4665
}
4766

48-
impl PlonkSnarkVerifierCircuitDeviceSetupWrapper {
49-
pub fn into_inner(self) -> PlonkSnarkVerifierCircuitDeviceSetup {
67+
impl SerializationWrapper for PlonkSnarkVerifierCircuitDeviceSetupWrapper {
68+
type Inner = PlonkSnarkVerifierCircuitDeviceSetup;
69+
fn into_inner(self) -> Self::Inner {
5070
self.0
5171
}
72+
73+
fn from_inner(inner: Self::Inner) -> Self {
74+
Self(inner)
75+
}
5276
}
5377

5478
pub struct FflonkSnarkVerifierCircuitDeviceSetupWrapper<A: HostAllocator>(
@@ -73,13 +97,18 @@ where
7397
}
7498
}
7599

76-
impl<A> FflonkSnarkVerifierCircuitDeviceSetupWrapper<A>
100+
impl<A> SerializationWrapper for FflonkSnarkVerifierCircuitDeviceSetupWrapper<A>
77101
where
78102
A: HostAllocator,
79103
{
80-
pub fn into_inner(self) -> fflonk::FflonkDeviceSetup<Bn256, FflonkSnarkVerifierCircuit, A> {
104+
type Inner = fflonk::FflonkDeviceSetup<Bn256, FflonkSnarkVerifierCircuit, A>;
105+
fn into_inner(self) -> Self::Inner {
81106
self.0
82107
}
108+
109+
fn from_inner(inner: Self::Inner) -> Self {
110+
Self(inner)
111+
}
83112
}
84113

85114
#[derive(serde::Serialize, serde::Deserialize)]

0 commit comments

Comments
 (0)