diff --git a/include/circuitbootstrapping.hpp b/include/circuitbootstrapping.hpp index 12ebb56..eb1053e 100644 --- a/include/circuitbootstrapping.hpp +++ b/include/circuitbootstrapping.hpp @@ -119,7 +119,8 @@ void CircuitBootstrappingFFTInv( { TLWE invtlwe; // HomNot - for (int i = 0; i <= brP::domainP::k*brP::domainP::n; i++) invtlwe[i] = -tlwe[i]; + for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++) + invtlwe[i] = -tlwe[i]; CircuitBootstrappingFFT(invtrgswfft, invtlwe, ek); } @@ -130,7 +131,8 @@ void CircuitBootstrappingFFTInv( { TLWE invtlwe; // HomNot - for (int i = 0; i <= iksP::domainP::k*iksP::domainP::n; i++) invtlwe[i] = -tlwe[i]; + for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++) + invtlwe[i] = -tlwe[i]; CircuitBootstrappingFFT(invtrgswfft, invtlwe, ek); } diff --git a/include/gate.hpp b/include/gate.hpp index dd85d03..edef132 100644 --- a/include/gate.hpp +++ b/include/gate.hpp @@ -298,18 +298,20 @@ void HomMUXwoSE(TRLWE &res, const TLWE &c1, const TLWE &c0, const EvalKey &ek) { - TLWE and1,and0; - for (int i = 0; i <= brP::domainP::k*brP::domainP::n; i++) and1[i] = cs[i] + c1[i]; - for (int i = 0; i <= brP::domainP::k*brP::domainP::n; i++) and0[i] = -cs[i] + c0[i]; - and1[brP::domainP::k*brP::domainP::n] -= brP::domainP::μ; - and0[brP::domainP::k*brP::domainP::n] -= brP::domainP::μ; + TLWE and1, and0; + for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++) + and1[i] = cs[i] + c1[i]; + for (int i = 0; i <= brP::domainP::k * brP::domainP::n; i++) + and0[i] = -cs[i] + c0[i]; + and1[brP::domainP::k * brP::domainP::n] -= brP::domainP::μ; + and0[brP::domainP::k * brP::domainP::n] -= brP::domainP::μ; TRLWE and0trlwe; BlindRotate(res, and1, ek.getbkfft(), μpolygen()); BlindRotate(and0trlwe, and0, ek.getbkfft(), μpolygen()); - for (int i = 0; i < brP::targetP::k*brP::targetP::n; i++) { + for (int i = 0; i < brP::targetP::k * brP::targetP::n; i++) { res[0][i] += and0trlwe[0][i]; res[1][i] += and0trlwe[1][i]; }; @@ -322,11 +324,13 @@ void HomMUXwoSE(TRLWE &res, const TLWE &c1, const TLWE &c0, const EvalKey &ek) { - TLWE temp1,temp0; - for (int i = 0; i <= iksP::domainP::k*iksP::domainP::n; i++) temp1[i] = cs[i] + c1[i]; - for (int i = 0; i <= iksP::domainP::k*iksP::domainP::n; i++) temp0[i] = -cs[i] + c0[i]; - temp1[iksP::domainP::k*iksP::domainP::n] -= iksP::domainP::μ; - temp0[iksP::domainP::k*iksP::domainP::n] -= iksP::domainP::μ; + TLWE temp1, temp0; + for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++) + temp1[i] = cs[i] + c1[i]; + for (int i = 0; i <= iksP::domainP::k * iksP::domainP::n; i++) + temp0[i] = -cs[i] + c0[i]; + temp1[iksP::domainP::k * iksP::domainP::n] -= iksP::domainP::μ; + temp0[iksP::domainP::k * iksP::domainP::n] -= iksP::domainP::μ; TLWE and1, and0; IdentityKeySwitch(and1, temp1, ek.getiksk()); IdentityKeySwitch(and0, temp0, ek.getiksk()); @@ -336,7 +340,7 @@ void HomMUXwoSE(TRLWE &res, BlindRotate(and0trlwe, and0, ek.getbkfft(), μpolygen()); - for (int i = 0; i < brP::targetP::k*brP::targetP::n; i++) { + for (int i = 0; i < brP::targetP::k * brP::targetP::n; i++) { res[0][i] += and0trlwe[0][i]; res[1][i] += and0trlwe[1][i]; }; diff --git a/include/nussbaumer.hpp b/include/nussbaumer.hpp index 599ff3e..897d047 100644 --- a/include/nussbaumer.hpp +++ b/include/nussbaumer.hpp @@ -1,17 +1,18 @@ #pragma once #include -namespace Nussbaumer{ +namespace Nussbaumer { template -inline void PolynomialMulByXai(const std::span res, const size_t a) +inline void PolynomialMulByXai(const std::span res, + const size_t a) { if (a == 0) return; - else{ - constexpr size_t r = 1ull< temp; - std::copy(res.begin(),res.end(),temp.begin()); + else { + constexpr size_t r = 1ull << rbit; + std::array temp; + std::copy(res.begin(), res.end(), temp.begin()); if (a < r) { for (int i = 0; i < a; i++) res[i] = -temp[i - a + r]; for (int i = a; i < r; i++) res[i] = temp[i - a]; @@ -24,90 +25,106 @@ inline void PolynomialMulByXai(const std::span res, const size_t a } } -template -void NussbaumerButterfly(const std::span res){ - constexpr size_t m = 1ull< +void NussbaumerButterfly(const std::span res) +{ + constexpr size_t m = 1ull << mbit; + constexpr size_t r = 1ull << rbit; + for (int i = 0; i < m / 2; i++) + for (int j = 0; j < r; j++) { + const T temp = res[i * r + j]; + res[i * r + j] += res[(i + m / 2) * r + j]; + res[(i + m / 2) * r + j] = temp - res[(i + m / 2) * r + j]; } - if constexpr(mbit!=1){ - constexpr size_t stride = 1ull<<(rbit-mbit); - for(int i = 1; i < m/2; i++) PolynomialMulByXai(static_cast>(res.subspan((i+m/2)*r,r)),i*stride); - NussbaumerButterfly(res.template subspan<0,m*r/2>()); - NussbaumerButterfly(res.template subspan()); + if constexpr (mbit != 1) { + constexpr size_t stride = 1ull << (rbit - mbit); + for (int i = 1; i < m / 2; i++) + PolynomialMulByXai( + static_cast>(res.subspan((i + m / 2) * r, r)), + i * stride); + NussbaumerButterfly( + res.template subspan<0, m * r / 2>()); + NussbaumerButterfly( + res.template subspan()); } } -template -void NussbaumerTransform(std::span res){ - if constexpr(Nbit == 1){ +template +void NussbaumerTransform(std::span res) +{ + if constexpr (Nbit == 1) { const T temp = res[0]; res[0] += res[1]; res[1] = temp - res[1]; - return; - }else{ - //initialize - constexpr uint mbit = Nbit/2; - constexpr size_t m = 1ull< temp; - std::copy(res.begin(),res.end(),temp.begin()); - //reorder - for(int i = 0; i < m; i++){ - for(int j = 0; j < r; j++) - res[i*r+j] = temp[m*j+i]; + return; + } + else { + // initialize + constexpr uint mbit = Nbit / 2; + constexpr size_t m = 1ull << mbit; + constexpr uint rbit = Nbit - mbit; + constexpr size_t r = 1ull << rbit; + std::array temp; + std::copy(res.begin(), res.end(), temp.begin()); + // reorder + for (int i = 0; i < m; i++) { + for (int j = 0; j < r; j++) res[i * r + j] = temp[m * j + i]; } - NussbaumerButterfly(res); - for(int i = 0; i < m; i++) - NussbaumerTransform(static_cast>(res.subspan(i*r,r))); + NussbaumerButterfly(res); + for (int i = 0; i < m; i++) + NussbaumerTransform( + static_cast>(res.subspan(i * r, r))); } } -template -void InverseNussbaumerButterfly(const std::span res){ - constexpr size_t m = 1ull<(res.template subspan<0,m*r/2>()); - InverseNussbaumerButterfly(res.template subspan()); - for(int i = 1; i < m/2; i++) PolynomialMulByXai(static_cast>(res.subspan((i+m/2)*r,r)),2*r-i*stride); +template +void InverseNussbaumerButterfly(const std::span res) +{ + constexpr size_t m = 1ull << mbit; + constexpr size_t r = 1ull << rbit; + if constexpr (mbit != 1) { + constexpr size_t stride = 1ull << (rbit - mbit); + InverseNussbaumerButterfly( + res.template subspan<0, m * r / 2>()); + InverseNussbaumerButterfly( + res.template subspan()); + for (int i = 1; i < m / 2; i++) + PolynomialMulByXai( + static_cast>(res.subspan((i + m / 2) * r, r)), + 2 * r - i * stride); } - for(int i = 0; i < m/2; i++) - for(int j = 0; j < r; j++){ - const T temp = res[i*r+j]; - res[i*r+j] += res[(i+m/2)*r+j]; - res[(i+m/2)*r+j] = temp - res[(i+m/2)*r+j]; + for (int i = 0; i < m / 2; i++) + for (int j = 0; j < r; j++) { + const T temp = res[i * r + j]; + res[i * r + j] += res[(i + m / 2) * r + j]; + res[(i + m / 2) * r + j] = temp - res[(i + m / 2) * r + j]; } } -template -void InverseNussbaumerTransform(std::span res){ - if constexpr(Nbit == 1){ +template +void InverseNussbaumerTransform(std::span res) +{ + if constexpr (Nbit == 1) { const T temp = res[0]; res[0] += res[1]; res[1] = temp - res[1]; - return; - }else{ - //initialize - constexpr uint mbit = Nbit/2; - constexpr size_t m = 1ull<(static_cast>(res.subspan(i*r,r))); - InverseNussbaumerButterfly(res); - std::array temp; - std::copy(res.begin(),res.end(),temp.begin()); - //reorder - for(int i = 0; i < m; i++) - for(int j = 0; j < r; j++) - res[m*j+i] = temp[i*r+j]; + return; + } + else { + // initialize + constexpr uint mbit = Nbit / 2; + constexpr size_t m = 1ull << mbit; + constexpr uint rbit = Nbit - mbit; + constexpr size_t r = 1ull << rbit; + for (int i = 0; i < m; i++) + InverseNussbaumerTransform( + static_cast>(res.subspan(i * r, r))); + InverseNussbaumerButterfly(res); + std::array temp; + std::copy(res.begin(), res.end(), temp.begin()); + // reorder + for (int i = 0; i < m; i++) + for (int j = 0; j < r; j++) res[m * j + i] = temp[i * r + j]; } } -} \ No newline at end of file +} // namespace Nussbaumer \ No newline at end of file diff --git a/include/params/128bit.hpp b/include/params/128bit.hpp index ecfca1d..dfd84e5 100644 --- a/include/params/128bit.hpp +++ b/include/params/128bit.hpp @@ -39,7 +39,8 @@ struct lvl1param { static constexpr T μ = 1U << 29; static constexpr uint32_t plain_modulus = 8; static constexpr double Δ = - static_cast(1ULL << std::numeric_limits::digits) / plain_modulus; + static_cast(1ULL << std::numeric_limits::digits) / + plain_modulus; }; struct lvl2param { diff --git a/test/gate.cpp b/test/gate.cpp index 794fd1f..8478b3e 100644 --- a/test/gate.cpp +++ b/test/gate.cpp @@ -146,44 +146,47 @@ void RunTest() if constexpr (std::is_same_v) { cout << "lvl0param" << endl; - Test

("NOT", TFHEpp::HomNOT

, NotChegk, p, cres, c, kNumTests, *sk, ek); - Test

("COPY", TFHEpp::HomCOPY

, CopyChegk, p, cres, c, kNumTests, *sk, - ek); - Test

("NAND", TFHEpp::HomNAND, - NandChegk, p, cres, c, kNumTests, *sk, ek); - Test

("OR", TFHEpp::HomOR, OrChegk, - p, cres, c, kNumTests, *sk, ek); - Test

("ORYN", TFHEpp::HomORYN, - OrYNChegk, p, cres, c, kNumTests, *sk, ek); - Test

("ORNY", TFHEpp::HomORNY, - OrNYChegk, p, cres, c, kNumTests, *sk, ek); - Test

("AND", TFHEpp::HomAND, - AndChegk, p, cres, c, kNumTests, *sk, ek); - Test

("ANDYN", TFHEpp::HomANDYN, - AndYNChegk, p, cres, c, kNumTests, *sk, ek); - Test

("ANDNY", TFHEpp::HomANDNY, - AndNYChegk, p, cres, c, kNumTests, *sk, ek); - Test

("XOR", TFHEpp::HomXOR, - XorChegk, p, cres, c, kNumTests, *sk, ek); - Test

("XNOR", TFHEpp::HomXNOR, - XnorChegk, p, cres, c, kNumTests, *sk, ek); - Test

("MUX", TFHEpp::HomMUX

, MuxChegk, p, cres, c, kNumTests, *sk, ek); - Test

("NMUX", TFHEpp::HomNMUX

, NMuxChegk, p, cres, c, kNumTests, *sk, - ek); - Test

("ConstantZero", TFHEpp::HomCONSTANTZERO

, ConstantZeroChegk, p, - cres, c, kNumTests, *sk, ek); - Test

("ConstantOne", TFHEpp::HomCONSTANTONE

, ConstantOneChegk, p, cres, - c, kNumTests, *sk, ek); + Test

("NOT", TFHEpp::HomNOT

, NotChegk, p, cres, c, kNumTests, *sk, + ek); + Test

("COPY", TFHEpp::HomCOPY

, CopyChegk, p, cres, c, kNumTests, + *sk, ek); + Test

("NAND", TFHEpp::HomNAND, + NandChegk, p, cres, c, kNumTests, *sk, ek); + Test

("OR", TFHEpp::HomOR, + OrChegk, p, cres, c, kNumTests, *sk, ek); + Test

("ORYN", TFHEpp::HomORYN, + OrYNChegk, p, cres, c, kNumTests, *sk, ek); + Test

("ORNY", TFHEpp::HomORNY, + OrNYChegk, p, cres, c, kNumTests, *sk, ek); + Test

("AND", TFHEpp::HomAND, + AndChegk, p, cres, c, kNumTests, *sk, ek); + Test

("ANDYN", TFHEpp::HomANDYN, + AndYNChegk, p, cres, c, kNumTests, *sk, ek); + Test

("ANDNY", TFHEpp::HomANDNY, + AndNYChegk, p, cres, c, kNumTests, *sk, ek); + Test

("XOR", TFHEpp::HomXOR, + XorChegk, p, cres, c, kNumTests, *sk, ek); + Test

("XNOR", TFHEpp::HomXNOR, + XnorChegk, p, cres, c, kNumTests, *sk, ek); + Test

("MUX", TFHEpp::HomMUX

, MuxChegk, p, cres, c, kNumTests, *sk, + ek); + Test

("NMUX", TFHEpp::HomNMUX

, NMuxChegk, p, cres, c, kNumTests, + *sk, ek); + Test

("ConstantZero", TFHEpp::HomCONSTANTZERO

, ConstantZeroChegk, + p, cres, c, kNumTests, *sk, ek); + Test

("ConstantOne", TFHEpp::HomCONSTANTONE

, ConstantOneChegk, p, + cres, c, kNumTests, *sk, ek); } else if constexpr (std::is_same_v) { cout << "lvl1param" << endl; - Test

("NOT", TFHEpp::HomNOT

, NotChegk, p, cres, c, kNumTests, *sk, ek); - Test

("COPY", TFHEpp::HomCOPY

, CopyChegk, p, cres, c, kNumTests, *sk, + Test

("NOT", TFHEpp::HomNOT

, NotChegk, p, cres, c, kNumTests, *sk, ek); + Test

("COPY", TFHEpp::HomCOPY

, CopyChegk, p, cres, c, kNumTests, + *sk, ek); Test

("NAND", TFHEpp::HomNAND, NandChegk, p, cres, c, kNumTests, *sk, ek); - Test

("OR", TFHEpp::HomOR, OrChegk, - p, cres, c, kNumTests, *sk, ek); + Test

("OR", TFHEpp::HomOR, + OrChegk, p, cres, c, kNumTests, *sk, ek); Test

("ORYN", TFHEpp::HomORYN, OrYNChegk, p, cres, c, kNumTests, *sk, ek); Test

("ORNY", TFHEpp::HomORNY, @@ -198,13 +201,14 @@ void RunTest() XorChegk, p, cres, c, kNumTests, *sk, ek); Test

("XNOR", TFHEpp::HomXNOR, XnorChegk, p, cres, c, kNumTests, *sk, ek); - Test

("MUX", TFHEpp::HomMUX

, MuxChegk, p, cres, c, kNumTests, *sk, ek); - Test

("NMUX", TFHEpp::HomNMUX

, NMuxChegk, p, cres, c, kNumTests, *sk, + Test

("MUX", TFHEpp::HomMUX

, MuxChegk, p, cres, c, kNumTests, *sk, ek); - Test

("ConstantZero", TFHEpp::HomCONSTANTZERO

, ConstantZeroChegk, p, + Test

("NMUX", TFHEpp::HomNMUX

, NMuxChegk, p, cres, c, kNumTests, + *sk, ek); + Test

("ConstantZero", TFHEpp::HomCONSTANTZERO

, ConstantZeroChegk, + p, cres, c, kNumTests, *sk, ek); + Test

("ConstantOne", TFHEpp::HomCONSTANTONE

, ConstantOneChegk, p, cres, c, kNumTests, *sk, ek); - Test

("ConstantOne", TFHEpp::HomCONSTANTONE

, ConstantOneChegk, p, cres, - c, kNumTests, *sk, ek); } } diff --git a/test/nussbaumer.cpp b/test/nussbaumer.cpp index a07bcdc..2286059 100644 --- a/test/nussbaumer.cpp +++ b/test/nussbaumer.cpp @@ -1,9 +1,9 @@ #include #include #include +#include #include #include -#include int main() { @@ -16,13 +16,16 @@ int main() // std::cout << "Start LVL1 test." << std::endl; for (int test = 0; test < num_test; test++) { using T = uint64_t; - std::array a,res; + std::array a, res; for (T &i : a) i = Torus32dist(engine); res = a; - Nussbaumer::NussbaumerTransform(std::span{res}); - Nussbaumer::InverseNussbaumerTransform(std::span{res}); + Nussbaumer::NussbaumerTransform( + std::span{res}); + Nussbaumer::InverseNussbaumerTransform( + std::span{res}); for (int i = 0; i < TFHEpp::lvl1param::n; i++) - assert(abs(static_cast(a[i] - res[i]/TFHEpp::lvl1param::n) <= 1)); + assert(abs(static_cast( + a[i] - res[i] / TFHEpp::lvl1param::n) <= 1)); } std::cout << "Id Passed" << std::endl; diff --git a/thirdparties/spqliox_aarch64/CMakeLists.txt b/thirdparties/spqliox_aarch64/CMakeLists.txt index d2a28ed..ea37a2d 100644 --- a/thirdparties/spqliox_aarch64/CMakeLists.txt +++ b/thirdparties/spqliox_aarch64/CMakeLists.txt @@ -8,7 +8,9 @@ add_library(spqliox_aarch64 STATIC ${SPQLIOX_AARCH64_SRCS} # Check if the platform is macOS and the architecture is ARM64 if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") # Add the include directory provided by Homebrew - target_include_directories(spqliox_aarch64 PUBLIC ${PROJECT_SOURCE_DIR}/include /opt/homebrew/include) + target_include_directories( + spqliox_aarch64 PUBLIC ${PROJECT_SOURCE_DIR}/include /opt/homebrew/include) else() - target_include_directories(spqliox_aarch64 PUBLIC ${PROJECT_SOURCE_DIR}/include) + target_include_directories(spqliox_aarch64 + PUBLIC ${PROJECT_SOURCE_DIR}/include) endif()