Skip to content

Commit

Permalink
Merge pull request #12 from Okm165/builtin_fix_in_contract
Browse files Browse the repository at this point in the history
Builtin fix in contract
  • Loading branch information
Okm165 authored Jul 14, 2024
2 parents 739a1dd + 1ad5d04 commit 324d49a
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 220 deletions.
667 changes: 509 additions & 158 deletions bootloader_input.json

Large diffs are not rendered by default.

41 changes: 34 additions & 7 deletions cairo0-bootloader/bootloader/contract/contract_bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ from starkware.cairo.common.cairo_builtins import (
KeccakBuiltin,
)
from starkware.cairo.common.registers import get_fp_and_pc
from contract_class.compiled_class import CompiledClass
from contract_class.compiled_class import CompiledClass, compiled_class_hash
from starkware.cairo.common.alloc import alloc

func main{
output_ptr: felt*,
Expand All @@ -25,32 +26,58 @@ func main{

%{
from bootloader.objects import ContractBootloaderInput
contract_bootloader_input = ContractBootloaderInput.Schema().load(program_input)
compiled_class = ContractBootloaderInput.Schema().load(program_input).compiled_class
%}

// Fetch contract data form hints.
%{
from starkware.starknet.core.os.contract_class.compiled_class_hash import create_bytecode_segment_structure
from contract_class.compiled_class_hash_utils import get_compiled_class_struct
bytecode_segment_structure = create_bytecode_segment_structure(
bytecode=contract_bootloader_input.compiled_class.bytecode,
bytecode_segment_lengths=contract_bootloader_input.compiled_class.bytecode_segment_lengths,
bytecode_segment_structure_no_footer = create_bytecode_segment_structure(
bytecode=compiled_class.bytecode,
bytecode_segment_lengths=compiled_class.bytecode_segment_lengths,
visited_pcs=None,
)
bytecode_segment_structure_with_footer = create_bytecode_segment_structure(
bytecode=compiled_class.bytecode,
bytecode_segment_lengths=compiled_class.bytecode_segment_lengths,
visited_pcs=None,
)
bytecode_segment_structure = bytecode_segment_structure_with_footer
cairo_contract = get_compiled_class_struct(
compiled_class=contract_bootloader_input.compiled_class,
compiled_class=compiled_class,
bytecode=bytecode_segment_structure.bytecode_with_skipped_segments()
)
ids.compiled_class = segments.gen_arg(cairo_contract)
%}

let (builtin_costs: felt*) = alloc();
assert builtin_costs[0] = 0;
assert builtin_costs[1] = 0;
assert builtin_costs[2] = 0;
assert builtin_costs[3] = 0;
assert builtin_costs[4] = 0;

assert compiled_class.bytecode_ptr[compiled_class.bytecode_length] = 0x208b7fff7fff7ffe;
assert compiled_class.bytecode_ptr[compiled_class.bytecode_length + 1] = cast(
builtin_costs, felt
);

%{ bytecode_segment_structure = bytecode_segment_structure_no_footer %}

let (local program_hash) = compiled_class_hash(compiled_class=compiled_class);

%{ bytecode_segment_structure = bytecode_segment_structure_with_footer %}

%{ print("program_hash", hex(ids.program_hash)) %}

%{
vm_load_program(
contract_bootloader_input.compiled_class.get_runnable_program(entrypoint_builtins=[]),
compiled_class.get_runnable_program(entrypoint_builtins=[]),
ids.compiled_class.bytecode_ptr
)
%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func execute_entry_point{
// Use tempvar to pass the rest of the arguments to contract_entry_point().
let current_ap = ap;
tempvar args = EntryPointCallArguments(
gas_builtin=1000000,
gas_builtin=2 ** 64 - 1,
syscall_ptr=syscall_ptr,
calldata_start=calldata_start,
calldata_end=calldata_end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func run_contract_bootloader{
);
let builtin_ptrs = &local_builtin_ptrs;

%{ print("builtin_ptrs.selectable.range_check: ", ids.builtin_ptrs.selectable.range_check) %}

local local_builtin_encodings: BuiltinEncodings = BuiltinEncodings(
pedersen='pedersen',
range_check='range_check',
Expand Down Expand Up @@ -93,13 +91,15 @@ func run_contract_bootloader{

local calldata: felt*;
%{ ids.calldata = segments.add() %}

assert calldata[0] = 0x3;
assert calldata[1] = 0x3;
assert calldata[2] = 0x4;
assert calldata[3] = 0x5;

local execution_info: ExecutionInfo = ExecutionInfo(selector=0x00e2054f8a912367e38a22ce773328ff8aabf8082c4120bad9ef085e1dbf29a7);
local execution_info: ExecutionInfo = ExecutionInfo(
selector=0x00e2054f8a912367e38a22ce773328ff8aabf8082c4120bad9ef085e1dbf29a7
);

local execution_context: ExecutionContext = ExecutionContext(
entry_point_type=ENTRY_POINT_TYPE_EXTERNAL,
Expand All @@ -112,5 +112,12 @@ func run_contract_bootloader{
let (retdata_size, retdata) = execute_entry_point(compiled_class, &execution_context);
}

let pedersen_ptr = cast(builtin_ptrs.selectable.pedersen, HashBuiltin*);
let ecdsa_ptr = builtin_ptrs.selectable.ecdsa;
let bitwise_ptr = builtin_ptrs.selectable.bitwise;
let ec_op_ptr = builtin_ptrs.selectable.ec_op;
let keccak_ptr = builtin_ptrs.non_selectable.keccak;
let poseidon_ptr = cast(builtin_ptrs.selectable.poseidon, PoseidonBuiltin*);

return ();
}
1 change: 0 additions & 1 deletion cairo0-bootloader/contract_class/compiled_class.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ from starkware.cairo.common.hash_state_poseidon import (
)
from starkware.cairo.common.math import assert_lt_felt
from starkware.cairo.common.poseidon_state import PoseidonBuiltinState
from starkware.cairo.common.registers import get_fp_and_pc

const COMPILED_CLASS_VERSION = 'COMPILED_CLASS_V1';

Expand Down
7 changes: 2 additions & 5 deletions cairo0-bootloader/contract_class/compiled_class_hash_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import os
from functools import lru_cache
from typing import List

from starkware.cairo.common.structs import CairoStructFactory, CairoStructProxy
from starkware.cairo.lang.cairo_constants import DEFAULT_PRIME
from starkware.cairo.lang.compiler.cairo_compile import compile_cairo_files
from starkware.cairo.lang.compiler.identifier_definition import ConstDefinition
from starkware.cairo.lang.compiler.identifier_manager import IdentifierManager
from starkware.cairo.lang.compiler.program import Program
from starkware.cairo.lang.compiler.scoped_name import ScopedName
from starkware.python.utils import as_non_optional, from_bytes
Expand Down Expand Up @@ -98,7 +95,7 @@ def get_compiled_class_struct(
assert len(bytecode) == len(compiled_class.bytecode)

return structs.CompiledClass(
compiled_class_version="",
compiled_class_version=22904329030628021342914013343516106642993, # COMPILED_CLASS_V1
n_external_functions=len(external_functions),
external_functions=flat_external_functions,
n_l1_handlers=len(l1_handlers),
Expand All @@ -107,4 +104,4 @@ def get_compiled_class_struct(
constructors=flat_constructors,
bytecode_length=len(bytecode),
bytecode_ptr=bytecode,
)
)
2 changes: 1 addition & 1 deletion cairo0-bootloader/contract_class/contract_class.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from starkware.cairo.common.hash_state_poseidon import (
)
from starkware.starknet.common.storage import normalize_address

const CONTRACT_CLASS_VERSION = 'CONTRACT_CLASS_V0.1.0';
const CONTRACT_CLASS_VERSION = 'COMPILED_CLASS_V1';

struct ContractEntryPoint {
// A field element that encodes the signature of the called function.
Expand Down
5 changes: 0 additions & 5 deletions cairo0-bootloader/contract_class/run.py

This file was deleted.

11 changes: 0 additions & 11 deletions cairo1/Scarb.toml

This file was deleted.

27 changes: 0 additions & 27 deletions cairo1/src/lib.cairo

This file was deleted.

2 changes: 2 additions & 0 deletions contract/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v

[[target.starknet-contract]]
sierra = true
casm = true
casm-add-pythonic-hints = true

[scripts]
test = "snforge test"

0 comments on commit 324d49a

Please sign in to comment.