Program Listing for File transform.h

Return to documentation for file (core/include/math/hal/transform.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.
//==================================================================================

/*
  This file contains the interface for the transforms in each math backend
 */

#ifndef LBCRYPTO_MATH_TRANSFORM_H
#define LBCRYPTO_MATH_TRANSFORM_H

#include "utils/inttypes.h"

#include <complex>
#include <map>
#include <utility>
#include <vector>

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

namespace lbcrypto {

template <typename VecType>
class ChineseRemainderTransformFTTInterface {
    using IntType = typename VecType::Integer;

public:
    virtual void ForwardTransformToBitReverse(const VecType& element, const IntType& rootOfUnity,
                                              const usint CycloOrder, VecType* result) = 0;

    virtual void ForwardTransformToBitReverseInPlace(const IntType& rootOfUnity, const usint CycloOrder,
                                                     VecType* element) = 0;

    virtual void InverseTransformFromBitReverse(const VecType& element, const IntType& rootOfUnity,
                                                const usint CycloOrder, VecType* result) = 0;

    virtual void InverseTransformFromBitReverseInPlace(const IntType& rootOfUnity, const usint CycloOrder,
                                                       VecType* element) = 0;

    virtual void PreCompute(const IntType& rootOfUnity, const usint CycloOrder, const IntType& modulus) = 0;

    virtual void PreCompute(std::vector<IntType>& rootOfUnity, const usint CycloOrder,
                            std::vector<IntType>& moduliChain) = 0;

    virtual void Reset() = 0;
};

template <typename VecType>
class ChineseRemainderTransformArbInterface {
    using IntType = typename VecType::Integer;

public:
    virtual void SetCylotomicPolynomial(const VecType& poly, const IntType& mod) = 0;

    virtual VecType ForwardTransform(const VecType& element, const IntType& root, const IntType& bigMod,
                                     const IntType& bigRoot, const usint cycloOrder) = 0;

    virtual VecType InverseTransform(const VecType& element, const IntType& root, const IntType& bigMod,
                                     const IntType& bigRoot, const usint cycloOrder) = 0;

    virtual void Reset() = 0;

    virtual void PreCompute(const usint cyclotoOrder, const IntType& modulus) = 0;

    virtual void SetPreComputedNTTModulus(usint cyclotoOrder, const IntType& modulus, const IntType& nttMod,
                                          const IntType& nttRoot) = 0;

    virtual void SetPreComputedNTTDivisionModulus(usint cyclotoOrder, const IntType& modulus, const IntType& nttMod,
                                                  const IntType& nttRoot) = 0;

    virtual VecType InversePolyMod(const VecType& cycloPoly, const IntType& modulus, usint power) = 0;

private:
    virtual VecType Pad(const VecType& element, const usint cycloOrder, bool forward) = 0;

    virtual VecType Drop(const VecType& element, const usint cycloOrder, bool forward, const IntType& bigMod,
                         const IntType& bigRoot) = 0;
};
}  // namespace lbcrypto

#endif