From ac9839efefaab7d529af46739b6e48612ccd8521 Mon Sep 17 00:00:00 2001 From: han0110 Date: Fri, 10 Jun 2022 16:32:09 +0800 Subject: [PATCH] feat: expose necessary structure of ConstraintSystem --- halo2_proofs/src/plonk.rs | 15 +++++ halo2_proofs/src/plonk/circuit.rs | 92 +++++++++++++++++++++++++-- halo2_proofs/src/plonk/lookup.rs | 18 ++++-- halo2_proofs/src/plonk/permutation.rs | 13 +++- 4 files changed, 127 insertions(+), 11 deletions(-) diff --git a/halo2_proofs/src/plonk.rs b/halo2_proofs/src/plonk.rs index 9be2fdbc79..f9a6587af6 100644 --- a/halo2_proofs/src/plonk.rs +++ b/halo2_proofs/src/plonk.rs @@ -110,6 +110,21 @@ impl VerifyingKey { cs: self.cs.pinned(), } } + + /// Returns commitments of fixed polynomials + pub fn fixed_commitments(&self) -> &Vec { + &self.fixed_commitments + } + + /// Returns `VerifyingKey` of permutation + pub fn permutation(&self) -> &permutation::VerifyingKey { + &self.permutation + } + + /// Returns `ConstraintSystem` + pub fn cs(&self) -> &ConstraintSystem { + &self.cs + } } /// Minimal representation of a verification key that can be used to identify diff --git a/halo2_proofs/src/plonk/circuit.rs b/halo2_proofs/src/plonk/circuit.rs index a0dce53110..460bdb5db6 100644 --- a/halo2_proofs/src/plonk/circuit.rs +++ b/halo2_proofs/src/plonk/circuit.rs @@ -33,7 +33,8 @@ impl Column { Column { index, column_type } } - pub(crate) fn index(&self) -> usize { + /// Index of this column. + pub fn index(&self) -> usize { self.index } @@ -280,6 +281,18 @@ pub struct FixedQuery { pub(crate) rotation: Rotation, } +impl FixedQuery { + /// Column index + pub fn column_index(&self) -> usize { + self.column_index + } + + /// Rotation of this query + pub fn rotation(&self) -> Rotation { + self.rotation + } +} + /// Query of advice column at a certain relative location #[derive(Copy, Clone, Debug)] pub struct AdviceQuery { @@ -291,6 +304,18 @@ pub struct AdviceQuery { pub(crate) rotation: Rotation, } +impl AdviceQuery { + /// Column index + pub fn column_index(&self) -> usize { + self.column_index + } + + /// Rotation of this query + pub fn rotation(&self) -> Rotation { + self.rotation + } +} + /// Query of instance column at a certain relative location #[derive(Copy, Clone, Debug)] pub struct InstanceQuery { @@ -302,6 +327,18 @@ pub struct InstanceQuery { pub(crate) rotation: Rotation, } +impl InstanceQuery { + /// Column index + pub fn column_index(&self) -> usize { + self.column_index + } + + /// Rotation of this query + pub fn rotation(&self) -> Rotation { + self.rotation + } +} + /// A fixed column of a lookup table. /// /// A lookup table can be loaded into this column via [`Layouter::assign_table`]. Columns @@ -930,7 +967,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)] -pub(crate) struct VirtualCell { +pub struct VirtualCell { pub(crate) column: Column, pub(crate) rotation: Rotation, } @@ -1054,8 +1091,9 @@ impl>, Iter: IntoIterator> IntoIterato } } +/// Gate #[derive(Clone, Debug)] -pub(crate) struct Gate { +pub struct Gate { name: &'static str, constraint_names: Vec<&'static str>, polys: Vec>, @@ -1074,7 +1112,8 @@ impl Gate { self.constraint_names[constraint_index] } - pub(crate) fn polynomials(&self) -> &[Expression] { + /// Returns constraints of this gate + pub fn polynomials(&self) -> &[Expression] { &self.polys } @@ -1648,6 +1687,51 @@ impl ConstraintSystem { // and the interstitial values.) + 1 // for at least one row } + + /// Returns number of fixed columns + pub fn num_fixed_columns(&self) -> usize { + self.num_fixed_columns + } + + /// Returns number of advice columns + pub fn num_advice_columns(&self) -> usize { + self.num_advice_columns + } + + /// Returns number of instance columns + pub fn num_instance_columns(&self) -> usize { + self.num_instance_columns + } + + /// Returns gates + pub fn gates(&self) -> &Vec> { + &self.gates + } + + /// Returns advice queries + pub fn advice_queries(&self) -> &Vec<(Column, Rotation)> { + &self.advice_queries + } + + /// Returns instance queries + pub fn instance_queries(&self) -> &Vec<(Column, Rotation)> { + &self.instance_queries + } + + /// Returns fixed queries + pub fn fixed_queries(&self) -> &Vec<(Column, Rotation)> { + &self.fixed_queries + } + + /// Returns permutation argument + pub fn permutation(&self) -> &permutation::Argument { + &self.permutation + } + + /// Returns lookup arguments + pub fn lookups(&self) -> &Vec> { + &self.lookups + } } /// Exposes the "virtual cells" that can be queried while creating a custom gate or lookup diff --git a/halo2_proofs/src/plonk/lookup.rs b/halo2_proofs/src/plonk/lookup.rs index 8b4e8258d3..68cda75d37 100644 --- a/halo2_proofs/src/plonk/lookup.rs +++ b/halo2_proofs/src/plonk/lookup.rs @@ -6,10 +6,10 @@ pub(crate) mod prover; pub(crate) mod verifier; #[derive(Clone)] -pub(crate) struct Argument { - pub name: &'static str, - pub input_expressions: Vec>, - pub table_expressions: Vec>, +pub struct Argument { + pub(crate) name: &'static str, + pub(crate) input_expressions: Vec>, + pub(crate) table_expressions: Vec>, } impl Debug for Argument { @@ -81,4 +81,14 @@ impl Argument { 2 + input_degree + table_degree, ) } + + /// Returns input of this argument + pub fn input_expressions(&self) -> &Vec> { + &self.input_expressions + } + + /// Returns table of this argument + pub fn table_expressions(&self) -> &Vec> { + &self.table_expressions + } } diff --git a/halo2_proofs/src/plonk/permutation.rs b/halo2_proofs/src/plonk/permutation.rs index 23ade0d3aa..26a4d805d6 100644 --- a/halo2_proofs/src/plonk/permutation.rs +++ b/halo2_proofs/src/plonk/permutation.rs @@ -13,7 +13,7 @@ use std::io; /// A permutation argument. #[derive(Debug, Clone)] -pub(crate) struct Argument { +pub struct Argument { /// A sequence of columns involved in the argument. pub(super) columns: Vec>, } @@ -67,17 +67,24 @@ impl Argument { } } - pub(crate) fn get_columns(&self) -> Vec> { + pub fn get_columns(&self) -> Vec> { self.columns.clone() } } /// The verifying key for a single permutation argument. #[derive(Clone, Debug)] -pub(crate) struct VerifyingKey { +pub struct VerifyingKey { commitments: Vec, } +impl VerifyingKey { + /// Returns commitments of sigma polynomials + pub fn commitments(&self) -> &Vec { + &self.commitments + } +} + /// The proving key for a single permutation argument. #[derive(Clone, Debug)] pub(crate) struct ProvingKey {