Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Bus Lookups (auto version) #1031

Open
wants to merge 78 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
889e592
bus: draft API
Sep 13, 2023
09f1586
bus: Bus constraints and BusPortDual
Sep 13, 2023
544d6ae
Merge branch 'develop' into bus
Sep 19, 2023
089fd73
bus: organize modules
Sep 19, 2023
90e2e53
bus: type safety with BusTerm
Sep 19, 2023
4e6b81f
bus: calculate witness
Sep 19, 2023
058aa4d
bus: witness generation of the bus accumulator
Sep 19, 2023
59a7039
bus: BusAssigner
Sep 19, 2023
8fac6a9
bus: rename value to message
Sep 19, 2023
962095c
bus: remove "multi" variant
Sep 19, 2023
60593a2
bus: implement batch inversion
Sep 20, 2023
ec90302
bus: simplify batch assignment
Sep 20, 2023
d249952
bus: BusOp for expr and values
Sep 20, 2023
b2bd7f6
bus: reverse count/message order
Sep 20, 2023
2bafc26
bus: draft TableBus
Sep 25, 2023
3ca2865
bus: draft EVM bytes lookup
Sep 25, 2023
da5855f
bus: working byte lookup
Sep 26, 2023
c59a287
bus: connect API
Sep 26, 2023
2a3acf9
bus: hiding details
Sep 26, 2023
0979a31
bus: cover all byte lookups
Sep 26, 2023
9c2c439
bus: adapt test
Sep 26, 2023
9b8fb6b
bus: safer "connect" API
Sep 26, 2023
f1f63a5
bus: introduce bus_codec
Sep 27, 2023
9aa3118
bus: draft multi-value messages
Sep 27, 2023
be26a88
Merge branch 'bus-demo' of github.com:scroll-tech/zkevm-circuits into…
Sep 27, 2023
0db957e
Merge branch 'develop' into bus-demo
naure Sep 28, 2023
a8a489c
Merge branch 'bus-demo' of github.com:scroll-tech/zkevm-circuits into…
Sep 27, 2023
67046d4
Merge branch 'bus-demo' of github.com:scroll-tech/zkevm-circuits into…
Sep 29, 2023
9ecaafe
bus: attach message type to builder
Sep 29, 2023
a45d8e0
bus: make generic and type-safe with message types
Sep 29, 2023
22b67dc
bus: support multiple values
Oct 2, 2023
a1e0826
bus: more flexible PortAssigner for multiport
Oct 3, 2023
aa2f190
bus: refactor BusAssigner and PortAssigner
Oct 3, 2023
5fbbc4d
bus: simplify API with a single bus_assigner object
Oct 3, 2023
f5a95fd
bus: lookup two bytes per operation
Oct 6, 2023
9539083
bus: faster hashtable. Separate traits for msg Expr/F.
Oct 6, 2023
64b5c9e
bus: review names (send, …)
Oct 6, 2023
e98765b
bus: separate enabled and count
Oct 7, 2023
ddf3218
bus: safer and lower degree bus check
Oct 7, 2023
c324383
bus: port batched
Oct 8, 2023
15595d7
bus: calculate degree
Oct 9, 2023
c797b87
bus: remove byte lookups
Oct 10, 2023
5ce5fe8
bus: introduce multi-table message
Oct 10, 2023
3bb7ccb
bus: simplify lookup table API
Oct 10, 2023
0761f43
bus: expose the fixed table
Oct 10, 2023
2b1508e
bus: introduce third phase cells
Oct 11, 2023
9a4732e
bus: track bus lookup expressions
Oct 11, 2023
ae635b7
bus: fixed lookups
Oct 12, 2023
1ef2a58
bus: RW lookup in EVM
Oct 13, 2023
62dba19
bus: trouble-shooting
Oct 13, 2023
3829be5
bus: support conditional lookups
Oct 13, 2023
40be2d0
bus: remove Fixed and RW lookups
Oct 13, 2023
3d976d2
Merge branch 'develop' into bus-demo
Oct 13, 2023
dea3528
bus: support high-degree lookups
Oct 13, 2023
0b8d655
bus: generalize table entries
Oct 13, 2023
dfc36b1
bus: tx table
Oct 20, 2023
d4c26c2
bus: couple RW table and bus assign
Oct 20, 2023
8bf9878
bus: move lookups to super_circuit
Oct 20, 2023
cca3c29
bus-auto: factorize bus table code
Nov 7, 2023
c258ee4
bus-auto: all tables except Block and Ecc
Nov 7, 2023
3393cb6
bus-auto: q_enable for Block and Ecc tables
Nov 7, 2023
efad68b
bus-auto: remove Execution lookups
Nov 7, 2023
38805ca
bus-auto: remove provide_msg mechanism
Nov 7, 2023
3806482
Merge branch 'develop' into bus-demo
Nov 13, 2023
f9ed8d9
bus-auto: move Byte and Fixed tables to the common chip
Nov 10, 2023
01d2d2b
bus-auto: patch to use the right version of halo2
Nov 13, 2023
8fbc10d
Merge branch 'bus-demo' into bus-auto
Nov 15, 2023
29b63a3
bus-demo: clippy
Nov 15, 2023
d1565e5
Merge branch 'bus-demo' into bus-auto
Nov 15, 2023
3fd44bc
bus-auto: fix clippy
Nov 15, 2023
1e1afa9
bus-auto: fmt
Nov 15, 2023
026ae9d
bus-auto: sync halo2 branch develop
Nov 30, 2023
6126059
Merge branch 'develop' into bus-auto-merge
Dec 7, 2023
0d54829
bus-auto: fixes after merge
Dec 7, 2023
9c52286
bus-auto: support for parallel assign_regions
Dec 8, 2023
4dc2d14
Merge branch 'develop' into bus-auto-merge
Dec 8, 2023
7f18032
bus-auto: support for the SHA256 table
Dec 8, 2023
5ede0c6
bus-auto: clippy
Dec 8, 2023
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
10 changes: 8 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ gobuild = { git = "https://github.com/scroll-tech/gobuild.git" }
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.0" }
[patch."https://github.com/privacy-scaling-explorations/poseidon.git"]
poseidon = { git = "https://github.com/scroll-tech/poseidon.git", branch = "main" }
[patch."https://github.com/privacy-scaling-explorations/halo2wrong.git"]
maingate = { git = "https://github.com/scroll-tech/halo2wrong", branch = "bus-auto" }


