serengil/LightPHE

GitHub: serengil/LightPHE

一个轻量级的 Python 部分同态加密库,支持多种经典密码学算法,用于在不泄露明文的前提下对密文进行高效的数学运算。

Stars: 102 | Forks: 12

# LightPHE
[![PyPI 下载量](https://static.pepy.tech/personalized-badge/lightphe?period=total&units=international_system&left_color=grey&right_color=blue&left_text=downloads)](https://pepy.tech/project/lightphe) [![Stars](https://img.shields.io/github/stars/serengil/LightPHE?color=yellow&style=flat&label=%E2%AD%90%20stars)](https://github.com/serengil/LightPHE/stargazers) [![许可证](http://img.shields.io/:license-MIT-green.svg?style=flat)](https://github.com/serengil/LightPHE/blob/master/LICENSE) [![DOI](http://img.shields.io/:DOI-10.3390/sym18050832-blue.svg?style=flat)](https://www.mdpi.com/2073-8994/18/5/832)

LightPHE 是一个轻量级的 Python 同态加密库,支持多种部分同态和些许同态加密方案,例如 [`RSA`](https://sefiks.com/2023/03/06/a-step-by-step-partially-homomorphic-encryption-example-with-rsa-in-python/)、[`ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/)、[`Exponential ElGamal`](https://sefiks.com/2023/03/27/a-step-by-step-partially-homomorphic-encryption-example-with-elgamal-in-python/)、[`Elliptic Curve ElGamal`](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/)([`Weierstrass`](https://sefiks.com/2016/03/13/the-math-behind-elliptic-curve-cryptography/)、[`Koblitz`](sefiks.com/2016/03/13/the-math-behind-elliptic-curves-over-binary-field/) 和 [`Edwards`](https://sefiks.com/2018/12/19/a-gentle-introduction-to-edwards-curves/) 形式)、[`Paillier`](https://sefiks.com/2023/04/03/a-step-by-step-partially-homomorphic-encryption-example-with-paillier-in-python/)、[`Damgard-Jurik`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-damgard-jurik-in-python/)、[`Okamoto–Uchiyama`](https://sefiks.com/2023/10/20/a-step-by-step-partially-homomorphic-encryption-example-with-okamoto-uchiyama-in-python/)、[`Benaloh`](https://sefiks.com/2023/10/06/a-step-by-step-partially-homomorphic-encryption-example-with-benaloh-in-python-from-scratch/)、[`Naccache–Stern`](https://sefiks.com/2023/10/26/a-step-by-step-partially-homomorphic-encryption-example-with-naccache-stern-in-python/)、[`Goldwasser–Micali`](https://sefiks.com/2023/10/27/a-step-by-step-partially-homomorphic-encryption-example-with-goldwasser-micali-in-python/)、[`Sander-Young-Yung`](https://sefiks.com/2026/04/02/a-step-by-step-partially-homomorphic-sander-young-yung-example-in-python/)、[`Boneh-Goh-Nissim`](https://sefiks.com/2026/04/02/a-step-by-step-somewhat-homomorphic-encryption-example-with-boneh-goh-nissim-in-python/)。 # 部分同态 vs 全同态加密 尽管近年来全同态加密 (FHE) 已经变得可用,但综合权衡之下,LightPHE 仍是更高效、更实用的选择。如果您的具体任务并不需要完全同态的能力,那么选择 LightPHE 进行部分同态加密是明智的决定。 - 🏎️ 显著更快的速度 - 💻 需要更少的计算资源 - 📏 生成小得多的密文 - 🔑 分发小得多的密钥 - 🧠 非常适合内存受限的环境 - ⚖️ 为实际用例取得了理想的平衡 # 安装 [![PyPI](https://img.shields.io/pypi/v/lightphe.svg)](https://pypi.org/project/lightphe/) 安装 LightPHE 包最简单的方法是从 Python 包索引 安装它。 ``` pip install lightphe ``` 然后您就可以导入该库并使用其功能了。 ``` from lightphe import LightPHE ``` # LightPHE 中不同密码系统的同态特性总结 总而言之,LightPHE 涵盖了以下算法,并且根据下表中提到的操作,它们分别是部分同态和些许同态的。 | 算法 | 乘法
同态 | 加法
同态 | 标量乘法 | 按位异或同态 | 按位与同态 | 密文
重新生成 | | --- | --- | --- | --- | --- | --- | --- | | RSA | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | | ElGamal | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | | Exponential ElGamal | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Elliptic Curve ElGamal | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Paillier | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Damgard-Jurik | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Benaloh | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Naccache-Stern | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Okamoto-Uchiyama | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | | Goldwasser-Micali | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | | Sander-Young-Yung | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | | Boneh-Goh-Nissim | 1️⃣ | ✅ | ✅ | ❌ | ❌ | ✅ | # 构建密码系统 一旦您导入了该库,就可以使用多种算法构建密码系统。这主要会生成私钥和公钥对。 ``` algorithms = [ "RSA", "ElGamal", "Exponential-ElGamal", "Paillier", "Damgard-Jurik", "Okamoto-Uchiyama", "Benaloh", "Naccache-Stern", "Goldwasser-Micali", "EllipticCurve-ElGamal", "Sander-Young-Yung", "Boneh-Goh-Nissim", ] cs = LightPHE(algorithm_name = algorithms[0]) ``` # 同态操作 以下示例演示了使用 LightPHE 和加法同态密码系统(例如 Paillier)的简单工作流程。首先,我们在本地构建密码系统并定义两个明文值。接下来,我们对明文进行加密,这可以在本地或云端完成,且不需要私钥。然后,可以对密文执行同态操作,例如加法和标量乘法——这些操作可以卸载到云端,利用其计算能力而无需泄露私钥或明文。最后,使用私钥在本地解密结果,验证对加密数据进行的同态操作是否产生了预期的明文结果。 ``` # 构建加法同态密码系统 cs = LightPHE(algorithm_name = "Paillier") # 定义明文 m1 = 10000 # base salary in usd m2 = 500 # wage increase in usd # 加密明文 - 不需要私钥。 c1 = cs.encrypt(m1) c2 = cs.encrypt(m2) # 同态加法 - 不需要私钥 c3 = c1 + c2 # 同态标量乘法 - 不需要私钥 k = 1.05 # increase something 5% c4 = k * c1 # 解密 - 需要私钥 assert cs.decrypt(c3) == m1 + m2 assert cs.decrypt(c4) == k * m1 ``` 另一方面,如果您采用乘法同态密码系统(例如 RSA 或 ElGamal),则可以在不泄露私钥或明文的情况下对密文进行相乘。 ``` # 构建乘法同态密码系统 cs = LightPHE(algorithm_name = "RSA") # 定义明文 m1 = 17 m2 = 21 # 加密明文 - 不需要私钥。 c1 = cs.encrypt(m1) c2 = cs.encrypt(m2) # 同态乘法 c3 = c1 * c2 # 解密 - 需要私钥 assert cs.decrypt(c3) == m1 * m2 ``` ### 密文重新生成 大多数同态算法允许您重新生成密文,同时不会破坏其明文的恢复。您可以考虑进行多次这种重新生成,以获得安全性更强的密文。 ``` c1_prime = cs.regenerate_ciphertext(c1) assert c1_prime.value != c1.value assert cs.decrypt(c1_prime) == m1 assert cs.decrypt(c1) == m1 ``` ### 椭圆曲线密码学 ECC 是一种强大的公钥密码系统,基于有限域上椭圆曲线的代数结构。该库支持 3 种椭圆曲线形式(weierstrass(默认)、edwards 和 koblitz)以及 100 多种标准椭圆曲线配置。 在 LightPHE 中,实现了 [Elliptic Curve ElGamal](https://sefiks.com/2018/08/21/elliptic-curve-elgamal-encryption/) 方案,提供了一个安全高效的同态加密选项。 ``` forms = ["weierstrass", "edwards", "koblitz"] phe = LightPHE( algorithm_name="EllipticCurve-ElGamal", form="edwards", # curve="ed448", # optinally you can specify the curve for given form ) ``` 决定椭圆曲线密码系统安全级别的关键因素之一是椭圆曲线的阶。曲线的阶是曲线上的点数,它直接影响加密的强度。较高的阶通常对应着更强的密码系统,使其更能抵抗密码攻击。 LightPHE 中的每条曲线都有特定的阶,这些阶经过精心选择以平衡性能和安全性。通过选择具有较大阶的椭圆曲线,您可以增加密码系统的安全性,但这可能会在计算效率上带来权衡。因此,根据您的应用程序的安全性和性能要求选择合适的曲线阶是一个关键的决定。 有关所有受支持的形式、曲线及其详细信息的列表,请参见 [`curves`](https://github.com/serengil/LightECC#supported-curves) 页面。 ### 向量嵌入 LightPHE 支持对向量嵌入进行同态加密。这在隐私保护机器学习、安全聚合和机密数据处理中非常有用。 ``` # 构建加法同态密码系统 (例如 Paillier) cs = LightPHE("Paillier") # 定义 plain embeddings t1 = [1.005, 2.05, 3.6, 4, 4.02, 3.5] t2 = [5, 6.2, 7.5, 8.02, 8.02, 4.5] t3 = [1.03, 2.04, 3.05, 7.02, 2.01, 1.06] # 加密 embeddings c1, c2 = cs.encrypt(t1), cs.encrypt(t2) # 执行两个加密 embeddings 的加法 c4 = c1 + c2 # 对一个 embedding 执行标量乘法 c5 = 3 * c1 # 在加密 embedding 和 plain embedding 之间执行逐元素乘法 c6 = c1 * t3 # 加密点积(同理 cosine similarity) c7 = c1 @ t3 # proof of work assert np.allclose(cs.decrypt(c4), [a + b for a, b in zip(t1, t2)], rtol=1e-2) assert np.allclose(cs.decrypt(c5), [a * 3 for a in t1], rtol=1e-2) assert np.allclose(cs.decrypt(c6), [a * b for a, b in zip(t1, t3)], rtol=1e-2) assert np.allclose(cs.decrypt(c7)[0], sum([a * b for a, b in zip(t1, t3)]), rtol=1e-2) ``` # 引用 如果 LightPHE 对您的研究有帮助,请在您的出版物中引用它。以下是它的 BibTeX 条目: ``` @article{sym18050832, title = {Sustainable Cryptography: Carbon Asymmetry in Partially Homomorphic Encryption in the Cloud}, author = {Ozpinar, Alper and Serengil, Sefik Ilkin}, journal = {Symmetry}, volume = {18}, number = {5}, pages = {832}, url = {https://www.mdpi.com/2073-8994/18/5/832}, doi = {10.3390/sym18050832}, note = {Special Issue: Symmetry in Cryptography and Cybersecurity}, year = {2026} } ``` 此外,如果您在项目中使用了 LightPHE,请将 `lightphe` 添加到 `requirements.txt` 中。 # 许可证 LightPHE 根据 MIT 许可证授权 - 有关更多详细信息,请参见 [`LICENSE`](https://github.com/serengil/LightPHE/blob/master/LICENSE)。
标签:Cryptography, Damgard-Jurik, ElGamal, meg, Okamoto–Uchiyama, Paillier, Python, RSA, 信息安全, 加密算法, 加密计算, 半同态加密, 同态加密, 后量子安全, 多方安全计算, 安全多方计算, 安全算法, 密码学, 手动系统调用, 数据加密, 无后门, 椭圆曲线, 网络安全, 网络安全, 轻量级库, 逆向工具, 部分同态加密, 隐私保护, 隐私保护, 隐私增强技术, 隐私计算