Skip to content

Commit

Permalink
Merge rust-bitcoin#3121: bip158: Improve casting
Browse files Browse the repository at this point in the history
ea9f6e8 Cast after calling min (Tobin C. Harding)
342fe18 Use From in map_to_range (Tobin C. Harding)
d933179 bip158: Fix typo (Tobin C. Harding)

Pull request description:

  Patch 1 is a typo fix, other two are cast improvements. Done as part of rust-bitcoin#2941.

  Leaves a cast to `u64` in there, will be removed once we get to the `ToU64` stuff. One other remaining cast in the module is obviously bit twiddling.

ACKs for top commit:
  Kixunil:
    ACK ea9f6e8
  apoelstra:
    ACK ea9f6e8 successfully ran local tests

Tree-SHA512: 92fa6225621f6c1eea4864472a5b9c60cb244e62cdfbee4074067da43b8f2bd4e3ed1a249ba40b994cad824e97585f3140747b03677ef1596c80be3007c94a7c
  • Loading branch information
apoelstra committed Aug 5, 2024
2 parents c28d5d5 + ea9f6e8 commit 865fdf7
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl GcsFilterReader {
}

/// Fast reduction of hash to [0, nm) range.
fn map_to_range(hash: u64, nm: u64) -> u64 { ((hash as u128 * nm as u128) >> 64) as u64 }
fn map_to_range(hash: u64, nm: u64) -> u64 { ((u128::from(hash) * u128::from(nm)) >> 64) as u64 }

/// Golomb-Rice encoded filter writer.
pub struct GcsFilterWriter<'a, W> {
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<'a, W: Write> GcsFilterWriter<'a, W> {
// write number of elements as varint
let mut wrote = VarInt::from(mapped.len()).consensus_encode(self.writer)?;

// write out deltas of sorted values into a Golonb-Rice coded bit stream
// write out deltas of sorted values into a Golomb-Rice coded bit stream
let mut writer = BitStreamWriter::new(self.writer);
let mut last = 0;
for data in mapped {
Expand Down Expand Up @@ -435,9 +435,9 @@ impl GcsFilter {
let mut wrote = 0;
let mut q = n >> self.p;
while q > 0 {
let nbits = cmp::min(q, 64);
wrote += writer.write(!0u64, nbits as u8)?;
q -= nbits;
let nbits = cmp::min(q, 64) as u8; // cast ok, 64 fits into a `u8`
wrote += writer.write(!0u64, nbits)?;
q -= u64::from(nbits);
}
wrote += writer.write(0, 1)?;
wrote += writer.write(n, self.p)?;
Expand Down

0 comments on commit 865fdf7

Please sign in to comment.