@@ -10,13 +10,18 @@ namespace bb::numeric {
10
10
11
11
namespace {
12
12
13
- #ifndef __wasm__
14
- // When working on native we allocate 1M of memory to sample randomness from urandom
15
- constexpr size_t RANDOM_BUFFER_SIZE = 1UL << 20 ;
16
- #else
17
- // In wasm the API we are using can only give 256 bytes per call, so there is no point in creating a larger buffer
13
+ #if defined(__wasm__) || defined(__APPLE__)
14
+
15
+ // In wasm and on mac os the API we are using can only give 256 bytes per call, so there is no point in creating a
16
+ // larger buffer
18
17
constexpr size_t RANDOM_BUFFER_SIZE = 256 ;
19
18
constexpr size_t BYTES_PER_GETENTROPY_READ = 256 ;
19
+
20
+ #else
21
+
22
+ // When working on native we allocate 1M of memory to sample randomness from urandom
23
+ constexpr size_t RANDOM_BUFFER_SIZE = 1UL << 20 ;
24
+
20
25
#endif
21
26
struct RandomBufferWrapper {
22
27
// Buffer with randomness sampled from a CSPRNG
@@ -46,14 +51,14 @@ template <size_t size_in_unsigned_ints> std::array<unsigned int, size_in_unsigne
46
51
uint8_t * current_offset = random_buffer_wrapper.buffer ;
47
52
// Sample until we fill the buffer
48
53
while (bytes_left != 0 ) {
49
- #ifndef __wasm__
50
- // Sample from urandom on native
51
- auto read_bytes = getrandom (current_offset, bytes_left, 0 );
52
- #else
54
+ #if defined(__wasm__) || defined(__APPLE__)
53
55
// Sample through a "syscall" on wasm. We can't request more than 256, it fails and results in an infinite
54
56
// loop
55
57
ssize_t read_bytes =
56
58
getentropy (current_offset, BYTES_PER_GETENTROPY_READ) == -1 ? -1 : BYTES_PER_GETENTROPY_READ;
59
+ #else
60
+ // Sample from urandom on native
61
+ auto read_bytes = getrandom (current_offset, bytes_left, 0 );
57
62
#endif
58
63
// If we read something, update the leftover
59
64
if (read_bytes != -1 ) {
0 commit comments