cairo_lang_starknet_classes/
casm_contract_class.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::cmp::Ordering;
use std::sync::LazyLock;

use cairo_lang_casm::assembler::AssembledCairoProgram;
use cairo_lang_casm::hints::{Hint, PythonicHint};
use cairo_lang_sierra::extensions::array::ArrayType;
use cairo_lang_sierra::extensions::bitwise::BitwiseType;
use cairo_lang_sierra::extensions::circuit::{AddModType, MulModType};
use cairo_lang_sierra::extensions::ec::EcOpType;
use cairo_lang_sierra::extensions::enm::EnumType;
use cairo_lang_sierra::extensions::felt252::Felt252Type;
use cairo_lang_sierra::extensions::gas::{CostTokenType, GasBuiltinType};
use cairo_lang_sierra::extensions::pedersen::PedersenType;
use cairo_lang_sierra::extensions::poseidon::PoseidonType;
use cairo_lang_sierra::extensions::range_check::{RangeCheck96Type, RangeCheckType};
use cairo_lang_sierra::extensions::segment_arena::SegmentArenaType;
use cairo_lang_sierra::extensions::snapshot::SnapshotType;
use cairo_lang_sierra::extensions::starknet::syscalls::SystemType;
use cairo_lang_sierra::extensions::structure::StructType;
use cairo_lang_sierra::extensions::NamedType;
use cairo_lang_sierra::ids::{ConcreteTypeId, GenericTypeId};
use cairo_lang_sierra::program::{ConcreteTypeLongId, GenericArg, TypeDeclaration};
use cairo_lang_sierra_to_casm::compiler::{
    CairoProgramDebugInfo, CompilationError, SierraToCasmConfig,
};
use cairo_lang_sierra_to_casm::metadata::{
    calc_metadata, MetadataComputationConfig, MetadataError,
};
use cairo_lang_utils::bigint::{deserialize_big_uint, serialize_big_uint, BigUintAsHex};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::require;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use convert_case::{Case, Casing};
use itertools::{chain, Itertools};
use num_bigint::BigUint;
use num_integer::Integer;
use num_traits::Signed;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt as Felt252;
use starknet_types_core::hash::{Poseidon, StarkHash};
use thiserror::Error;

use crate::allowed_libfuncs::AllowedLibfuncsError;
use crate::compiler_version::{
    current_compiler_version_id, current_sierra_version_id, VersionId,
    CONTRACT_SEGMENTATION_MINOR_VERSION,
};
use crate::contract_class::{ContractClass, ContractEntryPoint};
use crate::contract_segmentation::{
    compute_bytecode_segment_lengths, NestedIntList, SegmentationError,
};
use crate::felt252_serde::{sierra_from_felt252s, Felt252SerdeError};
use crate::keccak::starknet_keccak;

#[cfg(test)]
#[path = "casm_contract_class_test.rs"]
mod test;

/// The expected gas cost of an entrypoint.
pub const ENTRY_POINT_COST: i32 = 10000;

static CONSTRUCTOR_ENTRY_POINT_SELECTOR: LazyLock<BigUint> =
    LazyLock::new(|| starknet_keccak(b"constructor"));

