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

chore: fixing @safety warnings #11094

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

benesjan
Copy link
Contributor

@benesjan benesjan commented Jan 7, 2025

Fixes #11087

Note for reviewer

I originally addressed a bunch of other stuff in this PR but as @nventuro pointed out it became too messy so I separated those changes into a PR up the stack.

Merging currently blocked by this nargo fmt bug

Copy link
Contributor Author

benesjan commented Jan 7, 2025

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

Comment on lines 492 to 507
//@safety TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
// WARNING: This is insecure and should be temporary!
// The oracle repacks the arguments and returns a new args_hash.
// new_args = [selector, ...old_args], so as to make it suitable to call the public dispatch function.
// We don't validate or compute it in the circuit because a) it's harder to do with slices, and
// b) this is only temporary.
enqueue_public_function_call_internal(
contract_address,
function_selector,
args_hash,
counter,
is_static_call,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

@fcarreiro this is an old issue from october that doesn't seem to have much activity or tracking - is there any blocker to getting this done?

Copy link
Contributor

@fcarreiro fcarreiro Jan 8, 2025

Choose a reason for hiding this comment

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

Maybe it's a good idea to add tracking, you are right. The pointed ticket is tracking something else which is considered done.

Regarding a solution: TBH IDK how to solve it, and currently the AVM team is quite busy with other stuff. IDK if we'd be able to come back to this until just after Ignition (especially since it's not really an AVM thing).

