Skip to content

Commit

Permalink
Remove RemoveSign
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Aug 15, 2024
1 parent 8ba24e2 commit a23eb05
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 54 deletions.
23 changes: 9 additions & 14 deletions include/bfv++.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#pragma once
#include <bits/stdint-uintn.h>

#include <cstdint>

#include "keyswitch.hpp"
#include "mulfft.hpp"
#include "trgsw.hpp"
#include "trlwe.hpp"

// #include "hexl/hexl.hpp"

namespace TFHEpp {

template <class P>
void TRLWEMultWithoutRelinerization(TRLWE3<P> &res, const TRLWE<P> &a,
const TRLWE<P> &b)
{
UnsignedTRLWE<P> aa, bb;
RemoveSign<P>(aa[0], a[0]);
RemoveSign<P>(aa[1], a[1]);
RemoveSign<P>(bb[0], b[0]);
RemoveSign<P>(bb[1], b[1]);

PolynomialInFD<P> ffta, fftb, fftc;
TwistIFFT<P>(ffta, aa[0]);
TwistIFFT<P>(fftb, bb[1]);
TwistIFFTUInt<P>(ffta, a[0]);
TwistIFFTUInt<P>(fftb, b[1]);
MulInFD<P::n>(fftc, ffta, fftb);
TwistIFFT<P>(ffta, aa[1]);
TwistIFFT<P>(fftb, bb[0]);
TwistIFFTUInt<P>(ffta, a[1]);
TwistIFFTUInt<P>(fftb, b[0]);
FMAInFD<P::n>(fftc, ffta, fftb);
TwistFFTrescale<P>(res[0], fftc);

PolyMulRescaleUnsigned<P>(res[1], aa[1], bb[1]);
PolyMulRescaleUnsigned<P>(res[2], aa[0], bb[0]);
PolyMulRescaleUnsigned<P>(res[1], a[1], b[1]);
PolyMulRescaleUnsigned<P>(res[2], a[0], b[0]);

// for (int i = 0; i < P::n; i++) {
// uint64_t ri = 0;
Expand Down
46 changes: 22 additions & 24 deletions include/mulfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ inline void TwistIFFT(PolynomialInFD<P> &res, const Polynomial<P> &a)
static_assert(false_v<typename P::T>, "Undefined TwistIFFT!");
}

template <class P>
inline void TwistIFFTUInt(PolynomialInFD<P> &res, const Polynomial<P> &a)
{
if constexpr (std::is_same_v<P, lvl1param>) {
if constexpr (std::is_same_v<typename P::T, uint32_t>)
fftplvl1.execute_reverse_uint(res.data(), a.data());
// if constexpr (std::is_same_v<typename P::T, uint64_t>)
// fftplvl1.execute_reverse_torus64(res.data(), a.data());
}
// else if constexpr (std::is_same_v<typename P::T, uint64_t>)
// fftplvl2.execute_reverse_torus64(res.data(), a.data());
else
static_assert(false_v<typename P::T>, "Undefined TwistIFFT!");
}

template <uint32_t N>
inline void MulInFD(std::array<double, N> &res, const std::array<double, N> &b)
{
Expand Down Expand Up @@ -302,13 +317,13 @@ inline void PolyMul(Polynomial<P> &res, const Polynomial<P> &a,

template <class P>
inline void PolyMulRescaleUnsigned(Polynomial<P> &res,
const UnsignedPolynomial<P> &a,
const UnsignedPolynomial<P> &b)
const Polynomial<P> &a,
const Polynomial<P> &b)
{
// if constexpr (std::is_same_v<typename P::T, uint32_t>) {
PolynomialInFD<P> ffta, fftb;
TwistIFFT<P>(ffta, a);
TwistIFFT<P>(fftb, b);
TwistIFFTUInt<P>(ffta, a);
TwistIFFTUInt<P>(fftb, b);
MulInFD<P::n>(ffta, fftb);
TwistFFTrescale<P>(res, ffta);
// }
Expand All @@ -335,32 +350,15 @@ inline void PolyMulNaive(Polynomial<P> &res, const Polynomial<P> &a,
}

template <class P>
inline void PolyMulRescale(Polynomial<P> &res, const Polynomial<P> &a,
const Polynomial<P> &b)
{
if constexpr (std::is_same_v<typename P::T, uint32_t>) {
UnsignedPolynomial<P> aa, bb;
RemoveSign<P>(aa, a);
RemoveSign<P>(bb, b);
PolyMulRescaleUnsigned<P>(res, aa, bb);
}
else
static_assert(false_v<typename P::T>, "Undefined PolyMul!");
}

template <class P>
inline void PolyMulNaieveRescale(Polynomial<P> &res, const Polynomial<P> &a,
inline void PolyMulNaieveRescaleUnsigned(Polynomial<P> &res, const Polynomial<P> &a,
const Polynomial<P> &b)
{
Polynomial<P> aa, bb;
for (int i = 0; i < P::n; i++) aa[i] = (a[i] + 1) / 2;
for (int i = 0; i < P::n; i++) bb[i] = (b[i] + 1) / 2;
for (int i = 0; i < P::n; i++) {
__int128_t ri = 0;
for (int j = 0; j <= i; j++)
ri += static_cast<__int128_t>(aa[j]) * bb[i - j];
ri += static_cast<__int128_t>(a[j]) * static_cast<__int128_t>(b[i - j]);
for (int j = i + 1; j < P::n; j++)
ri -= static_cast<__int128_t>(aa[j]) * bb[P::n + i - j];
ri -= static_cast<__int128_t>(a[j]) * static_cast<__int128_t>(b[P::n + i - j]);
// res[i] = static_cast<typename P::T>((ri) >> (std::numeric_limits<typename P::T>::digits - 3));
res[i] = static_cast<typename P::T>((ri) >> 29);
}
Expand Down
4 changes: 0 additions & 4 deletions include/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ using TLWE = aligned_array<typename P::T, P::k * P::n + 1>;
template <class P>
using Polynomial = std::array<typename P::T, P::n>;
template <class P>
using UnsignedPolynomial = Polynomial<P>;
template <class P>
using PolynomialInFD = std::array<double, P::n>;
template <class P>
using PolynomialNTT = std::array<cuHEpp::INTorus, P::n>;
Expand All @@ -96,8 +94,6 @@ using DecomposedPolynomialRAINTT = std::array<PolynomialRAINTT<P>, P::l>;
template <class P>
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 = std::array<PolynomialInFD<P>, P::k + 1>;
Expand Down
6 changes: 0 additions & 6 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,4 @@ inline void Automorphism(Polynomial<P> &res, const Polynomial<P> &poly,
}
}

template <class P>
inline void RemoveSign(UnsignedPolynomial<P> &res, const Polynomial<P> &a)
{
for (int i = 0; i < P::n; i++) res[i] = (a[i] + 1) / 2;
}

} // namespace TFHEpp
2 changes: 1 addition & 1 deletion src/bfv.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #include<bfv++.hpp>

// thread_local intel::hexl::NTT nttlvl1(TFHEpp::lvl1param::n, TFHEpp::lvl1param::q);
// // thread_local intel::hexl::NTT nttlvl2(TFHEpp::lvl2param::n, TFHEpp::lvl2param::q);
// thread_local intel::hexl::NTT nttlvl2(TFHEpp::lvl2param::n, TFHEpp::lvl2param::q);
6 changes: 3 additions & 3 deletions test/polymul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ int main()
for (typename TFHEpp::lvl1param::T &i : p0) i = message(engine);
for (typename TFHEpp::lvl1param::T &i : p1) i = message(engine);

TFHEpp::PolyMulRescale<TFHEpp::lvl1param>(pres, p0, p1);
TFHEpp::PolyMulNaieveRescale<TFHEpp::lvl1param>(ptrue, p0, p1);
TFHEpp::PolyMulRescaleUnsigned<TFHEpp::lvl1param>(pres, p0, p1);
TFHEpp::PolyMulNaieveRescaleUnsigned<TFHEpp::lvl1param>(ptrue, p0, p1);

for (int i = 0; i < TFHEpp::lvl1param::n; i++) {
// std::cout<<pres[i]<<":"<<ptrue[i]<<std::endl;
assert(abs(static_cast<int>(pres[i] - ptrue[i])) <= 2);
}
}
std::cout << "PolyMulRescale Passed" << std::endl;
std::cout << "PolyMulRescaleUnsigned Passed" << std::endl;
return 0;
}
28 changes: 26 additions & 2 deletions thirdparties/spqlios/fft_processor_spqlios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ FFT_Processor_Spqlios::FFT_Processor_Spqlios(const int32_t N) : _2N(2 * N), N(N)
}
}

