| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module sys |
| 5 | |
| 6 | import math.bits |
| 7 | import rand.buffer |
| 8 | import rand.seed |
| 9 | |
| 10 | // Implementation note: |
| 11 | // ==================== |
| 12 | // C.rand returns a pseudorandom integer from 0 (inclusive) to C.RAND_MAX (exclusive) |
| 13 | // C.rand() is okay to use within its defined range. |
| 14 | // (See: https://web.archive.org/web/20180801210127/http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx) |
| 15 | // The problem is, this value varies with the libc implementation. On windows, |
| 16 | // for example, RAND_MAX is usually a measly 32767, whereas on (newer) linux it's generally |
| 17 | // 2147483647. The repetition period also varies wildly. In order to provide more entropy |
| 18 | // without altering the underlying algorithm too much, this implementation simply |
| 19 | // requests for more random bits until the necessary width for the integers is achieved. |
| 20 | |
| 21 | pub const seed_len = 1 |
| 22 | |
| 23 | const rand_limit = u64(C.RAND_MAX) |
| 24 | const rand_bitsize = bits.len_64(rand_limit) |
| 25 | const rand_bytesize = rand_bitsize / 8 |
| 26 | const u16_iter_count = calculate_iterations_for(16) |
| 27 | const u32_iter_count = calculate_iterations_for(32) |
| 28 | const u64_iter_count = calculate_iterations_for(64) |
| 29 | |
| 30 | fn calculate_iterations_for(bits_ int) int { |
| 31 | base := bits_ / rand_bitsize |
| 32 | extra := if bits_ % rand_bitsize == 0 { 0 } else { 1 } |
| 33 | return base + extra |
| 34 | } |
| 35 | |
| 36 | // SysRNG is the PRNG provided by default in the libc implementiation that V uses. |
| 37 | pub struct SysRNG { |
| 38 | buffer.PRNGBuffer |
| 39 | mut: |
| 40 | seed u32 = seed.time_seed_32() |
| 41 | } |
| 42 | |
| 43 | // r.seed() sets the seed of the accepting SysRNG to the given data. |
| 44 | pub fn (mut r SysRNG) seed(seed_data []u32) { |
| 45 | if seed_data.len != 1 { |
| 46 | eprintln('SysRNG needs one 32-bit unsigned integer as the seed.') |
| 47 | exit(1) |
| 48 | } |
| 49 | r.seed = seed_data[0] |
| 50 | C.srand(r.seed) |
| 51 | } |
| 52 | |
| 53 | // r.default_rand() exposes the default behavior of the system's RNG |
| 54 | // (equivalent to calling C.rand()). Recommended for testing/comparison |
| 55 | // b/w V and other languages using libc and not for regular use. |
| 56 | // This is also a one-off feature of SysRNG, similar to the global seed |
| 57 | // situation. Other generators will not have this. |
| 58 | @[inline] |
| 59 | pub fn (r SysRNG) default_rand() int { |
| 60 | return C.rand() |
| 61 | } |
| 62 | |
| 63 | // byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`. |
| 64 | @[inline] |
| 65 | pub fn (mut r SysRNG) u8() u8 { |
| 66 | if r.bytes_left >= 1 { |
| 67 | r.bytes_left -= 1 |
| 68 | value := u8(r.buffer) |
| 69 | r.buffer >>= 8 |
| 70 | return value |
| 71 | } |
| 72 | r.buffer = u64(r.default_rand()) |
| 73 | r.bytes_left = rand_bytesize - 1 |
| 74 | value := u8(r.buffer) |
| 75 | r.buffer >>= 8 |
| 76 | return value |
| 77 | } |
| 78 | |
| 79 | // u16 returns a uniformly distributed pseudorandom 16-bit unsigned positive `u16`. |
| 80 | @[inline] |
| 81 | pub fn (mut r SysRNG) u16() u16 { |
| 82 | if r.bytes_left >= 2 { |
| 83 | r.bytes_left -= 2 |
| 84 | value := u16(r.buffer) |
| 85 | r.buffer >>= 16 |
| 86 | return value |
| 87 | } |
| 88 | mut result := u16(C.rand()) |
| 89 | for i in 1 .. u16_iter_count { |
| 90 | result = result ^ (u16(C.rand()) << (rand_bitsize * i)) |
| 91 | } |
| 92 | return result |
| 93 | } |
| 94 | |
| 95 | // u32 returns a uniformly distributed pseudorandom 32-bit unsigned positive `u32`. |
| 96 | @[inline] |
| 97 | pub fn (r SysRNG) u32() u32 { |
| 98 | mut result := u32(C.rand()) |
| 99 | for i in 1 .. u32_iter_count { |
| 100 | result = result ^ (u32(C.rand()) << (rand_bitsize * i)) |
| 101 | } |
| 102 | return result |
| 103 | } |
| 104 | |
| 105 | // u64 returns a uniformly distributed pseudorandom 64-bit unsigned positive `u64`. |
| 106 | @[inline] |
| 107 | pub fn (r SysRNG) u64() u64 { |
| 108 | mut result := u64(C.rand()) |
| 109 | for i in 1 .. u64_iter_count { |
| 110 | result = result ^ (u64(C.rand()) << (rand_bitsize * i)) |
| 111 | } |
| 112 | return result |
| 113 | } |
| 114 | |
| 115 | // block_size returns the number of bits that the RNG can produce in a single iteration. |
| 116 | @[inline] |
| 117 | pub fn (r SysRNG) block_size() int { |
| 118 | return rand_bitsize |
| 119 | } |
| 120 | |
| 121 | // free should be called when the generator is no longer needed. |
| 122 | @[unsafe] |
| 123 | pub fn (mut rng SysRNG) free() { |
| 124 | } |
| 125 | |