Skip to content

Commit

Permalink
feat: serialize/deserialize verifying key
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 committed Jan 2, 2024
1 parent be95568 commit 50095e0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 27 deletions.
3 changes: 2 additions & 1 deletion halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ backtrace = { version = "0.3", optional = true }
rayon = "1.5.1"
ff = "0.13"
group = "0.13"
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2" }
halo2curves = { git = 'https://github.com/privacy-scaling-explorations/halo2curves', tag = "0.3.2", features=["derive_serde"]}
rand_core = { version = "0.6", default-features = false }
tracing = "0.1"
blake2b_simd = "1"
Expand All @@ -62,6 +62,7 @@ rand_chacha = "0.3"
# Developer tooling dependencies
plotters = { version = "0.3.0", optional = true }
tabbycat = { version = "0.1", features = ["attributes"], optional = true }
serde = "1"

[dev-dependencies]
assert_matches = "1.5"
Expand Down
4 changes: 3 additions & 1 deletion halo2_proofs/src/dev/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! Metadata about circuits.
use serde::{Serialize, Deserialize};

use super::metadata::Column as ColumnMetadata;
use crate::plonk::{self, Any};
use std::{
collections::HashMap,
fmt::{self, Debug},
};
/// Metadata about a column within a circuit.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Column {
/// The type of the column.
pub(super) column_type: Any,
Expand Down
15 changes: 14 additions & 1 deletion halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use crate::helpers::{
polynomial_slice_byte_length, read_polynomial_vec, write_polynomial_slice, SerdeCurveAffine,
SerdePrimeField,
};
use crate::poly::Rotation;
use crate::poly::{
commitment::Params, Coeff, EvaluationDomain, ExtendedLagrangeCoeff, LagrangeCoeff,
PinnedEvaluationDomain, Polynomial,
};
use crate::transcript::{ChallengeScalar, EncodedChallenge, Transcript};
use crate::SerdeFormat;
use serde::{Deserialize, Serialize};

mod assigned;
mod circuit;
Expand All @@ -44,7 +46,7 @@ use std::io;

/// This is a verifying key which allows for the verification of proofs for a
/// particular circuit.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VerifyingKey<C: CurveAffine> {
domain: EvaluationDomain<C::Scalar>,
fixed_commitments: Vec<C>,
Expand Down Expand Up @@ -164,6 +166,17 @@ where
params,
)
}

/// Remove debug info from the verifying key.
pub fn remove_debug_info(&mut self) {
self.cs.general_column_annotations.clear();
for gate in self.cs.gates.iter_mut() {
gate.name.clear();
gate.constraint_names.clear();
gate.queried_cells.clear();
gate.queried_selectors.clear();
}
}
}

impl<C: CurveAffine> VerifyingKey<C>
Expand Down
41 changes: 22 additions & 19 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::cmp::max;
use core::ops::{Add, Mul};
use ff::Field;
use sealed::SealedPhase;
use serde::{Serialize, Deserialize};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
Expand All @@ -27,7 +28,7 @@ pub trait ColumnType:
}

