cairo-native 0.9.0-rc.3

A compiler to convert Cairo's IR Sierra code to MLIR and execute it.
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
//! Functions and constructs related to elliptic curve operations on the STARK curve.
//!
//! This module provides implementations for various elliptic curve operations tailored for the
//! STARK curve.
//!
//! Curve information:
//! * Curve equation: y² ≡ x³ + α·x + β (mod p)
//! * α = 1
//! * β = 0x6f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89
//! * p = 0x0800000000000011000000000000000000000000000000000000000000000001 = 2^251 + 17 * 2^192 +
//! 1
//! Generator point:
//! * x = 0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca
//! * y = 0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f
//!
//! # Examples
//!
//! Creating points and basic operations:
//!
//! ```
//! // Create a point from coordinates
//! let point = EcPointTrait::new(
//!     x: 336742005567258698661916498343089167447076063081786685068305785816009957563,
//!     y: 1706004133033694959518200210163451614294041810778629639790706933324248611779,
//! ).unwrap();
//!
//! // Perform scalar multiplication
//! let result = point.mul(2);
//!
//! // Add points
//! let sum = point + result;
//!
//! // Subtract points
//! let diff = result - point;
//! ```
//!
//! Using EC state for batch operations:
//!
//! ```
//! let p = EcPointTrait::new_from_x(1).unwrap();
//! let p_nz = p.try_into().unwrap();
//!
//! // Initialize state
//! let mut state = EcStateTrait::init();
//!
//! // Add points and scalar multiplications
//! state.add(p_nz);
//! state.add_mul(1, p_nz);
//!
//! // Get the final result
//! let _result = state.finalize();
//! ```

use crate::RangeCheck;
#[allow(unused_imports)]
use crate::array::ArrayTrait;
#[allow(unused_imports)]
use crate::traits::{Into, TryInto};
use crate::zeroable::IsZeroResult;

pub mod stark_curve {
    /// The STARK Curve is defined by the equation y² ≡ x³ + α·x + β (mod p).
    pub const ALPHA: felt252 = 1;
    /// The STARK Curve is defined by the equation y² ≡ x³ + α·x + β (mod p).
    pub const BETA: felt252 = 0x6f21413efbe40de150e596d72f7a8c5609ad26c15c915c1f4cdfcb99cee9e89;
    /// The order (number of points) of the STARK Curve.
    pub const ORDER: felt252 = 0x800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f;
    /// The x coordinate of the generator point used in the ECDSA signature.
    pub const GEN_X: felt252 = 0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca;
    /// The y coordinate of the generator point used in the ECDSA signature.
    pub const GEN_Y: felt252 = 0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f;
}

pub extern type EcOp;

/// A point on the STARK curve.
///
/// Points can be created using [`EcPointTrait::new`] or [`EcPointTrait::new_from_x`].
/// The zero point represents the point at infinity.
pub extern type EcPoint;

impl EcPointCopy of Copy<EcPoint>;
impl EcPointDrop of Drop<EcPoint>;

/// A non-zero point on the STARK curve (cannot be the point at infinity).
pub type NonZeroEcPoint = NonZero<EcPoint>;

/// Returns the zero point of the curve ("the point at infinity").
extern fn ec_point_zero() -> EcPoint nopanic;

/// Constructs a non-zero point from its (x, y) coordinates.
/// Returns `None` if the point (x, y) is not on the curve.
extern fn ec_point_try_new_nz(x: felt252, y: felt252) -> Option<NonZeroEcPoint> nopanic;

/// Constructs a non-zero point from its x coordinate.
/// Returns `None` if no point of form (x, _) is on the curve.
extern fn ec_point_from_x_nz(x: felt252) -> Option<NonZeroEcPoint> implicits(RangeCheck) nopanic;

/// Unwraps a non-zero point into its (x, y) coordinates.
pub extern fn ec_point_unwrap(p: NonZeroEcPoint) -> (felt252, felt252) nopanic;

/// Computes the negation of an elliptic curve point (-p).
extern fn ec_neg(p: EcPoint) -> EcPoint nopanic;

/// Computes the negation of a non-zero elliptic curve point (-p).
extern fn ec_neg_nz(p: NonZeroEcPoint) -> NonZeroEcPoint nopanic;

/// Checks whether the given `EcPoint` is the zero point.
extern fn ec_point_is_zero(p: EcPoint) -> IsZeroResult<EcPoint> nopanic;

/// Converts `EcPoint` to `NonZeroEcPoint`.
impl EcPointTryIntoNonZero of TryInto<EcPoint, NonZeroEcPoint> {
    #[inline]
    fn try_into(self: EcPoint) -> Option<NonZeroEcPoint> {
        match ec_point_is_zero(self) {
            IsZeroResult::Zero => None,
            IsZeroResult::NonZero(p_nz) => Some(p_nz),
        }
    }
}

