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>
This commit is contained in:
Ryan Henry 2026-09-28 05:59:19 -06:00
parent 695f8e84f7
commit 0d22946a0e
1835 changed files with 170291 additions and 2849 deletions

109
python/pydpf.cpp Normal file
View file

@ -0,0 +1,109 @@
#include <cstdint>
#include <stdexcept>
#include <utility>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "pydpf_types.hpp"
#include "pydpf_eval.hpp"
// Header-only libdpf symbols are not `inline`; keep one TU.
#include "pydpf_eval.inc"
namespace py = pybind11;
PYBIND11_MODULE(pydpf, m)
{
m.doc() = "libdpf bindings: point/interval/full/sequence/recipe, "
"multileaf, wildcards, eval_until, it_dpf3";
py::class_<pydpf_detail::PointKeyPair>(m, "PointKeyPair");
py::class_<pydpf_detail::IdpfKeyPair>(m, "IdpfKeyPair");
py::class_<pydpf_detail::ItDpf3Keys>(m, "ItDpf3Keys");
py::class_<dpf::idpf_eval_ctx<pydpf_detail::idpf_key0>>(m, "IdpfEvalCtx0");
py::class_<dpf::idpf_eval_ctx<pydpf_detail::idpf_key1>>(m, "IdpfEvalCtx1");
m.def("make_dpf", [](std::uint8_t alpha, std::uint64_t beta) {
auto [a, b] = dpf::make_dpf(alpha, beta);
return pydpf_detail::PointKeyPair{std::move(a), std::move(b)};
}, py::arg("alpha"), py::arg("beta"));
m.def("eval_point", [](const pydpf_detail::PointKeyPair & keys, int party,
std::uint8_t x) {
pydpf_detail::require_party(party);
if (party == 0)
return pydpf_detail::share_limb(*dpf::eval_point(keys.k0, x));
return pydpf_detail::share_limb(*dpf::eval_point(keys.k1, x));
}, py::arg("keys"), py::arg("party"), py::arg("x"));
m.def("reconstruct", [](std::uint64_t s0, std::uint64_t s1) {
return s0 - s1;
}, py::arg("share0"), py::arg("share1"));
m.def("make_idpf16", [](std::uint16_t alpha) {
auto [a, b] = dpf::make_dpf(alpha, dpf::idpf_ones<16>());
return pydpf_detail::IdpfKeyPair{std::move(a), std::move(b)};
}, py::arg("alpha"));
m.def("make_eval_ctx", [](const pydpf_detail::IdpfKeyPair & keys, int party) {
pydpf_detail::require_party(party);
if (party == 0)
return py::cast(dpf::idpf_eval_ctx<pydpf_detail::idpf_key0>(keys.k0));
return py::cast(dpf::idpf_eval_ctx<pydpf_detail::idpf_key1>(keys.k1));
}, py::arg("keys"), py::arg("party"));
m.def("eval_until", [](py::object ctx, std::size_t level,
const std::vector<std::uint16_t> & prefixes) {
if (py::isinstance<dpf::idpf_eval_ctx<pydpf_detail::idpf_key0>>(ctx))
{
auto & c = ctx.cast<dpf::idpf_eval_ctx<pydpf_detail::idpf_key0> &>();
auto shares = dpf::eval_until(c, level, prefixes);
std::vector<std::uint64_t> out;
out.reserve(shares.size());
for (auto & s : shares)
out.push_back(s.raw());
return out;
}
if (py::isinstance<dpf::idpf_eval_ctx<pydpf_detail::idpf_key1>>(ctx))
{
auto & c = ctx.cast<dpf::idpf_eval_ctx<pydpf_detail::idpf_key1> &>();
auto shares = dpf::eval_until(c, level, prefixes);
std::vector<std::uint64_t> out;
out.reserve(shares.size());
for (auto & s : shares)
out.push_back(s.raw());
return out;
}
throw std::invalid_argument("eval_until: unknown context type");
}, py::arg("ctx"), py::arg("level"), py::arg("prefixes"));
m.def("ctx_node_count", [](py::object ctx) {
if (py::isinstance<dpf::idpf_eval_ctx<pydpf_detail::idpf_key0>>(ctx))
return ctx.cast<dpf::idpf_eval_ctx<pydpf_detail::idpf_key0> &>()
.node_count();
if (py::isinstance<dpf::idpf_eval_ctx<pydpf_detail::idpf_key1>>(ctx))
return ctx.cast<dpf::idpf_eval_ctx<pydpf_detail::idpf_key1> &>()
.node_count();
throw std::invalid_argument("ctx_node_count: unknown context type");
}, py::arg("ctx"));
m.def("make_it_dpf3", [](std::uint8_t alpha, std::uint64_t beta) {
auto [a, b, c] = dpf::make_it_dpf3(alpha, beta);
return pydpf_detail::ItDpf3Keys{std::move(a), std::move(b), std::move(c)};
}, py::arg("alpha"), py::arg("beta"));
m.def("eval_it_dpf3", [](const pydpf_detail::ItDpf3Keys & keys, int party,
std::uint8_t x) {
if (party == 0)
return dpf::eval_it_dpf3(keys.k0, x);
if (party == 1)
return dpf::eval_it_dpf3(keys.k1, x);
if (party == 2)
return dpf::eval_it_dpf3(keys.k2, x);
throw std::invalid_argument("eval_it_dpf3: party must be 0, 1, or 2");
}, py::arg("keys"), py::arg("party"), py::arg("x"));
pydpf_detail::register_eval(m);
}

