Skip to content
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

feat: #[derive(Packable)] #11531

Merged
merged 9 commits into from
Jan 29, 2025
Merged

feat: #[derive(Packable)] #11531

merged 9 commits into from
Jan 29, 2025

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Jan 27, 2025

In this PR I implement auto-derivation of Packable trait.

It uses the same underlying functionality as derivation of Serialize but it calls the generate_serialize_to_fields and generate_deserialize_from_fields with packing enabled. This means that if some of the struct members has the Packable trait implemented it gets used instead of the intrinsic Noir serialization.

address::AztecAddress,
constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator,
traits::{Packable, Serialize},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the files with notes now generally need to import Packable as because of the change in noir-projects/noir-protocol-circuits/crates/types/src/meta/mod.nr.

See comment in that file for details.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the serialize pr, it'd be good for derive to not require extra imports from the caller. We can either autoimport, or just use fully qualified paths in the derived impl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1,38 +1,9 @@
use dep::aztec::{macros::aztec, protocol_types::traits::{Deserialize, Packable, Serialize}};

// I tried using #[derive(Serialize, Deserialize)] macro here but for whatever reason it fails to compile.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the note to a separate file because then the derivation works.

I find it quite weird to have the note defined in the same file as the contract.

Do we find this bug worthy of trying to fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -104,8 +104,8 @@ pub comptime fn generate_deserialize_from_fields<N>(
}
let packed_fields = packed_fields_quotes.join(quote {,});

// No we call unpack on the type
result = quote { aztec::protocol_types::traits::Packable::unpack([ $packed_fields ]) };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason why the Packable needs to be imported now.

Having the full path here was a mistake since the dep could be called differently than aztec and this might also be used inside the protocol circuits where there is no aztec dep.

We probably could figure the correct import at comptime but would do that in a separate PR if we find this desirable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see. Can't we have the full path to proto_types::traits so that it'd work both here, in aztec, and in the user's code?

Copy link
Contributor Author

@benesjan benesjan Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would then need to figure out whether you are in the types crate or not as in the types crate you would not use dep::aztec::protocol_types but just crate.

Jake promised me he will send a PR such that I can quote a Type directly and get a correct path. This would then make this straighforward. Until that is done I've created this issue to track it.

quote {_}
} else {
token
};
Copy link
Contributor Author

@benesjan benesjan Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug and it got triggered by me using Serialize derivation in noir-projects/noir-contracts/contracts/card_game_contract/src/game.nr

@benesjan benesjan marked this pull request as ready for review January 27, 2025 20:42
@benesjan benesjan changed the title feat: #[derive(Packable)] feat: #[derive(Packable)] Jan 27, 2025
Copy link
Contributor

github-actions bot commented Jan 27, 2025

Changes to public function bytecode sizes

Generated at commit: 5deef6e40843f91aa45792ec78426ab6c5804ddb, compared to commit: 0cfd0fe72fb52dcb00eec26294b7d434a673992a

🧾 Summary (100% most significant diffs)

Program Bytecode size in bytes (+/-) %
CardGame::on_card_played +1,277 ❌ +24.82%
CardGame::on_game_joined +976 ❌ +21.19%
CardGame::start_game +976 ❌ +16.23%
CardGame::on_cards_claimed +970 ❌ +14.45%
Lending::get_asset +228 ❌ +11.41%
CardGame::public_dispatch +1,301 ❌ +9.52%
Lending::init +101 ❌ +3.84%
Lending::get_position +57 ❌ +1.34%
Lending::update_accumulator +63 ❌ +1.15%
Lending::public_dispatch +8 ❌ +0.03%

Full diff report 👇
Program Bytecode size in bytes (+/-) %
CardGame::on_card_played 6,422 (+1,277) +24.82%
CardGame::on_game_joined 5,583 (+976) +21.19%
CardGame::start_game 6,990 (+976) +16.23%
CardGame::on_cards_claimed 7,681 (+970) +14.45%
Lending::get_asset 2,227 (+228) +11.41%
CardGame::public_dispatch 14,967 (+1,301) +9.52%
Lending::init 2,728 (+101) +3.84%
Lending::get_position 4,310 (+57) +1.34%
Lending::update_accumulator 5,538 (+63) +1.15%
Lending::public_dispatch 25,988 (+8) +0.03%

@benesjan benesjan requested review from nventuro and sklppy88 January 27, 2025 21:58
@benesjan benesjan mentioned this pull request Jan 27, 2025
@benesjan benesjan force-pushed the 01-16-refactor_using_uintnote_in_crowdfunding_and_claim branch from 31ce3f7 to 787e700 Compare January 28, 2025 17:20
@benesjan benesjan force-pushed the 01-27-feat_derive_packable_ branch from 9f5f05a to 12261b5 Compare January 28, 2025 17:20
@benesjan benesjan force-pushed the 01-16-refactor_using_uintnote_in_crowdfunding_and_claim branch from 787e700 to f7e3d93 Compare January 28, 2025 18:29
@benesjan benesjan force-pushed the 01-27-feat_derive_packable_ branch from 12261b5 to 7b76054 Compare January 28, 2025 18:29
Copy link
Contributor

@sklppy88 sklppy88 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, have some tiny qns but will approve to unblock ! 🙏

@benesjan benesjan marked this pull request as draft January 29, 2025 13:48
@benesjan benesjan force-pushed the 01-16-refactor_using_uintnote_in_crowdfunding_and_claim branch from f7e3d93 to 612e2e8 Compare January 29, 2025 13:50
@benesjan benesjan force-pushed the 01-27-feat_derive_packable_ branch from 7b76054 to 0883f93 Compare January 29, 2025 13:50
Base automatically changed from 01-16-refactor_using_uintnote_in_crowdfunding_and_claim to master January 29, 2025 14:44
@benesjan benesjan force-pushed the 01-27-feat_derive_packable_ branch from 44c5bb7 to 3800e92 Compare January 29, 2025 14:45
@benesjan benesjan marked this pull request as ready for review January 29, 2025 14:45
@benesjan benesjan enabled auto-merge (squash) January 29, 2025 14:46
@benesjan benesjan merged commit 83fe192 into master Jan 29, 2025
70 of 71 checks passed
@benesjan benesjan deleted the 01-27-feat_derive_packable_ branch January 29, 2025 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants