Merge pull request #163 from zcash/region-helpers
Helpers for implementing regions
This commit is contained in:
commit
9fc45ad11e
|
@ -8,6 +8,7 @@ use halo2::{
|
||||||
use plotters::prelude::*;
|
use plotters::prelude::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
#[allow(clippy::many_single_char_names)]
|
||||||
fn main() {
|
fn main() {
|
||||||
/// This represents an advice column at a certain row in the ConstraintSystem
|
/// This represents an advice column at a certain row in the ConstraintSystem
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
@ -372,7 +373,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let a = Fp::rand();
|
let a = Fp::rand();
|
||||||
let a_squared = a * &a;
|
let a_squared = a * a;
|
||||||
let aux = Fp::one() + Fp::one();
|
let aux = Fp::one() + Fp::one();
|
||||||
let lookup_table = vec![aux, a, a, Fp::zero()];
|
let lookup_table = vec![aux, a, a, Fp::zero()];
|
||||||
let lookup_table_2 = vec![Fp::zero(), a, a_squared, Fp::zero()];
|
let lookup_table_2 = vec![Fp::zero(), a, a_squared, Fp::zero()];
|
||||||
|
|
|
@ -154,9 +154,9 @@ pub trait Layouter<C: Chip> {
|
||||||
/// region.assign_advice(self.config.a, offset, || { Some(value)});
|
/// 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
|
where
|
||||||
A: FnMut(Region<'_, C>) -> Result<(), Error>,
|
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
|
||||||
N: Fn() -> NR,
|
N: Fn() -> NR,
|
||||||
NR: Into<String>;
|
NR: Into<String>;
|
||||||
|
|
||||||
|
@ -202,9 +202,9 @@ impl<'a, C: Chip, L: Layouter<C> + 'a> Layouter<C> for NamespacedLayouter<'a, C,
|
||||||
self.0.config()
|
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
|
where
|
||||||
A: FnMut(Region<'_, C>) -> Result<(), Error>,
|
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
|
||||||
N: Fn() -> NR,
|
N: Fn() -> NR,
|
||||||
NR: Into<String>,
|
NR: Into<String>,
|
||||||
{
|
{
|
||||||
|
|
|
@ -106,9 +106,9 @@ impl<'a, C: Chip, CS: Assignment<C::Field> + 'a> Layouter<C> for SingleChip<'a,
|
||||||
&self.config
|
&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
|
where
|
||||||
A: FnMut(Region<'_, C>) -> Result<(), Error>,
|
A: FnMut(Region<'_, C>) -> Result<AR, Error>,
|
||||||
N: Fn() -> NR,
|
N: Fn() -> NR,
|
||||||
NR: Into<String>,
|
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);
|
self.cs.enter_region(name);
|
||||||
let mut region = SingleChipRegion::new(self, region_index);
|
let mut region = SingleChipRegion::new(self, region_index);
|
||||||
{
|
let result = {
|
||||||
let region: &mut dyn RegionLayouter<C> = &mut region;
|
let region: &mut dyn RegionLayouter<C> = &mut region;
|
||||||
assignment(region.into())?;
|
assignment(region.into())
|
||||||
}
|
}?;
|
||||||
self.cs.exit_region();
|
self.cs.exit_region();
|
||||||
|
|
||||||
Ok(())
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_root(&mut self) -> &mut Self::Root {
|
fn get_root(&mut self) -> &mut Self::Root {
|
||||||
|
|
|
@ -107,7 +107,7 @@ pub fn circuit_layout<F: Field, ConcreteCircuit: Circuit<F>, DB: DrawingBackend>
|
||||||
if let Some(offset) = region.offset {
|
if let Some(offset) = region.offset {
|
||||||
// Sort the region's columns according to the defined ordering.
|
// Sort the region's columns according to the defined ordering.
|
||||||
let mut columns: Vec<_> = region.columns.into_iter().collect();
|
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.
|
// Render contiguous parts of the same region as a single box.
|
||||||
let mut width = None;
|
let mut width = None;
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
use core::cmp::max;
|
use core::cmp::max;
|
||||||
use core::ops::{Add, Mul};
|
use core::ops::{Add, Mul};
|
||||||
use ff::Field;
|
use ff::Field;
|
||||||
use std::convert::TryFrom;
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
|
ops::{Neg, Sub},
|
||||||
|
};
|
||||||
|
|
||||||
use super::{lookup, permutation, Error};
|
use super::{lookup, permutation, Error};
|
||||||
use crate::poly::Rotation;
|
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> {
|
impl<F> Add for Expression<F> {
|
||||||
type Output = Expression<F>;
|
type Output = Expression<F>;
|
||||||
fn add(self, rhs: Expression<F>) -> 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> {
|
impl<F> Mul for Expression<F> {
|
||||||
type Output = Expression<F>;
|
type Output = Expression<F>;
|
||||||
fn mul(self, rhs: Expression<F>) -> Expression<F> {
|
fn mul(self, rhs: Expression<F>) -> Expression<F> {
|
||||||
|
|
Loading…
Reference in New Issue