Skip to content

Add BIP-374 DLEQ proof verification - #107

Open
FreeOnlineUser wants to merge 6 commits into
diybitcoinhardware:masterfrom
FreeOnlineUser:bip374-dleq
Open

FreeOnlineUser wants to merge 6 commits into
diybitcoinhardware:masterfrom
FreeOnlineUser:bip374-dleq

Conversation

@FreeOnlineUser

Copy link
Copy Markdown

Add BIP-374 DLEQ proof verification

Description

Adds DLEQ (Discrete Log Equality) proof verification as specified in BIP-374. This complements the existing BIP-375 PSBT field support in #79 by providing the actual cryptographic verification.

What this adds

  • verify_dleq_proof(A, B, C, proof) - Verify a BIP-374 DLEQ proof
  • generate_dleq_proof(a, B) - Generate a DLEQ proof (for testing/coordinators)
  • tagged_hash(tag, data) - BIP-340 style tagged hashing

Why this matters

BIP-375 Silent Payments PSBTs include DLEQ proofs (PSBT_GLOBAL_SP_DLEQ) to prove ECDH shares were computed correctly. Without verification, signing devices must trust the coordinator's math.
With this, a signing device can:

  1. Extract DLEQ proof from PSBT (using Implement BIP-375 Silent Payments PSBT Fields #79)
  2. Verify the proof cryptographically
  3. Confirm the Silent Payment output was derived correctly before signing

Usage

from embit.dleq import verify_dleq_proof

# Extract from BIP-375 PSBT fields
A = sum_of_input_pubkeys  # or single input pubkey
B = recipient_scan_key    # from PSBT_OUT_SP_V0_INFO
C = ecdh_share           # from PSBT_GLOBAL_SP_ECDH_SHARE
proof = dleq_proof       # from PSBT_GLOBAL_SP_DLEQ

if verify_dleq_proof(A, B, C, proof):
    print("ECDH share verified - safe to sign")

Implementation notes

  • Uses embit's existing secp256k1 bindings
  • ec_pubkey_negate() returns a new point (doesn't modify in-place)
  • 13 unit tests covering generation, verification, and edge cases

Related

References

Implement DLEQ proof verification and generation as per BIP-374. This includes functions to verify and generate DLEQ proofs for ECDH computations without revealing private keys.
This file contains tests for BIP-374 DLEQ proof verification and generation, including various scenarios for tagged hashing, proof generation, and verification.
Added tests to verify failure conditions for DLEQ proof generation.
@FreeOnlineUser

FreeOnlineUser commented Jan 11, 2026 •

Copy link
Copy Markdown
Author

Added edge case hardening based on cryptographic review:

Verification (verify_dleq_proof):

  • Added e >= SECP256K1_ORDER check (prevents malleability)
  • Added point-at-infinity validation for R1/R2 (rejects invalid 33-byte results)

Generation (generate_dleq_proof):

  • Added private key range validation (a ∈ [1, n))
  • Added nonce zero check after modular reduction

Tests:

  • Added 4 new tests for edge cases (17 total, all passing)
  • Tests cover: e >= n, s >= n, a == 0, a >= n, k == 0

Spec Compliance Note:

This implements BIP-374 DLEQ verification for BIP-375 Silent Payments use cases. The optional message parameter (m) is not implemented as BIP-375 doesn't use it. Proofs verify correctly for the BIP-375 workflow (message field empty). Can add message support if needed for other use cases.

Validation:

  • Round-trip tests (generate → verify)
  • Official BIP-374 test vectors (pending - can add if desired)
  • Interop testing with other implementations (pending)

No breaking changes to existing API signatures.

@quapka

quapka commented Apr 4, 2026

Copy link
Copy Markdown

Interop testing with other implementations (pending)

What about extending bitcoinfuzz with DLEQ-related targets? It could be tested against the reference implementation, NBitcoin, or rust-dleq.

@notTanveer

Copy link
Copy Markdown
Collaborator

If the nonce derivation skips the required r (aux randomness) input + the BIP0374/aux and BIP0374/nonce tagged hash flow, it can leak a.

could leak a if two proofs were constructed for the same a, B, and G but a different message m and an all-zero r.

The pure-python backend never implemented these (they were left as
commented-out stubs copied from the ctypes bindings), so any code using
them worked only when the prebuilt libsecp256k1 binary loaded.

Since ec_pubkey_parse returns immutable bytes in this backend, the
result cannot always be written back in place like libsecp256k1 does:
ec_pubkey_tweak_mul mutates the input when it is a bytearray and always
returns the resulting point. The ctypes ec_pubkey_tweak_mul now also
returns the tweaked pubkey so callers can use one return-value idiom
across both backends.
Address review feedback on the nonce derivation:

- Derive the nonce deterministically via the BIP0374/aux and
  BIP0374/nonce tagged hashes from the secret, fresh auxiliary
  randomness r, the points A and C, and the optional message m,
  as specified. The previous raw random nonce (and the caller-supplied
  nonce parameter, a reuse footgun) are gone; generate_dleq_proof now
  takes auxiliary randomness r instead.
- Support the optional 32-byte message m in generation and
  verification, committed in both the nonce and the challenge.
- Support a custom generator G in generation (verification already
  had it); normalize all points to compressed encodings for the
  challenge hash.
- Self-verify each generated proof before returning, as the reference
  implementation does.
- Run the official BIP-374 test vectors byte-exact
  (test_vectors_generate_proof.csv and test_vectors_verify_proof.csv,
  embedded verbatim), plus the existing property tests, now against
  both secp256k1 backends.
- Move the tests to tests/tests/test_dleq.py so the test runner
  actually discovers them, and drop the pytest dependency and typing
  import for micropython compatibility.
@FreeOnlineUser

Copy link
Copy Markdown
Author

Updated:

  • The nonce is now derived deterministically per BIP-374 (BIP0374/aux and BIP0374/nonce tagged hashes over the secret, fresh auxiliary randomness, A, C and the message). The raw random nonce and the caller-supplied nonce parameter are gone; generate_dleq_proof takes auxiliary randomness r instead. Thanks @notTanveer.
  • The optional 32-byte message m is supported in generation and verification.
  • The official BIP-374 test vectors (both CSVs) now run byte-exact in the suite, against both the ctypes and pure-python secp256k1 backends, which covers interop with the reference implementation.
  • Found while testing: the pure-python backend had no ec_pubkey_tweak_mul or ec_pubkey_combine (commented-out stubs only), so DLEQ verification silently returned False whenever the prebuilt libsecp256k1 did not load. Both are now implemented for backend parity.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants