pub struct Slapdash_Num_C {}Expand description
The struct Slapdash_Num_C 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 Slapdash_Num_C constructs
uses a pseudo-random number generator algorithm of rand() of C standard
library but it is still cryptographically not secure enough.
It is for non-cryptographic purpose. So, normally it is OK to use this
struct Slapdash_Num_C to create an object of pseudo-random number
generator. However, DO NOT USE THIS STRUCT FOR SERIOUS CRYPTOGRAPHIC
PURPOSE because it does not guarrantee the cryptographic security.
§QUICK START
You can use Slapdash_Num_C to create an if you use random number for
non-cryptographic purpose. Slapdash_Num_C is for normal
non-cryptographical purpose Look into the following examples.
§Example
use cryptocol::random::Slapdash_Num_C;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
let mut slapdash = Slapdash_Num_C::new();
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_Num_C
impl Slapdash_Num_C
Sourcepub fn new() -> Random_Generic<{ _ }>
pub fn new() -> Random_Generic<{ _ }>
Constructs a new Random_Generic object.
§Output
It returns a new object of Random_Generic.
§Example 1 for Slapdash_Num_C
use cryptocol::random::Slapdash_Num_C;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
let mut slapdash = Slapdash_Num_C::new();
println!("Slapdash number = {}", slapdash.random_usize());§Example 2 for Slapdash
use cryptocol::random::Slapdash;
use cryptocol::define_utypes_with;
define_utypes_with!(u64);
let mut slapdash = Slapdash::new();
println!("Slapdash number = {}", slapdash.random_u8());Examples found in repository?
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}Sourcepub fn new_with_seeds(seed: u64, aux: u64) -> Random_Generic<{ _ }>
pub fn new_with_seeds(seed: u64, aux: u64) -> Random_Generic<{ _ }>
Constructs a new struct Random_Generic with two seeds of type u64.
§Arguments
seedis the seed number of the typeu64.auxis the seed number of the typeu64.
§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 for Slapdash_Num_C
use cryptocol::random::Slapdash_Num_C;
let mut slapdash = Slapdash_Num_C::new_with_seeds(458861005, 793621585);
println!("Slapdash number = {}", slapdash.random_u64());§Example 2 for Slapdash
use cryptocol::random::Slapdash;
let mut slapdash = Slapdash::new_with_seeds(50558, 18782);
println!("Slapdash number = {}", slapdash.random_u32());Sourcepub fn new_with_seed_arrays(
seed: [u64; 8],
aux: [u64; 8],
) -> Random_Generic<{ _ }>
pub fn new_with_seed_arrays( seed: [u64; 8], aux: [u64; 8], ) -> Random_Generic<{ _ }>
Constructs a new struct Random_Generic with two seed arrays of type u64.
§Arguments
seedis the seed array and is of[u64; 8].auxis 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::Slapdash_Num_C;
let seed = [10500872879054459758_u64, 12_u64, 123456789_u64, 987654321_u64, 852648791354687_u64, 555555555555_u64, 777777777777_u64, 741258963_u64];
let aux = [15887751380961987625_u64, 789456123_u64, 9632587414_u64, 789654123_u64, 5_u64, 58976541235_u64, 9513574682_u64, 369258147_u64];
let mut slapdash = Slapdash_Num_C::new_with_seed_arrays(seed, aux);
println!("Slapdash number = {}", slapdash.random_u64());Sourcepub fn new_with_seed_collector(
seed_collector: fn() -> [u64; 8],
) -> Random_Generic<{ _ }>
pub fn new_with_seed_collector( seed_collector: fn() -> [u64; 8], ) -> Random_Generic<{ _ }>
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::Slapdash_Num_C;
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 slapdash = Slapdash::new_with_seed_collector(seed_collector);
println!("Slapdash number = {}", slapdash.random_u32());Sourcepub fn new_with_seed_collector_seeds(
seed_collector: fn() -> [u64; 8],
seed: u64,
aux: u64,
) -> Random_Generic<{ _ }>
pub fn new_with_seed_collector_seeds( seed_collector: fn() -> [u64; 8], seed: u64, aux: u64, ) -> Random_Generic<{ _ }>
Constructs a new struct Random_Generic with a seed collector function
and two seeds of type u64.
§Arguments
seed_collectoris a seed collector function to collect seeds, and is of the typefn() -> [u64; 8].seedis the seed number of the typeu64.auxis the seed number of the typeu64.
§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::Slapdash_Num_C;
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 slapdash = Slapdash::new_with_seed_collector_seeds(seed_collector, 50558, 18782);
println!("Slapdash number = {}", slapdash.random_u32());Sourcepub fn new_with_seed_collector_seed_arrays(
seed_collector: fn() -> [u64; 8],
seed: [u64; 8],
aux: [u64; 8],
) -> Random_Generic<{ _ }>
pub fn new_with_seed_collector_seed_arrays( seed_collector: fn() -> [u64; 8], seed: [u64; 8], aux: [u64; 8], ) -> Random_Generic<{ _ }>
Constructs a new struct Random_Generic with a seed collector function
and two seed arrays of type u64.
§Arguments
seed_collectoris a seed collector function to collect seeds, and is of the typefn() -> [u64; 8].seedis the seed array and is of[u64; 8].auxis 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::Slapdash_Num_C;
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, 12_u64, 123456789_u64, 987654321_u64, 852648791354687_u64, 555555555555_u64, 777777777777_u64, 741258963_u64];
let aux = [15887751380961987625_u64, 789456123_u64, 9632587414_u64, 789654123_u64, 5_u64, 58976541235_u64, 9513574682_u64, 369258147_u64];
let mut slapdash = Slapdash_Num_C::new_with_seed_collector_seed_arrays(seed_collector, seed, aux);
println!("Slapdash number = {}", slapdash.random_u64());