Skip to content

update to 2.10.0rc0 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
445 changes: 186 additions & 259 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ readme = "README.md"
keywords = ["starknet", "cairo", "compiler", "mlir"]

[dependencies]
cairo-lang-compiler = "2.9.1"
cairo-lang-filesystem = "2.9.1"
cairo-lang-runner = "2.9.1"
cairo-lang-sierra = "2.9.1"
cairo-lang-sierra-ap-change = "2.9.1"
cairo-lang-sierra-gas = "2.9.1"
cairo-lang-starknet-classes = "2.9.1"
cairo-lang-utils = "2.9.1"
cairo-lang-compiler = "2.10.0-rc.0"
cairo-lang-filesystem = "2.10.0-rc.0"
cairo-lang-runner = "2.10.0-rc.0"
cairo-lang-sierra = "2.10.0-rc.0"
cairo-lang-sierra-ap-change = "2.10.0-rc.0"
cairo-lang-sierra-gas = "2.10.0-rc.0"
cairo-lang-starknet-classes = "2.10.0-rc.0"
cairo-lang-utils = "2.10.0-rc.0"
clap = { version = "4.5.21", features = ["derive"] }
k256 = "0.13.4"
keccak = "0.1.5"
Expand All @@ -38,8 +38,8 @@ tracing = "0.1.41"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[dev-dependencies]
cairo-lang-compiler = "2.9.1"
cairo-lang-starknet = "2.9.1"
cairo-lang-compiler = "2.10.0-rc.0"
cairo-lang-starknet = "2.10.0-rc.0"

# On dev optimize dependencies a bit so it's not as slow.
[profile.dev.package."*"]
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

UNAME := $(shell uname)

CAIRO_2_VERSION=2.9.1
SCARB_VERSION = 2.8.5
CAIRO_2_VERSION=2.10.0-rc.0
SCARB_VERSION = 2.9.2

