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.
#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
#encrypt
licenseNo=$1
# ex: "23 65 78 127 255" (en ASCII)
filename=$2
# ex: "l" (extension will be .ct)
outputDir=$3
# ex: lcheck/
sample=$4
# ex: 40
keyDir=$5
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./patternSearch_encrypt_v2 "$licenseNo" "$filename" "$outputDir" "$sample" "$keyDir"
time ${CURR_DIR}/patternSearch_encrypt_v2 "$licenseNo" "$filename" "$outputDir" "$sample" "$keyDir"
\ No newline at end of file
# evaluate
licensePath=$1
# ex: lcheck/l.ct
licensePathList=${@: 2:$#-5}
# licensePathList="${@:2:102}"
# for var in "${@: 2:$#-7}"
# do
# echo "$var"
# done
# ex: parent/l0/l.ct parent/l1/l.ct parent/l2/l.ct parent/l3/l.ct parent/l4/l.ct parent/l5/l.ct parent/l6/l.ct parent/l7/l.ct parent/l8/l.ct parent/l9/l.ct
filename=${@: -4:1}
# ex: "l" (extension will be .ct)
outputDir=${@: -3:1}
# ex: result/
sample=${@: -2:1}
# ex: 40
KeyDir=${@: -1}
# ex: keys/
# linkingKeyPath=${@: -3:1}
# # ex: bfv.lk
# galoisKeyPath=${@: -2:1}
# # ex: bfv.gk
# publicKeyPath=${@: -1}
# # ex: bfv.pk
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./patternSearch_evaluate_v2 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$linkingKeyPath" "$galoisKeyPath" "$publicKeyPath"
# time ./patternSearch_evaluate_v2 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$KeyDir"
time ${CURR_DIR}/patternSearch_evaluate_v2 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$KeyDir"
\ No newline at end of file
#gen key
keyDir=$1
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./patternSearch_genkey_v2 "$keyDir"
time ${CURR_DIR}/patternSearch_genkey_v2 "$keyDir"
\ No newline at end of file
#include "seal_api.h"
#include "util.h"
using namespace seal;
using namespace std;
string ciphertext_name;
string secret_key_path;
string decrypt(string &ciphertext_dir);
bool decrypt_vector(string &ciphertext_dir);
string decrypt_vectors(string &parent_dir);
bool decrypt_isContain(string &result);
int main(int argc, char **argv)
{
if (argc != 3)
// cout << "[ERROR] please enter /full/path/to/file/to/decrypt full/path/key " << endl;
// cout << "[ERROR] please enter prefix_file_to_decrypt full/path/key /full/path/to/storage" << endl;
// cout << "[ERROR] please enter a ciphertext name, ciphertext directory and secret key path" << endl;
cout << "[ERROR] please enter a ciphertext file path and secret key path" << endl;
else
{
string dir = argv[1];
secret_key_path = argv[2];
string result_str = decrypt(dir);
// cout << result_str << endl;
bool result = decrypt_isContain(result_str);
cout << result << endl;
return 0;
}
}
bool decrypt_isContain(string &result)
{
long result_l = atol(result.c_str());
if (result_l == 0)
{
// cout << "true";
return true;
}
else
{
// cout << "false";
return false;
}
}
string decrypt(string &ciphertext_dir)
{
struct decryptor_t decr;
init_operator(decr, secret_key_path);
Ciphertext ct;
string str;
load_ciphertext(decr, ct, ciphertext_dir);
str = to_string(decrypt_ciphertext_and_return_value(decr, ct));
// decrypt_ciphertext(decr, ct);
delete_operator(decr);
return str;
}
// Used for dynamic search
// string decrypt_vectors(string &parent_dir)
// {
// vector<string> directories = get_directories(parent_dir);
// string path_result = "";
// bool result;
// for (const auto &entry : directories)
// {
// string ciphertext_dir = entry + "/";
// cout << ciphertext_dir << endl;
// result = decrypt_vector(ciphertext_dir);
// if (result == true)
// {
// path_result = ciphertext_dir;
// break;
// }
// }
// return path_result;
// }
// Used for dynamic search
// bool decrypt_vector(string &ciphertext_dir)
// {
// int nFiles = findNumberOfFilesInDirectory(ciphertext_dir);
// struct decryptor_t decr;
// init_operator(2048, 256, decr, secret_key_path);
// Ciphertext ct;
// string plaintext;
// stringstream ss;
// for (int index = 0; index <= nFiles - 1; index++)
// {
// load_ciphertext(decr, ct, ciphertext_dir + "/" + ciphertext_name + "_" + to_string(index) + ".ct");
// plaintext = decrypt_ciphertext_and_return_string(decr, ct);
// if (index == nFiles - 1)
// {
// ss << plaintext;
// }
// else
// {
// ss << plaintext << " ";
// }
// }
// // cout << to_string(ss.str().size());
// // cout << ss.str() << endl;
// int x;
// int index = 0;
// bool result = false;
// while (ss >> x)
// {
// if (x == 0)
// {
// result = true;
// }
// else
// {
// result = false;
// break;
// }
// index++;
// }
// // if (index > 0 && result == true)
// // {
// // cout << "true" << endl;
// // }
// delete_operator(decr);
// return result;
// }
#include "seal_api.h"
using namespace seal;
using namespace std;
int main(int argc, char **argv)
{
if(argc != 5)
// cout << "[ERROR] please enter 1 plaintext values, prefix pathstorage(exists) " << endl;
// cout << "[ERROR] please enter plaintext value (eg. 75 67 8 23 076 2 23), public key path, ciphertext output directory and ciphertext output file name" << endl;
cout << "[ERROR] please enter plaintext vector value (eg. 75 67 8 23 076 2 23), output ciphertext file name or prefix, public key path and ciphertext output file directory" << endl;
else {
string plaintext = argv[1];
string ciphertext_name = argv[2];
string public_key_path = argv[3];
string ciphertext_dir = argv[4];
// cout << plaintext << endl;
struct encryptor_t encr;
// init_operator(2048, 256, encr, public_key_path);
init_operator(encr, public_key_path);
stringstream ss;
ss << plaintext;
int64_t x = 0;
int index = 0;
Ciphertext ct;
while (ss >> x)
{
/* Encrypt */
init_ciphertext(encr, x, ct);
save_ciphertext(ct, ciphertext_dir + "/" + ciphertext_name + "_" + to_string(index) + ".ct");
index++;
}
delete_operator(encr);
return 0;
}
}
#include "seal_api.h"
using namespace seal;
using namespace std;
int main(int argc, char **argv)
{
if (argc != 6)
{
// cout << "[ERROR] please enter 1 plaintext values, prefix pathstorage(exists) " << endl;
cout << "[ERROR] please enter plaintext vector value (eg. 75 67 8 23 076 2 23), output ciphertext file name or "
"prefix, ciphertext output file directory, sample size and public key path"
<< endl;
return -1;
}
else
{
string plaintext = argv[1];
string ciphertext_name = argv[2];
string ciphertext_dir = argv[3];
int sample_size = atol(argv[4]);
string key_dir = argv[5];
struct encryptor_t encr;
// init_operator_batching(2048, 4294967296, encr, public_key_path);
// init_operator_batching(4096, 4294967296, encr, public_key_path);
init_operator_batching(4096, 4294967296, encr, key_dir);
// init_operator_batching(16384, 4294967296, encr, public_key_path);
// init_operator_batching(32768, 4294967296, encr, public_key_path);
stringstream ss;
ss << plaintext;
vector<int64_t> pod_matrix;
int64_t x = 0;
while (ss >> x)
{
pod_matrix.push_back(x);
}
if (pod_matrix.size() <= encr.bcode->slot_count() / 2 && pod_matrix.size() <= sample_size)
{
Ciphertext encrypted_matrix;
init_ciphermatrix(encr, pod_matrix, encrypted_matrix);
save_ciphertext(encrypted_matrix, ciphertext_dir + "/" + ciphertext_name + ".ct");
delete_operator_batching(encr);
return 0;
}
else
{
delete_operator_batching(encr);
return -1;
}
// delete_operator_batching(encr);
// return 0;
}
}
#include "seal_api.h"
#include "util.h"
#include <iostream>
// #include <algorithm>
// #include <iterator>
// #include <vector>
// #include <filesystem>
using namespace seal;
std::string relink_key_path;
void sub_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out);
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out);
void multiply_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out);
void isContain(std::vector<std::vector<std::string>> &v_v, std::string &result_name, std::string &result_dir, std::string &relink_key_path);
bool is_number(const std::string &s);
void printStrVector(const std::vector<std::string> &v);
std::vector<std::vector<std::string>> split_ends(const std::vector<std::string> &source, const std::vector<int> &ends);
void multiply_ciphertexts(struct evaluator_t &op_st, std::vector<Ciphertext> &cts, Ciphertext &ct_out);
void relinearize_inplace(struct evaluator_t &op_st, Ciphertext &ct);
void rescale_to_next_inplace(struct evaluator_t &op_st, Ciphertext &ct);
void multiply_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2);
void relinearize(struct evaluator_t &op_st, Ciphertext &ct, Ciphertext &ct_out);
void sub_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2);
void negate_inplace__ciphertext(struct evaluator_t &op_st, Ciphertext &ct);
void add_plain_inplace_ciphertext(struct evaluator_t &op_st, struct Ciphertext &ct, const Plaintext &plain);
void multiply_plain_inplace(struct evaluator_t &op_st, Ciphertext &ct, const Plaintext &plain);
void add_many_ciphertext(struct evaluator_t &op_st, std::vector<Ciphertext> &cts, Ciphertext &ct_out);
void exponentiate_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, uint64_t &exponent);
void sub_plain_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, const Plaintext &plain);
void mod_switch_to_next_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct);
// void sub_vector(std::string &ciphertext_name2, std::string &ciphertext_dir2, std::string &result_name, std::string &result_dir);
// void sub_vectors(std::string &parent_dir, std::string &result_name, std::string &result_dir);
int main(int argc, char **argv)
{
// if (argc != 7)
// std::cout << "[ERROR] please enter prefix_file_to_decrypt_1 /full/path/to/storage_1 "
// "/full/path/to/parent prefix_result_file /full/path/to/result_storage full/path/to/relink/key"
// << std::endl;
// cout << "[ERROR] please enter prefix_file_to_decrypt_1 /full/path/to/storage_1 prefix_file_to_decrypt_2 "
// "/full/path/to/storage_2 prefix_result_file /full/path/to/result_storage full/path/to/relink/key"
// << endl;
// cout << "[ERROR] please enter name and directory of 1st ciphertexts, name and directory of 2nd ciphertexts, name
// and directory of result ciphertexts, and relink key path" << endl;
// else
std::string ciphertext_name1 = argv[1];
// ciphertext_dir1 = argv[2];
std::string result_name = argv[argc - 3];
std::string result_dir = argv[argc - 2];
relink_key_path = argv[argc - 1];
std::vector<std::string> source;
// std::vector<int> ends;
std::vector<std::vector<std::string>> v_v;
for (int i = 2; i < argc - 3; i++)
{
if (is_number(argv[i]))
{
// ends.push_back(std::stoi( argv[i]) );
int len = std::stoi( argv[i]);
std::vector<std::string> source;
for (int i2 = 1; i2 <= len; i2++)
{
source.push_back(argv[i + i2]);
}
v_v.push_back(source);
}
// else
// {
// source.push_back(argv[i]);
// std::cout << "[ERROR]" << argv[i] << std::endl;
// }
}
// auto splitted = split_ends(source, ends);
// for (const auto& v: v_v) {
// printStrVector(v);
// // std::cout << '\n';
// }
isContain(v_v, result_name, result_dir, relink_key_path);
std::cout << "done" << std::endl;
return 1;
}
void isContain(std::vector<std::vector<std::string>> &v_v, std::string &result_name, std::string &result_dir, std::string &relink_key_path)
{
struct evaluator_t eval;
// init_operator(8192, 4294967296, eval, relink_key_path);
init_operator(eval, relink_key_path);
Ciphertext result;
std::vector<Ciphertext> cts;
// std::cout << v_v.size() -1 << std::endl;
for (int index = 1; index < v_v.size(); index++)
{
Ciphertext ct3;
int noFiles1 = v_v.at(0).size();
int noFiles2 = v_v.at(index).size();
// std::cout << noFiles1 -1 << std::endl;
if (noFiles1 == noFiles2)
{
std::vector<Ciphertext> cts2;
for (int index2 = 0; index2 < noFiles1; index2++)
{
Ciphertext ct1, ct2;
Ciphertext temp;
// std::cout << "[INFO] loading ciphertext 1" << std::endl;
load_ciphertext(eval, ct1, v_v.at(0).at(index2));
// std::cout << "[INFO] loading ciphertext 2" << std::endl;
load_ciphertext(eval, ct2, v_v.at(index).at(index2));
// std::cout << "[INFO] subtracting ciphertext 2 from ciphertext 1" << std::endl;
sub_ciphertext(eval, ct1, ct2, temp);
// u_int64_t x = 2;
// exponentiate_inplace_ciphertext(eval, temp, x);
cts2.push_back(temp);
}
add_many_ciphertext(eval, cts2, ct3);
relinearize_inplace(eval, ct3);
}
else
{
// number of files is different in 2 directories
}
cts.push_back(ct3);
// if(index == 1)
// {
// result = ct3;
// }
// else
// {
// multiply_inplace_ciphertext(eval, result, ct3);
// relinearize_inplace(eval, result);
// mod_switch_to_next_inplace_ciphertext(eval, result);
// }
}
multiply_ciphertexts(eval, cts, result);
// mod_switch_to_next_inplace_ciphertext(eval, result);
save_ciphertext(result, result_dir + "/" + result_name + ".ct");
delete_operator(eval);
}
void sub_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
op_st.eval->sub(ct1, ct2, ct_out);
}
void sub_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2)
{
op_st.eval->sub_inplace(ct1, ct2);
}
void sub_plain_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, const Plaintext &plain)
{
op_st.eval->sub_plain_inplace(ct, plain);
}
void negate_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct)
{
op_st.eval->negate_inplace(ct);
}
void add_plain_inplace_ciphertext(struct evaluator_t &op_st, struct Ciphertext &ct, const Plaintext &plain)
{
op_st.eval->add_plain_inplace(ct, plain);
}
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
op_st.eval->add(ct1, ct2, ct_out);
}
void add_many_ciphertext(struct evaluator_t &op_st, std::vector<Ciphertext> &cts, Ciphertext &ct_out)
{
op_st.eval->add_many(cts, ct_out);
}
void multiply_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
op_st.eval->multiply(ct1, ct2, ct_out);
}
void multiply_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2)
{
op_st.eval->multiply_inplace(ct1, ct2);
}
void multiply_ciphertexts(struct evaluator_t &op_st, std::vector<Ciphertext> &cts, Ciphertext &ct_out)
{
op_st.eval->multiply_many(cts, op_st.lk, ct_out);
}
void multiply_plain_inplace(struct evaluator_t &op_st, Ciphertext &ct, const Plaintext &plain)
{
op_st.eval->multiply_plain_inplace(ct, plain);
}
void relinearize_inplace(struct evaluator_t &op_st, Ciphertext &ct)
{
op_st.eval->relinearize_inplace(ct, op_st.lk);
}
void relinearize(struct evaluator_t &op_st, Ciphertext &ct, Ciphertext &ct_out)
{
op_st.eval->relinearize(ct, op_st.lk, ct_out);
}
void rescale_to_next_inplace(struct evaluator_t &op_st, Ciphertext &ct)
{
op_st.eval->rescale_to_next_inplace(ct);
}
void exponentiate_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, uint64_t &exponent)
{
op_st.eval->exponentiate_inplace(ct, exponent, op_st.lk);
}
void mod_switch_to_next_inplace_ciphertext(struct evaluator_t &op_st, Ciphertext &ct)
{
op_st.eval->mod_switch_to_next_inplace(ct);
}
// // void isContain(std::string &parent_dir, std::string &result_name, std::string &result_dir)
// void isContain(std::string *dirs, int &len, std::string &result_name, std::string &result_dir)
// {
// struct evaluator_t eval;
// // init_operator(2048, 256, eval, relink_key_path);
// init_operator(8192, 4294967296, eval, relink_key_path);
// Ciphertext ct1, ct2, ct3, result;
// // std::cout << "[ERROR]" << std::endl;
// for (int index = 0; index < len; index++)
// {
// // std::cout << "[ERROR]" << dirs[index] << std::endl;
// std::string ciphertext_dir2 = dirs[index];
// // std::cout << ciphertext_dir2 << std::endl;
// int noFiles1 = findNumberOfFilesInDirectory(ciphertext_dir1);
// int noFiles2 = findNumberOfFilesInDirectory(ciphertext_dir2);
// if (noFiles1 == noFiles2)
// {
// for (int index2 = 0; index2 < noFiles1; index2++)
// {
// // std::cout << "[INFO] loading ciphertext 1" << std::endl;
// // load_ciphertext(eval, ct1, ciphertext_dir1 + "/" + ciphertext_name1 + "_" + std::to_string(index) +
// // ".ct");
// load_ciphertext(eval, ct1, ciphertext_dir1 + ciphertext_name1 + "_" + std::to_string(index2) + ".ct");
// // std::cout << "[INFO] loading ciphertext 2" << std::endl;
// // load_ciphertext(eval, ct2, ciphertext_dir2 + "/" + ciphertext_name1 + "_" + std::to_string(index) +
// // ".ct");
// load_ciphertext(eval, ct2, ciphertext_dir2 + ciphertext_name1 + "_" + std::to_string(index2) + ".ct");
// // std::cout << "[INFO] subtracting ciphertext 2 from ciphertext 1" << std::endl;
// sub_ciphertext(eval, ct1, ct2, ct1);
// if (index2 == 0)
// {
// ct3 = ct1;
// }
// else
// {
// add_ciphertext(eval, ct3, ct1, ct3);
// }
// }
// }
// else
// {
// // number of files is different in 2 directories
// }
// if (index == 0)
// {
// result = ct3;
// }
// else
// {
// multiply_ciphertext(eval, result, ct3, ct3);
// }
// }
// save_ciphertext(ct3, result_dir + "/" + result_name + ".ct");
// delete_operator(eval);
// }
// // Used for dynamic search
// void sub_vectors(std::string &ciphertext_name1, std::string &ciphertext_dir1, std::string &parent_dir, std::string &result_name, std::string &result_dir, std::string &relink_key_path)
// {
// std::vector<std::string> directories = get_directories(parent_dir);
// // for (const auto &entry : std::filesystem::directory_iterator(path))
// for (const auto &entry : directories)
// {
// // std::cout << entry << std::endl;
// std::string ciphertext_dir2 = entry + "/";
// std::string result_dir2 = result_dir + ciphertext_dir2;
// // std::cout << result_dir2 << std::endl;
// // std::cout << ciphertext_dir2 << std::endl;
// std::filesystem::create_directories(result_dir2);
// sub_vector(ciphertext_name1, ciphertext_dir2, result_name, result_dir2);
// // file reduction
// // std::cout << "[INFO] file reduction!!!" << std::endl;
// struct evaluator_t eval;
// init_operator(2048, 256, eval, relink_key_path);
// Ciphertext ct1, ct2, ct3;
// int noFiles = findNumberOfFilesInDirectory(result_dir2);
// for (int index = 1; index < noFiles; index++)
// {
// // std::cout << "[INFO] loading ciphertext 1" << std::endl;
// load_ciphertext(eval, ct1, result_dir2 + result_name + "_" + "0" + ".ct");
// // std::cout << "[INFO] loading ciphertext 2" << std::endl;
// load_ciphertext(eval, ct2, result_dir2 + result_name + "_" + std::to_string(index) + ".ct");
// // std::cout << "[INFO] addition ciphertext 1 with ciphertext 2" << std::endl;
// add_ciphertext(eval, ct1, ct2, ct1);
// // std::cout << "[INFO] saving ciphertext addition to a new ciphertext file" << std::endl;
// save_ciphertext(ct1, result_dir2 + result_name + "_" + "0" + ".ct");
// std::filesystem::remove(result_dir2 + result_name + "_" + std::to_string(index) + ".ct");
// }
// }
// }
// // Used for dynamic search
// void sub_vector(std::string &ciphertext_name1, std::string &ciphertext_dir1, std::string &ciphertext_name2, std::string &ciphertext_dir2, std::string &result_name, std::string &result_dir, std::string &relink_key_path)
// {
// struct evaluator_t eval;
// int noFiles1 = findNumberOfFilesInDirectory(ciphertext_dir1);
// int noFiles2 = findNumberOfFilesInDirectory(ciphertext_dir2);
// // init_operator(2048, 256, eval);
// init_operator(2048, 256, eval, relink_key_path);
// Ciphertext ct1, ct2, ct3;
// if (noFiles1 == noFiles2)
// {
// for (int index = 0; index < noFiles1; index++)
// {
// // std::cout << "[INFO] loading ciphertext 1" << std::endl;
// load_ciphertext(eval, ct1, ciphertext_dir1 + "/" + ciphertext_name1 + "_" + std::to_string(index) + ".ct");
// // std::cout << "[INFO] loading ciphertext 2" << std::endl;
// load_ciphertext(eval, ct2, ciphertext_dir2 + "/" + ciphertext_name2 + "_" + std::to_string(index) + ".ct");
// // std::cout << "[INFO] subtracting ciphertext 2 from ciphertext 1" << std::endl;
// sub_ciphertext(eval, ct1, ct2, ct3);
// // std::cout << "[INFO] saving ciphertext subtraction to a new ciphertext file" << std::endl;
// save_ciphertext(ct3, result_dir + "/" + result_name + "_" + std::to_string(index) + ".ct");
// }
// }
// else
// {}
// delete_operator(eval);
// }
// Input processing
bool is_number(const std::string &s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
// C++11
// bool is_number(const std::string& s)
// {
// return !s.empty() && std::find_if(s.begin(),
// s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();
// }
// std::vector<std::vector<std::string>> split_ends(const std::vector<std::string> &source, const std::vector<int> &ends)
// {
// std::vector<std::vector<std::string>> result;
// result.reserve(ends.size());
// auto anchor_front = source.begin();
// for (auto one_end: ends)
// {
// auto anchor_end = std::next(source.begin(), one_end + 1);
// result.emplace_back(anchor_front, anchor_end);
// anchor_front = anchor_end;
// }
// return result;
// }
// void printStrVector(const std::vector<std::string>& v)
// {
// for (auto x: v)
// {
// std::cout << x << ' ';
// }
// }
\ No newline at end of file
#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, 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 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 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 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_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 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.encr->encrypt(plaintext_matrix, encrypted_matrix);
// 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;
}
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);
}
#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::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;
};
/* 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);
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
# 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=5
# # Number of chars/words of a driving license, eg: 9
# n_char=7
# # Encrypted number range, eg: 10, 100, 1000 for 1 2 3 4 digits
# range=128
# # (8-bit)
# # gen keys
# ./patternSearch_genkey
# 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
# ./patternSearch_encrypt "$value" "a" bfv.pk parent/l"$i"/
# 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"
# ./patternSearch_encrypt "$lcheck" "a" bfv.pk lcheck/
# # ./patternSearch_encrypt "47 25 76 23 30 21 1 47 88 3" "a" bfv.pk lcheck/
# # eval
# value1=""
# for i in $(seq 0 $n_char)
# do
# value1+="lcheck/a_${i}.ct "
# done
# value1="$(($n_char+1)) ${value1}"
# # v=$(($n_char+1))
# # echo $v
# value2=""
# for i in $(seq 0 $max)
# do
# value2="${value2} $(($n_char+1)) "
# for j in $(seq 0 $n_char)
# do
# value2+="parent/l${i}/a_${j}.ct "
# done
# # value2="${value2} $(($n_char+1))"
# done
# # value2="${value1} ${value2}"
# value3="$value1$value2"
# echo "$value3"
# ./patternSearch_evaluate "a" $value3 "a" result/ bfv.lk
# ./patternSearch_decrypt result/a.ct bfv.sk
# rm -r lcheck/*
# rm -r parent/*
# rm -r result/*
# # ./patternSearch_evaluate "a" 10 lcheck/a_0.ct lcheck/a_1.ct lcheck/a_2.ct lcheck/a_3.ct lcheck/a_4.ct lcheck/a_5.ct lcheck/a_6.ct lcheck/a_7.ct lcheck/a_8.ct lcheck/a_9.ct 10 parent/l0/a_0.ct parent/l0/a_1.ct parent/l0/a_2.ct parent/l0/a_3.ct parent/l0/a_4.ct parent/l0/a_5.ct parent/l0/a_6.ct parent/l0/a_7.ct parent/l0/a_8.ct parent/l0/a_9.ct 10 parent/l1/a_0.ct parent/l1/a_1.ct parent/l1/a_2.ct parent/l1/a_3.ct parent/l1/a_4.ct parent/l1/a_5.ct parent/l1/a_6.ct parent/l1/a_7.ct parent/l1/a_8.ct parent/l1/a_9.ct 10 parent/l2/a_0.ct parent/l2/a_1.ct parent/l2/a_2.ct parent/l2/a_3.ct parent/l2/a_4.ct parent/l2/a_5.ct parent/l2/a_6.ct parent/l2/a_7.ct parent/l2/a_8.ct parent/l2/a_9.ct 10 parent/l3/a_0.ct parent/l3/a_1.ct parent/l3/a_2.ct parent/l3/a_3.ct parent/l3/a_4.ct parent/l3/a_5.ct parent/l3/a_6.ct parent/l3/a_7.ct parent/l3/a_8.ct parent/l3/a_9.ct 10 parent/l4/a_0.ct parent/l4/a_1.ct parent/l4/a_2.ct parent/l4/a_3.ct parent/l4/a_4.ct parent/l4/a_5.ct parent/l4/a_6.ct parent/l4/a_7.ct parent/l4/a_8.ct parent/l4/a_9.ct 10 parent/l5/a_0.ct parent/l5/a_1.ct parent/l5/a_2.ct parent/l5/a_3.ct parent/l5/a_4.ct parent/l5/a_5.ct parent/l5/a_6.ct parent/l5/a_7.ct parent/l5/a_8.ct parent/l5/a_9.ct 10 parent/l6/a_0.ct parent/l6/a_1.ct parent/l6/a_2.ct parent/l6/a_3.ct parent/l6/a_4.ct parent/l6/a_5.ct parent/l6/a_6.ct parent/l6/a_7.ct parent/l6/a_8.ct parent/l6/a_9.ct 10 parent/l7/a_0.ct parent/l7/a_1.ct parent/l7/a_2.ct parent/l7/a_3.ct parent/l7/a_4.ct parent/l7/a_5.ct parent/l7/a_6.ct parent/l7/a_7.ct parent/l7/a_8.ct parent/l7/a_9.ct 10 parent/l8/a_0.ct parent/l8/a_1.ct parent/l8/a_2.ct parent/l8/a_3.ct parent/l8/a_4.ct parent/l8/a_5.ct parent/l8/a_6.ct parent/l8/a_7.ct parent/l8/a_8.ct parent/l8/a_9.ct 10 parent/l9/a_0.ct parent/l9/a_1.ct parent/l9/a_2.ct parent/l9/a_3.ct parent/l9/a_4.ct parent/l9/a_5.ct parent/l9/a_6.ct parent/l9/a_7.ct parent/l9/a_8.ct parent/l9/a_9.ct "a" result/ key/bfv.lk
# # ./patternSearch_encrypt "$lc1" "a" bfv.pk lcheck/
# #eval
# # ./patternSearch_evaluate "a" lcheck/ parent/ "a" result/ bfv.lk
# #decrypt
# # ./patternSearch_decrypt "a" bfv.sk result/parent/
# #example
# # lc1=$1;
# # lc2=$2;
# # mkdir -p l1/;
# # mkdir -p l2/;
# # mkdir -p result/;
# # ./patternSearch_genkey
# # ./patternSearch_encrypt "$lc1" "a" bfv.pk l1/
# # ./patternSearch_encrypt "$lc2" "a" bfv.pk l2/
# # ./patternSearch_evaluate "a" l1/ "a" l2/ "a" result/ bfv.lk
# # ./patternSearch_decrypt "a" bfv.sk result/
# # rm l1/*;
# # rm l2/*;
# # rm result/*;
\ No newline at end of file
#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;
// }
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