Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range proof and related gadgets optimization #147

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions algebra/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ impl LegendreSymbol {

#[derive(Debug)]
pub struct BitIterator<E> {
/// the element under consideration, given as a little
/// endian slice of u64.
t: E,
/// addresses the current bit, which is the one
/// according to `2^{n-1}`.
n: usize,
}

Expand All @@ -489,6 +493,8 @@ impl<E: AsRef<[u64]>> BitIterator<E> {
BitIterator { t, n }
}

/// Returns the bits of `s` in big endian order,
/// with leading zeroes dropped.
pub fn without_leading_zeros(s: E) -> impl Iterator<Item = bool> {
Self::new(s).skip_while(|b| !b)
}
Expand All @@ -501,10 +507,14 @@ impl<E: AsRef<[u64]>> Iterator for BitIterator<E> {
if self.n == 0 {
None
} else {
// the current bit corresponds to `2^{n-1}`
self.n -= 1;
// the limb of the current bit.
let part = self.n / 64;
// the exponent `n - 1 mod 64`, which corresponds to the current bit.
let bit = self.n - (64 * part);

// pick the current bit from the u64 limb.
Some(self.t.as_ref()[part] & (1 << bit) > 0)
}
}
Expand Down
Loading