Skip to content

Commit

Permalink
Implementing secret int inverse (#199)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #199

Implemented function that inverts secret int.

Reviewed By: chualynn

Differential Revision: D35856963

fbshipit-source-id: 4a1c994c765ea74d138aa2f0f0cf829d7a5c6a06
  • Loading branch information
RuiyuZhu authored and facebook-github-bot committed May 5, 2022
1 parent ea4765c commit bef560e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
2 changes: 2 additions & 0 deletions fbpcf/frontend/Int.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class Int {
operator-(const Int<isSigned, width, isSecretOther, schedulerId, usingBatch>&
other) const;

Int<isSigned, width, isSecret, schedulerId, usingBatch> operator-() const;

template <bool isSecretOther>
Bit<isSecret || isSecretOther, schedulerId, usingBatch> operator<(
const Int<isSigned, width, isSecretOther, schedulerId, usingBatch>& other)
Expand Down
31 changes: 26 additions & 5 deletions fbpcf/frontend/Int_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ Int<isSigned, width, isSecret, schedulerId, usingBatch>::operator+(
return rst;
}

template <
bool isSigned,
int8_t width,
bool isSecret,
int schedulerId,
bool usingBatch>
Int<isSigned, width, isSecret, schedulerId, usingBatch>
Int<isSigned, width, isSecret, schedulerId, usingBatch>::operator-() const {
// inverse operator
static_assert(
isSigned,
"Only signed integers have inverse"); // assert that integer is signed
Int<isSigned, width, isSecret, schedulerId, usingBatch> rst;
for (int8_t i = 1; i < width; i++) {
rst.data_[i] = !data_.at(i);
}
auto carry = !data_.at(0);
rst.data_[0] = data_.at(0);
for (int8_t i = 1; i < width; i++) {
rst.data_[i] = rst.data_[i] ^ carry;
carry = (!rst.data_[i]) & carry;
}
return rst;
}

template <
bool isSigned,
int8_t width,
Expand Down Expand Up @@ -668,11 +693,7 @@ Int<isSigned, width, isSecret1 || isSecret2, schedulerId, usingBatch> min(
template <int8_t width, bool isSecret, int schedulerId, bool usingBatch>
Int<true, width, isSecret, schedulerId, usingBatch> abs(
const Int<true, width, isSecret, schedulerId, usingBatch>& src) {
// The zero object is a helper for flipping negative to positive.
// Note that we create a public zero because it doesn't affect
// the output of zero - src, regardless src is public or secret.
static const Int<true, width, false, schedulerId, usingBatch> zero(0);
return src.mux(src[width - 1], zero - src);
return src.mux(src[width - 1], -src);
}

} // namespace fbpcf::frontend
36 changes: 36 additions & 0 deletions fbpcf/frontend/test/IntTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,42 @@ TEST(IntTest, testAdd) {
}
}

TEST(IntTest, testInverse) {
const int8_t width = 64;
int64_t largestSigned = std::numeric_limits<int64_t>().max();
int64_t smallestSigned = std::numeric_limits<int64_t>().min();
uint64_t largestUnsigned = std::numeric_limits<uint64_t>().max();
scheduler::SchedulerKeeper<0>::setScheduler(
std::make_unique<scheduler::PlaintextScheduler>(
scheduler::WireKeeper::createWithUnorderedMap()));
using secSignedInt = Integer<Secret<Signed<width>>, 0>;
using pubSignedInt = Integer<Public<Signed<width>>, 0>;
int partyId = 2;
secSignedInt mySignedInt;
secSignedInt myInverse;
pubSignedInt myPubSignedInt;
pubSignedInt myPubInverse;
std::random_device rd;
std::mt19937_64 e(rd());
std::uniform_int_distribution<int64_t> dist1(smallestSigned, largestSigned);
std::uniform_int_distribution<uint64_t> dist2(0, largestUnsigned);
int64_t v1;
for (int i = 0; i < 100; i++) {
v1 = dist1(e);
mySignedInt = secSignedInt(v1, partyId);
myInverse = -mySignedInt;
EXPECT_EQ(
mySignedInt.openToParty(partyId).getValue(),
-1 * myInverse.openToParty(partyId).getValue());
}
for (int i = 0; i < 100; i++) {
v1 = dist1(e);
myPubSignedInt = pubSignedInt(v1);
myPubInverse = -myPubSignedInt;
EXPECT_EQ(myPubSignedInt.getValue(), -1 * myPubInverse.getValue());
}
}

template <typename T>
std::vector<T> addVector(
const std::vector<T>& src1,
Expand Down

0 comments on commit bef560e

Please sign in to comment.