Skip to content

Commit b28fc40

Browse files
committed
Generalize build_keys_helper
When constructing a blinded path, two iterators are used: one for the pubkeys and another for Writeable TLVs. The first iterator is used in the build_keys_helper utility function while the second is used inside of a callback. Update this utility to work on any type that can be borrowed as a PublicKey. This allows for using a single iterator of tuples, which is necessary for padding the hops without additional allocations and clones.
1 parent 51d9218 commit b28fc40

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::util::ser::{Readable, Writeable};
2525

2626
use crate::io;
2727

28+
use core::borrow::Borrow;
29+
2830
#[allow(unused_imports)]
2931
use crate::prelude::*;
3032

@@ -38,22 +40,23 @@ macro_rules! build_keys_helper {
3840
let mut onion_packet_pubkey = msg_blinding_point.clone();
3941

4042
macro_rules! build_keys {
41-
($pk: expr, $blinded: expr, $encrypted_payload: expr) => {{
42-
let encrypted_data_ss = SharedSecret::new(&$pk, &msg_blinding_point_priv);
43+
($hop: expr, $blinded: expr, $encrypted_payload: expr) => {{
44+
let pk = *$hop.borrow();
45+
let encrypted_data_ss = SharedSecret::new(&pk, &msg_blinding_point_priv);
4346

44-
let blinded_hop_pk = if $blinded { $pk } else {
47+
let blinded_hop_pk = if $blinded { pk } else {
4548
let hop_pk_blinding_factor = {
4649
let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
4750
hmac.input(encrypted_data_ss.as_ref());
4851
Hmac::from_engine(hmac).to_byte_array()
4952
};
50-
$pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
53+
pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
5154
};
5255
let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);
5356

5457
let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref());
55-
let unblinded_pk_opt = if $blinded { None } else { Some($pk) };
56-
$callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
58+
let unblinded_hop_opt = if $blinded { None } else { Some($hop) };
59+
$callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_hop_opt, $encrypted_payload);
5760
(encrypted_data_ss, onion_packet_ss)
5861
}}
5962
}

0 commit comments

Comments
 (0)