Skip to main content

Random_Generic

Struct Random_Generic 

Source
pub struct Random_Generic<const COUNT: u128 = {u128::MAX}> { /* private fields */ }
Expand description

This generic struct Random_Generic is the struct for implementing a pseudo-random number generator both for primitive unsigned integers such as u8, u16, u32, u64, u128, and usize, and for BigUInt. Depending on the engine object, it uses hash algorithms or symmetric-key cryptographic algorithm to generate pseudo random numbers.

§Generic Parameters

  • COUNT: The length of the period of pseudo-random number sequence. If COUNT is 1000, a new seed will be used every 1000 pseudo-random numbers, for example. The default value of COUNT is u128::MAX.
  • NTR: How many bytes of the true random numbers to be used as a seed. If NTR is 32, thirty-two bytes of the true random number will be used as a seed. The default value of NTR is 64. NTR cannot exceed 64. Even if NTR is 100, only sixty-four bytes of the true random numbers will be used, for example.

§Feature

  • The generic parameter COUNT should be 1 ~ u128::MAX inclusively.
  • The default value of COUNT is 340282366920938463463374607431768211455 which is u128::MAX and far less than theoretical period of pseudo-random numbers recursively generated by 512-bit hash algorithms. `COUNT is the limited number of pseudo-random numbers generated from a seed.
  • After it generates pseudo-random numbers COUNT times, it finds a new seed from a system automatically and generates next pseudo-random numbers with a new seed. In other words, it uses new seeds every COUNT times of generation of pseudo-random numbers.

§Panics

If COUNT is 0, the constructor methods such as new() and new_with_seeds() will panic!

§Predetermined Provided Specific structs

  • Random_BIG_KECCAK_1024: uses a hash algorithm BIG_KECCAK_1024, and is cryptographically secure.

  • Random_SHA3_512: uses a hash algorithm SHA3_512, and is cryptographically secure and uses true random numbers far more frequently than Any_SHA3_512 does.

  • Random_SHA2_512: uses a hash algorithm SHA2_512, and is cryptographically secure and uses true random numbers far more frequently than Any_SHA2_512 does.

  • Any_SHAKE_256: uses a hash algorithm SHAKE_256, and is cryptographically secure.

  • Any_SHAKE_128: uses a hash algorithm SHAKE_128, and is cryptographically secure.

  • Any_SHA3_512: uses a hash algorithm SHA3_512, and is cryptographically secure and uses true random numbers far less often than Random_SHA3_512 does.

  • Any_SHA3_256: uses a hash algorithm SHA3_256, and is cryptographically secure and uses true random numbers far less often than Random_SHA3_256 does.

  • Any_SHA2_512: uses a hash algorithm SHA2_512, and is cryptographically secure and uses true random numbers far less often than Random_SHA2_512 does.

  • Any_SHA2_256: uses a hash algorithm SHA2_256, and is cryptographically secure.

  • Slapdash_SHA1: uses a hash algorithm SHA1, and is NOT cryptographically secure.

  • Slapdash_SHA0: uses a hash algorithm SHA0, and is NOT cryptographically secure.

  • Slapdash_Num_C: uses a pseudo-random number generator algorithm of the function rand() of C standard library, and is NOT cryptographically secure.

  • Slapdash_MD5: uses a hash algorithm MD5, and is NOT cryptographically secure.

  • Slapdash_MD4: uses a hash algorithm MD4, and is NOT cryptographically secure.

  • Random_Rijndael: uses a symmetric-key encryption algorithm Rijndael, and is cryptographically secure and uses true random numbers far more frequently than Any_Rijndael does..

  • Any_Rijndael: uses a symmetric-key encryption algorithm Rijndael, and is cryptographically secure and uses true random numbers far less often than Random_Rijndael does.

  • Slapdash_DES: uses a symmetric-key encryption algorithm DES, and is NOT cryptographically secure.

  • Random: a synonym of Random_BIG_KECCAK_1024 at the moment, and is cryptographically secure and uses true random numbers far more frequently than Any does.

  • Any: a synonym of Any_SHA2_512 at the moment, and is cryptographically secure and uses true random numbers far less often than Random does.

  • Slapdash: a synonym of Slapdash_Num_C at the moment, and is NOT cryptographically secure.

  • Random_* are cryptographically secure and uses true random numbers far more frequently than Any_* do.

  • Any_* are cryptographically secure and uses true random numbers far less often than Random_* do.

§QUICK START

You can use either struct Random or Any or Slapdash depending on your purpose. Random and Any are for cryptographical purpose while Slapdash` is for non-cryptographical purpose. Look into the following examples.

§Example

use cryptocol::number::BigUInt_Prime;
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = Random::new();
// let mut rand = Any::new();
println!("Random number = {}", rand.random_u128());
println!("Random number = {}", rand.random_u64());
println!("Random number = {}", rand.random_u32());
println!("Random number = {}", rand.random_u16());
println!("Random number = {}", rand.random_u8());
 
if let Some(num) = rand.random_under_uint(1234567890123456_u64)
    { println!("Random number u64 = {}", num); }
 
if let Some(num) = rand.random_minmax_uint(1234_u16, 6321)
    { println!("Random number u16 = {}", num); }
 
println!("Random odd number usize = {}", rand.random_odd_uint::<usize>());
if let Some(num) = rand.random_odd_under_uint(1234_u16)
    { println!("Random odd number u16 = {}", num); }
 
