Palisade pke headers 第二部分
文章目录
- Palisade pke headers 第二部分
- \pke\ciphertext.h
- \pke\ciphertext-ser.h
- \pke\pubkeylp-ser.h
- \pke\pubkeylp.h
-
-
- EncryptionResult 结构体
- DecryptionResult 结构体
- LPKey 类
- LPPublicKeyImpl 类
- LPEvalKeyImpl 类
- LPEvalKeyRelinImpl 类
- LPPrivateKeyImpl 类
- LPKeyPair 类
- LPParameterGenerationAlgorithm 类
- LPEncryptionAlgorithm 类
- LPLeveledSHEAlgorithm 类
- LPPREAlgorithm 类
- LPMultipartyAlgorithm 类
- LPSHEAlgorithm 类
- LPCryptoParameters 类
- PalisadeSchemeIdentifier 类
- LPPublicKeyEncryptionScheme 类
-
\pke\ciphertext.h
Operations for the representation of ciphertext in PALISADE.
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "palisade.h"
namespace lbcrypto {
template <typename Element>
class CiphertextImpl;
CiphertextImpl 类
/**
* @brief CiphertextImpl
*
* The CiphertextImpl object is used to contain encrypted text in the PALISADE
* library
*
* @tparam Element a ring element.
*/
template <class Element>
class CiphertextImpl : public CryptoObject<Element> {
public:
/**
* Default constructor
*/
CiphertextImpl()
: CryptoObject<Element>(),
m_depth(1),
encodingType(Unknown),
m_scalingFactor(1),
m_level(0) {
m_metadataMap = std::make_shared<std::map<string, shared_ptr<Metadata>>>();
}
/**
* Construct a new ciphertext in the given context
*
* @param cc
*/
explicit CiphertextImpl(CryptoContext<Element> cc, const string& id = "",
PlaintextEncodings encType = Unknown)
: CryptoObject<Element>(cc, id),
m_depth(1),
encodingType(encType),
m_scalingFactor(1),
m_level(0) {
m_metadataMap = std::make_shared<std::map<string, shared_ptr<Metadata>>>();
}
/**
* Construct a new ciphertext from the parameters of a given public key
*
* @param k key whose CryptoObject parameters will get cloned
*/
explicit CiphertextImpl(const shared_ptr<LPKey<Element>> k)
: CryptoObject<Element>(k->GetCryptoContext(), k->GetKeyTag()),
m_depth(1),
encodingType(Unknown),
m_scalingFactor(1),
m_level(0) {
m_metadataMap = std::make_shared<std::map<string, shared_ptr<Metadata>>>();
}
/**
* Copy constructor
*/
CiphertextImpl(const CiphertextImpl<Element>& ciphertext)
: CryptoObject<Element>(ciphertext) {
m_elements = ciphertext.m_elements;
m_depth = ciphertext.m_depth;
m_level = ciphertext.m_level;
m_scalingFactor = ciphertext.m_scalingFactor;
encodingType = ciphertext.encodingType;
m_metadataMap = ciphertext.m_metadataMap;
}
explicit CiphertextImpl(Ciphertext<Element> ciphertext)
: CryptoObject<Element>(*ciphertext) {
m_elements = ciphertext->m_elements;
m_depth = ciphertext->m_depth;
m_level = ciphertext->m_level;
m_scalingFactor = ciphertext->m_scalingFactor;
encodingType = ciphertext->encodingType;
m_metadataMap = ciphertext->m_metadataMap;
}
/**
* Move constructor
*/
CiphertextImpl(CiphertextImpl<Element>&& ciphertext)
: CryptoObject<Element>(ciphertext) {
m_elements = std::move(ciphertext.m_elements);
m_depth = std::move(ciphertext.m_depth);
m_level = std::move(ciphertext.m_level);
m_scalingFactor = std::move(ciphertext.m_scalingFactor);
encodingType = std::move(ciphertext.encodingType);
m_metadataMap = std::move(ciphertext.m_metadataMap);
}
explicit CiphertextImpl(Ciphertext<Element>&& ciphertext)
: CryptoObject<Element>(*ciphertext) {
m_elements = std::move(ciphertext->m_elements);
m_depth = std::move(ciphertext->m_depth);
m_level = std::move(ciphertext->m_level);
m_scalingFactor = std::move(ciphertext->m_scalingFactor);
encodingType = std::move(ciphertext->encodingType);
m_metadataMap = std::move(ciphertext->m_metadataMap);
}
/**
* This method creates a copy of this, skipping the actual encrypted
* elements. This means it copies parameters, key tags, encoding type,
* and metadata.
*/
virtual Ciphertext<Element> CloneEmpty() const {
Ciphertext<Element> ct(std::make_shared<CiphertextImpl<Element>>(
this->GetCryptoContext(), this->GetKeyTag(), this->GetEncodingType()));
ct->m_metadataMap =
std::make_shared<std::map<string, shared_ptr<Metadata>>>();
*(ct->m_metadataMap) = *(this->m_metadataMap);
return ct;
}
/**
* Destructor
*/
virtual ~CiphertextImpl() {
}
/**
* GetEncodingType
* @return how the Plaintext that this CiphertextImpl was created from was
* encoded
*/
PlaintextEncodings GetEncodingType() const {
return encodingType; }
/**
* SetEncodingType - after Encrypt, remember the CiphertextImpl's encoding
* type
* @param et
*/
void SetEncodingType(PlaintextEncodings et) {
encodingType = et; }
/**
* Assignment Operator.
*
* @param &rhs the CiphertextImpl to assign from
* @return this CiphertextImpl
*/
CiphertextImpl<Element>& operator=(const CiphertextImpl<Element>& rhs) {
if (this != &rhs) {
CryptoObject<Element>::operator=(rhs);
this->m_elements = rhs.m_elements;
this->m_depth = rhs.m_depth;
this->m_level = rhs.m_level;
this->m_scalingFactor = rhs.m_scalingFactor;
this->encodingType = rhs.encodingType;
this->m_metadataMap = rhs.m_metadataMap;
}
return *this;
}
/**
* Move Assignment Operator.
*
* @param &rhs the CiphertextImpl to move from
* @return this CiphertextImpl
*/
CiphertextImpl<Element>& operator=(CiphertextImpl<Element>&& rhs) {
if (this != &rhs) {
CryptoObject<Element>::operator=(rhs);
this->m_elements = std::move(rhs.m_elements);
this->m_depth = std::move(rhs.m_depth);
this->m_level = std::move(rhs.m_level);
this->m_scalingFactor = std::move(rhs.m_scalingFactor);
this->encodingType = std::move(rhs.encodingType);
this->m_metadataMap = std::move(rhs.m_metadataMap);
}
return *this;
}
/**
* GetElement - get the ring element for the cases that use only one element
* in the vector this method will throw an exception if it's ever called in
* cases with other than 1 element
* @return the first (and only!) ring element
*/
const Element& GetElement() const {
if (m_elements.size() == 1) return m_elements[0];
PALISADE_THROW(config_error,
"GetElement should only be used in cases with a "
"Ciphertext with a single element");
}
/**
* GetElement - get the ring element for the cases that use only one element
* in the vector this method will throw an exception if it's ever called in
* cases with other than 1 element
* @return the first (and only!) ring element
*/
Element& GetElement() {
if (m_elements.size() == 1) return m_elements[0];
PALISADE_THROW(config_error,
"GetElement should only be used in cases with a "
"Ciphertext with a single element");
}
/**
* GetElements: get all of the ring elements in the CiphertextImpl
* @return vector of ring elements
*/
const std::vector<Element>& GetElements() const {
return m_elements; }
/**
* GetElements: get all of the ring elements in the CiphertextImpl
* @return vector of ring elements
*/
std::vector<Element>& GetElements() {
return m_elements; }
/**
* SetElement - sets the ring element for the cases that use only one element
* in the vector this method will throw an exception if it's ever called in
* cases with other than 1 element
* @param &element is a polynomial ring element.
*/
void SetElement(const Element& element) {
if (m_elements.size() == 0)
m_elements.push_back(element);
else if (m_elements.size() == 1)
m_elements[0] = element;
else
PALISADE_THROW(config_error,
"SetElement should only be used in cases with a "
"Ciphertext with a single element");
}
/**
* Sets the data elements.
*
* @param &element is a polynomial ring element.
*/
void SetElements(const std::vector<Element>& elements) {
m_elements = elements;
}
/**
* Sets the data elements by std::move.
*
* @param &&element is a polynomial ring element.
*/
void SetElements(std::vector<Element>&& elements) {
m_elements = std::move(elements);
}
/**
* Get the depth of the ciphertext.
* It will be used in multiplication/addition/subtraction to handle the
* keyswitching.
*/
size_t GetDepth() const {
return m_depth; }
/**
* Set the depth of the ciphertext.
* It will be used in multiplication/addition/subtraction to handle the
* keyswitching.
*/
void SetDepth(size_t depth) {
m_depth = depth; }
/**
* Get the level of the ciphertext.
*/
size_t GetLevel() const {
return m_level; }
/**
* Set the level of the ciphertext.
*/
void SetLevel(size_t level) {
m_level = level; }
/**
* Get the scaling factor of the ciphertext.
*/
double GetScalingFactor() const {
return m_scalingFactor; }
/**
* Set the scaling factor of the ciphertext.
*/
void SetScalingFactor(double sf) {
m_scalingFactor = sf; }
/**
* Get the Metadata map of the ciphertext.
*/
MetadataMap GetMetadataMap() const {
return this->m_metadataMap; }
/**
* Set the Metadata map of the ciphertext.
*/
void SetMetadataMap(MetadataMap mdata) {
this->m_metadataMap = mdata; }
/**
* This method searches the metadata map for metadata of a specific key.
*
* @param key the string value which serves as a key in the metadata map
* @return an iterator pointing at the position in the map where the key
* was found (or the map.end() if not found).
*/
std::map<string, shared_ptr<Metadata>>::iterator FindMetadataByKey(
string key) const {
return m_metadataMap->find(key);
}
/**
* This method checks whether an iterator return from FindMetadataByKey
* corresponds to whether the key was found or not.
*
* @param it iterator pointing at the position in the map where the key
* was found (or the map.end() if not found).
* @return a boolean value indicating whether the key was found or not.
*/
bool MetadataFound(
std::map<string, shared_ptr<Metadata>>::iterator it) const {
return (it != m_metadataMap->end());
}
/**
* This method returns the Metadata object stored in the iterator position
* returned by FindMetadataByKey.
*
* @param it iterator pointing at the position in the map where the key
* was found (or the map.end() if not found).
* @return a shared pointer pointing to the Metadata object in the map.
*/
shared_ptr<Metadata>& GetMetadata(
std::map<string, shared_ptr<Metadata>>::iterator it) const {
return it->second;
}
/**
* Get a Metadata element from the Metadata map of the ciphertext.
*/
shared_ptr<Metadata> GetMetadataByKey(string key) const {
auto it = m_metadataMap->find(key);
return std::make_shared<Metadata>(*(it->second));
}
/**
* Set a Metadata element in the Metadata map of the ciphertext.
*/
void SetMetadataByKey(string key, shared_ptr<Metadata> value) {
(*m_metadataMap)[key] = value;
}
virtual Ciphertext<Element> Clone() const {
Ciphertext<Element> cRes = this->CloneEmpty();
cRes->SetElements(this->GetElements());
cRes->SetDepth(this->GetDepth());
cRes->SetLevel(this->GetLevel());
cRes->SetScalingFactor(this->GetScalingFactor());
return cRes;
}
bool operator==(const CiphertextImpl<Element>& rhs) const {
if (!CryptoObject<Element>::operator==(rhs)) return false;
if (this->m_depth != rhs.m_depth) return false;
if (this->m_level != rhs.m_level) return false;
if (this->m_scalingFactor != rhs.m_scalingFactor) return false;
const std::vector<Element>& lhsE = this->GetElements();
const std::vector<Element>& rhsE = rhs.GetElements();
if (lhsE.size() != rhsE.size()) return false;
for (size_t i = 0; i < lhsE.size(); i++) {
const Element& lE = lhsE[i];
const Element& rE = rhsE[i];
if (lE != rE) return false;
}
const shared_ptr<std::map<string, shared_ptr<Metadata>>> lhsMap =
this->m_metadataMap;
const shared_ptr<std::map<string, shared_ptr<Metadata>>> rhsMap =
rhs.m_metadataMap;
if (lhsMap->size() != rhsMap->size()) return false;
if (lhsMap->size() > 0) {
for (auto i = lhsMap->begin(), j = rhsMap->begin(); i != lhsMap->end();
++i, ++j)
if (!(*(i->second) == *(j->second))) return false;
}
return true;
}
bool operator!=(const CiphertextImpl<Element>& rhs) const {
return !(*this == rhs);
}
friend std::ostream& operator<<(std::ostream& out,
const CiphertextImpl<Element>& c) {
out << "enc=" << c.encodingType << " depth=" << c.m_depth << std::endl;
out << "metadata: [ ";
for (auto i = c.m_metadataMap->begin(); i != c.m_metadataMap->end(); ++i)
out << "(\"" << i->first << "\", " << *(i->second) << ") ";
out << "]" << std::endl;
for (size_t i = 0; i < c.m_elements.size(); i++) {
if (i != 0) out << std::endl;
out << "Element " << i << ": " << c.m_elements[i];
}
return out;
}
friend std::ostream& operator<<(std::ostream& out, Ciphertext<Element> c) {
return out << *c;
}
template <class Archive>
void save(Archive& ar, std::uint32_t const version) const {
ar(cereal::base_class<CryptoObject<Element>>(this));
ar(cereal::make_nvp("v", m_elements));
ar(cereal::make_nvp("d", m_depth));
ar(cereal::make_nvp("l", m_level));
ar(cereal::make_nvp("s", m_scalingFactor));
ar(cereal::make_nvp("e", encodingType));
ar(cereal::make_nvp("m", m_metadataMap));
}
template <class Archive>
void load(Archive& ar, std::uint32_t const version) {
if (version > SerializedVersion()) {
PALISADE_THROW(deserialize_error,
"serialized object version " + std::to_string(version) +
" is from a later version of the library");
}
ar(cereal::base_class<CryptoObject<Element>>(this));
ar(cereal::make_nvp("v", m_elements));
ar(cereal::make_nvp("d", m_depth));
ar(cereal::make_nvp("l", m_level));
ar(cereal::make_nvp("s", m_scalingFactor));
ar(cereal::make_nvp("e", encodingType));
ar(cereal::make_nvp("m", m_metadataMap));
}
std::string SerializedObjectName() const {
return "Ciphertext"; }
static uint32_t SerializedVersion() {
return 1; }
private:
// FUTURE ENHANCEMENT: current value of error norm
// BigInteger m_norm;
std::vector<Element>
m_elements; /*!< vector of ring elements for this Ciphertext */
uint32_t m_depth; // holds the multiplicative depth of the ciphertext.
PlaintextEncodings encodingType; /*!< how was this Ciphertext encoded? */
double m_scalingFactor;
uint32_t m_level; // holds the number of rescalings performed before getting
// this ciphertext - initially 0
// A map to hold different Metadata objects - used for flexible extensions of
// Ciphertext
MetadataMap m_metadataMap;
};
// TODO the op= are not doing the work in-place, and should be updated
/**
* operator+ overload for Ciphertexts. Performs EvalAdd.
*
* @tparam Element a ring element.
* @param &a ciphertext operand
* @param &b ciphertext operand
*
* @return The result of addition.
*/
template <class Element>
Ciphertext<Element> operator+(const Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a->GetCryptoContext()->EvalAdd(a, b);
}
/**
* operator+= overload for Ciphertexts. Performs EvalAdd.
*
* @tparam Element a ring element.
* @param &a ciphertext to be added to
* @param &b ciphertext to add to &a
*
* @return &a
*/
template <class Element>
const Ciphertext<Element>& operator+=(Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a = a->GetCryptoContext()->EvalAdd(a, b);
}
/**
* Unary negation operator.
*
* @param &a ciphertext operand
* @return the result of the negation.
*/
template <class Element>
Ciphertext<Element> operator-(const Ciphertext<Element>& a) {
return a->GetCryptoContext()->EvalNegate(a);
}
/**
* operator- overload. Performs EvalSub.
*
* @tparam Element a ring element.
* @param &a ciphertext operand
* @param &b ciphertext operand
*
* @return The result of subtraction.
*/
template <class Element>
Ciphertext<Element> operator-(const Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a->GetCryptoContext()->EvalSub(a, b);
}
/**
* operator-= overload for Ciphertexts. Performs EvalAdd.
*
* @tparam Element a ring element.
* @param &a ciphertext to be subtracted from
* @param &b ciphertext to subtract from &a
*
* @return &a
*/
template <class Element>
const Ciphertext<Element>& operator-=(Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a = a->GetCryptoContext()->EvalSub(a, b);
}
/**
* operator* overload. Performs EvalMult.
*
* @tparam Element a ring element.
* @param &a ciphertext operand
* @param &b ciphertext operand
*
* @return The result of multiplication.
*/
template <class Element>
Ciphertext<Element> operator*(const Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a->GetCryptoContext()->EvalMult(a, b);
}
/**
* operator*= overload for Ciphertexts. Performs EvalMult.
*
* @tparam Element a ring element.
* @param &a ciphertext to be multiplied
* @param &b ciphertext to multiply by &a
*
* @return &a
*/
template <class Element>
const Ciphertext<Element>& operator*=(Ciphertext<Element>& a,
const Ciphertext<Element>& b) {
return a = a->GetCryptoContext()->EvalMult(a, b);
}
} // namespace lbcrypto
\pke\ciphertext-ser.h
serialize ciphertexts; include this in any app that needs to serialize them
#ifndef LBCRYPTO_CRYPTO_CIPHERTEXTSER_H
#define LBCRYPTO_CRYPTO_CIPHERTEXTSER_H
#include "palisade.h"
#include "utils/serial.h"
#include "metadata-ser.h"
extern template class lbcrypto::CiphertextImpl<lbcrypto::Poly>;
extern template class lbcrypto::CiphertextImpl<lbcrypto::NativePoly>;
extern template class lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>;
CEREAL_CLASS_VERSION(
lbcrypto::CiphertextImpl<lbcrypto::Poly>,
lbcrypto::CiphertextImpl<lbcrypto::Poly>::SerializedVersion());
CEREAL_CLASS_VERSION(
lbcrypto::CiphertextImpl<lbcrypto::NativePoly>,
lbcrypto::CiphertextImpl<lbcrypto::NativePoly>::SerializedVersion());
CEREAL_CLASS_VERSION(
lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>,
lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>::SerializedVersion());
#endif
\pke\pubkeylp-ser.h
#ifndef LBCRYPTO_CRYPTO_PUBKEYLPSER_H
#define LBCRYPTO_CRYPTO_PUBKEYLPSER_H
#include "palisade.h"
#include "utils/serial.h"
extern template class lbcrypto::LPCryptoParameters<lbcrypto::Poly>;
extern template class lbcrypto::LPCryptoParameters<lbcrypto::NativePoly>;
extern template class lbcrypto::LPCryptoParametersRLWE<lbcrypto::Poly>;
extern template class lbcrypto::LPCryptoParametersRLWE<lbcrypto::NativePoly>;
extern template class lbcrypto::LPPublicKeyEncryptionScheme<lbcrypto::Poly>;
extern template class lbcrypto::LPPublicKeyEncryptionScheme<
lbcrypto::NativePoly>;
extern template class lbcrypto::LPEvalKeyImpl<lbcrypto::Poly>;
extern template class lbcrypto::LPEvalKeyImpl<lbcrypto::NativePoly>;
extern template class lbcrypto::LPEvalKeyRelinImpl<lbcrypto::Poly>;
extern template class lbcrypto::LPEvalKeyRelinImpl<lbcrypto::NativePoly>;
extern template class lbcrypto::LPCryptoParameters<lbcrypto::DCRTPoly>;
extern template class lbcrypto::LPCryptoParametersRLWE<lbcrypto::DCRTPoly>;
extern template class lbcrypto::LPPublicKeyEncryptionScheme<lbcrypto::DCRTPoly>;
extern template class lbcrypto::LPEvalKeyImpl<lbcrypto::DCRTPoly>;
extern template class lbcrypto::LPEvalKeyRelinImpl<lbcrypto::DCRTPoly>;
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParameters<lbcrypto::Poly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParameters<lbcrypto::NativePoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParametersRLWE<lbcrypto::Poly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParametersRLWE<lbcrypto::NativePoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPPublicKeyEncryptionScheme<lbcrypto::Poly>);
CEREAL_REGISTER_TYPE(
lbcrypto::LPPublicKeyEncryptionScheme<lbcrypto::NativePoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyImpl<lbcrypto::Poly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyImpl<lbcrypto::NativePoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyRelinImpl<lbcrypto::Poly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyRelinImpl<lbcrypto::NativePoly>);
CEREAL_REGISTER_POLYMORPHIC_RELATION(
lbcrypto::LPEvalKeyImpl<lbcrypto::Poly>,
lbcrypto::LPEvalKeyRelinImpl<lbcrypto::Poly>);
CEREAL_REGISTER_POLYMORPHIC_RELATION(
lbcrypto::LPEvalKeyImpl<lbcrypto::NativePoly>,
lbcrypto::LPEvalKeyRelinImpl<lbcrypto::NativePoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParameters<lbcrypto::DCRTPoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPCryptoParametersRLWE<lbcrypto::DCRTPoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPPublicKeyEncryptionScheme<lbcrypto::DCRTPoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyImpl<lbcrypto::DCRTPoly>);
CEREAL_REGISTER_TYPE(lbcrypto::LPEvalKeyRelinImpl<lbcrypto::DCRTPoly>);
CEREAL_REGISTER_POLYMORPHIC_RELATION(
lbcrypto::LPEvalKeyImpl<lbcrypto::DCRTPoly>,
lbcrypto::LPEvalKeyRelinImpl<lbcrypto::DCRTPoly>);
#endif
\pke\pubkeylp.h
很重要的头文件,四千多行
#include "lattice/elemparams.h"
#include "lattice/ilparams.h"
#include "lattice/ildcrtparams.h"
#include "lattice/ilelement.h"
#include "utils/caller_info.h"
#include "utils/hashutil.h"
#include "utils/inttypes.h"
#include "math/distrgen.h"
#include "encoding/encodingparams.h"
/**
* @namespace lbcrypto
* The namespace of lbcrypto
*/
namespace lbcrypto {
两个枚举
/* This struct holds the different options for
* key switching algorithms that are supported
* by the library.
*
*/
enum KeySwitchTechnique {
BV, GHS, HYBRID };
/* This struct holds the different options for
* mod switching algorithms that are supported
* by the library.
*
*/
enum ModSwitchMethod {
MANUAL, AUTO };
// forward declarations, used to resolve circular header dependencies
template <typename Element>
class CiphertextImpl;
template <typename Element>
class LPCryptoParameters;
template <typename Element>
class LPCryptoParametersBFV;
template <typename Element>
class CryptoObject;
EncryptionResult 结构体
struct EncryptResult {
EncryptResult() : isValid(false), numBytesEncrypted(0) {
}
explicit EncryptResult(size_t len) : isValid(true), numBytesEncrypted(len) {
}
bool isValid; // whether the encryption was successful
// count of the number of plaintext bytes that were encrypted
usint numBytesEncrypted;
};
DecryptionResult 结构体
/**
* @brief Decryption result. This represents whether the decryption of a
* cipheretext was performed correctly.
*
* This is intended to eventually incorporate information about the amount of
* padding in a decoded ciphertext, to ensure that the correct amount of
* padding is stripped away. It is intended to provided a very simple kind of
* checksum eventually. This notion of a decoding output is inherited from the
* crypto++ library. It is also intended to be used in a recover and restart
* robust functionality if not all ciphertext is recieved over a lossy
* channel, so that if all information is eventually recieved,
* decoding/decryption can be performed eventually. This is intended to be
* returned with the output of a decryption operation.
*/
struct DecryptResult {
/**
* Constructor that initializes all message lengths to 0.
*/
DecryptResult() : isValid(false), messageLength(0) {
}
/**
* Constructor that initializes all message lengths.
* @param len the new length.
*/
explicit DecryptResult(size_t len) : isValid(true), messageLength(len) {
}
bool isValid; /**< whether the decryption was successful */
usint messageLength; /**< the length of the decrypted plaintext message */
};
LPKey 类
继承自 CryptoObject
/**
* @brief Abstract interface class for LP Keys
*
* @tparam Element a ring element.
*/
template <class Element>
class LPKey : public CryptoObject<Element>, public Serializable {
public:
explicit LPKey(CryptoContext<Element> cc, const string &id = "")
: CryptoObject<Element>(cc, id) {
}
explicit LPKey(shared_ptr<CryptoObject<Element>> co)
: CryptoObject<Element>(co) {
}
virtual ~LPKey() {
}
template <class Archive>
void save(Archive &ar, std::uint32_t const version) const {
ar(::cereal::base_class<CryptoObject<Element>>(this));
}
template <class Archive>
void load(Archive &ar, std::uint32_t const version) {
ar(::cereal::base_class<CryptoObject<Element>>(this));
}
};
LPPublicKeyImpl 类
公钥
template <typename Element>
class LPPublicKeyImpl;
template <typename Element>
using LPPublicKey = shared_ptr<LPPublicKeyImpl<Element>>;
继承自 LPKey 类
private:
std::vector<Element> m_h;
/**
* @brief Class for LP public keys
* @tparam Element a ring element.
*/
template <typename Element>
class LPPublicKeyImpl : public LPKey<Element> {
public:
/**
* Basic constructor
*
* @param cc - CryptoContext
* @param id - key identifier
*/
explicit LPPublicKeyImpl(CryptoContext<Element> cc = 0, const string &id = "")
: LPKey<Element>(cc, id) {
}
/**
* Copy constructor
*
*@param &rhs LPPublicKeyImpl to copy from
*/
explicit LPPublicKeyImpl(const LPPublicKeyImpl<Element> &rhs)
: LPKey<Element>(rhs.GetCryptoContext(), rhs.GetKeyTag()) {
m_h = rhs.m_h;
}
/**
* Move constructor
*
*@param &rhs LPPublicKeyImpl to move from
*/
explicit LPPublicKeyImpl(LPPublicKeyImpl<Element> &&rhs)
: LPKey<Element>(rhs.GetCryptoContext(), rhs.GetKeyTag()) {
m_h = std::move(rhs.m_h);
}
operator bool() const {
return static_cast<bool>(this->context) && m_h.size() != 0;
}
/**
* Assignment Operator.
*
* @param &rhs LPPublicKeyImpl to copy from
*/
const LPPublicKeyImpl<Element> &operator=(
const LPPublicKeyImpl<Element> &rhs) {
CryptoObject<Element>::operator=(rhs);
this->m_h = rhs.m_h;
return *this;
}
/**
* Move Assignment Operator.
*
* @param &rhs LPPublicKeyImpl to copy from
*/
const LPPublicKeyImpl<Element> &operator=(LPPublicKeyImpl<Element> &&rhs) {
CryptoObject<Element>::operator=(rhs);
m_h = std::move(rhs.m_h);
return *this;
}
// @Get Properties
/**
* Gets the computed public key
* @return the public key element.
*/
const std::vector<Element> &GetPublicElements() const {
return this->m_h; }
// @Set Properties
/**
* Sets the public key vector of Element.
* @param &element is the public key Element vector to be copied.
*/
void SetPublicElements(const std::vector<Element> &element) {
m_h = element; }
/**
* Sets the public key vector of Element.
* @param &&element is the public key Element vector to be moved.
*/
void SetPublicElements(std::vector<Element> &&element) {
m_h = std::move(element);
}
/**
* Sets the public key Element at index idx.
* @param &element is the public key Element to be copied.
*/
void SetPublicElementAtIndex(usint idx, const Element &element) {
m_h.insert(m_h.begin() + idx, element);
}
/**
* Sets the public key Element at index idx.
* @param &&element is the public key Element to be moved.
*/
void SetPublicElementAtIndex(usint idx, Element &&element) {
m_h.insert(m_h.begin() + idx, std::move(element));
}
bool operator==(const LPPublicKeyImpl &other) const {
if (!CryptoObject<Element>::operator==(other)) {
return false;
}
if (m_h.size() != other.m_h.size()) {
return false;
}
for (size_t i = 0; i < m_h.size(); i++) {
if (m_h[i] != other.m_h[i]) {
return false;
}
}
return true;
}
bool operator!=(const LPPublicKeyImpl &other) const {
return !(*this == other);
}
template <class Archive>
void save(Archive &ar, std::uint32_t const version) const {
ar(::cereal::base_class<LPKey<Element>>(this));
ar(::cereal::make_nvp("h", m_h));
}
template <class Archive>
void load(Archive &ar, std::uint32_t const version) {
if (version > SerializedVersion()) {
PALISADE_THROW(deserialize_error,
"serialized object version " + std::to_string(version) +
" is from a later version of the library");
}
ar(::cereal::base_class<LPKey<Element>>(this));
ar(::cereal::make_nvp("h", m_h));
}
std::string SerializedObjectName() const {
return "PublicKey"; }
static uint32_t SerializedVersion() {
return 1; }
private:
std::vector<Element> m_h;
};
LPEvalKeyImpl 类
也是个抽象类
template <typename Element>
class LPEvalKeyImpl;
template <typename Element>
using LPEvalKey = shared_ptr<LPEvalKeyImpl<Element>>;
/**
* @brief Abstract interface for LP evaluation/proxy keys
* @tparam Element a ring element.
*/
template <class Element>
class LPEvalKeyImpl : public LPKey<Element> {
public:
/**
* Basic constructor for setting crypto params
*
* @param &cryptoParams is the reference to cryptoParams
*/
explicit LPEvalKeyImpl(CryptoContext<Element> cc = 0) : LPKey<Element>(cc) {
}
virtual ~LPEvalKeyImpl() {
}
/**
* Setter function to store Relinearization Element Vector A.
* Throws exception, to be overridden by derived class.
*
* @param &a is the Element vector to be copied.
*/
virtual void SetAVector(const std::vector<Element> &a) {
PALISADE_THROW(not_implemented_error,
"SetAVector copy operation not supported");
}
/**
* Setter function to store Relinearization Element Vector A.
* Throws exception, to be overridden by derived class.
*
* @param &&a is the Element vector to be moved.
*/
virtual void SetAVector(std::vector<Element> &&a) {
PALISADE_THROW(not_implemented_error,
"SetAVector move operation not supported");
}
/**
* Getter function to access Relinearization Element Vector A.
* Throws exception, to be overridden by derived class.
*
* @return Element vector A.
*/
virtual const std::vector<Element> &GetAVector() const {
PALISADE_THROW(not_implemented_error, "GetAVector operation not supported");
}
/**
* Setter function to store Relinearization Element Vector B.
* Throws exception, to be overridden by derived class.
*
* @param &b is the Element vector to be copied.
*/
virtual void SetBVector(const std::vector<Element> &b) {
PALISADE_THROW(not_implemented_error,
"SetBVector copy operation not supported");
}
/**
* Setter function to store Relinearization Element Vector B.
* Throws exception, to be overridden by derived class.
*
* @param &&b is the Element vector to be moved.
*/
virtual void SetBVector(std::vector<Element> &&b) {
PALISADE_THROW(not_implemented_error,
"SetBVector move operation not supported");
}
/**
* Getter function to access Relinearization Element Vector B.
* Throws exception, to be overridden by derived class.
*
* @return Element vector B.
*/
virtual const std::vector<Element> &GetBVector() const {
PALISADE_THROW(not_implemented_error, "GetBVector operation not supported");
}
/**
* Setter function to store key switch Element.
* Throws exception, to be overridden by derived class.
*
* @param &a is the Element to be copied.
*/
virtual void SetA(const Element &a) {
PALISADE_THROW(not_implemented_error, "SetA copy operation not supported");
}
/**
* Setter function to store key switch Element.
* Throws exception, to be overridden by derived class.
*
* @param &&a is the Element to be moved.
*/
virtual void SetA(Element &&a) {
PALISADE_THROW(not_implemented_error, "SetA move operation not supported");
}
/**
* Getter function to access key switch Element.
* Throws exception, to be overridden by derived class.
*
* @return Element.
*/
virtual const Element &GetA() const