Feat/disk cache (#354)

- Bump rust version to `nightly-2025-07-02` because some of the nightly features we were using have been stabilized.
- Introduce feature `disk_cache` which enables caching to disk.  Each time an artifact is retrieved from the cache it will be read and deserialized.  On a cache miss the artifact will be created, serialized and stored to disk.
- Introduce feature `mem_cache` which enables caching to memory.  All cached artifacts are kept in memory after they are created.  The mem cache implementation avoids cloning of artifacts by extending their lifetime to `'static`.  This is `unsafe` code, but I argue that this usage is safe.
- Add a `build.rs`
  - When the feature `disk_cache` is enabled, the `build.rs` will inject env variables to the process with the git commit information, which is used to index the cached artifacts
- Replace all previous cached artifacts from `LazyStatic` methods that call the cache API
- Derive `Serialize, Deserialize` for all `*Target` types so that they can be serialized for caching to disk
- Add finer level of caching: now we cache the `CircuitData` and `VerifierData` independently.  The reason for this is that `CircuitData` is a very big artifact which is not needed for verification.  So by only accessing `VerifierData` in verification we don't pay a big overhead for reading from disk and deserializing
- Add missing artifacts to the cache: like the `CircuitData` for the `MainPod` indexed by `Params`
- Add helper types to serialize and deserialize `CircuitData`, `CommonData` and `VerifierData` with the set of gates and generators used in the recursive MainPod circuit
- Tweak the ids of our custom gates so that they remain unique when their generic parameters change
- Bugfix: several tests were using the standard `vd_set` but were using MainPod circuits with non-default parameters.  This was working before because there was a bug: the MainPod circuit was reporting that the used verifier data was the standard one instead of picking the one corresponding to it's own Params.

Summary of breaking changes:
- One and only one of the features `mem_cache` or `disk_cache` need to be enabled.  By default it's `mem_cache`
  - To enable the `disk_cache` you need to disable the default features like this: `--no-default-features --features=backend_plonky2,zk,disk_cache`
- Removed `DEFAULT_PARAMS`, instead use `Params::default()`
- Removed `STANDARD_REC_MAIN_POD_CIRCUIT_DATA`, instead use `cache_get_standard_rec_main_pod_common_circuit_data`
- The library is now using `nightly-2025-07-02`.  Some rust language features are unstable in previous versions.
This commit is contained in:
Eduard S. 2025-07-24 12:15:31 +02:00 committed by GitHub
parent 745d654048
commit 8429cd224d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 831 additions and 207 deletions

View file

@ -19,6 +19,7 @@ use plonky2::{
},
util::serialization::{Buffer, IoResult, Read, Write},
};
use serde::{Deserialize, Serialize};
use crate::{
backends::plonky2::{
@ -39,7 +40,7 @@ use crate::{
pub const CODE_SIZE: usize = HASH_SIZE + 2;
const NUM_BITS: usize = 32;
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct ValueTarget {
pub elements: [Target; VALUE_SIZE],
}
@ -75,8 +76,9 @@ impl ValueTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct StatementArgTarget {
#[serde(with = "serde_arrays")]
pub elements: [Target; STATEMENT_ARG_F_LEN],
}
@ -128,7 +130,7 @@ impl StatementArgTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct StatementTarget {
pub predicate: PredicateTarget,
pub args: Vec<StatementArgTarget>,
@ -201,8 +203,9 @@ impl StatementTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct OperationTypeTarget {
#[serde(with = "serde_arrays")]
pub elements: [Target; Params::operation_type_size()],
}
@ -249,10 +252,11 @@ impl OperationTypeTarget {
}
// TODO: Implement Operation::to_field to determine the size of each element
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct OperationTarget {
pub op_type: OperationTypeTarget,
pub args: Vec<[Target; OPERATION_ARG_F_LEN]>,
#[serde(with = "serde_arrays")]
pub aux: [Target; OPERATION_AUX_F_LEN],
}
@ -304,8 +308,9 @@ impl NativePredicateTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct PredicateTarget {
#[serde(with = "serde_arrays")]
pub(crate) elements: [Target; Params::predicate_size()],
}
@ -386,8 +391,9 @@ impl LiteralOrWildcardTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct StatementTmplArgTarget {
#[serde(with = "serde_arrays")]
pub elements: [Target; Params::statement_tmpl_arg_size()],
}
@ -432,7 +438,7 @@ impl StatementTmplArgTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct StatementTmplTarget {
pub pred: PredicateTarget,
pub args: Vec<StatementTmplArgTarget>,
@ -449,7 +455,7 @@ impl StatementTmplTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct CustomPredicateTarget {
pub conjunction: BoolTarget,
// len = params.max_custom_predicate_arity
@ -468,7 +474,7 @@ impl CustomPredicateTarget {
}
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct CustomPredicateBatchTarget {
pub predicates: Vec<CustomPredicateTarget>,
}
@ -500,6 +506,7 @@ impl CustomPredicateBatchTarget {
}
/// Custom predicate table entry
#[derive(Clone, Serialize, Deserialize)]
pub struct CustomPredicateEntryTarget {
pub id: HashOutTarget,
pub index: Target,
@ -575,6 +582,7 @@ impl CustomPredicateEntryTarget {
}
// Custom predicate verification table entry
#[derive(Clone, Serialize, Deserialize)]
pub struct CustomPredicateVerifyEntryTarget {
pub custom_predicate_table_index: Target,
pub custom_predicate: CustomPredicateEntryTarget,
@ -631,6 +639,7 @@ impl CustomPredicateVerifyEntryTarget {
}
/// Query for the custom predicate verification table
#[derive(Clone, Serialize, Deserialize)]
pub struct CustomPredicateVerifyQueryTarget {
pub statement: StatementTarget,
pub op_type: OperationTypeTarget,
@ -1386,7 +1395,7 @@ impl CircuitBuilderPod<F, D> for CircuitBuilder {
}
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct LtMaskGenerator {
pub(crate) n: Target,
pub(crate) mask: Vec<Target>,