-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrspsi_tests.cc
179 lines (144 loc) · 4.89 KB
/
rspsi_tests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "test/rspsi_tests.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <time.h>
#include <iostream>
#include "psi/vole/psi/rspsi.h"
// #include "volePSI/RsCpsi.h"
#include "cryptoTools/Network/Channel.h"
#include "cryptoTools/Network/IOService.h"
#include "cryptoTools/Network/Session.h"
#include "network/mem_channel.h"
using primihub::link::Channel;
using primihub::link::MemoryChannel;
using ChannelRole = MemoryChannel::ChannelRole;
using namespace oc;
namespace primihub::crypto {
std::vector<u64> run(PRNG& prng, std::vector<block>& recvSet,
std::vector<block>& sendSet, bool mal,
std::string taskname, u64 nt = 1, bool reduced = false) {
RsPsiReceiver recver;
RsPsiSender sender;
recver.init(sendSet.size(), recvSet.size(), 40, prng.get(), mal, nt, reduced);
sender.init(sendSet.size(), recvSet.size(), 40, prng.get(), mal, nt, reduced);
std::shared_ptr<MemoryChannel> channel_impl1 =
std::make_shared<MemoryChannel>(ChannelRole::CLIENT);
std::shared_ptr<Channel> channel1 =
std::make_shared<Channel>(channel_impl1, taskname);
std::shared_ptr<MemoryChannel> channel_impl2 =
std::make_shared<MemoryChannel>(ChannelRole::SERVER);
std::shared_ptr<Channel> channel2 =
std::make_shared<Channel>(channel_impl2, taskname);
auto p0_recv = [&recver, &recvSet, &channel1]() {
recver.run(recvSet, channel1);
};
auto p1_send = [&sender, &sendSet, &channel2]() {
sender.run(sendSet, channel2);
};
auto p0_task = std::async(p0_recv);
auto p1_task = std::async(p1_send);
std::clock_t start = clock();
p0_task.get();
p1_task.get();
std::clock_t end = clock();
std::cout << taskname << "cost " << (double)(end - start) / CLOCKS_PER_SEC << "sec." << std::endl;
return recver.mIntersection;
}
TEST(RsPsiTest, EmptyTest) {
std::string taskname = "EmptyTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
prng.get(sendSet.data(), sendSet.size());
auto inter = run(prng, recvSet, sendSet, false, taskname);
if (inter.size()) throw RTE_LOC;
}
TEST(RsPsiTest, PartialTest) {
std::string taskname = "PartialTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
prng.get(sendSet.data(), sendSet.size());
std::set<u64> exp;
for (u64 i = 0; i < n; ++i) {
if (prng.getBit()) {
recvSet[i] = sendSet[(i + 312) % n];
exp.insert(i);
}
}
auto inter = run(prng, recvSet, sendSet, false, taskname);
std::set<u64> act(inter.begin(), inter.end());
if (act != exp) {
std::cout << "exp size " << exp.size() << std::endl;
std::cout << "act size " << act.size() << std::endl;
throw RTE_LOC;
}
}
TEST(RsPsiTest, FullTest) {
std::string taskname = "FullTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
sendSet = recvSet;
std::set<u64> exp;
for (u64 i = 0; i < n; ++i) exp.insert(i);
auto inter = run(prng, recvSet, sendSet, false, taskname);
std::set<u64> act(inter.begin(), inter.end());
if (act != exp) throw RTE_LOC;
}
TEST(RsPsiTest, ReducedTest) {
std::string taskname = "ReducedTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
sendSet = recvSet;
std::set<u64> exp;
for (u64 i = 0; i < n; ++i) exp.insert(i);
auto inter = run(prng, recvSet, sendSet, false, taskname, 1, true);
std::set<u64> act(inter.begin(), inter.end());
if (act != exp) throw RTE_LOC;
}
TEST(RsPsiTest, MultiThrdTest) {
std::string taskname = "MultiThrdTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
// u64 nt = cmd.getOr("nt", 8);
u64 nt = 8;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
sendSet = recvSet;
std::set<u64> exp;
for (u64 i = 0; i < n; ++i) exp.insert(i);
auto inter = run(prng, recvSet, sendSet, false, taskname, nt);
std::set<u64> act(inter.begin(), inter.end());
if (act != exp) throw RTE_LOC;
}
TEST(RsPsiTest, MalTest) {
std::string taskname = "MalTest";
// u64 n = cmd.getOr("n", 13243);
u64 n = 1000000;
std::vector<block> recvSet(n), sendSet(n);
PRNG prng(ZeroBlock);
prng.get(recvSet.data(), recvSet.size());
prng.get(sendSet.data(), sendSet.size());
std::set<u64> exp;
for (u64 i = 0; i < n; ++i) {
if (prng.getBit()) {
recvSet[i] = sendSet[(i + 312) % n];
exp.insert(i);
}
}
auto inter = run(prng, recvSet, sendSet, true, taskname);
std::set<u64> act(inter.begin(), inter.end());
if (act != exp) throw RTE_LOC;
}
} // namespace primihub::crypto