Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement oblivious index operation #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions example/edit_distance/AsciiString_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ AsciiString<
usingBatch>
AsciiString<maxWidth, isSecret, schedulerId, usingBatch>::concat(
const AsciiString<otherMaxWidth, isSecretOther, schedulerId, usingBatch>&
other) const {
throw std::runtime_error("Unimplemented");
}
other) const {}

template <int maxWidth, bool isSecret, int schedulerId, bool usingBatch>
template <bool isSecretChoice, bool isSecretOther>
Expand Down
69 changes: 17 additions & 52 deletions example/edit_distance/test/AsciiStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,64 +251,29 @@ TEST(AsciiStringTest, testMuxBatch) {
}
}

TEST(AsciiStringTest, testPrivateSize) {
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>;

int partyId = 0;

std::random_device rd;
std::mt19937_64 e(rd());
std::uniform_int_distribution<char> dist1('a', 'z');
std::uniform_int_distribution<size_t> strLength(1, 15);

for (size_t i = 0; i < 100; i++) {
auto len = strLength(e);
std::string v1 = randomString(dist1, e, len);

SecAsciiString secStr1(v1, partyId);

auto r1 = secStr1.privateSize<16>();

EXPECT_EQ(r1.openToParty(partyId).getValue(), v1.size());
}
}

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

int partyId = 0;
size_t batchSize = 9;

std::random_device rd;
std::mt19937_64 e(rd());
std::uniform_int_distribution<char> dist1('a', 'z');
std::uniform_int_distribution<size_t> strLength(1, 15);

for (size_t i = 0; i < 100; i++) {
std::vector<std::string> v1(batchSize);

for (size_t j = 0; j < batchSize; j++) {
auto len = strLength(e);
v1[j] = randomString(dist1, e, len);
}

SecBatchAsciiString secStr1(v1, partyId);

auto r1 = secStr1.privateSize<16>();

std::vector<size_t> expectedValue(batchSize);
for (size_t j = 0; j < batchSize; j++) {
expectedValue[j] = v1[j].size();
}
std::string s1 = "Foo";
std::string s2 = "Testing";

testVectorEq(r1.openToParty(partyId).getValue(), expectedValue);
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