Merge pull request #163 from zcash/region-helpers

Helpers for implementing regions
This commit is contained in:
ebfull 2021-02-12 10:03:39 -07:00 committed by GitHub
commit 9fc45ad11e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 13 deletions

View File

@ -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()];

View File

@ -154,9 +154,9 @@ pub trait Layouter<C: Chip> {
/// region.assign_advice(self.config.a, offset, || { Some(value)});
/// });
/// ```
fn assign_region<A, N, NR>(&mut self, name: N, assignment: A) -> Result<(), Error>
fn assign_region<A, AR, N, NR>(&mut self, name: N, assignment: A) -> Result<AR, Error>
where
A: FnMut(Region<'_, C>) -> Result<(), Error>,
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
N: Fn() -> NR,
NR: Into<String>;
@ -202,9 +202,9 @@ impl<'a, C: Chip, L: Layouter<C> + 'a> Layouter<C> for NamespacedLayouter<'a, C,
self.0.config()
}
fn assign_region<A, N, NR>(&mut self, name: N, assignment: A) -> Result<(), Error>
fn assign_region<A, AR, N, NR>(&mut self, name: N, assignment: A) -> Result<AR, Error>
where
A: FnMut(Region<'_, C>) -> Result<(), Error>,
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
N: Fn() -> NR,
NR: Into<String>,
{

View File

@ -106,9 +106,9 @@ impl<'a, C: Chip, CS: Assignment<C::Field> + 'a> Layouter<C> for SingleChip<'a,
&self.config
}
fn assign_region<A, N, NR>(&mut self, name: N, mut assignment: A) -> Result<(), Error>
fn assign_region<A, AR, N, NR>(&mut self, name: N, mut assignment: A) -> Result<AR, Error>
where
A: FnMut(Region<'_, C>) -> Result<(), Error>,
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
N: Fn() -> NR,
NR: Into<String>,
{
@ -136,13 +136,13 @@ impl<'a, C: Chip, CS: Assignment<C::Field> + 'a> Layouter<C> for SingleChip<'a,
self.cs.enter_region(name);
let mut region = SingleChipRegion::new(self, region_index);
{
let result = {
let region: &mut dyn RegionLayouter<C> = &mut region;
assignment(region.into())?;
}
assignment(region.into())
}?;
self.cs.exit_region();
Ok(())
Ok(result)
}
fn get_root(&mut self) -> &mut Self::Root {

View File

@ -107,7 +107,7 @@ pub fn circuit_layout<F: Field, ConcreteCircuit: Circuit<F>, 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;

View File

@ -1,7 +1,10 @@
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;
@ -316,6 +319,13 @@ impl<F: Field> Expression<F> {
}
}
impl<F: Field> Neg for Expression<F> {
type Output = Expression<F>;
fn neg(self) -> Self::Output {
Expression::Scaled(Box::new(self), -F::one())
}
}
impl<F> Add for Expression<F> {
type Output = Expression<F>;
fn add(self, rhs: Expression<F>) -> Expression<F> {
@ -323,6 +333,13 @@ impl<F> Add for Expression<F> {
}
}
impl<F: Field> Sub for Expression<F> {
type Output = Expression<F>;
fn sub(self, rhs: Expression<F>) -> Expression<F> {
Expression::Sum(Box::new(self), Box::new(-rhs))
}
}
impl<F> Mul for Expression<F> {
type Output = Expression<F>;
fn mul(self, rhs: Expression<F>) -> Expression<F> {