Merge pull request #229 from zcash/remove-metrics

Remove metrics crate and inline modelling
This commit is contained in:
str4d
2021-03-05 11:52:47 +13:00
committed by GitHub
10 changed files with 0 additions and 446 deletions

View File

@@ -44,7 +44,6 @@ subtle = "2.3"
crossbeam-utils = "0.8"
ff = "0.9"
group = "0.9"
metrics = "0.14.2"
num_cpus = "1.13"
rand = "0.8"
blake2b_simd = "0.5"

View File

@@ -1,321 +0,0 @@
use group::Curve;
use halo2::{
arithmetic::FieldExt,
model::ModelRecorder,
pasta::{EqAffine, Fp},
plonk::*,
poly::{
commitment::{Blind, Params},
Rotation,
},
transcript::{Blake2bRead, Blake2bWrite},
};
use std::marker::PhantomData;
/// This represents an advice column at a certain row in the ConstraintSystem
#[derive(Copy, Clone, Debug)]
pub struct Variable(Column<Advice>, usize);
#[derive(Clone)]
struct PLONKConfig {
a: Column<Advice>,
b: Column<Advice>,
c: Column<Advice>,
sa: Column<Fixed>,
sb: Column<Fixed>,
sc: Column<Fixed>,
sm: Column<Fixed>,
sp: Column<Fixed>,
perm: Permutation,
}
trait StandardCS<FF: FieldExt> {
fn raw_multiply<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
where
F: FnOnce() -> Result<(FF, FF, FF), Error>;
fn raw_add<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
where
F: FnOnce() -> Result<(FF, FF, FF), Error>;
fn copy(&mut self, a: Variable, b: Variable) -> Result<(), Error>;
fn public_input<F>(&mut self, f: F) -> Result<Variable, Error>
where
F: FnOnce() -> Result<FF, Error>;
}
#[derive(Clone)]
struct MyCircuit<F: FieldExt> {
a: Option<F>,
k: u32,
}
struct StandardPLONK<'a, F: FieldExt, CS: Assignment<F> + 'a> {
cs: &'a mut CS,
config: PLONKConfig,
current_gate: usize,
_marker: PhantomData<F>,
}
impl<'a, FF: FieldExt, CS: Assignment<FF>> StandardPLONK<'a, FF, CS> {
fn new(cs: &'a mut CS, config: PLONKConfig) -> Self {
StandardPLONK {
cs,
config,
current_gate: 0,
_marker: PhantomData,
}
}
}
impl<'a, FF: FieldExt, CS: Assignment<FF>> StandardCS<FF> for StandardPLONK<'a, FF, CS> {
fn raw_multiply<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
where
F: FnOnce() -> Result<(FF, FF, FF), Error>,
{
let index = self.current_gate;
self.current_gate += 1;
let mut value = None;
self.cs.assign_advice(
|| "lhs",
self.config.a,
index,
|| {
value = Some(f()?);
Ok(value.ok_or(Error::SynthesisError)?.0)
},
)?;
self.cs.assign_advice(
|| "rhs",
self.config.b,
index,
|| Ok(value.ok_or(Error::SynthesisError)?.1),
)?;
self.cs.assign_advice(
|| "out",
self.config.c,
index,
|| Ok(value.ok_or(Error::SynthesisError)?.2),
)?;
self.cs
.assign_fixed(|| "a", self.config.sa, index, || Ok(FF::zero()))?;
self.cs
.assign_fixed(|| "b", self.config.sb, index, || Ok(FF::zero()))?;
self.cs
.assign_fixed(|| "c", self.config.sc, index, || Ok(FF::one()))?;
self.cs
.assign_fixed(|| "a * b", self.config.sm, index, || Ok(FF::one()))?;
Ok((
Variable(self.config.a, index),
Variable(self.config.b, index),
Variable(self.config.c, index),
))
}
fn raw_add<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
where
F: FnOnce() -> Result<(FF, FF, FF), Error>,
{
let index = self.current_gate;
self.current_gate += 1;
let mut value = None;
self.cs.assign_advice(
|| "lhs",
self.config.a,
index,
|| {
value = Some(f()?);
Ok(value.ok_or(Error::SynthesisError)?.0)
},
)?;
self.cs.assign_advice(
|| "rhs",
self.config.b,
index,
|| Ok(value.ok_or(Error::SynthesisError)?.1),
)?;
self.cs.assign_advice(
|| "out",
self.config.c,
index,
|| Ok(value.ok_or(Error::SynthesisError)?.2),
)?;
self.cs
.assign_fixed(|| "a", self.config.sa, index, || Ok(FF::one()))?;
self.cs
.assign_fixed(|| "b", self.config.sb, index, || Ok(FF::one()))?;
self.cs
.assign_fixed(|| "c", self.config.sc, index, || Ok(FF::one()))?;
self.cs
.assign_fixed(|| "a * b", self.config.sm, index, || Ok(FF::zero()))?;
Ok((
Variable(self.config.a, index),
Variable(self.config.b, index),
Variable(self.config.c, index),
))
}
fn copy(&mut self, left: Variable, right: Variable) -> Result<(), Error> {
self.cs.copy(
&self.config.perm,
left.0.into(),
left.1,
right.0.into(),
right.1,
)
}
fn public_input<F>(&mut self, f: F) -> Result<Variable, Error>
where
F: FnOnce() -> Result<FF, Error>,
{
let index = self.current_gate;
self.current_gate += 1;
self.cs
.assign_advice(|| "value", self.config.a, index, || f())?;
self.cs
.assign_fixed(|| "public", self.config.sp, index, || Ok(FF::one()))?;
Ok(Variable(self.config.a, index))
}
}
impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
type Config = PLONKConfig;
fn configure(meta: &mut ConstraintSystem<F>) -> PLONKConfig {
let a = meta.advice_column();
let b = meta.advice_column();
let c = meta.advice_column();
let p = meta.instance_column();
let perm = meta.permutation(&[a.into(), b.into(), c.into()]);
let sm = meta.fixed_column();
let sa = meta.fixed_column();
let sb = meta.fixed_column();
let sc = meta.fixed_column();
let sp = meta.fixed_column();
meta.create_gate("Combined add-mult", |meta| {
let a = meta.query_advice(a, Rotation::cur());
let b = meta.query_advice(b, Rotation::cur());
let c = meta.query_advice(c, Rotation::cur());
let sa = meta.query_fixed(sa, Rotation::cur());
let sb = meta.query_fixed(sb, Rotation::cur());
let sc = meta.query_fixed(sc, Rotation::cur());
let sm = meta.query_fixed(sm, Rotation::cur());
a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one()))
});
meta.create_gate("Public input", |meta| {
let a = meta.query_advice(a, Rotation::cur());
let p = meta.query_instance(p, Rotation::cur());
let sp = meta.query_fixed(sp, Rotation::cur());
sp * (a + p * (-F::one()))
});
PLONKConfig {
a,
b,
c,
sa,
sb,
sc,
sm,
sp,
perm,
}
}
fn synthesize(&self, cs: &mut impl Assignment<F>, config: PLONKConfig) -> Result<(), Error> {
let mut cs = StandardPLONK::new(cs, config);
let _ = cs.public_input(|| Ok(F::one() + F::one()))?;
for _ in 0..((1 << (self.k - 1)) - 1) {
let mut a_squared = None;
let (a0, _, c0) = cs.raw_multiply(|| {
a_squared = self.a.map(|a| a.square());
Ok((
self.a.ok_or(Error::SynthesisError)?,
self.a.ok_or(Error::SynthesisError)?,
a_squared.ok_or(Error::SynthesisError)?,
))
})?;
let (a1, b1, _) = cs.raw_add(|| {
let fin = a_squared.and_then(|a2| self.a.map(|a| a + a2));
Ok((
self.a.ok_or(Error::SynthesisError)?,
a_squared.ok_or(Error::SynthesisError)?,
fin.ok_or(Error::SynthesisError)?,
))
})?;
cs.copy(a0, a1)?;
cs.copy(b1, c0)?;
}
Ok(())
}
}
fn main() {
let recorder = Box::leak(Box::new(ModelRecorder::default()));
metrics::set_recorder(recorder).unwrap();
// TODO: Make dynamic.
let k = 11;
// Initialize the polynomial commitment parameters
let params: Params<EqAffine> = Params::new(k);
let empty_circuit: MyCircuit<Fp> = MyCircuit { a: None, k };
// Initialize the proving key
let vk = keygen_vk(&params, &empty_circuit).expect("keygen_vk should not fail");
let pk = keygen_pk(&params, vk, &empty_circuit).expect("keygen_pk should not fail");
println!("[Keygen] {}", recorder);
recorder.clear();
let mut pubinputs = pk.get_vk().get_domain().empty_lagrange();
pubinputs[0] = Fp::one();
pubinputs[0] += Fp::one();
let pubinput = params
.commit_lagrange(&pubinputs, Blind::default())
.to_affine();
recorder.clear();
let circuit: MyCircuit<Fp> = MyCircuit {
a: Some(Fp::rand()),
k,
};
// Create a proof
let mut transcript = Blake2bWrite::init(vec![]);
create_proof(&params, &pk, &[circuit], &[&[pubinputs]], &mut transcript)
.expect("proof generation should not fail");
let proof: Vec<u8> = transcript.finalize();
println!("[Prover] {}", recorder);
recorder.clear();
let pubinput_slice = &[pubinput];
let msm = params.empty_msm();
let mut transcript = Blake2bRead::init(&proof[..]);
let guard = verify_proof(
&params,
pk.get_vk(),
msm,
&[pubinput_slice],
&mut transcript,
)
.unwrap();
let msm = guard.clone().use_challenges();
assert!(msm.eval());
println!("[Verifier] {}", recorder);
}

