Skip to content

Commit 16e5b34

Browse files
Merge branch 'main' into libfunc_counter
2 parents d5a557d + 4e2c9ef commit 16e5b34

File tree

20 files changed

+478
-103
lines changed

20 files changed

+478
-103
lines changed

.github/workflows/starknet-blocks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
with:
4444
repository: lambdaclass/sequencer
4545
path: sequencer
46-
ref: 7aaf0e38c2c80276b8121bca5df8e8389bcdc2f6
46+
ref: c12c6a72f2375413fc5ba04e50af87ef21096784
4747
- name: Cache RPC Calls
4848
uses: actions/cache@v4.2.0
4949
with:

src/execution_result.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,27 @@ use starknet_types_core::felt::Felt;
1919
serde::Deserialize,
2020
)]
2121
pub struct BuiltinStats {
22-
pub bitwise: usize,
23-
pub ec_op: usize,
2422
pub range_check: usize,
2523
pub pedersen: usize,
24+
pub bitwise: usize,
25+
pub ec_op: usize,
2626
pub poseidon: usize,
2727
pub segment_arena: usize,
28-
pub range_check_96: usize,
29-
pub circuit_add: usize,
30-
pub circuit_mul: usize,
28+
pub range_check96: usize,
29+
pub add_mod: usize,
30+
pub mul_mod: usize,
3131
}
3232

33+
pub const RANGE_CHECK_BUILTIN_SIZE: usize = 1;
34+
pub const PEDERSEN_BUILTIN_SIZE: usize = 3;
35+
pub const BITWISE_BUILTIN_SIZE: usize = 5;
36+
pub const EC_OP_BUILTIN_SIZE: usize = 7;
37+
pub const POSEIDON_BUILTIN_SIZE: usize = 6;
38+
pub const SEGMENT_ARENA_BUILTIN_SIZE: usize = 3;
39+
pub const RANGE_CHECK96_BUILTIN_SIZE: usize = 1;
40+
pub const ADD_MOD_BUILTIN_SIZE: usize = 7;
41+
pub const MUL_MOD_BUILTIN_SIZE: usize = 7;
42+
3343
/// The result of the JIT execution.
3444
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
3545
pub struct ExecutionResult {
@@ -56,6 +66,7 @@ pub struct ContractExecutionResult {
5666
pub failure_flag: bool,
5767
pub return_values: Vec<Felt>,
5868
pub error_msg: Option<String>,
69+
pub builtin_stats: BuiltinStats,
5970
}
6071

6172
impl ContractExecutionResult {
@@ -156,6 +167,7 @@ impl ContractExecutionResult {
156167
return_values,
157168
failure_flag,
158169
error_msg,
170+
builtin_stats: result.builtin_stats,
159171
})
160172
}
161173
}