Possible solutions: (1) The easy way to make this secure, IIRC, is to constrain the hashes there in place. Something like assert(old_hash == hash([a,b,c])) and assert(new_hash == hash([selector,a,b,c]). This would blow up the constraints and when I was doing this everyone was chipping away constraints so I thought it wouldn't fly.
(2) Something that I tried was to change the the private interfaces (macros) and call methods to take a slice. Then I could do the same as I did in public. This didn't work because sizes need to be known at comptime sadly.
(3) The other possible solution (more elegant from some PoV and less from other) is to change all the users of the "call with hash" method to include the selector themselves. IIRC from a discussion with Palla, the major offenders and only real need for passing the hash are the contract (?) entry points which then kind of call other functions with an already given set of params. Selectors are now an aztec-nr/js only concept but IIRC then it would be ok to change the JS code to insert the selector appropiately.

This is all from the top of my mind so there might be mistakes. Please add that info if you create a ticket, thanks!

is_static_call,
);
let args_hash = unsafe {
//@safety TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some whitespace and do // @safety? It looks very odd otherwise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently this makes the compiler cry. But it will be trivial to change later as it's just a string replacement.
image

Copy link
Contributor

Choose a reason for hiding this comment

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

noir-projects/aztec-nr/aztec/src/initializer.nr Outdated Show resolved Hide resolved
@@ -95,7 +95,7 @@ fn __derive_generators<let N: u32, let M: u32>(
// Same as from_field but:
// does not assert the limbs are 128 bits
// does not assert the decomposition does not overflow the EmbeddedCurveScalar
fn from_field_unsafe(scalar: Field) -> EmbeddedCurveScalar {
pub fn from_field_unsafe(scalar: Field) -> EmbeddedCurveScalar {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the Noir team would necessarily be happy about exposing this in their stdlib - did you check with them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, decided to go ahead and then tag Tom for a review as the change is easy to revert.

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to keep this as an internal implementation detail to the stdlib. If you need something similar then you can vendor it into aztec-nr.

Copy link
Contributor Author

@benesjan benesjan Jan 8, 2025

Choose a reason for hiding this comment

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

That would require me to copy not only from_field_unsafe but also compute_decomposition which seems like quite a lot of code to copy and I feel like it does not really belong to Aztec.nr (it feels too low-level).

But maybe we could make the API std nicer by moving the from_field_unsafe func to EmbeddedCurveScalar just like was done for from_field?

Copy link
Member

Choose a reason for hiding this comment

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

compute_decomposition is a very small function though?

Regardless, the noir stdlib is a very conservative library as breaking changes are a breaking change to the language itself. We're not going to be exposing this function oublically for that reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TomAFrench As you please. I will revert this change as the scope of the PR is already quite big and don't want to make it bigger.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regardless, the noir stdlib is a very conservative library as breaking changes are a breaking change to the language itself.

I feel like in this case this might not be that big of a deal because we are exposing the function and if we did the change I proposed we would be moving a function which was not intended for external use. So the breaking change would be only for users that used the func anyway just like we did in aztec-packages.

I would also argue that us needing this function is a good sign that it's desirable to expose.

But the best approach might be to just change the API such that we don't need the function. Which could be done by having an MSM func that takes in the values from the smaller field and then does the conversion on its own. This would also improve safety since we rely on MSM to constrain the limbs.

@TomAFrench WDYT?

noir/noir-repo/noir_stdlib/src/uint128.nr Outdated Show resolved Hide resolved
noir-projects/aztec-nr/aztec/src/utils/array/collapse.nr Outdated Show resolved Hide resolved
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from d579beb to e29c345 Compare January 7, 2025 22:07
@benesjan benesjan changed the title chore: fixing aztec-nr warnings chore: fixing aztec-nr and protocol circuits warnings Jan 7, 2025
@benesjan benesjan marked this pull request as ready for review January 7, 2025 22:08
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from e29c345 to 26d8be5 Compare January 8, 2025 02:36
@benesjan benesjan requested review from nventuro and TomAFrench and removed request for fcarreiro and dbanks12 January 8, 2025 03:00
@TomAFrench
Copy link
Member

Merging master as I want to check if failures in another branch are from master.

@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from 50c83c0 to d9308ca Compare January 8, 2025 18:14
@benesjan benesjan marked this pull request as draft January 8, 2025 18:39
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from 9461346 to 48fb467 Compare January 8, 2025 21:22
@benesjan benesjan removed the request for review from TomAFrench January 8, 2025 23:02
@benesjan benesjan added the e2e-all CI: Enables this CI job. label Jan 8, 2025
@benesjan benesjan marked this pull request as ready for review January 8, 2025 23:19
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from ca3ec19 to 35f4b12 Compare January 9, 2025 14:38
@benesjan benesjan changed the title chore: fixing aztec-nr and protocol circuits warnings chore: fixing Aztec.nr and protocol circuits @safety warnings Jan 9, 2025
@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 changed the title chore: fixing Aztec.nr and protocol circuits @safety warnings chore: fixing @safety warnings Jan 9, 2025
@TomAFrench
Copy link
Member

Note that the format of safety comments is in the process of changing.

@benesjan benesjan requested a review from nventuro January 9, 2025 16:25
@benesjan
Copy link
Contributor Author

benesjan commented Jan 10, 2025

@TomAFrench @nventuro could I get an approval here or are we waiting for new Noir sync?

I've already addressed all of Nico's comments here and it now contains only safety warnings so I think this should be fine to merge.

I am having too many PRs open...

@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from c9df6c8 to 7e6e87e Compare January 10, 2025 21:43
@TomAFrench
Copy link
Member

As soon as ci is unfucked we're going to be merging a change to the safety comment format so i think we should either hold off until that's in or just preemptively switch to the new format .

@TomAFrench
Copy link
Member

The new comment format is now in Aztec packages

@benesjan benesjan marked this pull request as draft January 13, 2025 17:49
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch 2 times, most recently from e1f8440 to fc91f1f Compare January 13, 2025 18:12
@benesjan benesjan marked this pull request as ready for review January 13, 2025 18:29
@benesjan benesjan force-pushed the 01-07-chore_fixing_aztec-nr_warnings branch from 3e094bc to 80d9ab4 Compare January 13, 2025 19:14
@benesjan benesjan added the S-blocked Status: Blocked label Jan 13, 2025
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. S-blocked Status: Blocked
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aztec-nr is breaking unsafe block rules
5 participants