Skip to content

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

Merged
bobbinth merged 5 commits into
nextfrom
ajl-avoid-vault-key-double-hash
Jun 27, 2026
Merged

perf: avoid double-hashing the asset vault key in fungible add/remove#3073
bobbinth merged 5 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.

partylikeits1983 and others added 3 commits June 24, 2026 11:00
…ouble-hash

# Conflicts:
#	crates/miden-protocol/asm/kernels/transaction/lib/epilogue.masm
#	crates/miden-testing/src/kernel_tests/tx/test_fee.rs
Address review feedback: hashing stays inside the asset_vault module so
callers don't have to think about it.

- get_asset is unchanged: it takes a raw ASSET_KEY and hashes internally.
- hash_asset_key stays private.
- peek_asset takes the pre-hashed ASSET_KEY_HASH; add_fungible_asset and
  remove_fungible_asset hash the key once at the start and reuse it for
  both the peek and the set, eliminating the double hash.
- the now-redundant set_asset wrapper is replaced by a direct smt::set.

@bobbinth bobbinth left a comment

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.

Looks good! Thank you!

@bobbinth bobbinth added this pull request to the merge queue Jun 27, 2026
Merged via the queue into next with commit 38b10d3 Jun 27, 2026
20 checks passed
@bobbinth bobbinth deleted the ajl-avoid-vault-key-double-hash branch June 27, 2026 23:43
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

3 participants