Struct number_theory::Mpz
source · pub struct Mpz { /* private fields */ }
Expand description
Arbitrary precision integer (Z)
Implementations§
source§impl Mpz
impl Mpz
sourcepub fn u_new(limbs: Vec<u64>) -> Self
pub fn u_new(limbs: Vec<u64>) -> Self
use number_theory::Mpz; // includes arbitrary-precision arithmetic
use number_theory::Sign; // allows sign
use number_theory::NumberTheory; // includes methods from NumberTheory trait
let bignum = Mpz::new(Sign::Positive, vec![5,5]);
let fortyfour = Mpz::from_u128(44u128);
let fortyfour_neg = Mpz::from_i128(-44i128);
let twopow = Mpz::from_u64(128);
assert_eq!("92233720368547758085", bignum.to_string());
assert_eq!("44", fortyfour.to_string());
assert_eq!("-44", fortyfour_neg.to_string());
assert_eq!("128", twopow.to_string());
Returns a positive x from a big-endian vector representation
sourcepub fn new(sign: Sign, limbs: Vec<u64>) -> Self
pub fn new(sign: Sign, limbs: Vec<u64>) -> Self
Returns a x from a big-endian vector representation and a sign
sourcepub fn unchecked_u_new(limbs: Vec<u64>) -> Self
pub fn unchecked_u_new(limbs: Vec<u64>) -> Self
Returns a positive x from a big-endian vector, this function does not eliminate extraneous leading zeros
sourcepub fn unchecked_new(sign: Sign, limbs: Vec<u64>) -> Self
pub fn unchecked_new(sign: Sign, limbs: Vec<u64>) -> Self
Returns a x from a big-endian vector and sign, this function does not eliminate extraneous leading zeros
sourcepub fn from_slice(sign: Sign, x: &[u64]) -> Self
pub fn from_slice(sign: Sign, x: &[u64]) -> Self
Returns x from a big_endian slice and sign
sourcepub fn set_limbs(len: usize, gen: fn() -> u64) -> Self
pub fn set_limbs(len: usize, gen: fn() -> u64) -> Self
Creates a integer from limbs generated by a function
sourcepub fn to_hex_string(&self) -> String
pub fn to_hex_string(&self) -> String
Converts n to a radix-16 string in linear time
sourcepub fn to_radix_vec(&self, radix: u64) -> Vec<u64>
pub fn to_radix_vec(&self, radix: u64) -> Vec<u64>
Returns the polynomial representation of self in the form of the coefficient vector. Here we see that 50620 to radix-127 is 3x^2 + 17x + 74. Accepts all radix in the interval 0;2^64-1.
use number_theory::Mpz;
let value = Mpz::from_u64(50620);
assert_eq!(value.to_radix_vec(127),vec![74,17,3])
sourcepub fn from_string(x: &str) -> Option<Self>
pub fn from_string(x: &str) -> Option<Self>
Conversion from radix-10^n string.
use number_theory::Mpz;
let num = "-3141592653589793238462643383279502884197169399375105820974944592307816406286".to_string();
let machine = Mpz::from_string(&num).unwrap();
assert_eq!(machine.to_string(),num)
sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Checks if n >= 0
sourcepub fn is_power_of_two(&self) -> bool
pub fn is_power_of_two(&self) -> bool
Checks if n in 2^Z
sourcepub fn set_bit(&mut self, k: usize)
pub fn set_bit(&mut self, k: usize)
Sets the bit at 2^k to 1, if it is already 1 then this does not change
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of 64-bit machine words used in this representation
sourcepub fn lead_digit(&self) -> u64
pub fn lead_digit(&self) -> u64
Returns the lead digit in 2^64 representation
sourcepub fn trailing_zeros(&self) -> u64
pub fn trailing_zeros(&self) -> u64
Returns k such that d*2^k = x
sourcepub fn leading_zeros(&self) -> u64
pub fn leading_zeros(&self) -> u64
Returns the number of extraneous zeros in bit representation
sourcepub fn bit_length(&self) -> u64
pub fn bit_length(&self) -> u64
Returns the number of bits used to represent x
sourcepub fn congruence_u64(&self, n: u64, c: u64) -> bool
pub fn congruence_u64(&self, n: u64, c: u64) -> bool
Checks if x mod n === c
sourcepub fn successor(&mut self)
pub fn successor(&mut self)
use number_theory::Mpz;
let mut one = Mpz::one();
one.successor(); //Applies the successor function ignoring sign
assert_eq!("2", one.to_string())
sourcepub fn inv_successor(&mut self)
pub fn inv_successor(&mut self)
Inverse successor function. Subtracts 1 ignoring sign
sourcepub fn eea(&self, other: &Self) -> (Self, Self, Self)
pub fn eea(&self, other: &Self) -> (Self, Self, Self)
Finite ring variant of Extended Euclidean algorithm, see trait definition of extended gcd
sourcepub fn sirp(infimum: u64, supremum: u64, modulo: u64, residue: u64) -> Self
pub fn sirp(infimum: u64, supremum: u64, modulo: u64, residue: u64) -> Self
Sequential Interval Residue Product, a generalization of the k-factorials
// see the sirp crate for greater extension of this functionality
use number_theory::Mpz;
let factorial_100 = Mpz::sirp(1,100,1,0);
// Even. Odd double factorial would be sirp(1,100,2,1)
let doublefact_100 = Mpz::sirp(1,100,2,0);
assert_eq!(
"3424322470251197624824643289520818597\
5118675053719198827915654463488000000000000",doublefact_100.to_string())
sourcepub fn cip(infimum: u64, supremum: u64, cond: fn(_: &u64) -> bool) -> Self
pub fn cip(infimum: u64, supremum: u64, cond: fn(_: &u64) -> bool) -> Self
Conditional Interval Product computes the product of integers satisfying an unary function. In this example the unary function is the primality function. In practice it can be any function that borrows a u64 and returns a boolean.
use number_theory::Mpz;
use number_theory::NumberTheory;
let primorial_25 = Mpz::cip(1,100,u64::is_prime);
source§impl Mpz
impl Mpz
sourcepub fn mut_and(&mut self, other: &Self)
pub fn mut_and(&mut self, other: &Self)
Performs bitwise AND operation between x and y storing the result in x
sourcepub fn mut_or(&mut self, other: &Self)
pub fn mut_or(&mut self, other: &Self)
Performs bitwise OR operation between x and y storing the result in x
sourcepub fn mut_xor(&mut self, other: &Self)
pub fn mut_xor(&mut self, other: &Self)
Performs bitwise XOR operation between x and y storing the result in x
sourcepub fn mut_addition(&mut self, other: Self)
pub fn mut_addition(&mut self, other: Self)
x+y stored in x
sourcepub fn ref_addition(&self, other: &Self) -> Self
pub fn ref_addition(&self, other: &Self) -> Self
Returns x+y
sourcepub fn mut_subtraction(&mut self, other: Self)
pub fn mut_subtraction(&mut self, other: Self)
x-y stored in x
sourcepub fn ref_subtraction(&self, other: &Self) -> Self
pub fn ref_subtraction(&self, other: &Self) -> Self
Returns x-y
sourcepub fn ref_product(&self, other: &Self) -> Self
pub fn ref_product(&self, other: &Self) -> Self
Returns x*y
sourcepub fn mut_product(&mut self, other: Self)
pub fn mut_product(&mut self, other: Self)
x*y stored in x
sourcepub fn mut_scale_add(&mut self, x: u64, y: u64)
pub fn mut_scale_add(&mut self, x: u64, y: u64)
Unsigned in-place multiply by x and add y
sourcepub fn ref_euclidean(&self, other: &Self) -> (Self, Self)
pub fn ref_euclidean(&self, other: &Self) -> (Self, Self)
Returns x/y and x mod y
source§impl Mpz
impl Mpz
sourcepub fn sprp_check(&self, n: usize) -> bool
pub fn sprp_check(&self, n: usize) -> bool
Performs n random base checks, can be combined with is_prime to strengthen it. As is_prime is equivalent to one random sprp check in the worst case, the function below has an error rate of less than 2^-64 in the worst case.
use number_theory::NumberTheory;
use number_theory::Mpz;
fn strong_check(x: &Mpz) -> bool{
if !x.is_prime(){
return false
}
x.sprp_check(31)
}
sourcepub fn is_sophie(&self) -> Option<Self>
pub fn is_sophie(&self) -> Option<Self>
Faster than naive evaluation of sophie prime, returns safe prime if true, otherwise None. As this uses is_prime, the accuracy matches that function exactly as the verification of the safe prime itself is deterministic and solely reliant on the accuracy of the verification of the sophie prime.
Trait Implementations§
source§impl NumberTheory for Mpz
impl NumberTheory for Mpz
source§fn sqrt(&self) -> (Self, Self)
fn sqrt(&self) -> (Self, Self)
Returns the integer component of sqrt(x)