Skip to content

Commit

Permalink
Fix stuff. (#416)
Browse files Browse the repository at this point in the history
* Fix format. Remove stuff from where it shouldn't be. Make testing syscalls optional. Remove a lot of `TODO`s. Use `Felt` instead of a marker type for the prime modulo meta.

* Fix formatting.
  • Loading branch information
azteca1998 authored Jan 10, 2024
1 parent 906a1a3 commit 5e00369
Show file tree
Hide file tree
Showing 34 changed files with 80 additions and 318 deletions.
4 changes: 1 addition & 3 deletions examples/easy_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use cairo_native::context::NativeContext;
use cairo_native::executor::JitNativeExecutor;
use cairo_native::values::JitValue;
use cairo_native::{context::NativeContext, executor::JitNativeExecutor, values::JitValue};
use starknet_types_core::felt::Felt;
use std::path::Path;

Expand Down
58 changes: 3 additions & 55 deletions examples/erc20.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_starknet::contract_class::compile_path;
use cairo_native::context::NativeContext;
use cairo_native::executor::JitNativeExecutor;
use cairo_native::utils::find_entry_point_by_idx;
use cairo_native::{
context::NativeContext,
executor::JitNativeExecutor,
metadata::syscall_handler::SyscallHandlerMeta,
starknet::{BlockInfo, ExecutionInfo, StarkNetSyscallHandler, SyscallResult, TxInfo, U256},
utils::find_entry_point_by_idx,
};
use starknet_types_core::felt::Felt;
use std::path::Path;
Expand Down Expand Up @@ -224,58 +224,6 @@ impl StarkNetSyscallHandler for SyscallHandler {
) -> SyscallResult<Option<cairo_native::starknet::Secp256k1Point>> {
todo!()
}

fn pop_log(&mut self) {
todo!()
}

fn set_account_contract_address(&mut self, _contract_address: Felt) {
todo!()
}

fn set_block_number(&mut self, _block_number: u64) {
todo!()
}

fn set_block_timestamp(&mut self, _block_timestamp: u64) {
todo!()
}

fn set_caller_address(&mut self, _address: Felt) {
todo!()
}

fn set_chain_id(&mut self, _chain_id: Felt) {
todo!()
}

fn set_contract_address(&mut self, _address: Felt) {
todo!()
}

fn set_max_fee(&mut self, _max_fee: u128) {
todo!()
}

fn set_nonce(&mut self, _nonce: Felt) {
todo!()
}

fn set_sequencer_address(&mut self, _address: Felt) {
todo!()
}

fn set_signature(&mut self, _signature: &[Felt]) {
todo!()
}

fn set_transaction_hash(&mut self, _transaction_hash: Felt) {
todo!()
}

fn set_version(&mut self, _version: Felt) {
todo!()
}
}

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions examples/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cairo_native::executor::JitNativeExecutor;
use cairo_native::utils::find_entry_point;
use cairo_native::{context::NativeContext, values::JitValue};
use cairo_native::{
context::NativeContext, executor::JitNativeExecutor, utils::find_entry_point, values::JitValue,
};
use std::path::Path;
use tracing_subscriber::{EnvFilter, FmtSubscriber};

Expand Down
6 changes: 3 additions & 3 deletions examples/starknet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_starknet::contract_class::compile_path;
use cairo_native::context::NativeContext;
use cairo_native::executor::JitNativeExecutor;
use cairo_native::utils::find_entry_point_by_idx;
use cairo_native::{
context::NativeContext,
executor::JitNativeExecutor,
metadata::syscall_handler::SyscallHandlerMeta,
starknet::{BlockInfo, ExecutionInfo, StarkNetSyscallHandler, SyscallResult, TxInfo, U256},
utils::find_entry_point_by_idx,
};
use starknet_types_core::felt::Felt;
use std::path::Path;
Expand Down
18 changes: 0 additions & 18 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,3 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_try_finalize_nz(
true
}
}

// TODO: Remove from here.
#[no_mangle]
pub extern "C" fn __debug__breakpoint_marker() {
println!("[DEBUG] Breakpoint marker.");
}

// TODO: Remove from here.
#[no_mangle]
pub extern "C" fn __debug__print_i1(value: bool) {
println!("[DEBUG] {value}");
}

