Skip to content

Commit 39c1d6b

Browse files
committed
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.
1 parent c89b96a commit 39c1d6b

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

lightning-invoice/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ rustdoc-args = ["--cfg", "docsrs"]
1717
[features]
1818
default = ["std"]
1919
no-std = ["lightning/no-std"]
20-
std = ["bitcoin/std", "num-traits/std", "lightning/std", "bech32/std"]
20+
std = ["bitcoin/std", "lightning/std", "bech32/std"]
2121

2222
[dependencies]
2323
bech32 = { version = "0.9.0", default-features = false }
2424
lightning = { version = "0.0.121", path = "../lightning", default-features = false }
2525
secp256k1 = { version = "0.27.0", default-features = false, features = ["recovery", "alloc"] }
26-
num-traits = { version = "0.2.8", default-features = false }
2726
serde = { version = "1.0.118", optional = true }
2827
bitcoin = { version = "0.30.2", default-features = false }
2928

lightning-invoice/src/de.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use lightning::ln::PaymentSecret;
1818
use lightning::routing::gossip::RoutingFees;
1919
use lightning::routing::router::{RouteHint, RouteHintHop};
2020

21-
use num_traits::{CheckedAdd, CheckedMul};
22-
2321
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
2422
use secp256k1::PublicKey;
2523

@@ -356,7 +354,7 @@ impl FromBase32 for PositiveTimestamp {
356354
if b32.len() != 7 {
357355
return Err(Bolt11ParseError::InvalidSliceLength("PositiveTimestamp::from_base32()".into()));
358356
}
359-
let timestamp: u64 = parse_int_be(b32, 32)
357+
let timestamp: u64 = parse_u64_be(b32)
360358
.expect("7*5bit < 64bit, no overflow possible");
361359
match PositiveTimestamp::from_unix_timestamp(timestamp) {
362360
Ok(t) => Ok(t),
@@ -382,16 +380,17 @@ impl FromBase32 for Bolt11InvoiceSignature {
382380
}
383381
}
384382

385-
pub(crate) fn parse_int_be<T, U>(digits: &[U], base: T) -> Option<T>
386-
where T: CheckedAdd + CheckedMul + From<u8> + Default,
387-
U: Into<u8> + Copy
388-
{
389-
digits.iter().fold(Some(Default::default()), |acc, b|
390-
acc
391-
.and_then(|x| x.checked_mul(&base))
392-
.and_then(|x| x.checked_add(&(Into::<u8>::into(*b)).into()))
393-
)
394-
}
383+
macro_rules! define_parse_int_be { ($name: ident, $ty: ty) => {
384+
fn $name(digits: &[u5]) -> Option<$ty> {
385+
digits.iter().fold(Some(Default::default()), |acc, b|
386+
acc
387+
.and_then(|x| x.checked_mul(32))
388+
.and_then(|x| x.checked_add((Into::<u8>::into(*b)).into()))
389+
)
390+
}
391+
} }
392+
define_parse_int_be!(parse_u16_be, u16);
393+
define_parse_int_be!(parse_u64_be, u64);
395394

396395
fn parse_tagged_parts(data: &[u5]) -> Result<Vec<RawTaggedField>, Bolt11ParseError> {
397396
let mut parts = Vec::<RawTaggedField>::new();
@@ -404,7 +403,7 @@ fn parse_tagged_parts(data: &[u5]) -> Result<Vec<RawTaggedField>, Bolt11ParseErr
404403

405404
// Ignore tag at data[0], it will be handled in the TaggedField parsers and
406405
// parse the length to find the end of the tagged field's data
407-
let len = parse_int_be(&data[1..3], 32).expect("can't overflow");
406+
let len = parse_u16_be(&data[1..3]).expect("can't overflow") as usize;
408407
let last_element = 3 + len;
409408

410409
if data.len() < last_element {
@@ -517,7 +516,7 @@ impl FromBase32 for ExpiryTime {
517516
type Err = Bolt11ParseError;
518517

519518
fn from_base32(field_data: &[u5]) -> Result<ExpiryTime, Bolt11ParseError> {
520-
match parse_int_be::<u64, u5>(field_data, 32)
519+
match parse_u64_be(field_data)
521520
.map(ExpiryTime::from_seconds)
522521
{
523522
Some(t) => Ok(t),
@@ -530,7 +529,7 @@ impl FromBase32 for MinFinalCltvExpiryDelta {
530529
type Err = Bolt11ParseError;
531530

532531
fn from_base32(field_data: &[u5]) -> Result<MinFinalCltvExpiryDelta, Bolt11ParseError> {
533-
let expiry = parse_int_be::<u64, u5>(field_data, 32);
532+
let expiry = parse_u64_be(field_data);
534533
if let Some(expiry) = expiry {
535534
Ok(MinFinalCltvExpiryDelta(expiry))
536535
} else {
@@ -761,12 +760,16 @@ mod test {
761760

762761
#[test]
763762
fn test_parse_int_from_bytes_be() {
764-
use crate::de::parse_int_be;
765-
766-
assert_eq!(parse_int_be::<u32, u8>(&[1, 2, 3, 4], 256), Some(16909060));
767-
assert_eq!(parse_int_be::<u32, u8>(&[1, 3], 32), Some(35));
768-
assert_eq!(parse_int_be::<u32, u8>(&[255, 255, 255, 255], 256), Some(4294967295));
769-
assert_eq!(parse_int_be::<u32, u8>(&[1, 0, 0, 0, 0], 256), None);
763+
use crate::de::parse_u16_be;
764+
765+
assert_eq!(parse_u16_be(&[
766+
u5::try_from_u8(1).unwrap(), u5::try_from_u8(2).unwrap(),
767+
u5::try_from_u8(3).unwrap(), u5::try_from_u8(4).unwrap()]
768+
), Some(34916));
769+
assert_eq!(parse_u16_be(&[
770+
u5::try_from_u8(2).unwrap(), u5::try_from_u8(0).unwrap(),
771+
u5::try_from_u8(0).unwrap(), u5::try_from_u8(0).unwrap()]
772+
), None);
770773
}
771774

772775
#[test]

lightning-invoice/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub mod utils;
3131

3232
extern crate bech32;
3333
#[macro_use] extern crate lightning;
34-
extern crate num_traits;
3534
extern crate secp256k1;
3635
extern crate alloc;
3736
#[cfg(any(test, feature = "std"))]

0 commit comments

Comments
 (0)