forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean.cc
301 lines (238 loc) · 9.26 KB
/
boolean.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2021 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "libspu/mpc/semi2k/boolean.h"
#include <functional>
#include "libspu/core/bit_utils.h"
#include "libspu/mpc/common/communicator.h"
#include "libspu/mpc/common/prg_state.h"
#include "libspu/mpc/common/pv2k.h"
#include "libspu/mpc/semi2k/state.h"
#include "libspu/mpc/semi2k/type.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::semi2k {
namespace {
size_t getNumBits(const NdArrayRef& in) {
if (in.eltype().isa<Pub2kTy>()) {
const auto field = in.eltype().as<Pub2kTy>()->field();
return DISPATCH_ALL_FIELDS(field, "_",
[&]() { return maxBitWidth<ring2k_t>(in); });
} else if (in.eltype().isa<BShrTy>()) {
return in.eltype().as<BShrTy>()->nbits();
} else {
SPU_THROW("should not be here, {}", in.eltype());
}
}
NdArrayRef makeBShare(const NdArrayRef& r, FieldType field, size_t nbits) {
const auto ty = makeType<BShrTy>(field, nbits);
return r.as(ty);
}
// TODO: DRY
PtType getBacktype(size_t nbits) {
if (nbits <= 8) {
return PT_U8;
}
if (nbits <= 16) {
return PT_U16;
}
if (nbits <= 32) {
return PT_U32;
}
if (nbits <= 64) {
return PT_U64;
}
if (nbits <= 128) {
return PT_U128;
}
SPU_THROW("invalid number of bits={}", nbits);
}
} // namespace
void CommonTypeB::evaluate(KernelEvalContext* ctx) const {
const Type& lhs = ctx->getParam<Type>(0);
const Type& rhs = ctx->getParam<Type>(1);
SPU_ENFORCE(lhs == rhs, "semi2k always use same bshare type, lhs={}, rhs={}",
lhs, rhs);
ctx->setOutput(lhs);
}
NdArrayRef CastTypeB::proc(KernelEvalContext*, const NdArrayRef& in,
const Type& to_type) const {
SPU_ENFORCE(in.eltype() == to_type,
"semi2k always use same bshare type, lhs={}, rhs={}", in.eltype(),
to_type);
return in;
}
NdArrayRef B2P::proc(KernelEvalContext* ctx, const NdArrayRef& in) const {
const auto field = in.eltype().as<Ring2k>()->field();
auto* comm = ctx->getState<Communicator>();
auto out = comm->allReduce(ReduceOp::XOR, in, kBindName);
return out.as(makeType<Pub2kTy>(field));
}
NdArrayRef P2B::proc(KernelEvalContext* ctx, const NdArrayRef& in) const {
const auto field = in.eltype().as<Ring2k>()->field();
auto* prg_state = ctx->getState<PrgState>();
auto* comm = ctx->getState<Communicator>();
auto [r0, r1] =
prg_state->genPrssPair(field, in.shape(), PrgState::GenPrssCtrl::Both);
auto x = ring_xor(r0, r1).as(makeType<BShrTy>(field, 0));
if (comm->getRank() == 0) {
ring_xor_(x, in);
}
return makeBShare(x, field, getNumBits(in));
}
NdArrayRef AndBP::proc(KernelEvalContext* ctx, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.shape() == rhs.shape());
const auto field = ctx->getState<Z2kState>()->getDefaultField();
const size_t out_nbits = std::min(getNumBits(lhs), getNumBits(rhs));
NdArrayRef out(makeType<BShrTy>(field, out_nbits), lhs.shape());
DISPATCH_ALL_FIELDS(field, "_", [&]() {
NdArrayView<ring2k_t> _lhs(lhs);
NdArrayView<ring2k_t> _rhs(rhs);
NdArrayView<ring2k_t> _out(out);
pforeach(0, lhs.numel(),
[&](int64_t idx) { _out[idx] = _lhs[idx] & _rhs[idx]; });
});
return out;
}
NdArrayRef AndBB::proc(KernelEvalContext* ctx, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.shape() == rhs.shape());
auto* comm = ctx->getState<Communicator>();
auto* beaver = ctx->getState<Semi2kState>()->beaver();
const auto field = ctx->getState<Z2kState>()->getDefaultField();
const size_t out_nbits = std::min(getNumBits(lhs), getNumBits(rhs));
const PtType backtype = getBacktype(out_nbits);
const int64_t numel = lhs.numel();
// semi2k always use the same storage type.
NdArrayRef out(makeType<BShrTy>(field, out_nbits), lhs.shape());
DISPATCH_ALL_FIELDS(field, "_", [&]() {
using T = ring2k_t;
NdArrayView<T> _lhs(lhs);
NdArrayView<T> _rhs(rhs);
DISPATCH_UINT_PT_TYPES(backtype, "_", [&]() {
using V = ScalarT;
// TODO: redefine beaver interface, generate variadic beaver and bits.
int64_t numBytes = numel * SizeOf(backtype);
int64_t numField = numBytes / SizeOf(field);
if (numBytes % SizeOf(field)) numField += 1;
auto [a, b, c] = beaver->And(field, {numField});
SPU_ENFORCE(a.buf()->size() >= static_cast<int64_t>(numBytes));
NdArrayView<V> _a(a);
NdArrayView<V> _b(b);
NdArrayView<V> _c(c);
// first half mask x^a, second half mask y^b.
std::vector<V> mask(numel * 2, 0);
pforeach(0, numel, [&](int64_t idx) {
mask[idx] = _lhs[idx] ^ _a[idx];
mask[numel + idx] = _rhs[idx] ^ _b[idx];
});
mask = comm->allReduce<V, std::bit_xor>(mask, "open(x^a,y^b)");
// Zi = Ci ^ ((X ^ A) & Bi) ^ ((Y ^ B) & Ai) ^ <(X ^ A) & (Y ^ B)>
NdArrayView<T> _z(out);
pforeach(0, numel, [&](int64_t idx) {
_z[idx] = _c[idx];
_z[idx] ^= mask[idx] & _b[idx];
_z[idx] ^= mask[numel + idx] & _a[idx];
if (comm->getRank() == 0) {
_z[idx] ^= mask[idx] & mask[numel + idx];
}
});
});
});
return out;
}
NdArrayRef XorBP::proc(KernelEvalContext* ctx, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.numel() == rhs.numel());
auto* comm = ctx->getState<Communicator>();
const auto field = lhs.eltype().as<Ring2k>()->field();
const size_t out_nbits = std::max(getNumBits(lhs), getNumBits(rhs));
if (comm->getRank() == 0) {
return makeBShare(ring_xor(lhs, rhs), field, out_nbits);
}
return makeBShare(lhs, field, out_nbits);
}
NdArrayRef XorBB::proc(KernelEvalContext* ctx, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.numel() == rhs.numel());
const auto field = ctx->getState<Z2kState>()->getDefaultField();
const size_t out_nbits = std::max(getNumBits(lhs), getNumBits(rhs));
return makeBShare(ring_xor(lhs, rhs), field, out_nbits);
}
NdArrayRef LShiftB::proc(KernelEvalContext*, const NdArrayRef& in,
size_t shift) const {
const auto field = in.eltype().as<Ring2k>()->field();
shift %= SizeOf(field) * 8;
size_t out_nbits = in.eltype().as<BShare>()->nbits() + shift;
out_nbits = std::clamp(out_nbits, static_cast<size_t>(0), SizeOf(field) * 8);
return makeBShare(ring_lshift(in, shift), field, out_nbits);
}
NdArrayRef RShiftB::proc(KernelEvalContext*, const NdArrayRef& in,
size_t shift) const {
const auto field = in.eltype().as<Ring2k>()->field();
shift %= SizeOf(field) * 8;
size_t nbits = in.eltype().as<BShare>()->nbits();
size_t out_nbits = nbits - std::min(nbits, shift);
SPU_ENFORCE(nbits <= SizeOf(field) * 8);
return makeBShare(ring_rshift(in, shift), field, out_nbits);
}
NdArrayRef ARShiftB::proc(KernelEvalContext*, const NdArrayRef& in,
size_t shift) const {
const auto field = in.eltype().as<Ring2k>()->field();
shift %= SizeOf(field) * 8;
// arithmetic right shift expects to work on ring, or the behaviour is
// undefined.
return makeBShare(ring_arshift(in, shift), field, SizeOf(field) * 8);
}
NdArrayRef BitrevB::proc(KernelEvalContext*, const NdArrayRef& in, size_t start,
size_t end) const {
const auto field = in.eltype().as<Ring2k>()->field();
SPU_ENFORCE(start <= end);
SPU_ENFORCE(end <= SizeOf(field) * 8);
const size_t out_nbits = std::max(getNumBits(in), end);
// TODO: more accurate bits.
return makeBShare(ring_bitrev(in, start, end), field, out_nbits);
}
NdArrayRef BitIntlB::proc(KernelEvalContext*, const NdArrayRef& in,
size_t stride) const {
const auto field = in.eltype().as<Ring2k>()->field();
const auto nbits = getNumBits(in);
SPU_ENFORCE(absl::has_single_bit(nbits));
NdArrayRef out(in.eltype(), in.shape());
auto numel = in.numel();
DISPATCH_ALL_FIELDS(field, "_", [&]() {
NdArrayView<ring2k_t> _in(in);
NdArrayView<ring2k_t> _out(out);
pforeach(0, numel, [&](int64_t idx) {
_out[idx] = BitIntl<ring2k_t>(_in[idx], stride, nbits);
});
});
return out;
}
NdArrayRef BitDeintlB::proc(KernelEvalContext*, const NdArrayRef& in,
size_t stride) const {
const auto field = in.eltype().as<Ring2k>()->field();
const auto nbits = getNumBits(in);
SPU_ENFORCE(absl::has_single_bit(nbits));
NdArrayRef out(in.eltype(), in.shape());
auto numel = in.numel();
DISPATCH_ALL_FIELDS(field, "_", [&]() {
NdArrayView<ring2k_t> _in(in);
NdArrayView<ring2k_t> _out(out);
pforeach(0, numel, [&](int64_t idx) {
_out[idx] = BitDeintl<ring2k_t>(_in[idx], stride, nbits);
});
});
return out;
}
} // namespace spu::mpc::semi2k