miden-protocol-macros

Crates.iomiden-protocol-macros
lib.rsmiden-protocol-macros
version0.13.2
created_at2026-01-17 07:41:42.041567+00
updated_at2026-01-22 08:42:22.350345+00
descriptionProcedural macros for Miden protocol
homepagehttps://miden.xyz
repositoryhttps://github.com/0xMiden/miden-base
max_upload_size
id2050099
size12,809
Bobbin Threadbare (bobbinth)

documentation

README

miden-protocol-macros

A collection of procedural macros for the Miden protocol.

WordWrapper

The WordWrapper derive macro automatically implements helpful accessor methods and conversions for tuple structs that wrap a Word type.

Usage

Add the derive macro to any tuple struct with a single Word field:

use miden_protocol_macros::WordWrapper;
use miden_crypto::word::Word;

#[derive(WordWrapper)]
pub struct NoteId(Word);

Generated Methods

The macro automatically generates the following methods:

Accessor Methods

  • new_unchecked(Word) -> Self - Construct without any checks
  • as_elements(&self) -> &[Felt] - Returns the elements representation of the wrapped Word
  • as_bytes(&self) -> [u8; 32] - Returns the byte representation
  • to_hex(&self) -> String - Returns a big-endian, hex-encoded string
  • as_word(&self) -> Word - Returns the underlying Word value

Example

use miden_protocol_macros::WordWrapper;
use miden_crypto::word::Word;

#[derive(Debug, Clone, Copy, PartialEq, Eq, WordWrapper)]
pub struct NoteId(Word);

// Create using new_unchecked (generated by the macro)
let word = Word::from([Felt::ONE, Felt::ZERO, Felt::ONE, Felt::ZERO]);
let note_id = NoteId::from_raw(word);

// Use accessor methods
let elements = note_id.as_elements();
let bytes = note_id.as_bytes();
let hex = note_id.to_hex();
let word_back = note_id.as_word();

Requirements

The macro can only be applied to:

  • Tuple structs (e.g., struct Foo(Word))
  • With exactly one field
  • Where that field is of type Word

Benefits

Using this macro eliminates boilerplate code. Instead of manually writing ~50 lines of implementation code for each Word wrapper type, you can simply add #[derive(WordWrapper)] to your struct definition.

This is particularly useful in the Miden codebase where many types like NoteId, TransactionId, Nullifier, BatchId, etc. all follow the same pattern of wrapping a Word and providing similar accessor methods.

Important Notes

  • The macro generates the new_unchecked constructor. You should not manually implement this method.
  • Previously, the macro also generated From<T> and From<&T> trait implementations for Word and [u8; 32]. These have been removed to give types more control over their conversions. If you need these conversions, implement them manually for your specific type.
Commit count: 1784

cargo fmt