-
Notifications
You must be signed in to change notification settings - Fork 28
/
_random.pyx
159 lines (126 loc) · 4.45 KB
/
_random.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# SPDX-License-Identifier: MIT
# Copyright (c) 2016, Elaborated Networks GmbH
# Copyright (c) 2019, Mathias Laurin
"""Random number generator (RNG) wrapper."""
from libc.stdlib cimport malloc, free
cimport mbedtls._platform as _plt
cimport mbedtls._random as _rnd
import numbers as _numbers
import mbedtls.mpi as _mpi
from mbedtls.exceptions import check_error
BPF = 53 # Number of bits in a float
RECIP_BPF = 2**-BPF
cdef class _Entropy:
def __cinit__(self):
"""Initialize the context."""
_rnd.mbedtls_entropy_init(&self._ctx)
def __dealloc__(self):
"""Free and clear the context."""
_rnd.mbedtls_entropy_free(&self._ctx)
def gather(self):
"""Trigger an extra gather poll for the accumulator."""
_rnd.mbedtls_entropy_gather(&self._ctx)
def retrieve(self, size_t length):
"""Retrieve entropy from the accumulator."""
cdef unsigned char *output = <unsigned char *> malloc(
length * sizeof(unsigned char)
)
if not output:
raise MemoryError()
try:
check_error(_rnd.mbedtls_entropy_func(&self._ctx, output, length))
return output[:length]
finally:
free(output)
def update(self, const unsigned char[:] data):
"""Add data to the accumulator manually."""
check_error(
_rnd.mbedtls_entropy_update_manual(
&self._ctx, &data[0], data.shape[0]
)
)
cdef class Random:
def __init__(self):
self._entropy = _Entropy()
check_error(
_rnd.mbedtls_ctr_drbg_seed(
&self._ctx,
&_rnd.mbedtls_entropy_func,
&self._entropy._ctx,
NULL, 0
)
)
def __cinit__(self):
"""Initialize the context."""
_rnd.mbedtls_ctr_drbg_init(&self._ctx)
def __dealloc__(self):
"""Free and clear the context."""
_rnd.mbedtls_ctr_drbg_free(&self._ctx)
@property
def _entropy(self):
return self._entropy
def _reseed(self, const unsigned char[:] data=None):
"""Reseed the RNG."""
if data is None:
check_error(_rnd.mbedtls_ctr_drbg_reseed(&self._ctx, NULL, 0))
else:
check_error(
_rnd.mbedtls_ctr_drbg_reseed(&self._ctx, &data[0], data.size)
)
def urandom(self, size_t length):
"""Returns `length` random bytes."""
cdef unsigned char *output = <unsigned char *> malloc(
length * sizeof(unsigned char)
)
if not output:
raise MemoryError()
try:
check_error(
_rnd.mbedtls_ctr_drbg_random(&self._ctx, output, length)
)
ret = output[:length]
_plt.mbedtls_platform_zeroize(output, length)
return ret
finally:
free(output)
def randbelow(self, upper_bound):
"""Return a random int in the range [0, n).
Raises ValueError if n <= 0.
"""
if upper_bound <= 0:
raise ValueError("Upper bound must be positive.")
kk = upper_bound.bit_length()
rr = self.getrandbits(kk)
while rr >= upper_bound:
rr = self.getrandbits(kk)
return rr
def random(self):
"""Return the next random floating point number."""
# Algorithm taken from Python's secrets and random libraries.
return float(
_mpi.MPI.from_bytes(self.urandom(7), "big") >> 3
) * RECIP_BPF
def getrandbits(self, k):
"""Generate an int with `k` random bits."""
# Algorithm adapted from Python's secrets and random libraries.
if k <= 0:
raise ValueError("number of bits must be greater than zero")
if not isinstance(k, _numbers.Integral):
raise TypeError("number of bits should be an integer")
numbytes = (k + 7) // 8
value = _mpi.MPI.from_bytes(self.urandom(numbytes), "big")
# Trim excess bits:
extra_bits = value.bit_length() - k
return value >> (0 if extra_bits <= 0 else extra_bits)
def choice(self, seq):
"""Return a random element from `seq`."""
try:
ii = self.randbelow(len(seq))
except ValueError:
raise IndexError("Cannot choose from an empty sequence")
return seq[ii]
cdef Random __rng = Random()
cdef Random default_rng():
return __rng
def default_rng():
return __rng