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

testing

parent c6c9d2f5

Too many changes to show.

To preserve performance only 269 of 269+ files are displayed.
#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;
}
}
#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 != 3)
cout << "[ERROR] please enter 2 plaintext values" << endl;
else {
timeval t0, t1;
unsigned long dt = 0;
struct encryptor_t encr;
init_operator(8192, 4294967296, encr);
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;
save_ciphertext(ct, "ct1.ct");
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/time.h>
#include "add.h"
using namespace std;
using namespace seal;
int main(int argc, char **argv)
{
if(argc != 3)
cout << "[ERROR] please enter 2 ciphertext files" << endl;
else {
timeval t0, t1;
unsigned long dt = 0;
struct evaluator_t eval;
init_operator(8192, 4294967296, eval);
Ciphertext ct1, ct2, ct3;
cout << "[INFO] loading ciphertext 1" << endl;
load_ciphertext(eval, ct1, "ct1.ct");
cout << "[INFO] loading ciphertext 2" << endl;
load_ciphertext(eval, ct2, "ct2.ct");
cout << "[INFO] adding ciphertext 1 to ciphertext 2" << endl;
gettimeofday(&t0, NULL);
add_ciphertext(eval, ct1, ct2, ct3);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] homomorphic addition time in seconds: " << ((float)dt)/1000000 << endl;
cout << "[INFO] saving ciphertext addition to a new ciphertext file" << endl;
save_ciphertext(ct3, "ct3.ct");
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;
gettimeofday(&t0, NULL);
generate_keys(8192, 4294967296, 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;
}
#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 << "[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, std::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 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;
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 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;
}
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");
}
}
void init_operator(size_t poly_d, size_t p_modulus, 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 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)
{
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 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)
{
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 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;
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);
}
#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);
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::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 generate_keys(size_t poly_d, size_t p_modulus, bool seriablizable = false);
/** for homomorphic operators management **/
void init_operator(size_t poly_d, size_t p_modulus, struct encryptor_t& op_st);
void init_operator(size_t poly_d, size_t p_modulus, struct decryptor_t& op_st);
void init_operator(size_t poly_d, size_t p_modulus, struct evaluator_t& op_st);
void delete_operator(struct encryptor_t& op_st);
void delete_operator(struct decryptor_t& op_st);
void delete_operator(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 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);
#endif
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
cmake_minimum_required(VERSION 3.10)
set(TEST_NAME patternSearch)
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(SCR_TEST test.sh)
set(SCR_ENC encrypt.sh)
set(SCR_DEC decrypt.sh)
set(SCR_DEC_RESULT decrypt_result.sh)
set(SCR_EVAL eval.sh)
set(SCR_GEN genkey.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}/util.cpp
${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}/util.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_evaluate.cpp
)
set(HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/seal_api.h
${CMAKE_CURRENT_LIST_DIR}/util.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}/v1"
)
set_target_properties(${ENCR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1"
)
set_target_properties(${DECR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1"
)
set_target_properties(${EVAL_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1"
)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_TEST}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_ENC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_DEC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_DEC_RESULT}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_EVAL}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_GEN}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_TEST_V2}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}/v1)
\ No newline at end of file
#decrypt
resultPath=$1
# ex: result/l.ct
sample=$2
# ex: 40
keyDir=$3
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./patternSearch_decrypt_v2 "$resultPath" "$sample" "$keyDir"
time ${CURR_DIR}/patternSearch_decrypt_v2 "$resultPath" "$sample" "$keyDir"
\ No newline at end of file
#decrypt result
resultPath=$1
# ex: result/l.ct
sample=$2
# ex: 40
keyDir=$3
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./patternSearch_decrypt_result_v2 "$resultPath" "$sample" "$keyDir"
time ${CURR_DIR}/patternSearch_decrypt_result_v2 "$resultPath" "$sample" "$keyDir"
\ No newline at end of file
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