Skip to content

Commit

Permalink
Merge pull request #23 from CEA-LIST/wifs_tuto_examples
Browse files Browse the repository at this point in the history
ease tfhe project creation. wifs tutorial examples
  • Loading branch information
ssmiler committed Dec 17, 2019
2 parents 60bfa20 + 7345c14 commit c2e42de
Show file tree
Hide file tree
Showing 22 changed files with 904 additions and 9 deletions.
17 changes: 8 additions & 9 deletions tests/tfhe/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
)
23 changes: 23 additions & 0 deletions tests/tfhe/_template/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 44 additions & 0 deletions tests/tfhe/_template/decrypt.cxx
Original file line number Diff line number Diff line change
@@ -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 <iostream>

/* local includes */
#include <tfhe_bit_exec.hxx>
#include <ci_context.hxx>
#include <ci_int.hxx>


/* 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<TfheBitExec>("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;
}
51 changes: 51 additions & 0 deletions tests/tfhe/_template/encrypt.cxx
Original file line number Diff line number Diff line change
@@ -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 <iostream>

/* local includes */
#include <tfhe_bit_exec.hxx>
#include <ci_context.hxx>
#include <ci_int.hxx>


/* namespaces */
using namespace std;
using namespace cingulata;

int main(int argc, char* argv[]) {
if (argc < 3) {
cerr << "Usage: " << argv[0] << " <a> <b>" << 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<TfheBitExec>("tfhe.sk", TfheBitExec::Secret));

CiInt a{a_val, 8, false};
CiInt b{b_val, 8, false};

a.encrypt().write("a");
b.encrypt().write("b");
}
55 changes: 55 additions & 0 deletions tests/tfhe/_template/main.cxx
Original file line number Diff line number Diff line change
@@ -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 <bit_exec/decorator/attach.hxx>
#include <bit_exec/decorator/stat.hxx>
#include <ci_context.hxx>
#include <ci_int.hxx>
#include <int_op_gen/size.hxx>
#include <tfhe_bit_exec.hxx>

/* 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<decorator::Attach<TfheBitExec, decorator::Stat<IBitExecFHE>>>(
"tfhe.pk", TfheBitExec::Public),
make_shared<IntOpGenSize>());

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<decorator::Stat<IBitExecFHE>>()->print();
}
44 changes: 44 additions & 0 deletions tests/tfhe/_template/run.sh.in
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions tests/tfhe/new_project.sh
Original file line number Diff line number Diff line change
@@ -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 <project name>"
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"
23 changes: 23 additions & 0 deletions tests/tfhe/wifs_cmp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
44 changes: 44 additions & 0 deletions tests/tfhe/wifs_cmp/decrypt.cxx
Original file line number Diff line number Diff line change
@@ -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 <iostream>

/* local includes */
#include <tfhe_bit_exec.hxx>
#include <ci_context.hxx>
#include <ci_int.hxx>


/* 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<TfheBitExec>("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;
}
51 changes: 51 additions & 0 deletions tests/tfhe/wifs_cmp/encrypt.cxx
Original file line number Diff line number Diff line change
@@ -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 <iostream>

/* local includes */
#include <tfhe_bit_exec.hxx>
#include <ci_context.hxx>
#include <ci_int.hxx>


/* namespaces */
using namespace std;
using namespace cingulata;

int main(int argc, char* argv[]) {
if (argc < 3) {
cerr << "Usage: " << argv[0] << " <a> <b>" << 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<TfheBitExec>("tfhe.sk", TfheBitExec::Secret));

CiInt a{a_val, 128, false};
CiInt b{b_val, 128, false};

a.encrypt().write("a");
b.encrypt().write("b");
}

0 comments on commit c2e42de

Please sign in to comment.