Support unsigned integral square root computations.
This commit is contained in:
@@ -3,9 +3,11 @@ base_impls!(U192, 3);
|
||||
random_impls!(U192, UniformU192);
|
||||
multiply_impls!(U192, U384);
|
||||
shift_impls!(U192, 3);
|
||||
subtraction_impls!(U192, 3);
|
||||
conversion_impls!(U192, U256);
|
||||
conversion_impls!(U192, U384);
|
||||
conversion_impls!(U192, U1024);
|
||||
sqrt_impls!(U192);
|
||||
addition_impls!(U256, U320);
|
||||
base_impls!(U256, 4);
|
||||
random_impls!(U256, UniformU256);
|
||||
@@ -24,6 +26,7 @@ conversion_impls!(U256, U512);
|
||||
conversion_impls!(U256, U576);
|
||||
conversion_impls!(U256, U2048);
|
||||
conversion_impls!(U256, U3072);
|
||||
sqrt_impls!(U256);
|
||||
prime_gen_impls!(U256);
|
||||
addition_impls!(U320, U384);
|
||||
base_impls!(U320, 5);
|
||||
@@ -92,6 +95,7 @@ subtraction_impls!(U1024, 16);
|
||||
conversion_impls!(U1024, U1088);
|
||||
conversion_impls!(U1024, U2048);
|
||||
conversion_impls!(U1024, U2112);
|
||||
sqrt_impls!(U1024);
|
||||
prime_gen_impls!(U1024);
|
||||
addition_impls!(U1088, U1152);
|
||||
base_impls!(U1088, 17);
|
||||
@@ -152,6 +156,7 @@ subtraction_impls!(U2048, 32);
|
||||
conversion_impls!(U2048, U2112);
|
||||
conversion_impls!(U2048, U4096);
|
||||
conversion_impls!(U2048, U4160);
|
||||
sqrt_impls!(U2048);
|
||||
prime_gen_impls!(U2048);
|
||||
addition_impls!(U2112, U2176);
|
||||
base_impls!(U2112, 33);
|
||||
@@ -184,6 +189,7 @@ subtraction_impls!(U3072, 48);
|
||||
conversion_impls!(U3072, U3136);
|
||||
conversion_impls!(U3072, U6144);
|
||||
conversion_impls!(U3072, U6208);
|
||||
sqrt_impls!(U3072);
|
||||
addition_impls!(U3136, U3200);
|
||||
base_impls!(U3136, 49);
|
||||
random_impls!(U3136, UniformU3136);
|
||||
@@ -608,6 +614,7 @@ mod tests {
|
||||
use super::super::*;
|
||||
use testing::{build_test_path,run_test};
|
||||
|
||||
generate_sub_tests!(U192, u192);
|
||||
generate_sub_tests!(U256, u256);
|
||||
generate_sub_tests!(U320, u320);
|
||||
generate_sub_tests!(U512, u512);
|
||||
@@ -901,6 +908,16 @@ mod tests {
|
||||
generate_square_tests!(ignore U8192, u8192, U16384);
|
||||
generate_square_tests!(ignore U15360, u15360, U30720);
|
||||
}
|
||||
mod sqrt {
|
||||
use super::super::*;
|
||||
use testing::{build_test_path,run_test};
|
||||
|
||||
generate_sqrt_tests!(U192, u192);
|
||||
generate_sqrt_tests!(U256, u256);
|
||||
generate_sqrt_tests!(U1024, u1024);
|
||||
generate_sqrt_tests!(U2048, u2048);
|
||||
generate_sqrt_tests!(U3072, u3072);
|
||||
}
|
||||
mod barrett_modsq {
|
||||
use super::super::*;
|
||||
use testing::{build_test_path,run_test};
|
||||
|
||||
@@ -44,6 +44,8 @@ mod rand;
|
||||
#[macro_use]
|
||||
mod shifts;
|
||||
#[macro_use]
|
||||
mod sqrt;
|
||||
#[macro_use]
|
||||
mod square;
|
||||
#[macro_use]
|
||||
mod sub;
|
||||
@@ -56,6 +58,7 @@ pub use self::modmul::ModMul;
|
||||
pub use self::modsq::ModSquare;
|
||||
pub use self::primes::PrimeGen;
|
||||
pub use self::square::Square;
|
||||
pub use self::sqrt::SquareRoot;
|
||||
|
||||
pub(crate) use self::add::unsafe_addition;
|
||||
|
||||
|
||||
66
src/unsigned/sqrt.rs
Normal file
66
src/unsigned/sqrt.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
pub trait SquareRoot {
|
||||
/// Compute the integer square root of the given value. The integer square
|
||||
/// root is the value Z such that the real root R satisfies Z <= R < Z+1.
|
||||
fn sqrt(&self) -> Self;
|
||||
}
|
||||
|
||||
macro_rules! sqrt_impls
|
||||
{
|
||||
($name: ident) => {
|
||||
impl SquareRoot for $name {
|
||||
fn sqrt(&self) -> Self {
|
||||
let mut num = self.clone();
|
||||
let mut res = $name::zero();
|
||||
let mut bit = $name::from(1u64) << ($name::bit_length() - 2);
|
||||
|
||||
while bit > num {
|
||||
bit >>= 2;
|
||||
}
|
||||
|
||||
while !bit.is_zero() {
|
||||
let mut resbit = res.clone();
|
||||
|
||||
resbit += &bit;
|
||||
if num >= resbit {
|
||||
num -= resbit;
|
||||
res += &bit << 1;
|
||||
}
|
||||
|
||||
res >>= 1;
|
||||
bit >>= 2;
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
macro_rules! generate_sqrt_tests {
|
||||
($name: ident, $lname: ident) => {
|
||||
#[test]
|
||||
fn $lname() {
|
||||
generate_sqrt_tests!(body $name, $lname);
|
||||
}
|
||||
};
|
||||
(ignore $name: ident, $lname: ident) => {
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn $lname() {
|
||||
generate_sqrt_tests!(body $name, $lname);
|
||||
}
|
||||
};
|
||||
(body $name: ident, $lname: ident) => {
|
||||
let fname = build_test_path("sqrt", stringify!($name));
|
||||
run_test(fname.to_string(), 2, |case| {
|
||||
let (neg0, abytes) = case.get("a").unwrap();
|
||||
let (neg1, rbytes) = case.get("r").unwrap();
|
||||
assert!(!neg0 && !neg1);
|
||||
|
||||
let a = $name::from_bytes(abytes);
|
||||
let r = $name::from_bytes(rbytes);
|
||||
assert_eq!(r, a.sqrt());
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user