Skip to content

Commit

Permalink
Implement oblivious index operation (facebookresearch#284)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookresearch#284

This is needed for the concat function in AsciiString when accessing a character in the AsciiString while the index is secret.

Overloaded the [ ] operator so the index could be a secret Int object. Iterate through the AsciiString and assign the return value to be the current character if we are at the secret index. Return a signed secret Int object.

Differential Revision: https://www.internalfb.com/diff/D37892641?entry_point=27

fbshipit-source-id: 0b81990773e5635c2d6c33a945c96410ae863f5a
  • Loading branch information
Aining Liu authored and facebook-github-bot committed Jul 21, 2022
1 parent 4fdd81e commit 8f0e184
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions example/edit_distance/AsciiString.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ class AsciiString : public scheduler::SchedulerKeeper<schedulerId> {
return data_[i];
}

frontend::Int<true, 8, true, schedulerId, usingBatch> operator[](
frontend::Int<false, maxWidth, true, schedulerId, usingBatch> i) {
frontend::Int<true, 8, true, schedulerId, usingBatch> ret(0, 0);
for (size_t j = 0; j < maxWidth; j++) {
frontend::Int<false, maxWidth, false, schedulerId, usingBatch> indexInInt(
j);
ret = ret.mux(indexInInt == i, data_[j]);
}
return ret;
}

AsciiString<maxWidth, isSecret, schedulerId, usingBatch> toUpperCase() const;
AsciiString<maxWidth, isSecret, schedulerId, usingBatch> toLowerCase() const;

Expand Down
26 changes: 26 additions & 0 deletions example/edit_distance/test/AsciiStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,30 @@ TEST(AsciiStringTest, testPrivateSizeBatch) {
}
}

TEST(AsciiStringTest, testObliviousIndex) {
scheduler::SchedulerKeeper<0>::setScheduler(
std::make_unique<scheduler::PlaintextScheduler>(
scheduler::WireKeeper::createWithUnorderedMap()));
using SecAsciiString = AsciiString<15, true, 0>;
using PubAsciiString = AsciiString<15, false, 0>;

std::string s1 = "Foo";
std::string s2 = "Testing";

PubAsciiString v1(s1);
SecAsciiString v2(s2, 1);

for (size_t i = 0; i < 15; i++) {
frontend::Int<false, 15, true, 0> privateIndex(i, 0);
if (i < s1.size())
EXPECT_EQ((int64_t)s1[i], v1[privateIndex].openToParty(0).getValue());
else
EXPECT_EQ(0, v1[privateIndex].openToParty(0).getValue());
if (i < s2.size())
EXPECT_EQ((int64_t)s2[i], v2[privateIndex].openToParty(1).getValue());
else
EXPECT_EQ(0, v2[privateIndex].openToParty(1).getValue());
}
}

} // namespace fbpcf::edit_distance

0 comments on commit 8f0e184

Please sign in to comment.