Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
28d3504
fixed clippy and fmt
SwordArt0820 Jan 18, 2022
0486113
fixed clippy and fmt
SwordArt0820 Jan 18, 2022
fb352e6
fixed fmt
SwordArt0820 Jan 18, 2022
800081e
fixed permission for run.sh
SwordArt0820 Jan 18, 2022
f8ee9e2
fixed some issue.
SwordArt0820 Jan 22, 2022
67cb9b9
fixed some issue.
SwordArt0820 Jan 22, 2022
061f1a5
add some text.
SwordArt0820 Jan 22, 2022
13894f9
fixed text issue.
SwordArt0820 Jan 22, 2022
b502e32
shl refactor completed.
AprilLier1504 Jan 22, 2022
f535f70
fixed some issues.
AprilLier1504 Jan 22, 2022
4caa177
Merge remote-tracking branch 'origin/main' into shl_opcode
lispc Feb 9, 2022
29397c2
Merge remote-tracking branch 'origin/main' into shr_new
lispc Feb 9, 2022
52b6206
fix
lispc Feb 9, 2022
0fb9164
fix
lispc Feb 9, 2022
f3548ca
Merge branch 'main' into shr_new
lispc Mar 4, 2022
12a9c23
fix all
lispc Mar 4, 2022
2d67c60
fixed some issue.
SwordArt0820 Jan 22, 2022
841425a
fixed clippy and fmt
SwordArt0820 Jan 18, 2022
e9a57f6
fixed clippy and fmt
SwordArt0820 Jan 18, 2022
2b0b9b4
fixed fmt
SwordArt0820 Jan 18, 2022
03fd7e4
fixed permission for run.sh
SwordArt0820 Jan 18, 2022
cfe8b37
fixed issues and merge main.
SwordArt0820 Mar 9, 2022
63f5107
add some notes
SwordArt0820 Mar 9, 2022
9db111e
fixed clippy.
SwordArt0820 Mar 9, 2022
141e96b
Merge branch 'main' into shr_new
lispc Mar 18, 2022
fbd7133
Merge remote-tracking branch 'scroll/main' into shr_new
lispc Mar 21, 2022
82298a2
lint
lispc Mar 21, 2022
89c3893
Merge remote-tracking branch 'origin/main' into shr_new
lispc Mar 25, 2022
6945a25
Merge remote-tracking branch 'origin/main' into shr_new
lispc Apr 5, 2022
8825859
fix conflict
lispc Apr 5, 2022
ef48eb1
Merge remote-tracking branch 'origin/main' into shr_new
lispc Apr 7, 2022
f7f6ae4
lint
lispc Apr 7, 2022
8c8dfbe
Merge remote-tracking branch 'scroll/shl_opcode' into shr_new
lispc Apr 7, 2022
81de62d
fix
lispc Apr 7, 2022
680b145
fix
lispc Apr 7, 2022
1b0db6c
fix shl
lispc Apr 7, 2022
e20ff81
Merge remote-tracking branch 'origin/main' into shl_shr_new
silathdiir Apr 28, 2022
88ebbf4
Merge remote-tracking branch 'origin/main' into shl_shr_new
silathdiir May 10, 2022
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
2 changes: 2 additions & 0 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ pub mod test {
FixedTableTag::Range1024,
FixedTableTag::SignByte,
FixedTableTag::ResponsibleOpcode,
FixedTableTag::Bitslevel,
FixedTableTag::Pow64,
],
)
}
Expand Down
10 changes: 10 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ mod pc;
mod pop;
mod push;
mod selfbalance;
mod shl;
mod shr;
mod signed_comparator;
mod signextend;
mod sload;
Expand Down Expand Up @@ -102,6 +104,8 @@ use pc::PcGadget;
use pop::PopGadget;
use push::PushGadget;
use selfbalance::SelfbalanceGadget;
use shl::ShlGadget;
use shr::ShrGadget;
use signed_comparator::SignedComparatorGadget;
use signextend::SignextendGadget;
use sload::SloadGadget;
Expand Down Expand Up @@ -176,6 +180,8 @@ pub(crate) struct ExecutionConfig<F> {
pop_gadget: PopGadget<F>,
push_gadget: PushGadget<F>,
selfbalance_gadget: SelfbalanceGadget<F>,
shl_gadget: ShlGadget<F>,
shr_gadget: ShrGadget<F>,
signed_comparator_gadget: SignedComparatorGadget<F>,
signextend_gadget: SignextendGadget<F>,
sload_gadget: SloadGadget<F>,
Expand Down Expand Up @@ -365,6 +371,8 @@ impl<F: Field> ExecutionConfig<F> {
pop_gadget: configure_gadget!(),
push_gadget: configure_gadget!(),
selfbalance_gadget: configure_gadget!(),
shl_gadget: configure_gadget!(),
shr_gadget: configure_gadget!(),
signed_comparator_gadget: configure_gadget!(),
signextend_gadget: configure_gadget!(),
sload_gadget: configure_gadget!(),
Expand Down Expand Up @@ -805,6 +813,8 @@ impl<F: Field> ExecutionConfig<F> {
ExecutionState::SELFBALANCE => assign_exec_step!(self.selfbalance_gadget),
ExecutionState::SIGNEXTEND => assign_exec_step!(self.signextend_gadget),
ExecutionState::SLOAD => assign_exec_step!(self.sload_gadget),
ExecutionState::SHL => assign_exec_step!(self.shl_gadget),
ExecutionState::SHR => assign_exec_step!(self.shr_gadget),
ExecutionState::SSTORE => assign_exec_step!(self.sstore_gadget),
ExecutionState::STOP => assign_exec_step!(self.stop_gadget),
ExecutionState::SWAP => assign_exec_step!(self.swap_gadget),
Expand Down
125 changes: 125 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/shl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
math_gadget::ShlWordsGadget,
},
witness::{Block, Call, ExecStep, Transaction},
},
util::Expr,
};