# Definition of benchmarks profile to use.
Expand Down
6 changes: 5 additions & 1 deletion eth-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ use crate::evm_types::Storage;
/// Trait used to reduce verbosity with the declaration of the [`Field`]
/// trait and its repr.
pub trait Field:
PrimeField<Repr = [u8; 32]> + hash_circuit::hash::Hashable + std::convert::From<Fr> + ScalarField
PrimeField<Repr = [u8; 32]>
+ hash_circuit::hash::Hashable
+ std::convert::From<Fr>
+ ScalarField
+ std::hash::Hash
{
/// Re-expose zero element as a function
fn zero() -> Self {
Expand Down
1 change: 1 addition & 0 deletions gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
halo2_proofs.workspace = true
sha3.workspace = true
eth-types = { path = "../eth-types" }
itertools.workspace = true
strum.workspace = true

[dev-dependencies]
Expand Down
30 changes: 30 additions & 0 deletions gadgets/src/bus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Bus module.

/// Bus chip that check the integrity of all bus accesses.
pub mod bus_chip;

/// The bus builder collects all the ports into a bus.
pub mod bus_builder;

/// A chip to access the bus.
pub mod bus_port;

/// A chip to access the bus, with batching.
pub mod port_multi;

/// A chip to expose a lookup table on a bus.
pub mod bus_lookup;

/// This module encodes messages into terms.
pub mod bus_codec;

/// This module helps ports with their assignments.
mod batch_assigner;

/// Utility functions.
mod util;

#[cfg(test)]
mod tests;

use eth_types::Field;
128 changes: 128 additions & 0 deletions gadgets/src/bus/batch_assigner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use super::{
bus_builder::BusAssigner, bus_codec::BusMessageF, bus_port::BusOpF, util::HelperBatch, Field,
};
use halo2_proofs::circuit::{Region, Value};
use std::{
collections::{
hash_map::Entry::{Occupied, Vacant},
HashMap,
},
marker::PhantomData,
};

/// Assigners are used to delay the assignment until helper values are computed.
pub trait Assigner<F: Field>: Send + Sync {
/// Given the helper value, assign ports and return (offset, term).
#[must_use = "terms must be added to the bus"]
fn assign(&self, region: &mut Region<'_, F>, helper: F) -> (usize, F);
}

/// BatchAssigner computes and assigns terms into helper cells and the bus.
pub struct BatchAssigner<F, M> {
assigners: HelperBatch<F, Box<dyn Assigner<F>>>,
_marker: PhantomData<M>,
}

impl<F: Field, M: BusMessageF<F>> Default for BatchAssigner<F, M> {
fn default() -> Self {
Self::new()
}
}

impl<F: Field, M: BusMessageF<F>> BatchAssigner<F, M> {
/// Create a new BatchAssigner.
pub fn new() -> Self {
Self {
assigners: HelperBatch::new(),
_marker: PhantomData,
}
}

/// Execute an assignment later, with the inverse of `denom`.
pub fn assign_later(&mut self, cmd: Box<dyn Assigner<F>>, denom: Value<F>) {
self.assigners.add_denom(denom, cmd)
}

/// Assign the helper cells and report the terms to the bus.
pub fn finish(self, region: &mut Region<'_, F>, bus_assigner: &mut BusAssigner<F, M>) {
self.assigners.invert().map(|commands| {
for (helper, command) in commands {
let (offset, term) = command.assign(region, helper);
bus_assigner.add_term(region.global_offset(offset), Value::known(term));
}
});
}

pub fn len(&self) -> usize {
self.assigners.len()
}
}

/// OpCounter tracks the messages received, to help generating the corresponding sends.
#[derive(Clone, Debug)]
pub struct BusOpCounter<F, M> {
counts: HashMap<M, isize>,
_marker: PhantomData<(F, M)>,
}

impl<F: Field, M: BusMessageF<F>> Default for BusOpCounter<F, M> {
fn default() -> Self {
Self::new()
}
}

impl<F: Field, M: BusMessageF<F>> BusOpCounter<F, M> {
/// Create a new BusOpCounter.
pub fn new() -> Self {
Self {
counts: HashMap::new(),
_marker: PhantomData,
}
}

/// Record an operation that went on the bus.
pub fn track_op(&mut self, op: &BusOpF<M>) {
if op.count() == 0 {
return;
}
match self.counts.entry(op.message()) {
Occupied(mut entry) => {
let count = entry.get_mut();
*count += op.count();
if *count == 0 {
entry.remove();
}
}
Vacant(entry) => {
entry.insert(op.count());
}
};
}

/// Count how many times a message was received (net of sends).
pub fn count_receives(&self, message: &M) -> isize {
(-self.count_ops(message)).max(0)
}

/// Count how many times a message was sent (net of receives).
pub fn count_sent(&self, message: &M) -> isize {
self.count_ops(message).max(0)
}

/// Count how many times a message was sent (net positive) or received (net negative).
fn count_ops(&self, message: &M) -> isize {
*self.counts.get(message).unwrap_or(&0)
}

/// Return true if all messages received have been sent.
pub fn is_complete(&self) -> bool {
self.counts.is_empty()
}

/// Merge another instance of BusOpCounter into self. The counts are accumulated.
pub fn merge(&mut self, other: Self) {
for (key, other_count) in other.counts {
*self.counts.entry(key).or_insert(0) += other_count;
}
}
}
Loading