From de81ac3f3ef45effc3b377b559a19fcaece38a59 Mon Sep 17 00:00:00 2001 From: youben11 Date: Fri, 27 Aug 2021 12:38:15 +0100 Subject: [PATCH] feat: Runtime library --- compiler/CMakeLists.txt | 2 +- compiler/include/zamalang/Runtime/context.h | 30 ++++++++++++ compiler/include/zamalang/Runtime/wrappers.h | 10 ++++ compiler/lib/CMakeLists.txt | 1 + compiler/lib/Runtime/CMakeLists.txt | 4 ++ compiler/lib/Runtime/context.c | 51 ++++++++++++++++++++ compiler/lib/Runtime/wrappers.c | 12 +++++ 7 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 compiler/include/zamalang/Runtime/context.h create mode 100644 compiler/include/zamalang/Runtime/wrappers.h create mode 100644 compiler/lib/Runtime/CMakeLists.txt create mode 100644 compiler/lib/Runtime/context.c create mode 100644 compiler/lib/Runtime/wrappers.c diff --git a/compiler/CMakeLists.txt b/compiler/CMakeLists.txt index 19e10e494..a4bfd71e4 100644 --- a/compiler/CMakeLists.txt +++ b/compiler/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.13) -project(zamacompiler LANGUAGES CXX) +project(zamacompiler LANGUAGES C CXX) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/compiler/include/zamalang/Runtime/context.h b/compiler/include/zamalang/Runtime/context.h new file mode 100644 index 000000000..e0f883176 --- /dev/null +++ b/compiler/include/zamalang/Runtime/context.h @@ -0,0 +1,30 @@ +#ifndef ZAMALANG_RUNTIME_CONTEXT_H +#define ZAMALANG_RUNTIME_CONTEXT_H + +#include "concrete-ffi.h" + +typedef struct RuntimeContext { + struct LweKeyswitchKey_u64 *ksk; + struct LweBootstrapKey_u64 *bsk; +} RuntimeContext; + +extern RuntimeContext *globalRuntimeContext; + +RuntimeContext *createRuntimeContext(LweKeyswitchKey_u64 *ksk, + LweBootstrapKey_u64 *bsk); + +void setGlobalRuntimeContext(RuntimeContext *context); + +RuntimeContext *getGlobalRuntimeContext(); + +LweKeyswitchKey_u64 *getGlobalKeyswitchKey(); + +LweBootstrapKey_u64 *getGlobalBootstrapKey(); + +LweKeyswitchKey_u64 *getKeyswitckKeyFromContext(RuntimeContext *context); + +LweBootstrapKey_u64 *getBootstrapKeyFromContext(RuntimeContext *context); + +bool checkError(int *err); + +#endif diff --git a/compiler/include/zamalang/Runtime/wrappers.h b/compiler/include/zamalang/Runtime/wrappers.h new file mode 100644 index 000000000..7abdb8632 --- /dev/null +++ b/compiler/include/zamalang/Runtime/wrappers.h @@ -0,0 +1,10 @@ +#ifndef ZAMALANG_RUNTIME_WRAPPERS_H +#define ZAMALANG_RUNTIME_WRAPPERS_H + +#include "concrete-ffi.h" + +ForeignPlaintextList_u64 *runtime_foreign_plaintext_list_u64( + int *err, uint64_t *allocated, uint64_t *aligned, uint64_t offset, + uint64_t size_dim0, uint64_t stride_dim0, uint64_t size); + +#endif diff --git a/compiler/lib/CMakeLists.txt b/compiler/lib/CMakeLists.txt index e94e4bb6d..83a8a0f9f 100644 --- a/compiler/lib/CMakeLists.txt +++ b/compiler/lib/CMakeLists.txt @@ -1,6 +1,7 @@ add_subdirectory(Dialect) add_subdirectory(Conversion) add_subdirectory(Support) +add_subdirectory(Runtime) # CAPI needed only for python bindings if (ZAMALANG_BINDINGS_PYTHON_ENABLED) diff --git a/compiler/lib/Runtime/CMakeLists.txt b/compiler/lib/Runtime/CMakeLists.txt new file mode 100644 index 000000000..693dae055 --- /dev/null +++ b/compiler/lib/Runtime/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(ZamalangRuntime SHARED + context.c + wrappers.c +) \ No newline at end of file diff --git a/compiler/lib/Runtime/context.c b/compiler/lib/Runtime/context.c new file mode 100644 index 000000000..6074858a7 --- /dev/null +++ b/compiler/lib/Runtime/context.c @@ -0,0 +1,51 @@ +#include "zamalang/Runtime/context.h" +#include + +RuntimeContext *globalRuntimeContext; + +RuntimeContext *createRuntimeContext(LweKeyswitchKey_u64 *ksk, + LweBootstrapKey_u64 *bsk) { + RuntimeContext *context = (RuntimeContext *)malloc(sizeof(RuntimeContext)); + context->ksk = ksk; + context->bsk = bsk; + return context; +} + +void setGlobalRuntimeContext(RuntimeContext *context) { + globalRuntimeContext = context; +} + +RuntimeContext *getGlobalRuntimeContext() { return globalRuntimeContext; } + +LweKeyswitchKey_u64 *getGlobalKeyswitchKey() { + return globalRuntimeContext->ksk; +} + +LweBootstrapKey_u64 *getGlobalBootstrapKey() { + return globalRuntimeContext->bsk; +} + +LweKeyswitchKey_u64 *getKeyswitckKeyFromContext(RuntimeContext *context) { + return context->ksk; +} + +LweBootstrapKey_u64 *getBootstrapKeyFromContext(RuntimeContext *context) { + return context->bsk; +} + +bool checkError(int *err) { + switch (*err) { + case ERR_INDEX_OUT_OF_BOUND: + fprintf(stderr, "Runtime: index out of bound"); + break; + case ERR_NULL_POINTER: + fprintf(stderr, "Runtime: null pointer"); + break; + case ERR_SIZE_MISMATCH: + fprintf(stderr, "Runtime: size mismatch"); + break; + default: + return false; + } + return true; +} \ No newline at end of file diff --git a/compiler/lib/Runtime/wrappers.c b/compiler/lib/Runtime/wrappers.c new file mode 100644 index 000000000..4bbd3ac3c --- /dev/null +++ b/compiler/lib/Runtime/wrappers.c @@ -0,0 +1,12 @@ +#include "zamalang/Runtime/wrappers.h" +#include + +ForeignPlaintextList_u64 *runtime_foreign_plaintext_list_u64( + int *err, uint64_t *allocated, uint64_t *aligned, uint64_t offset, + uint64_t size_dim0, uint64_t stride_dim0, uint64_t size) { + if (stride_dim0 != 1) { + fprintf(stderr, "Runtime: stride not equal to 1, check " + "runtime_foreign_plaintext_list_u64"); + } + return foreign_plaintext_list_u64(err, aligned + offset, size); +}