use bus_mapping::evm::OpcodeId;
use eth_types::Field;
use halo2_proofs::{circuit::Region, plonk::Error};

#[derive(Clone, Debug)]
pub(crate) struct ShlGadget<F> {
same_context: SameContextGadget<F>,
shl_words: ShlWordsGadget<F>,
}

impl<F: Field> ExecutionGadget<F> for ShlGadget<F> {
const NAME: &'static str = "SHL";

const EXECUTION_STATE: ExecutionState = ExecutionState::SHL;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let a = cb.query_word();
let shift = cb.query_word();

cb.stack_pop(shift.expr());
cb.stack_pop(a.expr());
let shl_words = ShlWordsGadget::construct(cb, a, shift);
cb.stack_push(shl_words.b().expr());

let step_state_transition = StepStateTransition {
rw_counter: Delta(3.expr()),
program_counter: Delta(1.expr()),
stack_pointer: Delta(1.expr()),
gas_left: Delta(-OpcodeId::SHL.constant_gas_cost().expr()),
..Default::default()
};
let same_context = SameContextGadget::construct(cb, opcode, step_state_transition);

Self {
same_context,
shl_words,
}
}

fn assign_exec_step(
&self,
region: &mut Region<'_, F>,
offset: usize,
block: &Block<F>,
_: &Transaction,
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;
let indices = [step.rw_indices[0], step.rw_indices[1], step.rw_indices[2]];
let [shift, a, b] = indices.map(|idx| block.rws[idx].stack_value());
self.shl_words.assign(region, offset, a, shift, b)
}
}

#[cfg(test)]
mod test {
use crate::evm_circuit::test::rand_word;
use crate::test_util::run_test_circuits;
use eth_types::evm_types::OpcodeId;
use eth_types::{bytecode, Word};
use mock::TestContext;
use rand::Rng;

fn test_ok(opcode: OpcodeId, a: Word, shift: Word) {
let bytecode = bytecode! {
PUSH32(a)
PUSH32(shift)
#[start]
.write_op(opcode)
STOP
};
assert_eq!(
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
None
),
Ok(())
);
}

#[test]
fn shl_gadget_simple() {
test_ok(OpcodeId::SHL, 0x02FF.into(), 0x1.into());
}

#[test]
fn shl_gadget_rand_normal_shift() {
let a = rand_word();
let mut rng = rand::thread_rng();
let shift = rng.gen_range(0..=255);
test_ok(OpcodeId::SHL, a, shift.into());
}

#[test]
fn shl_gadget_rand_overflow_shift() {
let a = rand_word();
let shift = Word::from_big_endian(&[255u8; 32]);
test_ok(OpcodeId::SHL, a, shift);
}

