Skip to content

Commit

Permalink
refactor: fix TODOs & leftover
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Apr 25, 2024
1 parent af68bf3 commit 32989af
Showing 1 changed file with 86 additions and 26 deletions.
112 changes: 86 additions & 26 deletions halo2_backend/src/poly/kzg/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ pub struct ParamsKZG<E: Engine> {
#[derive(Debug, Clone)]
pub struct ParamsVerifierKZG<E: Engine> {
pub(crate) k: u32,
// TODO
// Either remove this or turn into Arc or int reference
pub(crate) g_lagrange: Vec<E::G1Affine>,
pub(crate) s_g2: E::G2Affine,
}
Expand Down Expand Up @@ -67,15 +65,13 @@ where
}

/// Writes params to a buffer.
fn write<W: io::Write>(&self, _writer: &mut W) -> io::Result<()> {
//TODO
unimplemented!()
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
Self::write_custom(&self, writer, SerdeFormat::RawBytes)

Check failure on line 69 in halo2_backend/src/poly/kzg/commitment.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

this expression creates a reference which is immediately dereferenced by the compiler

error: this expression creates a reference which is immediately dereferenced by the compiler --> halo2_backend/src/poly/kzg/commitment.rs:69:28 | 69 | Self::write_custom(&self, writer, SerdeFormat::RawBytes) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-D clippy::needless-borrow` implied by `-D warnings`

Check warning on line 69 in halo2_backend/src/poly/kzg/commitment.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> halo2_backend/src/poly/kzg/commitment.rs:69:28 | 69 | Self::write_custom(&self, writer, SerdeFormat::RawBytes) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-W clippy::needless-borrow` implied by `-W clippy::all`

Check warning on line 69 in halo2_backend/src/poly/kzg/commitment.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> halo2_backend/src/poly/kzg/commitment.rs:69:28 | 69 | Self::write_custom(&self, writer, SerdeFormat::RawBytes) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-W clippy::needless-borrow` implied by `-W clippy::all`
}

/// Reads params from a buffer.
fn read<R: io::Read>(_reader: &mut R) -> io::Result<Self> {
//TODO
unimplemented!()
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
Self::read_custom(reader, SerdeFormat::RawBytes)
}
}

Expand All @@ -91,6 +87,87 @@ where
}
}

impl<E: Engine> ParamsVerifierKZG<E>
where
E::G1Affine: CurveAffine,
E::G1: CurveExt<AffineExt = E::G1Affine>,
{
pub fn write_custom<W: io::Write>(&self, writer: &mut W, format: SerdeFormat) -> io::Result<()>
where
E::G1Affine: SerdeCurveAffine,
E::G2Affine: SerdeCurveAffine,
{
writer.write_all(&self.k.to_le_bytes())?;
for point in self.g_lagrange.iter() {
point.write(writer, format)?;
}
self.s_g2.write(writer, format)?;
Ok(())
}

pub fn read_custom<R: io::Read>(reader: &mut R, format: SerdeFormat) -> io::Result<Self>
where
E::G1Affine: SerdeCurveAffine,
E::G2Affine: SerdeCurveAffine,
{
let mut k = [0u8; 4];
reader.read_exact(&mut k[..])?;
let k = u32::from_le_bytes(k);
let n = 1 << k;
// This is a generous bound on the size of the domain.
debug_assert!(k < 32);

let g_lagrange = match format {
SerdeFormat::Processed => {
use group::GroupEncoding;
let load_points_from_file_parallelly =
|reader: &mut R| -> io::Result<Vec<Option<E::G1Affine>>> {
let mut points_compressed = vec![
<<E as Engine>::G1Affine as GroupEncoding>::Repr::default();
n as usize
];
for points_compressed in points_compressed.iter_mut() {
reader.read_exact((*points_compressed).as_mut())?;
}

let mut points = vec![Option::<E::G1Affine>::None; n as usize];
parallelize(&mut points, |points, chunks| {
for (i, point) in points.iter_mut().enumerate() {
*point = Option::from(E::G1Affine::from_bytes(
&points_compressed[chunks + i],
));
}
});
Ok(points)
};

let g_lagrange = load_points_from_file_parallelly(reader)?;
g_lagrange
.iter()
.map(|point| {
point.ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "invalid point encoding")
})
})
.collect::<Result<_, _>>()?
}
SerdeFormat::RawBytes => (0..n)
.map(|_| <E::G1Affine as SerdeCurveAffine>::read(reader, format))
.collect::<Result<Vec<_>, _>>()?,
SerdeFormat::RawBytesUnchecked => (0..n)
.map(|_| <E::G1Affine as SerdeCurveAffine>::read(reader, format).unwrap())
.collect::<Vec<_>>(),
};
let s_g2 = E::G2Affine::read(reader, format)?;

Ok(Self {
k,
g_lagrange,
s_g2,
})
}
}

/// Umbrella commitment scheme construction for all KZG variants
#[derive(Debug)]
pub struct KZGCommitmentScheme<E: Engine> {
Expand Down Expand Up @@ -217,22 +294,10 @@ where
}
}

/// Returns gernerator on G2
// TODO Remove
pub fn g2(&self) -> E::G2Affine {
self.g2
}

/// Returns first power of secret on G2
// TODO Remove if possible
pub fn s_g2(&self) -> E::G2Affine {
self.s_g2
}

pub fn verifier_params(&self) -> ParamsVerifierKZG<E> {
ParamsVerifierKZG {
g_lagrange: self.g_lagrange.clone(),
k: self.k,
g_lagrange: self.g_lagrange.clone(),
s_g2: self.s_g2,
}
}
Expand Down Expand Up @@ -343,11 +408,6 @@ where
}
}

// TODO: see the issue at https://github.com/appliedzkp/halo2/issues/45
// So we probably need much smaller verifier key. However for new bases in g1 should be in verifier keys.
/// KZG multi-open verification parameters
// pub type ParamsVerifierKZG<E> = ParamsKZG<E>;

impl<E: Engine> Params<E::G1Affine> for ParamsKZG<E>
where
E::G1Affine: SerdeCurveAffine<ScalarExt = <E as Engine>::Fr, CurveExt = <E as Engine>::G1>,
Expand Down

0 comments on commit 32989af

Please sign in to comment.