Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuponitskiy-duality committed Dec 11, 2023
1 parent 1f02d17 commit 78075e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
17 changes: 6 additions & 11 deletions src/pke/lib/scheme/ckksrns/ckksrns-fhe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ void FHECKKSRNS::EvalBootstrapSetup(const CryptoContextImpl<DCRTPoly>& cc, std::
precom->m_dim1 = dim1[0];

uint32_t logSlots = std::log2(slots);

// even for the case of a single slot we need one level for rescaling
if (logSlots == 0) {
logSlots = 1;
Expand All @@ -115,24 +114,20 @@ void FHECKKSRNS::EvalBootstrapSetup(const CryptoContextImpl<DCRTPoly>& cc, std::
std::vector<uint32_t> newBudget = levelBudget;

if (newBudget[0] > logSlots) {
std::cerr << "\nWarning, the level budget for encoding cannot be this large. The budget was changed to "
<< uint32_t(logSlots) << std::endl;
newBudget[0] = uint32_t(logSlots);
std::cerr << "\nWarning, the level budget for encoding is too large. Setting it to " << logSlots << std::endl;
newBudget[0] = logSlots;
}
if (newBudget[0] < 1) {
std::cerr << "\nWarning, the level budget for encoding has to be at least 1. The budget was changed to " << 1
<< std::endl;
std::cerr << "\nWarning, the level budget for encoding can not be zero. Setting it to 1" << std::endl;
newBudget[0] = 1;
}

if (newBudget[1] > logSlots) {
std::cerr << "\nWarning, the level budget for decoding cannot be this large. The budget was changed to "
<< uint32_t(logSlots) << std::endl;
newBudget[1] = uint32_t(logSlots);
std::cerr << "\nWarning, the level budget for decoding is too large. Setting it to " << logSlots << std::endl;
newBudget[1] = logSlots;
}
if (newBudget[1] < 1) {
std::cerr << "\nWarning, the level budget for decoding has to be at least 1. The budget was changed to " << 1
<< std::endl;
std::cerr << "\nWarning, the level budget for decoding can not be zero. Setting it to 1" << std::endl;
newBudget[1] = 1;
}

Expand Down
27 changes: 8 additions & 19 deletions src/pke/lib/scheme/ckksrns/ckksrns-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,36 +807,26 @@ std::vector<uint32_t> SelectLayers(uint32_t logSlots, uint32_t budget) {
}

std::vector<int32_t> GetCollapsedFFTParams(uint32_t slots, uint32_t levelBudget, uint32_t dim1) {
// Need to compute how many layers are collapsed in each of the level from the budget.
// If there is no exact division between the maximum number of possible levels (log(slots)) and the
// level budget, the last level will contain the remaining layers collapsed.
int32_t layersCollapse;
int32_t remCollapse;

uint32_t logSlots = std::log2(slots);

// even for the case of a single slot we need one level for rescaling
if (logSlots == 0) {
logSlots = 1;
}

std::vector<uint32_t> dims = SelectLayers(logSlots, levelBudget);
layersCollapse = dims[0];
remCollapse = dims[2];
// Need to compute how many layers are collapsed in each of the level from the budget.
// If there is no exact division between the maximum number of possible levels (log(slots)) and the
// level budget, the last level will contain the remaining layers collapsed.
int32_t layersCollapse = dims[0];
int32_t remCollapse = dims[2];

int32_t flagRem = 0;
if (remCollapse == 0) {
flagRem = 0;
}
else {
flagRem = 1;
}
bool flagRem = (remCollapse == 0) ? false : true;

uint32_t numRotations = (1 << (layersCollapse + 1)) - 1;
uint32_t numRotationsRem = (1 << (remCollapse + 1)) - 1;

// Computing the baby-step b and the giant-step g for the collapsed layers for decoding.
int32_t b, g;
int32_t g;
if (dim1 == 0 || dim1 > numRotations) {
if (numRotations > 7) {
g = (1 << (int32_t(layersCollapse / 2) + 2));
Expand All @@ -848,11 +838,10 @@ std::vector<int32_t> GetCollapsedFFTParams(uint32_t slots, uint32_t levelBudget,
else {
g = dim1;
}
int32_t b = (numRotations + 1) / g;

b = (numRotations + 1) / g;
int32_t bRem = 0;
int32_t gRem = 0;

if (flagRem) {
if (numRotationsRem > 7) {
gRem = (1 << (int32_t(remCollapse / 2) + 2));
Expand Down

0 comments on commit 78075e9

Please sign in to comment.