chalamet_pir/lib.rs
1//! ChalametPIR: A Rust library implementation of the Chalamet **P**rivate **I**nformation **R**etrieval (PIR) protocol, described in <https://ia.cr/2024/092>.
2//!
3//! This crate provides a Rust library implementation of the ChalametPIR protocol, enabling efficient and private retrieval of value associated with a key, from encoded key-value database, stored server-side.
4//! It leverages Binary Fuse Filters for efficient indexing and storage of key-value database and LWE-based encryption for data confidentiality.
5//!
6//! ## Features
7//!
8//! * **Secure Private Information Retrieval:** Allows clients to retrieve value from a PIR server without disclosing corresponding key. Server learns neither the value nor the queried key.
9//! * **Error Handling:** Comprehensive error handling to catch and report issues during setup, query generation, and response processing.
10//! * **Flexibility:** Supports both 3-wise and 4-wise XOR Binary Fuse Filters, allowing a choice between trade-offs in client/server computation and communication costs.
11//! * **Efficient:** It supports offloading parts of the server-setup phase to a GPU, using Vulkan Compute API, which can drastically reduce time taken to setup PIR server, for large key-value databases.
12//!
13//! ## Usage
14//!
15//! This crate is designed to be used in conjunction with other crates which provides communication mechanism between clients and server.
16//! You'll typically interact with the `Client` and `Server` structs to perform/ handle queries and process responses.
17//!
18//! Add ChalametPIR as dependency to your `Cargo.toml`:
19//!
20//! ```toml
21//! [dependencies]
22//! chalametpir = "=0.6.0"
23//! # Or, if you want to offload server-setup to GPU.
24//! # chalamet_pir = { version = "=0.6.0", features = ["gpu"] }
25//! rand = "=0.9.0"
26//! rand_chacha = "=0.9.0"
27//! ```
28//!
29//! Then, you can use it in your code:
30//!
31//! ```rust
32//! use chalamet_pir::{client::Client, server::Server, SEED_BYTE_LEN};
33//! use rand::prelude::*;
34//! use rand_chacha::ChaCha8Rng;
35//! use std::collections::HashMap;
36//!
37//! // Example database (replace with your own)
38//! let mut db: HashMap<&[u8], &[u8]> = HashMap::new();
39//! db.insert(b"apple", b"red");
40//! db.insert(b"banana", b"yellow");
41//!
42//! // Server setup (offline phase)
43//! let mut rng = ChaCha8Rng::from_os_rng();
44//! let mut seed_μ = [0u8; SEED_BYTE_LEN]; // You'll want to generate a cryptographically secure random seed
45//! rng.fill_bytes(&mut seed_μ);
46//!
47//! let (server, hint_bytes, filter_param_bytes) = Server::setup::<3>(&seed_μ, db.clone()).expect("Server setup failed");
48//!
49//! // Client setup (offline phase)
50//! let mut client = Client::setup(&seed_μ, &hint_bytes, &filter_param_bytes).expect("Client setup failed");
51//!
52//! // Client query (online phase)
53//! let key = b"banana";
54//! if let Ok(query) = client.query(key) {
55//! // Send `query` to the server
56//!
57//! // Server response (online phase)
58//! let response = server.respond(&query).expect("Server failed to respond");
59//!
60//! // Client processes the response (online phase)
61//! if let Ok(value) = client.process_response(key, &response) {
62//! assert_eq!(value, b"yellow");
63//! println!("Retrieved value: '{}'", String::from_utf8_lossy(&value)); // Should print "yellow"
64//! } else {
65//! assert!(false);
66//! println!("Failed to retrieve value.");
67//! }
68//! } else {
69//! println!("Failed to generate query.");
70//! }
71//! ```
72//!
73//! ## Modules
74//!
75//! * `server`: Contains the `Server` struct and associated methods for setting up a PIR server from a key-value database and responding to client queries.
76//! * `client`: Contains the `Client` struct and associated methods for generating PIR queries and decoding server responses.
77//!
78//! For more see README in ChalametPIR repository @ <https://github.com/itzmeanjan/ChalametPIR>.
79
80pub use pir_internals::error::ChalametPIRError;
81pub use pir_internals::params::SEED_BYTE_LEN;
82pub mod client;
83pub mod server;
84
85mod pir_internals;
86
87mod test_pir;