Skip to content

Commit

Permalink
feat: use feature for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jun 21, 2024
1 parent fe92d09 commit 4f248d6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const-chunks = "0.3.0"
default = ["std"]
serde = ["dep:serde"]
std = ["hex/std", "serde/std"]
cache = []

[[bin]]
name = "build_binaries"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 2 additions & 1 deletion scripts/build_binaries.rs
Original file line number Diff line number Diff line change
@@ -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: _,
Expand Down
1 change: 1 addition & 0 deletions src/kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> = glob::glob(VERIFY_KZG_PROOF_TESTS)
Expand Down
4 changes: 4 additions & 0 deletions src/trusted_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ 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] =
unsafe { transmute(slice::from_raw_parts(G1_BYTES.as_ptr(), NUM_G1_POINTS)) };
g1
}

#[cfg(feature = "cache")]
pub const fn get_g2_points() -> &'static [G2Affine] {
const G2_BYTES: &[u8] = include_bytes!("g2.bin");
let g2: &[G2Affine] =
unsafe { transmute(slice::from_raw_parts(G2_BYTES.as_ptr(), NUM_G1_POINTS)) };
g2
}

#[cfg(feature = "cache")]
pub const fn get_kzg_settings() -> KzgSettings {
KzgSettings {
max_width: 16,
Expand All @@ -47,6 +50,7 @@ pub struct KzgSettingsOwned {
}

impl KzgSettings {
#[cfg(feature = "cache")]
#[sp1_derive::cycle_tracker]
pub fn load_trusted_setup_file() -> Result<Self, KzgError> {
Ok(get_kzg_settings())
Expand Down

0 comments on commit 4f248d6

Please sign in to comment.