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

Feat/keccak without rlc #216

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
14 changes: 8 additions & 6 deletions bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,17 @@ pub enum CopyDataType {
/// When the source for the copy event is the bytecode table.
Bytecode = 1,
/// When the source/destination for the copy event is memory.
Memory,
Memory = 2,
/// When the source for the copy event is tx's calldata.
TxCalldata,
TxCalldata = 3,
/// When the destination for the copy event is tx's log.
TxLog,
TxLog = 4,
///
SHA3 = 5,
/// When the destination rows are not directly for copying but for a special
/// scenario where we wish to accumulate the value (RLC) over all rows.
/// This is used for Copy Lookup from SHA3 opcode verification.
RlcAcc,
RlcAcc = 6,
}

impl From<CopyDataType> for usize {
Expand Down Expand Up @@ -252,10 +254,10 @@ impl CopyEvent {
.checked_sub(self.src_addr)
.unwrap_or_default(),
),
CopyDataType::RlcAcc | CopyDataType::TxLog => unreachable!(),
CopyDataType::RlcAcc | CopyDataType::TxLog | CopyDataType::SHA3 => unreachable!(),
};
let destination_rw_increase = match self.dst_type {
CopyDataType::RlcAcc | CopyDataType::Bytecode => 0,
CopyDataType::RlcAcc | CopyDataType::Bytecode | CopyDataType::SHA3 => 0,
CopyDataType::TxLog | CopyDataType::Memory => u64::try_from(step_index).unwrap() / 2,
CopyDataType::TxCalldata => unreachable!(),
};
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Opcode for Sha3 {
src_type: CopyDataType::Memory,
src_id: NumberOrHash::Number(call_id),
dst_addr: 0,
dst_type: CopyDataType::RlcAcc,
dst_id: NumberOrHash::Number(call_id),
dst_type: CopyDataType::SHA3,
dst_id: NumberOrHash::Hash(sha3.into()),
log_id: None,
rw_counter_start,
bytes: steps,
Expand Down
8 changes: 0 additions & 8 deletions circuit-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ pub mod tx_circuit;
#[cfg(feature = "benches")]
pub mod super_circuit;

#[cfg(test)]
#[cfg(feature = "benches")]
pub mod bit_keccak;

#[cfg(test)]
#[cfg(feature = "benches")]
pub mod packed_keccak;

#[cfg(test)]
#[cfg(feature = "benches")]
pub mod packed_multi_keccak;
Expand Down
25 changes: 18 additions & 7 deletions zkevm-circuits/src/bytecode_circuit/bytecode_unroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
evm_circuit::util::{
and, constraint_builder::BaseConstraintBuilder, not, or, select, RandomLinearCombination,
},
table::{BytecodeFieldTag, BytecodeTable, DynamicTableColumns, KeccakTable},
table::{BytecodeFieldTag, BytecodeTable, KeccakTable},
util::{Challenges, Expr},
};
use bus_mapping::evm::OpcodeId;
Expand All @@ -16,6 +16,7 @@ use halo2_proofs::{
},
poly::Rotation,
};
use itertools::zip;
use keccak256::plain::Keccak;
use std::vec;

