Skip to content

Commit dfe3ea1

Browse files
wangxiao1254claude
andcommitted
test: env party/port and NetIO::listen()/connect() factories
party from parse_party(argv); port/IP from peer_port()/peer_ip(). The stack `NetIO io(party==ALICE?nullptr:peer_ip(), port)` becomes `auto io = listen/ connect(...)` (unique_ptr); SH2PCSession takes io.get(), and the two helpers that borrow the channel by reference receive *io. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 9cbaf3f commit dfe3ea1

10 files changed

Lines changed: 54 additions & 44 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ protocol state, and `sess.direct_ctx()` is the gate context your values are buil
2727
#include <emp-sh2pc/emp-sh2pc.h>
2828
using namespace emp;
2929

30-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
30+
int party = parse_party(argv); // argv[1]; port/IP come from $EMP_PORT / $EMP_PEER_IP
31+
NetIO io(party == ALICE ? nullptr : peer_ip(), peer_port());
3132
SH2PCSession sess(&io, party);
3233

3334
using Ctx = SH2PCSession::DirectCtx; // the gate context values are built over

run

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
#!/bin/bash
22
# Two-party local test harness. Pick a fresh random port per invocation so back-
3-
# to-back ctest runs don't collide on a socket still in TIME_WAIT.
4-
PORT=$(( (RANDOM % 30000) + 20000 ))
3+
# to-back ctest runs don't collide on a socket still in TIME_WAIT. Port travels
4+
# via EMP_PORT (read by peer_port()); only the party id is a positional arg.
5+
export EMP_PORT=$(( (RANDOM % 30000) + 20000 ))
56
if [ "$1" == "-p1" ]
67
then
78
shift
8-
perf record $1 1 $PORT & (sleep 0.1; $1 2 $PORT)
9+
perf record $1 1 & (sleep 0.1; $1 2)
910
elif [ "$1" == "-p2" ]
1011
then
1112
shift
12-
(sleep 0.1; $1 1 $PORT) & (perf record $1 2 $PORT)
13+
(sleep 0.1; $1 1) & (perf record $1 2)
1314

1415
elif [ "$1" == "-m1" ]
1516
then
1617
shift
17-
valgrind --leak-check=full $1 1 $PORT & $1 2 $PORT
18+
valgrind --leak-check=full $1 1 & $1 2
1819
elif [ "$1" == "-m2" ]
1920
then
2021
shift
21-
$1 1 $PORT & valgrind --leak-check=full $1 2 $PORT
22+
$1 1 & valgrind --leak-check=full $1 2
2223
elif [ "$1" == "-t1" ]
2324
then
2425
shift
25-
time $1 1 $PORT & $1 2 $PORT
26+
time $1 1 & $1 2
2627
elif [ "$1" == "-t2" ]
2728
then
2829
shift
29-
$1 1 $PORT & time $1 2 $PORT
30+
$1 1 & time $1 2
3031

3132
else
32-
(sleep 0.05; $1 1 $PORT) & $1 2 $PORT
33+
(sleep 0.05; $1 1) & $1 2
3334
fi

test/test_aes.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ using namespace std;
1414