/// A column with an index and type
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Column<C: ColumnType> {
index: usize,
column_type: C,
Expand Down Expand Up @@ -94,8 +95,10 @@ impl<C: ColumnType> PartialOrd for Column<C> {
}

pub(crate) mod sealed {
use serde::{Serialize, Deserialize};

/// Phase of advice column
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct Phase(pub(super) u8);

impl Phase {
Expand Down Expand Up @@ -152,7 +155,7 @@ impl SealedPhase for super::ThirdPhase {
}

/// An advice column
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Advice {
pub(crate) phase: sealed::Phase,
}
Expand Down Expand Up @@ -191,15 +194,15 @@ impl std::fmt::Debug for Advice {
}

/// A fixed column
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Fixed;

/// An instance column
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Instance;

/// An enum over the Advice, Fixed, Instance structs
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum Any {
/// An Advice variant
Advice(Advice),
Expand Down Expand Up @@ -451,7 +454,7 @@ impl TryFrom<Column<Any>> for Column<Instance> {
/// Ok(())
/// }
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Selector(pub(crate) usize, bool);

impl Selector {
Expand All @@ -473,7 +476,7 @@ impl Selector {
}

/// Query of fixed column at a certain relative location
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct FixedQuery {
/// Query index
pub(crate) index: Option<usize>,
Expand All @@ -496,7 +499,7 @@ impl FixedQuery {
}

/// Query of advice column at a certain relative location
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct AdviceQuery {
/// Query index
pub(crate) index: Option<usize>,
Expand Down Expand Up @@ -526,7 +529,7 @@ impl AdviceQuery {
}

/// Query of instance column at a certain relative location
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct InstanceQuery {
/// Query index
pub(crate) index: Option<usize>,
Expand Down Expand Up @@ -577,7 +580,7 @@ impl TableColumn {
}

/// A challenge squeezed from transcript after advice columns at the phase have been committed.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Challenge {
index: usize,
pub(crate) phase: sealed::Phase,
Expand Down Expand Up @@ -786,7 +789,7 @@ pub trait Circuit<F: Field> {
}

/// Low-degree expression representing an identity that must hold over the committed columns.
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub enum Expression<F> {
/// This is a constant polynomial
Constant(F),
Expand Down Expand Up @@ -1351,7 +1354,7 @@ pub(crate) struct PointIndex(pub usize);

/// A "virtual cell" is a PLONK cell that has been queried at a particular relative offset
/// within a custom gate.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VirtualCell {
pub(crate) column: Column<Any>,
pub(crate) rotation: Rotation,
Expand Down Expand Up @@ -1486,15 +1489,15 @@ impl<F: Field, C: Into<Constraint<F>>, Iter: IntoIterator<Item = C>> IntoIterato
}

/// Gate
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Gate<F: Field> {
name: String,
constraint_names: Vec<String>,
pub(crate) name: String,
pub(crate) constraint_names: Vec<String>,
polys: Vec<Expression<F>>,
/// We track queried selectors separately from other cells, so that we can use them to
/// trigger debug checks on gates.
queried_selectors: Vec<Selector>,
queried_cells: Vec<VirtualCell>,
pub(crate) queried_selectors: Vec<Selector>,
pub(crate) queried_cells: Vec<VirtualCell>,
}

impl<F: Field> Gate<F> {
Expand Down Expand Up @@ -1522,7 +1525,7 @@ impl<F: Field> Gate<F> {

/// This is a description of the circuit environment, such as the gate, column and
/// permutation arrangements.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstraintSystem<F: Field> {
pub(crate) num_fixed_columns: usize,
pub(crate) num_advice_columns: usize,
Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/plonk/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::circuit::Expression;
use ff::Field;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug};

pub(crate) mod prover;
pub(crate) mod verifier;

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct Argument<F: Field> {
pub(crate) name: String,
pub(crate) input_expressions: Vec<Expression<F>>,
Expand Down
5 changes: 3 additions & 2 deletions halo2_proofs/src/plonk/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ pub(crate) mod prover;
pub(crate) mod verifier;

pub use keygen::Assembly;
use serde::{Serialize, Deserialize};

use std::io;

/// A permutation argument.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Argument {
/// A sequence of columns involved in the argument.
pub(super) columns: Vec<Column<Any>>,
Expand Down Expand Up @@ -83,7 +84,7 @@ impl Argument {
}

/// The verifying key for a single permutation argument.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VerifyingKey<C: CurveAffine> {
commitments: Vec<C>,
}
Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::SerdeFormat;

use ff::PrimeField;
use group::ff::{BatchInvert, Field};
use serde::{Serialize, Deserialize};
use std::fmt::Debug;
use std::io;
use std::marker::PhantomData;
Expand Down Expand Up @@ -304,7 +305,7 @@ impl<'a, F: Field, B: Basis> Sub<F> for &'a Polynomial<F, B> {
/// Describes the relative rotation of a vector. Negative numbers represent
/// reverse (leftmost) rotations and positive numbers represent forward (rightmost)
/// rotations. Zero represents no rotation.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Rotation(pub i32);

impl Rotation {
Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/poly/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use group::{
ff::{BatchInvert, Field, PrimeField},
Group,
};
use serde::{Serialize, Deserialize};

use std::marker::PhantomData;

/// This structure contains precomputed constants and other details needed for
/// performing operations on an evaluation domain of size $2^k$ and an extended
/// domain of size $2^{k} * j$ with $j \neq 0$.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EvaluationDomain<F: Field> {
n: u64,
k: u32,
Expand Down

0 comments on commit 50095e0

Please sign in to comment.