Skip to content

feat: NUT-12 deterministic DLEQ nonce derivation and storage optimization#1062

Open
a1denvalu3 wants to merge 2 commits into
cashubtc:mainfrom
a1denvalu3:feat/deterministic-dleq
Open

feat: NUT-12 deterministic DLEQ nonce derivation and storage optimization#1062
a1denvalu3 wants to merge 2 commits into
cashubtc:mainfrom
a1denvalu3:feat/deterministic-dleq

Conversation

@a1denvalu3

Copy link
Copy Markdown
Collaborator

This PR implements the NUT-12 deterministic DLEQ nonce derivation (rejection-sampled HMAC-SHA256) as specified in the merged spec pull request: cashubtc/nuts#368

Summary of Changes:

  1. Deterministic Nonce Derivation:

    • Isolated the rejection-sampled HMAC-SHA256 nonce derivation into a standalone helper function deterministic_dleq_nonce inside cashu/core/crypto/b_dhke.py.
    • Updated step2_bob_dleq to utilize the helper when p_bytes is not explicitly provided.
    • Retained full backward compatibility for existing tests/usage that supply a mock nonce scalar.
  2. Performance & Storage Optimization:

    • Avoided redundant point multiplication of $C'$ by computing it once at the start of step2_bob_dleq and passing it to the helper function.
    • Optimized promise storage to stop saving the e and s DLEQ parameters in the database (dleq_e and dleq_s columns are now set to empty string for new promises, saving substantial storage space for active mints).
    • Annotated existing promises table schema definitions in cashu/mint/migrations.py to document the deprecation of the unused DLEQ columns.
  3. On-the-fly Reconstruction:

    • Modified the mint's /restore and get_melt_quote endpoints to dynamically reconstruct missing DLEQ proof values on-the-fly using the mint's private keys.
    • Supports seamless backward compatibility: historical random-nonce proofs continue to load normally, while new promises have their proofs generated dynamically.
  4. Testing:

    • Added the official NUT-12 deterministic nonce test vectors in tests/test_crypto.py.
    • Verified that all mint and wallet restore test suites pass flawlessly.

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.41463% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.02%. Comparing base (13ba163) to head (7a281f5).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
cashu/mint/ledger.py 38.09% 13 Missing ⚠️
cashu/core/crypto/b_dhke.py 90.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1062      +/-   ##
==========================================
+ Coverage   74.79%   75.02%   +0.23%     
==========================================
  Files         111      111              
  Lines       12413    12452      +39     
==========================================
+ Hits         9284     9342      +58     
+ Misses       3129     3110      -19     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant