cairo-lang-runner 2.18.0

Basic cairo runner.
Documentation
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
use cairo_lang_casm::assembler::AssembledCairoProgram;
use cairo_lang_casm::inline::CasmContext;
use cairo_lang_casm::{casm, cell_ref};
use cairo_lang_sierra_to_casm::compiler::{CairoProgram, CairoProgramDebugInfo};
use cairo_lang_utils::byte_array::BYTE_ARRAY_MAGIC;
use cairo_vm::vm::runners::cairo_runner::RunResources;
use indoc::indoc;
use itertools::Itertools;
use num_traits::ToPrimitive;
use starknet_types_core::felt::Felt as Felt252;
use test_case::test_case;

use super::format_for_debug;
use crate::casm_run::contract_address::calculate_contract_address;
use crate::casm_run::{RunFunctionResult, run_function};
use crate::short_string::{as_cairo_short_string, as_cairo_short_string_ex};
use crate::{CairoHintProcessor, StarknetState, build_hints_dict};

/// Creates a new `AssembledCairoProgram` from the given `CasmContext`.
fn assembled(casm: CasmContext) -> AssembledCairoProgram {
    CairoProgram {
        instructions: casm.instructions,
        consts_info: Default::default(),
        debug_info: CairoProgramDebugInfo { sierra_statement_info: vec![] },
    }
    .assemble()
}

#[test_case(
    casm! {
        [ap] = (-5), ap++;
        [ap] = 7, ap++;
        ret;
    },
    2,
    &[-5, 7];
    "simple ap sets"
)]
#[test_case(
    casm! {
        ap += 1;
        [ap] = 123, ap++;
        [ap] = 456, ap++;
        [fp] = [ap - 2] + [ap - 1];
        ret;
    },
    3,
    &[579, 123, 456];
    "sum ap into fp"
)]
#[test_case(
    casm! {
        [ap] = 1, ap++;
        jmp rel 2 if [ap - 1] != 0;
        [ap] = 5, ap++;
        [ap] = 0, ap++;
        jmp rel 2 if [ap - 1] != 0;
        [ap] = 3, ap++;
        [ap] = 4, ap++;
        ret;
    },
    5,
    &[1, 5, 0, 3, 4];
    "jumps"
)]
#[test_case(
    casm! {
        [ap] = 39, ap++;
        %{ memory[ap] = 13 < memory[ap - 1] %}
        ap += 1;
        [ap] = [ap - 1] + 83, ap++;
        ret;
    },
    3,
    &[39, 1, 84];
    "less than hint"
)]
#[test_case(
    casm! {
        [ap] = 5, ap++;
        [ap] = 39, ap++;
        %{ (memory[ap], memory[ap + 1]) = divmod(memory[ap - 1], memory[ap - 2]) %}
        ap += 2;
        ret;
    },
    4,
    &[5, 39, 7, 4];
    "divmod hint"
)]
#[test_case(
    casm! {
        [ap + 0] = 1, ap++;
        [ap + 0] = 1, ap++;
        [ap + 0] = 13, ap++;
        call rel 3;
        ret;
        jmp rel 5 if [fp + -3] != 0;
        [ap + 0] = [fp + -5], ap++;
        jmp rel 8;
        [ap + 0] = [fp + -4], ap++;
        [ap + 0] = [fp + -5] + [fp + -4], ap++;
        [fp + -3] = [ap + 0] + 1, ap++;
        call rel (-9);
        ret;
    },
    1,
    &[377];
    "fib(1, 1, 13)"
)]
#[test_case(
    casm! {
        [ap + 0] = 2, ap++;
        [ap + 0] = 1, ap++;
        [ap - 1] = [ap + 0] * [ap - 2], ap++; // Calculates.
        [ap - 2] = [ap - 1] * [ap - 3]; // Validates the calculation.
        ret;
    },
    0,
    &[];
    "simple_division"
)]
fn test_runner(function: CasmContext, n_returns: usize, expected: &[i128]) {
    let program = assembled(function);
    let (hints_dict, string_to_hint) = build_hints_dict(&program.hints);
    let mut hint_processor = CairoHintProcessor {
        runner: None,
        user_args: vec![],
        string_to_hint,
        starknet_state: StarknetState::default(),
        run_resources: RunResources::default(),
        syscalls_used_resources: Default::default(),
        no_temporary_segments: true,
        markers: Default::default(),
        panic_traceback: Default::default(),
    };

    let RunFunctionResult { ap, memory, .. } =
        run_function(program.bytecode.iter(), vec![], |_| Ok(()), &mut hint_processor, hints_dict)
            .expect("Running code failed.");
    let ret_memory = memory.into_iter().skip(ap - n_returns);
    assert_eq!(
        ret_memory.take(n_returns).map(|cell| cell.unwrap()).collect_vec(),
        expected.iter().copied().map(Felt252::from).collect_vec()
    );
}

