Skip to content

Commit 92789f4

Browse files
committed
f - cache message digest
1 parent ab17126 commit 92789f4

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
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 & 4 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

@@ -28,17 +29,18 @@ tlv_stream!(SignatureTlvStream, SignatureTlvStreamRef, SIGNATURE_TYPES, {
2829
pub struct TaggedBytes<'a> {
2930
tag: &'a str,
3031
bytes: &'a [u8],
32+
digest: RefCell<Option<Message>>,
3133
}
3234

3335
impl<'a> TaggedBytes<'a> {
3436
/// Creates tagged bytes with the given parameters.
3537
pub fn new(tag: &'a str, bytes: &'a [u8]) -> Self {
36-
Self { tag, bytes }
38+
Self { tag, bytes, digest: RefCell::new(None) }
3739
}
3840

3941
/// Returns the digest to sign.
4042
pub fn digest(&self) -> Message {
41-
message_digest(self.tag, self.bytes)
43+
*self.digest.borrow_mut().get_or_insert_with(|| message_digest(self.tag, self.bytes))
4244
}
4345
}
4446

@@ -62,9 +64,9 @@ pub(super) fn sign_message<F, E>(
6264
sign: F, message: TaggedBytes, metadata: &[u8], pubkey: PublicKey,
6365
) -> Result<Signature, SignError<E>>
6466
where
65-
F: FnOnce(TaggedBytes, &[u8]) -> Result<Signature, E>
67+
F: FnOnce(&TaggedBytes, &[u8]) -> Result<Signature, E>
6668
{
67-
let signature = sign(message, metadata).map_err(|e| SignError::Signing(e))?;
69+
let signature = sign(&message, metadata).map_err(|e| SignError::Signing(e))?;
6870

6971
let pubkey = pubkey.into();
7072
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)