libdpf/examples/evaluation/eval_point.cpp

55 lines
1.6 KiB
C++
Raw Permalink Normal View History

#include <cstdint>
#include <iostream>
#include "dpf.hpp"
/// One input. `*eval_point` is that party's share of output 0.
/// Reconstruct with `dpf::reconstruct` (leaf outputs are subtractive).
int main()
{
const std::uint8_t alpha = 42;
const std::uint64_t beta = 7;
auto [k0, k1] = dpf::make_dpf(alpha, beta);
using key_t = dpf::unwrap_party_key_t<std::decay_t<decltype(k0)>>;
//! [eval-point]
auto y0 = *dpf::eval_point(k0, alpha);
auto y1 = *dpf::eval_point(k1, alpha);
std::uint64_t opened = dpf::reconstruct(y0, y1);
//! [eval-point]
if (opened != beta)
{
std::cerr << "eval_point at the programmed input\n";
return 1;
}
auto off0 = *dpf::eval_point(k0, std::uint8_t{41});
auto off1 = *dpf::eval_point(k1, std::uint8_t{41});
if (dpf::reconstruct(off0, off1) != 0)
{
std::cerr << "eval_point off the programmed input\n";
return 1;
}
//! [eval-point-memo]
// One mutable memoizer per key. Nearby points reuse the common prefix.
auto path0 = dpf::make_basic_path_memoizer<key_t>();
auto path1 = dpf::make_basic_path_memoizer<key_t>();
for (int x = 40; x <= 44; ++x)
{
auto s0 = *dpf::eval_point(k0, static_cast<std::uint8_t>(x), path0);
auto s1 = *dpf::eval_point(k1, static_cast<std::uint8_t>(x), path1);
std::uint64_t got = dpf::reconstruct(s0, s1);
std::uint64_t expect = (static_cast<std::uint8_t>(x) == alpha) ? beta : 0;
if (got != expect)
{
std::cerr << "path memoizer\n";
return 1;
}
}
//! [eval-point-memo]
std::cout << opened << "\n";
return 0;
}