From 766caf92146bdd88c46976d4465929fad1051b72 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Tue, 10 Nov 2020 00:45:52 +0800 Subject: [PATCH] Make getters for column index() and column_type() --- src/plonk/circuit.rs | 38 ++++++++++++++++++++++++-------------- src/plonk/keygen.rs | 4 ++-- src/plonk/prover.rs | 27 +++++++++++++++------------ src/plonk/verifier.rs | 6 +++--- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 2cbd59d..895cd8a 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -13,8 +13,18 @@ pub trait ColumnType: 'static + Sized {} /// A column with an index and type #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct Column { - pub(crate) index: usize, - pub(crate) column_type: C, + index: usize, + column_type: C, +} + +impl Column { + pub(crate) fn index(&self) -> usize { + self.index + } + + pub(crate) fn column_type(&self) -> &C { + &self.column_type + } } /// An advice column @@ -48,7 +58,7 @@ impl ColumnType for Any {} impl From> for Column { fn from(advice: Column) -> Column { Column { - index: advice.index, + index: advice.index(), column_type: Any::Advice, } } @@ -57,7 +67,7 @@ impl From> for Column { impl From> for Column { fn from(advice: Column) -> Column { Column { - index: advice.index, + index: advice.index(), column_type: Any::Fixed, } } @@ -66,7 +76,7 @@ impl From> for Column { impl From> for Column { fn from(advice: Column) -> Column { Column { - index: advice.index, + index: advice.index(), column_type: Any::Aux, } } @@ -76,10 +86,10 @@ impl TryFrom> for Column { type Error = &'static str; fn try_from(any: Column) -> Result { - match any.column_type { + match any.column_type() { Any::Advice => { return Ok(Column { - index: any.index, + index: any.index(), column_type: Advice, }) } @@ -92,10 +102,10 @@ impl TryFrom> for Column { type Error = &'static str; fn try_from(any: Column) -> Result { - match any.column_type { + match any.column_type() { Any::Fixed => { return Ok(Column { - index: any.index, + index: any.index(), column_type: Fixed, }) } @@ -108,10 +118,10 @@ impl TryFrom> for Column { type Error = &'static str; fn try_from(any: Column) -> Result { - match any.column_type { + match any.column_type() { Any::Aux => { return Ok(Column { - index: any.index, + index: any.index(), column_type: Aux, }) } @@ -427,7 +437,7 @@ impl ConstraintSystem { } fn query_any_index(&mut self, column: Column, at: i32) -> usize { - let index = match column.column_type { + let index = match column.column_type() { Any::Advice => self.query_advice_index(Column::::try_from(column).unwrap(), at), Any::Fixed => self.query_fixed_index(Column::::try_from(column).unwrap(), at), Any::Aux => self.query_aux_index(Column::::try_from(column).unwrap(), at), @@ -438,7 +448,7 @@ impl ConstraintSystem { /// Query an Any column at a relative position pub fn query_any(&mut self, column: Column, at: i32) -> Expression { - match column.column_type { + match column.column_type() { Any::Advice => Expression::Advice( self.query_advice_index(Column::::try_from(column).unwrap(), at), ), @@ -485,7 +495,7 @@ impl ConstraintSystem { } pub(crate) fn get_any_query_index(&self, column: Column, at: i32) -> usize { - let index = match column.column_type { + let index = match column.column_type() { Any::Advice => { self.get_advice_query_index(Column::::try_from(column).unwrap(), at) } diff --git a/src/plonk/keygen.rs b/src/plonk/keygen.rs index 13f2915..7624073 100644 --- a/src/plonk/keygen.rs +++ b/src/plonk/keygen.rs @@ -44,7 +44,7 @@ where ) -> Result<(), Error> { *self .fixed - .get_mut(column.index) + .get_mut(column.index()) .and_then(|v| v.get_mut(row)) .ok_or(Error::BoundsFailure)? = to()?; @@ -230,7 +230,7 @@ where .fixed_queries .iter() .map(|&(column, at)| { - let poly = fixed_polys[column.index].clone(); + let poly = fixed_polys[column.index()].clone(); domain.coeff_to_extended(poly, at) }) .collect(); diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 507235a..d13dd7a 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -45,7 +45,7 @@ impl Proof { ) -> Result<(), Error> { *self .advice - .get_mut(column.index) + .get_mut(column.index()) .and_then(|v| v.get_mut(row)) .ok_or(Error::BoundsFailure)? = to()?; @@ -120,7 +120,7 @@ impl Proof { .aux_queries .iter() .map(|&(column, at)| { - let poly = aux_polys[column.index].clone(); + let poly = aux_polys[column.index()].clone(); domain.coeff_to_extended(poly, at) }) .collect(); @@ -157,7 +157,7 @@ impl Proof { .advice_queries .iter() .map(|&(column, at)| { - let poly = advice_polys[column.index].clone(); + let poly = advice_polys[column.index()].clone(); domain.coeff_to_extended(poly, at) }) .collect(); @@ -192,7 +192,7 @@ impl Proof { parallelize(&mut modified_advice, |modified_advice, start| { for ((modified_advice, advice_value), permuted_advice_value) in modified_advice .iter_mut() - .zip(witness.advice[column.index][start..].iter()) + .zip(witness.advice[column.index()][start..].iter()) .zip(permuted_column_values[start..].iter()) { *modified_advice *= &(x_0 * permuted_advice_value + &x_1 + advice_value); @@ -226,7 +226,7 @@ impl Proof { let mut deltaomega = deltaomega * &omega.pow_vartime(&[start as u64, 0, 0, 0]); for (modified_advice, advice_value) in modified_advice .iter_mut() - .zip(witness.advice[column.index][start..].iter()) + .zip(witness.advice[column.index()][start..].iter()) { // Multiply by p_j(\omega^i) + \delta^j \omega^i \beta *modified_advice *= &(deltaomega * &x_0 + &x_1 + advice_value); @@ -395,7 +395,7 @@ impl Proof { .advice_queries .iter() .map(|&(column, at)| { - eval_polynomial(&advice_polys[column.index], domain.rotate_omega(x_3, at)) + eval_polynomial(&advice_polys[column.index()], domain.rotate_omega(x_3, at)) }) .collect(); @@ -403,7 +403,7 @@ impl Proof { .aux_queries .iter() .map(|&(column, at)| { - eval_polynomial(&aux_polys[column.index], domain.rotate_omega(x_3, at)) + eval_polynomial(&aux_polys[column.index()], domain.rotate_omega(x_3, at)) }) .collect(); @@ -411,7 +411,10 @@ impl Proof { .fixed_queries .iter() .map(|&(column, at)| { - eval_polynomial(&pk.fixed_polys[column.index], domain.rotate_omega(x_3, at)) + eval_polynomial( + &pk.fixed_polys[column.index()], + domain.rotate_omega(x_3, at), + ) }) .collect(); @@ -469,8 +472,8 @@ impl Proof { instances.push(ProverQuery { point, - poly: &advice_polys[column.index], - blind: advice_blinds[column.index], + poly: &advice_polys[column.index()], + blind: advice_blinds[column.index()], eval: advice_evals[query_index], }); } @@ -480,7 +483,7 @@ impl Proof { instances.push(ProverQuery { point, - poly: &aux_polys[column.index], + poly: &aux_polys[column.index()], blind: Blind::default(), eval: aux_evals[query_index], }); @@ -491,7 +494,7 @@ impl Proof { instances.push(ProverQuery { point, - poly: &pk.fixed_polys[column.index], + poly: &pk.fixed_polys[column.index()], blind: Blind::default(), eval: fixed_evals[query_index], }); diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 5ff8745..0ea391e 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -93,7 +93,7 @@ impl<'a, C: CurveAffine> Proof { let point = vk.domain.rotate_omega(x_3, at); queries.push(VerifierQuery { point, - commitment: &self.advice_commitments[column.index], + commitment: &self.advice_commitments[column.index()], eval: self.advice_evals[query_index], }); } @@ -102,7 +102,7 @@ impl<'a, C: CurveAffine> Proof { let point = vk.domain.rotate_omega(x_3, at); queries.push(VerifierQuery { point, - commitment: &aux_commitments[column.index], + commitment: &aux_commitments[column.index()], eval: self.aux_evals[query_index], }); } @@ -111,7 +111,7 @@ impl<'a, C: CurveAffine> Proof { let point = vk.domain.rotate_omega(x_3, at); queries.push(VerifierQuery { point, - commitment: &vk.fixed_commitments[column.index], + commitment: &vk.fixed_commitments[column.index()], eval: self.fixed_evals[query_index], }); }