Skip to content

Commit

Permalink
clang-formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed May 3, 2024
1 parent 8ec8d3f commit 905b692
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 233 deletions.
52 changes: 39 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,60 @@ option(USE_PERF "Use Google Profiler" OFF)

option(ENABLE_SHARED "Build as shared libraries" OFF)

set(TFHEpp_DEFINITIONS "" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
""
PARENT_SCOPE)

if(USE_RANDEN)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_RANDEN" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_RANDEN"
PARENT_SCOPE)
add_compile_definitions(USE_RANDEN)
endif()

if(USE_80BIT_SECURITY)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_80BIT_SECURITY" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_80BIT_SECURITY"
PARENT_SCOPE)
add_compile_definitions(USE_80BIT_SECURITY)
elseif(USE_COMPRESS)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_COMPRESS" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_COMPRESS"
PARENT_SCOPE)
add_compile_definitions(USE_COMPRESS)
elseif(USE_CGGI19)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_CGGI19" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_CGGI19"
PARENT_SCOPE)
add_compile_definitions(USE_CGGI19)
elseif(USE_CONCRETE)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_CONCRETE" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_CONCRETE"
PARENT_SCOPE)
add_compile_definitions(USE_CONCRETE)
elseif(USE_TFHE_RS)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_TFHE_RS" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_TFHE_RS"
PARENT_SCOPE)
add_compile_definitions(USE_TFHE_RS)
elseif(USE_TERNARY_CMUX)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_TERNARY_CMUX;USE_TERNARY" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_TERNARY_CMUX;USE_TERNARY"
PARENT_SCOPE)
add_compile_definitions(USE_TERNARY)
add_compile_definitions(USE_TERNARY_CMUX)
elseif(USE_TERNARY)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_TERNARY" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_TERNARY"
PARENT_SCOPE)
add_compile_definitions(USE_TERNARY)
endif()

if(NOT USE_TERNARY)
if(USE_KEY_BUNDLE)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_KEY_BUNDLE" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_KEY_BUNDLE"
PARENT_SCOPE)
add_compile_definitions(USE_KEY_BUNDLE)
endif()
endif()
Expand All @@ -85,11 +105,15 @@ if(USE_AVX512)
endif()

if(USE_FFTW3)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_FFTW3" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_FFTW3"
PARENT_SCOPE)
add_compile_definitions(USE_FFTW3)
add_subdirectory(thirdparties/fftw)
elseif(USE_SPQLIOX_AARCH64)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_SPQLIOX_AARCH64" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_SPQLIOX_AARCH64"
PARENT_SCOPE)
add_compile_definitions(USE_SPQLIOX_AARCH64)
add_subdirectory(thirdparties/spqliox_aarch64)
# Check if the platform is macOS and the architecture is ARM64
Expand All @@ -101,7 +125,9 @@ else()
endif()

