2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-06-01 21:13:56 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-06-09 15:06:07 +02:00
|
|
|
module sys
|
2020-06-01 21:13:56 +02:00
|
|
|
|
|
|
|
import math.bits
|
2021-01-26 14:55:09 +01:00
|
|
|
import rand.seed
|
2020-06-01 21:13:56 +02:00
|
|
|
|
|
|
|
// Implementation note:
|
|
|
|
// ====================
|
2021-02-14 19:37:32 +01: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 21:13:56 +02: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 19:37:32 +01:00
|
|
|
// for example, RAND_MAX is usually a measly 32767, whereas on (newer) linux it's generally
|
2020-06-01 21:13:56 +02: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 12:17:54 +01:00
|
|
|
|
|
|
|
pub const seed_len = 1
|
|
|
|
|
2020-06-01 21:13:56 +02:00
|
|
|
const (
|
|
|
|
rand_limit = u64(C.RAND_MAX)
|
|
|
|
rand_bitsize = bits.len_64(rand_limit)
|
2022-02-28 12:17:54 +01:00
|
|
|
rand_bytesize = rand_bitsize / 8
|
|
|
|
u16_iter_count = calculate_iterations_for(16)
|
2020-06-01 21:13:56 +02: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 14:55:09 +01:00
|
|
|
base := bits / sys.rand_bitsize
|
|
|
|
extra := if bits % sys.rand_bitsize == 0 { 0 } else { 1 }
|
2020-06-01 21:13:56 +02: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 12:17:54 +01:00
|
|
|
seed u32 = seed.time_seed_32()
|
|
|
|
buffer int
|
|
|
|
bytes_left int
|
2020-06-01 21:13:56 +02: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 19:37:32 +01:00
|
|
|
C.srand(r.seed)
|
2020-06-01 21:13:56 +02: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 12:17:54 +01:00
|
|
|
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
|
|
[inline]
|
2022-04-15 17:25:45 +02:00
|
|
|
pub fn (mut r SysRNG) u8() u8 {
|
2022-02-28 12:17:54 +01:00
|
|
|
if r.bytes_left >= 1 {
|
|
|
|
r.bytes_left -= 1
|
2022-04-15 13:58:56 +02:00
|
|
|
value := u8(r.buffer)
|
2022-02-28 12:17:54 +01:00
|
|
|
r.buffer >>= 8
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
r.buffer = r.default_rand()
|
|
|
|
r.bytes_left = sys.rand_bytesize - 1
|
2022-04-15 13:58:56 +02:00
|
|
|
value := u8(r.buffer)
|
2022-02-28 12:17:54 +01:00
|
|
|
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 21:13:56 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) u32() u32 {
|
|
|
|
mut result := u32(C.rand())
|
2021-01-26 14:55:09 +01:00
|
|
|
for i in 1 .. sys.u32_iter_count {
|
|
|
|
result = result ^ (u32(C.rand()) << (sys.rand_bitsize * i))
|
2020-06-01 21:13:56 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:17:54 +01:00
|
|
|
// u64 returns a uniformly distributed pseudorandom 64-bit unsigned positive `u64`.
|
2020-06-01 21:13:56 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (r SysRNG) u64() u64 {
|
|
|
|
mut result := u64(C.rand())
|
2021-01-26 14:55:09 +01:00
|
|
|
for i in 1 .. sys.u64_iter_count {
|
|
|
|
result = result ^ (u64(C.rand()) << (sys.rand_bitsize * i))
|
2020-06-01 21:13:56 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:17:54 +01: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 10:14:20 +02:00
|
|
|
// free should be called when the generator is no longer needed
|
|
|
|
[unsafe]
|
|
|
|
pub fn (mut rng SysRNG) free() {
|
|
|
|
unsafe { free(rng) }
|
|
|
|
}
|