10
python/pydpf_eval.hpp Normal file
View file

@ -0,0 +1,10 @@
#pragma once
#include <pybind11/pybind11.h>
namespace pydpf_detail
{
void register_eval(pybind11::module_ & m);
} // namespace pydpf_detail

172
python/pydpf_eval.inc Normal file
View file

@ -0,0 +1,172 @@
#include <cstdint>
#include <stdexcept>
#include <utility>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "pydpf_types.hpp"
#include "pydpf_eval.hpp"
namespace py = pybind11;
namespace pydpf_detail
{
namespace
{
template <typename Key>
std::vector<std::uint64_t> eval_full_limbs(const Key & key)
{
auto [buf, iter] = dpf::eval_full(key);
(void)buf;
return collect_limbs(iter);
}
template <typename Key>
std::vector<std::uint64_t> eval_interval_limbs(const Key & key,
std::uint8_t from, std::uint8_t to)
{
if (to < from)
throw std::invalid_argument("eval_interval: to < from");
auto [buf, iter] = dpf::eval_interval(key, from, to);
(void)buf;
return collect_limbs(iter);
}
template <typename Key>
std::vector<std::uint64_t> eval_sequence_limbs(const Key & key,
const std::vector<std::uint8_t> & xs)
{
require_sorted(xs);
auto [buf, iter] = dpf::eval_sequence(key, xs.begin(), xs.end());
(void)buf;
return collect_limbs(iter);
}
template <typename Key>
std::vector<std::uint64_t> eval_recipe_limbs(const Key & key,
const std::vector<std::uint8_t> & xs)
{
require_sorted(xs);
auto recipe = dpf::make_sequence_recipe(key, xs.begin(), xs.end());
auto [buf, iter] = dpf::eval_sequence(key, recipe);
(void)buf;
return collect_limbs(iter);
}
} // namespace
void register_eval(py::module_ & m)
{
py::class_<MultiKeyPair>(m, "MultiKeyPair");
py::class_<WildcardKeyPair>(m, "WildcardKeyPair");
m.def("eval_full", [](const PointKeyPair & keys, int party) {
require_party(party);
return party == 0 ? eval_full_limbs(keys.k0) : eval_full_limbs(keys.k1);
}, py::arg("keys"), py::arg("party"));
m.def("eval_interval", [](const PointKeyPair & keys, int party,
std::uint8_t from, std::uint8_t to) {
require_party(party);
return party == 0 ? eval_interval_limbs(keys.k0, from, to)
: eval_interval_limbs(keys.k1, from, to);
}, py::arg("keys"), py::arg("party"), py::arg("from"), py::arg("to"));
m.def("eval_sequence", [](const PointKeyPair & keys, int party,
const std::vector<std::uint8_t> & xs) {
require_party(party);
return party == 0 ? eval_sequence_limbs(keys.k0, xs)
: eval_sequence_limbs(keys.k1, xs);
}, py::arg("keys"), py::arg("party"), py::arg("points"));
m.def("eval_sequence_recipe", [](const PointKeyPair & keys, int party,
const std::vector<std::uint8_t> & xs) {
require_party(party);
return party == 0 ? eval_recipe_limbs(keys.k0, xs)
: eval_recipe_limbs(keys.k1, xs);
}, py::arg("keys"), py::arg("party"), py::arg("points"));
m.def("make_dpf_multi", [](std::uint8_t alpha, std::uint64_t beta0,
std::uint64_t beta1) {
auto [a, b] = dpf::make_dpf(alpha, beta0, beta1);
return MultiKeyPair{std::move(a), std::move(b)};
}, py::arg("alpha"), py::arg("beta0"), py::arg("beta1"));
m.def("eval_point_leaf", [](const MultiKeyPair & keys, int party, int leaf,
std::uint8_t x) {
require_party(party);
if (leaf == 0)
{
if (party == 0)
return share_limb(*dpf::eval_point<0>(keys.k0, x));
return share_limb(*dpf::eval_point<0>(keys.k1, x));
}
if (leaf == 1)
{
if (party == 0)
return share_limb(*dpf::eval_point<1>(keys.k0, x));
return share_limb(*dpf::eval_point<1>(keys.k1, x));
}
throw std::invalid_argument("eval_point_leaf: leaf must be 0 or 1");
}, py::arg("keys"), py::arg("party"), py::arg("leaf"), py::arg("x"));
m.def("eval_full_leaf", [](const MultiKeyPair & keys, int party, int leaf) {
require_party(party);
if (leaf == 0)
{
if (party == 0)
{
auto [buf, iter] = dpf::eval_full<0>(keys.k0);
(void)buf;
return collect_limbs(iter);
}
auto [buf, iter] = dpf::eval_full<0>(keys.k1);
(void)buf;
return collect_limbs(iter);
}
if (leaf == 1)
{
if (party == 0)
{
auto [buf, iter] = dpf::eval_full<1>(keys.k0);
(void)buf;
return collect_limbs(iter);
}
auto [buf, iter] = dpf::eval_full<1>(keys.k1);
(void)buf;
return collect_limbs(iter);
}
throw std::invalid_argument("eval_full_leaf: leaf must be 0 or 1");
}, py::arg("keys"), py::arg("party"), py::arg("leaf"));
m.def("make_dpf_wildcard", [](std::uint8_t alpha) {
auto [a, b] = dpf::make_dpf(alpha, dpf::wildcard_value<std::uint64_t>{});
return WildcardKeyPair{std::move(a), std::move(b)};
}, py::arg("alpha"));
m.def("assign_wildcard", [](WildcardKeyPair & keys, std::uint64_t beta) {
// Beaver assign takes additive shares of β (same as wildcard_test).
const std::uint64_t shr0 = dpf::uniform_sample<std::uint64_t>();
const std::uint64_t shr1 = beta - shr0;
assign_leaf_local(keys.k0, keys.k1, shr0, shr1);
}, py::arg("keys"), py::arg("beta"));
m.def("eval_point", [](const WildcardKeyPair & keys, int party,
std::uint8_t x) {
require_party(party);
if (party == 0)
return share_limb(*dpf::eval_point(keys.k0, x));
return share_limb(*dpf::eval_point(keys.k1, x));
}, py::arg("keys"), py::arg("party"), py::arg("x"));
m.def("eval_full", [](const WildcardKeyPair & keys, int party) {
require_party(party);
return party == 0 ? eval_full_limbs(keys.k0) : eval_full_limbs(keys.k1);
}, py::arg("keys"), py::arg("party"));
}
} // namespace pydpf_detail

