Skip to main content

Slapdash_PRNG_Creator_CPRNG_Engine

Struct Slapdash_PRNG_Creator_CPRNG_Engine 

Source
pub struct Slapdash_PRNG_Creator_CPRNG_Engine {}
Expand description

A PRNG creator that produces a Random_Generic instance using the CPRNG_Engine hash algorithm as its underlying engine.

This struct produces a pseudo-random number generator (PRNG) capable of generating both primitive unsigned integers (u8, u16, u32, u64, u128, usize) and BigUInt values. In other words, it is a factory struct for Random_Generic instances.

§Quick Start

Slapdash_PRNG_Creator_CPRNG_Engine is designed for non-cryptographic purposes. Unless you require a secure random number generator for cryptography, refer to the following examples.

§Example

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

Implementations§

Source§

impl Slapdash_PRNG_Creator_CPRNG_Engine

Source

pub fn create() -> Random_Generic<INSECURE_COUNT>

Creates a new Random_Generic object.

§Returns

It returns a new object of Random_Generic.

§Example 1 for Random_PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::Random_PRNG_Creator_BIG_KECCAK_1024;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = Random_PRNG_Creator_BIG_KECCAK_1024::create();
let num: U1024 = rand.random_with_msb_set_biguint();
println!("Random number = {}", num);
§For more examples,

click here

Examples found in repository?
examples/edu_examples.rs (line 153)
147fn small_rsa()
148{
149    println!("small_rsa");
150    use cryptocol::number::SmallUInt;
151    use cryptocol::random::Slapdash_PRNG_Creator;
152    
153    let mut slapdash = Slapdash_PRNG_Creator::create();
154    let mut prime1 = slapdash.random_u32();
155    let mut prime2 = slapdash.random_u32();
156
157    prime1.set_msb();
158    while !prime1.is_prime()
159    {
160        prime1 = slapdash.random_u32();
161        prime1.set_msb();
162    }
163
164    prime2.set_msb();
165    while !prime2.is_prime()
166    {
167        prime2 = slapdash.random_u32();
168        prime2.set_msb();
169    }
170
171    let modulo = prime1 as u64 * prime2 as u64;
172    println!("Prime 1 = {}", prime1);
173    println!("Prime 2 = {}", prime2);
174    println!("Modulo = {}", modulo);
175    let phi = (prime1 - 1) as u64 * (prime2 - 1) as u64;
176
177    let mut key1 = 2_u64;
178    let (mut one, mut key2, _) = key1.extended_gcd(phi);
179
180    while !one.is_one()
181    {
182        key1 += 1;
183        (one, key2, _) = key1.extended_gcd(phi);
184    }
185    if key2 > phi
186        { key2 = phi.wrapping_sub(0_u64.wrapping_sub(key2)); }
187    else
188        { key2 %= phi; }
189
190    println!("Public Key = {}", key1);
191    println!("Private Key = {}", key2);
192
193    let message = 3_u64;
194    let cipher = message.modular_pow(key1, modulo);
195    let recover = cipher.modular_pow(key2, modulo);
196    println!("Message = {}", message);
197    println!("Cipher = {}", cipher);
198    println!("Recover = {}", recover);
199
200    let product = key1.modular_mul(key2, phi);
201    println!("product = {} X {} (mod {}) = {}", key1, key2, phi, product);
202    println!("--------------------");
203}
Source

pub fn create_with_seeds(seed: u64, aux: u64) -> Random_Generic<INSECURE_COUNT>

Creates a new struct Random_Generic with two seeds of type u64.

§Arguments
  • seed is the seed number of the type u64.
  • aux is the seed number of the type u64.
§Returns

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use the method Creates_with_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 PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::PRNG_Creator_BIG_KECCAK_1024;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut rand = PRNG_Creator_BIG_KECCAK_1024::create_with_seeds(0, 0);
let num: U1024 = rand.random_with_msb_set_biguint();
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn create_with_seed_arrays( seed: [u64; 8], aux: [u64; 8], ) -> Random_Generic<INSECURE_COUNT>

Creates a new struct Random_Generic with two seed arrays of type u64.

§Arguments
  • seed is the seed array and is of [u64; 8].
  • aux is the seed array and is of [u64; 8].
§Returns

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use this method rather than the method create_with_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.

§Example 1 for PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::PRNG_Creator_BIG_KECCAK_1024;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let seed = [777777777777_u64, 10500872879054459758_u64, 12_u64, 555555555555_u64, 123456789_u64, 987654321_u64, 852648791354687_u64, 741258963_u64];
let aux = [789456123_u64, 15887751380961987625_u64, 789654123_u64, 5_u64, 9632587414_u64, 58976541235_u64, 9513574682_u64, 369258147_u64];
let mut rand = PRNG_Creator_BIG_KECCAK_1024::create_with_seed_arrays(seed, aux);
let num: U1024 = rand.random_with_msb_set_biguint();
§For more examples,

click here

Source

pub fn create_with_seed_collector( seed_collector: fn() -> [u64; 8], ) -> Random_Generic<INSECURE_COUNT>

Creates a new Random_Generic object with a seed collector function.

§Arguments

seed_collector is a seed collector function to collect seeds, and is of the type fn() -> [u64; 8].

§Returns

It returns a new object of Random_Generic.

§Example 1 for PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::PRNG_Creator_BIG_KECCAK_1024;
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 = PRNG_Creator_BIG_KECCAK_1024::create_with_seed_collector(seed_collector);
let num: U1024 = rand.random_with_msb_set_biguint();
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn create_with_seed_collector_seeds( seed_collector: fn() -> [u64; 8], seed: u64, aux: u64, ) -> Random_Generic<INSECURE_COUNT>

Creates a new struct Random_Generic with a seed collector function and two seeds of type u64.

§Arguments
  • seed_collector is a seed collector function to collect seeds, and is of the type fn() -> [u64; 8].
  • seed is the seed number of the type u64.
  • aux is the seed number of the type u64.
§Returns

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use the method create_with_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 PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::PRNG_Creator_BIG_KECCAK_1024;
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 = PRNG_Creator_BIG_KECCAK_1024::create_with_seed_collector_seeds(seed_collector, 0, 0);
let num: U1024 = rand.random_with_msb_set_biguint();
println!("Random number = {}", num);
§For more examples,

click here

Source

pub fn create_with_seed_collector_seed_arrays( seed_collector: fn() -> [u64; 8], seed: [u64; 8], aux: [u64; 8], ) -> Random_Generic<INSECURE_COUNT>

Creates a new struct Random_Generic with a seed collector function and two seed arrays of type u64.

§Arguments
  • 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].
§Returns

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use this method rather than the method create_with_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.

§Example 1 for PRNG_Creator_BIG_KECCAK_1024
use cryptocol::random::PRNG_Creator_BIG_KECCAK_1024;
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 = [777777777777_u64, 10500872879054459758_u64, 12_u64, 555555555555_u64, 123456789_u64, 987654321_u64, 852648791354687_u64, 741258963_u64];
let aux = [789456123_u64, 15887751380961987625_u64, 789654123_u64, 5_u64, 9632587414_u64, 58976541235_u64, 9513574682_u64, 369258147_u64];
let mut rand = PRNG_Creator_BIG_KECCAK_1024::create_with_seed_collector_seed_arrays(seed_collector, seed, aux);
let num: U1024 = rand.random_with_msb_set_biguint();
println!("Random number = {}", num);
§For more examples,

click here

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.