Add a bit length function that's handy for macros.
This commit is contained in:
@@ -67,6 +67,10 @@ macro_rules! signed_impls {
|
||||
fn zero() -> $sname {
|
||||
$sname{ negative: false, value: $name::zero() }
|
||||
}
|
||||
|
||||
fn bit_length() -> usize {
|
||||
$name::bit_length()
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
self.value.is_zero()
|
||||
|
||||
@@ -8,6 +8,8 @@ pub trait CryptoNum {
|
||||
fn is_even(&self) -> bool;
|
||||
/// Test if the number is odd.
|
||||
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
|
||||
/// zeros the bits above (len * 64).
|
||||
fn mask(&mut self, len: usize);
|
||||
@@ -26,6 +28,10 @@ macro_rules! generate_base
|
||||
$name{ value: [0; $size] }
|
||||
}
|
||||
|
||||
fn bit_length() -> usize {
|
||||
return $size * 64;
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
self.value.iter().all(|&x| x == 0)
|
||||
}
|
||||
@@ -58,7 +64,6 @@ macro_rules! generate_base
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl fmt::Debug for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(stringify!($name))?;
|
||||
|
||||
Reference in New Issue
Block a user