From 4bf46fc34908169e6bd6045cfaee10009ce9e084 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Sat, 13 Feb 2021 18:36:29 +0800 Subject: [PATCH] Add Expression::Const variant --- src/dev.rs | 2 ++ src/plonk/circuit.rs | 10 ++++++++++ src/plonk/lookup/prover.rs | 2 ++ src/plonk/lookup/verifier.rs | 1 + src/plonk/prover.rs | 1 + src/plonk/verifier.rs | 1 + src/poly/domain.rs | 17 +++++++++++++++++ 7 files changed, 34 insertions(+) diff --git a/src/dev.rs b/src/dev.rs index 1020c37..7bdbcee 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -296,6 +296,7 @@ impl MockProver { } if gate.evaluate( + &|scalar| scalar, &load(n, row, &self.cs.fixed_queries, &self.fixed), &load(n, row, &self.cs.advice_queries, &self.advice), &load(n, row, &self.cs.instance_queries, &self.instance), @@ -318,6 +319,7 @@ impl MockProver { for input_row in 0..n { let load = |expression: &Expression, row| { expression.evaluate( + &|scalar| scalar, &|index| { let column_index = self.cs.fixed_queries[index].0.index(); self.fixed[column_index][row as usize] diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 4b98494..dc84cbb 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -224,6 +224,8 @@ pub trait Circuit { /// Low-degree expression representing an identity that must hold over the committed columns. #[derive(Clone, Debug)] pub enum Expression { + /// This is a constant polynomial + Const(F), /// This is a fixed column queried at a certain relative location Fixed(usize), /// This is an advice (witness) column queried at a certain relative location @@ -243,6 +245,7 @@ impl Expression { /// operations. pub fn evaluate( &self, + const_column: &impl Fn(F) -> T, fixed_column: &impl Fn(usize) -> T, advice_column: &impl Fn(usize) -> T, instance_column: &impl Fn(usize) -> T, @@ -251,11 +254,13 @@ impl Expression { scaled: &impl Fn(T, F) -> T, ) -> T { match self { + Expression::Const(scalar) => const_column(*scalar), Expression::Fixed(index) => fixed_column(*index), Expression::Advice(index) => advice_column(*index), Expression::Instance(index) => instance_column(*index), Expression::Sum(a, b) => { let a = a.evaluate( + const_column, fixed_column, advice_column, instance_column, @@ -264,6 +269,7 @@ impl Expression { scaled, ); let b = b.evaluate( + const_column, fixed_column, advice_column, instance_column, @@ -275,6 +281,7 @@ impl Expression { } Expression::Product(a, b) => { let a = a.evaluate( + const_column, fixed_column, advice_column, instance_column, @@ -283,6 +290,7 @@ impl Expression { scaled, ); let b = b.evaluate( + const_column, fixed_column, advice_column, instance_column, @@ -294,6 +302,7 @@ impl Expression { } Expression::Scaled(a, f) => { let a = a.evaluate( + const_column, fixed_column, advice_column, instance_column, @@ -309,6 +318,7 @@ impl Expression { /// Compute the degree of this polynomial pub fn degree(&self) -> usize { match self { + Expression::Const(_) => 0, Expression::Fixed(_) => 1, Expression::Advice(_) => 1, Expression::Instance(_) => 1, diff --git a/src/plonk/lookup/prover.rs b/src/plonk/lookup/prover.rs index 7b559c0..a574a17 100644 --- a/src/plonk/lookup/prover.rs +++ b/src/plonk/lookup/prover.rs @@ -96,6 +96,7 @@ impl Argument { .iter() .map(|expression| { expression.evaluate( + &|scalar| pk.vk.domain.const_lagrange(scalar), &|index| { let query = pk.vk.cs.fixed_queries[index]; let column_index = query.0.index(); @@ -137,6 +138,7 @@ impl Argument { .iter() .map(|expression| { expression.evaluate( + &|scalar| pk.vk.domain.const_extended(scalar), &|index| fixed_cosets[index].clone(), &|index| advice_cosets[index].clone(), &|index| instance_cosets[index].clone(), diff --git a/src/plonk/lookup/verifier.rs b/src/plonk/lookup/verifier.rs index 9129b8a..e0c779b 100644 --- a/src/plonk/lookup/verifier.rs +++ b/src/plonk/lookup/verifier.rs @@ -120,6 +120,7 @@ impl Evaluated { .iter() .map(|expression| { expression.evaluate( + &|scalar| scalar, &|index| fixed_evals[index], &|index| advice_evals[index], &|index| instance_evals[index], diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index ed43f5b..a4597f7 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -373,6 +373,7 @@ pub fn create_proof, ConcreteCircuit: Circ // Custom constraints .chain(meta.gates.iter().map(move |(_, poly)| { poly.evaluate( + &|scalar| pk.vk.domain.const_extended(scalar), &|index| pk.fixed_cosets[index].clone(), &|index| advice.advice_cosets[index].clone(), &|index| instance.instance_cosets[index].clone(), diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index c854901..38f7485 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -168,6 +168,7 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( // Evaluate the circuit using the custom gates provided .chain(vk.cs.gates.iter().map(move |(_, poly)| { poly.evaluate( + &|scalar| scalar, &|index| fixed_evals[index], &|index| advice_evals[index], &|index| instance_evals[index], diff --git a/src/poly/domain.rs b/src/poly/domain.rs index c6bd2ce..2976ac3 100644 --- a/src/poly/domain.rs +++ b/src/poly/domain.rs @@ -177,6 +177,14 @@ impl EvaluationDomain { } } + /// Returns a constant polynomial in the Lagrange coefficient basis + pub fn const_lagrange(&self, scalar: G) -> Polynomial { + Polynomial { + values: vec![scalar; self.n as usize], + _marker: PhantomData, + } + } + /// Returns an empty (zero) polynomial in the extended Lagrange coefficient /// basis pub fn empty_extended(&self) -> Polynomial { @@ -186,6 +194,15 @@ impl EvaluationDomain { } } + /// Returns a constant polynomial in the extended Lagrange coefficient + /// basis + pub fn const_extended(&self, scalar: G) -> Polynomial { + Polynomial { + values: vec![scalar; self.extended_len()], + _marker: PhantomData, + } + } + /// This takes us from an n-length vector into the coefficient form. /// /// This function will panic if the provided vector is not the correct