shared_types/
config.rs

1use crate::circuit_hash::CircuitHashType;
2use serde::{Deserialize, Serialize};
3pub mod global_config;
4
5// ------------------ Circuit-specific (GKR) proving ------------------
6/// An enum listing the types of claim aggregation strategies.
7#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
8pub enum ClaimAggregationStrategy {
9    /// Interpolation-based claim aggregation strategy from "Thaler13".
10    Interpolative,
11    /// Claim aggregation using the random-linear combination strategy from "Libra"
12    RLC,
13}
14
15/// A config which informs a GKR circuit prover about how to prove a GKR circuit,
16/// including flags e.g. whether to use certain optimizations (e.g. `Evaluations`
17/// memory-efficient optimization), or e.g. which claim aggregation strategy to use.
18#[derive(Serialize, Deserialize, Debug, Clone)]
19pub struct GKRCircuitProverConfig {
20    /// Whether to evaluate the beta function within gate layers lazily,
21    /// i.e. to compute the values on the fly rather than via initializing
22    /// a beta evaluations "table".
23    lazy_beta_evals: bool,
24
25    /// The type of hash function to be used on hashing the circuit description
26    /// to be added to transcript.
27    circuit_description_hash_type: CircuitHashType,
28
29    /// Which claim aggregation (RLC vs. deterministic) to use for reducing
30    /// the validity of multiple claims on a layer to that of a single claim.
31    claim_agg_strategy: ClaimAggregationStrategy,
32
33    /// Whether to use the "constant column optimization", i.e. whether to
34    /// reduce the implicit degree of the prover-claimed polynomial
35    /// Q(x) =? V_i(l(x)) when there are variable indices within all claims
36    /// where all challenges within that index are identical.
37    claim_agg_constant_column_optimization: bool,
38
39    /// Hyrax input layer batch opening. Determines whether we attempt to aggregate
40    /// Hyrax PCS Evaluation proofs by grouping common challenge coordinates together.
41    hyrax_input_layer_batch_opening: bool,
42
43    /// Controls whether bit-packing is actually enabled. If set to `false`, the
44    /// `BitPackedVector` will default to storing each field element using the type
45    /// `F`, effectively behaving like a regular (immutable) `Vec<F>`. This is
46    /// needed because bit-packing incurs a noticable runtime slowdown, and we need
47    /// an easy way to turn it off if trading memory for speed is desirable.
48    ///
49    /// Note that because this is global, this option also implicitly affects
50    /// the verifier's `Evaluations<F>` structs!!!
51    enable_bit_packing: bool,
52}
53
54impl GKRCircuitProverConfig {
55    /// Returns a memory-optimal configuration for the GKR circuit prover.
56    ///
57    /// In particular, this turns on the `lazy_beta_evals` and `bit_packed_vector`
58    /// flags.
59    pub fn memory_optimized_default() -> Self {
60        Self {
61            lazy_beta_evals: true,
62            circuit_description_hash_type: CircuitHashType::Sha3_256,
63            claim_agg_strategy: ClaimAggregationStrategy::Interpolative,
64            claim_agg_constant_column_optimization: true,
65            hyrax_input_layer_batch_opening: true,
66            enable_bit_packing: true,
67        }
68    }
69
70    /// Returns a runtime-optimal configuration for the GKR circuit prover.
71    pub fn runtime_optimized_default() -> Self {
72        Self {
73            lazy_beta_evals: false,
74            circuit_description_hash_type: CircuitHashType::Sha3_256,
75            claim_agg_strategy: ClaimAggregationStrategy::RLC,
76            claim_agg_constant_column_optimization: true,
77            hyrax_input_layer_batch_opening: true,
78            enable_bit_packing: false,
79        }
80    }
81
82    /// Returns a runtime-optimal configuration for a version of the GKR
83    /// circuit prover which is compatible with our current Hyrax IP
84    /// implementation.
85    ///
86    /// In particular, this turns OFF the `enable_bit_packing` option, turns OFF
87    /// the `lazy_beta_evals` option, and additionally turns OFF the
88    /// `claim_agg_constant_column_optimization` option as well.
89    pub const fn hyrax_compatible_runtime_optimized_default() -> Self {
90        Self {
91            lazy_beta_evals: false,
92            circuit_description_hash_type: CircuitHashType::Sha3_256,
93            claim_agg_strategy: ClaimAggregationStrategy::RLC,
94            claim_agg_constant_column_optimization: false,
95            hyrax_input_layer_batch_opening: true,
96            enable_bit_packing: false,
97        }
98    }
99
100    /// Returns a memory-optimal configuration for a version of the GKR
101    /// circuit prover which is compatible with our current Hyrax IP
102    /// implementation.
103    ///
104    /// In particular, this turns ON the `enable_bit_packing` option, turns ON
105    /// the `lazy_beta_evals`, and additionally turns OFF the
106    /// `claim_agg_constant_column_optimization` option as well.
107    pub const fn hyrax_compatible_memory_optimized_default() -> Self {
108        Self {
109            lazy_beta_evals: true,
110            circuit_description_hash_type: CircuitHashType::Sha3_256,
111            claim_agg_strategy: ClaimAggregationStrategy::RLC,
112            claim_agg_constant_column_optimization: false,
113            hyrax_input_layer_batch_opening: true,
114            enable_bit_packing: true,
115        }
116    }
117
118    /// Constructs a new [GKRCircuitProverConfig] from scratch.
119    pub fn new(
120        lazy_beta_evals: bool,
121        circuit_description_hash_type: CircuitHashType,
122        claim_agg_strategy: ClaimAggregationStrategy,
123        claim_agg_constant_column_optimization: bool,
124        hyrax_input_layer_batch_opening: bool,
125        enable_bit_packing: bool,
126    ) -> Self {
127        Self {
128            lazy_beta_evals,
129            circuit_description_hash_type,
130            claim_agg_strategy,
131            claim_agg_constant_column_optimization,
132            hyrax_input_layer_batch_opening,
133            enable_bit_packing,
134        }
135    }
136
137    /// Setter function for lazy beta evals.
138    pub fn set_lazy_beta_evals(&mut self, updated_lazy_beta_evals: bool) {
139        self.lazy_beta_evals = updated_lazy_beta_evals;
140    }
141
142    /// Setter function for claim agg strategy.
143    pub fn set_claim_agg_strategy(&mut self, updated_claim_agg_strategy: ClaimAggregationStrategy) {
144        self.claim_agg_strategy = updated_claim_agg_strategy;
145    }
146
147    /// Setter function for circuit hash type.
148    pub fn set_circuit_description_hash_type(
149        &mut self,
150        updated_circuit_description_hash_type: CircuitHashType,
151    ) {
152        self.circuit_description_hash_type = updated_circuit_description_hash_type;
153    }
154
155    /// Setter function for constant column optimization.
156    pub fn set_claim_agg_constant_column_optimization(
157        &mut self,
158        updated_claim_agg_constant_column_optimization: bool,
159    ) {
160        self.claim_agg_constant_column_optimization =
161            updated_claim_agg_constant_column_optimization;
162    }
163
164    /// Setter function for enabling bit packing.
165    pub fn set_enable_bit_packing(&mut self, updated_enable_bit_packing: bool) {
166        self.enable_bit_packing = updated_enable_bit_packing;
167    }
168
169    /// Getter function for lazy beta evals.
170    pub fn get_lazy_beta_evals(&self) -> bool {
171        self.lazy_beta_evals
172    }
173
174    /// Getter function for claim agg strategy.
175    pub fn get_claim_agg_strategy(&self) -> ClaimAggregationStrategy {
176        self.claim_agg_strategy
177    }
178
179    /// Getter function for circuit hash type.
180    pub fn get_circuit_description_hash_type(&self) -> CircuitHashType {
181        self.circuit_description_hash_type
182    }
183
184    /// Getter function for constant column optimization.
185    pub fn get_claim_agg_constant_column_optimization(&self) -> bool {
186        self.claim_agg_constant_column_optimization
187    }
188
189    /// Getter function for Hyrax batch opening.
190    pub fn get_hyrax_batch_opening(&self) -> bool {
191        self.hyrax_input_layer_batch_opening
192    }
193
194    /// Getter function for enabling bit packing.
195    pub fn get_enable_bit_packing(&self) -> bool {
196        self.enable_bit_packing
197    }
198}
199
200/// A config which informs a GKR circuit verifier about how to verify a GKR circuit + proof,
201/// including flags e.g. how to aggregate claims.
202#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
203pub struct GKRCircuitVerifierConfig {
204    /// Whether to evaluate the beta function within gate layers lazily,
205    /// i.e. to compute the values on the fly rather than via initializing
206    /// a beta evaluations "table".
207    lazy_beta_evals: bool,
208
209    /// The type of hash function to be used for hashing the circuit description
210    /// to be added to transcript.
211    circuit_description_hash_type: CircuitHashType,
212
213    /// Which claim aggregation (RLC vs. deterministic) to use for reducing
214    /// the validity of multiple claims on a layer to that of a single claim.
215    claim_agg_strategy: ClaimAggregationStrategy,
216
217    /// Whether to use the "constant column optimization", i.e. whether to
218    /// reduce the implicit degree of the prover-claimed polynomial
219    /// Q(x) =? V_i(l(x)) when there are variable indices within all claims
220    /// where all challenges within that index are identical.
221    claim_agg_constant_column_optimization: bool,
222}
223
224impl GKRCircuitVerifierConfig {
225    /// Constructs a [GKRCircuitVerifierConfig] from the [GKRCircuitProverConfig]
226    /// used for the corresponding prover.
227    pub fn new_from_prover_config(
228        prover_config: &GKRCircuitProverConfig,
229        verifier_lazy_beta_evals: bool,
230    ) -> Self {
231        Self {
232            lazy_beta_evals: verifier_lazy_beta_evals,
233            claim_agg_strategy: prover_config.claim_agg_strategy,
234            circuit_description_hash_type: prover_config.circuit_description_hash_type,
235            claim_agg_constant_column_optimization: prover_config
236                .claim_agg_constant_column_optimization,
237        }
238    }
239
240    /// Constructs a new [GKRCircuitVerifierConfig] from scratch.
241    pub const fn new(
242        lazy_beta_evals: bool,
243        circuit_description_hash_type: CircuitHashType,
244        claim_agg_strategy: ClaimAggregationStrategy,
245        claim_agg_constant_column_optimization: bool,
246    ) -> Self {
247        Self {
248            lazy_beta_evals,
249            circuit_description_hash_type,
250            claim_agg_strategy,
251            claim_agg_constant_column_optimization,
252        }
253    }
254
255    /// Constructs a [GKRCircuitVerifierConfig] from the corresponding
256    /// [ProofConfig] used within the proof.
257    pub fn new_from_proof_config(
258        proof_config: &ProofConfig,
259        verifier_lazy_beta_evals: bool,
260    ) -> Self {
261        Self {
262            lazy_beta_evals: verifier_lazy_beta_evals,
263            claim_agg_strategy: proof_config.claim_agg_strategy,
264            claim_agg_constant_column_optimization: proof_config
265                .claim_agg_constant_column_optimization,
266            circuit_description_hash_type: proof_config.circuit_description_hash_type,
267        }
268    }
269
270    /// Returns a runtime-optimal configuration for a version of the GKR
271    /// circuit verifier compatible with Hyrax.
272    ///
273    /// In particular, this turns OFF the `lazy_beta_evals`, and additionally
274    /// turns OFF the `claim_agg_constant_column_optimization` option as well.
275    pub const fn hyrax_compatible_runtime_optimized_default() -> Self {
276        Self {
277            lazy_beta_evals: false,
278            circuit_description_hash_type: CircuitHashType::Sha3_256,
279            claim_agg_strategy: ClaimAggregationStrategy::RLC,
280            claim_agg_constant_column_optimization: false,
281        }
282    }
283
284    /// Setter function for lazy beta evals.
285    pub fn set_lazy_beta_evals(&mut self, updated_lazy_beta_evals: bool) {
286        self.lazy_beta_evals = updated_lazy_beta_evals;
287    }
288
289    /// Setter function for claim agg strategy.
290    pub fn set_claim_agg_strategy(&mut self, updated_claim_agg_strategy: ClaimAggregationStrategy) {
291        self.claim_agg_strategy = updated_claim_agg_strategy;
292    }
293
294    /// Setter function for circuit hash type.
295    pub fn set_circuit_description_hash_type(
296        &mut self,
297        updated_circuit_description_hash_type: CircuitHashType,
298    ) {
299        self.circuit_description_hash_type = updated_circuit_description_hash_type;
300    }
301
302    /// Setter function for constant column optimization.
303    pub fn set_claim_agg_constant_column_optimization(
304        &mut self,
305        updated_claim_agg_constant_column_optimization: bool,
306    ) {
307        self.claim_agg_constant_column_optimization =
308            updated_claim_agg_constant_column_optimization;
309    }
310
311    /// Getter function for lazy beta evals.
312    pub fn get_lazy_beta_evals(&self) -> bool {
313        self.lazy_beta_evals
314    }
315
316    /// Getter function for claim agg strategy.
317    pub fn get_claim_agg_strategy(&self) -> ClaimAggregationStrategy {
318        self.claim_agg_strategy
319    }
320
321    /// Getter function for circuit hash type.
322    pub fn get_circuit_description_hash_type(&self) -> CircuitHashType {
323        self.circuit_description_hash_type
324    }
325
326    /// Getter function for constant column optimization.
327    pub fn get_claim_agg_constant_column_optimization(&self) -> bool {
328        self.claim_agg_constant_column_optimization
329    }
330}
331
332// -------------------- Proof config --------------------
333
334/// A config which travels alongside a GKR proof stored within a `Transcript`
335/// which details the proof-specific configuration (i.e. how the verifier
336/// should be configured in order to appropriately parse and verify the proof).
337#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
338pub struct ProofConfig {
339    /// The type of hash function to be used on hashing the circuit description
340    /// to be added to transcript.
341    circuit_description_hash_type: CircuitHashType,
342
343    /// Which claim aggregation (RLC vs. deterministic) to use for reducing
344    /// the validity of multiple claims on a layer to that of a single claim.
345    claim_agg_strategy: ClaimAggregationStrategy,
346
347    /// Whether to use the "constant column optimization", i.e. whether to
348    /// reduce the implicit degree of the prover-claimed polynomial
349    /// Q(x) =? V_i(l(x)) when there are variable indices within all claims
350    /// where all challenges within that index are identical.
351    claim_agg_constant_column_optimization: bool,
352}
353impl ProofConfig {
354    /// Creates the associated [ProofConfig] from a [GKRCircuitProverConfig].
355    /// Note that a similar function should not be required for a
356    /// [GKRCircuitVerifierConfig], since only the circuit prover should specify
357    /// the proof config and the verifier should simply check to see if their
358    /// own config matches that of the proof given.
359    pub fn new_from_prover_config(prover_config: &GKRCircuitProverConfig) -> Self {
360        Self {
361            circuit_description_hash_type: prover_config.circuit_description_hash_type,
362            claim_agg_strategy: prover_config.claim_agg_strategy,
363            claim_agg_constant_column_optimization: prover_config
364                .claim_agg_constant_column_optimization,
365        }
366    }
367
368    /// Setter function for claim agg strategy.
369    pub fn set_claim_agg_strategy(&mut self, updated_claim_agg_strategy: ClaimAggregationStrategy) {
370        self.claim_agg_strategy = updated_claim_agg_strategy;
371    }
372
373    /// Setter function for circuit hash type.
374    pub fn set_circuit_description_hash_type(
375        &mut self,
376        updated_circuit_description_hash_type: CircuitHashType,
377    ) {
378        self.circuit_description_hash_type = updated_circuit_description_hash_type;
379    }
380
381    /// Setter function for constant column optimization.
382    pub fn set_claim_agg_constant_column_optimization(
383        &mut self,
384        updated_claim_agg_constant_column_optimization: bool,
385    ) {
386        self.claim_agg_constant_column_optimization =
387            updated_claim_agg_constant_column_optimization;
388    }
389
390    /// Getter function for claim agg strategy.
391    pub fn get_claim_agg_strategy(&self) -> ClaimAggregationStrategy {
392        self.claim_agg_strategy
393    }
394
395    /// Getter function for circuit hash type.
396    pub fn get_circuit_description_hash_type(&self) -> CircuitHashType {
397        self.circuit_description_hash_type
398    }
399
400    /// Getter function for constant column optimization.
401    pub fn get_claim_agg_constant_column_optimization(&self) -> bool {
402        self.claim_agg_constant_column_optimization
403    }
404}