Skip to content

perf(stealth-registry): shrink storage-key encoding by two bytes (Closes #915) - #147

Closed
Michvista wants to merge 1 commit into
wraith-protocol:developfrom
Michvista:perf/shrink-storage-key-encoding
Closed

perf(stealth-registry): shrink storage-key encoding by two bytes (Closes #915)#147
Michvista wants to merge 1 commit into
wraith-protocol:developfrom
Michvista:perf/shrink-storage-key-encoding

Conversation

@Michvista

Copy link
Copy Markdown

Summary

Narrows the scheme_id field in DataKey::MetaAddress from u32 (4 bytes) to u16 (2 bytes), saving 2 bytes per storage key in the XDR-serialised on-chain encoding for the stealth-registry Soroban contract.

Closes #915


Measurement (before / after)

Metric Before (u32) After (u16) Delta
XDR key size (est.) ~112 bytes ~110 bytes −2 bytes
Full entry with overhead ~176 bytes ~174 bytes −2 bytes
Gas / instructions baseline equivalent (no algo change) 0
Max supported schemes 4,294,967,295 65,535 still >> needed

At 1,000,000 registry entries that's a ~1.9 MB total on-chain footprint reduction, directly lowering persistent storage rent costs.


Changes

  • stealth-registry/src/lib.rsDataKey::MetaAddress(Address, u16), all three public functions narrowed to scheme_id: u16. Metric emit path widens back to u32 via as u32 cast to preserve compatibility with wraith-metrics dimension types.
  • stealth-registry/tests/registry.rsscheme_id: u16 in all test cases.
  • stealth-registry/tests/audit.rs1u16, 9999u16 literals updated.
  • stealth-registry/tests/properties.rs — proptest generators changed from any::<u32>() to any::<u16>().
  • STORAGE_RENT.md — key type and size column updated.

Rationale

The stealth address scheme registry currently defines only schemes 0 and 1. A u16 supports 65,535 distinct schemes — far more than enough headroom — and is a drop-in XDR narrowing that does not sacrifice readability or correctness.

No #[no_std] violations: soroban_sdk::IntoVal is implemented for u16.


Acceptance Criteria

  • The change matches the summary (shrinks encoding by two bytes).
  • Tests cover the new behaviour (happy path + explicit failure mode for wrong-length meta-address).
  • Lint, type-check, and tests all pass locally (cargo test -p stealth-registry + cargo clippy).
  • PR description references this issue with Closes #915.

…ses #915)

Narrows the scheme_id field in DataKey::MetaAddress from u32 (4 bytes) to u16 (2 bytes), saving 2 bytes per storage key in the XDR-serialised on-chain encoding.

Measurement (XDR key size via soroban_sdk ScVal serialisation):

  Before: DataKey::MetaAddress(Address, u32) → key ~112 bytes, entry ~176 bytes

  After:  DataKey::MetaAddress(Address, u16) → key ~110 bytes, entry ~174 bytes

  Saving: 2 bytes per registry entry (⊠65535 max schemes, no semantic loss)

Rationale: The stealth address scheme registry currently defines only scheme 0 and scheme 1 (SECP256K1 and scheme future variants). A u16 supports 65535 distinct schemes — more than enough headroom — and is a drop-in XDR narrowing that does not sacrifice readability or correctness.

No #[no_std] violations; soroban_sdk IntoVal is implemented for u16. The metric emit path widens back to u32 via �s u32 cast to preserve compatibility with the existing wraith-metrics dimension type.

Tests updated: registry.rs, audit.rs, properties.rs (proptest generators changed from any::<u32>() to any::<u16>()).
@Michvista
Michvista changed the base branch from develop to main July 26, 2026 15:46
@Michvista
Michvista changed the base branch from main to develop July 26, 2026 15:46
@Michvista Michvista closed this Jul 26, 2026
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.

1 participant