Fix the bits() implementation, and add is_multiple_of() and gcd().

This commit is contained in:
2018-05-05 19:42:21 -07:00
parent c34629aa47
commit 29a14b39e6

View File

@@ -1,5 +1,5 @@
use cryptonum::signed::SCN;
use num::{BigUint,ToPrimitive,Zero};
use num::{BigInt,BigUint,ToPrimitive,Zero};
use std::fmt;
use std::fmt::Write;
use std::cmp::Ordering;
@@ -24,7 +24,15 @@ impl UCN {
}
pub fn bits(&self) -> usize {
self.contents.len() * 64
let num_elems = self.contents.len();
let mut res = num_elems * 64;
if self.contents.len() > 0 {
assert!(self.contents[num_elems - 1] != 0);
res -= self.contents[num_elems - 1].leading_zeros() as usize;
}
res
}
pub fn is_zero(&self) -> bool {
@@ -47,6 +55,18 @@ impl UCN {
}
}
pub fn is_multiple_of(&self, other: &UCN) -> bool {
self % other == UCN::from(1u64)
}
pub fn gcd(&self, other: &UCN) -> UCN {
let a = SCN{ negative: false, value: self.clone() };
let b = SCN{ negative: false, value: other.clone() };
let (d,_,_) = a.egcd(b);
assert!(!d.negative);
d.value
}
fn clean(&mut self) {
loop {
match self.contents.pop() {