#[test]
fn test_allocate_segment() {
    let program = assembled(casm! {
        [ap] = 1337, ap++;
        %{ memory[ap] = segments.add() %}
        [ap - 1] = [[&cell_ref!([ap])]];
        ret;
    });
    let (hints_dict, string_to_hint) = build_hints_dict(&program.hints);
    let mut hint_processor = CairoHintProcessor {
        runner: None,
        user_args: vec![],
        string_to_hint,
        starknet_state: StarknetState::default(),
        run_resources: RunResources::default(),
        syscalls_used_resources: Default::default(),
        no_temporary_segments: true,
        markers: Default::default(),
        panic_traceback: Default::default(),
    };

    let RunFunctionResult { ap, memory, .. } =
        run_function(program.bytecode.iter(), vec![], |_| Ok(()), &mut hint_processor, hints_dict)
            .expect("Running code failed.");
    let ptr = memory[ap]
        .as_ref()
        .expect("Uninitialized value.")
        .to_usize()
        .expect("Number not in index range.");
    assert_eq!(memory[ptr], Some(Felt252::from(1337)));
}

#[test]
fn test_as_cairo_short_string() {
    // Simple short strings.
    assert_eq!(as_cairo_short_string(&Felt252::from(0)), Some("".to_string()));
    assert_eq!(as_cairo_short_string(&Felt252::from(0x61)), Some("a".to_string()));
    assert_eq!(
        as_cairo_short_string(&Felt252::from_hex_unchecked(
            "30313233343536373839303132333435363738393031323334353637383930"
        )),
        Some("0123456789012345678901234567890".to_string())
    );

    // With whitespace.
    assert_eq!(as_cairo_short_string(&Felt252::from(0x612062)), Some("a b".to_string()));
    assert_eq!(as_cairo_short_string(&Felt252::from(0x610962)), Some("a\tb".to_string()));

    // With null in the middle.
    assert_eq!(as_cairo_short_string(&Felt252::from(0x610062)), None);

    // Nulls in the end are OK.
    assert_eq!(as_cairo_short_string(&Felt252::from(0x6100)), Some("a".to_string()));
    assert_eq!(as_cairo_short_string(&Felt252::from(0x61000000)), Some("a".to_string()));

    // With a non-printable character.
    assert_eq!(as_cairo_short_string(&Felt252::from(0x610162)), None);
}

#[test]
fn test_as_cairo_short_string_ex() {
    // Simple short strings.
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0), 0), Some("".to_string()));
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x61), 1), Some("a".to_string()));
    assert_eq!(
        as_cairo_short_string_ex(
            &Felt252::from_hex_unchecked(
                "30313233343536373839303132333435363738393031323334353637383930",
            ),
            31
        ),
        Some("0123456789012345678901234567890".to_string())
    );

    // With whitespace.
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x612062), 3), Some("a b".to_string()));
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x610962), 3), Some("a\tb".to_string()));

    // Nulls are OK.
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x6100), 2), Some(r"a\0".to_string()));
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x610062), 3), Some(r"a\0b".to_string()));

    // With a non-printable character.
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x610162), 3), Some(r"a\x01b".to_string()));

    // More data than expected.
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x61), 0), None);
    assert_eq!(as_cairo_short_string_ex(&Felt252::from(0x6161), 1), None);

    // length > 31.
    assert_eq!(
        as_cairo_short_string_ex(
            &Felt252::from_hex_unchecked(
                "100000000000000000000000000000000000000000000000000000000000000",
            ),
            32
        ),
        None
    );
}

