Skip to content

Commit

Permalink
Add Mutex to global initialisation of generators (#371)
Browse files Browse the repository at this point in the history
* Mutex lock initialization call

* add comment on mutex

---------

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
maxhora and kevaundray authored Apr 21, 2023
1 parent a38e361 commit ecb6129
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cpp/src/barretenberg/crypto/pedersen_hash/pedersen_lookup.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "./pedersen_lookup.hpp"

#include <mutex>

#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"

namespace crypto {
Expand All @@ -10,6 +12,12 @@ std::array<std::vector<grumpkin::g1::affine_element>, NUM_PEDERSEN_TABLES> peder
std::vector<grumpkin::g1::affine_element> pedersen_iv_table;
std::array<grumpkin::g1::affine_element, NUM_PEDERSEN_TABLES> generators;

// Mutex is not available in the WASM context.
// WASM runs in a single-thread so this is acceptable.
#if !defined(__wasm__)
std::mutex init_mutex;
#endif

static bool inited = false;

void init_single_lookup_table(const size_t index)
Expand Down Expand Up @@ -66,6 +74,11 @@ void init()
{
ASSERT(BITS_PER_TABLE < BITS_OF_BETA);
ASSERT(BITS_PER_TABLE + BITS_OF_BETA < BITS_ON_CURVE);

#if !defined(__wasm__)
const std::lock_guard<std::mutex> lock(init_mutex);
#endif

if (inited) {
return;
}
Expand Down

0 comments on commit ecb6129

Please sign in to comment.