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

refactor: random fixes and readability improvements #11129

Draft
wants to merge 5 commits into
base: 01-07-chore_fixing_aztec-nr_warnings
Choose a base branch
from

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Jan 9, 2025

In a PR down the stack I fixed safety warnings. When doing that I stumbled upon a random hodgepodge of issues. I am fixing them in this PR to not clutter the original one.

Copy link
Contributor Author

benesjan commented Jan 9, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from 35f4b12 to b7b7b66 Compare January 9, 2025 15:27
@benesjan benesjan force-pushed the 01-09-refactor_random_fixes_and_readability_improvements branch from 8a4cad3 to 8f11ff7 Compare January 9, 2025 15:29
@@ -269,6 +269,7 @@ impl PrivateContext {
// for the correct public key has been received.
get_key_validation_request(pk_m_hash, key_index)
};
assert_eq(request.pk_m.hash(), pk_m_hash, "Obtained invalid key validation request");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find assert_eq nicer to read than assert(left == right) so I sneaked this change in here and in other places.

I think we didn't use it before as it was not yet implemented.

Copy link
Contributor

Choose a reason for hiding this comment

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

This assert is not present in the origianl code though?

+ (U128::from_integer(self.post.unwrap_unchecked()) << 32)
+ (U128::from_integer(self.block_of_change) << 64)
+ (U128::from_integer(self.post.is_some()) << 96)
+ (U128::from_integer(self.pre.is_some()) << 97);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Warnings fix. As discussed in a PR down the stack now I don't work with u64 limbs but I just pack directly. I needed to use U128 here to avoid overflows.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use Field directly? If Field doesn't support shifts then lets just multiply by the corresponding power of two. U128 will make this unnecessarily expensive.

@@ -13,7 +13,7 @@ use dep::aztec::{
},
};

global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header.
pub global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Warnings fix --> value was used in a test contract.

Copy link
Contributor

Choose a reason for hiding this comment

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

Which test contract? Accessing this is an antipattern.

assert(value <= next_power_2);

prev_power_2
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As proposed by Nico in a PR down the stack. Made sense to separate this into a separate func as it makes it easier to figure out how the height should be constrained.

@benesjan benesjan added the e2e-all CI: Enables this CI job. label Jan 9, 2025
@benesjan benesjan marked this pull request as ready for review January 9, 2025 17:30
Copy link
Contributor

github-actions bot commented Jan 9, 2025

Changes to public function bytecode sizes

Generated at commit: 1e23f8256f525e3ecf867396c8ec918b9b381b8b, compared to commit: b7b7b665426d16ba0b3eaafe57b0089d3b6ba1e7

🧾 Summary (100% most significant diffs)

Program Bytecode size in bytes (+/-) %
Auth::get_authorized_delay +2,254 ❌ +112.20%
Auth::set_authorized +3,253 ❌ +65.51%
Auth::set_authorized_delay +3,235 ❌ +63.91%
TokenBlacklist::update_roles +3,253 ❌ +49.50%
TokenBlacklist::constructor +3,253 ❌ +47.57%
Auth::public_dispatch +3,235 ❌ +34.43%
TokenBlacklist::public_dispatch +3,040 ❌ +12.65%

Full diff report 👇
Program Bytecode size in bytes (+/-) %
Auth::get_authorized_delay 4,263 (+2,254) +112.20%
Auth::set_authorized 8,219 (+3,253) +65.51%
Auth::set_authorized_delay 8,297 (+3,235) +63.91%
TokenBlacklist::update_roles 9,825 (+3,253) +49.50%
TokenBlacklist::constructor 10,092 (+3,253) +47.57%
Auth::public_dispatch 12,632 (+3,235) +34.43%
TokenBlacklist::public_dispatch 27,079 (+3,040) +12.65%

@benesjan benesjan requested a review from nventuro January 9, 2025 18:17
@@ -269,6 +269,7 @@ impl PrivateContext {
// for the correct public key has been received.
get_key_validation_request(pk_m_hash, key_index)
};
assert_eq(request.pk_m.hash(), pk_m_hash, "Obtained invalid key validation request");
Copy link
Contributor

Choose a reason for hiding this comment

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

This assert is not present in the origianl code though?

+ (U128::from_integer(self.post.unwrap_unchecked()) << 32)
+ (U128::from_integer(self.block_of_change) << 64)
+ (U128::from_integer(self.post.is_some()) << 96)
+ (U128::from_integer(self.pre.is_some()) << 97);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we use Field directly? If Field doesn't support shifts then lets just multiply by the corresponding power of two. U128 will make this unnecessarily expensive.

@@ -13,7 +13,7 @@ use dep::aztec::{
},
};

global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header.
pub global VALUE_NOTE_LEN: u32 = 3; // 3 plus a header.
Copy link
Contributor

Choose a reason for hiding this comment

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

Which test contract? Accessing this is an antipattern.

Comment on lines +33 to +43
// Find size of tree required
let height = unsafe {
//@safety This is safe because we derive next and previous powers of 2 below based on the height
// and the powers are then checked against the input value.
get_height(value, 0)
};

let next_power_2 = 2 << height;
let prev_power_2 = next_power_2 / 2;
assert((value == 0) | (value == 1) | (value > prev_power_2));
assert(value <= next_power_2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Find size of tree required
let height = unsafe {
//@safety This is safe because we derive next and previous powers of 2 below based on the height
// and the powers are then checked against the input value.
get_height(value, 0)
};
let next_power_2 = 2 << height;
let prev_power_2 = next_power_2 / 2;
assert((value == 0) | (value == 1) | (value > prev_power_2));
assert(value <= next_power_2);
let next_power_exponent = unsafe {
//@safety This is a hint that we'll use to compute the next and previous powers of two, which we check to be
// larger and smaller than respectively. The get_height function happens to return exactly what we need.
get_height(value, 0)
};
let next_power_2 = 2 << next_power_exponent;
let prev_power_2 = next_power_2 / 2;
assert((value == 0) | (value == 1) | (value > prev_power_2));
assert(value <= next_power_2);

This is more readable I think. Also this is odd - it seems like prev_power_2 is almost complete unconstrained if value is 0 or 1, it can be any value except 0.

@benesjan benesjan marked this pull request as draft January 10, 2025 01:37
…ree/variable_merkle_tree.nr

Co-authored-by: Nicolás Venturo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e-all CI: Enables this CI job.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants