Merge pull request #2828 from TheBlueMatt/2024-01-crypto-module
authorvalentinewallace <valentinewallace@users.noreply.github.com>
Wed, 17 Jan 2024 17:53:02 +0000 (12:53 -0500)
committerGitHub <noreply@github.com>
Wed, 17 Jan 2024 17:53:02 +0000 (12:53 -0500)
Move cryptographic algorithms and utilities to a new `crypto` mod

1  2 
lightning/src/ln/msgs.rs
lightning/src/ln/onion_utils.rs
lightning/src/routing/router.rs

diff --combined lightning/src/ln/msgs.rs
index bc914ce49756ee0933dd7cbcf8ace51f137375be,efd04d143d93a98d211608d59040671a52c758b1..d39f1a6084274762331b456d28494b725999d69e
@@@ -53,7 -53,7 +53,7 @@@ use crate::io::{self, Cursor, Read}
  use crate::io_extras::read_to_end;
  
  use crate::events::{EventsProvider, MessageSendEventsProvider};
- use crate::util::chacha20poly1305rfc::ChaChaPolyReadAdapter;
+ use crate::crypto::streams::ChaChaPolyReadAdapter;
  use crate::util::logger;
  use crate::util::ser::{LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited, BigSize};
  use crate::util::base32;
@@@ -1714,7 -1714,7 +1714,7 @@@ mod fuzzy_internal_msgs 
                        payment_relay: PaymentRelay,
                        payment_constraints: PaymentConstraints,
                        features: BlindedHopFeatures,
 -                      intro_node_blinding_point: PublicKey,
 +                      intro_node_blinding_point: Option<PublicKey>,
                },
                BlindedReceive {
                        sender_intended_htlc_amt_msat: u64,
@@@ -2394,7 -2394,7 +2394,7 @@@ impl<NS: Deref> ReadableArgs<(Option<Pu
                                                payment_relay,
                                                payment_constraints,
                                                features,
 -                                              intro_node_blinding_point: intro_node_blinding_point.ok_or(DecodeError::InvalidValue)?,
 +                                              intro_node_blinding_point,
                                        })
                                },
                                ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
index 137699521f482b6b186a4375477284a010bc897b,d9455fc1de2472f935e5cf1daf1b979c7820394f..1ea6fd83aa237f08e9f911dc052c42b06a042b01
@@@ -14,7 -14,8 +14,8 @@@ use crate::ln::wire::Encode
  use crate::routing::gossip::NetworkUpdate;
  use crate::routing::router::{BlindedTail, Path, RouteHop};
  use crate::sign::NodeSigner;
- use crate::util::chacha20::{ChaCha20, ChaChaReader};
+ use crate::crypto::chacha20::ChaCha20;
+ use crate::crypto::streams::ChaChaReader;
  use crate::util::errors::{self, APIError};
  use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, LengthCalculatingWriter};
  use crate::util::logger::Logger;
@@@ -188,10 -189,11 +189,10 @@@ pub(super) fn build_onion_payloads(path
                                for (i, blinded_hop) in hops.iter().enumerate() {
                                        if i == hops.len() - 1 {
                                                cur_value_msat += final_value_msat;
 -                                              cur_cltv += excess_final_cltv_expiry_delta;
                                                res.push(msgs::OutboundOnionPayload::BlindedReceive {
                                                        sender_intended_htlc_amt_msat: *final_value_msat,
                                                        total_msat,
 -                                                      cltv_expiry_height: cltv,
 +                                                      cltv_expiry_height: cur_cltv + excess_final_cltv_expiry_delta,
                                                        encrypted_tlvs: blinded_hop.encrypted_payload.clone(),
                                                        intro_node_blinding_point: blinding_point.take(),
                                                });
index 524bb1bc4c708baa983b65064a76d17ade17ae80,0b19369a4aa74bf95ed1cc4e3f038c6b381f486d..9a5c2dfbf215d5a21a3cab0fe2d2689a8453c496
@@@ -26,7 -26,7 +26,7 @@@ use crate::routing::scoring::{ChannelUs
  use crate::sign::EntropySource;
  use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
  use crate::util::logger::{Level, Logger};
- use crate::util::chacha20::ChaCha20;
+ use crate::crypto::chacha20::ChaCha20;
  
  use crate::io;
  use crate::prelude::*;
@@@ -114,14 -114,19 +114,14 @@@ impl<G: Deref<Target = NetworkGraph<L>
                                        None => return None,
                                };
                                let payment_relay: PaymentRelay = match details.counterparty.forwarding_info {
 -                                      Some(forwarding_info) => forwarding_info.into(),
 +                                      Some(forwarding_info) => match forwarding_info.try_into() {
 +                                              Ok(payment_relay) => payment_relay,
 +                                              Err(()) => return None,
 +                                      },
                                        None => return None,
                                };
  
 -                              // Avoid exposing esoteric CLTV expiry deltas
 -                              let cltv_expiry_delta = match payment_relay.cltv_expiry_delta {
 -                                      0..=40 => 40u32,
 -                                      41..=80 => 80u32,
 -                                      81..=144 => 144u32,
 -                                      145..=216 => 216u32,
 -                                      _ => return None,
 -                              };
 -
 +                              let cltv_expiry_delta = payment_relay.cltv_expiry_delta as u32;
                                let payment_constraints = PaymentConstraints {
                                        max_cltv_expiry: tlvs.payment_constraints.max_cltv_expiry + cltv_expiry_delta,
                                        htlc_minimum_msat: details.inbound_htlc_minimum_msat.unwrap_or(0),
@@@ -2701,7 -2706,7 +2701,7 @@@ where L::Target: Logger 
                                                }
                                        }
  
 -                                      // Means we succesfully traversed from the payer to the payee, now
 +                                      // Means we successfully traversed from the payer to the payee, now
                                        // save this path for the payment route. Also, update the liquidity
                                        // remaining on the used hops, so that we take them into account
                                        // while looking for more paths.
@@@ -3190,7 -3195,7 +3190,7 @@@ mod tests 
        use crate::offers::invoice::BlindedPayInfo;
        use crate::util::config::UserConfig;
        use crate::util::test_utils as ln_test_utils;
-       use crate::util::chacha20::ChaCha20;
+       use crate::crypto::chacha20::ChaCha20;
        use crate::util::ser::{Readable, Writeable};
        #[cfg(c_bindings)]
        use crate::util::ser::Writer;
        fn do_min_htlc_overpay_violates_max_htlc(blinded_payee: bool) {
                // Test that if overpaying to meet a later hop's min_htlc and causes us to violate an earlier
                // hop's max_htlc, we don't consider that candidate hop valid. Previously we would add this hop
 -              // to `targets` and build an invalid path with it, and subsquently hit a debug panic asserting
 +              // to `targets` and build an invalid path with it, and subsequently hit a debug panic asserting
                // that the used liquidity for a hop was less than its available liquidity limit.
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(ln_test_utils::TestLogger::new());
@@@ -8447,7 -8452,7 +8447,7 @@@ pub(crate) mod bench_utils 
                                                        }
                                                        break;
                                                }
 -                                              // If we couldn't find a path with a higer amount, reduce and try again.
 +                                              // If we couldn't find a path with a higher amount, reduce and try again.
                                                score_amt /= 100;
                                        }