forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.cc
114 lines (99 loc) · 3.78 KB
/
io.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
// 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/io.h"
#include "libspu/mpc/cheetah/type.h"
#include "libspu/mpc/common/pv2k.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::cheetah {
Type CheetahIo::getShareType(Visibility vis, int owner_rank) const {
if (vis == VIS_PUBLIC) {
return makeType<Pub2kTy>(field_);
} else if (vis == VIS_SECRET) {
if (owner_rank >= 0 && owner_rank < static_cast<int>(world_size_)) {
return makeType<Priv2kTy>(field_, owner_rank);
} else {
return makeType<AShrTy>(field_);
}
}
SPU_THROW("unsupported vis type {}", vis);
}
std::vector<NdArrayRef> CheetahIo::toShares(const NdArrayRef& raw,
Visibility vis,
int owner_rank) const {
SPU_ENFORCE(raw.eltype().isa<RingTy>(), "expected RingTy, got {}",
raw.eltype());
const auto field = raw.eltype().as<Ring2k>()->field();
SPU_ENFORCE(field == field_, "expect raw value encoded in field={}, got={}",
field_, field);
if (vis == VIS_PUBLIC) {
const auto share = raw.as(makeType<Pub2kTy>(field));
return std::vector<NdArrayRef>(world_size_, share);
} else if (vis == VIS_SECRET) {
if (owner_rank >= 0 && owner_rank < static_cast<int>(world_size_)) {
// indicates private
std::vector<NdArrayRef> shares;
const auto ty = makeType<Priv2kTy>(field, owner_rank);
for (int idx = 0; idx < static_cast<int>(world_size_); idx++) {
if (idx == owner_rank) {
shares.push_back(raw.as(ty));
} else {
shares.push_back(makeConstantArrayRef(ty, raw.shape()));
}
}
return shares;
} else {
// normal secret
SPU_ENFORCE(owner_rank == -1, "not a valid owner {}", owner_rank);
std::vector<NdArrayRef> shares;
const auto ty = makeType<AShrTy>(field);
// by default, make as arithmetic share.
const auto splits = ring_rand_additive_splits(raw, world_size_);
shares.reserve(splits.size());
for (const auto& split : splits) {
shares.emplace_back(split.as(ty));
}
return shares;
}
}
SPU_THROW("unsupported vis type {}", vis);
}
NdArrayRef CheetahIo::fromShares(const std::vector<NdArrayRef>& shares) const {
const auto& eltype = shares.at(0).eltype();
const auto field = eltype.as<Ring2k>()->field();
if (eltype.isa<Public>()) {
return shares[0].as(makeType<RingTy>(field));
} else if (eltype.isa<Priv2kTy>()) {
SPU_ENFORCE(field_ == eltype.as<Ring2k>()->field());
const size_t owner = eltype.as<Private>()->owner();
return shares[owner].as(makeType<RingTy>(field_));
} else if (eltype.isa<Secret>()) {
auto res = ring_zeros(field, shares[0].shape());
for (const auto& share : shares) {
if (eltype.isa<AShare>()) {
ring_add_(res, share);
} else if (eltype.isa<BShare>()) {
ring_xor_(res, share);
} else {
SPU_THROW("invalid share type {}", eltype);
}
}
return res;
}
SPU_THROW("unsupported eltype {}", eltype);
}
std::unique_ptr<CheetahIo> makeCheetahIo(FieldType field, size_t npc) {
cheetah::registerTypes();
return std::make_unique<CheetahIo>(field, npc);
}
} // namespace spu::mpc::cheetah