src/executor.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ pub use self::{aot::AotNativeExecutor, contract::AotContractExecutor, jit::JitNa
77
use crate::{
88
arch::{AbiArgument, ValueWithInfoWrapper},
99
error::{panic::ToNativeAssertError, Error},
10-
execution_result::{BuiltinStats, ExecutionResult},
10+
execution_result::{
11+
BuiltinStats, ExecutionResult, ADD_MOD_BUILTIN_SIZE, BITWISE_BUILTIN_SIZE,
12+
EC_OP_BUILTIN_SIZE, MUL_MOD_BUILTIN_SIZE, PEDERSEN_BUILTIN_SIZE, POSEIDON_BUILTIN_SIZE,
13+
RANGE_CHECK96_BUILTIN_SIZE, RANGE_CHECK_BUILTIN_SIZE, SEGMENT_ARENA_BUILTIN_SIZE,
14+
},
1115
native_panic,
1216
runtime::BUILTIN_COSTS,
1317
starknet::{handler::StarknetSyscallHandlerCallbacks, StarknetSyscallHandler},
@@ -277,22 +281,32 @@ fn invoke_dynamic(
277281
} as usize;
278282

279283
match type_info {
280-
CoreTypeConcrete::Bitwise(_) => builtin_stats.bitwise = value,
281-
CoreTypeConcrete::EcOp(_) => builtin_stats.ec_op = value,
282-
CoreTypeConcrete::RangeCheck(_) => builtin_stats.range_check = value,
283-
CoreTypeConcrete::Pedersen(_) => builtin_stats.pedersen = value,
284-
CoreTypeConcrete::Poseidon(_) => builtin_stats.poseidon = value,
284+
CoreTypeConcrete::RangeCheck(_) => {
285+
builtin_stats.range_check = value / RANGE_CHECK_BUILTIN_SIZE
286+
}
287+
CoreTypeConcrete::Pedersen(_) => {
288+
builtin_stats.pedersen = value / PEDERSEN_BUILTIN_SIZE
289+
}
290+
CoreTypeConcrete::Bitwise(_) => {
291+
builtin_stats.bitwise = value / BITWISE_BUILTIN_SIZE
292+
}
293+
CoreTypeConcrete::EcOp(_) => {
294+
builtin_stats.ec_op = value / EC_OP_BUILTIN_SIZE
295+
}
296+
CoreTypeConcrete::Poseidon(_) => {
297+
builtin_stats.poseidon = value / POSEIDON_BUILTIN_SIZE
298+
}
285299
CoreTypeConcrete::SegmentArena(_) => {
286-
builtin_stats.segment_arena = value
300+
builtin_stats.segment_arena = value / SEGMENT_ARENA_BUILTIN_SIZE
287301
}
288302
CoreTypeConcrete::RangeCheck96(_) => {
289-
builtin_stats.range_check_96 = value
303+
builtin_stats.range_check96 = value / RANGE_CHECK96_BUILTIN_SIZE
290304
}
291305
CoreTypeConcrete::Circuit(CircuitTypeConcrete::AddMod(_)) => {
292-
builtin_stats.circuit_add = value
306+
builtin_stats.add_mod = value / ADD_MOD_BUILTIN_SIZE
293307
}
294308
CoreTypeConcrete::Circuit(CircuitTypeConcrete::MulMod(_)) => {
295-
builtin_stats.circuit_mul = value
309+
builtin_stats.mul_mod = value / MUL_MOD_BUILTIN_SIZE
296310
}
297311
_ => native_panic!("given type should be a builtin: {type_id:?}"),
298312
}

src/executor/contract.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ use crate::{
3737
context::NativeContext,
3838
debug::libfunc_to_name,
3939
error::{panic::ToNativeAssertError, Error, Result},
40-
execution_result::{BuiltinStats, ContractExecutionResult},
40+
execution_result::{
41+
BuiltinStats, ContractExecutionResult, ADD_MOD_BUILTIN_SIZE, BITWISE_BUILTIN_SIZE,
42+
EC_OP_BUILTIN_SIZE, MUL_MOD_BUILTIN_SIZE, PEDERSEN_BUILTIN_SIZE, POSEIDON_BUILTIN_SIZE,
43+
RANGE_CHECK96_BUILTIN_SIZE, RANGE_CHECK_BUILTIN_SIZE, SEGMENT_ARENA_BUILTIN_SIZE,
44+
},
4145
executor::{invoke_trampoline, BuiltinCostsGuard},
4246
metadata::runtime_bindings::setup_runtime,
4347
module::NativeModule,
@@ -530,15 +534,31 @@ impl AotContractExecutor {
530534
let value = unsafe { *read_value::<u64>(return_ptr) } as usize;
531535

532536
match x {
533-
BuiltinType::Bitwise => builtin_stats.bitwise = value,
534-
BuiltinType::EcOp => builtin_stats.ec_op = value,
535-
BuiltinType::RangeCheck => builtin_stats.range_check = value,
536-
BuiltinType::SegmentArena => builtin_stats.segment_arena = value,
537-
BuiltinType::Poseidon => builtin_stats.poseidon = value,
538-
BuiltinType::Pedersen => builtin_stats.pedersen = value,
539-
BuiltinType::RangeCheck96 => builtin_stats.range_check_96 = value,
540-
BuiltinType::CircuitAdd => builtin_stats.circuit_add = value,
541-
BuiltinType::CircuitMul => builtin_stats.circuit_mul = value,
537+
BuiltinType::RangeCheck => {
538+
builtin_stats.range_check = value / RANGE_CHECK_BUILTIN_SIZE
539+
}
540+
BuiltinType::Pedersen => {
541+
builtin_stats.pedersen = value / PEDERSEN_BUILTIN_SIZE
542+
}
543+
BuiltinType::Bitwise => {
544+
builtin_stats.bitwise = value / BITWISE_BUILTIN_SIZE
545+
}
546+
BuiltinType::EcOp => builtin_stats.ec_op = value / EC_OP_BUILTIN_SIZE,
547+
BuiltinType::Poseidon => {
548+
builtin_stats.poseidon = value / POSEIDON_BUILTIN_SIZE
549+
}
550+
BuiltinType::SegmentArena => {
551+
builtin_stats.segment_arena = value / SEGMENT_ARENA_BUILTIN_SIZE
552+
}
553+
BuiltinType::RangeCheck96 => {
554+
builtin_stats.range_check96 = value / RANGE_CHECK96_BUILTIN_SIZE
555+
}
556+
BuiltinType::CircuitAdd => {
557+
builtin_stats.add_mod = value / ADD_MOD_BUILTIN_SIZE
558+
}
559+
BuiltinType::CircuitMul => {
560+
builtin_stats.mul_mod = value / MUL_MOD_BUILTIN_SIZE
561+
}
542562
BuiltinType::Gas => {}
543563
BuiltinType::System => {}
544564
BuiltinType::BuiltinCosts => {}
@@ -622,6 +642,7 @@ impl AotContractExecutor {
622642
failure_flag: tag != 0,
623643
return_values: array_value,
624644
error_msg,
645+
builtin_stats,
625646
})
626647
}
627648

src/libfuncs.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl LibfuncBuilder for CoreConcreteLibfunc {
102102
) -> Result<()> {
103103
match self {
104104
Self::ApTracking(_) | Self::BranchAlign(_) | Self::UnconditionalJump(_) => {
105-
build_noop::<0, true>(
105+
build_noop::<0, false>(
106106
context,
107107
registry,
108108
entry,
@@ -234,7 +234,7 @@ impl LibfuncBuilder for CoreConcreteLibfunc {
234234
Self::Uint512(selector) => self::uint512::build(
235235
context, registry, entry, location, helper, metadata, selector,
236236
),
237-
Self::UnwrapNonZero(info) => build_noop::<1, true>(
237+
Self::UnwrapNonZero(info) => build_noop::<1, false>(
238238
context,
239239
registry,
240240
entry,
@@ -497,6 +497,31 @@ fn increment_builtin_counter_by<'ctx: 'a, 'a>(
497497
))
498498
}
499499

500+
fn increment_builtin_counter_by_if<'ctx: 'a, 'a>(
501+
context: &'ctx Context,
502+
block: &'ctx Block<'ctx>,
503+
location: Location<'ctx>,
504+
value_to_inc: Value<'ctx, '_>,
505+
true_amount: impl Into<BigInt>,
506+
false_amount: impl Into<BigInt>,
507+
condition: Value<'ctx, '_>,
508+
) -> crate::error::Result<Value<'ctx, 'a>> {
509+
let true_amount_value = block.const_int(context, location, true_amount, 64)?;
510+
let false_amount_value = block.const_int(context, location, false_amount, 64)?;
511+
512+
let true_incremented =
513+
block.append_op_result(arith::addi(value_to_inc, true_amount_value, location))?;
514+
let false_incremented =
515+
block.append_op_result(arith::addi(value_to_inc, false_amount_value, location))?;
516+
517+
block.append_op_result(arith::select(
518+
condition,
519+
true_incremented,
520+
false_incremented,
521+
location,
522+
))
523+
}
524+
500525
fn build_noop<'ctx, 'this, const N: usize, const PROCESS_BUILTINS: bool>(
501526
context: &'ctx Context,
502527
registry: &ProgramRegistry<CoreType, CoreLibfunc>,

src/libfuncs/bounded_int.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use super::LibfuncHelper;
44
use crate::{
5-
error::Result,
5+
error::{panic::ToNativeAssertError, Result},
6+
execution_result::RANGE_CHECK_BUILTIN_SIZE,
67
metadata::MetadataStorage,
78
native_assert,
89
types::TypeBuilder,
@@ -12,7 +13,8 @@ use cairo_lang_sierra::{
1213
extensions::{
1314
bounded_int::{
1415
BoundedIntConcreteLibfunc, BoundedIntConstrainConcreteLibfunc,
15-
BoundedIntDivRemConcreteLibfunc, BoundedIntTrimConcreteLibfunc,
16+
BoundedIntDivRemAlgorithm, BoundedIntDivRemConcreteLibfunc,
17+
BoundedIntTrimConcreteLibfunc,
1618
},
1719
core::{CoreLibfunc, CoreType},
1820
lib_func::SignatureOnlyConcreteLibfunc,
@@ -453,8 +455,6 @@ fn build_divrem<'ctx, 'this>(
453455
_metadata: &mut MetadataStorage,
454456
info: &BoundedIntDivRemConcreteLibfunc,
455457
) -> Result<()> {
456-
let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?;
457-
458458
let lhs_value = entry.arg(1)?;
459459
let rhs_value = entry.arg(2)?;
460460

@@ -482,6 +482,12 @@ fn build_divrem<'ctx, 'this>(
482482
rhs_range.zero_based_bit_width()
483483
};
484484

485+
let div_rem_algorithm = BoundedIntDivRemAlgorithm::try_new(&lhs_range, &rhs_range)
486+
.to_native_assert_error(&format!(
487+
"div_rem of ranges: lhs = {:#?} and rhs= {:#?} is not supported yet",
488+
&lhs_range, &rhs_range
489+
))?;
490+
485491
// Calculate the computation range.
486492
let compute_range = Range {
487493
lower: (&lhs_range.lower)
@@ -583,6 +589,32 @@ fn build_divrem<'ctx, 'this>(
583589
rem_value
584590
};
585591

592+
// Increase range check builtin by 3, regardless of `div_rem_algorithm`:
593+
// https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L100
594+
let range_check = match div_rem_algorithm {
595+
BoundedIntDivRemAlgorithm::KnownSmallRhs => crate::libfuncs::increment_builtin_counter_by(
596+
context,
597+
entry,
598+
location,
599+
entry.arg(0)?,
600+
3 * RANGE_CHECK_BUILTIN_SIZE,
601+
)?,
602+
BoundedIntDivRemAlgorithm::KnownSmallQuotient { .. }
603+
| BoundedIntDivRemAlgorithm::KnownSmallLhs { .. } => {
604+
// If `div_rem_algorithm` is `KnownSmallQuotient` or `KnownSmallLhs`, increase range check builtin by 1.
605+
//
606+
// Case KnownSmallQuotient: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L129
607+
// Case KnownSmallLhs: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L157
608+
crate::libfuncs::increment_builtin_counter_by(
609+
context,
610+
entry,
611+
location,
612+
entry.arg(0)?,
613+
4 * RANGE_CHECK_BUILTIN_SIZE,
614+
)?
615+
}
616+
};
617+
586618
helper.br(entry, 0, &[range_check, div_value, rem_value], location)
587619
}
588620

@@ -805,7 +837,7 @@ fn build_wrap_non_zero<'ctx, 'this>(
805837
"value must not be zero"
806838
);
807839

808-
super::build_noop::<1, true>(
840+
super::build_noop::<1, false>(
809841
context,
810842
registry,
811843
entry,

src/libfuncs/box.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn build<'ctx, 'this>(
4343
BoxConcreteLibfunc::Unbox(info) => {
4444
build_unbox(context, registry, entry, location, helper, metadata, info)
4545
}
46-
BoxConcreteLibfunc::ForwardSnapshot(info) => super::build_noop::<1, true>(
46+
BoxConcreteLibfunc::ForwardSnapshot(info) => super::build_noop::<1, false>(
4747
context,
4848
registry,
4949
entry,

src/libfuncs/bytes31.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ pub fn build_from_felt252<'ctx, 'this>(
110110
metadata: &mut MetadataStorage,
111111
info: &SignatureOnlyConcreteLibfunc,
112112
) -> Result<()> {
113+
// The sierra-to-casm compiler uses the range check builtin a total of 3 times.
114+
// https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/misc.rs?plain=1#L266
113115
let range_check: Value =
114-
super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?;
116+
super::increment_builtin_counter_by(context, entry, location, entry.arg(0)?, 3)?;
115117

116118
let value: Value = entry.arg(1)?;
117119

0 commit comments

Comments
 (0)