Skip to content

Commit c9c7093

Browse files
committed
f - cache message digest
1 parent 84298a4 commit c9c7093

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

lightning/src/offers/invoice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a> UnsignedInvoice<'a> {
383383
/// This is not exported to bindings users as functions aren't currently mapped.
384384
pub fn sign<F, E>(self, sign: F) -> Result<Invoice, SignError<E>>
385385
where
386-
F: FnOnce(TaggedBytes, &[u8]) -> Result<Signature, E>
386+
F: FnOnce(&TaggedBytes, &[u8]) -> Result<Signature, E>
387387
{
388388
// Use the invoice_request bytes instead of the invoice_request TLV stream as the latter may
389389
// have contained unknown TLV records, which are not stored in `InvoiceRequestContents` or

lightning/src/offers/invoice_request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'a> UnsignedInvoiceRequest<'a> {
354354
/// This is not exported to bindings users as functions are not yet mapped.
355355
pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
356356
where
357-
F: FnOnce(TaggedBytes, &[u8]) -> Result<Signature, E>
357+
F: FnOnce(&TaggedBytes, &[u8]) -> Result<Signature, E>
358358
{
359359
// Use the offer bytes instead of the offer TLV stream as the offer may have contained
360360
// unknown TLV records, which are not stored in `OfferContents`.

lightning/src/offers/merkle.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use bitcoin::hashes::{Hash, HashEngine, sha256};
1313
use bitcoin::secp256k1::{Message, PublicKey, Secp256k1, self};
1414
use bitcoin::secp256k1::schnorr::Signature;
15+
use core::cell::RefCell;
1516
use crate::io;
1617
use crate::util::ser::{BigSize, Readable, Writeable, Writer};
1718

@@ -25,21 +26,21 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
2526
});
2627

2728
/// Bytes associated with a tag, which are used to produced a [`Message`] digest to sign.
28-
#[derive(Clone, Copy)]
2929
pub struct TaggedBytes<'a> {
3030
tag: &'a str,
3131
bytes: &'a [u8],
32+
digest: RefCell<Option<Message>>,
3233
}
3334

3435
impl<'a> TaggedBytes<'a> {
3536
/// Creates tagged bytes with the given parameters.
3637
pub fn new(tag: &'a str, bytes: &'a [u8]) -> Self {
37-
Self { tag, bytes }
38+
Self { tag, bytes, digest: RefCell::new(None) }
3839
}
3940

4041
/// Returns the digest to sign.
4142
pub fn digest(&self) -> Message {
42-
message_digest(self.tag, self.bytes)
43+
*self.digest.borrow_mut().get_or_insert_with(|| message_digest(self.tag, self.bytes))
4344
}
4445
}
4546

@@ -63,9 +64,9 @@ pub(super) fn sign_message<F, E>(
6364
sign: F, message: TaggedBytes, metadata: &[u8], pubkey: PublicKey,
6465
) -> Result<Signature, SignError<E>>
6566
where
66-
F: FnOnce(TaggedBytes, &[u8]) -> Result<Signature, E>
67+
F: FnOnce(&TaggedBytes, &[u8]) -> Result<Signature, E>
6768
{
68-
let signature = sign(message, metadata).map_err(|e| SignError::Signing(e))?;
69+
let signature = sign(&message, metadata).map_err(|e| SignError::Signing(e))?;
6970

7071
let pubkey = pubkey.into();
7172
let secp_ctx = Secp256k1::verification_only();

lightning/src/offers/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn payer_keys() -> KeyPair {
2525
KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap())
2626
}
2727

28-
pub(super) fn payer_sign(message: TaggedBytes, _metadata: &[u8]) -> Result<Signature, Infallible> {
28+
pub(super) fn payer_sign(message: &TaggedBytes, _metadata: &[u8]) -> Result<Signature, Infallible> {
2929
let secp_ctx = Secp256k1::new();
3030
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
3131
Ok(secp_ctx.sign_schnorr_no_aux_rand(&message.digest(), &keys))
@@ -41,7 +41,7 @@ pub(super) fn recipient_keys() -> KeyPair {
4141
}
4242

4343
pub(super) fn recipient_sign(
44-
message: TaggedBytes, _metadata: &[u8]
44+
message: &TaggedBytes, _metadata: &[u8]
4545
) -> Result<Signature, Infallible> {
4646
let secp_ctx = Secp256k1::new();
4747
let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[43; 32]).unwrap());

0 commit comments

Comments
 (0)