Skip to content

Commit f1733ec

Browse files
feat(apollo_starknet_os_program): copy OS cairo code
Signed-off-by: Dori Medini <[email protected]>
1 parent 4e93460 commit f1733ec

31 files changed

+10184
-0
lines changed
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
from starkware.cairo.common.cairo_secp.ec import EcPoint
2+
from starkware.cairo.common.sha256_state import Sha256Input, Sha256State
3+
from starkware.cairo.common.uint256 import Uint256
4+
5+
// Syscall selectors.
6+
7+
const CALL_CONTRACT_SELECTOR = 'CallContract';
8+
const DEPLOY_SELECTOR = 'Deploy';
9+
const EMIT_EVENT_SELECTOR = 'EmitEvent';
10+
const GET_BLOCK_HASH_SELECTOR = 'GetBlockHash';
11+
const GET_EXECUTION_INFO_SELECTOR = 'GetExecutionInfo';
12+
const SECP256K1_ADD_SELECTOR = 'Secp256k1Add';
13+
const SECP256K1_GET_POINT_FROM_X_SELECTOR = 'Secp256k1GetPointFromX';
14+
const SECP256K1_GET_XY_SELECTOR = 'Secp256k1GetXy';
15+
const SECP256K1_MUL_SELECTOR = 'Secp256k1Mul';
16+
const SECP256K1_NEW_SELECTOR = 'Secp256k1New';
17+
const SECP256R1_ADD_SELECTOR = 'Secp256r1Add';
18+
const SECP256R1_GET_POINT_FROM_X_SELECTOR = 'Secp256r1GetPointFromX';
19+
const SECP256R1_GET_XY_SELECTOR = 'Secp256r1GetXy';
20+
const SECP256R1_MUL_SELECTOR = 'Secp256r1Mul';
21+
const SECP256R1_NEW_SELECTOR = 'Secp256r1New';
22+
const KECCAK_SELECTOR = 'Keccak';
23+
const SHA256_PROCESS_BLOCK_SELECTOR = 'Sha256ProcessBlock';
24+
const LIBRARY_CALL_SELECTOR = 'LibraryCall';
25+
const REPLACE_CLASS_SELECTOR = 'ReplaceClass';
26+
const SEND_MESSAGE_TO_L1_SELECTOR = 'SendMessageToL1';
27+
const STORAGE_READ_SELECTOR = 'StorageRead';
28+
const STORAGE_WRITE_SELECTOR = 'StorageWrite';
29+
const GET_CLASS_HASH_AT_SELECTOR = 'GetClassHashAt';
30+
const META_TX_V0_SELECTOR = 'MetaTxV0';
31+
32+
// Syscall structs.
33+
34+
struct ExecutionInfo {
35+
block_info: BlockInfo*,
36+
tx_info: TxInfo*,
37+
38+
// Entry-point-specific info.
39+
40+
caller_address: felt,
41+
// The execution is done in the context of the contract at this address.
42+
// It controls the storage being used, messages sent to L1, calling contracts, etc.
43+
contract_address: felt,
44+
// The entry point selector.
45+
selector: felt,
46+
}
47+
48+
struct BlockInfo {
49+
block_number: felt,
50+
block_timestamp: felt,
51+
// The address of the sequencer that is creating this block.
52+
sequencer_address: felt,
53+
}
54+
55+
struct ResourceBounds {
56+
// The name of the resource (e.g., 'L1_GAS').
57+
resource: felt,
58+
// The maximum amount of the resource allowed for usage during the execution.
59+
max_amount: felt,
60+
// The maximum price the user is willing to pay for the resource unit.
61+
max_price_per_unit: felt,
62+
}
63+
64+
struct TxInfo {
65+
// The version of the transaction. It is fixed in the OS, and should be signed by the account
66+
// contract.
67+
// This field allows invalidating old transactions, whenever the meaning of the other
68+
// transaction fields is changed (in the OS).
69+
version: felt,
70+
// The account contract from which this transaction originates.
71+
account_contract_address: felt,
72+
// The max_fee field of the transaction.
73+
max_fee: felt,
74+
// The signature of the transaction.
75+
signature_start: felt*,
76+
signature_end: felt*,
77+
// The hash of the transaction.
78+
transaction_hash: felt,
79+
// The identifier of the chain.
80+
// This field can be used to prevent replay of testnet transactions on mainnet.
81+
chain_id: felt,
82+
// The transaction's nonce.
83+
nonce: felt,
84+
// An array of ResourceBounds structs.
85+
resource_bounds_start: ResourceBounds*,
86+
resource_bounds_end: ResourceBounds*,
87+
// The tip.
88+
tip: felt,
89+
// If specified, the paymaster should pay for the execution of the tx.
90+
// The data includes the address of the paymaster sponsoring the transaction, followed by extra
91+
// data to send to the paymaster.
92+
paymaster_data_start: felt*,
93+
paymaster_data_end: felt*,
94+
// The data availability mode for the nonce.
95+
nonce_data_availability_mode: felt,
96+
// The data availability mode for the account balance from which fee will be taken.
97+
fee_data_availability_mode: felt,
98+
// If nonempty, will contain the required data for deploying and initializing an account
99+
// contract: its class hash, address salt and constructor calldata.
100+
account_deployment_data_start: felt*,
101+
account_deployment_data_end: felt*,
102+
}
103+
104+
// Shared attributes.
105+
106+
struct RequestHeader {
107+
// The syscall selector.
108+
selector: felt,
109+
// The amount of gas left before the syscall execution.
110+
gas: felt,
111+
}
112+
113+
struct ResponseHeader {
114+
// The amount of gas left after the syscall execution.
115+
gas: felt,
116+
// 0 if the syscall succeeded; 1 otherwise.
117+
failure_flag: felt,
118+
}
119+
120+
struct FailureReason {
121+
start: felt*,
122+
end: felt*,
123+
}
124+
125+
// Syscall requests.
126+
127+
struct CallContractRequest {
128+
// The address of the L2 contract to call.
129+
contract_address: felt,
130+
// The selector of the function to call.
131+
selector: felt,
132+
// The calldata.
133+
calldata_start: felt*,
134+
calldata_end: felt*,
135+
}
136+
137+
struct LibraryCallRequest {
138+
// The hash of the class to run.
139+
class_hash: felt,
140+
// The selector of the function to call.
141+
selector: felt,
142+
// The calldata.
143+
calldata_start: felt*,
144+
calldata_end: felt*,
145+
}
146+
147+
struct EmptyRequest {
148+
}
149+
150+
struct DeployRequest {
151+
// The hash of the class to deploy.
152+
class_hash: felt,
153+
// A salt for the new contract address calculation.
154+
contract_address_salt: felt,
155+
// The calldata for the constructor.
156+
constructor_calldata_start: felt*,
157+
constructor_calldata_end: felt*,
158+
// Used for deterministic contract address deployment.
159+
deploy_from_zero: felt,
160+
}
161+
162+
struct GetBlockHashRequest {
163+
// The number of the block to get the hash for.
164+
block_number: felt,
165+
}
166+
167+
struct KeccakRequest {
168+
// The Span<u64> to be hashed.
169+
// See `keccak_padded_input` for more details.
170+
input_start: felt*,
171+
input_end: felt*,
172+
}
173+
174+
struct Sha256ProcessBlockRequest {
175+
state_ptr: Sha256State*,
176+
input_start: Sha256Input*,
177+
}
178+
179+
struct SecpAddRequest {
180+
p0: EcPoint*,
181+
p1: EcPoint*,
182+
}
183+
184+
using Secp256k1AddRequest = SecpAddRequest;
185+
using Secp256r1AddRequest = SecpAddRequest;
186+
187+
struct SecpMulRequest {
188+
p: EcPoint*,
189+
scalar: Uint256,
190+
}
191+
192+
using Secp256k1MulRequest = SecpMulRequest;
193+
using Secp256r1MulRequest = SecpMulRequest;
194+
195+
struct SecpNewRequest {
196+
// The x and y coordinates of the requested point on the Secp curve.
197+
// The point at infinity, can be created by passing (0, 0).
198+
x: Uint256,
199+
y: Uint256,
200+
}
201+
202+
using Secp256k1NewRequest = SecpNewRequest;
203+
using Secp256r1NewRequest = SecpNewRequest;
204+
205+
struct SecpGetPointFromXRequest {
206+
x: Uint256,
207+
y_parity: felt,
208+
}
209+
210+
using Secp256k1GetPointFromXRequest = SecpGetPointFromXRequest;
211+
using Secp256r1GetPointFromXRequest = SecpGetPointFromXRequest;
212+
213+
struct SecpGetXyRequest {
214+
// A pointer to the point.
215+
ec_point: EcPoint*,
216+
}
217+
218+
using Secp256k1GetXyRequest = SecpGetXyRequest;
219+
using Secp256r1GetXyRequest = SecpGetXyRequest;
220+
221+
struct StorageReadRequest {
222+
reserved: felt,
223+
key: felt,
224+
}
225+
226+
struct StorageWriteRequest {
227+
reserved: felt,
228+
key: felt,
229+
value: felt,
230+
}
231+
232+
struct EmitEventRequest {
233+
keys_start: felt*,
234+
keys_end: felt*,
235+
data_start: felt*,
236+
data_end: felt*,
237+
}
238+
239+
struct ReplaceClassRequest {
240+
class_hash: felt,
241+
}
242+
243+
struct SendMessageToL1Request {
244+
to_address: felt,
245+
payload_start: felt*,
246+
payload_end: felt*,
247+
}
248+
249+
// Syscall responses.
250+
251+
struct CallContractResponse {
252+
retdata_start: felt*,
253+
retdata_end: felt*,
254+
}
255+
256+
struct DeployResponse {
257+
contract_address: felt,
258+
constructor_retdata_start: felt*,
259+
constructor_retdata_end: felt*,
260+
}
261+
262+
struct KeccakResponse {
263+
result_low: felt,
264+
result_high: felt,
265+
}
266+
267+
struct Sha256ProcessBlockResponse {
268+
state_ptr: Sha256State*,
269+
}
270+
271+
struct SecpGetXyResponse {
272+
// The x and y coordinates of the given point. Returns (0, 0) for the point at infinity.
273+
x: Uint256,
274+
y: Uint256,
275+
}
276+
277+
using Secp256k1GetXyResponse = SecpGetXyResponse;
278+
using Secp256r1GetXyResponse = SecpGetXyResponse;
279+
280+
struct SecpNewResponse {
281+
// The syscall returns `Option<SecpPoint>` which is represented as two felts in memory.
282+
283+
// 1 if the point is not on the curve, 0 otherwise.
284+
not_on_curve: felt,
285+
// A pointer to the point in the case not_on_curve == 0, otherwise 0.
286+
ec_point: EcPoint*,
287+
}
288+
289+
using Secp256k1NewResponse = SecpNewResponse;
290+
using Secp256r1NewResponse = SecpNewResponse;
291+
292+
struct SecpOpResponse {
293+
// The result of Secp256k1 or Secp256r1 add or mul operations.
294+
ec_point: EcPoint*,
295+
}
296+
297+
using Secp256k1AddResponse = SecpOpResponse;
298+
using Secp256k1MulResponse = SecpOpResponse;
299+
using Secp256r1AddResponse = SecpOpResponse;
300+
using Secp256r1MulResponse = SecpOpResponse;
301+
302+
struct StorageReadResponse {
303+
value: felt,
304+
}
305+
306+
struct GetExecutionInfoResponse {
307+
execution_info: ExecutionInfo*,
308+
}
309+
310+
struct GetBlockHashResponse {
311+
block_hash: felt,
312+
}
313+
314+
struct GetClassHashAtRequest {
315+
contract_address: felt,
316+
}
317+
318+
struct GetClassHashAtResponse {
319+
class_hash: felt,
320+
}
321+
322+
struct MetaTxV0Request {
323+
// The address of the L2 contract to call.
324+
contract_address: felt,
325+
// The selector of the function to call.
326+
selector: felt,
327+
// The calldata.
328+
calldata_start: felt*,
329+
calldata_end: felt*,
330+
// The signature.
331+
signature_start: felt*,
332+
signature_end: felt*,
333+
}

0 commit comments

Comments
 (0)