Skip to content

Commit

Permalink
Change composite tuple generator to use map instead of unordered_map (#…
Browse files Browse the repository at this point in the history
…129)

Summary:
Pull Request resolved: #129

Since `unordered_map` might have a different order of iteration depending on the platform, there is a potential for different parties to try to generate each tuple size in a different order. This will cause the tuples to not match up since the rcot results will be consumed out of order from the buffer.

I split this up from the next diff to make it easier to review.

Reviewed By: elliottlawrence

Differential Revision: D34980786

fbshipit-source-id: fdcb138b9b86de04895608d70599f28984a41a90
  • Loading branch information
Tal Davidi authored and facebook-github-bot committed Mar 23, 2022
1 parent 5e47cbc commit eed1b21
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.
10 changes: 5 additions & 5 deletions fbpcf/engine/tuple_generator/DummyTupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class DummyTupleGenerator final : public ITupleGenerator {
/**
* @inherit doc
*/
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::unordered_map<size_t, uint32_t>& tupleSizes) override {
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>> result;
std::map<size_t, std::vector<CompositeBooleanTuple>> getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) override {
std::map<size_t, std::vector<CompositeBooleanTuple>> result;
for (auto& countOfTuples : tupleSizes) {
size_t tupleSize = countOfTuples.first;
uint32_t tupleCount = countOfTuples.second;
Expand All @@ -50,10 +50,10 @@ class DummyTupleGenerator final : public ITupleGenerator {
*/
std::pair<
std::vector<BooleanTuple>,
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>>
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSizes,
std::unordered_map<size_t, uint32_t>& compositeTupleSizes) override {
std::map<size_t, uint32_t>& compositeTupleSizes) override {
auto boolResult = getBooleanTuple(tupleSizes);
auto compositeBoolResult = getCompositeTuple(compositeTupleSizes);
return std::make_pair(
Expand Down
10 changes: 5 additions & 5 deletions fbpcf/engine/tuple_generator/ITupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#pragma once

#include <stdint.h>
#include <map>
#include <stdexcept>
#include <unordered_map>
#include <vector>

namespace fbpcf::engine::tuple_generator {
Expand Down Expand Up @@ -107,19 +107,19 @@ class ITupleGenerator {
* tuples to generate.
* @return A map of tuple sizes to vector of those tuples
*/
virtual std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::unordered_map<size_t, uint32_t>& tupleSizes) = 0;
virtual std::map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::map<size_t, uint32_t>& tupleSizes) = 0;

/**
* Wrapper method for getBooleanTuple() and getCompositeTuple() which performs
* only one round of communication.
*/
virtual std::pair<
std::vector<BooleanTuple>,
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>>
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t>& compositeTupleSizes) = 0;
std::map<size_t, uint32_t>& compositeTupleSizes) = 0;

/**
* Get the total amount of traffic transmitted.
Expand Down
11 changes: 4 additions & 7 deletions fbpcf/engine/tuple_generator/TupleGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,17 @@ std::vector<TupleGenerator::BooleanTuple> TupleGenerator::generateTuples(
return booleanTuples;
}

std::unordered_map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>
TupleGenerator::getCompositeTuple(
std::unordered_map<size_t, uint32_t>& tupleSizes) {
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>
TupleGenerator::getCompositeTuple(std::map<size_t, uint32_t>& tupleSizes) {
throw std::runtime_error("Not implemented");
}

std::pair<
std::vector<ITupleGenerator::BooleanTuple>,
std::unordered_map<
size_t,
std::vector<ITupleGenerator::CompositeBooleanTuple>>>
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>
TupleGenerator::getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t>& tupleSizes) {
std::map<size_t, uint32_t>& tupleSizes) {
throw std::runtime_error("Not implemented");
}

Expand Down
8 changes: 4 additions & 4 deletions fbpcf/engine/tuple_generator/TupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ class TupleGenerator final : public ITupleGenerator {
/**
* @inherit doc
*/
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::unordered_map<size_t, uint32_t>& tupleSizes) override;
std::map<size_t, std::vector<CompositeBooleanTuple>> getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) override;

/**
* @inherit doc
*/
std::pair<
std::vector<BooleanTuple>,
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>>
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t>& compositeTupleSizes) override;
std::map<size_t, uint32_t>& compositeTupleSizes) override;

std::pair<uint64_t, uint64_t> getTrafficStatistics() const override;

