Commit 0005e552 authored by Hoang Gia NGUYEN's avatar Hoang Gia NGUYEN
Browse files

test

parent 3704e126
#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"
using namespace std;
using namespace seal;
int main(int argc, char **argv)
{
string key_dir = argv[1];
size_t poly_d = 4096;
// Params option 1
int bit_size = 20;
// Params option 2
// uint64_t plain_modulus = 1032193;
// vector<int> bit_sizes = { 36, 36, 37 };
// timeval t0, t1;
// unsigned long dt = 0;
// gettimeofday(&t0, NULL);
batching_generate_keys(poly_d, bit_size, key_dir, true);
// batching_generate_keys(poly_d, bit_sizes, plain_modulus, key_dir, 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;
}
#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
mkdir -p keys/
mkdir -p lcheck/
mkdir -p parent/
mkdir -p result/
# rm ls.txt
# bash data_creating.sh 0 99 9 1000
# Number of driving licenses, eg: (0..99)
min=0
max=101
# max=1631
# max=101
# sim: (eval: 0.465s, 0.488s, 0.480s decrypt: 0.028s, 0.080s, 0.077s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=203
# (eval: 3.635s, 3.510s, 3.628s decrypt: 0.062s, 0.153s. 0.134s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=407
# (eval: 38.607s, 39.600s, 39.565s decrypt: 0.264s, 0.260s, 0.245s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=815
# (eval: x decrypt: x )
# Number of chars/words of a driving license, eg: 9
n_char=39
# Encrypted number range, eg: 10, 100, 1000 for 1 2 3 4 digits
range=255
# (8-bit)
# sampling size
sample=40
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# Note
# gen keys
# ./patternSearch_genkey_v2 keys/
${CURR_DIR}/patternSearch_genkey_v2 keys/
line=$((RANDOM %max))
lcheck=""
# gen data
for i in $(seq $min $max)
do
mkdir -p parent/l"$i"/
echo "Created folder $i"
value=""
for j in $(seq 0 $n_char)
do
value+="$((RANDOM %$range)) "
done
echo $value
echo $value >> ls.txt
# ./patternSearch_encrypt_v2 "$value" "l" parent/l"$i"/ $sample keys/
${CURR_DIR}/patternSearch_encrypt_v2 "$value" "l" parent/l"$i"/ $sample keys/
if [[ "$line" -eq "$i" ]]
then
lcheck="$value"
fi
done
# get random lc
# lcheck=$(sed -n "$((RANDOM %9){p;q}" ls.txt)
# line=$((RANDOM %max))
# lcheck="$(sed -n "${line}{p;q}" ls.txt)"
echo "Pick up license to check: $lcheck"
# ./patternSearch_encrypt_v2 "$lcheck" "l" lcheck/ $sample keys/
${CURR_DIR}/patternSearch_encrypt_v2 "$lcheck" "l" lcheck/ $sample keys/
# ./patternSearch_encrypt "47 25 76 23 30 21 1 47 88 3" "a" keys/bfv.pk lcheck/
# eval
value1="lcheck/l.ct "
value2=""
for i in $(seq 0 $max)
do
value2+="parent/l${i}/l.ct "
done
# value2="${value1} ${value2}"
value3="$value1$value2"
echo "$value3"
# time ./patternSearch_evaluate_v2 $value3 "l" result/ $sample keys/
# ./patternSearch_evaluate_v2 $value3 "l" result/ $sample keys/
time ${CURR_DIR}/patternSearch_evaluate_v2 $value3 "l" result/ $sample keys/
echo ""
# time ./patternSearch_decrypt_result_v2 result/l.ct $sample keys/
# ./patternSearch_decrypt_result_v2 result/l.ct $sample keys/
${CURR_DIR}/patternSearch_decrypt_result_v2 result/l.ct $sample keys/
# rm -r keys/*
# rm -r lcheck/*
# rm -r parent/*
# rm -r result/*
# rm ls.txt
#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;
// }
#ifndef _UTIL_H_
#define _UTIL_H_
#include <string>
#include <vector>
// int findNumberOfFilesInDirectory(const std::string &path);
bool sub_str_exist(const std::string &str, const std::string &sub_str);
// std::vector<std::string> get_directories(const std::string &s);
// std::vector<std::string> get_directories_deep(const std::string &s);
#endif
......@@ -7,8 +7,6 @@
#ifdef SEAL_USE_SHARED_MUTEX
#include <shared_mutex>
/*Building problem with shared_mutex!*/
#include <mutex>
namespace seal
{
......
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