diff --git a/src/signed/base.rs b/src/signed/base.rs index f8116f3..ae961e2 100644 --- a/src/signed/base.rs +++ b/src/signed/base.rs @@ -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() diff --git a/src/unsigned/base.rs b/src/unsigned/base.rs index b4793b7..a5d9b7d 100644 --- a/src/unsigned/base.rs +++ b/src/unsigned/base.rs @@ -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))?;