println!("Random 128-bit number u128 = {}", rand.random_with_msb_set_uint::<u128>());
println!("Random 16-bit odd number u16 = {}", rand.random_with_msb_set_uint::<u16>());
println!("Random prime number u64 = {}", rand.random_prime_using_miller_rabin_uint::<u64>(5));
println!("Random usize-sized prime number usize = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<usize>(5));
 
let num: [u128; 20] = rand.random_array();
for i in 0..20
    { println!("Random number {} => {}", i, num[i]); }
 
let mut num = [0_u64; 32];
rand.put_random_in_array(&mut num);
for i in 0..32
    { println!("Random number {} => {}", i, num[i]); }
 
let mut biguint: U512 = rand.random_biguint();
println!("Random Number: {}", biguint);
 
let mut ceiling = U1024::max().wrapping_div_uint(3_u8);
if let Some(r) = rand.random_under_biguint(&ceiling)
{
    println!("Random Number less than {} is\n{}", ceiling, r);
    assert!(r < ceiling);
}
 
ceiling = U1024::max().wrapping_div_uint(5_u8);
let r = rand.random_under_biguint_(&ceiling);
println!("Random Number less than {} is\n{}", ceiling, r);
assert!(r < ceiling);
 
ceiling = U1024::max().wrapping_div_uint(4_u8);
if let Some(r) = rand.random_odd_under_biguint(&ceiling)
{
    println!("Random odd Number less than {} is\n{}", ceiling, r);
    assert!(r < ceiling);
}
 
biguint = rand.random_with_msb_set_biguint();
println!("Random Number: {}", biguint);
 
biguint = rand.random_odd_with_msb_set_biguint();
println!("512-bit Random Odd Number = {}", biguint);
assert!(biguint > U512::halfmax());
assert!(biguint.is_odd());
 
biguint = rand.random_prime_using_miller_rabin_biguint(5);
println!("Random Prime Number = {}", biguint);
assert!(biguint.is_odd());
 
biguint = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("512-bit Random Prime Number = {}", biguint);
assert!(biguint.is_odd());

Implementations§

Source§

impl<const COUNT: u128> Random_Generic<COUNT>

Source

pub fn new_with<SG, AG>(main_generator: SG, aux_generator: AG) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
§Output

It returns a new object of Random_Generic.

§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for BIG_KECCAK_1024
use cryptocol::random::RandGen;
use cryptocol::hash::BIG_KECCAK_1024;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = RandGen::new_with(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new());
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn new_with_generators_seeds<SG, AG>( main_generator: SG, aux_generator: AG, seed: u64, aux: u64, ) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines and two seeds of type u64 given.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
  • seed is the seed number of u64.
  • aux is the seed number of u64.
§Output

It returns a new object of Random_Generic.

§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
  • You are highly recommended to use the method new_with_generators_seed_arrays() rather than this method for security reason. It is because the default seed collector function collects 1024 bits as a seed. If you use this method, it results that you give only ‘128’ bits (= ‘64’ bits + ‘64’ bits) as a seed and the other ‘896’ bits will be made out of the ‘128’ bits that you provided.
§Example 1 for BIG_KECCAK_1024
use cryptocol::random::RandGen;
use cryptocol::hash::BIG_KECCAK_1024;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = RandGen::new_with_generators_seeds(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new(), 10500872879054459758_u64, 15887751380961987625_u64);
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn new_with_generators_seed_arrays<SG, AG>( main_generator: SG, aux_generator: AG, seed: [u64; 8], aux: [u64; 8], ) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines and two seed arrays of type u64 given.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
  • seed is the seed array and is of [u64; 8].
  • aux is the seed array and is of [u64; 8].
§Output

It returns a new object of Random_Generic.

§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
  • You are highly recommended to use this method rather than the method new_with_generators_seeds for security reason. It is because the default seed collector function collects 1024 bits as a seed. If you use this method, it results that you give full ‘1024’ bits (= ‘64’ bits X ‘8’ X ‘2’) as a seed and it is equivalent to use a seed collector function.
§Example 1 for BIG_KECCAK_1024
use cryptocol::random::{ RandGen, AnyGen, SlapdashGen };
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
use cryptocol::hash::BIG_KECCAK_1024;
let seed = [10500872879054459758_u64, 14597581050087285790, 10790544591050087758, 17281050044597905758, 15900810579072854758, 10572800879059744558, 13758728710500905448, 15054105075808728459];
let aux = [10500054459758872879_u64, 15810500854459728790, 10790877585445910500, 10044597872810905758, 10579072855900814758, 14410572800879059558, 17105448597050095872, 18087279054105078459];
let mut rand = RandGen::new_with_generators_seed_arrays(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new(), seed, aux);
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn new_with_generators_seed_collector<SG, AG>( main_generator: SG, aux_generator: AG, seed_collector: fn() -> [u64; 8], ) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines and a seed collector method.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
  • seed_collector is a seed collector function to collect seeds, and is of the type fn() -> [u64; 8].
§Output

It returns a new object of Random_Generic.

§Features
  • The default seed collector function is provided in this module, but it is optimized for Unix/Linux though it works under Windows too.
  • If you use this crate under Windows and/or you have a better one, you can use your own seed collector function by replacing the default seed collector function with your own one.
§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for BIG_KECCAK_1024
use cryptocol::hash::BIG_KECCAK_1024;
use cryptocol::random::RandGen;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
fn seed_collector() -> [u64; 8]
{
    use std::time::{ SystemTime, UNIX_EPOCH };
    use cryptocol::number::LongerUnion;
 
    let ptr = seed_collector as *const fn() -> [u64; 8] as u64;
    let mut seed_buffer = [ptr; 8];
    for i in 0..8
        { seed_buffer[i] ^= ptr.swap_bytes().rotate_left(i as u32); }
 
    if let Ok(nanos) = SystemTime::now().duration_since(UNIX_EPOCH)
    {
        let common = LongerUnion::new_with(nanos.as_nanos());
        for i in 0..4
        {
            let j = i << 1;
            seed_buffer[j] = common.get_ulong_(0);
            seed_buffer[j + 1] = common.get_ulong_(1);
        }
    }
    seed_buffer
}
 
let mut rand = RandGen::new_with_generators_seed_collector(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new(), seed_collector);
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn new_with_generators_seed_collector_seeds<SG, AG>( main_generator: SG, aux_generator: AG, seed_collector: fn() -> [u64; 8], seed: u64, aux: u64, ) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines, a seed collector function, and two seeds of type u64 given.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
  • seed_collector is a seed collector function to collect seeds, and is of the type fn() -> [u64; 8].
  • seed is the seed number of u64.
  • aux is the seed number of u64.
§Output

It returns a new object of Random_Generic.

§Features
  • The default seed collector function is provided in this module, but it is optimized for Unix/Linux though it works under Windows too.
  • If you use this crate under Windows and/or you have a better one, you can use your own seed collector function by replacing the default seed collector function with your own one.
§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
  • You are highly recommended to use the method new_with_generators_seed_collector_seed_arrays() rather than this method for security reason. It is because the default seed collector function collects 1024 bits as a seed. If you use this method, it results that you give only ‘128’ bits (= ‘64’ bits + ‘64’ bits) as a seed and the other ‘896’ bits will be made out of the ‘128’ bits that you provided.
§Example 1 for BIG_KECCAK_1024
use cryptocol::hash::BIG_KECCAK_1024;
use cryptocol::random::RandGen;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
fn seed_collector() -> [u64; 8]
{
    use std::time::{ SystemTime, UNIX_EPOCH };
    use cryptocol::number::LongerUnion;
 
    let ptr = seed_collector as *const fn() -> [u64; 8] as u64;
    let mut seed_buffer = [ptr; 8];
    for i in 0..8
        { seed_buffer[i] ^= ptr.swap_bytes().rotate_left(i as u32); }
 
    if let Ok(nanos) = SystemTime::now().duration_since(UNIX_EPOCH)
    {
        let common = LongerUnion::new_with(nanos.as_nanos());
        for i in 0..4
        {
            let j = i << 1;
            seed_buffer[j] = common.get_ulong_(0);
            seed_buffer[j + 1] = common.get_ulong_(1);
        }
    }
    seed_buffer
}
 
let mut rand = RandGen::new_with_generators_seed_collector_seeds(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new(), seed_collector, 10500872879054459758_u64, 15887751380961987625_u64);
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn new_with_generators_seed_collector_seed_arrays<SG, AG>( main_generator: SG, aux_generator: AG, seed_collector: fn() -> [u64; 8], seed: [u64; 8], aux: [u64; 8], ) -> Self
where SG: Random_Engine + 'static, AG: Random_Engine + 'static,

Constructs a new Random_Generic object with two random number generator engines and two seed arrays of type u64.

§Arguments
  • main_generator is a main random number generator engine which is of Random_Engine-type and for generating main pseudo-random numbers.
  • aux_generator is an auxiliary random number generator engine which is of Random_Engine-type and for generating auxiliary pseudo-random numbers to use in generating the main pseudo-random numbers.
  • seed_collector is a seed collector function to collect seeds, and is of the type fn() -> [u64; 8].
  • seed is the seed array and is of [u64; 8].
  • aux is the seed array and is of [u64; 8].
§Output

It returns a new object of Random_Generic.

§Features
  • The default seed collector function is provided in this module, but it is optimized for Unix/Linux though it works under Windows too.
  • If you use this crate under Windows and/or you have a better one, you can use your own seed collector function by replacing the default seed collector function with your own one.
  • You are highly recommended to use this method rather than the method new_with_generators_seed_collector_seeds for security reason. It is because the default seed collector function collects 1024 bits as a seed. If you use this method, it results that you give full ‘1024’ bits (= ‘64’ bits X ‘8’ X ‘2’) as a seed and it is equivalent to use a seed collector function
§Panics

If COUNT is 0, this method will panic!

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for BIG_KECCAK_1024
use cryptocol::hash::BIG_KECCAK_1024;
use cryptocol::random::RandGen;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
fn seed_collector() -> [u64; 8]
{
    use std::time::{ SystemTime, UNIX_EPOCH };
    use cryptocol::number::LongerUnion;
 
    let ptr = seed_collector as *const fn() -> [u64; 8] as u64;
    let mut seed_buffer = [ptr; 8];
    for i in 0..8
        { seed_buffer[i] ^= ptr.swap_bytes().rotate_left(i as u32); }
 
    if let Ok(nanos) = SystemTime::now().duration_since(UNIX_EPOCH)
    {
        let common = LongerUnion::new_with(nanos.as_nanos());
        for i in 0..4
        {
            let j = i << 1;
            seed_buffer[j] = common.get_ulong_(0);
            seed_buffer[j + 1] = common.get_ulong_(1);
        }
    }
    seed_buffer
}
 
let seed = [10500872879054459758_u64, 14597581050087285790, 10790544591050087758, 17281050044597905758, 15900810579072854758, 10572800879059744558, 13758728710500905448, 15054105075808728459];
let aux = [10500054459758872879_u64, 15810500854459728790, 10790877585445910500, 10044597872810905758, 10579072855900814758, 14410572800879059558, 17105448597050095872, 18087279054105078459];
let mut rand = RandGen::new_with_generators_seed_collector_seed_arrays(BIG_KECCAK_1024::new(), BIG_KECCAK_1024::new(), seed_collector, seed, aux);
let num: U512 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn get_seed_collector(&self) -> fn() -> [u64; 8]

Returns the function pointer to the current seed collector function.

§Output

It returns the function pointer to the current seed collector function, and is of the type fn() -> [u64; 8].

§Example 1 for Random
use cryptocol::random::Random;
let rand = Random::new();
let seed = rand.get_seed_collector()();
print!("seed = ");
for i in 0..8
    { print!("{} ", seed[i]); }
§For more examples,

click here

Source

pub fn set_seed_collector(&mut self, seed_collector: fn() -> [u64; 8])

Replaces the default seed collector function with new seed collector function.

§Arguments

collect_seed is a new seed collector function to replace the default seed collector function, and is of the type fn() -> [u64; 8].

§Example 1 for Random
use cryptocol::random::Random;
fn seed_collector() -> [u64; 8]
{
    use std::time::{ SystemTime, UNIX_EPOCH };
    use cryptocol::number::LongerUnion;
 
    let ptr = seed_collector as *const fn() -> [u64; 8] as u64;
    let mut seed_buffer = [ptr; 8];
    for i in 0..8
        { seed_buffer[i] ^= ptr.swap_bytes().rotate_left(i as u32); }
 
    if let Ok(nanos) = SystemTime::now().duration_since(UNIX_EPOCH)
    {
        let common = LongerUnion::new_with(nanos.as_nanos());
        for i in 0..4
        {
            let j = i << 1;
            seed_buffer[j] = common.get_ulong_(0);
            seed_buffer[j + 1] = common.get_ulong_(1);
        }
    }
    seed_buffer
}
type Func = *const fn() -> [u64; 8];
 
let mut rand = Random::new();
rand.set_seed_collector(seed_collector);
assert_eq!(seed_collector as Func, rand.get_seed_collector() as Func);
println!("seed = {}", rand.random_u128());
§For more examples,

click here

Source

pub fn reset_seed_collector(&mut self)

Replace the current seed collector function with the default seed collector function.

§Features

After this method is performed, the previous seed collector function will be lost.

§Example 1 for Random
fn seed_collector() -> [u64; 8]
{
    [0_u64; 8]
}
type Func = *const fn() -> [u64; 8];
 
use cryptocol::random::Random;
let mut rand = Random::new();
let collector = rand.get_seed_collector();
rand.set_seed_collector(seed_collector);
assert_eq!(seed_collector as Func, rand.get_seed_collector() as Func);
rand.reset_seed_collector();
assert_eq!(collector as Func, rand.get_seed_collector() as Func);
§For more examples,

click here

Source

pub fn random_u8(&mut self) -> u8

Generates random numbers of type u8.

§Output

It returns a random number of type u8.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_u8()); }
§For more examples,

