p²RAG Runs 300x Faster Making Private AI Search Finally Practical
A new privacy-preserving RAG protocol replaces secure sorting with interactive bisection, cutting retrieval time by up to 300x for large k values.
ProTakeaways
- p²RAG replaces secure sorting with interactive bisection for privacy-preserving top-k retrieval, running 3-300x faster than PRAG.
- Two non-colluding servers hold secret-shared documents and prompts; neither can decrypt either side.
- Distributed Comparison Functions let servers count documents below a threshold without seeing the actual distances.
- Protects against malicious users via bounded iterations, capped result sizes, and PIR verification checks.
- Recall stays at 1.0 for k up to 1024 on BEIR trec-covid with Cohere embeddings; overhead vs plaintext RAG is roughly 10x.
- Code is open source on GitHub under Apache 2.0, built on the myl7/fss function secret sharing library.
Retrieval-augmented generation has a privacy problem the moment you outsource it. The RAG service can read the data owner's proprietary corpus and the user's prompt, and neither party may trust it. Existing cryptographic solutions handle this, but they choke when you ask for more than a handful of documents. A team from City University of Hong Kong and Peng Cheng Laboratory has published p²RAG, a protocol that ditches the expensive sorting step entirely and runs 3 to 300 times faster than the previous state of the art.
Why sorting encrypted documents falls apart at scale
Privacy-preserving RAG is usually framed as a secure top-k retrieval problem. Existing systems use techniques from secure k-Approximate Nearest Neighbor or secure sorting to compute similarity scores, sort the candidates, and select the top-k documents. The trouble is that comparisons under secret sharing or homomorphic encryption are expensive, and sorting requires a lot of them.
That penalty matters more than it used to. In deployment scenarios such as finance, law, and healthcare, engineering best practice suggests k = 100 to 150, causing huge overhead for existing privacy-preserving RAG systems. Meanwhile, recent studies show that within a particular yet large context threshold, such as 64K tokens, RAG with a large k generally outperforms RAG with a smaller k for modern long-context models. Cache-augmented retrieval pushes k higher still. So the field has an awkward situation where accuracy wants large k and cryptography wants small k.
Bisection instead of sorting
The core insight in p²RAG is that RAG applications only need the set of top-k documents, not their internal ranking. If you do not need to know which document is second versus fifth, you do not need to sort. You just need a distance threshold that happens to cut off exactly k of them.
The protocol runs an interactive binary search between the user and two non-colluding servers to find that threshold:
- The user secret-shares its prompt embedding across two servers.
- The servers compute encrypted dot-product distances between the prompt and every document using Beaver triples.
- The user proposes a distance threshold. The servers count how many documents fall below it, using Distributed Comparison Functions so neither server sees the actual distances.
- The count comes back to the user, who bisects the interval and tries a new threshold.
- After a fixed number of rounds, the servers return an encrypted 0/1 vector marking the in-range documents.
- The user pulls the matching texts via single-server Private Information Retrieval, with the servers verifying that only the marked indices are accessed.
Distributed Comparison Functions are the workhorse here. They let two servers each hold a share of a comparison function, evaluate it locally on their share of a value, and produce shares of the boolean result, all without either server learning the input or the threshold. That is what makes the counting step possible without decryption.
Keeping malicious users honest
The threat model is the interesting part. The two servers are semi-honest and non-colluding. That is, the servers honestly follow the protocol but passively attempt to infer information about the database and prompts. Users, on the other hand, are treated as fully malicious and may deviate arbitrarily to try to extract the database.
That user-side threat forces some careful design. A user could pick a giant threshold and mark every document as a hit. p²RAG blocks this by capping the number of returned indices with a public bound and verifying that the final 0/1 vector contains only zeros and ones. The bisection is also capped at a fixed number of iterations, both to bound how much information each query leaks and to prevent the servers from inferring anything about the prompt from the stopping time.
The authors quantify the leakage precisely. p²RAG's physical leakage is at most S = ⌈log₂(N/(k+ξ))⌉ values of c given to the user, where 0 ≤ c ≤ N. In practical settings this works out to roughly 1000 times less information than the baseline leakage inherent to returning k documents at all.
The numbers
Benchmarks were run against PRAG, the previous state of the art. P²RAG is 3× faster than PRAG when k'=16 and changing N, and 7 to 300× faster when N=2¹⁴ and k'=32 to 1024. The speedup widens dramatically with k because sorting cost grows with the number of results while bisection does not.
| N, k' | p²RAG | PRAG | Speedup |
|---|---|---|---|
| 2¹⁴, 16 | 0.106s | 0.400s | 3.78× |
| 2¹⁷, 16 | 0.751s | 2.84s | 3.78× |
| 2²⁰, 16 | 6.89s | 22.9s | 3.33× |
| 2¹⁷, 128 | 0.594s | 19.5s | 32.9× |
| 2¹⁷, 1024 | 0.448s | 144s | 322× |
For accuracy, the authors evaluated on the BEIR trec-covid dataset using Cohere's beir-embed-english-v3 embeddings. P²RAG's recall remains 1 for k' = 16 to 1024, showing high computation accuracy. The fixed-point conversion from floating-point embeddings introduces essentially no error.
Communication is manageable. For N=2²⁰ and k=16, user-server traffic totals 16.86MB over 17 round trips, with intra-server traffic reaching 335MB across data center links. A real-world deployment across Singapore and Hong Kong regions added around 7 seconds of network time in the slowest configuration, still leaving p²RAG faster overall than PRAG.
Where it fits and where it does not
The authors are direct about scope. When privacy is not critical, standard non-secure RAG is a simpler and more efficient choice because it avoids the cryptographic overhead introduced by p²RAG. Therefore, p²RAG is mainly useful for applications such as healthcare, finance, and legal services, where data privacy is a strict requirement. The overhead against a plaintext baseline is roughly 9-11x depending on database size.
A few practical constraints:
- Two non-colluding servers are required. Real deployments use hardware enclaves from different vendors, or split trust between a service provider and the user's device, as in Signal's SecureValueRecovery2 and Coinbase's WaaS.
- The embedding model must be public, and the user computes its own prompt embedding before querying.
- Trusted dealer setup is assumed for generating shared randomness, though the paper points to techniques that avoid it.
- Ordering within top-k is not preserved, which is fine for RAG context injection but not for tasks that need ranked results.
The implementation is open source under Apache 2.0, built on the myl7/fss library for function secret sharing. It uses AES-128 with Matyas-Meyer-Oseas one-way compression for the DCF pseudorandom generators. For anyone building RAG systems where the retrieval corpus is genuinely sensitive, this is the first protocol that makes the accuracy-friendly regime of k in the hundreds actually practical.