#[derive(Error, Debug, Eq, PartialEq)]
pub enum StarknetSierraCompilationError {
    #[error(transparent)]
    CompilationError(#[from] Box<CompilationError>),
    #[error(transparent)]
    Felt252SerdeError(#[from] Felt252SerdeError),
    #[error(transparent)]
    MetadataError(#[from] MetadataError),
    #[error(transparent)]
    AllowedLibfuncsError(#[from] AllowedLibfuncsError),
    #[error(transparent)]
    SegmentationError(#[from] SegmentationError),
    #[error("Invalid entry point.")]
    EntryPointError,
    #[error("Missing arguments in the entry point.")]
    InvalidEntryPointSignatureMissingArgs,
    #[error("Invalid entry point signature.")]
    InvalidEntryPointSignature,
    #[error("Invalid constructor entry point.")]
    InvalidConstructorEntryPoint,
    #[error("{0} is not a supported builtin type.")]
    InvalidBuiltinType(ConcreteTypeId),
    #[error("Invalid entry point signature - builtins are not in the expected order.")]
    InvalidEntryPointSignatureWrongBuiltinsOrder,
    #[error("Entry points not sorted by selectors.")]
    EntryPointsOutOfOrder,
    #[error("Duplicate entry point selector {selector}.")]
    DuplicateEntryPointSelector { selector: BigUint },
    #[error("Duplicate entry point function index {index}.")]
    DuplicateEntryPointSierraFunction { index: usize },
    #[error("Out of range value in serialization.")]
    ValueOutOfRange,
    #[error(
        "Cannot compile Sierra version {version_in_contract} with the current compiler (sierra \
         version: {version_of_compiler})"
    )]
    UnsupportedSierraVersion { version_in_contract: VersionId, version_of_compiler: VersionId },
}

fn skip_if_none<T>(opt_field: &Option<T>) -> bool {
    opt_field.is_none()
}

/// Represents a contract in the Starknet network.
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CasmContractClass {
    #[serde(serialize_with = "serialize_big_uint", deserialize_with = "deserialize_big_uint")]
    pub prime: BigUint,
    pub compiler_version: String,
    pub bytecode: Vec<BigUintAsHex>,
    #[serde(skip_serializing_if = "skip_if_none")]
    pub bytecode_segment_lengths: Option<NestedIntList>,
    pub hints: Vec<(usize, Vec<Hint>)>,

    // Optional pythonic hints in a format that can be executed by the python vm.
    #[serde(skip_serializing_if = "skip_if_none")]
    pub pythonic_hints: Option<Vec<(usize, Vec<String>)>>,
    pub entry_points_by_type: CasmContractEntryPoints,
}
impl CasmContractClass {
    /// Returns the hash value for the compiled contract class.
    pub fn compiled_class_hash(&self) -> Felt252 {
        // Compute hashes on each component separately.
        let external_funcs_hash = self.entry_points_hash(&self.entry_points_by_type.external);
        let l1_handlers_hash = self.entry_points_hash(&self.entry_points_by_type.l1_handler);
        let constructors_hash = self.entry_points_hash(&self.entry_points_by_type.constructor);
        let bytecode_hash = self.compute_bytecode_hash();

        // Compute total hash by hashing each component on top of the previous one.
        Poseidon::hash_array(&[
            Felt252::from_bytes_be_slice(b"COMPILED_CLASS_V1"),
            external_funcs_hash,
            l1_handlers_hash,
            constructors_hash,
            bytecode_hash,
        ])
    }

    /// Returns the lengths of the bytecode segments.
    ///
    /// If the length field is missing, the entire bytecode is considered a single segment.
    pub fn get_bytecode_segment_lengths(&self) -> NestedIntList {
        self.bytecode_segment_lengths.clone().unwrap_or(NestedIntList::Leaf(self.bytecode.len()))
    }

    /// Returns the hash for a set of entry points.
    fn entry_points_hash(&self, entry_points: &[CasmContractEntryPoint]) -> Felt252 {
        let mut entry_point_hash_elements = vec![];
        for entry_point in entry_points {
            entry_point_hash_elements.push(Felt252::from(&entry_point.selector));
            entry_point_hash_elements.push(Felt252::from(entry_point.offset));
            entry_point_hash_elements.push(Poseidon::hash_array(
                &entry_point
                    .builtins
                    .iter()
                    .map(|builtin| Felt252::from_bytes_be_slice(builtin.as_bytes()))
                    .collect_vec(),
            ));
        }
        Poseidon::hash_array(&entry_point_hash_elements)
    }

    /// Returns the bytecode hash.
    fn compute_bytecode_hash(&self) -> Felt252 {
        let mut bytecode_iter = self.bytecode.iter().map(|big_uint| Felt252::from(&big_uint.value));

        let (len, bytecode_hash) =
            bytecode_hash_node(&mut bytecode_iter, &self.get_bytecode_segment_lengths());
        assert_eq!(len, self.bytecode.len());

        bytecode_hash
    }
}

/// Computes the hash of a bytecode segment. See the documentation of `bytecode_hash_node` in
/// the Starknet OS.
///
/// Returns the length of the processed segment and its hash.
fn bytecode_hash_node(
    iter: &mut impl Iterator<Item = Felt252>,
    node: &NestedIntList,
) -> (usize, Felt252) {
    match node {
        NestedIntList::Leaf(len) => {
            let data = &iter.take(*len).collect_vec();
            assert_eq!(data.len(), *len);
            (*len, Poseidon::hash_array(data))
        }
        NestedIntList::Node(nodes) => {
            // Compute `1 + poseidon(len0, hash0, len1, hash1, ...)`.
            let inner_nodes = nodes.iter().map(|node| bytecode_hash_node(iter, node)).collect_vec();
            let hash = Poseidon::hash_array(
                &inner_nodes.iter().flat_map(|(len, hash)| [(*len).into(), *hash]).collect_vec(),
            ) + 1;
            (inner_nodes.iter().map(|(len, _)| len).sum(), hash)
        }
    }
}

/// Context for resolving types.
pub struct TypeResolver<'a> {
    type_decl: &'a [TypeDeclaration],
}

impl TypeResolver<'_> {
    fn get_long_id(&self, type_id: &ConcreteTypeId) -> &ConcreteTypeLongId {
        &self.type_decl[type_id.id as usize].long_id
    }

    fn get_generic_id(&self, type_id: &ConcreteTypeId) -> &GenericTypeId {
        &self.get_long_id(type_id).generic_id
    }

    fn is_felt252_array_snapshot(&self, ty: &ConcreteTypeId) -> bool {
        let long_id = self.get_long_id(ty);
        if long_id.generic_id != SnapshotType::id() {
            return false;
        }

        let [GenericArg::Type(inner_ty)] = long_id.generic_args.as_slice() else {
            return false;
        };

        self.is_felt252_array(inner_ty)
    }

    fn is_felt252_array(&self, ty: &ConcreteTypeId) -> bool {
        let long_id = self.get_long_id(ty);
        if long_id.generic_id != ArrayType::id() {
            return false;
        }

        let [GenericArg::Type(element_ty)] = long_id.generic_args.as_slice() else {
            return false;
        };

        *self.get_generic_id(element_ty) == Felt252Type::id()
    }

    fn is_felt252_span(&self, ty: &ConcreteTypeId) -> bool {
        let long_id = self.get_long_id(ty);
        if long_id.generic_id != StructType::ID {
            return false;
        }

        let [GenericArg::UserType(_), GenericArg::Type(element_ty)] =
            long_id.generic_args.as_slice()
        else {
            return false;
        };

        self.is_felt252_array_snapshot(element_ty)
    }

    fn is_valid_entry_point_return_type(&self, ty: &ConcreteTypeId) -> bool {
        // The return type must be an enum with two variants: (result, error).
        let Some((result_tuple_ty, err_ty)) = self.extract_result_ty(ty) else {
            return false;
        };

        // The result variant must be a tuple with one element: Span<felt252>;
        let Some(result_ty) = self.extract_struct1(result_tuple_ty) else {
            return false;
        };
        if !self.is_felt252_span(result_ty) {
            return false;
        }

        // If the error type is Array<felt252>, it's a good error type, using the old panic
        // mechanism.
        if self.is_felt252_array(err_ty) {
            return true;
        }

        // Otherwise, the error type must be a struct with two fields: (panic, data)
        let Some((_panic_ty, err_data_ty)) = self.extract_struct2(err_ty) else {
            return false;
        };

        // The data field must be a Span<felt252>.
        self.is_felt252_array(err_data_ty)
    }

    /// Extracts types `TOk`, `TErr` from the type `Result<TOk, TErr>`.
    fn extract_result_ty(&self, ty: &ConcreteTypeId) -> Option<(&ConcreteTypeId, &ConcreteTypeId)> {
        let long_id = self.get_long_id(ty);
        require(long_id.generic_id == EnumType::id())?;
        let [GenericArg::UserType(_), GenericArg::Type(result_tuple_ty), GenericArg::Type(err_ty)] =
            long_id.generic_args.as_slice()
        else {
            return None;
        };
        Some((result_tuple_ty, err_ty))
    }

    /// Extracts type `T` from the tuple type `(T,)`.
    fn extract_struct1(&self, ty: &ConcreteTypeId) -> Option<&ConcreteTypeId> {
        let long_id = self.get_long_id(ty);
        require(long_id.generic_id == StructType::id())?;
        let [GenericArg::UserType(_), GenericArg::Type(ty0)] = long_id.generic_args.as_slice()
        else {
            return None;
        };
        Some(ty0)
    }

    /// Extracts types `T0`, `T1` from the tuple type `(T0, T1)`.
    fn extract_struct2(&self, ty: &ConcreteTypeId) -> Option<(&ConcreteTypeId, &ConcreteTypeId)> {
        let long_id = self.get_long_id(ty);
        require(long_id.generic_id == StructType::id())?;
        let [GenericArg::UserType(_), GenericArg::Type(ty0), GenericArg::Type(ty1)] =
            long_id.generic_args.as_slice()
        else {
            return None;
        };
        Some((ty0, ty1))
    }
}

impl CasmContractClass {
    pub fn from_contract_class(
        contract_class: ContractClass,
        add_pythonic_hints: bool,
        max_bytecode_size: usize,
    ) -> Result<Self, StarknetSierraCompilationError> {
        Ok(Self::from_contract_class_with_debug_info(
            contract_class,
            add_pythonic_hints,
            max_bytecode_size,
        )?
        .0)
    }

