Skip to content

Commit bc6c301

Browse files
committed
refactor(TasmObject)!: Move checks to compile time
Add trait `TasmStruct`, moving struct-only capabilities out of `TasmObject`. This change neither adds nor removes functionality, it only moves checks from runtime to compile time.
1 parent 94fb493 commit bc6c301

File tree

7 files changed

+138
-264
lines changed

7 files changed

+138
-264
lines changed

tasm-lib/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub use crate::library::Library;
1212
pub use crate::memory::dyn_malloc::DynMalloc;
1313
pub use crate::memory::memcpy::MemCpy;
1414
pub use crate::structure::tasm_object::TasmObject;
15+
pub use crate::structure::tasm_object::TasmStruct;
1516
pub use crate::traits::basic_snippet::BasicSnippet;
1617
pub use crate::triton_vm::prelude::Digest;
1718
pub use crate::triton_vm::prelude::Tip5;

tasm-lib/src/structure/auto_generated_tasm_object_implementations.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use triton_vm::fri::AuthenticationStructure;
66
use triton_vm::prelude::*;
77
use triton_vm::proof_item::FriResponse;
88

9-
use crate::library::Library;
10-
use crate::prelude::TasmObject;
9+
use crate::prelude::*;
1110
use crate::twenty_first::prelude::MmrMembershipProof;
1211
use crate::twenty_first::util_types::mmr::mmr_accumulator::MmrAccumulator;
1312
use crate::twenty_first::util_types::mmr::mmr_successor_proof::MmrSuccessorProof;
@@ -22,18 +21,6 @@ macro_rules! derive_tasm_object_for {
2221
stringify!($actual).to_string()
2322
}
2423

25-
fn get_field(field_name: &str) -> Vec<LabelledInstruction> {
26-
$fake::get_field(field_name)
27-
}
28-
29-
fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction> {
30-
$fake::get_field_with_size(field_name)
31-
}
32-
33-
fn get_field_start_with_jump_distance(field_name: &str) -> Vec<LabelledInstruction> {
34-
$fake::get_field_start_with_jump_distance(field_name)
35-
}
36-
3724
fn compute_size_and_assert_valid_size_indicator(
3825
library: &mut Library,
3926
) -> Vec<LabelledInstruction> {
@@ -47,6 +34,20 @@ macro_rules! derive_tasm_object_for {
4734
Ok(Box::new(Self { $($field),* }))
4835
}
4936
}
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 get_field_start_with_jump_distance(field_name: &str) -> Vec<LabelledInstruction> {
48+
$fake::get_field_start_with_jump_distance(field_name)
49+
}
50+
}
5051
};
5152
}
5253

@@ -91,18 +92,6 @@ impl TasmObject for MmrAccumulator {
9192
"MmrAccumulator".to_string()
9293
}
9394

94-
fn get_field(field_name: &str) -> Vec<LabelledInstruction> {
95-
FakeMmrAccumulator::get_field(field_name)
96-
}
97-
98-
fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction> {
99-
FakeMmrAccumulator::get_field_with_size(field_name)
100-
}
101-
102-
fn get_field_start_with_jump_distance(field_name: &str) -> Vec<LabelledInstruction> {
103-
FakeMmrAccumulator::get_field_start_with_jump_distance(field_name)
104-
}
105-
10695
fn compute_size_and_assert_valid_size_indicator(
10796
library: &mut Library,
10897
) -> Vec<LabelledInstruction> {
@@ -117,3 +106,17 @@ impl TasmObject for MmrAccumulator {
117106
Ok(Box::new(Self::init(peaks, leaf_count)))
118107
}
119108
}
109+
110+
impl 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 get_field_start_with_jump_distance(field_name: &str) -> Vec<LabelledInstruction> {
120+
FakeMmrAccumulator::get_field_start_with_jump_distance(field_name)
121+
}
122+
}

tasm-lib/src/structure/manual_tasm_object_implementations.rs

Lines changed: 6 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,12 @@ use crate::prelude::*;
99

1010
impl<const N: usize, T> TasmObject for [T; N]
1111
where
12-
T: BFieldCodec + TasmObject,
12+
T: TasmObject,
1313
{
1414
fn label_friendly_name() -> String {
1515
format!("array{}___{}", N, T::label_friendly_name())
1616
}
1717

18-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
19-
todo!()
20-
}
21-
22-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
23-
todo!()
24-
}
25-
26-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
27-
todo!()
28-
}
29-
3018
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
3119
if let Some(static_size) = T::static_length() {
3220
let own_size = static_size * N;
@@ -49,24 +37,12 @@ where
4937

5038
impl<T> TasmObject for Vec<T>
5139
where
52-
T: BFieldCodec + TasmObject,
40+
T: TasmObject,
5341
{
5442
fn label_friendly_name() -> String {
5543
format!("vec___{}", T::label_friendly_name())
5644
}
5745

58-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
59-
panic!("`Vec` does not have fields; cannot access them")
60-
}
61-
62-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
63-
panic!("`Vec` does not have fields; cannot access them")
64-
}
65-
66-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
67-
panic!("`Vec` does not have fields; cannot access them")
68-
}
69-
7046
fn compute_size_and_assert_valid_size_indicator(
7147
library: &mut Library,
7248
) -> Vec<LabelledInstruction> {
@@ -209,18 +185,6 @@ impl TasmObject for BFieldElement {
209185
DataType::Bfe.label_friendly_name()
210186
}
211187

212-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
213-
panic!()
214-
}
215-
216-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
217-
panic!()
218-
}
219-
220-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
221-
panic!()
222-
}
223-
224188
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
225189
panic!("Size is known statically for BFieldElement encoding")
226190
}
@@ -236,18 +200,6 @@ impl TasmObject for XFieldElement {
236200
DataType::Xfe.label_friendly_name()
237201
}
238202

