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 WordWrapper;
use Word;
;
Generated Methods
The macro automatically generates the following methods:
Accessor Methods
new_unchecked(Word) -> Self- Construct without any checksas_elements(&self) -> &[Felt]- Returns the elements representation of the wrapped Wordas_bytes(&self) -> [u8; 32]- Returns the byte representationto_hex(&self) -> String- Returns a big-endian, hex-encoded stringas_word(&self) -> Word- Returns the underlying Word value
Example
use WordWrapper;
use Word;
;
// Create using new_unchecked (generated by the macro)
let word = from;
let note_id = from_raw;
// 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_uncheckedconstructor. You should not manually implement this method. - Previously, the macro also generated
From<T>andFrom<&T>trait implementations forWordand[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.