From: Matt Corallo Date: Wed, 13 Mar 2024 19:18:20 +0000 (+0000) Subject: Replace the generic `parse_int_be` with a macro called twice X-Git-Tag: v0.0.123-beta~30^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=39c1d6b2af4e1edc1616a844ba1c0ed48ae6471e;p=rust-lightning Replace the generic `parse_int_be` with a macro called twice `parse_int_be` is generic across integer types and also input types, but to do so it relies on the `num-traits` crate. There's not a lot of reason for this now that std has `from_be_bytes`, so we drop the generic now and replace it with a macro which is called twice to create two functions, both only supporting conversion from `u5` arrays. --- diff --git a/lightning-invoice/Cargo.toml b/lightning-invoice/Cargo.toml index 1d2a4cdbf..1692988ba 100644 --- a/lightning-invoice/Cargo.toml +++ b/lightning-invoice/Cargo.toml @@ -17,13 +17,12 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["std"] no-std = ["lightning/no-std"] -std = ["bitcoin/std", "num-traits/std", "lightning/std", "bech32/std"] +std = ["bitcoin/std", "lightning/std", "bech32/std"] [dependencies] bech32 = { version = "0.9.0", default-features = false } lightning = { version = "0.0.121", path = "../lightning", default-features = false } secp256k1 = { version = "0.27.0", default-features = false, features = ["recovery", "alloc"] } -num-traits = { version = "0.2.8", default-features = false } serde = { version = "1.0.118", optional = true } bitcoin = { version = "0.30.2", default-features = false } diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index b05563912..9284999b1 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -18,8 +18,6 @@ use lightning::ln::PaymentSecret; use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop}; -use num_traits::{CheckedAdd, CheckedMul}; - use secp256k1::ecdsa::{RecoveryId, RecoverableSignature}; use secp256k1::PublicKey; @@ -356,7 +354,7 @@ impl FromBase32 for PositiveTimestamp { if b32.len() != 7 { return Err(Bolt11ParseError::InvalidSliceLength("PositiveTimestamp::from_base32()".into())); } - let timestamp: u64 = parse_int_be(b32, 32) + let timestamp: u64 = parse_u64_be(b32) .expect("7*5bit < 64bit, no overflow possible"); match PositiveTimestamp::from_unix_timestamp(timestamp) { Ok(t) => Ok(t), @@ -382,16 +380,17 @@ impl FromBase32 for Bolt11InvoiceSignature { } } -pub(crate) fn parse_int_be(digits: &[U], base: T) -> Option - where T: CheckedAdd + CheckedMul + From + Default, - U: Into + Copy -{ - digits.iter().fold(Some(Default::default()), |acc, b| - acc - .and_then(|x| x.checked_mul(&base)) - .and_then(|x| x.checked_add(&(Into::::into(*b)).into())) - ) -} +macro_rules! define_parse_int_be { ($name: ident, $ty: ty) => { + fn $name(digits: &[u5]) -> Option<$ty> { + digits.iter().fold(Some(Default::default()), |acc, b| + acc + .and_then(|x| x.checked_mul(32)) + .and_then(|x| x.checked_add((Into::::into(*b)).into())) + ) + } +} } +define_parse_int_be!(parse_u16_be, u16); +define_parse_int_be!(parse_u64_be, u64); fn parse_tagged_parts(data: &[u5]) -> Result, Bolt11ParseError> { let mut parts = Vec::::new(); @@ -404,7 +403,7 @@ fn parse_tagged_parts(data: &[u5]) -> Result, Bolt11ParseErr // Ignore tag at data[0], it will be handled in the TaggedField parsers and // parse the length to find the end of the tagged field's data - let len = parse_int_be(&data[1..3], 32).expect("can't overflow"); + let len = parse_u16_be(&data[1..3]).expect("can't overflow") as usize; let last_element = 3 + len; if data.len() < last_element { @@ -517,7 +516,7 @@ impl FromBase32 for ExpiryTime { type Err = Bolt11ParseError; fn from_base32(field_data: &[u5]) -> Result { - match parse_int_be::(field_data, 32) + match parse_u64_be(field_data) .map(ExpiryTime::from_seconds) { Some(t) => Ok(t), @@ -530,7 +529,7 @@ impl FromBase32 for MinFinalCltvExpiryDelta { type Err = Bolt11ParseError; fn from_base32(field_data: &[u5]) -> Result { - let expiry = parse_int_be::(field_data, 32); + let expiry = parse_u64_be(field_data); if let Some(expiry) = expiry { Ok(MinFinalCltvExpiryDelta(expiry)) } else { @@ -761,12 +760,16 @@ mod test { #[test] fn test_parse_int_from_bytes_be() { - use crate::de::parse_int_be; - - assert_eq!(parse_int_be::(&[1, 2, 3, 4], 256), Some(16909060)); - assert_eq!(parse_int_be::(&[1, 3], 32), Some(35)); - assert_eq!(parse_int_be::(&[255, 255, 255, 255], 256), Some(4294967295)); - assert_eq!(parse_int_be::(&[1, 0, 0, 0, 0], 256), None); + use crate::de::parse_u16_be; + + assert_eq!(parse_u16_be(&[ + u5::try_from_u8(1).unwrap(), u5::try_from_u8(2).unwrap(), + u5::try_from_u8(3).unwrap(), u5::try_from_u8(4).unwrap()] + ), Some(34916)); + assert_eq!(parse_u16_be(&[ + u5::try_from_u8(2).unwrap(), u5::try_from_u8(0).unwrap(), + u5::try_from_u8(0).unwrap(), u5::try_from_u8(0).unwrap()] + ), None); } #[test] diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index a029c5f3d..20eaf7a82 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -31,7 +31,6 @@ pub mod utils; extern crate bech32; #[macro_use] extern crate lightning; -extern crate num_traits; extern crate secp256k1; extern crate alloc; #[cfg(any(test, feature = "std"))]