44 lines
1.6 KiB
C++
44 lines
1.6 KiB
C++
|
|
#include <array>
|
||
|
|
#include <cstdint>
|
||
|
|
#include <iostream>
|
||
|
|
#include <tuple>
|
||
|
|
#include <type_traits>
|
||
|
|
|
||
|
|
#include "dpf.hpp"
|
||
|
|
|
||
|
|
// (K,N) Shamir. The secret is the constant term of a degree K-1 polynomial.
|
||
|
|
// Party i holds that polynomial at x = i+1. Any K shares open it. (2,3) is
|
||
|
|
// shamir_share: make_shamir_shares(secret, slope) is deal<T, 2, 3>.
|
||
|
|
// fp61 and gf2n both open. For gf2n, N must be less than 2^k.
|
||
|
|
//
|
||
|
|
// c++ -std=c++17 -march=native -I include -I thirdparty examples/mwe/shamir.cpp
|
||
|
|
|
||
|
|
int main()
|
||
|
|
{
|
||
|
|
using F = dpf::fp61;
|
||
|
|
const F secret{10};
|
||
|
|
// p(x) = 10 + 2x + 3x^2. Parties 0, 2, and 4 are enough.
|
||
|
|
const std::array<F, 2> coeff{{F{2}, F{3}}};
|
||
|
|
auto shares = dpf::make_shamir_shares<3, 5>(secret, coeff);
|
||
|
|
const F opened = dpf::shamir::reconstruct(
|
||
|
|
std::get<0>(shares), std::get<2>(shares), std::get<4>(shares));
|
||
|
|
|
||
|
|
const F slope{3};
|
||
|
|
auto [s0, s1, s2] = dpf::make_shamir_shares(secret, slope);
|
||
|
|
static_assert(std::is_same_v<decltype(s0), dpf::shamir::share<F, 0, 2, 3>>);
|
||
|
|
const F opened23 = dpf::reconstruct(s1, s2);
|
||
|
|
const bool on_line = s0.raw() == secret + slope * F{1}
|
||
|
|
&& s2.raw() == secret + slope * F{3};
|
||
|
|
|
||
|
|
using G = dpf::gf28;
|
||
|
|
auto [g0, g1, g2] = dpf::make_shamir_shares(G{0x1b}, G{0x5a});
|
||
|
|
const G gopen = dpf::reconstruct(g0, g2);
|
||
|
|
const G gopen13 = dpf::reconstruct(g1, g2);
|
||
|
|
|
||
|
|
std::cout << opened.raw() << " " << opened23.raw() << " "
|
||
|
|
<< static_cast<unsigned>(gopen.raw()) << "\n";
|
||
|
|
return (opened == secret && opened23 == secret && on_line
|
||
|
|
&& gopen == G{0x1b} && gopen13 == G{0x1b})
|
||
|
|
? 0 : 1;
|
||
|
|
}
|