-
Notifications
You must be signed in to change notification settings - Fork 599
feat: implement manual Packable for structs with sub-Field members #21576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benesjan
merged 1 commit into
merge-train/fairies
from
nico/f-260-manual-packable-impls
Mar 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 43 additions & 1 deletion
44
...-projects/noir-contracts/contracts/app/app_subscription_contract/src/subscription_note.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,50 @@ | ||
| use aztec::{macros::notes::note, protocol::traits::Packable}; | ||
|
|
||
| #[derive(Eq, Packable)] | ||
| #[derive(Eq)] | ||
| #[note] | ||
| pub struct SubscriptionNote { | ||
| pub expiry_block_number: u32, | ||
| pub remaining_txs: u32, | ||
| } | ||
|
|
||
| impl Packable for SubscriptionNote { | ||
| let N: u32 = 1; | ||
|
|
||
| fn pack(self) -> [Field; Self::N] { | ||
| [(self.expiry_block_number as Field) * 2.pow_32(32) + (self.remaining_txs as Field)] | ||
| } | ||
|
|
||
| fn unpack(packed: [Field; Self::N]) -> Self { | ||
| let remaining_txs = packed[0] as u32; | ||
| let expiry_block_number = ((packed[0] - remaining_txs as Field) / 2.pow_32(32)) as u32; | ||
| Self { expiry_block_number, remaining_txs } | ||
| } | ||
| } | ||
|
|
||
| mod test { | ||
| use super::{Packable, SubscriptionNote}; | ||
|
|
||
| #[test] | ||
| fn test_pack_unpack_subscription_note() { | ||
| let note = SubscriptionNote { expiry_block_number: 1000, remaining_txs: 5 }; | ||
| let unpacked = SubscriptionNote::unpack(note.pack()); | ||
| assert_eq(unpacked.expiry_block_number, note.expiry_block_number); | ||
| assert_eq(unpacked.remaining_txs, note.remaining_txs); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pack_unpack_subscription_note_zeros() { | ||
| let note = SubscriptionNote { expiry_block_number: 0, remaining_txs: 0 }; | ||
| let unpacked = SubscriptionNote::unpack(note.pack()); | ||
| assert_eq(unpacked.expiry_block_number, 0); | ||
| assert_eq(unpacked.remaining_txs, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pack_unpack_subscription_note_max() { | ||
| let note = SubscriptionNote { expiry_block_number: 0xffffffff, remaining_txs: 0xffffffff }; | ||
| let unpacked = SubscriptionNote::unpack(note.pack()); | ||
| assert_eq(unpacked.expiry_block_number, note.expiry_block_number); | ||
| assert_eq(unpacked.remaining_txs, note.remaining_txs); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a comment here would be nice. these kinds of unpacking functions normally rely on shifts and bitmasks, I guess we don't have yet those in Noir?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you use the same approach in the other contracts, maybe worth adding a reusable function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have them in Noir but unlike in CPU instruction sets, bit shifts are super inefficient in circuits while multiplication and division are efficient (circuits have MUL and AND gates and you can perform division by multiplying by modulo inverse).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We intentionally use multiply/divide by powers of 2 rather than bit shifts — they map more efficiently to both AVM opcodes (for public functions) and proving backend primitives (for private functions). The
Packabletrait docs already recommend this approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered this but a reusable helper doesn't really help here — the
as TargetTypecasts (e.g.as u32,as u64,as u128) are the core of each unpacking since they provide the circuit range constraints, and each site uses different type combinations (u32+u32, u32+u64, u128+u64, u128+bool, bools+u32s). A helper returning raw Fields would require callers to still do all the casting, resulting in more lines not fewer. ThePackabletrait docs already serve as the guide with a worked example.