diff --git a/build.rs b/build.rs index ae087ca..8f9ebb1 100644 --- a/build.rs +++ b/build.rs @@ -6,7 +6,7 @@ include!("src/pairings.rs"); #[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,7 +229,7 @@ fn main() { .create(true) .truncate(true) .write(true) - .open("src/g2.bin") + .open(&g2_path) .unwrap(); g2_file.write_all(&g2_bytes).unwrap(); 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(),