Skip to content

Commit

Permalink
Fixes and enhancements to partial match and PSI logic
Browse files Browse the repository at this point in the history
* Fixes to partial match and PSI logic.
* Enhancements to perform ptxt-to-ctxt comparisons.

Co-authored-by: @hamishun
  • Loading branch information
jlhcrawford committed Oct 1, 2021
1 parent c6c175b commit e69aa98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ HElib 2.2.1, October 2021

* Improved NTT bechmark code
* CI update
* Improvements to partial match logic
* Bug fixes

HElib 2.2.0, September 2021
=========================
Expand Down
36 changes: 28 additions & 8 deletions include/helib/partialMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,24 @@ Matrix<Ctxt> calculateMasks(const EncryptedArray& ea,
mask = mask.columns(columns);
mask.inPlaceTranspose();

(mask -= database)
// FIXME: Avoid deep copy
// Ptxt Query
if constexpr (std::is_same_v<TXT, Ptxt<BGV>>) {
auto tmp = database.deepCopy();
(tmp -= mask)
.apply([&](auto& entry) { mapTo01(ea, entry); })
.apply([](auto& entry) { entry.negate(); })
.apply([](auto& entry) { entry.addConstant(NTL::ZZX(1l)); });

return mask;
return tmp;
} else { // Ctxt Query
(mask -= database)
.apply([&](auto& entry) { mapTo01(ea, entry); })
.apply([](auto& entry) { entry.negate(); })
.apply([](auto& entry) { entry.addConstant(NTL::ZZX(1l)); });

return mask;
}
}

/**
Expand Down Expand Up @@ -569,8 +581,8 @@ class Database
* match or no match respectively.
**/
template <typename TXT2>
Matrix<TXT2> contains(const Query_t& lookup_query,
const Matrix<TXT2>& query_data) const;
auto contains(const Query_t& lookup_query,
const Matrix<TXT2>& query_data) const;

// FIXME: Combination of TXT = ctxt and TXT2 = ptxt does not work
/**
Expand All @@ -583,8 +595,8 @@ class Database
* @return A `Matrix<TXT2>` containing a score on weighted matches.
**/
template <typename TXT2>
Matrix<TXT2> getScore(const Query_t& weighted_query,
const Matrix<TXT2>& query_data) const;
auto getScore(const Query_t& weighted_query,
const Matrix<TXT2>& query_data) const;

// TODO - correct name?
/**
Expand All @@ -593,14 +605,16 @@ class Database
**/
long columns() { return data.dims(1); }

Matrix<TXT>& getData();

private:
Matrix<TXT> data;
std::shared_ptr<const Context> context;
};

template <typename TXT>
template <typename TXT2>
inline Matrix<TXT2> Database<TXT>::contains(
inline auto Database<TXT>::contains(
const Query_t& lookup_query,
const Matrix<TXT2>& query_data) const
{
Expand All @@ -619,7 +633,7 @@ inline Matrix<TXT2> Database<TXT>::contains(

template <typename TXT>
template <typename TXT2>
inline Matrix<TXT2> Database<TXT>::getScore(
inline auto Database<TXT>::getScore(
const Query_t& weighted_query,
const Matrix<TXT2>& query_data) const
{
Expand All @@ -633,6 +647,12 @@ inline Matrix<TXT2> Database<TXT>::getScore(
return result;
}

template <typename TXT>
inline Matrix<TXT>& Database<TXT>::getData()
{
return data;
}

} // namespace helib

#endif
4 changes: 2 additions & 2 deletions utils/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#include <helib/helib.h>

std::string stripExtension(const std::string& s)
inline std::string stripExtension(const std::string& s)
{
std::size_t dotPos = s.find_last_of(".");
return (dotPos == std::string::npos) ? s : s.substr(0, dotPos);
}

std::string readline(std::istream& is)
inline std::string readline(std::istream& is)
{
std::string s;
getline(is, s);
Expand Down

0 comments on commit e69aa98

Please sign in to comment.