2021SC@SDUSC
PALISADE库结构以及专用对象分析
PALISADE被设计为一个层结构,每一层都提供了一系列的服务给上一层,使用下一层提供的服务。每一层的接口都被设计为API,下面为具体层结构:
-
Application:所有调用了PALISADE lib的程序都在这一层
-
Encoding:所有数据编码方法的实现都在这一层
编码层包含将原始明文(
PlainText
)消息转换成明文对象,以及在必要时解码回原文消息的类。编码层用来创建Plaintext
对象 -
Crypto:所有加密协议的实现都在这一层。
-
Lattice Operations:所有高级的格密码数学构造都在这一层
-
Primitive Math::所有低级通用数学运算,如多精度运算,算术实现,在这一层。
1 元素类型
所有的PALISADE开源库内的操作都是强类型的,要加密的明文会创造一个知道明文格式的密文(Ciphertext
)以及用来加密的特定的密钥,如果使用不正确的密文,解密操作将会失败。一次成功的解密将产生一个新的,其基本格式与传递给的初始明文相匹配的明文
加密。密文之间的同态操作和混合模式操作在密文和明文之间只允许在相同格式之间进行。
从palisade_manual.pdf的7.1节与7.13节中,我们了解到:
7.1 Typeing
All PALISADE operations are strongly typed. A Plaintext that is passed to encrypt will create a Ciphertext that is aware of the underlying format of the Plaintext, as well as the particular key that was used to encrypt the Plaintext. The decrypt operation will fail in cases where an improper key is used. A successful decrypt will produce a new Plaintext whose underlying format matches the initial Plaintext that was passed to encrypt. Homomorphic operations between Ciphertexts, and mixed-mode operations between a Ciphertext and a Plaintext will only be permitted for operands with formats and keys that match.
7.13 Element
The math layer performs operations on different kinds of Element. This is a representation of a vector in lattice space.
7.13.1 Poly
A Poly is a vector of polynomial coefficients. The coefficients are whatever the BigInteger type is for the selected math back-end, and the vector is simply a vector of these BigInteger, and an associated modulus. All operations on a Poly are done modulo this modulus.
7.13.2 NativePoly
A NativePoly is a vector of polynomial coefficients where each of the coefficients is of type 64-bit unsigned integer. The NativePoly also has a modulus of at most 64 bits (though our math implementations are most efficient for moduli of 60 bits or less). Again,all operations are done modulo the modulus.
7.13.3 DCRTPoly
A DCRTPoly implements a large Poly decomposed via residue arithmetic into a tower (set) of NativePoly element
翻译一下就是:所有的PALISADE开源库内的操作都是强类型的,要加密的明文会创造一个知道明文格式的密文(Ciphertext
)以及用来加密的特定的密钥,如果使用不正确的密文,解密操作将会失败。一次成功的解密将产生一个新的,其基本格式与传递给的初始明文相匹配的明文加密。密文之间的同态操作和混合模式操作在密文和明文之间只允许在相同格式之间进行。
PALISADE中有三种元素的类型:
-
Poly:
Poly是多项式系数的向量,系数的取值是后端选择的BigInteger。向量包括一个BigInteger类型的向量以及一个模数(
modulus
),所有在Poly类型上的操作都要模上这个模数。 -
NativePoly
NativePloy与Poly唯一不同的一点就是,多项式的系数的取值是64-bits位的无符号整数。
-
DCRTPoly
DCRTPoly,它将编码的明文表示为使用双中国余数定理的分解的堆栈NativePoly多项式。
2 CryptoContext
PALISADE的核心的类是CryptoContext
类,该类是提供所有 PALISADE 加密功能的类。PALISADE 实现中使用的所有对象均由CryptoContext
类创建。
PALISADE对象的所有操作必须在相同的属于CryptoContext
类的对象上进行,加密明文(Plaintext
)生成密文(Ciphertext
)的过程如下:
我的另一篇博客分析的加密过程就是属于这个流程。每个的加密过程的CryptoContext
都是独特的,由Scheme, Element type, ElementParams EncodingParams
生成。因此如果一个人在两台电脑前创建具有相同的Scheme, Element type, ElementParams EncodingParams
的CryptoContext
,那么这两个CryptoContext
应该是相同的。
3 PlainText
PALISADE中使用明文(PlainText
)表示未加密的内容。它实际上是PALISADE中支持的每种可能的纯文本编码的基类:
• PackedEncoding
• CKKSPackedEncoding
• CoefPackedEncoding
• StringEncoding
明文的用途:
-
创建明文
PlainText
是通过调用适当的CryptoContext
类的方法,方法的参数是未加密的信息 -
一旦创建,明文
PlainText
可以使用CryptoContext
的Encrypt
方法加密为密文Ciphertext
。 -
PlainText
还可以用作多个CryptoContext
同态操作的参数, -
当密文
Ciphertext
被解密时,解密方法创建一个新的Plaintext
来包含解密
明文有几个方法提供对明文中的信息的访问,在/core/include/encoding/plaintext.h中的319行:
virtual const std::string& GetStringValue() const {
PALISADE_THROW(type_error, "not a string");
}
virtual const vector<int64_t>& GetCoefPackedValue() const {
PALISADE_THROW(type_error, "not a packed coefficient vector");
}
virtual const vector<int64_t>& GetPackedValue() const {
PALISADE_THROW(type_error, "not a packed coefficient vector");
}
virtual const std::vector<std::complex<double>>& GetCKKSPackedValue() const {
PALISADE_THROW(type_error, "not a packed vector of complex numbers");
}
virtual const std::vector<double> GetRealPackedValue() const {
PALISADE_THROW(type_error, "not a packed vector of real numbers");
}
virtual void SetStringValue(const std::string&) {
PALISADE_THROW(type_error, "does not support a string");
}
virtual void SetIntVectorValue(const vector<int64_t>&) {
PALISADE_THROW(type_error, "does not support an int vector");
}
明文PlainText
类中对明文的编码类型(PlaintextEncodings
)、明文的元素类型都有定义:
enum PlaintextEncodings {
Unknown = 0,
CoefPacked,
Packed,
String,
CKKSPacked,
};
enum PtxtPolyType { IsPoly, IsDCRTPoly, IsNativePoly };
class PlaintextImpl {
protected:
//是否被编码
bool isEncoded;
//元素类型
PtxtPolyType typeFlag;
//编码参数
EncodingParams encodingParams;
}
4 Ciphertext
Ciphertext
对象在PALISADE中代表的是解密过的信息,Ciphertext
是由PlainText
明文对象通过解密方法创建的,然后再通过解密方法转回PlainText
明文对象,加密encrypt
和解密encrypt
方法需要的Keys
由CryptoContext
生成
如果操作数的编码匹配,则支持密文Ciphertext
对之间或密文Ciphertext
与明文PlainText
之间的同态操作。并且这些编码支持同态操作(例如。同态操作不支持字符串编码,如果尝试将被拒绝)。
利用ABEContext
的CP-ABE/GPV IBE
方案有自己独特的用于密文实现的类,这些类目前不支持同态操作。
5 ElementParams
ElementParams
是一个容器,用于存放正在使用的任何元素的配置参数。结构参数包括环的维数( ring dimension)、切圆次序( cyclotomic order)和单位的基元根。
6 EncodingParams
EncodingParams
是一个容器,用于存放编码明文所需要的所有参数。在大多数情况下,这只包含一个明文模数(plaintext modulus
),然而,有些编码需要更详细的参数。