Skip to content

Commit 2787ffe

Browse files
committed
Use a single iterator to construct a BlindedPath
Instead of using separate iterators for pubkeys and TLVs, use an iterator that yields a tuple of them. This allows for holding a mutable reference to the TLVs such that they can be padded. With two iterators, the pubkey iterator would have a reference to the ForwardNode slice when constructing a payment path. However, this would prevent holding mutable references in the TLVs iterator.
1 parent b28fc40 commit 2787ffe

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
386386
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
387387
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
388388

389-
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
389+
let path = pks.zip(tlvs);
390+
391+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
390392
}
391393

lightning/src/blinded_path/payment.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,10 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
466466
.chain(core::iter::once(payee_node_id));
467467
let tlvs = intermediate_nodes.iter().map(|node| BlindedPaymentTlvsRef::Forward(&node.tlvs))
468468
.chain(core::iter::once(BlindedPaymentTlvsRef::Receive(&payee_tlvs)));
469-
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
469+
470+
let path = pks.zip(tlvs);
471+
472+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
470473
}
471474

472475
/// `None` if underflow occurs.

lightning/src/blinded_path/utils.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ where
118118
}
119119

120120
#[inline]
121-
pub(super) fn construct_keys_callback_for_blinded_path<'a, T, I, F>(
121+
pub(super) fn construct_keys_callback_for_blinded_path<'a, T, I, F, H>(
122122
secp_ctx: &Secp256k1<T>, unblinded_path: I, session_priv: &SecretKey, mut callback: F,
123123
) -> Result<(), secp256k1::Error>
124124
where
125125
T: secp256k1::Signing + secp256k1::Verification,
126-
I: Iterator<Item=PublicKey>,
127-
F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>),
126+
H: Borrow<PublicKey>,
127+
I: Iterator<Item=H>,
128+
F: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<H>, Option<Vec<u8>>),
128129
{
129130
build_keys_helper!(session_priv, secp_ctx, callback);
130131

@@ -134,23 +135,32 @@ where
134135
Ok(())
135136
}
136137

137-
// Panics if `unblinded_tlvs` length is less than `unblinded_pks` length
138-
pub(crate) fn construct_blinded_hops<'a, T, I1, I2>(
139-
secp_ctx: &Secp256k1<T>, unblinded_pks: I1, mut unblinded_tlvs: I2, session_priv: &SecretKey
138+
struct PublicKeyWithTlvs<W: Writeable> {
139+
pubkey: PublicKey,
140+
tlvs: W,
141+
}
142+
143+
impl<W: Writeable> Borrow<PublicKey> for PublicKeyWithTlvs<W> {
144+
fn borrow(&self) -> &PublicKey {
145+
&self.pubkey
146+
}
147+
}
148+
149+
pub(crate) fn construct_blinded_hops<'a, T, I, W>(
150+
secp_ctx: &Secp256k1<T>, unblinded_path: I, session_priv: &SecretKey,
140151
) -> Result<Vec<BlindedHop>, secp256k1::Error>
141152
where
142153
T: secp256k1::Signing + secp256k1::Verification,
143-
I1: Iterator<Item=PublicKey>,
144-
I2: Iterator,
145-
I2::Item: Writeable
154+
I: Iterator<Item=(PublicKey, W)>,
155+
W: Writeable
146156
{
147-
let mut blinded_hops = Vec::with_capacity(unblinded_pks.size_hint().0);
157+
let mut blinded_hops = Vec::with_capacity(unblinded_path.size_hint().0);
148158
construct_keys_callback_for_blinded_path(
149-
secp_ctx, unblinded_pks, session_priv,
150-
|blinded_node_id, _, _, encrypted_payload_rho, _, _| {
159+
secp_ctx, unblinded_path.map(|(pubkey, tlvs)| PublicKeyWithTlvs { pubkey, tlvs }), session_priv,
160+
|blinded_node_id, _, _, encrypted_payload_rho, unblinded_hop_data, _| {
151161
blinded_hops.push(BlindedHop {
152162
blinded_node_id,
153-
encrypted_payload: encrypt_payload(unblinded_tlvs.next().unwrap(), encrypted_payload_rho),
163+
encrypted_payload: encrypt_payload(unblinded_hop_data.unwrap().tlvs, encrypted_payload_rho),
154164
});
155165
})?;
156166
Ok(blinded_hops)

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,19 +1392,17 @@ fn route_blinding_spec_test_vector() {
13921392
let blinding_override = PublicKey::from_secret_key(&secp_ctx, &dave_eve_session_priv);
13931393
assert_eq!(blinding_override, pubkey_from_hex("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f"));
13941394
// Can't use the public API here as the encrypted payloads contain unknown TLVs.
1395+
let path = [(dave_node_id, WithoutLength(&dave_unblinded_tlvs)), (eve_node_id, WithoutLength(&eve_unblinded_tlvs))];
13951396
let mut dave_eve_blinded_hops = blinded_path::utils::construct_blinded_hops(
1396-
&secp_ctx, [dave_node_id, eve_node_id].iter(),
1397-
&mut [WithoutLength(&dave_unblinded_tlvs), WithoutLength(&eve_unblinded_tlvs)].iter(),
1398-
&dave_eve_session_priv
1397+
&secp_ctx, path.into_iter(), &dave_eve_session_priv
13991398
).unwrap();
14001399

14011400
// Concatenate an additional Bob -> Carol blinded path to the Eve -> Dave blinded path.
14021401
let bob_carol_session_priv = secret_from_hex("0202020202020202020202020202020202020202020202020202020202020202");
14031402
let bob_blinding_point = PublicKey::from_secret_key(&secp_ctx, &bob_carol_session_priv);
1403+
let path = [(bob_node_id, WithoutLength(&bob_unblinded_tlvs)), (carol_node_id, WithoutLength(&carol_unblinded_tlvs))];
14041404
let bob_carol_blinded_hops = blinded_path::utils::construct_blinded_hops(
1405-
&secp_ctx, [bob_node_id, carol_node_id].iter(),
1406-
&mut [WithoutLength(&bob_unblinded_tlvs), WithoutLength(&carol_unblinded_tlvs)].iter(),
1407-
&bob_carol_session_priv
1405+
&secp_ctx, path.into_iter(), &bob_carol_session_priv
14081406
).unwrap();
14091407

14101408
let mut blinded_hops = bob_carol_blinded_hops;

0 commit comments

Comments
 (0)