Skip to content

Commit 38dfbf9

Browse files
committed
refactor to remove message_digest
We change the Bolt12Invoice struct to carry a tagged hash. Because message_digest is then only used in one place, we can inline it in the TaggedHash constructor.
1 parent b3e7aac commit 38dfbf9

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

lightning/src/offers/invoice.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl UnsignedBolt12Invoice {
439439
bytes: self.bytes,
440440
contents: self.contents,
441441
signature,
442+
tagged_hash: self.tagged_hash,
442443
})
443444
}
444445
}
@@ -463,6 +464,7 @@ pub struct Bolt12Invoice {
463464
bytes: Vec<u8>,
464465
contents: InvoiceContents,
465466
signature: Signature,
467+
tagged_hash: TaggedHash,
466468
}
467469

468470
/// The contents of an [`Bolt12Invoice`] for responding to either an [`Offer`] or a [`Refund`].
@@ -707,7 +709,7 @@ impl Bolt12Invoice {
707709

708710
/// Hash that was used for signing the invoice.
709711
pub fn signable_hash(&self) -> [u8; 32] {
710-
merkle::message_digest(SIGNATURE_TAG, &self.bytes).as_ref().clone()
712+
self.tagged_hash.as_digest().as_ref().clone()
711713
}
712714

713715
/// Verifies that the invoice was for a request or refund created using the given key. Returns
@@ -1212,11 +1214,11 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
12121214
None => return Err(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature)),
12131215
Some(signature) => signature,
12141216
};
1215-
let message = TaggedHash::new(SIGNATURE_TAG, &bytes);
1217+
let tagged_hash = TaggedHash::new(SIGNATURE_TAG, &bytes);
12161218
let pubkey = contents.fields().signing_pubkey;
1217-
merkle::verify_signature(&signature, message, pubkey)?;
1219+
merkle::verify_signature(&signature, &tagged_hash, pubkey)?;
12181220

1219-
Ok(Bolt12Invoice { bytes, contents, signature })
1221+
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash })
12201222
}
12211223
}
12221224

@@ -1431,7 +1433,7 @@ mod tests {
14311433
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
14321434

14331435
let message = TaggedHash::new(SIGNATURE_TAG, &invoice.bytes);
1434-
assert!(merkle::verify_signature(&invoice.signature, message, recipient_pubkey()).is_ok());
1436+
assert!(merkle::verify_signature(&invoice.signature, &message, recipient_pubkey()).is_ok());
14351437

14361438
let digest = Message::from_slice(&invoice.signable_hash()).unwrap();
14371439
let pubkey = recipient_pubkey().into();
@@ -1528,7 +1530,7 @@ mod tests {
15281530
assert_eq!(invoice.signing_pubkey(), recipient_pubkey());
15291531

15301532
let message = TaggedHash::new(SIGNATURE_TAG, &invoice.bytes);
1531-
assert!(merkle::verify_signature(&invoice.signature, message, recipient_pubkey()).is_ok());
1533+
assert!(merkle::verify_signature(&invoice.signature, &message, recipient_pubkey()).is_ok());
15321534

15331535
assert_eq!(
15341536
invoice.as_tlv_stream(),

lightning/src/offers/invoice_request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ impl TryFrom<Vec<u8>> for InvoiceRequest {
876876
Some(signature) => signature,
877877
};
878878
let message = TaggedHash::new(SIGNATURE_TAG, &bytes);
879-
merkle::verify_signature(&signature, message, contents.payer_id)?;
879+
merkle::verify_signature(&signature, &message, contents.payer_id)?;
880880

881881
Ok(InvoiceRequest { bytes, contents, signature })
882882
}
@@ -1013,7 +1013,7 @@ mod tests {
10131013
assert_eq!(invoice_request.payer_note(), None);
10141014

10151015
let message = TaggedHash::new(SIGNATURE_TAG, &invoice_request.bytes);
1016-
assert!(merkle::verify_signature(&invoice_request.signature, message, payer_pubkey()).is_ok());
1016+
assert!(merkle::verify_signature(&invoice_request.signature, &message, payer_pubkey()).is_ok());
10171017

10181018
assert_eq!(
10191019
invoice_request.as_tlv_stream(),

lightning/src/offers/merkle.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
3030
///
3131
/// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
3232
/// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md#signature-calculation
33-
#[derive(Debug, PartialEq)]
33+
#[derive(Clone, Debug, PartialEq)]
3434
pub struct TaggedHash(Message);
3535

3636
impl TaggedHash {
3737
/// Creates a tagged hash with the given parameters.
3838
///
3939
/// Panics if `tlv_stream` is not a well-formed TLV stream containing at least one TLV record.
4040
pub(super) fn new(tag: &str, tlv_stream: &[u8]) -> Self {
41-
Self(message_digest(tag, tlv_stream))
41+
let tag = sha256::Hash::hash(tag.as_bytes());
42+
let merkle_root = root_hash(tlv_stream);
43+
Self(Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap())
4244
}
4345

4446
/// Returns the digest to sign.
@@ -91,20 +93,14 @@ where
9193
/// Verifies the signature with a pubkey over the given message using a tagged hash as the message
9294
/// digest.
9395
pub(super) fn verify_signature(
94-
signature: &Signature, message: TaggedHash, pubkey: PublicKey,
96+
signature: &Signature, message: &TaggedHash, pubkey: PublicKey,
9597
) -> Result<(), secp256k1::Error> {
9698
let digest = message.as_digest();
9799
let pubkey = pubkey.into();
98100
let secp_ctx = Secp256k1::verification_only();
99101
secp_ctx.verify_schnorr(signature, digest, &pubkey)
100102
}
101103

102-
pub(super) fn message_digest(tag: &str, bytes: &[u8]) -> Message {
103-
let tag = sha256::Hash::hash(tag.as_bytes());
104-
let merkle_root = root_hash(bytes);
105-
Message::from_slice(&tagged_hash(tag, merkle_root)).unwrap()
106-
}
107-
108104
/// Computes a merkle root hash for the given data, which must be a well-formed TLV stream
109105
/// containing at least one TLV record.
110106
fn root_hash(data: &[u8]) -> sha256::Hash {

0 commit comments

Comments
 (0)