zk_evm 0.153.10

ZKsync out-of-circuit EraEVM implementation
Documentation
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use super::*;

use crate::zkevm_opcode_defs::{LogOpcode, Opcode, PrecompileCallABI, FIRST_MESSAGE_FLAG_IDX};
use num::abs;
use zk_evm_abstractions::aux::PubdataCost;
use zk_evm_abstractions::queries::LogQuery;
use zk_evm_abstractions::zkevm_opcode_defs::system_params::{
    MAX_PUBDATA_COST_PER_QUERY, TRANSIENT_STORAGE_AUX_BYTE,
};
use zk_evm_abstractions::zkevm_opcode_defs::{
    FatPointer, OpcodeVariantProps, PrecompileAuxData, VersionedHashHeader,
    VersionedHashNormalizedPreimage,
};

use crate::zkevm_opcode_defs::system_params::{
    EVENT_AUX_BYTE, L1_MESSAGE_AUX_BYTE, PRECOMPILE_AUX_BYTE, STORAGE_AUX_BYTE,
};

impl<const N: usize, E: VmEncodingMode<N>> DecodedOpcode<N, E> {
    pub fn log_opcode_apply<
        S: zk_evm_abstractions::vm::Storage,
        M: zk_evm_abstractions::vm::Memory,
        EV: zk_evm_abstractions::vm::EventSink,
        PP: zk_evm_abstractions::vm::PrecompilesProcessor,
        DP: zk_evm_abstractions::vm::DecommittmentProcessor,
        WT: crate::witness_trace::VmWitnessTracer<N, E>,
    >(
        &self,
        vm_state: &mut VmState<S, M, EV, PP, DP, WT, N, E>,
        prestate: PreState<N, E>,
    ) -> anyhow::Result<()> {
        let PreState {
            src0,
            src1,
            dst0_mem_location,
            new_pc,
            ..
        } = prestate;
        let PrimitiveValue {
            value: src0,
            is_pointer: _,
        } = src0;
        let PrimitiveValue {
            value: src1,
            is_pointer: _,
        } = src1;
        let inner_variant = match self.variant.opcode {
            Opcode::Log(inner) => inner,
            _ => unreachable!(),
        };
        vm_state.local_state.callstack.get_current_stack_mut().pc = new_pc;
        let is_first_message = self.variant.flags[FIRST_MESSAGE_FLAG_IDX];

        // this is the only case where we do extra checking for costs as it's related to pubdata
        // and shard_id

        // We do it as the following:
        // - check if we have enough
        // - if not - just set remaining ergs to 0 (that will cause an exception on the next cycle)
        // - DO NOT set any pending
        // - return

        // ergs exception handling
        let shard_id = vm_state
            .local_state
            .callstack
            .get_current_stack()
            .this_shard_id;
        let ergs_available = vm_state
            .local_state
            .callstack
            .get_current_stack()
            .ergs_remaining;
        let is_rollup = shard_id == 0;

        let timestamp_for_log = vm_state.timestamp_for_first_decommit_or_precompile_read();
        let tx_number_in_block = vm_state.local_state.tx_number_in_block;

        let mut decommit_preimage_format_is_invalid = false;
        // we make formal fat pointer of length of full kernel space "free" heap size,
        // and caller is responsible to truncate it if decommit is succesfull
        let mut preimage_len_in_bytes =
            zkevm_opcode_defs::system_params::NEW_KERNEL_FRAME_MEMORY_STIPEND;
        let mut decommit_header = VersionedHashHeader::default();
        let mut decommit_preimage_normalized = VersionedHashNormalizedPreimage::default();
        let mut buffer = [0u8; 32];

        let extra_cost = match inner_variant {
            LogOpcode::PrecompileCall => {
                let precompile_aux_data = PrecompileAuxData::from_u256(src1);

                precompile_aux_data.extra_ergs_cost
            }
            LogOpcode::Decommit => {
                // extra cost is in src1
                let extra_cost = src1.low_u32();

                // and we check format anyway
                use crate::zkevm_opcode_defs::*;
                src0.to_big_endian(&mut buffer);
                if ContractCodeSha256Format::is_valid(&buffer) {
                    let (header, normalized_preimage) =
                        ContractCodeSha256Format::normalize_for_decommitment(&buffer);
                    decommit_header = header;
                    decommit_preimage_normalized = normalized_preimage;
                } else if BlobSha256Format::is_valid(&buffer) {
                    let (header, normalized_preimage) =
                        BlobSha256Format::normalize_for_decommitment(&buffer);
                    decommit_header = header;
                    decommit_preimage_normalized = normalized_preimage;
                } else {
                    preimage_len_in_bytes = 0;
                    decommit_preimage_format_is_invalid = true;
                };

                extra_cost
            }
            _ => 0,
        };

        let (ergs_remaining, not_enough_power) = ergs_available.overflowing_sub(extra_cost);
        if not_enough_power {
            vm_state
                .local_state
                .callstack
                .get_current_stack_mut()
                .ergs_remaining = 0;
        } else {
            vm_state
                .local_state
                .callstack
                .get_current_stack_mut()
                .ergs_remaining = ergs_remaining;
        }

        let current_context = vm_state.local_state.callstack.get_current_stack();
        let address = current_context.this_address;
        let shard_id = current_context.this_shard_id;

        #[allow(dropping_references)]
        drop(current_context);

        let (pubdata_to_add_to_current_frame, ergs_refund) = match inner_variant {
            variant @ LogOpcode::StorageRead | variant @ LogOpcode::TransientStorageRead => {
                assert!(not_enough_power == false);
                let key = src0;

                let aux_byte = if variant == LogOpcode::StorageRead {
                    STORAGE_AUX_BYTE
                } else if variant == LogOpcode::TransientStorageRead {
                    TRANSIENT_STORAGE_AUX_BYTE
                } else {
                    unreachable!()
                };

                let partial_query = LogQuery {
                    timestamp: timestamp_for_log,
                    tx_number_in_block,
                    aux_byte,
                    shard_id,
                    address,
                    key,
                    read_value: U256::zero(),
                    written_value: U256::zero(),
                    rw_flag: false,
                    rollback: false,
                    is_service: is_first_message,
                };

                let refund = vm_state.refund_for_partial_query(
                    vm_state.local_state.monotonic_cycle_counter,
                    &partial_query,
                );
                let ergs_refund = refund.refund();

                let (query, pubdata_cost) = vm_state
                    .access_storage(vm_state.local_state.monotonic_cycle_counter, partial_query);

                if variant == LogOpcode::TransientStorageRead {
                    assert_eq!(ergs_refund, 0);
                } else {
                    assert!(ergs_refund <= LogOpcode::StorageRead.ergs_price());
                }
                assert_eq!(pubdata_cost.0, 0);

                let result = PrimitiveValue {
                    value: query.read_value,
                    is_pointer: false,
                };
                vm_state.perform_dst0_update(
                    vm_state.local_state.monotonic_cycle_counter,
                    result,
                    dst0_mem_location,
                    self,
                );

                (pubdata_cost, ergs_refund)
            }
            variant @ LogOpcode::StorageWrite | variant @ LogOpcode::TransientStorageWrite => {
                if not_enough_power {
                    // we can return immediatelly and do not need to update regs
                    return Ok(());
                }
                let key = src0;
                let written_value = src1;

                let aux_byte = if variant == LogOpcode::StorageWrite {
                    STORAGE_AUX_BYTE
                } else if variant == LogOpcode::TransientStorageWrite {
                    TRANSIENT_STORAGE_AUX_BYTE
                } else {
                    unreachable!()
                };

                let partial_query = LogQuery {
                    timestamp: timestamp_for_log,
                    tx_number_in_block,
                    aux_byte,
                    shard_id,
                    address,
                    key,
                    read_value: U256::zero(),
                    written_value,
                    rw_flag: true,
                    rollback: false,
                    is_service: is_first_message,
                };

                let refund = vm_state.refund_for_partial_query(
                    vm_state.local_state.monotonic_cycle_counter,
                    &partial_query,
                );
                let ergs_refund = refund.refund();

                let (_query, pubdata_cost) = vm_state
                    .access_storage(vm_state.local_state.monotonic_cycle_counter, partial_query);

                if variant == LogOpcode::TransientStorageWrite {
                    assert_eq!(pubdata_cost.0, 0);
                    assert_eq!(ergs_refund, 0);
                } else {
                    assert!(abs(pubdata_cost.0) <= MAX_PUBDATA_COST_PER_QUERY);
                    assert!(ergs_refund <= LogOpcode::StorageWrite.ergs_price());
                }

                if is_rollup == false {
                    assert_eq!(pubdata_cost.0, 0);
                }

                (pubdata_cost, ergs_refund)
            }
            variant @ LogOpcode::Event | variant @ LogOpcode::ToL1Message => {
                if not_enough_power {
                    assert_eq!(variant, LogOpcode::ToL1Message);
                    // we do not add anything into log and do not need to update
                    // registers
                    return Ok(());
                }
                let key = src0;
                let written_value = src1;

                let aux_byte = if variant == LogOpcode::Event {
                    EVENT_AUX_BYTE
                } else {
                    L1_MESSAGE_AUX_BYTE
                };

                let pubdata_cost = if variant == LogOpcode::Event {
                    PubdataCost(0i32)
                } else {
                    // those messages are reactive and may contain pubdata from different source, so
                    // we do not count them here
                    PubdataCost(0i32)
                };

                let query = LogQuery {
                    timestamp: timestamp_for_log,
                    tx_number_in_block,
                    aux_byte,
                    shard_id,
                    address,
                    key,
                    read_value: U256::zero(),
                    written_value,
                    rw_flag: true,
                    rollback: false,
                    is_service: is_first_message,
                };
                vm_state.emit_event(vm_state.local_state.monotonic_cycle_counter, query);

                (pubdata_cost, 0)
            }
            LogOpcode::PrecompileCall => {
                // add extra information about precompile abi in the "key" field
                if not_enough_power {
                    // we have to update register
                    vm_state.perform_dst0_update(
                        vm_state.local_state.monotonic_cycle_counter,
                        PrimitiveValue::empty(),
                        dst0_mem_location,
                        self,
                    );

                    (PubdataCost(0), 0)
                } else {
                    let mut precompile_abi = PrecompileCallABI::from_u256(src0);
                    let precompile_aux_data = PrecompileAuxData::from_u256(src1);

                    // normal execution
                    vm_state
                        .local_state
                        .callstack
                        .get_current_stack_mut()
                        .ergs_remaining = ergs_remaining;
                    if precompile_abi.memory_page_to_read == 0 {
                        let memory_page_to_read = heap_page_from_base(
                            vm_state
                                .local_state
                                .callstack
                                .get_current_stack()
                                .base_memory_page,
                        );
                        precompile_abi.memory_page_to_read = memory_page_to_read.0;
                    }

                    if precompile_abi.memory_page_to_write == 0 {
                        let memory_page_to_write = heap_page_from_base(
                            vm_state
                                .local_state
                                .callstack
                                .get_current_stack()
                                .base_memory_page,
                        );
                        precompile_abi.memory_page_to_write = memory_page_to_write.0;
                    }

                    let timestamp_to_read =
                        vm_state.timestamp_for_first_decommit_or_precompile_read();
                    debug_assert!(timestamp_to_read == timestamp_for_log);
                    let timestamp_to_write =
                        vm_state.timestamp_for_second_decommit_or_precompile_write();
                    debug_assert!(timestamp_to_read.0 + 1 == timestamp_to_write.0);

                    let precompile_abi_encoded = precompile_abi.to_u256();

                    let query = LogQuery {
                        timestamp: timestamp_for_log,
                        tx_number_in_block,
                        aux_byte: PRECOMPILE_AUX_BYTE,
                        shard_id,
                        address,
                        key: precompile_abi_encoded,
                        read_value: U256::zero(),
                        written_value: U256::zero(),
                        rw_flag: false,
                        rollback: false,
                        is_service: is_first_message,
                    };

                    vm_state.call_precompile(vm_state.local_state.monotonic_cycle_counter, query);
                    let result = PrimitiveValue {
                        value: U256::from(1u64),
                        is_pointer: false,
                    };
                    vm_state.perform_dst0_update(
                        vm_state.local_state.monotonic_cycle_counter,
                        result,
                        dst0_mem_location,
                        self,
                    );

                    let extra_pubdata_cost = precompile_aux_data.extra_pubdata_cost;
                    assert!(extra_pubdata_cost <= i32::MAX as u32);

                    (PubdataCost(extra_pubdata_cost as i32), 0)
                }
            }
            LogOpcode::Decommit => {
                // we take a preimage from src0, check it's format, normalize and add to decommit queue
                let (dst_0_value, (pubdata_cost, refund)) = if decommit_preimage_format_is_invalid
                    || not_enough_power
                {
                    // we have to update register anyway
                    (PrimitiveValue::empty(), (PubdataCost(0), 0))
                } else {
                    // add to decommit queue the normalized image
                    let timestamp_for_decommit =
                        vm_state.timestamp_for_first_decommit_or_precompile_read();
                    let memory_page_candidate_for_decommitment = heap_page_from_base(
                        vm_state
                            .local_state
                            .callstack
                            .get_current_stack()
                            .base_memory_page,
                    );
                    assert!(decommit_preimage_normalized.0 != [0u8; 28], "original buffer {:?} lead to zero normalized preimage, but didn't trigger exception", buffer);
                    let prepared_decommittment_query = vm_state.prepare_to_decommit(
                        vm_state.local_state.monotonic_cycle_counter,
                        decommit_header,
                        decommit_preimage_normalized,
                        memory_page_candidate_for_decommitment,
                        timestamp_for_decommit,
                    )?;

                    let refund = if prepared_decommittment_query.is_fresh == false {
                        extra_cost
                    } else {
                        0
                    };

                    vm_state.execute_decommit(
                        vm_state.local_state.monotonic_cycle_counter,
                        prepared_decommittment_query,
                    )?;

                    let output_memory_page = prepared_decommittment_query.memory_page;
                    // form a fat pointer
                    let fat_pointer = FatPointer {
                        offset: 0,
                        memory_page: output_memory_page.0,
                        start: 0,
                        length: preimage_len_in_bytes,
                    };

                    (
                        PrimitiveValue {
                            value: fat_pointer.to_u256(),
                            is_pointer: true,
                        },
                        (PubdataCost(0), refund),
                    )
                };

                // and update register
                vm_state.perform_dst0_update(
                    vm_state.local_state.monotonic_cycle_counter,
                    dst_0_value,
                    dst0_mem_location,
                    self,
                );

                (pubdata_cost, refund)
            }
        };

        // apply refund
        vm_state
            .local_state
            .callstack
            .get_current_stack_mut()
            .ergs_remaining += ergs_refund;

        vm_state.add_pubdata_cost(pubdata_to_add_to_current_frame);

        Ok(())
    }
}