Skip to content

Commit

Permalink
Resolve Prover optimization: memory reduction privacy-scaling-explora…
Browse files Browse the repository at this point in the history
…tions#77 (#6)

* Resolve taikoxyz/zkevm-circuits#77

* Please Clippy
  • Loading branch information
einar-taiko authored Jun 26, 2023
1 parent d0b65f4 commit 9eaccbb
Show file tree
Hide file tree
Showing 9 changed files with 1,130 additions and 307 deletions.
1 change: 1 addition & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ name = "fft"
harness = false

[dependencies]
itertools = "0.10"
backtrace = { version = "0.3", optional = true }
rayon = "1.5.1"
ff = "0.13"
Expand Down
11 changes: 3 additions & 8 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,11 @@ pub struct PinnedVerificationKey<'a, C: CurveAffine> {
#[derive(Clone, Debug)]
pub struct ProvingKey<C: CurveAffine> {
vk: VerifyingKey<C>,
l0: Polynomial<C::Scalar, ExtendedLagrangeCoeff>,
l_last: Polynomial<C::Scalar, ExtendedLagrangeCoeff>,
l_active_row: Polynomial<C::Scalar, ExtendedLagrangeCoeff>,
l0: Polynomial<C::Scalar, Coeff>,
l_last: Polynomial<C::Scalar, Coeff>,
l_active_row: Polynomial<C::Scalar, Coeff>,
fixed_values: Vec<Polynomial<C::Scalar, LagrangeCoeff>>,
fixed_polys: Vec<Polynomial<C::Scalar, Coeff>>,
fixed_cosets: Vec<Polynomial<C::Scalar, ExtendedLagrangeCoeff>>,
permutation: permutation::ProvingKey<C>,
ev: Evaluator<C>,
}
Expand All @@ -398,7 +397,6 @@ where
+ scalar_len * (self.l0.len() + self.l_last.len() + self.l_active_row.len())
+ polynomial_slice_byte_length(&self.fixed_values)
+ polynomial_slice_byte_length(&self.fixed_polys)
+ polynomial_slice_byte_length(&self.fixed_cosets)
+ self.permutation.bytes_length()
}
}
Expand All @@ -424,7 +422,6 @@ where
self.l_active_row.write(writer, format)?;
write_polynomial_slice(&self.fixed_values, writer, format)?;
write_polynomial_slice(&self.fixed_polys, writer, format)?;
write_polynomial_slice(&self.fixed_cosets, writer, format)?;
self.permutation.write(writer, format)?;
Ok(())
}
Expand Down Expand Up @@ -456,7 +453,6 @@ where
let l_active_row = Polynomial::read(reader, format)?;
let fixed_values = read_polynomial_vec(reader, format)?;
let fixed_polys = read_polynomial_vec(reader, format)?;
let fixed_cosets = read_polynomial_vec(reader, format)?;
let permutation = permutation::ProvingKey::read(reader, format)?;
let ev = Evaluator::new(vk.cs());
Ok(Self {
Expand All @@ -466,7 +462,6 @@ where
l_active_row,
fixed_values,
fixed_polys,
fixed_cosets,
permutation,
ev,
})
Expand Down
67 changes: 67 additions & 0 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use core::cmp::max;
use core::ops::{Add, Mul};
use ff::Field;
use itertools::Itertools;
use sealed::SealedPhase;
use std::cmp::Ordering;
use std::collections::HashMap;
Expand Down Expand Up @@ -1248,6 +1249,72 @@ impl<F: Field> Expression<F> {
&|a, _| a,
)
}

/// Extracts all used instance columns in this expression
pub fn extract_instances(&self) -> Vec<usize> {
self.evaluate(
&|_| vec![],
&|_| vec![],
&|_| vec![],
&|_| vec![],
&|query| vec![query.column_index],
&|_| vec![],
&|a| a,
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|a, _| a,
)
}

/// Extracts all used advice columns in this expression
pub fn extract_advices(&self) -> Vec<usize> {
self.evaluate(
&|_| vec![],
&|_| vec![],
&|_| vec![],
&|query| vec![query.column_index],
&|_| vec![],
&|_| vec![],
&|a| a,
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|a, _| a,
)
}

/// Extracts all used fixed columns in this expression
pub fn extract_fixed(&self) -> Vec<usize> {
self.evaluate(
&|_| vec![],
&|_| vec![],
&|query| vec![query.column_index],
&|_| vec![],
&|_| vec![],
&|_| vec![],
&|a| a,
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|mut a, b| {
a.extend(b);
a.into_iter().unique().collect()
},
&|a, _| a,
)
}
}

impl<F: std::fmt::Debug> std::fmt::Debug for Expression<F> {
Expand Down
Loading

0 comments on commit 9eaccbb

Please sign in to comment.