tasm_lib/structure/
manual_tasm_object_implementations.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use itertools::Itertools;
use triton_vm::prelude::*;
use twenty_first::error::BFieldCodecError;
use twenty_first::math::x_field_element::EXTENSION_DEGREE;
use twenty_first::prelude::Polynomial;

use super::tasm_object::Result;
use crate::data_type::DataType;
use crate::prelude::TasmObject;

impl<const N: usize, T> TasmObject for [T; N]
where
    T: BFieldCodec + TasmObject,
{
    fn label_friendly_name() -> String {
        format!("array{}___{}", N, T::label_friendly_name())
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut crate::prelude::Library,
    ) -> Vec<LabelledInstruction> {
        if let Some(static_size) = T::static_length() {
            let own_size = static_size * N;
            triton_asm!(
                // _ *elem[0]

                pop 1
                push {own_size}
                // _ own_size
            )
        } else {
            todo!()
        }
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(_iterator: &mut Itr) -> Result<Box<Self>> {
        todo!()
    }
}

impl<T> TasmObject for Vec<T>
where
    T: BFieldCodec + TasmObject,
{
    fn label_friendly_name() -> String {
        format!("vec___{}", T::label_friendly_name())
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("`Vec` does not have fields; cannot access them")
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("`Vec` does not have fields; cannot access them")
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("`Vec` does not have fields; cannot access them")
    }

    fn compute_size_and_assert_valid_size_indicator(
        library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        if let Some(static_size) = T::static_length() {
            // _ *list_len
            triton_asm!(
                read_mem 1
                pop 1
                // _ list_len

                push {static_size}
                mul
                addi 1
                // _ (list_len * elem_size + 1)
                // _ calculated_size
            )
        } else {
            // _ *list_len

            let verified_element_size = T::compute_size_and_assert_valid_size_indicator(library);

            let loop_label = format!(
                "tasmlib_structure_tasmobject_verify_size_indicators_dyn_elem_sizes___{}",
                T::label_friendly_name()
            );

            let loop_code = triton_asm!(
                // INVARIANT: _ remaining_elements acc_size *element_si
                {loop_label}:

                    dup 2
                    push 0
                    eq
                    skiz
                        return
                    // _ remaining_elements acc_size *element_si

                    read_mem 1
                    // _ remaining_elements acc_size element_si (*element_si-1)

                    /* Verify that max allowed size is not exceeded */
                    push {T::MAX_OFFSET}
                    dup 2
                    lt
                    assert error_id 210
                    // _ remaining_elements acc_size element_si (*element_si-1)

                    addi 2
                    // _ remaining_elements acc_size element_si *element

                    dup 0
                    {&verified_element_size}
                    // _ remaining_elements acc_size element_si *element calculated_elem_size

                    dup 2
                    eq
                    assert error_id 211
                    // _ remaining_elements acc_size element_si *element

                    dup 2
                    dup 2
                    add
                    // _ remaining_elements acc_size element_si *element acc_size'

                    /* Account for element's size indicator, since it's dynamically sized */
                    addi 1
                    // _ remaining_elements acc_size element_si *element acc_size'

                    swap 3
                    pop 1
                    // _ remaining_elements acc_size' element_si *element

                    add
                    // _ remaining_elements acc_size' *next_element

                    swap 2
                    addi -1
                    swap 2
                    // _ (remaining_elements-1) acc_size' *next_element

                    recurse
            );

            library.explicit_import(&loop_label, &loop_code);
            triton_asm!(
                // _ *list_len

                read_mem 1
                // _ list_len (*list_len - 1)

                push 0
                swap 1
                // _ list_len 0 (*list_len - 1)

                addi 2
                hint elem_si_ptr = stack[0]
                hint acc_size = stack[1]
                hint remaining_elements = stack[2]
                // _ list_len 0 (*list_len + 1)
                // _ remaining_elements acc_size *element[0]_si <-- rename

                call {loop_label}
                // _ 0 acc_size *EOF

                pop 1
                swap 1
                pop 1
                // _ acc_size

                /* Add size of (outer) list's length indicator */
                addi 1
            )
        }
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let vec_length = iterator.next().ok_or(BFieldCodecError::SequenceTooShort)?;
        let mut vector = vec![];
        for _ in 0..vec_length.value() {
            let item_length = if let Some(static_length) = T::static_length() {
                static_length
            } else {
                let dynamic_length = iterator.next().ok_or(BFieldCodecError::SequenceTooShort)?;
                usize::try_from(dynamic_length.value())?
            };
            let item_sequence = (0..item_length)
                .map(|_| iterator.next())
                .collect::<Option<Vec<_>>>()
                .ok_or(BFieldCodecError::SequenceTooShort)?;
            let item = *T::decode(&item_sequence).map_err(|e| e.into())?;
            vector.push(item);
        }

        Ok(Box::new(vector))
    }
}

impl TasmObject for BFieldElement {
    fn label_friendly_name() -> String {
        DataType::Bfe.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for BFieldElement encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let word = iterator.next().ok_or(BFieldCodecError::SequenceTooShort)?;
        Ok(Box::new(word))
    }
}

impl TasmObject for XFieldElement {
    fn label_friendly_name() -> String {
        DataType::Xfe.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for XFieldElement encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let (c_0, c_1, c_2) = iterator
            .next_tuple()
            .ok_or(BFieldCodecError::SequenceTooShort)?;

        Ok(Box::new(XFieldElement::new([c_0, c_1, c_2])))
    }
}

impl TasmObject for Digest {
    fn label_friendly_name() -> String {
        DataType::Digest.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for Digest encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let (d_0, d_1, d_2, d_3, d_4) = iterator
            .next_tuple()
            .ok_or(BFieldCodecError::SequenceTooShort)?;

        Ok(Box::new(Digest::new([d_0, d_1, d_2, d_3, d_4])))
    }
}

impl TasmObject for bool {
    fn label_friendly_name() -> String {
        DataType::Bool.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for bool encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let the_bool = iterator.next().ok_or(BFieldCodecError::SequenceTooShort)?;
        match the_bool.value() {
            0 => Ok(Box::new(false)),
            1 => Ok(Box::new(true)),
            _ => Err(Box::new(BFieldCodecError::SequenceTooShort)),
        }
    }
}

impl TasmObject for u32 {
    fn label_friendly_name() -> String {
        DataType::U32.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for u32 encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let word = iterator
            .next()
            .ok_or(BFieldCodecError::SequenceTooShort)?
            .try_into()
            .map_err(|_| BFieldCodecError::ElementOutOfRange)?;

        Ok(Box::new(word))
    }
}

impl TasmObject for u64 {
    fn label_friendly_name() -> String {
        DataType::U64.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for u64 encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let (val_lo, val_hi) = iterator
            .next_tuple()
            .ok_or(BFieldCodecError::SequenceTooShort)?;

        let too_big = |_| BFieldCodecError::ElementOutOfRange;
        let val_lo = u64::from(u32::try_from(val_lo).map_err(too_big)?);
        let val_hi = u64::from(u32::try_from(val_hi).map_err(too_big)?);

        Ok(Box::new((val_hi << 32) + val_lo))
    }
}

impl TasmObject for u128 {
    fn label_friendly_name() -> String {
        DataType::U128.label_friendly_name()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        panic!("Size is known statically for u128 encoding")
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let (lolo, lohi, hilo, hihi) = iterator
            .next_tuple()
            .ok_or(BFieldCodecError::SequenceTooShort)?;

        let too_big = |_| BFieldCodecError::ElementOutOfRange;
        let lolo = u128::from(u32::try_from(lolo).map_err(too_big)?);
        let lohi = u128::from(u32::try_from(lohi).map_err(too_big)?);
        let hilo = u128::from(u32::try_from(hilo).map_err(too_big)?);
        let hihi = u128::from(u32::try_from(hihi).map_err(too_big)?);

        Ok(Box::new((hihi << 96) + (hilo << 64) + (lohi << 32) + lolo))
    }
}

impl<T, S> TasmObject for (T, S)
where
    T: TasmObject + BFieldCodec,
    S: TasmObject + BFieldCodec,
{
    fn label_friendly_name() -> String {
        format!(
            "tuple_L_{}_{}_R",
            T::label_friendly_name(),
            S::label_friendly_name()
        )
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        let size_left = match T::static_length() {
            Some(static_size) => triton_asm!(
                // _ *left

                pop 1
                push { static_size }
                // _ left_size
            ),
            None => {
                let recursive_call = T::compute_size_and_assert_valid_size_indicator(library);

                triton_asm!(
                    // _ *left_si
                    hint left_si_ptr = stack[0]

                    read_mem 1
                    addi 2
                    // _ left_si *left

                    {&recursive_call}
                    hint calculated_left = stack[0]
                    // _ left_si calculated_left

                    dup 1
                    eq
                    assert error_id 220
                    // _ left_size

                    addi 1
                    // _ (left_size + 1)
                )
            }
        };
        let size_right = match S::static_length() {
            Some(static_size) => triton_asm!(
                // _ *right

                push { static_size }
                hint right_size = stack[0]
                 // _ *right right_size
            ),
            None => {
                let recursive_call = S::compute_size_and_assert_valid_size_indicator(library);

                triton_asm!(
                    // _ *right_si
                    hint right_si_ptr = stack[0]

                    read_mem 1
                    addi 1
                    swap 1
                    dup 1
                    addi 1
                    // _ *right_si right_si *right

                    {&recursive_call}
                    hint calculated_right = stack[0]
                    // _ *right_si right_si calculated_right

                    dup 1
                    eq
                    assert error_id 221
                    // _ *right_si right_size

                    /* Include size of size-indicator */
                    addi 1
                    // _ *right_si (right_size+1)
                )
            }
        };

        triton_asm!(
            // _ *tuple

            // TODO: addi 1 here?
            {&size_right}
            hint right_ptr_or_right_si = stack[1]
            hint right_size_incl_pot_si = stack[0]
            // _ *right right_size'

            swap 1
            dup 1
            add
            hint left = stack[0]
            // _ right_size' (*right + right_size')
            // _ right_size' *left

            {&size_left}
            hint left_size_incl_si = stack[0]
            // _ right_size' left_size'

            add
            // _ total_tuple_size <-- includes tuple-element's size-indicators (if dyn-sized)
        )
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(_iterator: &mut Itr) -> Result<Box<Self>> {
        todo!()
    }
}

impl TasmObject for Polynomial<'_, XFieldElement> {
    fn label_friendly_name() -> String {
        "polynomial_xfe".to_owned()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        todo!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut crate::prelude::Library,
    ) -> Vec<LabelledInstruction> {
        triton_asm!(
            // _ *field_size

            addi 1
            // _ *list_length

            read_mem 2
            pop 1
            // _ list_length field_size

            push {Polynomial::<XFieldElement>::MAX_OFFSET}
            dup 1
            lt
            assert
            // _ list_length field_size

            swap 1
            push {EXTENSION_DEGREE}
            mul
            addi 1
            // _ field_size calculated_field_size

            dup 1
            eq
            assert
            // _ field_size

            /* Account for a size-indicator */
            addi 1
        )
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(_iterator: &mut Itr) -> Result<Box<Self>> {
        todo!()
    }
}

impl TasmObject for Proof {
    fn label_friendly_name() -> String {
        "tvm_proof".to_owned()
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!()
    }

    fn compute_size_and_assert_valid_size_indicator(
        _library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        // Proofs are special, as the fields of a proof is only accessed through
        // the [`DequeueNextAs`](crate::verifier::vm_proof_iter::dequeue_next_as)
        // snippet which does some checks itself. So we just report the total size
        // here.

        triton_asm!(
            // _ *proof
            read_mem 1
            // _ field_0_len (*proof - 1)

            pop 1
            // _ field_0_len

            addi 1

            // _ own_size
        )
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let _ = iterator.next().ok_or(BFieldCodecError::SequenceTooShort)?;
        Ok(Box::new(Self(*Vec::decode_iter(iterator)?)))
    }
}

impl<T> TasmObject for Option<T>
where
    T: TasmObject + BFieldCodec,
{
    fn label_friendly_name() -> String {
        format!("option_L_{}_R", T::label_friendly_name())
    }

    fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("cannot get field of an option type");
    }

    fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("cannot get field with size of an option type");
    }

    fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
        panic!("cannot get field start with jump distance of an option type");
    }

    fn compute_size_and_assert_valid_size_indicator(
        library: &mut tasm_lib::Library,
    ) -> Vec<LabelledInstruction> {
        let get_payload_size = match T::static_length() {
            Some(static_size) => triton_asm!(
                // _ *value

                pop 1
                push { static_size }
                // _ value_size
            ),
            None => T::compute_size_and_assert_valid_size_indicator(library),
        };

        let some_branch_label = format!(
            "tasmlib_tasmobject_size_verifier_option_some_branch___{}",
            T::label_friendly_name()
        );
        let some_branch = triton_asm!(
            {some_branch_label}:

                // _ *value 1
                pop 1

                {&get_payload_size}
                // _ value_size

                /* Push 0 to avoid `None` branch from being taken */
                push 0

                return
        );

        let none_branch_label = "tasmlib_tasmobject_size_verifier_option_none".to_owned();
        let none_branch = triton_asm!(
            {none_branch_label}:
                // _ *ptr

                pop 1
                push 0
                // _ value_size

                return
        );

        library.explicit_import(&some_branch_label, &some_branch);
        library.explicit_import(&none_branch_label, &none_branch);

        triton_asm!(
            // _ *discriminant
            read_mem 1
            addi 2
            // _ discriminant (*discriminant + 1)

            /* Ensure discriminant has legal value */
            dup 1
            push 0
            eq
            dup 2
            push 1
            eq
            add
            // _ discriminant (*discriminant + 1) ((discriminant == 0) || (discriminant == 1))

            assert error_id 200

            swap 1
            // _ (*discriminant + 1) discriminant

            push 1
            swap 1
            // _ (*discriminant + 1) 1 discriminant

            push 1
            eq
            // _ (*discriminant + 1) 1 (discriminant == 1)

            skiz
                call {some_branch_label}
            skiz
                call {none_branch_label}

            // _ value_size
            addi 1

            // _ total_size
        )
    }

    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(iterator: &mut Itr) -> Result<Box<Self>> {
        let is_some = *bool::decode_iter(iterator)?;
        let the_option = is_some.then(|| T::decode_iter(iterator)).transpose()?;
        Ok(Box::new(the_option.map(|t| *t)))
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::fmt::Debug;

    use rand::random;

    use super::*;
    use crate::memory::encode_to_memory;

    fn decode_iter_prop<T: TasmObject + BFieldCodec + Eq + Debug>(obj_written: T) {
        let mut memory = HashMap::default();
        let address = random();
        encode_to_memory(&mut memory, address, &obj_written);
        let obj_read = *T::decode_from_memory(&memory, address).unwrap();
        assert_eq!(obj_written, obj_read);
    }

    #[test]
    fn decode_iter_bfe() {
        decode_iter_prop::<BFieldElement>(random());
    }

    #[test]
    fn decode_iter_xfe() {
        decode_iter_prop::<XFieldElement>(random());
    }

    #[test]
    fn decode_iter_digest() {
        decode_iter_prop::<Digest>(random());
    }

    #[test]
    fn decode_iter_bool() {
        decode_iter_prop::<bool>(random());
    }

    #[test]
    fn decode_iter_u32() {
        decode_iter_prop::<u32>(random());
    }

    #[test]
    fn decode_iter_u64() {
        decode_iter_prop::<u64>(random());
    }

    #[test]
    fn decode_iter_u128() {
        decode_iter_prop::<u128>(random());
    }
}