-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cc
More file actions
120 lines (102 loc) · 3.9 KB
/
state.cc
File metadata and controls
120 lines (102 loc) · 3.9 KB
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
// 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/state.h"
#include <future>
#include "libspu/core/context.h"
#include "libspu/core/ndarray_ref.h"
#include "libspu/core/prelude.h"
#include "libspu/mpc/utils/ring_ops.h"
namespace spu::mpc::cheetah {
void CheetahMulState::makeSureCacheSize(FieldType field, int64_t numel) {
// NOTE(juhou): make sure the lock is obtained
SPU_ENFORCE(numel > 0);
if (field_ != field) {
// drop all previous cache
cached_sze_ = 0;
}
if (cached_sze_ >= numel) {
return;
}
// create one batch OLE which then converted to Beavers
// Math:
// Alice samples rand0 and views it as rand0 = a0||b0
// Bob samples rand1 and views it as rand1 = b1||a0
// The multiplication rand0 * rand1 gives two cross term a0*b1||a1*b0
// Then the beaver (a0, b0, c0) and (a1, b1, c1)
// where c0 = a0*b0 + <a0*b1> + <a1*b0>
// c1 = a1*b1 + <a0*b1> + <a1*b0>
mul_prot_->LazyInitKeys(field);
const int rank = mul_prot_->Rank();
const int64_t ole_sze = mul_prot_->OLEBatchSize();
const int64_t num_ole = CeilDiv<size_t>(2 * numel, ole_sze);
const int64_t num_beaver = (num_ole * ole_sze) / 2;
auto rand = ring_rand(field, {num_ole * ole_sze});
auto cross = mul_prot_->MulOLE(rand, rank == 0);
NdArrayRef beaver[3];
NdArrayRef a0b1;
NdArrayRef a1b0;
if (rank == 0) {
beaver[0] = rand.slice({0}, {num_beaver}, {1});
beaver[1] = rand.slice({num_beaver}, {num_beaver * 2}, {1});
} else {
beaver[0] = rand.slice({num_beaver}, {num_beaver * 2}, {1});
beaver[1] = rand.slice({0}, {num_beaver}, {1});
}
a0b1 = cross.slice({0}, {num_beaver}, {1});
a1b0 = cross.slice({num_beaver}, {num_beaver * 2}, {1});
beaver[2] =
ring_add(ring_add(cross.slice({0}, {num_beaver}, {1}),
cross.slice({num_beaver}, {2 * num_beaver}, {1})),
ring_mul(beaver[0], beaver[1]));
DISPATCH_ALL_FIELDS(field, [&]() {
for (size_t i : {0, 1, 2}) {
auto tmp = ring_zeros(field, {num_beaver + cached_sze_});
NdArrayView<ring2k_t> new_cache(tmp);
// concate two array
if (cached_sze_ > 0) {
NdArrayView<const ring2k_t> old_cache(cached_beaver_[i]);
pforeach(0, cached_sze_,
[&](int64_t j) { new_cache[j] = old_cache[j]; });
}
NdArrayView<const ring2k_t> _beaver(beaver[i]);
pforeach(0, num_beaver,
[&](int64_t j) { new_cache[cached_sze_ + j] = _beaver[j]; });
cached_beaver_[i] = tmp;
}
});
field_ = field;
cached_sze_ += num_beaver;
SPU_ENFORCE(cached_sze_ >= numel);
}
std::array<NdArrayRef, 3> CheetahMulState::TakeCachedBeaver(FieldType field,
int64_t numel) {
SPU_ENFORCE(numel > 0);
std::unique_lock guard(lock_);
makeSureCacheSize(field, numel);
std::array<NdArrayRef, 3> ret;
for (size_t i : {0, 1, 2}) {
SPU_ENFORCE(cached_beaver_[i].numel() >= numel);
ret[i] = cached_beaver_[i].slice({0}, {numel}, {1});
if (cached_sze_ == numel) {
// empty cache now
// NOTE(lwj): should use `{0}` as empty while `{}` is a scalar
cached_beaver_[i] = NdArrayRef(cached_beaver_[i].eltype(), {0});
} else {
cached_beaver_[i] = cached_beaver_[i].slice({numel}, {cached_sze_}, {1});
}
}
cached_sze_ -= numel;
return ret;
}
} // namespace spu::mpc::cheetah