btc: use constant-time ECDH for silent payments
What changed, and why it matters
This commit hardens the BitBox02 hardware wallet's silent-payments feature against a side-channel attack. Previously, the device multiplied a secret key with a public key using a routine whose execution time could leak information about the secret. The change uses libsecp256k1's constant-time ECDH for the secret multiplication, and only performs a variable-time multiplication on a value that is already public to the connected computer. This reduces the risk that a local attacker or malware could recover private key material by timing how long the device takes.
No immediate user action required; this is a defensive hardening patch. Users should ensure firmware containing this commit is installed if they use silent payments. Developers should verify that `shared_secret_point` is indeed constant-time in the linked libsecp256k1 version and that no other secret-key multiplications remain on variable-time paths.
Security signals we found
Constant-time cryptographic operation introduced to replace variable-time secret-key multiplication
Variable-time scalar multiplication retained only on public operands
Silent payments private input key material (a_sum) no longer used in variable-time path
libsecp256k1 ECDH module enabled in build configuration
Evidence from the diff
The patch enables the libsecp256k1 ECDH module and changes the silent-payment output derivation. Instead of computing partial_secret = a_sum * inputs_hash (secret) and then scan_pubkey * partial_secret with a variable-time mul_tweak, it now computes c_pubkey = a_sum * scan_pubkey via secp256k1::ecdh::shared_secret_point, which is constant-time. The public c_pubkey is returned to the host as part of the DLEQ proof. The remaining tweak c_pubkey * inputs_hash uses variable-time mul_tweak, but both operands are public, so timing does not leak secret material. The create_dleq_proof helper is refactored to accept the precomputed c_pubkey rather than computing it internally.
Changed components
src/rust/streaming-silent-payments/src/lib.rssrc/rust/bitbox-secp256k1/build.rsBitBox02 silent payments implementationInspect captured patch +21 / −12
### src/rust/bitbox-secp256k1/build.rs
@@ -29,6 +29,7 @@ fn main() {
.define("COMB_TEETH", Some("5"))
.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"))
.define("ENABLE_MODULE_RECOVERY", Some("1")) // needed only in Rust unit tests.
+ .define("ENABLE_MODULE_ECDH", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
### src/rust/streaming-silent-payments/src/lib.rs
@@ -106,7 +106,7 @@ impl InputType {
// a_sum_pubkey is the corresponding pubkey.
// scan_pubkey is the output's silent payment scan pubkey (extracted from the silent payment address).
//
-// We create c_pubkey = a_sum*scan_pubkey and a DLEQ (discrete log equivalence) proof that
+// Given c_pubkey = a_sum*scan_pubkey, create a DLEQ (discrete log equivalence) proof that
// a_sum_pubkey has the same discrete log with respect to the secp256k1 base G as c_pubkey to the
// base scan_pubkey. The result returned to the host is
// `<33 byte compressed c_pubkey><64 byte dleq proof>`.
@@ -118,27 +118,24 @@ impl InputType {
// - verifying using the dleq proof that the discrete log of a_sum_pubkey to G is the same as the
// discrete log of c_pubkey to the base scan_pubkey.
// - re-computing the output (https://github.com/bitcoin/bips/blob/ad1d3bc2a7b0d84247c29f847e85c35283094e2f/bip-0352.mediawiki#creating-outputs)
-// using ecdsa_shared_secret = input_hash·a_sum·scan_pubkey = input_hash·c_pubkey
+// using ecdh_shared_secret = input_hash·a_sum·scan_pubkey = input_hash·c_pubkey
// - verifying that the created output matches the re-computed output.
fn create_dleq_proof(
secp: &Secp256k1<secp256k1::All>,
a_sum: &SecretKey,
a_sum_pubkey: &PublicKey,
scan_pubkey: &PublicKey,
+ c_pubkey: &PublicKey,
) -> Result<[u8; DLEQ_PROOF_SIZE], ()> {
- let c_pubkey = scan_pubkey
- .mul_tweak(secp, &Scalar::from(*a_sum))
- .map_err(|_| ())?;
-
let proof =
- bitbox_secp256k1::dleq_prove(secp, a_sum.as_ref(), scan_pubkey, a_sum_pubkey, &c_pubkey)?;
+ bitbox_secp256k1::dleq_prove(secp, a_sum.as_ref(), scan_pubkey, a_sum_pubkey, c_pubkey)?;
// Sanity check.
bitbox_secp256k1::dleq_verify(
secp,
proof.as_slice().try_into().unwrap(),
scan_pubkey,
a_sum_pubkey,
- &c_pubkey,
+ c_pubkey,
)?;
let mut result = c_pubkey.serialize().to_vec();
@@ -221,10 +218,15 @@ impl<'a> SilentPayment<'a> {
let inputs_hash =
hash::calculate_input_hash(self.smallest_outpoint.as_ref().ok_or(())?, a_sum_pubkey);
- let partial_secret = a_sum.mul_tweak(&inputs_hash).map_err(|_| ())?;
+ let xy = secp256k1::ecdh::shared_secret_point(&scan_pubkey, a_sum);
+ let mut uncompressed = [0u8; secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
+ uncompressed[0] = 0x04;
+ uncompressed[1..].copy_from_slice(&xy);
+ let c_pubkey = PublicKey::from_slice(&uncompressed).map_err(|_| ())?;
- let ecdh_shared_secret: PublicKey = scan_pubkey
- .mul_tweak(self.secp, &partial_secret.into())
+ // mul_tweak is variable-time. The host knows inputs_hash and receives c_pubkey in the proof.
+ let ecdh_shared_secret = c_pubkey
+ .mul_tweak(self.secp, &inputs_hash)
.map_err(|_| ())?;
// If we want to support more than one silent payment output, we need to get this value from
@@ -242,7 +244,13 @@ impl<'a> SilentPayment<'a> {
Ok(TransactionOutput {
pubkey: reskey_xonly,
- dleq_proof: create_dleq_proof(self.secp, a_sum, &a_sum_pubkey, &scan_pubkey)?,
+ dleq_proof: create_dleq_proof(
+ self.secp,
+ a_sum,
+ &a_sum_pubkey,
+ &scan_pubkey,
+ &c_pubkey,
+ )?,
})
}
}Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.