Skip to content

Commit

Permalink
Nicer pad_count_from_fe32_count()
Browse files Browse the repository at this point in the history
  • Loading branch information
optout21 committed Sep 8, 2024
1 parent 7136a4f commit 0fbc37c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,18 @@ where
}
}

/// Compute how many trailing extra 5-bit elements are needed
/// such that no significant bits are dropped if the last byte is dropped.
/// Returns 0 (result falls on byte boundary), 1, or 2.
fn pad_count_from_fe32_count(fe32_count: usize) -> u8 {
let remainder = (fe32_count * 5) % 8;
if remainder == 0 { 0 } else if remainder < 3 { 2 } else { 1 }
let leftover_bits = (fe32_count * 5) % 8;
if leftover_bits == 0 {
0
} else {
let needed_bits = 8 - leftover_bits; // 1..7
let needed_extra_fe32s = (needed_bits + (5 - 1)) / 5; // 1..2
needed_extra_fe32s as u8
}
}

fn padded_count(fe32_count: usize) -> usize {
Expand Down

0 comments on commit 0fbc37c

Please sign in to comment.