mirror of
https://github.com/arnaucube/miden-crypto.git
synced 2026-01-12 00:51:29 +01:00
feat: merkle mountain range
This commit is contained in:
46
src/merkle/mmr/bit.rs
Normal file
46
src/merkle/mmr/bit.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
/// Iterate over the bits of a `usize` and yields the bit positions for the true bits.
|
||||
pub struct TrueBitPositionIterator {
|
||||
value: usize,
|
||||
}
|
||||
|
||||
impl TrueBitPositionIterator {
|
||||
pub fn new(value: usize) -> TrueBitPositionIterator {
|
||||
TrueBitPositionIterator { value }
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for TrueBitPositionIterator {
|
||||
type Item = u32;
|
||||
|
||||
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
|
||||
// trailing_zeros is computed with the intrinsic cttz. [Rust 1.67.0] x86 uses the `bsf`
|
||||
// instruction. AArch64 uses the `rbit clz` instructions.
|
||||
let zeros = self.value.trailing_zeros();
|
||||
|
||||
if zeros == usize::BITS {
|
||||
None
|
||||
} else {
|
||||
let bit_position = zeros;
|
||||
let mask = 1 << bit_position;
|
||||
self.value ^= mask;
|
||||
Some(bit_position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DoubleEndedIterator for TrueBitPositionIterator {
|
||||
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
|
||||
// trailing_zeros is computed with the intrinsic ctlz. [Rust 1.67.0] x86 uses the `bsr`
|
||||
// instruction. AArch64 uses the `clz` instruction.
|
||||
let zeros = self.value.leading_zeros();
|
||||
|
||||
if zeros == usize::BITS {
|
||||
None
|
||||
} else {
|
||||
let bit_position = usize::BITS - zeros - 1;
|
||||
let mask = 1 << bit_position;
|
||||
self.value ^= mask;
|
||||
Some(bit_position)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user