Program Listing for File ckksrns-utils.h
↰ Return to documentation for file (pke/include/scheme/ckksrns/ckksrns-utils.h)
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
#ifndef _CKKSRNS_UTILS_H_
#define _CKKSRNS_UTILS_H_
#include "utils/exception.h"
#include <complex>
#include <memory>
#include <stdint.h>
#include <vector>
/*
* Subroutines used by the linear transformation homomorphic capability
*/
namespace lbcrypto {
template <typename VecDType>
struct longDiv {
std::vector<VecDType> q;
std::vector<VecDType> r;
longDiv() = default;
longDiv(const std::vector<VecDType>& q0, const std::vector<VecDType>& r0) : q(q0), r(r0) {}
};
inline bool IsNotEqualOne(double v, double delta = 0x1p-44) {
return std::abs(v - 1.0) > delta;
}
inline bool IsNotEqualZero(double v, double delta = 0x1p-44) {
return std::abs(v) > delta;
}
inline bool IsNotEqualNegOne(double v, double delta = 0x1p-44) {
return std::abs(v + 1.0) > delta;
}
inline bool IsNotEqualOne(std::complex<double> val, double delta = 0x1p-44) {
return IsNotEqualOne(val.real(), delta) || IsNotEqualZero(val.imag(), delta);
}
inline bool IsNotEqualZero(std::complex<double> val, double delta = 0x1p-44) {
return IsNotEqualZero(val.real(), delta) || IsNotEqualZero(val.imag(), delta);
}
inline double ToReal(double val) {
return val;
}
inline double ToReal(int64_t val) {
return static_cast<double>(val);
}
inline double ToReal(std::complex<double> val) {
return val.real();
}
template <typename VecDType>
uint32_t Degree(const std::vector<VecDType>& coefficients, double delta = 0.0) {
uint32_t i = coefficients.size();
if (i == 0)
OPENFHE_THROW("Coefficients vector can not be empty");
while (i > 0) {
if (IsNotEqualZero(coefficients[--i], delta))
break;
}
return i;
}
template <typename VecDType>
std::shared_ptr<longDiv<VecDType>> LongDivisionPoly(const std::vector<VecDType>& f, const std::vector<VecDType>& g);
template <typename VecDType>
std::shared_ptr<longDiv<VecDType>> LongDivisionChebyshev(const std::vector<VecDType>& f,
const std::vector<VecDType>& g);
std::vector<uint32_t> ComputeDegreesPS(uint32_t n);
uint32_t GetDepthByDegree(size_t degree);
template <typename VecDType>
uint32_t GetMultiplicativeDepthByCoeffVector(const std::vector<VecDType>& vec, bool isNormalized = false) {
if (vec.size() == 0)
OPENFHE_THROW("Cannot perform operation on empty vector. vec.size() == 0");
return GetDepthByDegree(vec.size() - 1) - isNormalized;
}
std::vector<std::complex<double>> ExtractShiftedDiagonal(const std::vector<std::vector<std::complex<double>>>& A,
int i);
template <typename VecDType>
std::vector<VecDType> Rotate(const std::vector<VecDType>& a, int32_t index);
template <typename VecDType>
std::vector<VecDType> RotateTwoHalves(const std::vector<VecDType>& a, int32_t index);
template <typename VecDType = std::vector<std::complex<double>>>
std::vector<VecDType> Fill(const std::vector<VecDType>& a, uint32_t slots);
template <typename VecDType = std::vector<std::complex<double>>>
std::vector<VecDType> Fill(std::initializer_list<VecDType> a, uint32_t slots);
/*
template <typename VecDType = std::complex<double>>
std::vector<VecDType> Fill(const std::vector<VecDType>& a, uint32_t slots);
*/
std::vector<std::vector<std::complex<double>>> CoeffEncodingOneLevel(const std::vector<std::complex<double>>& pows,
const std::vector<uint32_t>& rotGroup,
bool flag_i);
std::vector<std::vector<std::complex<double>>> CoeffDecodingOneLevel(const std::vector<std::complex<double>>& pows,
const std::vector<uint32_t>& rotGroup,
bool flag_i);
std::vector<std::vector<std::vector<std::complex<double>>>> CoeffEncodingCollapse(
const std::vector<std::complex<double>>& pows, const std::vector<uint32_t>& rotGroup, uint32_t levelBudget,
bool flag_i);
std::vector<std::vector<std::vector<std::complex<double>>>> CoeffDecodingCollapse(
const std::vector<std::complex<double>>& pows, const std::vector<uint32_t>& rotGroup, uint32_t levelBudget,
bool flag_i);
uint32_t ReduceRotation(int32_t index, uint32_t slots);
struct ckks_boot_params {
uint32_t lvlb; // level budget
uint32_t layersCollapse; // layers to collapse in one level
uint32_t remCollapse; // remaining layers to collapse
uint32_t numRotations; // umber of rotations in one level
uint32_t b; // baby step in the baby-step giant-step strategy
uint32_t g; // giant step in the baby-step giant-step strategy
uint32_t numRotationsRem; // number of rotations in the remaining level
uint32_t bRem; // baby step in the baby-step giant-step strategy for the remaining level
uint32_t gRem; // giant step in the baby-step giant-step strategy for the remaining level
};
struct ckks_boot_params GetCollapsedFFTParams(uint32_t slots, uint32_t levelBudget = 4, uint32_t dim1 = 0);
uint32_t getRatioBSGSLT(uint32_t slots);
std::vector<int32_t> FindLTRotationIndicesSwitch(uint32_t dim1, uint32_t m, uint32_t blockDimension);
std::vector<int32_t> FindLTRotationIndicesSwitchArgmin(uint32_t m, uint32_t blockDimension, uint32_t cols);
} // namespace lbcrypto
#endif