]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Generalize build_keys_helper
authorJeffrey Czyz <jkczyz@gmail.com>
Fri, 16 Aug 2024 01:05:33 +0000 (20:05 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 20 Aug 2024 21:35:43 +0000 (16:35 -0500)
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

index 860d91cfcf923536bd2a33dce0cfb006f2de5117..dccdbf24eac815b7766e261bc309a9908968d42d 100644 (file)
@@ -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::<Sha256>::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)
                }}
        }