Commit c6c9d2f5 authored by Hoang Gia NGUYEN's avatar Hoang Gia NGUYEN
Browse files

first commit

parent fe2d6195
Pipeline #60 failed with stages
in 0 seconds

Too many changes to show.

To preserve performance only 275 of 275+ files are displayed.
#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
#!/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
#!/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
#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;
}
}
#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;
}
}
#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;
}
}
#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;
}
#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;
}
#include <sys/time.h>
#include "seal_api.h"
using namespace std;
using namespace seal;
int main(int argc, char **argv)
{
timeval t0, t1;
unsigned long dt = 0;
// gen keys § path to sk
string pathSk = argv[1];
gettimeofday(&t0, NULL);
generate_keys(8192, 4294967296,pathSk, true);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] keys generation time in seconds: " << ((float)dt)/1000000 << endl;
return 0;
}
#!/bin/bash
#
# 2020 CEA LIST.
declare -r ERROR_NUMBER_PARAM=1
declare -r ERROR_KEY_NOT_FOUND=3
declare -r KEYGEN_NOT_PERFORMED=4
#/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
# path_to_key existing key pk encrypt
if [ "$#" -ne 1 ]; then
echo "You must enter exactly 1 command line arguments"
# display help if empty
echo " path to sk , "
exit $ERROR_NUMBER_PARAM
fi
path_to_key=$1
# encryption
echo "key generation"
${CURR_DIR}/generic_genkey $path_to_key
# recuperate result
if [ "$?" == "0" ]; then
echo ok done
else
echo KEYGEN_NOT_PERFORMED
exit 4
fi
exit
#include "multiply.h"
using namespace std;
using namespace seal;
void multiply_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->multiply_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 multiplication (pt*ct) time in (us): " << dt << endl;
}
void multiply_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->multiply(ct1, ct2, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic multiplication (ct*ct) time in (us): " << dt << endl;
}
#ifndef _MULT_H_
#define _MULT_H_
#include <sys/time.h>
#include <iostream>
#include <string>
#include "seal/seal.h"
#include "seal_api.h"
void multiply_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct, seal::Plaintext &pt, seal::Ciphertext &ct_out);
void multiply_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct1, seal::Ciphertext &ct2, seal::Ciphertext &ct_out);
#endif
#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, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR06] file opening failure" << filename << endl;
ret = 0;
}
return ret;
}
int open_binary_file(ofstream &out_file, const string &filename)
{
int ret = 1;
out_file = ofstream(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR07] file opening failure" << filename << 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,const std::string& pathname)
{
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);
//ruf
//cout << "[INFO] save bfvparams: " << pathname<< endl;
save_params(params, pathname);
context = SEALContext::Create(params);
print_context(context);
}
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;
std::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(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR 00] File opening failure " << filename << endl;
exit(1) ;
ret = 0;
}
else
k.save(out_file);
return ret;
}
int save_key(SecretKey &k, const string &filename)
{
int ret = 1;
ofstream out_file(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR 01] File opening failure " << filename << endl;
exit(1) ;
ret = 0;
}
else
k.save(out_file);
return ret;
}
int save_key(RelinKeys &k, const string &filename)
{
int ret = 1;
ofstream out_file(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR 02] File opening failure " << filename << endl;
exit(1) ;
ret = 0;
}
else
k.save(out_file);
}
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 load_key(shared_ptr<SEALContext> &context, const string &filename, PublicKey &k)
{
int ret = 0;
ifstream in_file(filename, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR 03] File opening failure " << filename << endl;
exit(1) ;
ret = 0;
}
else
k.load(context, in_file);
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, SecretKey &k)
{
int ret = 1;
ifstream in_file(filename, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR 04] File opening failure " << filename << endl;
ret = 0;
}
else
k.load(context, in_file);
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, RelinKeys &k)
{
int ret = 1;
ifstream in_file(filename, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR 05] File opening failure"<< filename << endl;
ret = 0;
}
else
k.load(context, in_file);
return ret;
}
void generate_keys(size_t poly_d, size_t p_modulus, const std::string& pathname, bool serializable )
//void generate_keys(size_t poly_d, size_t p_modulus, bool serializable ,const std::string& pathname)
{
shared_ptr<SEALContext> context;
string keypath("");
string keypath1("/bfv.pk");
string keypath2("/bfv.sk");
string keypath3("/bfv.lk");
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
if (suffix_exist(pathname, "/"))
{
keypath1="bfv.pk";
keypath2="bfv.sk";
keypath3="bfv.lk";
bfv_params_path1="bfv_params.conf";
}
bfv_params_path="";
bfv_params_path.append(pathname);
bfv_params_path.append(bfv_params_path1);
init_context(poly_d, p_modulus, context,bfv_params_path);
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
keypath.append(pathname);
keypath.append(keypath1);
//cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key(pk, keypath );
keypath="";
keypath.append(pathname);
keypath.append(keypath2);
SecretKey sk = keygen.secret_key();
//cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key(sk, keypath);
keypath="";
keypath.append(pathname);
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);
}
}
void init_operator(size_t poly_d, size_t p_modulus, struct encryptor_t &op_st,const std::string& pathname)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path="";
bfv_params_path.append(pathname);
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(pathname);
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 delete_operator(struct encryptor_t &op_st)
{
delete op_st.encr;
delete op_st.icode;
}
void init_operator(size_t poly_d, size_t p_modulus, struct decryptor_t &op_st,const std::string& pathname)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path="";
bfv_params_path.append(pathname);
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(pathname);
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 delete_operator(struct decryptor_t &op_st)
{
delete op_st.decr;
delete op_st.icode;
}
void init_operator(size_t poly_d, size_t p_modulus, struct evaluator_t &op_st,const std::string& pathname)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path="";
bfv_params_path.append(pathname);
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(pathname);
keypath.append(keypath1);
cout << "[INFO] Keypath evaluator: " << keypath << endl;
load_key(op_st.context, keypath, op_st.lk);
op_st.eval = new Evaluator(op_st.context);
}
void delete_operator(struct evaluator_t &op_st)
{
delete op_st.eval;
}
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 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;
}
int save_plaintext(Plaintext &pt, const string &filename)
{
int ret = 1;
ofstream out_file(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR1 File opening failure " << filename<< endl;
ret = 0;
}
else
pt.save(out_file);
return ret;
}
int save_ciphertext(Ciphertext &ct, const string &filename)
{
int ret = 1;
ofstream out_file(filename, std::ios::binary);
if(!out_file) {
cerr << "[ERRROR2] File opening failure " << filename << endl;
ret = 0;
}
else
ct.save(out_file);
return ret;
}
int load_plaintext(shared_ptr<SEALContext> &context, Plaintext &pt, const string &filename)
{
int ret = 1;
ifstream in_file(filename, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR3] File opening failure " << filename << endl;
ret = 0;
}
else
pt.load(context, in_file);
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(filename, std::ios::binary);
if(!in_file) {
cerr << "[ERRROR4] File opening failure " << filename << endl;
exit(1);
ret = 0;
}
else
ct.load(context, in_file);
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);
}
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;
}
#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"
/* definitions */
struct encryptor_t {
std::shared_ptr<seal::SEALContext> context;
seal::Encryptor *encr;
seal::IntegerEncoder *icode;
};
struct decryptor_t {
std::shared_ptr<seal::SEALContext> context;
seal::Decryptor *decr;
seal::IntegerEncoder *icode;
};
struct evaluator_t {
std::shared_ptr<seal::SEALContext> context;
seal::RelinKeys lk;
seal::Evaluator *eval;
};
/* 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,const std::string& pathname);
void load_context(std::shared_ptr<seal::SEALContext>& context, const std::string& filename,const std::string& pathname);
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::Serializable<seal::RelinKeys>& 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 init_operator(size_t poly_d, size_t p_modulus, struct encryptor_t& op_st,const std::string& pathname);
void init_operator(size_t poly_d, size_t p_modulus, struct decryptor_t& op_st,const std::string& pathname);
void init_operator(size_t poly_d, size_t p_modulus, struct evaluator_t& op_st,const std::string& pathname);
void delete_operator(struct encryptor_t& op_st);
void delete_operator(struct decryptor_t& op_st);
void delete_operator(struct evaluator_t& op_st);
//void generate_keys(size_t poly_d, size_t p_modulus, bool serializable = false ,const std::string& pathname);
void generate_keys(size_t poly_d, size_t p_modulus,const std::string& pathname, bool seriablizable = false);
/** 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 decrypt_ciphertext(struct decryptor_t& op_st, seal::Ciphertext& ct);
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);
bool suffix_exist(const std::string &str, const std::string &suffix) ;
#endif
#include "sub.h"
using namespace std;
using namespace seal;
void sub_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->sub_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 subtraction (pt+ct) time in (us): " << dt << endl;
}
void sub_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->sub(ct1, ct2, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic subtraction (ct+ct) time in (us): " << dt << endl;
}
#ifndef _SUB_H_
#define _SUB_H_
#include <sys/time.h>
#include <iostream>
#include <string>
#include "seal/seal.h"
#include "seal_api.h"
void sub_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct, seal::Plaintext &pt, seal::Ciphertext &ct_out);
void sub_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct1, seal::Ciphertext &ct2, seal::Ciphertext &ct_out);
#endif
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
cmake_minimum_required(VERSION 3.10)
set(TEST_NAME hello)
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(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(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})
#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)
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}"
)
#include "add.h"
using namespace std;
using namespace seal;
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, Plaintext &pt, Ciphertext &ct_out)
{
op_st.eval->add_plain(ct, pt, ct_out);
}
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
op_st.eval->add(ct1, ct2, ct_out);
}
#ifndef _ADD_H_
#define _ADD_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
#include <sys/time.h>
#include "seal_api.h"
using namespace std;
using namespace seal;
int main(int argc, char **argv)
{
if(argc != 2)
cout << "[ERROR] please enter a ciphertext file" << endl;
else {
timeval t0, t1;
unsigned long dt = 0;
struct decryptor_t decr;
init_operator(8192, 4294967296, decr);
Ciphertext ct;
gettimeofday(&t0, NULL);
load_ciphertext(decr, ct, argv[1]);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] ciphertext loading time in seconds: " << ((float)dt)/1000000 << endl;
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;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment