PIsberg/blindbean

GitHub: PIsberg/blindbean

一个基于 Java 26 的开发者优先同态加密库,通过注解机制屏蔽密码学复杂度,让开发者像操作普通 Java 对象一样对加密数据执行算术运算。

Stars: 0 | Forks: 1

# BlindBean FHE 库 ![Java 26](https://img.shields.io/badge/Java-26-orange.svg?logo=java) ![Project Panama FFM](https://img.shields.io/badge/Project_Panama-FFM-blue.svg) ![Cryptography](https://img.shields.io/badge/Cryptography-FHE-red.svg) ![Microsoft SEAL](https://img.shields.io/badge/Microsoft_SEAL-4.1-0078D4.svg) [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/PIsberg/blindbean/badge)](https://securityscorecards.dev/viewer/?uri=github.com/PIsberg/blindbean) [![codecov](https://codecov.io/gh/PIsberg/blindbean/graph/badge.svg?token=Y6W85Z8B6B)](https://codecov.io/gh/PIsberg/blindbean) ![License](https://img.shields.io/badge/License-PolyForm_Commercial-blue.svg) BlindBean 是一个开发者优先的 Java 26 库,使同态加密 (HE) 对终端用户完全透明。它允许您使用标准 Java 对象对加密数据执行安全的私密算术运算,将复杂的密码学完全隐藏在注解背后。 ![blind-bean-inforgraphics-v1](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/1c4a7da8b0164237.jpg) ## 愿景 如果感觉像是在做数学,那我们就失败了。它应该感觉像 Hibernate。您只需添加注解,我们来负责计算。 ## 功能 - **纯 Java Paillier**:利用 Java 26 Vector API (Project Panama SIMD) 实现并行化的部分同态加密。 - **原生 FHE 桥接**:通过 Microsoft SEAL 4.1 支持 **BFV** (精确整数) 和 **CKKS** (近似实数) 方案,通过 Project Panama FFM 进行桥接 —— 零 JNI。 - **开发者优先的注解**:只需在您的领域实体上添加 `@Homomorphic` 注解即可。 - **AutoCloseable 资源**:`FheContext` 和 `FheCiphertextNative` 支持 try-with-resources,以实现确定性的原生内存清理。 ## 了解密码学 如果您想了解 BlindBean 背后的密码学原理,请查看以下可靠资源: - [什么是同态加密?(IBM)](https://www.ibm.com/topics/homomorphic-encryption) - [Microsoft SEAL 仓库](https://github.com/microsoft/SEAL) - [Paillier 密码系统 (维基百科)](https://en.wikipedia.org/wiki/Paillier_cryptosystem) - [HomomorphicEncryption.org 标准](https://homomorphicencryption.org/standard/) ## 前置条件 | 需求 | 版本 | |:------------|:--------| | JDK | 26-ea (需使用 `--enable-preview`) | | CMake | 3.16+ | | vcpkg | 最新版 | | C++ 编译器 | MSVC 2019+ / GCC 11+ / Clang 14+ (C++17) | ## 构建 ``` # 构建原生 SEAL 支持的 DLL cmake -S src/main/native -B build-native \ -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake cmake --build build-native --config Release # 构建并安装 Java 库 ./mvnw clean install -B -Dblindbean.native.path=build-native # 运行测试 ./mvnw clean test -Dblindbean.native.path=build-native ``` 在 Windows 上,请使用 `mvnw.cmd` 和 `-Dblindbean.native.path=build-native/Release`。 ## CI GitHub Actions 运行以下内容: - 在 Linux 和 macOS 上针对注解处理器和核心回归的快速纯 Java 验证门禁 - 在 Linux、macOS 和 Windows 上的原生构建矩阵,将构建的共享库发布为 artifact - 在 Windows 上针对发布的 `blindbean_fhe.dll` 运行完整的 Maven 测试套件 ## 快速入门 在 5 行代码内为加密余额加 500: ``` // 1. Initialize Context BlindContext.init(); // 2. Fetch or Create Entity UserAccount user = repository.findById(1); // User whose balance is entirely encrypted! // 3. Transparently Wrap using the Auto-Generated Helper UserAccountBlindWrapper wrapper = new UserAccountBlindWrapper(user); // 4. Add the encrypted amount Ciphertext amountToAdd = BlindContext.getPaillier().encrypt(BigInteger.valueOf(500)); wrapper.addBalance(amountToAdd); // Math happens right there, without decryption! ``` ### 存储任意类型 您可以原生且安全地存储标准 Java 类型,例如 `String`、`boolean` 或任何数字类型 (`byte`、`short`、`int`、`long`、`float`、`double`): ``` @Homomorphic(scheme = Scheme.PAILLIER, type = int.class) private String age; @Homomorphic(scheme = Scheme.BFV, type = long.class) private String balance; @Homomorphic(scheme = Scheme.CKKS, type = double.class) private String precisionValue; ``` *注意:对于文本和逻辑结构 (String/boolean),同态数学函数(加法/乘法)在结构上被省略,以防止数学数据损坏。* ### 向量批处理 (SIMD 数组) 在使用 BFV 方案时,您可以使用 `long[].class` 参数原生批处理包含数千个变量的同构完整数组。 ``` @Homomorphic(scheme = Scheme.BFV, type = long[].class) private String batchedMetrics; ``` 数学运算(例如 `wrapper.addBatchedMetrics(...)`)会自动高效地并发传播到底层 C++ 层的所有坐标,而不会增加哪怕一毫秒的开销。注意:最大批处理容量受限于方案原生设定的多项式次数大小限制 (`8,192`)。 ### 使用 BFV (全同态 —— 整数) ``` try (var ctx = FheContext.bfv(8192)) { MemorySegment encrypted = ctx.encryptLong(42L); MemorySegment doubled = ctx.add(encrypted, encrypted); long result = ctx.decryptLong(doubled); System.out.println("42 + 42 = " + result); // 84, exact } ``` ### 使用 CKKS (全同态 —— 实数) ``` try (var ctx = FheContext.ckks(8192, Math.pow(2, 40))) { MemorySegment encrypted = ctx.encryptDouble(3.14); MemorySegment doubled = ctx.add(encrypted, encrypted); double result = ctx.decryptDouble(doubled); System.out.println("3.14 + 3.14 ≈ " + result); // ≈ 6.28 } ``` ## 运行基准测试 我们使用 JMH 对照标准 `long` 加法测试我们的吞吐量。 ``` ./mvnw clean verify java -jar target/benchmarks.jar ``` *注意:需要使用启用了 `--enable-preview` 和 `--add-modules jdk.incubator.vector` 的 JDK 26。*
标签:Bash脚本, BFV方案, CKKS方案, FFM, FHE, Java 26, Java库, Microsoft SEAL, Paillier算法, Project Panama, SIMD, Vector API, 全同态加密, 同态加密, 域名枚举, 安全多方计算, 密码学, 开发者优先, 手动系统调用, 机密计算, 注解驱动, 纯Java, 自动资源管理, 隐私保护算术, 隐私计算, 零JNI