Expand Down
10 changes: 4 additions & 6 deletions fbpcf/engine/tuple_generator/TwoPartyTupleGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,18 @@ TwoPartyTupleGenerator::generateTuples(uint64_t size) {
return booleanTuples;
}

std::unordered_map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>
TwoPartyTupleGenerator::getCompositeTuple(
std::unordered_map<size_t, uint32_t>& tupleSizes) {
std::map<size_t, uint32_t>& tupleSizes) {
throw std::runtime_error("Not implemented");
}

std::pair<
std::vector<ITupleGenerator::BooleanTuple>,
std::unordered_map<
size_t,
std::vector<ITupleGenerator::CompositeBooleanTuple>>>
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>
TwoPartyTupleGenerator::getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t>& tupleSizes) {
std::map<size_t, uint32_t>& tupleSizes) {
throw std::runtime_error("Not implemented");
}

Expand Down
8 changes: 4 additions & 4 deletions fbpcf/engine/tuple_generator/TwoPartyTupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ class TwoPartyTupleGenerator final : public ITupleGenerator {
/**
* @inherit doc
*/
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::unordered_map<size_t, uint32_t>& tupleSizes) override;
std::map<size_t, std::vector<CompositeBooleanTuple>> getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) override;

/**
* @inherit doc
*/
std::pair<
std::vector<BooleanTuple>,
std::unordered_map<size_t, std::vector<CompositeBooleanTuple>>>
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t>& compositeTupleSizes) override;
std::map<size_t, uint32_t>& compositeTupleSizes) override;

/**
* @inherit doc
Expand Down
20 changes: 9 additions & 11 deletions fbpcf/engine/tuple_generator/test/TupleGeneratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <future>
#include <map>
#include <memory>
#include <thread>

Expand Down Expand Up @@ -37,7 +38,7 @@ void testTupleGenerator(
std::reference_wrapper<communication::IPartyCommunicationAgentFactory>
agentFactory,
uint32_t tupleSize,
std::unordered_map<size_t, uint32_t> compositeTupleSizes) {
std::map<size_t, uint32_t> compositeTupleSizes) {
auto generator = creator(numberOfParty, myId, agentFactory)->create();
if constexpr (testCompositeTuples) {
return generator->getNormalAndCompositeBooleanTuples(
Expand All @@ -46,7 +47,7 @@ void testTupleGenerator(
auto tuples = generator->getBooleanTuple(tupleSize);
return std::make_pair(
std::move(tuples),
std::unordered_map<
std::map<
size_t,
std::vector<ITupleGenerator::CompositeBooleanTuple>>());
}
Expand All @@ -55,19 +56,18 @@ void testTupleGenerator(
// this size is larger than the AES and tuple generator buffer size so we can
// test regeneration.
uint32_t tupleSize = kTestBufferSize * 4;
std::unordered_map<size_t, uint32_t> compositeTuplesSizes(
std::map<size_t, uint32_t> compositeTuplesSizes(
{{8, tupleSize},
{32, tupleSize},
{64, tupleSize},
{128, tupleSize},
{50, tupleSize},
{31, tupleSize}});
{31, tupleSize},
{256, tupleSize}});

std::vector<std::future<std::pair<
std::vector<ITupleGenerator::BooleanTuple>,
std::unordered_map<
size_t,
std::vector<ITupleGenerator::CompositeBooleanTuple>>>>>
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>>>
futures;
for (int i = 0; i < numberOfParty; i++) {
futures.push_back(std::async(
Expand All @@ -83,9 +83,7 @@ void testTupleGenerator(

std::vector<std::pair<
std::vector<ITupleGenerator::BooleanTuple>,
std::unordered_map<
size_t,
std::vector<ITupleGenerator::CompositeBooleanTuple>>>>
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>>
results;
for (int i = 0; i < numberOfParty; i++) {
results.push_back(futures[i].get());
Expand Down Expand Up @@ -126,7 +124,7 @@ void testTupleGenerator(
ASSERT_EQ(cShares.size(), compositeSize);
for (int k = 0; k < compositeSize; k++) {
a.at(k) = a.at(k) ^ aShares[k];
c.at(k) = c.at(k) ^ aShares[k];
c.at(k) = c.at(k) ^ cShares[k];
}
}

Expand Down

0 comments on commit eed1b21

Please sign in to comment.