cairo_lang_sierra/extensions/modules/
felt252.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
use num_bigint::BigInt;
use num_traits::Zero;

use super::is_zero::{IsZeroLibfunc, IsZeroTraits};
use super::non_zero::nonzero_ty;
use crate::define_libfunc_hierarchy;
use crate::extensions::lib_func::{
    DeferredOutputKind, LibfuncSignature, OutputVarInfo, ParamSignature, SierraApChange,
    SignatureSpecializationContext, SpecializationContext,
};
use crate::extensions::{
    GenericLibfunc, NamedLibfunc, NamedType, NoGenericArgsGenericType, OutputVarReferenceInfo,
    SignatureBasedConcreteLibfunc, SpecializationError,
};
use crate::ids::{GenericLibfuncId, GenericTypeId};
use crate::program::GenericArg;

/// Type for felt252.
/// The native type of the Cairo architecture.
#[derive(Default)]
pub struct Felt252Type {}
impl NoGenericArgsGenericType for Felt252Type {
    const ID: GenericTypeId = GenericTypeId::new_inline("felt252");
    const STORABLE: bool = true;
    const DUPLICATABLE: bool = true;
    const DROPPABLE: bool = true;
    const ZERO_SIZED: bool = false;
}

define_libfunc_hierarchy! {
    pub enum Felt252Libfunc {
        BinaryOperation(Felt252BinaryOperationLibfunc),
        Const(Felt252ConstLibfunc),
        IsZero(Felt252JumpNotZeroLibfunc),
    }, Felt252Concrete
}

define_libfunc_hierarchy! {
    pub enum Felt252BinaryOperationLibfunc {
        WithVar(Felt252BinaryOperationWithVarLibfunc),
        WithConst(Felt252BinaryOperationWithConstLibfunc),
    }, Felt252BinaryOperationConcrete
}

#[derive(Default)]
pub struct Felt252Traits {}
impl IsZeroTraits for Felt252Traits {
    const IS_ZERO: &'static str = "felt252_is_zero";
    const GENERIC_TYPE_ID: GenericTypeId = <Felt252Type as NamedType>::ID;
}
pub type Felt252JumpNotZeroLibfunc = IsZeroLibfunc<Felt252Traits>;

/// Felt252 binary operators.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Felt252BinaryOperator {
    Add,
    Sub,
    Mul,
    Div,
}

/// Libfunc for felt252 binary operations.
pub struct Felt252BinaryOperationWithVarLibfunc {
    pub operator: Felt252BinaryOperator,
}
impl Felt252BinaryOperationWithVarLibfunc {
    fn new(operator: Felt252BinaryOperator) -> Self {
        Self { operator }
    }
    const ADD: &'static str = "felt252_add";
    const SUB: &'static str = "felt252_sub";
    const MUL: &'static str = "felt252_mul";
    const DIV: &'static str = "felt252_div";
}
impl GenericLibfunc for Felt252BinaryOperationWithVarLibfunc {
    type Concrete = Felt252BinaryOpConcreteLibfunc;

    fn supported_ids() -> Vec<GenericLibfuncId> {
        vec![
            GenericLibfuncId::from(Self::ADD),
            GenericLibfuncId::from(Self::SUB),
            GenericLibfuncId::from(Self::MUL),
            GenericLibfuncId::from(Self::DIV),
        ]
    }

    fn by_id(id: &GenericLibfuncId) -> Option<Self> {
        match id.0.as_str() {
            Self::ADD => Some(Self::new(Felt252BinaryOperator::Add)),
            Self::SUB => Some(Self::new(Felt252BinaryOperator::Sub)),
            Self::MUL => Some(Self::new(Felt252BinaryOperator::Mul)),
            Self::DIV => Some(Self::new(Felt252BinaryOperator::Div)),
            _ => None,
        }
    }

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        args: &[GenericArg],
    ) -> Result<LibfuncSignature, SpecializationError> {
        let ty = context.get_concrete_type(Felt252Type::id(), &[])?;
        let (second_param_type, output_ref_info) =
            if matches!(self.operator, Felt252BinaryOperator::Div) {
                (nonzero_ty(context, &ty)?, OutputVarReferenceInfo::NewTempVar { idx: 0 })
            } else {
                (ty.clone(), OutputVarReferenceInfo::Deferred(DeferredOutputKind::Generic))
            };
        match args {
            [] => Ok(LibfuncSignature::new_non_branch_ex(
                vec![
                    ParamSignature::new(ty.clone()),
                    ParamSignature::new(second_param_type).with_allow_const(),
                ],
                vec![OutputVarInfo { ty, ref_info: output_ref_info }],
                SierraApChange::Known { new_vars_only: true },
            )),
            _ => Err(SpecializationError::WrongNumberOfGenericArgs),
        }
    }

    fn specialize(
        &self,
        context: &dyn SpecializationContext,
        args: &[GenericArg],
    ) -> Result<Self::Concrete, SpecializationError> {
        match args {
            [] => Ok({
                Felt252BinaryOpConcreteLibfunc {
                    operator: self.operator,
                    signature: self.specialize_signature(context.upcast(), args)?,
                }
            }),
            _ => Err(SpecializationError::WrongNumberOfGenericArgs),
        }
    }
}

