plonky2/evm/src/memory/segments.rs

171 lines
6.8 KiB
Rust
Raw Normal View History

#[allow(dead_code)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
2023-02-15 18:18:26 -08:00
pub enum Segment {
/// Contains EVM bytecode.
Code = 0,
/// The program stack.
Stack = 1,
/// Main memory, owned by the contract code.
MainMemory = 2,
/// Data passed to the current context by its caller.
Calldata = 3,
/// Data returned to the current context by its latest callee.
Returndata = 4,
/// A segment which contains a few fixed-size metadata fields, such as the caller's context, or the
/// size of `CALLDATA` and `RETURNDATA`.
GlobalMetadata = 5,
ContextMetadata = 6,
/// General purpose kernel memory, used by various kernel functions.
/// In general, calling a helper function can result in this memory being clobbered.
KernelGeneral = 7,
2022-10-05 20:31:36 -07:00
/// Another segment for general purpose kernel use.
KernelGeneral2 = 8,
2022-10-21 18:00:41 +02:00
/// Segment to hold account code for opcodes like `CODESIZE, CODECOPY,...`.
KernelAccountCode = 9,
/// Contains normalized transaction fields; see `NormalizedTxnField`.
2022-10-21 18:00:41 +02:00
TxnFields = 10,
/// Contains the data field of a transaction.
2022-10-21 18:00:41 +02:00
TxnData = 11,
/// A buffer used to hold raw RLP data.
2022-10-21 18:00:41 +02:00
RlpRaw = 12,
/// Contains all trie data. Tries are stored as immutable, copy-on-write trees, so this is an
/// append-only buffer. It is owned by the kernel, so it only lives on context 0.
2022-10-21 18:00:41 +02:00
TrieData = 13,
2022-10-11 08:46:40 -07:00
/// A buffer used to store the encodings of a branch node's children.
2022-10-21 18:00:41 +02:00
TrieEncodedChild = 14,
2022-10-11 08:46:40 -07:00
/// A buffer used to store the lengths of the encodings of a branch node's children.
2022-10-21 18:00:41 +02:00
TrieEncodedChildLen = 15,
/// A table of values 2^i for i=0..255 for use with shift
/// instructions; initialised by `kernel/asm/shift.asm::init_shift_table()`.
ShiftTable = 16,
2022-11-20 12:46:15 -08:00
JumpdestBits = 17,
EcdsaTable = 18,
BnWnafA = 19,
BnWnafB = 20,
BnTableQ = 21,
2023-02-13 12:32:40 -08:00
BnPairing = 22,
/// List of addresses that have been accessed in the current transaction.
AccessedAddresses = 23,
/// List of storage keys that have been accessed in the current transaction.
AccessedStorageKeys = 24,
/// List of addresses that have called SELFDESTRUCT in the current transaction.
2023-04-01 07:29:22 +02:00
SelfDestructList = 25,
/// Journal of state changes. List of pointers to `JournalData`. Length in `GlobalMetadata`.
Journal = 26,
JournalData = 27,
JournalCheckpoints = 28,
/// List of addresses that have been touched in the current transaction.
TouchedAddresses = 29,
}
2022-07-02 23:17:33 -07:00
impl Segment {
pub(crate) const COUNT: usize = 30;
2022-07-02 23:17:33 -07:00
pub(crate) fn all() -> [Self; Self::COUNT] {
[
Self::Code,
Self::Stack,
Self::MainMemory,
Self::Calldata,
Self::Returndata,
Self::GlobalMetadata,
Self::ContextMetadata,
Self::KernelGeneral,
2022-10-05 20:31:36 -07:00
Self::KernelGeneral2,
2022-10-21 18:00:41 +02:00
Self::KernelAccountCode,
Self::TxnFields,
Self::TxnData,
Self::RlpRaw,
Self::TrieData,
2022-10-11 08:46:40 -07:00
Self::TrieEncodedChild,
Self::TrieEncodedChildLen,
Self::ShiftTable,
2022-11-20 12:46:15 -08:00
Self::JumpdestBits,
Self::EcdsaTable,
Self::BnWnafA,
Self::BnWnafB,
Self::BnTableQ,
2023-02-13 12:32:40 -08:00
Self::BnPairing,
Self::AccessedAddresses,
Self::AccessedStorageKeys,
2023-04-01 07:29:22 +02:00
Self::SelfDestructList,
Self::Journal,
Self::JournalData,
Self::JournalCheckpoints,
Self::TouchedAddresses,
]
}
/// The variable name that gets passed into kernel assembly code.
pub(crate) fn var_name(&self) -> &'static str {
match self {
Segment::Code => "SEGMENT_CODE",
Segment::Stack => "SEGMENT_STACK",
Segment::MainMemory => "SEGMENT_MAIN_MEMORY",
Segment::Calldata => "SEGMENT_CALLDATA",
Segment::Returndata => "SEGMENT_RETURNDATA",
Segment::GlobalMetadata => "SEGMENT_GLOBAL_METADATA",
Segment::ContextMetadata => "SEGMENT_CONTEXT_METADATA",
Segment::KernelGeneral => "SEGMENT_KERNEL_GENERAL",
2022-10-05 20:31:36 -07:00
Segment::KernelGeneral2 => "SEGMENT_KERNEL_GENERAL_2",
2022-10-21 18:00:41 +02:00
Segment::KernelAccountCode => "SEGMENT_KERNEL_ACCOUNT_CODE",
Segment::TxnFields => "SEGMENT_NORMALIZED_TXN",
Segment::TxnData => "SEGMENT_TXN_DATA",
Segment::RlpRaw => "SEGMENT_RLP_RAW",
Segment::TrieData => "SEGMENT_TRIE_DATA",
2022-10-11 08:46:40 -07:00
Segment::TrieEncodedChild => "SEGMENT_TRIE_ENCODED_CHILD",
Segment::TrieEncodedChildLen => "SEGMENT_TRIE_ENCODED_CHILD_LEN",
Segment::ShiftTable => "SEGMENT_SHIFT_TABLE",
2022-11-20 12:46:15 -08:00
Segment::JumpdestBits => "SEGMENT_JUMPDEST_BITS",
Segment::EcdsaTable => "SEGMENT_KERNEL_ECDSA_TABLE",
Segment::BnWnafA => "SEGMENT_KERNEL_BN_WNAF_A",
Segment::BnWnafB => "SEGMENT_KERNEL_BN_WNAF_B",
Segment::BnTableQ => "SEGMENT_KERNEL_BN_TABLE_Q",
2023-02-13 12:32:40 -08:00
Segment::BnPairing => "SEGMENT_KERNEL_BN_PAIRING",
Segment::AccessedAddresses => "SEGMENT_ACCESSED_ADDRESSES",
Segment::AccessedStorageKeys => "SEGMENT_ACCESSED_STORAGE_KEYS",
2023-04-01 07:29:22 +02:00
Segment::SelfDestructList => "SEGMENT_SELFDESTRUCT_LIST",
Segment::Journal => "SEGMENT_JOURNAL",
Segment::JournalData => "SEGMENT_JOURNAL_DATA",
Segment::JournalCheckpoints => "SEGMENT_JOURNAL_CHECKPOINTS",
Segment::TouchedAddresses => "SEGMENT_TOUCHED_ADDRESSES",
}
}
2022-07-25 10:34:18 +02:00
#[allow(dead_code)]
pub(crate) fn bit_range(&self) -> usize {
match self {
Segment::Code => 8,
Segment::Stack => 256,
Segment::MainMemory => 8,
Segment::Calldata => 8,
Segment::Returndata => 8,
Segment::GlobalMetadata => 256,
Segment::ContextMetadata => 256,
2022-07-27 10:05:31 +02:00
Segment::KernelGeneral => 256,
2022-10-05 20:31:36 -07:00
Segment::KernelGeneral2 => 256,
2022-10-27 14:57:17 +02:00
Segment::KernelAccountCode => 8,
2022-07-27 10:16:04 +02:00
Segment::TxnFields => 256,
2023-04-08 09:06:02 +02:00
Segment::TxnData => 8,
Segment::RlpRaw => 8,
Segment::TrieData => 256,
2022-10-11 08:46:40 -07:00
Segment::TrieEncodedChild => 256,
Segment::TrieEncodedChildLen => 6,
Segment::ShiftTable => 256,
2022-11-20 12:46:15 -08:00
Segment::JumpdestBits => 1,
Segment::EcdsaTable => 256,
Segment::BnWnafA => 8,
Segment::BnWnafB => 8,
Segment::BnTableQ => 256,
2023-02-13 12:32:40 -08:00
Segment::BnPairing => 256,
Segment::AccessedAddresses => 256,
Segment::AccessedStorageKeys => 256,
2023-04-01 07:29:22 +02:00
Segment::SelfDestructList => 256,
Segment::Journal => 256,
Segment::JournalData => 256,
Segment::JournalCheckpoints => 256,
Segment::TouchedAddresses => 256,
}
}
}