From 83ce7808e8d6c6e4c721a2669de57e888e00a2f6 Mon Sep 17 00:00:00 2001 From: chokermaxx <135603985+chokermaxx@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:53:07 +0900 Subject: [PATCH 1/3] Make it Eq to make it easier for tests --- halo2_proofs/src/plonk/circuit.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/halo2_proofs/src/plonk/circuit.rs b/halo2_proofs/src/plonk/circuit.rs index a5736f28ca..d52cfd0127 100644 --- a/halo2_proofs/src/plonk/circuit.rs +++ b/halo2_proofs/src/plonk/circuit.rs @@ -478,7 +478,7 @@ impl Selector { } /// Query of fixed column at a certain relative location -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct FixedQuery { /// Query index pub(crate) index: Option, @@ -501,7 +501,7 @@ impl FixedQuery { } /// Query of advice column at a certain relative location -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct AdviceQuery { /// Query index pub(crate) index: Option, @@ -531,7 +531,7 @@ impl AdviceQuery { } /// Query of instance column at a certain relative location -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct InstanceQuery { /// Query index pub(crate) index: Option, @@ -792,7 +792,7 @@ pub trait Circuit { } /// Low-degree expression representing an identity that must hold over the committed columns. -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq)] pub enum Expression { /// This is a constant polynomial Constant(F), From 98f33edcdf1cab6b60fdd457d005a93eb94517da Mon Sep 17 00:00:00 2001 From: chokermaxx <135603985+chokermaxx@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:53:24 +0900 Subject: [PATCH 2/3] Implement Sum and Product for Expression --- halo2_proofs/src/plonk/circuit.rs | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/halo2_proofs/src/plonk/circuit.rs b/halo2_proofs/src/plonk/circuit.rs index d52cfd0127..b42146a130 100644 --- a/halo2_proofs/src/plonk/circuit.rs +++ b/halo2_proofs/src/plonk/circuit.rs @@ -11,6 +11,7 @@ use ff::Field; use sealed::SealedPhase; use std::collections::HashMap; use std::fmt::Debug; +use std::iter::{Product, Sum}; use std::{ convert::TryFrom, ops::{Neg, Sub}, @@ -1352,6 +1353,34 @@ impl Mul for Expression { } } +impl Sum for Expression { + fn sum>(mut iter: I) -> Self { + if let Some(mut result) = iter.next() { + for term in iter { + result = result + term; + } + + result + } else { + Self::Constant(F::ZERO) + } + } +} + +impl Product for Expression { + fn product>(mut iter: I) -> Self { + if let Some(mut result) = iter.next() { + for term in iter { + result = result * term; + } + + result + } else { + Self::Constant(F::ONE) + } + } +} + /// Represents an index into a vector where each entry corresponds to a distinct /// point that polynomials are queried at. #[derive(Copy, Clone, Debug)] @@ -2439,3 +2468,47 @@ impl<'a, F: Field> VirtualCells<'a, F> { Expression::Challenge(challenge) } } + +#[cfg(test)] +mod tests { + use super::Expression; + use halo2curves::bn256::Fr; + + #[test] + fn iter_sum() { + let exprs: Vec> = vec![ + Expression::Constant(1.into()), + Expression::Constant(2.into()), + Expression::Constant(3.into()), + ]; + let happened: Expression = exprs.into_iter().sum(); + let expected: Expression = Expression::Sum( + Box::new(Expression::Sum( + Box::new(Expression::Constant(1.into())), + Box::new(Expression::Constant(2.into())), + )), + Box::new(Expression::Constant(3.into())), + ); + + assert_eq!(happened, expected); + } + + #[test] + fn iter_product() { + let exprs: Vec> = vec![ + Expression::Constant(1.into()), + Expression::Constant(2.into()), + Expression::Constant(3.into()), + ]; + let happened: Expression = exprs.into_iter().product(); + let expected: Expression = Expression::Product( + Box::new(Expression::Product( + Box::new(Expression::Constant(1.into())), + Box::new(Expression::Constant(2.into())), + )), + Box::new(Expression::Constant(3.into())), + ); + + assert_eq!(happened, expected); + } +} From 4bf886da9e74481e4f5927acc2ee74728e430e5c Mon Sep 17 00:00:00 2001 From: chokermaxx <135603985+chokermaxx@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:28:38 +0900 Subject: [PATCH 3/3] Make it readable --- halo2_proofs/src/plonk/circuit.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/halo2_proofs/src/plonk/circuit.rs b/halo2_proofs/src/plonk/circuit.rs index b42146a130..713d1106aa 100644 --- a/halo2_proofs/src/plonk/circuit.rs +++ b/halo2_proofs/src/plonk/circuit.rs @@ -1354,30 +1354,16 @@ impl Mul for Expression { } impl Sum for Expression { - fn sum>(mut iter: I) -> Self { - if let Some(mut result) = iter.next() { - for term in iter { - result = result + term; - } - - result - } else { - Self::Constant(F::ZERO) - } + fn sum>(iter: I) -> Self { + iter.reduce(|acc, x| acc + x) + .unwrap_or(Expression::Constant(F::ZERO)) } } impl Product for Expression { - fn product>(mut iter: I) -> Self { - if let Some(mut result) = iter.next() { - for term in iter { - result = result * term; - } - - result - } else { - Self::Constant(F::ONE) - } + fn product>(iter: I) -> Self { + iter.reduce(|acc, x| acc * x) + .unwrap_or(Expression::Constant(F::ONE)) } }