click here

Source

pub fn random_u16(&mut self) -> u16

Generates random numbers of type u16.

§Output

It returns a random number of type u16.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_u16()); }
§For more examples,

click here

Source

pub fn random_u32(&mut self) -> u32

Generates random numbers of type u32.

§Output

It returns a random number of type u32.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_u32()); }
§For more examples,

click here

Examples found in repository?
examples/edu_examples.rs (line 142)
137fn small_rsa()
138{
139    println!("small_rsa");
140
141    let mut slapdash = Slapdash::new();
142    let mut prime1 = slapdash.random_u32();
143    let mut prime2 = slapdash.random_u32();
144
145    prime1.set_msb();
146    while !prime1.is_prime()
147    {
148        prime1 = slapdash.random_u32();
149        prime1.set_msb();
150    }
151
152    prime2.set_msb();
153    while !prime2.is_prime()
154    {
155        prime2 = slapdash.random_u32();
156        prime2.set_msb();
157    }
158
159    let modulo = prime1 as u64 * prime2 as u64;
160    println!("Prime 1 = {}", prime1);
161    println!("Prime 2 = {}", prime2);
162    println!("Modulo = {}", modulo);
163    let phi = (prime1 - 1) as u64 * (prime2 - 1) as u64;
164
165    let mut key1 = 2_u64;
166    let (mut one, mut key2, _) = key1.extended_gcd(phi);
167
168    while !one.is_one()
169    {
170        key1 += 1;
171        (one, key2, _) = key1.extended_gcd(phi);
172    }
173    if key2 > phi
174        { key2 = phi.wrapping_sub(0_u64.wrapping_sub(key2)); }
175    else
176        { key2 %= phi; }
177
178    println!("Public Key = {}", key1);
179    println!("Private Key = {}", key2);
180
181    let message = 3_u64;
182    let cipher = message.modular_pow(key1, modulo);
183    let recover = cipher.modular_pow(key2, modulo);
184    println!("Message = {}", message);
185    println!("Cipher = {}", cipher);
186    println!("Recover = {}", recover);
187
188    let product = key1.modular_mul(key2, phi);
189    println!("product = {} X {} (mod {}) = {}", key1, key2, phi, product);
190    println!("--------------------");
191}
Source

