diff --git a/provekit/prover/src/lib.rs b/provekit/prover/src/lib.rs index 98f91c0ca..a9c6a83f8 100644 --- a/provekit/prover/src/lib.rs +++ b/provekit/prover/src/lib.rs @@ -1,10 +1,7 @@ #[cfg(test)] use crate::r1cs::R1CSSolver; use { - crate::{ - r1cs::{CompressedLayers, CompressedR1CS}, - whir_r1cs::WhirR1CSProver, - }, + crate::whir_r1cs::WhirR1CSProver, acir::native_types::{Witness, WitnessMap}, anyhow::{Context, Result}, provekit_common::{ @@ -122,12 +119,9 @@ impl Prove for Prover { drop(self.program); drop(self.witness_generator); - // R1CS matrices are only needed at sumcheck; compress to free memory during - // commits. - let compressed_r1cs = - CompressedR1CS::compress(self.r1cs).context("While compressing R1CS")?; - let num_witnesses = compressed_r1cs.num_witnesses(); - let num_constraints = compressed_r1cs.num_constraints(); + let r1cs = self.r1cs; + let num_witnesses = r1cs.num_witnesses(); + let num_constraints = r1cs.num_constraints(); // Set up transcript with public inputs bound to the instance. let instance = public_inputs.hash_bytes(); @@ -153,14 +147,10 @@ impl Prove for Prover { .context("While solving w1 witnesses")?; } - // Compress w2 layers to free memory during w1 commit (only when - // challenges exist; otherwise just drop them). + // Hold w2 layers across the w1 commit only when challenges exist. let has_challenges = self.whir_for_witness.num_challenges > 0; - let compressed_w2_layers = if has_challenges { - Some( - CompressedLayers::compress(self.split_witness_builders.w2_layers) - .context("While compressing w2 layers")?, - ) + let w2_layers = if has_challenges { + Some(self.split_witness_builders.w2_layers) } else { drop(self.split_witness_builders.w2_layers); None @@ -168,7 +158,6 @@ impl Prove for Prover { debug!( witness_heap_bytes = witness.capacity() * size_of::>(), - compressed_r1cs_blob_bytes = compressed_r1cs.blob_len(), "component sizes after solve_w1" ); @@ -199,10 +188,7 @@ impl Prove for Prover { .context("While committing to w1")?; let commitments = if has_challenges { - let w2_layers = compressed_w2_layers - .unwrap() - .decompress() - .context("While decompressing w2 layers")?; + let w2_layers = w2_layers.expect("w2_layers is Some whenever has_challenges is true"); { let _s = info_span!("solve_w2").entered(); crate::r1cs::solve_witness_vec( @@ -234,11 +220,6 @@ impl Prove for Prover { vec![commitment_1] }; - // Decompress R1CS for the sumcheck and matrix operations. - let r1cs = compressed_r1cs - .decompress() - .context("While decompressing R1CS")?; - #[cfg(test)] r1cs.test_witness_satisfaction(&witness.iter().map(|w| w.unwrap()).collect::>()) .context("While verifying R1CS instance")?; diff --git a/provekit/prover/src/r1cs.rs b/provekit/prover/src/r1cs.rs index c23134dde..21436c28f 100644 --- a/provekit/prover/src/r1cs.rs +++ b/provekit/prover/src/r1cs.rs @@ -1,78 +1,18 @@ +#[cfg(test)] +use provekit_common::R1CS; use { crate::witness::witness_builder::WitnessBuilderSolver, acir::native_types::WitnessMap, - anyhow::{Context, Result}, + anyhow::Result, provekit_common::{ utils::batch_inverse_montgomery, witness::{LayerType, LayeredWitnessBuilders, WitnessBuilder}, - FieldElement, NoirElement, TranscriptSponge, R1CS, + FieldElement, NoirElement, TranscriptSponge, }, tracing::instrument, whir::transcript::ProverState, }; -/// Serialized R1CS matrices held as a compact postcard blob. -/// -/// The R1CS is only needed during the sumcheck phase. Compressing it -/// into a serialized blob frees that memory during the commit phase, -/// then decompresses when the sumcheck begins. -pub struct CompressedR1CS { - num_constraints: usize, - num_witnesses: usize, - blob: Vec, -} - -/// Serialized witness builder layers held as a compact postcard blob. -/// -/// Same strategy as [`CompressedR1CS`]: the w2 layers are not needed -/// until challenge-dependent witness solving, so we compress them to -/// free memory during the w1 commit. -pub struct CompressedLayers { - blob: Vec, -} - -impl CompressedLayers { - pub fn compress(layers: LayeredWitnessBuilders) -> Result { - let blob = postcard::to_allocvec(&layers) - .context("LayeredWitnessBuilders serialization failed")?; - Ok(Self { blob }) - } - - pub fn decompress(self) -> Result { - postcard::from_bytes(&self.blob).context("LayeredWitnessBuilders deserialization failed") - } -} - -impl CompressedR1CS { - pub fn compress(r1cs: R1CS) -> Result { - let num_constraints = r1cs.num_constraints(); - let num_witnesses = r1cs.num_witnesses(); - let blob = postcard::to_allocvec(&r1cs).context("R1CS serialization failed")?; - drop(r1cs); - Ok(Self { - num_constraints, - num_witnesses, - blob, - }) - } - - pub fn decompress(self) -> Result { - postcard::from_bytes(&self.blob).context("R1CS deserialization failed") - } - - pub const fn num_constraints(&self) -> usize { - self.num_constraints - } - - pub const fn num_witnesses(&self) -> usize { - self.num_witnesses - } - - pub fn blob_len(&self) -> usize { - self.blob.len() - } -} - #[cfg(test)] pub trait R1CSSolver { fn test_witness_satisfaction(&self, witness: &[FieldElement]) -> anyhow::Result<()>;