Skip to content

Commit

Permalink
Added tlweSymPhase and fix IntDecrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Apr 3, 2024
1 parent a58bfa9 commit 37cc841
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions include/tlwe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,37 @@ TLWE<P> tlweSymIntEncrypt(const typename P::T p, const Key<P> &key)
}

template <class P>
bool tlweSymDecrypt(const TLWE<P> &c, const Key<P> &key)
typename P::T tlweSymPhase(const TLWE<P> &c, const Key<P> &key)
{
typename P::T phase = c[P::k * P::n];
for (int k = 0; k < P::k; k++)
for (int i = 0; i < P::n; i++)
phase -= c[k * P::n + i] * key[k * P::n + i];
return phase;
}

template <class P>
bool tlweSymDecrypt(const TLWE<P> &c, const Key<P> &key)
{
typename P::T phase = tlweSymPhase<P>(c, key);
bool res =
static_cast<typename std::make_signed<typename P::T>::type>(phase) > 0;
return res;
}

template <class P, const uint plain_modulus>
typename P::T tlweSymIntDecrypt(const TLWE<P> &c, const Key<P> &key)
{
constexpr double Δ = 2* static_cast<double>(1ULL << (std::numeric_limits<typename P::T>::digits - 1))/plain_modulus;
const typename P::T phase = tlweSymPhase<P>(c, key);
typename P::T res = static_cast<typename P::T>(std::round(phase / Δ));
return res >= plain_modulus/2 ? res - plain_modulus : res;
}

template <class P>
typename P::T tlweSymIntDecrypt(const TLWE<P> &c, const Key<P> &key)
{
typename P::T phase = c[P::k * P::n];
for (int k = 0; k < P::k; k++)
for (int i = 0; i < P::n; i++)
phase -= c[k * P::n + i] * key[k * P::n + i];
typename P::T res = static_cast<typename P::T>(std::round(phase / P::Δ)) %
(2 * P::plain_modulus);
return res;
return tlweSymIntDecrypt<P,P::plain_modulus>(c, key);
}

template <class P>
Expand Down

0 comments on commit 37cc841

Please sign in to comment.