Skip to content

Commit

Permalink
feat: load trusted setup with const function
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jun 21, 2024
1 parent 0111e2d commit 419abbe
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 80,637 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ const-chunks = "0.3.0"
default = ["std"]
serde = ["dep:serde"]
std = ["hex/std", "serde/std"]

[[bin]]
name = "build_binaries"
path = "scripts/build_binaries.rs"
39 changes: 39 additions & 0 deletions scripts/build_binaries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::{fs, io::Write};

use bls12_381::{G1Affine, G2Affine};
use kzg_rs::{load_trusted_setup_file_brute, KzgSettings, KzgSettingsOwned};

fn main() {
let KzgSettingsOwned {
max_width: _,
g1_points,
g2_points,
} = load_trusted_setup_file_brute().unwrap();

let mut g1_bytes: Vec<u8> = Vec::new();
let mut g2_bytes: Vec<u8> = Vec::new();

g1_points.iter().for_each(|&v| {
g1_bytes.extend_from_slice(unsafe { &std::mem::transmute::<G1Affine, [u8; 104]>(v) });
});

g2_points.iter().for_each(|&v| {
g2_bytes.extend_from_slice(unsafe { &std::mem::transmute::<G2Affine, [u8; 200]>(v) });
});

let mut g1_file = fs::OpenOptions::new()
.create(true)
.write(true)
.open("src/g1.bin")
.unwrap();

g1_file.write_all(&g1_bytes).unwrap();

let mut g2_file = fs::OpenOptions::new()
.create(true)
.write(true)
.open("src/g2.bin")
.unwrap();

g2_file.write_all(&g2_bytes).unwrap();
}
2 changes: 1 addition & 1 deletion src/dtypes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::enums::KzgError;
use crate::{hex_to_bytes, NUM_G1_POINTS};
use crate::hex_to_bytes;

macro_rules! define_bytes_type {
($name:ident, $size:expr) => {
Expand Down
Binary file modified src/g1.bin
Binary file not shown.
Binary file removed src/g1_points.bin
Binary file not shown.
Binary file modified src/g2.bin
Binary file not shown.
Binary file removed src/g2_points.bin
Binary file not shown.
64 changes: 2 additions & 62 deletions src/kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,71 +84,11 @@ impl KzgProof {

#[cfg(test)]
mod tests {
use core::{
mem::{self, transmute},
slice,
};
use std::{
fs,
io::Write,
path::{self, PathBuf},
};

use bls12_381::{G1Affine, G2Affine};

use crate::{
get_g1_points, test_format::Test, KzgProof, KzgSettings, BYTES_PER_G1_POINT, NUM_G1_POINTS,
};
use crate::{test_format::Test, KzgProof, KzgSettings};
use std::{fs, path::PathBuf};

const VERIFY_KZG_PROOF_TESTS: &str = "tests/verify_kzg_proof/*/*";

// TODO: Move to script
// #[test]
// fn build_binary_file() {
// let kzg_settings = KzgSettings::load_trusted_setup_file();
// let g1_values = kzg_settings.g1_points.clone();
// let g2_values = kzg_settings.g2_points.clone();

// let mut g1_bytes: Vec<u8> = Vec::new();
// let mut g2_bytes: Vec<u8> = Vec::new();

// g1_values.iter().for_each(|&v| {
// g1_bytes.extend_from_slice(unsafe { &std::mem::transmute::<G1Affine, [u8; 104]>(v) });
// });

// g2_values.iter().for_each(|&v| {
// g2_bytes.extend_from_slice(unsafe { &std::mem::transmute::<G2Affine, [u8; 200]>(v) });
// });

// let mut g1_file = fs::OpenOptions::new()
// .create(true)
// .write(true)
// .open("g1.bin")
// .unwrap();

// g1_file.write_all(&g1_bytes).unwrap();

// let mut g2_file = fs::OpenOptions::new()
// .create(true)
// .write(true)
// .open("g2.bin")
// .unwrap();

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

#[test]
fn build_from_binary_file() {
let g1_bytes = include_bytes!("g1.bin");
let g1 = get_g1_points();
// let g1: &[G1Affine] =
// unsafe { transmute(slice::from_raw_parts(g1_bytes.as_ptr(), NUM_G1_POINTS)) };
println!("{:?}", g1);
// G1Affine { x: 0x025a6f586726c68d45f00ad0f5a4436523317939a47713f78fd4fe81cd74236fdac1b04ecd97c2d0267d6f4981d7beb1, y: 0x09a1275f9efcc1e3166cdea9eff740ac675d8ec22fc07467f17c933e66ef3502e44dc20dcefd2f29621de1b9f64400f9, infinity: Choice(0) }

// println!("{}", g1_bytes.len() / mem::size_of::<G1Affine>())
}

#[test]
fn test_verify_kzg_proof() {
let kzg_settings = KzgSettings::load_trusted_setup_file();
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod consts;
pub mod dtypes;
pub mod enums;
pub mod kzg_proof;
mod parse;
pub mod test_format;
pub mod trusted_setup;

Expand Down
90 changes: 0 additions & 90 deletions src/parse.rs

This file was deleted.

Loading

0 comments on commit 419abbe

Please sign in to comment.