diff --git a/tests/tfhe/CMakeLists.txt b/tests/tfhe/CMakeLists.txt index 617e603..76ebd23 100644 --- a/tests/tfhe/CMakeLists.txt +++ b/tests/tfhe/CMakeLists.txt @@ -1,18 +1,17 @@ cmake_minimum_required(VERSION 3.9) -set(test_names - hello - trans1 - trans2 -) +FILE(GLOB test_names [^_]*) set(target_names "") -foreach (name IN ITEMS ${test_names}) - add_subdirectory(${name}) - list(APPEND target_names "tfhe-${name}") +foreach (child ${test_names}) + if(IS_DIRECTORY ${child}) + get_filename_component(name ${child} NAME) + add_subdirectory(${name}) + list(APPEND target_names "tfhe-${name}") + endif() endforeach() add_custom_target(tests_tfhe ALL DEPENDS ${target_names} -) +) \ No newline at end of file diff --git a/tests/tfhe/_template/CMakeLists.txt b/tests/tfhe/_template/CMakeLists.txt new file mode 100644 index 0000000..82abf8e --- /dev/null +++ b/tests/tfhe/_template/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.9) + +set(NAME "@NAME@") # project name + +add_executable(tfhe-${NAME}-exec main.cxx) +target_link_libraries(tfhe-${NAME}-exec common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-encrypt encrypt.cxx) +target_link_libraries(tfhe-${NAME}-encrypt common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-decrypt decrypt.cxx) +target_link_libraries(tfhe-${NAME}-decrypt common tfhe_bit_exec) + +add_custom_target(tfhe-${NAME} + DEPENDS + tfhe-${NAME}-exec + tfhe-${NAME}-encrypt + tfhe-${NAME}-decrypt + tfhe +) + +set(APPS_DIR ${CMAKE_BINARY_DIR}/apps) +configure_file("run.sh.in" "run.sh" @ONLY) diff --git a/tests/tfhe/_template/decrypt.cxx b/tests/tfhe/_template/decrypt.cxx new file mode 100644 index 0000000..4b3187d --- /dev/null +++ b/tests/tfhe/_template/decrypt.cxx @@ -0,0 +1,44 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + CiInt c{(uint16_t)-1}; // automatically determine size and signedness from value + + c.read("c").decrypt(); + + uint16_t c_val = c.get_val(); + + cout << c_val << endl; +} diff --git a/tests/tfhe/_template/encrypt.cxx b/tests/tfhe/_template/encrypt.cxx new file mode 100644 index 0000000..55039eb --- /dev/null +++ b/tests/tfhe/_template/encrypt.cxx @@ -0,0 +1,51 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main(int argc, char* argv[]) { + if (argc < 3) { + cerr << "Usage: " << argv[0] << " " << endl; + exit(-1); + } + + /* get input values */ + int a_val = atoi(argv[1]); + int b_val = atoi(argv[2]); + + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + CiInt a{a_val, 8, false}; + CiInt b{b_val, 8, false}; + + a.encrypt().write("a"); + b.encrypt().write("b"); +} diff --git a/tests/tfhe/_template/main.cxx b/tests/tfhe/_template/main.cxx new file mode 100644 index 0000000..3723c9d --- /dev/null +++ b/tests/tfhe/_template/main.cxx @@ -0,0 +1,55 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +/* local includes */ +#include +#include +#include +#include +#include +#include + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Set context to tfhe bit executor and size minimized integer + * operations */ + CiContext::set_config( + make_shared>>( + "tfhe.pk", TfheBitExec::Public), + make_shared()); + + CiInt a{CiInt::u8}; // create from unsigned 8-bit template + CiInt b{0, 8, false}; // manually specify value, size and signedness + CiInt c{ + (uint16_t)-1}; // automatically determine size and signedness from value + + a.read("a"); + b.read("b"); + + c = a + b; + // c = a * a * b - a; + + c.write("c"); + + CiContext::get_bit_exec_t>()->print(); +} diff --git a/tests/tfhe/_template/run.sh.in b/tests/tfhe/_template/run.sh.in new file mode 100644 index 0000000..f633a07 --- /dev/null +++ b/tests/tfhe/_template/run.sh.in @@ -0,0 +1,44 @@ +#!/bin/bash + +# +# (C) Copyright 2019 CEA LIST. All Rights Reserved. +# Contributor(s): Cingulata team (formerly Armadillo team) +# +# This software is governed by the CeCILL-C license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-C +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-C license and that you accept its terms. +# +# + +APPS_DIR=@APPS_DIR@ +NAME=@NAME@ + +cd `dirname $0` + +rm -f a_* b_* c_* + +echo "Input encryption" + +# Generate keys +echo "TFHE key generation" +$APPS_DIR/tfhe-keygen + +# Encrypt input "a" and "b" +./tfhe-$NAME-encrypt 5 3 + +echo "Homomorphic execution..." +time ./tfhe-$NAME-exec + +echo "Output decryption" +./tfhe-$NAME-decrypt diff --git a/tests/tfhe/new_project.sh b/tests/tfhe/new_project.sh new file mode 100644 index 0000000..117708c --- /dev/null +++ b/tests/tfhe/new_project.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Script for creating a new tfhe project. The new project is the same as _template project. + +SCRIPT_DIR=$0 + +if [ "$#" -lt 1 ]; then + echo "Usage: $SCRIPT_DIR " + exit +fi + +NAME=$1 + +# goto script directory, as next commands are relative to it +cd `dirname $SCRIPT_DIR` + +mkdir $NAME || exit + +cp -rn _template/* $NAME/ + +# rename variables +sed -i "s/@NAME@/$NAME/g" $NAME/CMakeLists.txt +sed -i "s/@NAME@/$NAME/g" $NAME/run.sh.in + +touch CMakeLists.txt #do it so that cmake rebuilts +echo "Project $NAME created" \ No newline at end of file diff --git a/tests/tfhe/wifs_cmp/CMakeLists.txt b/tests/tfhe/wifs_cmp/CMakeLists.txt new file mode 100644 index 0000000..e429b30 --- /dev/null +++ b/tests/tfhe/wifs_cmp/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.9) + +set(NAME "wifs_cmp") # project name + +add_executable(tfhe-${NAME}-exec main.cxx) +target_link_libraries(tfhe-${NAME}-exec common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-encrypt encrypt.cxx) +target_link_libraries(tfhe-${NAME}-encrypt common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-decrypt decrypt.cxx) +target_link_libraries(tfhe-${NAME}-decrypt common tfhe_bit_exec) + +add_custom_target(tfhe-${NAME} + DEPENDS + tfhe-${NAME}-exec + tfhe-${NAME}-encrypt + tfhe-${NAME}-decrypt + tfhe +) + +set(APPS_DIR ${CMAKE_BINARY_DIR}/apps) +configure_file("run.sh.in" "run.sh" @ONLY) diff --git a/tests/tfhe/wifs_cmp/decrypt.cxx b/tests/tfhe/wifs_cmp/decrypt.cxx new file mode 100644 index 0000000..2efc5e9 --- /dev/null +++ b/tests/tfhe/wifs_cmp/decrypt.cxx @@ -0,0 +1,44 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + CiBit c; // automatically determine size and signedness from value + + c.read("c").decrypt(); + + uint16_t c_val = c.get_val(); + + cout << c_val << endl; +} diff --git a/tests/tfhe/wifs_cmp/encrypt.cxx b/tests/tfhe/wifs_cmp/encrypt.cxx new file mode 100644 index 0000000..f86f5f8 --- /dev/null +++ b/tests/tfhe/wifs_cmp/encrypt.cxx @@ -0,0 +1,51 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main(int argc, char* argv[]) { + if (argc < 3) { + cerr << "Usage: " << argv[0] << " " << endl; + exit(-1); + } + + /* get input values */ + int a_val = atoi(argv[1]); + int b_val = atoi(argv[2]); + + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + CiInt a{a_val, 128, false}; + CiInt b{b_val, 128, false}; + + a.encrypt().write("a"); + b.encrypt().write("b"); +} diff --git a/tests/tfhe/wifs_cmp/main.cxx b/tests/tfhe/wifs_cmp/main.cxx new file mode 100644 index 0000000..a7978ad --- /dev/null +++ b/tests/tfhe/wifs_cmp/main.cxx @@ -0,0 +1,52 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +/* local includes */ +#include +#include +#include +#include +#include +#include + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Set context to tfhe bit executor and size minimized integer + * operations */ + CiContext::set_config( + make_shared>>( + "tfhe.pk", TfheBitExec::Public), + make_shared()); + + CiInt a{0, 128, false}; // create from unsigned 8-bit template + CiInt b{0, 128, false}; // manually specify value, size and signedness + + a.read("a"); + b.read("b"); + + CiBit c = a <= b; + + c.write("c"); + + CiContext::get_bit_exec_t>()->print(); +} diff --git a/tests/tfhe/wifs_cmp/run.sh.in b/tests/tfhe/wifs_cmp/run.sh.in new file mode 100644 index 0000000..7cf8275 --- /dev/null +++ b/tests/tfhe/wifs_cmp/run.sh.in @@ -0,0 +1,44 @@ +#!/bin/bash + +# +# (C) Copyright 2019 CEA LIST. All Rights Reserved. +# Contributor(s): Cingulata team (formerly Armadillo team) +# +# This software is governed by the CeCILL-C license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-C +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-C license and that you accept its terms. +# +# + +APPS_DIR=@APPS_DIR@ +NAME=wifs_cmp + +cd `dirname $0` + +rm -f a_* b_* c_* + +echo "Input encryption" + +# Generate keys +echo "TFHE key generation" +$APPS_DIR/tfhe-keygen + +# Encrypt input "a" and "b" +./tfhe-$NAME-encrypt 5 3 + +echo "Homomorphic execution..." +time ./tfhe-$NAME-exec + +echo "Output decryption" +./tfhe-$NAME-decrypt diff --git a/tests/tfhe/wifs_min/CMakeLists.txt b/tests/tfhe/wifs_min/CMakeLists.txt new file mode 100644 index 0000000..31341b3 --- /dev/null +++ b/tests/tfhe/wifs_min/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.9) + +set(NAME "wifs_min") # project name + +add_definitions(-DN=4) + +add_executable(tfhe-${NAME}-exec main.cxx) +target_link_libraries(tfhe-${NAME}-exec common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-encrypt encrypt.cxx) +target_link_libraries(tfhe-${NAME}-encrypt common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-decrypt decrypt.cxx) +target_link_libraries(tfhe-${NAME}-decrypt common tfhe_bit_exec) + +add_custom_target(tfhe-${NAME} + DEPENDS + tfhe-${NAME}-exec + tfhe-${NAME}-encrypt + tfhe-${NAME}-decrypt + tfhe +) + +set(APPS_DIR ${CMAKE_BINARY_DIR}/apps) +configure_file("run.sh.in" "run.sh" @ONLY) diff --git a/tests/tfhe/wifs_min/decrypt.cxx b/tests/tfhe/wifs_min/decrypt.cxx new file mode 100644 index 0000000..1d1d73f --- /dev/null +++ b/tests/tfhe/wifs_min/decrypt.cxx @@ -0,0 +1,41 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + CiInt r{CiInt::u8}; + r.read("r").decrypt(); + uint r_val = r.get_val(); + cout << r_val << endl; +} diff --git a/tests/tfhe/wifs_min/encrypt.cxx b/tests/tfhe/wifs_min/encrypt.cxx new file mode 100644 index 0000000..c611540 --- /dev/null +++ b/tests/tfhe/wifs_min/encrypt.cxx @@ -0,0 +1,47 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main(int argc, char* argv[]) { + if (argc < 1+N) { + cerr << "Usage: " << argv[0] << " []+" << endl; + exit(-1); + } + + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + for (int i = 0; i < N; ++i) { + CiInt a{CiInt::u8}; + a = atoi(argv[i+1]); + a.encrypt().write("a_" + to_string(i)); + } +} diff --git a/tests/tfhe/wifs_min/main.cxx b/tests/tfhe/wifs_min/main.cxx new file mode 100644 index 0000000..c84cd41 --- /dev/null +++ b/tests/tfhe/wifs_min/main.cxx @@ -0,0 +1,55 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +/* local includes */ +#include +#include +#include +#include +#include +#include +#include + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Set context to tfhe bit executor and size minimized integer + * operations */ + CiContext::set_config( + make_shared>>( + "tfhe.pk", TfheBitExec::Public), + make_shared()); + + vector a(N, CiInt::u8); + for (int i = 0; i < N; ++i) + a[i].read("a_" + to_string(i)); + + + CiInt r = a[0]; + for (int i = 1; i < N; ++i) { + r = select(r > a[i], a[i], r); + } + + r.write("r"); + + CiContext::get_bit_exec_t>()->print(); +} diff --git a/tests/tfhe/wifs_min/run.sh.in b/tests/tfhe/wifs_min/run.sh.in new file mode 100644 index 0000000..f165c93 --- /dev/null +++ b/tests/tfhe/wifs_min/run.sh.in @@ -0,0 +1,44 @@ +#!/bin/bash + +# +# (C) Copyright 2019 CEA LIST. All Rights Reserved. +# Contributor(s): Cingulata team (formerly Armadillo team) +# +# This software is governed by the CeCILL-C license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-C +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-C license and that you accept its terms. +# +# + +APPS_DIR=@APPS_DIR@ +NAME=wifs_min + +cd `dirname $0` + +rm -f a_* b_* c_* + +echo "Input encryption" + +# Generate keys +echo "TFHE key generation" +$APPS_DIR/tfhe-keygen + +# Encrypt input "a" and "b" +./tfhe-$NAME-encrypt 5 3 1 -10 + +echo "Homomorphic execution..." +time ./tfhe-$NAME-exec + +echo "Output decryption" +./tfhe-$NAME-decrypt diff --git a/tests/tfhe/wifs_sort/CMakeLists.txt b/tests/tfhe/wifs_sort/CMakeLists.txt new file mode 100644 index 0000000..248446a --- /dev/null +++ b/tests/tfhe/wifs_sort/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.9) + +set(NAME "wifs_sort") # project name + +add_definitions(-DN=4) + +add_executable(tfhe-${NAME}-exec main.cxx) +target_link_libraries(tfhe-${NAME}-exec common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-encrypt encrypt.cxx) +target_link_libraries(tfhe-${NAME}-encrypt common tfhe_bit_exec) + +add_executable(tfhe-${NAME}-decrypt decrypt.cxx) +target_link_libraries(tfhe-${NAME}-decrypt common tfhe_bit_exec) + +add_custom_target(tfhe-${NAME} + DEPENDS + tfhe-${NAME}-exec + tfhe-${NAME}-encrypt + tfhe-${NAME}-decrypt + tfhe +) + +set(APPS_DIR ${CMAKE_BINARY_DIR}/apps) +configure_file("run.sh.in" "run.sh" @ONLY) diff --git a/tests/tfhe/wifs_sort/decrypt.cxx b/tests/tfhe/wifs_sort/decrypt.cxx new file mode 100644 index 0000000..5d4b8de --- /dev/null +++ b/tests/tfhe/wifs_sort/decrypt.cxx @@ -0,0 +1,43 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + for (int i = 0; i < N; ++i) { + CiInt r{CiInt::u8}; + r.read("r_" + to_string(i)).decrypt(); + uint r_val = r.get_val(); + cout << r_val << endl; + } +} diff --git a/tests/tfhe/wifs_sort/encrypt.cxx b/tests/tfhe/wifs_sort/encrypt.cxx new file mode 100644 index 0000000..c611540 --- /dev/null +++ b/tests/tfhe/wifs_sort/encrypt.cxx @@ -0,0 +1,47 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +#include + +/* local includes */ +#include +#include +#include + + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main(int argc, char* argv[]) { + if (argc < 1+N) { + cerr << "Usage: " << argv[0] << " []+" << endl; + exit(-1); + } + + /* Only tfhe bit executor is needed for encryption/decryption and IO operations */ + CiContext::set_bit_exec(make_shared("tfhe.sk", TfheBitExec::Secret)); + + for (int i = 0; i < N; ++i) { + CiInt a{CiInt::u8}; + a = atoi(argv[i+1]); + a.encrypt().write("a_" + to_string(i)); + } +} diff --git a/tests/tfhe/wifs_sort/main.cxx b/tests/tfhe/wifs_sort/main.cxx new file mode 100644 index 0000000..f488da9 --- /dev/null +++ b/tests/tfhe/wifs_sort/main.cxx @@ -0,0 +1,68 @@ +/* + (C) Copyright 2019 CEA LIST. All Rights Reserved. + Contributor(s): Cingulata team (formerly Armadillo team) + + This software is governed by the CeCILL-C license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL-C + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL-C license and that you accept its terms. +*/ + +/* local includes */ +#include +#include +#include +#include +#include +#include +#include + +/* namespaces */ +using namespace std; +using namespace cingulata; + +int main() { + /* Set context to tfhe bit executor and size minimized integer + * operations */ + CiContext::set_config( + make_shared>>( + "tfhe.pk", TfheBitExec::Public), + make_shared()); + + vector a(N, CiInt::u8); + for (int i = 0; i < N; ++i) + a[i].read("a_" + to_string(i)); + + for (int i = 0; i < N-1; ++i) { + for (int j = i+1; j < N; ++j) { + CiBit swap = a[i] > a[j]; + CiInt t = select(swap, a[i], a[j]); + a[i] = select(swap, a[j], a[i]); + a[j] = t; + } + } + + // for (int i = 0; i < N-1; ++i) { + // for (int j = 1; j < N-i; ++j) { + // CiBit swap = a[j-1] > a[j]; + // CiInt t = select(swap, a[j-1], a[j]); + // a[j-1] = select(swap, a[j], a[j-1]); + // a[j] = t; + // } + // } + + for (int i = 0; i < N; ++i) + a[i].write("r_" + to_string(i)); + + CiContext::get_bit_exec_t>()->print(); +} diff --git a/tests/tfhe/wifs_sort/run.sh.in b/tests/tfhe/wifs_sort/run.sh.in new file mode 100644 index 0000000..e1d3760 --- /dev/null +++ b/tests/tfhe/wifs_sort/run.sh.in @@ -0,0 +1,44 @@ +#!/bin/bash + +# +# (C) Copyright 2019 CEA LIST. All Rights Reserved. +# Contributor(s): Cingulata team (formerly Armadillo team) +# +# This software is governed by the CeCILL-C license under French law and +# abiding by the rules of distribution of free software. You can use, +# modify and/ or redistribute the software under the terms of the CeCILL-C +# license as circulated by CEA, CNRS and INRIA at the following URL +# "http://www.cecill.info". +# +# As a counterpart to the access to the source code and rights to copy, +# modify and redistribute granted by the license, users are provided only +# with a limited warranty and the software's author, the holder of the +# economic rights, and the successive licensors have only limited +# liability. +# +# The fact that you are presently reading this means that you have had +# knowledge of the CeCILL-C license and that you accept its terms. +# +# + +APPS_DIR=@APPS_DIR@ +NAME=wifs_sort + +cd `dirname $0` + +rm -f a_* b_* c_* + +echo "Input encryption" + +# Generate keys +echo "TFHE key generation" +$APPS_DIR/tfhe-keygen + +# Encrypt input "a" and "b" +./tfhe-$NAME-encrypt 5 3 1 10 + +echo "Homomorphic execution..." +time ./tfhe-$NAME-exec + +echo "Output decryption" +./tfhe-$NAME-decrypt