-
Notifications
You must be signed in to change notification settings - Fork 501
/
lib.cairo
406 lines (338 loc) · 9.11 KB
/
lib.cairo
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
pub mod traits;
#[feature("deprecated-index-traits")]
#[feature("deprecated-op-assign-traits")]
#[allow(unused_imports)]
use traits::{
Add, AddEq, BitAnd, BitNot, BitOr, BitXor, Copy, Div, DivEq, DivRem, Drop, Mul, MulEq,
PartialEq, PartialOrd, Rem, RemEq, Sub, SubEq, TupleSize0Copy, TupleSize0Drop, Not, Neg, Into,
TryInto, Index, IndexView, Destruct, Default, Felt252DictValue, PanicDestruct,
};
use serde::Serde;
pub type usize = u32;
#[derive(Copy, Drop, Default)]
pub enum bool {
#[default]
False,
True,
}
impl BoolSerde of Serde<bool> {
fn serialize(self: @bool, ref output: Array<felt252>) {
if *self {
1_felt252
} else {
0_felt252
}.serialize(ref output);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<bool> {
Option::Some(*serialized.pop_front()? != 0)
}
}
extern fn bool_and_impl(lhs: bool, rhs: bool) -> (bool,) implicits() nopanic;
impl BoolBitAnd of BitAnd<bool> {
#[inline]
fn bitand(lhs: bool, rhs: bool) -> bool {
let (r,) = bool_and_impl(lhs, rhs);
r
}
}
extern fn bool_or_impl(lhs: bool, rhs: bool) -> (bool,) implicits() nopanic;
impl BoolBitOr of BitOr<bool> {
#[inline]
fn bitor(lhs: bool, rhs: bool) -> bool {
let (r,) = bool_or_impl(lhs, rhs);
r
}
}
extern fn bool_not_impl(a: bool) -> (bool,) implicits() nopanic;
#[inline]
impl BoolNot of Not<bool> {
#[inline]
fn not(a: bool) -> bool implicits() nopanic {
let (r,) = bool_not_impl(a);
r
}
}
extern fn bool_xor_impl(lhs: bool, rhs: bool) -> (bool,) implicits() nopanic;
impl BoolBitXor of BitXor<bool> {
#[inline]
fn bitxor(lhs: bool, rhs: bool) -> bool {
let (r,) = bool_xor_impl(lhs, rhs);
r
}
}
impl BoolPartialEq of PartialEq<bool> {
#[inline]
fn eq(lhs: @bool, rhs: @bool) -> bool {
match lhs {
false => !*rhs,
true => *rhs,
}
}
#[inline]
fn ne(lhs: @bool, rhs: @bool) -> bool {
match lhs {
false => *rhs,
true => !*rhs,
}
}
}
/// Default values for felt252_dict values.
impl BoolFelt252DictValue of Felt252DictValue<bool> {
#[inline]
fn zero_default() -> bool nopanic {
false
}
}
extern fn bool_to_felt252(a: bool) -> felt252 implicits() nopanic;
impl BoolIntoFelt252 of Into<bool, felt252> {
#[inline]
fn into(self: bool) -> felt252 implicits() nopanic {
bool_to_felt252(self)
}
}
pub mod boolean;
pub mod circuit;
/// General purpose implicits.
pub extern type RangeCheck;
pub extern type SegmentArena;
/// felt252.
mod felt_252;
#[allow(unused_imports)]
use felt_252::{Felt252One, Felt252Zero};
#[derive(Copy, Drop)]
pub extern type felt252;
extern fn felt252_const<const value: felt252>() -> felt252 nopanic;
impl Felt252Serde of Serde<felt252> {
fn serialize(self: @felt252, ref output: Array<felt252>) {
output.append(*self);
}
fn deserialize(ref serialized: Span<felt252>) -> Option<felt252> {
let mut snapshot = serialized.snapshot;
match crate::array::array_snapshot_pop_front(ref snapshot) {
Option::Some(x) => {
serialized = Span { snapshot };
Option::Some(*x.unbox())
},
Option::None => {
serialized = Span { snapshot };
Option::None
},
}
}
}
impl Felt252Add of Add<felt252> {
#[inline]
fn add(lhs: felt252, rhs: felt252) -> felt252 {
felt252_add(lhs, rhs)
}
}
impl Felt252AddEq of AddEq<felt252> {
#[inline]
fn add_eq(ref self: felt252, other: felt252) {
self = Add::add(self, other);
}
}
extern fn felt252_add(lhs: felt252, rhs: felt252) -> felt252 nopanic;
impl Felt252Sub of Sub<felt252> {
#[inline]
fn sub(lhs: felt252, rhs: felt252) -> felt252 {
felt252_sub(lhs, rhs)
}
}
impl Felt252SubEq of SubEq<felt252> {
#[inline]
fn sub_eq(ref self: felt252, other: felt252) {
self = Sub::sub(self, other);
}
}
extern fn felt252_sub(lhs: felt252, rhs: felt252) -> felt252 nopanic;
impl Felt252Mul of Mul<felt252> {
#[inline]
fn mul(lhs: felt252, rhs: felt252) -> felt252 {
felt252_mul(lhs, rhs)
}
}
impl Felt252MulEq of MulEq<felt252> {
#[inline]
fn mul_eq(ref self: felt252, other: felt252) {
self = Mul::mul(self, other);
}
}
extern fn felt252_mul(lhs: felt252, rhs: felt252) -> felt252 nopanic;
impl Felt252Neg of Neg<felt252> {
#[inline]
fn neg(a: felt252) -> felt252 {
a * -1
}
}
pub extern fn felt252_div(lhs: felt252, rhs: NonZero<felt252>) -> felt252 nopanic;
impl Felt252PartialEq of PartialEq<felt252> {
#[inline]
fn eq(lhs: @felt252, rhs: @felt252) -> bool {
match *lhs - *rhs {
0 => true,
_ => false,
}
}
}
extern fn felt252_is_zero(lhs: felt252) -> zeroable::IsZeroResult<felt252> nopanic;
impl Felt252TryIntoNonZero of TryInto<felt252, NonZero<felt252>> {
fn try_into(self: felt252) -> Option<NonZero<felt252>> {
match felt252_is_zero(self) {
zeroable::IsZeroResult::Zero => Option::None,
zeroable::IsZeroResult::NonZero(x) => Option::Some(x),
}
}
}
impl Felt252Default of Default<felt252> {
#[inline]
fn default() -> felt252 nopanic {
0
}
}
impl Felt252Felt252DictValue of Felt252DictValue<felt252> {
#[inline]
fn zero_default() -> felt252 nopanic {
0
}
}
// TODO(spapini): Constraint using Copy and Drop traits.
extern fn dup<T>(obj: T) -> (T, T) nopanic;
extern fn drop<T>(obj: T) nopanic;
/// Boxes.
pub mod box;
#[allow(unused_imports)]
use box::{Box, BoxTrait};
/// Nullable
pub mod nullable;
#[allow(unused_imports)]
use nullable::{Nullable, NullableTrait, match_nullable, null, nullable_from_box};
/// Module for `Array` and other continuous same type collections.
pub mod array;
#[allow(unused_imports)]
use array::{Array, ArrayTrait};
/// Span.
#[allow(unused_imports)]
use array::{Span, SpanTrait};
/// Dictionary.
pub mod dict;
#[allow(unused_imports)]
use dict::{
Felt252Dict, SquashedFelt252Dict, felt252_dict_new, felt252_dict_squash, Felt252DictTrait,
};
/// Result.
pub mod result;
#[allow(unused_imports)]
use result::{Result, ResultTrait};
/// Option.
pub mod option;
#[allow(unused_imports)]
use option::{Option, OptionTrait};
/// Clone.
pub mod clone;
#[allow(unused_imports)]
use clone::Clone;
/// EC.
pub mod ec;
#[allow(unused_imports)]
use ec::{EcOp, EcPoint, EcState};
pub mod ecdsa;
/// Integer.
#[feature("corelib-internal-use")]
pub mod integer;
#[allow(unused_imports)]
use integer::{
i8, I8IntoFelt252, i16, I16IntoFelt252, i32, I32IntoFelt252, i64, I64IntoFelt252, i128,
I128IntoFelt252, NumericLiteral, u128, u128_is_zero, u8, u16, u32, u64, u256, Felt252TryIntoU8,
U8IntoFelt252, Felt252TryIntoU16, U16IntoFelt252, Felt252TryIntoU32, U32IntoFelt252,
Felt252TryIntoU64, U64IntoFelt252, Felt252TryIntoU128, U128IntoFelt252, Felt252IntoU256,
Bitwise,
};
#[feature("corelib-internal-use")]
#[deprecated(feature: "corelib-internal-use", note: "Use `core::num::traits::Sqrt` instead")]
#[allow(unused_imports)]
use integer::{u128_sqrt, u256_sqrt};
/// Math.
#[feature("corelib-internal-use")]
pub mod math;
/// Module containing the traits for relevant for numeric types.
pub mod num;
/// Module containing the operations that can be performed on the different types.
pub mod ops;
/// Module for comparison operations.
pub mod cmp;
/// Module for handling gas operations.
pub mod gas;
#[allow(unused_imports)]
use gas::{BuiltinCosts, GasBuiltin, get_builtin_costs};
/// Panics.
pub mod panics;
#[allow(unused_imports)]
use panics::{panic, Panic, PanicResult};
pub enum never {}
#[inline]
pub fn panic_with_felt252(err_code: felt252) -> never {
panic(array![err_code])
}
#[inline]
pub fn assert(cond: bool, err_code: felt252) {
if !cond {
panic_with_felt252(err_code)
}
}
/// Serialization and Deserialization.
pub mod serde;
/// Hash functions.
pub mod hash;
pub mod keccak;
pub mod sha256;
/// Pedersen
pub mod pedersen;
#[allow(unused_imports)]
use pedersen::Pedersen;
/// Poseidon
pub mod poseidon;
#[allow(unused_imports)]
use poseidon::Poseidon;
/// Debug.
pub mod debug;
pub mod fmt;
/// Starknet
#[feature("corelib-internal-use")]
pub mod starknet;
#[allow(unused_imports)]
use starknet::System;
/// Internals.
pub mod internal;
/// Zeroable.
pub mod zeroable;
#[allow(unused_imports)]
use zeroable::{Zeroable, NonZero};
/// bytes31.
pub mod bytes_31;
#[allow(unused_imports)]
use bytes_31::{
bytes31, bytes31_const, Bytes31IndexView, Bytes31IntoFelt252, Bytes31Trait,
Felt252TryIntoBytes31,
};
/// BytesArray.
pub mod byte_array;
#[allow(unused_imports)]
use byte_array::{ByteArray, ByteArrayIndexView, ByteArrayStringLiteral, ByteArrayTrait};
/// String.
pub mod string;
#[allow(unused_imports)]
use string::StringLiteral;
/// to_byte_array.
pub mod to_byte_array;
#[cfg(test)]
mod test;
/// Module for testing only.
pub mod testing;
/// Metaprogramming.
pub mod metaprogramming;
/// Preludes.
#[allow(unused_imports)]
mod prelude;
/// Iterators.
pub mod iter;