├── server_side_sgx ├── .Rhistory ├── host │ ├── src │ │ ├── classifier.h │ │ ├── layer.h │ │ ├── demo.h │ │ ├── list.h │ │ ├── tree.h │ │ ├── box.h │ │ ├── matrix.h │ │ ├── parser.h │ │ ├── col2im.h │ │ ├── im2col.h │ │ ├── l2norm_layer.h │ │ ├── cuda.h │ │ ├── logistic_layer.h │ │ ├── option_list.h │ │ ├── upsample_layer.h │ │ ├── activation_layer.h │ │ ├── region_layer.h │ │ ├── batchnorm_layer.h │ │ ├── shortcut_layer.h │ │ ├── lstm_layer.h │ │ ├── crop_layer.h │ │ ├── iseg_layer.h │ │ ├── reorg_layer.h │ │ ├── route_layer.h │ │ ├── dropout_layer.h │ │ ├── yolo_layer.h │ │ ├── detection_layer.h │ │ ├── cost_layer.h │ │ ├── softmax_layer.h │ │ ├── gru_layer.h │ │ ├── avgpool_layer.h │ │ ├── network.h │ │ ├── rnn_layer.h │ │ ├── maxpool_layer.h │ │ ├── normalization_layer.h │ │ ├── crnn_layer.h │ │ ├── connected_layer.h │ │ ├── deconvolutional_layer.h │ │ ├── gemm.h │ │ ├── local_layer.h │ │ ├── dropout_layer_kernels.cu │ │ ├── col2im.c │ │ ├── im2col.c │ │ ├── list.c │ │ ├── avgpool_layer_kernels.cu │ │ ├── utils.h │ │ ├── dropout_layer.c │ │ ├── activation_layer.c │ │ ├── l2norm_layer.c │ │ ├── avgpool_layer.c │ │ ├── data.h │ │ ├── logistic_layer.c │ │ ├── convolutional_layer.h │ │ ├── im2col_kernels.cu │ │ ├── col2im_kernels.cu │ │ ├── image.h │ │ └── activations.h │ ├── examples │ │ ├── detector.py │ │ ├── art.c │ │ ├── detector-scipy-opencv.py │ │ └── swag.c │ ├── CMakeLists.txt │ └── Makefile ├── .DS_Store ├── .gitattributes ├── cfg │ ├── .DS_Store │ ├── mnist.dataset │ ├── greedy-cnn-layer1.cfg │ ├── greedy-cnn-layer2.cfg │ ├── greedy-cnn-layer3.cfg │ ├── greedy-cnn-aux.cfg │ ├── mnist_greedy-cnn.cfg │ └── mnist_lenet.cfg ├── results │ ├── .DS_Store │ └── mnist │ │ ├── mnist_greedy-cnn.weights │ │ ├── mnist_lenet_pp68.weights │ │ ├── mnist_greedy-cnn.weights_tee │ │ ├── mnist_lenet_pp68.weights_ree │ │ ├── mnist_lenet_pp68.weights_tee │ │ └── mnist_lenet_pp999999.weights ├── enclave │ ├── secure_aggregation.conf │ ├── CMakeLists.txt │ ├── Makefile │ └── aes.h ├── Makefile ├── README.md ├── secure_aggregation.edl ├── LICENSE ├── CMakeLists.txt └── fl_script │ ├── fl_tee_standard_noss.sh │ └── fl_tee_transfer_once.sh ├── client_side_trustzone ├── host │ ├── src │ │ ├── classifier.h │ │ ├── layer.h │ │ ├── demo.h │ │ ├── diffprivate.h │ │ ├── list.h │ │ ├── tcp_transfer.h │ │ ├── tree.h │ │ ├── box.h │ │ ├── matrix.h │ │ ├── col2im.h │ │ ├── im2col.h │ │ ├── parser.h │ │ ├── l2norm_layer.h │ │ ├── cuda.h │ │ ├── logistic_layer.h │ │ ├── option_list.h │ │ ├── upsample_layer.h │ │ ├── activation_layer.h │ │ ├── region_layer.h │ │ ├── batchnorm_layer.h │ │ ├── shortcut_layer.h │ │ ├── lstm_layer.h │ │ ├── crop_layer.h │ │ ├── iseg_layer.h │ │ ├── reorg_layer.h │ │ ├── route_layer.h │ │ ├── dropout_layer.h │ │ ├── yolo_layer.h │ │ ├── detection_layer.h │ │ ├── cost_layer.h │ │ ├── softmax_layer.h │ │ ├── gru_layer.h │ │ ├── avgpool_layer.h │ │ ├── network.h │ │ ├── rnn_layer.h │ │ ├── maxpool_layer.h │ │ ├── normalization_layer.h │ │ ├── crnn_layer.h │ │ ├── connected_layer.h │ │ ├── diffprivate.c │ │ ├── deconvolutional_layer.h │ │ ├── local_layer.h │ │ ├── dropout_layer_kernels.cu │ │ ├── col2im.c │ │ ├── im2col.c │ │ ├── gemm.h │ │ ├── list.c │ │ ├── avgpool_layer_kernels.cu │ │ ├── utils.h │ │ ├── activation_layer.c │ │ ├── l2norm_layer.c │ │ ├── dropout_layer.c │ │ ├── depthwise_convolutional_layer.h │ │ ├── data.h │ │ ├── logistic_layer.c │ │ ├── avgpool_layer.c │ │ ├── convolutional_layer.h │ │ ├── im2col_kernels.cu │ │ ├── col2im_kernels.cu │ │ ├── image.h │ │ └── activations.h │ ├── Makefile_original_bak │ ├── examples │ │ └── art.c │ ├── Makefile_changed_bak │ └── Makefile ├── ta │ ├── Android.mk │ ├── include │ │ ├── diffprivate_TA.h │ │ ├── col2im_TA.h │ │ ├── im2col_TA.h │ │ ├── parser_TA.h │ │ ├── utils_TA.h │ │ ├── avgpool_layer_TA.h │ │ ├── gemm_TA.h │ │ ├── connected_layer_TA.h │ │ ├── maxpool_layer_TA.h │ │ ├── dropout_layer_TA.h │ │ ├── softmax_layer_TA.h │ │ ├── cost_layer_TA.h │ │ ├── batchnorm_layer_TA.h │ │ ├── network_TA.h │ │ ├── math_TA.h │ │ ├── user_ta_header_defines.h │ │ ├── convolutional_layer_TA.h │ │ ├── blas_TA.h │ │ ├── darknetp_ta.h │ │ ├── depthwise_convolutional_layer_TA.h │ │ └── aes_TA.h │ ├── sub.mk │ ├── Makefile │ ├── diffprivate_TA.c │ ├── utils_TA.c │ ├── col2im_TA.c │ ├── im2col_TA.c │ ├── avgpool_layer_TA.c │ ├── softmax_layer_TA.c │ ├── dropout_layer_TA.c │ └── cost_layer_TA.c ├── Makefile ├── Android.mk ├── CMakeLists.txt └── LICENSE ├── ppfl_logo.png ├── .gitattributes ├── LICENSE └── config.mk /server_side_sgx/.Rhistory: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/classifier.h: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/classifier.h: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/layer.h: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/layer.h: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | -------------------------------------------------------------------------------- /ppfl_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/ppfl_logo.png -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /server_side_sgx/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/.DS_Store -------------------------------------------------------------------------------- /server_side_sgx/.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /server_side_sgx/cfg/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/cfg/.DS_Store -------------------------------------------------------------------------------- /server_side_sgx/host/src/demo.h: -------------------------------------------------------------------------------- 1 | #ifndef DEMO_H 2 | #define DEMO_H 3 | 4 | #include "image.h" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /server_side_sgx/results/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/.DS_Store -------------------------------------------------------------------------------- /client_side_trustzone/host/src/demo.h: -------------------------------------------------------------------------------- 1 | #ifndef DEMO_H 2 | #define DEMO_H 3 | 4 | #include "image.h" 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_greedy-cnn.weights: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_greedy-cnn.weights -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_lenet_pp68.weights: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_lenet_pp68.weights -------------------------------------------------------------------------------- /client_side_trustzone/ta/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | local_module := 8aaaf200-2450-11e4-abe2-0002a5d5c51b.ta 4 | include $(BUILD_OPTEE_MK) 5 | -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_greedy-cnn.weights_tee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_greedy-cnn.weights_tee -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_lenet_pp68.weights_ree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_lenet_pp68.weights_ree -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_lenet_pp68.weights_tee: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_lenet_pp68.weights_tee -------------------------------------------------------------------------------- /server_side_sgx/results/mnist/mnist_lenet_pp999999.weights: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mofanv/PPFL/HEAD/server_side_sgx/results/mnist/mnist_lenet_pp999999.weights -------------------------------------------------------------------------------- /client_side_trustzone/host/src/diffprivate.h: -------------------------------------------------------------------------------- 1 | #ifndef DIFF_TA_H 2 | #define DIFF_TA_H 3 | 4 | void diff_private_func(float *input, int len_input); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/diffprivate_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef DIFF_TA_H 2 | #define DIFF_TA_H 3 | 4 | void *diff_private_SGD(float *input, int len_input); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/sub.mk: -------------------------------------------------------------------------------- 1 | global-incdirs-y += include 2 | 3 | srcs-y += $(wildcard *.c) 4 | #srcs-y += darknetp_ta.c 5 | 6 | # To remove a certain compiler flag, add a line like this 7 | #cflags-template_ta.c-y += -Wno-strict-prototypes 8 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/col2im_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_TA_H 2 | #define COL2IM_TA_H 3 | 4 | void col2im_cpu_TA(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | #endif 8 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/im2col_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_TA_H 2 | #define IM2COL_TA_H 3 | 4 | void im2col_cpu_TA(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | #endif 8 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/list.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | #include "darknet.h" 4 | 5 | list *make_list(); 6 | int list_find(list *l, void *val); 7 | 8 | void list_insert(list *, void *); 9 | 10 | 11 | void free_list_contents(list *l); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/list.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | #include "darknet.h" 4 | 5 | list *make_list(); 6 | int list_find(list *l, void *val); 7 | 8 | void list_insert(list *, void *); 9 | 10 | 11 | void free_list_contents(list *l); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/tcp_transfer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int tcp_transfer(char *file_dir, char *op); 10 | -------------------------------------------------------------------------------- /server_side_sgx/enclave/secure_aggregation.conf: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | # Enclave settings: 5 | Debug=1 6 | NumHeapPages=1024 7 | NumStackPages=1024 8 | NumTCS=1 9 | ProductID=1 10 | SecurityVersion=1 11 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include "darknet.h" 4 | 5 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride); 6 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef TREE_H 2 | #define TREE_H 3 | #include "darknet.h" 4 | 5 | int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride); 6 | float get_hierarchy_probability(float *x, tree *hier, int c, int stride); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/parser_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef PAR_TA_H 2 | #define PAR_TA_H 3 | #include "darknet_TA.h" 4 | 5 | void load_weights_TA(float *vec, int length, int layer_i, char type, int transpose); 6 | 7 | void save_weights_TA(float *weights_encrypted, int length, int layer_i, char type); 8 | #endif 9 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/utils_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H_TA 2 | #define UTILS_H_TA 3 | #include 4 | #include "darknet_TA.h" 5 | 6 | float sum_array_TA(float *a, int n); 7 | float rand_uniform_TA(float min, float max); 8 | float rand_normal_TA(float mu, float sigma); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "darknet.h" 4 | 5 | typedef struct{ 6 | float dx, dy, dw, dh; 7 | } dbox; 8 | 9 | float box_rmse(box a, box b); 10 | dbox diou(box a, box b); 11 | box decode_box(box b, box anchor); 12 | box encode_box(box b, box anchor); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "darknet.h" 4 | 5 | typedef struct{ 6 | float dx, dy, dw, dh; 7 | } dbox; 8 | 9 | float box_rmse(box a, box b); 10 | dbox diou(box a, box b); 11 | box decode_box(box b, box anchor); 12 | box encode_box(box b, box anchor); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | #include "darknet.h" 4 | 5 | matrix copy_matrix(matrix m); 6 | void print_matrix(matrix m); 7 | 8 | matrix hold_out_matrix(matrix *m, int n); 9 | matrix resize_matrix(matrix m, int size); 10 | 11 | float *pop_column(matrix *m, int c); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef MATRIX_H 2 | #define MATRIX_H 3 | #include "darknet.h" 4 | 5 | matrix copy_matrix(matrix m); 6 | void print_matrix(matrix m); 7 | 8 | matrix hold_out_matrix(matrix *m, int n); 9 | matrix resize_matrix(matrix m, int size); 10 | 11 | float *pop_column(matrix *m, int c); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | #include "darknet.h" 4 | #include "network.h" 5 | 6 | extern int partition_point1; 7 | extern int partition_point2; 8 | extern int sepa_save_bool; 9 | extern int frozen_bool; 10 | 11 | void save_network(network net, char *filename); 12 | void save_weights_double(network net, char *filename); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /server_side_sgx/cfg/mnist.dataset: -------------------------------------------------------------------------------- 1 | classes=10 2 | train = ../../../Github/darknet/data/mnist/mnist.train.list 3 | valid = ../../../Github/darknet/data/mnist/mnist.valid.list 4 | test = ../../../Github/darknet/data/mnist/mnist.test.list 5 | backup = results/ 6 | labels = ../../../Github/darknet/data/mnist/mnist.labels.list 7 | names = ../../../Github/darknet/data/mnist/mnist.names.list 8 | top = 5 9 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/col2im.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_H 2 | #define COL2IM_H 3 | 4 | void col2im_cpu(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | 8 | #ifdef GPU 9 | void col2im_gpu(float *data_col, 10 | int channels, int height, int width, 11 | int ksize, int stride, int pad, float *data_im); 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/col2im.h: -------------------------------------------------------------------------------- 1 | #ifndef COL2IM_H 2 | #define COL2IM_H 3 | 4 | void col2im_cpu(float* data_col, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_im); 7 | 8 | #ifdef GPU 9 | void col2im_gpu(float *data_col, 10 | int channels, int height, int width, 11 | int ksize, int stride, int pad, float *data_im); 12 | #endif 13 | #endif 14 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/im2col.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_H 2 | #define IM2COL_H 3 | 4 | void im2col_cpu(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | 8 | #ifdef GPU 9 | 10 | void im2col_gpu(float *im, 11 | int channels, int height, int width, 12 | int ksize, int stride, int pad,float *data_col); 13 | 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/im2col.h: -------------------------------------------------------------------------------- 1 | #ifndef IM2COL_H 2 | #define IM2COL_H 3 | 4 | void im2col_cpu(float* data_im, 5 | int channels, int height, int width, 6 | int ksize, int stride, int pad, float* data_col); 7 | 8 | #ifdef GPU 9 | 10 | void im2col_gpu(float *im, 11 | int channels, int height, int width, 12 | int ksize, int stride, int pad,float *data_col); 13 | 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/parser.h: -------------------------------------------------------------------------------- 1 | #ifndef PARSER_H 2 | #define PARSER_H 3 | #include "darknet.h" 4 | #include "network.h" 5 | 6 | extern int partition_point1; 7 | extern int partition_point2; 8 | extern int frozen_bool; 9 | extern int sepa_save_bool; 10 | extern int count_global; 11 | extern int global_dp; 12 | 13 | void save_network(network net, char *filename); 14 | void save_weights_double(network net, char *filename); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /client_side_trustzone/Makefile: -------------------------------------------------------------------------------- 1 | export V?=0 2 | 3 | # If _HOST or _TA specific compilers are not specified, then use CROSS_COMPILE 4 | HOST_CROSS_COMPILE ?= $(CROSS_COMPILE) 5 | TA_CROSS_COMPILE ?= $(CROSS_COMPILE) 6 | 7 | .PHONY: all 8 | all: 9 | $(MAKE) -C host CROSS_COMPILE="$(HOST_CROSS_COMPILE)" --no-builtin-variables 10 | $(MAKE) -C ta CROSS_COMPILE="$(TA_CROSS_COMPILE)" LDFLAGS="" 11 | 12 | .PHONY: clean 13 | clean: 14 | $(MAKE) -C host clean 15 | $(MAKE) -C ta clean 16 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/l2norm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef L2NORM_LAYER_H 2 | #define L2NORM_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_l2norm_layer(int batch, int inputs); 7 | void forward_l2norm_layer(const layer l, network net); 8 | void backward_l2norm_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_l2norm_layer_gpu(const layer l, network net); 12 | void backward_l2norm_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/l2norm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef L2NORM_LAYER_H 2 | #define L2NORM_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_l2norm_layer(int batch, int inputs); 7 | void forward_l2norm_layer(const layer l, network net); 8 | void backward_l2norm_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_l2norm_layer_gpu(const layer l, network net); 12 | void backward_l2norm_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/Makefile: -------------------------------------------------------------------------------- 1 | CFG_TEE_TA_LOG_LEVEL ?= 4 2 | CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL) 3 | 4 | # The UUID for the Trusted Application 5 | BINARY=7fc5c039-0542-4ee1-80af-b4eab2f1998d 6 | 7 | -include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk 8 | 9 | ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), ) 10 | clean: 11 | @echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA' 12 | @echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)' 13 | endif 14 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/cuda.h: -------------------------------------------------------------------------------- 1 | #ifndef CUDA_H 2 | #define CUDA_H 3 | 4 | #include "darknet.h" 5 | 6 | #ifdef GPU 7 | 8 | void check_error(cudaError_t status); 9 | cublasHandle_t blas_handle(); 10 | int *cuda_make_int_array(int *x, size_t n); 11 | void cuda_random(float *x_gpu, size_t n); 12 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s); 13 | dim3 cuda_gridsize(size_t n); 14 | 15 | #ifdef CUDNN 16 | cudnnHandle_t cudnn_handle(); 17 | #endif 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/cuda.h: -------------------------------------------------------------------------------- 1 | #ifndef CUDA_H 2 | #define CUDA_H 3 | 4 | #include "darknet.h" 5 | 6 | #ifdef GPU 7 | 8 | void check_error(cudaError_t status); 9 | cublasHandle_t blas_handle(); 10 | int *cuda_make_int_array(int *x, size_t n); 11 | void cuda_random(float *x_gpu, size_t n); 12 | float cuda_compare(float *x_gpu, float *x, size_t n, char *s); 13 | dim3 cuda_gridsize(size_t n); 14 | 15 | #ifdef CUDNN 16 | cudnnHandle_t cudnn_handle(); 17 | #endif 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/logistic_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGISTIC_LAYER_H 2 | #define LOGISTIC_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_logistic_layer(int batch, int inputs); 7 | void forward_logistic_layer(const layer l, network net); 8 | void backward_logistic_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_logistic_layer_gpu(const layer l, network net); 12 | void backward_logistic_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/avgpool_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "darknet_TA.h" 5 | 6 | typedef layer_TA avgpool_layer_TA; 7 | 8 | avgpool_layer_TA make_avgpool_layer_TA(int batch, int w, int h, int c); 9 | void resize_avgpool_layer_TA(avgpool_layer_TA *l, int w, int h); 10 | void forward_avgpool_layer_TA_new(const avgpool_layer_TA l, network_TA net); 11 | void backward_avgpool_layer_TA_new(const avgpool_layer_TA l, network_TA net); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/logistic_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGISTIC_LAYER_H 2 | #define LOGISTIC_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | layer make_logistic_layer(int batch, int inputs); 7 | void forward_logistic_layer(const layer l, network net); 8 | void backward_logistic_layer(const layer l, network net); 9 | 10 | #ifdef GPU 11 | void forward_logistic_layer_gpu(const layer l, network net); 12 | void backward_logistic_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/gemm_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_TA_H 2 | #define GEMM_TA_H 3 | 4 | void gemm_TA(int TA, int TB, int M, int N, int K, float ALPHA, 5 | float *A, int lda, 6 | float *B, int ldb, 7 | float BETA, 8 | float *C, int ldc); 9 | 10 | void gemm_cpu_TA(int TA, int TB, int M, int N, int K, float ALPHA, 11 | float *A, int lda, 12 | float *B, int ldb, 13 | float BETA, 14 | float *C, int ldc); 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/connected_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_TA_H 2 | #define CONNECTED_LAYER_TA_H 3 | #include "darknet_TA.h" 4 | 5 | void forward_connected_layer_TA_new(layer_TA l, network_TA net); 6 | 7 | void backward_connected_layer_TA_new(layer_TA l, network_TA net); 8 | 9 | void update_connected_layer_TA_new(layer_TA l, update_args_TA a); 10 | 11 | layer_TA make_connected_layer_TA_new(int batch, int inputs, int outputs, ACTIVATION_TA activation, int batch_normalize, int adam); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/option_list.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTION_LIST_H 2 | #define OPTION_LIST_H 3 | #include "list.h" 4 | 5 | typedef struct{ 6 | char *key; 7 | char *val; 8 | int used; 9 | } kvp; 10 | 11 | 12 | int read_option(char *s, list *options); 13 | void option_insert(list *l, char *key, char *val); 14 | char *option_find(list *l, char *key); 15 | float option_find_float(list *l, char *key, float def); 16 | float option_find_float_quiet(list *l, char *key, float def); 17 | void option_unused(list *l); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/option_list.h: -------------------------------------------------------------------------------- 1 | #ifndef OPTION_LIST_H 2 | #define OPTION_LIST_H 3 | #include "list.h" 4 | 5 | typedef struct{ 6 | char *key; 7 | char *val; 8 | int used; 9 | } kvp; 10 | 11 | 12 | int read_option(char *s, list *options); 13 | void option_insert(list *l, char *key, char *val); 14 | char *option_find(list *l, char *key); 15 | float option_find_float(list *l, char *key, float def); 16 | float option_find_float_quiet(list *l, char *key, float def); 17 | void option_unused(list *l); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/maxpool_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "darknet_TA.h" 5 | 6 | typedef layer_TA maxpool_layer_TA; 7 | 8 | maxpool_layer_TA make_maxpool_layer_TA(int batch, int h, int w, int c, int size, int stride, int padding); 9 | void resize_maxpool_layer_TA(maxpool_layer_TA *l, int w, int h); 10 | void forward_maxpool_layer_TA_new(const maxpool_layer_TA l, network_TA net); 11 | void backward_maxpool_layer_TA_new(const maxpool_layer_TA l, network_TA net); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/dropout_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_TA_H 2 | #define DROPOUT_LAYER_TA_H 3 | 4 | #include "darknet_TA.h" 5 | 6 | typedef layer_TA dropout_layer_TA; 7 | 8 | dropout_layer_TA make_dropout_layer_TA_new(int batch, int inputs, float probability, int w, int h, int c, int netnum); 9 | 10 | void forward_dropout_layer_TA_new(dropout_layer_TA l, network_TA net); 11 | void backward_dropout_layer_TA_new(dropout_layer_TA l, network_TA net); 12 | void resize_dropout_layer_TA(dropout_layer_TA *l, int inputs); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/upsample_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef UPSAMPLE_LAYER_H 2 | #define UPSAMPLE_LAYER_H 3 | #include "darknet.h" 4 | 5 | layer make_upsample_layer(int batch, int w, int h, int c, int stride); 6 | void forward_upsample_layer(const layer l, network net); 7 | void backward_upsample_layer(const layer l, network net); 8 | void resize_upsample_layer(layer *l, int w, int h); 9 | 10 | #ifdef GPU 11 | void forward_upsample_layer_gpu(const layer l, network net); 12 | void backward_upsample_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/upsample_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef UPSAMPLE_LAYER_H 2 | #define UPSAMPLE_LAYER_H 3 | #include "darknet.h" 4 | 5 | layer make_upsample_layer(int batch, int w, int h, int c, int stride); 6 | void forward_upsample_layer(const layer l, network net); 7 | void backward_upsample_layer(const layer l, network net); 8 | void resize_upsample_layer(layer *l, int w, int h); 9 | 10 | #ifdef GPU 11 | void forward_upsample_layer_gpu(const layer l, network net); 12 | void backward_upsample_layer_gpu(const layer l, network net); 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /server_side_sgx/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | .PHONY: all build clean run simulate 5 | 6 | OE_CRYPTO_LIB := mbedtls 7 | export OE_CRYPTO_LIB 8 | 9 | all: build 10 | 11 | build: 12 | $(MAKE) -C enclave 13 | $(MAKE) -C host 14 | 15 | clean: 16 | $(MAKE) -C enclave clean 17 | $(MAKE) -C host clean 18 | 19 | run: 20 | host/secure_aggregation_host ./enclave/secure_aggregation_enc.signed 21 | 22 | simulate: 23 | host/secure_aggregation_host ./enclave/secure_aggregation_enc.signed --simulate 24 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/activation_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATION_LAYER_H 2 | #define ACTIVATION_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation); 9 | 10 | void forward_activation_layer(layer l, network net); 11 | void backward_activation_layer(layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_activation_layer_gpu(layer l, network net); 15 | void backward_activation_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/activation_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATION_LAYER_H 2 | #define ACTIVATION_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation); 9 | 10 | void forward_activation_layer(layer l, network net); 11 | void backward_activation_layer(layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_activation_layer_gpu(layer l, network net); 15 | void backward_activation_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/softmax_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_TA_H 2 | #define SOFTMAX_LAYER_TA_H 3 | #include "darknet_TA.h" 4 | 5 | typedef layer_TA softmax_layer_TA; 6 | 7 | void softmax_array_TA(float *input, int n, float temp, float *output); 8 | softmax_layer_TA make_softmax_layer_TA_new(int batch, int inputs, int groups, float temperature, int w, int h, int c, int spatial, int noloss); 9 | void forward_softmax_layer_TA(const softmax_layer_TA l, network_TA net); 10 | void backward_softmax_layer_TA(const softmax_layer_TA l, network_TA net); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/cost_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_TA_H 2 | #define COST_LAYER_TA_H 3 | 4 | #include "darknet_TA.h" 5 | 6 | typedef layer_TA cost_layer_TA; 7 | 8 | COST_TYPE_TA get_cost_type_TA(char *s); 9 | 10 | char *get_cost_string_TA(COST_TYPE_TA a); 11 | 12 | cost_layer_TA make_cost_layer_TA_new(int batch, int inputs, COST_TYPE_TA cost_type, float scale, float ratio, float noobject_scale, float thresh); 13 | 14 | void forward_cost_layer_TA(cost_layer_TA l, network_TA net); 15 | 16 | void backward_cost_layer_TA(const cost_layer_TA l, network_TA net); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REGION_LAYER_H 2 | #define REGION_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_region_layer(int batch, int w, int h, int n, int classes, int coords); 9 | void forward_region_layer(const layer l, network net); 10 | void backward_region_layer(const layer l, network net); 11 | void resize_region_layer(layer *l, int w, int h); 12 | 13 | #ifdef GPU 14 | void forward_region_layer_gpu(const layer l, network net); 15 | void backward_region_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REGION_LAYER_H 2 | #define REGION_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_region_layer(int batch, int w, int h, int n, int classes, int coords); 9 | void forward_region_layer(const layer l, network net); 10 | void backward_region_layer(const layer l, network net); 11 | void resize_region_layer(layer *l, int w, int h); 12 | 13 | #ifdef GPU 14 | void forward_region_layer_gpu(const layer l, network net); 15 | void backward_region_layer_gpu(layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/batchnorm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_H 2 | #define BATCHNORM_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_batchnorm_layer(int batch, int w, int h, int c); 9 | void forward_batchnorm_layer(layer l, network net); 10 | void backward_batchnorm_layer(layer l, network net); 11 | 12 | #ifdef GPU 13 | void forward_batchnorm_layer_gpu(layer l, network net); 14 | void backward_batchnorm_layer_gpu(layer l, network net); 15 | void pull_batchnorm_layer(layer l); 16 | void push_batchnorm_layer(layer l); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /server_side_sgx/README.md: -------------------------------------------------------------------------------- 1 | # Secure aggregation using SGX 2 | 3 | This is an application providing secure aggregation, on a server orchestrating federated learning, using SGX based on [OpenEnclave SDK](https://github.com/openenclave/openenclave). 4 | 5 | ### More info: 6 | 1) The data transmission between server and clients is using `scp`. 7 | 2) We simplify the communication and agreement between server and client by `ssh` from the server to the client directly. 8 | 3) Weights of layers being run in TEEs are encrypted/decrypted by hand-coded symmetric key (AES-CBC (128bit)) in both server SGX and client TrustZone. 9 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/shortcut_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SHORTCUT_LAYER_H 2 | #define SHORTCUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2); 8 | void forward_shortcut_layer(const layer l, network net); 9 | void backward_shortcut_layer(const layer l, network net); 10 | void resize_shortcut_layer(layer *l, int w, int h); 11 | 12 | #ifdef GPU 13 | void forward_shortcut_layer_gpu(const layer l, network net); 14 | void backward_shortcut_layer_gpu(const layer l, network net); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/batchnorm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_H 2 | #define BATCHNORM_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_batchnorm_layer(int batch, int w, int h, int c); 9 | void forward_batchnorm_layer(layer l, network net); 10 | void backward_batchnorm_layer(layer l, network net); 11 | 12 | #ifdef GPU 13 | void forward_batchnorm_layer_gpu(layer l, network net); 14 | void backward_batchnorm_layer_gpu(layer l, network net); 15 | void pull_batchnorm_layer(layer l); 16 | void push_batchnorm_layer(layer l); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/shortcut_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SHORTCUT_LAYER_H 2 | #define SHORTCUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | layer make_shortcut_layer(int batch, int index, int w, int h, int c, int w2, int h2, int c2); 8 | void forward_shortcut_layer(const layer l, network net); 9 | void backward_shortcut_layer(const layer l, network net); 10 | void resize_shortcut_layer(layer *l, int w, int h); 11 | 12 | #ifdef GPU 13 | void forward_shortcut_layer_gpu(const layer l, network net); 14 | void backward_shortcut_layer_gpu(const layer l, network net); 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/lstm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LSTM_LAYER_H 2 | #define LSTM_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | #define USET 8 | 9 | layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_lstm_layer(layer l, network net); 12 | void update_lstm_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_lstm_layer_gpu(layer l, network net); 16 | void backward_lstm_layer_gpu(layer l, network net); 17 | void update_lstm_layer_gpu(layer l, update_args a); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/lstm_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LSTM_LAYER_H 2 | #define LSTM_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | #define USET 8 | 9 | layer make_lstm_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_lstm_layer(layer l, network net); 12 | void update_lstm_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_lstm_layer_gpu(layer l, network net); 16 | void backward_lstm_layer_gpu(layer l, network net); 17 | void update_lstm_layer_gpu(layer l, update_args a); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/crop_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CROP_LAYER_H 2 | #define CROP_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | typedef layer crop_layer; 9 | 10 | image get_crop_image(crop_layer l); 11 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure); 12 | void forward_crop_layer(const crop_layer l, network net); 13 | void resize_crop_layer(layer *l, int w, int h); 14 | 15 | #ifdef GPU 16 | void forward_crop_layer_gpu(crop_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/iseg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ISEG_LAYER_H 2 | #define ISEG_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_iseg_layer(int batch, int w, int h, int classes, int ids); 9 | void forward_iseg_layer(const layer l, network net); 10 | void backward_iseg_layer(const layer l, network net); 11 | void resize_iseg_layer(layer *l, int w, int h); 12 | int iseg_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_iseg_layer_gpu(const layer l, network net); 16 | void backward_iseg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/crop_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CROP_LAYER_H 2 | #define CROP_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | typedef layer crop_layer; 9 | 10 | image get_crop_image(crop_layer l); 11 | crop_layer make_crop_layer(int batch, int h, int w, int c, int crop_height, int crop_width, int flip, float angle, float saturation, float exposure); 12 | void forward_crop_layer(const crop_layer l, network net); 13 | void resize_crop_layer(layer *l, int w, int h); 14 | 15 | #ifdef GPU 16 | void forward_crop_layer_gpu(crop_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/iseg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ISEG_LAYER_H 2 | #define ISEG_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_iseg_layer(int batch, int w, int h, int classes, int ids); 9 | void forward_iseg_layer(const layer l, network net); 10 | void backward_iseg_layer(const layer l, network net); 11 | void resize_iseg_layer(layer *l, int w, int h); 12 | int iseg_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_iseg_layer_gpu(const layer l, network net); 16 | void backward_iseg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/reorg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REORG_LAYER_H 2 | #define REORG_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra); 10 | void resize_reorg_layer(layer *l, int w, int h); 11 | void forward_reorg_layer(const layer l, network net); 12 | void backward_reorg_layer(const layer l, network net); 13 | 14 | #ifdef GPU 15 | void forward_reorg_layer_gpu(layer l, network net); 16 | void backward_reorg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/route_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUTE_LAYER_H 2 | #define ROUTE_LAYER_H 3 | #include "network.h" 4 | #include "layer.h" 5 | 6 | typedef layer route_layer; 7 | 8 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_size); 9 | void forward_route_layer(const route_layer l, network net); 10 | void backward_route_layer(const route_layer l, network net); 11 | void resize_route_layer(route_layer *l, network *net); 12 | 13 | #ifdef GPU 14 | void forward_route_layer_gpu(const route_layer l, network net); 15 | void backward_route_layer_gpu(const route_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/reorg_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef REORG_LAYER_H 2 | #define REORG_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra); 10 | void resize_reorg_layer(layer *l, int w, int h); 11 | void forward_reorg_layer(const layer l, network net); 12 | void backward_reorg_layer(const layer l, network net); 13 | 14 | #ifdef GPU 15 | void forward_reorg_layer_gpu(layer l, network net); 16 | void backward_reorg_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/route_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef ROUTE_LAYER_H 2 | #define ROUTE_LAYER_H 3 | #include "network.h" 4 | #include "layer.h" 5 | 6 | typedef layer route_layer; 7 | 8 | route_layer make_route_layer(int batch, int n, int *input_layers, int *input_size); 9 | void forward_route_layer(const route_layer l, network net); 10 | void backward_route_layer(const route_layer l, network net); 11 | void resize_route_layer(route_layer *l, network *net); 12 | 13 | #ifdef GPU 14 | void forward_route_layer_gpu(const route_layer l, network net); 15 | void backward_route_layer_gpu(const route_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/dropout_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_H 2 | #define DROPOUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer dropout_layer; 8 | 9 | dropout_layer make_dropout_layer(int batch, int inputs, float probability); 10 | 11 | void forward_dropout_layer(dropout_layer l, network net); 12 | void backward_dropout_layer(dropout_layer l, network net); 13 | void resize_dropout_layer(dropout_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_dropout_layer_gpu(dropout_layer l, network net); 17 | void backward_dropout_layer_gpu(dropout_layer l, network net); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/yolo_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef YOLO_LAYER_H 2 | #define YOLO_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes); 9 | void forward_yolo_layer(const layer l, network net); 10 | void backward_yolo_layer(const layer l, network net); 11 | void resize_yolo_layer(layer *l, int w, int h); 12 | int yolo_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_yolo_layer_gpu(const layer l, network net); 16 | void backward_yolo_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/dropout_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_LAYER_H 2 | #define DROPOUT_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer dropout_layer; 8 | 9 | dropout_layer make_dropout_layer(int batch, int inputs, float probability); 10 | 11 | void forward_dropout_layer(dropout_layer l, network net); 12 | void backward_dropout_layer(dropout_layer l, network net); 13 | void resize_dropout_layer(dropout_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_dropout_layer_gpu(dropout_layer l, network net); 17 | void backward_dropout_layer_gpu(dropout_layer l, network net); 18 | 19 | #endif 20 | #endif 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/yolo_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef YOLO_LAYER_H 2 | #define YOLO_LAYER_H 3 | 4 | #include "darknet.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes); 9 | void forward_yolo_layer(const layer l, network net); 10 | void backward_yolo_layer(const layer l, network net); 11 | void resize_yolo_layer(layer *l, int w, int h); 12 | int yolo_num_detections(layer l, float thresh); 13 | 14 | #ifdef GPU 15 | void forward_yolo_layer_gpu(const layer l, network net); 16 | void backward_yolo_layer_gpu(layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/detection_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DETECTION_LAYER_H 2 | #define DETECTION_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer detection_layer; 8 | 9 | detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore); 10 | void forward_detection_layer(const detection_layer l, network net); 11 | void backward_detection_layer(const detection_layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_detection_layer_gpu(const detection_layer l, network net); 15 | void backward_detection_layer_gpu(detection_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /client_side_trustzone/Android.mk: -------------------------------------------------------------------------------- 1 | ###################### optee-my-test ###################### 2 | LOCAL_PATH := $(call my-dir) 3 | 4 | OPTEE_CLIENT_EXPORT = $(LOCAL_PATH)/../../optee_client/out/export 5 | 6 | include $(CLEAR_VARS) 7 | LOCAL_CFLAGS += -DANDROID_BUILD 8 | LOCAL_CFLAGS += -Wall 9 | 10 | LOCAL_SRC_FILES += host/main.c 11 | 12 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/ta/include \ 13 | $(OPTEE_CLIENT_EXPORT)/include \ 14 | 15 | LOCAL_SHARED_LIBRARIES := libteec 16 | LOCAL_MODULE := optee_example_darknetp 17 | LOCAL_VENDOR_MODULE := true 18 | LOCAL_MODULE_TAGS := optional 19 | include $(BUILD_EXECUTABLE) 20 | 21 | include $(LOCAL_PATH)/ta/Android.mk 22 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/detection_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DETECTION_LAYER_H 2 | #define DETECTION_LAYER_H 3 | 4 | #include "layer.h" 5 | #include "network.h" 6 | 7 | typedef layer detection_layer; 8 | 9 | detection_layer make_detection_layer(int batch, int inputs, int n, int size, int classes, int coords, int rescore); 10 | void forward_detection_layer(const detection_layer l, network net); 11 | void backward_detection_layer(const detection_layer l, network net); 12 | 13 | #ifdef GPU 14 | void forward_detection_layer_gpu(const detection_layer l, network net); 15 | void backward_detection_layer_gpu(detection_layer l, network net); 16 | #endif 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/cost_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_H 2 | #define COST_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer cost_layer; 7 | 8 | COST_TYPE get_cost_type(char *s); 9 | char *get_cost_string(COST_TYPE a); 10 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); 11 | void forward_cost_layer(const cost_layer l, network net); 12 | void backward_cost_layer(const cost_layer l, network net); 13 | void resize_cost_layer(cost_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_cost_layer_gpu(cost_layer l, network net); 17 | void backward_cost_layer_gpu(const cost_layer l, network net); 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/cost_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef COST_LAYER_H 2 | #define COST_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer cost_layer; 7 | 8 | COST_TYPE get_cost_type(char *s); 9 | char *get_cost_string(COST_TYPE a); 10 | cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale); 11 | void forward_cost_layer(const cost_layer l, network net); 12 | void backward_cost_layer(const cost_layer l, network net); 13 | void resize_cost_layer(cost_layer *l, int inputs); 14 | 15 | #ifdef GPU 16 | void forward_cost_layer_gpu(cost_layer l, network net); 17 | void backward_cost_layer_gpu(const cost_layer l, network net); 18 | #endif 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /server_side_sgx/cfg/greedy-cnn-layer1.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=32 5 | width=32 6 | channels=3 7 | momentum=0.9 8 | decay=0.00005 9 | max_crop=28 10 | 11 | learning_rate=0.01 12 | policy=poly 13 | power=4 14 | max_batches=10 15 | 16 | angle=1 17 | hue=1 18 | saturation=1 19 | exposure=1 20 | aspect=1 21 | 22 | 23 | # layer 1 24 | [convolutional] 25 | filters=2 26 | size=3 27 | stride=1 28 | pad=1 29 | activation=relu 30 | 31 | 32 | # auxiliary net 33 | [convolutional] 34 | filters=2 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=relu 39 | 40 | [connected] 41 | output=10 42 | activation=linear 43 | 44 | [softmax] 45 | groups=1 46 | 47 | [cost] 48 | type=sse 49 | -------------------------------------------------------------------------------- /client_side_trustzone/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project (darknetp C) 2 | 3 | set(THREADS_PREFER_PTHREAD_FLAG ON) 4 | 5 | set( THREADS_PTHREAD_ARG 6 | "0" 7 | CACHE STRING "Result from TRY_RUN" FORCE) 8 | 9 | find_package(Threads REQUIRED) 10 | 11 | FILE(GLOB SRC host/src/*.c) 12 | FILE(GLOB EXAM host/examples/*.c) 13 | set(MAIN host/main.c) 14 | 15 | add_executable (${PROJECT_NAME} ${SRC} ${EXAM} ${MAIN}) 16 | 17 | target_include_directories(${PROJECT_NAME} 18 | PRIVATE host/include 19 | PRIVATE host/src 20 | PRIVATE include) 21 | 22 | target_link_libraries (${PROJECT_NAME} PRIVATE teec -lm Threads::Threads) 23 | 24 | install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) 25 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_H 2 | #define SOFTMAX_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer softmax_layer; 7 | 8 | void softmax_array(float *input, int n, float temp, float *output); 9 | softmax_layer make_softmax_layer(int batch, int inputs, int groups); 10 | void forward_softmax_layer(const softmax_layer l, network net); 11 | void backward_softmax_layer(const softmax_layer l, network net); 12 | 13 | #ifdef GPU 14 | void pull_softmax_layer_output(const softmax_layer l); 15 | void forward_softmax_layer_gpu(const softmax_layer l, network net); 16 | void backward_softmax_layer_gpu(const softmax_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/softmax_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_LAYER_H 2 | #define SOFTMAX_LAYER_H 3 | #include "layer.h" 4 | #include "network.h" 5 | 6 | typedef layer softmax_layer; 7 | 8 | void softmax_array(float *input, int n, float temp, float *output); 9 | softmax_layer make_softmax_layer(int batch, int inputs, int groups); 10 | void forward_softmax_layer(const softmax_layer l, network net); 11 | void backward_softmax_layer(const softmax_layer l, network net); 12 | 13 | #ifdef GPU 14 | void pull_softmax_layer_output(const softmax_layer l); 15 | void forward_softmax_layer_gpu(const softmax_layer l, network net); 16 | void backward_softmax_layer_gpu(const softmax_layer l, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/gru_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef GRU_LAYER_H 3 | #define GRU_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_gru_layer(layer l, network state); 12 | void backward_gru_layer(layer l, network state); 13 | void update_gru_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_gru_layer_gpu(layer l, network state); 17 | void backward_gru_layer_gpu(layer l, network state); 18 | void update_gru_layer_gpu(layer l, update_args a); 19 | void push_gru_layer(layer l); 20 | void pull_gru_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/gru_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef GRU_LAYER_H 3 | #define GRU_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_gru_layer(int batch, int inputs, int outputs, int steps, int batch_normalize, int adam); 10 | 11 | void forward_gru_layer(layer l, network state); 12 | void backward_gru_layer(layer l, network state); 13 | void update_gru_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_gru_layer_gpu(layer l, network state); 17 | void backward_gru_layer_gpu(layer l, network state); 18 | void update_gru_layer_gpu(layer l, update_args a); 19 | void push_gru_layer(layer l); 20 | void pull_gru_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/avgpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer avgpool_layer; 10 | 11 | image get_avgpool_image(avgpool_layer l); 12 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c); 13 | void resize_avgpool_layer(avgpool_layer *l, int w, int h); 14 | void forward_avgpool_layer(const avgpool_layer l, network net); 15 | void backward_avgpool_layer(const avgpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_avgpool_layer_gpu(avgpool_layer l, network net); 19 | void backward_avgpool_layer_gpu(avgpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/avgpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef AVGPOOL_LAYER_H 2 | #define AVGPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer avgpool_layer; 10 | 11 | image get_avgpool_image(avgpool_layer l); 12 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c); 13 | void resize_avgpool_layer(avgpool_layer *l, int w, int h); 14 | void forward_avgpool_layer(const avgpool_layer l, network net); 15 | void backward_avgpool_layer(const avgpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_avgpool_layer_gpu(avgpool_layer l, network net); 19 | void backward_avgpool_layer_gpu(avgpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/network.h: -------------------------------------------------------------------------------- 1 | // Oh boy, why am I about to do this.... 2 | #ifndef NETWORK_H 3 | #define NETWORK_H 4 | #include "darknet.h" 5 | 6 | #include "image.h" 7 | #include "layer.h" 8 | #include "data.h" 9 | #include "tree.h" 10 | 11 | 12 | #ifdef GPU 13 | void pull_network_output(network *net); 14 | #endif 15 | 16 | void compare_networks(network *n1, network *n2, data d); 17 | char *get_layer_string(LAYER_TYPE a); 18 | 19 | network *make_network(int n); 20 | 21 | 22 | float network_accuracy_multi(network *net, data d, int n); 23 | int get_predicted_class_network(network *net); 24 | void print_network(network *net); 25 | int resize_network(network *net, int w, int h); 26 | void calc_network_cost(network *net); 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /client_side_trustzone/host/Makefile_original_bak: -------------------------------------------------------------------------------- 1 | CC ?= $(CROSS_COMPILE)gcc 2 | LD ?= $(CROSS_COMPILE)ld 3 | AR ?= $(CROSS_COMPILE)ar 4 | NM ?= $(CROSS_COMPILE)nm 5 | OBJCOPY ?= $(CROSS_COMPILE)objcopy 6 | OBJDUMP ?= $(CROSS_COMPILE)objdump 7 | READELF ?= $(CROSS_COMPILE)readelf 8 | 9 | OBJS = main.o 10 | 11 | CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include 12 | #Add/link other required libraries here 13 | LDADD += -lteec -L$(TEEC_EXPORT)/lib 14 | 15 | BINARY = optee_example_darknetp 16 | 17 | .PHONY: all 18 | all: $(BINARY) 19 | 20 | $(BINARY): $(OBJS) 21 | $(CC) -o -lm -pthread $@ $< $(LDADD) 22 | 23 | .PHONY: clean 24 | clean: 25 | rm -f $(OBJS) $(BINARY) 26 | 27 | %.o: %.c 28 | $(CC) $(CFLAGS) -c $< -o $@ 29 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/network.h: -------------------------------------------------------------------------------- 1 | // Oh boy, why am I about to do this.... 2 | #ifndef NETWORK_H 3 | #define NETWORK_H 4 | #include "darknet.h" 5 | 6 | #include "image.h" 7 | #include "layer.h" 8 | #include "data.h" 9 | #include "tree.h" 10 | 11 | 12 | #ifdef GPU 13 | void pull_network_output(network *net); 14 | #endif 15 | 16 | void compare_networks(network *n1, network *n2, data d); 17 | char *get_layer_string(LAYER_TYPE a); 18 | 19 | network *make_network(int n); 20 | 21 | 22 | float network_accuracy_multi(network *net, data d, int n); 23 | int get_predicted_class_network(network *net); 24 | void print_network(network *net); 25 | int resize_network(network *net, int w, int h); 26 | void calc_network_cost(network *net); 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/rnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RNN_LAYER_H 3 | #define RNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | #define USET 9 | 10 | layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam); 11 | 12 | void forward_rnn_layer(layer l, network net); 13 | void backward_rnn_layer(layer l, network net); 14 | void update_rnn_layer(layer l, update_args a); 15 | 16 | #ifdef GPU 17 | void forward_rnn_layer_gpu(layer l, network net); 18 | void backward_rnn_layer_gpu(layer l, network net); 19 | void update_rnn_layer_gpu(layer l, update_args a); 20 | void push_rnn_layer(layer l); 21 | void pull_rnn_layer(layer l); 22 | #endif 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/rnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RNN_LAYER_H 3 | #define RNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | #define USET 9 | 10 | layer make_rnn_layer(int batch, int inputs, int outputs, int steps, ACTIVATION activation, int batch_normalize, int adam); 11 | 12 | void forward_rnn_layer(layer l, network net); 13 | void backward_rnn_layer(layer l, network net); 14 | void update_rnn_layer(layer l, update_args a); 15 | 16 | #ifdef GPU 17 | void forward_rnn_layer_gpu(layer l, network net); 18 | void backward_rnn_layer_gpu(layer l, network net); 19 | void update_rnn_layer_gpu(layer l, update_args a); 20 | void push_rnn_layer(layer l); 21 | void pull_rnn_layer(layer l); 22 | #endif 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer maxpool_layer; 10 | 11 | image get_maxpool_image(maxpool_layer l); 12 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding); 13 | void resize_maxpool_layer(maxpool_layer *l, int w, int h); 14 | void forward_maxpool_layer(const maxpool_layer l, network net); 15 | void backward_maxpool_layer(const maxpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_maxpool_layer_gpu(maxpool_layer l, network net); 19 | void backward_maxpool_layer_gpu(maxpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/maxpool_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOL_LAYER_H 2 | #define MAXPOOL_LAYER_H 3 | 4 | #include "image.h" 5 | #include "cuda.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | typedef layer maxpool_layer; 10 | 11 | image get_maxpool_image(maxpool_layer l); 12 | maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding); 13 | void resize_maxpool_layer(maxpool_layer *l, int w, int h); 14 | void forward_maxpool_layer(const maxpool_layer l, network net); 15 | void backward_maxpool_layer(const maxpool_layer l, network net); 16 | 17 | #ifdef GPU 18 | void forward_maxpool_layer_gpu(maxpool_layer l, network net); 19 | void backward_maxpool_layer_gpu(maxpool_layer l, network net); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/normalization_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_LAYER_H 2 | #define NORMALIZATION_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa); 9 | void resize_normalization_layer(layer *layer, int h, int w); 10 | void forward_normalization_layer(const layer layer, network net); 11 | void backward_normalization_layer(const layer layer, network net); 12 | void visualize_normalization_layer(layer layer, char *window); 13 | 14 | #ifdef GPU 15 | void forward_normalization_layer_gpu(const layer layer, network net); 16 | void backward_normalization_layer_gpu(const layer layer, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/normalization_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_LAYER_H 2 | #define NORMALIZATION_LAYER_H 3 | 4 | #include "image.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa); 9 | void resize_normalization_layer(layer *layer, int h, int w); 10 | void forward_normalization_layer(const layer layer, network net); 11 | void backward_normalization_layer(const layer layer, network net); 12 | void visualize_normalization_layer(layer layer, char *window); 13 | 14 | #ifdef GPU 15 | void forward_normalization_layer_gpu(const layer layer, network net); 16 | void backward_normalization_layer_gpu(const layer layer, network net); 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/crnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRNN_LAYER_H 3 | #define CRNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize); 10 | 11 | void forward_crnn_layer(layer l, network net); 12 | void backward_crnn_layer(layer l, network net); 13 | void update_crnn_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_crnn_layer_gpu(layer l, network net); 17 | void backward_crnn_layer_gpu(layer l, network net); 18 | void update_crnn_layer_gpu(layer l, update_args a); 19 | void push_crnn_layer(layer l); 20 | void pull_crnn_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/crnn_layer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef CRNN_LAYER_H 3 | #define CRNN_LAYER_H 4 | 5 | #include "activations.h" 6 | #include "layer.h" 7 | #include "network.h" 8 | 9 | layer make_crnn_layer(int batch, int h, int w, int c, int hidden_filters, int output_filters, int steps, ACTIVATION activation, int batch_normalize); 10 | 11 | void forward_crnn_layer(layer l, network net); 12 | void backward_crnn_layer(layer l, network net); 13 | void update_crnn_layer(layer l, update_args a); 14 | 15 | #ifdef GPU 16 | void forward_crnn_layer_gpu(layer l, network net); 17 | void backward_crnn_layer_gpu(layer l, network net); 18 | void update_crnn_layer_gpu(layer l, update_args a); 19 | void push_crnn_layer(layer l); 20 | void pull_crnn_layer(layer l); 21 | #endif 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/connected_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_H 2 | #define CONNECTED_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam); 9 | 10 | void forward_connected_layer(layer l, network net); 11 | void backward_connected_layer(layer l, network net); 12 | void update_connected_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_connected_layer_gpu(layer l, network net); 16 | void backward_connected_layer_gpu(layer l, network net); 17 | void update_connected_layer_gpu(layer l, update_args a); 18 | void push_connected_layer(layer l); 19 | void pull_connected_layer(layer l); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/connected_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONNECTED_LAYER_H 2 | #define CONNECTED_LAYER_H 3 | 4 | #include "activations.h" 5 | #include "layer.h" 6 | #include "network.h" 7 | 8 | layer make_connected_layer(int batch, int inputs, int outputs, ACTIVATION activation, int batch_normalize, int adam); 9 | 10 | void forward_connected_layer(layer l, network net); 11 | void backward_connected_layer(layer l, network net); 12 | void update_connected_layer(layer l, update_args a); 13 | 14 | #ifdef GPU 15 | void forward_connected_layer_gpu(layer l, network net); 16 | void backward_connected_layer_gpu(layer l, network net); 17 | void update_connected_layer_gpu(layer l, update_args a); 18 | void push_connected_layer(layer l); 19 | void pull_connected_layer(layer l); 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/batchnorm_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef BATCHNORM_LAYER_TA_H 2 | #define BATCHNORM_LAYER_TA_H 3 | 4 | void forward_batchnorm_layer_TA(layer_TA l, network_TA net); 5 | 6 | void backward_scale_cpu_TA(float *x_norm, float *delta, int batch, int n, int size, float *scale_updates); 7 | 8 | void mean_delta_cpu_TA(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta); 9 | 10 | void variance_delta_cpu_TA(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta); 11 | 12 | void normalize_delta_cpu_TA(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta); 13 | 14 | void backward_batchnorm_layer_TA(layer_TA l, network_TA net); 15 | #endif 16 | -------------------------------------------------------------------------------- /server_side_sgx/cfg/greedy-cnn-layer2.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | batch=1 3 | subdivisions=1 4 | height=32 5 | width=32 6 | channels=3 7 | momentum=0.9 8 | decay=0.00005 9 | max_crop=28 10 | 11 | learning_rate=0.01 12 | policy=poly 13 | power=4 14 | max_batches=10 15 | 16 | angle=1 17 | hue=1 18 | saturation=1 19 | exposure=1 20 | aspect=1 21 | 22 | 23 | # layer 1 24 | [convolutional] 25 | filters=2 26 | size=3 27 | stride=1 28 | pad=1 29 | activation=relu 30 | stopbackward=1 31 | 32 | # layer 2 33 | [convolutional] 34 | filters=2 35 | size=3 36 | stride=1 37 | pad=1 38 | activation=relu 39 | 40 | # auxiliary net 41 | [convolutional] 42 | filters=2 43 | size=3 44 | stride=1 45 | pad=1 46 | activation=relu 47 | 48 | [connected] 49 | output=10 50 | activation=linear 51 | 52 | [softmax] 53 | groups=1 54 | 55 | [cost] 56 | type=sse 57 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/network_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef NETWORK_TA_H 2 | #define NETWORK_TA_H 3 | 4 | extern network_TA netta; 5 | extern float *ta_net_input; 6 | extern float *ta_net_delta; 7 | extern float *ta_net_output; 8 | 9 | void make_network_TA(int n, float learning_rate, float momentum, float decay, int time_steps, int notruth, int batch, int subdivisions, int random, int adam, float B1, float B2, float eps, int h, int w, int c, int inputs, int max_crop, int min_crop, float max_ratio, float min_ratio, int center, float clip, float angle, float aspect, float saturation, float exposure, float hue, int burn_in, float power, int max_batches); 10 | 11 | void calc_network_cost_TA(); 12 | 13 | void calc_network_loss_TA(int n, int batch); 14 | 15 | void forward_network_TA(); 16 | 17 | void backward_network_TA(float *ca_net_input); 18 | 19 | void update_network_TA(update_args_TA a); 20 | #endif 21 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/diffprivate_TA.c: -------------------------------------------------------------------------------- 1 | #include "math_TA.h" 2 | #include "utils_TA.h" 3 | #include "diffprivate_TA.h" 4 | 5 | void *diff_private_SGD(float *input, int len_input) 6 | { 7 | //initial parameters 8 | float bound = 4; 9 | float epsilon = 4; 10 | float delta = 0.00001; 11 | 12 | float sum = 0; 13 | for(int i=0; i0?(a):(-(a))) 12 | 13 | float ta_max(float a, float b); 14 | double ta_pow(double a,int n); 15 | double ta_eee(double x); 16 | double ta_exp(double x); 17 | float ta_rand(); 18 | int ta_floor(double x); 19 | double ta_sqrt(double x); 20 | double ta_ln(double x); 21 | double ta_log(double a,double N); 22 | double ta_sin(double x); 23 | double ta_cos(double x); 24 | double ta_tan(double x); 25 | 26 | void reverse(char *str, int len); 27 | int intToStr(int x, char str[], int d); 28 | void ftoa(float n, char *res, int afterpoint); 29 | void bubble_sort_top(float *arr, int len); 30 | 31 | #endif /*TA_MY_TEST_H_MATH*/ 32 | -------------------------------------------------------------------------------- /server_side_sgx/secure_aggregation.edl: -------------------------------------------------------------------------------- 1 | // Copyright (c) Open Enclave SDK contributors. 2 | // Licensed under the MIT License. 3 | 4 | enclave { 5 | from "openenclave/edl/syscall.edl" import *; 6 | from "platform.edl" import *; 7 | 8 | trusted { 9 | public void enclave_initialize( 10 | [in, count=buf_size] float* input_buf, 11 | size_t buf_size); 12 | 13 | public void enclave_secure_aggregation(bool last_one, 14 | [in, count=buf_size] float* input_buf, 15 | [out, count=buf_size] float* output_buf, 16 | size_t buf_size); 17 | 18 | public void enclave_weights_decryption( 19 | [in, out, count=buf_size] float* input_buf, 20 | size_t buf_size); 21 | 22 | public void enclave_weights_encryption( 23 | [in, out, count=buf_size] float* input_buf, 24 | size_t buf_size); 25 | }; 26 | 27 | untrusted { 28 | void host_secure_aggregation(); 29 | }; 30 | }; 31 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/user_ta_header_defines.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef USER_TA_HEADER_DEFINES_H 3 | #define USER_TA_HEADER_DEFINES_H 4 | 5 | /* To get the TA UUID definition */ 6 | #include 7 | 8 | #define TA_UUID TA_DARKNETP_UUID 9 | 10 | /* 11 | * TA properties: multi-instance TA, no specific attribute 12 | * TA_FLAG_EXEC_DDR is meaningless but mandated. 13 | */ 14 | #define TA_FLAGS TA_FLAG_EXEC_DDR 15 | 16 | /* Provisioned stack size */ 17 | #define TA_STACK_SIZE (1 * 1024 * 1024) 18 | 19 | /* Provisioned heap size for TEE_Malloc() and friends */ 20 | #define TA_DATA_SIZE (10 * 1024 * 1024) 21 | 22 | /* Extra properties (give a version id and a string name) */ 23 | #define TA_CURRENT_TA_EXT_PROPERTIES \ 24 | { "gp.ta.description", USER_TA_PROP_TYPE_STRING, \ 25 | "Example of OP-TEE My Test Trusted Application" }, \ 26 | { "gp.ta.version", USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0010 } } 27 | 28 | #endif /* USER_TA_HEADER_DEFINES_H */ 29 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/deconvolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DECONVOLUTIONAL_LAYER_H 2 | #define DECONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | #ifdef GPU 11 | void forward_deconvolutional_layer_gpu(layer l, network net); 12 | void backward_deconvolutional_layer_gpu(layer l, network net); 13 | void update_deconvolutional_layer_gpu(layer l, update_args a); 14 | void push_deconvolutional_layer(layer l); 15 | void pull_deconvolutional_layer(layer l); 16 | #endif 17 | 18 | layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam); 19 | void resize_deconvolutional_layer(layer *l, int h, int w); 20 | void forward_deconvolutional_layer(const layer l, network net); 21 | void update_deconvolutional_layer(layer l, update_args a); 22 | void backward_deconvolutional_layer(layer l, network net); 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/deconvolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DECONVOLUTIONAL_LAYER_H 2 | #define DECONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | #ifdef GPU 11 | void forward_deconvolutional_layer_gpu(layer l, network net); 12 | void backward_deconvolutional_layer_gpu(layer l, network net); 13 | void update_deconvolutional_layer_gpu(layer l, update_args a); 14 | void push_deconvolutional_layer(layer l); 15 | void pull_deconvolutional_layer(layer l); 16 | #endif 17 | 18 | layer make_deconvolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int adam); 19 | void resize_deconvolutional_layer(layer *l, int h, int w); 20 | void forward_deconvolutional_layer(const layer l, network net); 21 | void update_deconvolutional_layer(layer l, update_args a); 22 | void backward_deconvolutional_layer(layer l, network net); 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/convolutional_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_TA_H 2 | #define CONVOLUTIONAL_LAYER_TA_H 3 | 4 | #include "darknet_TA.h" 5 | 6 | typedef layer_TA convolutional_layer_TA; 7 | 8 | void add_bias_TA(float *output, float *biases, int batch, int n, int size); 9 | 10 | void scale_bias_TA(float *output, float *scales, int batch, int n, int size); 11 | 12 | void backward_bias_TA(float *bias_updates, float *delta, int batch, int n, int size); 13 | 14 | convolutional_layer_TA make_convolutional_layer_TA_new(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION_TA activation, int batch_normalize, int binary, int xnor, int adam, int flipped, float dot); 15 | 16 | void forward_convolutional_layer_TA_new(convolutional_layer_TA l, network_TA net); 17 | 18 | void backward_convolutional_layer_TA_new(convolutional_layer_TA l, network_TA net); 19 | 20 | void update_convolutional_layer_TA_new(convolutional_layer_TA l, update_args_TA a); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /server_side_sgx/host/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | add_custom_command( 4 | OUTPUT secure_aggregation_u.h secure_aggregation_u.c secure_aggregation_args.h 5 | DEPENDS ${CMAKE_SOURCE_DIR}/secure_aggregation.edl 6 | COMMAND 7 | openenclave::oeedger8r --untrusted ${CMAKE_SOURCE_DIR}/secure_aggregation.edl 8 | --search-path ${OE_INCLUDEDIR} --search-path 9 | ${OE_INCLUDEDIR}/openenclave/edl/sgx) 10 | 11 | add_executable(secure_aggregation_host host.c 12 | ${CMAKE_CURRENT_BINARY_DIR}/secure_aggregation_u.c) 13 | 14 | if (WIN32) 15 | copy_oedebugrt_target(secure_aggregation_host_oedebugrt) 16 | add_dependencies(secure_aggregation_host secure_aggregation_host_oedebugrt) 17 | endif () 18 | 19 | target_include_directories( 20 | secure_aggregation_host PRIVATE # Needed for the generated file secure_aggregation_u.h 21 | ${CMAKE_CURRENT_BINARY_DIR}) 22 | 23 | target_link_libraries(secure_aggregation_host openenclave::oehost) 24 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/gemm.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_H 2 | #define GEMM_H 3 | 4 | void gemm_bin(int M, int N, int K, float ALPHA, 5 | char *A, int lda, 6 | float *B, int ldb, 7 | float *C, int ldc); 8 | 9 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 10 | float *A, int lda, 11 | float *B, int ldb, 12 | float BETA, 13 | float *C, int ldc); 14 | 15 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 16 | float *A, int lda, 17 | float *B, int ldb, 18 | float BETA, 19 | float *C, int ldc); 20 | 21 | #ifdef GPU 22 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 23 | float *A_gpu, int lda, 24 | float *B_gpu, int ldb, 25 | float BETA, 26 | float *C_gpu, int ldc); 27 | 28 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 29 | float *A, int lda, 30 | float *B, int ldb, 31 | float BETA, 32 | float *C, int ldc); 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/local_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_LAYER_H 2 | #define LOCAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer local_layer; 11 | 12 | #ifdef GPU 13 | void forward_local_layer_gpu(local_layer layer, network net); 14 | void backward_local_layer_gpu(local_layer layer, network net); 15 | void update_local_layer_gpu(local_layer layer, update_args a); 16 | 17 | void push_local_layer(local_layer layer); 18 | void pull_local_layer(local_layer layer); 19 | #endif 20 | 21 | local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation); 22 | 23 | void forward_local_layer(const local_layer layer, network net); 24 | void backward_local_layer(local_layer layer, network net); 25 | void update_local_layer(local_layer layer, update_args a); 26 | 27 | void bias_output(float *output, float *biases, int batch, int n, int size); 28 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/local_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_LAYER_H 2 | #define LOCAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer local_layer; 11 | 12 | #ifdef GPU 13 | void forward_local_layer_gpu(local_layer layer, network net); 14 | void backward_local_layer_gpu(local_layer layer, network net); 15 | void update_local_layer_gpu(local_layer layer, update_args a); 16 | 17 | void push_local_layer(local_layer layer); 18 | void pull_local_layer(local_layer layer); 19 | #endif 20 | 21 | local_layer make_local_layer(int batch, int h, int w, int c, int n, int size, int stride, int pad, ACTIVATION activation); 22 | 23 | void forward_local_layer(const local_layer layer, network net); 24 | void backward_local_layer(local_layer layer, network net); 25 | void update_local_layer(local_layer layer, update_args a); 26 | 27 | void bias_output(float *output, float *biases, int batch, int n, int size); 28 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Fan Mo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /server_side_sgx/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Vincent 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /client_side_trustzone/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Fan Mo, f.mo18@imperial.ac.uk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /client_side_trustzone/ta/utils_TA.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "utils_TA.h" 6 | #include "math_TA.h" 7 | 8 | float sum_array_TA(float *a, int n) 9 | { 10 | int i; 11 | float sum = 0; 12 | for(i = 0; i < n; ++i) sum += a[i]; 13 | 14 | return sum; 15 | } 16 | 17 | float rand_uniform_TA(float min, float max) 18 | { 19 | if(max < min){ 20 | float swap = min; 21 | min = max; 22 | max = swap; 23 | } 24 | return ((float)ta_rand() * (max - min)) + min; 25 | } 26 | 27 | 28 | float rand_normal_TA(float mu, float sigma){ 29 | float U1, U2, W, mult; 30 | static float X1, X2; 31 | static int call = 0; 32 | 33 | if (call == 1) 34 | { 35 | call = !call; 36 | return (mu + sigma * (float) X2); 37 | } 38 | 39 | do 40 | { 41 | U1 = -1 + (float) ta_rand () * 2; 42 | U2 = -1 + (float) ta_rand () * 2; 43 | W = ta_pow (U1, 2) + ta_pow (U2, 2); 44 | } 45 | while (W >= 1 || W == 0); 46 | 47 | mult = ta_sqrt ((-2 * ta_ln (W)) / W); //ta_log 48 | X1 = U1 * mult; 49 | X2 = U2 * mult; 50 | 51 | call = !call; 52 | 53 | return (mu + sigma * (float) X1); 54 | } 55 | -------------------------------------------------------------------------------- /config.mk: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | # Perform common configuration for building sample enclaves and hosts. 5 | 6 | # Detect compiler. 7 | ifneq ($(CC),cc) 8 | # CC explicitly specified. 9 | else ifneq ($(shell $(CC) --version | grep clang),) 10 | # CC is default (cc), and aliases to clang. 11 | else 12 | # CC is default (cc), and does not alias to clang. 13 | CLANG_VERSION = $(shell for v in "10" "9" "8"; do \ 14 | if [ -n "$$(command -v clang-$$v)" ]; then \ 15 | echo $$v; \ 16 | break; \ 17 | fi; \ 18 | done) 19 | 20 | ifneq ($(CLANG_VERSION),) 21 | CC = clang-$(CLANG_VERSION) 22 | CXX = clang++-$(CLANG_VERSION) 23 | endif 24 | endif 25 | 26 | # Choose the right pkg-config based on CC. 27 | C_COMPILER = clang 28 | CXX_COMPILER = clang++ 29 | ifeq ($(shell $(CC) --version | grep clang),) 30 | C_COMPILER = gcc 31 | CXX_COMPILER = g++ 32 | endif 33 | 34 | # Define COMPILER for samples that use only C. 35 | COMPILER = $(C_COMPILER) 36 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/blas_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef BLAS_TA_H 2 | #define BLAS_TA_H 3 | #include "darknet_TA.h" 4 | 5 | void fill_cpu_TA(int N, float ALPHA, float *X, int INCX); 6 | 7 | void copy_cpu_TA(int N, float *X, int INCX, float *Y, int INCY); 8 | 9 | void mean_cpu_TA(float *x, int batch, int filters, int spatial, float *mean); 10 | 11 | void variance_cpu_TA(float *x, float *mean, int batch, int filters, int spatial, float *variance); 12 | 13 | void scal_cpu_TA(int N, float ALPHA, float *X, int INCX); 14 | 15 | void axpy_cpu_TA(int N, float ALPHA, float *X, int INCX, float *Y, int INCY); 16 | 17 | void normalize_cpu_TA(float *x, float *mean, float *variance, int batch, int filters, int spatial); 18 | 19 | void softmax_TA(float *input, int n, float temp, int stride, float *output); 20 | 21 | void softmax_cpu_TA(float *input, int n, int batch, int batch_offset, int groups, int group_offset, int stride, float temp, float *output); 22 | 23 | void softmax_x_ent_cpu_TA(int n, float *pred, float *truth, float *delta, float *error); 24 | 25 | void smooth_l1_cpu_TA(int n, float *pred, float *truth, float *delta, float *error); 26 | 27 | void l1_cpu_TA(int n, float *pred, float *truth, float *delta, float *error); 28 | 29 | void l2_cpu_TA(int n, float *pred, float *truth, float *delta, float *error); 30 | #endif 31 | -------------------------------------------------------------------------------- /server_side_sgx/enclave/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | # Use the edger8r to generate C bindings from the EDL file. 5 | add_custom_command( 6 | OUTPUT secure_aggregation_t.h secure_aggregation_t.c secure_aggregation_args.h 7 | DEPENDS ${CMAKE_SOURCE_DIR}/secure_aggregation.edl 8 | COMMAND 9 | openenclave::oeedger8r --trusted ${CMAKE_SOURCE_DIR}/secure_aggregation.edl 10 | --search-path ${OE_INCLUDEDIR} --search-path 11 | ${OE_INCLUDEDIR}/openenclave/edl/sgx) 12 | 13 | add_executable(enclave enc.c ${CMAKE_CURRENT_BINARY_DIR}/secure_aggregation_t.c) 14 | 15 | if (WIN32) 16 | maybe_build_using_clangw(enclave) 17 | endif () 18 | 19 | target_compile_definitions(enclave PUBLIC OE_API_VERSION=2) 20 | 21 | # Need for the generated file secure_aggregation_t.h 22 | target_include_directories(enclave PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) 23 | 24 | if (LVI_MITIGATION MATCHES ControlFlow) 25 | # Helper to enable compiler options for LVI mitigation. 26 | apply_lvi_mitigation(enclave) 27 | # Link against LVI-mitigated libraries. 28 | target_link_libraries(enclave openenclave::oeenclave-lvi-cfg 29 | openenclave::oelibc-lvi-cfg) 30 | else () 31 | target_link_libraries(enclave openenclave::oeenclave openenclave::oelibc) 32 | endif () 33 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/darknetp_ta.h: -------------------------------------------------------------------------------- 1 | #ifndef TA_DARKNETP_H 2 | #define TA_DARKNETP_H 3 | 4 | #include "darknet_TA.h" 5 | #include "network_TA.h" 6 | 7 | extern float *netta_truth; 8 | extern int debug_summary_com; 9 | extern int debug_summary_pass; 10 | 11 | /* 12 | * This UUID is generated with uuidgen 13 | * the ITU-T UUID generator at http://www.itu.int/ITU-T/asn1/uuid.html 14 | */ 15 | #define TA_DARKNETP_UUID \ 16 | { 0x7fc5c039, 0x0542, 0x4ee1, \ 17 | { 0x80, 0xaf, 0xb4, 0xea, 0xb2, 0xf1, 0x99, 0x8d} } 18 | 19 | /* The function IDs implemented in this TA */ 20 | #define MAKE_NETWORK_CMD 1 21 | #define WORKSPACE_NETWORK_CMD 2 22 | #define MAKE_CONV_CMD 3 23 | #define MAKE_MAX_CMD 4 24 | #define MAKE_DROP_CMD 5 25 | #define MAKE_CONNECTED_CMD 6 26 | #define MAKE_SOFTMAX_CMD 7 27 | #define MAKE_COST_CMD 8 28 | #define FORWARD_CMD 9 29 | #define BACKWARD_CMD 10 30 | #define BACKWARD_ADD_CMD 11 31 | #define UPDATE_CMD 12 32 | #define NET_TRUTH_CMD 13 33 | #define CALC_LOSS_CMD 14 34 | #define TRANS_WEI_CMD 15 35 | #define OUTPUT_RETURN_CMD 16 36 | #define SAVE_WEI_CMD 17 37 | 38 | #define FORWARD_BACK_CMD 18 39 | #define BACKWARD_BACK_CMD 19 40 | #define BACKWARD_BACK_ADD_CMD 20 41 | 42 | #define MAKE_AVG_CMD 21 43 | #define MAKE_DWCONV_CMD 22 44 | 45 | void summary_array(char *print_name, float *arr, int n); 46 | 47 | #endif /*TA_DARKNETP_H*/ 48 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/dropout_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "dropout_layer.h" 7 | #include "cuda.h" 8 | #include "utils.h" 9 | } 10 | 11 | __global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) 12 | { 13 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 14 | if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; 15 | } 16 | 17 | void forward_dropout_layer_gpu(dropout_layer layer, network net) 18 | { 19 | if (!net.train) return; 20 | int size = layer.inputs*layer.batch; 21 | cuda_random(layer.rand_gpu, size); 22 | /* 23 | int i; 24 | for(i = 0; i < size; ++i){ 25 | layer.rand[i] = rand_uniform(); 26 | } 27 | cuda_push_array(layer.rand_gpu, layer.rand, size); 28 | */ 29 | 30 | yoloswag420blazeit360noscope<<>>(net.input_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 31 | check_error(cudaPeekAtLastError()); 32 | } 33 | 34 | void backward_dropout_layer_gpu(dropout_layer layer, network net) 35 | { 36 | if(!net.delta_gpu) return; 37 | int size = layer.inputs*layer.batch; 38 | 39 | yoloswag420blazeit360noscope<<>>(net.delta_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 40 | check_error(cudaPeekAtLastError()); 41 | } 42 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/dropout_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "dropout_layer.h" 7 | #include "cuda.h" 8 | #include "utils.h" 9 | } 10 | 11 | __global__ void yoloswag420blazeit360noscope(float *input, int size, float *rand, float prob, float scale) 12 | { 13 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 14 | if(id < size) input[id] = (rand[id] < prob) ? 0 : input[id]*scale; 15 | } 16 | 17 | void forward_dropout_layer_gpu(dropout_layer layer, network net) 18 | { 19 | if (!net.train) return; 20 | int size = layer.inputs*layer.batch; 21 | cuda_random(layer.rand_gpu, size); 22 | /* 23 | int i; 24 | for(i = 0; i < size; ++i){ 25 | layer.rand[i] = rand_uniform(); 26 | } 27 | cuda_push_array(layer.rand_gpu, layer.rand, size); 28 | */ 29 | 30 | yoloswag420blazeit360noscope<<>>(net.input_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 31 | check_error(cudaPeekAtLastError()); 32 | } 33 | 34 | void backward_dropout_layer_gpu(dropout_layer layer, network net) 35 | { 36 | if(!net.delta_gpu) return; 37 | int size = layer.inputs*layer.batch; 38 | 39 | yoloswag420blazeit360noscope<<>>(net.delta_gpu, size, layer.rand_gpu, layer.probability, layer.scale); 40 | check_error(cudaPeekAtLastError()); 41 | } 42 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/depthwise_convolutional_layer_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef DEPTHWISE_CONVOLUTIONAL_LAYER_TA_H 2 | #define DEPTHWISE_CONVOLUTIONAL_LAYER_TA_H 3 | 4 | #include "activations_TA.h" 5 | //#include "layer_TA.h" 6 | #include "network_TA.h" 7 | #include "darknet_TA.h" 8 | 9 | typedef layer_TA depthwise_convolutional_layer_TA; 10 | 11 | depthwise_convolutional_layer_TA make_depthwise_convolutional_layer_TA_new(int batch, int h, int w, int c, int size, int stride, int padding, ACTIVATION_TA activation, int batch_normalize); 12 | 13 | void forward_depthwise_convolutional_layer_TA_new(const depthwise_convolutional_layer_TA layer, network_TA net); 14 | 15 | void backward_depthwise_convolutional_layer_TA_new(depthwise_convolutional_layer_TA layer, network_TA net); 16 | 17 | void update_depthwise_convolutional_layer_TA_new(depthwise_convolutional_layer_TA layer, update_args_TA a); 18 | 19 | void resize_depthwise_convolutional_layer_TA(depthwise_convolutional_layer_TA *layer, int w, int h); 20 | 21 | void denormalize_depthwise_convolutional_layer_TA(depthwise_convolutional_layer_TA l); 22 | 23 | void add_bias_TA(float *output, float *biases, int batch, int n, int size); 24 | void backward_bias_TA(float *bias_updates, float *delta, int batch, int n, int size); 25 | 26 | int depthwise_convolutional_out_height_TA(depthwise_convolutional_layer_TA layer); 27 | int depthwise_convolutional_out_width_TA(depthwise_convolutional_layer_TA layer); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/col2im.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void col2im_add_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad, float val) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return; 11 | im[col + width*(row + height*channel)] += val; 12 | } 13 | //This one might be too, can't remember. 14 | void col2im_cpu(float* data_col, 15 | int channels, int height, int width, 16 | int ksize, int stride, int pad, float* data_im) 17 | { 18 | int c,h,w; 19 | int height_col = (height + 2*pad - ksize) / stride + 1; 20 | int width_col = (width + 2*pad - ksize) / stride + 1; 21 | 22 | int channels_col = channels * ksize * ksize; 23 | for (c = 0; c < channels_col; ++c) { 24 | int w_offset = c % ksize; 25 | int h_offset = (c / ksize) % ksize; 26 | int c_im = c / ksize / ksize; 27 | for (h = 0; h < height_col; ++h) { 28 | for (w = 0; w < width_col; ++w) { 29 | int im_row = h_offset + h * stride; 30 | int im_col = w_offset + w * stride; 31 | int col_index = (c * height_col + h) * width_col + w; 32 | double val = data_col[col_index]; 33 | col2im_add_pixel(data_im, height, width, channels, 34 | im_row, im_col, c_im, pad, val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/im2col.c: -------------------------------------------------------------------------------- 1 | #include "im2col.h" 2 | #include 3 | float im2col_get_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return 0; 11 | return im[col + width*(row + height*channel)]; 12 | } 13 | 14 | //From Berkeley Vision's Caffe! 15 | //https://github.com/BVLC/caffe/blob/master/LICENSE 16 | void im2col_cpu(float* data_im, 17 | int channels, int height, int width, 18 | int ksize, int stride, int pad, float* data_col) 19 | { 20 | int c,h,w; 21 | int height_col = (height + 2*pad - ksize) / stride + 1; 22 | int width_col = (width + 2*pad - ksize) / stride + 1; 23 | 24 | int channels_col = channels * ksize * ksize; 25 | for (c = 0; c < channels_col; ++c) { 26 | int w_offset = c % ksize; 27 | int h_offset = (c / ksize) % ksize; 28 | int c_im = c / ksize / ksize; 29 | for (h = 0; h < height_col; ++h) { 30 | for (w = 0; w < width_col; ++w) { 31 | int im_row = h_offset + h * stride; 32 | int im_col = w_offset + w * stride; 33 | int col_index = (c * height_col + h) * width_col + w; 34 | data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/col2im.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void col2im_add_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad, float val) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return; 11 | im[col + width*(row + height*channel)] += val; 12 | } 13 | //This one might be too, can't remember. 14 | void col2im_cpu(float* data_col, 15 | int channels, int height, int width, 16 | int ksize, int stride, int pad, float* data_im) 17 | { 18 | int c,h,w; 19 | int height_col = (height + 2*pad - ksize) / stride + 1; 20 | int width_col = (width + 2*pad - ksize) / stride + 1; 21 | 22 | int channels_col = channels * ksize * ksize; 23 | for (c = 0; c < channels_col; ++c) { 24 | int w_offset = c % ksize; 25 | int h_offset = (c / ksize) % ksize; 26 | int c_im = c / ksize / ksize; 27 | for (h = 0; h < height_col; ++h) { 28 | for (w = 0; w < width_col; ++w) { 29 | int im_row = h_offset + h * stride; 30 | int im_col = w_offset + w * stride; 31 | int col_index = (c * height_col + h) * width_col + w; 32 | double val = data_col[col_index]; 33 | col2im_add_pixel(data_im, height, width, channels, 34 | im_row, im_col, c_im, pad, val); 35 | } 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/im2col.c: -------------------------------------------------------------------------------- 1 | #include "im2col.h" 2 | #include 3 | float im2col_get_pixel(float *im, int height, int width, int channels, 4 | int row, int col, int channel, int pad) 5 | { 6 | row -= pad; 7 | col -= pad; 8 | 9 | if (row < 0 || col < 0 || 10 | row >= height || col >= width) return 0; 11 | return im[col + width*(row + height*channel)]; 12 | } 13 | 14 | //From Berkeley Vision's Caffe! 15 | //https://github.com/BVLC/caffe/blob/master/LICENSE 16 | void im2col_cpu(float* data_im, 17 | int channels, int height, int width, 18 | int ksize, int stride, int pad, float* data_col) 19 | { 20 | int c,h,w; 21 | int height_col = (height + 2*pad - ksize) / stride + 1; 22 | int width_col = (width + 2*pad - ksize) / stride + 1; 23 | 24 | int channels_col = channels * ksize * ksize; 25 | for (c = 0; c < channels_col; ++c) { 26 | int w_offset = c % ksize; 27 | int h_offset = (c / ksize) % ksize; 28 | int c_im = c / ksize / ksize; 29 | for (h = 0; h < height_col; ++h) { 30 | for (w = 0; w < width_col; ++w) { 31 | int im_row = h_offset + h * stride; 32 | int im_col = w_offset + w * stride; 33 | int col_index = (c * height_col + h) * width_col + w; 34 | data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/col2im_TA.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "math_TA.h" 3 | 4 | void col2im_add_pixel_TA(float *im, int height, int width, int channels, 5 | int row, int col, int channel, int pad, float val) 6 | { 7 | row -= pad; 8 | col -= pad; 9 | 10 | if (row < 0 || col < 0 || 11 | row >= height || col >= width) return; 12 | im[col + width*(row + height*channel)] += val; 13 | } 14 | //This one might be too, can't remember. 15 | void col2im_cpu_TA(float* data_col, 16 | int channels, int height, int width, 17 | int ksize, int stride, int pad, float* data_im) 18 | { 19 | int c,h,w; 20 | int height_col = (height + 2*pad - ksize) / stride + 1; 21 | int width_col = (width + 2*pad - ksize) / stride + 1; 22 | 23 | int channels_col = channels * ksize * ksize; 24 | for (c = 0; c < channels_col; ++c) { 25 | int w_offset = c % ksize; 26 | int h_offset = (c / ksize) % ksize; 27 | int c_im = c / ksize / ksize; 28 | for (h = 0; h < height_col; ++h) { 29 | for (w = 0; w < width_col; ++w) { 30 | int im_row = h_offset + h * stride; 31 | int im_col = w_offset + w * stride; 32 | int col_index = (c * height_col + h) * width_col + w; 33 | double val = data_col[col_index]; 34 | col2im_add_pixel_TA(data_im, height, width, channels, 35 | im_row, im_col, c_im, pad, val); 36 | } 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/gemm.h: -------------------------------------------------------------------------------- 1 | #ifndef GEMM_H 2 | #define GEMM_H 3 | 4 | void gemm_bin(int M, int N, int K, float ALPHA, 5 | char *A, int lda, 6 | float *B, int ldb, 7 | float *C, int ldc); 8 | 9 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 10 | float *A, int lda, 11 | float *B, int ldb, 12 | float BETA, 13 | float *C, int ldc); 14 | void gemm_diff(int TA, int TB, int M, int N, int K, float ALPHA, 15 | float *A, int lda, 16 | float *B, int ldb, 17 | float BETA, 18 | float *C, int ldc); 19 | 20 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 21 | float *A, int lda, 22 | float *B, int ldb, 23 | float BETA, 24 | float *C, int ldc); 25 | void gemm_cpu_diff(int TA, int TB, int M, int N, int K, float ALPHA, 26 | float *A, int lda, 27 | float *B, int ldb, 28 | float BETA, 29 | float *C, int ldc); 30 | 31 | #ifdef GPU 32 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 33 | float *A_gpu, int lda, 34 | float *B_gpu, int ldb, 35 | float BETA, 36 | float *C_gpu, int ldc); 37 | 38 | void gemm_gpu(int TA, int TB, int M, int N, int K, float ALPHA, 39 | float *A, int lda, 40 | float *B, int ldb, 41 | float BETA, 42 | float *C, int ldc); 43 | #endif 44 | #endif 45 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/im2col_TA.c: -------------------------------------------------------------------------------- 1 | #include "im2col_TA.h" 2 | #include 3 | 4 | float im2col_get_pixel_TA(float *im, int height, int width, int channels, 5 | int row, int col, int channel, int pad) 6 | { 7 | row -= pad; 8 | col -= pad; 9 | 10 | if (row < 0 || col < 0 || 11 | row >= height || col >= width) return 0; 12 | return im[col + width*(row + height*channel)]; 13 | } 14 | 15 | //From Berkeley Vision's Caffe! 16 | //https://github.com/BVLC/caffe/blob/master/LICENSE 17 | void im2col_cpu_TA(float* data_im, 18 | int channels, int height, int width, 19 | int ksize, int stride, int pad, float* data_col) 20 | { 21 | int c,h,w; 22 | int height_col = (height + 2*pad - ksize) / stride + 1; 23 | int width_col = (width + 2*pad - ksize) / stride + 1; 24 | 25 | int channels_col = channels * ksize * ksize; 26 | for (c = 0; c < channels_col; ++c) { 27 | int w_offset = c % ksize; 28 | int h_offset = (c / ksize) % ksize; 29 | int c_im = c / ksize / ksize; 30 | for (h = 0; h < height_col; ++h) { 31 | for (w = 0; w < width_col; ++w) { 32 | int im_row = h_offset + h * stride; 33 | int im_col = w_offset + w * stride; 34 | int col_index = (c * height_col + h) * width_col + w; 35 | data_col[col_index] = im2col_get_pixel_TA(data_im, height, width, channels, 36 | im_row, im_col, c_im, pad); 37 | } 38 | } 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /server_side_sgx/host/examples/art.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | 3 | #include 4 | 5 | void demo_art(char *cfgfile, char *weightfile, int cam_index) 6 | { 7 | #ifdef OPENCV 8 | network *net = load_network(cfgfile, weightfile, 0); 9 | set_batch_network(net, 1); 10 | 11 | srand(2222222); 12 | 13 | void * cap = open_video_stream(0, cam_index, 0,0,0); 14 | 15 | char *window = "ArtJudgementBot9000!!!"; 16 | if(!cap) error("Couldn't connect to webcam.\n"); 17 | int i; 18 | int idx[] = {37, 401, 434}; 19 | int n = sizeof(idx)/sizeof(idx[0]); 20 | 21 | while(1){ 22 | image in = get_image_from_stream(cap); 23 | image in_s = resize_image(in, net->w, net->h); 24 | 25 | float *p = network_predict(net, in_s.data); 26 | 27 | printf("\033[2J"); 28 | printf("\033[1;1H"); 29 | 30 | float score = 0; 31 | for(i = 0; i < n; ++i){ 32 | float s = p[idx[i]]; 33 | if (s > score) score = s; 34 | } 35 | score = score; 36 | printf("I APPRECIATE THIS ARTWORK: %10.7f%%\n", score*100); 37 | printf("["); 38 | int upper = 30; 39 | for(i = 0; i < upper; ++i){ 40 | printf("%c", ((i+.5) < score*upper) ? 219 : ' '); 41 | } 42 | printf("]\n"); 43 | 44 | show_image(in, window, 1); 45 | free_image(in_s); 46 | free_image(in); 47 | } 48 | #endif 49 | } 50 | 51 | 52 | void run_art(int argc, char **argv) 53 | { 54 | int cam_index = find_int_arg(argc, argv, "-c", 0); 55 | char *cfg = argv[2]; 56 | char *weights = argv[3]; 57 | demo_art(cfg, weights, cam_index); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /client_side_trustzone/host/examples/art.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | 3 | #include 4 | 5 | void demo_art(char *cfgfile, char *weightfile, int cam_index) 6 | { 7 | #ifdef OPENCV 8 | network *net = load_network(cfgfile, weightfile, 0); 9 | set_batch_network(net, 1); 10 | 11 | srand(2222222); 12 | 13 | void * cap = open_video_stream(0, cam_index, 0,0,0); 14 | 15 | char *window = "ArtJudgementBot9000!!!"; 16 | if(!cap) error("Couldn't connect to webcam.\n"); 17 | int i; 18 | int idx[] = {37, 401, 434}; 19 | int n = sizeof(idx)/sizeof(idx[0]); 20 | 21 | while(1){ 22 | image in = get_image_from_stream(cap); 23 | image in_s = resize_image(in, net->w, net->h); 24 | 25 | float *p = network_predict(net, in_s.data); 26 | 27 | printf("\033[2J"); 28 | printf("\033[1;1H"); 29 | 30 | float score = 0; 31 | for(i = 0; i < n; ++i){ 32 | float s = p[idx[i]]; 33 | if (s > score) score = s; 34 | } 35 | score = score; 36 | printf("I APPRECIATE THIS ARTWORK: %10.7f%%\n", score*100); 37 | printf("["); 38 | int upper = 30; 39 | for(i = 0; i < upper; ++i){ 40 | printf("%c", ((i+.5) < score*upper) ? 219 : ' '); 41 | } 42 | printf("]\n"); 43 | 44 | show_image(in, window, 1); 45 | free_image(in_s); 46 | free_image(in); 47 | } 48 | #endif 49 | } 50 | 51 | 52 | void run_art(int argc, char **argv) 53 | { 54 | int cam_index = find_int_arg(argc, argv, "-c", 0); 55 | char *cfg = argv[2]; 56 | char *weights = argv[3]; 57 | demo_art(cfg, weights, cam_index); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /server_side_sgx/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | cmake_minimum_required(VERSION 3.11) 5 | 6 | if (LVI_MITIGATION MATCHES ControlFlow) 7 | # Configure the cmake to use customized compilation toolchain. 8 | # This package has to be added before `project()`. 9 | find_package(OpenEnclave-LVI-Mitigation CONFIG REQUIRED) 10 | endif () 11 | 12 | project("Secure Aggregation" LANGUAGES C CXX) 13 | 14 | # Currently the `OpenEnclave` package depends on `project()`. 15 | find_package(OpenEnclave CONFIG REQUIRED) 16 | 17 | set(CMAKE_CXX_STANDARD 11) 18 | 19 | add_subdirectory(enclave) 20 | add_subdirectory(host) 21 | 22 | # Generate key 23 | add_custom_command( 24 | OUTPUT private.pem public.pem 25 | COMMAND openssl genrsa -out private.pem -3 3072 26 | COMMAND openssl rsa -in private.pem -pubout -out public.pem) 27 | 28 | # Sign enclave 29 | add_custom_command( 30 | OUTPUT enclave/enclave.signed 31 | DEPENDS enclave enclave/secureaggregation.conf private.pem 32 | COMMAND openenclave::oesign sign -e $ -c 33 | ${CMAKE_SOURCE_DIR}/enclave/secureaggregation.conf -k private.pem) 34 | 35 | add_custom_target(sign ALL DEPENDS enclave/enclave.signed) 36 | 37 | if ((NOT DEFINED ENV{OE_SIMULATION}) OR (NOT $ENV{OE_SIMULATION})) 38 | add_custom_target( 39 | run 40 | DEPENDS secureaggregation_host sign 41 | COMMAND secureaggregation_host ${CMAKE_BINARY_DIR}/enclave/enclave.signed) 42 | endif () 43 | 44 | add_custom_target( 45 | simulate 46 | DEPENDS secureaggregation_host sign 47 | COMMAND secureaggregation_host ${CMAKE_BINARY_DIR}/enclave/enclave.signed --simulate) 48 | -------------------------------------------------------------------------------- /server_side_sgx/host/examples/detector-scipy-opencv.py: -------------------------------------------------------------------------------- 1 | # Stupid python path shit. 2 | # Instead just add darknet.py to somewhere in your python path 3 | # OK actually that might not be a great idea, idk, work in progress 4 | # Use at your own risk. or don't, i don't care 5 | 6 | from scipy.misc import imread 7 | import cv2 8 | 9 | def array_to_image(arr): 10 | arr = arr.transpose(2,0,1) 11 | c = arr.shape[0] 12 | h = arr.shape[1] 13 | w = arr.shape[2] 14 | arr = (arr/255.0).flatten() 15 | data = dn.c_array(dn.c_float, arr) 16 | im = dn.IMAGE(w,h,c,data) 17 | return im 18 | 19 | def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): 20 | boxes = dn.make_boxes(net) 21 | probs = dn.make_probs(net) 22 | num = dn.num_boxes(net) 23 | dn.network_detect(net, image, thresh, hier_thresh, nms, boxes, probs) 24 | res = [] 25 | for j in range(num): 26 | for i in range(meta.classes): 27 | if probs[j][i] > 0: 28 | res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h))) 29 | res = sorted(res, key=lambda x: -x[1]) 30 | dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num) 31 | return res 32 | 33 | import sys, os 34 | sys.path.append(os.path.join(os.getcwd(),'python/')) 35 | 36 | import darknet as dn 37 | 38 | # Darknet 39 | net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0) 40 | meta = dn.load_meta("cfg/coco.data") 41 | r = dn.detect(net, meta, "data/dog.jpg") 42 | print r 43 | 44 | # scipy 45 | arr= imread('data/dog.jpg') 46 | im = array_to_image(arr) 47 | r = detect2(net, meta, im) 48 | print r 49 | 50 | # OpenCV 51 | arr = cv2.imread('data/dog.jpg') 52 | im = array_to_image(arr) 53 | dn.rgbgr_image(im) 54 | r = detect2(net, meta, im) 55 | print r 56 | 57 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | list *make_list() 6 | { 7 | list *l = malloc(sizeof(list)); 8 | l->size = 0; 9 | l->front = 0; 10 | l->back = 0; 11 | return l; 12 | } 13 | 14 | /* 15 | void transfer_node(list *s, list *d, node *n) 16 | { 17 | node *prev, *next; 18 | prev = n->prev; 19 | next = n->next; 20 | if(prev) prev->next = next; 21 | if(next) next->prev = prev; 22 | --s->size; 23 | if(s->front == n) s->front = next; 24 | if(s->back == n) s->back = prev; 25 | } 26 | */ 27 | 28 | void *list_pop(list *l){ 29 | if(!l->back) return 0; 30 | node *b = l->back; 31 | void *val = b->val; 32 | l->back = b->prev; 33 | if(l->back) l->back->next = 0; 34 | free(b); 35 | --l->size; 36 | 37 | return val; 38 | } 39 | 40 | void list_insert(list *l, void *val) 41 | { 42 | node *new = malloc(sizeof(node)); 43 | new->val = val; 44 | new->next = 0; 45 | 46 | if(!l->back){ 47 | l->front = new; 48 | new->prev = 0; 49 | }else{ 50 | l->back->next = new; 51 | new->prev = l->back; 52 | } 53 | l->back = new; 54 | ++l->size; 55 | } 56 | 57 | void free_node(node *n) 58 | { 59 | node *next; 60 | while(n) { 61 | next = n->next; 62 | free(n); 63 | n = next; 64 | } 65 | } 66 | 67 | void free_list(list *l) 68 | { 69 | free_node(l->front); 70 | free(l); 71 | } 72 | 73 | void free_list_contents(list *l) 74 | { 75 | node *n = l->front; 76 | while(n){ 77 | free(n->val); 78 | n = n->next; 79 | } 80 | } 81 | 82 | void **list_to_array(list *l) 83 | { 84 | void **a = calloc(l->size, sizeof(void*)); 85 | int count = 0; 86 | node *n = l->front; 87 | while(n){ 88 | a[count++] = n->val; 89 | n = n->next; 90 | } 91 | return a; 92 | } 93 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | 5 | list *make_list() 6 | { 7 | list *l = malloc(sizeof(list)); 8 | l->size = 0; 9 | l->front = 0; 10 | l->back = 0; 11 | return l; 12 | } 13 | 14 | /* 15 | void transfer_node(list *s, list *d, node *n) 16 | { 17 | node *prev, *next; 18 | prev = n->prev; 19 | next = n->next; 20 | if(prev) prev->next = next; 21 | if(next) next->prev = prev; 22 | --s->size; 23 | if(s->front == n) s->front = next; 24 | if(s->back == n) s->back = prev; 25 | } 26 | */ 27 | 28 | void *list_pop(list *l){ 29 | if(!l->back) return 0; 30 | node *b = l->back; 31 | void *val = b->val; 32 | l->back = b->prev; 33 | if(l->back) l->back->next = 0; 34 | free(b); 35 | --l->size; 36 | 37 | return val; 38 | } 39 | 40 | void list_insert(list *l, void *val) 41 | { 42 | node *new = malloc(sizeof(node)); 43 | new->val = val; 44 | new->next = 0; 45 | 46 | if(!l->back){ 47 | l->front = new; 48 | new->prev = 0; 49 | }else{ 50 | l->back->next = new; 51 | new->prev = l->back; 52 | } 53 | l->back = new; 54 | ++l->size; 55 | } 56 | 57 | void free_node(node *n) 58 | { 59 | node *next; 60 | while(n) { 61 | next = n->next; 62 | free(n); 63 | n = next; 64 | } 65 | } 66 | 67 | void free_list(list *l) 68 | { 69 | free_node(l->front); 70 | free(l); 71 | } 72 | 73 | void free_list_contents(list *l) 74 | { 75 | node *n = l->front; 76 | while(n){ 77 | free(n->val); 78 | n = n->next; 79 | } 80 | } 81 | 82 | void **list_to_array(list *l) 83 | { 84 | void **a = calloc(l->size, sizeof(void*)); 85 | int count = 0; 86 | node *n = l->front; 87 | while(n){ 88 | a[count++] = n->val; 89 | n = n->next; 90 | } 91 | return a; 92 | } 93 | -------------------------------------------------------------------------------- /server_side_sgx/enclave/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | include ../../config.mk 5 | 6 | CRYPTO_LDFLAGS := $(shell pkg-config oeenclave-$(COMPILER) --variable=${OE_CRYPTO_LIB}libs) 7 | 8 | ifeq ($(LVI_MITIGATION), ControlFlow) 9 | ifeq ($(LVI_MITIGATION_BINDIR),) 10 | $(error LVI_MITIGATION_BINDIR is not set) 11 | endif 12 | # Only run once. 13 | ifeq (,$(findstring $(LVI_MITIGATION_BINDIR),$(CC))) 14 | CC := $(LVI_MITIGATION_BINDIR)/$(CC) 15 | endif 16 | COMPILER := $(COMPILER)-lvi-cfg 17 | CRYPTO_LDFLAGS := $(shell pkg-config oeenclave-$(COMPILER) --variable=${OE_CRYPTO_LIB}libslvicfg) 18 | endif 19 | 20 | CFLAGS=$(shell pkg-config oeenclave-$(COMPILER) --cflags) 21 | LDFLAGS=$(shell pkg-config oeenclave-$(COMPILER) --libs) 22 | INCDIR=$(shell pkg-config oeenclave-$(COMPILER) --variable=includedir) 23 | 24 | all: 25 | $(MAKE) build 26 | $(MAKE) keys 27 | $(MAKE) sign 28 | 29 | build: 30 | @ echo "Compilers used: $(CC), $(CXX)" 31 | oeedger8r ../secure_aggregation.edl --trusted \ 32 | --search-path $(INCDIR) \ 33 | --search-path $(INCDIR)/openenclave/edl/sgx 34 | $(CC) -g -c $(CFLAGS) -DOE_API_VERSION=2 aes.c -o aes.o 35 | $(CC) -g -c $(CFLAGS) -DOE_API_VERSION=2 enc.c -o enc.o 36 | $(CC) -g -c $(CFLAGS) -DOE_API_VERSION=2 secure_aggregation_t.c -o secure_aggregation_t.o 37 | $(CC) -o secure_aggregation_enc secure_aggregation_t.o enc.o aes.o $(LDFLAGS) $(CRYPTO_LDFLAGS) 38 | 39 | sign: 40 | oesign sign -e secure_aggregation_enc -c secure_aggregation.conf -k private.pem 41 | 42 | clean: 43 | rm -f enc.o secure_aggregation_enc secure_aggregation_enc.signed private.pem public.pem secure_aggregation_t.o secure_aggregation_t.h secure_aggregation_t.c secure_aggregation_args.h aes.o 44 | 45 | keys: 46 | openssl genrsa -out private.pem -3 3072 47 | openssl rsa -in private.pem -pubout -out public.pem 48 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/avgpool_layer_TA.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer_TA.h" 2 | 3 | #include 4 | 5 | avgpool_layer_TA make_avgpool_layer_TA(int batch, int w, int h, int c) 6 | { 7 | avgpool_layer_TA l = {0}; 8 | l.type = AVGPOOL_TA; 9 | l.batch = batch; 10 | l.h = h; 11 | l.w = w; 12 | l.c = c; 13 | l.out_w = 1; 14 | l.out_h = 1; 15 | l.out_c = c; 16 | l.outputs = l.out_c; 17 | l.inputs = h*w*c; 18 | int output_size = l.outputs * batch; 19 | l.output = calloc(output_size, sizeof(float)); 20 | l.delta = calloc(output_size, sizeof(float)); 21 | l.forward_TA = forward_avgpool_layer_TA_new; 22 | l.backward_TA = backward_avgpool_layer_TA_new; 23 | 24 | return l; 25 | } 26 | 27 | void resize_avgpool_layer_TA(avgpool_layer_TA *l, int w, int h) 28 | { 29 | l->w = w; 30 | l->h = h; 31 | l->inputs = h*w*l->c; 32 | } 33 | 34 | void forward_avgpool_layer_TA_new(const avgpool_layer_TA l, network_TA net) 35 | { 36 | int b,i,k; 37 | 38 | for(b = 0; b < l.batch; ++b){ 39 | for(k = 0; k < l.c; ++k){ 40 | int out_index = k + b*l.c; 41 | l.output[out_index] = 0; 42 | for(i = 0; i < l.h*l.w; ++i){ 43 | int in_index = i + l.h*l.w*(k + b*l.c); 44 | l.output[out_index] += net.input[in_index]; 45 | } 46 | l.output[out_index] /= l.h*l.w; 47 | } 48 | } 49 | } 50 | 51 | void backward_avgpool_layer_TA_new(const avgpool_layer_TA l, network_TA net) 52 | { 53 | int b,i,k; 54 | 55 | for(b = 0; b < l.batch; ++b){ 56 | for(k = 0; k < l.c; ++k){ 57 | int out_index = k + b*l.c; 58 | for(i = 0; i < l.h*l.w; ++i){ 59 | int in_index = i + l.h*l.w*(k + b*l.c); 60 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/avgpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "avgpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) 11 | { 12 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 13 | if(id >= n) return; 14 | 15 | int k = id % c; 16 | id /= c; 17 | int b = id; 18 | 19 | int i; 20 | int out_index = (k + c*b); 21 | output[out_index] = 0; 22 | for(i = 0; i < w*h; ++i){ 23 | int in_index = i + h*w*(k + b*c); 24 | output[out_index] += input[in_index]; 25 | } 26 | output[out_index] /= w*h; 27 | } 28 | 29 | __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) 30 | { 31 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 32 | if(id >= n) return; 33 | 34 | int k = id % c; 35 | id /= c; 36 | int b = id; 37 | 38 | int i; 39 | int out_index = (k + c*b); 40 | for(i = 0; i < w*h; ++i){ 41 | int in_index = i + h*w*(k + b*c); 42 | in_delta[in_index] += out_delta[out_index] / (w*h); 43 | } 44 | } 45 | 46 | extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net) 47 | { 48 | size_t n = layer.c*layer.batch; 49 | 50 | forward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu); 51 | check_error(cudaPeekAtLastError()); 52 | } 53 | 54 | extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net) 55 | { 56 | size_t n = layer.c*layer.batch; 57 | 58 | backward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu); 59 | check_error(cudaPeekAtLastError()); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/avgpool_layer_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "avgpool_layer.h" 7 | #include "cuda.h" 8 | } 9 | 10 | __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output) 11 | { 12 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 13 | if(id >= n) return; 14 | 15 | int k = id % c; 16 | id /= c; 17 | int b = id; 18 | 19 | int i; 20 | int out_index = (k + c*b); 21 | output[out_index] = 0; 22 | for(i = 0; i < w*h; ++i){ 23 | int in_index = i + h*w*(k + b*c); 24 | output[out_index] += input[in_index]; 25 | } 26 | output[out_index] /= w*h; 27 | } 28 | 29 | __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta) 30 | { 31 | int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x; 32 | if(id >= n) return; 33 | 34 | int k = id % c; 35 | id /= c; 36 | int b = id; 37 | 38 | int i; 39 | int out_index = (k + c*b); 40 | for(i = 0; i < w*h; ++i){ 41 | int in_index = i + h*w*(k + b*c); 42 | in_delta[in_index] += out_delta[out_index] / (w*h); 43 | } 44 | } 45 | 46 | extern "C" void forward_avgpool_layer_gpu(avgpool_layer layer, network net) 47 | { 48 | size_t n = layer.c*layer.batch; 49 | 50 | forward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.input_gpu, layer.output_gpu); 51 | check_error(cudaPeekAtLastError()); 52 | } 53 | 54 | extern "C" void backward_avgpool_layer_gpu(avgpool_layer layer, network net) 55 | { 56 | size_t n = layer.c*layer.batch; 57 | 58 | backward_avgpool_layer_kernel<<>>(n, layer.w, layer.h, layer.c, net.delta_gpu, layer.delta_gpu); 59 | check_error(cudaPeekAtLastError()); 60 | } 61 | 62 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | #include 4 | #include 5 | #include "darknet.h" 6 | #include "list.h" 7 | 8 | #define TIME(a) \ 9 | do { \ 10 | double start = what_time_is_it_now(); \ 11 | a; \ 12 | printf("%s took: %f seconds\n", #a, what_time_is_it_now() - start); \ 13 | } while (0) 14 | 15 | #define TWO_PI 6.2831853071795864769252866f 16 | 17 | double what_time_is_it_now(); 18 | void shuffle(void *arr, size_t n, size_t size); 19 | void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections); 20 | void free_ptrs(void **ptrs, int n); 21 | int alphanum_to_int(char c); 22 | char int_to_alphanum(int i); 23 | int read_int(int fd); 24 | void write_int(int fd, int n); 25 | void read_all(int fd, char *buffer, size_t bytes); 26 | void write_all(int fd, char *buffer, size_t bytes); 27 | int read_all_fail(int fd, char *buffer, size_t bytes); 28 | int write_all_fail(int fd, char *buffer, size_t bytes); 29 | void find_replace(char *str, char *orig, char *rep, char *output); 30 | void malloc_error(); 31 | void file_error(char *s); 32 | void strip(char *s); 33 | void strip_char(char *s, char bad); 34 | list *split_str(char *s, char delim); 35 | char *fgetl(FILE *fp); 36 | list *parse_csv_line(char *line); 37 | char *copy_string(char *s); 38 | int count_fields(char *line); 39 | float *parse_fields(char *line, int n); 40 | void translate_array(float *a, int n, float s); 41 | float constrain(float min, float max, float a); 42 | int constrain_int(int a, int min, int max); 43 | float rand_scale(float s); 44 | int rand_int(int min, int max); 45 | void mean_arrays(float **a, int n, int els, float *avg); 46 | float dist_array(float *a, float *b, int n, int sub); 47 | float **one_hot_encode(float *a, int n, int k); 48 | float sec(clock_t clocks); 49 | void print_statistics(float *a, int n); 50 | int int_index(int *a, int val, int n); 51 | 52 | char* concat(char *s1, char *s2); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/dropout_layer.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include 5 | #include 6 | 7 | dropout_layer make_dropout_layer(int batch, int inputs, float probability) 8 | { 9 | dropout_layer l = {0}; 10 | l.type = DROPOUT; 11 | l.probability = probability; 12 | l.inputs = inputs; 13 | l.outputs = inputs; 14 | l.batch = batch; 15 | l.rand = calloc(inputs*batch, sizeof(float)); 16 | l.scale = 1./(1.-probability); 17 | l.forward = forward_dropout_layer; 18 | l.backward = backward_dropout_layer; 19 | #ifdef GPU 20 | l.forward_gpu = forward_dropout_layer_gpu; 21 | l.backward_gpu = backward_dropout_layer_gpu; 22 | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); 23 | #endif 24 | fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs); 25 | return l; 26 | } 27 | 28 | void resize_dropout_layer(dropout_layer *l, int inputs) 29 | { 30 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 31 | #ifdef GPU 32 | cuda_free(l->rand_gpu); 33 | 34 | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); 35 | #endif 36 | } 37 | 38 | void forward_dropout_layer(dropout_layer l, network net) 39 | { 40 | int i; 41 | if (!net.train) return; 42 | for(i = 0; i < l.batch * l.inputs; ++i){ 43 | //printf("i = %d; total = %d\n",i, l.batch * l.inputs); 44 | float r = rand_uniform(0, 1); 45 | l.rand[i] = r; 46 | if(r < l.probability) net.input[i] = 0; 47 | else net.input[i] *= l.scale; 48 | } 49 | } 50 | 51 | void backward_dropout_layer(dropout_layer l, network net) 52 | { 53 | int i; 54 | if(!net.delta) return; 55 | for(i = 0; i < l.batch * l.inputs; ++i){ 56 | float r = l.rand[i]; 57 | if(r < l.probability) net.delta[i] = 0; 58 | else net.delta[i] *= l.scale; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /client_side_trustzone/host/Makefile_changed_bak: -------------------------------------------------------------------------------- 1 | CC ?= $(CROSS_COMPILE)gcc 2 | LD ?= $(CROSS_COMPILE)ld 3 | AR ?= $(CROSS_COMPILE)ar 4 | NM ?= $(CROSS_COMPILE)nm 5 | OBJCOPY ?= $(CROSS_COMPILE)objcopy 6 | OBJDUMP ?= $(CROSS_COMPILE)objdump 7 | READELF ?= $(CROSS_COMPILE)readelf 8 | 9 | VPATH=./src/:./examples 10 | ALIB=libdarknet.a 11 | ARFLAGS=rcs 12 | LDFLAGS= -lm -pthread 13 | 14 | OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o iseg_layer.o image_opencv.o 15 | EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o instance-segmenter.o darknet.o 16 | #EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) 17 | #OBJS = $(addprefix $(OBJDIR), $(OBJ)) 18 | EXECOBJ = EXECOBJA 19 | OBJS = OBJ 20 | DEPS = $(wildcard src/*.h) Makefile include/darknet.h 21 | 22 | #EXECOBJ += main.o 23 | 24 | CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include 25 | CFLAGS += -Iinclude/ -Isrc/ 26 | 27 | #Add/link other required libraries here 28 | LDADD += -lteec -L$(TEEC_EXPORT)/lib 29 | 30 | BINARY = optee_example_realtest # that might be the EXEC 31 | 32 | .PHONY: all 33 | all: $(ALIB) $(BINARY) 34 | 35 | $(BINARY): $(EXECOBJ) $(ALIB) 36 | $(CC) $(CFLAGS) -g $^ -o $@ $(LDFLAGS) $(ALIB) 37 | # $(CC) -o $@ $< $(LDADD) 38 | 39 | $(ALIB): $(OBJS) 40 | $(AR) $(ARFLAGS) $@ $^ 41 | 42 | .PHONY: clean 43 | clean: 44 | rm -f $(OBJS) $(BINARY) 45 | 46 | %.o: %.c $(DEPS) 47 | $(CC) $(CFLAGS) -c $< -o $@ 48 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | #include 4 | #include 5 | #include "darknet.h" 6 | #include "list.h" 7 | 8 | #define TIME(a) \ 9 | do { \ 10 | double start = what_time_is_it_now(); \ 11 | a; \ 12 | printf("%s took: %f seconds\n", #a, what_time_is_it_now() - start); \ 13 | } while (0) 14 | 15 | #define TWO_PI 6.2831853071795864769252866f 16 | 17 | double what_time_is_it_now(); 18 | void shuffle(void *arr, size_t n, size_t size); 19 | void sorta_shuffle(void *arr, size_t n, size_t size, size_t sections); 20 | void free_ptrs(void **ptrs, int n); 21 | int alphanum_to_int(char c); 22 | char int_to_alphanum(int i); 23 | int read_int(int fd); 24 | void write_int(int fd, int n); 25 | void read_all(int fd, char *buffer, size_t bytes); 26 | void write_all(int fd, char *buffer, size_t bytes); 27 | int read_all_fail(int fd, char *buffer, size_t bytes); 28 | int write_all_fail(int fd, char *buffer, size_t bytes); 29 | void find_replace(char *str, char *orig, char *rep, char *output); 30 | void malloc_error(); 31 | void file_error(char *s); 32 | void strip(char *s); 33 | void strip_char(char *s, char bad); 34 | list *split_str(char *s, char delim); 35 | char *fgetl(FILE *fp); 36 | list *parse_csv_line(char *line); 37 | char *copy_string(char *s); 38 | int count_fields(char *line); 39 | float *parse_fields(char *line, int n); 40 | void translate_array(float *a, int n, float s); 41 | float constrain(float min, float max, float a); 42 | int constrain_int(int a, int min, int max); 43 | float rand_scale(float s); 44 | int rand_int(int min, int max); 45 | void mean_arrays(float **a, int n, int els, float *avg); 46 | float dist_array(float *a, float *b, int n, int sub); 47 | float **one_hot_encode(float *a, int n, int k); 48 | float sec(clock_t clocks); 49 | void print_statistics(float *a, int n); 50 | int int_index(int *a, int val, int n); 51 | 52 | float rand_normal_ms(float mu, float sigma); 53 | char* concat(char *s1, char *s2); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/activation_layer.c: -------------------------------------------------------------------------------- 1 | #include "activation_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include "gemm.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation) 13 | { 14 | layer l = {0}; 15 | l.type = ACTIVE; 16 | 17 | l.inputs = inputs; 18 | l.outputs = inputs; 19 | l.batch=batch; 20 | 21 | l.output = calloc(batch*inputs, sizeof(float*)); 22 | l.delta = calloc(batch*inputs, sizeof(float*)); 23 | 24 | l.forward = forward_activation_layer; 25 | l.backward = backward_activation_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_activation_layer_gpu; 28 | l.backward_gpu = backward_activation_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | l.activation = activation; 34 | fprintf(stderr, "Activation Layer: %d inputs\n", inputs); 35 | return l; 36 | } 37 | 38 | void forward_activation_layer(layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, l.activation); 42 | } 43 | 44 | void backward_activation_layer(layer l, network net) 45 | { 46 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 47 | copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1); 48 | } 49 | 50 | #ifdef GPU 51 | 52 | void forward_activation_layer_gpu(layer l, network net) 53 | { 54 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 55 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 56 | } 57 | 58 | void backward_activation_layer_gpu(layer l, network net) 59 | { 60 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 61 | copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1); 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/activation_layer.c: -------------------------------------------------------------------------------- 1 | #include "activation_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | #include "blas.h" 5 | #include "gemm.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_activation_layer(int batch, int inputs, ACTIVATION activation) 13 | { 14 | layer l = {0}; 15 | l.type = ACTIVE; 16 | 17 | l.inputs = inputs; 18 | l.outputs = inputs; 19 | l.batch=batch; 20 | 21 | l.output = calloc(batch*inputs, sizeof(float*)); 22 | l.delta = calloc(batch*inputs, sizeof(float*)); 23 | 24 | l.forward = forward_activation_layer; 25 | l.backward = backward_activation_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_activation_layer_gpu; 28 | l.backward_gpu = backward_activation_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 32 | #endif 33 | l.activation = activation; 34 | fprintf(stderr, "Activation Layer: %d inputs\n", inputs); 35 | return l; 36 | } 37 | 38 | void forward_activation_layer(layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, l.activation); 42 | } 43 | 44 | void backward_activation_layer(layer l, network net) 45 | { 46 | gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta); 47 | copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1); 48 | } 49 | 50 | #ifdef GPU 51 | 52 | void forward_activation_layer_gpu(layer l, network net) 53 | { 54 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 55 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation); 56 | } 57 | 58 | void backward_activation_layer_gpu(layer l, network net) 59 | { 60 | gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu); 61 | copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1); 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/l2norm_layer.c: -------------------------------------------------------------------------------- 1 | #include "l2norm_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_l2norm_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "l2norm %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = L2NORM; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.output = calloc(inputs*batch, sizeof(float)); 21 | l.scales = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | 24 | l.forward = forward_l2norm_layer; 25 | l.backward = backward_l2norm_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_l2norm_layer_gpu; 28 | l.backward_gpu = backward_l2norm_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.scales_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 33 | #endif 34 | return l; 35 | } 36 | 37 | void forward_l2norm_layer(const layer l, network net) 38 | { 39 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 40 | l2normalize_cpu(l.output, l.scales, l.batch, l.out_c, l.out_w*l.out_h); 41 | } 42 | 43 | void backward_l2norm_layer(const layer l, network net) 44 | { 45 | //axpy_cpu(l.inputs*l.batch, 1, l.scales, 1, l.delta, 1); 46 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 47 | } 48 | 49 | #ifdef GPU 50 | 51 | void forward_l2norm_layer_gpu(const layer l, network net) 52 | { 53 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 54 | l2normalize_gpu(l.output_gpu, l.scales_gpu, l.batch, l.out_c, l.out_w*l.out_h); 55 | } 56 | 57 | void backward_l2norm_layer_gpu(const layer l, network net) 58 | { 59 | axpy_gpu(l.batch*l.inputs, 1, l.scales_gpu, 1, l.delta_gpu, 1); 60 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/l2norm_layer.c: -------------------------------------------------------------------------------- 1 | #include "l2norm_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_l2norm_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "l2norm %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = L2NORM; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.output = calloc(inputs*batch, sizeof(float)); 21 | l.scales = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | 24 | l.forward = forward_l2norm_layer; 25 | l.backward = backward_l2norm_layer; 26 | #ifdef GPU 27 | l.forward_gpu = forward_l2norm_layer_gpu; 28 | l.backward_gpu = backward_l2norm_layer_gpu; 29 | 30 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 31 | l.scales_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 33 | #endif 34 | return l; 35 | } 36 | 37 | void forward_l2norm_layer(const layer l, network net) 38 | { 39 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 40 | l2normalize_cpu(l.output, l.scales, l.batch, l.out_c, l.out_w*l.out_h); 41 | } 42 | 43 | void backward_l2norm_layer(const layer l, network net) 44 | { 45 | //axpy_cpu(l.inputs*l.batch, 1, l.scales, 1, l.delta, 1); 46 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 47 | } 48 | 49 | #ifdef GPU 50 | 51 | void forward_l2norm_layer_gpu(const layer l, network net) 52 | { 53 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 54 | l2normalize_gpu(l.output_gpu, l.scales_gpu, l.batch, l.out_c, l.out_w*l.out_h); 55 | } 56 | 57 | void backward_l2norm_layer_gpu(const layer l, network net) 58 | { 59 | axpy_gpu(l.batch*l.inputs, 1, l.scales_gpu, 1, l.delta_gpu, 1); 60 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/dropout_layer.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer.h" 2 | #include "utils.h" 3 | #include "cuda.h" 4 | 5 | #include "parser.h" 6 | 7 | #include 8 | #include 9 | 10 | dropout_layer make_dropout_layer(int batch, int inputs, float probability) 11 | { 12 | dropout_layer l = {0}; 13 | l.type = DROPOUT; 14 | l.probability = probability; 15 | l.inputs = inputs; 16 | l.outputs = inputs; 17 | l.batch = batch; 18 | l.rand = calloc(inputs*batch, sizeof(float)); 19 | l.scale = 1./(1.-probability); 20 | l.forward = forward_dropout_layer; 21 | l.backward = backward_dropout_layer; 22 | #ifdef GPU 23 | l.forward_gpu = forward_dropout_layer_gpu; 24 | l.backward_gpu = backward_dropout_layer_gpu; 25 | l.rand_gpu = cuda_make_array(l.rand, inputs*batch); 26 | #endif 27 | 28 | if(count_global <= partition_point1 || count_global > partition_point2){ 29 | fprintf(stderr, "dropout p = %.2f %4d -> %4d\n", probability, inputs, inputs); 30 | }else{ 31 | fprintf(stderr, "dropout_TA p = %.2f %4d -> %4d\n", probability, inputs, inputs); 32 | } 33 | return l; 34 | } 35 | 36 | void resize_dropout_layer(dropout_layer *l, int inputs) 37 | { 38 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 39 | #ifdef GPU 40 | cuda_free(l->rand_gpu); 41 | 42 | l->rand_gpu = cuda_make_array(l->rand, inputs*l->batch); 43 | #endif 44 | } 45 | 46 | void forward_dropout_layer(dropout_layer l, network net) 47 | { 48 | int i; 49 | if (!net.train) return; 50 | for(i = 0; i < l.batch * l.inputs; ++i){ 51 | //printf("i = %d; total = %d\n",i, l.batch * l.inputs); 52 | float r = rand_uniform(0, 1); 53 | l.rand[i] = r; 54 | if(r < l.probability) net.input[i] = 0; 55 | else net.input[i] *= l.scale; 56 | } 57 | } 58 | 59 | void backward_dropout_layer(dropout_layer l, network net) 60 | { 61 | int i; 62 | if(!net.delta) return; 63 | for(i = 0; i < l.batch * l.inputs; ++i){ 64 | float r = l.rand[i]; 65 | if(r < l.probability) net.delta[i] = 0; 66 | else net.delta[i] *= l.scale; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/avgpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer.h" 2 | #include "cuda.h" 3 | #include 4 | 5 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c) 6 | { 7 | fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c); 8 | avgpool_layer l = {0}; 9 | l.type = AVGPOOL; 10 | l.batch = batch; 11 | l.h = h; 12 | l.w = w; 13 | l.c = c; 14 | l.out_w = 1; 15 | l.out_h = 1; 16 | l.out_c = c; 17 | l.outputs = l.out_c; 18 | l.inputs = h*w*c; 19 | int output_size = l.outputs * batch; 20 | l.output = calloc(output_size, sizeof(float)); 21 | l.delta = calloc(output_size, sizeof(float)); 22 | l.forward = forward_avgpool_layer; 23 | l.backward = backward_avgpool_layer; 24 | #ifdef GPU 25 | l.forward_gpu = forward_avgpool_layer_gpu; 26 | l.backward_gpu = backward_avgpool_layer_gpu; 27 | l.output_gpu = cuda_make_array(l.output, output_size); 28 | l.delta_gpu = cuda_make_array(l.delta, output_size); 29 | #endif 30 | return l; 31 | } 32 | 33 | void resize_avgpool_layer(avgpool_layer *l, int w, int h) 34 | { 35 | l->w = w; 36 | l->h = h; 37 | l->inputs = h*w*l->c; 38 | } 39 | 40 | void forward_avgpool_layer(const avgpool_layer l, network net) 41 | { 42 | int b,i,k; 43 | 44 | for(b = 0; b < l.batch; ++b){ 45 | for(k = 0; k < l.c; ++k){ 46 | int out_index = k + b*l.c; 47 | l.output[out_index] = 0; 48 | for(i = 0; i < l.h*l.w; ++i){ 49 | int in_index = i + l.h*l.w*(k + b*l.c); 50 | l.output[out_index] += net.input[in_index]; 51 | } 52 | l.output[out_index] /= l.h*l.w; 53 | } 54 | } 55 | } 56 | 57 | void backward_avgpool_layer(const avgpool_layer l, network net) 58 | { 59 | int b,i,k; 60 | 61 | for(b = 0; b < l.batch; ++b){ 62 | for(k = 0; k < l.c; ++k){ 63 | int out_index = k + b*l.c; 64 | for(i = 0; i < l.h*l.w; ++i){ 65 | int in_index = i + l.h*l.w*(k + b*l.c); 66 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 67 | } 68 | } 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /client_side_trustzone/host/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= $(CROSS_COMPILE)gcc 2 | LD ?= $(CROSS_COMPILE)ld 3 | AR ?= $(CROSS_COMPILE)ar 4 | NM ?= $(CROSS_COMPILE)nm 5 | OBJCOPY ?= $(CROSS_COMPILE)objcopy 6 | OBJDUMP ?= $(CROSS_COMPILE)objdump 7 | READELF ?= $(CROSS_COMPILE)readelf 8 | 9 | #ALIB=libdarknet.a 10 | #ARFLAGS = -rcs 11 | LDFLAGS= -lm -pthread 12 | 13 | OBJSCR = src/ 14 | EXECSCR = examples/ 15 | 16 | OBJ = tcp.transfer.o diffprivate.o gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o depthwise_convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o iseg_layer.o 17 | 18 | EXECOBJ = captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o instance-segmenter.o darknet.o 19 | 20 | OBJS = $(addprefix $(OBJSCR), $(OBJ)) 21 | EXECOBJS = $(addprefix $(EXECSCR), $(EXECOBJ)) 22 | 23 | EXECMAIN = main.o 24 | 25 | CFLAGS += -Wall -I../ta/include -I$(TEEC_EXPORT)/include -I./include 26 | CFLAGS += -Iinclude/ -Isrc/ -Iexamples/ -I./src/ -I./examples/ 27 | 28 | #Add/link other required libraries here 29 | LDADD += -lteec -L$(TEEC_EXPORT)/lib 30 | #DEPS = $(wildcard src/*.h) inlcude/darknet.h inlcude/main.h 31 | 32 | BINARY = optee_example_darknetp 33 | 34 | .PHONY: all 35 | #all: $(ALIB) $(BINARY) 36 | all: $(BINARY) 37 | 38 | $(BINARY): $(EXECMAIN) $(EXECOBJS) $(OBJS) 39 | #$(CC) $(CFLAGS) -g $^ -o $@ $(LDFLAGS) $(LDADD) $(ALIB) 40 | $(CC) -o $@ $^ $(LDFLAGS) $(LDADD) 41 | 42 | #$(ALIB): $(OBJS) 43 | # $(AR) $(ARFLAGS) $@ $^ 44 | 45 | $(OBJSCR)%.o: $(OBJSCR)%.c 46 | $(CC) $(CFLAGS) -c $< -o $@ 47 | 48 | $(EXECSCR)%.o: $(EXECSCR)%.c 49 | $(CC) $(CFLAGS) -c $< -o $@ 50 | 51 | %.o: %.c 52 | $(CC) $(CFLAGS) -c $< -o $@ 53 | 54 | .PHONY: clean 55 | clean: 56 | rm -f $(OBJS) $(EXECOBJS) $(EXECMAIN) $(BINARY) 57 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/depthwise_convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef DEPTHWISE_CONVOLUTIONAL_LAYER_H 2 | #define DEPTHWISE_CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer depthwise_convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_depthwise_convolutional_layer_gpu(depthwise_convolutional_layer layer, network net); 14 | void backward_depthwise_convolutional_layer_gpu(depthwise_convolutional_layer layer, network net); 15 | void update_depthwise_convolutional_layer_gpu(depthwise_convolutional_layer layer, update_args a); 16 | 17 | void push_depthwise_convolutional_layer(depthwise_convolutional_layer layer); 18 | void pull_depthwise_convolutional_layer(depthwise_convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_depthwise_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | depthwise_convolutional_layer make_depthwise_convolutional_layer(int batch, int h, int w, int c, int size, int stride, int padding, ACTIVATION activation, int batch_normalize); 29 | void resize_depthwise_convolutional_layer(depthwise_convolutional_layer *layer, int w, int h); 30 | void forward_depthwise_convolutional_layer(const depthwise_convolutional_layer layer, network net); 31 | void update_depthwise_convolutional_layer(depthwise_convolutional_layer layer, update_args a); 32 | 33 | 34 | void denormalize_depthwise_convolutional_layer(depthwise_convolutional_layer l); 35 | void backward_depthwise_convolutional_layer(depthwise_convolutional_layer layer, network net); 36 | 37 | void add_bias(float *output, float *biases, int batch, int n, int size); 38 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 39 | 40 | 41 | 42 | int depthwise_convolutional_out_height(depthwise_convolutional_layer layer); 43 | int depthwise_convolutional_out_width(depthwise_convolutional_layer layer); 44 | 45 | #endif 46 | 47 | 48 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/softmax_layer_TA.c: -------------------------------------------------------------------------------- 1 | #include "softmax_layer_TA.h" 2 | #include "blas_TA.h" 3 | #include "math_TA.h" 4 | #include "utils_TA.h" 5 | 6 | //#include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | 15 | softmax_layer_TA make_softmax_layer_TA_new(int batch, int inputs, int groups, float temperature, int w, int h, int c, int spatial, int noloss) 16 | { 17 | assert(inputs%groups == 0); 18 | //IMSG("softmax_TA %4d\n", inputs); 19 | softmax_layer_TA l = {0}; 20 | l.type = SOFTMAX_TA; 21 | l.batch = batch; 22 | l.groups = groups; 23 | 24 | l.inputs = inputs; 25 | l.outputs = inputs; 26 | l.loss = calloc(inputs*batch, sizeof(float)); 27 | l.output = calloc(inputs*batch, sizeof(float)); 28 | l.delta = calloc(inputs*batch, sizeof(float)); 29 | l.cost = calloc(1, sizeof(float)); 30 | 31 | l.temperature = temperature; 32 | l.w = w; 33 | l.h = h; 34 | l.c = c; 35 | l.spatial = spatial; 36 | l.noloss = noloss; 37 | 38 | l.forward_TA = forward_softmax_layer_TA; 39 | l.backward_TA = backward_softmax_layer_TA; 40 | 41 | return l; 42 | } 43 | 44 | 45 | 46 | void forward_softmax_layer_TA(const softmax_layer_TA l, network_TA net) 47 | { 48 | if(l.softmax_tree){ 49 | int i; 50 | int count = 0; 51 | for (i = 0; i < l.softmax_tree->groups; ++i) { 52 | int group_size = l.softmax_tree->group_size[i]; 53 | softmax_cpu_TA(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count); 54 | count += group_size; 55 | } 56 | } else { 57 | softmax_cpu_TA(net.input, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output); 58 | } 59 | 60 | if(net.truth && !l.noloss){ 61 | softmax_x_ent_cpu_TA(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 62 | l.cost[0] = sum_array_TA(l.loss, l.batch*l.inputs); 63 | } 64 | } 65 | 66 | 67 | void backward_softmax_layer_TA(const softmax_layer_TA l, network_TA net) 68 | { 69 | axpy_cpu_TA(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 70 | } 71 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | #include 4 | 5 | #include "darknet.h" 6 | #include "matrix.h" 7 | #include "list.h" 8 | #include "image.h" 9 | #include "tree.h" 10 | 11 | static inline float distance_from_edge(int x, int max) 12 | { 13 | int dx = (max/2) - x; 14 | if (dx < 0) dx = -dx; 15 | dx = (max/2) + 1 - dx; 16 | dx *= 2; 17 | float dist = (float)dx/max; 18 | if (dist > 1) dist = 1; 19 | return dist; 20 | } 21 | void load_data_blocking(load_args args); 22 | 23 | 24 | void print_letters(float *pred, int n); 25 | data load_data_captcha(char **paths, int n, int m, int k, int w, int h); 26 | data load_data_captcha_encode(char **paths, int n, int m, int w, int h); 27 | data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure); 28 | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 29 | matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 30 | data load_data_super(char **paths, int n, int m, int w, int h, int scale); 31 | data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 32 | data load_data_regression(char **paths, int n, int m, int classes, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 33 | data load_go(char *filename); 34 | 35 | 36 | data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h); 37 | 38 | void get_random_batch(data d, int n, float *X, float *y); 39 | data get_data_part(data d, int part, int total); 40 | data get_random_data(data d, int num); 41 | data load_categorical_data_csv(char *filename, int target, int k); 42 | void normalize_data_rows(data d); 43 | void scale_data_rows(data d, float s); 44 | void translate_data_rows(data d, float s); 45 | void randomize_data(data d); 46 | data *split_data(data d, int part, int total); 47 | data concat_datas(data *d, int n); 48 | void fill_truth(char *path, char **labels, int k, float *truth); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/data.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_H 2 | #define DATA_H 3 | #include 4 | 5 | #include "darknet.h" 6 | #include "matrix.h" 7 | #include "list.h" 8 | #include "image.h" 9 | #include "tree.h" 10 | 11 | static inline float distance_from_edge(int x, int max) 12 | { 13 | int dx = (max/2) - x; 14 | if (dx < 0) dx = -dx; 15 | dx = (max/2) + 1 - dx; 16 | dx *= 2; 17 | float dist = (float)dx/max; 18 | if (dist > 1) dist = 1; 19 | return dist; 20 | } 21 | void load_data_blocking(load_args args); 22 | 23 | 24 | void print_letters(float *pred, int n); 25 | data load_data_captcha(char **paths, int n, int m, int k, int w, int h); 26 | data load_data_captcha_encode(char **paths, int n, int m, int w, int h); 27 | data load_data_detection(int n, char **paths, int m, int w, int h, int boxes, int classes, float jitter, float hue, float saturation, float exposure); 28 | data load_data_tag(char **paths, int n, int m, int k, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 29 | matrix load_image_augment_paths(char **paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 30 | data load_data_super(char **paths, int n, int m, int w, int h, int scale); 31 | data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure, int center); 32 | data load_data_regression(char **paths, int n, int m, int classes, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure); 33 | data load_go(char *filename); 34 | 35 | 36 | data load_data_writing(char **paths, int n, int m, int w, int h, int out_w, int out_h); 37 | 38 | void get_random_batch(data d, int n, float *X, float *y); 39 | data get_data_part(data d, int part, int total); 40 | data get_random_data(data d, int num); 41 | data load_categorical_data_csv(char *filename, int target, int k); 42 | void normalize_data_rows(data d); 43 | void scale_data_rows(data d, float s); 44 | void translate_data_rows(data d, float s); 45 | void randomize_data(data d); 46 | data *split_data(data d, int part, int total); 47 | data concat_datas(data *d, int n); 48 | void fill_truth(char *path, char **labels, int k, float *truth); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/logistic_layer.c: -------------------------------------------------------------------------------- 1 | #include "logistic_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_logistic_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "logistic x entropy %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = LOGXENT; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.loss = calloc(inputs*batch, sizeof(float)); 21 | l.output = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | l.cost = calloc(1, sizeof(float)); 24 | 25 | l.forward = forward_logistic_layer; 26 | l.backward = backward_logistic_layer; 27 | #ifdef GPU 28 | l.forward_gpu = forward_logistic_layer_gpu; 29 | l.backward_gpu = backward_logistic_layer_gpu; 30 | 31 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 33 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 34 | #endif 35 | return l; 36 | } 37 | 38 | void forward_logistic_layer(const layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, LOGISTIC); 42 | if(net.truth){ 43 | logistic_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 44 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 45 | } 46 | } 47 | 48 | void backward_logistic_layer(const layer l, network net) 49 | { 50 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 51 | } 52 | 53 | #ifdef GPU 54 | 55 | void forward_logistic_layer_gpu(const layer l, network net) 56 | { 57 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 58 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, LOGISTIC); 59 | if(net.truth){ 60 | logistic_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 61 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 62 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 63 | } 64 | } 65 | 66 | void backward_logistic_layer_gpu(const layer l, network net) 67 | { 68 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/logistic_layer.c: -------------------------------------------------------------------------------- 1 | #include "logistic_layer.h" 2 | #include "activations.h" 3 | #include "blas.h" 4 | #include "cuda.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | layer make_logistic_layer(int batch, int inputs) 13 | { 14 | fprintf(stderr, "logistic x entropy %4d\n", inputs); 15 | layer l = {0}; 16 | l.type = LOGXENT; 17 | l.batch = batch; 18 | l.inputs = inputs; 19 | l.outputs = inputs; 20 | l.loss = calloc(inputs*batch, sizeof(float)); 21 | l.output = calloc(inputs*batch, sizeof(float)); 22 | l.delta = calloc(inputs*batch, sizeof(float)); 23 | l.cost = calloc(1, sizeof(float)); 24 | 25 | l.forward = forward_logistic_layer; 26 | l.backward = backward_logistic_layer; 27 | #ifdef GPU 28 | l.forward_gpu = forward_logistic_layer_gpu; 29 | l.backward_gpu = backward_logistic_layer_gpu; 30 | 31 | l.output_gpu = cuda_make_array(l.output, inputs*batch); 32 | l.loss_gpu = cuda_make_array(l.loss, inputs*batch); 33 | l.delta_gpu = cuda_make_array(l.delta, inputs*batch); 34 | #endif 35 | return l; 36 | } 37 | 38 | void forward_logistic_layer(const layer l, network net) 39 | { 40 | copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1); 41 | activate_array(l.output, l.outputs*l.batch, LOGISTIC); 42 | if(net.truth){ 43 | logistic_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss); 44 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 45 | } 46 | } 47 | 48 | void backward_logistic_layer(const layer l, network net) 49 | { 50 | axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1); 51 | } 52 | 53 | #ifdef GPU 54 | 55 | void forward_logistic_layer_gpu(const layer l, network net) 56 | { 57 | copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1); 58 | activate_array_gpu(l.output_gpu, l.outputs*l.batch, LOGISTIC); 59 | if(net.truth){ 60 | logistic_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu); 61 | cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs); 62 | l.cost[0] = sum_array(l.loss, l.batch*l.inputs); 63 | } 64 | } 65 | 66 | void backward_logistic_layer_gpu(const layer l, network net) 67 | { 68 | axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1); 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_H 2 | #define CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_convolutional_layer_gpu(convolutional_layer layer, network net); 14 | void backward_convolutional_layer_gpu(convolutional_layer layer, network net); 15 | void update_convolutional_layer_gpu(convolutional_layer layer, update_args a); 16 | 17 | void push_convolutional_layer(convolutional_layer layer); 18 | void pull_convolutional_layer(convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam); 29 | void resize_convolutional_layer(convolutional_layer *layer, int w, int h); 30 | void forward_convolutional_layer(const convolutional_layer layer, network net); 31 | void update_convolutional_layer(convolutional_layer layer, update_args a); 32 | image *visualize_convolutional_layer(convolutional_layer layer, char *window, image *prev_weights); 33 | void binarize_weights(float *weights, int n, int size, float *binary); 34 | void swap_binary(convolutional_layer *l); 35 | void binarize_weights2(float *weights, int n, int size, char *binary, float *scales); 36 | 37 | void backward_convolutional_layer(convolutional_layer layer, network net); 38 | 39 | void add_bias(float *output, float *biases, int batch, int n, int size); 40 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 41 | 42 | image get_convolutional_image(convolutional_layer layer); 43 | image get_convolutional_delta(convolutional_layer layer); 44 | image get_convolutional_weight(convolutional_layer layer, int i); 45 | 46 | int convolutional_out_height(convolutional_layer layer); 47 | int convolutional_out_width(convolutional_layer layer); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/avgpool_layer.c: -------------------------------------------------------------------------------- 1 | #include "avgpool_layer.h" 2 | #include "cuda.h" 3 | #include "parser.h" 4 | 5 | #include 6 | 7 | avgpool_layer make_avgpool_layer(int batch, int w, int h, int c) 8 | { 9 | 10 | if(count_global <= partition_point1 || count_global > partition_point2){ 11 | fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c); 12 | }else{ 13 | fprintf(stderr, "avg_TA %4d x%4d x%4d -> %4d\n", w, h, c, c); 14 | } 15 | avgpool_layer l = {0}; 16 | l.type = AVGPOOL; 17 | l.batch = batch; 18 | l.h = h; 19 | l.w = w; 20 | l.c = c; 21 | l.out_w = 1; 22 | l.out_h = 1; 23 | l.out_c = c; 24 | l.outputs = l.out_c; 25 | l.inputs = h*w*c; 26 | int output_size = l.outputs * batch; 27 | l.output = calloc(output_size, sizeof(float)); 28 | l.delta = calloc(output_size, sizeof(float)); 29 | l.forward = forward_avgpool_layer; 30 | l.backward = backward_avgpool_layer; 31 | #ifdef GPU 32 | l.forward_gpu = forward_avgpool_layer_gpu; 33 | l.backward_gpu = backward_avgpool_layer_gpu; 34 | l.output_gpu = cuda_make_array(l.output, output_size); 35 | l.delta_gpu = cuda_make_array(l.delta, output_size); 36 | #endif 37 | return l; 38 | } 39 | 40 | void resize_avgpool_layer(avgpool_layer *l, int w, int h) 41 | { 42 | l->w = w; 43 | l->h = h; 44 | l->inputs = h*w*l->c; 45 | } 46 | 47 | void forward_avgpool_layer(const avgpool_layer l, network net) 48 | { 49 | int b,i,k; 50 | 51 | for(b = 0; b < l.batch; ++b){ 52 | for(k = 0; k < l.c; ++k){ 53 | int out_index = k + b*l.c; 54 | l.output[out_index] = 0; 55 | for(i = 0; i < l.h*l.w; ++i){ 56 | int in_index = i + l.h*l.w*(k + b*l.c); 57 | l.output[out_index] += net.input[in_index]; 58 | } 59 | l.output[out_index] /= l.h*l.w; 60 | } 61 | } 62 | } 63 | 64 | void backward_avgpool_layer(const avgpool_layer l, network net) 65 | { 66 | int b,i,k; 67 | 68 | for(b = 0; b < l.batch; ++b){ 69 | for(k = 0; k < l.c; ++k){ 70 | int out_index = k + b*l.c; 71 | for(i = 0; i < l.h*l.w; ++i){ 72 | int in_index = i + l.h*l.w*(k + b*l.c); 73 | net.delta[in_index] += l.delta[out_index] / (l.h*l.w); 74 | } 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/dropout_layer_TA.c: -------------------------------------------------------------------------------- 1 | #include "dropout_layer_TA.h" 2 | #include "utils_TA.h" 3 | #include "math_TA.h" 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | dropout_layer_TA make_dropout_layer_TA_new(int batch, int inputs, float probability, int w, int h, int c, int netnum) 12 | { 13 | dropout_layer_TA l = {0}; 14 | l.type = DROPOUT_TA; 15 | l.probability = probability; 16 | l.inputs = inputs; 17 | l.outputs = inputs; 18 | l.batch = batch; 19 | l.rand = calloc(inputs*batch, sizeof(float)); 20 | l.scale = 1./(1.-probability); 21 | 22 | l.netnum = netnum; 23 | 24 | l.output = malloc(sizeof(float) * inputs*batch); 25 | l.delta = malloc(sizeof(float) * inputs*batch); 26 | 27 | l.forward_TA = forward_dropout_layer_TA_new; 28 | l.backward_TA = backward_dropout_layer_TA_new; 29 | l.w = w; 30 | l.h = h; 31 | l.c = c; 32 | 33 | char prob[20]; 34 | ftoa(probability,prob,3); 35 | //IMSG("dropout_TA p = %s %4d -> %4d\n", prob, inputs, inputs); 36 | return l; 37 | } 38 | 39 | void resize_dropout_layer_TA(dropout_layer_TA *l, int inputs) 40 | { 41 | l->rand = realloc(l->rand, l->inputs*l->batch*sizeof(float)); 42 | } 43 | 44 | void forward_dropout_layer_TA_new(dropout_layer_TA l, network_TA net) 45 | { 46 | int i; 47 | if (!net.train) return; 48 | 49 | float *pter; 50 | if(l.netnum == 0){ 51 | for(i = 0; i < l.batch * l.inputs; ++i){ 52 | l.output[i] = net.input[i]; 53 | } 54 | 55 | pter = l.output; 56 | }else{ 57 | pter = net.input; 58 | } 59 | 60 | for(i = 0; i < l.batch * l.inputs; ++i){ 61 | //printf("i = %d; total = %d\n",i, l.batch * l.inputs); 62 | float r = rand_uniform_TA(0, 1); 63 | l.rand[i] = r; 64 | if(r < l.probability) pter[i] = 0; 65 | else pter[i] *= l.scale; 66 | } 67 | } 68 | 69 | void backward_dropout_layer_TA_new(dropout_layer_TA l, network_TA net) 70 | { 71 | int i; 72 | if(!net.delta) return; 73 | 74 | float *pter; 75 | if(l.netnum == 0){ 76 | pter = l.delta; 77 | }else{ 78 | pter = net.delta; 79 | } 80 | 81 | for(i = 0; i < l.batch * l.inputs; ++i){ 82 | float r = l.rand[i]; 83 | if(r < l.probability) pter[i] = 0; 84 | else pter[i] *= l.scale; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/convolutional_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTIONAL_LAYER_H 2 | #define CONVOLUTIONAL_LAYER_H 3 | 4 | #include "cuda.h" 5 | #include "image.h" 6 | #include "activations.h" 7 | #include "layer.h" 8 | #include "network.h" 9 | 10 | typedef layer convolutional_layer; 11 | 12 | #ifdef GPU 13 | void forward_convolutional_layer_gpu(convolutional_layer layer, network net); 14 | void backward_convolutional_layer_gpu(convolutional_layer layer, network net); 15 | void update_convolutional_layer_gpu(convolutional_layer layer, update_args a); 16 | 17 | void push_convolutional_layer(convolutional_layer layer); 18 | void pull_convolutional_layer(convolutional_layer layer); 19 | 20 | void add_bias_gpu(float *output, float *biases, int batch, int n, int size); 21 | void backward_bias_gpu(float *bias_updates, float *delta, int batch, int n, int size); 22 | void adam_update_gpu(float *w, float *d, float *m, float *v, float B1, float B2, float eps, float decay, float rate, int n, int batch, int t); 23 | #ifdef CUDNN 24 | void cudnn_convolutional_setup(layer *l); 25 | #endif 26 | #endif 27 | 28 | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int groups, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam); 29 | void resize_convolutional_layer(convolutional_layer *layer, int w, int h); 30 | void forward_convolutional_layer(const convolutional_layer layer, network net); 31 | void update_convolutional_layer(convolutional_layer layer, update_args a); 32 | image *visualize_convolutional_layer(convolutional_layer layer, char *window, image *prev_weights); 33 | void binarize_weights(float *weights, int n, int size, float *binary); 34 | void swap_binary(convolutional_layer *l); 35 | void binarize_weights2(float *weights, int n, int size, char *binary, float *scales); 36 | 37 | void backward_convolutional_layer(convolutional_layer layer, network net); 38 | 39 | void add_bias(float *output, float *biases, int batch, int n, int size); 40 | void backward_bias(float *bias_updates, float *delta, int batch, int n, int size); 41 | 42 | image get_convolutional_image(convolutional_layer layer); 43 | image get_convolutional_delta(convolutional_layer layer); 44 | image get_convolutional_weight(convolutional_layer layer, int i); 45 | 46 | int convolutional_out_height(convolutional_layer layer); 47 | int convolutional_out_width(convolutional_layer layer); 48 | 49 | void backward_bias_diff(float *bias_updates, float *delta, int batch, int n, int size); 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/im2col_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "im2col.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void im2col_gpu_kernel(const int n, const float* data_im, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_col) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | int w_out = index % width_col; 22 | int h_index = index / width_col; 23 | int h_out = h_index % height_col; 24 | int channel_in = h_index / height_col; 25 | int channel_out = channel_in * ksize * ksize; 26 | int h_in = h_out * stride - pad; 27 | int w_in = w_out * stride - pad; 28 | float* data_col_ptr = data_col; 29 | data_col_ptr += (channel_out * height_col + h_out) * width_col + w_out; 30 | const float* data_im_ptr = data_im; 31 | data_im_ptr += (channel_in * height + h_in) * width + w_in; 32 | for (int i = 0; i < ksize; ++i) { 33 | for (int j = 0; j < ksize; ++j) { 34 | int h = h_in + i; 35 | int w = w_in + j; 36 | 37 | *data_col_ptr = (h >= 0 && w >= 0 && h < height && w < width) ? 38 | data_im_ptr[i * width + j] : 0; 39 | 40 | //*data_col_ptr = data_im_ptr[ii * width + jj]; 41 | 42 | data_col_ptr += height_col * width_col; 43 | } 44 | } 45 | } 46 | } 47 | 48 | void im2col_gpu(float *im, 49 | int channels, int height, int width, 50 | int ksize, int stride, int pad, float *data_col){ 51 | // We are going to launch channels * height_col * width_col kernels, each 52 | // kernel responsible for copying a single-channel grid. 53 | int height_col = (height + 2 * pad - ksize) / stride + 1; 54 | int width_col = (width + 2 * pad - ksize) / stride + 1; 55 | int num_kernels = channels * height_col * width_col; 56 | im2col_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 57 | BLOCK>>>( 58 | num_kernels, im, height, width, ksize, pad, 59 | stride, height_col, 60 | width_col, data_col); 61 | } 62 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/im2col_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "im2col.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void im2col_gpu_kernel(const int n, const float* data_im, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_col) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | int w_out = index % width_col; 22 | int h_index = index / width_col; 23 | int h_out = h_index % height_col; 24 | int channel_in = h_index / height_col; 25 | int channel_out = channel_in * ksize * ksize; 26 | int h_in = h_out * stride - pad; 27 | int w_in = w_out * stride - pad; 28 | float* data_col_ptr = data_col; 29 | data_col_ptr += (channel_out * height_col + h_out) * width_col + w_out; 30 | const float* data_im_ptr = data_im; 31 | data_im_ptr += (channel_in * height + h_in) * width + w_in; 32 | for (int i = 0; i < ksize; ++i) { 33 | for (int j = 0; j < ksize; ++j) { 34 | int h = h_in + i; 35 | int w = w_in + j; 36 | 37 | *data_col_ptr = (h >= 0 && w >= 0 && h < height && w < width) ? 38 | data_im_ptr[i * width + j] : 0; 39 | 40 | //*data_col_ptr = data_im_ptr[ii * width + jj]; 41 | 42 | data_col_ptr += height_col * width_col; 43 | } 44 | } 45 | } 46 | } 47 | 48 | void im2col_gpu(float *im, 49 | int channels, int height, int width, 50 | int ksize, int stride, int pad, float *data_col){ 51 | // We are going to launch channels * height_col * width_col kernels, each 52 | // kernel responsible for copying a single-channel grid. 53 | int height_col = (height + 2 * pad - ksize) / stride + 1; 54 | int width_col = (width + 2 * pad - ksize) / stride + 1; 55 | int num_kernels = channels * height_col * width_col; 56 | im2col_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 57 | BLOCK>>>( 58 | num_kernels, im, height, width, ksize, pad, 59 | stride, height_col, 60 | width_col, data_col); 61 | } 62 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/col2im_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "col2im.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void col2im_gpu_kernel(const int n, const float* data_col, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_im) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | float val = 0; 22 | int w = index % width + pad; 23 | int h = (index / width) % height + pad; 24 | int c = index / (width * height); 25 | // compute the start and end of the output 26 | int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1; 27 | int w_col_end = min(w / stride + 1, width_col); 28 | int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1; 29 | int h_col_end = min(h / stride + 1, height_col); 30 | // equivalent implementation 31 | int offset = 32 | (c * ksize * ksize + h * ksize + w) * height_col * width_col; 33 | int coeff_h_col = (1 - stride * ksize * height_col) * width_col; 34 | int coeff_w_col = (1 - stride * height_col * width_col); 35 | for (int h_col = h_col_start; h_col < h_col_end; ++h_col) { 36 | for (int w_col = w_col_start; w_col < w_col_end; ++w_col) { 37 | val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col]; 38 | } 39 | } 40 | data_im[index] += val; 41 | } 42 | } 43 | 44 | void col2im_gpu(float *data_col, 45 | int channels, int height, int width, 46 | int ksize, int stride, int pad, float *data_im){ 47 | // We are going to launch channels * height_col * width_col kernels, each 48 | // kernel responsible for copying a single-channel grid. 49 | int height_col = (height + 2 * pad - ksize) / stride + 1; 50 | int width_col = (width + 2 * pad - ksize) / stride + 1; 51 | int num_kernels = channels * height * width; 52 | col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 53 | BLOCK>>>( 54 | num_kernels, data_col, height, width, ksize, pad, 55 | stride, height_col, 56 | width_col, data_im); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/col2im_kernels.cu: -------------------------------------------------------------------------------- 1 | #include "cuda_runtime.h" 2 | #include "curand.h" 3 | #include "cublas_v2.h" 4 | 5 | extern "C" { 6 | #include "col2im.h" 7 | #include "cuda.h" 8 | } 9 | 10 | // src: https://github.com/BVLC/caffe/blob/master/src/caffe/util/im2col.cu 11 | // You may also want to read: https://github.com/BVLC/caffe/blob/master/LICENSE 12 | 13 | __global__ void col2im_gpu_kernel(const int n, const float* data_col, 14 | const int height, const int width, const int ksize, 15 | const int pad, 16 | const int stride, 17 | const int height_col, const int width_col, 18 | float *data_im) { 19 | int index = blockIdx.x*blockDim.x+threadIdx.x; 20 | for(; index < n; index += blockDim.x*gridDim.x){ 21 | float val = 0; 22 | int w = index % width + pad; 23 | int h = (index / width) % height + pad; 24 | int c = index / (width * height); 25 | // compute the start and end of the output 26 | int w_col_start = (w < ksize) ? 0 : (w - ksize) / stride + 1; 27 | int w_col_end = min(w / stride + 1, width_col); 28 | int h_col_start = (h < ksize) ? 0 : (h - ksize) / stride + 1; 29 | int h_col_end = min(h / stride + 1, height_col); 30 | // equivalent implementation 31 | int offset = 32 | (c * ksize * ksize + h * ksize + w) * height_col * width_col; 33 | int coeff_h_col = (1 - stride * ksize * height_col) * width_col; 34 | int coeff_w_col = (1 - stride * height_col * width_col); 35 | for (int h_col = h_col_start; h_col < h_col_end; ++h_col) { 36 | for (int w_col = w_col_start; w_col < w_col_end; ++w_col) { 37 | val += data_col[offset + h_col * coeff_h_col + w_col * coeff_w_col]; 38 | } 39 | } 40 | data_im[index] += val; 41 | } 42 | } 43 | 44 | void col2im_gpu(float *data_col, 45 | int channels, int height, int width, 46 | int ksize, int stride, int pad, float *data_im){ 47 | // We are going to launch channels * height_col * width_col kernels, each 48 | // kernel responsible for copying a single-channel grid. 49 | int height_col = (height + 2 * pad - ksize) / stride + 1; 50 | int width_col = (width + 2 * pad - ksize) / stride + 1; 51 | int num_kernels = channels * height * width; 52 | col2im_gpu_kernel<<<(num_kernels+BLOCK-1)/BLOCK, 53 | BLOCK>>>( 54 | num_kernels, data_col, height, width, ksize, pad, 55 | stride, height_col, 56 | width_col, data_im); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "box.h" 10 | #include "darknet.h" 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #ifdef OPENCV 17 | void *open_video_stream(const char *f, int c, int w, int h, int fps); 18 | image get_image_from_stream(void *p); 19 | image load_image_cv(char *filename, int channels); 20 | int show_image_cv(image im, const char* name, int ms); 21 | #endif 22 | 23 | float get_color(int c, int x, int max); 24 | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); 25 | void draw_bbox(image a, box bbox, int w, float r, float g, float b); 26 | void write_label(image a, int r, int c, image *characters, char *string, float *rgb); 27 | image image_distance(image a, image b); 28 | void scale_image(image m, float s); 29 | image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect); 30 | image random_crop_image(image im, int w, int h); 31 | image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h); 32 | augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h); 33 | void letterbox_image_into(image im, int w, int h, image boxed); 34 | image resize_max(image im, int max); 35 | void translate_image(image m, float s); 36 | void embed_image(image source, image dest, int dx, int dy); 37 | void place_image(image im, int w, int h, int dx, int dy, image canvas); 38 | void saturate_image(image im, float sat); 39 | void exposure_image(image im, float sat); 40 | void distort_image(image im, float hue, float sat, float val); 41 | void saturate_exposure_image(image im, float sat, float exposure); 42 | void rgb_to_hsv(image im); 43 | void hsv_to_rgb(image im); 44 | void yuv_to_rgb(image im); 45 | void rgb_to_yuv(image im); 46 | 47 | 48 | image collapse_image_layers(image source, int border); 49 | image collapse_images_horz(image *ims, int n); 50 | image collapse_images_vert(image *ims, int n); 51 | 52 | void show_image_normalized(image im, const char *name); 53 | void show_images(image *ims, int n, char *window); 54 | void show_image_layers(image p, char *name); 55 | void show_image_collapsed(image p, char *name); 56 | 57 | void print_image(image m); 58 | 59 | image make_empty_image(int w, int h, int c); 60 | void copy_image_into(image src, image dest); 61 | 62 | image get_image_layer(image m, int l); 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "box.h" 10 | #include "darknet.h" 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #ifdef OPENCV 17 | void *open_video_stream(const char *f, int c, int w, int h, int fps); 18 | image get_image_from_stream(void *p); 19 | image load_image_cv(char *filename, int channels); 20 | int show_image_cv(image im, const char* name, int ms); 21 | #endif 22 | 23 | float get_color(int c, int x, int max); 24 | void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b); 25 | void draw_bbox(image a, box bbox, int w, float r, float g, float b); 26 | void write_label(image a, int r, int c, image *characters, char *string, float *rgb); 27 | image image_distance(image a, image b); 28 | void scale_image(image m, float s); 29 | image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect); 30 | image random_crop_image(image im, int w, int h); 31 | image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h); 32 | augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h); 33 | void letterbox_image_into(image im, int w, int h, image boxed); 34 | image resize_max(image im, int max); 35 | void translate_image(image m, float s); 36 | void embed_image(image source, image dest, int dx, int dy); 37 | void place_image(image im, int w, int h, int dx, int dy, image canvas); 38 | void saturate_image(image im, float sat); 39 | void exposure_image(image im, float sat); 40 | void distort_image(image im, float hue, float sat, float val); 41 | void saturate_exposure_image(image im, float sat, float exposure); 42 | void rgb_to_hsv(image im); 43 | void hsv_to_rgb(image im); 44 | void yuv_to_rgb(image im); 45 | void rgb_to_yuv(image im); 46 | 47 | 48 | image collapse_image_layers(image source, int border); 49 | image collapse_images_horz(image *ims, int n); 50 | image collapse_images_vert(image *ims, int n); 51 | 52 | void show_image_normalized(image im, const char *name); 53 | void show_images(image *ims, int n, char *window); 54 | void show_image_layers(image p, char *name); 55 | void show_image_collapsed(image p, char *name); 56 | 57 | void print_image(image m); 58 | 59 | image make_empty_image(int w, int h, int c); 60 | void copy_image_into(image src, image dest); 61 | 62 | image get_image_layer(image m, int l); 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /server_side_sgx/host/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) Open Enclave SDK contributors. 2 | # Licensed under the MIT License. 3 | 4 | include ../../config.mk 5 | 6 | CFLAGS=$(shell pkg-config oehost-$(COMPILER) --cflags) 7 | LDFLAGS=$(shell pkg-config oehost-$(COMPILER) --libs) 8 | INCDIR=$(shell pkg-config oehost-$(COMPILER) --variable=includedir) 9 | 10 | COMMON= -Iinclude/ -Isrc/ 11 | MPFLAGS= -lm -pthread 12 | OBJSCR = src/ 13 | EXECSCR = examples/ 14 | 15 | OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o iseg_layer.o 16 | 17 | CFILE=gemm.c utils.c cuda.c deconvolutional_layer.c convolutional_layer.c list.c image.c activations.c im2col.c col2im.c blas.c crop_layer.c dropout_layer.c maxpool_layer.c softmax_layer.c data.c matrix.c network.c connected_layer.c cost_layer.c parser.c option_list.c detection_layer.c route_layer.c upsample_layer.c box.c normalization_layer.c avgpool_layer.c layer.c local_layer.c shortcut_layer.c logistic_layer.c activation_layer.c rnn_layer.c gru_layer.c crnn_layer.c demo.c batchnorm_layer.c region_layer.c reorg_layer.c tree.c lstm_layer.c l2norm_layer.c yolo_layer.c iseg_layer.c 18 | 19 | EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o server.o coco.o yolo.o detector.o nightmare.o instance-segmenter.o darknet.o 20 | 21 | EXECFILE=captcha.c lsd.c super.c art.c tag.c cifar.c go.c rnn.c segmenter.c regressor.c classifier.c server.c coco.c yolo.c detector.c nightmare.c instance-segmenter.c darknet.c 22 | 23 | CFILES = $(addprefix $(OBJSCR), $(CFILE)) 24 | EXECFILES = $(addprefix $(EXECSCR), $(EXECFILE)) 25 | 26 | build: 27 | @ echo "Compilers used: $(CC), $(CXX)" 28 | oeedger8r ../secure_aggregation.edl --untrusted \ 29 | --search-path $(INCDIR) \ 30 | --search-path $(INCDIR)/openenclave/edl/sgx 31 | 32 | $(CC) -g -c $(COMMON) $(CFLAGS) $(CFILES) 33 | $(CC) -g -c $(COMMON) $(CFLAGS) $(EXECFILES) 34 | $(CC) -g -c $(CFLAGS) secure_aggregation_u.c 35 | $(CC) -o secure_aggregation_host secure_aggregation_u.o $(OBJ) $(EXECOBJA) $(MPFLAGS) $(LDFLAGS) 36 | 37 | clean: 38 | rm -f secure_aggregation_host secure_aggregation_u.o secure_aggregation_u.c secure_aggregation_u.h secure_aggregation_args.h $(OBJ) $(EXECOBJA) 39 | -------------------------------------------------------------------------------- /server_side_sgx/host/examples/swag.c: -------------------------------------------------------------------------------- 1 | #include "darknet.h" 2 | #include 3 | 4 | void train_swag(char *cfgfile, char *weightfile) 5 | { 6 | char *train_images = "data/voc.0712.trainval"; 7 | char *backup_directory = "/home/pjreddie/backup/"; 8 | srand(time(0)); 9 | char *base = basecfg(cfgfile); 10 | printf("%s\n", base); 11 | float avg_loss = -1; 12 | network net = parse_network_cfg(cfgfile); 13 | if(weightfile){ 14 | load_weights(&net, weightfile); 15 | } 16 | printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); 17 | int imgs = net.batch*net.subdivisions; 18 | int i = *net.seen/imgs; 19 | data train, buffer; 20 | 21 | layer l = net.layers[net.n - 1]; 22 | 23 | int side = l.side; 24 | int classes = l.classes; 25 | float jitter = l.jitter; 26 | 27 | list *plist = get_paths(train_images); 28 | //int N = plist->size; 29 | char **paths = (char **)list_to_array(plist); 30 | 31 | load_args args = {0}; 32 | args.w = net.w; 33 | args.h = net.h; 34 | args.paths = paths; 35 | args.n = imgs; 36 | args.m = plist->size; 37 | args.classes = classes; 38 | args.jitter = jitter; 39 | args.num_boxes = side; 40 | args.d = &buffer; 41 | args.type = REGION_DATA; 42 | 43 | pthread_t load_thread = load_data_in_thread(args); 44 | clock_t time; 45 | //while(i*imgs < N*120){ 46 | while(get_current_batch(net) < net.max_batches){ 47 | i += 1; 48 | time=clock(); 49 | pthread_join(load_thread, 0); 50 | train = buffer; 51 | load_thread = load_data_in_thread(args); 52 | 53 | printf("Loaded: %lf seconds\n", sec(clock()-time)); 54 | 55 | time=clock(); 56 | float loss = train_network(net, train); 57 | if (avg_loss < 0) avg_loss = loss; 58 | avg_loss = avg_loss*.9 + loss*.1; 59 | 60 | printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs); 61 | if(i%1000==0 || i == 600){ 62 | char buff[256]; 63 | sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i); 64 | save_weights(net, buff); 65 | } 66 | free_data(train); 67 | } 68 | char buff[256]; 69 | sprintf(buff, "%s/%s_final.weights", backup_directory, base); 70 | save_weights(net, buff); 71 | } 72 | 73 | void run_swag(int argc, char **argv) 74 | { 75 | if(argc < 4){ 76 | fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); 77 | return; 78 | } 79 | 80 | char *cfg = argv[3]; 81 | char *weights = (argc > 4) ? argv[4] : 0; 82 | if(0==strcmp(argv[2], "train")) train_swag(cfg, weights); 83 | } 84 | -------------------------------------------------------------------------------- /server_side_sgx/fl_script/fl_tee_standard_noss.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | . /opt/openenclave/share/openenclave/openenclaverc 4 | 5 | IP_CLIENT_1=192.168.0.22 6 | PASSWORD_CLIENT_1=fl-tee-system 7 | 8 | NUM_ROUNDS=2 9 | NUM_CLIENTS=2 10 | 11 | PP_START=6 12 | PP_END=8 13 | 14 | DATASET=mnist 15 | MODEL=lenet 16 | 17 | SERVER_APP_DIR="/media/vincent/program/my_oesamples/secure-aggregation-SGX/" 18 | SERVER_AVERAGED_DIR="./results/${DATASET}/averaged_standard_noss/" 19 | SERVER_UPDATES_DIR="./results/${DATASET}/client_updates_standard_noss/" 20 | CLIENT_MODEL_DIR="/root/models/${DATASET}/" 21 | DM="${DATASET}_${MODEL}" 22 | 23 | STARTER="./results/${DATASET}/${DM}_pp${PP_START}${PP_END}.weights" 24 | 25 | 26 | echo "============= initialization =============" 27 | cd ${SERVER_APP_DIR} 28 | 29 | rm -rf ${SERVER_UPDATES_DIR} 30 | mkdir ${SERVER_UPDATES_DIR} 31 | 32 | rm -rf ${SERVER_AVERAGED_DIR} 33 | mkdir ${SERVER_AVERAGED_DIR} 34 | 35 | cp ${STARTER} "${SERVER_AVERAGED_DIR}${DM}_averaged_r0.weights" 36 | 37 | for ((r=1;r<=NUM_ROUNDS;r++)) 38 | do 39 | echo "============= round ${r} =============" 40 | for ((c=1;c<=NUM_CLIENTS;c++)) 41 | do 42 | echo "============= copy weights server -> client ${c} =============" 43 | rp=$((r-1)) 44 | time sshpass -p ${PASSWORD_CLIENT_1} scp "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights" "root@${IP_CLIENT_1}:${CLIENT_MODEL_DIR}${DM}_global.weights" 45 | filesize=$(stat --format=%s "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights") 46 | echo "${filesize} Bytes" 47 | sleep 3s 48 | 49 | echo "============= ssh to the client and local training =============" 50 | if [ ${PP_START} == 999 ] 51 | then 52 | # training without TEEs 53 | time sshpass -p ${PASSWORD_CLIENT_1} ssh "root@${IP_CLIENT_1}" darknetp classifier train "cfg/${DATASET}.dataset" "cfg/${DM}.cfg" "${CLIENT_MODEL_DIR}${DM}_global.weights" 54 | else 55 | # training with TEEs 56 | time sshpass -p ${PASSWORD_CLIENT_1} ssh "root@${IP_CLIENT_1}" darknetp classifier train -pp_start ${PP_START} -pp_end ${PP_END} "cfg/${DATASET}.dataset" "cfg/${DM}.cfg" "${CLIENT_MODEL_DIR}${DM}_global.weights" 57 | fi 58 | sleep 3s 59 | 60 | echo "============= copy weights client ${c} -> server =============" 61 | time sshpass -p ${PASSWORD_CLIENT_1} scp "root@${IP_CLIENT_1}:${CLIENT_MODEL_DIR}${DM}.weights" "${SERVER_UPDATES_DIR}${DM}_c${c}.weights" 62 | filesize=$(stat --format=%s "${SERVER_UPDATES_DIR}${DM}_c${c}.weights") 63 | echo "${filesize} Bytes" 64 | sleep 3s 65 | done 66 | 67 | echo "============= averaging =============" 68 | time host/secure_aggregation_host server model_aggregation -pp_start ${PP_START} -pp_end ${PP_END} "cfg/${DM}.cfg" ${SERVER_UPDATES_DIR} 69 | mv "${SERVER_UPDATES_DIR}${DM}_averaged.weights" "${SERVER_AVERAGED_DIR}${DM}_averaged_r${r}.weights" 70 | done 71 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/cost_layer_TA.c: -------------------------------------------------------------------------------- 1 | #include "cost_layer_TA.h" 2 | 3 | #include "utils_TA.h" 4 | #include "blas_TA.h" 5 | #include "math_TA.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | COST_TYPE_TA get_cost_type_TA(char *s) 15 | { 16 | if (strcmp(s, "seg")==0) return SEG_TA; 17 | if (strcmp(s, "sse")==0) return SSE_TA; 18 | if (strcmp(s, "masked")==0) return MASKED_TA; 19 | if (strcmp(s, "smooth")==0) return SMOOTH_TA; 20 | if (strcmp(s, "L1")==0) return L1_TA; 21 | if (strcmp(s, "wgan")==0) return WGAN_TA; 22 | IMSG("Couldn't find cost type %s, going with SSE\n", s); 23 | return SSE_TA; 24 | } 25 | 26 | 27 | char *get_cost_string_TA(COST_TYPE_TA a) 28 | { 29 | switch(a){ 30 | case SEG_TA: 31 | return "seg"; 32 | case SSE_TA: 33 | return "sse"; 34 | case MASKED_TA: 35 | return "masked"; 36 | case SMOOTH_TA: 37 | return "smooth"; 38 | case L1_TA: 39 | return "L1"; 40 | case WGAN_TA: 41 | return "wgan"; 42 | } 43 | return "sse"; 44 | } 45 | 46 | 47 | cost_layer_TA make_cost_layer_TA_new(int batch, int inputs, COST_TYPE_TA cost_type, float scale, float ratio, float noobject_scale, float thresh) 48 | { 49 | //IMSG("cost_TA %4d\n", inputs); 50 | cost_layer_TA l = {0}; 51 | l.type = COST_TA; 52 | 53 | l.scale = scale; 54 | l.batch = batch; 55 | l.inputs = inputs; 56 | l.outputs = inputs; 57 | l.cost_type = cost_type; 58 | l.delta = calloc(inputs*batch, sizeof(float)); 59 | l.output = calloc(inputs*batch, sizeof(float)); 60 | l.cost = calloc(1, sizeof(float)); 61 | 62 | l.scale = scale; 63 | l.ratio = ratio; 64 | l.noobject_scale = noobject_scale; 65 | l.thresh = thresh; 66 | 67 | l.forward_TA = forward_cost_layer_TA; 68 | l.backward_TA = backward_cost_layer_TA; 69 | 70 | return l; 71 | } 72 | 73 | 74 | 75 | void forward_cost_layer_TA(cost_layer_TA l, network_TA net) 76 | { 77 | if (!net.truth) return; 78 | if(l.cost_type == MASKED_TA){ 79 | int i; 80 | for(i = 0; i < l.batch*l.inputs; ++i){ 81 | if(net.truth[i] == SECRET_NUM_TA) net.input[i] = SECRET_NUM_TA; 82 | } 83 | } 84 | if(l.cost_type == SMOOTH_TA){ 85 | smooth_l1_cpu_TA(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 86 | }else if(l.cost_type == L1_TA){ 87 | l1_cpu_TA(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 88 | } else { 89 | l2_cpu_TA(l.batch*l.inputs, net.input, net.truth, l.delta, l.output); 90 | } 91 | l.cost[0] = sum_array_TA(l.output, l.batch*l.inputs); 92 | 93 | } 94 | 95 | 96 | 97 | void backward_cost_layer_TA(const cost_layer_TA l, network_TA net) 98 | { 99 | axpy_cpu_TA(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1); 100 | } 101 | -------------------------------------------------------------------------------- /server_side_sgx/enclave/aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | // #define the macros below to 1/0 to enable/disable the mode of operation. 7 | // 8 | // CBC enables AES encryption in CBC-mode of operation. 9 | // CTR enables encryption in counter-mode. 10 | // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | #ifndef CTR 22 | #define CTR 1 23 | #endif 24 | 25 | 26 | #define AES128 1 27 | //#define AES192 1 28 | //#define AES256 1 29 | 30 | #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only 31 | 32 | #if defined(AES256) && (AES256 == 1) 33 | #define AES_KEYLEN 32 34 | #define AES_keyExpSize 240 35 | #elif defined(AES192) && (AES192 == 1) 36 | #define AES_KEYLEN 24 37 | #define AES_keyExpSize 208 38 | #else 39 | #define AES_KEYLEN 16 // Key length in bytes 40 | #define AES_keyExpSize 176 41 | #endif 42 | 43 | struct AES_ctx 44 | { 45 | uint8_t RoundKey[AES_keyExpSize]; 46 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 47 | uint8_t Iv[AES_BLOCKLEN]; 48 | #endif 49 | }; 50 | 51 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 52 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 53 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 54 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 55 | #endif 56 | 57 | #if defined(ECB) && (ECB == 1) 58 | // buffer size is exactly AES_BLOCKLEN bytes; 59 | // you need only AES_init_ctx as IV is not used in ECB 60 | // NB: ECB is considered insecure for most uses 61 | void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); 62 | void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); 63 | 64 | #endif // #if defined(ECB) && (ECB == !) 65 | 66 | 67 | #if defined(CBC) && (CBC == 1) 68 | // buffer size MUST be mutile of AES_BLOCKLEN; 69 | // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 70 | // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() 71 | // no IV should ever be reused with the same key 72 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 73 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 74 | 75 | #endif // #if defined(CBC) && (CBC == 1) 76 | 77 | 78 | #if defined(CTR) && (CTR == 1) 79 | 80 | // Same function for encrypting as for decrypting. 81 | // IV is incremented for every block, and used after encryption as XOR-compliment for output 82 | // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 83 | // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() 84 | // no IV should ever be reused with the same key 85 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 86 | 87 | #endif // #if defined(CTR) && (CTR == 1) 88 | 89 | 90 | #endif //_AES_H_ 91 | -------------------------------------------------------------------------------- /client_side_trustzone/ta/include/aes_TA.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | // #define the macros below to 1/0 to enable/disable the mode of operation. 7 | // 8 | // CBC enables AES encryption in CBC-mode of operation. 9 | // CTR enables encryption in counter-mode. 10 | // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 1 19 | #endif 20 | 21 | #ifndef CTR 22 | #define CTR 1 23 | #endif 24 | 25 | 26 | #define AES128 1 27 | //#define AES192 1 28 | //#define AES256 1 29 | 30 | #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only 31 | 32 | #if defined(AES256) && (AES256 == 1) 33 | #define AES_KEYLEN 32 34 | #define AES_keyExpSize 240 35 | #elif defined(AES192) && (AES192 == 1) 36 | #define AES_KEYLEN 24 37 | #define AES_keyExpSize 208 38 | #else 39 | #define AES_KEYLEN 16 // Key length in bytes 40 | #define AES_keyExpSize 176 41 | #endif 42 | 43 | struct AES_ctx 44 | { 45 | uint8_t RoundKey[AES_keyExpSize]; 46 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 47 | uint8_t Iv[AES_BLOCKLEN]; 48 | #endif 49 | }; 50 | 51 | void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key); 52 | #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1)) 53 | void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv); 54 | void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv); 55 | #endif 56 | 57 | #if defined(ECB) && (ECB == 1) 58 | // buffer size is exactly AES_BLOCKLEN bytes; 59 | // you need only AES_init_ctx as IV is not used in ECB 60 | // NB: ECB is considered insecure for most uses 61 | void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf); 62 | void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf); 63 | 64 | #endif // #if defined(ECB) && (ECB == !) 65 | 66 | 67 | #if defined(CBC) && (CBC == 1) 68 | // buffer size MUST be mutile of AES_BLOCKLEN; 69 | // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 70 | // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv() 71 | // no IV should ever be reused with the same key 72 | void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 73 | void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 74 | 75 | #endif // #if defined(CBC) && (CBC == 1) 76 | 77 | 78 | #if defined(CTR) && (CTR == 1) 79 | 80 | // Same function for encrypting as for decrypting. 81 | // IV is incremented for every block, and used after encryption as XOR-compliment for output 82 | // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme 83 | // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv() 84 | // no IV should ever be reused with the same key 85 | void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length); 86 | 87 | #endif // #if defined(CTR) && (CTR == 1) 88 | 89 | 90 | #endif //_AES_H_ 91 | -------------------------------------------------------------------------------- /server_side_sgx/host/src/activations.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATIONS_H 2 | #define ACTIVATIONS_H 3 | #include "darknet.h" 4 | #include "cuda.h" 5 | #include "math.h" 6 | 7 | ACTIVATION get_activation(char *s); 8 | 9 | char *get_activation_string(ACTIVATION a); 10 | float activate(float x, ACTIVATION a); 11 | float gradient(float x, ACTIVATION a); 12 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); 13 | void activate_array(float *x, const int n, const ACTIVATION a); 14 | #ifdef GPU 15 | void activate_array_gpu(float *x, int n, ACTIVATION a); 16 | void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); 17 | #endif 18 | 19 | static inline float stair_activate(float x) 20 | { 21 | int n = floor(x); 22 | if (n%2 == 0) return floor(x/2.); 23 | else return (x - n) + floor(x/2.); 24 | } 25 | static inline float hardtan_activate(float x) 26 | { 27 | if (x < -1) return -1; 28 | if (x > 1) return 1; 29 | return x; 30 | } 31 | static inline float linear_activate(float x){return x;} 32 | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} 33 | static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;} 34 | static inline float relu_activate(float x){return x*(x>0);} 35 | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} 36 | static inline float selu_activate(float x){return (x >= 0)*1.0507*x + (x < 0)*1.0507*1.6732*(exp(x)-1);} 37 | static inline float relie_activate(float x){return (x>0) ? x : .01*x;} 38 | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} 39 | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} 40 | static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);} 41 | static inline float plse_activate(float x) 42 | { 43 | if(x < -4) return .01 * (x + 4); 44 | if(x > 4) return .01 * (x - 4) + 1; 45 | return .125*x + .5; 46 | } 47 | 48 | static inline float lhtan_activate(float x) 49 | { 50 | if(x < 0) return .001*x; 51 | if(x > 1) return .001*(x-1) + 1; 52 | return x; 53 | } 54 | static inline float lhtan_gradient(float x) 55 | { 56 | if(x > 0 && x < 1) return 1; 57 | return .001; 58 | } 59 | 60 | static inline float hardtan_gradient(float x) 61 | { 62 | if (x > -1 && x < 1) return 1; 63 | return 0; 64 | } 65 | static inline float linear_gradient(float x){return 1;} 66 | static inline float logistic_gradient(float x){return (1-x)*x;} 67 | static inline float loggy_gradient(float x) 68 | { 69 | float y = (x+1.)/2.; 70 | return 2*(1-y)*y; 71 | } 72 | static inline float stair_gradient(float x) 73 | { 74 | if (floor(x) == x) return 0; 75 | return 1; 76 | } 77 | static inline float relu_gradient(float x){return (x>0);} 78 | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} 79 | static inline float selu_gradient(float x){return (x >= 0)*1.0507 + (x < 0)*(x + 1.0507*1.6732);} 80 | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} 81 | static inline float ramp_gradient(float x){return (x>0)+.1;} 82 | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} 83 | static inline float tanh_gradient(float x){return 1-x*x;} 84 | static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;} 85 | 86 | #endif 87 | 88 | -------------------------------------------------------------------------------- /client_side_trustzone/host/src/activations.h: -------------------------------------------------------------------------------- 1 | #ifndef ACTIVATIONS_H 2 | #define ACTIVATIONS_H 3 | #include "darknet.h" 4 | #include "cuda.h" 5 | #include "math.h" 6 | 7 | ACTIVATION get_activation(char *s); 8 | 9 | char *get_activation_string(ACTIVATION a); 10 | float activate(float x, ACTIVATION a); 11 | float gradient(float x, ACTIVATION a); 12 | void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); 13 | void activate_array(float *x, const int n, const ACTIVATION a); 14 | #ifdef GPU 15 | void activate_array_gpu(float *x, int n, ACTIVATION a); 16 | void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); 17 | #endif 18 | 19 | static inline float stair_activate(float x) 20 | { 21 | int n = floor(x); 22 | if (n%2 == 0) return floor(x/2.); 23 | else return (x - n) + floor(x/2.); 24 | } 25 | static inline float hardtan_activate(float x) 26 | { 27 | if (x < -1) return -1; 28 | if (x > 1) return 1; 29 | return x; 30 | } 31 | static inline float linear_activate(float x){return x;} 32 | static inline float logistic_activate(float x){return 1./(1. + exp(-x));} 33 | static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;} 34 | static inline float relu_activate(float x){return x*(x>0);} 35 | static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);} 36 | static inline float selu_activate(float x){return (x >= 0)*1.0507*x + (x < 0)*1.0507*1.6732*(exp(x)-1);} 37 | static inline float relie_activate(float x){return (x>0) ? x : .01*x;} 38 | static inline float ramp_activate(float x){return x*(x>0)+.1*x;} 39 | static inline float leaky_activate(float x){return (x>0) ? x : .1*x;} 40 | static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);} 41 | static inline float plse_activate(float x) 42 | { 43 | if(x < -4) return .01 * (x + 4); 44 | if(x > 4) return .01 * (x - 4) + 1; 45 | return .125*x + .5; 46 | } 47 | 48 | static inline float lhtan_activate(float x) 49 | { 50 | if(x < 0) return .001*x; 51 | if(x > 1) return .001*(x-1) + 1; 52 | return x; 53 | } 54 | static inline float lhtan_gradient(float x) 55 | { 56 | if(x > 0 && x < 1) return 1; 57 | return .001; 58 | } 59 | 60 | static inline float hardtan_gradient(float x) 61 | { 62 | if (x > -1 && x < 1) return 1; 63 | return 0; 64 | } 65 | static inline float linear_gradient(float x){return 1;} 66 | static inline float logistic_gradient(float x){return (1-x)*x;} 67 | static inline float loggy_gradient(float x) 68 | { 69 | float y = (x+1.)/2.; 70 | return 2*(1-y)*y; 71 | } 72 | static inline float stair_gradient(float x) 73 | { 74 | if (floor(x) == x) return 0; 75 | return 1; 76 | } 77 | static inline float relu_gradient(float x){return (x>0);} 78 | static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);} 79 | static inline float selu_gradient(float x){return (x >= 0)*1.0507 + (x < 0)*(x + 1.0507*1.6732);} 80 | static inline float relie_gradient(float x){return (x>0) ? 1 : .01;} 81 | static inline float ramp_gradient(float x){return (x>0)+.1;} 82 | static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;} 83 | static inline float tanh_gradient(float x){return 1-x*x;} 84 | static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;} 85 | 86 | #endif 87 | 88 | -------------------------------------------------------------------------------- /server_side_sgx/fl_script/fl_tee_transfer_once.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | . /opt/openenclave/share/openenclave/openenclaverc 4 | 5 | IP_CLIENT_1=192.168.0.22 6 | PASSWORD_CLIENT_1=fl-tee-system 7 | 8 | NUM_ROUNDS=2 9 | NUM_CLIENTS=2 10 | 11 | PP_START=6 12 | PP_END=8 13 | 14 | DATASET=mnist 15 | MODEL=lenet 16 | 17 | SERVER_APP_DIR="/media/vincent/program/my_oesamples/secure-aggregation-SGX/" 18 | SERVER_AVERAGED_DIR="./results/${DATASET}/averaged_transfer_once/" 19 | SERVER_UPDATES_DIR="./results/${DATASET}/client_updates_transfer_once/" 20 | CLIENT_MODEL_DIR="/root/models/${DATASET}/" 21 | DM="${DATASET}_${MODEL}" 22 | 23 | STARTER_REE="./results/${DATASET}/${DM}_pp${PP_START}${PP_END}.weights_ree" 24 | STARTER_TEE="./results/${DATASET}/${DM}_pp${PP_START}${PP_END}.weights_tee" 25 | 26 | 27 | echo "============= initialization =============" 28 | cd ${SERVER_APP_DIR} 29 | 30 | rm -rf ${SERVER_UPDATES_DIR} 31 | mkdir ${SERVER_UPDATES_DIR} 32 | 33 | rm -rf ${SERVER_AVERAGED_DIR} 34 | mkdir ${SERVER_AVERAGED_DIR} 35 | 36 | cp ${STARTER_REE} "${SERVER_AVERAGED_DIR}${DM}_averaged_r0.weights_ree" 37 | cp ${STARTER_TEE} "${SERVER_AVERAGED_DIR}${DM}_averaged_r0.weights_tee" 38 | 39 | 40 | for ((r=1;r<=NUM_ROUNDS;r++)) 41 | do 42 | echo "============= round ${r} =============" 43 | for ((c=1;c<=NUM_CLIENTS;c++)) 44 | do 45 | echo "============= copy weights server -> client ${c} =============" 46 | rp=$((r-1)) 47 | 48 | if [ ${r} == 1 ] 49 | then 50 | time sshpass -p ${PASSWORD_CLIENT_1} scp "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights_ree" "root@${IP_CLIENT_1}:${CLIENT_MODEL_DIR}${DM}_global.weights_ree" 51 | filesize=$(stat --format=%s "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights_ree") 52 | echo "ree weights: ${filesize} Bytes" 53 | sleep 3s 54 | fi 55 | 56 | time sshpass -p ${PASSWORD_CLIENT_1} scp "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights_tee" "root@${IP_CLIENT_1}:${CLIENT_MODEL_DIR}${DM}_global.weights_tee" 57 | filesize=$(stat --format=%s "${SERVER_AVERAGED_DIR}${DM}_averaged_r${rp}.weights_tee") 58 | echo "tee weights: ${filesize} Bytes" 59 | sleep 3s 60 | 61 | echo "============= ssh to the client and local training =============" 62 | 63 | # training with TEEs (for ss, only support tee) 64 | time sshpass -p ${PASSWORD_CLIENT_1} ssh "root@${IP_CLIENT_1}" darknetp classifier train -pp_start_f ${PP_START} -pp_end ${PP_END} -ss 1 "cfg/${DATASET}.dataset" "cfg/${DM}.cfg" "${CLIENT_MODEL_DIR}${DM}_global.weights" 65 | sleep 3s 66 | 67 | echo "============= copy weights client ${c} -> server =============" 68 | rm -rf "${SERVER_UPDATES_DIR}${DM}_c${c}.weights" 69 | mkdir "${SERVER_UPDATES_DIR}${DM}_c${c}.weights" 70 | 71 | time sshpass -p ${PASSWORD_CLIENT_1} scp "root@${IP_CLIENT_1}:${CLIENT_MODEL_DIR}${DM}.weights_tee" "${SERVER_UPDATES_DIR}${DM}_c${c}.weights/_tee" 72 | filesize=$(stat --format=%s "${SERVER_UPDATES_DIR}${DM}_c${c}.weights/_tee") 73 | echo "tee weights: ${filesize} Bytes" 74 | sleep 3s 75 | done 76 | 77 | echo "============= averaging =============" 78 | time host/secure_aggregation_host server model_aggregation -pp_start_f ${PP_START} -pp_end ${PP_END} -ss 1 "cfg/${DM}.cfg" ${SERVER_UPDATES_DIR} 79 | mv "${SERVER_UPDATES_DIR}${DM}_averaged.weights_tee" "${SERVER_AVERAGED_DIR}${DM}_averaged_r${r}.weights_tee" 80 | done 81 | --------------------------------------------------------------------------------