- 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.
81 lines
2.2 KiB
Rust
81 lines
2.2 KiB
Rust
pub mod basetypes;
|
|
pub mod circuits;
|
|
pub mod emptypod;
|
|
mod error;
|
|
pub mod mainpod;
|
|
pub mod mock;
|
|
pub mod primitives;
|
|
pub mod recursion;
|
|
mod serialization;
|
|
pub mod signedpod;
|
|
|
|
use base64::{prelude::BASE64_STANDARD, Engine};
|
|
pub use error::*;
|
|
use plonky2::util::serialization::{Buffer, Read};
|
|
|
|
use crate::{
|
|
backends::plonky2::{
|
|
basetypes::{CommonCircuitData, Proof},
|
|
circuits::mainpod::{MainPodVerifyTarget, NUM_PUBLIC_INPUTS},
|
|
recursion::RecursiveCircuit,
|
|
serialization::CommonCircuitDataSerializer,
|
|
},
|
|
cache::{self, CacheEntry},
|
|
middleware::Params,
|
|
timed,
|
|
};
|
|
|
|
pub fn cache_get_standard_rec_main_pod_common_circuit_data(
|
|
) -> CacheEntry<CommonCircuitDataSerializer> {
|
|
let params = Params::default();
|
|
cache::get(
|
|
"standard_rec_main_pod_common_circuit_data",
|
|
¶ms,
|
|
|params| {
|
|
let circuit_data = timed!(
|
|
"recursive MainPod circuit_data",
|
|
RecursiveCircuit::<MainPodVerifyTarget>::target_and_circuit_data(
|
|
params.max_input_recursive_pods,
|
|
NUM_PUBLIC_INPUTS,
|
|
params
|
|
)
|
|
.expect("calculate circuit_data")
|
|
);
|
|
CommonCircuitDataSerializer(circuit_data.1.common)
|
|
},
|
|
)
|
|
.expect("cache ok")
|
|
}
|
|
|
|
pub fn serialize_bytes(bytes: &[u8]) -> String {
|
|
BASE64_STANDARD.encode(bytes)
|
|
}
|
|
|
|
pub fn deserialize_bytes(data: &str) -> Result<Vec<u8>> {
|
|
BASE64_STANDARD.decode(data).map_err(|e| {
|
|
Error::custom(format!(
|
|
"Failed to decode data from base64: {}. Value: {}",
|
|
e, data
|
|
))
|
|
})
|
|
}
|
|
|
|
pub fn deserialize_proof(common: &CommonCircuitData, proof: &str) -> Result<Proof> {
|
|
let decoded = deserialize_bytes(proof)?;
|
|
let mut buf = Buffer::new(&decoded);
|
|
let proof = buf.read_proof(common).map_err(|e| {
|
|
Error::custom(format!(
|
|
"Failed to read proof from buffer: {}. Value: {}",
|
|
e, proof
|
|
))
|
|
})?;
|
|
|
|
Ok(proof)
|
|
}
|
|
|
|
pub fn serialize_proof(proof: &Proof) -> String {
|
|
let mut buffer = Vec::new();
|
|
use plonky2::util::serialization::Write;
|
|
buffer.write_proof(proof).unwrap();
|
|
serialize_bytes(&buffer)
|
|
}
|