/// Libfunc for felt252 binary operations with const.
pub struct Felt252BinaryOperationWithConstLibfunc {
    pub operator: Felt252BinaryOperator,
}
impl Felt252BinaryOperationWithConstLibfunc {
    fn new(operator: Felt252BinaryOperator) -> Self {
        Self { operator }
    }
    const ADD: &'static str = "felt252_add_const";
    const SUB: &'static str = "felt252_sub_const";
    const MUL: &'static str = "felt252_mul_const";
    const DIV: &'static str = "felt252_div_const";
}
impl GenericLibfunc for Felt252BinaryOperationWithConstLibfunc {
    type Concrete = Felt252OperationWithConstConcreteLibfunc;

    fn supported_ids() -> Vec<GenericLibfuncId> {
        vec![
            GenericLibfuncId::from(Self::ADD),
            GenericLibfuncId::from(Self::SUB),
            GenericLibfuncId::from(Self::MUL),
            GenericLibfuncId::from(Self::DIV),
        ]
    }

    fn by_id(id: &GenericLibfuncId) -> Option<Self> {
        match id.0.as_str() {
            Self::ADD => Some(Self::new(Felt252BinaryOperator::Add)),
            Self::SUB => Some(Self::new(Felt252BinaryOperator::Sub)),
            Self::MUL => Some(Self::new(Felt252BinaryOperator::Mul)),
            Self::DIV => Some(Self::new(Felt252BinaryOperator::Div)),
            _ => None,
        }
    }

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        args: &[GenericArg],
    ) -> Result<LibfuncSignature, SpecializationError> {
        let ty = context.get_concrete_type(Felt252Type::id(), &[])?;
        match args {
            [GenericArg::Value(c)] => {
                let output_ref_info = if matches!(self.operator, Felt252BinaryOperator::Div) {
                    if c.is_zero() {
                        return Err(SpecializationError::UnsupportedGenericArg);
                    }
                    OutputVarReferenceInfo::NewTempVar { idx: 0 }
                } else {
                    OutputVarReferenceInfo::Deferred(DeferredOutputKind::Generic)
                };

                Ok(LibfuncSignature::new_non_branch(
                    vec![ty.clone()],
                    vec![OutputVarInfo { ty, ref_info: output_ref_info }],
                    SierraApChange::Known { new_vars_only: true },
                ))
            }
            _ => Err(SpecializationError::WrongNumberOfGenericArgs),
        }
    }

    fn specialize(
        &self,
        context: &dyn SpecializationContext,
        args: &[GenericArg],
    ) -> Result<Self::Concrete, SpecializationError> {
        match args {
            [GenericArg::Value(c)] => {
                if matches!(self.operator, Felt252BinaryOperator::Div) && c.is_zero() {
                    Err(SpecializationError::UnsupportedGenericArg)
                } else {
                    Ok(Felt252OperationWithConstConcreteLibfunc {
                        operator: self.operator,
                        c: c.clone(),
                        signature: self.specialize_signature(context.upcast(), args)?,
                    })
                }
            }
            _ => Err(SpecializationError::WrongNumberOfGenericArgs),
        }
    }
}

pub struct Felt252BinaryOpConcreteLibfunc {
    pub operator: Felt252BinaryOperator,
    pub signature: LibfuncSignature,
}
impl SignatureBasedConcreteLibfunc for Felt252BinaryOpConcreteLibfunc {
    fn signature(&self) -> &LibfuncSignature {
        &self.signature
    }
}

/// Felt252 operations with a const.
pub struct Felt252OperationWithConstConcreteLibfunc {
    pub operator: Felt252BinaryOperator,
    pub c: BigInt,
    pub signature: LibfuncSignature,
}

impl SignatureBasedConcreteLibfunc for Felt252OperationWithConstConcreteLibfunc {
    fn signature(&self) -> &LibfuncSignature {
        &self.signature
    }
}

/// Libfunc for creating a constant felt252.
#[derive(Default)]
pub struct Felt252ConstLibfunc {}
impl NamedLibfunc for Felt252ConstLibfunc {
    type Concrete = Felt252ConstConcreteLibfunc;
    const STR_ID: &'static str = "felt252_const";

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        _args: &[GenericArg],
    ) -> Result<LibfuncSignature, SpecializationError> {
        Ok(LibfuncSignature::new_non_branch(
            vec![],
            vec![OutputVarInfo {
                ty: context.get_concrete_type(Felt252Type::id(), &[])?,
                ref_info: OutputVarReferenceInfo::Deferred(DeferredOutputKind::Const),
            }],
            SierraApChange::Known { new_vars_only: true },
        ))
    }

    fn specialize(
        &self,
        context: &dyn SpecializationContext,
        args: &[GenericArg],
    ) -> Result<Self::Concrete, SpecializationError> {
        match args {
            [GenericArg::Value(c)] => Ok(Felt252ConstConcreteLibfunc {
                c: c.clone(),
                signature: <Self as NamedLibfunc>::specialize_signature(
                    self,
                    context.upcast(),
                    args,
                )?,
            }),
            _ => Err(SpecializationError::UnsupportedGenericArg),
        }
    }
}

pub struct Felt252ConstConcreteLibfunc {
    pub c: BigInt,
    pub signature: LibfuncSignature,
}
impl SignatureBasedConcreteLibfunc for Felt252ConstConcreteLibfunc {
    fn signature(&self) -> &LibfuncSignature {
        &self.signature
    }
}