From b28fc402604d368bda454a85e0327a0cc416ebb9 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 15 Aug 2024 20:05:33 -0500 Subject: [PATCH] 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. --- lightning/src/blinded_path/utils.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lightning/src/blinded_path/utils.rs b/lightning/src/blinded_path/utils.rs index 860d91cfc..dccdbf24e 100644 --- a/lightning/src/blinded_path/utils.rs +++ b/lightning/src/blinded_path/utils.rs @@ -25,6 +25,8 @@ use crate::util::ser::{Readable, Writeable}; use crate::io; +use core::borrow::Borrow; + #[allow(unused_imports)] use crate::prelude::*; @@ -38,22 +40,23 @@ macro_rules! build_keys_helper { let mut onion_packet_pubkey = msg_blinding_point.clone(); macro_rules! build_keys { - ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {{ - let encrypted_data_ss = SharedSecret::new(&$pk, &msg_blinding_point_priv); + ($hop: expr, $blinded: expr, $encrypted_payload: expr) => {{ + let pk = *$hop.borrow(); + let encrypted_data_ss = SharedSecret::new(&pk, &msg_blinding_point_priv); - let blinded_hop_pk = if $blinded { $pk } else { + let blinded_hop_pk = if $blinded { pk } else { let hop_pk_blinding_factor = { let mut hmac = HmacEngine::::new(b"blinded_node_id"); hmac.input(encrypted_data_ss.as_ref()); Hmac::from_engine(hmac).to_byte_array() }; - $pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())? + pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())? }; let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv); let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref()); - let unblinded_pk_opt = if $blinded { None } else { Some($pk) }; - $callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload); + let unblinded_hop_opt = if $blinded { None } else { Some($hop) }; + $callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_hop_opt, $encrypted_payload); (encrypted_data_ss, onion_packet_ss) }} } -- 2.39.5