-
Notifications
You must be signed in to change notification settings - Fork 322
/
precompiles.cairo
255 lines (236 loc) · 10.7 KB
/
precompiles.cairo
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
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math_cmp import is_nn, is_not_zero, is_in_range
from starkware.starknet.common.syscalls import library_call
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.memcpy import memcpy
from kakarot.interfaces.interfaces import ICairo1Helpers
from kakarot.storages import Kakarot_cairo1_helpers_class_hash
from kakarot.errors import Errors
from kakarot.precompiles.blake2f import PrecompileBlake2f
from kakarot.precompiles.kakarot_precompiles import KakarotPrecompiles
from kakarot.precompiles.identity import PrecompileIdentity
from kakarot.precompiles.ec_recover import PrecompileEcRecover
from kakarot.precompiles.p256verify import PrecompileP256Verify
from kakarot.precompiles.ripemd160 import PrecompileRIPEMD160
from kakarot.precompiles.sha256 import PrecompileSHA256
from kakarot.precompiles.precompiles_helpers import (
PrecompilesHelpers,
LAST_ETHEREUM_PRECOMPILE_ADDRESS,
FIRST_ROLLUP_PRECOMPILE_ADDRESS,
FIRST_KAKAROT_PRECOMPILE_ADDRESS,
)
from utils.utils import Helpers
// @title Precompile related functions.
namespace Precompiles {
// @notice Executes associated function of precompiled evm_address.
// @dev This function uses an internal jump table to execute the corresponding precompile impmentation.
// @param precompile_address The precompile evm_address.
// @param input_len The length of the input array.
// @param input The input array.
// @param caller_code_address The address of the code of the contract that calls the precompile.
// @param caller_address The address of the caller of the precompile. Delegatecall rules apply.
// @return output_len The output length.
// @return output The output array.
// @return gas_used The gas usage of precompile.
// @return reverted The reverted code in {0(success), REVERTED, EXCEPTIONAL_HALT}.
func exec_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(
precompile_address: felt,
input_len: felt,
input: felt*,
caller_code_address: felt,
caller_address: felt,
) -> (output_len: felt, output: felt*, gas_used: felt, reverted: felt) {
let is_eth_precompile = is_nn(LAST_ETHEREUM_PRECOMPILE_ADDRESS - precompile_address);
tempvar syscall_ptr = syscall_ptr;
tempvar pedersen_ptr = pedersen_ptr;
tempvar range_check_ptr = range_check_ptr;
jmp eth_precompile if is_eth_precompile != 0;
let is_rollup_precompile_ = PrecompilesHelpers.is_rollup_precompile(precompile_address);
tempvar syscall_ptr = syscall_ptr;
tempvar pedersen_ptr = pedersen_ptr;
tempvar range_check_ptr = range_check_ptr;
jmp rollup_precompile if is_rollup_precompile_ != 0;
let is_kakarot_precompile_ = PrecompilesHelpers.is_kakarot_precompile(precompile_address);
tempvar syscall_ptr = syscall_ptr;
tempvar pedersen_ptr = pedersen_ptr;
tempvar range_check_ptr = range_check_ptr;
jmp kakarot_precompile if is_kakarot_precompile_ != 0;
jmp unauthorized_call;
eth_precompile:
tempvar index = precompile_address;
jmp call_precompile;
rollup_precompile:
tempvar index = (LAST_ETHEREUM_PRECOMPILE_ADDRESS + 1) + (
precompile_address - FIRST_ROLLUP_PRECOMPILE_ADDRESS
);
jmp call_precompile;
unauthorized_call:
// Prepare arguments if none of the above conditions are met
[ap] = syscall_ptr, ap++;
[ap] = pedersen_ptr, ap++;
[ap] = range_check_ptr, ap++;
[ap] = bitwise_ptr, ap++;
call unauthorized_precompile;
ret;
call_precompile:
// Compute the corresponding offset in the jump table:
// count 1 for "next line" and 3 steps per index: call, precompile, ret
tempvar offset = 1 + 3 * index;
// Prepare arguments
[ap] = syscall_ptr, ap++;
[ap] = pedersen_ptr, ap++;
[ap] = range_check_ptr, ap++;
[ap] = bitwise_ptr, ap++;
[ap] = precompile_address, ap++;
[ap] = input_len, ap++;
[ap] = input, ap++;
// call precompile precompile_address
jmp rel offset;
call unknown_precompile; // 0x0
ret;
call PrecompileEcRecover.run; // 0x1
ret;
call external_precompile; // 0x2
ret;
call PrecompileRIPEMD160.run; // 0x3
ret;
call PrecompileIdentity.run; // 0x4
ret;
call not_implemented_precompile; // 0x5
ret;
call external_precompile; // 0x6
ret;
call external_precompile; // 0x7
ret;
call not_implemented_precompile; // 0x8
ret;
call PrecompileBlake2f.run; // 0x9
ret;
call not_implemented_precompile; // 0x0a: POINT_EVALUATION_PRECOMPILE
ret;
// Rollup precompiles. Offset must have been computed appropriately,
// based on the address of the precompile and the last ethereum precompile
call PrecompileP256Verify.run; // offset 0x0b: precompile 0x100
ret;
kakarot_precompile:
let is_whitelisted = KakarotPrecompiles.is_caller_whitelisted(caller_code_address);
tempvar is_not_authorized = 1 - is_whitelisted;
tempvar syscall_ptr = syscall_ptr;
tempvar pedersen_ptr = pedersen_ptr;
tempvar range_check_ptr = range_check_ptr;
jmp unauthorized_call if is_not_authorized != 0;
tempvar index = precompile_address - FIRST_KAKAROT_PRECOMPILE_ADDRESS;
tempvar offset = 1 + 3 * index;
// Prepare arguments
[ap] = syscall_ptr, ap++;
[ap] = pedersen_ptr, ap++;
[ap] = range_check_ptr, ap++;
[ap] = bitwise_ptr, ap++;
[ap] = input_len, ap++;
[ap] = input, ap++;
[ap] = caller_address, ap++;
// Kakarot precompiles. Offset must have been computed appropriately,
// based on the total number of kakarot precompiles
jmp rel offset;
call KakarotPrecompiles.cairo_precompile; // offset 0x0c: precompile 0x75001
ret;
call KakarotPrecompiles.cairo_message; // offset 0x0d: precompile 0x75002
ret;
}
// @notice A placeholder for attempts to call a precompile without permissions
// @dev Halts execution with an unauthorized precompile error.
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func unauthorized_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}() -> (output_len: felt, output: felt*, gas_used: felt, reverted: felt) {
let (revert_reason_len, revert_reason) = Errors.unauthorizedPrecompile();
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}
// @notice A placeholder for precompiles that don't exist.
// @dev Halts execution with an unknown precompile error.
// @param evm_address The address of the unknown precompile.
// @param input_len The length of the input array (unused).
// @param input The input array (unused).
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func unknown_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(evm_address: felt, input_len: felt, input: felt*) -> (
output_len: felt, output: felt*, gas_used: felt, reverted: felt
) {
let (revert_reason_len, revert_reason) = Errors.unknownPrecompile(evm_address);
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}
// @notice A placeholder for precompiles that are not implemented yet.
// @dev Halts execution with a not implemented precompile error.
// @param evm_address The address of the not implemented precompile.
// @param input_len The length of the input array (unused).
// @param input The input array (unused).
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func not_implemented_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(evm_address: felt, input_len: felt, input: felt*) -> (
output_len: felt, output: felt*, gas_used: felt, reverted: felt
) {
let (revert_reason_len, revert_reason) = Errors.notImplementedPrecompile(evm_address);
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}
// @notice Executes an external precompile using a Cairo 1 helper contract.
// @dev Calls the library_call_exec_precompile function of the ICairo1Helpers interface.
// @param evm_address The address of the external precompile.
// @param input_len The length of the input array.
// @param input The input array.
// @return output_len The length of the output data.
// @return output The output data.
// @return gas_used The gas used by the precompile execution.
// @return reverted 0 if successful, EXCEPTIONAL_HALT if execution failed.
func external_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(evm_address: felt, input_len: felt, input: felt*) -> (
output_len: felt, output: felt*, gas_used: felt, reverted: felt
) {
alloc_locals;
let (implementation) = Kakarot_cairo1_helpers_class_hash.read();
let (calldata: felt*) = alloc();
assert [calldata] = evm_address;
assert [calldata + 1] = input_len;
memcpy(calldata + 2, input, input_len);
let (
success, gas, return_data_len, return_data
) = ICairo1Helpers.library_call_exec_precompile(
class_hash=implementation, address=evm_address, data_len=input_len, data=input
);
if (success != FALSE) {
return (return_data_len, return_data, gas, 0);
}
// Precompiles can only revert with exceptions. Thus if the execution failed, it's an error EXCEPTIONAL_HALT.
return (return_data_len, return_data, 0, Errors.EXCEPTIONAL_HALT);
}
}