// TODO: Remove from here.
#[no_mangle]
pub extern "C" fn __debug__print_pointer(value: *const ()) {
println!("[DEBUG] {value:018x?}");
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use self::{
compile::Error as CompileError, jit_engine::Error as JitRunnerError,
libfuncs::Error as CoreLibfuncBuilderError, types::Error as CoreTypeBuilderError,
};

pub mod compile;
pub mod jit_engine;
pub mod libfuncs;
Expand Down
39 changes: 1 addition & 38 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,44 +484,7 @@ impl<'a> ArgumentMapper<'a> {
| (CoreTypeConcrete::Poseidon(_), _)
| (CoreTypeConcrete::RangeCheck(_), _)
| (CoreTypeConcrete::SegmentArena(_), _) => {}
(sierra_ty, arg_ty) => match sierra_ty {
CoreTypeConcrete::Array(_) => todo!("Array {arg_ty:?}"),
CoreTypeConcrete::Bitwise(_) => todo!("Bitwise {arg_ty:?}"),
CoreTypeConcrete::Box(_) => todo!("Box {arg_ty:?}"),
CoreTypeConcrete::EcOp(_) => todo!("EcOp {arg_ty:?}"),
CoreTypeConcrete::EcPoint(_) => todo!("EcPoint {arg_ty:?}"),
CoreTypeConcrete::EcState(_) => todo!("EcState {arg_ty:?}"),
CoreTypeConcrete::Felt252(_) => todo!("Felt252 {arg_ty:?}"),
CoreTypeConcrete::GasBuiltin(_) => todo!("GasBuiltin {arg_ty:?}"),
CoreTypeConcrete::BuiltinCosts(_) => todo!("BuiltinCosts {arg_ty:?}"),
CoreTypeConcrete::Uint8(_) => todo!("Uint8 {arg_ty:?}"),
CoreTypeConcrete::Uint16(_) => todo!("Uint16 {arg_ty:?}"),
CoreTypeConcrete::Uint32(_) => todo!("Uint32 {arg_ty:?}"),
CoreTypeConcrete::Uint64(_) => todo!("Uint64 {arg_ty:?}"),
CoreTypeConcrete::Uint128(_) => todo!("Uint128 {arg_ty:?}"),
CoreTypeConcrete::Uint128MulGuarantee(_) => todo!("Uint128MulGuarantee {arg_ty:?}"),
CoreTypeConcrete::Sint8(_) => todo!("Sint8 {arg_ty:?}"),
CoreTypeConcrete::Sint16(_) => todo!("Sint16 {arg_ty:?}"),
CoreTypeConcrete::Sint32(_) => todo!("Sint32 {arg_ty:?}"),
CoreTypeConcrete::Sint64(_) => todo!("Sint64 {arg_ty:?}"),
CoreTypeConcrete::Sint128(_) => todo!("Sint128 {arg_ty:?}"),
CoreTypeConcrete::NonZero(_) => todo!("NonZero {arg_ty:?}"),
CoreTypeConcrete::Nullable(_) => todo!("Nullable {arg_ty:?}"),
CoreTypeConcrete::RangeCheck(_) => todo!("RangeCheck {arg_ty:?}"),
CoreTypeConcrete::Uninitialized(_) => todo!("Uninitialized {arg_ty:?}"),
CoreTypeConcrete::Enum(_) => todo!("Enum {arg_ty:?}"),
CoreTypeConcrete::Struct(_) => todo!("Struct {arg_ty:?}"),
CoreTypeConcrete::Felt252Dict(_) => todo!("Felt252Dict {arg_ty:?}"),
CoreTypeConcrete::Felt252DictEntry(_) => todo!("Felt252DictEntry {arg_ty:?}"),
CoreTypeConcrete::SquashedFelt252Dict(_) => todo!("SquashedFelt252Dict {arg_ty:?}"),
CoreTypeConcrete::Pedersen(_) => todo!("Pedersen {arg_ty:?}"),
CoreTypeConcrete::Poseidon(_) => todo!("Poseidon {arg_ty:?}"),
CoreTypeConcrete::Span(_) => todo!("Span {arg_ty:?}"),
CoreTypeConcrete::StarkNet(_) => todo!("StarkNet {arg_ty:?}"),
CoreTypeConcrete::SegmentArena(_) => todo!("SegmentArena {arg_ty:?}"),
CoreTypeConcrete::Snapshot(_) => todo!("Snapshot {arg_ty:?}"),
CoreTypeConcrete::Bytes31(_) => todo!("Bytes31 {arg_ty:?}"),
},
(_, _) => todo!(),
}

Ok(())
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/ap_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
// TODO: Is `revoke_ap_tracking` also a no-op? If it turns out it is NOT a no-op, update the
// docs.
match selector {
ApTrackingConcreteLibfunc::Revoke(info) => {
build_revoke(context, registry, entry, location, helper, metadata, info)
Expand Down
5 changes: 2 additions & 3 deletions src/libfuncs/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
// same signature at sierra level
// Equivalent to `array_pop_front_consume` for our purposes.
build_pop_front(context, registry, entry, location, helper, metadata, info)
}

Expand All @@ -828,8 +828,7 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
// same signature at sierra level
// TODO: is this really equal?
// Equivalent to `array_pop_front` for our purposes.
build_pop_front(context, registry, entry, location, helper, metadata, info)
}

Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/bitwise.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # Bitwise libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions src/libfuncs/bytes31.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! # Struct-related libfuncs
//! # Bytes31-related libfuncs
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down Expand Up @@ -209,7 +209,7 @@ mod test {
use starknet_types_core::felt::Felt;

lazy_static! {
// todo: bytes31_const is not yet possible in current cairo version.
// TODO: Test `bytes31_const` once the compiler supports it.
static ref BYTES31_ROUNDTRIP: (String, Program) = load_cairo! {
use core::bytes_31::{bytes31_try_from_felt252, bytes31_to_felt252};

Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/cast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # Casting libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/debug.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![allow(dead_code)]

//! # Debug libfuncs
//!
//! TODO
// Printable: 9-13, 27, 32, 33-126
// is_ascii_graphic() -> 33-126
Expand Down
2 changes: 1 addition & 1 deletion src/libfuncs/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
// TODO: Implement drop for arrays.
// Note: Complex types implement drop within the type itself (in `build_drop`).

let ty = registry.get_type(&info.signature.param_signatures[0].ty)?;
ty.build_drop(
Expand Down
6 changes: 5 additions & 1 deletion src/libfuncs/dup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
// TODO: Handle non-trivially-copyable types (ex. arrays) and maybe update docs.
// Note: All non-trivially-copyable are automatically handled by the cairo compiler (to Sierra).
// In other words, this function will only be called for copyable types.
//
// Proof: The following code will fail in Cairo with an unsupported generic argument:
// `dup(ArrayTrait::<u8>::new())`.

entry.append_operation(helper.br(
0,
Expand Down
10 changes: 3 additions & 7 deletions src/libfuncs/ec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # Elliptic curve libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand All @@ -11,10 +9,7 @@ use crate::{
metadata::{
prime_modulo::PrimeModuloMeta, runtime_bindings::RuntimeBindingsMeta, MetadataStorage,
},
types::{
felt252::{register_prime_modulo_meta, Felt252},
TypeBuilder,
},
types::{felt252::register_prime_modulo_meta, TypeBuilder},
utils::{get_integer_layout, ProgramRegistryExt},
};
use cairo_lang_sierra::{
Expand All @@ -37,6 +32,7 @@ use melior::{
},
Context,
};
use starknet_types_core::felt::Felt;

/// Select and call the correct libfunc builder function from the selector.
pub fn build<'ctx, 'this, TType, TLibfunc>(
Expand Down Expand Up @@ -192,7 +188,7 @@ where
context,
&format!(
"{} : i252",
match metadata.get::<PrimeModuloMeta<Felt252>>() {
match metadata.get::<PrimeModuloMeta<Felt>>() {
Some(x) => x.prime(),
None => {
// Since the `EcPoint` type is external, there is no guarantee that
Expand Down
13 changes: 7 additions & 6 deletions src/libfuncs/felt252.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
CoreTypeBuilderError,
},
metadata::{prime_modulo::PrimeModuloMeta, MetadataStorage},
types::{felt252::Felt252, TypeBuilder},
types::TypeBuilder,
utils::{mlir_asm, ProgramRegistryExt},
};
use cairo_lang_sierra::{
Expand All @@ -33,6 +33,7 @@ use melior::{
Context,
};
use num_bigint::{Sign, ToBigInt};
use starknet_types_core::felt::Felt;

/// Select and call the correct libfunc builder function from the selector.
pub fn build<'ctx, 'this, TType, TLibfunc>(
Expand Down Expand Up @@ -99,7 +100,7 @@ where
&format!(
"{} : {i256}",
metadata
.get::<PrimeModuloMeta<Felt252>>()
.get::<PrimeModuloMeta<Felt>>()
.ok_or(ErrorImpl::MissingMetadata)?
.prime()
),
Expand All @@ -110,7 +111,7 @@ where
&format!(
"{} : {i512}",
metadata
.get::<PrimeModuloMeta<Felt252>>()
.get::<PrimeModuloMeta<Felt>>()
.ok_or(ErrorImpl::MissingMetadata)?
.prime()
),
Expand Down Expand Up @@ -138,7 +139,7 @@ where
let value = match operation.c.sign() {
Sign::Minus => {
let prime = metadata
.get::<PrimeModuloMeta<Felt252>>()
.get::<PrimeModuloMeta<Felt>>()
.ok_or(ErrorImpl::MissingMetadata)?
.prime();
(&operation.c + prime.to_bigint().expect("always is Some"))
Expand All @@ -151,7 +152,7 @@ where
let attr_c = Attribute::parse(context, &format!("{value} : {felt252_ty}"))
.ok_or(ErrorImpl::MissingMetadata)?;

// TODO: Ensure that the constant is on the right side of the operation.
// TODO: Ensure that the constant is on the correct side of the operation.
mlir_asm! { context, entry, location =>
; rhs = "arith.constant"() { "value" = attr_c } : () -> felt252_ty
}
Expand Down Expand Up @@ -420,7 +421,7 @@ where
let value = match info.c.sign() {
Sign::Minus => {
let prime = metadata
.get::<PrimeModuloMeta<Felt252>>()
.get::<PrimeModuloMeta<Felt>>()
.ok_or(ErrorImpl::MissingMetadata)?
.prime();
(&info.c + prime.to_bigint().expect("always is Some"))
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/felt252_dict_entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # `Felt` dictionary entry libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/gas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # Gas management libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/snapshot_take.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # Snapshot taking libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
2 changes: 0 additions & 2 deletions src/libfuncs/stark_net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! # StarkNet libfuncs
//!
//! TODO
use super::{LibfuncBuilder, LibfuncHelper};
use crate::{
Expand Down
Loading

0 comments on commit 5e00369

Please sign in to comment.