Program Listing for File field2n.h

Return to documentation for file (core/include/lattice/field2n.h)

//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2023, 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.
//==================================================================================

/*
  Represents and defines power-of-2 fields
 */

#ifndef LBCRYPTO_INC_LATTICE_FIELD2N_H
#define LBCRYPTO_INC_LATTICE_FIELD2N_H

#include "lattice/lat-hal.h"

#include "math/matrix.h"

#include "utils/exception.h"
#include "utils/inttypes.h"
#include "utils/serializable.h"

#include <complex>
#include <limits>
#include <string>
#include <vector>

namespace lbcrypto {
class Field2n : public std::vector<std::complex<double>>, public Serializable {
private:
    // Format of the field element
    Format format{Format::COEFFICIENT};

public:
    Field2n() noexcept = default;

    explicit Field2n(Format f) : format(f) {}

    Field2n(usint size, Format f = Format::EVALUATION, bool initializeElementToZero = false)  // NOLINT
        : std::vector<std::complex<double>>(size, initializeElementToZero ? 0 : -std::numeric_limits<double>::max()),
          format(f) {}

    explicit Field2n(const Poly& element);

    explicit Field2n(const NativePoly& element);

    explicit Field2n(const DCRTPoly& element);

    explicit Field2n(const Matrix<int64_t>& element);

    Format GetFormat() const {
        return format;
    }

    Field2n Inverse() const;

    Field2n Plus(const Field2n& rhs) const;

    Field2n Plus(double rhs) const;

    Field2n Minus(const Field2n& rhs) const;

    Field2n Times(const Field2n& rhs) const;

    Field2n ShiftRight();

    Field2n AutomorphismTransform(size_t i) const;

    Field2n Transpose() const;

    Field2n ExtractOdd() const;

    Field2n ExtractEven() const;

    Field2n Permute() const;

    Field2n InversePermute() const;

    Field2n ScalarMult(double d);

    void SwitchFormat();

    inline void SetFormat(Format f) {
        if (format != f)
            SwitchFormat();
    }

    size_t Size() const {
        return this->std::vector<std::complex<double>>::size();
    }

    inline std::complex<double>& operator[](size_t idx) {
        return this->std::vector<std::complex<double>>::operator[](idx);
    }

    inline const std::complex<double>& operator[](size_t idx) const {
        return this->std::vector<std::complex<double>>::operator[](idx);
    }

    Field2n& operator+=(const Field2n& element) {
        return *this = this->Plus(element);
    }

    Field2n& operator-=(const Field2n& element) {
        return *this = this->Minus(element);
    }

    Field2n operator-() const {
        return Field2n(size(), this->GetFormat(), true) - *this;
    }

    friend inline Field2n operator-(const Field2n& a, const Field2n& b) {
        return a.Minus(b);
    }

    friend inline Field2n operator+(const Field2n& a, const Field2n& b) {
        return a.Plus(b);
    }

    friend inline Field2n operator+(const Field2n& a, double scalar) {
        return a.Plus(scalar);
    }

    friend inline Field2n operator*(const Field2n& a, const Field2n& b) {
        return a.Times(b);
    }

    template <class Archive>
    void save(Archive& ar, std::uint32_t const version) const {
        ar(::cereal::base_class<std::vector<std::complex<double>>>(this));
        ar(::cereal::make_nvp("f", format));
    }

    template <class Archive>
    void load(Archive& ar, std::uint32_t const version) {
        if (version > SerializedVersion()) {
            OPENFHE_THROW("serialized object version " + std::to_string(version) +
                          " is from a later version of the library");
        }
        ar(::cereal::base_class<std::vector<std::complex<double>>>(this));
        ar(::cereal::make_nvp("f", format));
    }

    std::string SerializedObjectName() const override {
        return "Field2n";
    }
    static uint32_t SerializedVersion() {
        return 1;
    }
};

inline std::ostream& operator<<(std::ostream& os, const Field2n& m) {
    os << "[ ";
    for (size_t row = 0; row < m.size(); ++row)
        os << m.at(row) << " ";
    os << " ]\n";
    return os;
}

}  // namespace lbcrypto

#endif