miden-stdlib 0.19.1

Miden VM standard library
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use miden_air::RowIndex;
use miden_processor::{ExecutionError, ZERO};
use miden_utils_testing::{build_expected_hash, build_expected_perm, expect_exec_error_matches};

#[test]
fn test_invalid_end_addr() {
    // end_addr can not be smaller than start_addr
    let empty_range = "
    use.std::crypto::hashes::rpo

    begin
        push.0999 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words
    end
    ";
    let test = build_test!(empty_range, &[]);

    expect_exec_error_matches!(
        test,
        ExecutionError::FailedAssertion{ clk, err_code, err_msg , label: _, source_file: _ }
        if clk == RowIndex::from(24) && err_code == ZERO && err_msg.is_none()
    );
}

#[test]
fn test_hash_empty() {
    // computes the hash for 8 consecutive zeros using mem_stream directly
    let two_zeros_mem_stream = "
    use.std::crypto::hashes::rpo

    begin
        # mem_stream state
        push.1000 padw padw padw
        mem_stream hperm

        # drop everything except the hash
        exec.rpo::squeeze_digest movup.4 drop

        # truncate stack
        swapw dropw
    end
    ";

    #[rustfmt::skip]
    let zero_hash: Vec<u64> = build_expected_hash(&[
        0, 0, 0, 0,
        0, 0, 0, 0,
    ]).into_iter().map(|e| e.as_int()).collect();
    build_test!(two_zeros_mem_stream, &[]).expect_stack(&zero_hash);

    // checks the hash compute from 8 zero elements is the same when using hash_memory_words
    let two_zeros = "
    use.std::crypto::hashes::rpo

    begin
        push.1008 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    build_test!(two_zeros, &[]).expect_stack(&zero_hash);
}

#[test]
fn test_single_iteration() {
    // computes the hash of 1 using mem_stream
    let one_memstream = "
    use.std::crypto::hashes::rpo

    begin
        # insert 1 to memory
        push.1.1000 mem_store

        # mem_stream state
        push.1000 padw padw padw
        mem_stream hperm

        # drop everything except the hash
        exec.rpo::squeeze_digest movup.4 drop

        # truncate stack
        swapw dropw
    end
    ";

    #[rustfmt::skip]
    let one_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
        0, 0, 0, 0,
    ]).into_iter().map(|e| e.as_int()).collect();
    build_test!(one_memstream, &[]).expect_stack(&one_hash);

    // checks the hash of 1 is the same when using hash_memory_words
    // Note: This is testing the hashing of two words, so no padding is added
    // here
    let one_element = "
    use.std::crypto::hashes::rpo

    begin
        # insert 1 to memory
        push.1.1000 mem_store

        push.1008 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    build_test!(one_element, &[]).expect_stack(&one_hash);
}

#[test]
fn test_hash_one_word() {
    // computes the hash of a single 1, the procedure adds padding as required

    // This slice must not have the second word, that will be padded by the hasher with the correct
    // value
    #[rustfmt::skip]
    let one_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
    ]).into_iter().map(|e| e.as_int()).collect();

    // checks the hash of 1 is the same when using hash_memory_words
    let one_element = "
    use.std::crypto::hashes::rpo

    begin
        push.1.1000 mem_store # push data to memory

        push.1004 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    build_test!(one_element, &[]).expect_stack(&one_hash);
}

#[test]
fn test_hash_even_words() {
    // checks the hash of two words
    let even_words = "
    use.std::crypto::hashes::rpo

    begin
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw

        push.1008 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    #[rustfmt::skip]
    let even_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
        0, 1, 0, 0,
    ]).into_iter().map(|e| e.as_int()).collect();
    build_test!(even_words, &[]).expect_stack(&even_hash);
}

#[test]
fn test_hash_odd_words() {
    // checks the hash of three words
    let odd_words = "
    use.std::crypto::hashes::rpo

    begin
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw
        push.0.0.1.0.1008 mem_storew_be dropw

        push.1012 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    #[rustfmt::skip]
    let odd_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
    ]).into_iter().map(|e| e.as_int()).collect();
    build_test!(odd_words, &[]).expect_stack(&odd_hash);
}

