From db0477a606ae460d7e72cb320d85f1188e8584fd Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 29 Jan 2021 00:45:48 +0000 Subject: [PATCH 1/4] impl {Neg, Sub} for Expression --- src/plonk/circuit.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 21879cb..3e74d7d 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -1,10 +1,13 @@ use core::cmp::max; use core::ops::{Add, Mul}; use ff::Field; -use std::convert::TryFrom; +use std::{ + convert::TryFrom, + ops::{Neg, Sub}, +}; use super::{lookup, permutation, Error}; -use crate::poly::Rotation; +use crate::{arithmetic::FieldExt, poly::Rotation}; /// A column type pub trait ColumnType: 'static + Sized {} @@ -316,6 +319,13 @@ impl Expression { } } +impl Neg for Expression { + type Output = Expression; + fn neg(self) -> Self::Output { + Expression::Scaled(Box::new(self), -F::one()) + } +} + impl Add for Expression { type Output = Expression; fn add(self, rhs: Expression) -> Expression { @@ -323,6 +333,13 @@ impl Add for Expression { } } +impl Sub for Expression { + type Output = Expression; + fn sub(self, rhs: Expression) -> Expression { + Expression::Sum(Box::new(self), Box::new(-rhs)) + } +} + impl Mul for Expression { type Output = Expression; fn mul(self, rhs: Expression) -> Expression { From 3fc245343e3dd3eba7572e18213312669dd12101 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 29 Jan 2021 00:52:55 +0000 Subject: [PATCH 2/4] Return results from assigned regions This makes it easier to pass variables out of a region. --- src/circuit.rs | 8 ++++---- src/circuit/layouter.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index 562246f..e9ad299 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -154,9 +154,9 @@ pub trait Layouter { /// region.assign_advice(self.config.a, offset, || { Some(value)}); /// }); /// ``` - fn assign_region(&mut self, name: N, assignment: A) -> Result<(), Error> + fn assign_region(&mut self, name: N, assignment: A) -> Result where - A: FnMut(Region<'_, C>) -> Result<(), Error>, + A: FnMut(Region<'_, C>) -> Result, N: Fn() -> NR, NR: Into; @@ -202,9 +202,9 @@ impl<'a, C: Chip, L: Layouter + 'a> Layouter for NamespacedLayouter<'a, C, self.0.config() } - fn assign_region(&mut self, name: N, assignment: A) -> Result<(), Error> + fn assign_region(&mut self, name: N, assignment: A) -> Result where - A: FnMut(Region<'_, C>) -> Result<(), Error>, + A: FnMut(Region<'_, C>) -> Result, N: Fn() -> NR, NR: Into, { diff --git a/src/circuit/layouter.rs b/src/circuit/layouter.rs index 6f1a348..88ad9cc 100644 --- a/src/circuit/layouter.rs +++ b/src/circuit/layouter.rs @@ -106,9 +106,9 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, &self.config } - fn assign_region(&mut self, name: N, mut assignment: A) -> Result<(), Error> + fn assign_region(&mut self, name: N, mut assignment: A) -> Result where - A: FnMut(Region<'_, C>) -> Result<(), Error>, + A: FnMut(Region<'_, C>) -> Result, N: Fn() -> NR, NR: Into, { @@ -136,13 +136,13 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, self.cs.enter_region(name); let mut region = SingleChipRegion::new(self, region_index); - { + let result = { let region: &mut dyn RegionLayouter = &mut region; - assignment(region.into())?; - } + assignment(region.into()) + }?; self.cs.exit_region(); - Ok(()) + Ok(result) } fn get_root(&mut self) -> &mut Self::Root { From 590523017487a87f9a1f5695a8f03ff68c2e11dc Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 2 Feb 2021 13:15:36 +0000 Subject: [PATCH 3/4] Fix stable clippy lints --- examples/circuit-layout.rs | 3 ++- src/dev/graph/layout.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/circuit-layout.rs b/examples/circuit-layout.rs index 6020686..93ad054 100644 --- a/examples/circuit-layout.rs +++ b/examples/circuit-layout.rs @@ -8,6 +8,7 @@ use halo2::{ use plotters::prelude::*; use std::marker::PhantomData; +#[allow(clippy::many_single_char_names)] fn main() { /// This represents an advice column at a certain row in the ConstraintSystem #[derive(Copy, Clone, Debug)] @@ -372,7 +373,7 @@ fn main() { } let a = Fp::rand(); - let a_squared = a * &a; + let a_squared = a * a; let aux = Fp::one() + Fp::one(); let lookup_table = vec![aux, a, a, Fp::zero()]; let lookup_table_2 = vec![Fp::zero(), a, a_squared, Fp::zero()]; diff --git a/src/dev/graph/layout.rs b/src/dev/graph/layout.rs index 7131680..78c8657 100644 --- a/src/dev/graph/layout.rs +++ b/src/dev/graph/layout.rs @@ -107,7 +107,7 @@ pub fn circuit_layout, DB: DrawingBackend> if let Some(offset) = region.offset { // Sort the region's columns according to the defined ordering. let mut columns: Vec<_> = region.columns.into_iter().collect(); - columns.sort_unstable_by(|a, b| column_index(a).cmp(&column_index(b))); + columns.sort_unstable_by_key(|a| column_index(a)); // Render contiguous parts of the same region as a single box. let mut width = None; From 821bca0abe0f0d7d88155e4da91dbb1e48d93ed7 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 12 Feb 2021 16:52:42 +0000 Subject: [PATCH 4/4] Reduce FieldExt bound to Field for Neg and Sub impls on Expression --- src/plonk/circuit.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 3e74d7d..3e8c71f 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -7,7 +7,7 @@ use std::{ }; use super::{lookup, permutation, Error}; -use crate::{arithmetic::FieldExt, poly::Rotation}; +use crate::poly::Rotation; /// A column type pub trait ColumnType: 'static + Sized {} @@ -319,7 +319,7 @@ impl Expression { } } -impl Neg for Expression { +impl Neg for Expression { type Output = Expression; fn neg(self) -> Self::Output { Expression::Scaled(Box::new(self), -F::one()) @@ -333,7 +333,7 @@ impl Add for Expression { } } -impl Sub for Expression { +impl Sub for Expression { type Output = Expression; fn sub(self, rhs: Expression) -> Expression { Expression::Sum(Box::new(self), Box::new(-rhs))