feat: u256 to felt scaling procedure#2331
Conversation
|
#2336 Shows how the |
bobbinth
left a comment
There was a problem hiding this comment.
Thank you! Looks good. I left some more comments inline.
bobbinth
left a comment
There was a problem hiding this comment.
Looks good! Thank you! I left some more comments inline - but I think we are pretty close.
| /// # Errors | ||
| /// - [`EthAmountError::ScaleTooLarge`] if scale_exp > 18 | ||
| /// - [`EthAmountError::ScaledValueNotCanonicalFelt`] if the result doesn't fit in a canonical | ||
| /// Felt | ||
| /// - [`EthAmountError::ScaledValueDoesNotFitU64`] if the result doesn't fit in a u64 | ||
| /// - [`EthAmountError::ScaledValueExceedsMaxFungibleAmount`] if the scaled value exceeds the | ||
| /// maximum fungible token amount |
There was a problem hiding this comment.
Related to the above, we can probably delete line 249 as if we get a valid token amount, we know for sure it fits into Felt.
There was a problem hiding this comment.
removed line 249
db9825d to
3ea2737
Compare
|
I rebased to Addressing comments from @mmagician from here: #2435 |
There was a problem hiding this comment.
FYI: in #2401, the underlying repr of EthAmount changes to just contain bytes, and many of the helper methods are removed (some can be added back if needed, but wanted to keep the interface minimal when they weren't needed).
There was a problem hiding this comment.
Especially after #2413, we know that the way CLAIM note is constructed will be correct in terms of endianness.
I believe the main changes there w.r.t. amounts are:
let amount = EthAmount::new(hex_to_bytes(&self.amount).expect("valid amount hex")); // this should be refactored to take a decimal string, not hex string
let amount_felts: Vec<Felt> = amount.to_elements();where to_elements() returns the field elements in BE-limb order, but LE-byte order 😱
It appears that verify_u256_to_native_amount_conversion operates on the reverse on both fronts (LE-limb order but BE-byte order?) but please correct me if I'm wrong.
So what I would suggest is coupling the changes in this PR to changes from the linked PR above.
Then we could have a bridgin-in test where:
- we know the leaf data hashes correctly to the leaf value (related to this since the amount is part of the leaf data)
- proof data verifies against GER (not directly related, but the computation of leaf value in 1. matters for the verification)
- There is an output note created with the correct amount (the real safety check)
|
I think there are two changes needed on my side (a follow-up to #2413):
After which I think the best course of actions is either:
I have a strong preference for 2: conflicts need to be dealt with anyways eventually, and we'll be sure of compatibility right away. |
|
Merged latest changes from |
mmagician
left a comment
There was a problem hiding this comment.
two comments about the changes left, once these are addressed let's merge.
Also a note for the future regarding cherry-picking, but I think we'll be able to work around this later
|
|
||
| # reverse limbs and byte endianness | ||
| exec.reverse_limbs_and_change_byte_endianness | ||
| # => [x0, x1, x2, x3, x4, x5, x6, x7, scale_exp, y] | ||
|
|
There was a problem hiding this comment.
Currently the changes in 30f2416 include both the procedure itself, which was cherry-picked, but also other changes that incorporate that procedure into the rest of the code.
I'm not sure if the rebase will work smoothly afterwards because of this. Ideally, these would have been two separate commits
Co-authored-by: Marti <marti@miden.team>
Co-authored-by: Marti <marti@miden.team>
This PR adds functionality which checks that a given
Feltvalue is the correctly scaled amount from au256value. This is to ensure that au256asset amount is correctly scaled to aFeltvalue on Miden.Follow up to this PR is to add the scaled
Feltamount asNoteStorageto theCLAIMnote.Resolves point 2.2 here: #2237
Builds on top of: #2270
Context: #2011 (comment)