Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
#include <chrono>

using namespace bb;

int main(int, char**)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <benchmark/benchmark.h>

using namespace benchmark;
using namespace bb;

grumpkin::fq poseidon_function(const size_t count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void ThreadPool::worker_loop(size_t /*unused*/)
}
} // namespace

namespace bb {
/**
* A thread pooled strategy that uses atomics to prevent needing constantly lock on a queue.
* The main thread acts as a worker also, and when it completes, it spins until thread workers are done.
Expand All @@ -112,3 +113,4 @@ void parallel_for_atomic_pool(size_t num_iterations, const std::function<void(si
pool.start_tasks(num_iterations, func);
// info("done");
}
} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <thread>
#include <vector>

namespace {
class ThreadPool {
public:
ThreadPool(size_t num_threads)
Expand Down Expand Up @@ -85,7 +86,9 @@ class ThreadPool {
}
}
};
} // namespace

namespace bb {
/**
* A Thread pooled strategy that uses a popular lock-free multiple-producer multiple-consume queue library by
* "moodycamel" as the underlying mechanism to distribute work and join on completion.
Expand All @@ -97,3 +100,4 @@ void parallel_for_moody(size_t num_iterations, const std::function<void(size_t)>

pool.start_tasks(func, num_iterations);
}
} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void ThreadPool::worker_loop(size_t /*unused*/)
}
} // namespace

namespace bb {
/**
* A thread pooled strategy that uses std::mutex for protection. Each worker increments the "iteration" and processes.
* The main thread acts as a worker also, and when it completes, it spins until thread workers are done.
Expand All @@ -127,3 +128,4 @@ void parallel_for_mutex_pool(size_t num_iterations, const std::function<void(siz
pool.start_tasks(num_iterations, func);
// info("done");
}
} // namespace bb
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/common/parallel_for_omp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstddef>
#include <functional>

namespace bb {
void parallel_for_omp(size_t num_iterations, const std::function<void(size_t)>& func)
{
#ifndef NO_OMP_MULTITHREADING
Expand All @@ -10,3 +11,4 @@ void parallel_for_omp(size_t num_iterations, const std::function<void(size_t)>&
func(i);
}
}
} // namespace bb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <thread>
#include <vector>

namespace {
class ThreadPool {
public:
ThreadPool(size_t num_threads);
Expand Down Expand Up @@ -100,7 +101,9 @@ void ThreadPool::worker_loop(size_t /*unused*/)
}
// info("worker exit ", worker_num);
}
} // namespace

