Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

represent u256 with hi/lo instead of rlc #98

Open
wants to merge 14 commits into
base: v0.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
350 changes: 139 additions & 211 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
ethers-core = "2.0.7"
itertools = "0.10.5"
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0901"}
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-1201"}
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
rand = "0.8"
lazy_static = "1.4.0"
Expand All @@ -23,7 +23,7 @@ thiserror = "1.0"
log = "0.4"

[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "develop" }
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.0" }
[patch.crates-io]
ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" }

Expand All @@ -32,7 +32,7 @@ ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "
print_layout = ["halo2_proofs/dev-graph"]

[dev-dependencies]
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", rev = "7d9bc181953cfc6e7baf82ff0ce651281fd70a8a" }
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", rev = "fb271ea6d3cff6712ac0213fed66a9e2fccee60c" }
# mpt-zktrie = { path = "../scroll-circuits/zktrie" }
rand_chacha = "0.3.0"
plotters = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2022-12-10
nightly-2023-12-03
54 changes: 36 additions & 18 deletions src/constraint_builder.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
use crate::gadgets::poseidon::PoseidonLookup;
use halo2_proofs::{
arithmetic::FieldExt,
plonk::{ConstraintSystem, SecondPhase},
};
use halo2_proofs::halo2curves::ff::PrimeField;
use halo2_proofs::plonk::ConstraintSystem;
use itertools::Itertools;

mod binary_column;
mod binary_query;
mod column;
mod query;
mod to_queries;
mod word_columns;

pub use binary_column::BinaryColumn;
pub use binary_query::BinaryQuery;
pub use column::{AdviceColumn, FixedColumn, SecondPhaseAdviceColumn, SelectorColumn};
pub use column::{AdviceColumn, FixedColumn, SelectorColumn};
pub use query::Query;
use to_queries::ToQueries;
pub use word_columns::WordColumns;

