Skip to content

Commit

Permalink
Use a constant for checksum length
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Aug 5, 2022
1 parent 9a57c97 commit 1735cb3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub trait WriteBase32 {
fn write_u5(&mut self, data: u5) -> Result<(), Self::Err>;
}

const CHECKSUM_LENGTH: usize = 6;

/// Allocationless Bech32 writer that accumulates the checksum data internally and writes them out
/// in the end.
pub struct Bech32Writer<'a> {
Expand Down Expand Up @@ -187,13 +189,13 @@ impl<'a> Bech32Writer<'a> {

fn inner_finalize(&mut self) -> fmt::Result {
// Pad with 6 zeros
for _ in 0..6 {
for _ in 0..CHECKSUM_LENGTH {
self.polymod_step(u5(0))
}

let plm: u32 = self.chk ^ self.variant.constant();

for p in 0..6 {
for p in 0..CHECKSUM_LENGTH {
self.formatter
.write_char(u5(((plm >> (5 * (5 - p))) & 0x1f) as u8).to_char())?;
}
Expand Down Expand Up @@ -469,7 +471,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
/// Returns the HRP in lowercase..
pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
// Ensure overall length is within bounds
if s.len() < 8 {
if s.len() < CHECKSUM_LENGTH + 2 {
return Err(Error::InvalidLength);
}

Expand All @@ -481,7 +483,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
(hrp, &data[1..])
}
};
if raw_data.len() < 6 {
if raw_data.len() < CHECKSUM_LENGTH {
return Err(Error::InvalidLength);
}

Expand Down Expand Up @@ -533,7 +535,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
Some(variant) => {
// Remove checksum from data payload
let dbl: usize = data.len();
data.truncate(dbl - 6);
data.truncate(dbl - CHECKSUM_LENGTH);

Ok((hrp_lower, data, variant))
}
Expand Down

0 comments on commit 1735cb3

Please sign in to comment.