Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
> the API is not yet frozen and headers may move between alphas.
> Requires emp-tool ≥ 1.0.0-alpha and emp-ot ≥ 1.0.0-alpha.

Header-only semi-honest 2PC built on top of [emp-tool](https://github.com/emp-toolkit/emp-tool) and [emp-ot](https://github.com/emp-toolkit/emp-ot): garbled-circuit evaluation (half-gates) for `Bit` / `Integer` / `Float` over a `NetIO` channel, with batched IKNP COT for input wires.
Header-only semi-honest 2PC built on top of [emp-tool](https://github.com/emp-toolkit/emp-tool) and [emp-ot](https://github.com/emp-toolkit/emp-ot): garbled-circuit evaluation (half-gates) for `Bit` / `Integer` / `Float` over a `NetIO` channel, with batched correlated OT (IKNP by default) for the evaluator's input wires.

## Usage

Expand Down Expand Up @@ -51,6 +51,22 @@ compiled once with the emp-tool frontend and replayed over the session's context
`frontend::run(sess.ctx(), circuit, args...)`. There is no global backend — the
session is explicit.

### Choosing the OT extension

`SH2PCSession` is `SH2PCSessionT<IKNP>`. `SH2PCSessionT<OTExt>` takes any emp-ot OT
extension for the correlated OTs behind BOB's private input bits, the only inputs
that use OT; both parties must choose the same one:

```cpp
SH2PCSessionT<SoftSpoken<4>> sess(io.get(), party); // or SH2PCSessionT<Ferret>
```

`SoftSpoken<k>` sends fewer bytes per OT than IKNP, fewer as `k` grows, for more AES
work per OT. `Ferret` sends the fewest bytes per OT but has a larger fixed setup and
more computation per OT, so it pays off when BOB's inputs are large and bandwidth is
the bottleneck. Values are built over the same `ctx_t` whichever extension the
session uses.

## Requirements

- CMake ≥ 3.21
Expand Down
4 changes: 3 additions & 1 deletion emp-sh2pc/sh2pc_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace emp {

template <class OTExt> class SH2PCSessionT;

class SH2PCCtx {
public:
// The wire IS the live garbled label: block (__m128i) is semiregular, which
Expand Down Expand Up @@ -45,7 +47,7 @@ class SH2PCCtx {
}

private:
friend class SH2PCSession;
template <class> friend class SH2PCSessionT;
// Bind to the owning session's protocol primitives. party / io / delta /
// constant are fixed once (after the session handshake) and copied by value;
// mitccrh is shared by pointer (the session owns it, gid advances per AND).
Expand Down
48 changes: 36 additions & 12 deletions emp-sh2pc/sh2pc_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define EMP_SH2PC_SESSION_H__

// SH2PCSession — the public handle for semi-honest 2PC (garbled circuits), the
// SH2PC peer of ClearSession. It owns ALL protocol state — IO, IKNP-OT, Delta, the
// synchronized PRG, the half-gate MITCCRH/constants, and the COT refill buffer —
// and the I/O boundary (input / reveal); sess.ctx() is the gate context values are
// built over. Gates are eager (an AND garbles/evaluates as it is called), so there
// is no chunk/checkpoint model: a value's wire IS the live garbled label.
// SH2PC peer of ClearSession. It owns ALL protocol state — IO, the OT extension,
// Delta, the synchronized PRG, the half-gate MITCCRH/constants, and the COT refill
// buffer — and the I/O boundary (input / reveal); sess.ctx() is the gate context
// values are built over. Gates are eager (an AND garbles/evaluates as it is
// called), so there is no chunk/checkpoint model: a value's wire IS the live
// garbled label.
//
// SH2PCSession sess(io, party);
// using Ctx = SH2PCSession::ctx_t; using UInt32 = UInt_T<Ctx, 32>;
Expand All @@ -17,6 +18,14 @@
//
// Public constants stay value/context-level: UInt32::constant(sess.ctx(), 7).
// There is no global backend and no virtual dispatch — the session is explicit.
//
// SH2PCSession is SH2PCSessionT<IKNP>. SH2PCSessionT<OTExt> takes any emp-ot OT
// extension (IKNP, SoftSpoken<k>, Ferret) for the correlated OTs behind BOB's
// private input bits, the only inputs that use OT; both parties must choose the
// same one. SoftSpoken<k> sends fewer bytes per OT than IKNP, fewer as k grows,
// for more AES work per OT. Ferret sends the fewest bytes per OT but has a larger
// fixed setup and more computation per OT, so it pays off when BOB's inputs are
// large and bandwidth is the bottleneck.

#include "emp-sh2pc/sh2pc_ctx.h" // SH2PCCtx (Wire = block)
#include "emp-tool/emp-tool.h"
Expand All @@ -34,7 +43,11 @@

namespace emp {

class SH2PCSession {
template <class OTExt = IKNP>
class SH2PCSessionT {
static_assert(std::is_base_of_v<OTExtension, OTExt>,
"SH2PCSessionT<OTExt>: OTExt must be an emp-ot OT extension "
"(IKNP, SoftSpoken<k>, Ferret)");
public:
using ctx_t = SH2PCCtx; // the direct/user gate context; values are UInt_T<ctx_t,N> etc.
// reveal returns std::optional<clear_t> (the session contract): the value is
Expand All @@ -43,7 +56,7 @@ class SH2PCSession {
// and std::nullopt on a party that does not learn it.
template <class V> using reveal_t = std::optional<typename V::clear_t>;

SH2PCSession(IOChannel* io, int party, int batch_sz = 1024 * 16)
SH2PCSessionT(IOChannel* io, int party, int batch_sz = 1024 * 16)
: party_(party), io_(io), batch_size_(batch_sz) {
if (party_ != ALICE && party_ != BOB)
error("SH2PCSession: party must be ALICE or BOB");
Expand All @@ -58,7 +71,14 @@ class SH2PCSession {
constant_[1] = constant_[1] ^ delta_;
io_->send_block(tmp + 1, 1);
mitccrh_.setS(tmp[1]);
ot_ = std::make_unique<IKNP>(ALICE, io_, /*malicious=*/false);
// A fresh session id, set before the OT extension starts, separates
// its hash inputs (and those of its base OTs) from every other
// session's.
block sid;
PRG().random_block(&sid, 1);
io_->send_block(&sid, 1);
ot_ = std::make_unique<OTExt>(ALICE, io_, /*malicious=*/false);
ot_->set_sid(sid);
bool db[128];
const uint8_t* d = reinterpret_cast<const uint8_t*>(&delta_);
for (int i = 0; i < 128; ++i) db[i] = (d[i / 8] >> (i % 8)) & 1;
Expand All @@ -70,7 +90,9 @@ class SH2PCSession {
io_->recv_block(constant_, 2);
block tmp; io_->recv_block(&tmp, 1);
mitccrh_.setS(tmp);
ot_ = std::make_unique<IKNP>(BOB, io_, /*malicious=*/false);
block sid; io_->recv_block(&sid, 1);
ot_ = std::make_unique<OTExt>(BOB, io_, /*malicious=*/false);
ot_->set_sid(sid);
block seed; io_->recv_block(&seed, 1);
shared_prg_.reseed(&seed);
}
Expand All @@ -79,8 +101,8 @@ class SH2PCSession {
refill_();
ctx_.bind_(party_, io_, delta_, constant_, &mitccrh_);
}
SH2PCSession(const SH2PCSession&) = delete;
SH2PCSession& operator=(const SH2PCSession&) = delete;
SH2PCSessionT(const SH2PCSessionT&) = delete;
SH2PCSessionT& operator=(const SH2PCSessionT&) = delete;

void finalize() {}

Expand Down Expand Up @@ -147,7 +169,7 @@ class SH2PCSession {
block constant_[2];
block delta_{}; // meaningful for ALICE (the garbler)
MITCCRH<8> mitccrh_;
std::unique_ptr<IKNP> ot_;
std::unique_ptr<OTExt> ot_;
PRG shared_prg_;
std::unique_ptr<block[]> buf_;
std::unique_ptr<bool[]> buff_;
Expand Down Expand Up @@ -246,6 +268,8 @@ class SH2PCSession {
}
};

using SH2PCSession = SH2PCSessionT<>;

static_assert(Session<SH2PCSession>);
static_assert(DirectSession<SH2PCSession>);
static_assert(SessionIO<SH2PCSession, UInt_T<SH2PCSession::ctx_t, 32>>);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ endif()
add_test_case_with_run(test_example)
add_test_case_with_run(test_repeat)
add_test_case_with_run(test_aes)
add_test_case_with_run(test_ot)
add_test_case_with_run(test_context_sh2pc)
add_test_case_with_run(test_circuit_fn_sh2pc)
add_test_case(test_session_concepts_sh2pc)
65 changes: 65 additions & 0 deletions test/test_ot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "emp-sh2pc/emp-sh2pc.h"
#include <memory>
#include <vector>
using namespace emp;
using namespace std;

// SH2PCSessionT<OTExt>: the same checks over each OT extension. Only BOB's
// private input bits use OT. With a 1024-OT refill batch, BOB's inputs below are
// served from the buffered batch (1 and 1000 bits), straddle a refill (100 bits)
// and exceed the batch (5000 bits, extended directly); every bit must come back
// unchanged. The 32-bit dot product then needs the OT labels and the garbling to
// share one Delta.
template <class OTExt>
static int run_case(IOChannel* io, int party, const char* name) {
SH2PCSessionT<OTExt> sess(io, party, /*batch_sz=*/1024);
int bad = 0;

int offset = 0;
for (int n : {1, 1000, 100, 5000}) {
// A public bit pattern, so both parties can check BOB's bits.
auto in = make_unique<bool[]>(n), out = make_unique<bool[]>(n);
for (int i = 0; i < n; ++i) in[i] = ((unsigned)(offset + i) * 2654435761u >> 13) & 1;
vector<block> w = sess.input_bits(BOB, in.get(), n);
sess.reveal_bits(out.get(), PUBLIC, w.data(), n);
for (int i = 0; i < n; ++i)
if (out[i] != in[i]) {
cout << name << ": BOB input bit " << offset + i << " changed" << endl;
++bad;
break;
}
offset += n;
}

using U32 = UInt_T<typename SH2PCSessionT<OTExt>::ctx_t, 32>;
U32 acc = U32::constant(sess.ctx(), 0);
uint32_t want = 0;
for (uint32_t i = 0; i < 32; ++i) {
uint32_t av = 0x9e3779b9u * (i + 1), bv = 0x85ebca6bu * (i + 7);
U32 a = sess.template input<U32>(ALICE, av);
U32 b = sess.template input<U32>(BOB, bv);
acc = acc + a * b;
want += av * bv;
}
uint32_t got = (uint32_t)sess.reveal(acc, PUBLIC).value();
if (got != want) {
cout << name << ": dot product " << got << ", want " << want << endl;
++bad;
}

sess.finalize();
cout << name << (bad ? ": FAIL" : ": ok") << endl;
return bad;
}

int main(int argc, char** argv) {
int party = parse_party(argv);
int port = peer_port();
auto io = (party == ALICE) ? NetIO::listen(port, /*quiet=*/true)
: NetIO::connect(peer_ip(), port, /*quiet=*/true);
int bad = 0;
bad += run_case<IKNP>(io.get(), party, "IKNP");
bad += run_case<SoftSpoken<4>>(io.get(), party, "SoftSpoken<4>");
bad += run_case<Ferret>(io.get(), party, "Ferret");
return bad == 0 ? 0 : 1;
}
6 changes: 6 additions & 0 deletions test/test_session_concepts_sh2pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ static_assert(std::is_same_v<SH2PCSession::ctx_t, SH2PCCtx>);
static_assert(BooleanContext<SH2PCCtx>);
static_assert(std::is_same_v<UInt_T<SH2PCSession::ctx_t, 32>, UInt_T<SH2PCCtx, 32>>);

// Any OT extension gives the same session surface over the same gate context.
static_assert(std::is_same_v<SH2PCSession, SH2PCSessionT<IKNP>>);
static_assert(Session<SH2PCSessionT<SoftSpoken<4>>>);
static_assert(DirectSession<SH2PCSessionT<Ferret>>);
static_assert(std::is_same_v<SH2PCSessionT<Ferret>::ctx_t, SH2PCSession::ctx_t>);

// reveal returns std::optional<clear_t> (nullopt on a party that does not learn it).
static_assert(std::is_same_v<
SH2PCSession::reveal_t<UInt_T<SH2PCSession::ctx_t, 32>>,
Expand Down
Loading