Skip to content

Commit

Permalink
feat: load g1 and g2 points unsafely
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jun 19, 2024
1 parent 58d6bc8 commit 7a142b1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
26 changes: 23 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ serde = { version = "1.0", optional = true, default-features = false, features =
"alloc",
"derive",
] }
bls12_381 = "0.8.0"
bls12_381 = { path = "../bls12_381" }
glob = "0.3.1"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
serde_derive = "1.0"
serde_yaml = { version = "0.9" }
sp1-derive = { git = "https://github.com/succinctlabs/sp1.git", branch = "main" }

[features]
default = ["std"]
Expand Down
3 changes: 3 additions & 0 deletions src/dtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ macro_rules! define_bytes_type {
pub struct $name([u8; $size]);

impl $name {
#[sp1_derive::cycle_tracker]
pub fn from_slice(slice: &[u8]) -> Result<Self, KzgError> {
if slice.len() != $size {
return Err(KzgError::InvalidBytesLength(
Expand All @@ -18,12 +19,14 @@ macro_rules! define_bytes_type {
Ok($name(bytes))
}

#[sp1_derive::cycle_tracker]
pub fn from_hex(hex_str: &str) -> Result<Self, KzgError> {
Self::from_slice(&hex_to_bytes(hex_str).unwrap())
}
}

impl Into<[u8; $size]> for $name {
#[sp1_derive::cycle_tracker]
fn into(self) -> [u8; $size] {
self.0
}
Expand Down
5 changes: 4 additions & 1 deletion src/kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::trusted_setup::KzgSettings;
use alloc::vec::Vec;
use bls12_381::{pairing, G1Affine, G2Affine, Scalar};

// #[sp1_derive::cycle_tracker]
fn safe_g1_affine_from_bytes(bytes: &Bytes48) -> Result<G1Affine, KzgError> {
let g1 = G1Affine::from_compressed(&(bytes.clone().into()));
if g1.is_none().into() {
Expand All @@ -14,6 +15,7 @@ fn safe_g1_affine_from_bytes(bytes: &Bytes48) -> Result<G1Affine, KzgError> {
Ok(g1.unwrap())
}

// #[sp1_derive::cycle_tracker]
fn safe_scalar_affine_from_bytes(bytes: &Bytes32) -> Result<Scalar, KzgError> {
let lendian: [u8; 32] = Into::<[u8; 32]>::into(bytes.clone())
.iter()
Expand All @@ -34,6 +36,7 @@ fn safe_scalar_affine_from_bytes(bytes: &Bytes32) -> Result<Scalar, KzgError> {
pub struct KzgProof {}

impl KzgProof {
#[sp1_derive::cycle_tracker]
pub fn verify_kzg_proof(
commitment_bytes: &Bytes48,
z_bytes: &Bytes32,
Expand Down Expand Up @@ -114,7 +117,7 @@ mod tests {
}
Err(e) => {
assert!(test.get_output().is_none());
eprintln!("Error: {:?}", e);
eprintln!("Error: {:?}", e);
}
}
}
Expand Down
48 changes: 40 additions & 8 deletions src/trusted_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ pub struct KzgSettings {
}

impl KzgSettings {
#[sp1_derive::cycle_tracker]
pub fn load_trusted_setup_file() -> Result<Self, KzgError> {
println!("cycle-tracker-start: parse-trusted-setup-file");
let trusted_setup_file: Vec<String> = TRUSTED_SETUP
.to_string()
.split("\n")
.map(|x| x.to_string())
.collect();
println!("cycle-tracker-end: parse-trusted-setup-file");

let num_g1_points = trusted_setup_file[0].parse::<usize>().unwrap();
let num_g2_points = trusted_setup_file[1].parse::<usize>().unwrap();
let g1_points_idx = num_g1_points + 2;
let g2_points_idx = g1_points_idx + num_g2_points;

println!("cycle-tracker-start: parse-g1-points");
let g1_points: Vec<[u8; BYTES_PER_G1_POINT]> =
hex_to_bytes(&trusted_setup_file[2..g1_points_idx].join(""))
.unwrap()
Expand All @@ -38,6 +42,8 @@ impl KzgSettings {
array
})
.collect();
println!("cycle-tracker-end: parse-g1-points");
println!("cycle-tracker-start: parse-g2-points");
let g2_points: Vec<[u8; BYTES_PER_G2_POINT]> =
hex_to_bytes(&trusted_setup_file[g1_points_idx..g2_points_idx].join(""))
.unwrap()
Expand All @@ -48,36 +54,59 @@ impl KzgSettings {
array
})
.collect();
println!("cycle-tracker-end: parse-g2-points");

assert_eq!(g1_points.len(), num_g1_points);
assert_eq!(g2_points.len(), num_g2_points);

Self::load_trusted_setup(g1_points, g2_points)
}

#[sp1_derive::cycle_tracker]
pub fn load_trusted_setup(
g1_points: Vec<[u8; BYTES_PER_G1_POINT]>,
g2_points: Vec<[u8; BYTES_PER_G2_POINT]>,
) -> Result<Self, KzgError> {
let mut kzg_settings = KzgSettings::default();

println!("cycle-tracker-start: max-width-calculation");
let mut max_scale = 0;
while (1 << max_scale) < g1_points.len() {
max_scale += 1;
}
kzg_settings.max_width = 1 << max_scale;

// Convert all bytes to points
g1_points.iter().for_each(|bytes| {
let g1_affine =
G1Affine::from_compressed(bytes).expect("load_trusted_setup Invalid g1 bytes");
println!("cycle-tracker-end: max-width-calculation");

println!("cycle-tracker-start: convert-g1-bytes-to-points");
g1_points.iter().enumerate().for_each(|(i, bytes)| {
println!(
"cycle-tracker-start: convert-g1-bytes-to-points-iteration-{}",
i
);
let g1_affine = G1Affine::from_compressed_unchecked(bytes)
.expect("load_trusted_setup Invalid g1 bytes");
kzg_settings.g1_values.push(g1_affine);
println!(
"cycle-tracker-end: convert-g1-bytes-to-points-iteration-{}",
i
);
});
g2_points.iter().for_each(|bytes| {
let g2_affine =
G2Affine::from_compressed(bytes).expect("load_trusted_setup Invalid g2 bytes");
println!("cycle-tracker-end: convert-g1-bytes-to-points");
println!("cycle-tracker-start: convert-g2-bytes-to-points");
g2_points.iter().enumerate().for_each(|(i, bytes)| {
println!(
"cycle-tracker-start: convert-g2-bytes-to-points-iteration-{}",
i
);
let g2_affine = G2Affine::from_compressed_unchecked(bytes)
.expect("load_trusted_setup Invalid g2 bytes");
kzg_settings.g2_values.push(g2_affine);
println!(
"cycle-tracker-end: convert-g2-bytes-to-points-iteration-{}",
i
);
});
println!("cycle-tracker-end: convert-g2-bytes-to-points");

let _ = is_trusted_setup_in_lagrange_form(&kzg_settings);

Expand All @@ -87,6 +116,7 @@ impl KzgSettings {
Ok(kzg_settings)
}
}
// #[sp1_derive::cycle_tracker]
fn bit_reversal_permutation(g1_values: Vec<G1Affine>) -> Result<Vec<G1Affine>, KzgError> {
let n = g1_values.len();
assert!(n.is_power_of_two(), "n must be a power of 2");
Expand All @@ -102,12 +132,14 @@ fn bit_reversal_permutation(g1_values: Vec<G1Affine>) -> Result<Vec<G1Affine>, K
Ok(bit_reversed_permutation)
}

// #[sp1_derive::cycle_tracker]
fn pairings_verify(a1: G1Affine, a2: G2Affine, b1: G1Affine, b2: G2Affine) -> bool {
let pairing1 = bls12_381::pairing(&a1, &a2);
let pairing2 = bls12_381::pairing(&b1, &b2);
pairing1 == pairing2
}

// #[sp1_derive::cycle_tracker]
fn is_trusted_setup_in_lagrange_form(kzg_settings: &KzgSettings) -> Result<(), KzgError> {
let n1 = kzg_settings.g1_values.len();
let n2 = kzg_settings.g2_values.len();
Expand Down

0 comments on commit 7a142b1

Please sign in to comment.