-
Notifications
You must be signed in to change notification settings - Fork 115
feat: implement u256 agglayer asset scaling up procedure #2141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
53c326f
feat: implement u256 agglayer asset scaling procedure
partylikeits1983 44ef81c
refactor: change doc comment example
partylikeits1983 c917836
fix: cargo.toml fmt
partylikeits1983 f325b69
Update crates/miden-lib/asm/agglayer/account_components/asset_convers…
partylikeits1983 7f07013
Update crates/miden-testing/tests/agglayer/asset_conversion.rs
partylikeits1983 5f34f6b
Update crates/miden-lib/src/agglayer/utils.rs
partylikeits1983 75a8870
Update crates/miden-lib/asm/agglayer/account_components/asset_convers…
partylikeits1983 363af83
Update crates/miden-lib/asm/agglayer/account_components/asset_convers…
partylikeits1983 2607a68
refactor: fix felts_to_u256_bytes & refactor masm comments
partylikeits1983 b1d9ac3
refactor: clean up masm doc comment
partylikeits1983 ffa0b7e
Merge branch 'agglayer' into ajl-asset-conversion-agglayer
partylikeits1983 cb5af54
Update crates/miden-lib/asm/agglayer/account_components/asset_convers…
partylikeits1983 17870c2
refactor: add more asset conversion tests
partylikeits1983 02ca823
refactor: use u32assert, add masm comments, & improve tests
partylikeits1983 f7eb5db
refactor: add basic examples test
partylikeits1983 642f7da
Update crates/miden-testing/tests/agglayer/asset_conversion.rs
partylikeits1983 d41e591
Update crates/miden-lib/asm/agglayer/account_components/asset_convers…
partylikeits1983 3167d82
refactor: address test name & fix tests to call new proc name
partylikeits1983 c4fef2a
chore: merge agglayer into ajl-asset-conversion-agglayer
partylikeits1983 e7bb55f
fix: taplo fmt
partylikeits1983 38d4032
chore: merge agglayer into ajl-asset-conversion-agglayer
partylikeits1983 79129a5
refactor: rename procedure & add comments
partylikeits1983 a2ccbed
refactor: use u64::overflowing_mul
partylikeits1983 4acf1a8
refactor: use asset_conversion_library fn
partylikeits1983 ce42f7f
Merge branch 'agglayer' into ajl-asset-conversion-agglayer
partylikeits1983 7c0fd06
refactor: update comment in scale_native_amount_to_u256
partylikeits1983 301124c
refactor: update to return little-endian u256
partylikeits1983 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
crates/miden-lib/asm/agglayer/account_components/asset_conversion.masm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use.std::math::u64 | ||
| use.std::word | ||
|
|
||
| # CONSTANTS | ||
| # ================================================================================================= | ||
|
|
||
| const.MAX_SCALING_FACTOR=18 | ||
|
|
||
| # ERRORS | ||
| # ================================================================================================= | ||
| const.ERR_SCALE_AMOUNT_EXCEEDED_LIMIT="maximum scaling factor is 18" | ||
|
|
||
| #! Calculate 10^scale where scale is a u8 exponent. | ||
| #! | ||
| #! Inputs: [scale] | ||
| #! Outputs: [10^scale] | ||
| #! | ||
| #! Where: | ||
| #! - scale is expected to be a small integer (0-18 typical for crypto decimals) | ||
| #! | ||
| #! Panics if: | ||
| #! - scale > 18 (overflow protection) | ||
| proc pow10 | ||
| u32assert.err=ERR_SCALE_AMOUNT_EXCEEDED_LIMIT | ||
| # => [scale] | ||
|
|
||
| dup u32lte.MAX_SCALING_FACTOR assert.err=ERR_SCALE_AMOUNT_EXCEEDED_LIMIT | ||
| # => [scale] | ||
|
|
||
| push.1 swap | ||
| # => [scale, result] | ||
|
|
||
| dup neq.0 | ||
| # => [is_not_zero, scale, result] | ||
|
|
||
| # Loop to calculate 10^scale | ||
| while.true | ||
| # => [scale, result] | ||
|
|
||
| # result *= 10 | ||
| swap mul.10 swap | ||
| # => [scale, result*10] | ||
|
|
||
| # scale -= 1 | ||
| sub.1 | ||
| # => [scale-1, result*10] | ||
|
|
||
| dup neq.0 | ||
| # => [is_not_zero, scale-1, result*10] | ||
| end | ||
| # => [0, result] | ||
|
|
||
| drop | ||
| # => [result] | ||
| end | ||
partylikeits1983 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #! Convert an asset amount to a scaled U256 representation for bridging to Agglayer. | ||
| #! | ||
| #! This procedure is used to convert Miden asset amounts to EVM asset amounts. | ||
| #! It multiplies the input amount by 10^target_scale to adjust for decimal differences | ||
| #! between the current representation and the target chain's native decimals. | ||
| #! | ||
| #! The procedure first calculates 10^target_scale using the pow10 helper, then converts | ||
| #! both the amount and scale factor to U64 format, performs U64 multiplication, and | ||
| #! returns the result as 8 u32 limbs in little-endian order (U256 format). | ||
| #! | ||
| #! Inputs: [amount, target_scale] | ||
bobbinth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #! Outputs: [[RESULT_U256[0], RESULT_U256[1]]] | ||
| #! | ||
| #! Where: | ||
| #! - amount: The asset amount to be converted (range: 0 to 2^63 - 2^31) | ||
| #! - target_scale: Exponent for scaling factor (10^target_scale) | ||
| #! - [RESULT_U256[0], RESULT_U256[1]]: U256 value as 8 u32 limbs in little-endian order | ||
| #! (least significant limb at the top of the stack, each limb stored in little-endian format) | ||
| #! | ||
| #! Examples: | ||
| #! - USDC: amount=1000000000, target_scale=0 → 1000000000 (no scaling) | ||
| #! - ETH: amount=1e10, target_scale=8 → 1e18 | ||
| #! | ||
| #! Invocation: exec | ||
| pub proc scale_native_amount_to_u256 | ||
| swap | ||
| # => [target_scale, amount] | ||
|
|
||
| exec.pow10 | ||
| # => [scale, amount] | ||
|
|
||
| u32split | ||
| # => [scale_hi, scale_lo, amount] | ||
|
|
||
| movup.2 u32split | ||
| # => [amount_hi, amount_lo, scale_hi, scale_lo] | ||
|
|
||
| # Perform U64 multiplication: amount * scale | ||
| # This is safe because both the scaling factor and amount are guaranteed to be smaller | ||
| # than 2^64, so we will never overflow a 256-bit value. | ||
| exec.u64::overflowing_mul | ||
| # => [res_hi, res_mid_hi, res_mid_lo, res_lo] | ||
|
|
||
| exec.word::reverse | ||
| # => [res_lo, res_mid_lo, res_mid_hi, res_hi] | ||
|
|
||
| # convert to U256 & little endian | ||
| padw swapw | ||
| # => [RESULT_U256[0], RESULT_U256[1]] | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing
U256type.