pub fn random_u64(&mut self) -> u64

Generates random numbers of type u64.

§Output

It returns a random number of type u64.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_u64()); }
§For more examples,

click here

Source

pub fn random_u128(&mut self) -> u128

Generates random numbers of type u128.

§Output

It returns a random number of type u128.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_u128()); }
§For more examples,

click here

Source

pub fn random_usize(&mut self) -> usize

Generates random numbers of type usize.

§Output

It returns a random number of type usize.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_usize()); }
§For more examples,

click here

Source

pub fn random_uint<T>(&mut self) -> T
where T: SmallUInt,

Generates random numbers of type T.

§Output

It returns a random number of type T.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
for i in 0..10
    { println!("{} Random number (Random) = {}", i, rand.random_uint::<u8>()); }
§For more examples,

click here

Source

pub fn random_under_uint<T>(&mut self, ceiling: T) -> Option<T>
where T: SmallUInt,

Generates random numbers of type T less than ceiling.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of the type T.

§Output

It returns a random number of type T less than ceiling wrapped by enum Some of Option.

§Features

If ceiling is 0, it returns None. Otherwise, it returns a random number of type T wrapped by enum Some of Option.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
if let Some(num) = rand.random_under_uint(12_u8)
    { println!("Random number u8 = {}", num); }
§For more examples,

click here

Source

pub fn random_under_uint_<T>(&mut self, ceiling: T) -> T
where T: SmallUInt,

Generates random numbers of type T less than ceiling.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of the type T.

§Output

It returns a random number of type T less than ceiling.

§Panics

If ceiling is 0, it will panic.

§Caution

Use only when ceiling is not 0.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
let num = rand.random_under_uint_(12_u8);
println!("Random number u8 = {}", num);
§For more examples,

click here

Source

pub fn random_minmax_uint<T>(&mut self, from: T, ceiling: T) -> Option<T>
where T: SmallUInt,

Generates random numbers of type T less than ceiling exclusively and greater than or equal to from inclusively.

§Arguments
  • from is the lower limitation which the generated random number should be greater than or equal to, and is of the type T..
  • ceiling is the upper limitation which the generated random number should be less than, and is of the type T.
§Output

If ceiling is 0 or from is greater than or equal to ceiling, it returns a random number of type T less than ceiling and greater than or equal to from, and the returned random number is wrapped by enum Some of Option. Otherwise, it returns enum None of Option.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1
use cryptocol::random::Random;
let mut rand = Random::new();
if let Some(num) = rand.random_minmax_uint(12_u8, 21)
    { println!("Random number u8 = {}", num); }
§For more examples,

click here

Source

pub fn random_minmax_uint_<T>(&mut self, from: T, ceiling: T) -> T
where T: SmallUInt,

Generates random numbers of type T less than ceiling exclusively and greater than or equal to from inclusively.

§Arguments
  • from is the lower limitation which the generated random number should be greater than or equal to, and is of the type T..
  • ceiling is the upper limitation which the generated random number should be less than, and is of the type T.
§Output

It returns a random number of type T less than ceiling and greater than or equal to from.

§Panics

If ceiling is 0 or from is greater than or equal to ceiling, it will panic.

§Caution