pub struct ConstraintBuilder<F: FieldExt> {
pub struct ConstraintBuilder<F: PrimeField> {
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: PrimeField> ConstraintBuilder<F> {
pub fn new(every_row: SelectorColumn) -> Self {
Self {
constraints: vec![],
Expand All @@ -40,16 +42,39 @@ impl<F: FieldExt> ConstraintBuilder<F> {
.clone()
}

pub fn assert_zero(&mut self, name: &'static str, query: Query<F>) {
pub fn assert_zero<const N: usize, T: ToQueries<F, N>>(
&mut self,
name: &'static str,
queries: T,
) {
let condition = self
.conditions
.iter()
.fold(BinaryQuery::one(), |a, b| a.and(b.clone()));
self.constraints.push((name, condition.condition(query)))
for query in queries.to_queries() {
self.constraints
.push((name, condition.clone().condition(query)));
}
}

pub fn assert_equal(&mut self, name: &'static str, left: Query<F>, right: Query<F>) {
self.assert_zero(name, left - right)
pub fn assert_equal<const N: usize, T: ToQueries<F, N>>(
&mut self,
name: &'static str,
left: T,
right: T,
) {
let a = left.to_queries();
let b = right.to_queries();
assert_eq!(a.len(), b.len());
let differences: [Query<F>; N] = left
.to_queries()
.into_iter()
.zip_eq(right.to_queries())
.map(|(left, right)| left - right)
.collect::<Vec<_>>()
.try_into()
.unwrap();
self.assert_zero(name, differences)
}

pub fn assert(&mut self, name: &'static str, condition: BinaryQuery<F>) {
Expand Down Expand Up @@ -146,13 +171,6 @@ impl<F: FieldExt> ConstraintBuilder<F> {
[0; N].map(|_| AdviceColumn(cs.advice_column()))
}

pub fn second_phase_advice_columns<const N: usize>(
&self,
cs: &mut ConstraintSystem<F>,
) -> [SecondPhaseAdviceColumn; N] {
[0; N].map(|_| SecondPhaseAdviceColumn(cs.advice_column_in(SecondPhase)))
}

pub fn binary_columns<const N: usize>(
&mut self,
cs: &mut ConstraintSystem<F>,
Expand Down
21 changes: 13 additions & 8 deletions src/constraint_builder/binary_column.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{BinaryQuery, ConstraintBuilder, Query};
use halo2_proofs::halo2curves::ff::PrimeField;
use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
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: PrimeField>(&self, i: i32) -> BinaryQuery<F> {
BinaryQuery(Query::Advice(self.0, i))
}

pub fn current<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn current<F: PrimeField>(&self) -> BinaryQuery<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn previous<F: PrimeField>(&self) -> BinaryQuery<F> {
self.rotation(-1)
}

pub fn next<F: FieldExt>(&self) -> BinaryQuery<F> {
pub fn next<F: PrimeField>(&self) -> BinaryQuery<F> {
self.rotation(1)
}

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

pub fn assign<F: FieldExt>(&self, region: &mut Region<'_, F>, offset: usize, value: bool) {
pub fn assign<F: PrimeField>(&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::halo2curves::ff::PrimeField;
use halo2_proofs::{
arithmetic::{Field, FieldExt},
arithmetic::Field,
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: PrimeField> 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: PrimeField> 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: PrimeField> 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
53 changes: 15 additions & 38 deletions src/constraint_builder/column.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{BinaryQuery, Query};
use halo2_proofs::halo2curves::ff::PrimeField;
use halo2_proofs::{
arithmetic::FieldExt,
circuit::{Region, Value},
plonk::{Advice, Column, Fixed},
};
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: PrimeField>(self) -> BinaryQuery<F> {
self.rotation(0)
}

pub fn rotation<F: FieldExt>(self, i: i32) -> BinaryQuery<F> {
pub fn rotation<F: PrimeField>(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: PrimeField>(&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: PrimeField>(self, i: i32) -> Query<F> {
Query::Fixed(self.0, i)
}

pub fn current<F: FieldExt>(self) -> Query<F> {
pub fn current<F: PrimeField>(self) -> Query<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(self) -> Query<F> {
pub fn previous<F: PrimeField>(self) -> Query<F> {
self.rotation(-1)
}

pub fn assign<F: FieldExt, T: Copy + TryInto<F>>(
pub fn assign<F: PrimeField, 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: PrimeField>(self, i: i32) -> Query<F> {
Query::Advice(self.0, i)
}

pub fn current<F: FieldExt>(self) -> Query<F> {
pub fn current<F: PrimeField>(self) -> Query<F> {
self.rotation(0)
}

pub fn previous<F: FieldExt>(self) -> Query<F> {
pub fn previous<F: PrimeField>(self) -> Query<F> {
self.rotation(-1)
}

pub fn next<F: FieldExt>(self) -> Query<F> {
pub fn next<F: PrimeField>(self) -> Query<F> {
self.rotation(1)
}

pub fn delta<F: FieldExt>(self) -> Query<F> {
pub fn delta<F: PrimeField>(self) -> Query<F> {
self.current() - self.previous()
}

pub fn assign<F: FieldExt, T: Copy + TryInto<F>>(
pub fn assign<F: PrimeField, T: Copy + TryInto<F>>(
&self,
region: &mut Region<'_, F>,
offset: usize,
Expand All @@ -102,26 +102,3 @@ impl AdviceColumn {
.expect("failed assign_advice");
}
}

#[derive(Clone, Copy)]
pub struct SecondPhaseAdviceColumn(pub Column<Advice>);

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

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

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

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

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum Query<F: Clone> {
Constant(F),
Advice(Column<Advice>, i32),
Expand All @@ -24,7 +24,7 @@ pub enum Query<F: Clone> {
Mul(Box<Self>, Box<Self>),
}

impl<F: FieldExt> Query<F> {
impl<F: PrimeField> 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: PrimeField> 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: PrimeField> 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: PrimeField> From<BinaryQuery<F>> for Query<F> {
fn from(b: BinaryQuery<F>) -> Self {
b.0
}
Expand Down
Loading
Loading