Expand Down Expand Up @@ -349,15 +350,20 @@ impl<F: Field> Config<F> {
meta.query_advice(is_final, Rotation::cur()),
not::expr(meta.query_advice(padding, Rotation::cur())),
]);
let lookup_columns = vec![hash_input_rlc, code_length, bytecode_table.code_hash];
let lookup_input_columns = vec![hash_input_rlc, code_length, bytecode_table.code_hash];
let lookup_table_columns = vec![
keccak_table.input_rlc,
keccak_table.input_len,
keccak_table.output_rlc,
];
let mut constraints = vec![(
enable.clone(),
meta.query_advice(keccak_table.is_enabled, Rotation::cur()),
)];
for (i, column) in keccak_table.columns().iter().skip(1).enumerate() {
for (input_column, table_column) in zip(lookup_input_columns, lookup_table_columns) {
constraints.push((
enable.clone() * meta.query_advice(lookup_columns[i], Rotation::cur()),
meta.query_advice(*column, Rotation::cur()),
enable.clone() * meta.query_advice(input_column, Rotation::cur()),
meta.query_advice(table_column, Rotation::cur()),
))
}
constraints
Expand Down Expand Up @@ -793,10 +799,15 @@ mod tests {
}

/// Tests a circuit with incomplete bytecode
#[ignore = "keccak rows larger than bytecode rows"]
#[test]
fn bytecode_incomplete() {
let k = 9;
test_bytecode_circuit_unrolled::<Fr>(k, vec![unroll(vec![7u8; 2usize.pow(k) + 1])], false);
let k = 12;
test_bytecode_circuit_unrolled::<Fr>(
k - 3,
vec![unroll(vec![7u8; 2usize.pow(k) + 1])],
false,
);
}

/// Tests multiple bytecodes in a single circuit
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/bytecode_circuit/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub fn test_bytecode_circuit_unrolled<F: Field>(
bytecodes: Vec<UnrolledBytecode<F>>,
success: bool,
) {
let k = k + 3;
let circuit = BytecodeCircuitTester::<F> {
bytecodes,
size: 2usize.pow(k),
Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ impl<F: Field> ExecutionGadget<F> for Sha3Gadget<F> {
cb.copy_table_lookup(
cb.curr.state.call_id.expr(),
CopyDataType::Memory.expr(),
cb.curr.state.call_id.expr(),
CopyDataType::RlcAcc.expr(),
sha3_rlc.expr(),
CopyDataType::SHA3.expr(),
memory_address.offset(),
memory_address.address(),
0.expr(), // dst_addr for CopyDataType::RlcAcc is 0.
memory_address.length(),
rlc_acc.expr(),
0.expr(),
copy_rwc_inc.expr(),
);
});
cb.condition(not::expr(memory_address.has_length()), |cb| {
cb.require_zero("copy_rwc_inc == 0 for size = 0", copy_rwc_inc.expr());
cb.require_zero("rlc_acc == 0 for size = 0", rlc_acc.expr());
});
cb.keccak_table_lookup(rlc_acc.expr(), memory_address.length(), sha3_rlc.expr());
cb.keccak_table_lookup(sha3_rlc.expr(), sha3_rlc.expr());

let memory_expansion = MemoryExpansionGadget::construct(cb, [memory_address.address()]);
let memory_copier_gas = MemoryCopierGasGadget::construct(
Expand Down Expand Up @@ -167,7 +167,7 @@ mod tests {
TestContext::<2, 1>::simple_ctx_with_bytecode(code).unwrap(),
None,
CircuitsParams {
max_rws: 5500,
max_rws: 11000,
..Default::default()
}
),
Expand Down
14 changes: 8 additions & 6 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ pub(crate) enum Lookup<F> {
/// Lookup to keccak table.
KeccakTable {
/// Accumulator to the input.
input_rlc: Expression<F>,
hash_id: Expression<F>,
/// Length of input that is being hashed.
input_len: Expression<F>,
//input_len: Expression<F>,
/// Output (hash) until this state. This is the RLC representation of
/// the final output keccak256 hash of the input.
output_rlc: Expression<F>,
Expand Down Expand Up @@ -381,14 +381,16 @@ impl<F: Field> Lookup<F> {
rwc_inc.clone(),
],
Self::KeccakTable {
input_rlc,
input_len,
hash_id,
output_rlc,
} => vec![
1.expr(), // is_enabled
input_rlc.clone(),
input_len.clone(),
//input_rlc.clone(),
//input_len.clone(),
output_rlc.clone(),
hash_id.clone(),
//0.expr(), // byte_value
//0.expr(), // bytes_left
],
Self::ExpTable {
identifier,
Expand Down
6 changes: 2 additions & 4 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,15 +1214,13 @@ impl<'a, F: Field> ConstraintBuilder<'a, F> {

pub(crate) fn keccak_table_lookup(
&mut self,
input_rlc: Expression<F>,
input_len: Expression<F>,
hash_id: Expression<F>,
output_rlc: Expression<F>,
) {
self.add_lookup(
"keccak lookup",
Lookup::KeccakTable {
input_rlc,
input_len,
hash_id,
output_rlc,
},
);
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/keccak_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! The keccak circuit implementation.

/// Keccak bit
pub mod keccak_bit;
//pub mod keccak_bit;
/// Keccak packed
pub mod keccak_packed;
//pub mod keccak_packed;
/// Keccak packed multi
pub mod keccak_packed_multi;
/// Util
Expand Down
Loading