2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-06-01 22:13:56 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-06-09 16:06:07 +03:00
|
|
|
module sys
|
2020-06-01 22:13:56 +03:00
|
|
|
|
|
|
|
import math.bits
|
2021-01-26 16:55:09 +03:00
|
|
|
import rand.seed
|
2020-06-01 22:13:56 +03:00
|
|
|
|
|
|
|
// Implementation note:
|
|
|
|
// ====================
|
2021-02-14 21:37:32 +03:00
|
|
|
// C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive)
|
|
|
|
// C.rand() is okay to use within its defined range.
|
2020-06-01 22:13:56 +03:00
|
|
|
// (See: https://web.archive.org/web/20180801210127/http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx)
|
|
|
|
// The problem is, this value varies with the libc implementation. On windows,
|
2021-02-14 21:37:32 +03:00
|
|
|
// for example, RAND_MAX is usually a measly 32767, whereas on (newer) linux it's generally
|
2020-06-01 22:13:56 +03:00
|
|
|
// 2147483647. The repetition period also varies wildly. In order to provide more entropy
|
|
|
|
// without altering the underlying algorithm too much, this implementation simply
|
|
|
|
// requests for more random bits until the necessary width for the integers is achieved.
|
2022-02-28 14:17:54 +03:00
|
|
|
|
|
|
|
pub const seed_len = 1
|
|
|
|
|
2020-06-01 22:13:56 +03:00
|
|
|
const (
|
|
|
|
rand_limit = u64(C.RAND_MAX)
|
|
|
|
rand_bitsize = bits.len_64(rand_limit)
|
2022-02-28 14:17:54 +03:00
|
|
|
rand_bytesize = rand_bitsize / 8
|
|
|
|
u16_iter_count = calculate_iterations_for(16)
|
2020-06-01 22:13:56 +03:00
|
|
|
u32_iter_count = calculate_iterations_for(32)
|
|
|
|
u64_iter_count = calculate_iterations_for(64)
|
|
|
|
)
|
|
|
|
|
|
|
|
fn calculate_iterations_for(bits int) int {
|
2021-01-26 16:55:09 +03:00
|
|
|
base := bits / sys.rand_bitsize
|
|
|
|
extra := if bits % sys.rand_bitsize == 0 { 0 } else { 1 }
|
2020-06-01 22:13:56 +03:00
|
|
|
return base + extra
|
|
|
|
}
|
|
|
|
|
|
|
|
// SysRNG is the PRNG provided by default in the libc implementiation that V uses.
|
|
|
|
pub struct SysRNG {
|
|
|
|
mut:
|
2022-02-28 14:17:54 +03:00
|
|
|
seed u32 = seed.time_seed_32()
|
|
|
|
buffer int
|
|
|
|
bytes_left int
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// r.seed() sets the seed of the accepting SysRNG to the given data.
|
|
|
|
pub fn (mut r SysRNG) seed(seed_data []u32) {
|
|
|
|
if seed_data.len != 1 {
|
|
|
|
eprintln('SysRNG needs one 32-bit unsigned integer as the seed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
r.seed = seed_data[0]
|
2021-02-14 21:37:32 +03:00
|
|
|
C.srand(r.seed)
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// r.default_rand() exposes the default behavior of the system's RNG
|
|
|
|
// (equivalent to calling C.rand()). Recommended for testing/comparison
|
|
|
|
// b/w V and other languages using libc and not for regular use.
|
|
|
|
// This is also a one-off feature of SysRNG, similar to the global seed
|
|
|
|
// situation. Other generators will not have this.
|
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) default_rand() int {
|
|
|
|
return C.rand()
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:17:54 +03:00
|
|
|
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
|
|
[inline]
|
|
|
|
pub fn (mut r SysRNG) byte() byte {
|
|
|
|
if r.bytes_left >= 1 {
|
|
|
|
r.bytes_left -= 1
|
|
|
|
value := byte(r.buffer)
|
|
|
|
r.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
r.buffer = r.default_rand()
|
|
|
|
r.bytes_left = sys.rand_bytesize - 1
|
|
|
|
value := byte(r.buffer)
|
|
|
|
r.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// u16 returns a uniformly distributed pseudorandom 16-bit unsigned positive `u16`.
|
|
|
|
[inline]
|
|
|
|
pub fn (mut r SysRNG) u16() u16 {
|
|
|
|
if r.bytes_left >= 2 {
|
|
|
|
r.bytes_left -= 2
|
|
|
|
value := u16(r.buffer)
|
|
|
|
r.buffer >>= 16
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
mut result := u16(C.rand())
|
|
|
|
for i in 1 .. sys.u16_iter_count {
|
|
|
|
result = result ^ (u16(C.rand()) << (sys.rand_bitsize * i))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// u32 returns a uniformly distributed pseudorandom 32-bit unsigned positive `u32`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) u32() u32 {
|
|
|
|
mut result := u32(C.rand())
|
2021-01-26 16:55:09 +03:00
|
|
|
for i in 1 .. sys.u32_iter_count {
|
|
|
|
result = result ^ (u32(C.rand()) << (sys.rand_bitsize * i))
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:17:54 +03:00
|
|
|
// u64 returns a uniformly distributed pseudorandom 64-bit unsigned positive `u64`.
|
2020-06-01 22:13:56 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) u64() u64 {
|
|
|
|
mut result := u64(C.rand())
|
2021-01-26 16:55:09 +03:00
|
|
|
for i in 1 .. sys.u64_iter_count {
|
|
|
|
result = result ^ (u64(C.rand()) << (sys.rand_bitsize * i))
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:17:54 +03:00
|
|
|
// block_size returns the number of bits that the RNG can produce in a single iteration.
|
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) block_size() int {
|
|
|
|
return sys.rand_bitsize
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:14:20 +03:00
|
|
|
// free should be called when the generator is no longer needed
|
|
|
|
[unsafe]
|
|
|
|
pub fn (mut rng SysRNG) free() {
|
|
|
|
unsafe { free(rng) }
|
|
|
|
}
|