Skip to content

Harden signdatapqchash PQC counter accounting and usage reporting #45

Closed

Description

@kiwidream

Problem statement

signdatapqchash consumes a bounded PQC signature outside normal transaction signing. The implementation appears to use wallet-backed counter reservation through the same provider path as transaction signing, but coverage does not fully prove the intended policy for data-hash signing: pre-reservation failures must not move counters, and wallet-backed reservations that are already persisted should be treated as consumed rather than rolled back.

The same fix should make that policy explicit, test it, and improve visibility when signdatapqchash consumes a counter, especially around multi-leaf retries and post-reservation failures.

Evidence

  • signdatapqchash builds a PQCUsageRecorder, calls wallet data-hash signing, then appends usage only on success: src/wallet/rpc/signmessage.cpp:219.
  • Wallet data-hash signing delegates through matching SPKMs: src/wallet/wallet.cpp:2806.
  • DescriptorScriptPubKeyMan::SignDataPQCHash obtains a private FlatSigningProvider with the observer wired in: src/wallet/scriptpubkeyman.cpp:2499.
  • Wallet-backed providers install reserver, batch reserver, and observer hooks: src/wallet/scriptpubkeyman.cpp:1951.
  • SignP2MRDataHash filters candidate leaves and may continue to later leaves after a signing failure: src/wallet/scriptpubkeyman.cpp:2454.
  • FlatSigningProvider::SignPQC reserves before raw signing. Rollback currently exists only for the older writer path, not the wallet-backed reserver path: src/script/signingprovider.cpp:173, src/script/signingprovider.cpp:206.
  • Functional data-hash coverage checks the happy path and include_pqc_usage=false, but not persistence, failures, or lifecycle accounting: test/functional/feature_p2mr.py:528.
  • wallet_pqc_usage.py covers transaction signing RPC usage surfaces but not signdatapqchash: test/functional/wallet_pqc_usage.py:5.

Proposed acceptance criteria

  • Document/enforce policy: wallet-backed PQC reservations are committed once persisted; post-reservation signing failure must not reuse the reserved counter.
  • Pre-reservation signdatapqchash failures leave counters unchanged.
  • Successful signdatapqchash advances exactly one counter and reports the same usage fields as transaction signing.
  • If a post-reservation failure consumes a counter, it is visible: either included in the success usage report when a later leaf succeeds, or logged clearly when the RPC ultimately fails.
  • Near-limit behavior is tested: PQC_MAX_SIGNATURES - 1 signs once and reports exhausted/warnings; PQC_MAX_SIGNATURES fails without movement.
  • include_pqc_usage=false hides fields only; it must not change reservation or persistence behavior.

Suggested success and failure cases

  • Success: default leaf, explicit pubkey/leaf/control block, multi-leaf selected leaf, reload persistence.
  • Pre-reservation failure: bad requested pubkey, leaf mismatch, control block mismatch, missing/watch-only key, locked wallet, plaintext PQC validation blocked.
  • Post-reservation failure: forced raw signing failure after wallet reservation.
  • Multi-leaf: first candidate unavailable, first candidate post-reservation fails then later leaf succeeds, explicit failing leaf.
  • Limit: one remaining signature, no remaining signatures.

Recommended test location

  • Add focused unit tests in src/wallet/test/wallet_tests.cpp or src/test/pqc_tests.cpp for reservation-commit and post-reservation failure semantics.
  • Extend test/functional/wallet_pqc_usage.py with signdatapqchash lifecycle/reporting cases.
  • Keep proof/verification behavior in test/functional/feature_p2mr.py.

Recommended resolutions

  • Treat wallet-backed PQC counter reservations as committed once persisted. Do not add rollback for the wallet reserver path unless there is a stronger guarantee that raw signing did not consume the reserved signing slot.
  • Make consumed reservations observable even when raw signing fails after reservation. For wallet-backed pqc_counter_reserver, invoke the usage observer for the committed range even if CPQCKey::Sign later returns false.
  • If multi-leaf signdatapqchash consumes a counter on one candidate and later succeeds on another, include both consumed keys in pqc_key_states and warnings.
  • If signdatapqchash ultimately fails after consuming a counter, log the consumed counter movement in the wallet log. Avoid changing the RPC error shape in this issue.
  • Keep pre-reservation failures counter-neutral. Requested pubkey/leaf/control mismatches, missing keys, locked wallets, plaintext validation blocks, and exhausted counters should fail before reservation and leave usage unchanged.
  • Distinguish exhausted PQC budget from missing private key where practical, so operators can tell "cannot sign because the key is exhausted" from "cannot sign because the wallet lacks the key."
  • Add comments/tests that align data-hash signing with transaction signing's conservative policy: once a wallet counter is durably reserved, the signature slot is not reused.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions