forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermute.cc
140 lines (115 loc) · 4.45 KB
/
permute.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
// Copyright 2023 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/permute.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/permute.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::semi2k {
namespace {
NdArrayRef wrap_a2v(SPUContext* ctx, const NdArrayRef& x, size_t rank) {
return UnwrapValue(a2v(ctx, WrapValue(x), rank));
}
inline bool isOwner(KernelEvalContext* ctx, const Type& type) {
auto* comm = ctx->getState<Communicator>();
return type.as<Priv2kTy>()->owner() == static_cast<int64_t>(comm->getRank());
}
inline int64_t getOwner(const NdArrayRef& x) {
return x.eltype().as<Priv2kTy>()->owner();
}
// Secure inverse permutation of x by perm_rank's permutation pv
// The idea here is:
// Input permutation pv, beaver generates perm pair {<A>, <B>} that
// InversePermute(A, pv) = B. So we can get <y> = InversePermute(open(<x> -
// <A>), pv) + <B> that y = InversePermute(x, pv).
NdArrayRef SecureInvPerm(KernelEvalContext* ctx, const NdArrayRef& x,
size_t perm_rank, absl::Span<const int64_t> pv) {
const auto lctx = ctx->lctx();
const auto field = x.eltype().as<AShrTy>()->field();
auto* beaver = ctx->getState<Semi2kState>()->beaver();
auto perm_pair = beaver->PermPair(field, x.shape(), perm_rank, pv);
auto t = wrap_a2v(ctx->sctx(), ring_sub(x, perm_pair.first).as(x.eltype()),
perm_rank);
if (lctx->Rank() == perm_rank) {
SPU_ENFORCE(pv.size());
ring_add_(perm_pair.second, applyInvPerm(t, pv));
return perm_pair.second.as(x.eltype());
} else {
return perm_pair.second.as(x.eltype());
}
}
} // namespace
NdArrayRef RandPermM::proc(KernelEvalContext* ctx, const Shape& shape) const {
NdArrayRef out(makeType<PShrTy>(), shape);
// generate a RandU64 as permutation seed
auto* prg_state = ctx->getState<PrgState>();
const auto seed = prg_state->genPriv(FieldType::FM64, {1});
NdArrayView<uint64_t> _seed(seed);
const auto perm_vector = genRandomPerm(out.numel(), _seed[0]);
const auto field = out.eltype().as<PShrTy>()->field();
DISPATCH_ALL_FIELDS(field, "_", [&]() {
NdArrayView<ring2k_t> _out(out);
pforeach(0, out.numel(),
[&](int64_t idx) { _out[idx] = ring2k_t(perm_vector[idx]); });
});
return out;
}
NdArrayRef PermAM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
auto* comm = ctx->getState<Communicator>();
PermVector pv = ring2pv(perm);
NdArrayRef out(in);
for (size_t i = 0; i < comm->getWorldSize(); ++i) {
out = SecureInvPerm(ctx, out, i, pv);
}
return out;
}
NdArrayRef PermAP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
PermVector pv = ring2pv(perm);
auto out = applyPerm(in, pv);
return out;
}
NdArrayRef InvPermAM::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
auto* comm = ctx->getState<Communicator>();
PermVector pv = ring2pv(perm);
NdArrayRef out(in);
auto inv_pv = genInversePerm(pv);
for (int i = comm->getWorldSize() - 1; i >= 0; --i) {
out = SecureInvPerm(ctx, out, i, inv_pv);
}
return out;
}
NdArrayRef InvPermAP::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
PermVector pv = ring2pv(perm);
auto out = applyInvPerm(in, pv);
return out;
}
NdArrayRef InvPermAV::proc(KernelEvalContext* ctx, const NdArrayRef& in,
const NdArrayRef& perm) const {
PermVector pv;
const auto lctx = ctx->lctx();
if (isOwner(ctx, perm.eltype())) {
pv = ring2pv(perm);
}
auto out = SecureInvPerm(ctx, in, getOwner(perm), pv);
return out;
}
} // namespace spu::mpc::semi2k