/// Elliptic curve state.
///
/// Use this to perform multiple point operations efficiently.
/// Initialize with [`EcStateTrait::init`], add points with [`EcStateTrait::add`]
/// or [`EcStateTrait::add_mul`], and finalize with [`EcStateTrait::finalize`].
pub extern type EcState;

impl EcStateDrop of Drop<EcState>;

mod internal {
    impl EcStateCopy of Copy<super::EcState>;
    pub impl EcStateClone of Clone<super::EcState> {
        #[inline]
        fn clone(self: @super::EcState) -> super::EcState {
            *self
        }
    }
}

impl EcStateClone = internal::EcStateClone;

/// Initializes an EC computation with the zero point.
extern fn ec_state_init() -> EcState nopanic;

/// Adds a point to the computation.
extern fn ec_state_add(ref s: EcState, p: NonZeroEcPoint) nopanic;

/// Adds the product `p * scalar` to the state.
extern fn ec_state_add_mul(
    ref s: EcState, scalar: felt252, p: NonZeroEcPoint,
) implicits(EcOp) nopanic;

/// Finalizes the EC computation and returns the result (returns `None` if the result is the
/// zero point).
extern fn ec_state_try_finalize_nz(s: EcState) -> Option<NonZeroEcPoint> nopanic;

#[generate_trait]
pub impl EcStateImpl of EcStateTrait {
    /// Initializes an EC computation with the zero point.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut state = EcStateTrait::init();
    /// ```
    #[must_use]
    fn init() -> EcState nopanic {
        ec_state_init()
    }

    /// Adds a point to the computation.
    ///
    /// # Arguments
    ///
    /// * `p` - The non-zero point to add
    #[inline]
    fn add(ref self: EcState, p: NonZeroEcPoint) nopanic {
        ec_state_add(ref self, :p);
    }

    /// Subtracts a point to the computation.
    ///
    /// # Arguments
    ///
    /// * `p` - The non-zero point to subtract
    #[inline]
    fn sub(ref self: EcState, p: NonZeroEcPoint) {
        ec_state_add(ref self, -p);
    }

    /// Adds the product `p * scalar` to the state.
    ///
    /// # Arguments
    ///
    /// * `scalar` - The scalar to multiply the point by
    /// * `p` - The non-zero point to multiply and add
    #[inline]
    fn add_mul(ref self: EcState, scalar: felt252, p: NonZeroEcPoint) nopanic {
        ec_state_add_mul(ref self, :scalar, :p);
    }

    /// Finalizes the EC computation and returns the result as a non-zero point.
    ///
    /// # Returns
    ///
    /// * `Option<NonZeroEcPoint>` - The resulting point, or None if the result is the zero point.
    #[inline]
    fn finalize_nz(self: EcState) -> Option<NonZeroEcPoint> nopanic {
        ec_state_try_finalize_nz(self)
    }

    /// Finalizes the EC computation and returns the result.
    ///
    /// Returns the zero point if the computation results in the point at infinity.
    #[inline]
    fn finalize(self: EcState) -> EcPoint {
        match self.finalize_nz() {
            Some(p_nz) => p_nz.into(),
            None => ec_point_zero(),
        }
    }
}

#[generate_trait]
pub impl EcPointImpl of EcPointTrait {
    /// Creates a new EC point from its (x, y) coordinates.
    ///
    /// # Arguments
    ///
    /// * `x` - The x-coordinate of the point
    /// * `y` - The y-coordinate of the point
    ///
    /// # Returns
    ///
    /// Returns `None` if the point (x, y) is not on the curve.
    ///
    /// # Examples
    ///
    /// ```
    /// let point = EcPointTrait::new(
    ///     x: 336742005567258698661916498343089167447076063081786685068305785816009957563,
    ///     y: 1706004133033694959518200210163451614294041810778629639790706933324248611779,
    /// ).unwrap();
    /// ```
    #[inline]
    fn new(x: felt252, y: felt252) -> Option<EcPoint> {
        Some(Self::new_nz(:x, :y)?.into())
    }

    /// Creates a new NonZero EC point from its (x, y) coordinates.
    #[inline]
    fn new_nz(x: felt252, y: felt252) -> Option<NonZeroEcPoint> {
        ec_point_try_new_nz(:x, :y)
    }

    /// Creates a new EC point from its x coordinate.
    ///
    /// # Arguments
    ///
    /// * `x` - The x-coordinate of the point
    ///
    /// # Returns
    ///
    /// Returns `None` if no point with the given x-coordinate exists on the curve.
    ///
    /// # Examples
    ///
    /// ```
    /// let valid = EcPointTrait::new_from_x(1);
    /// assert!(valid.is_some());
    /// let invalid = EcPointTrait::new_from_x(0);
    /// assert!(invalid.is_none());
    /// ```
    #[inline]
    fn new_from_x(x: felt252) -> Option<EcPoint> {
        Some(Self::new_nz_from_x(:x)?.into())
    }

