Skip to content

Commit 8d22f99

Browse files
committed
Refactor helper macro from construct_keys_callback
When constructing a BlindedPath, utils::construct_blinded_hops uses two iterators. However, this makes it difficult to pad blinded hops to equal sizes without allocating a vector or cloning data. Refactor the construct_keys_callback utility function so that is can be used with an Iterator with different Item types. This allows using a single Iterator of tuples while still supporting its use only with pubkeys.
1 parent bbfa15e commit 8d22f99

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,11 @@ use crate::io;
2929
use crate::prelude::*;
3030

3131
// TODO: DRY with onion_utils::construct_onion_keys_callback
32-
#[inline]
33-
pub(crate) fn construct_keys_callback<'a, T, I, F>(
34-
secp_ctx: &Secp256k1<T>, unblinded_path: I, destination: Option<Destination>,
35-
session_priv: &SecretKey, mut callback: F
36-
) -> Result<(), secp256k1::Error>
37-
where
38-
T: secp256k1::Signing + secp256k1::Verification,
39-
I: Iterator<Item=&'a PublicKey>,
40-
F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>),
32+
macro_rules! build_keys_helper {
33+
($session_priv: ident, $secp_ctx: ident, $callback: ident) =>
4134
{
42-
let mut msg_blinding_point_priv = session_priv.clone();
43-
let mut msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
35+
let mut msg_blinding_point_priv = $session_priv.clone();
36+
let mut msg_blinding_point = PublicKey::from_secret_key($secp_ctx, &msg_blinding_point_priv);
4437
let mut onion_packet_pubkey_priv = msg_blinding_point_priv.clone();
4538
let mut onion_packet_pubkey = msg_blinding_point.clone();
4639

@@ -54,13 +47,13 @@ where
5447
hmac.input(encrypted_data_ss.as_ref());
5548
Hmac::from_engine(hmac).to_byte_array()
5649
};
57-
$pk.mul_tweak(secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
50+
$pk.mul_tweak($secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
5851
};
5952
let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);
6053

6154
let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref());
6255
let unblinded_pk_opt = if $blinded { None } else { Some($pk) };
63-
callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
56+
$callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
6457
(encrypted_data_ss, onion_packet_ss)
6558
}}
6659
}
@@ -77,7 +70,7 @@ where
7770
};
7871

7972
msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())?;
80-
msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
73+
msg_blinding_point = PublicKey::from_secret_key($secp_ctx, &msg_blinding_point_priv);
8174

8275
let onion_packet_pubkey_blinding_factor = {
8376
let mut sha = Sha256::engine();
@@ -86,9 +79,22 @@ where
8679
Sha256::from_engine(sha).to_byte_array()
8780
};
8881
onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())?;
89-
onion_packet_pubkey = PublicKey::from_secret_key(secp_ctx, &onion_packet_pubkey_priv);
82+
onion_packet_pubkey = PublicKey::from_secret_key($secp_ctx, &onion_packet_pubkey_priv);
9083
};
9184
}
85+
}}
86+
87+
#[inline]
88+
pub(crate) fn construct_keys_callback<'a, T, I, F>(
89+
secp_ctx: &Secp256k1<T>, unblinded_path: I, destination: Option<Destination>,
90+
session_priv: &SecretKey, mut callback: F
91+
) -> Result<(), secp256k1::Error>
92+
where
93+
T: secp256k1::Signing + secp256k1::Verification,
94+
I: Iterator<Item=&'a PublicKey>,
95+
F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>),
96+
{
97+
build_keys_helper!(session_priv, secp_ctx, callback);
9298

9399
for pk in unblinded_path {
94100
build_keys_in_loop!(*pk, false, None);

0 commit comments

Comments
 (0)