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 "encoding/encodingparams.h"
#include "encoding/plaintext.h"
#include "utils/inttypes.h"
#include <functional>
#include <initializer_list>
#include <map>
#include <memory>
#include <numeric>
#include <utility>
#include <vector>
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, PACKED_ENCODING) {}
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, PACKED_ENCODING), 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, PACKED_ENCODING), value(coeffs) {}
explicit PackedEncoding(const std::vector<int64_t>& rhs)
: PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr, PACKED_ENCODING), value(rhs) {}
PackedEncoding(std::initializer_list<int64_t> arr)
: PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr, PACKED_ENCODING), value(arr) {}
PackedEncoding() : PlaintextImpl(std::shared_ptr<Poly::Params>(0), nullptr, PACKED_ENCODING), value() {}
static uint32_t GetAutomorphismGenerator(uint32_t m) {
return m_automorphismGenerator[m];
}
bool Encode() override;
bool Decode() override;
const std::vector<int64_t>& GetPackedValue() const override {
return value;
}
void SetIntVectorValue(const std::vector<int64_t>& val) override {
value = val;
}
size_t GetLength() const override {
return value.size();
}
static void SetParams(uint32_t m, EncodingParams params);
static void SetParams(uint32_t m, const PlaintextModulus& modulus)
__attribute__((deprecated("use SetParams(uint32_t m, EncodingParams p)")));
void SetLength(size_t siz) override {
value.resize(siz);
}
static void Destroy();
protected:
void PrintValue(std::ostream& out) const override {
out << "(";
// for sanity's sake: get rid of all trailing zeroes and print "..." instead
size_t i = value.size();
bool allZeroes = true;
while (i > 0) {
--i;
if (value[i] != 0) {
allZeroes = false;
break;
}
}
if (allZeroes == false) {
for (size_t j = 0; j <= i; ++j)
out << value[j] << ", ";
}
out << "... )";
}
bool CompareTo(const PlaintextImpl& rhs) const override {
if (typeid(rhs) != typeid(PackedEncoding))
return false;
const auto& el = static_cast<const PackedEncoding&>(rhs);
return value == el.value;
}
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<uint32_t, uint32_t> m_automorphismGenerator;
static std::map<uint32_t, std::vector<uint32_t>> m_toCRTPerm;
static std::map<uint32_t, std::vector<uint32_t>> m_fromCRTPerm;
static void SetParams_2n(uint32_t m, NativeInteger modulusNI);
static void SetParams_2n(uint32_t m, EncodingParams params);
template <typename P>
void Pack(P* ring, const PlaintextModulus& modulus) const;
// Optimized version of packing designed for DCRTPoly and NativePoly
void PackNativeVector(const PlaintextModulus& modulus, uint32_t m, NativeVector* values) const;
template <typename P>
void Unpack(P* ring, const PlaintextModulus& modulus) const;
};
} // namespace lbcrypto
#endif