Skip to content

pkc%feat: add typed ECDH on secp256k1 keys - #140

Open
ZocoLini wants to merge 1 commit into
dashpay:developfrom
ZocoLini:feat/pkc-ecdh
Open

ZocoLini wants to merge 1 commit into
dashpay:developfrom
ZocoLini:feat/pkc-ecdh

Conversation

@ZocoLini

@ZocoLini ZocoLini commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The typed secp256k1 keys in pkc cannot compute an ECDH shared secret, so Platform exports the secret scalar into raw secp256k1 types to encrypt DashPay contact data and for DashConnect.

Closes #137.

Additional Information

EcdsaSecretKey gains ecdh_shared_secret, the hashed 32-byte secret (SHA256(0x02 | (y & 1) || x), as secp256k1::ecdh::SharedSecret::new), and ecdh_shared_point, the raw 64-byte x || y point. Both use the constant-time ECDH of libsecp256k1 and return Zeroizing buffers.

dashpay/rust-dashcore#1446 exposes them through dashcore::crypto::ecdsa once this is released.

How Has This Been Tested?

A unit test checks that both sides derive the same point and secret, and that the secret is the SHA256 of the prefixed X coordinate.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional tests
  • I have made corresponding changes to the documentation
  • I did not use agentic assistance (i.e. LLMs, AI) except as disclosed and can answer questions,
    comments and concerns without said assistance. I assume authorship of the code submitted.
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

Agent disclosure

May be assisted by Opus 5.5, medium effort.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

Note

This pull request has no conflicts! 🎊 🎉 🎊

@thepastaclaw

thepastaclaw commented Nov 19, 2026 •

Copy link
Copy Markdown

⛔ Final review complete — 1 blocking finding(s) (commit b983106) · triage: critical

@github-actions github-actions Bot added the status:behind This pull request can be rebased on the base branch. label Nov 19, 2026
`EcdsaSecretKey` gains `ecdh_shared_secret`, the hashed 32-byte secret,
and `ecdh_shared_point`, the raw 64-byte point, so Platform no longer
exports the secret scalar into raw `secp256k1` types.
@PastaPastaPasta

Copy link
Copy Markdown
Member

Steward: this one waits for the maintainer. He is away, and the steward reviews and merges in his place under the rules in #131. Those rules do not let me merge this PR. Nothing is wrong with the code as far as I have read it; this is about who decides.

  • Any change to operative crypto in dash-pkc is decided by the maintainer, whatever its size. ECDH is key agreement on the secp256k1 keys, so it is one. While he is away, such a change can go to a unanimous review council in his place, but only when a consumer is blocked without it.
  • No consumer is blocked. As feat(pkc): typed ECDH on secp256k1 keys #137 says, Platform computes these secrets today with its own secp256k1 dependency. The PR also changes the CodeQL model, which the steward does not change.

It is question 5 in #131, so he sees it first on his return. The PR stays open, and I will not close it.

If you want to prepare it for his review, his closest code is BlsSecretKey::dh_exchange (pkgs/pkc/src/bls/secret_ops.rs:152). It returns the unhashed point as the BlsDhBytes byte bag and is tested against reference vectors (dh_exchange_matches_vectors). This PR uses a different name and different return types, adds a hashed form, and has no reference vector. He decides whether that matters; I am not asking for changes.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Final validation — Phase 1 + Phase 2

The typed ECDH methods use the correct libsecp256k1 operations and the relevant CI checks succeeded. The hashed-secret method nevertheless leaves a copy of the shared secret in a backend SharedSecret value that is not erased on drop, and the raw-point API needs an explicit key-derivation warning. The unit test verifies symmetry and the internal hash relationship but lacks an independent compatibility vector.