Use this method only when you are sure that ceiling is not 0 and from is less than ceiling.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
let num = rand.;random_minmax_uint_(12_u8, 21)
println!("Random number u8 = {}", num);
§For more examples,

click here

Source

pub fn random_odd_uint<T>(&mut self) -> T
where T: SmallUInt,

Generates random odd numbers of type T.

§Output

It returns a random odd number of type T.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
println!("Random odd number u8 = {}", rand.random_odd_uint::<u8>());
println!("Random odd number u16 = {}", rand.random_odd_uint::<u16>());
println!("Random odd number u32 = {}", rand.random_odd_uint::<u32>());
println!("Random odd number u64 = {}", rand.random_odd_uint::<u64>());
println!("Random odd number u128 = {}", rand.random_odd_uint::<u128>());
println!("Random odd number usize = {}", rand.random_odd_uint::<usize>());
§For more examples,

click here

Source

pub fn random_odd_under_uint<T>(&mut self, ceiling: T) -> Option<T>
where T: SmallUInt,

Generates random odd numbers of type T less than ceiling.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of the type T.

§Output

It returns a random odd numbers of type T less than ceiling wrapped by enum Some of Option.

§Features

If ceiling is less than 2, it returns None. Otherwise, it returns a random odd numbers of type T wrapped by enum Some of Option.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
if let Some(num) = rand.random_odd_under_uint(12_u8)
    { println!("Random odd number u8 = {}", num); }
if let Some(num) = rand.random_odd_under_uint(1234_u16)
    { println!("Random odd number u16 = {}", num); }
if let Some(num) = rand.random_odd_under_uint(12345678_u32)
    { println!("Random odd number u32 = {}", num); }
if let Some(num) = rand.random_odd_under_uint(1234567890123456_u64)
    { println!("Random odd number u64 = {}", num); }
if let Some(num) = rand.random_odd_under_uint(12345678901234567890_u128)
    { println!("Random odd number u128 = {}", num); }
if let Some(num) = rand.random_odd_under_uint(123456789_usize)
    { println!("Random odd number usize = {}", num); }
if let Some(num) = rand.random_odd_under_uint(0_usize)
    { println!("Random odd number usize = {}", num); }
else
    { println!("No random unsigned odd number under 0!"); }
§For more examples,

click here

Source

pub fn random_odd_under_uint_<T>(&mut self, ceiling: T) -> T
where T: SmallUInt,

Generates random odd numbers of type T less than ceiling.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of the type T.

§Output

It returns a random odd numbers of type T less than ceiling.

§Panics

If ceiling is less than 2, it will panic.

§Caution

Use this method only when ceiling is greater than 1.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
 
let num = rand.random_odd_under_uint_(12_u8);
println!("Random odd number u8 = {}", num);
 
let num = rand.random_odd_under_uint_(1234_u16);
println!("Random odd number u16 = {}", num);
 
let num = rand.random_odd_under_uint_(12345678_u32);
println!("Random odd number u32 = {}", num);
 
let num = rand.random_odd_under_uint_(1234567890123456_u64);
println!("Random odd number u64 = {}", num);
 
let num = rand.random_odd_under_uint_(12345678901234567890_u128);
println!("Random odd number u128 = {}", num);
 
let num = rand.random_odd_under_uint_(123456789_usize);
println!("Random odd number usize = {}", num);
§For more examples,

click here

Source

pub fn random_with_msb_set_uint<T>(&mut self) -> T
where T: SmallUInt,

Generates random numbers of type T and of maximum size of T.

§Output

It returns random numbers of type T and of maximum size of T.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
println!("Random 8-bit number = {}", rand.random_with_msb_set_uint::<u8>());
println!("Random 16-bit number = {}", rand.random_with_msb_set_uint::<u16>());
println!("Random 32-bit number = {}", rand.random_with_msb_set_uint::<u32>());
println!("Random 64-bit number = {}", rand.random_with_msb_set_uint::<u64>());
println!("Random 128-bit number = {}", rand.random_with_msb_set_uint::<u128>());
println!("Random usize-sized number = {}", rand.random_with_msb_set_uint::<usize>());
§For more examples,

click here

Source

pub fn random_odd_with_msb_set_uint<T>(&mut self) -> T
where T: SmallUInt,

Generates random odd numbers of type T and of maximum size of T.

§Output

It returns random odd numbers of type T and of maximum size of T.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
println!("Random 8-bit odd number = {}", rand.random_odd_with_msb_set_uint::<u8>());
println!("Random 16-bit odd number = {}", rand.random_odd_with_msb_set_uint::<u16>());
println!("Random 32-bit odd number = {}", rand.random_odd_with_msb_set_uint::<u32>());
println!("Random 64-bit odd number = {}", rand.random_odd_with_msb_set_uint::<u64>());
println!("Random 128-bit odd number = {}", rand.random_odd_with_msb_set_uint::<u128>());
println!("Random usize-sized odd number = {}", rand.random_odd_with_msb_set_uint::<usize>());
§For more examples,

click here

Source

pub fn random_prime_using_miller_rabin_uint<T>( &mut self, repetition: usize, ) -> T
where T: SmallUInt,

Returns a random prime number.

§Argument

The argument repetition defines how many times it tests whether or not the generated random number is prime. Usually, repetition is given to be 5 to have 99.9% hit rate.

§Output

A random prime number whose range is from 2 up to T::max() inclusively.

§Features
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in notprime number, the probability that the tested number is not a prime number is 1/4 (= 25%). So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4 = 6.25%). Therefore, if you test any number 5 times and they all result in a prime number, the probability that the tested number is not a prime number is 1/1024 = (= 1/4 * 1/4 * 1/4 * 1/4 * 1/4 = 0.09765625%). In other words, it is about 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods

If you want to use a (sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_uint() rather than this method.

§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
println!("Random 8-bit prime number = {}", rand.random_prime_using_miller_rabin_uint::<u8>(5));
println!("Random 16-bit prime number = {}", rand.random_prime_using_miller_rabin_uint::<u16>(5));
println!("Random 32-bit prime number = {}", rand.random_prime_using_miller_rabin_uint::<u32>(5));
println!("Random 64-bit prime number = {}", rand.random_prime_using_miller_rabin_uint::<u64>(5));
println!("Random 128-bit prime number = {}", rand.random_prime_using_miller_rabin_uint::<u128>(5));
println!("Random usize-sized prime number = {}", rand.random_prime_using_miller_rabin_uint::<usize>(5));
§For more examples,

click here

Source

pub fn random_prime_with_msb_set_using_miller_rabin_uint<T>( &mut self, repetition: usize, ) -> T
where T: SmallUInt,

Returns a full-sized random prime number, which is its MSB (Most Segnificant Bit) is set 1.

§Argument

The argument repetition defines how many times it tests whether or not the generated random number is prime. Usually, repetition is given to be 5 to have 99.9% hit rate.

§Output

A full-sized random prime number, which is its MSB (Most Segnificant Bit) is set 1 and whose range is from 2 up to T::max() inclusively.

§Features
  • This method generates a random number, and then simply sets its MSB (Most Significant Bit) to be one, and then checks whether or not the generated random number is prime number, and then it repeats until it will generate a prime number.
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in notprime number, the probability that the tested number is not a prime number is 1/4 (= 25%). So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4 = 6.25%). Therefore, if you test any number 5 times and they all result in a prime number, the probability that the tested number is not a prime number is 1/1024 = (= 1/4 * 1/4 * 1/4 * 1/4 * 1/4 = 0.09765625%). In other words, it is about 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
println!("Random 8-bit prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<u8>(5));
println!("Random 16-bit prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<u16>(5));
println!("Random 32-bit prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<u32>(5));
println!("Random 64-bit prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<u64>(5));
println!("Random 128-bit prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<u128>(5));
println!("Random usize-sized prime number = {}", rand.random_prime_with_msb_set_using_miller_rabin_uint::<usize>(5));
§For more examples,

click here

Source

pub fn random_array<T, const N: usize>(&mut self) -> [T; N]
where T: SmallUInt,

Returns random number array [T; N].

§Output

A random number array [T; N] each element of which has a range from 0 up to T::max() inclusively for both ends.

§Features

The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods

If you want random BigUInt, you are highly recommended to use the method random_biguint() rather than this method.

§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
let num: [u128; 5] = rand.random_array();
for i in 0..5
    { println!("Random number {} => {}", i, num[i]); }
§For more examples,

click here

Source

pub fn put_random_in_array<T, const N: usize>(&mut self, out: &mut [T; N])
where T: SmallUInt,

Puts random number array [T; N] in out.

§Argument

out is a random number array [T; N] each element of which has a range from 0 up to T::max() inclusively for both ends.

§Features

The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods

If you want random BigUInt, you are highly recommended to use the method random_biguint() rather than this method.

§Example 1 for Random
use cryptocol::random::Random;
let mut rand = Random::new();
let mut num = [0_u128; 5];
rand.put_random_in_array(&mut num);
for i in 0..5
    { println!("Random number {} => {}", i, num[i]); }
§For more examples,

click here

Source

pub fn random_biguint<T, const N: usize>(&mut self) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random value.

§Output

A random number whose range is from 0 up to BigUInt::max() inclusively for both ends.

§Features

The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u128);
 
let mut rand = Random::new();
let biguint: U256 = rand.random_biguint();
println!("Random Number: {}", biguint);
§For more examples,

click here

Source

pub fn random_under_biguint<T, const N: usize>( &mut self, ceiling: &BigUInt<T, N>, ) -> Option<BigUInt<T, N>>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random value less than a certain value, wrapped by enum Some of Option.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of type &BigUInt<T, N>.

§Output

A random number wrapped by enum Some of Option, whose range is between 0 inclusively and the certain value exclusively when ceiling is not zero. If ceiling is zero, None will be returned.

§Features
  • This method generates a random number, and then simply divides it by the certain value to get its remainder.
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = Random::new();
let ceiling = U16384::max().wrapping_div_uint(3_u8);
if let Some(r) = rand.random_under_biguint(&ceiling)
{
    println!("Random Number less than {} is\n{}", ceiling, r);
    assert!(r < ceiling);
}
§For more examples,

click here

Source

pub fn random_under_biguint_<T, const N: usize>( &mut self, ceiling: &BigUInt<T, N>, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random value less than a certain value.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of type &BigUInt<T, N>.

§Output

The random number whose range is between 0 inclusively and the certain value exclusively.

§Features
  • This method generates a random number, and then simply divides it by the certain value to get its remainder.
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Panics

If ceiling is zero, this method will panic.

§Caution

Use this method only when you are sure that ceiling is not zero.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u32);
 
let mut rand = Random::new();
let ceiling = U16384::max().wrapping_div_uint(3_u8);
let r = rand.random_under_biguint_(&ceiling);
println!("Random Number less than {} is\n{}", ceiling, r);
assert!(r < ceiling);
§For more examples,

click here

Source

pub fn random_odd_biguint<T, const N: usize>(&mut self) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random odd value.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of type &BigUInt<T, N>.

§Output

The random number that this method any_odd() returns is a pure random odd number whose range is from 1 up to BigUInt::max() inclusively for both ends.

§Features
  • This method generates a random number, and then simply set the LSB (Least Significant Bit).
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u16);
 
let mut rand = Random::new();
let r: U16384 = rand.random_odd_biguint();
println!("Random odd number is {}.", r);
assert!(r.is_odd());
§For more examples,

click here

Source

pub fn random_odd_under_biguint<T, const N: usize>( &mut self, ceiling: &BigUInt<T, N>, ) -> Option<BigUInt<T, N>>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random odd value less than a certain value, wrapped by enum Some of Option.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of type &BigUInt<T, N>.

§Output

The random odd number whose range is between 0 inclusively and the certain value exclusively, wrapped by enum Some of Option if ceiling is not zero. If ceiling is zero, None will be returned.

§Features
  • This method generates a random number, and then simply divides it by the certain value to get its remainder and then simply set the LSB (Least Significant Bit).
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u8);
 
let mut rand = Random::new();
let ceiling = U16384::max().wrapping_div_uint(3_u8);
if let Some(r) = rand.random_odd_under_biguint(&ceiling)
{
    println!("Random odd number less than {} is\n{}", ceiling, r);
    assert!(r < ceiling);
    assert!(r.is_odd());
}
§For more examples,

click here

Source

pub fn random_odd_under_biguint_<T, const N: usize>( &mut self, ceiling: &BigUInt<T, N>, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random odd value less than a certain value.

§Argument

The argument ceiling is the upper limitation which the generated random number should be less than, and is of type &BigUInt<T, N>.

§Output

The random odd number whose range is between 0 inclusively and the certain value exclusively.

§Features
  • This method generates a random number, and then simply divides it by the certain value to get its remainder and then simply set the LSB (Least Significant Bit).
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Panics

If ceiling is zero, this method will panic.

§Caution

Use this method only when you are sure that ceiling is not zero.

§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u128);
 
let mut rand = Random::new();
let ceiling = U16384::max().wrapping_div_uint(3_u8);
let r = rand.random_odd_under_biguint_(&ceiling);
println!("Random odd number less than {} is\n{}", ceiling, r);
assert!(r < ceiling);
assert!(r.is_odd());
§For more examples,

click here

Source

pub fn random_with_msb_set_biguint<T, const N: usize>( &mut self, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random value with (N * sizeof::<T>() * 8)-bit length.

§Output

The random number whose range is from !(BigUInt::max() >> 1) up to BigUInt::max() inclusively.

§Features
  • This method generates a random number, and then simply set the MSB (Most Significant Bit).
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = Random::new();
let r: u64384 = rand.random_with_msb_set_biguint();
println!("Random number is {}.", r);
assert!(r > u64384::halfmax());
§For more examples,

click here

Source

pub fn random_odd_with_msb_set_biguint<T, const N: usize>( &mut self, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which has the random odd value with (N * sizeof::<T>() * 8)-bit length

§Output

The random number that this method random_odd_with_msb_set() returns is a random odd number whose range is from !(BigUInt::max() >> 1) + 1 up to BigUInt::max() inclusively.

§Features
  • This method generates a random number, and then simply set the MSB (Most Significant Bit) and LSB (Least Significant Bit).
  • The random numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u32);
 
let mut rand = Random::new();
let r: U16384 = rand.random_odd_with_msb_set_biguint();
println!("Random number is {}.", r);
assert!(r > U16384::halfmax());
assert!(r.is_odd());
§For more examples,

click here

Source

pub fn random_prime_using_miller_rabin_biguint<T, const N: usize>( &mut self, repetition: usize, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which represents a random prime number.

§Argument

The argument repetition defines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.

§Output

The random prime number that this method random_prime_Miller_Rabin() returns is a random prime number whose range is from 2 up to BigUInt::max() inclusively.

§Features
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random prime number, you are highly recommended to use the method random_prime_with_msb_set_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u16);
 
let mut rand = Random::new();
let prime: U256 = rand.random_prime_using_miller_rabin_biguint(5);
println!("Random prime number: {}", prime);
§For more examples,

click here

Source

pub fn random_prime_with_msb_set_using_miller_rabin_biguint<T, const N: usize>( &mut self, repetition: usize, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which represents a random prime number of full-size of BigUInt<T, N>.

§Argument

The argument repetition defines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.

§Output

The random prime numbers which ranges from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features
  • This method uses concurrency.
  • This method generates a random number, and then simply sets its MSB (Most Significant Bit) to be one, and then checks whether or not the generated random number is prime number, and then it repeats until it will generate a prime number.
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u8);
 
let mut rand = Random::new();
let prime: U256 = rand.random_prime_with_msb_set_using_miller_rabin_biguint(5);
println!("Random prime number: {}", prime);
§For more examples,

click here

Source

pub fn random_prime_with_msb_set_using_miller_rabin_biguint_sequentially<T, const N: usize>( &mut self, repetition: usize, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which represents a random prime number of full-size of BigUInt<T, N>.

§Argument

The argument repetition defines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.

§Output

The random prime numbers which ranges from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features
  • This method does not use concurrency.
  • This method generates a random number, and then simply sets its MSB (Most Significant Bit) to be one, and then checks whether or not the generated random number is prime number, and then it repeats until it will generate a prime number.
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u8);
 
let mut rand = Random::new();
let prime: U256 = rand.random_prime_with_msb_set_using_miller_rabin_biguint_sequentially(5);
println!("Random prime number: {}", prime);
§For more examples,

click here

Source

pub fn random_primes_with_msb_set_using_miller_rabin_biguint<T, const N: usize>( &mut self, repetition: usize, how_many: usize, ) -> Vec<BigUInt<T, N>>
where T: SmallUInt,

Returns a collection of new BigUInt<T, N>-type objects which represent random prime numbers of full-size of BigUInt<T, N>.

§Argument
  • repetition: determines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.
  • how_many: determines how many prime numbers this method returns.
§Output

The random prime number which ranges from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features
  • This method generates several threads, each of which checks whether or not the given random number is prime number, and then it repeats until it will find how_many prime numbers.
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
Source

pub fn random_prime_with_half_length_using_miller_rabin_biguint<T, const N: usize>( &mut self, repetition: usize, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which represents a N * T::size_in_bits() / 2-bit random prime number.

§Argument

The argument repetition defines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.

§Output

This method returns a random prime number whose length is N * T::size_in_bits() / 2 bits.

§Features
  • This method generates a random number of half length, and then simply sets all the bytes from N * T::size_in_bits() / 2-th bit to N * T::size_in_bits() - 1-th bit to be zero, and then checks whether or not the generated random number is prime number, and then it repeats until it will generate a prime number. Here, 0-th bit is LSB (Least Significant Bit) and N * T::size_in_bits() - 1-th bit is MSB (Most Significant Bit).
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
§Cryptographical Security
  • If you use either Random_* or Any_*, it is considered to be cryptographically secure.
  • If you use Slapdash_*, it is considered that it may be cryptographically insecure.
§Counterpart Methods
  • If you want to use a normal random number, you are highly recommended to use the method random_biguint() rather than this method.
  • If you want to use a random number less than a certain value, you are highly recommended to use the method random_under_biguint() rather than this method.
  • If you want to use a random odd number, you are highly recommended to use the method random_odd_biguint() rather than this method.
  • If you want to use a random odd number less than a certain value, you are highly recommended to use the method ranodm_odd_under_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random number, you are highly recommended to use the method random_with_msb_set_biguint() rather than this method.
  • If you want to use a (N * sizeof::<T>() * 8)-bit long random odd number, you are highly recommended to use the method random_odd_with_msb_set_biguint() rather than this method.
  • If you want to use a normal random prime number, you are highly recommended to use the method random_prime_using_miller_rabin_biguint() rather than this method.
§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u128);
 
let mut rand = Random::new();
let prime: U256 = rand.random_prime_with_half_length_using_miller_rabin_biguint(5);
println!("Random prime number: {}", prime);
§For more examples,

click here

Source

pub fn random_primes_with_half_length_using_miller_rabin_biguint<T, const N: usize>( &mut self, repetition: usize, how_many: usize, ) -> Vec<BigUInt<T, N>>
where T: SmallUInt,

Returns a collection of new BigUInt<T, N>-type objects which represent random prime numbers of half-size of BigUInt<T, N>.

§Argument
  • repetition: determines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.
  • how_many: determines how many prime numbers this method returns.
§Output

The random prime number which ranges from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features
  • This method generates several threads, each of which checks whether or not the given random number is prime number, and then it repeats until it will find how_many prime numbers.
  • It uses Miller Rabin algorithm.
  • If this test results in composite number, the tested number is surely a composite number. If this test results in prime number, the probability that the tested number is not a prime number is 1/4. So, if the test results in prime number twice, the probability that the tested number is not a prime number is 1/16 (= 1/4 * 1/4). Therefore, if you test any number 5 times and they all result in a prime number, it is 99.9% that the number is a prime number.
  • The random prime numbers that may or may not be cryptographically secure depending on what pseudo-random number generator is used.
Source

pub fn random_prime_with_half_length_using_rsa_biguint<T, const N: usize>( &mut self, repetition: usize, ) -> (BigUInt<T, N>, BigUInt<T, N>)
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object which represents a random prime number.

§Argument

The argument repetition defines how many times it tests whether the generated random number is prime. Usually, repetition is given to be 5 for 99.9% accuracy or 7 for 99.99% accuracy.

§Output

The tuple of the two random prime numbers that this method returns is the tuple of the two random prime number each of which ranges from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features

This method uses Millar-Rabin algorithm and then determines whether or not the generated random prime number candidate is really prime with the RSA test. If it fails, this method repeats searching for another ranmdom prime candidate until this method finds the true random prime number, and return it.

§Example
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut prng = Random::new();
let (prime1, prime2): (U1024, U1024) = prng.random_prime_with_half_length_using_rsa_biguint(7);
let (prime1, prime2): (U512, U512) = (prime1.into_biguint(), prime2.into_biguint());
println!("U512 Prime number: {}", prime1);
println!("U512 Prime number: {}", prime2);
Source

pub fn prepared_random_prime_with_msb_set<T, const N: usize>( &mut self, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object out of the prepared prime number pool, which represents a prime number of full-size of BigUInt<T, N>.

§Output

This method returns a prime number randomly chosen out of the prepared prime number pool consisting of many prime numbers, whose range is from BigUInt::halfmax() up to BigUInt::max() inclusively.

§Features
  • This method chooses a prime number ramdomly out of the prepared prime number pool consisting of many prime numbers whose MSB (Most Significant Bit) is set to be one.
  • It uses a prime number pool.
§Caution and Cryptographical Security

The source code of this method is openly publicised. It means that anyone can have the full familiarity of the prime number pool which this method uses. So, you are NOT encouraged to use this method for serious cryptographical purposes. You can use this method temporarily for testing or debugging purposes because of the slowness of the Millar-Rabin algorithm for big numbers.

§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = Random::new();
let biguint: U256 = rand.prepared_random_prime_with_msb_set();
println!("Random Number: {}", biguint);
§For more examples,

click here

Source

pub fn prepared_random_prime_with_half_length<T, const N: usize>( &mut self, ) -> BigUInt<T, N>
where T: SmallUInt,

Constucts a new BigUInt<T, N>-type object out of the prepared prime number pool, which represents a prime number of half-size of BigUInt<T, N>.

§Output

This method returns a prime number randomly chosen out of the prepared prime number pool consisting of many prime numbers, whose length is N * T::size_in_bits() / 2 bits.

§Features
  • This method chooses a prime number ramdomly out of the prepared prime number pool consisting of many prime numbers whose length is N * T::size_in_bits() / 2 bits.
  • It uses a prime number pool.
§Caution and Cryptographical Security

The source code of this method is openly publicised. It means that anyone can have the full familiarity of the prime number pool which this method uses. So, you are NOT encouraged to use this method for serious cryptographical purposes. You can use this method temporarily for testing or debugging purposes because of the slowness of the Millar-Rabin algorithm for big numbers.

§Example 1 for Random
use cryptocol::random::Random;
use cryptocol::define_utypes_with;
define_utypes_with!(u32);
 
let mut rand = Random::new();
let biguint: U256 = rand.prepared_random_prime_with_half_length();
println!("Random Number: {}", biguint);
§For more examples,

click here

Source

pub fn prepared_random_prime_numbers() -> &'static [[&'static str; 100]; 5]

Returns a refenece to the prepared prime number pool, which represents a two-dimensional array prime numbers.

§Output

A refenece to the prepared prime number pool, which represents a two-dimensional array prime number strings.

§Features
  • The zeroth one-dimensional array contains 4096-bit prime number array.
  • The first one-dimensional array contains 2048-bit prime number array.
  • The second one-dimensional array contains 1024-bit prime number array.
  • The third one-dimensional array contains 512-bit prime number array.
  • The fourth one-dimensional array contains 256-bit prime number array.
Source

pub fn shuffle<T>(&mut self, slice: &mut [T])

Shuffles an array slice.

Auto Trait Implementations§

§

impl<const COUNT: u128> Freeze for Random_Generic<COUNT>

§

impl<const COUNT: u128 = {u128::MAX}> !RefUnwindSafe for Random_Generic<COUNT>

§

impl<const COUNT: u128 = {u128::MAX}> !Send for Random_Generic<COUNT>

§

impl<const COUNT: u128 = {u128::MAX}> !Sync for Random_Generic<COUNT>

§

impl<const COUNT: u128> Unpin for Random_Generic<COUNT>

§

impl<const COUNT: u128> UnsafeUnpin for Random_Generic<COUNT>

§

impl<const COUNT: u128 = {u128::MAX}> !UnwindSafe for Random_Generic<COUNT>

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.