From 0bfd9804662b3342f58cc6d017d27609de0d20fa Mon Sep 17 00:00:00 2001 From: nindanaoto Date: Thu, 28 Sep 2023 11:31:13 +0000 Subject: [PATCH] Added support for clang and _BigInt --- include/circuitbootstrapping.hpp | 1 - include/cloudkey.hpp | 1 - include/detwfa.hpp | 1 - include/externs/circuitbootstrapping.hpp | 5 ++- include/externs/cloudkey.hpp | 5 ++- include/externs/detwfa.hpp | 5 ++- include/externs/gate.hpp | 5 ++- include/externs/gatebootstrapping.hpp | 5 ++- include/externs/keyswitch.hpp | 5 ++- include/externs/tlwe.hpp | 5 ++- include/externs/trgsw.hpp | 5 ++- include/externs/trlwe.hpp | 5 ++- include/gate.hpp | 1 - include/gatebootstrapping.hpp | 1 - include/keyswitch.hpp | 1 - include/mulfft.hpp | 52 ++++++++++++------------ include/params.hpp | 10 ++--- include/raintt.hpp | 19 ++++++--- include/tfhe++.hpp | 14 +++++++ include/tlwe.hpp | 1 - include/trgsw.hpp | 1 - include/trlwe.hpp | 1 - src/CMakeLists.txt | 1 + test/raintt.cpp | 12 +++--- 24 files changed, 102 insertions(+), 60 deletions(-) diff --git a/include/circuitbootstrapping.hpp b/include/circuitbootstrapping.hpp index faf3bdc..3220302 100644 --- a/include/circuitbootstrapping.hpp +++ b/include/circuitbootstrapping.hpp @@ -149,6 +149,5 @@ void CircuitBootstrappingFFTwithInv( TwistIFFT(invtrgswfft[i][j], trgsw[i][j]); } -#include "externs/circuitbootstrapping.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/cloudkey.hpp b/include/cloudkey.hpp index 17ad08d..4148e5c 100644 --- a/include/cloudkey.hpp +++ b/include/cloudkey.hpp @@ -594,5 +594,4 @@ struct EvalKey { } }; -#include "externs/cloudkey.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/detwfa.hpp b/include/detwfa.hpp index f327f9b..008899f 100644 --- a/include/detwfa.hpp +++ b/include/detwfa.hpp @@ -121,5 +121,4 @@ void CMUXRAINTTwithPolynomialMulByXaiMinusOne(TRLWE

&acc, const TRGSWRAINTT

( \ TRGSW & trgsw, \ @@ -41,4 +43,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_CIRCUIT_BOOTSTRAPPING(INST) TRGSWFFT & invtrgswfft, \ const TLWE &tlwe, const EvalKey &ek) TFHEPP_EXPLICIT_INSTANTIATION_CIRCUIT_BOOTSTRAPPING(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/cloudkey.hpp b/include/externs/cloudkey.hpp index fd2127e..76e2c32 100644 --- a/include/externs/cloudkey.hpp +++ b/include/externs/cloudkey.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../cloudkey.hpp" +namespace TFHEpp{ #define INST(P) \ extern template void bkgen

(BootstrappingKey

& bk, const SecretKey& sk) TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) @@ -121,4 +123,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST) extern template SubsetPrivateKeySwitchingKey

& EvalKey::getsubprivksk

( \ const std::string& key) const TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/detwfa.hpp b/include/externs/detwfa.hpp index 4710e46..74434f5 100644 --- a/include/externs/detwfa.hpp +++ b/include/externs/detwfa.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../detwfa.hpp" +namespace TFHEpp{ #define INST(P) \ extern template void CMUXFFT

(TRLWE

& res, const TRGSWFFT

&cs, \ const TRLWE

&c1, const TRLWE

&c0) @@ -17,4 +19,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) extern template void CMUXNTTwithPolynomialMulByXaiMinusOne

( \ TRLWE

& acc, const TRGSWNTT

&cs, const typename P::T a) TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/gate.hpp b/include/externs/gate.hpp index 16df734..657b9d0 100644 --- a/include/externs/gate.hpp +++ b/include/externs/gate.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../gate.hpp" +namespace TFHEpp{ #define INST(P) extern template void HomCONSTANTONE

(TLWE

& res) INST(lvl1param); INST(lvl0param); @@ -184,4 +186,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) const TLWE &c1, \ const TLWE &c0, const EvalKey &ek) TFHEPP_EXPLICIT_INSTANTIATION_GATE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/gatebootstrapping.hpp b/include/externs/gatebootstrapping.hpp index 1eed62f..b9792ba 100644 --- a/include/externs/gatebootstrapping.hpp +++ b/include/externs/gatebootstrapping.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../gatebootstrapping.hpp" +namespace TFHEpp{ #define INST(P) \ extern template void GateBootstrappingTLWE2TLWEFFT

