Add YPIR algorithm implementation#380
Conversation
sh1k4ku
commented
Mar 11, 2026
- Add YPIR PIR scheme with SimplePIR and DoublePIR support
- Add AES PRNG, HEXL NTT integration, and utility modules
- Add Bazel build rules for YPIR and hexl dependency
- Update Spiral algorithm (NTT, params, poly matrix, client/server)
- Add integration tests and basic flow tests
- Add YPIR PIR scheme with SimplePIR and DoublePIR support - Add AES PRNG, HEXL NTT integration, and utility modules - Add Bazel build rules for YPIR and hexl dependency - Update Spiral algorithm (NTT, params, poly matrix, client/server) - Add integration tests and basic flow tests
|
All contributors have signed the CLA ✍️ ✅ |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly expands the system's PIR capabilities by adding a YPIR scheme with SimplePIR and DoublePIR support. It incorporates several key improvements, including optimized NTT operations via HEXL, enhanced build rules, and updates to the Spiral algorithm. The addition of integration and flow tests ensures the stability and correctness of these new features. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new PIR scheme, YPIR, with support for both SimplePIR and DoublePIR. It includes the core algorithm implementation, integration with HEXL for NTT, and new utility modules. The changes also update the existing Spiral algorithm, including parameters, NTT implementation, and client/server logic.
My review focuses on several key areas. I've identified critical security vulnerabilities related to the use of fixed seeds for random number generation in core cryptographic functions. These must be addressed by using a secure source of randomness. I also found a potential bug in a utility function that appears to discard computed results, and some maintainability issues with hardcoded parameters. Additionally, there are build-related concerns regarding portability due to a hardcoded dependency path and architecture-specific compiler flags. While the overall implementation of the new algorithm is substantial, these issues should be resolved to ensure the code is secure, correct, and maintainable.
Note: Security Review did not run due to the size of the PR.
| PolyMatrixRaw PolyMatrixRaw::Random(const Params& params, size_t rows, | ||
| size_t cols) { | ||
| yacl::crypto::Prg<uint64_t> prg(yacl::crypto::SecureRandU128()); | ||
| yacl::crypto::Prg<uint64_t> prg(yacl::MakeUint128(0, 20001)); |
There was a problem hiding this comment.
The Random method is implemented using a fixed seed. This means it will produce the same "random" matrix every time, which is a major security risk if this function is used in a production context where unpredictability is required. For testing, a fixed seed is acceptable, but for production code, a source of true randomness like yacl::crypto::SecureRandU128() should be used. The function name is also misleading. If this is for testing, it should be renamed to something like RandomForTest. A similar issue exists for PolyMatrixNtt::Random on line 333.
| yacl::crypto::Prg<uint64_t> prg(yacl::MakeUint128(0, 20001)); | |
| yacl::crypto::Prg<uint64_t> prg(yacl::crypto::SecureRandU128()); |
| uint128_t seed = yacl::MakeUint128(0, 10001); | ||
| yacl::crypto::Prg<uint64_t> rng(seed); | ||
| uint128_t pub_seed = yacl::crypto::SecureRandU128(); | ||
| uint128_t pub_seed = yacl::MakeUint128(0, 10002); | ||
| yacl::crypto::Prg<uint64_t> rng_pub(pub_seed); |
There was a problem hiding this comment.
The GenPublicKeys function uses fixed seeds for its random number generators. This is a critical security vulnerability as it makes the public keys, and any encryption performed with them, deterministic and predictable. Cryptographic keys must be generated with a secure source of randomness. Please use yacl::crypto::SecureRandU128() to seed the PRNGs.
| uint128_t seed = yacl::MakeUint128(0, 10001); | |
| yacl::crypto::Prg<uint64_t> rng(seed); | |
| uint128_t pub_seed = yacl::crypto::SecureRandU128(); | |
| uint128_t pub_seed = yacl::MakeUint128(0, 10002); | |
| yacl::crypto::Prg<uint64_t> rng_pub(pub_seed); | |
| uint128_t seed = yacl::crypto::SecureRandU128(); | |
| yacl::crypto::Prg<uint64_t> rng(seed); | |
| uint128_t pub_seed = yacl::crypto::SecureRandU128(); | |
| yacl::crypto::Prg<uint64_t> rng_pub(pub_seed); |
| yacl::crypto::Prg<uint64_t> rng(yacl::MakeUint128(0, 10003)); | ||
| // a empty query | ||
| SpiralQuery query; | ||
| uint128_t query_seed; | ||
| rng.Fill(absl::MakeSpan(reinterpret_cast<uint8_t*>(&query_seed), | ||
| sizeof(query_seed))); | ||
| uint128_t query_seed = yacl::MakeUint128(0, 10004); | ||
| yacl::crypto::Prg<uint64_t> rng_pub(query_seed); | ||
| query.seed_ = query_seed; |
There was a problem hiding this comment.
The GenQueryInternal function uses fixed seeds for its random number generators. This makes the query generation deterministic, which is a critical security flaw in a cryptographic protocol. An attacker could potentially predict queries. A secure random seed must be used.
| yacl::crypto::Prg<uint64_t> rng(yacl::MakeUint128(0, 10003)); | |
| // a empty query | |
| SpiralQuery query; | |
| uint128_t query_seed; | |
| rng.Fill(absl::MakeSpan(reinterpret_cast<uint8_t*>(&query_seed), | |
| sizeof(query_seed))); | |
| uint128_t query_seed = yacl::MakeUint128(0, 10004); | |
| yacl::crypto::Prg<uint64_t> rng_pub(query_seed); | |
| query.seed_ = query_seed; | |
| yacl::crypto::Prg<uint64_t> rng(yacl::crypto::SecureRandU128()); | |
| // a empty query | |
| SpiralQuery query; | |
| uint128_t query_seed; | |
| rng.Fill(absl::MakeSpan(reinterpret_cast<uint8_t*>(&query_seed), | |
| sizeof(query_seed))); | |
| yacl::crypto::Prg<uint64_t> rng_pub(query_seed); | |
| query.seed_ = query_seed; |
| new_local_repository( | ||
| name = "hexl", | ||
| build_file = "//bazel:hexl.BUILD", | ||
| path = "/usr/local", | ||
| ) |
There was a problem hiding this comment.
The new_local_repository for hexl uses a hardcoded path /usr/local. This makes the build non-portable and dependent on the local environment setup. It will fail on systems where hexl is installed in a different location or not at all. Consider using http_archive to fetch and build hexl from a source repository to ensure a hermetic and portable build. If hexl must be a local dependency, its path should be configurable, for example, via an environment variable or a .bazelrc file.
| for (size_t r = 0; r < in.Rows(); ++r) { | ||
| size_t c = 0; | ||
|
|
||
| auto in_poly = in.Poly(r, c); | ||
| auto scratch_sub = scratch.subspan(0, poly_size); | ||
| std::copy(in_poly.begin(), in_poly.end(), scratch_sub.begin()); | ||
|
|
||
| arith::NttInverse(params, scratch_sub); | ||
|
|
||
| if (r == 0) { | ||
| size_t raw_poly_idx = out.PolyStartIndex(r, c); | ||
| std::vector<uint64_t> temp_vec(scratch_sub.begin(), scratch_sub.end()); | ||
| for (size_t i = 0; i < params.PolyLen(); ++i) { | ||
| out.Data()[raw_poly_idx + i] = params.CrtCompose(temp_vec, i); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
In FromNttScratch, the loop iterates over the rows of the input matrix in, which is enforced to have 2 rows. However, the if (r == 0) block means that the output matrix out is only updated for the first row (r=0). For the second row (r=1), arith::NttInverse is called, but its result is not used to update out. This seems incorrect, as the second row of the result is never computed. The function should likely process and store the results for all rows.
| DEBUG_FLAGS = ["-O0", "-g", "-DSPDLOG_ACTIVE_LEVEL=1"] | ||
| RELEASE_FLAGS = ["-O2"] | ||
| FAST_FLAGS = ["-O1"] | ||
| FAST_FLAGS = ["-O3", "-march=native"] |
There was a problem hiding this comment.
The FAST_FLAGS now include -march=native. While this can improve performance by generating code optimized for the specific CPU of the build machine, it can lead to portability issues. The resulting binary may not run on machines with older CPUs that lack the instruction sets used. If the compiled artifacts are intended to be distributed and run on various machines, it's safer to target a more general architecture (e.g., -march=x86-64-v2) or remove this flag.
|
I have read the CLA Document and I hereby sign the CLA |
|
recheck |