Trait TasmStruct

Source
pub trait TasmStruct: TasmObject {
    // Required methods
    fn get_field(field_name: &str) -> Vec<LabelledInstruction>;
    fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction>;
    fn destructure() -> Vec<LabelledInstruction>;
}
Expand description

This trait defines methods for dealing with custom-defined struct types from within the VM, assuming they live in memory as they are encoded with BFieldCodec.

The arguments referring to fields are strings. For structs with unnamed fields, the nth field name is implicitly field_n.

§Dyn-Compatibility

This trait is not dyn-compatible (previously known as “object safe”).

Required Methods§

Source

fn get_field(field_name: &str) -> Vec<LabelledInstruction>

Tasm code that returns a pointer to the field of the object, assuming:

  • that a pointer to the said object lives on top of the stack;
  • said object has a type that implements the TasmObject trait;
  • said object lives in memory encoded as BFieldCodec specifies.
BEFORE: _ *object
AFTER:  _ *field
Source

fn get_field_with_size(field_name: &str) -> Vec<LabelledInstruction>

Tasm code that returns a pointer to the field of the object, along with the size of that field in number of BFieldElements, assuming:

  • that a pointer to the said object lives on top of the stack;
  • said object has a type that implements the TasmObject trait;
  • said object lives in memory encoded as BFieldCodec specifies.
BEFORE: _ *object
AFTER:  _ *field field_size

See also: get_field if you just want the field without the size.

Source

fn destructure() -> Vec<LabelledInstruction>

Destructure a struct into the pointers to its fields.

BEFORE: _ *struct
AFTER:  _ [pointers to all fields]
§Example

The example below defines a struct Foo and encodes an instance of it into memory. It then creates a Triton VM program to read and destructure the Foo instance, extracting and outputting the bar field. Finally, it runs the program and asserts that the extracted value matches the original bar value.

#  // ^^^^^^^ derive macro `BFieldCodec` does not behave nicely; todo
# use tasm_lib::prelude::*;
# use tasm_lib::triton_vm::prelude::*;
# use tasm_lib::memory::encode_to_memory;
#[derive(BFieldCodec, TasmObject)]
struct Foo {
    bar: u32,
    baz: XFieldElement,
}

let foo = Foo { bar: 13, baz: xfe!(0) };
let foo_ptr = bfe!(42);
let mut non_determinism = NonDeterminism::default();
encode_to_memory(&mut non_determinism.ram, foo_ptr, &foo);

let program = triton_program! {
    read_io 1               // _ *foo
    {&Foo::destructure()}   // _ *baz *bar
    read_mem 1              // _ *baz bar (*bar - 1)
    pop 1                   // _ *baz bar
    write_io 1              // _ *baz
    halt
};

let output = VM::run(program, PublicInput::new(vec![foo_ptr]), non_determinism).unwrap();
let [bar] = output[..] else { panic!() };
assert_eq!(bfe!(foo.bar), bar);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl TasmStruct for Claim

Source§

impl TasmStruct for FriResponse

Source§

impl TasmStruct for MmrAccumulator

Source§

impl TasmStruct for MmrMembershipProof

Source§

impl TasmStruct for MmrSuccessorProof

Implementors§