( \ TLWE & res, \ @@ -7,4 +9,5 @@ const BootstrappingKeyFFT

&bkfft, \ const Polynomial &testvector) TFHEPP_EXPLICIT_INSTANTIATION_BLIND_ROTATE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/keyswitch.hpp b/include/externs/keyswitch.hpp index 6ce5c59..92e47b8 100644 --- a/include/externs/keyswitch.hpp +++ b/include/externs/keyswitch.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../keyswitch.hpp" +namespace TFHEpp{ #define INST(P) \ extern template void IdentityKeySwitch

(TLWE & res, \ const TLWE &tlwe, \ @@ -45,4 +47,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST) const TLWE &tlwe, \ const SubsetPrivateKeySwitchingKey

&privksk) TFHEPP_EXPLICIT_INSTANTIATION_KEY_SWITCH_TO_TRLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/tlwe.hpp b/include/externs/tlwe.hpp index 6e9acf8..25477d5 100644 --- a/include/externs/tlwe.hpp +++ b/include/externs/tlwe.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../tlwe.hpp" +namespace TFHEpp{ #define INST(P) \ extern template TLWE

tlweSymEncrypt

(const typename P::T p, const double α, \ const Key

&key) @@ -33,4 +35,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TLWE(INST) extern template std::vector bootsSymDecrypt

( \ const std::vector> &c, const SecretKey &sk) TFHEPP_EXPLICIT_INSTANTIATION_TLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/trgsw.hpp b/include/externs/trgsw.hpp index 6ec7f4e..39cdeb7 100644 --- a/include/externs/trgsw.hpp +++ b/include/externs/trgsw.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../trgsw.hpp" +namespace TFHEpp{ #define INST(P) \ extern template void Decomposition

( \ DecomposedPolynomial

& decpoly, const Polynomial

&poly, \ @@ -48,4 +50,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) extern template TRGSWNTT

trgswnttSymEncrypt

( \ const Polynomial

&p, const double α, const Key

&key) TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/externs/trlwe.hpp b/include/externs/trlwe.hpp index b198df8..771fb35 100644 --- a/include/externs/trlwe.hpp +++ b/include/externs/trlwe.hpp @@ -1,5 +1,7 @@ #pragma once +#include"../trlwe.hpp" +namespace TFHEpp{ #define INST(P) \ extern template TRLWE

trlweSymEncryptZero

(const double α, const Key

&key) TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) @@ -40,4 +42,5 @@ TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) extern template void InvSampleExtractIndex

( \ TRLWE

& trlwe, const TLWE

&tlwe, const int index) TFHEPP_EXPLICIT_INSTANTIATION_TRLWE(INST) -#undef INST \ No newline at end of file +#undef INST +} \ No newline at end of file diff --git a/include/gate.hpp b/include/gate.hpp index b9d5ee5..0b3a276 100644 --- a/include/gate.hpp +++ b/include/gate.hpp @@ -279,5 +279,4 @@ void ExtractSwitchAndHomMUX(TRLWE &res, const TRLWE &csr, const TRLWE &c1r, const TRLWE &c0r, const EvalKey &ek); -#include "externs/gate.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/gatebootstrapping.hpp b/include/gatebootstrapping.hpp index 0ec44c6..77a375a 100644 --- a/include/gatebootstrapping.hpp +++ b/include/gatebootstrapping.hpp @@ -281,5 +281,4 @@ void GateBootstrappingNTT(TLWE &res, μpolygen()); } -#include "externs/gatebootstrapping.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/keyswitch.hpp b/include/keyswitch.hpp index a072643..954fced 100644 --- a/include/keyswitch.hpp +++ b/include/keyswitch.hpp @@ -298,5 +298,4 @@ void SubsetPrivKeySwitch(TRLWE &res, } } -#include "externs/keyswitch.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/mulfft.hpp b/include/mulfft.hpp index c4670eb..9deb294 100644 --- a/include/mulfft.hpp +++ b/include/mulfft.hpp @@ -21,21 +21,21 @@ namespace TFHEpp { inline const std::unique_ptr< - const std::array, 2>> - ntttwistlvl1 = cuHEpp::TwistGen(); + const std::array, 2>> + ntttwistlvl1 = cuHEpp::TwistGen(); inline const std::unique_ptr< - const std::array, 2>> - ntttablelvl1 = cuHEpp::TableGen(); + const std::array, 2>> + ntttablelvl1 = cuHEpp::TableGen(); inline const std::unique_ptr< - const std::array, 2>> - ntttwistlvl2 = cuHEpp::TwistGen(); + const std::array, 2>> + ntttwistlvl2 = cuHEpp::TwistGen(); inline const std::unique_ptr< - const std::array, 2>> - ntttablelvl2 = cuHEpp::TableGen(); -inline const std::unique_ptr, 2>> - raintttwist = raintt::TwistGen(); -inline const std::unique_ptr, 2>, 2>> - raintttable = raintt::TableGen(); + const std::array, 2>> + ntttablelvl2 = cuHEpp::TableGen(); +inline const std::unique_ptr, 2>> + raintttwist = raintt::TwistGen(); +inline const std::unique_ptr, 2>, 2>> + raintttable = raintt::TableGen(); #ifdef USE_HEXL // Biggest prime number less than 2^30 and staisfies 1 mod 2N. constexpr uint64_t lvl1P = 1073707009; @@ -44,23 +44,23 @@ constexpr uint64_t lvl1P = 1073707009; template inline void TwistNTT(Polynomial