namespace bb {
/**
* A thread pooled strategey that assumed that thread pools would be more efficient than spawning threads.
* Every iteration becomes a task in a queue. That's probably not very efficient.
Expand All @@ -120,3 +123,4 @@ void parallel_for_queued(size_t num_iterations, const std::function<void(size_t)
pool.wait();
// info("pool finished work");
}
} // namespace bb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "thread.hpp"
// #include "log.hpp"

namespace bb {
/**
* A very simple strategy. Spawn a worker thread for every iteration (but no more than num cores).
* Worker threads tight-loop incrementing an atomic variable from 0-num_iterations, until num_iterations reached.
Expand Down Expand Up @@ -40,3 +40,4 @@ void parallel_for_spawning(size_t num_iterations, const std::function<void(size_
}
// info("joined!\n\n");
}
} // namespace bb
40 changes: 39 additions & 1 deletion barretenberg/cpp/src/barretenberg/common/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Haven't done deeper analysis. Defaulting to mutex_pool.
*/

namespace bb {
// 64 core aws r5.
// pippenger run: pippenger_bench/1048576
// coset_fft run: coset_fft_bench_parallel/4194304
Expand Down Expand Up @@ -223,4 +224,41 @@ void run_loop_in_parallel_if_effective_internal(size_t num_points,
template void run_loop_in_parallel_if_effective_internal(
size_t, const std::function<void(size_t, size_t)>&, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
template void run_loop_in_parallel_if_effective_internal(
size_t, const std::function<void(size_t, size_t, size_t)>&, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
size_t, const std::function<void(size_t, size_t, size_t)>&, size_t, size_t, size_t, size_t, size_t, size_t, size_t);

/**
* @brief calculates number of threads to create based on minimum iterations per thread
* @details Finds the number of cpus with get_num_cpus(), and calculates `desired_num_threads`
* Returns the min of `desired_num_threads` and `max_num_threads`.
* Note that it will not calculate a power of 2 necessarily, use `calculate_num_threads_pow2` instead
*
* @param num_iterations
* @param min_iterations_per_thread
* @return size_t
*/
size_t calculate_num_threads(size_t num_iterations, size_t min_iterations_per_thread)
{
size_t max_num_threads = get_num_cpus(); // number of available threads
size_t desired_num_threads = num_iterations / min_iterations_per_thread;
size_t num_threads = std::min(desired_num_threads, max_num_threads); // fewer than max if justified
num_threads = num_threads > 0 ? num_threads : 1; // ensure num_threads is at least 1
return num_threads;
}

/**
* @brief calculates number of threads to create based on minimum iterations per thread, guaranteed power of 2
* @details Same functionality as `calculate_num_threads` but guaranteed power of 2
* @param num_iterations
* @param min_iterations_per_thread
* @return size_t
*/
size_t calculate_num_threads_pow2(size_t num_iterations, size_t min_iterations_per_thread)
{
size_t max_num_threads = get_num_cpus_pow2(); // number of available threads (power of 2)
size_t desired_num_threads = num_iterations / min_iterations_per_thread;
desired_num_threads = static_cast<size_t>(1ULL << numeric::get_msb(desired_num_threads));
size_t num_threads = std::min(desired_num_threads, max_num_threads); // fewer than max if justified
num_threads = num_threads > 0 ? num_threads : 1; // ensure num_threads is at least 1
return num_threads;
}
} // namespace bb
32 changes: 30 additions & 2 deletions barretenberg/cpp/src/barretenberg/common/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <thread>
#include <vector>

namespace bb {

inline size_t get_num_cpus()
{
#ifdef NO_MULTITHREADING
Expand All @@ -19,7 +21,7 @@ inline size_t get_num_cpus()
// For algorithms that need to be divided amongst power of 2 threads.
inline size_t get_num_cpus_pow2()
{
return static_cast<size_t>(1ULL << bb::numeric::get_msb(get_num_cpus()));
return static_cast<size_t>(1ULL << numeric::get_msb(get_num_cpus()));
}

void parallel_for(size_t num_iterations, const std::function<void(size_t)>& func);
Expand Down Expand Up @@ -89,4 +91,30 @@ inline void run_loop_in_parallel_if_effective_with_index(size_t num_points,
group_element_doublings_per_iteration,
scalar_multiplications_per_iteration,
sequential_copy_ops_per_iteration);
}
}

const size_t DEFAULT_MIN_ITERS_PER_THREAD = 1 << 4;

/**
* @brief calculates number of threads to create based on minimum iterations per thread
* @details Finds the number of cpus with get_num_cpus(), and calculates `desired_num_threads`
* Returns the min of `desired_num_threads` and `max_num_theads`.
* Note that it will not calculate a power of 2 necessarily, use `calculate_num_threads_pow2` instead
*
* @param num_iterations
* @param min_iterations_per_thread
* @return size_t
*/
size_t calculate_num_threads(size_t num_iterations, size_t min_iterations_per_thread = DEFAULT_MIN_ITERS_PER_THREAD);

/**
* @brief calculates number of threads to create based on minimum iterations per thread, guaranteed power of 2
* @details Same functionality as `calculate_num_threads` but guaranteed power of 2
* @param num_iterations
* @param min_iterations_per_thread
* @return size_t
*/
size_t calculate_num_threads_pow2(size_t num_iterations,
size_t min_iterations_per_thread = DEFAULT_MIN_ITERS_PER_THREAD);

} // namespace bb
40 changes: 0 additions & 40 deletions barretenberg/cpp/src/barretenberg/common/thread_utils.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions barretenberg/cpp/src/barretenberg/common/thread_utils.hpp

This file was deleted.

3 changes: 0 additions & 3 deletions barretenberg/cpp/src/barretenberg/crypto/blake2s/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using namespace bb;

extern "C" {

WASM_EXPORT void blake2s(uint8_t const* data, out_buf32 out)
{
std::vector<uint8_t> inputv;
Expand All @@ -31,4 +29,3 @@ WASM_EXPORT void blake2s_to_field_(uint8_t const* data, fr::out_buf r)
auto result = bb::fr::serialize_from_buffer(output.data());
bb::fr::serialize_to_buffer(result, r);
}
}
6 changes: 0 additions & 6 deletions barretenberg/cpp/src/barretenberg/crypto/blake2s/c_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@
#include <cstddef>
#include <cstdint>

extern "C" {

using namespace bb;

WASM_EXPORT void blake2s(uint8_t const* data, out_buf32 r);

WASM_EXPORT void blake2s_to_field_(uint8_t const* data, fr::out_buf r);
}
18 changes: 10 additions & 8 deletions barretenberg/cpp/src/barretenberg/crypto/ecdsa/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "ecdsa.hpp"
#include <barretenberg/ecc/curves/secp256k1/secp256k1.hpp>

using namespace bb;
using namespace bb::crypto;

WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf)
{
auto priv_key = from_buffer<secp256k1::fr>(private_key);
Expand All @@ -18,9 +21,9 @@ WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message,
using serialize::write;
auto priv_key = from_buffer<secp256k1::fr>(private_key);
secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key;
bb::crypto::ecdsa_key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key };
ecdsa_key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key };

auto sig = bb::crypto::ecdsa_construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
auto sig = ecdsa_construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), key_pair);
write(output_sig_r, sig.r);
write(output_sig_s, sig.s);
Expand All @@ -39,10 +42,9 @@ WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

bb::crypto::ecdsa_signature sig = { r, s, v };
auto recovered_pub_key =
bb::crypto::ecdsa_recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), sig);
ecdsa_signature sig = { r, s, v };
auto recovered_pub_key = ecdsa_recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), sig);
serialize::write(output_pub_key, recovered_pub_key);
}

Expand All @@ -59,7 +61,7 @@ WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message,
std::copy(sig_s, sig_s + 32, s.begin());
const uint8_t v = *sig_v;

bb::crypto::ecdsa_signature sig = { r, s, v };
return bb::crypto::ecdsa_verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
ecdsa_signature sig = { r, s, v };
return ecdsa_verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>(
std::string((char*)message, msg_len), pubk, sig);
}
Loading