Skip to content

Commit

Permalink
clang-formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nindanaoto committed Mar 14, 2024
1 parent 5262998 commit 76223d1
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 132 deletions.
6 changes: 4 additions & 2 deletions include/circuitbootstrapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ void CircuitBootstrappingFFTInv(
{
TLWE<typename brP::domainP> 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<brP, privksP>(invtrgswfft, invtlwe, ek);
}

Expand All @@ -130,7 +131,8 @@ void CircuitBootstrappingFFTInv(
{
TLWE<typename iksP::domainP> 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<iksP, bkP, privksP>(invtrgswfft, invtlwe, ek);
}

Expand Down
28 changes: 16 additions & 12 deletions include/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,20 @@ void HomMUXwoSE(TRLWE<typename brP::targetP> &res,
const TLWE<typename brP::domainP> &c1,
const TLWE<typename brP::domainP> &c0, const EvalKey &ek)
{
TLWE<typename brP::domainP> 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<typename brP::domainP> 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<typename brP::targetP> and0trlwe;
BlindRotate<brP>(res, and1, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());
BlindRotate<brP>(and0trlwe, and0, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());

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];
};
Expand All @@ -322,11 +324,13 @@ void HomMUXwoSE(TRLWE<typename brP::targetP> &res,
const TLWE<typename iksP::domainP> &c1,
const TLWE<typename iksP::domainP> &c0, const EvalKey &ek)
{
TLWE<typename iksP::domainP> 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<typename iksP::domainP> 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<typename iksP::targetP> and1, and0;
IdentityKeySwitch<iksP>(and1, temp1, ek.getiksk<iksP>());
IdentityKeySwitch<iksP>(and0, temp0, ek.getiksk<iksP>());
Expand All @@ -336,7 +340,7 @@ void HomMUXwoSE(TRLWE<typename brP::targetP> &res,
BlindRotate<brP>(and0trlwe, and0, ek.getbkfft<brP>(),
μpolygen<typename brP::targetP, brP::targetP::μ>());

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];
};
Expand Down
163 changes: 90 additions & 73 deletions include/nussbaumer.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#pragma once
#include <span>

namespace Nussbaumer{
namespace Nussbaumer {

template <typename T, uint rbit>
inline void PolynomialMulByXai(const std::span<T,1ull<<rbit> res, const size_t a)
inline void PolynomialMulByXai(const std::span<T, 1ull << rbit> res,
const size_t a)
{
if (a == 0)
return;
else{
constexpr size_t r = 1ull<<rbit;
std::array<T,r> temp;
std::copy(res.begin(),res.end(),temp.begin());
else {
constexpr size_t r = 1ull << rbit;
std::array<T, r> 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];
Expand All @@ -24,90 +25,106 @@ inline void PolynomialMulByXai(const std::span<T,1ull<<rbit> res, const size_t a
}
}

template<typename T, uint mbit, uint rbit>
void NussbaumerButterfly(const std::span<T,(1u<<(rbit+mbit))> 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];
template <typename T, uint mbit, uint rbit>
void NussbaumerButterfly(const std::span<T, (1u << (rbit + mbit))> 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<T, rbit>(static_cast<std::span<T,r>>(res.subspan((i+m/2)*r,r)),i*stride);
NussbaumerButterfly<T,mbit-1,rbit>(res.template subspan<0,m*r/2>());
NussbaumerButterfly<T,mbit-1,rbit>(res.template subspan<m*r/2,m*r/2>());
if constexpr (mbit != 1) {
constexpr size_t stride = 1ull << (rbit - mbit);
for (int i = 1; i < m / 2; i++)
PolynomialMulByXai<T, rbit>(
static_cast<std::span<T, r>>(res.subspan((i + m / 2) * r, r)),
i * stride);
NussbaumerButterfly<T, mbit - 1, rbit>(
res.template subspan<0, m * r / 2>());
NussbaumerButterfly<T, mbit - 1, rbit>(
res.template subspan<m * r / 2, m * r / 2>());
}
}

template<typename T, uint Nbit>
void NussbaumerTransform(std::span<T,(1ull<<Nbit)> res){
if constexpr(Nbit == 1){
template <typename T, uint Nbit>
void NussbaumerTransform(std::span<T, (1ull << Nbit)> 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<<mbit;
constexpr uint rbit = Nbit-mbit;
constexpr size_t r = 1ull<<rbit;
std::array<T,(1ull<<Nbit)> 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<T, (1ull << Nbit)> 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<T,mbit,rbit>(res);
for(int i = 0; i < m; i++)
NussbaumerTransform<T,rbit>(static_cast<std::span<T,r>>(res.subspan(i*r,r)));
NussbaumerButterfly<T, mbit, rbit>(res);
for (int i = 0; i < m; i++)
NussbaumerTransform<T, rbit>(
static_cast<std::span<T, r>>(res.subspan(i * r, r)));
}
}

template<typename T, uint mbit, uint rbit>
void InverseNussbaumerButterfly(const std::span<T,(1u<<(rbit+mbit))> 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<T,mbit-1,rbit>(res.template subspan<0,m*r/2>());
InverseNussbaumerButterfly<T,mbit-1,rbit>(res.template subspan<m*r/2,m*r/2>());
for(int i = 1; i < m/2; i++) PolynomialMulByXai<T, rbit>(static_cast<std::span<T,r>>(res.subspan((i+m/2)*r,r)),2*r-i*stride);
template <typename T, uint mbit, uint rbit>
void InverseNussbaumerButterfly(const std::span<T, (1u << (rbit + mbit))> 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<T, mbit - 1, rbit>(
res.template subspan<0, m * r / 2>());
InverseNussbaumerButterfly<T, mbit - 1, rbit>(
res.template subspan<m * r / 2, m * r / 2>());
for (int i = 1; i < m / 2; i++)
PolynomialMulByXai<T, rbit>(
static_cast<std::span<T, r>>(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<typename T, uint Nbit>
void InverseNussbaumerTransform(std::span<T,(1ull<<Nbit)> res){
if constexpr(Nbit == 1){
template <typename T, uint Nbit>
void InverseNussbaumerTransform(std::span<T, (1ull << Nbit)> 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<<mbit;
constexpr uint rbit = Nbit-mbit;
constexpr size_t r = 1ull<<rbit;
for(int i = 0; i < m; i++)
InverseNussbaumerTransform<T,rbit>(static_cast<std::span<T,r>>(res.subspan(i*r,r)));
InverseNussbaumerButterfly<T,mbit,rbit>(res);
std::array<T,(1ull<<Nbit)> 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<T, rbit>(
static_cast<std::span<T, r>>(res.subspan(i * r, r)));
InverseNussbaumerButterfly<T, mbit, rbit>(res);
std::array<T, (1ull << Nbit)> 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];
}
}
}
} // namespace Nussbaumer
3 changes: 2 additions & 1 deletion include/params/128bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct lvl1param {
static constexpr T μ = 1U << 29;
static constexpr uint32_t plain_modulus = 8;
static constexpr double Δ =
static_cast<double>(1ULL << std::numeric_limits<T>::digits) / plain_modulus;
static_cast<double>(1ULL << std::numeric_limits<T>::digits) /
plain_modulus;
};

struct lvl2param {
Expand Down
78 changes: 41 additions & 37 deletions test/gate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,47 @@ void RunTest()

if constexpr (std::is_same_v<P, lvl0param>) {
cout << "lvl0param" << endl;
Test<P>("NOT", TFHEpp::HomNOT<P>, NotChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("COPY", TFHEpp::HomCOPY<P>, CopyChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("NAND", TFHEpp::HomNAND<lvl01param, lvl1param::μ, lvl10param>,
NandChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("OR", TFHEpp::HomOR<lvl01param, lvl1param::μ, lvl10param>, OrChegk,
p, cres, c, kNumTests, *sk, ek);
Test<P>("ORYN", TFHEpp::HomORYN<lvl01param, lvl1param::μ, lvl10param>,
OrYNChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ORNY", TFHEpp::HomORNY<lvl01param, lvl1param::μ, lvl10param>,
OrNYChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("AND", TFHEpp::HomAND<lvl01param, lvl1param::μ, lvl10param>,
AndChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ANDYN", TFHEpp::HomANDYN<lvl01param, lvl1param::μ, lvl10param>,
AndYNChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ANDNY", TFHEpp::HomANDNY<lvl01param, lvl1param::μ, lvl10param>,
AndNYChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("XOR", TFHEpp::HomXOR<lvl01param, lvl1param::μ, lvl10param>,
XorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("XNOR", TFHEpp::HomXNOR<lvl01param, lvl1param::μ, lvl10param>,
XnorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("MUX", TFHEpp::HomMUX<P>, MuxChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("NMUX", TFHEpp::HomNMUX<P>, NMuxChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("ConstantZero", TFHEpp::HomCONSTANTZERO<P>, ConstantZeroChegk, p,
cres, c, kNumTests, *sk, ek);
Test<P>("ConstantOne", TFHEpp::HomCONSTANTONE<P>, ConstantOneChegk, p, cres,
c, kNumTests, *sk, ek);
Test<P>("NOT", TFHEpp::HomNOT<P>, NotChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("COPY", TFHEpp::HomCOPY<P>, CopyChegk, p, cres, c, kNumTests,
*sk, ek);
Test<P>("NAND", TFHEpp::HomNAND<lvl01param, lvl1param::μ, lvl10param>,
NandChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("OR", TFHEpp::HomOR<lvl01param, lvl1param::μ, lvl10param>,
OrChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ORYN", TFHEpp::HomORYN<lvl01param, lvl1param::μ, lvl10param>,
OrYNChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ORNY", TFHEpp::HomORNY<lvl01param, lvl1param::μ, lvl10param>,
OrNYChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("AND", TFHEpp::HomAND<lvl01param, lvl1param::μ, lvl10param>,
AndChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ANDYN", TFHEpp::HomANDYN<lvl01param, lvl1param::μ, lvl10param>,
AndYNChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ANDNY", TFHEpp::HomANDNY<lvl01param, lvl1param::μ, lvl10param>,
AndNYChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("XOR", TFHEpp::HomXOR<lvl01param, lvl1param::μ, lvl10param>,
XorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("XNOR", TFHEpp::HomXNOR<lvl01param, lvl1param::μ, lvl10param>,
XnorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("MUX", TFHEpp::HomMUX<P>, MuxChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("NMUX", TFHEpp::HomNMUX<P>, NMuxChegk, p, cres, c, kNumTests,
*sk, ek);
Test<P>("ConstantZero", TFHEpp::HomCONSTANTZERO<P>, ConstantZeroChegk,
p, cres, c, kNumTests, *sk, ek);
Test<P>("ConstantOne", TFHEpp::HomCONSTANTONE<P>, ConstantOneChegk, p,
cres, c, kNumTests, *sk, ek);
}
else if constexpr (std::is_same_v<P, lvl1param>) {
cout << "lvl1param" << endl;
Test<P>("NOT", TFHEpp::HomNOT<P>, NotChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("COPY", TFHEpp::HomCOPY<P>, CopyChegk, p, cres, c, kNumTests, *sk,
Test<P>("NOT", TFHEpp::HomNOT<P>, NotChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("COPY", TFHEpp::HomCOPY<P>, CopyChegk, p, cres, c, kNumTests,
*sk, ek);
Test<P>("NAND", TFHEpp::HomNAND<lvl10param, lvl01param, lvl1param::μ>,
NandChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("OR", TFHEpp::HomOR<lvl10param, lvl01param, lvl1param::μ>, OrChegk,
p, cres, c, kNumTests, *sk, ek);
Test<P>("OR", TFHEpp::HomOR<lvl10param, lvl01param, lvl1param::μ>,
OrChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ORYN", TFHEpp::HomORYN<lvl10param, lvl01param, lvl1param::μ>,
OrYNChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("ORNY", TFHEpp::HomORNY<lvl10param, lvl01param, lvl1param::μ>,
Expand All @@ -198,13 +201,14 @@ void RunTest()
XorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("XNOR", TFHEpp::HomXNOR<lvl10param, lvl01param, lvl1param::μ>,
XnorChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("MUX", TFHEpp::HomMUX<P>, MuxChegk, p, cres, c, kNumTests, *sk, ek);
Test<P>("NMUX", TFHEpp::HomNMUX<P>, NMuxChegk, p, cres, c, kNumTests, *sk,
Test<P>("MUX", TFHEpp::HomMUX<P>, MuxChegk, p, cres, c, kNumTests, *sk,
ek);
Test<P>("ConstantZero", TFHEpp::HomCONSTANTZERO<P>, ConstantZeroChegk, p,
Test<P>("NMUX", TFHEpp::HomNMUX<P>, NMuxChegk, p, cres, c, kNumTests,
*sk, ek);
Test<P>("ConstantZero", TFHEpp::HomCONSTANTZERO<P>, ConstantZeroChegk,
p, cres, c, kNumTests, *sk, ek);
Test<P>("ConstantOne", TFHEpp::HomCONSTANTONE<P>, ConstantOneChegk, p,
cres, c, kNumTests, *sk, ek);
Test<P>("ConstantOne", TFHEpp::HomCONSTANTONE<P>, ConstantOneChegk, p, cres,
c, kNumTests, *sk, ek);
}
}

Expand Down
Loading

0 comments on commit 76223d1

Please sign in to comment.