From a0e6893da05a551cac972a855d620af97302bedc Mon Sep 17 00:00:00 2001 From: liorbiu Date: Wed, 22 Aug 2018 15:04:35 +0300 Subject: [PATCH 1/8] Add install and run scripts --- MATRIX/build.sh | 9 +++++++++ MATRIX/run.sh | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 MATRIX/build.sh create mode 100755 MATRIX/run.sh diff --git a/MATRIX/build.sh b/MATRIX/build.sh new file mode 100644 index 00000000..16c0ebc3 --- /dev/null +++ b/MATRIX/build.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +# for custom, up-to-date CMake installed to ~/prefix +export PATH="$HOME/prefix/bin:$PATH" + +cd .. +mkdir -p build && cd build +cmake .. -DABY_BUILD_EXE=On +make -j`nproc` diff --git a/MATRIX/run.sh b/MATRIX/run.sh new file mode 100755 index 00000000..25d14289 --- /dev/null +++ b/MATRIX/run.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# read the arguments without party id + +argc=$# +argv=($@) +values="" + +for (( j=1; j(now).time_since_epoch().count(); + m_cpuStartTimes[taskIdx][currentIterationNumber] = ms; + } + + void endSubTask(string taskName, int currentIterationNumber) + { + int taskIdx = getTaskIdx(taskName); + auto now = system_clock::now(); + + //Cast the time point to ms, then get its duration, then get the duration's count. + auto ms = time_point_cast(now).time_since_epoch().count(); + m_cpuEndTimes[taskIdx][currentIterationNumber] = ms; + + // if this is the last task and last iteration write the data to file + if (taskIdx == m_tasksNames.size() - 1 && currentIterationNumber == m_cpuEndTimes[0].size() - 1) + { + string logFileName = getcwdStr() + "/" + m_arguments + ".log"; + cout << "Log file name : " << logFileName << endl; + ofstream logFile(logFileName); + if (logFile.is_open()) + { + //write to file + int numberOfIterations = m_cpuEndTimes[0].size(); + for (size_t idx = 0; idx < m_tasksNames.size(); ++idx) + { + logFile << m_tasksNames[idx] + ":"; + cout << "taskName : " << m_tasksNames[idx] << endl; + for (size_t idx2 = 0; idx2 < numberOfIterations; ++idx2) + { + cout << "value : " << m_cpuEndTimes[idx][idx2] << endl; + logFile << to_string(m_cpuEndTimes[idx][idx2]- m_cpuStartTimes[taskIdx][currentIterationNumber]) + + ","; + } + + logFile << "\n"; + } + logFile.close(); + } + } + } +}; + +#endif //ABY_MATRIXMEASUREMENT_H diff --git a/src/examples/innerproduct/common/innerproduct.cpp b/src/examples/innerproduct/common/innerproduct.cpp index 4930fba6..0606a443 100644 --- a/src/examples/innerproduct/common/innerproduct.cpp +++ b/src/examples/innerproduct/common/innerproduct.cpp @@ -18,11 +18,28 @@ #include "innerproduct.h" #include "../../../abycore/sharing/sharing.h" +#include "../../../../MATRIX/MatrixMeasurement.h" int32_t test_inner_product_circuit(e_role role, const std::string& address, uint16_t port, seclvl seclvl, uint32_t nvals, uint32_t bitlen, uint32_t nthreads, e_mt_gen_alg mt_alg, e_sharing sharing, uint32_t num) { + /** + Step 0: Init logger. + */ + char * argv[4]; + argv[0] = (char*)"innerproduct"; + argv[1] = (char*)malloc(16); + sprintf(argv[1], "%d", role); + argv[2] = (char*)malloc(16); + sprintf(argv[2], "%d", bitlen); + argv[3] = (char*)malloc(16); + sprintf(argv[3], "%d", seclvl.symbits); + + vector subTaskNames{"BuildInnerProductCircuit", "ExecCircuit"}; + + MatrixMeasurement log(4, argv, subTaskNames, 1); + /** Step 1: Create the ABYParty object which defines the basis of all the operations which are happening. Operations performed are on the @@ -31,6 +48,7 @@ int32_t test_inner_product_circuit(e_role role, const std::string& address, uint ABYParty* party = new ABYParty(role, address, port, seclvl, bitlen, nthreads, mt_alg); + /** Step 2: Get to know all the sharing types available in the program. */ @@ -92,8 +110,10 @@ int32_t test_inner_product_circuit(e_role role, const std::string& address, uint problem by passing the shared objects and circuit object. Don't forget to type cast the circuit object to type of share */ + log.startSubTask("BuildInnerProductCircuit", 0); s_out = BuildInnerProductCircuit(s_x_vec, s_y_vec, num, (ArithmeticCircuit*) circ); + log.endSubTask("BuildInnerProductCircuit", 0); /** Step 8: Output the value of s_out (the computation result) to both parties @@ -104,7 +124,9 @@ int32_t test_inner_product_circuit(e_role role, const std::string& address, uint Step 9: Executing the circuit using the ABYParty object evaluate the problem. */ + log.startSubTask("ExecCircuit", 0); party->ExecCircuit(); + log.endSubTask("ExecCircuit", 0); /** Step 10: Type caste the plaintext output to 16 bit unsigned integer. From e32f802e0471e1a3653644fbe9860a36cab7dc28 Mon Sep 17 00:00:00 2001 From: liorbiu Date: Thu, 23 Aug 2018 15:57:28 +0300 Subject: [PATCH 3/8] logs are created under MATRIX dir Add dummy log. --- MATRIX/MatrixMeasurement.h | 3 +-- MATRIX/logs/dummylog | 0 2 files changed, 1 insertion(+), 2 deletions(-) create mode 100644 MATRIX/logs/dummylog diff --git a/MATRIX/MatrixMeasurement.h b/MATRIX/MatrixMeasurement.h index 55dce431..9c7ec766 100644 --- a/MATRIX/MatrixMeasurement.h +++ b/MATRIX/MatrixMeasurement.h @@ -75,8 +75,7 @@ class MatrixMeasurement // if this is the last task and last iteration write the data to file if (taskIdx == m_tasksNames.size() - 1 && currentIterationNumber == m_cpuEndTimes[0].size() - 1) { - string logFileName = getcwdStr() + "/" + m_arguments + ".log"; - cout << "Log file name : " << logFileName << endl; + string logFileName = getcwdStr() + "/../../MATRIX/logs/" + m_arguments + ".log"; ofstream logFile(logFileName); if (logFile.is_open()) { diff --git a/MATRIX/logs/dummylog b/MATRIX/logs/dummylog new file mode 100644 index 00000000..e69de29b From d98919d7eac678e3bc715a7dface858219679c4e Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sun, 30 Sep 2018 18:24:04 +0200 Subject: [PATCH 4/8] move Matrix logger and innerproduct run script The Matrix logger class can now be included as ``. Since the run.sh script is specific for the innerproduct example, it is moved to the corresponding directory. --- {MATRIX => src/abycore/MATRIX}/MatrixMeasurement.h | 0 src/examples/innerproduct/common/innerproduct.cpp | 2 +- MATRIX/run.sh => src/examples/innerproduct/run_matrix.sh | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename {MATRIX => src/abycore/MATRIX}/MatrixMeasurement.h (100%) rename MATRIX/run.sh => src/examples/innerproduct/run_matrix.sh (100%) diff --git a/MATRIX/MatrixMeasurement.h b/src/abycore/MATRIX/MatrixMeasurement.h similarity index 100% rename from MATRIX/MatrixMeasurement.h rename to src/abycore/MATRIX/MatrixMeasurement.h diff --git a/src/examples/innerproduct/common/innerproduct.cpp b/src/examples/innerproduct/common/innerproduct.cpp index 0606a443..4268bd86 100644 --- a/src/examples/innerproduct/common/innerproduct.cpp +++ b/src/examples/innerproduct/common/innerproduct.cpp @@ -18,7 +18,7 @@ #include "innerproduct.h" #include "../../../abycore/sharing/sharing.h" -#include "../../../../MATRIX/MatrixMeasurement.h" +#include "../../../abycore/MATRIX/MatrixMeasurement.h" int32_t test_inner_product_circuit(e_role role, const std::string& address, uint16_t port, seclvl seclvl, uint32_t nvals, uint32_t bitlen, uint32_t nthreads, e_mt_gen_alg mt_alg, diff --git a/MATRIX/run.sh b/src/examples/innerproduct/run_matrix.sh similarity index 100% rename from MATRIX/run.sh rename to src/examples/innerproduct/run_matrix.sh From b67fe4a98c8a45ce90acb922cf007876701cb1e2 Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Sun, 30 Sep 2018 18:36:17 +0200 Subject: [PATCH 5/8] change includes in the innerproduct example Instead of relative imports, we now use the form `#include `. --- src/examples/innerproduct/common/innerproduct.cpp | 4 ++-- src/examples/innerproduct/common/innerproduct.h | 8 ++++---- src/examples/innerproduct/innerproduct_test.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/examples/innerproduct/common/innerproduct.cpp b/src/examples/innerproduct/common/innerproduct.cpp index 4268bd86..0f97ef6f 100644 --- a/src/examples/innerproduct/common/innerproduct.cpp +++ b/src/examples/innerproduct/common/innerproduct.cpp @@ -17,8 +17,8 @@ */ #include "innerproduct.h" -#include "../../../abycore/sharing/sharing.h" -#include "../../../abycore/MATRIX/MatrixMeasurement.h" +#include +#include int32_t test_inner_product_circuit(e_role role, const std::string& address, uint16_t port, seclvl seclvl, uint32_t nvals, uint32_t bitlen, uint32_t nthreads, e_mt_gen_alg mt_alg, diff --git a/src/examples/innerproduct/common/innerproduct.h b/src/examples/innerproduct/common/innerproduct.h index bea627b1..68847dae 100644 --- a/src/examples/innerproduct/common/innerproduct.h +++ b/src/examples/innerproduct/common/innerproduct.h @@ -19,10 +19,10 @@ #ifndef __INNERPRODUCT_H_ #define __INNERPRODUCT_H_ -#include "../../../abycore/circuit/booleancircuits.h" -#include "../../../abycore/circuit/arithmeticcircuits.h" -#include "../../../abycore/circuit/circuit.h" -#include "../../../abycore/aby/abyparty.h" +#include +#include +#include +#include #include #include diff --git a/src/examples/innerproduct/innerproduct_test.cpp b/src/examples/innerproduct/innerproduct_test.cpp index 6fe93a35..1df5edd2 100644 --- a/src/examples/innerproduct/innerproduct_test.cpp +++ b/src/examples/innerproduct/innerproduct_test.cpp @@ -20,7 +20,7 @@ #include #include //ABY Party class -#include "../../abycore/aby/abyparty.h" +#include #include "common/innerproduct.h" From c3605b2da1b29dc1179f9825833ec762cb9e0dab Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Tue, 9 Oct 2018 21:35:28 +0200 Subject: [PATCH 6/8] fix issues in Matrix logger class --- src/abycore/MATRIX/MatrixMeasurement.h | 89 +++++++++++++++----------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/src/abycore/MATRIX/MatrixMeasurement.h b/src/abycore/MATRIX/MatrixMeasurement.h index 9c7ec766..260096bc 100644 --- a/src/abycore/MATRIX/MatrixMeasurement.h +++ b/src/abycore/MATRIX/MatrixMeasurement.h @@ -9,6 +9,7 @@ #include #include #include +#include using namespace std; @@ -17,26 +18,31 @@ using namespace std::chrono; class MatrixMeasurement { private: - string getcwdStr() + string getcwdStr() const { - char* buff;//automatically cleaned when it exits scope - return string(getcwd(buff,255)); + char buff[256]; + auto res = getcwd(buff, 255); + assert(res != NULL); + return std::string(buff); } - int getTaskIdx(string name) + size_t getTaskIdx(string name) const { auto it = std::find(m_tasksNames.begin(), m_tasksNames.end(), name); auto idx = distance(m_tasksNames.begin(), it); return idx; } + size_t numberOfIterations; + vector m_tasksNames; vector> m_cpuStartTimes; vector> m_cpuEndTimes; string m_arguments = ""; - vector m_tasksNames; public: - MatrixMeasurement(size_t argc, char* argv[], vector tasksNames, int numberOfIterations): + MatrixMeasurement(size_t argc, char* argv[], vector tasksNames, size_t numberOfIterations): + numberOfIterations(numberOfIterations), + m_tasksNames(tasksNames), m_cpuStartTimes(vector>(tasksNames.size(), vector(numberOfIterations))), m_cpuEndTimes(vector>(tasksNames.size(), vector(numberOfIterations))) { @@ -50,53 +56,60 @@ class MatrixMeasurement m_arguments += s; } - m_tasksNames = tasksNames; } - void startSubTask(string taskName, int currentIterationNumber) + auto get_ms_since_epoch() const { - int taskIdx = getTaskIdx(taskName); - auto now = system_clock::now(); - //Cast the time point to ms, then get its duration, then get the duration's count. - auto ms = time_point_cast(now).time_since_epoch().count(); - m_cpuStartTimes[taskIdx][currentIterationNumber] = ms; + auto now = system_clock::now(); + return time_point_cast(now).time_since_epoch().count(); } - void endSubTask(string taskName, int currentIterationNumber) + void startSubTask(string taskName, size_t currentIterationNumber) { - int taskIdx = getTaskIdx(taskName); - auto now = system_clock::now(); + auto taskIdx = getTaskIdx(taskName); + m_cpuStartTimes[taskIdx][currentIterationNumber] = get_ms_since_epoch(); + } - //Cast the time point to ms, then get its duration, then get the duration's count. - auto ms = time_point_cast(now).time_since_epoch().count(); - m_cpuEndTimes[taskIdx][currentIterationNumber] = ms; + void endSubTask(string taskName, size_t currentIterationNumber) + { + auto taskIdx = getTaskIdx(taskName); + m_cpuEndTimes[taskIdx][currentIterationNumber] = get_ms_since_epoch(); // if this is the last task and last iteration write the data to file if (taskIdx == m_tasksNames.size() - 1 && currentIterationNumber == m_cpuEndTimes[0].size() - 1) { - string logFileName = getcwdStr() + "/../../MATRIX/logs/" + m_arguments + ".log"; - ofstream logFile(logFileName); - if (logFile.is_open()) + write_log(); + } + } + + void write_log() const + { + string logFileName = getcwdStr() + "/../../MATRIX/logs/" + m_arguments + ".log"; + ofstream logFile(logFileName); + if (!logFile.is_open()) + { + cerr << "MatrixMeasurement: Could not open log file '" + << logFileName + << "'\n"; + return; + } + //write to file + for (size_t task_idx = 0; task_idx < m_tasksNames.size(); ++task_idx) + { + logFile << m_tasksNames[task_idx] + ":"; + cout << "taskName : " << m_tasksNames[task_idx] << endl; + for (size_t iteration = 0; iteration < numberOfIterations; ++iteration) { - //write to file - int numberOfIterations = m_cpuEndTimes[0].size(); - for (size_t idx = 0; idx < m_tasksNames.size(); ++idx) - { - logFile << m_tasksNames[idx] + ":"; - cout << "taskName : " << m_tasksNames[idx] << endl; - for (size_t idx2 = 0; idx2 < numberOfIterations; ++idx2) - { - cout << "value : " << m_cpuEndTimes[idx][idx2] << endl; - logFile << to_string(m_cpuEndTimes[idx][idx2]- m_cpuStartTimes[taskIdx][currentIterationNumber]) - + ","; - } - - logFile << "\n"; - } - logFile.close(); + cout << "value : " << m_cpuEndTimes[task_idx][iteration] << endl; + logFile << to_string(m_cpuEndTimes[task_idx][iteration] + - m_cpuStartTimes[task_idx][iteration]) + << ","; } + + logFile << "\n"; } + logFile.close(); } }; From c1496d45ebf03649f6959776f14e0a3fe0c7a3ed Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Fri, 12 Oct 2018 13:54:20 +0200 Subject: [PATCH 7/8] add compile time option for matrix output The parameter `-DABY_ENABLE_MATRIX=On` to cmake now enables output for the MATRIX framework. Also, the `write_log` method is not called automatically anymore after the last task. --- CMakeLists.txt | 1 + MATRIX/build.sh | 2 +- README.md | 3 +++ src/abycore/MATRIX/MatrixMeasurement.h | 6 ------ src/examples/innerproduct/CMakeLists.txt | 3 +++ src/examples/innerproduct/common/innerproduct.cpp | 4 ++++ 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a03b222e..eab3b031 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.10) project(ABY LANGUAGES CXX) option(ABY_BUILD_EXE "Build executables" OFF) +option(ABY_ENABLE_MATRIX "Enable logging for the MATRIX framework in examples applications" OFF) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") diff --git a/MATRIX/build.sh b/MATRIX/build.sh index 16c0ebc3..9481ea90 100644 --- a/MATRIX/build.sh +++ b/MATRIX/build.sh @@ -5,5 +5,5 @@ export PATH="$HOME/prefix/bin:$PATH" cd .. mkdir -p build && cd build -cmake .. -DABY_BUILD_EXE=On +cmake .. -DABY_BUILD_EXE=On -DABY_ENABLE_MATRIX=On make -j`nproc` diff --git a/README.md b/README.md index 76922336..34475eff 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,9 @@ applications, you use the `ABY_BUILD_EXE` option: cmake .. -DABY_BUILD_EXE=On ``` +If also `-DABY_ENABLE_MATRIX=On` is given, then the examples output data for +the [MATRIX framework](https://github.com/cryptobiu/MATRIX) (if available). + ###### Build Options You can choose the build type, e.g. `Release` or `Debug` using diff --git a/src/abycore/MATRIX/MatrixMeasurement.h b/src/abycore/MATRIX/MatrixMeasurement.h index 260096bc..1c8cab93 100644 --- a/src/abycore/MATRIX/MatrixMeasurement.h +++ b/src/abycore/MATRIX/MatrixMeasurement.h @@ -75,12 +75,6 @@ class MatrixMeasurement { auto taskIdx = getTaskIdx(taskName); m_cpuEndTimes[taskIdx][currentIterationNumber] = get_ms_since_epoch(); - - // if this is the last task and last iteration write the data to file - if (taskIdx == m_tasksNames.size() - 1 && currentIterationNumber == m_cpuEndTimes[0].size() - 1) - { - write_log(); - } } void write_log() const diff --git a/src/examples/innerproduct/CMakeLists.txt b/src/examples/innerproduct/CMakeLists.txt index be90b660..419fb7d7 100644 --- a/src/examples/innerproduct/CMakeLists.txt +++ b/src/examples/innerproduct/CMakeLists.txt @@ -1,3 +1,6 @@ add_executable(innerproduct_test innerproduct_test.cpp common/innerproduct.cpp) target_link_libraries(innerproduct_test ABY::aby ENCRYPTO_utils::encrypto_utils) +if(ABY_ENABLE_MATRIX) + target_compile_definitions(innerproduct_test PRIVATE ABY_ENABLE_MATRIX) +endif() diff --git a/src/examples/innerproduct/common/innerproduct.cpp b/src/examples/innerproduct/common/innerproduct.cpp index 0f97ef6f..2cefdf0c 100644 --- a/src/examples/innerproduct/common/innerproduct.cpp +++ b/src/examples/innerproduct/common/innerproduct.cpp @@ -128,6 +128,10 @@ int32_t test_inner_product_circuit(e_role role, const std::string& address, uint party->ExecCircuit(); log.endSubTask("ExecCircuit", 0); +#ifdef ABY_ENABLE_MATRIX + log.write_log(); +#endif + /** Step 10: Type caste the plaintext output to 16 bit unsigned integer. */ From 39d88cf3e66f444b360dac1433a9b58668a23bda Mon Sep 17 00:00:00 2001 From: Lennart Braun Date: Fri, 26 Oct 2018 14:14:31 +0200 Subject: [PATCH 8/8] run_matrix.sh: enable errexit, nounset and pipefail --- src/examples/innerproduct/run_matrix.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/examples/innerproduct/run_matrix.sh b/src/examples/innerproduct/run_matrix.sh index 25d14289..6d78a4aa 100755 --- a/src/examples/innerproduct/run_matrix.sh +++ b/src/examples/innerproduct/run_matrix.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -euo pipefail + # read the arguments without party id argc=$# @@ -22,4 +24,4 @@ idx=${1} addr=${parties[0]} cd ../build/bin -./innerproduct_test -r ${idx} -a ${addr} ${values} \ No newline at end of file +./innerproduct_test -r ${idx} -a ${addr} ${values}