Skip to content

Commit 19978bc

Browse files
committed
feat(proof-compression): implement proof systems
1 parent 480c0cc commit 19978bc

File tree

11 files changed

+716
-646
lines changed

11 files changed

+716
-646
lines changed

crates/proof-compression/src/artifacts.rs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,44 @@ use std::io::Read;
33
use boojum::cs::implementations::fast_serialization::MemcopySerializable;
44
use boojum::cs::implementations::setup::FinalizationHintsForProver;
55
use boojum::cs::traits::gate::FinalizationHintSerialized;
6-
use circuit_definitions::circuit_definitions::aux_layer::compression_modes::CompressionTreeHasherForWrapper;
7-
use circuit_definitions::circuit_definitions::aux_layer::{
8-
CompressionProofsTreeHasher, ZkSyncCompressionVerificationKey,
9-
};
6+
use circuit_definitions::circuit_definitions::aux_layer::ZkSyncCompressionVerificationKey;
107
use circuit_definitions::circuit_definitions::recursion_layer::ZkSyncRecursionVerificationKey;
11-
use shivini::cs::GpuSetup as BoojumDeviceSetup;
128

139
use super::*;
1410

1511
pub trait BlobStorage {
16-
fn save<T>(artifact: T)
12+
fn save<W>(data: W)
1713
where
18-
T: MemcopySerializable;
14+
W: std::io::Write;
1915
}
2016

2117
pub struct AsyncHandler<T> {
22-
precomputation: std::sync::mpsc::Receiver<T>, // This is indeed a receiver
18+
receiver: std::sync::mpsc::Receiver<T>,
2319
}
2420

25-
impl<T> AsyncHandler<T> {
26-
pub fn into_inner(self) -> T {
27-
todo!()
21+
impl<T> AsyncHandler<T>
22+
where
23+
T: Send + Sync + 'static,
24+
{
25+
pub fn spawn<F>(f: F) -> Self
26+
where
27+
F: FnOnce() -> std::sync::mpsc::Receiver<T> + Send + Sync + 'static,
28+
{
29+
let receiver = std::thread::spawn(f);
30+
31+
Self {
32+
receiver: receiver.join().unwrap(),
33+
}
34+
}
35+
36+
pub fn wait(self) -> T {
37+
self.receiver.recv().unwrap()
2838
}
2939
}
30-
pub trait ArtifactLoader: Sized {
40+
41+
use shivini::cs::GpuSetup;
42+
43+
pub trait ArtifactLoader: Sized + Send + Sync {
3144
fn init<BS>(bs: BS) -> Self
3245
where
3346
BS: BlobStorage;
@@ -54,18 +67,16 @@ pub trait ArtifactLoader: Sized {
5467
fn get_compression_layer_precomputation(
5568
&self,
5669
circuit_id: u8,
57-
) -> AsyncHandler<TreeHasherCompatibleGpuSetup<CompressionProofsTreeHasher>>;
70+
) -> Box<dyn Read + Send + Sync + 'static>;
5871
fn get_compression_wrapper_precomputation(
5972
&self,
6073
circuit_id: u8,
61-
) -> AsyncHandler<TreeHasherCompatibleGpuSetup<CompressionTreeHasherForWrapper>>;
74+
) -> Box<dyn Read + Send + Sync + 'static>;
6275
fn get_plonk_snark_wrapper_precomputation(
6376
&self,
6477
circuit_id: u8,
65-
) -> AsyncHandler<PlonkSnarkVerifierCircuitDeviceSetupWrapper>;
66-
fn get_fflonk_snark_wrapper_precomputation(
67-
&self,
68-
) -> AsyncHandler<FflonkSnarkVerifierCircuitDeviceSetupWrapper>;
78+
) -> Box<dyn Read + Send + Sync + 'static>;
79+
fn get_fflonk_snark_wrapper_precomputation(&self) -> Box<dyn Read + Send + Sync + 'static>;
6980
}
7081

7182
pub struct SimpleArtifactLoader;
@@ -139,27 +150,25 @@ impl ArtifactLoader for SimpleArtifactLoader {
139150
fn get_compression_layer_precomputation(
140151
&self,
141152
circuit_id: u8,
142-
) -> AsyncHandler<TreeHasherCompatibleGpuSetup<CompressionProofsTreeHasher>> {
153+
) -> Box<dyn Read + Send + Sync + 'static> {
143154
todo!()
144155
}
145156

146157
fn get_compression_wrapper_precomputation(
147158
&self,
148159
circuit_id: u8,
149-
) -> AsyncHandler<TreeHasherCompatibleGpuSetup<CompressionTreeHasherForWrapper>> {
160+
) -> Box<dyn Read + Send + Sync + 'static> {
150161
todo!()
151162
}
152163