View File

@@ -25,4 +25,3 @@ pub mod poly;
pub mod transcript;
pub mod dev;
pub mod model;

View File

@@ -1,111 +0,0 @@
//! Helpers for modelling halo2 circuit performance.
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use metrics::{GaugeValue, Key, Recorder, Unit};
/// A [`metrics`] recorder for examining halo2 metrics.
///
/// # Examples
///
/// ```
/// use halo2::model::ModelRecorder;
///
/// let recorder = Box::leak(Box::new(ModelRecorder::default()));
/// metrics::set_recorder(recorder).unwrap();
///
/// // Create circuit, build and/or verify proof.
///
/// println!("{}", recorder);
/// recorder.clear();
///
/// // Perform another operation to collect separate metrics.
/// ```
#[derive(Debug)]
pub struct ModelRecorder {
counters: Arc<RefCell<HashMap<Key, u64>>>,
}
impl Default for ModelRecorder {
fn default() -> Self {
ModelRecorder {
counters: Default::default(),
}
}
}
impl fmt::Display for ModelRecorder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut counters = self
.counters
.try_borrow()
.unwrap()
.iter()
.map(|(k, v)| (k.clone(), *v))
.collect::<Vec<_>>();
counters.sort_by(|(k1, _), (k2, _)| {
let key1 = (
k1.name().to_string(),
k1.labels()
.map(|l| (l.key(), l.value()))
.collect::<Vec<_>>(),
);
let key2 = (
k2.name().to_string(),
k2.labels()
.map(|l| (l.key(), l.value()))
.collect::<Vec<_>>(),
);
key1.cmp(&key2)
});
writeln!(f, "Recorded metrics:")?;
for (key, value) in counters.iter() {
writeln!(f, "- {}: {}", key, value)?;
}
Ok(())
}
}
impl Recorder for ModelRecorder {
fn register_counter(&self, _key: Key, _unit: Option<Unit>, _description: Option<&'static str>) {
}
fn register_gauge(&self, _key: Key, _unit: Option<Unit>, _description: Option<&'static str>) {}
fn register_histogram(
&self,
_key: Key,
_unit: Option<Unit>,
_description: Option<&'static str>,
) {
}
fn increment_counter(&self, key: Key, value: u64) {
*self
.counters
.try_borrow_mut()
.unwrap()
.entry(key)
.or_default() += value;
}
fn update_gauge(&self, _key: Key, _value: GaugeValue) {
unimplemented!()
}
fn record_histogram(&self, _key: Key, _value: f64) {
unimplemented!()
}
}
impl ModelRecorder {
/// Clear all recorded metrics.
pub fn clear(&self) {
self.counters.try_borrow_mut().unwrap().clear();
}
}

View File

@@ -58,7 +58,6 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
C::Curve::batch_normalize(&instance_commitments_projective, &mut instance_commitments);
let instance_commitments = instance_commitments;
drop(instance_commitments_projective);
metrics::counter!("instance_commitments", instance_commitments.len() as u64);
for commitment in &instance_commitments {
transcript
@@ -209,7 +208,6 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
C::Curve::batch_normalize(&advice_commitments_projective, &mut advice_commitments);
let advice_commitments = advice_commitments;
drop(advice_commitments_projective);
metrics::counter!("advice_commitments", advice_commitments.len() as u64);
for commitment in &advice_commitments {
transcript

View File

@@ -118,7 +118,6 @@ impl<C: CurveAffine> Params<C> {
/// slice of coefficients. The commitment will be blinded by the blinding
/// factor `r`.
pub fn commit(&self, poly: &Polynomial<C::Scalar, Coeff>, r: Blind<C::Scalar>) -> C::Curve {
metrics::increment_counter!("multiexp", "size" => format!("{}", poly.len() + 1), "fn" => "commit");
let mut tmp_scalars = Vec::with_capacity(poly.len() + 1);
let mut tmp_bases = Vec::with_capacity(poly.len() + 1);
@@ -139,7 +138,6 @@ impl<C: CurveAffine> Params<C> {
poly: &Polynomial<C::Scalar, LagrangeCoeff>,
r: Blind<C::Scalar>,
) -> C::Curve {
metrics::increment_counter!("multiexp", "size" => format!("{}", poly.len() + 1), "fn" => "commit_lagrange");
let mut tmp_scalars = Vec::with_capacity(poly.len() + 1);
let mut tmp_bases = Vec::with_capacity(poly.len() + 1);

View File

@@ -144,7 +144,6 @@ impl<'a, C: CurveAffine> MSM<'a, C> {
assert_eq!(scalars.len(), len);
metrics::increment_counter!("multiexp", "size" => format!("{}", len), "fn" => "MSM::eval");
bool::from(best_multiexp(&scalars, &bases).is_identity())
}
}

View File

@@ -95,14 +95,12 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>>(
//
// TODO: If we modify multiexp to take "extra" bases, we could speed
// this piece up a bit by combining the multiexps.
metrics::counter!("multiexp", 2, "val" => "l/r", "size" => format!("{}", half));
let l = best_multiexp(&a[half..], &g[0..half]);
let r = best_multiexp(&a[0..half], &g[half..]);
let value_l = compute_inner_product(&a[half..], &b[0..half]);
let value_r = compute_inner_product(&a[0..half], &b[half..]);
let l_randomness = C::Scalar::rand();
let r_randomness = C::Scalar::rand();
metrics::counter!("multiexp", 2, "val" => "l/r", "size" => "2");
let l = l + &best_multiexp(&[value_l * &z, l_randomness], &[params.u, params.h]);
let r = r + &best_multiexp(&[value_r * &z, r_randomness], &[params.u, params.h]);
let l = l.to_affine();
@@ -147,7 +145,6 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>>(
fn parallel_generator_collapse<C: CurveAffine>(g: &mut [C], challenge: C::Scalar) {
let len = g.len() / 2;
let (mut g_lo, g_hi) = g.split_at_mut(len);
metrics::counter!("scalar_multiplication", len as u64, "fn" => "parallel_generator_collapse");
parallelize(&mut g_lo, |g_lo, start| {
let g_hi = &g_hi[start..];

View File

@@ -55,7 +55,6 @@ impl<'a, C: CurveAffine> Guard<'a, C> {
pub fn compute_g(&self) -> C {
let s = compute_s(&self.challenges, C::Scalar::one());
metrics::increment_counter!("multiexp", "size" => format!("{}", s.len()), "fn" => "compute_g");
let mut tmp = best_multiexp(&s, &self.msm.params.g);
tmp += self.msm.params.h;
tmp.to_affine()

View File

@@ -211,7 +211,6 @@ impl<G: Group> EvaluationDomain<G> {
assert_eq!(a.values.len(), 1 << self.k);
// Perform inverse FFT to obtain the polynomial in coefficient form
metrics::increment_counter!("ifft", "size" => format!("{}", a.len()), "fn" => "lagrange_to_coeff");
Self::ifft(&mut a.values, self.omega_inv, self.k, self.ifft_divisor);
Polynomial {
@@ -246,7 +245,6 @@ impl<G: Group> EvaluationDomain<G> {
Self::distribute_powers(&mut a.values, g);
}
a.values.resize(self.extended_len(), G::group_zero());
metrics::increment_counter!("fft", "size" => format!("{}", self.extended_len()), "fn" => "coeff_to_extended");
best_fft(&mut a.values, self.extended_omega, self.extended_k);
Polynomial {
@@ -265,7 +263,6 @@ impl<G: Group> EvaluationDomain<G> {
assert_eq!(a.values.len(), self.extended_len());
// Inverse FFT
metrics::increment_counter!("ifft", "size" => format!("{}", a.len()), "fn" => "extended_to_coeff");
Self::ifft(
&mut a.values,
self.extended_omega_inv,