#[test]
fn test_absorb_double_words_from_memory() {
    let even_words = "
    use.std::sys
    use.std::crypto::hashes::rpo

    begin
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw

        push.1008      # end address
        push.1000      # start address
        padw padw padw # hasher state
        exec.rpo::absorb_double_words_from_memory

        # truncate stack
        exec.sys::truncate_stack
    end
    ";

    #[rustfmt::skip]
    let mut even_hash: Vec<u64> = build_expected_perm(&[
        0, 0, 0, 0, // capacity, no padding required
        1, 0, 0, 0, // first word of the rate
        0, 1, 0, 0, // second word of the rate
    ]).into_iter().map(|e| e.as_int()).collect();

    // start and end addr
    even_hash.push(1008);
    even_hash.push(1008);

    build_test!(even_words, &[]).expect_stack(&even_hash);
}

#[test]
fn test_hash_memory_double_words() {
    // test the standard case
    let double_words = "
    use.std::sys
    use.std::crypto::hashes::rpo

    begin
        # store four words (two double words) in memory
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw
        push.0.0.1.0.1008 mem_storew_be dropw
        push.0.0.0.1.1012 mem_storew_be dropw

        push.1016      # end address
        push.1000      # start address
        # => [start_addr, end_addr]

        exec.rpo::hash_memory_double_words
        # => [HASH]

        # truncate stack
        exec.sys::truncate_stack
        # => [HASH]
    end
    ";

    #[rustfmt::skip]
    let resulting_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1,
    ]).into_iter().map(|e| e.as_int()).collect();

    build_test!(double_words, &[]).expect_stack(&resulting_hash);

    // test the corner case when the end pointer equals to the start pointer
    let empty_double_words = r#"
    use.std::sys
    use.std::crypto::hashes::rpo

    begin
        push.1000.1000 # start and end addresses
        # => [start_addr, end_addr]

        exec.rpo::hash_memory_double_words
        # => [HASH]

        # assert that the resulting hash is equal to the empty word
        dupw padw assert_eqw.err="resulting hash should be equal to the empty word"

        # truncate stack
        exec.sys::truncate_stack
        # => [HASH]
    end
    "#;

    build_test!(empty_double_words, &[]).expect_stack(&[0u64; 4]);
}

#[test]
fn test_squeeze_digest() {
    let even_words = "
    use.std::crypto::hashes::rpo

    begin
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw
        push.0.0.1.0.1008 mem_storew_be dropw
        push.0.0.0.1.1012 mem_storew_be dropw

        push.1016      # end address
        push.1000      # start address
        padw padw padw # hasher state
        exec.rpo::absorb_double_words_from_memory

        exec.rpo::squeeze_digest

        # truncate stack
        swapdw dropw dropw
    end
    ";

    #[rustfmt::skip]
    let mut even_hash: Vec<u64> = build_expected_hash(&[
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1,
    ]).into_iter().map(|e| e.as_int()).collect();

    // start and end addr
    even_hash.push(1016);
    even_hash.push(1016);

    build_test!(even_words, &[]).expect_stack(&even_hash);
}

#[test]
fn test_copy_digest() {
    let copy_digest = r#"
    use.std::sys
    use.std::crypto::hashes::rpo

    begin
        push.1.0.0.0.1000 mem_storew_be dropw
        push.0.1.0.0.1004 mem_storew_be dropw

        push.1008      # end address
        push.1000      # start address
        padw padw padw # hasher state
        exec.rpo::absorb_double_words_from_memory
        # => [C, B, A, end_ptr, end_ptr]

        # drop the pointers
        movup.12 drop movup.12 drop
        # => [C, B, A]

        # copy the result of the permutation (second word, B)
        exec.rpo::copy_digest
        # => [B, C, B, A]

        # assert that the copied word is equal to the second word in the hasher state
        dupw.2 dupw.1 assert_eqw.err="copied word should be equal to the second word in the hasher state"
        # => [B, C, B, A]

        # truncate stack
        exec.sys::truncate_stack
    end
    "#;

    #[rustfmt::skip]
    let mut resulting_stack: Vec<u64> = build_expected_perm(&[
        0, 0, 0, 0, // capacity, no padding required
        1, 0, 0, 0, // first word of the rate
        0, 1, 0, 0, // second word of the rate
    ]).into_iter().map(|e| e.as_int()).collect();

    // push the permutation result on the top of the resulting stack
    resulting_stack[4..8]
        .to_vec()
        .iter()
        .rev()
        .for_each(|hash_element| resulting_stack.insert(0, *hash_element));

    build_test!(copy_digest, &[]).expect_stack(&resulting_stack);
}

