Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
hoang-gia.nguyen
dap-pattern-search-standalone
Commits
d8df7d47
Commit
d8df7d47
authored
May 05, 2023
by
Hoang Gia NGUYEN
Browse files
testing
parent
c6c9d2f5
Changes
269
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
2943 additions
and
0 deletions
+2943
-0
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.cpp
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.cpp
+1247
-0
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.h
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.h
+185
-0
docker/bigpiseal3.5.1/native/examples/ANN/v1/test_v1.sh
docker/bigpiseal3.5.1/native/examples/ANN/v1/test_v1.sh
+115
-0
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.cpp
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.cpp
+73
-0
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.h
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.h
+15
-0
docker/bigpiseal3.5.1/native/examples/CMakeLists.txt
docker/bigpiseal3.5.1/native/examples/CMakeLists.txt
+14
-0
docker/bigpiseal3.5.1/native/examples/SEALExamples.vcxproj
docker/bigpiseal3.5.1/native/examples/SEALExamples.vcxproj
+206
-0
docker/bigpiseal3.5.1/native/examples/SEALExamples.vcxproj.filters
...gpiseal3.5.1/native/examples/SEALExamples.vcxproj.filters
+52
-0
docker/bigpiseal3.5.1/native/examples/examples.cpp
docker/bigpiseal3.5.1/native/examples/examples.cpp
+100
-0
docker/bigpiseal3.5.1/native/examples/examples.h
docker/bigpiseal3.5.1/native/examples/examples.h
+223
-0
docker/bigpiseal3.5.1/native/examples/generic/CMakeLists.txt
docker/bigpiseal3.5.1/native/examples/generic/CMakeLists.txt
+114
-0
docker/bigpiseal3.5.1/native/examples/generic/add.cpp
docker/bigpiseal3.5.1/native/examples/generic/add.cpp
+30
-0
docker/bigpiseal3.5.1/native/examples/generic/add.h
docker/bigpiseal3.5.1/native/examples/generic/add.h
+13
-0
docker/bigpiseal3.5.1/native/examples/generic/decrypt_gen.sh
docker/bigpiseal3.5.1/native/examples/generic/decrypt_gen.sh
+100
-0
docker/bigpiseal3.5.1/native/examples/generic/encrypt_gen.sh
docker/bigpiseal3.5.1/native/examples/generic/encrypt_gen.sh
+126
-0
docker/bigpiseal3.5.1/native/examples/generic/generic_decrypt.cpp
...igpiseal3.5.1/native/examples/generic/generic_decrypt.cpp
+71
-0
docker/bigpiseal3.5.1/native/examples/generic/generic_encrypt.cpp
...igpiseal3.5.1/native/examples/generic/generic_encrypt.cpp
+68
-0
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate.cpp
...gpiseal3.5.1/native/examples/generic/generic_evaluate.cpp
+63
-0
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate_multiply.cpp
...5.1/native/examples/generic/generic_evaluate_multiply.cpp
+65
-0
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate_sub.cpp
...eal3.5.1/native/examples/generic/generic_evaluate_sub.cpp
+63
-0
No files found.
Too many changes to show.
To preserve performance only
269 of 269+
files are displayed.
Plain diff
Email patch
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.cpp
0 → 100644
View file @
d8df7d47
#include "seal_api.h"
/* namespaces */
using
namespace
std
;
using
namespace
seal
;
/* functions */
int
open_binary_file
(
ifstream
&
in_file
,
const
string
&
filename
)
{
int
ret
=
1
;
in_file
=
ifstream
(
filename
,
ios
::
binary
);
if
(
!
in_file
)
{
// cerr << "[ERRROR] file opening failure" << endl;
ret
=
0
;
}
return
ret
;
}
int
open_binary_file
(
ofstream
&
out_file
,
const
string
&
filename
)
{
int
ret
=
1
;
out_file
=
ofstream
(
filename
,
ios
::
binary
);
if
(
!
out_file
)
{
// cerr << "[ERRROR] file opening failure" << endl;
ret
=
0
;
}
return
ret
;
}
int
save_params
(
EncryptionParameters
&
params
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
params
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
load_params
(
EncryptionParameters
&
params
,
const
string
&
filename
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
params
.
load
(
in_file
);
else
ret
=
0
;
return
ret
;
}
void
init_context
(
size_t
poly_d
,
size_t
p_modulus
,
shared_ptr
<
SEALContext
>
&
context
)
{
EncryptionParameters
params
(
scheme_type
::
BFV
);
params
.
set_poly_modulus_degree
(
poly_d
);
params
.
set_coeff_modulus
(
CoeffModulus
::
BFVDefault
(
poly_d
));
params
.
set_plain_modulus
(
p_modulus
);
save_params
(
params
,
"bfv_params.conf"
);
context
=
SEALContext
::
Create
(
params
);
print_context
(
context
);
}
void
init_context_batching
(
size_t
poly_d
,
int
bit_size
,
shared_ptr
<
SEALContext
>
&
context
,
const
std
::
string
&
key_dir
)
{
EncryptionParameters
params
(
scheme_type
::
BFV
);
params
.
set_poly_modulus_degree
(
poly_d
);
params
.
set_coeff_modulus
(
CoeffModulus
::
BFVDefault
(
poly_d
));
params
.
set_plain_modulus
(
PlainModulus
::
Batching
(
poly_d
,
bit_size
));
save_params
(
params
,
key_dir
+
"bfv_params.conf"
);
context
=
SEALContext
::
Create
(
params
);
print_context
(
context
);
}
// void ckks_init_context_batching(
// size_t poly_d, int bit_size, shared_ptr<SEALContext> &context, const std::string &key_dir)
// {
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_d));
// params.set_plain_modulus(PlainModulus::Batching(poly_d, bit_size));
// save_params(params, key_dir + "bfv_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// }
void
init_context_batching
(
size_t
poly_d
,
vector
<
int
>
bit_sizes
,
std
::
uint64_t
plain_modulus
,
shared_ptr
<
SEALContext
>
&
context
,
const
std
::
string
&
key_dir
)
{
EncryptionParameters
params
(
scheme_type
::
BFV
);
params
.
set_poly_modulus_degree
(
poly_d
);
params
.
set_coeff_modulus
(
CoeffModulus
::
Create
(
poly_d
,
{
36
,
36
,
37
}));
params
.
set_plain_modulus
(
plain_modulus
);
save_params
(
params
,
key_dir
+
"bfv_params.conf"
);
context
=
SEALContext
::
Create
(
params
);
print_context
(
context
);
}
// void ckks_init_context_batching(
// size_t poly_d, vector<int> bit_sizes, std::uint64_t plain_modulus, shared_ptr<SEALContext> &context,
// const std::string &key_dir)
// {
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 36, 36, 37 }));
// params.set_plain_modulus(plain_modulus);
// save_params(params, key_dir + "ckks_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// }
// void init_context_ckks(size_t poly_d, std::uint64_t plain_modulus, shared_ptr<SEALContext> &context,
// const std::string &key_dir)
// {
// int ret = 1;
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// switch (poly_d)
// {
// case 4096:
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 30, 20, 20, 30 }));
// break;
// case 8192:
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 60, 40, 40, 60 }));
// break;
// default:
// cout << "[seal-error] enter valid poly degree: 4096 or 8192" << endl;
// break;
// }
// // params.set_plain_modulus(plain_modulus);
// save_params(params, key_dir + "ckks_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// // context = SEALContext(params);
// // if (context.parameter_error_message() == "valid")
// // {
// // clog << "[seal-ckks] parameter validation: valid" << endl;
// // save_params(params, "ckks_params.conf");
// // }
// // else
// // {
// // clog << "[seal-error] parameter validation: invalid" << endl;
// // ret = 0;
// // }
// // return ret;
// }
// int init_context_ckks(uint32_t poly_deg, SEALContext& context)
int
init_context_ckks
(
uint32_t
poly_deg
,
shared_ptr
<
SEALContext
>
&
context
,
const
std
::
string
&
key_dir
)
{
int
ret
=
1
;
EncryptionParameters
params
(
scheme_type
::
CKKS
);
params
.
set_poly_modulus_degree
(
poly_deg
);
switch
(
poly_deg
)
{
case
4096
:
params
.
set_coeff_modulus
(
CoeffModulus
::
Create
(
poly_deg
,
{
30
,
20
,
20
,
30
}));
break
;
case
8192
:
params
.
set_coeff_modulus
(
CoeffModulus
::
Create
(
poly_deg
,
{
60
,
40
,
40
,
60
}));
break
;
case
16384
:
params
.
set_coeff_modulus
(
CoeffModulus
::
Create
(
poly_deg
,
{
60
,
40
,
40
,
40
,
40
,
40
,
40
,
40
,
60
}));
// params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 }));
// params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 30, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30 }));
break
;
default:
cout
<<
"[seal-error] enter valid poly degree: 4096 or 8192"
<<
endl
;
break
;
}
context
=
SEALContext
::
Create
(
params
);
// if (context.parameter_error_message() == "valid") {
// clog << "[seal-ckks] parameter validation: valid" << endl;
save_params
(
params
,
key_dir
+
"ckks_params.conf"
);
// }
// else {
// clog << "[seal-error] parameter validation: invalid" << endl;
// ret = 0;
// }
print_context
(
context
);
return
ret
;
}
void
load_context
(
shared_ptr
<
SEALContext
>
&
context
,
const
string
&
filename
)
{
EncryptionParameters
params
;
load_params
(
params
,
filename
);
context
=
SEALContext
::
Create
(
params
);
print_context
(
context
);
}
/* print paramaeters function from examples.h */
void
print_context
(
shared_ptr
<
SEALContext
>
&
context
)
{
if
(
!
context
)
throw
invalid_argument
(
"[ERROR] context is not set"
);
auto
&
context_data
=
*
context
->
key_context_data
();
/* which scheme is used */
string
scheme_name
;
switch
(
context_data
.
parms
().
scheme
())
{
case
seal
::
scheme_type
::
BFV
:
scheme_name
=
"BFV"
;
break
;
case
seal
::
scheme_type
::
CKKS
:
scheme_name
=
"CKKS"
;
break
;
default:
throw
invalid_argument
(
"[ERROR] unsupported scheme"
);
}
cout
<<
"[CONTEXT] scheme: "
<<
scheme_name
<<
endl
;
cout
<<
"[CONTEXT] poly_modulus_degree: "
<<
context_data
.
parms
().
poly_modulus_degree
()
<<
endl
;
// /* Print the size of the true (product) coefficient modulus */
cout
<<
"[CONTEXT] coeff_modulus size: "
;
cout
<<
context_data
.
total_coeff_modulus_bit_count
()
<<
" ("
;
auto
coeff_modulus
=
context_data
.
parms
().
coeff_modulus
();
size_t
coeff_modulus_size
=
coeff_modulus
.
size
();
for
(
size_t
i
=
0
;
i
<
coeff_modulus_size
-
1
;
i
++
)
cout
<<
coeff_modulus
[
i
].
bit_count
()
<<
" + "
;
cout
<<
coeff_modulus
.
back
().
bit_count
();
cout
<<
") bits"
<<
endl
;
cout
<<
"[CONTEXT] coeff_modulus values: ("
;
for
(
int
i
=
0
;
i
<
(
coeff_modulus
.
size
()
-
1
);
++
i
)
{
cout
<<
coeff_modulus
[
i
].
value
()
<<
",
\t
"
;
cout
<<
coeff_modulus
[
coeff_modulus
.
size
()
-
1
].
value
()
<<
")"
<<
endl
;
}
/* For the BFV scheme print the plain_modulus parameter */
if
(
context_data
.
parms
().
scheme
()
==
seal
::
scheme_type
::
BFV
)
{
cout
<<
"[CONTEXT] plain_modulus: "
<<
context_data
.
parms
().
plain_modulus
().
value
()
<<
endl
;
}
}
int
save_key
(
PublicKey
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_key
(
SecretKey
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_key
(
RelinKeys
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_key
(
Serializable
<
RelinKeys
>
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_key
(
GaloisKeys
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_key
(
Serializable
<
GaloisKeys
>
&
k
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
k
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
load_key
(
shared_ptr
<
SEALContext
>
&
context
,
const
string
&
filename
,
PublicKey
&
k
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
k
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
int
load_key
(
shared_ptr
<
SEALContext
>
&
context
,
const
string
&
filename
,
SecretKey
&
k
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
k
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
int
load_key
(
shared_ptr
<
SEALContext
>
&
context
,
const
string
&
filename
,
RelinKeys
&
k
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
k
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
int
load_key
(
shared_ptr
<
SEALContext
>
&
context
,
const
string
&
filename
,
GaloisKeys
&
k
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
k
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
void
generate_keys
(
size_t
poly_d
,
size_t
p_modulus
,
bool
serializable
)
{
shared_ptr
<
SEALContext
>
context
;
init_context
(
poly_d
,
p_modulus
,
context
);
KeyGenerator
keygen
(
context
);
PublicKey
pk
=
keygen
.
public_key
();
save_key
(
pk
,
"bfv.pk"
);
SecretKey
sk
=
keygen
.
secret_key
();
save_key
(
sk
,
"bfv.sk"
);
if
(
serializable
)
{
Serializable
<
RelinKeys
>
lk
=
keygen
.
relin_keys
();
save_key
(
lk
,
"bfv.lk"
);
}
else
{
RelinKeys
lk
=
keygen
.
relin_keys_local
();
save_key
(
lk
,
"bfv.lk"
);
}
}
bool
suffix_exist
(
const
std
::
string
&
str
,
const
std
::
string
&
suffix
)
{
return
str
.
size
()
>=
suffix
.
size
()
&&
str
.
compare
(
str
.
size
()
-
suffix
.
size
(),
suffix
.
size
(),
suffix
)
==
0
;
}
/*
Helper function: Prints the parameters in a SEALContext.
*/
inline
void
print_parameters
(
std
::
shared_ptr
<
seal
::
SEALContext
>
context
)
{
// Verify parameters
if
(
!
context
)
{
throw
std
::
invalid_argument
(
"context is not set"
);
}
auto
&
context_data
=
*
context
->
key_context_data
();
/*
Which scheme are we using?
*/
std
::
string
scheme_name
;
switch
(
context_data
.
parms
().
scheme
())
{
case
seal
::
scheme_type
::
BFV
:
scheme_name
=
"BFV"
;
break
;
case
seal
::
scheme_type
::
CKKS
:
scheme_name
=
"CKKS"
;
break
;
default:
throw
std
::
invalid_argument
(
"unsupported scheme"
);
}
// cout << "/" << std::endl;
// cout << "| Encryption parameters :" << std::endl;
// cout << "| scheme: " << scheme_name << std::endl;
// cout << "| poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << std::endl;
/*
Print the size of the true (product) coefficient modulus.
*/
// cout << "| coeff_modulus size: ";
// cout << context_data.total_coeff_modulus_bit_count() << " (";
auto
coeff_modulus
=
context_data
.
parms
().
coeff_modulus
();
std
::
size_t
coeff_modulus_size
=
coeff_modulus
.
size
();
// for (std::size_t i = 0; i < coeff_modulus_size - 1; i++)
// {
// cout << coeff_modulus[i].bit_count() << " + ";
// }
// cout << coeff_modulus.back().bit_count();
// cout << ") bits" << std::endl;
/*
For the BFV scheme print the plain_modulus parameter.
*/
// if (context_data.parms().scheme() == seal::scheme_type::BFV)
// {
// cout << "| plain_modulus: " << context_data.parms().plain_modulus().value() << std::endl;
// }
// cout << "\\" << std::endl;
}
void
batching_generate_keys
(
size_t
poly_d
,
int
bit_size
,
string
key_dir
,
bool
serializable
)
{
shared_ptr
<
SEALContext
>
context
;
// init_context_batching(poly_d, bit_size, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string
keypath
(
""
);
string
keypath1
(
"/bfv.pk"
);
string
keypath2
(
"/bfv.sk"
);
string
keypath3
(
"/bfv.lk"
);
string
keypath4
(
"/bfv.gk"
);
string
bfv_params_path
(
key_dir
+
"/"
);
if
(
suffix_exist
(
key_dir
,
"/"
))
{
keypath1
=
"bfv.pk"
;
keypath2
=
"bfv.sk"
;
keypath3
=
"bfv.lk"
;
keypath4
=
"bfv.gk"
;
bfv_params_path
=
key_dir
;
}
init_context_batching
(
poly_d
,
bit_size
,
context
,
bfv_params_path
);
KeyGenerator
keygen
(
context
);
PublicKey
pk
=
keygen
.
public_key
();
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key
(
pk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath2
);
SecretKey
sk
=
keygen
.
secret_key
();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key
(
sk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath3
);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
RelinKeys
>
lk
=
keygen
.
relin_keys
();
save_key
(
lk
,
keypath
);
}
else
{
RelinKeys
lk
=
keygen
.
relin_keys_local
();
save_key
(
lk
,
keypath
);
}
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath4
);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
GaloisKeys
>
gk
=
keygen
.
galois_keys
();
save_key
(
gk
,
keypath
);
}
else
{
GaloisKeys
gk
=
keygen
.
galois_keys_local
();
save_key
(
gk
,
keypath
);
}
// print_parameters(context);
}
void
generate_keys_ckks
(
size_t
poly_d
,
int
bit_size
,
string
key_dir
,
bool
serializable
)
{
shared_ptr
<
SEALContext
>
context
;
// init_context_batching(poly_d, bit_size, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string
keypath
(
""
);
string
keypath1
(
"/ckks.pk"
);
string
keypath2
(
"/ckks.sk"
);
string
keypath3
(
"/ckks.lk"
);
string
keypath4
(
"/ckks.gk"
);
string
ckks_params_path
(
key_dir
+
"/"
);
if
(
suffix_exist
(
key_dir
,
"/"
))
{
keypath1
=
"ckks.pk"
;
keypath2
=
"ckks.sk"
;
keypath3
=
"ckks.lk"
;
keypath4
=
"ckks.gk"
;
ckks_params_path
=
key_dir
;
}
// ckks_init_context_batching(poly_d, bit_size, context, ckks_params_path);
// init_context_ckks(poly_d, bit_size, context, ckks_params_path);
init_context_ckks
(
poly_d
,
context
,
ckks_params_path
);
KeyGenerator
keygen
(
context
);
PublicKey
pk
=
keygen
.
public_key
();
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key
(
pk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath2
);
SecretKey
sk
=
keygen
.
secret_key
();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key
(
sk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath3
);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
RelinKeys
>
lk
=
keygen
.
relin_keys
();
save_key
(
lk
,
keypath
);
}
else
{
RelinKeys
lk
=
keygen
.
relin_keys_local
();
save_key
(
lk
,
keypath
);
}
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath4
);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
GaloisKeys
>
gk
=
keygen
.
galois_keys
();
save_key
(
gk
,
keypath
);
}
else
{
GaloisKeys
gk
=
keygen
.
galois_keys_local
();
save_key
(
gk
,
keypath
);
}
print_parameters
(
context
);
}
void
batching_generate_keys
(
size_t
poly_d
,
vector
<
int
>
bit_sizes
,
std
::
uint64_t
plain_modulus
,
string
key_dir
,
bool
serializable
)
{
shared_ptr
<
SEALContext
>
context
;
// init_context_batching(poly_d, bit_sizes, plain_modulus, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string
keypath
(
""
);
string
keypath1
(
"/bfv.pk"
);
string
keypath2
(
"/bfv.sk"
);
string
keypath3
(
"/bfv.lk"
);
string
keypath4
(
"/bfv.gk"
);
string
bfv_params_path
(
key_dir
+
"/"
);
if
(
suffix_exist
(
key_dir
,
"/"
))
{
keypath1
=
"bfv.pk"
;
keypath2
=
"bfv.sk"
;
keypath3
=
"bfv.lk"
;
keypath4
=
"bfv.gk"
;
bfv_params_path
=
key_dir
;
}
init_context_batching
(
poly_d
,
bit_sizes
,
plain_modulus
,
context
,
bfv_params_path
);
KeyGenerator
keygen
(
context
);
PublicKey
pk
=
keygen
.
public_key
();
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key
(
pk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath2
);
SecretKey
sk
=
keygen
.
secret_key
();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key
(
sk
,
keypath
);
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath3
);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
RelinKeys
>
lk
=
keygen
.
relin_keys
();
save_key
(
lk
,
keypath
);
}
else
{
RelinKeys
lk
=
keygen
.
relin_keys_local
();
save_key
(
lk
,
keypath
);
}
keypath
=
""
;
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath4
);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if
(
serializable
)
{
Serializable
<
GaloisKeys
>
gk
=
keygen
.
galois_keys
();
save_key
(
gk
,
keypath
);
}
else
{
GaloisKeys
gk
=
keygen
.
galois_keys_local
();
save_key
(
gk
,
keypath
);
}
// print_parameters(context);
}
void
init_operator
(
struct
encryptor_t
&
op_st
)
{
load_context
(
op_st
.
context
,
"bfv_params.conf"
);
PublicKey
pk
;
load_key
(
op_st
.
context
,
"bfv.pk"
,
pk
);
op_st
.
encr
=
new
Encryptor
(
op_st
.
context
,
pk
);
op_st
.
icode
=
new
IntegerEncoder
(
op_st
.
context
);
}
void
init_operator
(
struct
encryptor_t
&
op_st
,
const
string
&
key_dir
)
{
string
bfv_params_path
(
""
);
string
bfv_params_path1
(
"/bfv_params.conf"
);
bfv_params_path
=
""
;
bfv_params_path
.
append
(
key_dir
);
bfv_params_path
.
append
(
bfv_params_path1
);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context
(
op_st
.
context
,
bfv_params_path
);
PublicKey
pk
;
string
keypath
(
""
);
string
keypath1
(
"bfv.pk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
pk
);
op_st
.
encr
=
new
Encryptor
(
op_st
.
context
,
pk
);
op_st
.
icode
=
new
IntegerEncoder
(
op_st
.
context
);
}
// void init_operator_ckks(struct encryptor_t &op_st, const string &key_dir)
void
init_operator_ckks
(
struct
cencryptor_t
&
op_st
,
const
string
&
key_dir
)
{
string
ckks_params_path
(
""
);
string
ckks_params_path1
(
"/ckks_params.conf"
);
ckks_params_path
=
""
;
ckks_params_path
.
append
(
key_dir
);
ckks_params_path
.
append
(
ckks_params_path1
);
// cout << "[INFO] ckksparams: " << ckks_params_path << endl;
load_context
(
op_st
.
context
,
ckks_params_path
);
PublicKey
pk
;
string
keypath
(
""
);
string
keypath1
(
"ckks.pk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
pk
);
op_st
.
encr
=
new
Encryptor
(
op_st
.
context
,
pk
);
// op_st.icode = new IntegerEncoder(op_st.context);
op_st
.
ccode
=
new
CKKSEncoder
(
op_st
.
context
);
// auto &context_data = *(op_st.context.key_context_data());
auto
&
context_data
=
*
op_st
.
context
->
key_context_data
();
int
poly_deg
=
context_data
.
parms
().
poly_modulus_degree
();
switch
(
poly_deg
)
{
case
4096
:
op_st
.
scale
=
pow
(
2.0
,
20
);
case
8192
:
op_st
.
scale
=
pow
(
2.0
,
40
);
case
16384
:
op_st
.
scale
=
pow
(
2.0
,
40
);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
void
init_operator_ckks
(
struct
cdecryptor_t
&
op_st
,
const
string
&
key_dir
)
{
string
ckks_params_path
(
""
);
string
ckks_params_path1
(
"/ckks_params.conf"
);
ckks_params_path
=
""
;
ckks_params_path
.
append
(
key_dir
);
ckks_params_path
.
append
(
ckks_params_path1
);
// cout << "[INFO] ckksparams: " << ckks_params_path << endl;
load_context
(
op_st
.
context
,
ckks_params_path
);
// PublicKey pk;
SecretKey
sk
;
string
keypath
(
""
);
string
keypath1
(
"ckks.sk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
sk
);
op_st
.
decr
=
new
Decryptor
(
op_st
.
context
,
sk
);
// op_st.icode = new IntegerEncoder(op_st.context);
op_st
.
ccode
=
new
CKKSEncoder
(
op_st
.
context
);
// auto &context_data = *(op_st.context.key_context_data());
auto
&
context_data
=
*
op_st
.
context
->
key_context_data
();
int
poly_deg
=
context_data
.
parms
().
poly_modulus_degree
();
switch
(
poly_deg
)
{
case
4096
:
op_st
.
scale
=
pow
(
2.0
,
20
);
case
8192
:
op_st
.
scale
=
pow
(
2.0
,
40
);
case
16384
:
op_st
.
scale
=
pow
(
2.0
,
40
);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
void
init_operator_ckks
(
struct
cevaluator_t
&
op_st
,
const
string
&
key_dir
)
{
string
ckks_params_path
(
""
);
string
ckks_params_path1
(
"/ckks_params.conf"
);
ckks_params_path
=
""
;
ckks_params_path
.
append
(
key_dir
);
ckks_params_path
.
append
(
ckks_params_path1
);
// cout << "[INFO] bfvparams: " << ckks_params_path << endl;
load_context
(
op_st
.
context
,
ckks_params_path
);
string
keypath
(
""
);
string
keypath1
(
"ckks.lk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath evaluator: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
op_st
.
lk
);
string
keypath3
(
""
);
string
keypath4
(
"ckks.gk"
);
keypath3
.
append
(
key_dir
);
keypath3
.
append
(
keypath4
);
// cout << "[INFO] Keypath evaluator: " << keypath3 << endl;
load_key
(
op_st
.
context
,
keypath3
,
op_st
.
gk
);
// op_st.eval = new Evaluator(op_st.context, sk);
// op_st.icode = new IntegerEncoder(op_st.context);
// op_st.ccode = new CKKSEncoder(op_st.context);
op_st
.
eval
=
new
Evaluator
(
op_st
.
context
);
// auto &context_data = *(op_st.context.key_context_data());
auto
&
context_data
=
*
op_st
.
context
->
key_context_data
();
int
poly_deg
=
context_data
.
parms
().
poly_modulus_degree
();
switch
(
poly_deg
)
{
case
4096
:
op_st
.
scale
=
pow
(
2.0
,
20
);
case
8192
:
op_st
.
scale
=
pow
(
2.0
,
40
);
case
16384
:
op_st
.
scale
=
pow
(
2.0
,
40
);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
// int init_operator_ckks(struct cencryptor_t &op_st, const string &key_dir)
// {
// int ret = 1;
// // if (!load_context("/ckks_params.conf", op_st.context)) {
// // cerr << "[seal-error] failure for loading ckks context during encryptor setting!" << endl;
// // ret = 0;
// // }
// // else {
// // PublicKey pk;
// // if (!load_key(op_st.context, "ckks.pk", pk)) {
// // cerr << "[seal-error] failure for loading ckks public key during encryptor setting!" << endl;
// // ret = 0;
// // }
// // else {
// op_st.encr = new Encryptor(op_st.context, pk);
// op_st.ccode = new CKKSEncoder(op_st.context);
// auto &context_data = *(op_st.context.key_context_data());
// int poly_deg = context_data.parms().poly_modulus_degree();
// switch (poly_deg)
// {
// case 4096:
// op_st.scale = pow(2.0, 20);
// case 8192:
// op_st.scale = pow(2.0, 40);
// }
// // }
// // }
// return ret;
// }
void
init_operator_batching
(
struct
encryptor_t
&
op_st
,
const
string
&
key_dir
)
{
init_operator
(
op_st
,
key_dir
);
op_st
.
bcode
=
new
BatchEncoder
(
op_st
.
context
);
}
void
delete_operator
(
struct
encryptor_t
&
op_st
)
{
delete
op_st
.
encr
;
delete
op_st
.
icode
;
}
void
delete_operator_batching
(
struct
encryptor_t
&
op_st
)
{
delete_operator
(
op_st
);
delete
op_st
.
bcode
;
}
void
delete_operator_ckks
(
struct
cencryptor_t
&
op_st
)
{
delete
op_st
.
encr
;
delete
op_st
.
ccode
;
}
void
delete_operator_ckks
(
struct
cdecryptor_t
&
op_st
)
{
delete
op_st
.
decr
;
delete
op_st
.
ccode
;
}
void
delete_operator_ckks
(
struct
cevaluator_t
&
op_st
)
{
delete
op_st
.
eval
;
}
void
init_operator
(
struct
decryptor_t
&
op_st
)
{
load_context
(
op_st
.
context
,
"bfv_params.conf"
);
SecretKey
sk
;
load_key
(
op_st
.
context
,
"bfv.sk"
,
sk
);
op_st
.
decr
=
new
Decryptor
(
op_st
.
context
,
sk
);
op_st
.
icode
=
new
IntegerEncoder
(
op_st
.
context
);
}
void
init_operator
(
struct
decryptor_t
&
op_st
,
const
std
::
string
&
key_dir
)
{
string
bfv_params_path
(
""
);
string
bfv_params_path1
(
"/bfv_params.conf"
);
bfv_params_path
=
""
;
bfv_params_path
.
append
(
key_dir
);
bfv_params_path
.
append
(
bfv_params_path1
);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context
(
op_st
.
context
,
bfv_params_path
);
SecretKey
sk
;
string
keypath
(
""
);
string
keypath1
(
"bfv.sk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath decrypt: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
sk
);
op_st
.
decr
=
new
Decryptor
(
op_st
.
context
,
sk
);
op_st
.
icode
=
new
IntegerEncoder
(
op_st
.
context
);
}
void
init_operator_batching
(
struct
decryptor_t
&
op_st
,
const
string
&
key_dir
)
{
init_operator
(
op_st
,
key_dir
);
op_st
.
bcode
=
new
BatchEncoder
(
op_st
.
context
);
}
void
delete_operator
(
struct
decryptor_t
&
op_st
)
{
delete
op_st
.
decr
;
delete
op_st
.
icode
;
}
void
delete_operator_batching
(
struct
decryptor_t
&
op_st
)
{
delete_operator
(
op_st
);
delete
op_st
.
bcode
;
}
void
init_operator
(
struct
evaluator_t
&
op_st
)
{
load_context
(
op_st
.
context
,
"bfv_params.conf"
);
load_key
(
op_st
.
context
,
"bfv.lk"
,
op_st
.
lk
);
op_st
.
eval
=
new
Evaluator
(
op_st
.
context
);
}
void
init_operator
(
struct
evaluator_t
&
op_st
,
const
string
&
relink_key_path
)
{
load_context
(
op_st
.
context
,
"bfv_params.conf"
);
load_key
(
op_st
.
context
,
relink_key_path
,
op_st
.
lk
);
op_st
.
eval
=
new
Evaluator
(
op_st
.
context
);
}
void
init_operator_batching
(
struct
evaluator_t
&
op_st
,
const
string
&
key_dir
)
{
string
bfv_params_path
(
""
);
string
bfv_params_path1
(
"/bfv_params.conf"
);
bfv_params_path
=
""
;
bfv_params_path
.
append
(
key_dir
);
bfv_params_path
.
append
(
bfv_params_path1
);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context
(
op_st
.
context
,
bfv_params_path
);
string
keypath
(
""
);
string
keypath1
(
"bfv.lk"
);
keypath
.
append
(
key_dir
);
keypath
.
append
(
keypath1
);
// cout << "[INFO] Keypath evaluator: " << keypath << endl;
load_key
(
op_st
.
context
,
keypath
,
op_st
.
lk
);
string
keypath3
(
""
);
string
keypath4
(
"bfv.gk"
);
keypath3
.
append
(
key_dir
);
keypath3
.
append
(
keypath4
);
// cout << "[INFO] Keypath evaluator: " << keypath3 << endl;
load_key
(
op_st
.
context
,
keypath3
,
op_st
.
gk
);
op_st
.
eval
=
new
Evaluator
(
op_st
.
context
);
}
void
delete_operator
(
struct
evaluator_t
&
op_st
)
{
delete
op_st
.
eval
;
}
void
delete_operator_batching
(
struct
evaluator_t
&
op_st
)
{
delete_operator
(
op_st
);
}
void
init_plaintext
(
struct
encryptor_t
&
op_st
,
int64_t
plain
,
Plaintext
&
pt
)
{
pt
=
Plaintext
(
op_st
.
icode
->
encode
(
plain
));
}
void
init_ciphertext
(
struct
encryptor_t
&
op_st
,
int64_t
plain
,
Ciphertext
&
ct
)
{
// cout << "[INFO] encrypting: " << endl;
op_st
.
encr
->
encrypt
(
Plaintext
(
op_st
.
icode
->
encode
(
plain
)),
ct
);
// cout << plain << endl;
}
void
init_ciphermatrix
(
struct
encryptor_t
&
op_st
,
vector
<
int64_t
>
&
plain_matrix
,
Ciphertext
&
encrypted_matrix
)
{
// cout << "[INFO] encrypting: " << endl;
Plaintext
plaintext_matrix
;
// BatchEncoder batch_encoder(op_st.context);
// batch_encoder.encode(plain_matrix, plaintext_matrix);
op_st
.
bcode
->
encode
(
plain_matrix
,
plaintext_matrix
);
// op_st.ccode->encode(plain_matrix, plaintext_matrix);
op_st
.
encr
->
encrypt
(
plaintext_matrix
,
encrypted_matrix
);
// cout << plain << endl;
}
/* ckks plaintext management */
// void init_plaintext_ckks(CKKSEncoder& encoder, double scale, vector<double>& p_vector, Plaintext& pt, bool
// print_info) void init_plaintext_ckks(CKKSEncoder& encoder, double scale, vector<double>& p_vector, Plaintext& pt)
// {
// try {
// encoder.encode(p_vector, scale, pt);
// }
// catch (std::exception &e) {
// clog << "[seal-error] catched exception: " << e.what() << endl;
// clog << "[seal-error] ckks slots encoding error" << endl;
// }
// if (print_info) {
// clog << "[seal-ckks] plaintexts: " << endl;
// print_vectors(p_vector);
// }
// }
/* ckks ciphertext management */
// void init_ciphertext(CKKSEncoder& encoder, Encryptor& encryptor, double scale, vector<double>& c_vector, Ciphertext&
// ct, bool print_info)
void
init_ciphertext_ckks
(
CKKSEncoder
&
encoder
,
Encryptor
&
encryptor
,
double
scale
,
vector
<
double
>
&
c_vector
,
Ciphertext
&
ct
)
{
Plaintext
pt
;
try
{
encoder
.
encode
(
c_vector
,
scale
,
pt
);
}
catch
(
std
::
exception
&
e
)
{
clog
<<
"[seal-error] catched exception: "
<<
e
.
what
()
<<
endl
;
clog
<<
"[seal-error] ckks slots encoding error"
<<
endl
;
}
encryptor
.
encrypt
(
pt
,
ct
);
// if (print_info) {
// clog << "[seal-ckks] ciphertext inputs: " << endl;
// print_vectors(c_vector);
// }
}
void
init_ciphertext_ckks
(
cencryptor_t
&
op_st
,
double
scale
,
vector
<
double
>
&
c_vector
,
Ciphertext
&
ct
)
{
init_ciphertext_ckks
(
*
(
op_st
.
ccode
),
*
(
op_st
.
encr
),
scale
,
c_vector
,
ct
);
}
void
init_ciphertext_ckks
(
cencryptor_t
&
op_st
,
vector
<
double
>
&
c_vector
,
Ciphertext
&
ct
)
{
init_ciphertext_ckks
(
*
(
op_st
.
ccode
),
*
(
op_st
.
encr
),
op_st
.
scale
,
c_vector
,
ct
);
}
/* ckks decryption */
void
decrypt_ciphertext_ckks
(
cdecryptor_t
&
op_st
,
Ciphertext
&
ct
,
vector
<
double
>
&
pt_val
)
{
decrypt_ciphertext_ckks
(
*
(
op_st
.
ccode
),
*
(
op_st
.
decr
),
ct
,
pt_val
);
}
void
decrypt_ciphertext_ckks
(
CKKSEncoder
&
encoder
,
Decryptor
&
decryptor
,
Ciphertext
&
ct
,
vector
<
double
>
&
pt_val
)
{
Plaintext
pt
;
decryptor
.
decrypt
(
ct
,
pt
);
encoder
.
decode
(
pt
,
pt_val
);
// if (print_info) {
// clog << "[seal-ckks] decrypted plaintexts: " << endl;
// print_vectors(pt_val);
// }
}
// vector<double> decrypt_ciphermatrix_ckks(struct decryptor_t &op_st, Ciphertext &ct)
// {
// Plaintext pt;
// op_st.decr->decrypt(ct, pt);
// vector<double> pod_matrix;
// // BatchEncoder batch_encoder(op_st.context);
// // batch_encoder.decode(pt, pod_matrix);
// encoder.decode(pt, pod_matrix);
// // print_matrix(pod_matrix, 3);
// decrypt_ciphertext_ckks
// return pod_matrix;
// }
/* saving ciphertexts and plaintexts */
// plaintexts
int
load_plaintext_ckks
(
struct
cencryptor_t
&
op_st
,
Plaintext
&
pt
,
const
string
&
filename
)
{
return
load_plaintext
(
op_st
.
context
,
pt
,
filename
);
}
int
load_plaintext_ckks
(
struct
cdecryptor_t
&
op_st
,
Plaintext
&
pt
,
const
string
&
filename
)
{
return
load_plaintext
(
op_st
.
context
,
pt
,
filename
);
}
int
load_plaintext_ckks
(
struct
cevaluator_t
&
op_st
,
Plaintext
&
pt
,
const
string
&
filename
)
{
return
load_plaintext
(
op_st
.
context
,
pt
,
filename
);
}
// ciphertexts
int
load_ciphertext_ckks
(
struct
cevaluator_t
&
op_st
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
return
load_ciphertext
(
op_st
.
context
,
ct
,
filename
);
}
int
load_ciphertext_ckks
(
struct
cencryptor_t
&
op_st
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
return
load_ciphertext
(
op_st
.
context
,
ct
,
filename
);
}
int
load_ciphertext_ckks
(
struct
cdecryptor_t
&
op_st
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
return
load_ciphertext
(
op_st
.
context
,
ct
,
filename
);
}
void
decrypt_ciphertext
(
struct
decryptor_t
&
op_st
,
Ciphertext
&
ct
)
{
Plaintext
pt
;
op_st
.
decr
->
decrypt
(
ct
,
pt
);
int64_t
res
=
op_st
.
icode
->
decode_int64
(
pt
);
// cout << "[INFO] decrypted result: " << res << endl;
}
int64_t
decrypt_ciphertext_and_return_value
(
struct
decryptor_t
&
op_st
,
Ciphertext
&
ct
)
{
Plaintext
pt
;
op_st
.
decr
->
decrypt
(
ct
,
pt
);
int64_t
res
=
op_st
.
icode
->
decode_int64
(
pt
);
// cout << "[INFO] Decrypted result: " << res << endl;
return
res
;
}
vector
<
int64_t
>
decrypt_ciphermatrix
(
struct
decryptor_t
&
op_st
,
Ciphertext
&
ct
)
{
Plaintext
pt
;
op_st
.
decr
->
decrypt
(
ct
,
pt
);
vector
<
int64_t
>
pod_matrix
;
BatchEncoder
batch_encoder
(
op_st
.
context
);
batch_encoder
.
decode
(
pt
,
pod_matrix
);
// print_matrix(pod_matrix, 3);
return
pod_matrix
;
}
int
save_plaintext
(
Plaintext
&
pt
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
pt
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
save_ciphertext
(
Ciphertext
&
ct
,
const
string
&
filename
)
{
int
ret
=
1
;
ofstream
out_file
;
if
(
open_binary_file
(
out_file
,
filename
))
ct
.
save
(
out_file
);
else
ret
=
0
;
return
ret
;
}
int
load_plaintext
(
shared_ptr
<
SEALContext
>
&
context
,
Plaintext
&
pt
,
const
string
&
filename
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
pt
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
int
load_plaintext
(
struct
evaluator_t
&
op_st
,
Plaintext
&
pt
,
const
string
&
filename
)
{
return
load_plaintext
(
op_st
.
context
,
pt
,
filename
);
}
int
load_plaintext
(
struct
decryptor_t
&
op_st
,
Plaintext
&
pt
,
const
string
&
filename
)
{
return
load_plaintext
(
op_st
.
context
,
pt
,
filename
);
}
int
load_ciphertext
(
shared_ptr
<
SEALContext
>
&
context
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
int
ret
=
1
;
ifstream
in_file
;
if
(
open_binary_file
(
in_file
,
filename
))
ct
.
load
(
context
,
in_file
);
else
ret
=
0
;
return
ret
;
}
int
load_ciphertext
(
struct
evaluator_t
&
op_st
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
return
load_ciphertext
(
op_st
.
context
,
ct
,
filename
);
}
int
load_ciphertext
(
struct
decryptor_t
&
op_st
,
Ciphertext
&
ct
,
const
string
&
filename
)
{
return
load_ciphertext
(
op_st
.
context
,
ct
,
filename
);
}
/* ckks plaintext management */
void
init_plaintext_ckks
(
CKKSEncoder
&
encoder
,
double
scale
,
vector
<
double
>
&
p_vector
,
Plaintext
&
pt
,
bool
print_info
)
{
try
{
encoder
.
encode
(
p_vector
,
scale
,
pt
);
}
catch
(
std
::
exception
&
e
)
{
clog
<<
"[seal-error] catched exception: "
<<
e
.
what
()
<<
endl
;
clog
<<
"[seal-error] ckks slots encoding error"
<<
endl
;
}
// if (print_info)
// {
// clog << "[seal-ckks] plaintexts: " << endl;
// print_vectors(p_vector);
// }
}
void
init_plaintext_ckks
(
cencryptor_t
&
op_st
,
double
scale
,
vector
<
double
>
&
p_vector
,
Plaintext
&
pt
,
bool
print_info
)
{
init_plaintext_ckks
(
*
(
op_st
.
ccode
),
scale
,
p_vector
,
pt
,
print_info
);
}
void
init_plaintext_ckks
(
cencryptor_t
&
op_st
,
vector
<
double
>
&
p_vector
,
Plaintext
&
pt
,
bool
print_info
)
{
init_plaintext_ckks
(
*
(
op_st
.
ccode
),
op_st
.
scale
,
p_vector
,
pt
,
print_info
);
}
void
init_plaintext_ckks
(
cdecryptor_t
&
op_st
,
double
scale
,
vector
<
double
>
&
p_vector
,
Plaintext
&
pt
,
bool
print_info
)
{
init_plaintext_ckks
(
*
(
op_st
.
ccode
),
scale
,
p_vector
,
pt
,
print_info
);
}
void
init_plaintext_ckks
(
cdecryptor_t
&
op_st
,
vector
<
double
>
&
p_vector
,
Plaintext
&
pt
,
bool
print_info
)
{
init_plaintext_ckks
(
*
(
op_st
.
ccode
),
op_st
.
scale
,
p_vector
,
pt
,
print_info
);
}
\ No newline at end of file
docker/bigpiseal3.5.1/native/examples/ANN/v1/seal_api.h
0 → 100644
View file @
d8df7d47
#ifndef _SEAL_API_H_
#define _SEAL_API_H_
/* includes */
#include <sys/time.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cassert>
#include <cstdint>
#include <boost/lexical_cast.hpp>
#include "seal/seal.h"
#include "csv_api.h"
/** BFV definitions **/
struct
encryptor_t
{
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
Encryptor
*
encr
;
seal
::
BatchEncoder
*
bcode
;
seal
::
IntegerEncoder
*
icode
;
};
struct
decryptor_t
{
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
Decryptor
*
decr
;
seal
::
BatchEncoder
*
bcode
;
seal
::
IntegerEncoder
*
icode
;
};
struct
evaluator_t
{
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
RelinKeys
lk
;
seal
::
GaloisKeys
gk
;
seal
::
Evaluator
*
eval
;
};
/** CKKS definitions **/
struct
cencryptor_t
{
// seal::SEALContext context{0};
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
Encryptor
*
encr
{
nullptr
};
seal
::
CKKSEncoder
*
ccode
{
nullptr
};
double
scale
{
0
};
};
struct
cdecryptor_t
{
// seal::SEALContext context{0};
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
Decryptor
*
decr
{
nullptr
};
seal
::
CKKSEncoder
*
ccode
{
nullptr
};
double
scale
{
0
};
};
struct
cevaluator_t
{
// seal::SEALContext context{0};
std
::
shared_ptr
<
seal
::
SEALContext
>
context
;
seal
::
RelinKeys
lk
{};
seal
::
GaloisKeys
gk
{};
seal
::
Evaluator
*
eval
{
nullptr
};
// seal::CKKSEncoder* ccode{nullptr};
double
scale
{
0
};
};
/** prototypes **/
/** for binary files management **/
int
open_binary_file
(
std
::
ifstream
&
in_file
,
const
std
::
string
&
filename
);
int
open_binary_file
(
std
::
ofstream
&
out_file
,
const
std
::
string
&
filename
);
/** for context management **/
int
save_params
(
seal
::
EncryptionParameters
&
params
,
const
std
::
string
&
filename
);
int
load_params
(
seal
::
EncryptionParameters
&
params
,
const
std
::
string
&
filename
);
void
init_context
(
size_t
poly_d
,
size_t
p_modulus
,
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
);
void
load_context
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
const
std
::
string
&
filename
);
void
print_context
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
);
/** for key management **/
int
save_key
(
seal
::
PublicKey
&
k
,
const
std
::
string
&
filename
);
int
save_key
(
seal
::
SecretKey
&
k
,
const
std
::
string
&
filename
);
int
save_key
(
seal
::
RelinKeys
&
k
,
const
std
::
string
&
filename
);
int
save_key
(
seal
::
GaloisKeys
&
k
,
const
std
::
string
&
filename
);
int
load_key
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
const
std
::
string
&
filename
,
seal
::
PublicKey
&
k
);
int
load_key
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
const
std
::
string
&
filename
,
seal
::
SecretKey
&
k
);
int
load_key
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
const
std
::
string
&
filename
,
seal
::
RelinKeys
&
k
);
void
generate_keys
(
size_t
poly_d
,
size_t
p_modulus
,
bool
seriablizable
=
false
);
void
batching_generate_keys
(
size_t
poly_d
,
int
bit_size
,
const
std
::
string
key_path
,
bool
serializable
=
false
);
void
batching_generate_keys
(
size_t
poly_d
,
std
::
vector
<
int
>
bit_sizes
,
std
::
uint64_t
plain_modulus
,
std
::
string
key_dir
,
bool
serializable
=
false
);
/** for homomorphic operators management **/
void
init_operator
(
struct
encryptor_t
&
op_st
);
void
init_operator
(
struct
encryptor_t
&
op_st
,
const
std
::
string
&
public_key_path
);
void
init_operator_batching
(
struct
encryptor_t
&
op_st
,
const
std
::
string
&
key_dir
);
void
init_operator
(
struct
decryptor_t
&
op_st
);
void
init_operator
(
struct
decryptor_t
&
op_st
,
const
std
::
string
&
secret_key_path
);
void
init_operator_batching
(
struct
decryptor_t
&
op_st
,
const
std
::
string
&
key_dir
);
void
init_operator
(
struct
evaluator_t
&
op_st
);
void
init_operator
(
struct
evaluator_t
&
op_st
,
const
std
::
string
&
relink_key_path
);
void
init_operator_batching
(
struct
evaluator_t
&
op_st
,
const
std
::
string
&
key_dir
);
void
delete_operator
(
struct
encryptor_t
&
op_st
);
void
delete_operator_batching
(
struct
encryptor_t
&
op_st
);
void
delete_operator
(
struct
decryptor_t
&
op_st
);
void
delete_operator_batching
(
struct
decryptor_t
&
op_st
);
void
delete_operator
(
struct
evaluator_t
&
op_st
);
void
delete_operator_batching
(
struct
evaluator_t
&
op_st
);
/** for plaintexts and ciphertexts management **/
void
init_plaintext
(
struct
encryptor_t
&
op_st
,
int64_t
plain
,
seal
::
Plaintext
&
pt
);
void
init_ciphertext
(
struct
encryptor_t
&
op_st
,
int64_t
plain
,
seal
::
Ciphertext
&
ct
);
void
init_ciphermatrix
(
struct
encryptor_t
&
op_st
,
std
::
vector
<
int64_t
>
&
plain_matrix
,
seal
::
Ciphertext
&
encrypted_matrix
);
void
decrypt_ciphertext
(
struct
decryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
);
std
::
int64_t
decrypt_ciphertext_and_return_value
(
struct
decryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
);
std
::
vector
<
int64_t
>
decrypt_ciphermatrix
(
struct
decryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
);
/*** ckks plaintexts ***/
void
init_plaintext_ckks
(
seal
::
CKKSEncoder
&
encoder
,
double
scale
,
std
::
vector
<
double
>&
p_vector
,
seal
::
Plaintext
&
pt
,
bool
print_info
=
1
);
void
init_plaintext_ckks
(
struct
cencryptor_t
&
op_st
,
double
scale
,
std
::
vector
<
double
>&
p_vector
,
seal
::
Plaintext
&
pt
,
bool
print_info
=
1
);
void
init_plaintext_ckks
(
struct
cencryptor_t
&
op_st
,
std
::
vector
<
double
>&
p_vector
,
seal
::
Plaintext
&
pt
,
bool
print_info
=
1
);
void
init_plaintext_ckks
(
struct
cdecryptor_t
&
op_st
,
double
scale
,
std
::
vector
<
double
>&
p_vector
,
seal
::
Plaintext
&
pt
,
bool
print_info
=
1
);
void
init_plaintext_ckks
(
struct
cdecryptor_t
&
op_st
,
std
::
vector
<
double
>&
p_vector
,
seal
::
Plaintext
&
pt
,
bool
print_info
=
1
);
int
save_plaintext
(
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
save_ciphertext
(
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_plaintext
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_plaintext
(
struct
evaluator_t
&
op_st
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_plaintext
(
struct
decryptor_t
&
op_st
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_ciphertext
(
std
::
shared_ptr
<
seal
::
SEALContext
>&
context
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext
(
struct
evaluator_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext
(
struct
decryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext_ckks
(
struct
cevaluator_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext_ckks
(
struct
cdecryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
/** prototypes **/
/*** ckks opertors ***/
// int init_operator(struct cencryptor_t& op_st);
// int init_operator_ckks(struct cencryptor_t& op_st);
void
init_operator_ckks
(
struct
cencryptor_t
&
op_st
,
const
std
::
string
&
key_dir
);
void
init_operator_ckks
(
struct
cdecryptor_t
&
op_st
,
const
std
::
string
&
key_dir
);
void
init_operator_ckks
(
struct
cevaluator_t
&
op_st
,
const
std
::
string
&
key_dir
);
// int init_operator(struct cdecryptor_t& op_st);
// int init_operator(struct cevaluator_t& op_st);
void
delete_operator_ckks
(
struct
cencryptor_t
&
op_st
);
void
delete_operator_ckks
(
struct
cdecryptor_t
&
op_st
);
void
delete_operator_ckks
(
struct
cevaluator_t
&
op_st
);
// void delete_operator(struct cevaluator_t& op_st);
/*** ckks plaintexts ***/
// void init_plaintext(seal::CKKSEncoder& encoder, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cencryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cencryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cdecryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cdecryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
/*** ckks ciphertexts ***/
void
init_ciphertext_ckks
(
seal
::
CKKSEncoder
&
encoder
,
seal
::
Encryptor
&
encryptor
,
double
scale
,
std
::
vector
<
double
>&
c_vector
,
seal
::
Ciphertext
&
ct
);
void
init_ciphertext_ckks
(
struct
cencryptor_t
&
op_st
,
double
scale
,
std
::
vector
<
double
>&
c_vector
,
seal
::
Ciphertext
&
ct
);
void
init_ciphertext_ckks
(
struct
cencryptor_t
&
op_st
,
std
::
vector
<
double
>&
c_vector
,
seal
::
Ciphertext
&
ct
);
/*** ckks decryption ***/
void
decrypt_ciphertext_ckks
(
seal
::
CKKSEncoder
&
encoder
,
seal
::
Decryptor
&
decryptor
,
seal
::
Ciphertext
&
ct
,
std
::
vector
<
double
>&
pt_val
);
void
decrypt_ciphertext_ckks
(
struct
cdecryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
std
::
vector
<
double
>&
pt_val
);
// std::vector<double> decrypt_ciphermatrix_ckks(struct cdecryptor_t &op_st, seal::Ciphertext &ct);
/*** saving plaintexts and ciphertexts ***/
int
load_plaintext_ckks
(
struct
cevaluator_t
&
op_st
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_plaintext_ckks
(
struct
cencryptor_t
&
op_st
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_plaintext_ckks
(
struct
cdecryptor_t
&
op_st
,
seal
::
Plaintext
&
pt
,
const
std
::
string
&
filename
);
int
load_ciphertext_ckks
(
struct
cevaluator_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext_ckks
(
struct
cencryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
int
load_ciphertext_ckks
(
struct
cdecryptor_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
const
std
::
string
&
filename
);
void
generate_keys_ckks
(
size_t
poly_d
,
int
bit_size
,
const
std
::
string
key_path
,
bool
serializable
=
false
);
/*** sum ***/
// void sum_batch_ciphertext(struct cevaluator_t& op_st, std::vector<seal::Ciphertext>& ct, seal::Ciphertext& ct_out);
/*** testing ***/
// void test_ckks_batch_functions(const uint32_t poly_deg);
#endif
docker/bigpiseal3.5.1/native/examples/ANN/v1/test_v1.sh
0 → 100644
View file @
d8df7d47
mkdir
-p
keys/
mkdir
-p
lcheck/
mkdir
-p
parent/
mkdir
-p
result/
# rm ls.txt
# bash data_creating.sh 0 99 9 1000
# Number of driving licenses, eg: (0..99)
min
=
0
max
=
101
# max=1631
# max=101
# sim: (eval: 0.465s, 0.488s, 0.480s decrypt: 0.028s, 0.080s, 0.077s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=203
# (eval: 3.635s, 3.510s, 3.628s decrypt: 0.062s, 0.153s. 0.134s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=407
# (eval: 38.607s, 39.600s, 39.565s decrypt: 0.264s, 0.260s, 0.245s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=815
# (eval: x decrypt: x )
# Number of chars/words of a driving license, eg: 9
n_char
=
39
# Encrypted number range, eg: 10, 100, 1000 for 1 2 3 4 digits
range
=
255
# (8-bit)
# sampling size
sample
=
40
CURR_DIR
=
$(
cd
"
$(
dirname
"
${
BASH_SOURCE
[0]
}
"
)
"
&&
pwd
)
# Note
# gen keys
# ./ANN_genkey_v1 keys/
${
CURR_DIR
}
/ANN_genkey_v1 keys/
line
=
$((
RANDOM
%
max
))
lcheck
=
""
# gen data
for
i
in
$(
seq
$min
$max
)
do
mkdir
-p
parent/l
"
$i
"
/
echo
"Created folder
$i
"
value
=
""
for
j
in
$(
seq
0
$n_char
)
do
value+
=
"
$((
RANDOM
%
$range
))
"
done
echo
$value
echo
$value
>>
ls.txt
# ./ANN_encrypt_v1 "$value" "l" parent/l"$i"/ $sample keys/
${
CURR_DIR
}
/ANN_encrypt_v1
"
$value
"
"l"
parent/l
"
$i
"
/
$sample
keys/
if
[[
"
$line
"
-eq
"
$i
"
]]
then
lcheck
=
"
$value
"
fi
done
# get random lc
# lcheck=$(sed -n "$((RANDOM %9){p;q}" ls.txt)
# line=$((RANDOM %max))
# lcheck="$(sed -n "${line}{p;q}" ls.txt)"
echo
"Pick up license to check:
$lcheck
"
# ./ANN_encrypt_v1 "$lcheck" "l" lcheck/ $sample keys/
${
CURR_DIR
}
/ANN_encrypt_v1
"
$lcheck
"
"l"
lcheck/
$sample
keys/
# ./ANN_encrypt "47 25 76 23 30 21 1 47 88 3" "a" keys/bfv.pk lcheck/
# eval
value1
=
"lcheck/l.ct "
value2
=
""
for
i
in
$(
seq
0
$max
)
do
value2+
=
"parent/l
${
i
}
/l.ct "
done
# value2="${value1} ${value2}"
value3
=
"
$value1$value2
"
echo
"
$value3
"
# time ./ANN_evaluate_v1 $value3 "l" result/ $sample keys/
# ./ANN_evaluate_v1 $value3 "l" result/ $sample keys/
time
${
CURR_DIR
}
/ANN_evaluate_v1
$value3
"l"
result/
$sample
keys/
echo
""
# time ./ANN_decrypt_result_v1 result/l.ct $sample keys/
# ./ANN_decrypt_result_v1 result/l.ct $sample keys/
${
CURR_DIR
}
/ANN_decrypt_result_v1 result/l.ct
$sample
keys/
# rm -r keys/*
# rm -r lcheck/*
# rm -r parent/*
# rm -r result/*
# rm ls.txt
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.cpp
0 → 100644
View file @
d8df7d47
#include "util.h"
// #include <filesystem>
#include "seal_api.h"
// #include <boost/filesystem.hpp>
// namespace fs = boost::filesystem;
bool
sub_str_exist
(
const
std
::
string
&
str
,
const
std
::
string
&
sub_str
)
{
return
str
.
size
()
>=
sub_str
.
size
()
&&
str
.
compare
(
str
.
size
()
-
sub_str
.
size
(),
sub_str
.
size
(),
sub_str
)
==
0
;
}
// int findNumberOfFilesInDirectory(const std::string &path)
// {
// auto dirIter = std::filesystem::directory_iterator(path);
// int fileCount = std::count_if(
// begin(dirIter),
// end(dirIter),
// [](auto &entry) { return entry.is_regular_file(); });
// return fileCount;
// }
// int findNumberOfFilesInDirectory(std::string &path, std::string &ext)
// int findNumberOfFilesInDirectory(const std::string &path)
// {
// // namespace fs = boost::filesystem;
// std::string ext = ".ct";
// fs::path Path(path);
// int Nb_ext = 0;
// fs::directory_iterator end_iter; // Default constructor for an iterator is the end iterator
// for (fs::directory_iterator iter(Path); iter != end_iter; ++iter)
// if (iter->path().extension() == ext)
// ++Nb_ext;
// return Nb_ext;
// }
// c++ 17
// std::vector<std::string> get_directories(const std::string &s)
// {
// std::vector<std::string> r;
// for(auto& p : std::filesystem::recursive_directory_iterator(s))
// if (p.is_directory())
// r.push_back(p.path().string());
// return r;
// }
// struct path_leaf_string
// {
// std::string operator()(const boost::filesystem::directory_entry &entry) const
// {
// return entry.path().leaf().string();
// }
// };
// std::vector<std::string> get_directories(const std::string &s)
// {
// std::vector<std::string> v;
// boost::filesystem::path p(s);
// boost::filesystem::directory_iterator start(p);
// boost::filesystem::directory_iterator end;
// std::transform(start, end, std::back_inserter(v), path_leaf_string());
// std::copy(v.begin(), v.end(),
// std::ostream_iterator<std::string>(std::cout, "\n"));
// return v;
// }
docker/bigpiseal3.5.1/native/examples/ANN/v1/util.h
0 → 100644
View file @
d8df7d47
#ifndef _UTIL_H_
#define _UTIL_H_
#include <string>
#include <vector>
// int findNumberOfFilesInDirectory(const std::string &path);
bool
sub_str_exist
(
const
std
::
string
&
str
,
const
std
::
string
&
sub_str
);
// std::vector<std::string> get_directories(const std::string &s);
// std::vector<std::string> get_directories_deep(const std::string &s);
#endif
docker/bigpiseal3.5.1/native/examples/CMakeLists.txt
0 → 100644
View file @
d8df7d47
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
target_sources
(
sealexamples
PRIVATE
${
CMAKE_CURRENT_LIST_DIR
}
/examples.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/1_bfv_basics.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/2_encoders.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/3_levels.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/4_ckks_basics.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/5_rotation.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/6_serialization.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/7_performance.cpp
)
\ No newline at end of file
docker/bigpiseal3.5.1/native/examples/SEALExamples.vcxproj
0 → 100644
View file @
d8df7d47
<?xml version="1.0" encoding="utf-8"?>
<Project
DefaultTargets=
"Build"
ToolsVersion=
"15.0"
xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003"
>
<ItemGroup
Label=
"ProjectConfigurations"
>
<ProjectConfiguration
Include=
"Debug|Win32"
>
<Configuration>
Debug
</Configuration>
<Platform>
Win32
</Platform>
</ProjectConfiguration>
<ProjectConfiguration
Include=
"Debug|x64"
>
<Configuration>
Debug
</Configuration>
<Platform>
x64
</Platform>
</ProjectConfiguration>
<ProjectConfiguration
Include=
"Release|Win32"
>
<Configuration>
Release
</Configuration>
<Platform>
Win32
</Platform>
</ProjectConfiguration>
<ProjectConfiguration
Include=
"Release|x64"
>
<Configuration>
Release
</Configuration>
<Platform>
x64
</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup
Label=
"Globals"
>
<ProjectGuid>
{2B57D847-26DC-45FF-B9AF-EE33910B5093}
</ProjectGuid>
<Keyword>
Win32Proj
</Keyword>
<RootNamespace>
SEALExamples
</RootNamespace>
<WindowsTargetPlatformVersion>
10.0
</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.Default.props"
/>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
Label=
"Configuration"
>
<ConfigurationType>
Application
</ConfigurationType>
<UseDebugLibraries>
true
</UseDebugLibraries>
<PlatformToolset>
v142
</PlatformToolset>
<CharacterSet>
Unicode
</CharacterSet>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label=
"Configuration"
>
<ConfigurationType>
Application
</ConfigurationType>
<UseDebugLibraries>
true
</UseDebugLibraries>
<PlatformToolset>
v142
</PlatformToolset>
<CharacterSet>
Unicode
</CharacterSet>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
Label=
"Configuration"
>
<ConfigurationType>
Application
</ConfigurationType>
<UseDebugLibraries>
false
</UseDebugLibraries>
<PlatformToolset>
v142
</PlatformToolset>
<WholeProgramOptimization>
true
</WholeProgramOptimization>
<CharacterSet>
Unicode
</CharacterSet>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
Label=
"Configuration"
>
<ConfigurationType>
Application
</ConfigurationType>
<UseDebugLibraries>
false
</UseDebugLibraries>
<PlatformToolset>
v142
</PlatformToolset>
<WholeProgramOptimization>
true
</WholeProgramOptimization>
<CharacterSet>
Unicode
</CharacterSet>
</PropertyGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.props"
/>
<ImportGroup
Label=
"ExtensionSettings"
>
</ImportGroup>
<ImportGroup
Label=
"Shared"
>
</ImportGroup>
<ImportGroup
Label=
"PropertySheets"
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
</ImportGroup>
<ImportGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label=
"PropertySheets"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
</ImportGroup>
<ImportGroup
Label=
"PropertySheets"
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
</ImportGroup>
<ImportGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
Label=
"PropertySheets"
>
<Import
Project=
"$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
Condition=
"exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
Label=
"LocalAppDataPlatform"
/>
</ImportGroup>
<PropertyGroup
Label=
"UserMacros"
/>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
<LinkIncremental>
true
</LinkIncremental>
<OutDir>
$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\
</OutDir>
<IntDir>
$(ProjectDir)obj\$(Platform)\$(Configuration)\
</IntDir>
<TargetName>
sealexamples
</TargetName>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<LinkIncremental>
true
</LinkIncremental>
<OutDir>
$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\
</OutDir>
<IntDir>
$(ProjectDir)obj\$(Platform)\$(Configuration)\
</IntDir>
<TargetName>
sealexamples
</TargetName>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
<LinkIncremental>
false
</LinkIncremental>
<OutDir>
$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\
</OutDir>
<IntDir>
$(ProjectDir)obj\$(Platform)\$(Configuration)\
</IntDir>
<TargetName>
sealexamples
</TargetName>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
<LinkIncremental>
false
</LinkIncremental>
<OutDir>
$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\
</OutDir>
<IntDir>
$(ProjectDir)obj\$(Platform)\$(Configuration)\
</IntDir>
<TargetName>
sealexamples
</TargetName>
</PropertyGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<Optimization>
Disabled
</Optimization>
<PreprocessorDefinitions>
_ENABLE_EXTENDED_ALIGNED_STORAGE
</PreprocessorDefinitions>
<SDLCheck>
true
</SDLCheck>
<AdditionalIncludeDirectories>
$(SolutionDir)native\src
</AdditionalIncludeDirectories>
<LanguageStandard>
stdcpp17
</LanguageStandard>
<AdditionalOptions>
/Zc:__cplusplus %(AdditionalOptions)
</AdditionalOptions>
<ControlFlowGuard>
Guard
</ControlFlowGuard>
<DebugInformationFormat>
ProgramDatabase
</DebugInformationFormat>
<MultiProcessorCompilation>
true
</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>
true
</GenerateDebugInformation>
<AdditionalLibraryDirectories>
$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)
</AdditionalLibraryDirectories>
<AdditionalDependencies>
seal.lib;%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<Optimization>
Disabled
</Optimization>
<PreprocessorDefinitions>
_ENABLE_EXTENDED_ALIGNED_STORAGE
</PreprocessorDefinitions>
<SDLCheck>
true
</SDLCheck>
<AdditionalIncludeDirectories>
$(SolutionDir)native\src
</AdditionalIncludeDirectories>
<LanguageStandard>
stdcpp17
</LanguageStandard>
<AdditionalOptions>
/Zc:__cplusplus %(AdditionalOptions)
</AdditionalOptions>
<ControlFlowGuard>
Guard
</ControlFlowGuard>
<DebugInformationFormat>
ProgramDatabase
</DebugInformationFormat>
<MultiProcessorCompilation>
true
</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>
true
</GenerateDebugInformation>
<AdditionalLibraryDirectories>
$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)
</AdditionalLibraryDirectories>
<AdditionalDependencies>
seal.lib;%(AdditionalDependencies)
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<Optimization>
MaxSpeed
</Optimization>
<FunctionLevelLinking>
true
</FunctionLevelLinking>
<IntrinsicFunctions>
true
</IntrinsicFunctions>
<PreprocessorDefinitions>
_ENABLE_EXTENDED_ALIGNED_STORAGE
</PreprocessorDefinitions>
<SDLCheck>
true
</SDLCheck>
<AdditionalIncludeDirectories>
$(SolutionDir)native\src
</AdditionalIncludeDirectories>
<LanguageStandard>
stdcpp17
</LanguageStandard>
<AdditionalOptions>
/Zc:__cplusplus %(AdditionalOptions)
</AdditionalOptions>
<ControlFlowGuard>
Guard
</ControlFlowGuard>
<MultiProcessorCompilation>
true
</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>
true
</GenerateDebugInformation>
<EnableCOMDATFolding>
true
</EnableCOMDATFolding>
<OptimizeReferences>
true
</OptimizeReferences>
<AdditionalLibraryDirectories>
$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)
</AdditionalLibraryDirectories>
<AdditionalDependencies>
seal.lib;%(AdditionalDependencies)
</AdditionalDependencies>
<Profile>
true
</Profile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<Optimization>
MaxSpeed
</Optimization>
<FunctionLevelLinking>
true
</FunctionLevelLinking>
<IntrinsicFunctions>
true
</IntrinsicFunctions>
<PreprocessorDefinitions>
_ENABLE_EXTENDED_ALIGNED_STORAGE
</PreprocessorDefinitions>
<SDLCheck>
true
</SDLCheck>
<AdditionalIncludeDirectories>
$(SolutionDir)native\src
</AdditionalIncludeDirectories>
<LanguageStandard>
stdcpp17
</LanguageStandard>
<AdditionalOptions>
/Zc:__cplusplus %(AdditionalOptions)
</AdditionalOptions>
<ControlFlowGuard>
Guard
</ControlFlowGuard>
<MultiProcessorCompilation>
true
</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>
true
</GenerateDebugInformation>
<EnableCOMDATFolding>
true
</EnableCOMDATFolding>
<OptimizeReferences>
true
</OptimizeReferences>
<AdditionalLibraryDirectories>
$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)
</AdditionalLibraryDirectories>
<AdditionalDependencies>
seal.lib;%(AdditionalDependencies)
</AdditionalDependencies>
<Profile>
true
</Profile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile
Include=
"1_bfv_basics.cpp"
/>
<ClCompile
Include=
"2_encoders.cpp"
/>
<ClCompile
Include=
"4_ckks_basics.cpp"
/>
<ClCompile
Include=
"5_rotation.cpp"
/>
<ClCompile
Include=
"3_levels.cpp"
/>
<ClCompile
Include=
"6_serialization.cpp"
/>
<ClCompile
Include=
"7_performance.cpp"
/>
<ClCompile
Include=
"examples.cpp"
/>
</ItemGroup>
<ItemGroup>
<Text
Include=
"CMakeLists.txt"
/>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"examples.h"
/>
</ItemGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
/>
</Project>
\ No newline at end of file
docker/bigpiseal3.5.1/native/examples/SEALExamples.vcxproj.filters
0 → 100644
View file @
d8df7d47
<?xml version="1.0" encoding="utf-8"?>
<Project
ToolsVersion=
"4.0"
xmlns=
"http://schemas.microsoft.com/developer/msbuild/2003"
>
<ItemGroup>
<Filter
Include=
"Source Files"
>
<UniqueIdentifier>
{4FC737F1-C7A5-4376-A066-2A32D752A2FF}
</UniqueIdentifier>
<Extensions>
cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
</Extensions>
</Filter>
<Filter
Include=
"Header Files"
>
<UniqueIdentifier>
{93995380-89BD-4b04-88EB-625FBE52EBFB}
</UniqueIdentifier>
<Extensions>
h;hh;hpp;hxx;hm;inl;inc;xsd
</Extensions>
</Filter>
<Filter
Include=
"Other"
>
<UniqueIdentifier>
{abd2e216-316f-4dad-a2a4-a72ffccfd92b}
</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"examples.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"3_levels.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"5_rotation.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"1_bfv_basics.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"2_encoders.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"4_ckks_basics.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"7_performance.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"6_serialization.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text
Include=
"CMakeLists.txt"
>
<Filter>
Other
</Filter>
</Text>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"examples.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
docker/bigpiseal3.5.1/native/examples/examples.cpp
0 → 100644
View file @
d8df7d47
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "examples.h"
using
namespace
std
;
using
namespace
seal
;
int
main
()
{
cout
<<
"Microsoft SEAL version: "
<<
SEAL_VERSION
<<
endl
;
while
(
true
)
{
cout
<<
"+---------------------------------------------------------+"
<<
endl
;
cout
<<
"| The following examples should be executed while reading |"
<<
endl
;
cout
<<
"| comments in associated files in native/examples/. |"
<<
endl
;
cout
<<
"+---------------------------------------------------------+"
<<
endl
;
cout
<<
"| Examples | Source Files |"
<<
endl
;
cout
<<
"+----------------------------+----------------------------+"
<<
endl
;
cout
<<
"| 1. BFV Basics | 1_bfv_basics.cpp |"
<<
endl
;
cout
<<
"| 2. Encoders | 2_encoders.cpp |"
<<
endl
;
cout
<<
"| 3. Levels | 3_levels.cpp |"
<<
endl
;
cout
<<
"| 4. CKKS Basics | 4_ckks_basics.cpp |"
<<
endl
;
cout
<<
"| 5. Rotation | 5_rotation.cpp |"
<<
endl
;
cout
<<
"| 6. Serialization | 6_serialization.cpp |"
<<
endl
;
cout
<<
"| 7. Performance Test | 7_performance.cpp |"
<<
endl
;
cout
<<
"+----------------------------+----------------------------+"
<<
endl
;
/*
Print how much memory we have allocated from the current memory pool.
By default the memory pool will be a static global pool and the
MemoryManager class can be used to change it. Most users should have
little or no reason to touch the memory allocation system.
*/
size_t
megabytes
=
MemoryManager
::
GetPool
().
alloc_byte_count
()
>>
20
;
cout
<<
"["
<<
setw
(
7
)
<<
right
<<
megabytes
<<
" MB] "
<<
"Total allocation from the memory pool"
<<
endl
;
int
selection
=
0
;
bool
invalid
=
true
;
do
{
cout
<<
endl
<<
"> Run example (1 ~ 7) or exit (0): "
;
if
(
!
(
cin
>>
selection
))
{
invalid
=
false
;
}
else
if
(
selection
<
0
||
selection
>
7
)
{
invalid
=
false
;
}
else
{
invalid
=
true
;
}
if
(
!
invalid
)
{
cout
<<
" [Beep~~] Invalid option: type 0 ~ 7"
<<
endl
;
cin
.
clear
();
cin
.
ignore
(
numeric_limits
<
streamsize
>::
max
(),
'\n'
);
}
}
while
(
!
invalid
);
switch
(
selection
)
{
case
1
:
example_bfv_basics
();
break
;
case
2
:
example_encoders
();
break
;
case
3
:
example_levels
();
break
;
case
4
:
example_ckks_basics
();
break
;
case
5
:
example_rotation
();
break
;
case
6
:
example_serialization
();
break
;
case
7
:
example_performance_test
();
break
;
case
0
:
return
0
;
}
}
return
0
;
}
docker/bigpiseal3.5.1/native/examples/examples.h
0 → 100644
View file @
d8df7d47
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/seal.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <memory>
#include <mutex>
#include <numeric>
#include <random>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
void
example_bfv_basics
();
void
example_encoders
();
void
example_levels
();
void
example_ckks_basics
();
void
example_rotation
();
void
example_serialization
();
void
example_performance_test
();
/*
Helper function: Prints the name of the example in a fancy banner.
*/
inline
void
print_example_banner
(
std
::
string
title
)
{
if
(
!
title
.
empty
())
{
std
::
size_t
title_length
=
title
.
length
();
std
::
size_t
banner_length
=
title_length
+
2
*
10
;
std
::
string
banner_top
=
"+"
+
std
::
string
(
banner_length
-
2
,
'-'
)
+
"+"
;
std
::
string
banner_middle
=
"|"
+
std
::
string
(
9
,
' '
)
+
title
+
std
::
string
(
9
,
' '
)
+
"|"
;
std
::
cout
<<
std
::
endl
<<
banner_top
<<
std
::
endl
<<
banner_middle
<<
std
::
endl
<<
banner_top
<<
std
::
endl
;
}
}
/*
Helper function: Prints the parameters in a SEALContext.
*/
inline
void
print_parameters
(
std
::
shared_ptr
<
seal
::
SEALContext
>
context
)
{
// Verify parameters
if
(
!
context
)
{
throw
std
::
invalid_argument
(
"context is not set"
);
}
auto
&
context_data
=
*
context
->
key_context_data
();
/*
Which scheme are we using?
*/
std
::
string
scheme_name
;
switch
(
context_data
.
parms
().
scheme
())
{
case
seal
::
scheme_type
::
BFV
:
scheme_name
=
"BFV"
;
break
;
case
seal
::
scheme_type
::
CKKS
:
scheme_name
=
"CKKS"
;
break
;
default:
throw
std
::
invalid_argument
(
"unsupported scheme"
);
}
std
::
cout
<<
"/"
<<
std
::
endl
;
std
::
cout
<<
"| Encryption parameters :"
<<
std
::
endl
;
std
::
cout
<<
"| scheme: "
<<
scheme_name
<<
std
::
endl
;
std
::
cout
<<
"| poly_modulus_degree: "
<<
context_data
.
parms
().
poly_modulus_degree
()
<<
std
::
endl
;
/*
Print the size of the true (product) coefficient modulus.
*/
std
::
cout
<<
"| coeff_modulus size: "
;
std
::
cout
<<
context_data
.
total_coeff_modulus_bit_count
()
<<
" ("
;
auto
coeff_modulus
=
context_data
.
parms
().
coeff_modulus
();
std
::
size_t
coeff_modulus_size
=
coeff_modulus
.
size
();
for
(
std
::
size_t
i
=
0
;
i
<
coeff_modulus_size
-
1
;
i
++
)
{
std
::
cout
<<
coeff_modulus
[
i
].
bit_count
()
<<
" + "
;
}
std
::
cout
<<
coeff_modulus
.
back
().
bit_count
();
std
::
cout
<<
") bits"
<<
std
::
endl
;
/*
For the BFV scheme print the plain_modulus parameter.
*/
if
(
context_data
.
parms
().
scheme
()
==
seal
::
scheme_type
::
BFV
)
{
std
::
cout
<<
"| plain_modulus: "
<<
context_data
.
parms
().
plain_modulus
().
value
()
<<
std
::
endl
;
}
std
::
cout
<<
"
\\
"
<<
std
::
endl
;
}
/*
Helper function: Prints the `parms_id' to std::ostream.
*/
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
stream
,
seal
::
parms_id_type
parms_id
)
{
/*
Save the formatting information for std::cout.
*/
std
::
ios
old_fmt
(
nullptr
);
old_fmt
.
copyfmt
(
std
::
cout
);
stream
<<
std
::
hex
<<
std
::
setfill
(
'0'
)
<<
std
::
setw
(
16
)
<<
parms_id
[
0
]
<<
" "
<<
std
::
setw
(
16
)
<<
parms_id
[
1
]
<<
" "
<<
std
::
setw
(
16
)
<<
parms_id
[
2
]
<<
" "
<<
std
::
setw
(
16
)
<<
parms_id
[
3
]
<<
" "
;
/*
Restore the old std::cout formatting.
*/
std
::
cout
.
copyfmt
(
old_fmt
);
return
stream
;
}
/*
Helper function: Prints a vector of floating-point values.
*/
template
<
typename
T
>
inline
void
print_vector
(
std
::
vector
<
T
>
vec
,
std
::
size_t
print_size
=
4
,
int
prec
=
3
)
{
/*
Save the formatting information for std::cout.
*/
std
::
ios
old_fmt
(
nullptr
);
old_fmt
.
copyfmt
(
std
::
cout
);
std
::
size_t
slot_count
=
vec
.
size
();
std
::
cout
<<
std
::
fixed
<<
std
::
setprecision
(
prec
);
std
::
cout
<<
std
::
endl
;
if
(
slot_count
<=
2
*
print_size
)
{
std
::
cout
<<
" ["
;
for
(
std
::
size_t
i
=
0
;
i
<
slot_count
;
i
++
)
{
std
::
cout
<<
" "
<<
vec
[
i
]
<<
((
i
!=
slot_count
-
1
)
?
","
:
" ]
\n
"
);
}
}
else
{
vec
.
resize
(
std
::
max
(
vec
.
size
(),
2
*
print_size
));
std
::
cout
<<
" ["
;
for
(
std
::
size_t
i
=
0
;
i
<
print_size
;
i
++
)
{
std
::
cout
<<
" "
<<
vec
[
i
]
<<
","
;
}
if
(
vec
.
size
()
>
2
*
print_size
)
{
std
::
cout
<<
" ...,"
;
}
for
(
std
::
size_t
i
=
slot_count
-
print_size
;
i
<
slot_count
;
i
++
)
{
std
::
cout
<<
" "
<<
vec
[
i
]
<<
((
i
!=
slot_count
-
1
)
?
","
:
" ]
\n
"
);
}
}
std
::
cout
<<
std
::
endl
;
/*
Restore the old std::cout formatting.
*/
std
::
cout
.
copyfmt
(
old_fmt
);
}
/*
Helper function: Prints a matrix of values.
*/
template
<
typename
T
>
inline
void
print_matrix
(
std
::
vector
<
T
>
matrix
,
std
::
size_t
row_size
)
{
/*
We're not going to print every column of the matrix (there are 2048). Instead
print this many slots from beginning and end of the matrix.
*/
std
::
size_t
print_size
=
5
;
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
" ["
;
for
(
std
::
size_t
i
=
0
;
i
<
print_size
;
i
++
)
{
std
::
cout
<<
std
::
setw
(
3
)
<<
std
::
right
<<
matrix
[
i
]
<<
","
;
}
std
::
cout
<<
std
::
setw
(
3
)
<<
" ...,"
;
for
(
std
::
size_t
i
=
row_size
-
print_size
;
i
<
row_size
;
i
++
)
{
std
::
cout
<<
std
::
setw
(
3
)
<<
matrix
[
i
]
<<
((
i
!=
row_size
-
1
)
?
","
:
" ]
\n
"
);
}
std
::
cout
<<
" ["
;
for
(
std
::
size_t
i
=
row_size
;
i
<
row_size
+
print_size
;
i
++
)
{
std
::
cout
<<
std
::
setw
(
3
)
<<
matrix
[
i
]
<<
","
;
}
std
::
cout
<<
std
::
setw
(
3
)
<<
" ...,"
;
for
(
std
::
size_t
i
=
2
*
row_size
-
print_size
;
i
<
2
*
row_size
;
i
++
)
{
std
::
cout
<<
std
::
setw
(
3
)
<<
matrix
[
i
]
<<
((
i
!=
2
*
row_size
-
1
)
?
","
:
" ]
\n
"
);
}
std
::
cout
<<
std
::
endl
;
}
/*
Helper function: Print line number.
*/
inline
void
print_line
(
int
line_number
)
{
std
::
cout
<<
"Line "
<<
std
::
setw
(
3
)
<<
line_number
<<
" --> "
;
}
docker/bigpiseal3.5.1/native/examples/generic/CMakeLists.txt
0 → 100644
View file @
d8df7d47
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
cmake_minimum_required
(
VERSION 3.10
)
set
(
TEST_NAME generic
)
set
(
GENKEY_BIN
${
TEST_NAME
}
_genkey
)
set
(
ENCR_BIN
${
TEST_NAME
}
_encrypt
)
set
(
DECR_BIN
${
TEST_NAME
}
_decrypt
)
set
(
EVAL_BIN
${
TEST_NAME
}
_evaluate
)
set
(
EVAL_BIN_MUL
${
TEST_NAME
}
_evaluate_multiply
)
set
(
EVAL_BIN_SUB
${
TEST_NAME
}
_evaluate_sub
)
set
(
SCR_ENC encrypt_gen.sh
)
set
(
SCR_DEC decrypt_gen.sh
)
set
(
SCR_GEN genkey_gen.sh
)
set
(
GENKEY_SRCS
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_genkey.cpp
)
set
(
ENCR_SRCS
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_encrypt.cpp
)
set
(
DECR_SRCS
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_decrypt.cpp
)
set
(
EVAL_SRCS
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/add.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_evaluate.cpp
)
set
(
EVAL_SRCS_SUB
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/sub.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_evaluate_sub.cpp
)
set
(
EVAL_SRCS_MUL
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/multiply.cpp
${
CMAKE_CURRENT_LIST_DIR
}
/
${
TEST_NAME
}
_evaluate_multiply.cpp
)
set
(
HEADER_DIR
${
CMAKE_SOURCE_DIR
}
/
${
TEST_NAME
}
)
set
(
HEADER_FILES
${
CMAKE_CURRENT_LIST_DIR
}
/seal_api.h
${
CMAKE_CURRENT_LIST_DIR
}
/add.h
)
add_executable
(
${
GENKEY_BIN
}
${
GENKEY_SRCS
}
${
HEADER_FILES
}
)
add_executable
(
${
ENCR_BIN
}
${
ENCR_SRCS
}
${
HEADER_FILES
}
)
add_executable
(
${
DECR_BIN
}
${
DECR_SRCS
}
${
HEADER_FILES
}
)
add_executable
(
${
EVAL_BIN
}
${
EVAL_SRCS
}
${
HEADER_FILES
}
)
add_executable
(
${
EVAL_BIN_SUB
}
${
EVAL_SRCS_SUB
}
${
HEADER_FILES
}
)
add_executable
(
${
EVAL_BIN_MUL
}
${
EVAL_SRCS_MUL
}
${
HEADER_FILES
}
)
#target_include_directories(${TEST_NAME} PRIVATE ${HEADER_DIR})
# Import Microsoft SEAL
find_package
(
SEAL 3.5 REQUIRED
)
# Link Microsoft SEAL
target_link_libraries
(
${
GENKEY_BIN
}
SEAL::seal
)
target_link_libraries
(
${
ENCR_BIN
}
SEAL::seal
)
target_link_libraries
(
${
DECR_BIN
}
SEAL::seal
)
target_link_libraries
(
${
EVAL_BIN
}
SEAL::seal
)
target_link_libraries
(
${
EVAL_BIN_SUB
}
SEAL::seal
)
target_link_libraries
(
${
EVAL_BIN_MUL
}
SEAL::seal
)
set
(
CMAKE_RUNTIME_OUTPUT_DIRECTORY
${
CMAKE_SOURCE_DIR
}
/bin
)
set_target_properties
(
${
GENKEY_BIN
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
set_target_properties
(
${
ENCR_BIN
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
set_target_properties
(
${
DECR_BIN
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
set_target_properties
(
${
EVAL_BIN
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
set_target_properties
(
${
EVAL_BIN_SUB
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
set_target_properties
(
${
EVAL_BIN_MUL
}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
"
)
file
(
COPY
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
SCR_ENC
}
DESTINATION
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
)
file
(
COPY
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
SCR_DEC
}
DESTINATION
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
)
file
(
COPY
${
CMAKE_CURRENT_SOURCE_DIR
}
/
${
SCR_GEN
}
DESTINATION
${
CMAKE_RUNTIME_OUTPUT_DIRECTORY
}
/
${
TEST_NAME
}
)
docker/bigpiseal3.5.1/native/examples/generic/add.cpp
0 → 100644
View file @
d8df7d47
#include "add.h"
using
namespace
std
;
using
namespace
seal
;
void
add_ciphertext
(
struct
evaluator_t
&
op_st
,
Ciphertext
&
ct
,
Plaintext
&
pt
,
Ciphertext
&
ct_out
)
{
timeval
t0
,
t1
;
unsigned
long
dt
=
0
;
gettimeofday
(
&
t0
,
NULL
);
op_st
.
eval
->
add_plain
(
ct
,
pt
,
ct_out
);
gettimeofday
(
&
t1
,
NULL
);
dt
=
1000000
*
(
t1
.
tv_sec
-
t0
.
tv_sec
)
+
(
t1
.
tv_usec
-
t0
.
tv_usec
);
cout
<<
"[INFO] Homomorphic addition (pt+ct) time in (us): "
<<
dt
<<
endl
;
}
void
add_ciphertext
(
struct
evaluator_t
&
op_st
,
Ciphertext
&
ct1
,
Ciphertext
&
ct2
,
Ciphertext
&
ct_out
)
{
timeval
t0
,
t1
;
unsigned
long
dt
=
0
;
gettimeofday
(
&
t0
,
NULL
);
op_st
.
eval
->
add
(
ct1
,
ct2
,
ct_out
);
gettimeofday
(
&
t1
,
NULL
);
dt
=
1000000
*
(
t1
.
tv_sec
-
t0
.
tv_sec
)
+
(
t1
.
tv_usec
-
t0
.
tv_usec
);
cout
<<
"[INFO] Homomorphic addition (ct+ct) time in (us): "
<<
dt
<<
endl
;
}
docker/bigpiseal3.5.1/native/examples/generic/add.h
0 → 100644
View file @
d8df7d47
#ifndef _ADD_H_
#define _ADD_H_
#include <sys/time.h>
#include <iostream>
#include <string>
#include "seal/seal.h"
#include "seal_api.h"
void
add_ciphertext
(
struct
evaluator_t
&
op_st
,
seal
::
Ciphertext
&
ct
,
seal
::
Plaintext
&
pt
,
seal
::
Ciphertext
&
ct_out
);
void
add_ciphertext
(
struct
evaluator_t
&
op_st
,
seal
::
Ciphertext
&
ct1
,
seal
::
Ciphertext
&
ct2
,
seal
::
Ciphertext
&
ct_out
);
#endif
docker/bigpiseal3.5.1/native/examples/generic/decrypt_gen.sh
0 → 100644
View file @
d8df7d47
#!/bin/bash
#
# 2020 CEA LIST.
declare
-r
ERROR_NUMBER_PARAM
=
1
declare
-r
ERROR_VALUE_PARAM
=
2
declare
-r
ERROR_KEY_NOT_FOUND
=
3
declare
-r
ERRROR_ENCRYPT_DECRYPT_NOT_PERFORMED
=
4
declare
-r
ERROR_FILE_ZIP_NOT
FOUND
=
5
CURR_DIR
=
$(
cd
"
$(
dirname
"
${
BASH_SOURCE
[0]
}
"
)
"
&&
pwd
)
# /opt/seal/native/bin
# /opt/pki/seal/
# /opt/pki/seal/natives/examples/generic
# seal preparation executable seal_generic_decrypt
# code genéré pour 63bits signés.
# params
# 1 size_bit 1 63
# 2 signed 0 unsigned 1 signed
# 3 path_to_key .pk
# 4 path_to_storage
# 5 prefix
if
[
"$#"
-ne
5
]
;
then
echo
"You must enter exactly 5 command line arguments"
echo
"size bit <1..63>, signed <0,1> , path to sk , path to storage, prefix "
exit
1
fi
# size bit 1 63
case
$1
in
''
|
*
[!
0-9]
*
)
echo
only numeric size 1..63
;;
*
)
echo
numeric
if
[
"
$1
"
-ge
1
-a
"
$1
"
-le
63
]
;
then
echo
"size input Ok"
else
echo echo
"ERROR_SIZING VALUE "
$1
",size bit is 1..63"
exit
2
fi
;;
esac
size_bit
=
$1
# signed 0 or 1
case
$2
in
''
|
*
[!
0-1]
*
)
echo
bad only 0 or 1
for
signed value
;
exit
2
;;
*
)
echo
Signed value OK
;;
esac
signed
=
$2
# path to sk an key existence
# if not found error
path_to_key
=
$3
# si $3 contient .sk à la fin, alors c'est correct sinon
# si cest directory alors ajouter bfv.sk a la fin
# si c'est un fichier, verifier l'existence
if
[[
"
$path_to_key
"
==
*
/
]]
then
file
=
"
$path_to_key
"
bfv.sk
else
file
=
"
$path_to_key
"
/bfv.sk
fi
if
test
-f
"
$file
"
;
then
echo
"
$file
exist"
else
echo
" ERROR_KEY_NOT_FOUND "
$3
/bfv.sk
" not found"
$file
exit
3
fi
# path to result
path_to_storage
=
$4
if
[[
"
$path_to_storage
"
==
*
/
]]
then
file
=
"
$path_to_storage
"
else
file
=
"
$path_to_storage
"
/
fi
prefix
=
$5
if
[
-d
"
$path_to_storage
"
]
then
echo
1
else
echo
3
#~ extract_zip
fi
${
CURR_DIR
}
/generic_decrypt
$prefix
$path_to_key
$path_to_storage
# ./generic_decrypt ct2 /home/bigpi/ storage/
# recuperate result
if
[
"
$?
"
==
"0"
]
;
then
echo
ok
done
else
echo
DECRYPTION_NOT_PERFORMED
exit
4
fi
docker/bigpiseal3.5.1/native/examples/generic/encrypt_gen.sh
0 → 100644
View file @
d8df7d47
#!/bin/bash
#
# 2020 CEA LIST.
declare
-r
ERROR_NUMBER_PARAM
=
1
declare
-r
ERROR_VALUE_PARAM
=
2
declare
-r
ERROR_KEY_NOT_FOUND
=
3
declare
-r
ERRROR_ENCRYPT_DECRYPT_NOT_PERFORMED
=
4
declare
-r
ERROR_FILE_ZIP_NOTFOUND
=
5
#/opt/seal/native/bin
# /opt/pki/seal/
# /opt/pki/seal/natives/examples/generic
CURR_DIR
=
$(
cd
"
$(
dirname
"
${
BASH_SOURCE
[0]
}
"
)
"
&&
pwd
)
# params executable seal_generic_encrypt
# value
# size_bit 1 63 , limited to 63 bits Seal -ou 2-**63signed max=x value to test
# signed 0 unsigned 1 signed
# path_to_key existing key pk encrypt
# path_storage
if
[
"$#"
-ne
6
]
;
then
echo
"You must enter exactly 6 command line arguments"
# display help if empty
echo
"value , size bit <1..63>, signed <0,1> , path to sk , path to storage, prefix "
exit
$ERROR_NUMBER_PARAM
fi
[
-n
"
$1
"
]
&&
[
"
$1
"
-eq
"
$1
"
]
2>/dev/null
if
[
$?
-ne
0
]
;
then
echo
$1
is not number
exit
2
fi
[
-n
"
$2
"
]
&&
[
"
$2
"
-eq
"
$2
"
]
2>/dev/null
if
[
$?
-ne
0
]
;
then
echo
$2
is not number
exit
2
else
if
[
"
$2
"
-ge
1
-a
"
$2
"
-le
63
]
;
then
echo
"size input Ok"
else
echo
"ERROR_SIZING VALUE "
$2
",size bit is 1..63"
exit
2
fi
fi
value
=
$1
size_bit
=
$2
case
$3
in
''
|
*
[!
0-1]
*
)
echo
bad signed value
$3
, only 0 or 1
;
exit
2
;;
*
)
echo
Signed value OK
;;
esac
Signed
=
$3
path_to_key
=
$4
if
[[
"
$path_to_key
"
==
*
/
]]
then
file
=
"
$path_to_key
"
bfv.pk
else
file
=
"
$path_to_key
"
/bfv.pk
fi
if
test
-f
"
$file
"
;
then
echo
"
$file
exist"
else
echo
"ERROR_KEY_NOT_FOUND "
$4
/bfv.pk
" not found"
exit
3
fi
directory
=
$5
if
[
-d
"
$directory
"
]
then
echo
"OK"
else
echo
" Path to storage directory "
$5
" not found"
mkdir
$directory
#
echo
" path to storage created"
fi
path_storage
=
$5
prefix
=
$6
defaut_coherence_test_value_sizebit_seal
()
{
# define max value equal 2*2 ..*2 size_bit time
((
X
=(
2
**
$size_bit
)
-1
))
;
echo
"max value"
$X
((
Y
=(
2
**
63
)
-1
))
;
echo
"max value 2**63 -1"
$Y
# let "maxvalue=1"
# for i in $(seq $1 $size_bit)
# do
# let "maxvalue=maxvalue*2"
# done
if
[
"
$value
"
-gt
"
$Y
"
]
;
then
echo
" value to high or equal"
,
$value
,
" to max value"
$Y
exit
2
fi
}
defaut_coherence_test_value_sizebit_seal
# encryption
echo
"encryption"
${
CURR_DIR
}
/generic_encrypt
$value
$prefix
$path_to_key
$path_storage
# recuperate result
if
[
"
$?
"
==
"0"
]
;
then
echo
ok
done
else
echo
ENCRYPTION_NOT_PERFORMED
exit
4
fi
exit
docker/bigpiseal3.5.1/native/examples/generic/generic_decrypt.cpp
0 → 100644
View file @
d8df7d47
#include <sys/time.h>
#include "seal_api.h"
using
namespace
std
;
using
namespace
seal
;
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
4
)
cout
<<
"[ERROR] please enter prefix_file_to_decrypt full/path/key /full/path/to/storage"
<<
endl
;
// ./_decrypt prefix_name ct3 /home/bigpi/ storage/
else
{
timeval
t0
,
t1
;
unsigned
long
dt
=
0
;
struct
decryptor_t
decr
;
string
pathK
=
argv
[
2
];
string
Ctfile
=
argv
[
1
];
string
prefix
(
""
);
if
(
suffix_exist
(
pathK
,
"/"
))
{
prefix
.
append
(
pathK
);
}
else
{
prefix
.
append
(
pathK
);
prefix
.
append
(
"/"
);
}
//cout << "[INFO] Keypathname sk: " << prefix << endl;
init_operator
(
8192
,
4294967296
,
decr
,
prefix
);
// path to storage argv[3]
string
pathStorage
=
argv
[
3
];
prefix
=
""
;
if
(
suffix_exist
(
pathStorage
,
"/"
))
{
prefix
.
append
(
pathStorage
);
}
else
{
prefix
.
append
(
pathStorage
);
prefix
.
append
(
"/"
);
}
//cout << "[INFO] Ctx de " << prefix << endl;
Ciphertext
ct
;
if
(
Ctfile
.
at
(
0
)
==
'/'
)
load_ciphertext
(
decr
,
ct
,
Ctfile
);
else
{
prefix
.
append
(
Ctfile
);
load_ciphertext
(
decr
,
ct
,
prefix
);
}
gettimeofday
(
&
t0
,
NULL
);
load_ciphertext
(
decr
,
ct
,
prefix
);
gettimeofday
(
&
t0
,
NULL
);
decrypt_ciphertext
(
decr
,
ct
);
gettimeofday
(
&
t1
,
NULL
);
dt
=
1000000
*
(
t1
.
tv_sec
-
t0
.
tv_sec
)
+
(
t1
.
tv_usec
-
t0
.
tv_usec
);
cout
<<
"[INFO] ciphertext decryption time in seconds: "
<<
((
float
)
dt
)
/
1000000
<<
endl
;
delete_operator
(
decr
);
return
0
;
}
}
docker/bigpiseal3.5.1/native/examples/generic/generic_encrypt.cpp
0 → 100644
View file @
d8df7d47
#include <cstdint>
#include <sys/time.h>
#include <boost/lexical_cast.hpp>
#include "seal_api.h"
using
namespace
std
;
using
namespace
seal
;
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
5
)
cerr
<<
"[ERROR] please enter 1 plaintext values, prefix , pathstorage(exists) "
<<
endl
;
// ./generic_encrypt 21 ct1 /home/bigpi/ storage/
else
{
timeval
t0
,
t1
;
unsigned
long
dt
=
0
;
struct
encryptor_t
encr
;
string
pathK
=
argv
[
3
];
string
Prefix
=
argv
[
2
];
string
pathStorage
=
argv
[
4
];
//cout << "[INFO] Prefix . : " << Prefix << endl;
string
prefix
(
""
);
//string postfix1(".ct");
prefix
=
""
;
if
(
suffix_exist
(
pathK
,
"/"
))
{
prefix
.
append
(
pathK
);
}
else
{
prefix
.
append
(
pathK
);
prefix
.
append
(
"/"
);
}
init_operator
(
8192
,
4294967296
,
encr
,
prefix
);
Ciphertext
ct
;
int64_t
plain
=
boost
::
lexical_cast
<
int64_t
>
(
argv
[
1
]);
gettimeofday
(
&
t0
,
NULL
);
init_ciphertext
(
encr
,
plain
,
ct
);
gettimeofday
(
&
t1
,
NULL
);
dt
=
1000000
*
(
t1
.
tv_sec
-
t0
.
tv_sec
)
+
(
t1
.
tv_usec
-
t0
.
tv_usec
);
cout
<<
"[INFO] plaintext encryption time in seconds: "
<<
((
float
)
dt
)
/
1000000
<<
endl
;
prefix
=
""
;
if
(
suffix_exist
(
pathStorage
,
"/"
))
{
prefix
.
append
(
pathStorage
);
}
else
{
prefix
.
append
(
pathStorage
);
prefix
.
append
(
"/"
);
}
prefix
.
append
(
Prefix
);
prefix
.
append
(
".ct"
);
//cout << "[INFO] suffix .ct : " << prefix << endl;
save_ciphertext
(
ct
,
prefix
);
//~ plain = boost::lexical_cast<int64_t>(argv[2]);
//~ init_ciphertext(encr, plain, ct);
//~ save_ciphertext(ct, "ct2.ct");
delete_operator
(
encr
);
return
0
;
}
}
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate.cpp
0 → 100644
View file @
d8df7d47
#include <sys/stat.h>
#include "add.h"
using
namespace
std
;
using
namespace
seal
;
inline
bool
exists_file
(
const
std
::
string
&
name
)
{
struct
stat
buffer
;
return
(
stat
(
name
.
c_str
(),
&
buffer
)
==
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
5
)
{
cout
<<
"[ERROR] please enter 3 ciphertext /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct full/path/to/publickey"
<<
endl
;
// ../generic_evaluate /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct /home/bigpi/path/to/pubKey
return
EXIT_FAILURE
;
}
else
{
struct
evaluator_t
eval
;
string
Ct1
=
argv
[
1
];
string
Ct2
=
argv
[
2
];
string
Ct3
=
argv
[
3
];
string
pathK1
=
argv
[
4
];
string
pathK
=
""
;
if
(
exists_file
(
Ct1
)
==
false
||
exists_file
(
Ct2
)
==
false
)
{
cout
<<
"[ERROR] please enter 2 first ciphertext input /path/to/ct1.ct /path/to/ct2.ct"
<<
endl
;
return
EXIT_FAILURE
;
}
/* if (suffix_exist(Ct3, "/") == false) {
cout << "[ERROR] please enter 3rd parameter /path/to/result/name.ct/you/wish" << endl;
return EXIT_FAILURE;
} */
if
(
suffix_exist
(
pathK1
,
"/"
))
{
pathK
=
pathK1
;
}
else
{
pathK
.
append
(
pathK1
);
pathK
.
append
(
"/"
);
}
init_operator
(
2048
,
256
,
eval
,
pathK
);
Ciphertext
ct1
,
ct2
,
ct3
;
string
prefix
(
""
);
load_ciphertext
(
eval
,
ct1
,
Ct1
);
load_ciphertext
(
eval
,
ct2
,
Ct2
);
add_ciphertext
(
eval
,
ct1
,
ct2
,
ct3
);
save_ciphertext
(
ct3
,
Ct3
);
delete_operator
(
eval
);
return
0
;
}
}
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate_multiply.cpp
0 → 100644
View file @
d8df7d47
#include <sys/stat.h>
#include "multiply.h"
using
namespace
std
;
using
namespace
seal
;
inline
bool
exists_file
(
const
std
::
string
&
name
)
{
struct
stat
buffer
;
return
(
stat
(
name
.
c_str
(),
&
buffer
)
==
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
5
)
{
cout
<<
"ct1.ct x ct2.ct = ct3.ct"
<<
endl
;
cout
<<
"[ERROR] please enter 3 ciphertext /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct full/path/to/publickey"
<<
endl
;
// ../generic_evaluate /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct /home/bigpi/path/to/pubKey
return
EXIT_FAILURE
;
}
struct
evaluator_t
eval
;
string
Ct1
=
argv
[
1
];
string
Ct2
=
argv
[
2
];
string
Ct3
=
argv
[
3
];
string
pathK1
=
argv
[
4
];
string
pathK
=
""
;
if
(
exists_file
(
Ct1
)
==
false
||
exists_file
(
Ct2
)
==
false
)
{
cout
<<
"[ERROR] please enter 2 first ciphertext input /path/to/ct1.ct /path/to/ct2.ct"
<<
endl
;
return
EXIT_FAILURE
;
}
/* if (suffix_exist(Ct3, "/") == false) {
cout << "[ERROR] please enter 3rd parameter /path/to/result/name.ct/you/wish" << endl;
return EXIT_FAILURE;
} */
if
(
suffix_exist
(
pathK1
,
"/"
))
{
pathK
=
pathK1
;
}
else
{
pathK
.
append
(
pathK1
);
pathK
.
append
(
"/"
);
}
init_operator
(
2048
,
256
,
eval
,
pathK
);
Ciphertext
ct1
,
ct2
,
ct3
;
string
prefix
(
""
);
load_ciphertext
(
eval
,
ct1
,
Ct1
);
load_ciphertext
(
eval
,
ct2
,
Ct2
);
multiply_ciphertext
(
eval
,
ct1
,
ct2
,
ct3
);
save_ciphertext
(
ct3
,
Ct3
);
delete_operator
(
eval
);
return
0
;
}
docker/bigpiseal3.5.1/native/examples/generic/generic_evaluate_sub.cpp
0 → 100644
View file @
d8df7d47
#include <sys/stat.h>
#include "sub.h"
using
namespace
std
;
using
namespace
seal
;
inline
bool
exists_file
(
const
std
::
string
&
name
)
{
struct
stat
buffer
;
return
(
stat
(
name
.
c_str
(),
&
buffer
)
==
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
5
)
{
cout
<<
"ct1.ct - ct2.ct = ct3.ct"
<<
endl
;
cout
<<
"[ERROR] please enter 3 ciphertext /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct full/path/to/publickey"
<<
endl
;
// ../generic_evaluate /path/to/ct1.ct /path/to/ct2.ct /path/to/result.ct /home/bigpi/path/to/pubKey
return
EXIT_FAILURE
;
}
struct
evaluator_t
eval
;
string
Ct1
=
argv
[
1
];
string
Ct2
=
argv
[
2
];
string
Ct3
=
argv
[
3
];
string
pathK1
=
argv
[
4
];
string
pathK
=
""
;
if
(
exists_file
(
Ct1
)
==
false
||
exists_file
(
Ct2
)
==
false
)
{
cout
<<
"[ERROR] please enter 2 first ciphertext input /path/to/ct1.ct /path/to/ct2.ct"
<<
endl
;
return
EXIT_FAILURE
;
}
/* if (suffix_exist(Ct3, "/") == false) {
cout << "[ERROR] please enter 3rd parameter /path/to/result/name.ct/you/wish" << endl;
return EXIT_FAILURE;
} */
if
(
suffix_exist
(
pathK1
,
"/"
))
{
pathK
=
pathK1
;
}
else
{
pathK
.
append
(
pathK1
);
pathK
.
append
(
"/"
);
}
init_operator
(
2048
,
256
,
eval
,
pathK
);
Ciphertext
ct1
,
ct2
,
ct3
;
string
prefix
(
""
);
load_ciphertext
(
eval
,
ct1
,
Ct1
);
load_ciphertext
(
eval
,
ct2
,
Ct2
);
sub_ciphertext
(
eval
,
ct1
,
ct2
,
ct3
);
save_ciphertext
(
ct3
,
Ct3
);
delete_operator
(
eval
);
return
0
;
}
Prev
1
…
4
5
6
7
8
9
10
11
12
…
14
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment