Scoop API

scoop:scoop.platform:0.1.0

src/random/system_random.scoop

Selected production source · src/random/system_random.scoop

1package scoop.platform.random
3import scoop.platform.io.requireTransfer
4import scoop.platform.memory.NativeByteBuffer
5import scoop.platform.system.*
6import scoop.random.Random
8/**
9 * A random source backed by the operating system, independent of deterministic seeded generators.
10 */
11public object SystemRandom: Random {
12 /**
13 * Returns the requested number of random bits from the operating-system source.
14 */
15 public override fun nextBits(bitCount: Int): Int {
16 if (bitCount < 0 || bitCount > 32)
17 throw IllegalArgumentException(Some("invalid random bit count"))
18 if (bitCount == 0)
19 return 0
20 val bytes = nextBytes(4L)
21 var result = 0u
22 for (byte in bytes)
23 result = (result shl 8) or byte.toUByte().toUInt()
24 return (result shr (32L - bitCount.toLong())).toInt()
25 }
27 /**
28 * Returns new random bytes or fills the specified mutable-array range, depending on the
29 * overload.
30 */
31 public override fun nextBytes(size: Long): Array<Byte> {
32 if (size < 0L)
33 throw IllegalArgumentException(Some("negative byte count"))
34 val array = MutableArray<Byte>(size) { 0.toByte() }
35 nextBytes(array)
36 return array.toArray()
37 }
39 /**
40 * Returns new random bytes or fills the specified mutable-array range, depending on the
41 * overload.
42 */
43 public override fun nextBytes(
44 array: MutableArray<Byte>,
45 fromIndex: Long,
46 toIndex: Long
47 ): MutableArray<Byte> {
48 if (fromIndex < 0L || toIndex < 0L || fromIndex > array.size || toIndex > array.size)
49 throw IndexOutOfBoundsException()
50 if (fromIndex > toIndex)
51 throw IllegalArgumentException(Some("reversed byte range"))
52 if (fromIndex == toIndex)
53 return array
54 @Unsafe
55 {
56 array.withDataPointer {
57 pointer,
58 _ -> @Unsafe
59 {
60 var offset = fromIndex
61 while (offset < toIndex)
62 offset += entropy(pointer + offset, toIndex - offset)
63 }
64 }
65 }
66 return array
67 }
69 /**
70 * Appends count operating-system random bytes to the buffer's writable region.
71 */
72 public fun fill(buffer: NativeByteBuffer, count: Long = buffer.writableBytes): Long {
73 requireTransfer(count, buffer.writableBytes)
74 var remaining = count
75 while (remaining > 0L) {
76 val requested = remaining
77 @Unsafe
78 {
79 val written =
80 buffer.withWritableBytes {
81 pointer,
82 _ -> @Unsafe
83 {
84 return@withWritableBytes entropy(pointer!!, requested)
85 }
86 }
87 remaining -= written
88 }
89 }
90 return count
91 }
94@Unsafe
95private fun entropy(pointer: Ptr<Byte>, count: Long): Long {
96 val result = nativeEntropy(pointer, count)
97 if (result.count < 0L) {
98 val domain =
99 if (operatingSystem == OperatingSystem.Windows)
100 NativeErrorDomain.NtStatus
101 else
102 NativeErrorDomain.PosixErrno
103 val error = NativeError(domain, result.error)
104 throw PlatformException("system entropy", error.kind, Some(error))
105 }
106 return result.count
109@CLayout
110private struct NativeEntropy(val count: Long, val error: Int)
112@Extern(name = "scoop_platform_entropy")
113private fun nativeEntropy(pointer: Ptr<Byte>, count: Long): NativeEntropy