239-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
240-
panic!()
241-
}
242-
243-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
244-
panic!()
245-
}
246-
247-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
248-
panic!()
249-
}
250-
251203
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
252204
panic!("Size is known statically for XFieldElement encoding")
253205
}
@@ -266,18 +218,6 @@ impl TasmObject for Digest {
266218
DataType::Digest.label_friendly_name()
267219
}
268220

269-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
270-
panic!()
271-
}
272-
273-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
274-
panic!()
275-
}
276-
277-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
278-
panic!()
279-
}
280-
281221
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
282222
panic!("Size is known statically for Digest encoding")
283223
}
@@ -296,18 +236,6 @@ impl TasmObject for bool {
296236
DataType::Bool.label_friendly_name()
297237
}
298238

299-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
300-
panic!()
301-
}
302-
303-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
304-
panic!()
305-
}
306-
307-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
308-
panic!()
309-
}
310-
311239
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
312240
panic!("Size is known statically for bool encoding")
313241
}
@@ -327,18 +255,6 @@ impl TasmObject for u32 {
327255
DataType::U32.label_friendly_name()
328256
}
329257

330-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
331-
panic!()
332-
}
333-
334-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
335-
panic!()
336-
}
337-
338-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
339-
panic!()
340-
}
341-
342258
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
343259
panic!("Size is known statically for u32 encoding")
344260
}
@@ -359,18 +275,6 @@ impl TasmObject for u64 {
359275
DataType::U64.label_friendly_name()
360276
}
361277

362-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
363-
panic!()
364-
}
365-
366-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
367-
panic!()
368-
}
369-
370-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
371-
panic!()
372-
}
373-
374278
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
375279
panic!("Size is known statically for u64 encoding")
376280
}
@@ -393,18 +297,6 @@ impl TasmObject for u128 {
393297
DataType::U128.label_friendly_name()
394298
}
395299

396-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
397-
panic!()
398-
}
399-
400-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
401-
panic!()
402-
}
403-
404-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
405-
panic!()
406-
}
407-
408300
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
409301
panic!("Size is known statically for u128 encoding")
410302
}
@@ -426,8 +318,8 @@ impl TasmObject for u128 {
426318

427319
impl<T, S> TasmObject for (T, S)
428320
where
429-
T: TasmObject + BFieldCodec,
430-
S: TasmObject + BFieldCodec,
321+
T: TasmObject,
322+
S: TasmObject,
431323
{
432324
fn label_friendly_name() -> String {
433325
format!(
@@ -437,18 +329,6 @@ where
437329
)
438330
}
439331

440-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
441-
panic!()
442-
}
443-
444-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
445-
panic!()
446-
}
447-
448-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
449-
panic!()
450-
}
451-
452332
fn compute_size_and_assert_valid_size_indicator(
453333
library: &mut Library,
454334
) -> Vec<LabelledInstruction> {
@@ -558,18 +438,6 @@ impl TasmObject for Polynomial<'_, XFieldElement> {
558438
"polynomial_xfe".to_owned()
559439
}
560440

561-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
562-
todo!()
563-
}
564-
565-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
566-
todo!()
567-
}
568-
569-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
570-
todo!()
571-
}
572-
573441
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
574442
triton_asm!(
575443
// _ *field_size
@@ -613,18 +481,6 @@ impl TasmObject for Proof {
613481
"tvm_proof".to_owned()
614482
}
615483

616-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
617-
panic!()
618-
}
619-
620-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
621-
panic!()
622-
}
623-
624-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
625-
panic!()
626-
}
627-
628484
fn compute_size_and_assert_valid_size_indicator(_: &mut Library) -> Vec<LabelledInstruction> {
629485
// Proofs are special, as the fields of a proof is only accessed through
630486
// the [`DequeueNextAs`](crate::verifier::vm_proof_iter::dequeue_next_as)
@@ -653,24 +509,12 @@ impl TasmObject for Proof {
653509

654510
impl<T> TasmObject for Option<T>
655511
where
656-
T: TasmObject + BFieldCodec,
512+
T: TasmObject,
657513
{
658514
fn label_friendly_name() -> String {
659515
format!("option_L_{}_R", T::label_friendly_name())
660516
}
661517

662-
fn get_field(_field_name: &str) -> Vec<LabelledInstruction> {
663-
panic!("cannot get field of an option type");
664-
}
665-
666-
fn get_field_with_size(_field_name: &str) -> Vec<LabelledInstruction> {
667-
panic!("cannot get field with size of an option type");
668-
}
669-
670-
fn get_field_start_with_jump_distance(_field_name: &str) -> Vec<LabelledInstruction> {
671-
panic!("cannot get field start with jump distance of an option type");
672-
}
673-
674518
fn compute_size_and_assert_valid_size_indicator(
675519
library: &mut Library,
676520
) -> Vec<LabelledInstruction> {
@@ -777,7 +621,7 @@ mod tests {
777621
use super::*;
778622
use crate::memory::encode_to_memory;
779623

780-
fn decode_iter_prop<T: TasmObject + BFieldCodec + Eq + Debug>(obj_written: T) {
624+
fn decode_iter_prop<T: TasmObject + Eq + Debug>(obj_written: T) {
781625
let mut memory = HashMap::default();
782626
let address = random();
783627
encode_to_memory(&mut memory, address, &obj_written);

0 commit comments

Comments
 (0)