-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsse_filter.cpp
More file actions
90 lines (77 loc) · 3.08 KB
/
Copy pathsse_filter.cpp
File metadata and controls
90 lines (77 loc) · 3.08 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
#include "sse_filter.hpp"
SseFilterMsk SseFilter::msk_gen(const CharVec& key){
// Create the msk instance.
SseFilterMsk msk{};
msk.counter = 0;
msk.prf = std::make_unique<PRF>(key);
return msk;
}
CharMat SseFilter::enc(SseFilterMsk& msk, const Vec& x){
// Create the object to hold ct.
CharMat ct;
// Make convert each input to CharVec and digest.
std::visit([&ct, &msk](auto&& input_x){
using T = std::decay_t<decltype(input_x)>;
if constexpr (std::is_same_v<T, IntVec>){
for (const auto& each_x : input_x)
ct.push_back(msk.prf->digest(
Helper::str_to_char_vec(std::to_string(each_x) + std::to_string(msk.counter))
));
}
else if (std::is_same_v<T, StrVec>){
for (const auto& each_x : input_x)
ct.push_back(msk.prf->digest(
Helper::str_to_char_vec(each_x + std::to_string(msk.counter))
));
}
else throw std::invalid_argument("The input type is not supported.");
}, x);
// Increment the counter.
++msk.counter;
return ct;
}
CharMat SseFilter::keygen(const SseFilterMsk& msk, const Vec& y, const int row){
// Create the object to hold sk.
CharMat sk;
// Make convert each input to CharVec and digest.
std::visit([&sk, &msk, row](auto&& input_y){
using T = std::decay_t<decltype(input_y)>;
if constexpr (std::is_same_v<T, IntVec>){
for (int i = 0; i < row; ++i){
sk.push_back(msk.prf->digest(
Helper::str_to_char_vec(std::to_string(input_y[0]) + std::to_string(i))
));
for (int j = 1; j < input_y.size(); ++j){
CharVec temp = msk.prf->digest(
Helper::str_to_char_vec(std::to_string(input_y[j]) + std::to_string(i))
);
std::transform(
sk.back().begin(), sk.back().end(), temp.begin(), sk.back().begin(),
[](const unsigned char a, const unsigned char b){ return a ^ b; }
);
}
}
}
else if (std::is_same_v<T, StrVec>){
for (int i = 0; i < row; ++i){
sk.push_back(msk.prf->digest(
Helper::str_to_char_vec(input_y[0] + std::to_string(i))
));
for (int j = 1; j < input_y.size(); ++j){
CharVec temp = msk.prf->digest(
Helper::str_to_char_vec(input_y[j] + std::to_string(i))
);
std::transform(
sk.back().begin(), sk.back().end(), temp.begin(), sk.back().begin(),
[](const unsigned char a, const unsigned char b){ return a ^ b; }
);
}
}
}
else throw std::invalid_argument("The input type is not supported.");
}, y);
return sk;
}
bool SseFilter::dec(const CharMat& ct, const CharVec& sk){
return Helper::xor_char_vec(ct) == sk;
}