From 2b566aeceae426ed30370b5193f7b424f497a38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Walter?= Date: Fri, 13 Sep 2024 14:22:34 +0200 Subject: [PATCH] Add get_env MWE (#24) * Add get_env mwe * Add output_ptr --- cairo_programs/get_env.cairo | 70 +++++ cairo_programs/get_env.json | 540 +++++++++++++++++++++++++++++++++++ 2 files changed, 610 insertions(+) create mode 100644 cairo_programs/get_env.cairo create mode 100644 cairo_programs/get_env.json diff --git a/cairo_programs/get_env.cairo b/cairo_programs/get_env.cairo new file mode 100644 index 00000000..811ecbc4 --- /dev/null +++ b/cairo_programs/get_env.cairo @@ -0,0 +1,70 @@ +%builtins output + +// Represents an integer in the range [0, 2^256). +struct Uint256 { + // The low 128 bits of the value. + low: felt, + // The high 128 bits of the value. + high: felt, +} + +// @notice Store all environment data relevant to the current execution context. +// @param origin The origin of the transaction. +// @param gas_price The gas price for the call. +// @param chain_id The chain id of the current block. +// @param prev_randao The previous RANDAO value. +// @param block_number The block number of the current block. +// @param block_gas_limit The gas limit for the current block. +// @param block_timestamp The timestamp of the current block. +// @param coinbase The address of the miner of the current block. +// @param base_fee The basefee of the current block. +struct Environment { + origin: felt, + gas_price: felt, + chain_id: felt, + prev_randao: Uint256, + block_number: felt, + block_gas_limit: felt, + block_timestamp: felt, + coinbase: felt, + base_fee: felt, +} + +// @notice Populate an Environment with hint +func get_env() -> Environment* { + tempvar env = cast(nondet %{ segments.add() %}, Environment*); + + // The hint should populate env. + %{ + ids.env.origin=1 + ids.env.gas_price=2 + ids.env.chain_id=3 + ids.env.prev_randao.low=4 + ids.env.prev_randao.high=4 + ids.env.block_number=5 + ids.env.block_gas_limit=6 + ids.env.block_timestamp=7 + ids.env.coinbase=8 + ids.env.base_fee=9 + %} + + return env; +} + +func main{output_ptr: felt*}() { + let env = get_env(); + + assert [output_ptr] = env.origin; + assert [output_ptr + 1] = env.gas_price; + assert [output_ptr + 2] = env.chain_id; + assert [output_ptr + 3] = env.prev_randao.low; + assert [output_ptr + 4] = env.prev_randao.high; + assert [output_ptr + 5] = env.block_number; + assert [output_ptr + 6] = env.block_gas_limit; + assert [output_ptr + 7] = env.block_timestamp; + assert [output_ptr + 8] = env.coinbase; + assert [output_ptr + 9] = env.base_fee; + let output_ptr = output_ptr + 10; + + return (); +} diff --git a/cairo_programs/get_env.json b/cairo_programs/get_env.json new file mode 100644 index 00000000..5b91b891 --- /dev/null +++ b/cairo_programs/get_env.json @@ -0,0 +1,540 @@ +{ + "attributes": [], + "builtins": ["output"], + "compiler_version": "0.13.1", + "data": [ + "0x40780017fff7fff", + "0x1", + "0x1104800180018000", + "0x7", + "0x10780017fff7fff", + "0x0", + "0x40780017fff7fff", + "0x1", + "0x208b7fff7fff7ffe", + "0x1104800180018000", + "0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffffe", + "0x480080007fff8000", + "0x400280007ffd7fff", + "0x480080017ffe8000", + "0x400280017ffd7fff", + "0x480080027ffd8000", + "0x400280027ffd7fff", + "0x480080037ffc8000", + "0x400280037ffd7fff", + "0x480080047ffb8000", + "0x400280047ffd7fff", + "0x480080057ffa8000", + "0x400280057ffd7fff", + "0x480080067ff98000", + "0x400280067ffd7fff", + "0x480080077ff88000", + "0x400280077ffd7fff", + "0x480080087ff78000", + "0x400280087ffd7fff", + "0x480080097ff68000", + "0x400280097ffd7fff", + "0x482680017ffd8000", + "0xa", + "0x208b7fff7fff7ffe" + ], + "debug_info": null, + "hints": { + "6": [ + { + "accessible_scopes": ["__main__", "__main__.get_env"], + "code": "memory[ap] = to_felt_or_relocatable(segments.add())", + "flow_tracking_data": { + "ap_tracking": { + "group": 2, + "offset": 0 + }, + "reference_ids": {} + } + } + ], + "8": [ + { + "accessible_scopes": ["__main__", "__main__.get_env"], + "code": "ids.env.origin=1\nids.env.gas_price=2\nids.env.chain_id=3\nids.env.prev_randao.low=4\nids.env.prev_randao.high=4\nids.env.block_number=5\nids.env.block_gas_limit=6\nids.env.block_timestamp=7\nids.env.coinbase=8\nids.env.base_fee=9", + "flow_tracking_data": { + "ap_tracking": { + "group": 2, + "offset": 1 + }, + "reference_ids": { + "__main__.get_env.__temp0": 0, + "__main__.get_env.env": 1 + } + } + } + ] + }, + "identifiers": { + "__main__.Environment": { + "full_name": "__main__.Environment", + "members": { + "base_fee": { + "cairo_type": "felt", + "offset": 9 + }, + "block_gas_limit": { + "cairo_type": "felt", + "offset": 6 + }, + "block_number": { + "cairo_type": "felt", + "offset": 5 + }, + "block_timestamp": { + "cairo_type": "felt", + "offset": 7 + }, + "chain_id": { + "cairo_type": "felt", + "offset": 2 + }, + "coinbase": { + "cairo_type": "felt", + "offset": 8 + }, + "gas_price": { + "cairo_type": "felt", + "offset": 1 + }, + "origin": { + "cairo_type": "felt", + "offset": 0 + }, + "prev_randao": { + "cairo_type": "__main__.Uint256", + "offset": 3 + } + }, + "size": 10, + "type": "struct" + }, + "__main__.Uint256": { + "full_name": "__main__.Uint256", + "members": { + "high": { + "cairo_type": "felt", + "offset": 1 + }, + "low": { + "cairo_type": "felt", + "offset": 0 + } + }, + "size": 2, + "type": "struct" + }, + "__main__.__end__": { + "pc": 4, + "type": "label" + }, + "__main__.__start__": { + "pc": 0, + "type": "label" + }, + "__main__.get_env": { + "decorators": [], + "pc": 6, + "type": "function" + }, + "__main__.get_env.Args": { + "full_name": "__main__.get_env.Args", + "members": {}, + "size": 0, + "type": "struct" + }, + "__main__.get_env.ImplicitArgs": { + "full_name": "__main__.get_env.ImplicitArgs", + "members": {}, + "size": 0, + "type": "struct" + }, + "__main__.get_env.Return": { + "cairo_type": "__main__.Environment*", + "type": "type_definition" + }, + "__main__.get_env.SIZEOF_LOCALS": { + "type": "const", + "value": 0 + }, + "__main__.get_env.__temp0": { + "cairo_type": "felt", + "full_name": "__main__.get_env.__temp0", + "references": [ + { + "ap_tracking_data": { + "group": 2, + "offset": 1 + }, + "pc": 8, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.get_env.env": { + "cairo_type": "__main__.Environment*", + "full_name": "__main__.get_env.env", + "references": [ + { + "ap_tracking_data": { + "group": 2, + "offset": 1 + }, + "pc": 8, + "value": "[cast(ap + (-1), __main__.Environment**)]" + } + ], + "type": "reference" + }, + "__main__.main": { + "decorators": [], + "pc": 9, + "type": "function" + }, + "__main__.main.Args": { + "full_name": "__main__.main.Args", + "members": {}, + "size": 0, + "type": "struct" + }, + "__main__.main.ImplicitArgs": { + "full_name": "__main__.main.ImplicitArgs", + "members": { + "output_ptr": { + "cairo_type": "felt*", + "offset": 0 + } + }, + "size": 1, + "type": "struct" + }, + "__main__.main.Return": { + "cairo_type": "()", + "type": "type_definition" + }, + "__main__.main.SIZEOF_LOCALS": { + "type": "const", + "value": 0 + }, + "__main__.main.__temp1": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp1", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 4 + }, + "pc": 12, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp10": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp10", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 13 + }, + "pc": 30, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp2": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp2", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 5 + }, + "pc": 14, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp3": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp3", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 6 + }, + "pc": 16, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp4": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp4", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 7 + }, + "pc": 18, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp5": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp5", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 8 + }, + "pc": 20, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp6": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp6", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 9 + }, + "pc": 22, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp7": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp7", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 10 + }, + "pc": 24, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp8": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp8", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 11 + }, + "pc": 26, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.__temp9": { + "cairo_type": "felt", + "full_name": "__main__.main.__temp9", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 12 + }, + "pc": 28, + "value": "[cast(ap + (-1), felt*)]" + } + ], + "type": "reference" + }, + "__main__.main.env": { + "cairo_type": "__main__.Environment*", + "full_name": "__main__.main.env", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 3 + }, + "pc": 11, + "value": "[cast(ap + (-1), __main__.Environment**)]" + } + ], + "type": "reference" + }, + "__main__.main.output_ptr": { + "cairo_type": "felt*", + "full_name": "__main__.main.output_ptr", + "references": [ + { + "ap_tracking_data": { + "group": 3, + "offset": 0 + }, + "pc": 9, + "value": "[cast(fp + (-3), felt**)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 13 + }, + "pc": 31, + "value": "cast([fp + (-3)] + 10, felt*)" + } + ], + "type": "reference" + } + }, + "main_scope": "__main__", + "prime": "0x800000000000011000000000000000000000000000000000000000000000001", + "reference_manager": { + "references": [ + { + "ap_tracking_data": { + "group": 2, + "offset": 1 + }, + "pc": 8, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 2, + "offset": 1 + }, + "pc": 8, + "value": "[cast(ap + (-1), __main__.Environment**)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 0 + }, + "pc": 9, + "value": "[cast(fp + (-3), felt**)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 3 + }, + "pc": 11, + "value": "[cast(ap + (-1), __main__.Environment**)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 4 + }, + "pc": 12, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 5 + }, + "pc": 14, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 6 + }, + "pc": 16, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 7 + }, + "pc": 18, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 8 + }, + "pc": 20, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 9 + }, + "pc": 22, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 10 + }, + "pc": 24, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 11 + }, + "pc": 26, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 12 + }, + "pc": 28, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 13 + }, + "pc": 30, + "value": "[cast(ap + (-1), felt*)]" + }, + { + "ap_tracking_data": { + "group": 3, + "offset": 13 + }, + "pc": 31, + "value": "cast([fp + (-3)] + 10, felt*)" + } + ] + } +}