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

test

parent 3704e126
# Other stuff
cmake/SEALConfig.cmake
cmake/SEALConfigVersion.cmake
cmake/SEALTargets.cmake
native/src/seal/util/config.h
native/src/gsl
**/CMakeCache.txt
**/CMakeFiles
**/Makefile
**/.config
**/autom4te.cache/*
**/cmake_install.cmake
**/install_manifest.txt
.ycm_extra_conf.py
.vimrc
.lvimrc
.local_vimrc
**/*.code-workspace
**/.vscode
**/build
**/*.build
**/compile_commands.json
**/.DS_Store
**/GSL
**/*.args.json
thirdparty/zlib/*
!thirdparty/zlib/CMakeLists.txt
thirdparty/msgsl/*
!thirdparty/msgsl/CMakeLists.txt
thirdparty/googletest/*
!thirdparty/googletest/CMakeLists.txt
dotnet/nuget/nuget.exe
dotnet/nuget/SEALNet.nuspec
dotnet/nuget/SEALNet-multi.nuspec
dotnet/nuget/SEALNet.targets
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ib/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Typescript v1 declaration files
typings/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
...@@ -738,11 +738,7 @@ if(SEAL_BUILD_EXAMPLES) ...@@ -738,11 +738,7 @@ if(SEAL_BUILD_EXAMPLES)
add_subdirectory(native/examples) add_subdirectory(native/examples)
add_subdirectory(native/examples/hello) add_subdirectory(native/examples/hello)
add_subdirectory(native/examples/generic) add_subdirectory(native/examples/generic)
#add_subdirectory(native/examples/patternSearch) add_subdirectory(native/examples/patternSearch)
add_subdirectory(native/examples/patternSearch/v1)
add_subdirectory(native/examples/patternSearch/v2)
add_subdirectory(native/examples/similarityMatch/v1)
add_subdirectory(native/examples/ANN/v1)
target_link_libraries(sealexamples PRIVATE seal) target_link_libraries(sealexamples PRIVATE seal)
endif() endif()
......
This diff is collapsed.
# 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(GENKEY_BIN_2 ${TEST_NAME}_genkey_v2)
set(ENCR_BIN_2 ${TEST_NAME}_encrypt_v2)
set(DECR_BIN_2 ${TEST_NAME}_decrypt_v2)
set(DECR_RESULT_BIN_2 ${TEST_NAME}_decrypt_result_v2)
set(EVAL_BIN_2 ${TEST_NAME}_evaluate_v2)
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(SCR_TEST_V2 test_v2.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(GENKEY_SRCS_2
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_genkey_v2.cpp
)
set(ENCR_SRCS_2
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_encrypt_v2.cpp
)
set(DECR_SRCS_2
${CMAKE_CURRENT_LIST_DIR}/util.cpp
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_decrypt_v2.cpp
)
set(DECR_RESULT_SRCS_2
${CMAKE_CURRENT_LIST_DIR}/util.cpp
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_decrypt_result_v2.cpp
)
set(EVAL_SRCS_2
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/util.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_evaluate_v2.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})
add_executable(${GENKEY_BIN_2} ${GENKEY_SRCS_2} ${HEADER_FILES})
add_executable(${ENCR_BIN_2} ${ENCR_SRCS_2} ${HEADER_FILES})
add_executable(${DECR_BIN_2} ${DECR_SRCS_2} ${HEADER_FILES})
add_executable(${DECR_RESULT_BIN_2} ${DECR_RESULT_SRCS_2} ${HEADER_FILES})
add_executable(${EVAL_BIN_2} ${EVAL_SRCS_2} ${HEADER_FILES})
#target_include_directories(${TEST_NAME} PRIVATE ${HEADER_DIR})
# Import Microsoft SEAL
find_package(SEAL 3.5 REQUIRED)
# Link Microsoft SEAL
target_link_libraries(${GENKEY_BIN} SEAL::seal)
target_link_libraries(${ENCR_BIN} SEAL::seal)
target_link_libraries(${DECR_BIN} SEAL::seal)
target_link_libraries(${EVAL_BIN} SEAL::seal)
target_link_libraries(${GENKEY_BIN_2} SEAL::seal)
target_link_libraries(${ENCR_BIN_2} SEAL::seal)
target_link_libraries(${DECR_BIN_2} SEAL::seal)
target_link_libraries(${DECR_RESULT_BIN_2} SEAL::seal)
target_link_libraries(${EVAL_BIN_2} SEAL::seal)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set_target_properties(${GENKEY_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${ENCR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${DECR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${EVAL_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${GENKEY_BIN_2}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${ENCR_BIN_2}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${DECR_BIN_2}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${DECR_RESULT_BIN_2}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${EVAL_BIN_2}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_TEST}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_ENC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_DEC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_DEC_RESULT}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_EVAL}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_GEN}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_TEST_V2}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
\ No newline at end of file
#include "add.h"
using namespace std;
using namespace seal;
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, Plaintext &pt, Ciphertext &ct_out)
{
timeval t0, t1;
unsigned long dt = 0;
gettimeofday(&t0, NULL);
op_st.eval->add_plain(ct, pt, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic addition (pt+ct) time in (us): " << dt << endl;
}
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
timeval t0, t1;
unsigned long dt = 0;
gettimeofday(&t0, NULL);
op_st.eval->add(ct1, ct2, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic addition (ct+ct) time in (us): " << dt << endl;
}
#ifndef _ADD_H_
#define _ADD_H_
#include <sys/time.h>
#include <iostream>
#include <string>
#include "seal/seal.h"
#include "seal_api.h"
void add_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct, seal::Plaintext &pt, seal::Ciphertext &ct_out);
void add_ciphertext(struct evaluator_t& op_st, seal::Ciphertext &ct1, seal::Ciphertext &ct2, seal::Ciphertext &ct_out);
#endif
#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
84 2 30 99 33 9 38 92 28 47
47 44 39 0 73 83 71 9 86 66
59 28 11 67 99 36 39 28 12 4
36 15 40 12 93 5 20 85 58 98
73 95 64 79 16 52 8 26 8 26
54 24 60 39 41 57 87 7 60 62
94 78 67 66 27 90 87 78 35 50
78 31 21 15 26 57 56 35 65 14
64 99 78 31 58 59 47 55 9 92
6 33 9 68 36 74 67 93 94 77
49 35 62 93 55 16 50 44 42 7
59 76 99 2 20 48 76 25 71 10
51 38 9 66 15 95 44 72 58 76
26 37 47 55 21 55 10 69 18 66
17 6 15 93 28 97 49 87 62 92
15 77 50 72 94 88 58 99 52 21
88 65 84 55 25 15 78 96 37 57
41 26 66 54 70 26 17 14 26 71
11 79 62 25 96 33 99 69 26 74
4 2 58 4 62 92 43 70 86 49
#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 <iomanip>
#include "seal_api.h"
#include "util.h"
using namespace seal;
using namespace std;
string ciphertext_name;
string key_dir;
// bool decrypt(size_t poly_d, size_t p_modulus, int &sample_size, string &ciphertext_dir);
bool decrypt(int &sample_size, string &ciphertext_dir);
int main(int argc, char **argv)
{
if (argc != 4)
{
// 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 file path, sample size and secret key path" << endl;
return -1;
}
else
{
string dir = argv[1];
int sample_size = atol(argv[2]);
key_dir = argv[3];
bool result_str = decrypt(sample_size, dir);
cout << result_str << endl;
return 0;
}
}
bool decrypt(int &sample_size, string &ciphertext_dir)
{
struct decryptor_t decr;
init_operator_batching(decr, key_dir);
bool isContain = false;
if (sample_size <= decr.bcode->slot_count()/2)
{
Ciphertext cipher_matrix;
vector<int64_t> pod_matrix;
load_ciphertext(decr, cipher_matrix, ciphertext_dir);
pod_matrix = decrypt_ciphermatrix(decr, cipher_matrix);
// cout << pod_matrix.size() << endl;
int no_dual_vectors = (decr.bcode->slot_count() / 2) / (sample_size);
vector<int64_t> v1, v2;
for (size_t i = 0; i < no_dual_vectors * sample_size; i++)
{
v1.push_back(pod_matrix[i]);
v2.push_back(pod_matrix[(pod_matrix.size() / 2) + i]);
}
int64_t sum = 0;
if (isContain == false)
{
// cout << "1 : ";
for (size_t i = 0; i < v1.size(); i++)
{
// cout << v1[i];
if ((i + 1) % sample_size == 0)
{
// cout << endl;
if (i < v1.size() - 1)
{
// cout << ((i + 1) / 40) + 1 << " : ";
}
}
else
{
// cout << ", ";
}
if (v1[i] == 0)
{
sum = sum + 1;
}
else
{
sum = 0;
}
if (sum == sample_size)
{
isContain = true;
}
}
}
// cout << endl;
if (isContain == false)
{
sum = 0;
// cout << (v2.size() + 1) / 40 + 1 << " : ";
for (size_t i = 0; i < v2.size(); i++)
{
// cout << v2[i];
if ((i + 1) % sample_size == 0)
{
// cout << endl;
if (i < v2.size() - 1)
{
// cout << ((v2.size() + i + 1) / 40) + 1 << " : ";
}
}
else
{
// cout << ", ";
}
if (v2[i] == 0)
{
sum = sum + 1;
}
else
{
sum = 0;
}
if (sum == sample_size)
{
isContain = true;
}
}
}
}
else
{
// cout << endl << "Sample size is too large" << endl;
}
delete_operator_batching(decr);
return isContain;
}
#include <iomanip>
#include "seal_api.h"
#include "util.h"
using namespace seal;
using namespace std;
string ciphertext_name;
string key_dir;
// vector<int64_t> decrypt(size_t poly_d, size_t p_modulus, int &sample_size, string &ciphertext_dir);
vector<int64_t> decrypt(int &sample_size, string &ciphertext_dir);
int main(int argc, char **argv)
{
if (argc != 4)
{
// 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 file path, sample size and secret key path" << endl;
return -1;
}
else
{
string dir = argv[1];
int sample_size = atol(argv[2]);
key_dir = argv[3];
vector<int64_t> result = decrypt(sample_size, dir);
for (int i = 0; i < result.size(); ++i)
{
std::cout << result[i] << ' ';
}
std::cout << endl;
return 0;
}
}
vector<int64_t> decrypt(int &sample_size, string &ciphertext_dir)
{
struct decryptor_t decr;
init_operator_batching(decr, key_dir);
// init_operator_batching(2048, 4294967296, decr, key_dir);
// init_operator_batching(4096, 4294967296, decr, key_dir);
// init_operator_batching(8192, 4294967296, decr, key_dir);
// init_operator_batching(16384, 4294967296, decr, key_dir);
// init_operator_batching(32768, 4294967296, decr, key_dir);
vector<int64_t> v;
if (sample_size <= decr.bcode->slot_count()/2)
{
Ciphertext cipher_matrix;
vector<int64_t> pod_matrix;
load_ciphertext(decr, cipher_matrix, ciphertext_dir);
pod_matrix = decrypt_ciphermatrix(decr, cipher_matrix);
for (size_t i = 0; i < sample_size; i++)
{
v.push_back(pod_matrix[i]);
}
}
else
{
// cout << endl << "Sample size is too large" << endl;
}
delete_operator_batching(decr);
return v;
}
#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"
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(encr, key_dir);
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 <iostream>
#include "seal_api.h"
#include "util.h"
// #include <algorithm>
// #include <iterator>
// #include <vector>
// #include <filesystem>
using namespace seal;
using namespace std;
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);
bool is_number(const string &s);
void printStrVector(const vector<string> &v);
vector<vector<string>> split_ends(const vector<string> &data, const vector<int> &ends);
void multiply_ciphertexts(struct evaluator_t &op_st, 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, 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);
int simpleCheck(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir);
int checkSq(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir);
Ciphertext check(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir,
struct encryptor_t &encr, struct evaluator_t &eval);
// string relink_key_path;
// string galois_key_path;
// string public_key_path;
string key_dir = "";
int main(int argc, char **argv)
{
// input processing - begin
// string result_name = argv[argc - 6];
// string result_dir = argv[argc - 5];
// int sample_size = atoi(argv[argc - 4]);
// relink_key_path = argv[argc - 3];
// galois_key_path = argv[argc - 2];
// public_key_path = argv[argc - 1];
string source = argv[1];
string result_name = argv[argc - 4];
string result_dir = argv[argc - 3];
int sample_size = atoi(argv[argc - 2]);
key_dir = argv[argc - 1];
vector<string> data;
for (int i = 2; i < argc - 4; i++)
{
data.push_back(argv[i]);
}
// if (source == "" || data.size() == 0 || result_name == "" || result_dir == "" || sample_size == 0 ||
// relink_key_path == "" || galois_key_path == "" || public_key_path == "")
if (source == "" || data.size() == 0 || result_name == "" || result_dir == "" || sample_size == 0 || key_dir == "")
{
// error handling
cout << "[ERROR] please enter a source path, data paths, output ciphertext file name or prefix, output "
"ciphertext directory, sample size, linking key path, galois key path and public key path"
<< endl;
return -1;
}
// input processing - end
// simple algo
// int result = simpleCheck(source, data, result_name, result_dir, sample_size, key_dir);
// sequence algo
int result = checkSq(source, data, result_name, result_dir, sample_size, key_dir);
// error handling
if (result == -1)
{
fprintf(stderr, "error!\n");
}
else
{
cout << "done";
}
// checkSq(source, data, result_name, result_dir, sample_size, relink_key_path, galois_key_path, public_key_path);
return result;
}
int simpleCheck(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir)
{
struct evaluator_t eval;
init_operator_batching(eval, key_dir);
struct encryptor_t encr;
init_operator_batching(encr, key_dir);
// cout << sample_size*data.size() << endl;
// cout << encr.bcode->slot_count() << endl;
if (sample_size*data.size() > encr.bcode->slot_count() || sample_size > encr.bcode->slot_count()/2)
{
// error handling
delete_operator_batching(encr);
delete_operator_batching(eval);
return -1;
}
else
{
Ciphertext encrypted_result_matrix;
vector<int64_t> result_matrix;
init_ciphermatrix(encr, result_matrix, encrypted_result_matrix);
vector<int64_t> dummy_matrix;
for (size_t i = 0; i < sample_size; i++)
{
dummy_matrix.push_back(1);
}
Ciphertext encrypted_dummy_matrix;
init_ciphermatrix(encr, dummy_matrix, encrypted_dummy_matrix);
// normalize input data if its size is odd
int normalized_data_size;
if (data.size() % 2 == 0)
{
normalized_data_size = data.size();
}
else
{
normalized_data_size = data.size() + 1;
}
int required_range = normalized_data_size * sample_size;
int required_no_row_elements = required_range / 2;
int required_range_row = required_range / 2;
// create padding matrix
int padding_slots = (encr.bcode->slot_count() / 2) - required_range_row;
vector<int64_t> padding_matrix(encr.bcode->slot_count(), 0ULL);
for (size_t i = 0; i < padding_slots; i++)
{
padding_matrix[required_no_row_elements + i] = 1;
padding_matrix[encr.bcode->slot_count() - i] = 1;
}
Ciphertext encrypted_padding_matrix;
init_ciphermatrix(encr, padding_matrix, encrypted_padding_matrix);
if (required_range_row <= encr.bcode->slot_count() && required_range_row > 0)
{
for (int index = 0; index < normalized_data_size / 2; index++)
{
Ciphertext ct1, ct2, ct3;
Ciphertext temp1, temp2;
// cout << "[INFO] loading ciphertext 1" << endl;
load_ciphertext(eval, ct1, source);
// cout << "[INFO] loading ciphertext 2" << endl;
load_ciphertext(eval, ct2, data.at(index));
sub_ciphertext(eval, ct1, ct2, temp1);
if ((normalized_data_size / 2) + index < data.size())
{
// cout << "[INFO] loading ciphertext 3" << endl;
load_ciphertext(eval, ct3, data.at((normalized_data_size / 2) + index));
sub_ciphertext(eval, ct1, ct3, temp2);
}
else
{
// add dummy vector for oddy data
temp2 = encrypted_dummy_matrix;
}
eval.eval->rotate_columns_inplace(temp2, eval.gk);
add_ciphertext(eval, temp1, temp2, temp1);
add_ciphertext(eval, temp1, encrypted_result_matrix, encrypted_result_matrix);
// avoid the last shift
if (index + 1 != (normalized_data_size / 2))
{
eval.eval->rotate_rows_inplace(encrypted_result_matrix, -sample_size, eval.gk);
}
}
// add renmaining padding slots
add_ciphertext(eval, encrypted_result_matrix, encrypted_padding_matrix, encrypted_result_matrix);
}
save_ciphertext(encrypted_result_matrix, result_dir + "/" + result_name + ".ct");
delete_operator_batching(encr);
delete_operator_batching(eval);
return 0;
}
}
// int checkSq(
// string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size,
// string &relink_key_path, string &galois_key_path, string &public_key_path)
int checkSq(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir)
{
struct evaluator_t eval;
// init_operator_batching(2048, 4294967296, eval, relink_key_path, galois_key_path);
// init_operator_batching(4096, 4294967296, eval, relink_key_path, galois_key_path);
// init_operator_batching(8192, 4294967296, eval, relink_key_path, galois_key_path);
// init_operator_batching(16384, 4294967296, eval, relink_key_path, galois_key_path);
// init_operator_batching(32768, 4294967296, eval, relink_key_path, galois_key_path);
init_operator_batching(eval, key_dir);
struct encryptor_t encr;
// init_operator_batching(4096, 4294967296, encr, public_key_path);
// init_operator_batching(8192, 4294967296, encr, public_key_path);
// init_operator_batching(16384, 4294967296, encr, public_key_path);
// init_operator_batching(32768, 4294967296, encr, public_key_path);
init_operator_batching(encr, key_dir);
// if (sample_size*data.size() > encr.bcode->slot_count() || sample_size > encr.bcode->slot_count()/2)
if (sample_size > encr.bcode->slot_count()/2)
{
// error handling
delete_operator_batching(encr);
delete_operator_batching(eval);
return -1;
}
else
{
// struct evaluator_t eval;
// // init_operator_batching(2048, 4294967296, eval, relink_key_path, galois_key_path);
// // init_operator_batching(4096, 4294967296, eval, relink_key_path, galois_key_path);
// init_operator_batching(8192, 4294967296, eval, relink_key_path, galois_key_path);
// // init_operator_batching(16384, 4294967296, eval, relink_key_path, galois_key_path);
// // init_operator_batching(32768, 4294967296, eval, relink_key_path, galois_key_path);
// struct encryptor_t encr;
// // init_operator_batching(4096, 4294967296, encr, public_key_path);
// init_operator_batching(8192, 4294967296, encr, public_key_path);
// // init_operator_batching(16384, 4294967296, encr, public_key_path);
// // init_operator_batching(32768, 4294967296, encr, public_key_path);
int capacity = (encr.bcode->slot_count()) / sample_size;
// cout << capacity << endl;
// vector<string> v_temp;
// vector<string> v_v_temp;
// Ciphertext result;
vector<Ciphertext> v_result;
int nSq = data.size() / capacity;
for (size_t i = 0; i < nSq; i++)
{
Ciphertext result;
// cout << "sq : " << i << endl;
vector<string> v_temp;
for (size_t j = 0; j < capacity; j++)
{
string str = data.back();
v_temp.push_back(str);
data.pop_back();
}
if (i == 0)
{
// cout << "sq : init" << endl;
// result = check(
// source, v_temp, result_name, result_dir, sample_size, relink_key_path, galois_key_path,
// public_key_path, encr, eval);
result = check(source, v_temp, result_name, result_dir, sample_size, key_dir, encr, eval);
v_result.push_back(result);
}
else
{
// cout << "sq : other" << endl;
// Ciphertext result2 = check(
// source, v_temp, result_name, result_dir, sample_size, relink_key_path, galois_key_path,
// public_key_path, encr, eval);
// multiply_ciphertext(eval, result2, result, result);
// relinearize_inplace(eval, result);
// result = check(
// source, v_temp, result_name, result_dir, sample_size, relink_key_path, galois_key_path,
// public_key_path, encr, eval);
result = check(source, v_temp, result_name, result_dir, sample_size, key_dir, encr, eval);
v_result.push_back(result);
}
}
if (data.size() % capacity != 0)
{
// cout << "oddy!!! " << endl;
// Ciphertext result = check(
// source, data, result_name, result_dir, sample_size, relink_key_path, galois_key_path,
// public_key_path, encr, eval);
Ciphertext result = check(source, data, result_name, result_dir, sample_size, key_dir, encr, eval);
// multiply_ciphertext(eval, result2, result, result);
// relinearize_inplace(eval, result);
v_result.push_back(result);
}
Ciphertext result;
multiply_ciphertexts(eval, v_result, result);
relinearize_inplace(eval, result);
save_ciphertext(result, result_dir + "/" + result_name + ".ct");
delete_operator_batching(encr);
delete_operator_batching(eval);
return 0;
}
}
// Ciphertext check(
// string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size,
// string &relink_key_path, string &galois_key_path, string &public_key_path, struct encryptor_t &encr,
// struct evaluator_t &eval)
Ciphertext check(
string &source, vector<string> &data, string &result_name, string &result_dir, int &sample_size, string &key_dir,
struct encryptor_t &encr, struct evaluator_t &eval)
{
// if (source == "" || data.size() == 0 || result_name == "" || result_dir == "" || sample_size == 0)
// {
// // error handling
// // return -1;
// }
// else
// {
Ciphertext encrypted_result_matrix;
vector<int64_t> result_matrix;
init_ciphermatrix(encr, result_matrix, encrypted_result_matrix);
vector<int64_t> dummy_matrix;
for (size_t i = 0; i < sample_size; i++)
{
dummy_matrix.push_back(1);
}
Ciphertext encrypted_dummy_matrix;
init_ciphermatrix(encr, dummy_matrix, encrypted_dummy_matrix);
// normalize input data if its size is odd
int normalized_data_size;
if (data.size() % 2 == 0)
{
normalized_data_size = data.size();
}
else
{
normalized_data_size = data.size() + 1;
}
int required_range = normalized_data_size * sample_size;
int required_no_row_elements = required_range / 2;
int required_range_row = required_range / 2;
// create padding matrix
int padding_slots = (encr.bcode->slot_count() / 2) - required_range_row;
vector<int64_t> padding_matrix(encr.bcode->slot_count(), 0ULL);
for (size_t i = 0; i < padding_slots; i++)
{
padding_matrix[required_no_row_elements + i] = 1;
padding_matrix[encr.bcode->slot_count() - i] = 1;
}
Ciphertext encrypted_padding_matrix;
init_ciphermatrix(encr, padding_matrix, encrypted_padding_matrix);
if (required_range_row <= encr.bcode->slot_count() && required_range_row > 0)
{
for (int index = 0; index < normalized_data_size / 2; index++)
{
Ciphertext ct1, ct2, ct3;
Ciphertext temp1, temp2;
// cout << "[INFO] loading ciphertext 1" << endl;
load_ciphertext(eval, ct1, source);
// cout << "[INFO] loading ciphertext 2" << endl;
load_ciphertext(eval, ct2, data.at(index));
sub_ciphertext(eval, ct1, ct2, temp1);
if ((normalized_data_size / 2) + index < data.size())
{
// cout << "[INFO] loading ciphertext 3" << endl;
load_ciphertext(eval, ct3, data.at((normalized_data_size / 2) + index));
sub_ciphertext(eval, ct1, ct3, temp2);
}
else
{
// add dummy vector for oddy data
temp2 = encrypted_dummy_matrix;
}
eval.eval->rotate_columns_inplace(temp2, eval.gk);
add_ciphertext(eval, temp1, temp2, temp1);
add_ciphertext(eval, temp1, encrypted_result_matrix, encrypted_result_matrix);
// avoid the last shift
if (index + 1 != (normalized_data_size / 2))
{
eval.eval->rotate_rows_inplace(encrypted_result_matrix, -sample_size, eval.gk);
}
}
// add renmaining padding slots
add_ciphertext(eval, encrypted_result_matrix, encrypted_padding_matrix, encrypted_result_matrix);
}
// // save_ciphertext(encrypted_result_matrix, result_dir + "/" + result_name + ".ct");
// delete_operator_batching(eval);
return encrypted_result_matrix;
// }
}
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, 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, 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);
}
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