cairo_lang_sierra/extensions/modules/starknet/
mod.rs

1use crate::extensions::lib_func::SignatureSpecializationContext;
2use crate::extensions::{NamedType, SpecializationError};
3use crate::ids::{ConcreteTypeId, UserTypeId};
4use crate::program::GenericArg;
5use crate::{define_libfunc_hierarchy, define_type_hierarchy};
6
7pub mod storage;
8use storage::{
9    GetBlockHashLibfunc, StorageAddressToFelt252Libfunc, StorageBaseAddressConstLibfunc,
10    StorageBaseAddressType, StorageReadLibfunc, StorageWriteLibfunc,
11};
12
13pub mod syscalls;
14use syscalls::{GetClassHashAtLibfunc, ReplaceClassLibfunc, SystemType};
15
16pub mod emit_event;
17use emit_event::EmitEventLibfunc;
18
19pub mod getter;
20
21pub mod secp256;
22use secp256::{Secp256Libfunc, Secp256PointType};
23pub mod secp256k1;
24pub mod secp256r1;
25
26pub mod testing;
27
28pub mod interoperability;
29use interoperability::{
30    CallContractLibfunc, ContractAddressConstLibfunc, ContractAddressType, MetaTxV0Libfunc,
31};
32
33use self::getter::{GetExecutionInfoTrait, GetExecutionInfoV2Trait, GetterLibfunc};
34use self::interoperability::{
35    ClassHashConstLibfunc, ClassHashToFelt252Libfunc, ClassHashTryFromFelt252Trait, ClassHashType,
36    ContractAddressToFelt252Libfunc, ContractAddressTryFromFelt252Libfunc, DeployLibfunc,
37    LibraryCallLibfunc, SendMessageToL1Libfunc,
38};
39use self::storage::{
40    StorageAddressFromBaseAndOffsetLibfunc, StorageAddressFromBaseLibfunc,
41    StorageAddressTryFromFelt252Trait, StorageAddressType, StorageBaseAddressFromFelt252Libfunc,
42};
43use self::syscalls::{
44    KeccakLibfunc, Sha256ProcessBlockLibfunc, Sha256StateHandleDigestLibfunc,
45    Sha256StateHandleInitLibfunc, Sha256StateHandleType,
46};
47use self::testing::TestingLibfunc;
48use super::array::ArrayType;
49use super::felt252::Felt252Type;
50use super::int::unsigned::Uint64Type;
51use super::snapshot::snapshot_ty;
52use super::structure::StructType;
53use super::try_from_felt252::TryFromFelt252Libfunc;
54
55define_type_hierarchy! {
56    pub enum StarknetType {
57        ClassHash(ClassHashType),
58        ContractAddress(ContractAddressType),
59        StorageBaseAddress(StorageBaseAddressType),
60        StorageAddress(StorageAddressType),
61        System(SystemType),
62        Secp256Point(Secp256PointType),
63        Sha256StateHandle(Sha256StateHandleType),
64    }, StarknetTypeConcrete
65}
66
67define_libfunc_hierarchy! {
68    pub enum StarknetLibfunc {
69         CallContract(CallContractLibfunc),
70         ClassHashConst(ClassHashConstLibfunc),
71         ClassHashTryFromFelt252(TryFromFelt252Libfunc<ClassHashTryFromFelt252Trait>),
72         ClassHashToFelt252(ClassHashToFelt252Libfunc),
73         ContractAddressConst(ContractAddressConstLibfunc),
74         ContractAddressTryFromFelt252(TryFromFelt252Libfunc<ContractAddressTryFromFelt252Libfunc>),
75         ContractAddressToFelt252(ContractAddressToFelt252Libfunc),
76         StorageRead(StorageReadLibfunc),
77         StorageWrite(StorageWriteLibfunc),
78         StorageBaseAddressConst(StorageBaseAddressConstLibfunc),
79         StorageBaseAddressFromFelt252(StorageBaseAddressFromFelt252Libfunc),
80         StorageAddressFromBase(StorageAddressFromBaseLibfunc),
81         StorageAddressFromBaseAndOffset(StorageAddressFromBaseAndOffsetLibfunc),
82         StorageAddressToFelt252(StorageAddressToFelt252Libfunc),
83         StorageAddressTryFromFelt252(TryFromFelt252Libfunc<StorageAddressTryFromFelt252Trait>),
84         EmitEvent(EmitEventLibfunc),
85         GetBlockHash(GetBlockHashLibfunc),
86         GetExecutionInfo(GetterLibfunc<GetExecutionInfoTrait>),
87         GetExecutionInfoV2(GetterLibfunc<GetExecutionInfoV2Trait>),
88         Deploy(DeployLibfunc),
89         Keccak(KeccakLibfunc),
90         Sha256ProcessBlock(Sha256ProcessBlockLibfunc),
91         Sha256StateHandleInit(Sha256StateHandleInitLibfunc),
92         Sha256StateHandleDigest(Sha256StateHandleDigestLibfunc),
93         LibraryCall(LibraryCallLibfunc),
94         ReplaceClass(ReplaceClassLibfunc),
95         GetClassHashAt(GetClassHashAtLibfunc),
96         SendMessageToL1(SendMessageToL1Libfunc),
97         MetaTxV0(MetaTxV0Libfunc),
98         Testing(TestingLibfunc),
99         Secp256(Secp256Libfunc),
100    }, StarknetConcreteLibfunc
101}
102
103/// User type for `Span<T>`.
104fn span_ty(
105    context: &dyn SignatureSpecializationContext,
106    wrapped_ty: ConcreteTypeId,
107    wrapped_ty_name: &str,
108) -> Result<ConcreteTypeId, SpecializationError> {
109    context.get_concrete_type(
110        StructType::id(),
111        &[
112            GenericArg::UserType(UserTypeId::from_string(format!(
113                "core::array::Span::<{wrapped_ty_name}>"
114            ))),
115            GenericArg::Type(snapshot_ty(
116                context,
117                context.get_wrapped_concrete_type(ArrayType::id(), wrapped_ty)?,
118            )?),
119        ],
120    )
121}
122
123/// User type for `Span<felt252>`.
124fn felt252_span_ty(
125    context: &dyn SignatureSpecializationContext,
126) -> Result<ConcreteTypeId, SpecializationError> {
127    span_ty(context, context.get_concrete_type(Felt252Type::id(), &[])?, "core::felt252")
128}
129
130/// User type for `Span<u64>`.
131fn u64_span_ty(
132    context: &dyn SignatureSpecializationContext,
133) -> Result<ConcreteTypeId, SpecializationError> {
134    span_ty(context, context.get_concrete_type(Uint64Type::id(), &[])?, "core::integer::u64")
135}