1515
int main(int argc, char** argv) {
1616
int port, party;
17-
parse_party_and_port(argv, &party, &port);
18-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
17+
party = parse_party(argv);
18+
port = peer_port();
19+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
1920

20-
SH2PCSession sess(&io, party);
21+
SH2PCSession sess(io.get(), party);
2122

2223
using Wire = SH2PCCtx::Wire; // = block: the wire is the live garbled label
2324
bool zero[128];

test/test_bit.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ static int test_bit(SH2PCSession& sess, NetIO& io, int party) {
7373

7474
int main(int argc, char** argv) {
7575
int port, party;
76-
parse_party_and_port(argv, &party, &port);
77-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
78-
SH2PCSession sess(&io, party);
76+
party = parse_party(argv);
77+
port = peer_port();
78+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
79+
SH2PCSession sess(io.get(), party);
7980

80-
int fails = test_bit(sess, io, party);
81+
int fails = test_bit(sess, *io, party);
8182

8283
sess.finalize();
8384
if (party == BOB) cout << "test_bit: " << (fails ? "FAILED" : "PASS") << endl;

test/test_circuit_fn_sh2pc.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// live SH2PCSession context — compile-once / run-on-any-context. Both parties compile the
44
// same (deterministic) circuit and replay it in lockstep. C++20.
55

6-
#include "emp-sh2pc/emp-sh2pc.h" // NetIO, parse_party_and_port, SH2PCSession
6+
#include "emp-sh2pc/emp-sh2pc.h" // NetIO, parse_party, SH2PCSession
77
#include "emp-tool/circuits/frontend/circuit_fn.h" // frontend::compile / run
88
#include "emp-tool/circuits/frontend/rec.h" // rec::UInt / rec::Float shapes
99
#include <cstdint>
@@ -17,10 +17,11 @@ using F32 = Float_T<SH2PCSession::ctx_t, 32>;
1717

1818
int main(int argc, char** argv) {
1919
int port, party;
20-
parse_party_and_port(argv, &party, &port);
21-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
20+
party = parse_party(argv);
21+
port = peer_port();
22+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
2223

23-
SH2PCSession sess(&io, party);
24+
SH2PCSession sess(io.get(), party);
2425
int fails = 0;
2526

2627
// Compile once, host-side (no protocol, no I/O): pure circuit functions.

test/test_context_sh2pc.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// input<T>()/reveal<T>() own the OT. Demonstrates a templated integer kernel and
55
// an IR-replay float builtin. C++20.
66

7-
#include "emp-sh2pc/emp-sh2pc.h" // NetIO, parse_party_and_port, ALICE/BOB
7+
#include "emp-sh2pc/emp-sh2pc.h" // NetIO, parse_party, ALICE/BOB
88
#include "emp-sh2pc/sh2pc_session.h" // SH2PCSession
99
#include "emp-tool/circuits/typed.h"
1010
#include <cstdint>
@@ -14,10 +14,11 @@ using namespace emp;
1414

1515
int main(int argc, char** argv) {
1616
int port, party;
17-
parse_party_and_port(argv, &party, &port);
18-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
17+
party = parse_party(argv);
18+
port = peer_port();
19+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
1920

20-
SH2PCSession sess(&io, party); // owns crypto/IO/OT/Delta — no global backend
21+
SH2PCSession sess(io.get(), party); // owns crypto/IO/OT/Delta — no global backend
2122
int fails = 0;
2223

2324
// 1) keep-templated kernel: UInt32 add (ALICE owns a, BOB owns b).

test/test_example.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ void test_millionare(SH2PCSession& sess, int number) {
1818

1919
int main(int argc, char** argv) {
2020
int port, party;
21-
parse_party_and_port(argv, &party, &port);
21+
party = parse_party(argv);
22+
port = peer_port();
2223
int num = 20;
23-
if (argc > 3) num = atoi(argv[3]);
24-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
24+
if (argc > 2) num = atoi(argv[2]);
25+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
2526

26-
SH2PCSession sess(&io, party);
27+
SH2PCSession sess(io.get(), party);
2728
test_millionare(sess, num);
2829
cout << sess.num_and() << endl;
2930
sess.finalize();

test/test_float.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,24 @@ int fp_checks(SH2PCSession& sess) {
148148

149149
int main(int argc, char** argv) {
150150
int port, party;
151-
parse_party_and_port(argv, &party, &port);
152-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
151+
party = parse_party(argv);
152+
port = peer_port();
153+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
153154

154155
// Agree on the PRG seed before the session opens so matched inputs are not
155156
// folded into the garbled-circuit transcript.
156157
block test_seed;
157158
if (party == ALICE) {
158159
PRG().random_block(&test_seed, 1);
159-
io.send_data(&test_seed, sizeof(block));
160+
io->send_data(&test_seed, sizeof(block));
160161
} else {
161-
io.recv_data(&test_seed, sizeof(block));
162+
io->recv_data(&test_seed, sizeof(block));
162163
}
163-
io.flush();
164+
io->flush();
164165
PRG prg(&test_seed);
165166

166167
// Larger BOB-input COT batch keeps repeated float ops off the refill path.
167-
SH2PCSession sess(&io, party, 1024 * 1024);
168+
SH2PCSession sess(io.get(), party, 1024 * 1024);
168169

169170
int fails = 0;
170171
fails += fp_checks(sess);

test/test_int.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ int test_int(SH2PCSession& sess, int runs = kRuns) {
4444

4545
int main(int argc, char** argv) {
4646
int port, party;
47-
parse_party_and_port(argv, &party, &port);
48-
NetIO io(party == ALICE ? nullptr : "127.0.0.1", port);
47+
party = parse_party(argv);
48+
port = peer_port();
49+
auto io = (party == ALICE) ? NetIO::listen(port) : NetIO::connect(peer_ip(), port);
4950

5051
// Agree on the PRG seed for matched test inputs, before the session opens
5152
// so it isn't folded into the garbled-circuit transcript.
5253
if (party == ALICE) {
5354
PRG().random_block(&test_seed, 1);
54-
io.send_data(&test_seed, sizeof(block));
55+
io->send_data(&test_seed, sizeof(block));
5556
} else {
56-
io.recv_data(&test_seed, sizeof(block));
57+
io->recv_data(&test_seed, sizeof(block));
5758
}
58-
io.flush();
59+
io->flush();
5960

60-
SH2PCSession sess(&io, party);
61+
SH2PCSession sess(io.get(), party);
6162

6263
int fails = 0;
6364
fails += test_int<std::plus<uint32_t>, std::plus<U32>>(sess);

test/test_repeat.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ static int test_int_reveal(int party, int port, int number) {
1313
// Brief pause so the previous iteration's listener has fully released the
1414
// port before this one re-binds it (same-port sequential reconnects).
1515
usleep(100);
16-
NetIO netio(party == ALICE ? nullptr : "127.0.0.1", port, true);
17-
SH2PCSession sess(&netio, party, 1024);
16+
auto netio = (party == ALICE) ? NetIO::listen(port, true) : NetIO::connect(peer_ip(), port, true);
17+
SH2PCSession sess(netio.get(), party, 1024);
1818

1919
SI a = sess.input<SI>(ALICE, (int64_t)number);
2020
SI b;
@@ -32,7 +32,8 @@ static int test_int_reveal(int party, int port, int number) {
3232

3333
int main(int argc, char** argv) {
3434
int party, port;
35-
parse_party_and_port(argv, &party, &port);
35+
party = parse_party(argv);
36+
port = peer_port();
3637

3738
int fails = 0;
3839
for (int i = 0; i < 100; ++i)

0 commit comments

Comments
 (0)