//this testcase manage to check the split is correct.
#[test]
fn shl_gadget_constant_shift() {
let a = rand_word();
test_ok(OpcodeId::SHL, a, 8.into());
test_ok(OpcodeId::SHL, a, 64.into());
}
}
124 changes: 124 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/shr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta},
math_gadget::ShrWordsGadget,
},
witness::{Block, Call, ExecStep, Transaction},
},
util::Expr,
};
use bus_mapping::evm::OpcodeId;
use eth_types::Field;
use halo2_proofs::{circuit::Region, plonk::Error};

#[derive(Clone, Debug)]
pub(crate) struct ShrGadget<F> {
same_context: SameContextGadget<F>,
shr_words: ShrWordsGadget<F>,
}

impl<F: Field> ExecutionGadget<F> for ShrGadget<F> {
const NAME: &'static str = "SHR";

const EXECUTION_STATE: ExecutionState = ExecutionState::SHR;

fn configure(cb: &mut ConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();

let a = cb.query_word();
let shift = cb.query_word();

cb.stack_pop(shift.expr());
cb.stack_pop(a.expr());
let shr_words = ShrWordsGadget::construct(cb, a, shift);
cb.stack_push(shr_words.b().expr());

let step_state_transition = StepStateTransition {
rw_counter: Delta(3.expr()),
program_counter: Delta(1.expr()),
stack_pointer: Delta(1.expr()),
gas_left: Delta(-OpcodeId::SHR.constant_gas_cost().expr()),
..Default::default()
};
let same_context = SameContextGadget::construct(cb, opcode, step_state_transition);

Self {
same_context,
shr_words,
}
}

fn assign_exec_step(
&self,
region: &mut Region<'_, F>,
offset: usize,
block: &Block<F>,
_: &Transaction,
_: &Call,
step: &ExecStep,
) -> Result<(), Error> {
self.same_context.assign_exec_step(region, offset, step)?;
let indices = [step.rw_indices[0], step.rw_indices[1], step.rw_indices[2]];
let [shift, a, b] = indices.map(|idx| block.rws[idx].stack_value());
self.shr_words.assign(region, offset, a, shift, b)
}
}

#[cfg(test)]
mod test {
use crate::evm_circuit::test::rand_word;
use crate::test_util::run_test_circuits;
use eth_types::evm_types::OpcodeId;
use eth_types::{bytecode, Word};
use mock::TestContext;
use rand::Rng;

fn test_ok(opcode: OpcodeId, a: Word, shift: Word) {
let bytecode = bytecode! {
PUSH32(a)
PUSH32(shift)
#[start]
.write_op(opcode)
STOP
};
assert_eq!(
run_test_circuits(
TestContext::<2, 1>::simple_ctx_with_bytecode(bytecode).unwrap(),
None
),
Ok(())
);
}

#[test]
fn shr_gadget_simple() {
test_ok(OpcodeId::SHR, 0x02FF.into(), 0x1.into());
}

#[test]
fn shr_gadget_rand_normal_shift() {
let a = rand_word();
let mut rng = rand::thread_rng();
let shift = rng.gen_range(0..=255);
test_ok(OpcodeId::SHR, a, shift.into());
}

#[test]
fn shr_gadget_rand_overflow_shift() {
let a = rand_word();
let shift = Word::from_big_endian(&[255u8; 32]);
test_ok(OpcodeId::SHR, a, shift);
}

//this testcase manage to check the split is correct.
#[test]
fn shr_gadget_constant_shift() {
let a = rand_word();
test_ok(OpcodeId::SHR, a, 8.into());
test_ok(OpcodeId::SHR, a, 64.into());
}
}
15 changes: 15 additions & 0 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum FixedTableTag {
BitwiseOr,
BitwiseXor,
ResponsibleOpcode,
Bitslevel,
Pow64,
}

impl FixedTableTag {
Expand All @@ -58,6 +60,8 @@ impl FixedTableTag {
Self::BitwiseOr,
Self::BitwiseXor,
Self::ResponsibleOpcode,
Self::Bitslevel,
Self::Pow64,
]
.iter()
.copied()
Expand Down Expand Up @@ -120,6 +124,17 @@ impl FixedTableTag {
})
}))
}
Self::Bitslevel => Box::new((0..9).flat_map(move |level| {
(0..(1 << level)).map(move |idx| [tag, F::from(level), F::from(idx), F::zero()])
})),
Self::Pow64 => Box::new((0..64).map(move |idx| {
[
tag,
F::from(idx),
F::from_u128(1u128 << idx),
F::from_u128(1u128 << (64 - idx)),
]
})),
}
}
}
Expand Down
Loading