Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lightning-dns-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ mod test {
context,
&keys,
secp_ctx,
)
.unwrap()])
)])
}
}
impl Deref for DirectlyConnectedRouter {
Expand Down Expand Up @@ -349,8 +348,7 @@ mod test {
query_context,
&*payer_keys,
&secp_ctx,
)
.unwrap();
);
payer.pending_messages.lock().unwrap().push((
DNSResolverMessage::DNSSECQuery(msg),
MessageSendInstructions::WithSpecifiedReplyPath {
Expand Down
21 changes: 8 additions & 13 deletions lightning/src/blinded_path/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,19 @@ impl BlindedMessagePath {
pub fn one_hop<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
recipient_node_id: PublicKey, local_node_receive_key: ReceiveAuthKey,
context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1<T>,
) -> Result<Self, ()>
) -> Self
where
ES::Target: EntropySource,
{
Self::new(&[], recipient_node_id, local_node_receive_key, context, entropy_source, secp_ctx)
}

/// Create a path for an onion message, to be forwarded along `node_pks`. The last node
/// pubkey in `node_pks` will be the destination node.
///
/// Errors if no hops are provided or if `node_pk`(s) are invalid.
// TODO: make all payloads the same size with padding + add dummy hops
/// Create a path for an onion message, to be forwarded along `node_pks`.
pub fn new<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey,
local_node_receive_key: ReceiveAuthKey, context: MessageContext, entropy_source: ES,
secp_ctx: &Secp256k1<T>,
) -> Result<Self, ()>
) -> Self
where
ES::Target: EntropySource,
{
Expand All @@ -96,7 +92,7 @@ impl BlindedMessagePath {
intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey,
dummy_hop_count: usize, local_node_receive_key: ReceiveAuthKey, context: MessageContext,
entropy_source: ES, secp_ctx: &Secp256k1<T>,
) -> Result<Self, ()>
) -> Self
where
ES::Target: EntropySource,
{
Expand All @@ -107,7 +103,7 @@ impl BlindedMessagePath {
let blinding_secret =
SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");

Ok(Self(BlindedPath {
Self(BlindedPath {
introduction_node,
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
blinded_hops: blinded_hops(
Expand All @@ -118,9 +114,8 @@ impl BlindedMessagePath {
context,
&blinding_secret,
local_node_receive_key,
)
.map_err(|_| ())?,
}))
),
})
}

/// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
Expand Down Expand Up @@ -669,7 +664,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
recipient_node_id: PublicKey, dummy_hop_count: usize, context: MessageContext,
session_priv: &SecretKey, local_node_receive_key: ReceiveAuthKey,
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
) -> Vec<BlindedHop> {
let dummy_count = cmp::min(dummy_hop_count, MAX_DUMMY_HOPS_COUNT);
let pks = intermediate_nodes
.iter()
Expand Down
6 changes: 2 additions & 4 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ impl BlindedPaymentPath {
/// Create a blinded path for a payment, to be forwarded along `intermediate_nodes`.
///
/// Errors if:
/// * a provided node id is invalid
/// * [`BlindedPayInfo`] calculation results in an integer overflow
/// * any unknown features are required in the provided [`ForwardTlvs`]
// TODO: make all payloads the same size with padding + add dummy hops
Expand Down Expand Up @@ -151,8 +150,7 @@ impl BlindedPaymentPath {
payee_node_id,
payee_tlvs,
&blinding_secret,
)
.map_err(|_| ())?,
),
},
payinfo: blinded_payinfo,
})
Expand Down Expand Up @@ -663,7 +661,7 @@ pub(crate) const PAYMENT_PADDING_ROUND_OFF: usize = 30;
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[PaymentForwardNode], payee_node_id: PublicKey,
payee_tlvs: ReceiveTlvs, session_priv: &SecretKey,
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
) -> Vec<BlindedHop> {
let pks = intermediate_nodes
.iter()
.map(|node| (node.node_id, None))
Expand Down
32 changes: 13 additions & 19 deletions lightning/src/blinded_path/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ macro_rules! build_keys_helper {
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())
Copy link
Contributor

@joostjager joostjager Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what the original reason was for making an exception here and not panicking for blinded paths specifically? Is there any way in which input causing this could be provided from the outside?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I don't believe so, I wrote the code originally and I think it just made the code look better and there was no reason not to handle the error at the time.

.expect("RNG is busted")
};
let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);

Expand Down Expand Up @@ -84,9 +82,9 @@ macro_rules! build_keys_helper {
Sha256::from_engine(sha).to_byte_array()
};

msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(
&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap(),
)?;
msg_blinding_point_priv = msg_blinding_point_priv
.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())
.expect("RNG is busted");
msg_blinding_point =
PublicKey::from_secret_key($secp_ctx, &msg_blinding_point_priv);

Expand All @@ -96,9 +94,9 @@ macro_rules! build_keys_helper {
sha.input(onion_packet_ss.as_ref());
Sha256::from_engine(sha).to_byte_array()
};
onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(
&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap(),
)?;
onion_packet_pubkey_priv = onion_packet_pubkey_priv
.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())
.expect("RNG is busted");
onion_packet_pubkey =
PublicKey::from_secret_key($secp_ctx, &onion_packet_pubkey_priv);
};
Expand All @@ -109,8 +107,7 @@ macro_rules! build_keys_helper {
pub(crate) fn construct_keys_for_onion_message<'a, T, I, F>(
secp_ctx: &Secp256k1<T>, unblinded_path: I, destination: Destination, session_priv: &SecretKey,
mut callback: F,
) -> Result<(), secp256k1::Error>
where
) where
T: secp256k1::Signing + secp256k1::Verification,
I: Iterator<Item = PublicKey>,
F: FnMut(SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>),
Expand All @@ -134,13 +131,11 @@ where
}
},
}
Ok(())
}