void FFT_Processor_Spqlios::execute_reverse_uint(double *res, const uint32_t *a) {
//for (int32_t i=0; i<N; i++) real_inout_rev[i]=(double)a[i];
{
double *dst = res;
// double *dst = real_inout_rev;
const uint32_t *ait = a;
const uint32_t *aend = a + N;
__asm__ __volatile__ (
"0:\n"
"vmovupd (%1),%%xmm0\n"
"vcvtudq2pd %%xmm0,%%ymm1\n"
"vmovapd %%ymm1,(%0)\n"
"addq $16,%1\n"
"addq $32,%0\n"
"cmpq %2,%1\n"
"jb 0b\n"
: "=r"(dst), "=r"(ait), "=r"(aend)
: "0"(dst), "1"(ait), "2"(aend)
: "%xmm0", "%ymm1", "memory"
);
}
ifft(tables_reverse, res);
}

void FFT_Processor_Spqlios::execute_reverse_int(double *res, const int32_t *a) {
//for (int32_t i=0; i<N; i++) real_inout_rev[i]=(double)a[i];
{
Expand Down Expand Up @@ -163,7 +187,7 @@ void FFT_Processor_Spqlios::execute_direct_torus32_rescale(uint32_t *res, const
);
}
fft(tables_direct, real_inout_direct);
for (int32_t i = 0; i < N; i++) res[i] = static_cast<uint32_t>(int64_t(real_inout_direct[i]/(Δ/4)));
for (int32_t i = 0; i < N; i++) res[i] = static_cast<uint32_t>(int64_t(real_inout_direct[i]/Δ));
}

void FFT_Processor_Spqlios::execute_direct_torus64(uint64_t* res, const double* a) {
Expand Down Expand Up @@ -245,7 +269,7 @@ void FFT_Processor_Spqlios::execute_direct_torus64_rescale(uint64_t* res, const
);
}
fft(tables_direct,real_inout_direct);
for (int i=0; i<N; i++) res[i] = uint64_t(std::round(real_inout_direct[i]/(Δ/4)));
for (int i=0; i<N; i++) res[i] = uint64_t(std::round(real_inout_direct[i]/Δ));
}

FFT_Processor_Spqlios::~FFT_Processor_Spqlios() {
Expand Down
2 changes: 2 additions & 0 deletions thirdparties/spqlios/fft_processor_spqlios.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class FFT_Processor_Spqlios {

void execute_reverse_int(double *res, const int32_t *a);

void execute_reverse_uint(double *res, const uint32_t *a);

void execute_reverse_torus32(double *res, const uint32_t *a);

void execute_direct_torus32(uint32_t *res, const double *a);
Expand Down

0 comments on commit a23eb05

Please sign in to comment.