Skip to content

Commit

Permalink
Update ITupleGenerator Interface. Add const + switch composite tuple …
Browse files Browse the repository at this point in the history
…order (#162)

Summary:
Pull Request resolved: #162

Couple of small changes in this diff. Separating out from next diff to ease review

- Make composite tuple input map const
- Switch order of composite tuple from `a = vec<bool>` and `b = bool` to `a = bool` and `b = vec<bool>`. This makes consuming the tuples in the Secret Share engine consistent with normal. (i.e the secrets to be revealed are `u + a` and `v[i] + b[i]` rather than `u + b` and `v[i] + a[i]`.
- Add boolean function to indicate whether composite tuples are supported and won't throw exception

Reviewed By: elliottlawrence

Differential Revision: D35197215

fbshipit-source-id: 29de4c9180b9d0676ef713b623ac30f1e218b5a9
  • Loading branch information
Tal Davidi authored and facebook-github-bot committed Apr 8, 2022
1 parent 38b6d62 commit ea2a7d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 42 deletions.
13 changes: 10 additions & 3 deletions fbpcf/engine/tuple_generator/DummyTupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DummyTupleGenerator final : public ITupleGenerator {
* @inherit doc
*/
std::map<size_t, std::vector<CompositeBooleanTuple>> getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) override {
const 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;
Expand All @@ -36,8 +36,8 @@ class DummyTupleGenerator final : public ITupleGenerator {
result.emplace(tupleSize, std::vector<CompositeBooleanTuple>(tupleCount));
for (int i = 0; i < tupleCount; i++) {
result.at(tupleSize).at(i) = CompositeBooleanTuple(
std::vector<bool>(tupleSize, 0),
0,
std::vector<bool>(tupleSize, 0),
std::vector<bool>(tupleSize, 0));
}
}
Expand All @@ -53,13 +53,20 @@ class DummyTupleGenerator final : public ITupleGenerator {
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSizes,
std::map<size_t, uint32_t>& compositeTupleSizes) override {
const std::map<size_t, uint32_t>& compositeTupleSizes) override {
auto boolResult = getBooleanTuple(tupleSizes);
auto compositeBoolResult = getCompositeTuple(compositeTupleSizes);
return std::make_pair(
std::move(boolResult), std::move(compositeBoolResult));
}

/**
* @inherit doc
*/
bool supportsCompositeTupleGeneration() override {
return true;
}

std::pair<uint64_t, uint64_t> getTrafficStatistics() const override {
return {0, 0};
}
Expand Down
28 changes: 17 additions & 11 deletions fbpcf/engine/tuple_generator/ITupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class ITupleGenerator {
public:
CompositeBooleanTuple() {}

CompositeBooleanTuple(std::vector<bool> a, bool b, std::vector<bool> c)
: a_{a}, c_{c}, b_{b} {
if (a.size() != c.size()) {
CompositeBooleanTuple(bool a, std::vector<bool> b, std::vector<bool> c)
: a_{a}, b_{b}, c_{c} {
if (b.size() != c.size()) {
throw std::invalid_argument("Sizes of a and c must be equal");
}
}

// get the vector of A bit shares
std::vector<bool> getA() {
// get the secret-share of shared bit A
bool getA() {
return a_;
}

// get the secret-share of shared bit B
bool getB() {
// get the vector of B bit shares
std::vector<bool> getB() {
return b_;
}

Expand All @@ -91,8 +91,8 @@ class ITupleGenerator {
}

private:
std::vector<bool> a_, c_;
bool b_;
bool a_;
std::vector<bool> b_, c_;
};

/**
Expand All @@ -108,7 +108,7 @@ class ITupleGenerator {
* @return A map of tuple sizes to vector of those tuples
*/
virtual std::map<size_t, std::vector<CompositeBooleanTuple>>
getCompositeTuple(std::map<size_t, uint32_t>& tupleSizes) = 0;
getCompositeTuple(const std::map<size_t, uint32_t>& tupleSizes) = 0;

/**
* Wrapper method for getBooleanTuple() and getCompositeTuple() which performs
Expand All @@ -119,7 +119,13 @@ class ITupleGenerator {
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::map<size_t, uint32_t>& compositeTupleSizes) = 0;
const std::map<size_t, uint32_t>& compositeTupleSizes) = 0;

/**
* Temporary method to indicate whether it's safe to call composite tuple
* generation methods
*/
virtual bool supportsCompositeTupleGeneration() = 0;

/**
* Get the total amount of traffic transmitted.
Expand Down
5 changes: 3 additions & 2 deletions fbpcf/engine/tuple_generator/TupleGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ std::vector<TupleGenerator::BooleanTuple> TupleGenerator::generateTuples(
}

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

Expand All @@ -68,7 +69,7 @@ std::pair<
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>
TupleGenerator::getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::map<size_t, uint32_t>& tupleSizes) {
const std::map<size_t, uint32_t>& tupleSizes) {
throw std::runtime_error("Not implemented");
}

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

/**
* @inherit doc
Expand All @@ -53,7 +53,11 @@ class TupleGenerator final : public ITupleGenerator {
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::map<size_t, uint32_t>& compositeTupleSizes) override;
const std::map<size_t, uint32_t>& compositeTupleSizes) override;

bool supportsCompositeTupleGeneration() override {
return false;
}

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

Expand Down
30 changes: 15 additions & 15 deletions fbpcf/engine/tuple_generator/TwoPartyTupleGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TwoPartyTupleGenerator::getBooleanTuple(uint32_t size) {

std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>
TwoPartyTupleGenerator::getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) {
const std::map<size_t, uint32_t>& tupleSizes) {
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>> tuples;
for (auto& tupleSizeToCount : tupleSizes) {
size_t tupleSize = std::get<0>(tupleSizeToCount);
Expand All @@ -81,7 +81,7 @@ std::pair<
std::map<size_t, std::vector<ITupleGenerator::CompositeBooleanTuple>>>
TwoPartyTupleGenerator::getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::map<size_t, uint32_t>& tupleSizes) {
const std::map<size_t, uint32_t>& tupleSizes) {
auto normalTuples = getBooleanTuple(tupleSize);
auto compositeTuples = getCompositeTuple(tupleSizes);
return std::make_pair(std::move(normalTuples), std::move(compositeTuples));
Expand Down Expand Up @@ -218,25 +218,25 @@ TwoPartyTupleGenerator::expandRCOTResults(
hashFromAes_.inPlaceHash(receiverMessages);

for (size_t i = 0; i < sender0Messages.size(); i++) {
// a1 = H(k0) ^ H(k1) / a2 = H(l0) ^ H(l1)
__m128i a = _mm_xor_si128(sender0Messages.at(i), sender1Messages.at(i));
// b1 = r / b2 = p
bool b = choiceBits.at(i);
// a1 = r / a2 = p
bool a = choiceBits.at(i);
// b1 = H(k0) ^ H(k1) / b2 = H(l0) ^ H(l1)
__m128i b = _mm_xor_si128(sender0Messages.at(i), sender1Messages.at(i));
// c1 = (H(k0) ^ H(k1)) & r ^ H(k0) + H(lr)
// = H(kr) + H(lr) /
// c2 = (H(l0) ^ H(l1)) & p ^ H(l0) + H(kp)
// = H(lp) + H(kp)
__m128i c = b
__m128i c = a
? _mm_xor_si128(
_mm_xor_si128(a, sender0Messages.at(i)),
_mm_xor_si128(b, sender0Messages.at(i)),
receiverMessages.at(i))
: _mm_xor_si128(sender0Messages.at(i), receiverMessages.at(i));

std::vector<bool> aBits(requestedTupleSize);
std::vector<bool> bBits(requestedTupleSize);
std::vector<bool> cBits(requestedTupleSize);
util::extractLnbToVector(a, aBits);
util::extractLnbToVector(b, bBits);
util::extractLnbToVector(c, cBits);
result[i] = CompositeBooleanTuple(aBits, b, cBits);
result[i] = CompositeBooleanTuple(a, bBits, cBits);
}
} else {
for (size_t i = 0; i < sender0Messages.size(); i++) {
Expand All @@ -250,12 +250,12 @@ TwoPartyTupleGenerator::expandRCOTResults(
// H(lr) / H(kp)
util::AesPrg(receiverMessages.at(i)).getRandomBitsInPlace(receiverGen);

std::vector<bool> a(requestedTupleSize);
auto b = choiceBits.at(i);
auto a = choiceBits.at(i);
std::vector<bool> b(requestedTupleSize);
std::vector<bool> c(requestedTupleSize);
for (size_t j = 0; j < requestedTupleSize; j++) {
a[j] = sender0Gen[j] ^ sender1Gen[j];
c[j] = (a[j] & b) ^ sender0Gen[j] ^ receiverGen[j];
b[j] = sender0Gen[j] ^ sender1Gen[j];
c[j] = (b[j] & a) ^ sender0Gen[j] ^ receiverGen[j];
}
result[i] = CompositeBooleanTuple(a, b, c);
}
Expand Down
8 changes: 6 additions & 2 deletions fbpcf/engine/tuple_generator/TwoPartyTupleGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TwoPartyTupleGenerator final : public ITupleGenerator {
* @inherit doc
*/
std::map<size_t, std::vector<CompositeBooleanTuple>> getCompositeTuple(
std::map<size_t, uint32_t>& tupleSizes) override;
const std::map<size_t, uint32_t>& tupleSizes) override;

/**
* @inherit doc
Expand All @@ -49,7 +49,11 @@ class TwoPartyTupleGenerator final : public ITupleGenerator {
std::map<size_t, std::vector<CompositeBooleanTuple>>>
getNormalAndCompositeBooleanTuples(
uint32_t tupleSize,
std::map<size_t, uint32_t>& compositeTupleSizes) override;
const std::map<size_t, uint32_t>& compositeTupleSizes) override;

bool supportsCompositeTupleGeneration() override {
return true;
}

/**
* @inherit doc
Expand Down
14 changes: 7 additions & 7 deletions fbpcf/engine/tuple_generator/test/TupleGeneratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ void assertResults(
}

for (int i = 0; i < tupleCount; i++) {
std::vector<bool> a(compositeSize);
bool b = false;
bool a = false;
std::vector<bool> b(compositeSize);
std::vector<bool> c(compositeSize);

for (int j = 0; j < numberOfParty; j++) {
b ^= std::get<1>(results[j]).at(compositeSize)[i].getB();
auto aShares = std::get<1>(results[j]).at(compositeSize)[i].getA();
a ^= std::get<1>(results[j]).at(compositeSize)[i].getA();
auto bShares = std::get<1>(results[j]).at(compositeSize)[i].getB();
auto cShares = std::get<1>(results[j]).at(compositeSize)[i].getC();

ASSERT_EQ(aShares.size(), compositeSize);
ASSERT_EQ(bShares.size(), compositeSize);
ASSERT_EQ(cShares.size(), compositeSize);
for (int k = 0; k < compositeSize; k++) {
a.at(k) = a.at(k) ^ aShares[k];
b.at(k) = b.at(k) ^ bShares[k];
c.at(k) = c.at(k) ^ cShares[k];
}
}

for (int k = 0; k < compositeSize; k++) {
ASSERT_EQ(c[k], a[k] & b);
ASSERT_EQ(c[k], a & b[k]);
}
}
}
Expand Down

0 comments on commit ea2a7d1

Please sign in to comment.