This repository was archived by the owner on Apr 18, 2025. It is now read-only.
forked from privacy-ethereum/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 387
Opcode SHL
#157
Closed
Closed
Opcode SHL
#157
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1714ac5
Add `ShlGadget`.
silathdiir 558fd36
Add `ShlWordsGadget` to `math_gadget.rs`.
silathdiir 4dd4f9b
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir 85686bb
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir 7cbbe28
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir d738dd4
Merge both opcode `SHL` and `SHR` into `MulDivModShlShrGadget.
silathdiir eb89a00
Add test cases.
silathdiir a57c20e
Delete old `SHL` and `SHR` gadgets.
silathdiir df635e7
Use `generate_lagrange_base_polynomial` to check opcode (as `is_mul`)
silathdiir 0f59351
Add a constraint `pop1 == pop1.cells[0] when divisor != 0 for opcode …
silathdiir 7d187ed
Add `shf_mod64` and `shf_div64` lookup constraints for opcode `SHL` and
silathdiir 45a37bd
Fix Infra doc issue.
silathdiir dbcb834
Fix `shf_div64` and `shf_mod64` to `shf_div128` and `shf_mod128`.
silathdiir de5caed
Delete commenting for tests.
silathdiir a0c98b3
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir b0655f1
Fix contraint `divisor_hi == shf_div128`.
silathdiir 51bd0a2
Fix clippy issue.
silathdiir b99dc14
Fix comments.
silathdiir d450aaf
Fix to `shf_lo` and `shf_hi`.
silathdiir 926a4da
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir c763b78
Merge remote-tracking branch 'origin/main' into feat/opcode-shl
silathdiir 98c0ef3
Fix `Pow2` lookup table and update to use `shf0` for lookup.
silathdiir eaba996
Add missing limit `divisor != 0` for `Pow2` lookup.
silathdiir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use crate::{ | ||
| evm_circuit::{ | ||
| execution::ExecutionGadget, | ||
| step::ExecutionState, | ||
| util::{ | ||
| common_gadget::SameContextGadget, | ||
| constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta}, | ||
| math_gadget::ShlWordsGadget, | ||
| CachedRegion, | ||
| }, | ||
| witness::{Block, Call, ExecStep, Transaction}, | ||
| }, | ||
| util::Expr, | ||
| }; | ||
| use bus_mapping::evm::OpcodeId; | ||
| use eth_types::Field; | ||
| use halo2_proofs::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 CachedRegion<'_, '_, 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, Word::from(0xABCD) << 240, 8.into()); | ||
| test_ok(OpcodeId::SHL, Word::from(0x1234) << 240, 7.into()); | ||
| test_ok(OpcodeId::SHL, Word::from(0x8765) << 240, 17.into()); | ||
| test_ok(OpcodeId::SHL, Word::from(0x4321) << 240, 0.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(&[255_u8; 32]); | ||
| test_ok(OpcodeId::SHL, a, shift); | ||
| } | ||
|
|
||
| // This case validates if 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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.