-
Notifications
You must be signed in to change notification settings - Fork 189
/
boolean-serial-json.cpp
168 lines (126 loc) · 5.88 KB
/
boolean-serial-json.cpp
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
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: [email protected]
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
/*
Example for FHEW with JSON serialization
*/
#include "binfhecontext-ser.h"
using namespace lbcrypto;
// path where files will be written to
const std::string DATAFOLDER = "demoData";
int main() {
// Generating the crypto context
auto cc1 = BinFHEContext();
cc1.GenerateBinFHEContext(TOY);
std::cout << "Generating keys." << std::endl;
// Generating the secret key
auto sk1 = cc1.KeyGen();
// Generate the bootstrapping keys
cc1.BTKeyGen(sk1);
std::cout << "Done generating all keys." << std::endl;
// Encryption for a ciphertext that will be serialized
auto ct1 = cc1.Encrypt(sk1, 1);
// CODE FOR SERIALIZATION
// Serializing key-independent crypto context
if (!Serial::SerializeToFile(DATAFOLDER + "/cryptoContext.txt", cc1, SerType::JSON)) {
std::cerr << "Error serializing the cryptocontext" << std::endl;
return 1;
}
std::cout << "The cryptocontext has been serialized." << std::endl;
// Serializing refreshing and key switching keys (needed for bootstrapping)
if (!Serial::SerializeToFile(DATAFOLDER + "/refreshKey.txt", cc1.GetRefreshKey(), SerType::JSON)) {
std::cerr << "Error serializing the refreshing key" << std::endl;
return 1;
}
std::cout << "The refreshing key has been serialized." << std::endl;
if (!Serial::SerializeToFile(DATAFOLDER + "/ksKey.txt", cc1.GetSwitchKey(), SerType::JSON)) {
std::cerr << "Error serializing the switching key" << std::endl;
return 1;
}
std::cout << "The key switching key has been serialized." << std::endl;
// Serializing private keys
if (!Serial::SerializeToFile(DATAFOLDER + "/sk1.txt", sk1, SerType::JSON)) {
std::cerr << "Error serializing sk1" << std::endl;
return 1;
}
std::cout << "The secret key sk1 key been serialized." << std::endl;
// Serializing a ciphertext
if (!Serial::SerializeToFile(DATAFOLDER + "/ct1.txt", ct1, SerType::JSON)) {
std::cerr << "Error serializing ct1" << std::endl;
return 1;
}
std::cout << "A ciphertext has been serialized." << std::endl;
// CODE FOR DESERIALIZATION
// Deserializing the cryptocontext
BinFHEContext cc;
if (Serial::DeserializeFromFile(DATAFOLDER + "/cryptoContext.txt", cc, SerType::JSON) == false) {
std::cerr << "Could not deserialize the cryptocontext" << std::endl;
return 1;
}
std::cout << "The cryptocontext has been deserialized." << std::endl;
// deserializing the refreshing and switching keys (for bootstrapping)
RingGSWACCKey refreshKey;
if (Serial::DeserializeFromFile(DATAFOLDER + "/refreshKey.txt", refreshKey, SerType::JSON) == false) {
std::cerr << "Could not deserialize the refresh key" << std::endl;
return 1;
}
std::cout << "The refresh key has been deserialized." << std::endl;
LWESwitchingKey ksKey;
if (Serial::DeserializeFromFile(DATAFOLDER + "/ksKey.txt", ksKey, SerType::JSON) == false) {
std::cerr << "Could not deserialize the switching key" << std::endl;
return 1;
}
std::cout << "The switching key has been deserialized." << std::endl;
// Loading the keys in the cryptocontext
cc.BTKeyLoad({refreshKey, ksKey});
// Deserializing the secret key
LWEPrivateKey sk;
if (Serial::DeserializeFromFile(DATAFOLDER + "/sk1.txt", sk, SerType::JSON) == false) {
std::cerr << "Could not deserialize the secret key" << std::endl;
return 1;
}
std::cout << "The secret key has been deserialized." << std::endl;
// Deserializing a previously serialized ciphertext
LWECiphertext ct;
if (Serial::DeserializeFromFile(DATAFOLDER + "/ct1.txt", ct, SerType::JSON) == false) {
std::cerr << "Could not deserialize the ciphertext" << std::endl;
return 1;
}
std::cout << "The ciphertext has been deserialized." << std::endl;
// OPERATIONS WITH DESERIALIZED KEYS AND CIPHERTEXTS
auto ct2 = cc.Encrypt(sk, 1);
std::cout << "Running the computation" << std::endl;
auto ctResult = cc.EvalBinGate(AND, ct, ct2);
std::cout << "The computation has completed" << std::endl;
LWEPlaintext result;
cc.Decrypt(sk, ctResult, &result);
std::cout << "result of 1 AND 1 = " << result << std::endl;
return 0;
}