Skip to content

perf: avoid double-hashing the asset vault key in fungible add/remove#3073

Open
partylikeits1983 wants to merge 2 commits into
nextfrom
ajl-avoid-vault-key-double-hash
Open

perf: avoid double-hashing the asset vault key in fungible add/remove#3073
partylikeits1983 wants to merge 2 commits into
nextfrom
ajl-avoid-vault-key-double-hash

Conversation

@partylikeits1983

Copy link
Copy Markdown
Contributor

Summary

Closes #3059.

add_fungible_asset and remove_fungible_asset hashed the raw asset vault key twice: once inside peek_asset and again inside set_asset. This PR hashes the key once at the start of each vault modifier and changes peek_asset / get_asset to accept the already-hashed ASSET_KEY_HASH, removing one poseidon2::hash per fungible add/remove, including the fee-removing path in the epilogue. The set_asset helper became a bare smt::set wrapper and is removed; hash_asset_key is now pub for external callers (account.masm get-asset wrappers and test code).

Cycle constants

Re-measured via num_tx_cycles_after_compute_fee_are_less_than_estimated: worst-case post-compute_fee cycles dropped from 863 to 843 (one poseidon2 hash, 20 cycles). VAULT_KEY_HASH_CYCLES is lowered from 50 to 30 in epilogue.masm and its mirror in test_fee.rs, keeping the same 45-cycle safety margin. The zero-fee baseline (NUM_POST_COMPUTE_FEE_CYCLES) is unaffected.

Testing

make format and make test pass locally (1193/1193).

Hash the raw asset vault key once at the start of each asset vault
modifier and have peek_asset/get_asset accept the already-hashed key,
removing one poseidon2 hash per fungible asset add/remove (including
the fee-removing path in the epilogue). The set_asset helper becomes a
bare smt::set wrapper and is removed.

Worst-case post-compute_fee cycles drop from 863 to 843; VAULT_KEY_HASH_CYCLES
is re-measured from 50 to 30 (same 45-cycle margin).

Closes #3059
@partylikeits1983 partylikeits1983 marked this pull request as ready for review June 10, 2026 04:01
Comment on lines +802 to 808
# hash the asset vault key before using it as the SMT key
exec.asset_vault::hash_asset_key
# => [ASSET_KEY_HASH, vault_root_ptr]

# get the asset
exec.asset_vault::get_asset
# => [ASSET_VALUE]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would keep key hashing internal to the vault, so that callers of the module's procedure do not have to think about this detail.

So, get_asset stays as it was before the PR (takes ASSET_KEY).

And if we change peek and set asset to this:

#! Inputs:  [ASSET_KEY_HASH, vault_root_ptr]
#! Outputs: [ASSET_VALUE]
pub proc peek_asset

#! Inputs:  [ASSET_VALUE, ASSET_KEY_HASH, VAULT_ROOT]
#! Outputs: [OLD_VALUE, NEW_VAULT_ROOT]
proc set_asset

Then we should be able to avoid double hashing in add_fungible_asset and remove_fungible_asset as well, right? We should be able to keep hash_asset_key private.

That way, all asset key hashing is done in asset_vault and callers always provide ASSET_KEY uniformly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid double-hashing the asset vault key in add/remove fungible asset masm

2 participants