Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenfeizhang committed Oct 27, 2023
1 parent d2c8750 commit 5c1420c
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 128 deletions.
6 changes: 3 additions & 3 deletions src/constraint_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::gadgets::poseidon::PoseidonLookup;
use halo2_proofs::{
arithmetic::FieldExt,
halo2curves::ff::FromUniformBytes,
plonk::{ConstraintSystem, SecondPhase},
};
use itertools::Itertools;
Expand All @@ -15,15 +15,15 @@ pub use binary_query::BinaryQuery;
pub use column::{AdviceColumn, FixedColumn, SecondPhaseAdviceColumn, SelectorColumn};
pub use query::Query;

pub struct ConstraintBuilder<F: FieldExt> {
pub struct ConstraintBuilder<F: FromUniformBytes<64> + Ord> {
constraints: Vec<(&'static str, Query<F>)>,
#[allow(clippy::type_complexity)]
lookups: Vec<(&'static str, Vec<(Query<F>, Query<F>)>)>,

conditions: Vec<BinaryQuery<F>>,
}

impl<F: FieldExt> ConstraintBuilder<F> {
impl<F: FromUniformBytes<64> + Ord> ConstraintBuilder<F> {
pub fn new(every_row: SelectorColumn) -> Self {
Self {
constraints: vec![],
Expand Down
26 changes: 18 additions & 8 deletions src/constraint_builder/binary_column.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{BinaryQuery, ConstraintBuilder, Query};
use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
halo2curves::ff::FromUniformBytes,
plonk::ConstraintSystem,
plonk::{Advice, Column},
};
Expand All @@ -10,23 +10,23 @@ use halo2_proofs::{
pub struct BinaryColumn(pub Column<Advice>);

impl BinaryColumn {
pub fn rotation<F: FieldExt>(&self, i: i32) -> BinaryQuery<F> {
pub fn rotation<F: FromUniformBytes<64> + Ord>(&self, i: i32) -> BinaryQuery<F> {
BinaryQuery(Query::Advice(self.0, i))
}

pub fn current<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn current<F: FromUniformBytes<64> + Ord>(&self) -> BinaryQuery<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn previous<F: FromUniformBytes<64> + Ord>(&self) -> BinaryQuery<F> {
self.rotation(-1)
}

pub fn next<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn next<F: FromUniformBytes<64> + Ord>(&self) -> BinaryQuery<F> {
self.rotation(1)
}

pub fn configure<F: FieldExt>(
pub fn configure<F: FromUniformBytes<64> + Ord>(
cs: &mut ConstraintSystem<F>,
cb: &mut ConstraintBuilder<F>,
) -> Self {
Expand All @@ -38,9 +38,19 @@ impl BinaryColumn {
binary_column
}

pub fn assign<F: FieldExt>(&self, region: &mut Region<'_, F>, offset: usize, value: bool) {
pub fn assign<F: FromUniformBytes<64> + Ord>(
&self,
region: &mut Region<'_, F>,
offset: usize,
value: bool,
) {
region
.assign_advice(|| "binary", self.0, offset, || Value::known(F::from(value)))
.assign_advice(
|| "binary",
self.0,
offset,
|| Value::known(F::from(value as u64)),
)
.expect("failed assign_advice");
}
}
9 changes: 5 additions & 4 deletions src/constraint_builder/binary_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Query;
use halo2_proofs::{
arithmetic::{Field, FieldExt},
arithmetic::Field,
halo2curves::ff::FromUniformBytes,
plonk::{Expression, VirtualCells},
};
// use std::iter::Sum;
Expand All @@ -9,7 +10,7 @@ use halo2_proofs::{
#[derive(Clone)]
pub struct BinaryQuery<F: Field>(pub Query<F>);

impl<F: FieldExt> BinaryQuery<F> {
impl<F: FromUniformBytes<64> + Ord> BinaryQuery<F> {
pub fn zero() -> Self {
Self(Query::from(0))
}
Expand All @@ -35,13 +36,13 @@ impl<F: FieldExt> BinaryQuery<F> {
}
}

impl<F: FieldExt> BinaryQuery<F> {
impl<F: FromUniformBytes<64> + Ord> BinaryQuery<F> {
pub fn run(self, meta: &mut VirtualCells<'_, F>) -> Expression<F> {
self.0.run(meta)
}
}

impl<F: FieldExt> std::ops::Not for BinaryQuery<F> {
impl<F: FromUniformBytes<64> + Ord> std::ops::Not for BinaryQuery<F> {
type Output = Self;

// In general this can cause a ConstraintPoisoned. You need to add a selector column that's all ones to be safe.
Expand Down
43 changes: 24 additions & 19 deletions src/constraint_builder/column.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{BinaryQuery, Query};
use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
halo2curves::ff::FromUniformBytes,
plonk::{Advice, Column, Fixed},
};
use std::fmt::Debug;
Expand All @@ -10,17 +10,17 @@ use std::fmt::Debug;
pub struct SelectorColumn(pub Column<Fixed>);

impl SelectorColumn {
pub fn current<F: FieldExt>(self) -> BinaryQuery<F> {
pub fn current<F: FromUniformBytes<64> + Ord>(self) -> BinaryQuery<F> {
self.rotation(0)
}

pub fn rotation<F: FieldExt>(self, i: i32) -> BinaryQuery<F> {
pub fn rotation<F: FromUniformBytes<64> + Ord>(self, i: i32) -> BinaryQuery<F> {
BinaryQuery(Query::Fixed(self.0, i))
}

pub fn enable<F: FieldExt>(&self, region: &mut Region<'_, F>, offset: usize) {
pub fn enable<F: FromUniformBytes<64> + Ord>(&self, region: &mut Region<'_, F>, offset: usize) {
region
.assign_fixed(|| "selector", self.0, offset, || Value::known(F::one()))
.assign_fixed(|| "selector", self.0, offset, || Value::known(F::ONE))
.expect("failed enable selector");
}
}
Expand All @@ -29,19 +29,19 @@ impl SelectorColumn {
pub struct FixedColumn(pub Column<Fixed>);

impl FixedColumn {
pub fn rotation<F: FieldExt>(self, i: i32) -> Query<F> {
pub fn rotation<F: FromUniformBytes<64> + Ord>(self, i: i32) -> Query<F> {
Query::Fixed(self.0, i)
}

pub fn current<F: FieldExt>(self) -> Query<F> {
pub fn current<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(self) -> Query<F> {
pub fn previous<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(-1)
}

pub fn assign<F: FieldExt, T: Copy + TryInto<F>>(
pub fn assign<F: FromUniformBytes<64> + Ord, T: Copy + TryInto<F>>(
&self,
region: &mut Region<'_, F>,
offset: usize,
Expand All @@ -64,27 +64,27 @@ impl FixedColumn {
pub struct AdviceColumn(pub Column<Advice>);

impl AdviceColumn {
pub fn rotation<F: FieldExt>(self, i: i32) -> Query<F> {
pub fn rotation<F: FromUniformBytes<64> + Ord>(self, i: i32) -> Query<F> {
Query::Advice(self.0, i)
}

pub fn current<F: FieldExt>(self) -> Query<F> {
pub fn current<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(self) -> Query<F> {
pub fn previous<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(-1)
}

pub fn next<F: FieldExt>(self) -> Query<F> {
pub fn next<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(1)
}

pub fn delta<F: FieldExt>(self) -> Query<F> {
pub fn delta<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.current() - self.previous()
}

pub fn assign<F: FieldExt, T: Copy + TryInto<F>>(
pub fn assign<F: FromUniformBytes<64> + Ord, T: Copy + TryInto<F>>(
&self,
region: &mut Region<'_, F>,
offset: usize,
Expand All @@ -107,19 +107,24 @@ impl AdviceColumn {
pub struct SecondPhaseAdviceColumn(pub Column<Advice>);

impl SecondPhaseAdviceColumn {
fn rotation<F: FieldExt>(self, i: i32) -> Query<F> {
fn rotation<F: FromUniformBytes<64> + Ord>(self, i: i32) -> Query<F> {
Query::Advice(self.0, i)
}

pub fn current<F: FieldExt>(self) -> Query<F> {
pub fn current<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(self) -> Query<F> {
pub fn previous<F: FromUniformBytes<64> + Ord>(self) -> Query<F> {
self.rotation(-1)
}

pub fn assign<F: FieldExt>(&self, region: &mut Region<'_, F>, offset: usize, value: Value<F>) {
pub fn assign<F: FromUniformBytes<64> + Ord>(
&self,
region: &mut Region<'_, F>,
offset: usize,
value: Value<F>,
) {
region
.assign_advice(|| "second phase advice", self.0, offset, || value)
.expect("failed assign_advice");
Expand Down
14 changes: 7 additions & 7 deletions src/constraint_builder/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::BinaryQuery;
use halo2_proofs::{
arithmetic::{Field, FieldExt},
halo2curves::{bn256::Fr, group::ff::PrimeField},
arithmetic::Field,
halo2curves::{bn256::Fr, ff::FromUniformBytes, group::ff::PrimeField},
plonk::{Advice, Challenge, Column, Expression, Fixed, VirtualCells},
poly::Rotation,
};
Expand All @@ -24,7 +24,7 @@ pub enum Query<F: Clone> {
Mul(Box<Self>, Box<Self>),
}

impl<F: FieldExt> Query<F> {
impl<F: FromUniformBytes<64> + Ord> Query<F> {
pub fn zero() -> Self {
Self::from(0)
}
Expand All @@ -43,7 +43,7 @@ impl<F: FieldExt> Query<F> {
Query::Advice(c, r) => meta.query_advice(*c, Rotation(*r)),
Query::Fixed(c, r) => meta.query_fixed(*c, Rotation(*r)),
Query::Challenge(c) => meta.query_challenge(*c),
Query::Neg(q) => Expression::Constant(F::zero()) - q.run(meta),
Query::Neg(q) => Expression::Constant(F::ZERO) - q.run(meta),
Query::Add(q, u) => q.run(meta) + u.run(meta),
Query::Mul(q, u) => q.run(meta) * u.run(meta),
}
Expand All @@ -54,13 +54,13 @@ impl<F: FieldExt> Query<F> {
}
}

impl<F: FieldExt> From<u64> for Query<F> {
impl<F: FromUniformBytes<64> + Ord> From<u64> for Query<F> {
fn from(x: u64) -> Self {
Self::Constant(F::from(x))
}
}

impl<F: FieldExt> From<Fr> for Query<F> {
impl<F: FromUniformBytes<64> + Ord> From<Fr> for Query<F> {
fn from(x: Fr) -> Self {
let little_endian_bytes = x.to_repr();
let little_endian_limbs = little_endian_bytes
Expand All @@ -73,7 +73,7 @@ impl<F: FieldExt> From<Fr> for Query<F> {
}
}

impl<F: FieldExt> From<BinaryQuery<F>> for Query<F> {
impl<F: FromUniformBytes<64> + Ord> From<BinaryQuery<F>> for Query<F> {
fn from(b: BinaryQuery<F>) -> Self {
b.0
}
Expand Down
21 changes: 11 additions & 10 deletions src/gadgets/byte_bit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::constraint_builder::{ConstraintBuilder, FixedColumn, Query};
use halo2_proofs::{arithmetic::FieldExt, circuit::Region, plonk::ConstraintSystem};
use halo2_proofs::{circuit::Region, halo2curves::ff::FromUniformBytes, plonk::ConstraintSystem};

// TODO: fix name to configggggggg
#[derive(Clone)]
Expand All @@ -10,53 +10,54 @@ pub struct ByteBitGadget {
}

pub trait RangeCheck8Lookup {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 1];
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 1];
}

pub trait RangeCheck256Lookup {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 1];
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 1];
}

pub trait ByteBitLookup {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 3];
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 3];
}

impl ByteBitGadget {
pub fn configure<F: FieldExt>(
pub fn configure<F: FromUniformBytes<64> + Ord>(
cs: &mut ConstraintSystem<F>,
cb: &mut ConstraintBuilder<F>,
) -> Self {
let ([], [byte, index, bit], []) = cb.build_columns(cs);
Self { byte, index, bit }
}

pub fn assign<F: FieldExt>(&self, region: &mut Region<'_, F>) {
pub fn assign<F: FromUniformBytes<64> + Ord>(&self, region: &mut Region<'_, F>) {
let mut offset = 0;
for byte in 0..256 {
for index in 0..8 {
self.byte.assign(region, offset, byte);
self.index.assign(region, offset, index);
self.bit.assign(region, offset, byte & (1 << index) != 0);
self.bit
.assign(region, offset, (byte & (1 << index) != 0) as u64);
offset += 1;
}
}
}
}

impl RangeCheck8Lookup for ByteBitGadget {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 1] {
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 1] {
[self.index.current()]
}
}

impl RangeCheck256Lookup for ByteBitGadget {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 1] {
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 1] {
[self.byte.current()]
}
}

impl ByteBitLookup for ByteBitGadget {
fn lookup<F: FieldExt>(&self) -> [Query<F>; 3] {
fn lookup<F: FromUniformBytes<64> + Ord>(&self) -> [Query<F>; 3] {
[
self.byte.current(),
self.index.current(),
Expand Down
Loading

0 comments on commit 5c1420c

Please sign in to comment.