From bf5d62d4332548ac7798085eb98cedea88131d9d Mon Sep 17 00:00:00 2001 From: Innokentii Sennovskii Date: Fri, 1 Nov 2024 23:55:39 +0000 Subject: [PATCH] fix: Fix random for Mac users (#9670) mac doesn't have the getrandom function, but should have getentropy --- .../barretenberg/ecc/curves/bn254/fr.test.cpp | 5 +--- .../barretenberg/numeric/random/engine.cpp | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp b/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp index 9e49512c501..803fe96d79a 100644 --- a/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp +++ b/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp @@ -357,10 +357,7 @@ TEST(fr, BatchInvert) } for (size_t i = 0; i < n; ++i) { - EXPECT_EQ(coeffs[i].data[0], 0UL); - EXPECT_EQ(coeffs[i].data[1], 0UL); - EXPECT_EQ(coeffs[i].data[2], 0UL); - EXPECT_EQ(coeffs[i].data[3], 0UL); + EXPECT_TRUE(coeffs[i].is_zero()); } } diff --git a/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp b/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp index a6cc39686c6..6288e007562 100644 --- a/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp +++ b/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp @@ -10,13 +10,18 @@ namespace bb::numeric { namespace { -#ifndef __wasm__ -// When working on native we allocate 1M of memory to sample randomness from urandom -constexpr size_t RANDOM_BUFFER_SIZE = 1UL << 20; -#else -// In wasm the API we are using can only give 256 bytes per call, so there is no point in creating a larger buffer +#if defined(__wasm__) || defined(__APPLE__) + +// 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 +// larger buffer constexpr size_t RANDOM_BUFFER_SIZE = 256; constexpr size_t BYTES_PER_GETENTROPY_READ = 256; + +#else + +// When working on native we allocate 1M of memory to sample randomness from urandom +constexpr size_t RANDOM_BUFFER_SIZE = 1UL << 20; + #endif struct RandomBufferWrapper { // Buffer with randomness sampled from a CSPRNG @@ -46,14 +51,14 @@ template std::array