fn construct_keys_for_blinded_path<'a, T, I, F, H>(
secp_ctx: &Secp256k1<T>, unblinded_path: I, session_priv: &SecretKey, mut callback: F,
) -> Result<(), secp256k1::Error>
where
) where
T: secp256k1::Signing + secp256k1::Verification,
H: Borrow<PublicKey>,
I: Iterator<Item = H>,
Expand All @@ -151,7 +146,6 @@ where
for pk in unblinded_path {
build_keys_in_loop!(pk, false, None);
}
Ok(())
}

struct PublicKeyWithTlvs<W: Writeable> {
Expand All @@ -168,7 +162,7 @@ impl<W: Writeable> Borrow<PublicKey> for PublicKeyWithTlvs<W> {

pub(crate) fn construct_blinded_hops<'a, T, I, W>(
secp_ctx: &Secp256k1<T>, unblinded_path: I, session_priv: &SecretKey,
) -> Result<Vec<BlindedHop>, secp256k1::Error>
) -> Vec<BlindedHop>
where
T: secp256k1::Signing + secp256k1::Verification,
I: Iterator<Item = ((PublicKey, Option<ReceiveAuthKey>), W)>,
Expand All @@ -194,8 +188,8 @@ where
),
});
},
)?;
Ok(blinded_hops)
);
blinded_hops
}

/// Encrypt TLV payload to be used as a [`crate::blinded_path::BlindedHop::encrypted_payload`].
Expand Down
10 changes: 5 additions & 5 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ fn route_blinding_spec_test_vector() {
];
let mut dave_eve_blinded_hops = blinded_path::utils::construct_blinded_hops(
&secp_ctx, path.into_iter(), &dave_eve_session_priv,
).unwrap();
);

// Concatenate an additional Bob -> Carol blinded path to the Eve -> Dave blinded path.
let bob_carol_session_priv = secret_from_hex("0202020202020202020202020202020202020202020202020202020202020202");
Expand All @@ -1563,7 +1563,7 @@ fn route_blinding_spec_test_vector() {
];
let bob_carol_blinded_hops = blinded_path::utils::construct_blinded_hops(
&secp_ctx, path.into_iter(), &bob_carol_session_priv,
).unwrap();
);

let mut blinded_hops = bob_carol_blinded_hops;
blinded_hops.append(&mut dave_eve_blinded_hops);
Expand Down Expand Up @@ -2030,7 +2030,7 @@ fn do_test_trampoline_single_hop_receive(success: bool) {
let path = [((carol_node_id, None), WithoutLength(&carol_unblinded_tlvs))];
blinded_path::utils::construct_blinded_hops(
&secp_ctx, path.into_iter(), &carol_alice_trampoline_session_priv,
).unwrap()
)
} else {
let payee_tlvs = blinded_path::payment::TrampolineForwardTlvs {
next_trampoline: alice_node_id,
Expand All @@ -2051,7 +2051,7 @@ fn do_test_trampoline_single_hop_receive(success: bool) {
let path = [((carol_node_id, None), WithoutLength(&carol_unblinded_tlvs))];
blinded_path::utils::construct_blinded_hops(
&secp_ctx, path.into_iter(), &carol_alice_trampoline_session_priv,
).unwrap()
)
};

let route = Route {
Expand Down Expand Up @@ -2255,7 +2255,7 @@ fn test_trampoline_unblinded_receive() {
let carol_blinding_point = PublicKey::from_secret_key(&secp_ctx, &carol_alice_trampoline_session_priv);
let carol_blinded_hops = blinded_path::utils::construct_blinded_hops(
&secp_ctx, path.into_iter(), &carol_alice_trampoline_session_priv,
).unwrap();
);

let route = Route {
paths: vec![Path {
Expand Down
21 changes: 14 additions & 7 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8958,12 +8958,19 @@ where
}
}

#[rustfmt::skip]
fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK> where L::Target: Logger {
debug_assert!(self.holder_commitment_point.next_transaction_number() <= INITIAL_COMMITMENT_NUMBER - 2);
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
let per_commitment_secret = self.context.holder_signer.as_ref()
.release_commitment_secret(self.holder_commitment_point.next_transaction_number() + 2).ok();
fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK>
where
L::Target: Logger,
{
debug_assert!(
self.holder_commitment_point.next_transaction_number() <= INITIAL_COMMITMENT_NUMBER - 2
);
let signer = &self.context.holder_signer;
self.holder_commitment_point.try_resolve_pending(signer, &self.context.secp_ctx, logger);
let per_commitment_secret = signer
.as_ref()
.release_commitment_secret(self.holder_commitment_point.next_transaction_number() + 2)
.ok();
if let Some(per_commitment_secret) = per_commitment_secret {
if self.holder_commitment_point.can_advance() {
self.context.signer_pending_revoke_and_ack = false;
Expand All @@ -8973,7 +8980,7 @@ where
next_per_commitment_point: self.holder_commitment_point.next_point(),
#[cfg(taproot)]
next_local_nonce: None,
})
});
}
}
if !self.holder_commitment_point.can_advance() {
Expand Down
Loading
Loading