-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathconversion.cc
200 lines (163 loc) · 6.31 KB
/
conversion.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
// 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/conversion.h"
#include "libspu/core/trace.h"
#include "libspu/core/vectorize.h"
#include "libspu/mpc/ab_api.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 {
static NdArrayRef wrap_add_bb(SPUContext* ctx, const NdArrayRef& x,
const NdArrayRef& y) {
SPU_ENFORCE(x.shape() == y.shape());
return UnwrapValue(add_bb(ctx, WrapValue(x), WrapValue(y)));
}
static NdArrayRef wrap_a2b(SPUContext* ctx, const NdArrayRef& x) {
return UnwrapValue(a2b(ctx, WrapValue(x)));
}
NdArrayRef A2B::proc(KernelEvalContext* ctx, const NdArrayRef& x) const {
const auto field = x.eltype().as<Ring2k>()->field();
auto* comm = ctx->getState<Communicator>();
auto* prg_state = ctx->getState<PrgState>();
std::vector<NdArrayRef> bshrs;
const auto bty = makeType<BShrTy>(field);
for (size_t idx = 0; idx < comm->getWorldSize(); idx++) {
auto [r0, r1] =
prg_state->genPrssPair(field, x.shape(), PrgState::GenPrssCtrl::Both);
auto b = ring_xor(r0, r1).as(bty);
if (idx == comm->getRank()) {
ring_xor_(b, x);
}
bshrs.push_back(b.as(bty));
}
NdArrayRef res = vreduce(bshrs.begin(), bshrs.end(),
[&](const NdArrayRef& xx, const NdArrayRef& yy) {
return wrap_add_bb(ctx->sctx(), xx, yy);
});
return res.as(bty);
}
NdArrayRef B2A::proc(KernelEvalContext* ctx, const NdArrayRef& x) const {
const auto field = x.eltype().as<Ring2k>()->field();
auto* comm = ctx->getState<Communicator>();
auto* prg_state = ctx->getState<PrgState>();
auto r_v = prg_state->genPriv(field, x.shape());
auto r_a = r_v.as(makeType<AShrTy>(field));
// convert r to boolean share.
auto r_b = wrap_a2b(ctx->sctx(), r_a);
// evaluate adder circuit on x & r, and reveal x+r
auto x_plus_r = comm->allReduce(ReduceOp::XOR,
wrap_add_bb(ctx->sctx(), x, r_b), kBindName);
// compute -r + (x+r)
ring_neg_(r_a);
if (comm->getRank() == 0) {
ring_add_(r_a, x_plus_r);
}
return r_a;
}
NdArrayRef B2A_Randbit::proc(KernelEvalContext* ctx,
const NdArrayRef& x) const {
const auto field = x.eltype().as<Ring2k>()->field();
auto* comm = ctx->getState<Communicator>();
auto* beaver = ctx->getState<Semi2kState>()->beaver();
const int64_t nbits = x.eltype().as<BShare>()->nbits();
SPU_ENFORCE((size_t)nbits <= SizeOf(field) * 8, "invalid nbits={}", nbits);
if (nbits == 0) {
// special case, it's known to be zero.
return ring_zeros(field, x.shape()).as(makeType<AShrTy>(field));
}
auto numel = x.numel();
auto randbits = beaver->RandBit(field, {numel * static_cast<int64_t>(nbits)});
auto res = NdArrayRef(makeType<AShrTy>(field), x.shape());
DISPATCH_ALL_FIELDS(field, kBindName, [&]() {
using U = ring2k_t;
NdArrayView<U> _randbits(randbits);
NdArrayView<U> _x(x);
// algorithm begins.
// Ref: III.D @ https://eprint.iacr.org/2019/599.pdf (SPDZ-2K primitives)
std::vector<U> x_xor_r(numel);
pforeach(0, numel, [&](int64_t idx) {
// use _r[i*nbits, (i+1)*nbits) to construct rb[i]
U mask = 0;
for (int64_t bit = 0; bit < nbits; ++bit) {
mask += (_randbits[idx * nbits + bit] & 0x1) << bit;
}
x_xor_r[idx] = _x[idx] ^ mask;
});
// open c = x ^ r
x_xor_r = comm->allReduce<U, std::bit_xor>(x_xor_r, "open(x^r)");
NdArrayView<U> _res(res);
pforeach(0, numel, [&](int64_t idx) {
_res[idx] = 0;
for (int64_t bit = 0; bit < nbits; bit++) {
auto c_i = (x_xor_r[idx] >> bit) & 0x1;
if (comm->getRank() == 0) {
_res[idx] += (c_i + (1 - c_i * 2) * _randbits[idx * nbits + bit])
<< bit;
} else {
_res[idx] += ((1 - c_i * 2) * _randbits[idx * nbits + bit]) << bit;
}
}
});
});
return res;
}
NdArrayRef MsbA2B::proc(KernelEvalContext* ctx, const NdArrayRef& in) const {
const auto field = in.eltype().as<Ring2k>()->field();
auto* comm = ctx->getState<Communicator>();
auto* prg_state = ctx->getState<PrgState>();
// For if k > 2 parties does not collude with each other, then we can
// construct two additive share and use carray out circuit directly.
SPU_ENFORCE(comm->getWorldSize() == 2, "only support for 2PC, got={}",
comm->getWorldSize());
std::vector<NdArrayRef> bshrs;
const auto bty = makeType<BShrTy>(field);
for (size_t idx = 0; idx < comm->getWorldSize(); idx++) {
auto [r0, r1] =
prg_state->genPrssPair(field, in.shape(), PrgState::GenPrssCtrl::Both);
auto b = ring_xor(r0, r1).as(bty);
if (idx == comm->getRank()) {
ring_xor_(b, in);
}
bshrs.push_back(b.as(bty));
}
// Compute the k-1'th carry bit.
size_t k = SizeOf(field) * 8 - 1;
if (in.numel() == 0) {
k = 0; // Empty matrix
}
auto* sctx = ctx->sctx();
const Shape shape = {in.numel()};
auto m = WrapValue(bshrs[0]);
auto n = WrapValue(bshrs[1]);
{
auto carry = carry_a2b(sctx, m, n, k);
// Compute the k'th bit.
// (m^n)[k] ^ carry
auto msb = xor_bb(sctx, rshift_b(sctx, xor_bb(sctx, m, n), k), carry);
return UnwrapValue(msb);
}
}
void CommonTypeV::evaluate(KernelEvalContext* ctx) const {
const Type& lhs = ctx->getParam<Type>(0);
const Type& rhs = ctx->getParam<Type>(1);
SPU_TRACE_MPC_DISP(ctx, lhs, rhs);
const auto* lhs_v = lhs.as<Priv2kTy>();
const auto* rhs_v = rhs.as<Priv2kTy>();
ctx->setOutput(makeType<AShrTy>(std::max(lhs_v->field(), rhs_v->field())));
}
} // namespace spu::mpc::semi2k