plonky2/evm/src/memory/segments.rs

73 lines
2.4 KiB
Rust
Raw Normal View History

#[allow(dead_code)] // TODO: Not all segments are used yet.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
pub(crate) 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`.
Metadata = 5,
/// General purpose kernel memory, used by various kernel functions.
/// In general, calling a helper function can result in this memory being clobbered.
KernelGeneral = 6,
/// Contains transaction data (after it's parsed and converted to a standard format).
TxnData = 7,
/// Raw RLP data.
RlpRaw = 8,
}
2022-07-02 23:17:33 -07:00
impl Segment {
2022-07-20 15:05:09 -07:00
pub(crate) const COUNT: usize = 9;
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::Metadata,
Self::KernelGeneral,
Self::TxnData,
Self::RlpRaw,
]
}
/// 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::Metadata => "SEGMENT_METADATA",
Segment::KernelGeneral => "SEGMENT_KERNEL_GENERAL",
Segment::TxnData => "SEGMENT_TXN_DATA",
Segment::RlpRaw => "SEGMENT_RLP_RAW",
}
}
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::Metadata => 8,
Segment::KernelGeneral => 8,
Segment::TxnData => 8,
Segment::RlpRaw => 8,
}
}
}