Merge PR 'Include recovery ID in invoice signature ordering' (#5516)
What changed, and why it matters
This change fixes how Lightning invoices are sorted when they contain signatures. Previously, two invoices that differed only by their 'recovery ID' (a small extra number attached to a signature) were treated as equal during sorting. That could cause inconsistent ordering, duplicate entries, or subtle logic bugs in code that relies on sorted invoice collections. The patch now includes the recovery ID in the comparison so signatures are ordered correctly and consistently.
Treat as a correctness fix. Include in normal release notes as a bugfix for invoice signature ordering. No emergency response is indicated absent evidence of active exploitation, but downstream users relying on sorted collections of invoices should update to avoid ordering/deduplication inconsistencies.
Security signals we found
Incorrect ordering/equality for cryptographic objects can lead to deduplication failures or consensus-adjacent inconsistencies in protocols relying on canonical ordering.
The fix is narrowly scoped to a single `Ord` implementation and includes a regression test.
No explicit security claim, CVE, or advisory is present in the commit or supplied references.
Evidence from the diff
The commit modifies the Ord implementation for Bolt11InvoiceSignature in lightning-invoice/src/lib.rs. Previously, cmp only compared the 64-byte compact signature bytes returned by serialize_compact().1, ignoring the recovery ID in .0. Because secp256k1 recoverable signatures can share the same 64-byte payload with different recovery IDs, two distinct signatures could compare as Ordering::Equal. The new implementation compares signature bytes first, then breaks ties using the recovery ID. A regression test constructs two valid invoices whose signatures differ only by recovery ID and asserts they are not equal and do not compare equal.
Changed components
lightning-invoice/src/lib.rsBolt11InvoiceSignatureOrd/PartialOrd for Bolt11InvoiceSignatureInspect captured patch +49 / −1
### lightning-invoice/src/lib.rs
@@ -606,7 +606,9 @@ impl PartialOrd for Bolt11InvoiceSignature {
impl Ord for Bolt11InvoiceSignature {
fn cmp(&self, other: &Self) -> Ordering {
- self.0.serialize_compact().1.cmp(&other.0.serialize_compact().1)
+ let (self_recovery_id, self_signature) = self.0.serialize_compact();
+ let (other_recovery_id, other_signature) = other.0.serialize_compact();
+ self_signature.cmp(&other_signature).then(self_recovery_id.cmp(&other_recovery_id))
}
}
@@ -2749,4 +2751,50 @@ mod test {
assert_eq!(field10.partial_cmp(&field11).unwrap(), std::cmp::Ordering::Less);
assert_eq!(field20.partial_cmp(&field21).unwrap(), std::cmp::Ordering::Less);
}
+
+ #[test]
+ fn invoice_signature_ordering_includes_recovery_id() {
+ use crate::{
+ Bolt11Invoice, Bolt11InvoiceSignature, Currency, InvoiceBuilder, PaymentHash,
+ PaymentSecret, SignedRawBolt11Invoice,
+ };
+ use bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
+ use bitcoin::secp256k1::{Secp256k1, SecretKey};
+ use core::cmp::Ordering;
+ use core::time::Duration;
+
+ let secp_ctx = Secp256k1::new();
+ let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
+ let invoice = InvoiceBuilder::new(Currency::Bitcoin)
+ .description("Test".to_string())
+ .payment_hash(PaymentHash([0; 32]))
+ .payment_secret(PaymentSecret([21; 32]))
+ .min_final_cltv_expiry_delta(144)
+ .duration_since_epoch(Duration::from_secs(1234567))
+ .build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &private_key))
+ .unwrap();
+
+ let (raw_invoice, hash, signature) = invoice.into_signed_raw().into_parts();
+ let (recovery_id, signature_bytes) = signature.0.serialize_compact();
+ let alternate_recovery_id = RecoveryId::from_i32(recovery_id.to_i32() ^ 1).unwrap();
+ let alternate_signature =
+ RecoverableSignature::from_compact(&signature_bytes, alternate_recovery_id).unwrap();
+
+ let original = Bolt11Invoice::from_signed(SignedRawBolt11Invoice {
+ raw_invoice: raw_invoice.clone(),
+ hash,
+ signature,
+ })
+ .unwrap();
+ let alternate = Bolt11Invoice::from_signed(SignedRawBolt11Invoice {
+ raw_invoice,
+ hash,
+ signature: Bolt11InvoiceSignature(alternate_signature),
+ })
+ .unwrap();
+
+ assert_ne!(original.get_payee_pub_key(), alternate.get_payee_pub_key());
+ assert_ne!(original, alternate);
+ assert_ne!(original.cmp(&alternate), Ordering::Equal);
+ }
}Why this scored 42/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.