spec.rs: Add lebs2ip function bounded on const generic L.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
This commit is contained in:
therealyingtong 2021-06-13 23:19:12 +08:00
parent b299a51b31
commit b7b8126ccf
2 changed files with 13 additions and 1 deletions

View File

@ -14,7 +14,7 @@ mod constants;
mod sinsemilla_s;
pub use constants::*;
pub fn lebs2ip_k(bits: &[bool]) -> u32 {
fn lebs2ip_k(bits: &[bool]) -> u32 {
assert!(bits.len() == K);
bits.iter()
.enumerate()

View File

@ -179,6 +179,18 @@ pub(crate) fn extract_p_bottom(point: CtOption<pallas::Point>) -> CtOption<palla
point.map(|p| extract_p(&p))
}
/// The u64 integer represented by an L-bit little-endian bitstring.
///
/// Panics
///
/// Panics if the bitstring is longer than 64 bits.
pub fn lebs2ip<const L: usize>(bits: &[bool; L]) -> u64 {
assert!(L <= 64);
bits.iter()
.enumerate()
.fold(0u64, |acc, (i, b)| acc + if *b { 1 << i } else { 0 })
}
#[cfg(test)]
mod tests {
use group::Group;