    // TODO(ilya): Reduce the size of CompilationError.
    #[allow(clippy::result_large_err)]
    pub fn from_contract_class_with_debug_info(
        contract_class: ContractClass,
        add_pythonic_hints: bool,
        max_bytecode_size: usize,
    ) -> Result<(Self, CairoProgramDebugInfo), StarknetSierraCompilationError> {
        let prime = Felt252::prime();
        for felt252 in &contract_class.sierra_program {
            if felt252.value >= prime {
                return Err(StarknetSierraCompilationError::ValueOutOfRange);
            }
        }

        let (sierra_version, _, program) = sierra_from_felt252s(&contract_class.sierra_program)?;
        let current_sierra_version = current_sierra_version_id();
        if !(sierra_version.major == current_sierra_version.major
            && sierra_version.minor <= current_sierra_version.minor)
        {
            return Err(StarknetSierraCompilationError::UnsupportedSierraVersion {
                version_in_contract: sierra_version,
                version_of_compiler: current_sierra_version,
            });
        }

        match &contract_class.entry_points_by_type.constructor.as_slice() {
            [] => {}
            [ContractEntryPoint { selector, .. }]
                if selector == &*CONSTRUCTOR_ENTRY_POINT_SELECTOR => {}
            _ => {
                return Err(StarknetSierraCompilationError::InvalidConstructorEntryPoint);
            }
        };

        for entry_points in [
            &contract_class.entry_points_by_type.constructor,
            &contract_class.entry_points_by_type.external,
            &contract_class.entry_points_by_type.l1_handler,
        ] {
            for (prev, next) in entry_points.iter().tuple_windows() {
                match prev.selector.cmp(&next.selector) {
                    Ordering::Less => {}
                    Ordering::Equal => {
                        return Err(StarknetSierraCompilationError::DuplicateEntryPointSelector {
                            selector: prev.selector.clone(),
                        });
                    }
                    Ordering::Greater => {
                        return Err(StarknetSierraCompilationError::EntryPointsOutOfOrder);
                    }
                }
            }
        }

        let entrypoint_function_indices = chain!(
            &contract_class.entry_points_by_type.constructor,
            &contract_class.entry_points_by_type.external,
            &contract_class.entry_points_by_type.l1_handler,
        )
        .map(|entrypoint| entrypoint.function_idx);
        // Count the number of times each function is used as an entry point.
        let mut function_idx_usages = UnorderedHashMap::<usize, usize>::default();
        for index in entrypoint_function_indices.clone() {
            let usages = function_idx_usages.entry(index).or_default();
            *usages += 1;
            const MAX_SIERRA_FUNCTION_USAGES: usize = 2;
            if *usages > MAX_SIERRA_FUNCTION_USAGES {
                return Err(StarknetSierraCompilationError::DuplicateEntryPointSierraFunction {
                    index,
                });
            }
        }
        let entrypoint_ids = entrypoint_function_indices.map(|idx| program.funcs[idx].id.clone());
        // TODO(lior): Remove this assert and condition once the equation solver is removed in major
        //   version 2.
        assert_eq!(sierra_version.major, 1);
        let no_eq_solver = sierra_version.minor >= 4;
        let metadata_computation_config = MetadataComputationConfig {
            function_set_costs: entrypoint_ids
                .map(|id| (id, [(CostTokenType::Const, ENTRY_POINT_COST)].into()))
                .collect(),
            linear_gas_solver: no_eq_solver,
            linear_ap_change_solver: no_eq_solver,
            skip_non_linear_solver_comparisons: false,
            compute_runtime_costs: false,
        };
        let metadata = calc_metadata(&program, metadata_computation_config)?;

        let cairo_program = cairo_lang_sierra_to_casm::compiler::compile(
            &program,
            &metadata,
            SierraToCasmConfig { gas_usage_check: true, max_bytecode_size },
        )?;

        let AssembledCairoProgram { bytecode, hints } = cairo_program.assemble();
        let bytecode = bytecode
            .iter()
            .map(|big_int| {
                let (_q, reminder) = big_int.magnitude().div_rem(&prime);
                BigUintAsHex {
                    value: if big_int.is_negative() { &prime - reminder } else { reminder },
                }
            })
            .collect_vec();

        let bytecode_segment_lengths =
            if sierra_version.minor >= CONTRACT_SEGMENTATION_MINOR_VERSION {
                Some(compute_bytecode_segment_lengths(&program, &cairo_program, bytecode.len())?)
            } else {
                None
            };

        let builtin_types = UnorderedHashSet::<GenericTypeId>::from_iter([
            RangeCheckType::id(),
            BitwiseType::id(),
            PedersenType::id(),
            EcOpType::id(),
            PoseidonType::id(),
            SegmentArenaType::id(),
            GasBuiltinType::id(),
            SystemType::id(),
            RangeCheck96Type::id(),
            AddModType::id(),
            MulModType::id(),
        ]);

        let as_casm_entry_point = |contract_entry_point: ContractEntryPoint| {
            let Some(function) = program.funcs.get(contract_entry_point.function_idx) else {
                return Err(StarknetSierraCompilationError::EntryPointError);
            };
            let statement_id = function.entry_point;

            // The expected return types are [builtins.., gas_builtin, system, PanicResult].
            require(function.signature.ret_types.len() >= 3)
                .ok_or(StarknetSierraCompilationError::InvalidEntryPointSignatureMissingArgs)?;

            let (input_span, input_builtins) = function.signature.param_types.split_last().unwrap();

            let type_resolver = TypeResolver { type_decl: &program.type_declarations };
            require(type_resolver.is_felt252_span(input_span))
                .ok_or(StarknetSierraCompilationError::InvalidEntryPointSignature)?;

            let (panic_result, output_builtins) =
                function.signature.ret_types.split_last().unwrap();

            require(input_builtins == output_builtins)
                .ok_or(StarknetSierraCompilationError::InvalidEntryPointSignature)?;
            require(type_resolver.is_valid_entry_point_return_type(panic_result))
                .ok_or(StarknetSierraCompilationError::InvalidEntryPointSignature)?;

            for type_id in input_builtins.iter() {
                if !builtin_types.contains(type_resolver.get_generic_id(type_id)) {
                    return Err(StarknetSierraCompilationError::InvalidBuiltinType(
                        type_id.clone(),
                    ));
                }
            }
            let (system_ty, builtins) = input_builtins.split_last().unwrap();
            let (gas_ty, builtins) = builtins.split_last().unwrap();

            // Check that the last builtins are gas and system.
            if *type_resolver.get_generic_id(system_ty) != SystemType::id()
                || *type_resolver.get_generic_id(gas_ty) != GasBuiltinType::id()
            {
                return Err(
                    StarknetSierraCompilationError::InvalidEntryPointSignatureWrongBuiltinsOrder,
                );
            }

            let builtins = builtins
                .iter()
                .map(|type_id| match type_resolver.get_generic_id(type_id).0.as_str() {
                    "RangeCheck96" => "range_check96".to_string(),
                    name => name.to_case(Case::Snake),
                })
                .collect_vec();

            let code_offset = cairo_program
                .debug_info
                .sierra_statement_info
                .get(statement_id.0)
                .ok_or(StarknetSierraCompilationError::EntryPointError)?
                .start_offset;
            assert_eq!(
                metadata.gas_info.function_costs[&function.id],
                OrderedHashMap::from_iter([(CostTokenType::Const, ENTRY_POINT_COST as i64)]),
                "Unexpected entry point cost."
            );
            Ok::<CasmContractEntryPoint, StarknetSierraCompilationError>(CasmContractEntryPoint {
                selector: contract_entry_point.selector,
                offset: code_offset,
                builtins,
            })
        };

        let as_casm_entry_points = |contract_entry_points: Vec<ContractEntryPoint>| {
            let mut entry_points = vec![];
            for contract_entry_point in contract_entry_points.into_iter() {
                entry_points.push(as_casm_entry_point(contract_entry_point)?);
            }
            Ok::<Vec<CasmContractEntryPoint>, StarknetSierraCompilationError>(entry_points)
        };

        let pythonic_hints = if add_pythonic_hints {
            Some(
                hints
                    .iter()
                    .map(|(pc, hints)| {
                        (*pc, hints.iter().map(|hint| hint.get_pythonic_hint()).collect_vec())
                    })
                    .collect_vec(),
            )
        } else {
            None
        };

        let compiler_version = current_compiler_version_id().to_string();
        let casm_contract_class = Self {
            prime,
            compiler_version,
            bytecode,
            bytecode_segment_lengths,
            hints,
            pythonic_hints,
            entry_points_by_type: CasmContractEntryPoints {
                external: as_casm_entry_points(contract_class.entry_points_by_type.external)?,
                l1_handler: as_casm_entry_points(contract_class.entry_points_by_type.l1_handler)?,
                constructor: as_casm_entry_points(contract_class.entry_points_by_type.constructor)?,
            },
        };

        Ok((casm_contract_class, cairo_program.debug_info))
    }
}

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CasmContractEntryPoint {
    /// A field element that encodes the signature of the called function.
    #[serde(serialize_with = "serialize_big_uint", deserialize_with = "deserialize_big_uint")]
    pub selector: BigUint,
    /// The offset of the instruction that should be called within the contract bytecode.
    pub offset: usize,
    // list of builtins.
    pub builtins: Vec<String>,
}

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CasmContractEntryPoints {
    #[serde(rename = "EXTERNAL")]
    pub external: Vec<CasmContractEntryPoint>,
    #[serde(rename = "L1_HANDLER")]
    pub l1_handler: Vec<CasmContractEntryPoint>,
    #[serde(rename = "CONSTRUCTOR")]
    pub constructor: Vec<CasmContractEntryPoint>,
}