Skip to content

Commit

Permalink
Fix nested_cmux
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Mar 25, 2024
1 parent 165c43f commit 6bfb9e2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
2 changes: 1 addition & 1 deletion include/params/concrete.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct lvl2param {
static constexpr std::uint32_t Bg = 1 << Bgbit;
static constexpr ErrorDistribution errordist =
ErrorDistribution::ModularGaussian;
static const inline double α = std::pow(2.0, -39); // fresh noise
static const inline double α = std::pow(2.0, -38); // fresh noise
using T = uint64_t; // Torus representation
static constexpr T μ = 1ULL << 61;
static constexpr uint32_t plain_modulus = 8;
Expand Down
14 changes: 9 additions & 5 deletions include/trlwe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,23 @@ TRLWE<P> trlweSymIntEncrypt(const std::array<typename P::T, P::n> &p,
}

template <class P>
std::array<bool, P::n> trlweSymDecrypt(const TRLWE<P> &c, const Key<P> &key)
Polynomial<P> trlwePhase(const TRLWE<P> &c, const Key<P> &key)
{
Polynomial<P> phase = c[P::k];
for (int k = 0; k < P::k; k++) {
Polynomial<P> mulres;
std::array<typename P::T, P::n> partkey;
for (int i = 0; i < P::n; i++) partkey[i] = key[k * P::n + i];
PolyMul<P>(mulres, c[k], partkey);
if constexpr (hasq<P>)
for (int i = 0; i < P::n; i++) phase[i] += P::q - mulres[i];
else
for (int i = 0; i < P::n; i++) phase[i] -= mulres[i];
for (int i = 0; i < P::n; i++) phase[i] -= mulres[i];
}
return phase;
}

template <class P>
std::array<bool, P::n> trlweSymDecrypt(const TRLWE<P> &c, const Key<P> &key)
{
Polynomial<P> phase = trlwePhase<P>(c, key);

std::array<bool, P::n> p;
if constexpr (hasq<P>) {
Expand Down
19 changes: 3 additions & 16 deletions test/nested_cmux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,10 @@ PolyLvl1 uint2weight(uint64_t n)
TRLWELvl1 trivial_TRLWELvl1(const PolyLvl1 &src)
{
TRLWELvl1 ret = {};
ret[1] = src;
ret[TFHEpp::lvl1param::k] = src;
return ret;
}

PolyLvl1 phase_of_TRLWELvl1(const TRLWELvl1 &src, const SecretKey &skey)
{
PolyLvl1 as;

TFHEpp::Polynomial<TFHEpp::lvl1param> partkey;
for (int i = 0; i < TFHEpp::lvl1param::n; i++)
partkey[i] = skey.key.lvl1[0 * TFHEpp::lvl1param::n + i];
TFHEpp::PolyMul<Lvl1>(as, src[0], partkey);
PolyLvl1 phase = src[1];
for (size_t i = 0; i < Lvl1::n; i++) phase[i] -= as[i];
return phase;
}

void dump_histgram_of_phase_of_TRLWELvl1(std::ostream &os, const PolyLvl1 &src)
{
std::vector<size_t> hist(10, 0);
Expand Down Expand Up @@ -73,13 +60,13 @@ int main()
c0 = trivial_TRLWELvl1(uint2weight(0));
TRLWELvl1 res = c1;
dump_histgram_of_phase_of_TRLWELvl1(std::cout,
phase_of_TRLWELvl1(res, skey));
TFHEpp::trlwePhase<TFHEpp::lvl1param>(res, skey.key.lvl1));
for (size_t i = 0; i < N; i++) {
TRLWELvl1 tmp = res;
TFHEpp::CMUXFFT<Lvl1>(res, guard.at(i), tmp, c0);
}
dump_histgram_of_phase_of_TRLWELvl1(std::cout,
phase_of_TRLWELvl1(res, skey));
TFHEpp::trlwePhase<TFHEpp::lvl1param>(res, skey.key.lvl1));

/*
PolyLvl1 testvec1 = {}, testvec2 = {};
Expand Down
21 changes: 4 additions & 17 deletions test/nested_cmux_from_cb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,10 @@ PolyLvl1 uint2weight(uint64_t n)
TRLWELvl1 trivial_TRLWELvl1(const PolyLvl1 &src)
{
TRLWELvl1 ret = {};
ret[1] = src;
ret[TFHEpp::lvl1param::k] = src;
return ret;
}

PolyLvl1 phase_of_TRLWELvl1(const TRLWELvl1 &src, const SecretKey &skey)
{
PolyLvl1 as;

TFHEpp::Polynomial<TFHEpp::lvl1param> partkey;
for (int i = 0; i < TFHEpp::lvl1param::n; i++)
partkey[i] = skey.key.lvl1[0 * TFHEpp::lvl1param::n + i];
TFHEpp::PolyMul<Lvl1>(as, src[0], partkey);
PolyLvl1 phase = src[1];
for (size_t i = 0; i < Lvl1::n; i++) phase[i] -= as[i];
return phase;
}

void dump_histgram_of_phase_of_TRLWELvl1(std::ostream &os, const PolyLvl1 &src)
{
std::vector<size_t> hist(10, 0);
Expand All @@ -57,7 +44,7 @@ void dump_histgram_of_phase_of_TRLWELvl1(std::ostream &os, const PolyLvl1 &src)

int main()
{
const size_t N = 500;
const size_t N = 10;
std::cout << "N = " << N << std::endl;

SecretKey skey;
Expand All @@ -80,13 +67,13 @@ int main()
c0 = trivial_TRLWELvl1(uint2weight(0));
TRLWELvl1 res = c1;
dump_histgram_of_phase_of_TRLWELvl1(std::cout,
phase_of_TRLWELvl1(res, skey));
TFHEpp::trlwePhase<TFHEpp::lvl1param>(res, skey.key.lvl1));
for (size_t i = 0; i < N; i++) {
TRLWELvl1 tmp = res;
TFHEpp::CMUXFFT<Lvl1>(res, guard.at(i), tmp, c0);
}
dump_histgram_of_phase_of_TRLWELvl1(std::cout,
phase_of_TRLWELvl1(res, skey));
TFHEpp::trlwePhase<TFHEpp::lvl1param>(res, skey.key.lvl1));

/*
PolyLvl1 testvec1 = {}, testvec2 = {};
Expand Down

0 comments on commit 6bfb9e2

Please sign in to comment.