This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 841
Circuit for opcode TIMESTAMP #307
Merged
ed255
merged 11 commits into
privacy-ethereum:main
from
scroll-tech:feat/opcode-timestamp
Feb 2, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db23162
Update StackOnlyOpcode to allow N=0 and use it for COINBASE op code.
5047ad8
Add op code implementation for TIMESTAMP
27f977c
Fix typos
8a98e4e
Add TimestampGadget
5833d6d
Set mock block author to be a non-default value.
de70e76
cleanup
27dbcca
Fix module name
192ca32
Don't change time witness type
58d488a
fix tests
lispc 7518c31
Simplify code with query_rlc
f83788a
Merge branch 'main' of https://github.com/appliedzkp/zkevm-circuits i…
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
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,62 @@ | ||
| #[cfg(test)] | ||
| mod timestamp_tests { | ||
| use crate::{ | ||
| circuit_input_builder::{ExecStep, TransactionContext}, | ||
| mock::BlockData, | ||
| operation::RW, | ||
| Error, | ||
| }; | ||
| use eth_types::{bytecode, evm_types::StackAddress}; | ||
| use mock::new_single_tx_trace_code_at_start; | ||
| use pretty_assertions::assert_eq; | ||
|
|
||
| #[test] | ||
| fn timestamp_opcode_impl() -> Result<(), Error> { | ||
| let code = bytecode! { | ||
| #[start] | ||
| TIMESTAMP | ||
| STOP | ||
| }; | ||
|
|
||
| // Get the execution steps from the external tracer | ||
| let block = | ||
| BlockData::new_from_geth_data(new_single_tx_trace_code_at_start(&code).unwrap()); | ||
|
|
||
| let mut builder = block.new_circuit_input_builder(); | ||
| builder.handle_tx(&block.eth_tx, &block.geth_trace).unwrap(); | ||
|
|
||
| let mut test_builder = block.new_circuit_input_builder(); | ||
| let mut tx = test_builder.new_tx(&block.eth_tx).unwrap(); | ||
| let mut tx_ctx = TransactionContext::new(&block.eth_tx); | ||
|
|
||
| // Generate step corresponding to TIMESTAMP | ||
| let mut step = ExecStep::new( | ||
| &block.geth_trace.struct_logs[0], | ||
| 0, | ||
| test_builder.block_ctx.rwc, | ||
| 0, | ||
| ); | ||
| let mut state_ref = test_builder.state_ref(&mut tx, &mut tx_ctx, &mut step); | ||
|
|
||
| // Add the last Stack write | ||
| state_ref.push_stack_op( | ||
| RW::WRITE, | ||
| StackAddress::from(1024 - 1), | ||
| block.b_constant.timestamp, | ||
| ); | ||
|
|
||
| tx.steps_mut().push(step); | ||
| test_builder.block.txs_mut().push(tx); | ||
|
|
||
| // Compare first step bus mapping instance | ||
| assert_eq!( | ||
| builder.block.txs()[0].steps()[0].bus_mapping_instance, | ||
| test_builder.block.txs()[0].steps()[0].bus_mapping_instance, | ||
| ); | ||
|
|
||
| // Compare containers | ||
| assert_eq!(builder.block.container, test_builder.block.container); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
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,97 @@ | ||
| use crate::evm_circuit::param::N_BYTES_U64; | ||
| use crate::{ | ||
| evm_circuit::{ | ||
| execution::ExecutionGadget, | ||
| step::ExecutionState, | ||
| table::BlockContextFieldTag, | ||
| util::{ | ||
| common_gadget::SameContextGadget, | ||
| constraint_builder::{ConstraintBuilder, StepStateTransition, Transition::Delta}, | ||
| from_bytes, RandomLinearCombination, | ||
| }, | ||
| witness::{Block, Call, ExecStep, Transaction}, | ||
| }, | ||
| util::Expr, | ||
| }; | ||
| use halo2::{arithmetic::FieldExt, circuit::Region, plonk::Error}; | ||
| use std::convert::TryFrom; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct TimestampGadget<F> { | ||
| same_context: SameContextGadget<F>, | ||
| timestamp: RandomLinearCombination<F, N_BYTES_U64>, | ||
| } | ||
|
|
||
| impl<F: FieldExt> ExecutionGadget<F> for TimestampGadget<F> { | ||
| const NAME: &'static str = "TIMESTAMP"; | ||
|
|
||
| const EXECUTION_STATE: ExecutionState = ExecutionState::TIMESTAMP; | ||
|
|
||
| fn configure(cb: &mut ConstraintBuilder<F>) -> Self { | ||
| let timestamp = cb.query_rlc(); | ||
| cb.stack_push(timestamp.expr()); | ||
|
|
||
| // Lookup block table with timestamp | ||
| cb.block_lookup( | ||
| BlockContextFieldTag::Time.expr(), | ||
| None, | ||
| from_bytes::expr(×tamp.cells), | ||
| ); | ||
|
|
||
| // State transition | ||
| let opcode = cb.query_cell(); | ||
| let step_state_transition = StepStateTransition { | ||
| rw_counter: Delta(1.expr()), | ||
| program_counter: Delta(1.expr()), | ||
| stack_pointer: Delta((-1).expr()), | ||
| ..Default::default() | ||
| }; | ||
| let same_context = SameContextGadget::construct(cb, opcode, step_state_transition, None); | ||
|
|
||
| Self { | ||
| same_context, | ||
| timestamp, | ||
| } | ||
| } | ||
|
|
||
| fn assign_exec_step( | ||
| &self, | ||
| region: &mut Region<'_, F>, | ||
| offset: usize, | ||
| block: &Block<F>, | ||
| _: &Transaction<F>, | ||
| _: &Call<F>, | ||
| step: &ExecStep, | ||
| ) -> Result<(), Error> { | ||
| self.same_context.assign_exec_step(region, offset, step)?; | ||
|
|
||
| let timestamp = block.rws[step.rw_indices[0]].stack_value(); | ||
|
|
||
| self.timestamp.assign( | ||
| region, | ||
| offset, | ||
| Some(u64::try_from(timestamp).unwrap().to_le_bytes()), | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::test_util::run_test_circuits; | ||
| use eth_types::bytecode; | ||
|
|
||
| fn test_ok() { | ||
| let bytecode = bytecode! { | ||
| #[start] | ||
| TIMESTAMP | ||
| STOP | ||
| }; | ||
| assert_eq!(run_test_circuits(bytecode), Ok(())); | ||
| } | ||
| #[test] | ||
| fn timestamp_gadget_test() { | ||
| test_ok(); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! :)