needs-cairo2:
ifeq ($(wildcard ./cairo2/.),)
Expand Down
1 change: 1 addition & 0 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ pub fn libfunc_to_name(value: &CoreConcreteLibfunc) -> &'static str {
BoundedIntConcreteLibfunc::Constrain(_) => "bounded_int_constrain",
BoundedIntConcreteLibfunc::IsZero(_) => "bounded_int_is_zero",
BoundedIntConcreteLibfunc::WrapNonZero(_) => "bounded_int_wrap_non_zero",
BoundedIntConcreteLibfunc::Trim(_) => "bounded_int_trim",
},
CoreConcreteLibfunc::IntRange(selector) => match selector {
IntRangeConcreteLibfunc::TryNew(_) => "int_range_try_new",
Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ mod test {

use cairo_lang_compiler::CompilerConfig;
use cairo_lang_starknet::compile::compile_path;
use sierra_emu::{
starknet::StubSyscallHandler, ContractExecutionResult,
VirtualMachine,
};
use sierra_emu::{starknet::StubSyscallHandler, ContractExecutionResult, VirtualMachine};

#[test]
fn test_contract() {
Expand Down
5 changes: 1 addition & 4 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use cairo_lang_compiler::{
use cairo_lang_filesystem::db::init_dev_corelib;
use cairo_lang_sierra::program::Program;

use crate::{
find_entry_point_by_idx, starknet::StubSyscallHandler, Value,
VirtualMachine,
};
use crate::{find_entry_point_by_idx, starknet::StubSyscallHandler, Value, VirtualMachine};

#[macro_export]
macro_rules! load_cairo {
Expand Down
6 changes: 4 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub enum Value {
y1: Felt,
},
I128(i128),
I64(i64),
I32(i32),
I16(i16),
I8(i8),
Struct(Vec<Self>),
U256(u128, u128),
Expand Down Expand Up @@ -177,8 +179,8 @@ impl Value {
CoreTypeConcrete::Uint16(_) => matches!(self, Self::U16(_)),
CoreTypeConcrete::Uint64(_) => matches!(self, Self::U64(_)),
CoreTypeConcrete::Uint128MulGuarantee(_) => matches!(self, Self::Unit),
CoreTypeConcrete::Sint16(_) => todo!(),
CoreTypeConcrete::Sint64(_) => todo!(),
CoreTypeConcrete::Sint16(_) => matches!(self, Self::I16(_)),
CoreTypeConcrete::Sint64(_) => matches!(self, Self::I64(_)),
CoreTypeConcrete::Nullable(info) => self.is(registry, &info.ty),
CoreTypeConcrete::Uninitialized(_) => matches!(self, Self::Uninitialized { .. }),
CoreTypeConcrete::Felt252DictEntry(info) => {
Expand Down
12 changes: 9 additions & 3 deletions src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
debug::libfunc_to_name, gas::{GasMetadata, MetadataComputationConfig}, starknet::StarknetSyscallHandler, ProgramTrace, StateDump, Value
debug::libfunc_to_name,
gas::{GasMetadata, MetadataComputationConfig},
starknet::StarknetSyscallHandler,
ProgramTrace, StateDump, Value,
};
use cairo_lang_sierra::{
edit_state,
Expand Down Expand Up @@ -374,8 +377,11 @@ impl VirtualMachine {
Some((pc_snapshot, state_snapshot))
}

/// Run all the statement and return the trace.
pub fn run_with_trace(&mut self, syscall_handler: &mut impl StarknetSyscallHandler) -> ProgramTrace {
/// Run all the statement and return the trace.
pub fn run_with_trace(
&mut self,
syscall_handler: &mut impl StarknetSyscallHandler,
) -> ProgramTrace {
let mut trace = ProgramTrace::new();

while let Some((statement_idx, state)) = self.step(syscall_handler) {
Expand Down
127 changes: 125 additions & 2 deletions src/vm/bounded_int.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::EvalAction;
use crate::Value;
use crate::{debug::debug_signature, Value};
use cairo_lang_sierra::{
extensions::{
bounded_int::{
BoundedIntConcreteLibfunc, BoundedIntConstrainConcreteLibfunc,
BoundedIntDivRemConcreteLibfunc,
BoundedIntDivRemConcreteLibfunc, BoundedIntTrimConcreteLibfunc,
},
core::{CoreLibfunc, CoreType, CoreTypeConcrete},
lib_func::SignatureOnlyConcreteLibfunc,
ConcreteLibfunc,
},
program_registry::ProgramRegistry,
};
use num_bigint::BigInt;
use smallvec::smallvec;

pub fn eval(
Expand All @@ -27,6 +28,7 @@ pub fn eval(
BoundedIntConcreteLibfunc::Constrain(info) => eval_constrain(registry, info, args),
BoundedIntConcreteLibfunc::IsZero(info) => eval_is_zero(registry, info, args),
BoundedIntConcreteLibfunc::WrapNonZero(info) => eval_wrap_non_zero(registry, info, args),
BoundedIntConcreteLibfunc::Trim(info) => eval_trim(registry, info, args),
}
}

Expand Down Expand Up @@ -271,3 +273,124 @@ pub fn eval_wrap_non_zero(

EvalAction::NormalBranch(0, smallvec![value])
}

pub fn eval_trim(
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
info: &BoundedIntTrimConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [value] = args.try_into().unwrap();
let value = match value {
Value::I8(v) => BigInt::from(v),
Value::I16(v) => BigInt::from(v),
Value::I32(v) => BigInt::from(v),
Value::I64(v) => BigInt::from(v),
Value::I128(v) => BigInt::from(v),
Value::U8(v) => BigInt::from(v),
Value::U16(v) => BigInt::from(v),
Value::U32(v) => BigInt::from(v),
Value::U64(v) => BigInt::from(v),
Value::U128(v) => BigInt::from(v),
_ => panic!("Not a valid integer type"),
};
let is_invalid = value == info.trimmed_value;
let int_range = match registry
.get_type(&info.branch_signatures()[1].vars[0].ty)
.unwrap()
{
CoreTypeConcrete::BoundedInt(info) => info.range.clone(),
_ => panic!("should be bounded int"),
};

if !is_invalid {
let range = int_range.lower.clone()..int_range.upper.clone();
EvalAction::NormalBranch(1, smallvec![Value::BoundedInt { range, value }])
} else {
EvalAction::NormalBranch(0, smallvec![])
}
}

#[cfg(test)]
mod tests {

use num_bigint::BigInt;

use super::Value;

use crate::{load_cairo, test_utils::run_test_program};

#[test]
fn test_trim_i8() {
let (_, program) = load_cairo!(
use core::internal::{OptionRev, bounded_int::BoundedInt};
use core::internal::bounded_int;
fn main() -> BoundedInt<-127, 127> {
let num = match bounded_int::trim::<i8, -0x80>(1) {
OptionRev::Some(n) => n,
OptionRev::None => 1,
};

num
}
);

let result = run_test_program(program);
let result = result.last().unwrap();
let expected = Value::BoundedInt {
range: BigInt::from(-127)..BigInt::from(128),
value: BigInt::from(1u8),
};

assert_eq!(*result, expected);
}

#[test]
fn test_trim_u32() {
let (_, program) = load_cairo!(
use core::internal::{OptionRev, bounded_int::BoundedInt};
use core::internal::bounded_int;
fn main() -> BoundedInt<0, 4294967294> {
let num = match bounded_int::trim::<u32, 0xffffffff>(0xfffffffe) {
OptionRev::Some(n) => n,
OptionRev::None => 0,
};

num
}
);

let result = run_test_program(program);
let result = result.last().unwrap();
let expected = Value::BoundedInt {
range: BigInt::from(0)..BigInt::from(4294967295u32),
value: BigInt::from(0xfffffffeu32),
};

assert_eq!(*result, expected);
}

#[test]
fn test_trim_none() {
let (_, program) = load_cairo!(
use core::internal::{OptionRev, bounded_int::BoundedInt};
use core::internal::bounded_int;
fn main() -> BoundedInt<-32767, 32767> {
let num = match bounded_int::trim::<i16, -0x8000>(-0x8000) {
OptionRev::Some(n) => n,
OptionRev::None => 0,
};

num
}
);

let result = run_test_program(program);
let result = result.last().unwrap();
let expected = Value::BoundedInt {
range: BigInt::from(-32767)..BigInt::from(32768),
value: BigInt::from(0),
};

assert_eq!(*result, expected);
}
}
28 changes: 20 additions & 8 deletions src/vm/const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,30 @@ fn inner(
[GenericArg::Value(value)] => Value::I128(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Sint64(_) => match inner_data {
[GenericArg::Value(value)] => Value::U64(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Sint32(_) => match inner_data {
[GenericArg::Value(value)] => Value::I32(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Sint16(_) => match inner_data {
[GenericArg::Value(value)] => Value::I16(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Sint8(_) => match inner_data {
[GenericArg::Value(value)] => Value::I8(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Uint128(_) => match inner_data {
[GenericArg::Value(value)] => Value::U128(value.try_into().unwrap()),
[GenericArg::Type(type_id)] => match registry.get_type(type_id).unwrap() {
CoreTypeConcrete::Const(info) => inner(registry, &info.inner_ty, &info.inner_data),
_ => unreachable!(),
},
_ => unreachable!(),
},
CoreTypeConcrete::Uint64(_) => match inner_data {
[GenericArg::Value(value)] => Value::U64(value.try_into().unwrap()),
_ => unreachable!(),
Expand All @@ -113,16 +129,12 @@ fn inner(
},
_ => unreachable!(),
},
CoreTypeConcrete::Uint8(_) => match inner_data {
[GenericArg::Value(value)] => Value::U8(value.try_into().unwrap()),
CoreTypeConcrete::Uint16(_) => match inner_data {
[GenericArg::Value(value)] => Value::U16(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Uint128(_) => match inner_data {
[GenericArg::Value(value)] => Value::U128(value.try_into().unwrap()),
[GenericArg::Type(type_id)] => match registry.get_type(type_id).unwrap() {
CoreTypeConcrete::Const(info) => inner(registry, &info.inner_ty, &info.inner_data),
_ => unreachable!(),
},
CoreTypeConcrete::Uint8(_) => match inner_data {
[GenericArg::Value(value)] => Value::U8(value.try_into().unwrap()),
_ => unreachable!(),
},
CoreTypeConcrete::Struct(_) => {
Expand Down
25 changes: 24 additions & 1 deletion src/vm/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub fn eval(
GasConcreteLibfunc::WithdrawGas(info) => {
eval_withdraw_gas(registry, info, args, gas, statement_idx)
}
GasConcreteLibfunc::RedepositGas(_) => todo!(),
GasConcreteLibfunc::RedepositGas(info) => {
eval_redeposit_gas(registry, info, args, gas, statement_idx)
}
GasConcreteLibfunc::GetAvailableGas(_) => todo!(),
GasConcreteLibfunc::BuiltinWithdrawGas(info) => {
eval_builtin_withdraw_gas(registry, info, args, gas, statement_idx)
Expand Down Expand Up @@ -83,6 +85,27 @@ pub fn eval_withdraw_gas(
}
}

pub fn eval_redeposit_gas(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
gas_meta: &GasMetadata,
statement_idx: StatementIdx,
) -> EvalAction {
let [Value::U64(gas)]: [Value; 1] = args.try_into().unwrap() else {
panic!()
};

match gas_meta.get_gas_cost_for_statement(statement_idx) {
Some(c) => {
let new_gas = gas.saturating_add(c);

EvalAction::NormalBranch(0, smallvec![Value::U64(new_gas)])
}
None => EvalAction::NormalBranch(0, smallvec![Value::U64(gas)]),
}
}

pub fn eval_get_builtin_costs(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
Expand Down
1 change: 0 additions & 1 deletion tests/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fn run_syscall(func_name: &str) -> ProgramTrace {

let syscall_handler = &mut StubSyscallHandler::default();


vm.run_with_trace(syscall_handler)
}

Expand Down
Loading