Skip to content

Commit

Permalink
feat: skip build binaries for build docs (#15)
Browse files Browse the repository at this point in the history
* feat: skip build when building docs

* chore: bump version

* feat: use `OUT_DIR`
  • Loading branch information
0xWOLAND authored Aug 30, 2024
1 parent b11832b commit 916b19c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Rust implementation of KZG point evaluation"
edition = "2021"
license = "MIT"
name = "kzg-rs"
version = "0.2.2"
version = "0.2.3"
repository = "https://github.com/succinctlabs/kzg-rs"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cargo add kzg-rs
```
Or add
```toml
kzg-rs = { version = "0.2.2" }
kzg-rs = { version = "0.2.3" }
```

You can rebuild `roots_of_unity.bin`, `g1.bin`, and `g2.bin` by running
Expand Down
25 changes: 15 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ include!("src/enums.rs");
include!("src/consts.rs");
include!("src/pairings.rs");

#[cfg(not(target_arch = "riscv32"))]
#[cfg(not(any(target_arch = "riscv32", doc)))]
fn main() {
use std::{fs, io::Write, path::Path};
use std::{env, fs, io::Write, path::Path};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KzgSettingsOwned {
pub roots_of_unity: [Scalar; NUM_ROOTS_OF_UNITY],
Expand Down Expand Up @@ -169,9 +169,14 @@ fn main() {
Ok(expanded)
}

let g1_exists = Path::new("./src/g1.bin").exists();
let g2_exists = Path::new("./src/g2.bin").exists();
let roots_of_unity_exists = Path::new("./src/roots_of_unity.bin").exists();
let out_dir = env::var("OUT_DIR").unwrap();
let g1_path = Path::new(&out_dir).join("g1.bin");
let g2_path = Path::new(&out_dir).join("g2.bin");
let roots_of_unity_path = Path::new(&out_dir).join("roots_of_unity.bin");

let g1_exists = g1_path.exists();
let g2_exists = g2_path.exists();
let roots_of_unity_exists = roots_of_unity_path.exists();

if g1_exists && g2_exists && roots_of_unity_exists {
println!("cargo:rerun-if-changed=src/trusted_setup.rs"); // Re-run this build script if the `g1.bin`,`g2.bin`, or `roots_of_unity.bin` files are changed
Expand Down Expand Up @@ -204,7 +209,7 @@ fn main() {
.create(true)
.truncate(true)
.write(true)
.open("src/roots_of_unity.bin")
.open(&roots_of_unity_path)
.unwrap();

roots_of_unity_file
Expand All @@ -215,7 +220,7 @@ fn main() {
.create(true)
.truncate(true)
.write(true)
.open("src/g1.bin")
.open(&g1_path)
.unwrap();

g1_file.write_all(&g1_bytes).unwrap();
Expand All @@ -224,13 +229,13 @@ fn main() {
.create(true)
.truncate(true)
.write(true)
.open("src/g2.bin")
.open(&g2_path)
.unwrap();

g2_file.write_all(&g2_bytes).unwrap();
}

#[cfg(target_arch = "riscv32")]
#[cfg(any(target_arch = "riscv32", doc))]
fn main() {
// Binaries cannot be built in a RISC-V environment
// Binaries cannot be built in a RISC-V environment or when building docs
}
Binary file removed src/g1.bin
Binary file not shown.
Binary file removed src/g2.bin
Binary file not shown.
Binary file removed src/roots_of_unity.bin
Binary file not shown.
39 changes: 19 additions & 20 deletions src/trusted_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,31 @@ use core::{
};
use spin::Once;

pub const fn get_roots_of_unity() -> &'static [Scalar] {
const ROOT_OF_UNITY_BYTES: &[u8] = include_bytes!("roots_of_unity.bin");
let roots_of_unity: &[Scalar] = unsafe {
transmute(slice::from_raw_parts(
ROOT_OF_UNITY_BYTES.as_ptr(),
NUM_ROOTS_OF_UNITY,
))
};
roots_of_unity
pub fn get_roots_of_unity() -> &'static [Scalar] {
static ROOTS_OF_UNITY: Once<&'static [Scalar]> = Once::new();
ROOTS_OF_UNITY.call_once(|| {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/roots_of_unity.bin"));
unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_ROOTS_OF_UNITY)) }
})
}

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
pub fn get_g1_points() -> &'static [G1Affine] {
static G1_POINTS: Once<&'static [G1Affine]> = Once::new();
G1_POINTS.call_once(|| {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/g1.bin"));
unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_G1_POINTS)) }
})
}

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
pub fn get_g2_points() -> &'static [G2Affine] {
static G2_POINTS: Once<&'static [G2Affine]> = Once::new();
G2_POINTS.call_once(|| {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/g2.bin"));
unsafe { transmute(slice::from_raw_parts(bytes.as_ptr(), NUM_G1_POINTS)) }
})
}

pub const fn get_kzg_settings() -> KzgSettings {
pub fn get_kzg_settings() -> KzgSettings {
KzgSettings {
roots_of_unity: get_roots_of_unity(),
g1_points: get_g1_points(),
Expand Down

0 comments on commit 916b19c

Please sign in to comment.