if(USE_HEXL)
set(TFHEpp_DEFINITIONS "${TFHEpp_DEFINITIONS};USE_HEXL" PARENT_SCOPE)
set(TFHEpp_DEFINITIONS
"${TFHEpp_DEFINITIONS};USE_HEXL"
PARENT_SCOPE)
add_compile_definitions(USE_HEXL)
add_subdirectory(thirdparties/hexl)
# set(CMAKE_CXX_FLAGS "-march=native -O3 -g -funroll-loops -Wall -Wextra
Expand Down
12 changes: 7 additions & 5 deletions include/cloudkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void bkrainttgen(BootstrappingKeyRAINTT<P>& bkraintt,
const Key<typename P::domainP>& domainkey,
const Key<typename P::targetP>& targetkey)
{
#pragma omp parallel for
#pragma omp parallel for
for (int i = 0; i < P::domainP::k * P::domainP::n; i++) {
Polynomial<typename P::targetP> plainpoly = {};
plainpoly[0] = domainkey[i];
Expand Down Expand Up @@ -378,9 +378,11 @@ struct EvalKey {
template <class Archive>
void serialize(Archive& archive)
{
archive(params, bklvl01, bklvlh1, bklvl02, bklvlh2, bkfftlvl01, bkfftlvlh1, bkfftlvl02, bkfftlvlh2, bknttlvl01,
bknttlvlh1, bknttlvl02, bknttlvlh2, iksklvl10, iksklvl1h, iksklvl20, iksklvl21, iksklvl22, iksklvl31,
privksklvl11, privksklvl21, privksklvl22);
archive(params, bklvl01, bklvlh1, bklvl02, bklvlh2, bkfftlvl01,
bkfftlvlh1, bkfftlvl02, bkfftlvlh2, bknttlvl01, bknttlvlh1,
bknttlvl02, bknttlvlh2, iksklvl10, iksklvl1h, iksklvl20,
iksklvl21, iksklvl22, iksklvl31, privksklvl11, privksklvl21,
privksklvl22);
}

// emplace keys
Expand Down Expand Up @@ -505,7 +507,7 @@ struct EvalKey {
for (int i = 0; i < lvl01param::domainP::n; i++)
(*bknttlvl01)[i] = ApplyNTT2trgsw<lvl1param>((*bklvl01)[i][0]);
}
else if constexpr (std::is_same_v<P, lvlh1param>){
else if constexpr (std::is_same_v<P, lvlh1param>) {
bknttlvlh1 = std::make_unique_for_overwrite<
BootstrappingKeyNTT<lvlh1param>>();
for (int i = 0; i < lvlh1param::domainP::n; i++)
Expand Down
106 changes: 61 additions & 45 deletions include/homdecomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,79 @@
#include "gatebootstrapping.hpp"

namespace TFHEpp {
/*!
* @brief Generates a Polynomial with each coefficient subtracted by a base value
* @tparam P The parameter set for the Polynomial
* @tparam basebit The base to be subtracted
* @return A Polynomial of the parameter type with coefficients subtracted by the base value
*/
template <class P, uint basebit> constexpr Polynomial<P> subtractpolygen() {
/*!
* @brief Generates a Polynomial with each coefficient subtracted by a base
* value
* @tparam P The parameter set for the Polynomial
* @tparam basebit The base to be subtracted
* @return A Polynomial of the parameter type with coefficients subtracted by
* the base value
*/
template <class P, uint basebit>
constexpr Polynomial<P> subtractpolygen()
{
Polynomial<P> poly;
for (int i = 0; i < P::n; i++)
poly[i] = 1ULL << (std::numeric_limits<typename P::T>::digits - basebit - 2);
poly[i] =
1ULL << (std::numeric_limits<typename P::T>::digits - basebit - 2);
return poly;
}
}

// https://eprint.iacr.org/2023/645
/*!
* @brief Homomorphically decomposes an input ciphertext into an array of level 1 ciphertexts
* @tparam high2midP The parameter set for the transition from high to mid level
* @tparam mid2lowP The parameter set for the transition from mid to low level
* @tparam brP The bootstrapping parameter set
* @tparam basebit The base value
* @tparam numdigit The number of digits
* @param cres Array of output ciphertexts
* @param cin Input ciphertext
* @param kskh2m The key switching key for high to mid level
* @param kskm2l The key switching key for mid to low level
* @param bkfft The bootstrapping key FFT
*/
template <class high2midP, class mid2lowP, class brP, uint basebit, uint numdigit>
void HomDecomp(std::array<TLWE<typename high2midP::targetP>, numdigit> &cres,
const TLWE<typename high2midP::domainP> &cin, const KeySwitchingKey<high2midP> &kskh2m,
const KeySwitchingKey<mid2lowP> &kskm2l, const BootstrappingKeyFFT<brP> &bkfft) {
// https://eprint.iacr.org/2023/645
/*!
* @brief Homomorphically decomposes an input ciphertext into an array of level
* 1 ciphertexts
* @tparam high2midP The parameter set for the transition from high to mid level
* @tparam mid2lowP The parameter set for the transition from mid to low level
* @tparam brP The bootstrapping parameter set
* @tparam basebit The base value
* @tparam numdigit The number of digits
* @param cres Array of output ciphertexts
* @param cin Input ciphertext
* @param kskh2m The key switching key for high to mid level
* @param kskm2l The key switching key for mid to low level
* @param bkfft The bootstrapping key FFT
*/
template <class high2midP, class mid2lowP, class brP, uint basebit,
uint numdigit>
void HomDecomp(std::array<TLWE<typename high2midP::targetP>, numdigit> &cres,
const TLWE<typename high2midP::domainP> &cin,
const KeySwitchingKey<high2midP> &kskh2m,
const KeySwitchingKey<mid2lowP> &kskm2l,
const BootstrappingKeyFFT<brP> &bkfft)
{
TFHEpp::TLWE<typename mid2lowP::targetP> tlwelvlhalf;
TFHEpp::TLWE<typename high2midP::targetP> subtlwe;

// cres will be used as a reusable buffer
constexpr uint32_t plain_modulusbit = basebit * numdigit;
#pragma omp parallel for default(none) shared(cin, cres, kskh2m)
for (int digit = 1; digit <= numdigit; digit++) {
TFHEpp::TLWE<typename high2midP::domainP> switchedtlwe;
for (int i = 0; i <= high2midP::domainP::k * high2midP::domainP::n; i++)
switchedtlwe[i] = cin[i] << (plain_modulusbit - basebit * digit);
IdentityKeySwitch<high2midP>(cres[digit - 1], switchedtlwe, kskh2m);
TFHEpp::TLWE<typename high2midP::domainP> switchedtlwe;
for (int i = 0; i <= high2midP::domainP::k * high2midP::domainP::n; i++)
switchedtlwe[i] = cin[i] << (plain_modulusbit - basebit * digit);
IdentityKeySwitch<high2midP>(cres[digit - 1], switchedtlwe, kskh2m);
}
for (int digit = 1; digit <= numdigit; digit++) {
if (digit != 1) {
for (int i = 0; i <= high2midP::targetP::k * high2midP::targetP::n; i++)
cres[digit - 1][i] += subtlwe[i];
cres[digit - 1][high2midP::targetP::k * high2midP::targetP::n] -=
1ULL << (std::numeric_limits<typename high2midP::targetP::T>::digits - basebit - 1);
}
IdentityKeySwitch<mid2lowP>(tlwelvlhalf, cres[digit - 1], kskm2l);
tlwelvlhalf[mid2lowP::targetP::k * mid2lowP::targetP::n] +=
1ULL << (std::numeric_limits<typename mid2lowP::targetP::T>::digits - basebit - 1);
if (digit != numdigit)
GateBootstrappingTLWE2TLWEFFT<brP>(subtlwe, tlwelvlhalf, bkfft,
subtractpolygen<typename high2midP::targetP, basebit>());
if (digit != 1) {
for (int i = 0; i <= high2midP::targetP::k * high2midP::targetP::n;
i++)
cres[digit - 1][i] += subtlwe[i];
cres[digit - 1][high2midP::targetP::k * high2midP::targetP::n] -=
1ULL << (std::numeric_limits<
typename high2midP::targetP::T>::digits -
basebit - 1);
}
IdentityKeySwitch<mid2lowP>(tlwelvlhalf, cres[digit - 1], kskm2l);
tlwelvlhalf[mid2lowP::targetP::k * mid2lowP::targetP::n] +=
1ULL
<< (std::numeric_limits<typename mid2lowP::targetP::T>::digits -
basebit - 1);
if (digit != numdigit)
GateBootstrappingTLWE2TLWEFFT<brP>(
subtlwe, tlwelvlhalf, bkfft,
subtractpolygen<typename high2midP::targetP, basebit>());
}
}
}

} // namespace TFHEpp
} // namespace TFHEpp
36 changes: 21 additions & 15 deletions include/params/128bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct lvl0param {
ErrorDistribution::ModularGaussian;
static const inline double α = 0.000'092'511'997'467'675'6; // fresh noise
using T = uint16_t; // Torus representation
static constexpr std::make_signed_t<T> μ = 1LL << (std::numeric_limits<T>::digits - 3);
static constexpr std::make_signed_t<T> μ =
1LL << (std::numeric_limits<T>::digits - 3);
static constexpr uint32_t plain_modulus = 8;
static constexpr double Δ =
static_cast<double>(1ULL << std::numeric_limits<T>::digits) /
Expand All @@ -25,15 +26,17 @@ struct lvlhalfparam {
static constexpr int32_t key_value_max = 1;
static constexpr int32_t key_value_min = 0;
static constexpr int32_t key_value_diff = key_value_max - key_value_min;
static constexpr std::uint32_t n = 760; // dimension
static constexpr std::uint32_t n = 760; // dimension
static constexpr std::uint32_t k = 1;
static constexpr ErrorDistribution errordist =
ErrorDistribution::ModularGaussian;
static const inline double α = std::pow(2.0, -17); // fresh noise
using T = uint32_t; // Torus representation
static const inline double α = std::pow(2.0, -17); // fresh noise
using T = uint32_t; // Torus representation
static constexpr T μ = 1U << (std::numeric_limits<T>::digits - 3);
static constexpr uint32_t plain_modulus = 8;
static constexpr double Δ = static_cast<double>(1ULL << std::numeric_limits<T>::digits) / plain_modulus;
static constexpr double Δ =
static_cast<double>(1ULL << std::numeric_limits<T>::digits) /
plain_modulus;
};

struct lvl1param {
Expand Down Expand Up @@ -81,17 +84,17 @@ struct lvl2param {
struct lvl3param {
static constexpr int32_t key_value_max = 1;
static constexpr int32_t key_value_min = -1;
static const std::uint32_t nbit = 13; // dimension must be a power of 2 for
static const std::uint32_t nbit = 13; // dimension must be a power of 2 for
// ease of polynomial multiplication.
static constexpr std::uint32_t n = 1 << nbit; // dimension
static constexpr std::uint32_t n = 1 << nbit; // dimension
static constexpr std::uint32_t k = 1;
static constexpr std::uint32_t l = 4;
static constexpr std::uint32_t Bgbit = 9;
static constexpr std::uint32_t Bg = 1 << Bgbit;
static constexpr ErrorDistribution errordist =
ErrorDistribution::ModularGaussian;
static const inline double α = std::pow(2.0, -47); // fresh noise
using T = uint64_t; // Torus representation
static const inline double α = std::pow(2.0, -47); // fresh noise
using T = uint64_t; // Torus representation
static constexpr T μ = 1ULL << 61;
static constexpr uint32_t plain_modulusbit = 31;
static constexpr uint64_t plain_modulus = 1ULL << plain_modulusbit;
Expand All @@ -111,9 +114,11 @@ struct lvl10param {
};

struct lvl1hparam {
static constexpr std::uint32_t t = 10; // number of addition in keyswitching
static constexpr std::uint32_t basebit = 3; // how many bit should be encrypted in keyswitching key
static const inline double α = lvlhalfparam::α; // key noise
static constexpr std::uint32_t t =
10; // number of addition in keyswitching
static constexpr std::uint32_t basebit =
3; // how many bit should be encrypted in keyswitching key
static const inline double α = lvlhalfparam::α; // key noise
using domainP = lvl1param;
using targetP = lvlhalfparam;
};
Expand Down Expand Up @@ -165,9 +170,10 @@ struct lvl22param {
};

struct lvl31param {
static constexpr std::uint32_t t = 7; // number of addition in keyswitching
static constexpr std::uint32_t basebit = 2; // how many bit should be encrypted in keyswitching key
static const inline double α = lvl1param::α; // key noise
static constexpr std::uint32_t t = 7; // number of addition in keyswitching
static constexpr std::uint32_t basebit =
2; // how many bit should be encrypted in keyswitching key
static const inline double α = lvl1param::α; // key noise
using domainP = lvl3param;
using targetP = lvl1param;
};
Loading

0 comments on commit 905b692

Please sign in to comment.