Skip to content

Commit e4d615c

Browse files
committed
Merge #253: primitives: fix min/max size hint in HrpFe32Iter
ca024f9 primitives: fix min/max size hint in HrpFe32Iter (Renato Britto) Pull request description: While running mutation tests, I noticed a defect. `HrpFe32Iter` models the BIP 173 HRP expansion used for checksum input. In [BIP 173](https://en.bitcoin.it/wiki/BIP_0173), HRP expansion is: `[ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]` In `size_hint()`, the high branch already includes that single inserted separator element. Adding another +1 in the final sum overcounts the remaining length by one. This patch removes the extra +1 from the final min and max calculations and adds a test asserting that `size_hint()` matches the exact remaining length as the iterator is drained. This change only corrects iterator length accounting. It does not change the yielded sequence of field elements or checksum semantics. ACKs for top commit: apoelstra: ACK ca024f9; successfully ran local tests clarkmoody: utACK ca024f9 tcharding: ACK ca024f9 Tree-SHA512: 6542bf50f693f32a33fad62421702b2eefa486618106ad2b33260f7c3558e9aee261b06dff50d9457cb282aef05b70dadd0577c82a157f4c587519c5b82c0e4b
2 parents 9f486a5 + ca024f9 commit e4d615c

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/primitives/checksum.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ impl Iterator for HrpFe32Iter<'_> {
502502
None => (0, Some(0)),
503503
};
504504

505-
let min = high.0 + 1 + low.0;
506-
let max = high.1.zip(low.1).map(|(high, low)| high + 1 + low);
505+
let min = high.0 + low.0;
506+
let max = high.1.zip(low.1).map(|(high, low)| high + low);
507507

508508
(min, max)
509509
}
@@ -599,6 +599,19 @@ mod tests {
599599
#[cfg(feature = "std")]
600600
println!("{}", _s);
601601
}
602+
603+
#[test]
604+
fn hrp_fe32_iter_size_hint_matches_remaining_length() {
605+
let hrp = Hrp::parse_unchecked("ab");
606+
let mut iter = HrpFe32Iter::new(&hrp);
607+
608+
for remaining in (0..=(2 * hrp.len() + 1)).rev() {
609+
assert_eq!(iter.size_hint(), (remaining, Some(remaining)));
610+
if remaining > 0 {
611+
iter.next().expect("iterator has more elements");
612+
}
613+
}
614+
}
602615
}
603616

604617
#[cfg(bench)]

0 commit comments

Comments
 (0)