forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_semi2k.cc
153 lines (128 loc) · 5.01 KB
/
arithmetic_semi2k.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
// 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/cheetah/arithmetic.h"
#include "libspu/mpc/cheetah/state.h"
#include "libspu/mpc/cheetah/type.h"
#include "libspu/mpc/common/communicator.h"
#include "libspu/mpc/common/prg_state.h"
#include "libspu/mpc/common/pv2k.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::cheetah {
/// Kernels that identical to semi2k ///
NdArrayRef RandA::proc(KernelEvalContext* ctx, const Shape& shape) const {
auto* prg_state = ctx->getState<PrgState>();
const auto field = ctx->getState<Z2kState>()->getDefaultField();
return ring_rshift(prg_state->genPriv(field, shape), 2)
.as(makeType<AShrTy>(field));
}
NdArrayRef P2A::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_sub(r0, r1).as(makeType<AShrTy>(field));
if (comm->getRank() == 0) {
ring_add_(x, in);
}
return x.as(makeType<AShrTy>(field));
}
NdArrayRef A2P::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::ADD, in, kBindName);
return out.as(makeType<Pub2kTy>(field));
}
NdArrayRef A2V::proc(KernelEvalContext* ctx, const NdArrayRef& in,
size_t rank) const {
auto* comm = ctx->getState<Communicator>();
const auto field = in.eltype().as<AShrTy>()->field();
auto out_ty = makeType<Priv2kTy>(field, rank);
auto numel = in.numel();
return DISPATCH_ALL_FIELDS(field, "_", [&]() {
std::vector<ring2k_t> share(numel);
NdArrayView<ring2k_t> _in(in);
pforeach(0, numel, [&](int64_t idx) { share[idx] = _in[idx]; });
std::vector<std::vector<ring2k_t>> shares =
comm->gather<ring2k_t>(share, rank, "a2v"); // comm => 1, k
if (comm->getRank() == rank) {
SPU_ENFORCE(shares.size() == comm->getWorldSize());
NdArrayRef out(out_ty, in.shape());
NdArrayView<ring2k_t> _out(out);
pforeach(0, numel, [&](int64_t idx) {
ring2k_t s = 0;
for (auto& share : shares) {
s += share[idx];
}
_out[idx] = s;
});
return out;
} else {
return makeConstantArrayRef(out_ty, in.shape());
}
});
}
NdArrayRef V2A::proc(KernelEvalContext* ctx, const NdArrayRef& in) const {
const auto* in_ty = in.eltype().as<Priv2kTy>();
const size_t owner_rank = in_ty->owner();
const auto field = in_ty->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_sub(r0, r1).as(makeType<AShrTy>(field));
if (comm->getRank() == owner_rank) {
ring_add_(x, in);
}
return x.as(makeType<AShrTy>(field));
}
NdArrayRef NotA::proc(KernelEvalContext* ctx, const NdArrayRef& in) const {
auto* comm = ctx->getState<Communicator>();
auto res = ring_neg(in);
if (comm->getRank() == 0) {
const auto field = in.eltype().as<Ring2k>()->field();
ring_add_(res, ring_not(ring_zeros(field, in.shape())));
}
return res.as(in.eltype());
}
NdArrayRef AddAP::proc(KernelEvalContext* ctx, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.numel() == rhs.numel());
auto* comm = ctx->getState<Communicator>();
if (comm->getRank() == 0) {
return ring_add(lhs, rhs).as(lhs.eltype());
}
return lhs;
}
NdArrayRef AddAA::proc(KernelEvalContext*, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
SPU_ENFORCE(lhs.numel() == rhs.numel());
SPU_ENFORCE(lhs.eltype() == rhs.eltype());
return ring_add(lhs, rhs).as(lhs.eltype());
}
NdArrayRef MulAP::proc(KernelEvalContext*, const NdArrayRef& lhs,
const NdArrayRef& rhs) const {
return ring_mul(lhs, rhs).as(lhs.eltype());
}
NdArrayRef MatMulAP::proc(KernelEvalContext*, const NdArrayRef& x,
const NdArrayRef& y) const {
return ring_mmul(x, y).as(x.eltype());
}
NdArrayRef LShiftA::proc(KernelEvalContext*, const NdArrayRef& in,
size_t bits) const {
const auto field = in.eltype().as<Ring2k>()->field();
bits %= SizeOf(field) * 8;
return ring_lshift(in, bits).as(in.eltype());
}
} // namespace spu::mpc::cheetah