Struct Generator

Source
pub struct Generator;
Expand description

§Generator

This is the most commonly used struct. It is used to generate:

  • Large Unsigned Integers
  • Composite Numbers
  • Prime Numbers
  • Safe Primes
 
use num_primes::{Generator,Verification};
 
fn main(){
    let prime = Generator::new_prime(512);
    let _uint = Generator::new_uint(1024);
 
    // p = 2q + 1 || where p is safe prime
    let _safe_prime = Generator::safe_prime(64);
 
    let _ver: bool = Verification::is_prime(&prime);
}

Implementations§

Source§

impl Generator

Source

pub fn new_composite(n: usize) -> BigUint

§Generate Large Composite Numbers

This function guarantees the returned value is a composite number and is not prime.

use num_primes::Generator;
 
fn main(){
    // Generate Composite Number of 1024 bits
    let composite = Generator::new_composite(1024);
 
    // Print Out Composite Number
    println!("Composite Number: {}",composite);
}
Examples found in repository?
examples/new_composite.rs (line 5)
3fn main(){
4    // Generate Composite Number of 1024 bits
5    let composite = Generator::new_composite(1024);
6
7    // Print Out Composite Number
8    println!("Composite Number: {}",composite);
9}
Source

pub fn new_uint(n: usize) -> BigUint

§Generate Large Unsigned Integer

This function takes an input (n) for the number of bits the unsigned integer should be

use num_primes::Generator;

fn main() {
    // Generate Large Random Unsigned Integer of 1024 bits
    let x = Generator::new_uint(1024);
 
    // Print Integer
    println!("Large Unsigned Integer: {}",x);
}
Examples found in repository?
examples/new_unsigned_int.rs (line 5)
3fn main() {
4    // Generate Large Random Unsigned Integer of 1024 bits
5    let x = Generator::new_uint(1024);
6
7    // Print Integer
8    println!("Large Unsigned Integer: {}",x);
9}
More examples
Hide additional examples
examples/factorization.rs (line 5)
3fn main() {
4    // Generates New Unsighed Integer of 32 bits
5    let uint = Generator::new_uint(32);
6    
7    // Prime Factorization    
8    let factor = Factorization::prime_factor(uint);
9
10    match factor {
11        Some(factor) => println!("Largest Prime Factor: {}",factor),
12        None => println!("No Prime Factors Found"),
13    }
14}
Source

pub fn new_prime(n: usize) -> BigUint

§Generate Prime Number

This function generates a prime number of n-bits using three primality tests.

use num_primes::Generator;
 
fn main(){
    // Generate Two Primes (p,q) of 512 bits
    let p = Generator::new_prime(512);
    let q = Generator::new_prime(512);
 
    // Multiply p and q and return n
    let n = p * q;
 
    // Print n
    println!("n: {}",n);
}
Examples found in repository?
examples/new_prime.rs (line 5)
3fn main(){
4    // Generate Two Primes (p,q) of 512 bits
5    let p = Generator::new_prime(512);
6    let q = Generator::new_prime(512);
7
8    // Multiply p and q and return n
9    let _n = p * q;
10}
More examples
Hide additional examples
examples/verification.rs (line 4)
3fn main(){
4    let prime = Generator::new_prime(1024);
5    let safe = Generator::safe_prime(128);
6
7    let is_prime: bool = Verification::is_prime(&prime);
8    let is_safe_prime: bool = Verification::is_safe_prime(&safe);
9
10    assert_eq!(is_prime, true);
11    assert_eq!(is_safe_prime, true);
12}
Source

pub fn safe_prime(n: usize) -> BigUint

§Generate Safe Primes

This function will generate safe prime numbers, or numbers of the form p = 2q + 1 where p is the safe prime.

use num_primes::Generator;
 
fn main(){
    // p = 2q + 1 where p is the safe prime. This generates a number of 64 bits.
    let safe_prime = Generator::safe_prime(64);
}
Examples found in repository?
examples/safe_prime.rs (line 5)
3fn main(){
4    // p = 2q + 1 where p is the safe prime and q is a prime that fits the equation
5    let safe_prime = Generator::safe_prime(128);
6
7    // Prints safe prime and prime number
8    println!("safe prime: {}",safe_prime);
9}
More examples
Hide additional examples
examples/verification.rs (line 5)
3fn main(){
4    let prime = Generator::new_prime(1024);
5    let safe = Generator::safe_prime(128);
6
7    let is_prime: bool = Verification::is_prime(&prime);
8    let is_safe_prime: bool = Verification::is_safe_prime(&safe);
9
10    assert_eq!(is_prime, true);
11    assert_eq!(is_safe_prime, true);
12}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.