125
python/pydpf_types.hpp Normal file
View file

@ -0,0 +1,125 @@
#pragma once
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include "dpf.hpp"
namespace pydpf_detail
{
using point_key0 = decltype(dpf::make_dpf(std::uint8_t{0},
std::uint64_t{0}).first);
using point_key1 = decltype(dpf::make_dpf(std::uint8_t{0},
std::uint64_t{0}).second);
using multi_key0 = decltype(dpf::make_dpf(std::uint8_t{0},
std::uint64_t{0}, std::uint64_t{0}).first);
using multi_key1 = decltype(dpf::make_dpf(std::uint8_t{0},
std::uint64_t{0}, std::uint64_t{0}).second);
using wild_key0 = decltype(dpf::make_dpf(std::uint8_t{0},
dpf::wildcard_value<std::uint64_t>{}).first);
using wild_key1 = decltype(dpf::make_dpf(std::uint8_t{0},
dpf::wildcard_value<std::uint64_t>{}).second);
using idpf_key0 = decltype(dpf::make_dpf(std::uint16_t{0},
dpf::idpf_ones<16>()).first);
using idpf_key1 = decltype(dpf::make_dpf(std::uint16_t{0},
dpf::idpf_ones<16>()).second);
struct PointKeyPair
{
point_key0 k0;
point_key1 k1;
};
struct MultiKeyPair
{
multi_key0 k0;
multi_key1 k1;
};
struct WildcardKeyPair
{
wild_key0 k0;
wild_key1 k1;
};
struct IdpfKeyPair
{
idpf_key0 k0;
idpf_key1 k1;
};
struct ItDpf3Keys
{
dpf::it_dpf3_key k0;
dpf::it_dpf3_key k1;
dpf::it_dpf3_key k2;
};
template <typename Share, typename = void>
struct has_raw_member : std::false_type {};
template <typename Share>
struct has_raw_member<Share,
std::void_t<decltype(std::declval<const Share &>().raw())>>
: std::true_type {};
template <typename Share>
std::uint64_t share_limb(const Share & s)
{
if constexpr (has_raw_member<Share>::value)
return static_cast<std::uint64_t>(s.raw());
else
return static_cast<std::uint64_t>(s);
}
template <typename Iterable>
std::vector<std::uint64_t> collect_limbs(Iterable && iterable)
{
std::vector<std::uint64_t> out;
for (auto it = std::begin(iterable); it != std::end(iterable); ++it)
out.push_back(share_limb(*it));
return out;
}
/// In-process wildcard leaf assign (same messages as asio, no socket).
template <std::size_t I = 0, typename DpfKey0, typename DpfKey1, typename ShareT>
void assign_leaf_local(DpfKey0 & dpf0, DpfKey1 & dpf1, const ShareT & shr0,
const ShareT & shr1)
{
auto & w0 = std::get<I>(dpf0.leaf_nodes);
auto & w1 = std::get<I>(dpf1.leaf_nodes);
if (w0.is_ready())
w0.begin_update();
if (w1.is_ready())
w1.begin_update();
const auto b0 = w0.compute_and_get_blinded_output_share(shr0);
const auto b1 = w1.compute_and_get_blinded_output_share(shr1);
const auto l0 = w0.compute_and_get_leaf_share(b1);
const auto l1 = w1.compute_and_get_leaf_share(b0);
w0.reconstruct_correction_word(l1);
w1.reconstruct_correction_word(l0);
}
inline void require_party(int party)
{
if (party != 0 && party != 1)
throw std::invalid_argument("party must be 0 or 1");
}
inline void require_sorted(const std::vector<std::uint8_t> & xs)
{
for (std::size_t i = 1; i < xs.size(); ++i)
{
if (xs[i] < xs[i - 1])
throw std::invalid_argument("sequence points must be nondecreasing");
}
}
} // namespace pydpf_detail

