Skip to content

Commit

Permalink
Merge pull request #39 from HerodotusDev/slr_contract_cairo1_package
Browse files Browse the repository at this point in the history
2 - slr contract & cairo1 package
  • Loading branch information
Okm165 authored Jul 16, 2024
2 parents c81800d + 0fc10b9 commit dc928ed
Show file tree
Hide file tree
Showing 51 changed files with 12,264 additions and 3,843 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI Build and Test Workflow
on:
pull_request:
branches:
- main
- '**' # This pattern matches all branches
jobs:
test:
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ src/hdp_input.json
# Whitelist - these files should not be ignored
!tests/cairo_programs/fixtures/*.json
!tools/js/package.json
!*_contract.json
!packages/cairo-lang-0.13.1.zip

# Scarb & snfoundry
target
.snfoundry_cache/
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ CAIRO_FILES := $(shell find ./tests/cairo_programs -name "*.cairo")
build: clean
@echo "Building project..."
./tools/make/build.sh
./tools/make/build_cairo1.sh
@echo "Build complete."

# Setup environment
setup:
@echo "Setting up the environment..."
./tools/make/install_cairo1_run.sh
./tools/make/build_cairo1.sh
./tools/make/setup.sh $(VENV_PATH)
@echo "Setup complete."

Expand Down
19 changes: 19 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "hdp_cairo"
version = "0.1.0"

[[package]]
name = "simple_linear_regression"
version = "0.1.0"
dependencies = [
"hdp_cairo",
"snforge_std",
]

[[package]]
name = "snforge_std"
version = "0.24.0"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.24.0#95e9fb09cb91b3c05295915179ee1b55bf923653"
12 changes: 12 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
name = "hdp_cairo"
version = "0.1.0"
edition = "2023_11"

members = [
"cairo",
"src/contracts/simple_linear_regression"
]

[workspace.dependencies]
hdp_cairo = { path = "cairo" }
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[package]
name = "simple_linear_regression"
name = "hdp_cairo"
version = "0.1.0"
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[cairo]
enable-gas = false

[dependencies]
[dependencies]
starknet = "2.6.3"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod header_memorizer;
pub mod memorizer;

#[derive(Serde, Drop)]
pub struct HDP {
Expand Down
1 change: 1 addition & 0 deletions cairo/src/memorizer.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod header_memorizer;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::option::OptionTrait;
use example::contract::hdp_context::{Memorizer};
use hdp_cairo::Memorizer;
use starknet::syscalls::call_contract_syscall;
use starknet::{SyscallResult, SyscallResultTrait};
use core::poseidon::poseidon_hash_span;
Expand Down
12 changes: 0 additions & 12 deletions compile.sh

This file was deleted.

18 changes: 15 additions & 3 deletions packages/contract_bootloader/contract_bootloader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from starkware.starknet.core.os.builtins import (
BuiltinInstanceSizes,
SelectableBuiltins,
)
from starkware.cairo.common.builtin_poseidon.poseidon import poseidon_hash_many
from contract_bootloader.execute_entry_point import execute_entry_point
from starkware.starknet.core.os.constants import ENTRY_POINT_TYPE_EXTERNAL
from contract_bootloader.execute_syscalls import ExecutionContext, ExecutionInfo
Expand All @@ -39,9 +40,6 @@ func run_contract_bootloader{
ec_op_ptr,
keccak_ptr: KeccakBuiltin*,
poseidon_ptr: PoseidonBuiltin*,
header_dict: DictAccess*,
headers: Header*,
pow2_array: felt*,
}(compiled_class: CompiledClass*, calldata_size: felt, calldata: felt*) -> (
retdata_size: felt, retdata: felt*
) {
Expand Down Expand Up @@ -108,3 +106,17 @@ func run_contract_bootloader{

return (retdata_size, retdata);
}

// Computes the hash of a program.
// Arguments:
// * program_data_ptr - the pointer to the program to be hashed.
// Return values:
// * hash - the computed program hash.
func compute_program_hash{poseidon_ptr: PoseidonBuiltin*}(
bytecode_length: felt, bytecode_ptr: felt*
) -> (hash: felt) {
let (hash) = poseidon_hash_many{poseidon_ptr=poseidon_ptr}(
n=bytecode_length, elements=bytecode_ptr
);
return (hash=hash);
}
56 changes: 56 additions & 0 deletions packages/contract_bootloader/dryrun_syscall_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from rlp import decode
from typing import (
Dict,
Iterable,
)
from tools.py.utils import little_8_bytes_chunks_to_bytes
from starkware.cairo.lang.vm.relocatable import RelocatableValue, MaybeRelocatable
from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager
from contract_bootloader.syscall_handler_base import SyscallHandlerBase
from starkware.cairo.common.dict import DictManager
from starkware.cairo.common.structs import CairoStructProxy
from tools.py.block_header import BlockHeaderDencun as Block
from starkware.cairo.lang.vm.crypto import poseidon_hash_many
from starkware.starknet.business_logic.execution.objects import (
CallResult,
)


class SyscallHandler(SyscallHandlerBase):
"""
A handler for system calls; used by the BusinessLogic entry point execution.
"""

def __init__(
self,
dict_manager: DictManager,
segments: MemorySegmentManager,
):
super().__init__(segments=segments, initial_syscall_ptr=None)
self.syscall_counter: Dict[str, int] = {}
self.dict_manager = dict_manager

def set_syscall_ptr(self, syscall_ptr: RelocatableValue):
assert self._syscall_ptr is None, "syscall_ptr is already set."
self._syscall_ptr = syscall_ptr

def allocate_segment(self, data: Iterable[MaybeRelocatable]) -> RelocatableValue:
segment_start = self.segments.add()
self.segments.write_arg(ptr=segment_start, arg=data)
return segment_start

def _allocate_segment_for_retdata(self, retdata: Iterable[int]) -> RelocatableValue:
return self.allocate_segment(data=retdata)

def _call_contract_helper(
self, request: CairoStructProxy, syscall_name: str
) -> CallResult:
calldata = self._get_felt_range(
start_addr=request.calldata_start, end_addr=request.calldata_end
)

return CallResult(
gas_consumed=0,
failure_flag=0,
retdata=[],
)
18 changes: 5 additions & 13 deletions packages/contract_bootloader/execute_entry_point.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ from starkware.starknet.core.os.constants import (
)
from contract_bootloader.contract_class.compiled_class import CompiledClass, CompiledClassEntryPoint
from contract_bootloader.execute_syscalls import ExecutionContext, execute_syscalls
from starkware.cairo.common.dict_access import DictAccess
from src.types import Header

// Represents the arguments pushed to the stack before calling an entry point.
struct EntryPointCallArguments {
Expand Down Expand Up @@ -52,9 +50,6 @@ func call_execute_syscalls{
poseidon_ptr: PoseidonBuiltin*,
syscall_ptr: felt*,
builtin_ptrs: BuiltinPointers*,
header_dict: DictAccess*,
headers: Header*,
pow2_array: felt*,
}(execution_context: ExecutionContext*, syscall_ptr_end: felt*) {
execute_syscalls(execution_context, syscall_ptr_end);
return ();
Expand Down Expand Up @@ -120,9 +115,6 @@ func execute_entry_point{
poseidon_ptr: PoseidonBuiltin*,
builtin_ptrs: BuiltinPointers*,
builtin_params: BuiltinParams*,
header_dict: DictAccess*,
headers: Header*,
pow2_array: felt*,
}(compiled_class: CompiledClass*, execution_context: ExecutionContext*) -> (
retdata_size: felt, retdata: felt*
) {
Expand All @@ -148,12 +140,12 @@ func execute_entry_point{
%{
from contract_bootloader.syscall_handler import SyscallHandler
ids.syscall_ptr = segments.add()
if '__dict_manager' not in globals():
raise RuntimeError('Dictionary manager is not initialized')
syscall_handler = SyscallHandler(segments=segments, dict_manager=__dict_manager)
from starkware.cairo.common.dict import DictManager
__dict_manager = DictManager()
ids.syscall_ptr = segments.add()
syscall_handler = SyscallHandler(segments=segments, dict_manager=__dict_manager)
syscall_handler.set_syscall_ptr(syscall_ptr=ids.syscall_ptr)
%}

Expand All @@ -176,7 +168,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
19 changes: 0 additions & 19 deletions packages/contract_bootloader/execute_syscalls.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ from starkware.starknet.common.new_syscalls import (
)
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin, PoseidonBuiltin
from starkware.starknet.core.os.builtins import BuiltinPointers
from starkware.cairo.common.dict_access import DictAccess
from src.types import Header
from src.memorizer import HeaderMemorizer, gen_header_key
from src.decoders.header_decoder import HeaderDecoder, HeaderField
from starkware.cairo.common.uint256 import Uint256, uint256_reverse_endian
Expand Down Expand Up @@ -41,9 +39,6 @@ func execute_syscalls{
poseidon_ptr: PoseidonBuiltin*,
syscall_ptr: felt*,
builtin_ptrs: BuiltinPointers*,
header_dict: DictAccess*,
headers: Header*,
pow2_array: felt*,
}(execution_context: ExecutionContext*, syscall_ptr_end: felt*) {
if (syscall_ptr == syscall_ptr_end) {
return ();
Expand All @@ -62,9 +57,6 @@ func execute_call_contract{
poseidon_ptr: PoseidonBuiltin*,
syscall_ptr: felt*,
builtin_ptrs: BuiltinPointers*,
header_dict: DictAccess*,
headers: Header*,
pow2_array: felt*,
}(caller_execution_context: ExecutionContext*) {
alloc_locals;
let request_header = cast(syscall_ptr, RequestHeader*);
Expand All @@ -73,23 +65,12 @@ func execute_call_contract{
let call_contract_request = cast(syscall_ptr, CallContractRequest*);
let syscall_ptr = syscall_ptr + CallContractRequest.SIZE;

let chain_id = call_contract_request.calldata_start[4];
let block_number = call_contract_request.calldata_start[5];

let header = HeaderMemorizer.get(chain_id=chain_id, block_number=block_number);
let parentHash = HeaderDecoder.get_field(header.rlp, HeaderField.PARENT);
let (value) = uint256_reverse_endian(parentHash);

let response_header = cast(syscall_ptr, ResponseHeader*);
let syscall_ptr = syscall_ptr + ResponseHeader.SIZE;

let call_contract_response = cast(syscall_ptr, CallContractResponse*);
let syscall_ptr = syscall_ptr + CallContractResponse.SIZE;

assert call_contract_response.retdata_end - call_contract_response.retdata_start = 2;
assert call_contract_response.retdata_start[0] = value.low;
assert call_contract_response.retdata_start[1] = value.high;

return ();
}

Expand Down
20 changes: 0 additions & 20 deletions packages/contract_bootloader/objects.py

This file was deleted.

11 changes: 0 additions & 11 deletions packages/hdp_bootloader/bootloader/builtins.py

This file was deleted.

Loading

0 comments on commit dc928ed

Please sign in to comment.