Skip to content

Commit

Permalink
feat: use OUT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Aug 30, 2024
1 parent 9e5622b commit 3cf3d48
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
19 changes: 12 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
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,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();
Expand Down
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 3cf3d48

Please sign in to comment.