Add a bit length function that's handy for macros.

This commit is contained in:
2018-11-14 21:33:58 -05:00
parent 1a2f3aaa7a
commit 62e36d79cb
2 changed files with 10 additions and 1 deletions

View File

@@ -68,6 +68,10 @@ macro_rules! signed_impls {
$sname{ negative: false, value: $name::zero() } $sname{ negative: false, value: $name::zero() }
} }
fn bit_length() -> usize {
$name::bit_length()
}
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self.value.is_zero() self.value.is_zero()
} }

View File

@@ -8,6 +8,8 @@ pub trait CryptoNum {
fn is_even(&self) -> bool; fn is_even(&self) -> bool;
/// Test if the number is odd. /// Test if the number is odd.
fn is_odd(&self) -> bool; fn is_odd(&self) -> bool;
/// The size of this number in bits.
fn bit_length() -> usize;
/// Mask off the high parts of the number. In particular, it /// Mask off the high parts of the number. In particular, it
/// zeros the bits above (len * 64). /// zeros the bits above (len * 64).
fn mask(&mut self, len: usize); fn mask(&mut self, len: usize);
@@ -26,6 +28,10 @@ macro_rules! generate_base
$name{ value: [0; $size] } $name{ value: [0; $size] }
} }
fn bit_length() -> usize {
return $size * 64;
}
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
self.value.iter().all(|&x| x == 0) self.value.iter().all(|&x| x == 0)
} }
@@ -58,7 +64,6 @@ macro_rules! generate_base
} }
} }
#[cfg(test)]
impl fmt::Debug for $name { impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(stringify!($name))?; f.write_str(stringify!($name))?;