#[test]
fn test_format_for_debug() {
    // Valid short string.
    let felts = vec![Felt252::from_hex_unchecked("68656c6c6f")];
    assert_eq!(format_for_debug(felts.into_iter()), "[DEBUG]\t0x68656c6c6f ('hello')\n");

    // felt252
    let felts = vec![Felt252::from(1)];
    assert_eq!(format_for_debug(felts.into_iter()), "[DEBUG]\t0x1\n");

    // Valid string with < 31 characters (no full words).
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from(0), // No full words.
        Felt252::from_hex_unchecked("73686f72742c2062757420737472696e67"), /* 'short, but
                           * string' */
        Felt252::from(17), // pending word length
    ];
    assert_eq!(format_for_debug(felts.into_iter()), "short, but string");

    // Valid string with > 31 characters (with a full word).
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // A single full word.
        Felt252::from(1),
        // full word: 'This is a long string with more'
        Felt252::from_hex_unchecked(
            "546869732069732061206c6f6e6720737472696e672077697468206d6f7265",
        ),
        // pending word: ' than 31 characters.'
        Felt252::from_hex_unchecked("207468616e20333120636861726163746572732e"),
        // pending word length
        Felt252::from(20),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        "This is a long string with more than 31 characters."
    );

    // Only magic.
    let felts = vec![Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC)];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3\n"
    );

    // num_full_words > usize.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from_hex_unchecked("100000000"),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3
                [DEBUG]\t0x100000000
                "
        )
    );

    // Not enough data after num_full_words.
    let felts = vec![Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC), Felt252::from(0)];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3\n[DEBUG]\t0x0 \
         ('')\n"
    );

    // Not enough full words.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from(1),
        Felt252::from(0),
        Felt252::from(0),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3
            [DEBUG]\t0x1
            [DEBUG]\t0x0 ('')\n[DEBUG]\t0x0 ('')
            "
        )
    );

    // Too much data in full word.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from(1),
        Felt252::from_hex_unchecked(
            "161616161616161616161616161616161616161616161616161616161616161",
        ),
        Felt252::from(0),
        Felt252::from(0),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3
            [DEBUG]\t0x1
            [DEBUG]\t0x161616161616161616161616161616161616161616161616161616161616161
            [DEBUG]\t0x0 ('')
            [DEBUG]\t0x0 ('')
            "
        )
    );

    // num_pending_bytes > usize.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from(0),
        Felt252::from(0),
        Felt252::from_hex_unchecked("100000000"),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3
            [DEBUG]\t0x0 ('')
            [DEBUG]\t0x0 ('')
            [DEBUG]\t0x100000000
            "
        )
    );

    // "Not enough" data in pending_word (nulls in the beginning).
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // No full words.
        Felt252::from(0),
        // 'a'
        Felt252::from_hex_unchecked("61"),
        // pending word length. Bigger than the actual data in the pending word.
        Felt252::from(2),
    ];
    assert_eq!(format_for_debug(felts.into_iter()), "\\0a");

    // Too much data in pending_word.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // No full words.
        Felt252::from(0),
        // 'aa'
        Felt252::from_hex_unchecked("6161"),
        // pending word length. Smaller than the actual data in the pending word.
        Felt252::from(1),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x46a6158a16a947e5916b2a2ca68501a45e93d7110e81aa2d6438b1c57c879a3
            [DEBUG]\t0x0 ('')
            [DEBUG]\t0x6161 ('aa')
            [DEBUG]\t0x1
            "
        )
    );

    // Valid string with Null.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // No full words.
        Felt252::from(0),
        // pending word: 'Hello\0world'
        Felt252::from_hex_unchecked("48656c6c6f00776f726c64"),
        // pending word length
        Felt252::from(11),
    ];
    assert_eq!(format_for_debug(felts.into_iter()), "Hello\\0world");

    // Valid string with a non printable character.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // No full words.
        Felt252::from(0),
        // pending word: 'Hello\x11world'
        Felt252::from_hex_unchecked("48656c6c6f11776f726c64"),
        // pending word length
        Felt252::from(11),
    ];
    assert_eq!(format_for_debug(felts.into_iter()), "Hello\\x11world");

    // Valid string with a newline.
    let felts = vec![
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        // No full words.
        Felt252::from(0),
        // pending word: 'Hello\nworld'
        Felt252::from_hex_unchecked("48656c6c6f0a776f726c64"),
        // pending word length
        Felt252::from(11),
    ];
    assert_eq!(format_for_debug(felts.into_iter()), "Hello\nworld");

    // Multiple values: (felt, string, short_string, felt)
    let felts = vec![
        // felt: 0x9999
        Felt252::from(0x9999),
        // String: "hello"
        Felt252::from_hex_unchecked(BYTE_ARRAY_MAGIC),
        Felt252::from(0),
        Felt252::from_hex_unchecked("68656c6c6f"),
        Felt252::from(5),
        // Short string: 'world'
        Felt252::from_hex_unchecked("776f726c64"),
        // felt: 0x8888
        Felt252::from(0x8888),
    ];
    assert_eq!(
        format_for_debug(felts.into_iter()),
        indoc!(
            "[DEBUG]\t0x9999
            hello
            [DEBUG]\t0x776f726c64 ('world')
            [DEBUG]\t0x8888
            "
        )
    );
}

#[test]
fn test_calculate_contract_address() {
    let salt = Felt252::from_dec_str("122660764594045088044512115").unwrap();
    let deployer_address = Felt252::from(0x01);
    let class_hash = Felt252::from_dec_str(
        "1779576919126046589190499439779938629977579841313883525093195577363779864274",
    )
    .unwrap();
    let calldata = vec![deployer_address, salt];
    let deployed_contract_address =
        calculate_contract_address(&salt, &class_hash, &calldata, &deployer_address);

    assert_eq!(
        Felt252::from_dec_str(
            "2288343933438457476985546536845198482236255384896285993520343604559094835567"
        )
        .unwrap(),
        deployed_contract_address
    );
}