Skip to main content

Any_SHA3_512

Struct Any_SHA3_512 

Source
pub struct Any_SHA3_512 {}
Expand description

The struct Any_SHA3_512 that constructs the Random_Generic object for implementing a pseudo-random number generator both for primitive unsigned integers such as u8, u16, u32, u64, u128, and usize, and for BigUInt. The object which this Any_SHA3_512 constructs uses the hash algorithm Any_SHA3_512 as a pseudo-random number engine generator.

§QUICK START

You can use Any_SHA3_512 to create an if you use random number for cryptographic purpose. Any_SHA3_512 is for normal cryptographical purpose Look into the following examples.

§Example

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

Implementations§

Source§

impl Any_SHA3_512

Source

pub fn new() -> AnyGen

Constructs a new Random_Generic object.

§Output

It returns a new object of Random_Generic.

§Example 1
use cryptocol::random::Any_SHA3_512;
let mut any = Any_SHA3_512::new();
println!("Any number = {}", any.random_u64());
Source

pub fn new_with_seeds(seed: u64, aux: u64) -> Random_Generic

Constructs 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.
§Output

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use the method new_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
use cryptocol::random::Any_SHA3_512;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut any = Any_SHA3_512::new_with_seeds(u64::MAX, u64::MAX);
let num: U768 = any.random_odd_biguint();
println!("Any number = {}", num);
Source

pub fn new_with_seed_arrays(seed: [u64; 8], aux: [u64; 8]) -> AnyGen

Constructs 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].
§Output

It returns a new object of Random_Generic.

§Cryptographical Security

You are highly recommended to use this method rather than the method new_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
use cryptocol::random::Any_SHA3_512;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
 
let mut any = Any_SHA3_512::new_with_seeds(u64::MAX, u64::MAX);
let num: U768 = any.random_odd_biguint();
println!("Any number = {}", num);
Source

pub fn new_with_seed_collector(seed_collector: fn() -> [u64; 8]) -> AnyGen

Constructs 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].

§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.
§Example 1
use cryptocol::random::Any_SHA3_512;
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 any = Any_SHA3_512::new_with_seed_collector(seed_collector);
let num: U512 = any.random_odd_biguint();
println!("Random number = {}", num);
Source

pub fn new_with_seed_collector_seeds( seed_collector: fn() -> [u64; 8], seed: u64, aux: u64, ) -> AnyGen

Constructs 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.
§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.
§Cryptographical Security

You are highly recommended to use the method new_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
use cryptocol::random::Any_SHA3_512;
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 any = Any_SHA3_512::new_with_seed_collector_seeds(seed_collector, u64::MAX, u64::MAX);
let num: U768 = any.random_odd_biguint();
println!("Any number = {}", num);
Source

pub fn new_with_seed_collector_seed_arrays( seed_collector: fn() -> [u64; 8], seed: [u64; 8], aux: [u64; 8], ) -> AnyGen

Constructs 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].
§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.
§Cryptographical Security

You are highly recommended to use this method rather than the method new_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
use cryptocol::random::Any_SHA3_512;
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 = [12_u64, 123456789_u64, 852648791354687_u64, 10500872879054459758_u64, 987654321_u64, 555555555555_u64, 777777777777_u64, 741258963_u64];
let aux = [9513574682_u64, 9632587414_u64, 15887751380961987625_u64, 789456123_u64,58976541235_u64, 9513574682_u64, 369258147_u64, 789654123_u64, 5_u64];
let mut any = Any_SHA3_512::new_with_seed_collector_seed_arrays(seed_collector, seed, aux);
let num: U512 = any.random_odd_biguint();
println!("Random number = {}", num);

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.