libdpf/examples/applications/keyword_pir.cpp
Ryan Henry 0d22946a0e Checkpoint the party/runtime stack before share-program and malicious-mode work.
Ship the TLS mesh, composer, Beaver/Yao/leaf MPC, prep/online paths, apps, and docs so the tree is pushable before elevating share_expr, security_mode, and prep resume.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 05:59:19 -06:00

60 lines
1.7 KiB
C++

#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
#include "dpf.hpp"
#include "dpf/app_flow.hpp"
#include "dpf/app_plans.hpp"
// Two-server keyword PIR, the DPF step (Gilboa and Ishai, EUROCRYPT 2014).
// `eval_sequence_xor` folds the selected records into one XOR accumulator
// without materializing a bit vector.
//
// c++ -std=c++17 -march=native -pthread -I include -I thirdparty \
// examples/applications/keyword_pir.cpp
// DPF_EXPERIMENT_DIR=/tmp/kw ./a.out # optional CSV dump
namespace
{
using keyword = dpf::keyword<3, dpf::alphabets::lowercase_alpha>;
} // namespace
int main()
{
const std::vector<keyword> dict{keyword{"bat"}, keyword{"cat"},
keyword{"dog"}, keyword{"pig"}};
const std::vector<int> records{56, 12, 34, 78};
auto [k0, k1] = dpf::make_dpf(keyword{"bat"}, dpf::bit::one);
const int selected = dpf::eval_sequence_xor(k0, dict.begin(), dict.end(),
records)
^ dpf::eval_sequence_xor(k1, dict.begin(), dict.end(), records);
if (selected != 56)
{
std::cerr << "keyword hit\n";
return 1;
}
auto [m0, m1] = dpf::make_dpf(keyword{"rat"}, dpf::bit::one);
const int missing = dpf::eval_sequence_xor(m0, dict.begin(), dict.end(),
records)
^ dpf::eval_sequence_xor(m1, dict.begin(), dict.end(), records);
if (missing != 0)
{
std::cerr << "keyword miss\n";
return 1;
}
{
constexpr std::size_t depth = dpf::utils::bitlength_of<keyword>::value;
if (int rc = dpf::app::run_measured("keyword_pir",
dpf::protocol::keyword_pir_compose_plan(0, depth), 2))
return rc;
}
std::cout << selected << "\n";
return 0;
}