#[test]
fn test_hash_memory() {
    // hash fewer than 8 elements
    let compute_inputs_hash_5 = "
    use.std::crypto::hashes::rpo

    begin
        push.1.2.3.4.1000 mem_storew_be dropw
        push.5.0.0.0.1004 mem_storew_be dropw
        push.11

        push.5.1000

        exec.rpo::hash_memory

        # truncate stack
        swapdw dropw dropw
    end
    ";

    #[rustfmt::skip]
    let mut expected_hash: Vec<u64> = build_expected_hash(&[
        1, 2, 3, 4, 5
    ]).into_iter().map(|e| e.as_int()).collect();
    // make sure that value `11` stays unchanged
    expected_hash.push(11);
    build_test!(compute_inputs_hash_5, &[]).expect_stack(&expected_hash);

    // hash exactly 8 elements
    let compute_inputs_hash_8 = "
    use.std::crypto::hashes::rpo

    begin
        push.1.2.3.4.1000 mem_storew_be dropw
        push.5.6.7.8.1004 mem_storew_be dropw
        push.11

        push.8.1000

        exec.rpo::hash_memory

        # truncate stack
        swapdw dropw dropw
    end
    ";

    #[rustfmt::skip]
    let mut expected_hash: Vec<u64> = build_expected_hash(&[
        1, 2, 3, 4, 5, 6, 7, 8
    ]).into_iter().map(|e| e.as_int()).collect();
    // make sure that value `11` stays unchanged
    expected_hash.push(11);
    build_test!(compute_inputs_hash_8, &[]).expect_stack(&expected_hash);

    // hash more than 8 elements
    let compute_inputs_hash_15 = "
    use.std::crypto::hashes::rpo

    begin
        push.1.2.3.4.1000 mem_storew_be dropw
        push.5.6.7.8.1004 mem_storew_be dropw
        push.9.10.11.12.1008 mem_storew_be dropw
        push.13.14.15.0.1012 mem_storew_be dropw
        push.11

        push.15.1000

        exec.rpo::hash_memory

        # truncate stack
        swapdw dropw dropw
    end
    ";

    #[rustfmt::skip]
    let mut expected_hash: Vec<u64> = build_expected_hash(&[
        1, 2, 3, 4,
        5, 6, 7, 8,
        9, 10, 11, 12,
        13, 14, 15
    ]).into_iter().map(|e| e.as_int()).collect();
    // make sure that value `11` stays unchanged
    expected_hash.push(11);
    build_test!(compute_inputs_hash_15, &[]).expect_stack(&expected_hash);
}

#[test]
fn test_hash_memory_empty() {
    // absorb_double_words_from_memory
    let source = "
    use.std::sys
    use.std::crypto::hashes::rpo

    begin
        push.1000      # end address
        push.1000      # start address
        padw padw padw # hasher state

        exec.rpo::absorb_double_words_from_memory

        # truncate stack
        exec.sys::truncate_stack
    end
    ";

    let mut expected_stack = vec![0; 12];
    expected_stack.push(1000);
    expected_stack.push(1000);

    build_test!(source, &[]).expect_stack(&expected_stack);

    // hash_memory_words
    let source = "
    use.std::crypto::hashes::rpo

    begin
        push.1000 # end address
        push.1000 # start address

        exec.rpo::hash_memory_words

        # truncate stack
        swapw dropw
    end
    ";

    build_test!(source, &[]).expect_stack(&[0; 4]);

    // hash_memory
    let source = "
    use.std::crypto::hashes::rpo

    begin
        push.0    # number of elements to hash
        push.1000 # start address

        exec.rpo::hash_memory

        # truncate stack
        swapw dropw
    end
    ";

    build_test!(source, &[]).expect_stack(&[0; 16]);
}