&res, PolynomialNTT

&a) { - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) #ifdef USE_HEXL { - std::array temp; - static intel::hexl::NTT nttlvl1(TFHEpp::lvl1param::n, lvl1P); + std::array 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(res, a, (*ntttablelvl1)[0], + cuHEpp::TwistNTT(res, a, (*ntttablelvl1)[0], (*ntttwistlvl1)[0]); #endif else if constexpr (std::is_same_v) { - cuHEpp::TwistNTT(res, a, (*ntttablelvl2)[0], + cuHEpp::TwistNTT(res, a, (*ntttablelvl2)[0], (*ntttwistlvl2)[0]); } else @@ -70,7 +70,7 @@ inline void TwistNTT(Polynomial

&res, PolynomialNTT

&a) template inline void TwistFFT(Polynomial

&res, const PolynomialInFD

&a) { - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v) { if constexpr (std::is_same_v) fftplvl1.execute_direct_torus32(res.data(), a.data()); if constexpr (std::is_same_v) @@ -96,13 +96,13 @@ inline void TwistFFTrescale(Polynomial

&res, const PolynomialInFD

&a) template inline void TwistINTT(PolynomialNTT

&res, const Polynomial

&a) { - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) #ifdef USE_HEXL { - std::array temp; - for (int i = 0; i < TFHEpp::lvl1param::n; i++) + std::array temp; + for (int i = 0; i < lvl1param::n; i++) temp[i] = (lvl1P * static_cast(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 @@ -119,7 +119,7 @@ inline void TwistINTT(PolynomialNTT

&res, const Polynomial

&a) template inline void TwistIFFT(PolynomialInFD

&res, const Polynomial

&a) { - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v) { if constexpr (std::is_same_v) fftplvl1.execute_reverse_torus32(res.data(), a.data()); if constexpr (std::is_same_v) diff --git a/include/params.hpp b/include/params.hpp index a53251f..60a9660 100644 --- a/include/params.hpp +++ b/include/params.hpp @@ -50,11 +50,11 @@ template using TLWE = std::array; template -using Polynomial alignas(64) = std::array; +using Polynomial = std::array; template using UnsignedPolynomial = Polynomial

; template -using PolynomialInFD alignas(64) = std::array; +using PolynomialInFD = std::array; template using PolynomialNTT = std::array; template @@ -68,13 +68,13 @@ using DecomposedPolynomialRAINTT = std::array, P::l>; template -using TRLWE alignas(64) = std::array, P::k + 1>; +using TRLWE = std::array, P::k + 1>; template using UnsignedTRLWE = std::array, P::k + 1>; template using TRLWE3 = std::array, 3>; template -using TRLWEInFD alignas(64) = std::array, P::k + 1>; +using TRLWEInFD = std::array, P::k + 1>; template using TRLWENTT = std::array, P::k + 1>; template @@ -83,7 +83,7 @@ using TRLWERAINTT = std::array, P::k + 1>; template using TRGSW = std::array, (P::k + 1) * P::l>; template -using TRGSWFFT alignas(64) = std::array, (P::k + 1) * P::l>; +using TRGSWFFT = std::array, (P::k + 1) * P::l>; template using TRGSWNTT = std::array, (P::k + 1) * P::l>; template diff --git a/include/raintt.hpp b/include/raintt.hpp index 8c44bb3..92edb86 100644 --- a/include/raintt.hpp +++ b/include/raintt.hpp @@ -14,10 +14,20 @@ 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); @@ -25,7 +35,6 @@ constexpr Word K = ipow(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; @@ -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) @@ -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) diff --git a/include/tfhe++.hpp b/include/tfhe++.hpp index cf71c14..aa41c16 100644 --- a/include/tfhe++.hpp +++ b/include/tfhe++.hpp @@ -14,3 +14,17 @@ #include "tlwe.hpp" #include "trgsw.hpp" #include "trlwe.hpp" + +#include "externs/cloudkey.hpp" +#include "externs/detwfa.hpp" +#include "externs/keyswitch.hpp" +#include "externs/tlwe.hpp" +#include "externs/trgsw.hpp" +#include "externs/trlwe.hpp" + +#ifndef __clang__ +// Because of some resons (may be clang bug?) this will gives linking error caused by mismatching name mangling. +#include "externs/circuitbootstrapping.hpp" +#include "externs/gate.hpp" +#include "externs/gatebootstrapping.hpp" +#endif \ No newline at end of file diff --git a/include/tlwe.hpp b/include/tlwe.hpp index 543e555..03a20d6 100644 --- a/include/tlwe.hpp +++ b/include/tlwe.hpp @@ -98,5 +98,4 @@ std::vector bootsSymDecrypt(const std::vector> &c, return bootsSymDecrypt

