tasm_lib/structure/
auto_generated_tasm_object_implementations.rs

1// Contains field-by-field copies of various types defined outside this
2// repository to derive `TasmObject` on. These “fake” types are then used
3// to implement `TasmObject` for the actual type by forwarding function calls.
4
5use triton_vm::fri::AuthenticationStructure;
6use triton_vm::prelude::*;
7use triton_vm::proof_item::FriResponse;
8
9use crate::prelude::*;
10use crate::twenty_first::prelude::MmrMembershipProof;
11use crate::twenty_first::util_types::mmr::mmr_accumulator::MmrAccumulator;
12use crate::twenty_first::util_types::mmr::mmr_successor_proof::MmrSuccessorProof;
13
14macro_rules! derive_tasm_object_for {
15    ($actual:ident using $fake:ident {$($field:ident: $field_type:ty,)* $(,)?}) => {
16        #[derive(BFieldCodec, TasmObject)]
17        struct $fake { $($field: $field_type),* }
18
19        impl TasmObject for $actual {
20            fn label_friendly_name() -> String {
21                stringify!($actual).to_string()
22            }
23
24            fn compute_size_and_assert_valid_size_indicator(
25                library: &mut Library,
26            ) -> Vec<LabelledInstruction> {
27                $fake::compute_size_and_assert_valid_size_indicator(library)
28            }
29
30            fn decode_iter<Itr: Iterator<Item = BFieldElement>>(
31                iterator: &mut Itr,
32            ) -> Result<Box<Self>, Box<dyn core::error::Error + Send + Sync>> {
33                let $fake { $($field),* } = *$fake::decode_iter(iterator)?;
34                Ok(Box::new(Self { $($field),* }))
35            }
36        }
37
38        impl TasmStruct for $actual {
39            fn get_field(field_name: &str) -> Vec<LabelledInstruction> {
40                $fake::get_field(field_name)
41            }
42
43            fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction> {
44                $fake::get_field_with_size(field_name)
45            }
46
47            fn destructure() -> Vec<LabelledInstruction> {
48                $fake::destructure()
49            }
50        }
51    };
52}
53
54derive_tasm_object_for!(
55    Claim using FakeClaim {
56        program_digest: Digest,
57        version: u32,
58        input: Vec<BFieldElement>,
59        output: Vec<BFieldElement>,
60    }
61);
62
63derive_tasm_object_for!(
64    FriResponse using FakeFriResponse {
65        auth_structure: AuthenticationStructure,
66        revealed_leaves: Vec<XFieldElement>,
67    }
68);
69
70derive_tasm_object_for!(
71    MmrMembershipProof using FakeMmrMembershipProof {
72        authentication_path: Vec<Digest>,
73    }
74);
75
76derive_tasm_object_for!(
77    MmrSuccessorProof using FakeMmrSuccessorProof {
78        paths: Vec<Digest>,
79    }
80);
81
82// can't use macro: MmrAccumulator has private fields and uses a dedicated
83// constructor
84#[derive(BFieldCodec, TasmObject)]
85struct FakeMmrAccumulator {
86    leaf_count: u64,
87    peaks: Vec<Digest>,
88}
89
90impl TasmObject for MmrAccumulator {
91    fn label_friendly_name() -> String {
92        "MmrAccumulator".to_string()
93    }
94
95    fn compute_size_and_assert_valid_size_indicator(
96        library: &mut Library,
97    ) -> Vec<LabelledInstruction> {
98        FakeMmrAccumulator::compute_size_and_assert_valid_size_indicator(library)
99    }
100
101    fn decode_iter<Itr: Iterator<Item = BFieldElement>>(
102        iterator: &mut Itr,
103    ) -> Result<Box<Self>, Box<dyn core::error::Error + Send + Sync>> {
104        let FakeMmrAccumulator { leaf_count, peaks } = *FakeMmrAccumulator::decode_iter(iterator)?;
105
106        Ok(Box::new(Self::init(peaks, leaf_count)))
107    }
108}
109
110impl TasmStruct for MmrAccumulator {
111    fn get_field(field_name: &str) -> Vec<LabelledInstruction> {
112        FakeMmrAccumulator::get_field(field_name)
113    }
114
115    fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction> {
116        FakeMmrAccumulator::get_field_with_size(field_name)
117    }
118
119    fn destructure() -> Vec<LabelledInstruction> {
120        FakeMmrAccumulator::destructure()
121    }
122}