-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathbinary_fhe_stackable.cu
More file actions
242 lines (193 loc) · 5.55 KB
/
binary_fhe_stackable.cu
File metadata and controls
242 lines (193 loc) · 5.55 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF in CUDA C++ Core Libraries,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/**
* @file
* @brief A toy example to illustrate how we can compose logical operations over encrypted data
*/
#include <cuda/experimental/__stf/utility/stackable_ctx.cuh>
#include <cuda/experimental/stf.cuh>
using namespace cuda::experimental::stf;
#include <memory>
class ciphertext;
class plaintext
{
public:
plaintext(const stackable_ctx& ctx)
: ctx(ctx)
{}
plaintext(stackable_ctx& ctx, ::std::vector<char> v)
: values(mv(v))
, ctx(ctx)
, ld(ctx.logical_data(values.data(), values.size()))
{}
auto& set_symbol(const std::string& s)
{
ld.set_symbol(s);
symbol = s;
return *this;
}
const std::string& get_symbol() const
{
return symbol;
}
// This will asynchronously fill string s
void convert_to_vector(std::vector<char>& v)
{
ctx.host_launch(ld.read()).set_symbol("to_vector")->*[&](auto dl) {
v.resize(dl.size());
for (size_t i = 0; i < dl.size(); i++)
{
v[i] = dl(i);
}
};
}
ciphertext encrypt() const;
private:
std::vector<char> values;
mutable stackable_ctx ctx;
::std::string symbol;
public:
mutable stackable_logical_data<slice<char>> ld;
};
class ciphertext
{
public:
ciphertext() = default;
// We need a deep-copy semantic
ciphertext(const ciphertext& other)
: ctx(other.ctx)
, symbol(other.symbol)
{
copy_content(ctx, other, *this);
}
ciphertext(const stackable_ctx& ctx)
: ctx(ctx)
{}
ciphertext(ciphertext&&) = default;
ciphertext& operator=(ciphertext&&) = default;
static void copy_content(stackable_ctx& ctx, const ciphertext& src, ciphertext& dst)
{
dst.ld = ctx.logical_data(src.ld.shape());
ctx.parallel_for(src.ld.shape(), src.ld.read(), dst.ld.write()).set_symbol("copy")->*
[] __device__(size_t i, auto src, auto dst) {
dst(i) = src(i);
};
}
auto& set_symbol(std::string s)
{
ld.set_symbol(s);
symbol = mv(s);
return *this;
}
const std::string& get_symbol() const
{
return symbol;
}
plaintext decrypt() const
{
plaintext p(ctx);
p.ld = ctx.logical_data(shape_of<slice<char>>(ld.shape().size()));
ctx.parallel_for(ld.shape(), ld.read(), p.ld.write()).set_symbol("decrypt")->*
[] __device__(size_t i, auto cipher_data, auto plain_data) {
plain_data(i) = static_cast<char>(cipher_data(i) >> 32);
};
return p;
}
// Copy assignment operator
// We need a deep-copy semantic
ciphertext& operator=(const ciphertext& other)
{
if (this != &other)
{
fprintf(stderr, "CTX copy assignment (src = %s)\n", other.symbol.c_str());
ctx = other.ctx;
symbol = other.symbol;
copy_content(ctx, other, *this);
}
return *this;
}
ciphertext operator|(const ciphertext& other) const
{
ciphertext result(ctx);
result.ld = ctx.logical_data(ld.shape());
ctx.parallel_for(ld.shape(), ld.read(), other.ld.read(), result.ld.write()).set_symbol("OR")->*
[] __device__(size_t i, auto d_c1, auto d_c2, auto d_res) {
d_res(i) = d_c1(i) | d_c2(i);
};
return result;
}
ciphertext operator&(const ciphertext& other) const
{
ciphertext result(ctx);
result.ld = ctx.logical_data(ld.shape());
ctx.parallel_for(ld.shape(), ld.read(), other.ld.read(), result.ld.write()).set_symbol("AND")->*
[] __device__(size_t i, auto d_c1, auto d_c2, auto d_res) {
d_res(i) = d_c1(i) & d_c2(i);
};
return result;
}
ciphertext operator~() const
{
ciphertext result(ctx);
result.ld = ctx.logical_data(ld.shape());
ctx.parallel_for(ld.shape(), ld.read(), result.ld.write()).set_symbol("NOT")->*
[] __device__(size_t i, auto d_c, auto d_res) {
d_res(i) = ~d_c(i);
};
return result;
}
mutable stackable_logical_data<slice<uint64_t>> ld;
private:
mutable stackable_ctx ctx;
::std::string symbol;
};
ciphertext plaintext::encrypt() const
{
ciphertext c(ctx);
c.ld = ctx.logical_data(shape_of<slice<uint64_t>>(ld.shape().size()));
ctx.parallel_for(ld.shape(), ld.read(), c.ld.write()).set_symbol("encrypt")->*
[] __device__(size_t i, auto dptxt, auto dctxt) {
// A super safe encryption !
dctxt(i) = ((uint64_t) (dptxt(i)) << 32 | 0x4);
};
return c;
}
template <typename T>
T circuit(const T& a, const T& b)
{
return ~((a | ~b) & (~a | b));
}
int main()
{
stackable_ctx ctx;
const std::vector<char> vA{3, 3, 2, 2, 17};
plaintext pA(ctx, std::vector<char>(vA));
pA.set_symbol("A");
const std::vector<char> vB{1, 7, 7, 7, 49};
plaintext pB(ctx, std::vector<char>(vB));
pB.set_symbol("B");
auto s_encrypt = ctx.dot_section("encrypt");
auto eA = pA.encrypt().set_symbol("A");
auto eB = pB.encrypt().set_symbol("B");
s_encrypt.end();
ctx.push();
auto s_circuit = ctx.dot_section("circuit");
auto out = circuit(eA, eB);
s_circuit.end();
ctx.pop();
std::vector<char> v_out;
out.decrypt().convert_to_vector(v_out);
ctx.finalize();
for (size_t i = 0; i < v_out.size(); i++)
{
char expected = circuit(vA[i], vB[i]);
EXPECT(expected == v_out[i]);
}
}