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 <vector>
#include <complex>
#include <iostream>
#include <stdint.h>
#include <memory>

/*
 * Subroutines used by the linear transformation homomorphic capability
 */

namespace lbcrypto {

struct longDiv {
    std::vector<double> q;
    std::vector<double> r;

    longDiv() {}
    longDiv(const std::vector<double>& q0, const std::vector<double>& r0) : q(q0), r(r0) {}
};

uint32_t Degree(const std::vector<double>& coefficients);

std::shared_ptr<longDiv> LongDivisionPoly(const std::vector<double>& f, const std::vector<double>& g);

std::shared_ptr<longDiv> LongDivisionChebyshev(const std::vector<double>& f, const std::vector<double>& g);

std::vector<uint32_t> ComputeDegreesPS(const uint32_t n);

uint32_t GetMultiplicativeDepthByCoeffVector(const std::vector<double>& vec, bool isNormalized = false);

std::vector<std::complex<double>> ExtractShiftedDiagonal(const std::vector<std::vector<std::complex<double>>>& A,
                                                         int index);

std::vector<std::complex<double>> Rotate(const std::vector<std::complex<double>>& a, int32_t index);

std::vector<std::complex<double>> Fill(const std::vector<std::complex<double>>& a, int 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);

std::vector<uint32_t> SelectLayers(uint32_t logSlots, uint32_t budget = 4);

std::vector<int32_t> 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 CKKS_BOOT_PARAMS {
enum {
    LEVEL_BUDGET,  // the level budget
    LAYERS_COLL,   // the number of layers to collapse in one level
    LAYERS_REM,  // the number of layers remaining to be collapsed in one level to have exactly the number of levels specified in the level budget
    NUM_ROTATIONS,      // the number of rotations in one level
    BABY_STEP,          // the baby step in the baby-step giant-step strategy
    GIANT_STEP,         // the giant step in the baby-step giant-step strategy
    NUM_ROTATIONS_REM,  // the number of rotations in the remaining level
    BABY_STEP_REM,      // the baby step in the baby-step giant-step strategy for the remaining level
    GIANT_STEP_REM,     // the giant step in the baby-step giant-step strategy for the remaining level
    TOTAL_ELEMENTS      // total number of elements in the vector
};
}  // namespace CKKS_BOOT_PARAMS

}  // namespace lbcrypto

#endif