Skip to content

Commit

Permalink
Minor changes to run midnight_circuits (#26)
Browse files Browse the repository at this point in the history
* Minor changes to run midnight_circuits

* Write k instead of n, and bugfix of a misuse of k instead of n

* Addressing review comments
  • Loading branch information
iquerejeta authored Jan 16, 2025
1 parent e2d2525 commit da31397
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ backtrace = { version = "0.3", optional = true }
ff = "0.13"
group = "0.13"
halo2curves = { version = "0.7.0", default-features = false }
blstrs = { git = "https://github.com/davidnevadoc/blstrs", rev = "8ca6da7" }
rand_core = { version = "0.6", default-features = false }
tracing = "0.1"
blake2b_simd = "1" # MSRV 1.66.0
Expand Down
17 changes: 13 additions & 4 deletions src/poly/kzg/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl<E: Engine> Params for ParamsKZG<E> {
}

impl<E: Engine + Debug> ParamsKZG<E> {
/// Downsize the current parameters to match a smaller `k`.
pub fn downsize(&mut self, new_k: u32) {
let n = 1 << new_k;
assert!(n < self.g_lagrange.len() as u32);
self.g.truncate(n as usize);
self.g_lagrange = g_to_lagrange(self.g.iter().map(|g| g.to_curve()).collect(), new_k);
}

/// Initializes parameters for the curve, draws toxic secret from given rng.
/// MUST NOT be used in production.
pub fn unsafe_setup<R: RngCore>(k: u32, rng: R) -> Self {
Expand Down Expand Up @@ -136,7 +144,7 @@ impl<E: Engine + Debug> ParamsKZG<E> {
E::G1Affine: CurveAffine + ProcessedSerdeObject,
E::G2Affine: CurveAffine + ProcessedSerdeObject,
{
writer.write_all(&(self.g.len() as u64).to_le_bytes())?;
writer.write_all(&self.g.len().ilog2().to_le_bytes())?;
for el in self.g.iter() {
el.write(writer, format)?;
}
Expand All @@ -154,9 +162,10 @@ impl<E: Engine + Debug> ParamsKZG<E> {
E::G1Affine: CurveAffine + ProcessedSerdeObject,
E::G2Affine: CurveAffine + ProcessedSerdeObject,
{
let mut n = [0u8; 8];
reader.read_exact(&mut n[..])?;
let n = u64::from_le_bytes(n) as usize;
let mut k = [0u8; 4];
reader.read_exact(&mut k[..])?;
let k = u32::from_le_bytes(k);
let n = 1 << k;

let (g, g_lagrange) = match format {
SerdeFormat::Processed => {
Expand Down
58 changes: 54 additions & 4 deletions src/transcript/implementors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::transcript::{
Hashable, Sampleable, TranscriptHash, BLAKE2B_PREFIX_CHALLENGE, BLAKE2B_PREFIX_COMMON,
};
use blake2b_simd::{Params, State as Blake2bState};
use ff::FromUniformBytes;
use ff::{FromUniformBytes, PrimeField};
use group::GroupEncoding;
use halo2curves::bn256::{Fr, G1Affine};

Expand Down Expand Up @@ -33,6 +33,7 @@ impl TranscriptHash for Blake2bState {
///////////////////////////////////////////////////

impl Hashable<Blake2bState> for G1Affine {
/// Converts it to compressed form in bytes
fn to_input(&self) -> Vec<u8> {
self.to_bytes().as_ref().to_vec()
}
Expand All @@ -45,10 +46,59 @@ impl Hashable<Blake2bState> for Fr {
}

impl Sampleable<Blake2bState> for Fr {
fn sample(out: Vec<u8>) -> Self {
assert!(out.len() <= 64);
fn sample(hash_output: Vec<u8>) -> Self {
assert!(hash_output.len() <= 64);
let mut bytes = [0u8; 64];
bytes[..out.len()].copy_from_slice(&out);
bytes[..hash_output.len()].copy_from_slice(&hash_output);
Fr::from_uniform_bytes(&bytes)
}
}

//////////////////////////////////////////////////////////
/// Implementation of Hashable for BLS12-381 with Blake //
//////////////////////////////////////////////////////////

impl Hashable<Blake2bState> for blstrs::G1Affine {
/// Converts it to compressed form in bytes
fn to_input(&self) -> Vec<u8> {
self.to_bytes().as_ref().to_vec()
}
}

impl Hashable<Blake2bState> for blstrs::Scalar {
fn to_input(&self) -> Vec<u8> {
self.to_repr().to_vec()
}
}

impl Sampleable<Blake2bState> for blstrs::Scalar {
fn sample(hash_output: Vec<u8>) -> Self {
assert!(hash_output.len() <= 64);
assert!(hash_output.len() >= (blstrs::Scalar::NUM_BITS as usize / 8) + 12);
let mut bytes = [0u8; 64];
bytes[..hash_output.len()].copy_from_slice(&hash_output);
blstrs::Scalar::from_uniform_bytes(&bytes)
}
}

impl Hashable<Blake2bState> for halo2curves::bls12381::G1Affine {
/// Converts it to compressed form in bytes
fn to_input(&self) -> Vec<u8> {
self.to_bytes().as_ref().to_vec()
}
}

impl Hashable<Blake2bState> for halo2curves::bls12381::Fr {
fn to_input(&self) -> Vec<u8> {
self.to_repr().as_ref().to_vec()
}
}

impl Sampleable<Blake2bState> for halo2curves::bls12381::Fr {
fn sample(hash_output: Vec<u8>) -> Self {
assert!(hash_output.len() <= 64);
let mut bytes = [0u8; 64];
bytes[..hash_output.len()].copy_from_slice(&hash_output);
halo2curves::bls12381::Fr::from_uniform_bytes(&bytes)
}
}
2 changes: 1 addition & 1 deletion src/transcript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait Hashable<H: TranscriptHash> {
/// Trait to represent values that can be sampled from a `TranscriptHash`
pub trait Sampleable<H: TranscriptHash> {
/// Converts `H`'s output to Self
fn sample(out: H::Output) -> Self;
fn sample(hash_output: H::Output) -> Self;
}

/// Generic transcript view
Expand Down

0 comments on commit da31397

Please sign in to comment.