Skip to content

Commit

Permalink
Generalize next_hop_packet_pubkey onion util
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Jul 13, 2023
1 parent bddb2a1 commit 769e222
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2784,9 +2784,9 @@ where
outgoing_cltv_value,
}, ..
} => {
let next_pk = onion_utils::next_hop_packet_pubkey(&self.secp_ctx,
let next_packet_pk = onion_utils::next_hop_pubkey(&self.secp_ctx,
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_pk))
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_packet_pk))
},
// We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
// inbound channel's state.
Expand Down
11 changes: 7 additions & 4 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ pub(super) fn gen_pad_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
Hmac::from_engine(hmac).into_inner()
}

pub(crate) fn next_hop_packet_pubkey<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, packet_pubkey: PublicKey, packet_shared_secret: &[u8; 32]) -> Result<PublicKey, secp256k1::Error> {
/// Calculates a pubkey for the next hop, such as the next hop's packet pubkey or blinding point.
pub(crate) fn next_hop_pubkey<T: secp256k1::Signing + secp256k1::Verification>(
secp_ctx: &Secp256k1<T>, curr_pubkey: PublicKey, shared_secret: &[u8]
) -> Result<PublicKey, secp256k1::Error> {
let blinding_factor = {
let mut sha = Sha256::engine();
sha.input(&packet_pubkey.serialize()[..]);
sha.input(packet_shared_secret);
sha.input(&curr_pubkey.serialize()[..]);
sha.input(shared_secret);
Sha256::from_engine(sha).into_inner()
};

packet_pubkey.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
curr_pubkey.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
}

// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
Expand Down
15 changes: 5 additions & 10 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ where
// unwrapping the onion layers to get to the final payload. Since we don't have the option
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
// for now.
let new_pubkey = match onion_utils::next_hop_packet_pubkey(&self.secp_ctx, msg.onion_routing_packet.public_key, &onion_decode_ss) {
let new_pubkey = match onion_utils::next_hop_pubkey(&self.secp_ctx, msg.onion_routing_packet.public_key, &onion_decode_ss) {
Ok(pk) => pk,
Err(e) => {
log_trace!(self.logger, "Failed to compute next hop packet pubkey: {}", e);
Expand All @@ -507,21 +507,16 @@ where
blinding_point: match next_blinding_override {
Some(blinding_point) => blinding_point,
None => {
let blinding_factor = {
let mut sha = Sha256::engine();
sha.input(&msg.blinding_point.serialize()[..]);
sha.input(control_tlvs_ss.as_ref());
Sha256::from_engine(sha).into_inner()
};
let next_blinding_point = msg.blinding_point;
match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
match onion_utils::next_hop_pubkey(
&self.secp_ctx, msg.blinding_point, control_tlvs_ss.as_ref()
) {
Ok(bp) => bp,
Err(e) => {
log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
return
}
}
},
}
},
onion_routing_packet: outgoing_packet,
};
Expand Down

0 comments on commit 769e222

Please sign in to comment.