    /// Creates a new NonZero EC point from its x coordinate.
    #[inline]
    fn new_nz_from_x(x: felt252) -> Option<NonZeroEcPoint> {
        ec_point_from_x_nz(:x)
    }


    /// Returns the coordinates of the EC point.
    ///
    /// # Returns
    ///
    /// A tuple containing the (x, y) coordinates of the point.
    ///
    /// # Examples
    ///
    /// ```
    /// let point_nz = EcPointTrait::new_nz_from_x(1).unwrap();
    /// let (x, _y) = point_nz.coordinates();
    /// assert!(x == 1);
    /// ```
    fn coordinates(self: NonZeroEcPoint) -> (felt252, felt252) {
        ec_point_unwrap(self)
    }

    /// Returns the x coordinate of the EC point.
    ///
    /// # Panics
    ///
    /// Panics if the point is the point at infinity.
    ///
    /// # Examples
    ///
    /// ```
    /// let point_nz = EcPointTrait::new_nz_from_x(1).unwrap();
    /// let x = point_nz.x();
    /// assert!(x == 1);
    /// ```
    fn x(self: NonZeroEcPoint) -> felt252 {
        let (x, _) = self.coordinates();
        x
    }

    /// Returns the y coordinate of the EC point.
    ///
    /// # Panics
    ///
    /// Panics if the point is the point at infinity.
    ///
    /// # Examples
    ///
    /// ```
    /// let gen_point =
    /// EcPointTrait::new_nz_from_x(0x1ef15c18599971b7beced415a40f0c7deacfd9b0d1819e03d723d8bc943cfca).unwrap();
    /// let y = gen_point.y();
    /// assert!(y == 0x5668060aa49730b7be4801df46ec62de53ecd11abe43a32873000c36e8dc1f);
    /// ```
    fn y(self: NonZeroEcPoint) -> felt252 {
        let (_, y) = self.coordinates();
        y
    }

    /// Computes the product of an EC point by the given scalar.
    ///
    /// # Arguments
    ///
    /// * `scalar` - The scalar to multiply the point by
    ///
    /// # Returns
    ///
    /// The resulting point after scalar multiplication.
    fn mul(self: EcPoint, scalar: felt252) -> EcPoint {
        match self.try_into() {
            Some(self_nz) => {
                let mut state = EcStateTrait::init();
                state.add_mul(scalar, self_nz);
                state.finalize()
            },
            None => self,
        }
    }
}

impl EcPointNeg of Neg<EcPoint> {
    fn neg(a: EcPoint) -> EcPoint {
        ec_neg(a)
    }
}

#[cfg(sierra: "future")]
impl NonZeroEcPointNeg of Neg<NonZeroEcPoint> {
    fn neg(a: NonZeroEcPoint) -> NonZeroEcPoint {
        ec_neg_nz(a)
    }
}

// TODO(orizi): Remove this impl on next Sierra release.
#[cfg(not(sierra: "future"))]
impl NonZeroEcPointNeg of Neg<NonZeroEcPoint> {
    fn neg(a: NonZeroEcPoint) -> NonZeroEcPoint {
        let p: EcPoint = a.into();
        (-p).try_into().unwrap()
    }
}

impl EcPointAdd of Add<EcPoint> {
    /// Computes the sum of two points on the curve.
    fn add(lhs: EcPoint, rhs: EcPoint) -> EcPoint {
        let Some(lhs_nz) = lhs.try_into() else {
            return rhs;
        };
        let Some(rhs_nz) = rhs.try_into() else {
            return lhs;
        };
        let mut state = ec_state_init();
        state.add(lhs_nz);
        state.add(rhs_nz);

        state.finalize()
    }
}

#[feature("deprecated-op-assign-traits")]
impl EcPointAddEq of crate::traits::AddEq<EcPoint> {
    #[inline]
    fn add_eq(ref self: EcPoint, other: EcPoint) {
        self = Add::add(self, other);
    }
}

impl EcPointSub of Sub<EcPoint> {
    /// Computes the difference between two points on the curve.
    fn sub(lhs: EcPoint, rhs: EcPoint) -> EcPoint {
        let nz_point: Option<NonZero<EcPoint>> = rhs.try_into();
        if nz_point.is_none() {
            // lhs - 0 = lhs.
            return lhs;
        }
        // lhs - rhs = lhs + (-rhs).
        lhs + (-rhs)
    }
}

#[feature("deprecated-op-assign-traits")]
impl EcPointSubEq of crate::traits::SubEq<EcPoint> {
    #[inline]
    fn sub_eq(ref self: EcPoint, other: EcPoint) {
        self = Sub::sub(self, other);
    }
}