1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/rand/sys/system_rng.c.v

128 lines
3.7 KiB
V
Raw Normal View History

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
import rand.seed
2020-06-01 22:13:56 +03:00
// Implementation note:
// ====================
// 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,
// 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.
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)
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 {
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:
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]
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()
}
// 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())
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
}
// 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())
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
}
// 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) }
}