zkevm_opcode_defs/definitions/
context.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use super::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ContextOpcode {
    This = 0,
    Caller,
    CodeAddress,
    Meta,
    ErgsLeft,
    Sp,
    GetContextU128,
    SetContextU128,
    AuxMutating0,
    IncrementTxNumber,
}

impl OpcodeVariantProps for ContextOpcode {
    fn all_variants() -> Vec<Self> {
        vec![
            ContextOpcode::This,
            ContextOpcode::Caller,
            ContextOpcode::CodeAddress,
            ContextOpcode::Meta,
            ContextOpcode::ErgsLeft,
            ContextOpcode::Sp,
            ContextOpcode::GetContextU128,
            ContextOpcode::SetContextU128,
            ContextOpcode::AuxMutating0,
            ContextOpcode::IncrementTxNumber,
        ]
    }

    fn max_variant_idx_for_version(_version: ISAVersion) -> usize {
        ContextOpcode::IncrementTxNumber.variant_index()
    }

    fn minimal_version(&self) -> ISAVersion {
        ALL_ISA_VERSIONS[0]
    }

    fn variant_index(&self) -> usize {
        (*self as u8) as usize
    }

    fn from_variant_index_for_version(index: usize, _version: &ISAVersion) -> Option<Self> {
        match index {
            i if i == ContextOpcode::This.variant_index() => Some(ContextOpcode::This),
            i if i == ContextOpcode::Caller.variant_index() => Some(ContextOpcode::Caller),
            i if i == ContextOpcode::CodeAddress.variant_index() => {
                Some(ContextOpcode::CodeAddress)
            }
            i if i == ContextOpcode::Meta.variant_index() => Some(ContextOpcode::Meta),
            i if i == ContextOpcode::ErgsLeft.variant_index() => Some(ContextOpcode::ErgsLeft),
            i if i == ContextOpcode::Sp.variant_index() => Some(ContextOpcode::Sp),
            i if i == ContextOpcode::GetContextU128.variant_index() => {
                Some(ContextOpcode::GetContextU128)
            }
            i if i == ContextOpcode::SetContextU128.variant_index() => {
                Some(ContextOpcode::SetContextU128)
            }
            i if i == ContextOpcode::AuxMutating0.variant_index() => {
                Some(ContextOpcode::AuxMutating0)
            }
            i if i == ContextOpcode::IncrementTxNumber.variant_index() => {
                Some(ContextOpcode::IncrementTxNumber)
            }
            _ => None,
        }
    }

    fn ergs_price(&self) -> u32 {
        AVERAGE_OPCODE_ERGS
    }
}

impl OpcodeProps for ContextOpcode {
    fn name(&self) -> &'static str {
        "Context opcode"
    }
    fn variants_data(&self, version: ISAVersion) -> Vec<OpcodeVariantData> {
        match version {
            ISAVersion(0) => {
                full_variants_product(0..=Self::max_variant_idx_for_version(version), 0, 0)
            }
            ISAVersion(1) => {
                full_variants_product(0..=Self::max_variant_idx_for_version(version), 0, 0)
            }
            ISAVersion(2) => {
                full_variants_product(0..=Self::max_variant_idx_for_version(version), 0, 0)
            }
            _ => unimplemented!(),
        }
    }
    fn max_variant_idx(&self, _version: ISAVersion) -> usize {
        ContextOpcode::IncrementTxNumber.variant_index()
    }
    fn input_operands(&self, _version: ISAVersion) -> Vec<Operand> {
        match self {
            ContextOpcode::SetContextU128 | ContextOpcode::AuxMutating0 => {
                vec![Operand::RegOnly]
            }
            _ => vec![],
        }
    }
    fn output_operands(&self, _version: ISAVersion) -> Vec<Operand> {
        match self {
            ContextOpcode::SetContextU128
            | ContextOpcode::AuxMutating0
            | ContextOpcode::IncrementTxNumber => vec![],
            _ => vec![Operand::RegOnly],
        }
    }
    fn requires_kernel_mode(&self) -> bool {
        match self {
            ContextOpcode::SetContextU128
            | ContextOpcode::AuxMutating0
            | ContextOpcode::IncrementTxNumber => true,
            _ => false,
        }
    }
    fn can_be_used_in_static_context(&self) -> bool {
        match self {
            ContextOpcode::SetContextU128
            | ContextOpcode::AuxMutating0
            | ContextOpcode::IncrementTxNumber => false,
            _ => true,
        }
    }

    fn src0_can_be_pointer(&self) -> bool {
        false
    }

    fn src1_can_be_pointer(&self) -> bool {
        false
    }
}