Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write and Read KZG setup parameters #312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# kzg params
/params/*
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ thiserror = "1.0"
group = "0.13.0"
once_cell = "1.18.0"
itertools = "0.12.0"
serde_json = "1.0.114"

[target.'cfg(any(target_arch = "x86_64", target_arch = "aarch64"))'.dependencies]
pasta-msm = { version = "0.1.4" }
Expand Down
100 changes: 84 additions & 16 deletions src/provider/hyperkzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use itertools::Itertools;
use rand_core::OsRng;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::env::var;
use std::io::BufWriter;
use std::{fs::{self, File}, io::{self, BufReader}};

/// Alias to points on G1 that are in preprocessed form
type G1Affine<E> = <<E as Engine>::GE as DlogGroup>::AffineGroupElement;
Expand All @@ -50,6 +53,56 @@ where
self.ck.len()
}
}
/// This enum specifies how various types are serialized and deserialized.
#[derive(Clone, Copy, Debug)]
pub enum SerdeFormat {
/// Bincode format
Bincode,
/// JSON format
Json,
}

impl<E: Engine> CommitmentKey<E>
where
E::GE: PairingGroup,
{
/// Write the commitment key to a writer
pub fn write_custom<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) -> io::Result<()>
where
E: Engine,
E::GE: PairingGroup,
{
match format {
SerdeFormat::Bincode => {
unimplemented!("Bincode serialization is not yet supported");
}
SerdeFormat::Json => {
let ser = serde_json::to_string(&self)?;
writer.write_all(ser.as_bytes())?;
}
}
Ok(())
}

/// Read the commitment key from a reader
pub fn read_custom<R: io::Read>(reader: &mut R, format: SerdeFormat) -> io::Result<Self>
where
E: Engine,
E::GE: PairingGroup,
{
match format {
SerdeFormat::Bincode => {
unimplemented!("Bincode serialization is not yet supported");
}
SerdeFormat::Json => {
let mut ser = String::new();
reader.read_to_string(&mut ser)?;
let res: Self = serde_json::from_str(&ser)?;
Ok(res)
}
}
}
}

/// A KZG commitment
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -217,27 +270,42 @@ where
type Commitment = Commitment<E>;
type CommitmentKey = CommitmentKey<E>;

/// Attempts to read the srs from a file found in `./params/kzg_bn254_{k}.srs` or `{dir}/kzg_bn254_{k}.srs` if `PARAMS_DIR` env var is specified, creates a file it if it does not exist.
fn setup(_label: &'static [u8], n: usize) -> Self::CommitmentKey {
// NOTE: this is for testing purposes and should not be used in production
// TODO: we need to decide how to generate load/store parameters
let tau = E::Scalar::random(OsRng);
let num_gens = n.next_power_of_two();
let k = num_gens.trailing_zeros();
let dir = var("PARAMS_DIR").unwrap_or_else(|_| "./params".to_string());
let path = format!("{dir}/kzg_bn254_{k}.srs");
match File::open(path.as_str()) {
Ok(f) => {
let mut reader = BufReader::new(f);
Self::CommitmentKey::read_custom(&mut reader, SerdeFormat::Json).unwrap()
}
Err(_) => {
fs::create_dir_all(dir).unwrap();
let tau = E::Scalar::random(OsRng);

// Compute powers of tau in E::Scalar, then scalar muls in parallel
let mut powers_of_tau: Vec<E::Scalar> = Vec::with_capacity(num_gens);
powers_of_tau.insert(0, E::Scalar::ONE);
for i in 1..num_gens {
powers_of_tau.insert(i, powers_of_tau[i - 1] * tau);
}

// Compute powers of tau in E::Scalar, then scalar muls in parallel
let mut powers_of_tau: Vec<E::Scalar> = Vec::with_capacity(num_gens);
powers_of_tau.insert(0, E::Scalar::ONE);
for i in 1..num_gens {
powers_of_tau.insert(i, powers_of_tau[i - 1] * tau);
}

let ck: Vec<G1Affine<E>> = (0..num_gens)
.into_par_iter()
.map(|i| (<E::GE as DlogGroup>::gen() * powers_of_tau[i]).affine())
.collect();
let ck: Vec<G1Affine<E>> = (0..num_gens)
.into_par_iter()
.map(|i| (<E::GE as DlogGroup>::gen() * powers_of_tau[i]).affine())
.collect();

let tau_H = (<<E::GE as PairingGroup>::G2 as DlogGroup>::gen() * tau).affine();
let tau_H = (<<E::GE as PairingGroup>::G2 as DlogGroup>::gen() * tau).affine();

Self::CommitmentKey { ck, tau_H }
let params = Self::CommitmentKey { ck, tau_H };
params
.write_custom(&mut BufWriter::new(File::create(path).unwrap()), SerdeFormat::Json)
.unwrap();
params
}
}
}

fn commit(ck: &Self::CommitmentKey, v: &[E::Scalar]) -> Self::Commitment {
Expand Down