Skip to content

Commit 0b1de05

Browse files
committed
bigint: replace /dev/random with std::random_device
1 parent 9e02538 commit 0b1de05

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

libff/algebra/fields/bigint.tcc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define BIGINT_TCC_
1212
#include <cassert>
1313
#include <cstring>
14+
#include <random>
1415

1516
namespace libff {
1617

@@ -165,11 +166,14 @@ bool bigint<n>::test_bit(const std::size_t bitno) const
165166
template<mp_size_t n>
166167
bigint<n>& bigint<n>::randomize()
167168
{
168-
assert(GMP_NUMB_BITS == sizeof(mp_limb_t) * 8);
169-
FILE *fp = fopen("/dev/urandom", "r"); //TODO Remove hard-coded use of /dev/urandom.
170-
size_t bytes_read = fread(this->data, 1, sizeof(mp_limb_t) * n, fp);
171-
assert(bytes_read == sizeof(mp_limb_t) * n);
172-
fclose(fp);
169+
static_assert(GMP_NUMB_BITS == sizeof(mp_limb_t) * 8, "Wrong GMP_NUMB_BITS value");
170+
std::random_device rd;
171+
constexpr size_t num_random_words = sizeof(mp_limb_t) * n / sizeof(std::random_device::result_type);
172+
auto random_words = reinterpret_cast<std::random_device::result_type*>(this->data);
173+
for (size_t i = 0; i < num_random_words; ++i)
174+
{
175+
random_words[i] = rd();
176+
}
173177

174178
return (*this);
175179
}

0 commit comments

Comments
 (0)