(c, sk.key.get

()); } -#include "externs/tlwe.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/trgsw.hpp b/include/trgsw.hpp index cc5f625..fedffbb 100644 --- a/include/trgsw.hpp +++ b/include/trgsw.hpp @@ -318,5 +318,4 @@ TRGSWRAINTT

trgswrainttSymEncrypt(const Polynomial

&p, const double α, return ApplyRAINTT2trgsw

(trgsw); } -#include "externs/trgsw.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/include/trlwe.hpp b/include/trlwe.hpp index faa6e7e..f63c961 100644 --- a/include/trlwe.hpp +++ b/include/trlwe.hpp @@ -105,5 +105,4 @@ void InvSampleExtractIndex(TRLWE

&trlwe, const TLWE

&tlwe, trlwe[P::k][index] = tlwe[P::k * P::n]; } -#include "externs/trlwe.hpp" } // namespace TFHEpp \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2845c90..eb10fae 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,6 +16,7 @@ target_include_directories( ${PROJECT_SOURCE_DIR}/thirdparties/spqliox_aarch64/xbyak_aarch64/xbyak_aarch64 ${PROJECT_SOURCE_DIR}/thirdparties/hexl/hexl/hexl/include ${PROJECT_SOURCE_DIR}/thirdparties/randen + ${PROJECT_SOURCE_DIR}/thirdparties/HLS_arbitrary_Precision_Types/include ${PROJECT_SOURCE_DIR}/thirdparties/cereal/include) if(USE_RANDEN) diff --git a/test/raintt.cpp b/test/raintt.cpp index c1692ad..029f555 100644 --- a/test/raintt.cpp +++ b/test/raintt.cpp @@ -44,8 +44,8 @@ int main() std::uniform_int_distribution Bgdist(0, TFHEpp::lvl1param::Bg); std::uniform_int_distribution Torus32dist( 0, std::numeric_limits::max()); - std::uniform_int_distribution Pdist(0, raintt::P-1); - std::uniform_int_distribution sPdist(-raintt::P, raintt::P-1); + std::uniform_int_distribution Pdist(0, raintt::P-1); + std::uniform_int_distribution sPdist(-raintt::P, raintt::P-1); for (int i = 0; i < num_test; i++) { raintt::Word a = Pdist(engine); @@ -53,7 +53,7 @@ int main() (static_cast(a) * raintt::R) % raintt::P; raintt::Word res = raintt::MulREDC(a, raintt::R2); if (res != tres) { - std::cout << "REDC:" << tres << ":" << res << ":" << a << std::endl; + std::cout << "REDC:" << static_cast(tres) << ":" << static_cast(res) << ":" << static_cast(a) << std::endl; exit(1); } } @@ -66,7 +66,7 @@ int main() raintt::SWord res = raintt::MulSREDC(a, raintt::R2); res = res < 0 ? res + raintt::P : res; if (res != tres) { - std::cout << "SREDC:" << tres << ":" << res << ":" << a + std::cout << "SREDC:" <(tres) << ":" << static_cast(res) << ":" << static_cast(a) << std::endl; exit(1); } @@ -110,7 +110,7 @@ int main() // raintt::INTT(temp, (*tablelvl1)[1]); // for (int i = 0; i < TFHEpp::lvl1param::n/2+1; i++) // if (temp[i] != res[i]) - // std::cout << i << ":" <(res[i])<<":"<(temp[i])<(res, (*tablelvl1)[0]); // radix4 // for(int i = 0; i < TFHEpp::lvl1param::n>>2; i++){ @@ -133,7 +133,7 @@ int main() for (int i = 0; i < TFHEpp::lvl1param::n; i++) a[i] = static_cast(a[i]) < 0? a[i]+raintt::P: a[i]; for (int i = 0; i < TFHEpp::lvl1param::n/2+2; i++) - if (a[i] != res[i]) std::cout << i << ":" <(a[i])<(res[i])<<":"<(a[i])<