153164
fn get_plonk_snark_wrapper_precomputation(
154165
&self,
155166
circuit_id: u8,
156-
) -> AsyncHandler<PlonkSnarkVerifierCircuitDeviceSetupWrapper> {
167+
) -> Box<dyn Read + Send + Sync + 'static> {
157168
todo!()
158169
}
159170

160-
fn get_fflonk_snark_wrapper_precomputation(
161-
&self,
162-
) -> AsyncHandler<FflonkSnarkVerifierCircuitDeviceSetupWrapper> {
171+
fn get_fflonk_snark_wrapper_precomputation(&self) -> Box<dyn Read + Send + Sync + 'static> {
163172
todo!()
164173
}
165174
}
Lines changed: 62 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,200 +1,88 @@
1-
use circuit_definitions::circuit_definitions::aux_layer::{
2-
compression::ProofCompressionFunction,
3-
compression_modes::{
4-
CompressionMode1, CompressionMode1ForWrapper, CompressionMode2, CompressionMode3,
5-
CompressionMode4, CompressionMode5ForWrapper,
6-
},
7-
};
8-
91
use super::*;
102

11-
pub struct CompressionChain<C: CompressionStep, U>(std::marker::PhantomData<(C, U)>);
12-
13-
impl<C, U> CompressionChain<C, U>
14-
where
15-
C: CompressionStep,
16-
{
17-
pub fn new(step: C) -> CompressionChain<C, U>
18-
where
19-
U: CompressionStep,
20-
{
21-
todo!()
22-
}
23-
24-
pub fn next(self) -> U {
25-
todo!()
26-
}
3+
pub enum SnarkWrapper {
4+
Plonk,
5+
FFfonk,
276
}
28-
29-
impl<C, U> StepDefinition for CompressionChain<C, U>
30-
where
31-
C: CompressionStep,
32-
{
33-
type PreviousProofSystem = C::PreviousProofSystem;
34-
type ThisProofSystem = C::ThisProofSystem;
7+
pub enum SnarkWrapperProof {
8+
Plonk(PlonkSnarkVerifierCircuitProof),
9+
FFfonk(FflonkSnarkVerifierCircuitProof),
3510
}
36-
37-
impl<C, U> ProofCompressionFunction for CompressionChain<C, U>
38-
where
39-
C: CompressionStep,
40-
{
41-
type PreviousLayerPoW = C::PreviousLayerPoW;
42-
43-
type ThisLayerPoW = C::ThisLayerPoW;
44-
45-
type ThisLayerHasher = C::ThisLayerHasher;
46-
47-
type ThisLayerTranscript = C::ThisLayerTranscript;
48-
49-
fn this_layer_transcript_parameters(
50-
) -> <Self::ThisLayerTranscript as boojum::cs::implementations::transcript::Transcript<
51-
GoldilocksField,
52-
>>::TransciptParameters {
53-
C::this_layer_transcript_parameters()
54-
}
55-
56-
fn description_for_compression_step() -> String {
57-
C::description_for_compression_step()
58-
}
59-
60-
fn size_hint_for_compression_step() -> (usize, usize) {
61-
C::size_hint_for_compression_step()
62-
}
63-
64-
fn geometry_for_compression_step() -> boojum::cs::CSGeometry {
65-
C::geometry_for_compression_step()
66-
}
67-
68-
fn lookup_parameters_for_compression_step() -> boojum::cs::LookupParameters {
69-
C::lookup_parameters_for_compression_step()
70-
}
71-
72-
fn configure_builder_for_compression_step<
73-
T: boojum::cs::cs_builder::CsBuilderImpl<GoldilocksField, T>,
74-
GC: boojum::cs::GateConfigurationHolder<GoldilocksField>,
75-
TB: boojum::cs::StaticToolboxHolder,
76-
>(
77-
builder: boojum::cs::cs_builder::CsBuilder<T, GoldilocksField, GC, TB>,
78-
) -> boojum::cs::cs_builder::CsBuilder<
79-
T,
80-
GoldilocksField,
81-
impl boojum::cs::GateConfigurationHolder<GoldilocksField>,
82-
impl boojum::cs::StaticToolboxHolder,
83-
> {
84-
C::configure_builder_for_compression_step(builder)
85-
}
86-
87-
fn previous_step_builder_for_compression<
88-
CS: boojum::cs::traits::cs::ConstraintSystem<GoldilocksField> + 'static,
89-
>() -> Box<
90-
dyn boojum::cs::traits::circuit::ErasedBuilderForRecursiveVerifier<
91-
GoldilocksField,
92-
GoldilocksExt2,
93-
CS,
94-
>,
95-
> {
96-
C::previous_step_builder_for_compression()
97-
}
98-
99-
fn proof_config_for_compression_step() -> boojum::cs::implementations::prover::ProofConfig {
100-
C::proof_config_for_compression_step()
101-
}
102-
}
103-
104-
impl<C, T> CompressionStep for CompressionChain<C, T>
105-
where
106-
C: CompressionStep,
107-
{
108-
const MODE: u8 = C::MODE;
109-
const IS_WRAPPER: bool = C::IS_WRAPPER;
110-
}
111-
112-
pub struct CompressionChainBuilder<C: CompressionStep>(std::marker::PhantomData<C>);
113-
114-
impl<C> CompressionChainBuilder<C>
115-
where
116-
C: CompressionStep,
117-
{
118-
pub fn new(step: C) -> CompressionChainBuilder<C> {
119-
todo!()
120-
}
121-
}
122-
123-
impl<C> CompressionChainBuilder<C>
124-
where
125-
C: CompressionStep,
126-
{
127-
pub fn link_compression_step<CC>(
128-
self,
129-
step: CC,
130-
) -> CompressionChainBuilder<CompressionChain<C, CC>>
131-
where
132-
CC: CompressionStep,
133-
{
134-
todo!()
135-
}
136-
137-
pub fn build(self) -> C {
138-
todo!()
139-
}
140-
}
141-
142-
pub fn build_full_chain_with_fflonk() -> CompressionChain<
143-
CompressionChain<
144-
CompressionChain<CompressionChain<CompressionMode1, CompressionMode2>, CompressionMode3>,
145-
CompressionMode4,
146-
>,
147-
CompressionMode5ForWrapper,
148-
> {
149-
let chain = CompressionChainBuilder::new(CompressionMode1)
150-
.link_compression_step(CompressionMode2)
151-
.link_compression_step(CompressionMode3)
152-
.link_compression_step(CompressionMode4)
153-
.link_compression_step(CompressionMode5ForWrapper)
154-
.build();
155-
156-
chain
157-
}
158-
159-
pub fn build_full_chain_with_plonk() -> CompressionChain<CompressionMode1ForWrapper, ()> {
160-
todo!()
161-
}
162-
163-
pub fn run_step_chain_with_fflonk<BS>(
11+
pub fn wrap_proof<BS>(
16412
input_proof: SchedulerProof,
13+
snark_wrapper: SnarkWrapper,
16514
blob_storage: BS,
166-
) -> FflonkSnarkVerifierCircuitProof
15+
) -> SnarkWrapperProof
16716
where
16817
BS: BlobStorage,
16918
{
170-
let chain = build_full_chain_with_fflonk();
171-
run_step_chain::<_, _, FflonkSnarkWrapper, _>(chain, input_proof, blob_storage)
19+
match snark_wrapper {
20+
SnarkWrapper::Plonk => {
21+
let proof = run_step_chain_with_plonk(input_proof, blob_storage);
22+
SnarkWrapperProof::Plonk(proof)
23+
}
24+
SnarkWrapper::FFfonk => {
25+
let proof = run_step_chain_with_fflonk(input_proof, blob_storage);
26+
SnarkWrapperProof::FFfonk(proof)
27+
}
28+
}
17229
}
17330

174-
pub fn run_step_chain_with_plonk<BS>(
31+
pub(crate) fn run_step_chain_with_fflonk<BS>(
17532
input_proof: SchedulerProof,
17633
blob_storage: BS,
177-
) -> PlonkSnarkVerifierCircuitProof
34+
) -> FflonkSnarkVerifierCircuitProof
17835
where
17936
BS: BlobStorage,
18037
{
181-
let chain = build_full_chain_with_plonk();
182-
todo!()
38+
let artifact_loader = SimpleArtifactLoader::init(blob_storage);
39+
let context_initializor = SimpelContextInitializor::new();
40+
41+
let next_proof = CompressionMode1::prove_compression_step::<_, SimpelContextInitializor>(
42+
input_proof,
43+
&artifact_loader,
44+
);
45+
let next_proof = CompressionMode2::prove_compression_step::<_, SimpelContextInitializor>(
46+
next_proof,
47+
&artifact_loader,
48+
);
49+
let next_proof = CompressionMode3::prove_compression_step::<_, SimpelContextInitializor>(
50+
next_proof,
51+
&artifact_loader,
52+
);
53+
let next_proof = CompressionMode4::prove_compression_step::<_, SimpelContextInitializor>(
54+
next_proof,
55+
&artifact_loader,
56+
);
57+
let next_proof = CompressionMode5ForWrapper::prove_compression_step::<
58+
_,
59+
SimpelContextInitializor,
60+
>(next_proof, &artifact_loader);
61+
let final_proof = FflonkSnarkWrapper::prove_snark_wrapper_step::<_, SimpelContextInitializor>(
62+
next_proof,
63+
&artifact_loader,
64+
);
65+
66+
final_proof
18367
}
18468

185-
pub fn run_step_chain<C1, C2, SW, BS>(
186-
chain: CompressionChain<C1, C2>,
69+
pub(crate) fn run_step_chain_with_plonk<BS>(
18770
input_proof: SchedulerProof,
18871
blob_storage: BS,
189-
) -> <SW::ThisProofSystem as ProofSystemDefinition>::Proof
72+
) -> PlonkSnarkVerifierCircuitProof
19073
where
191-
C1: CompressionStep,
192-
C2: CompressionStep,
193-
SW: SnarkWrapperStep,
19474
BS: BlobStorage,
19575
{
19676
let artifact_loader = SimpleArtifactLoader::init(blob_storage);
77+
let next_proof = CompressionMode1ForWrapper::prove_compression_step::<
78+
_,
79+
SimpelContextInitializor,
80+
>(input_proof, &artifact_loader);
81+
82+
let final_proof = PlonkSnarkWrapper::prove_snark_wrapper_step::<_, SimpelContextInitializor>(
83+
next_proof,
84+
&artifact_loader,
85+
);
19786

198-
let next_step = chain.next();
199-
todo!()
87+
final_proof
20088
}

crates/proof-compression/src/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use super::*;
12
use circuit_definitions::circuit_definitions::{
23
aux_layer::{
3-
compression::{CompressionLayerCircuit, ProofCompressionFunction},
4-
compression_modes::CompressionTranscriptForWrapper,
54
CompressionProofsTreeHasher, CompressionProofsTreeHasherForWrapper,
65
ZkSyncCompressionForWrapperCircuit, ZkSyncCompressionLayerCircuit, ZkSyncCompressionProof,
76
ZkSyncCompressionProofForWrapper, ZkSyncCompressionVerificationKey,

0 commit comments

Comments
 (0)