Program Listing for File packedencoding.h
↰ Return to documentation for file (pke/include/encoding/packedencoding.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.
//==================================================================================
/*
Represents and defines plaintext encodings in OpenFHE with packing capabilities
*/
#ifndef LBCRYPTO_UTILS_PACKEDEXTENCODING_H
#define LBCRYPTO_UTILS_PACKEDEXTENCODING_H
#include <functional>
#include <initializer_list>
#include <map>
#include <memory>
#include <numeric>
#include <utility>
#include <vector>
#include "encoding/encodingparams.h"
#include "encoding/plaintext.h"
#include "utils/inttypes.h"
namespace lbcrypto {
// STL pair used as a key for some tables in PackedEncoding
using ModulusM = std::pair<NativeInteger, uint64_t>;
class PackedEncoding : public PlaintextImpl {
std::vector<int64_t> value;
public:
// these two constructors are used inside of Decrypt
template <typename T, typename std::enable_if<std::is_same<T, Poly::Params>::value ||
std::is_same<T, NativePoly::Params>::value ||
std::is_same<T, DCRTPoly::Params>::value,
bool>::type = true>
PackedEncoding(std::shared_ptr<T> vp, EncodingParams ep) : PlaintextImpl(vp, ep) {}
template <typename T, typename std::enable_if<std::is_same<T, Poly::Params>::value ||
std::is_same<T, NativePoly::Params>::value ||
std::is_same<T, DCRTPoly::Params>::value,
bool>::type = true>
PackedEncoding(std::shared_ptr<T> vp, EncodingParams ep, const std::vector<int64_t>& coeffs)
: PlaintextImpl(vp, ep), value(coeffs) {}
template <typename T, typename std::enable_if<std::is_same<T, Poly::Params>::value ||
std::is_same<T, NativePoly::Params>::value ||
std::is_same<T, DCRTPoly::Params>::value,
bool>::type = true>
PackedEncoding(std::shared_ptr<T> vp, EncodingParams ep, std::initializer_list<int64_t> coeffs)
: PlaintextImpl(vp, ep), value(coeffs) {}
explicit PackedEncoding(const std::vector<int64_t>& rhs)
: PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr), value(rhs) {}
PackedEncoding(std::initializer_list<int64_t> arr)
: PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr), value(arr) {}
PackedEncoding() : PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr), value() {}
static usint GetAutomorphismGenerator(usint m) {
return m_automorphismGenerator[m];
}
bool Encode();
bool Decode();
const std::vector<int64_t>& GetPackedValue() const {
return value;
}
void SetIntVectorValue(const std::vector<int64_t>& val) {
value = val;
}
PlaintextEncodings GetEncodingType() const {
return PACKED_ENCODING;
}
size_t GetLength() const {
return value.size();
}
static void SetParams(usint m, EncodingParams params);
static void SetParams(usint m, const PlaintextModulus& modulus)
__attribute__((deprecated("use SetParams(usint m, EncodingParams p)")));
void SetLength(size_t siz) {
value.resize(siz);
}
bool CompareTo(const PlaintextImpl& other) const {
const auto& rv = static_cast<const PackedEncoding&>(other);
return this->value == rv.value;
}
static void Destroy();
void PrintValue(std::ostream& out) const {
// for sanity's sake, trailing zeros get elided into "..."
out << "(";
size_t i = value.size();
while (--i > 0)
if (value[i] != 0)
break;
for (size_t j = 0; j <= i; j++)
out << ' ' << value[j];
out << " ... )";
}
private:
// initial root of unity for plaintext space
static std::map<ModulusM, NativeInteger> m_initRoot;
// modulus and root of unity to be used for Arbitrary CRT
static std::map<ModulusM, NativeInteger> m_bigModulus;
static std::map<ModulusM, NativeInteger> m_bigRoot;
// stores the list of primitive roots used in packing.
static std::map<usint, usint> m_automorphismGenerator;
static std::map<usint, std::vector<usint>> m_toCRTPerm;
static std::map<usint, std::vector<usint>> m_fromCRTPerm;
static void SetParams_2n(usint m, const NativeInteger& modulusNI);
static void SetParams_2n(usint m, EncodingParams params);
template <typename P>
void Pack(P* ring, const PlaintextModulus& modulus) const;
template <typename P>
void Unpack(P* ring, const PlaintextModulus& modulus) const;
};
} // namespace lbcrypto
#endif