From 4f248d6a3736952881adebfb6f6c7d942b75d09b Mon Sep 17 00:00:00 2001 From: Bhargav Annem Date: Fri, 21 Jun 2024 11:44:40 -0700 Subject: [PATCH] feat: use feature for caching --- .github/workflows/rust.yml | 4 ++-- Cargo.toml | 1 + README.md | 12 ++++++++++++ scripts/build_binaries.rs | 3 ++- src/kzg_proof.rs | 1 + src/trusted_setup.rs | 4 ++++ 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 000bb2c..31e2cc1 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -17,6 +17,6 @@ jobs: steps: - uses: actions/checkout@v4 - name: Build - run: cargo build --verbose + run: cargo build --verbose --features=cache - name: Run tests - run: cargo test --verbose + run: cargo test --verbose --features=cache diff --git a/Cargo.toml b/Cargo.toml index a9053df..275656a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ const-chunks = "0.3.0" default = ["std"] serde = ["dep:serde"] std = ["hex/std", "serde/std"] +cache = [] [[bin]] name = "build_binaries" diff --git a/README.md b/README.md index 53b352a..cf19cf1 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,15 @@ An endpoint for `verify_kzg_proof` in [c-kzg-4844](https://github.com/ethereum/c | `load_trusted_setup` | 391 | Checkout the SP1 profile at [0xWOLAND/sp1-revm-kzg-profile](https://github.com/0xWOLAND/sp1-revm-kzg-profile). This crate has been used in a [fork of SP1's patch of `revm`](https://github.com/0xWOLAND/revm/tree/patch-v5.0.0), which passes all tests. Additionally, `kzg-rs` is based on [this](https://github.com/0xWOLAND/bls12_381) slightly modified fork of `bls12_381`. + +## Usage +Add +```toml +kzg-rs = { git = "https://github.com/0xWOLAND/kzg-rs", default-features = false, features=['cache'] optional = true } +``` + +You can rebuild `g1.bin` and `g2.bin` by running + +```sh +cargo run --bin build_binaries +``` \ No newline at end of file diff --git a/scripts/build_binaries.rs b/scripts/build_binaries.rs index af92174..b7b1ed9 100644 --- a/scripts/build_binaries.rs +++ b/scripts/build_binaries.rs @@ -1,8 +1,9 @@ use std::{fs, io::Write}; use bls12_381::{G1Affine, G2Affine}; -use kzg_rs::{load_trusted_setup_file_brute, KzgSettings, KzgSettingsOwned}; +use kzg_rs::{load_trusted_setup_file_brute, KzgSettingsOwned}; +#[cfg(not(feature = "cache"))] fn main() { let KzgSettingsOwned { max_width: _, diff --git a/src/kzg_proof.rs b/src/kzg_proof.rs index 84070dd..658d267 100644 --- a/src/kzg_proof.rs +++ b/src/kzg_proof.rs @@ -90,6 +90,7 @@ mod tests { const VERIFY_KZG_PROOF_TESTS: &str = "tests/verify_kzg_proof/*/*"; #[test] + #[cfg(feature = "cache")] fn test_verify_kzg_proof() { let kzg_settings = KzgSettings::load_trusted_setup_file().unwrap(); let test_files: Vec = glob::glob(VERIFY_KZG_PROOF_TESTS) diff --git a/src/trusted_setup.rs b/src/trusted_setup.rs index 862fb32..159ae63 100644 --- a/src/trusted_setup.rs +++ b/src/trusted_setup.rs @@ -10,6 +10,7 @@ use crate::{ const TRUSTED_SETUP_FILE: &str = include_str!("trusted_setup.txt"); +#[cfg(feature = "cache")] pub const fn get_g1_points() -> &'static [G1Affine] { const G1_BYTES: &[u8] = include_bytes!("g1.bin"); let g1: &[G1Affine] = @@ -17,6 +18,7 @@ pub const fn get_g1_points() -> &'static [G1Affine] { g1 } +#[cfg(feature = "cache")] pub const fn get_g2_points() -> &'static [G2Affine] { const G2_BYTES: &[u8] = include_bytes!("g2.bin"); let g2: &[G2Affine] = @@ -24,6 +26,7 @@ pub const fn get_g2_points() -> &'static [G2Affine] { g2 } +#[cfg(feature = "cache")] pub const fn get_kzg_settings() -> KzgSettings { KzgSettings { max_width: 16, @@ -47,6 +50,7 @@ pub struct KzgSettingsOwned { } impl KzgSettings { + #[cfg(feature = "cache")] #[sp1_derive::cycle_tracker] pub fn load_trusted_setup_file() -> Result { Ok(get_kzg_settings())