Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix integration [WIP] #94

Open
wants to merge 8 commits into
base: public
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
9 changes: 9 additions & 0 deletions MATRIX/build.sh
Original file line number Diff line number Diff line change
@@ -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 -DABY_ENABLE_MATRIX=On
make -j`nproc`
Empty file added MATRIX/logs/dummylog
Empty file.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
110 changes: 110 additions & 0 deletions src/abycore/MATRIX/MatrixMeasurement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Created by liork on 8/22/18.
//

#ifndef ABY_MATRIXMEASUREMENT_H
#define ABY_MATRIXMEASUREMENT_H

#include <string>
#include <chrono>
#include <fstream>
#include <iostream>
#include <unistd.h>


using namespace std;
using namespace std::chrono;

class MatrixMeasurement
{
private:
string getcwdStr() const
{
char buff[256];
auto res = getcwd(buff, 255);
assert(res != NULL);
return std::string(buff);
}

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<string> m_tasksNames;
vector<vector<long>> m_cpuStartTimes;
vector<vector<long>> m_cpuEndTimes;
string m_arguments = "";

public:
MatrixMeasurement(size_t argc, char* argv[], vector<string> tasksNames, size_t numberOfIterations):
numberOfIterations(numberOfIterations),
m_tasksNames(tasksNames),
m_cpuStartTimes(vector<vector<long>>(tasksNames.size(), vector<long>(numberOfIterations))),
m_cpuEndTimes(vector<vector<long>>(tasksNames.size(), vector<long>(numberOfIterations)))
{

for(size_t idx = 0; idx < argc; ++idx)
{
string s(argv[idx]);
if (idx < argc - 1)
m_arguments += s + "*";
else
m_arguments += s;
}

}

auto get_ms_since_epoch() const
{
//Cast the time point to ms, then get its duration, then get the duration's count.
auto now = system_clock::now();
return time_point_cast<milliseconds>(now).time_since_epoch().count();
}

void startSubTask(string taskName, size_t currentIterationNumber)
{
auto taskIdx = getTaskIdx(taskName);
m_cpuStartTimes[taskIdx][currentIterationNumber] = get_ms_since_epoch();
}

void endSubTask(string taskName, size_t currentIterationNumber)
{
auto taskIdx = getTaskIdx(taskName);
m_cpuEndTimes[taskIdx][currentIterationNumber] = get_ms_since_epoch();
}

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)
{
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();
}
};

#endif //ABY_MATRIXMEASUREMENT_H
3 changes: 3 additions & 0 deletions src/examples/innerproduct/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
28 changes: 27 additions & 1 deletion src/examples/innerproduct/common/innerproduct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@
*/

#include "innerproduct.h"
#include "../../../abycore/sharing/sharing.h"
#include <abycore/sharing/sharing.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,
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<string> 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
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand All @@ -104,7 +124,13 @@ 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);

#ifdef ABY_ENABLE_MATRIX
log.write_log();
#endif

/**
Step 10: Type caste the plaintext output to 16 bit unsigned integer.
Expand Down
8 changes: 4 additions & 4 deletions src/examples/innerproduct/common/innerproduct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <abycore/circuit/booleancircuits.h>
#include <abycore/circuit/arithmeticcircuits.h>
#include <abycore/circuit/circuit.h>
#include <abycore/aby/abyparty.h>
#include <math.h>
#include <cassert>

Expand Down
2 changes: 1 addition & 1 deletion src/examples/innerproduct/innerproduct_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <ENCRYPTO_utils/crypto/crypto.h>
#include <ENCRYPTO_utils/parse_options.h>
//ABY Party class
#include "../../abycore/aby/abyparty.h"
#include <abycore/aby/abyparty.h>

#include "common/innerproduct.h"

Expand Down
27 changes: 27 additions & 0 deletions src/examples/innerproduct/run_matrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -euo pipefail

# read the arguments without party id

argc=$#
argv=($@)
values=""

for (( j=1; j<argc; j++ )); do
values+=`echo ${argv[j]}`
values+=' '
done

# read parties file
parties=()
while IFS= read -r line || [[ -n "${line}" ]]; do
l=`echo ${line} | cut -d'=' -f2`
parties+=(${l})
done < parties.conf

idx=${1}
addr=${parties[0]}

cd ../build/bin
./innerproduct_test -r ${idx} -a ${addr} ${values}