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
use primitive_types::{H160, U256};
use zk_evm_abstractions::{
    aux::Timestamp,
    precompiles::{
        ecrecover::ecrecover_function, keccak256::keccak256_rounds_function,
        secp256r1_verify::secp256r1_verify_function, sha256::sha256_rounds_function,
    },
    queries::LogQuery,
    vm::Memory,
};
use zkevm_opcode_defs::{
    system_params::{
        ECRECOVER_INNER_FUNCTION_PRECOMPILE_ADDRESS, KECCAK256_ROUND_FUNCTION_PRECOMPILE_ADDRESS,
        SECP256R1_VERIFY_PRECOMPILE_ADDRESS, SHA256_ROUND_FUNCTION_PRECOMPILE_ADDRESS,
    },
    PrecompileAuxData, PrecompileCallABI,
};
use zksync_vm2_interface::{opcodes, CycleStats, HeapId, Tracer};

use super::{common::boilerplate_ext, ret::spontaneous_panic};
use crate::{
    addressing_modes::{Arguments, Destination, Register1, Register2, Source},
    heap::Heaps,
    instruction::ExecutionStatus,
    Instruction, VirtualMachine, World,
};

fn precompile_call<T: Tracer, W: World<T>>(
    vm: &mut VirtualMachine<T, W>,
    world: &mut W,
    tracer: &mut T,
) -> ExecutionStatus {
    boilerplate_ext::<opcodes::PrecompileCall, _, _>(vm, world, tracer, |vm, args, _, tracer| {
        // The user gets to decide how much gas to burn
        // This is safe because system contracts are trusted
        let aux_data = PrecompileAuxData::from_u256(Register2::get(args, &mut vm.state));
        let Ok(()) = vm.state.use_gas(aux_data.extra_ergs_cost) else {
            vm.state.current_frame.pc = spontaneous_panic();
            return;
        };

        #[allow(clippy::cast_possible_wrap)]
        {
            vm.world_diff.pubdata.0 += aux_data.extra_pubdata_cost as i32;
        }

        let mut abi = PrecompileCallABI::from_u256(Register1::get(args, &mut vm.state));
        if abi.memory_page_to_read == 0 {
            abi.memory_page_to_read = vm.state.current_frame.heap.as_u32();
        }
        if abi.memory_page_to_write == 0 {
            abi.memory_page_to_write = vm.state.current_frame.heap.as_u32();
        }

        let query = LogQuery {
            timestamp: Timestamp(0),
            key: abi.to_u256(),
            // only two first fields are read by the precompile
            tx_number_in_block: Default::default(),
            aux_byte: Default::default(),
            shard_id: Default::default(),
            address: H160::default(),
            read_value: U256::default(),
            written_value: U256::default(),
            rw_flag: Default::default(),
            rollback: Default::default(),
            is_service: Default::default(),
        };

        let address_bytes = vm.state.current_frame.address.0;
        let address_low = u16::from_le_bytes([address_bytes[19], address_bytes[18]]);
        let heaps = &mut vm.state.heaps;

        #[allow(clippy::cast_possible_truncation)]
        // if we're having `> u32::MAX` cycles, we've got larger issues
        match address_low {
            KECCAK256_ROUND_FUNCTION_PRECOMPILE_ADDRESS => {
                tracer.on_extra_prover_cycles(CycleStats::Keccak256(
                    keccak256_rounds_function::<_, false>(0, query, heaps).0 as u32,
                ));
            }
            SHA256_ROUND_FUNCTION_PRECOMPILE_ADDRESS => {
                tracer.on_extra_prover_cycles(CycleStats::Sha256(
                    sha256_rounds_function::<_, false>(0, query, heaps).0 as u32,
                ));
            }
            ECRECOVER_INNER_FUNCTION_PRECOMPILE_ADDRESS => {
                tracer.on_extra_prover_cycles(CycleStats::EcRecover(
                    ecrecover_function::<_, false>(0, query, heaps).0 as u32,
                ));
            }
            SECP256R1_VERIFY_PRECOMPILE_ADDRESS => {
                tracer.on_extra_prover_cycles(CycleStats::Secp256r1Verify(
                    secp256r1_verify_function::<_, false>(0, query, heaps).0 as u32,
                ));
            }
            _ => {
                // A precompile call may be used just to burn gas
            }
        }

        Register1::set(args, &mut vm.state, 1.into());
    })
}

impl Memory for Heaps {
    fn execute_partial_query(
        &mut self,
        _monotonic_cycle_counter: u32,
        mut query: zk_evm_abstractions::queries::MemoryQuery,
    ) -> zk_evm_abstractions::queries::MemoryQuery {
        let page = HeapId::from_u32_unchecked(query.location.page.0);

        let start = query.location.index.0 * 32;
        if query.rw_flag {
            self.write_u256(page, start, query.value);
        } else {
            query.value = self[page].read_u256(start);
            query.value_is_pointer = false;
        }
        query
    }

    fn specialized_code_query(
        &mut self,
        _monotonic_cycle_counter: u32,
        _query: zk_evm_abstractions::queries::MemoryQuery,
    ) -> zk_evm_abstractions::queries::MemoryQuery {
        todo!()
    }

    fn read_code_query(
        &self,
        _monotonic_cycle_counter: u32,
        _query: zk_evm_abstractions::queries::MemoryQuery,
    ) -> zk_evm_abstractions::queries::MemoryQuery {
        todo!()
    }
}

impl<T: Tracer, W: World<T>> Instruction<T, W> {
    /// Creates a [`PrecompileCall`](opcodes::PrecompileCall) instruction with the provided params.
    pub fn from_precompile_call(
        abi: Register1,
        burn: Register2,
        out: Register1,
        arguments: Arguments,
    ) -> Self {
        Self {
            arguments: arguments
                .write_source(&abi)
                .write_source(&burn)
                .write_destination(&out),
            handler: precompile_call,
        }
    }
}