🔴 1 blocking | 🟡 2 suggestion(s)

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-astra (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-astra (agent: phase2-reviewer, role: rust-quality); reviewer 5: gpt-6.2-astra (agent: phase2-reviewer, role: security-auditor); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: critical by gpt-6.2-sol (effort low) — The change adds ECDH shared-secret and shared-point derivation to typed secp256k1 secret keys in pkgs/pkc/src/ecdsa/secret_ops.rs, directly modifying cryptographic key-handling behavior.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Single stage: Phase 1 and Phase 2 reviewed this head side by side, with no blocker gate between them (triage tier)
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-astra — general (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.2-astra — security-auditor (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `pkgs/pkc/src/ecdsa/secret_ops.rs`:
- [BLOCKING] pkgs/pkc/src/ecdsa/secret_ops.rs:309-311: Erase the temporary backend SharedSecret
  `SharedSecret::new(...).to_secret_bytes()` copies the derived secret into the returned array, then drops the backend `SharedSecret`. In the pinned secp256k1 implementation, `SharedSecret` is `Copy` and provides `non_secure_erase()` but has no zeroizing `Drop` implementation. Therefore, wrapping only the copied array in `Zeroizing` leaves another shared-secret copy in the function's storage after return, undermining the secret-zeroization guarantees used throughout this module. Keep the backend value in a mutable local, copy its bytes, explicitly erase it, and then wrap the copied bytes.
- [SUGGESTION] pkgs/pkc/src/ecdsa/secret_ops.rs:313-316: Warn that the raw ECDH point must be hashed
  The public method exposes the unhashed `x || y` coordinates, but its documentation only describes the layout. Callers can mistake this output for ready-to-use key material even though the upstream API warns that a strong hash or KDF may be critical. Document the security requirement and direct callers to `ecdh_shared_secret` unless they intentionally need a custom KDF.
- [SUGGESTION] pkgs/pkc/src/ecdsa/secret_ops.rs:426-436: Add an independent ECDH compatibility vector
  The test checks symmetry and recomputes the hashed secret from `ecdh_shared_point()`, but both values are produced by the same backend and the expected hash is derived from the result under test. A consistently wrong coordinate convention or compatibility choice could therefore remain undetected. Add a fixed scalar/public-key known-answer vector with independently sourced `x || y` and SHA-256 values; this should also cover the alternate shared-point Y-parity branch.

Comment on lines +309 to +311
pub fn ecdh_shared_secret(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 32]> {
Zeroizing::new(SharedSecret::new(peer.as_inner(), &self.inner).to_secret_bytes())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Blocking: Erase the temporary backend SharedSecret

SharedSecret::new(...).to_secret_bytes() copies the derived secret into the returned array, then drops the backend SharedSecret. In the pinned secp256k1 implementation, SharedSecret is Copy and provides non_secure_erase() but has no zeroizing Drop implementation. Therefore, wrapping only the copied array in Zeroizing leaves another shared-secret copy in the function's storage after return, undermining the secret-zeroization guarantees used throughout this module. Keep the backend value in a mutable local, copy its bytes, explicitly erase it, and then wrap the copied bytes.

Suggested change
pub fn ecdh_shared_secret(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 32]> {
Zeroizing::new(SharedSecret::new(peer.as_inner(), &self.inner).to_secret_bytes())
}
pub fn ecdh_shared_secret(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 32]> {
let mut shared = SharedSecret::new(peer.as_inner(), &self.inner);
let bytes = shared.to_secret_bytes();
shared.non_secure_erase();
Zeroizing::new(bytes)
}

source: muse-spark-1.3-contributor (phase1-reviewer: general, rust-quality); gpt-6.2-astra (phase2-reviewer: general, rust-quality, security-auditor)

Comment on lines +313 to +316
/// ECDH shared point with `peer`, as the unhashed `x || y` coordinates.
pub fn ecdh_shared_point(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 64]> {
Zeroizing::new(shared_secret_point(peer.as_inner(), &self.inner))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Suggestion: Warn that the raw ECDH point must be hashed

The public method exposes the unhashed x || y coordinates, but its documentation only describes the layout. Callers can mistake this output for ready-to-use key material even though the upstream API warns that a strong hash or KDF may be critical. Document the security requirement and direct callers to ecdh_shared_secret unless they intentionally need a custom KDF.

Suggested change
/// ECDH shared point with `peer`, as the unhashed `x || y` coordinates.
pub fn ecdh_shared_point(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 64]> {
Zeroizing::new(shared_secret_point(peer.as_inner(), &self.inner))
}
/// ECDH shared point with `peer`, as the unhashed `x || y` coordinates.
///
/// # Security
///
/// Hash this point before using it as key material. The raw coordinates
/// must not be used directly as a secret; prefer
/// [`ecdh_shared_secret`](Self::ecdh_shared_secret) unless a custom KDF
/// over the point is required.
pub fn ecdh_shared_point(&self, peer: &EcdsaPublicKey) -> Zeroizing<[u8; 64]> {
Zeroizing::new(shared_secret_point(peer.as_inner(), &self.inner))
}

source: muse-spark-1.3-contributor (phase1-reviewer: general, rust-quality)

Comment on lines +426 to +436
fn ecdh_is_symmetric_and_hashes_the_point(alice_sk: EcdsaSecretKey, bob_sk: EcdsaSecretKey) {
let point = alice_sk.ecdh_shared_point(&bob_sk.public_key());
assert_eq!(*point, *bob_sk.ecdh_shared_point(&alice_sk.public_key()));

let secret = alice_sk.ecdh_shared_secret(&bob_sk.public_key());
assert_eq!(*secret, *bob_sk.ecdh_shared_secret(&alice_sk.public_key()));

let mut prefixed = [0x02 | (point[63] & 1); 33];
prefixed[1..].copy_from_slice(&point[..32]);
assert_eq!(*secret, sha256::Hash::hash(&prefixed).to_byte_array());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Suggestion: Add an independent ECDH compatibility vector

The test checks symmetry and recomputes the hashed secret from ecdh_shared_point(), but both values are produced by the same backend and the expected hash is derived from the result under test. A consistently wrong coordinate convention or compatibility choice could therefore remain undetected. Add a fixed scalar/public-key known-answer vector with independently sourced x || y and SHA-256 values; this should also cover the alternate shared-point Y-parity branch.

source: muse-spark-1.3-contributor (phase1-reviewer: general, rust-quality); gpt-6.2-astra (phase2-reviewer: general)

This branch has not been deployed

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

Labels

status:behind This pull request can be rebased on the base branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(pkc): typed ECDH on secp256k1 keys

3 participants