diff --git a/Cargo.lock b/Cargo.lock index ed2df59..a8f4e2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,7 +203,7 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "kzg-rs" -version = "0.2.2" +version = "0.2.3" dependencies = [ "ff", "hex", diff --git a/Cargo.toml b/Cargo.toml index 0ae911c..3aa1a3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/README.md b/README.md index d11960a..3f7aff4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.rs b/build.rs index f1a9813..8f9ebb1 100644 --- a/build.rs +++ b/build.rs @@ -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], @@ -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 @@ -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 @@ -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(); @@ -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 } diff --git a/src/g1.bin b/src/g1.bin deleted file mode 100644 index 006b78c..0000000 Binary files a/src/g1.bin and /dev/null differ diff --git a/src/g2.bin b/src/g2.bin deleted file mode 100644 index acd642d..0000000 Binary files a/src/g2.bin and /dev/null differ diff --git a/src/roots_of_unity.bin b/src/roots_of_unity.bin deleted file mode 100644 index 4058fe2..0000000 Binary files a/src/roots_of_unity.bin and /dev/null differ diff --git a/src/trusted_setup.rs b/src/trusted_setup.rs index bca2fcb..981311d 100644 --- a/src/trusted_setup.rs +++ b/src/trusted_setup.rs @@ -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(),