Skip to content

Commit bf5d62d

Browse files
authored
fix: Fix random for Mac users (#9670)
mac doesn't have the getrandom function, but should have getentropy
1 parent eeea55a commit bf5d62d

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,7 @@ TEST(fr, BatchInvert)
357357
}
358358

359359
for (size_t i = 0; i < n; ++i) {
360-
EXPECT_EQ(coeffs[i].data[0], 0UL);
361-
EXPECT_EQ(coeffs[i].data[1], 0UL);
362-
EXPECT_EQ(coeffs[i].data[2], 0UL);
363-
EXPECT_EQ(coeffs[i].data[3], 0UL);
360+
EXPECT_TRUE(coeffs[i].is_zero());
364361
}
365362
}
366363

barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ namespace bb::numeric {
1010

1111
namespace {
1212

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
1817
constexpr size_t RANDOM_BUFFER_SIZE = 256;
1918
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+
2025
#endif
2126
struct RandomBufferWrapper {
2227
// 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
4651
uint8_t* current_offset = random_buffer_wrapper.buffer;
4752
// Sample until we fill the buffer
4853
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__)
5355
// Sample through a "syscall" on wasm. We can't request more than 256, it fails and results in an infinite
5456
// loop
5557
ssize_t read_bytes =
5658
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);
5762
#endif
5863
// If we read something, update the leftover
5964
if (read_bytes != -1) {

0 commit comments

Comments
 (0)