Make structs use the user defined order for mapping to witness indices #1110
Labels
aztec.nr
Helpful for development of Aztec.nr the smart contract framework
enhancement
New feature or request
Problem
Currently in Noir, when a user defines the following struct:
Under the hood, we are sorting them by alphabetical order and mapping witness indices to the sorted order.
For example:
If we were to define the struct by switching its fields:
Then the mapping would still be:
This is a problem for the usecase where the Noir program is being fed into a VM circuit and it expects certain values at certain public indices. The workaround that we have seen for this is users prepending the field_name to force a particular ordering.
It is clear that we need to have a user defined ordering, but then the question is whether we should allow users to pick between a user defined ordering and the alphabetical ordering via some attribute or whether we should go for one ordering.
Proposed solution
Preamble
In Rust, we have
#[repr(C)]
which is similar to a "user defined ordering".The default is for Rust to decide which ordering internally and it does this so that the compiler can re-arrange and pack the field in the most efficient way.
Proposal
I think we require some more research on this, as its clear that for contract syntax, it would be good to have user defined mapping as the default, but for non-contract semantics, I do not know whether this is also the case.
For me, it seemed like making alphabetical ordering the default and needing users to do
Seemed like a viable solution, however @TomAFrench and @zac-williamson have made good points to user-defined being the default and also to the fact that if we re-arrange the arguments in the main function, then this also changes the mapping.
We could then alternatively do:
The above signifying that user-defined mapping is the default and one needs to specifically say that they want their struct to be alphabetically ordered.
Alternatives considered
No response
Additional context
Before landing a PR:
Note, we can only specify that our own struct is user-defined/alphabtically sorted. If i depend on a library that is doing user defined mapping and I need alphabetically sorted mapping, this is okay. BUT note that if that library changes their fields, they need to be aware that it is a breaking change.
I'd like to get some feedback from @Cheethas and Lasse
No response
Submission Checklist
The text was updated successfully, but these errors were encountered: