Skip to content

Commit

Permalink
Added support for clang and _BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Sep 28, 2023
1 parent abab111 commit 0bfd980
Show file tree
Hide file tree
Showing 24 changed files with 102 additions and 60 deletions.
1 change: 0 additions & 1 deletion include/circuitbootstrapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,5 @@ void CircuitBootstrappingFFTwithInv(
TwistIFFT<typename privksP::targetP>(invtrgswfft[i][j],
trgsw[i][j]);
}
#include "externs/circuitbootstrapping.hpp"

} // namespace TFHEpp
1 change: 0 additions & 1 deletion include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,5 +594,4 @@ struct EvalKey {
}
};

#include "externs/cloudkey.hpp"
} // namespace TFHEpp
1 change: 0 additions & 1 deletion include/detwfa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,4 @@ void CMUXRAINTTwithPolynomialMulByXaiMinusOne(TRLWE<P> &acc, const TRGSWRAINTT<P
for (int i = 0; i < P::n; i++) acc[k][i] += temp[k][i];
}

#include "externs/detwfa.hpp"
} // namespace TFHEpp
5 changes: 4 additions & 1 deletion include/externs/circuitbootstrapping.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../circuitbootstrapping.hpp"

namespace TFHEpp{
#define INST(iksP, bkP, privksP) \
extern template void CircuitBootstrapping<iksP, bkP, privksP>( \
TRGSW<typename privksP::targetP> & trgsw, \
Expand Down Expand Up @@ -41,4 +43,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_CIRCUIT_BOOTSTRAPPING(INST)
TRGSWFFT<typename privksP::targetP> & invtrgswfft, \
const TLWE<typename iksP::domainP> &tlwe, const EvalKey &ek)
TFHEPP_EXPLICIT_INSTANTIATION_CIRCUIT_BOOTSTRAPPING(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/cloudkey.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../cloudkey.hpp"

namespace TFHEpp{
#define INST(P) \
extern template void bkgen<P>(BootstrappingKey<P> & bk, const SecretKey& sk)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
Expand Down Expand Up @@ -121,4 +123,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST)
extern template SubsetPrivateKeySwitchingKey<P>& EvalKey::getsubprivksk<P>( \
const std::string& key) const
TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/detwfa.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../detwfa.hpp"

namespace TFHEpp{
#define INST(P) \
extern template void CMUXFFT<P>(TRLWE<P> & res, const TRGSWFFT<P> &cs, \
const TRLWE<P> &c1, const TRLWE<P> &c0)
Expand All @@ -17,4 +19,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
extern template void CMUXNTTwithPolynomialMulByXaiMinusOne<P>( \
TRLWE<P> & acc, const TRGSWNTT<P> &cs, const typename P::T a)
TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/gate.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../gate.hpp"

namespace TFHEpp{
#define INST(P) extern template void HomCONSTANTONE<P>(TLWE<P> & res)
INST(lvl1param);
INST(lvl0param);
Expand Down Expand Up @@ -184,4 +186,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
const TLWE<typename iksP::domainP> &c1, \
const TLWE<typename iksP::domainP> &c0, const EvalKey &ek)
TFHEPP_EXPLICIT_INSTANTIATION_GATE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/gatebootstrapping.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once
#include"../gatebootstrapping.hpp"

namespace TFHEpp{
#define INST(P) \
extern template void GateBootstrappingTLWE2TLWEFFT<P>( \
TLWE<typename P::targetP> & res, \
const TLWE<typename P::domainP> &tlwe, \
const BootstrappingKeyFFT<P> &bkfft, \
const Polynomial<typename P::targetP> &testvector)
TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/keyswitch.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../keyswitch.hpp"

namespace TFHEpp{
#define INST(P) \
extern template void IdentityKeySwitch<P>(TLWE<typename P::targetP> & res, \
const TLWE<typename P::domainP> &tlwe, \
Expand Down Expand Up @@ -45,4 +47,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST)
const TLWE<typename P::targetP> &tlwe, \
const SubsetPrivateKeySwitchingKey<P> &privksk)
TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/tlwe.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../tlwe.hpp"

namespace TFHEpp{
#define INST(P) \
extern template TLWE<P> tlweSymEncrypt<P>(const typename P::T p, const double α, \
const Key<P> &key)
Expand Down Expand Up @@ -33,4 +35,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TLWE(INST)
extern template std::vector<uint8_t> bootsSymDecrypt<P>( \
const std::vector<TLWE<P>> &c, const SecretKey &sk)
TFHEPP_EXPLICIT_INSTANTIATION_TLWE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/trgsw.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../trgsw.hpp"

namespace TFHEpp{
#define INST(P) \
extern template void Decomposition<P>( \
DecomposedPolynomial<P> & decpoly, const Polynomial<P> &poly, \
Expand Down Expand Up @@ -48,4 +50,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
extern template TRGSWNTT<P> trgswnttSymEncrypt<P>( \
const Polynomial<P> &p, const double α, const Key<P> &key)
TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
#undef INST
#undef INST
}
5 changes: 4 additions & 1 deletion include/externs/trlwe.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include"../trlwe.hpp"

namespace TFHEpp{
#define INST(P) \
extern template TRLWE<P> trlweSymEncryptZero<P>(const double α, const Key<P> &key)
TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
Expand Down Expand Up @@ -40,4 +42,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
extern template void InvSampleExtractIndex<P>( \
TRLWE<P> & trlwe, const TLWE<P> &tlwe, const int index)
TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST)
#undef INST
#undef INST
}
1 change: 0 additions & 1 deletion include/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,4 @@ void ExtractSwitchAndHomMUX(TRLWE<lvl1param> &res, const TRLWE<lvl1param> &csr,
const TRLWE<lvl1param> &c1r,
const TRLWE<lvl1param> &c0r, const EvalKey &ek);

#include "externs/gate.hpp"
} // namespace TFHEpp
1 change: 0 additions & 1 deletion include/gatebootstrapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,4 @@ void GateBootstrappingNTT(TLWE<typename bkP::targetP> &res,
μpolygen<typename bkP::targetP, μ>());
}

#include "externs/gatebootstrapping.hpp"
} // namespace TFHEpp
1 change: 0 additions & 1 deletion include/keyswitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,4 @@ void SubsetPrivKeySwitch(TRLWE<typename P::targetP> &res,
}
}

#include "externs/keyswitch.hpp"
} // namespace TFHEpp
52 changes: 26 additions & 26 deletions include/mulfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
namespace TFHEpp {

inline const std::unique_ptr<
const std::array<std::array<cuHEpp::INTorus, TFHEpp::lvl1param::n>, 2>>
ntttwistlvl1 = cuHEpp::TwistGen<TFHEpp::lvl1param::nbit>();
const std::array<std::array<cuHEpp::INTorus, lvl1param::n>, 2>>
ntttwistlvl1 = cuHEpp::TwistGen<lvl1param::nbit>();
inline const std::unique_ptr<
const std::array<std::array<cuHEpp::INTorus, TFHEpp::lvl1param::n>, 2>>
ntttablelvl1 = cuHEpp::TableGen<TFHEpp::lvl1param::nbit>();
const std::array<std::array<cuHEpp::INTorus, lvl1param::n>, 2>>
ntttablelvl1 = cuHEpp::TableGen<lvl1param::nbit>();
inline const std::unique_ptr<
const std::array<std::array<cuHEpp::INTorus, TFHEpp::lvl2param::n>, 2>>
ntttwistlvl2 = cuHEpp::TwistGen<TFHEpp::lvl2param::nbit>();
const std::array<std::array<cuHEpp::INTorus, lvl2param::n>, 2>>
ntttwistlvl2 = cuHEpp::TwistGen<lvl2param::nbit>();
inline const std::unique_ptr<
const std::array<std::array<cuHEpp::INTorus, TFHEpp::lvl2param::n>, 2>>
ntttablelvl2 = cuHEpp::TableGen<TFHEpp::lvl2param::nbit>();
inline const std::unique_ptr<std::array<std::array<raintt::SWord, TFHEpp::lvl1param::n>, 2>>
raintttwist = raintt::TwistGen<TFHEpp::lvl1param::nbit,3>();
inline const std::unique_ptr<std::array<std::array<std::array<raintt::SWord, TFHEpp::lvl1param::n>, 2>, 2>>
raintttable = raintt::TableGen<TFHEpp::lvl1param::nbit>();
const std::array<std::array<cuHEpp::INTorus, lvl2param::n>, 2>>
ntttablelvl2 = cuHEpp::TableGen<lvl2param::nbit>();
inline const std::unique_ptr<std::array<std::array<raintt::SWord, lvl1param::n>, 2>>
raintttwist = raintt::TwistGen<lvl1param::nbit,3>();
inline const std::unique_ptr<std::array<std::array<std::array<raintt::SWord, lvl1param::n>, 2>, 2>>
raintttable = raintt::TableGen<lvl1param::nbit>();
#ifdef USE_HEXL
// Biggest prime number less than 2^30 and staisfies 1 mod 2N.
constexpr uint64_t lvl1P = 1073707009;
Expand All @@ -44,23 +44,23 @@ constexpr uint64_t lvl1P = 1073707009;
template <class P>
inline void TwistNTT(Polynomial<P> &res, PolynomialNTT<P> &a)
{
if constexpr (std::is_same_v<P, TFHEpp::lvl1param>)
if constexpr (std::is_same_v<P, lvl1param>)
#ifdef USE_HEXL
{
std::array<uint64_t, TFHEpp::lvl1param::n> temp;
static intel::hexl::NTT nttlvl1(TFHEpp::lvl1param::n, lvl1P);
std::array<uint64_t, lvl1param::n> temp;
static intel::hexl::NTT nttlvl1(lvl1param::n, lvl1P);
nttlvl1.ComputeInverse(temp.data(), &(a[0].value), 1, 1);
for (int i = 0; i < TFHEpp::lvl1param::n; i++)
for (int i = 0; i < lvl1param::n; i++)
res[i] = (temp[i] << 32) / lvl1P;
}
#else
cuHEpp::TwistNTT<typename TFHEpp::lvl1param::T,
TFHEpp::lvl1param::nbit>(res, a, (*ntttablelvl1)[0],
cuHEpp::TwistNTT<typename lvl1param::T,
lvl1param::nbit>(res, a, (*ntttablelvl1)[0],
(*ntttwistlvl1)[0]);
#endif
else if constexpr (std::is_same_v<typename P::T, uint64_t>) {
cuHEpp::TwistNTT<typename TFHEpp::lvl2param::T,
TFHEpp::lvl2param::nbit>(res, a, (*ntttablelvl2)[0],
cuHEpp::TwistNTT<typename lvl2param::T,
lvl2param::nbit>(res, a, (*ntttablelvl2)[0],
(*ntttwistlvl2)[0]);
}
else
Expand All @@ -70,7 +70,7 @@ inline void TwistNTT(Polynomial<P> &res, PolynomialNTT<P> &a)
template <class P>
inline void TwistFFT(Polynomial<P> &res, const PolynomialInFD<P> &a)
{
if constexpr (std::is_same_v<P, TFHEpp::lvl1param>) {
if constexpr (std::is_same_v<P, lvl1param>) {
if constexpr (std::is_same_v<typename P::T, uint32_t>)
fftplvl1.execute_direct_torus32(res.data(), a.data());
if constexpr (std::is_same_v<typename P::T, uint64_t>)
Expand All @@ -96,13 +96,13 @@ inline void TwistFFTrescale(Polynomial<P> &res, const PolynomialInFD<P> &a)
template <class P>
inline void TwistINTT(PolynomialNTT<P> &res, const Polynomial<P> &a)
{
if constexpr (std::is_same_v<P, TFHEpp::lvl1param>)
if constexpr (std::is_same_v<P, lvl1param>)
#ifdef USE_HEXL
{
std::array<uint64_t, TFHEpp::lvl1param::n> temp;
for (int i = 0; i < TFHEpp::lvl1param::n; i++)
std::array<uint64_t, lvl1param::n> temp;
for (int i = 0; i < lvl1param::n; i++)
temp[i] = (lvl1P * static_cast<uint64_t>(a[i])) >> 32;
static intel::hexl::NTT nttlvl1(TFHEpp::lvl1param::n, lvl1P);
static intel::hexl::NTT nttlvl1(lvl1param::n, lvl1P);
nttlvl1.ComputeForward(&(res[0].value), temp.data(), 1, 1);
}
#else
Expand All @@ -119,7 +119,7 @@ inline void TwistINTT(PolynomialNTT<P> &res, const Polynomial<P> &a)
template <class P>
inline void TwistIFFT(PolynomialInFD<P> &res, const Polynomial<P> &a)
{
if constexpr (std::is_same_v<P, TFHEpp::lvl1param>) {
if constexpr (std::is_same_v<P, lvl1param>) {
if constexpr (std::is_same_v<typename P::T, uint32_t>)
fftplvl1.execute_reverse_torus32(res.data(), a.data());
if constexpr (std::is_same_v<typename P::T, uint64_t>)
Expand Down
10 changes: 5 additions & 5 deletions include/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ template <class P>
using TLWE = std::array<typename P::T, P::k * P::n + 1>;

template <class P>
using Polynomial alignas(64) = std::array<typename P::T, P::n>;
using Polynomial = std::array<typename P::T, P::n>;
template <class P>
using UnsignedPolynomial = Polynomial<P>;
template <class P>
using PolynomialInFD alignas(64) = std::array<double, P::n>;
using PolynomialInFD = std::array<double, P::n>;
template <class P>
using PolynomialNTT = std::array<cuHEpp::INTorus, P::n>;
template <class P>
Expand All @@ -68,13 +68,13 @@ using DecomposedPolynomialRAINTT = std::array<PolynomialRAINTT<P>, P::l>;


template <class P>
using TRLWE alignas(64) = std::array<Polynomial<P>, P::k + 1>;
using TRLWE = std::array<Polynomial<P>, P::k + 1>;
template <class P>
using UnsignedTRLWE = std::array<Polynomial<P>, P::k + 1>;
template <class P>
using TRLWE3 = std::array<Polynomial<P>, 3>;
template <class P>
using TRLWEInFD alignas(64) = std::array<PolynomialInFD<P>, P::k + 1>;
using TRLWEInFD = std::array<PolynomialInFD<P>, P::k + 1>;
template <class P>
using TRLWENTT = std::array<PolynomialNTT<P>, P::k + 1>;
template <class P>
Expand All @@ -83,7 +83,7 @@ using TRLWERAINTT = std::array<PolynomialRAINTT<P>, P::k + 1>;
template <class P>
using TRGSW = std::array<TRLWE<P>, (P::k + 1) * P::l>;
template <class P>
using TRGSWFFT alignas(64) = std::array<TRLWEInFD<P>, (P::k + 1) * P::l>;
using TRGSWFFT = std::array<TRLWEInFD<P>, (P::k + 1) * P::l>;
template <class P>
using TRGSWNTT = std::array<TRLWENTT<P>, (P::k + 1) * P::l>;
template <class P>
Expand Down
19 changes: 14 additions & 5 deletions include/raintt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ constexpr T ipow(T num, unsigned int pow)
: num * ipow(num, pow - 1);
}

#ifdef __clang__
//Currently _BigInt is only implemented in clang
constexpr uint wordbits = 31;
using Word = unsigned _BitInt(wordbits);
using SWord = signed _BitInt(wordbits);
using DoubleWord = unsigned _BitInt(2*wordbits);
using DoubleSWord = signed _BitInt(2*wordbits);
#else
constexpr uint wordbits = 32;
using Word = uint32_t;
using SWord = int32_t;
using DoubleWord = uint64_t;
using DoubleSWord = int64_t;
#endif
constexpr uint k = 5;
constexpr uint radixbit = 3;
constexpr uint radixs2 = 1U << (radixbit - 1);
constexpr Word K = ipow<Word>(k, radixs2);
constexpr uint shiftunit = 5;
constexpr uint shiftamount = radixs2 * shiftunit;
constexpr SWord shiftval = 1 << shiftamount;
constexpr uint wordbits = 32;
constexpr Word wordmask = (1ULL << wordbits) - 1;
constexpr SWord P = (K << shiftamount) + 1;

Expand Down Expand Up @@ -54,9 +63,9 @@ inline SWord SREDC(const DoubleSWord a)
return a1 - t1;
}

inline SWord AddMod(const SWord a, const SWord b)
inline SWord AddMod(const DoubleSWord a, const DoubleSWord b)
{
SWord add = a + b;
DoubleSWord add = a + b;
if (add >= P)
return add - P;
else if (add <= -P)
Expand All @@ -65,9 +74,9 @@ inline SWord AddMod(const SWord a, const SWord b)
return add;
}

inline SWord SubMod(const SWord a, const SWord b)
inline SWord SubMod(const DoubleSWord a, const DoubleSWord b)
{
SWord sub = a - b;
DoubleSWord sub = a - b;
if (sub >= P)
return sub - P;
else if (sub <= -P)
Expand Down
Loading

0 comments on commit 0bfd980

Please sign in to comment.