Binary file not shown.

Binary file not shown.

159
python/tests/test_pydpf.py Normal file
View file

@ -0,0 +1,159 @@
"""Smoke tests for the optional pydpf module."""
import pydpf
def test_point_mass():
keys = pydpf.make_dpf(7, 11)
on = pydpf.reconstruct(
pydpf.eval_point(keys, 0, 7),
pydpf.eval_point(keys, 1, 7),
)
off = pydpf.reconstruct(
pydpf.eval_point(keys, 0, 8),
pydpf.eval_point(keys, 1, 8),
)
assert on == 11
assert off == 0
def test_eval_until_prefix_resume():
keys = pydpf.make_idpf16(0xBEEF)
ctx0 = pydpf.make_eval_ctx(keys, 0)
ctx1 = pydpf.make_eval_ctx(keys, 1)
# High bit of 0xBEEF is 1.
s0 = pydpf.eval_until(ctx0, 1, [0, 1])
s1 = pydpf.eval_until(ctx1, 1, [0, 1])
assert pydpf.ctx_node_count(ctx0) == 2
assert pydpf.reconstruct(s0[0], s1[0]) == 0
assert pydpf.reconstruct(s0[1], s1[1]) == 1
def test_it_dpf3_reconstruction():
keys = pydpf.make_it_dpf3(42, 7)
mask = (1 << 64) - 1
on = (
pydpf.eval_it_dpf3(keys, 0, 42)
+ pydpf.eval_it_dpf3(keys, 1, 42)
+ pydpf.eval_it_dpf3(keys, 2, 42)
) & mask
off = (
pydpf.eval_it_dpf3(keys, 0, 41)
+ pydpf.eval_it_dpf3(keys, 1, 41)
+ pydpf.eval_it_dpf3(keys, 2, 41)
) & mask
assert on == 7
assert off == 0
def _open_vec(s0, s1):
return [pydpf.reconstruct(a, b) for a, b in zip(s0, s1)]
def test_eval_full():
keys = pydpf.make_dpf(7, 11)
opened = _open_vec(pydpf.eval_full(keys, 0), pydpf.eval_full(keys, 1))
assert len(opened) == 256
assert opened[7] == 11
assert all(v == 0 for i, v in enumerate(opened) if i != 7)
def test_eval_interval_matches_full_slice():
keys = pydpf.make_dpf(40, 9)
full = _open_vec(pydpf.eval_full(keys, 0), pydpf.eval_full(keys, 1))
lo, hi = 30, 50
interval = _open_vec(
pydpf.eval_interval(keys, 0, lo, hi),
pydpf.eval_interval(keys, 1, lo, hi),
)
assert interval == full[lo : hi + 1]
def test_eval_sequence_matches_point():
keys = pydpf.make_dpf(5, 3)
points = [1, 5, 5, 200]
seq = _open_vec(
pydpf.eval_sequence(keys, 0, points),
pydpf.eval_sequence(keys, 1, points),
)
for p, got in zip(points, seq):
expect = pydpf.reconstruct(
pydpf.eval_point(keys, 0, p),
pydpf.eval_point(keys, 1, p),
)
assert got == expect
def test_eval_sequence_recipe():
keys = pydpf.make_dpf(5, 3)
points = [1, 5, 200]
via_seq = _open_vec(
pydpf.eval_sequence(keys, 0, points),
pydpf.eval_sequence(keys, 1, points),
)
via_recipe = _open_vec(
pydpf.eval_sequence_recipe(keys, 0, points),
pydpf.eval_sequence_recipe(keys, 1, points),
)
assert via_seq == via_recipe
try:
pydpf.eval_sequence(keys, 0, [3, 1])
assert False, "expected unsorted rejection"
except ValueError:
pass
def test_multileaf():
keys = pydpf.make_dpf_multi(9, 100, 200)
assert pydpf.reconstruct(
pydpf.eval_point_leaf(keys, 0, 0, 9),
pydpf.eval_point_leaf(keys, 1, 0, 9),
) == 100
assert pydpf.reconstruct(
pydpf.eval_point_leaf(keys, 0, 1, 9),
pydpf.eval_point_leaf(keys, 1, 1, 9),
) == 200
assert pydpf.reconstruct(
pydpf.eval_point_leaf(keys, 0, 0, 8),
pydpf.eval_point_leaf(keys, 1, 0, 8),
) == 0
full0 = _open_vec(
pydpf.eval_full_leaf(keys, 0, 0),
pydpf.eval_full_leaf(keys, 1, 0),
)
assert full0[9] == 100
assert full0[0] == 0
def test_wildcard():
keys = pydpf.make_dpf_wildcard(12)
try:
pydpf.eval_point(keys, 0, 12)
assert False, "expected eval before assign to fail"
except RuntimeError:
pass
pydpf.assign_wildcard(keys, 77)
assert pydpf.reconstruct(
pydpf.eval_point(keys, 0, 12),
pydpf.eval_point(keys, 1, 12),
) == 77
assert pydpf.reconstruct(
pydpf.eval_point(keys, 0, 13),
pydpf.eval_point(keys, 1, 13),
) == 0
opened = _open_vec(pydpf.eval_full(keys, 0), pydpf.eval_full(keys, 1))
assert opened[12] == 77
assert opened[0] == 0
if __name__ == "__main__":
test_point_mass()
test_eval_until_prefix_resume()
test_it_dpf3_reconstruction()
test_eval_full()
test_eval_interval_matches_full_slice()
test_eval_sequence_matches_point()
test_eval_sequence_recipe()
test_multileaf()
test_wildcard()
print("ok")