Skip to content

Commit

Permalink
Refactor ORAM code a bit to improve readability (#141)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #141

As title

Reviewed By: ashiquemfb

Differential Revision: D35098605

fbshipit-source-id: 3acd932ffc1f5ae21bd4888b57095b07c5672922
  • Loading branch information
RuiyuZhu authored and facebook-github-bot committed Mar 24, 2022
1 parent b47a72e commit 801c397
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
10 changes: 9 additions & 1 deletion fbpcf/mpc_std_lib/oram/DummyDifferenceCalculator_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <stdexcept>
#include "fbpcf/mpc_std_lib/util/util.h"

namespace fbpcf::mpc_std_lib::oram::insecure {
Expand Down Expand Up @@ -61,7 +62,14 @@ DummyDifferenceCalculator<T, indicatorSumWidth>::calculateDifferenceBatch(
subtrahend = subtrahendShares.at(i) - subtrahend;

T minuend = util::Adapters<T>::convertFromBits(boolBuffer.at(i));
rst[i] = indicator * (minuend - subtrahend);
if (indicator == 1) {
rst[i] = minuend - subtrahend;
} else if (indicator == -1) {
rst[i] = subtrahend - minuend;
} else {
throw std::runtime_error("invalid indicator!");
}

} else {
agent_->sendSingleT(indicatorShares.at(i));
agent_->sendSingleT(subtrahendShares.at(i));
Expand Down
7 changes: 4 additions & 3 deletions fbpcf/mpc_std_lib/oram/WriteOnlyOram_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ std::vector<std::vector<T>> WriteOnlyOram<T>::generateMasks(

auto difference = calculator_->calculateDifferenceBatch(
indicatorShares, values, subtrahendShares);

for (size_t i = 0; i < batchSize; i++) {
for (size_t j = 0; j < size_; j++) {
rst[i][j] =
rst[i][j] + indicatorKeyPairs.at(i).first.at(j) * difference.at(i);
bool indicator = indicatorKeyPairs.at(i).first.at(j);
if (indicator) {
rst[i][j] = rst[i][j] + difference.at(i);
}
}
}
return rst;
Expand Down
4 changes: 2 additions & 2 deletions fbpcf/mpc_std_lib/oram/test/WriteOnlyORAMTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void testWriteOnlyOram(
std::unique_ptr<IWriteOnlyOramFactory<T>> factory0,
std::unique_ptr<IWriteOnlyOramFactory<T>> factory1,
size_t oramSize) {
size_t batchSize = 16384;
size_t batchSize = oramSize * 30;

auto [input0, input1, expectedValue] =
util::generateRandomValuesToAdd<T>(oramSize, batchSize);
Expand Down Expand Up @@ -113,7 +113,7 @@ void runOramTestWithDummyComponents() {
insecure::DummyDifferenceCalculatorFactory<T, indicatorSumWidth>>(
false, 0, *factories[1]));

size_t oramSize = 150;
size_t oramSize = 10; // use a smaller number due to performance issue.
testWriteOnlyOram<T>(std::move(factory0), std::move(factory1), oramSize);
}

Expand Down
12 changes: 0 additions & 12 deletions fbpcf/mpc_std_lib/util/aggregationValue_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ inline AggregationValue operator-(const AggregationValue& v) {
return AggregationValue{-v.conversionCount, -v.conversionValue};
}

inline AggregationValue operator*(int sign, const AggregationValue& v1) {
if (sign == 1) {
return v1;
} else if (sign == 0) {
return AggregationValue(0);
} else if (sign == -1) {
return -v1;
} else {
throw std::invalid_argument("can only multiply with -1, 0, 1");
}
}

inline void operator+=(AggregationValue& v1, const AggregationValue& v2) {
v1.conversionCount += v2.conversionCount;
v1.conversionValue += v2.conversionValue;
Expand Down

0 comments on commit 801c397

Please sign in to comment.