From 884f7adc0b42857f1563813b6d8db2d4ba4e80ec Mon Sep 17 00:00:00 2001 From: Elliott Lawrence Date: Wed, 16 Mar 2022 12:35:22 -0700 Subject: [PATCH] Add helper for generating random bool vectors (#82) Summary: Pull Request resolved: https://github.com/facebookresearch/fbpcf/pull/82 This is used in a couple places. Reviewed By: adshastri Differential Revision: D34830908 fbshipit-source-id: 8800332c1221c05874faaa6b7df40ef891205a69 --- .../test/benchmarks/OtBenchmark.cpp | 33 ++++--------------- .../util/test/benchmarks/BenchmarkHelper.h | 9 +++++ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/fbpcf/engine/tuple_generator/oblivious_transfer/test/benchmarks/OtBenchmark.cpp b/fbpcf/engine/tuple_generator/oblivious_transfer/test/benchmarks/OtBenchmark.cpp index 0236f846..3f751e51 100644 --- a/fbpcf/engine/tuple_generator/oblivious_transfer/test/benchmarks/OtBenchmark.cpp +++ b/fbpcf/engine/tuple_generator/oblivious_transfer/test/benchmarks/OtBenchmark.cpp @@ -37,14 +37,7 @@ class NpBaseObliviousTransferBenchmark : public util::NetworkedBenchmark { sender_ = factory.create(std::move(agent0)); receiver_ = factory.create(std::move(agent1)); - std::random_device rd; - std::mt19937_64 e(rd()); - std::uniform_int_distribution randomChoice(0, 1); - - choice_ = std::vector(size_); - for (auto i = 0; i < size_; ++i) { - choice_[i] = randomChoice(e); - } + choice_ = util::getRandomBoolVector(size_); } void runSender() override { @@ -213,13 +206,13 @@ class BidirectionObliviousTransferBenchmark : public util::NetworkedBenchmark { std::make_unique>( 1, *agentFactory1_, getRcotFactory()); - senderInput0_ = getRandomBoolVector(); - senderInput1_ = getRandomBoolVector(); - senderChoice_ = getRandomBoolVector(); + senderInput0_ = util::getRandomBoolVector(size_); + senderInput1_ = util::getRandomBoolVector(size_); + senderChoice_ = util::getRandomBoolVector(size_); - receiverInput0_ = getRandomBoolVector(); - receiverInput1_ = getRandomBoolVector(); - receiverChoice_ = getRandomBoolVector(); + receiverInput0_ = util::getRandomBoolVector(size_); + receiverInput1_ = util::getRandomBoolVector(size_); + receiverChoice_ = util::getRandomBoolVector(size_); } void runSender() override { @@ -241,18 +234,6 @@ class BidirectionObliviousTransferBenchmark : public util::NetworkedBenchmark { getRcotFactory() = 0; private: - std::vector getRandomBoolVector() { - std::random_device rd; - std::mt19937_64 e(rd()); - std::uniform_int_distribution randomChoice(0, 1); - - auto result = std::vector(size_); - for (auto i = 0; i < size_; ++i) { - result[i] = randomChoice(e); - } - return result; - } - size_t size_ = 1000000; std::unique_ptr diff --git a/fbpcf/engine/util/test/benchmarks/BenchmarkHelper.h b/fbpcf/engine/util/test/benchmarks/BenchmarkHelper.h index c1faccda..bf6c676e 100644 --- a/fbpcf/engine/util/test/benchmarks/BenchmarkHelper.h +++ b/fbpcf/engine/util/test/benchmarks/BenchmarkHelper.h @@ -18,10 +18,19 @@ #include "fbpcf/engine/communication/IPartyCommunicationAgent.h" #include "fbpcf/engine/communication/IPartyCommunicationAgentFactory.h" #include "fbpcf/engine/communication/SocketPartyCommunicationAgentFactory.h" +#include "folly/Random.h" #include "folly/logging/xlog.h" namespace fbpcf::engine::util { +inline std::vector getRandomBoolVector(size_t size) { + auto result = std::vector(size); + for (auto i = 0; i < size; ++i) { + result[i] = folly::Random::secureOneIn(2); + } + return result; +} + inline std::pair< std::unique_ptr, std::unique_ptr>