├── cmake-build-debug ├── .cmake │ └── api │ │ └── v1 │ │ └── query │ │ ├── cache-v2 │ │ ├── cmakeFiles-v1 │ │ ├── codemodel-v2 │ │ └── toolchains-v1 └── CMakeFiles │ ├── cmake.check_cache │ ├── 3.27.8 │ ├── CompilerIdC │ │ └── a.exe │ ├── CompilerIdCXX │ │ └── a.exe │ ├── CMakeSystem.cmake │ ├── CMakeCCompiler.cmake │ └── CMakeCXXCompiler.cmake │ ├── clion-environment.txt │ ├── clion-Debug-log.txt │ └── CMakeConfigureLog.yaml ├── native ├── examples │ ├── build │ │ ├── CMakeFiles │ │ │ ├── Progress │ │ │ │ ├── 2 │ │ │ │ └── count.txt │ │ │ ├── progress.marks │ │ │ ├── 3.18.3 │ │ │ │ ├── CompilerIdCUDA │ │ │ │ │ ├── tmp │ │ │ │ │ │ ├── a_dlink.reg.c │ │ │ │ │ │ ├── CMakeCUDACompilerId.module_id │ │ │ │ │ │ ├── a_dlink.o │ │ │ │ │ │ ├── a_dlink.fatbin │ │ │ │ │ │ ├── CMakeCUDACompilerId.o │ │ │ │ │ │ ├── a_dlink.sm_52.cubin │ │ │ │ │ │ ├── CMakeCUDACompilerId.fatbin │ │ │ │ │ │ ├── CMakeCUDACompilerId.sm_52.cubin │ │ │ │ │ │ ├── CMakeCUDACompilerId.ptx │ │ │ │ │ │ ├── CMakeCUDACompilerId.cudafe1.gpu │ │ │ │ │ │ ├── CMakeCUDACompilerId.cudafe1.stub.c │ │ │ │ │ │ ├── CMakeCUDACompilerId.cudafe1.c │ │ │ │ │ │ ├── CMakeCUDACompilerId.fatbin.c │ │ │ │ │ │ └── a_dlink.fatbin.c │ │ │ │ │ └── a.out │ │ │ │ ├── CompilerIdCXX │ │ │ │ │ └── a.out │ │ │ │ ├── CMakeDetermineCompilerABI_CUDA.bin │ │ │ │ ├── CMakeDetermineCompilerABI_CXX.bin │ │ │ │ ├── CMakeSystem.cmake │ │ │ │ └── CMakeCUDACompiler.cmake │ │ │ ├── sealexamples.dir │ │ │ │ ├── progress.make │ │ │ │ ├── 2_encoders.cpp.o │ │ │ │ ├── link.txt │ │ │ │ ├── flags.make │ │ │ │ ├── cmake_clean.cmake │ │ │ │ ├── DependInfo.cmake │ │ │ │ ├── depend.internal │ │ │ │ └── build.make │ │ │ ├── cmake.check_cache │ │ │ ├── TargetDirectories.txt │ │ │ ├── FindThreads │ │ │ │ └── CheckForPthreads.cxx │ │ │ ├── CMakeDirectoryInformation.cmake │ │ │ ├── CMakeError.log │ │ │ ├── Makefile.cmake │ │ │ └── Makefile2 │ │ └── cmake_install.cmake │ ├── CMakeLists.txt │ └── examples.cpp ├── tests │ ├── seal │ │ ├── testrunner.cpp │ │ ├── util │ │ │ ├── CMakeLists.txt │ │ │ ├── hash.cpp │ │ │ ├── clipnormal.cpp │ │ │ └── galois.cpp │ │ ├── CMakeLists.txt │ │ ├── randomtostd.cpp │ │ ├── secretkey.cpp │ │ ├── memorymanager.cpp │ │ ├── publickey.cpp │ │ └── batchencoder.cpp │ └── CMakeLists.txt ├── src │ └── seal │ │ ├── util │ │ ├── fips202.h │ │ ├── cgmanifest.json │ │ ├── iterator.cpp │ │ ├── clipnormal.cpp │ │ ├── hash.cpp │ │ ├── croots.h │ │ ├── common.cpp │ │ ├── hash.h │ │ ├── scalingvariant.h │ │ ├── config.h.in │ │ ├── common.cuh │ │ ├── gpu_data.h │ │ ├── croots.cpp │ │ ├── clipnormal.h │ │ ├── msvc.h │ │ ├── ntt_helper.cuh │ │ ├── globals.h │ │ ├── helper.cu │ │ ├── ztools.h │ │ ├── clang.h │ │ ├── uintcore.cpp │ │ ├── gcc.h │ │ ├── blake2-impl.h │ │ ├── hestdparms.h │ │ └── helper.cuh │ │ ├── memorymanager.cpp │ │ ├── seal.h │ │ ├── version.h │ │ ├── randomtostd.h │ │ ├── galoiskeys.h │ │ ├── CMakeLists.txt │ │ ├── relinkeys.h │ │ └── kswitchkeys.cpp └── bench │ ├── keygen.cpp │ ├── CMakeLists.txt │ └── ntt.cpp ├── image.png ├── .idea ├── misc.xml ├── codeStyles │ ├── codeStyleConfig.xml │ └── Project.xml ├── .gitignore └── modules.xml ├── pkgconfig ├── seal_msgsl.pc.in ├── seal_shared.pc.in └── seal.pc.in ├── tools └── Makefile ├── cmake ├── CheckMemset.cmake ├── ExternalMSGSL.cmake ├── ExternalGTest.cmake ├── CleanArtifacts.cmake ├── EnableCXXCompilerFlags.cmake ├── CheckCXXIntrinsicsHeader.cmake ├── ExternalIntelHEXL.cmake ├── ExternalZSTD.cmake ├── functions.iOS.cmake ├── ExternalZLIB.cmake ├── EnableCXX17.cmake ├── ExternalBenchmark.cmake ├── CheckCXXIntrinsicsSpecific.cmake └── SEALMacros.cmake ├── LICENSE ├── README.md ├── Dockerfile ├── NOTICE └── .clang-format /cmake-build-debug/.cmake/api/v1/query/cache-v2: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/Progress/2: -------------------------------------------------------------------------------- 1 | empty -------------------------------------------------------------------------------- /cmake-build-debug/.cmake/api/v1/query/cmakeFiles-v1: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cmake-build-debug/.cmake/api/v1/query/codemodel-v2: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cmake-build-debug/.cmake/api/v1/query/toolchains-v1: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/Progress/count.txt: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/image.png -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.reg.c: -------------------------------------------------------------------------------- 1 | #define NUM_PRELINKED_OBJECTS 0 2 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 1 2 | CMAKE_PROGRESS_2 = 2 3 | 4 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.module_id: -------------------------------------------------------------------------------- 1 | _84c43d9c_22_CMakeCUDACompilerId_cu_bd57c623 -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/cmake.check_cache: -------------------------------------------------------------------------------- 1 | # This file is generated by cmake for dependency checking of the CMakeCache.txt file 2 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/cmake.check_cache: -------------------------------------------------------------------------------- 1 | # This file is generated by cmake for dependency checking of the CMakeCache.txt file 2 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.27.8/CompilerIdC/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/cmake-build-debug/CMakeFiles/3.27.8/CompilerIdC/a.exe -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.27.8/CompilerIdCXX/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/cmake-build-debug/CMakeFiles/3.27.8/CompilerIdCXX/a.exe -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/a.out -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCXX/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCXX/a.out -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/2_encoders.cpp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/sealexamples.dir/2_encoders.cpp.o -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.o -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/c++ -static CMakeFiles/sealexamples.dir/2_encoders.cpp.o -o bin/sealexamples /usr/local/lib/libseal-4.1.a -pthread 2 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CMakeDetermineCompilerABI_CUDA.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CMakeDetermineCompilerABI_CUDA.bin -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CMakeDetermineCompilerABI_CXX.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CMakeDetermineCompilerABI_CXX.bin -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.fatbin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.fatbin -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.o -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.sm_52.cubin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.sm_52.cubin -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.fatbin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.fatbin -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.sm_52.cubin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Rayman96/CAT/HEAD/native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.sm_52.cubin -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/clion-environment.txt: -------------------------------------------------------------------------------- 1 | ToolSet: 11.0 w64 (local)@C:\Program Files\JetBrains\CLion 2023.3.4\bin\mingw 2 | Options: 3 | 4 | Options:-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.3.4/bin/ninja/win/x64/ninja.exe -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/TargetDirectories.txt: -------------------------------------------------------------------------------- 1 | /home/liqirui/SEAL/native/examples/build/CMakeFiles/rebuild_cache.dir 2 | /home/liqirui/SEAL/native/examples/build/CMakeFiles/edit_cache.dir 3 | /home/liqirui/SEAL/native/examples/build/CMakeFiles/sealexamples.dir 4 | -------------------------------------------------------------------------------- /pkgconfig/seal_msgsl.pc.in: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | Name: Microsoft GSL for IDEA SEAL_GPU 5 | Version: @Microsoft.GSL_VERSION@ 6 | Description: Guidelines Support Library 7 | 8 | Cflags: -I@SEAL_PKGCONFIG_MSGSL_INCLUDE_DIR@ 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | # compile CXX with /usr/bin/c++ 5 | CXX_DEFINES = 6 | 7 | CXX_INCLUDES = -isystem /usr/local/include/SEAL-4.1 8 | 9 | CXX_FLAGS = -pthread -std=gnu++17 10 | 11 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.ptx: -------------------------------------------------------------------------------- 1 | // 2 | // Generated by NVIDIA NVVM Compiler 3 | // 4 | // Compiler Build ID: CL-31294372 5 | // Cuda compilation tools, release 11.7, V11.7.64 6 | // Based on NVVM 7.0.1 7 | // 8 | 9 | .version 7.7 10 | .target sm_52 11 | .address_size 64 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /native/tests/seal/testrunner.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "gtest/gtest.h" 5 | 6 | /** 7 | Main entry point for Google Test unit tests. 8 | */ 9 | int main(int argc, char **argv) 10 | { 11 | testing::InitGoogleTest(&argc, argv); 12 | return RUN_ALL_TESTS(); 13 | } 14 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "CMakeFiles/sealexamples.dir/2_encoders.cpp.o" 3 | "bin/sealexamples" 4 | "bin/sealexamples.pdb" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang CXX) 9 | include(CMakeFiles/sealexamples.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/FindThreads/CheckForPthreads.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void* start_routine(void* args) 4 | { 5 | return args; 6 | } 7 | 8 | int main(void) 9 | { 10 | /* This is a compile and link test, no code to actually run things. */ 11 | pthread_t thread; 12 | pthread_create(&thread, 0, start_routine, 0); 13 | pthread_join(thread, 0); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.gpu: -------------------------------------------------------------------------------- 1 | typedef char __nv_bool; 2 | # 216 "/usr/local/gcc-8.2/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include/stddef.h" 3 3 | typedef unsigned long size_t; 4 | #include "crt/device_runtime.h" 5 | # 238 "/usr/local/gcc-8.2/include/c++/8.2.0/x86_64-pc-linux-gnu/bits/c++config.h" 3 6 | typedef unsigned long _ZSt6size_t; 7 | #include "common_functions.h" 8 | -------------------------------------------------------------------------------- /native/src/seal/util/fips202.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include 7 | #include 8 | 9 | #if defined(__cplusplus) 10 | extern "C" 11 | { 12 | #endif 13 | 14 | void shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen); 15 | 16 | #if defined(__cplusplus) 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /native/src/seal/memorymanager.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/memorymanager.h" 5 | 6 | using namespace std; 7 | 8 | namespace seal 9 | { 10 | #ifndef _M_CEE 11 | mutex MemoryManager::switch_mutex_; 12 | #else 13 | #pragma message("WARNING: MemoryManager compiled thread-unsafe and MMProfGuard disabled to support /clr") 14 | #endif 15 | } // namespace seal 16 | -------------------------------------------------------------------------------- /tools/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | SHELL=/bin/bash 5 | COMPR_SYSTEM_INFO_ARCHIVE=../system_info.tar.gz 6 | 7 | .PHONY: system_info clean 8 | 9 | system_info: clean $(COMPR_SYSTEM_INFO_ARCHIVE) 10 | 11 | $(COMPR_SYSTEM_INFO_ARCHIVE): scripts/collect_system_info.sh 12 | @$(SHELL) scripts/collect_system_info.sh 13 | 14 | clean: 15 | @rm -f $(COMPR_SYSTEM_INFO_ARCHIVE) 16 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.27.8/CMakeSystem.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_HOST_SYSTEM "Windows-10.0.22631") 2 | set(CMAKE_HOST_SYSTEM_NAME "Windows") 3 | set(CMAKE_HOST_SYSTEM_VERSION "10.0.22631") 4 | set(CMAKE_HOST_SYSTEM_PROCESSOR "AMD64") 5 | 6 | 7 | 8 | set(CMAKE_SYSTEM "Windows-10.0.22631") 9 | set(CMAKE_SYSTEM_NAME "Windows") 10 | set(CMAKE_SYSTEM_VERSION "10.0.22631") 11 | set(CMAKE_SYSTEM_PROCESSOR "AMD64") 12 | 13 | set(CMAKE_CROSSCOMPILING "FALSE") 14 | 15 | set(CMAKE_SYSTEM_LOADED 1) 16 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CMakeSystem.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_HOST_SYSTEM "Linux-5.4.0-42-generic") 2 | set(CMAKE_HOST_SYSTEM_NAME "Linux") 3 | set(CMAKE_HOST_SYSTEM_VERSION "5.4.0-42-generic") 4 | set(CMAKE_HOST_SYSTEM_PROCESSOR "x86_64") 5 | 6 | 7 | 8 | set(CMAKE_SYSTEM "Linux-5.4.0-42-generic") 9 | set(CMAKE_SYSTEM_NAME "Linux") 10 | set(CMAKE_SYSTEM_VERSION "5.4.0-42-generic") 11 | set(CMAKE_SYSTEM_PROCESSOR "x86_64") 12 | 13 | set(CMAKE_CROSSCOMPILING "FALSE") 14 | 15 | set(CMAKE_SYSTEM_LOADED 1) 16 | -------------------------------------------------------------------------------- /cmake/CheckMemset.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Check for memset_s 5 | check_cxx_source_runs(" 6 | #define __STDC_WANT_LIB_EXT1__ 1 7 | #include 8 | int main(void) 9 | { 10 | char str[] = \"ghghghghghghghghghghgh\"; 11 | int r = memset_s(str, sizeof(str), 'a', 5); 12 | return r; 13 | }" 14 | SEAL_MEMSET_S_FOUND) 15 | 16 | # Check for explicit_bzero 17 | check_symbol_exists(explicit_bzero "string.h" SEAL_EXPLICIT_BZERO_FOUND) 18 | 19 | # Check for explicit_memset 20 | check_symbol_exists(explicit_memset "string.h" SEAL_EXPLICIT_MEMSET_FOUND) 21 | -------------------------------------------------------------------------------- /native/src/seal/util/cgmanifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/component-detection-manifest.json", 3 | "Registrations": [ 4 | { 5 | "component": { 6 | "type": "git", 7 | "git": { 8 | "repositoryUrl": "https://github.com/BLAKE2/BLAKE2", 9 | "commitHash": "997fa5ba1e14b52c554fb03ce39e579e6f27b90c" 10 | } 11 | } 12 | }, 13 | { 14 | "component": { 15 | "type": "git", 16 | "git": { 17 | "repositoryUrl": "https://github.com/pq-crystals/kyber", 18 | "commitHash": "844057468e69527bd15b17fbe03f4b61f9a22065" 19 | } 20 | } 21 | } 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /pkgconfig/seal_shared.pc.in: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=${prefix}/lib 6 | includedir=${prefix}/@SEAL_INCLUDES_INSTALL_DIR@ 7 | 8 | Name: IDEA SEAL_GPU 9 | Version: @SEAL_VERSION_MAJOR@.@SEAL_VERSION_MINOR@ 10 | Description: An easy-to-use homomorphic encryption library 11 | 12 | Requires: @SEAL_PKGCONFIG_MSGSL_REQUIRES_STRING@ 13 | Requires: @SEAL_PKGCONFIG_ZLIB_REQUIRES_STRING@ 14 | Requires: @SEAL_PKGCONFIG_ZSTD_REQUIRES_STRING@ 15 | Requires: @SEAL_PKGCONFIG_INTEL_HEXL_REQUIRES_STRING@ 16 | 17 | Libs: -L${libdir} -lseal 18 | Cflags: @SEAL_LANG_FLAG@ -I${includedir} 19 | -------------------------------------------------------------------------------- /pkgconfig/seal.pc.in: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | prefix=@CMAKE_INSTALL_PREFIX@ 5 | libdir=${prefix}/lib 6 | includedir=${prefix}/@SEAL_INCLUDES_INSTALL_DIR@ 7 | 8 | Name: IDEA SEAL_GPU 9 | Version: @SEAL_VERSION@ 10 | Description: An easy-to-use homomorphic encryption library 11 | 12 | Requires: @SEAL_PKGCONFIG_MSGSL_REQUIRES_STRING@ 13 | Requires: @SEAL_PKGCONFIG_ZLIB_REQUIRES_STRING@ 14 | Requires: @SEAL_PKGCONFIG_ZSTD_REQUIRES_STRING@ 15 | Requires: @SEAL_PKGCONFIG_INTEL_HEXL_REQUIRES_STRING@ 16 | 17 | Libs: -L${libdir} -lseal-@SEAL_VERSION_MAJOR@.@SEAL_VERSION_MINOR@ 18 | Cflags: @SEAL_LANG_FLAG@ -I${includedir} 19 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/liqirui/SEAL/native/examples") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/liqirui/SEAL/native/examples/build") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "CXX" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_CXX 7 | "/home/liqirui/SEAL/native/examples/2_encoders.cpp" "/home/liqirui/SEAL/native/examples/build/CMakeFiles/sealexamples.dir/2_encoders.cpp.o" 8 | ) 9 | set(CMAKE_CXX_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_CXX_TARGET_INCLUDE_PATH 13 | "/usr/local/include/SEAL-4.1" 14 | ) 15 | 16 | # Targets to which this target links. 17 | set(CMAKE_TARGET_LINKED_INFO_FILES 18 | ) 19 | 20 | # Fortran module output directory. 21 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 22 | -------------------------------------------------------------------------------- /native/src/seal/util/iterator.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/ciphertext.cuh" 5 | #include "seal/util/iterator.h" 6 | 7 | namespace seal 8 | { 9 | namespace util 10 | { 11 | PolyIter::PolyIter(Ciphertext &ct) : self_type(ct.data(), ct.poly_modulus_degree(), ct.coeff_modulus_size()) 12 | {} 13 | 14 | ConstPolyIter::ConstPolyIter(const Ciphertext &ct) 15 | : self_type(ct.data(), ct.poly_modulus_degree(), ct.coeff_modulus_size()) 16 | {} 17 | 18 | ConstPolyIter::ConstPolyIter(Ciphertext &ct) 19 | : self_type(ct.data(), ct.poly_modulus_degree(), ct.coeff_modulus_size()) 20 | {} 21 | } // namespace util 22 | } // namespace seal 23 | -------------------------------------------------------------------------------- /cmake/ExternalMSGSL.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | FetchContent_Declare( 5 | msgsl 6 | GIT_REPOSITORY https://github.com/microsoft/GSL.git 7 | GIT_TAG a3534567187d2edc428efd3f13466ff75fe5805c # 4.0.0 8 | ) 9 | FetchContent_GetProperties(msgsl) 10 | 11 | if(NOT msgsl_POPULATED) 12 | FetchContent_Populate(msgsl) 13 | 14 | set(GSL_CXX_STANDARD "14" CACHE STRING "" FORCE) 15 | set(GSL_TEST OFF CACHE BOOL "" FORCE) 16 | mark_as_advanced(GSL_CXX_STANDARD ) 17 | mark_as_advanced(GSL_TEST) 18 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_MSGSL) 19 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_MSGSL) 20 | 21 | add_subdirectory( 22 | ${msgsl_SOURCE_DIR} 23 | ${msgsl_SOURCE_DIR}/../msgsl-build 24 | EXCLUDE_FROM_ALL) 25 | endif() 26 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.stub.c: -------------------------------------------------------------------------------- 1 | #pragma GCC diagnostic push 2 | #pragma GCC diagnostic ignored "-Wunused-function" 3 | #pragma GCC diagnostic ignored "-Wcast-qual" 4 | #define __NV_CUBIN_HANDLE_STORAGE__ static 5 | #if !defined(__CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__) 6 | #define __CUDA_INCLUDE_COMPILER_INTERNAL_HEADERS__ 7 | #endif 8 | #include "crt/host_runtime.h" 9 | #include "CMakeCUDACompilerId.fatbin.c" 10 | static void __nv_cudaEntityRegisterCallback(void **); 11 | static void __sti____cudaRegisterAll(void) __attribute__((__constructor__)); 12 | static void __nv_cudaEntityRegisterCallback(void **__T0){__nv_dummy_param_ref(__T0);__nv_save_fatbinhandle_for_managed_rt(__T0);} 13 | static void __sti____cudaRegisterAll(void){__cudaRegisterBinary(__nv_cudaEntityRegisterCallback);} 14 | 15 | #pragma GCC diagnostic pop 16 | -------------------------------------------------------------------------------- /native/src/seal/seal.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/batchencoder.h" 7 | #include "seal/ciphertext.cuh" 8 | #include "seal/ckks.h" 9 | #include "seal/context.cuh" 10 | #include "seal/decryptor.h" 11 | #include "seal/dynarray.h" 12 | #include "seal/encryptionparams.cuh" 13 | #include "seal/encryptor.h" 14 | #include "seal/evaluator.h" 15 | #include "seal/galoiskeys.h" 16 | #include "seal/keygenerator.h" 17 | #include "seal/memorymanager.h" 18 | #include "seal/modulus.h" 19 | #include "seal/plaintext.cuh" 20 | #include "seal/publickey.h" 21 | #include "seal/randomgen.h" 22 | #include "seal/randomtostd.h" 23 | #include "seal/relinkeys.h" 24 | #include "seal/secretkey.cuh" 25 | #include "seal/serializable.h" 26 | #include "seal/serialization.h" 27 | #include "seal/valcheck.h" 28 | #include "seal/version.h" -------------------------------------------------------------------------------- /native/src/seal/util/clipnormal.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/util/clipnormal.h" 5 | #include 6 | 7 | using namespace std; 8 | 9 | namespace seal 10 | { 11 | namespace util 12 | { 13 | ClippedNormalDistribution::ClippedNormalDistribution( 14 | result_type mean, result_type standard_deviation, result_type max_deviation) 15 | : normal_(mean, standard_deviation), max_deviation_(max_deviation) 16 | { 17 | // Verify arguments. 18 | if (standard_deviation < 0) 19 | { 20 | throw invalid_argument("standard_deviation"); 21 | } 22 | if (max_deviation < 0) 23 | { 24 | throw invalid_argument("max_deviation"); 25 | } 26 | } 27 | } // namespace util 28 | } // namespace seal 29 | -------------------------------------------------------------------------------- /cmake/ExternalGTest.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | FetchContent_Declare( 5 | googletest 6 | GIT_REPOSITORY https://github.com/google/googletest.git 7 | GIT_TAG 58d77fa8070e8cec2dc1ed015d66b454c8d78850 # 1.12.1 8 | ) 9 | FetchContent_GetProperties(googletest) 10 | 11 | if(NOT googletest_POPULATED) 12 | FetchContent_Populate(googletest) 13 | 14 | set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) 15 | set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) 16 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 17 | mark_as_advanced(BUILD_GMOCK) 18 | mark_as_advanced(INSTALL_GTEST) 19 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_GOOGLETEST) 20 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_GOOGLETEST) 21 | 22 | add_subdirectory( 23 | ${googletest_SOURCE_DIR} 24 | ${THIRDPARTY_BINARY_DIR}/googletest-src 25 | EXCLUDE_FROM_ALL) 26 | endif() 27 | -------------------------------------------------------------------------------- /cmake/CleanArtifacts.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Remove native/src/gsl directory which is no longer used in version >= 3.5.0 5 | if(EXISTS ${SEAL_INCLUDES_DIR}/gsl) 6 | message(STATUS "Removing ${SEAL_INCLUDES_DIR}/gsl; this is no longer used by IDEA SEAL_GPU >= 3.5.0") 7 | file(REMOVE_RECURSE ${SEAL_INCLUDES_DIR}/gsl) 8 | endif() 9 | 10 | # Remove thirdparty/zlib/src/CMakeCache.txt: the location changed in SEAL >= 3.5.4 11 | if(EXISTS ${SEAL_THIRDPARTY_DIR}/zlib/src/CMakeCache.txt) 12 | message(STATUS "Removing old ${SEAL_THIRDPARTY_DIR}/zlib/src/CMakeCache.txt") 13 | file(REMOVE ${SEAL_THIRDPARTY_DIR}/zlib/src/CMakeCache.txt) 14 | endif() 15 | 16 | # Remove config.h from source tree 17 | if(EXISTS ${SEAL_INCLUDES_DIR}/seal/util/config.h) 18 | message(STATUS "Removing old ${SEAL_INCLUDES_DIR}/seal/util/config.h") 19 | file(REMOVE ${SEAL_INCLUDES_DIR}/seal/util/config.h) 20 | endif() 21 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/clion-Debug-log.txt: -------------------------------------------------------------------------------- 1 | "C:\Program Files\JetBrains\CLion 2023.3.4\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2023.3.4/bin/ninja/win/x64/ninja.exe" -G Ninja -S C:\Users\IDEA\Downloads\seal_gpu-dev_for_publish\seal_gpu-dev_for_publish -B C:\Users\IDEA\Downloads\seal_gpu-dev_for_publish\seal_gpu-dev_for_publish\cmake-build-debug 2 | -- Build type (CMAKE_BUILD_TYPE): Debug 3 | -- GPU_SUPPORT: ON 4 | -- The CXX compiler identification is GNU 13.1.0 5 | -- The C compiler identification is GNU 13.1.0 6 | CMake Error at C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCUDACompiler.cmake:180 (message): 7 | Failed to find nvcc. 8 | 9 | Compiler requires the CUDA toolkit. Please set the CUDAToolkit_ROOT 10 | variable. 11 | Call Stack (most recent call first): 12 | CMakeLists.txt:37 (project) 13 | 14 | 15 | -- Configuring incomplete, errors occurred! 16 | -------------------------------------------------------------------------------- /native/src/seal/util/hash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/util/hash.h" 5 | 6 | using namespace std; 7 | 8 | namespace seal 9 | { 10 | namespace util 11 | { 12 | // Required for C++14 compliance: static constexpr member variables are not necessarily inlined so need to 13 | // ensure symbol is created. 14 | constexpr size_t HashFunction::hash_block_uint64_count; 15 | 16 | // Required for C++14 compliance: static constexpr member variables are not necessarily inlined so need to 17 | // ensure symbol is created. 18 | constexpr size_t HashFunction::hash_block_byte_count; 19 | 20 | // Required for C++14 compliance: static constexpr member variables are not necessarily inlined so need to 21 | // ensure symbol is created. 22 | constexpr HashFunction::hash_block_type HashFunction::hash_zero_block; 23 | } // namespace util 24 | } // namespace seal 25 | -------------------------------------------------------------------------------- /cmake/EnableCXXCompilerFlags.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | include(CheckCXXCompilerFlag) 5 | 6 | # For easier adding of CXX compiler flags 7 | function(seal_enable_cxx_compiler_flag_if_supported flag) 8 | string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set) 9 | if(flag_already_set EQUAL -1) 10 | message(STATUS "Adding CXX compiler flag: ${flag} ...") 11 | check_cxx_compiler_flag("${flag}" flag_supported) 12 | if(flag_supported) 13 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) 14 | endif() 15 | unset(flag_supported CACHE) 16 | endif() 17 | endfunction() 18 | 19 | if(NOT MSVC AND SEAL_DEBUG) 20 | seal_enable_cxx_compiler_flag_if_supported("-Wall") 21 | seal_enable_cxx_compiler_flag_if_supported("-Wextra") 22 | seal_enable_cxx_compiler_flag_if_supported("-Wconversion") 23 | seal_enable_cxx_compiler_flag_if_supported("-Wshadow") 24 | seal_enable_cxx_compiler_flag_if_supported("-pedantic") 25 | endif() 26 | -------------------------------------------------------------------------------- /native/src/seal/util/croots.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/memorymanager.h" 7 | #include "seal/util/defines.h" 8 | #include "seal/util/uintcore.h" 9 | #include 10 | #include 11 | #include 12 | 13 | namespace seal 14 | { 15 | namespace util 16 | { 17 | class ComplexRoots 18 | { 19 | public: 20 | ComplexRoots() = delete; 21 | 22 | ComplexRoots(std::size_t degree_of_roots, MemoryPoolHandle pool); 23 | 24 | SEAL_NODISCARD std::complex get_root(std::size_t index) const; 25 | 26 | private: 27 | static constexpr double PI_ = 3.1415926535897932384626433832795028842; 28 | 29 | // Contains 0~(n/8-1)-th powers of the n-th primitive root. 30 | util::Pointer> roots_; 31 | 32 | std::size_t degree_of_roots_; 33 | 34 | MemoryPoolHandle pool_; 35 | }; 36 | } // namespace util 37 | } // namespace seal 38 | -------------------------------------------------------------------------------- /native/tests/seal/util/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | target_sources(sealtest 5 | PRIVATE 6 | ${CMAKE_CURRENT_LIST_DIR}/clipnormal.cpp 7 | ${CMAKE_CURRENT_LIST_DIR}/common.cpp 8 | ${CMAKE_CURRENT_LIST_DIR}/galois.cpp 9 | ${CMAKE_CURRENT_LIST_DIR}/hash.cpp 10 | ${CMAKE_CURRENT_LIST_DIR}/iterator.cpp 11 | ${CMAKE_CURRENT_LIST_DIR}/locks.cpp 12 | ${CMAKE_CURRENT_LIST_DIR}/mempool.cpp 13 | ${CMAKE_CURRENT_LIST_DIR}/numth.cpp 14 | ${CMAKE_CURRENT_LIST_DIR}/polyarithsmallmod.cpp 15 | ${CMAKE_CURRENT_LIST_DIR}/polycore.cpp 16 | ${CMAKE_CURRENT_LIST_DIR}/rns.cpp 17 | ${CMAKE_CURRENT_LIST_DIR}/ntt.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/stringtouint64.cpp 19 | ${CMAKE_CURRENT_LIST_DIR}/uint64tostring.cpp 20 | ${CMAKE_CURRENT_LIST_DIR}/uintarith.cpp 21 | ${CMAKE_CURRENT_LIST_DIR}/uintarithmod.cpp 22 | ${CMAKE_CURRENT_LIST_DIR}/uintarithsmallmod.cpp 23 | ${CMAKE_CURRENT_LIST_DIR}/uintcore.cpp 24 | ) 25 | -------------------------------------------------------------------------------- /cmake/CheckCXXIntrinsicsHeader.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Check for intrin.h or x86intrin.h 5 | if(SEAL_USE_INTRIN) 6 | set(CMAKE_REQUIRED_QUIET_OLD ${CMAKE_REQUIRED_QUIET}) 7 | set(CMAKE_REQUIRED_QUIET ON) 8 | 9 | if(MSVC) 10 | set(SEAL_INTRIN_HEADER "intrin.h") 11 | else() 12 | if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") 13 | set(SEAL_ARM64 ON) 14 | else() 15 | set(SEAL_ARM64 OFF) 16 | endif() 17 | if(SEAL_ARM64) 18 | set(SEAL_INTRIN_HEADER "arm_neon.h") 19 | elseif(EMSCRIPTEN) 20 | set(SEAL_INTRIN_HEADER "wasm_simd128.h") 21 | else() 22 | set(SEAL_INTRIN_HEADER "x86intrin.h") 23 | endif() 24 | endif() 25 | 26 | check_include_file_cxx(${SEAL_INTRIN_HEADER} SEAL_INTRIN_HEADER_FOUND) 27 | set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_OLD}) 28 | 29 | if(SEAL_INTRIN_HEADER_FOUND) 30 | message(STATUS "${SEAL_INTRIN_HEADER} - found") 31 | else() 32 | message(STATUS "${SEAL_INTRIN_HEADER} - not found") 33 | endif() 34 | endif() 35 | -------------------------------------------------------------------------------- /cmake/ExternalIntelHEXL.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | FetchContent_Declare( 5 | hexl 6 | PREFIX hexl 7 | GIT_REPOSITORY https://github.com/intel/hexl 8 | GIT_TAG f95acf1 # 1.2.5 9 | ) 10 | FetchContent_GetProperties(hexl) 11 | 12 | if(NOT hexl_POPULATED) 13 | FetchContent_Populate(hexl) 14 | set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING "" FORCE) 15 | set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE STRING "" FORCE) 16 | set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE STRING "" FORCE) 17 | set(HEXL_BENCHMARK OFF CACHE BOOL "" FORCE) 18 | set(HEXL_COVERAGE OFF CACHE BOOL "" FORCE) 19 | set(HEXL_TESTING OFF CACHE BOOL "" FORCE) 20 | set(HEXL_SHARED_LIB ${BUILD_SHARED_LIBS} CACHE BOOL "" FORCE) 21 | set(EXCLUDE_FROM_ALL TRUE) 22 | 23 | mark_as_advanced(BUILD_HEXL) 24 | mark_as_advanced(INSTALL_HEXL) 25 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_HEXL) 26 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_HEXL) 27 | 28 | add_subdirectory( 29 | ${hexl_SOURCE_DIR} 30 | ${hexl_SOURCE_DIR}/../hexl-build 31 | EXCLUDE_FROM_ALL 32 | ) 33 | endif() 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) IDEA Corporation. All rights reserved. 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 | -------------------------------------------------------------------------------- /native/tests/seal/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | target_sources(sealtest 5 | PRIVATE 6 | # ${CMAKE_CURRENT_LIST_DIR}/ciphertext.cpp 7 | # ${CMAKE_CURRENT_LIST_DIR}/ckks.cpp 8 | # ${CMAKE_CURRENT_LIST_DIR}/context.cpp 9 | # ${CMAKE_CURRENT_LIST_DIR}/encryptionparams.cpp 10 | # ${CMAKE_CURRENT_LIST_DIR}/encryptor.cpp 11 | ${CMAKE_CURRENT_LIST_DIR}/evaluator.cpp 12 | # ${CMAKE_CURRENT_LIST_DIR}/galoiskeys.cpp 13 | # ${CMAKE_CURRENT_LIST_DIR}/dynarray.cpp 14 | # ${CMAKE_CURRENT_LIST_DIR}/keygenerator.cpp 15 | # ${CMAKE_CURRENT_LIST_DIR}/memorymanager.cpp 16 | # ${CMAKE_CURRENT_LIST_DIR}/modulus.cpp 17 | # ${CMAKE_CURRENT_LIST_DIR}/plaintext.cpp 18 | # ${CMAKE_CURRENT_LIST_DIR}/publickey.cpp 19 | # ${CMAKE_CURRENT_LIST_DIR}/randomgen.cpp 20 | # ${CMAKE_CURRENT_LIST_DIR}/randomtostd.cpp 21 | # ${CMAKE_CURRENT_LIST_DIR}/relinkeys.cpp 22 | # ${CMAKE_CURRENT_LIST_DIR}/secretkey.cpp 23 | # ${CMAKE_CURRENT_LIST_DIR}/serialization.cpp 24 | ${CMAKE_CURRENT_LIST_DIR}/testrunner.cpp 25 | ) 26 | 27 | add_subdirectory(util) 28 | -------------------------------------------------------------------------------- /cmake/ExternalZSTD.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | FetchContent_Declare( 5 | zstd 6 | GIT_REPOSITORY https://github.com/facebook/zstd.git 7 | GIT_TAG e47e674cd09583ff0503f0f6defd6d23d8b718d3 # 1.5.2 8 | ) 9 | FetchContent_GetProperties(zstd) 10 | if(NOT zstd_POPULATED) 11 | FetchContent_Populate(zstd) 12 | 13 | set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "" FORCE) 14 | set(ZSTD_BUILD_SHARED OFF CACHE BOOL "" FORCE) 15 | set(ZLIB_BUILD_STATIC ON CACHE BOOL "" FORCE) 16 | set(ZSTD_BUILD_TESTS OFF CACHE BOOL "" FORCE) 17 | set(ZSTD_MULTITHREAD_SUPPORT OFF CACHE BOOL "" FORCE) 18 | mark_as_advanced(ZSTD_BUILD_CONTRIB) 19 | mark_as_advanced(ZSTD_BUILD_PROGRAMS) 20 | mark_as_advanced(ZSTD_BUILD_SHARED) 21 | mark_as_advanced(ZSTD_BUILD_STATIC) 22 | mark_as_advanced(ZSTD_BUILD_TESTS) 23 | mark_as_advanced(ZSTD_LEGACY_SUPPORT) 24 | mark_as_advanced(ZSTD_MULTITHREAD_SUPPORT) 25 | mark_as_advanced(ZSTD_PROGRAMS_LINK_SHARED) 26 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_ZSTD) 27 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_ZSTD) 28 | 29 | add_subdirectory( 30 | ${zstd_SOURCE_DIR}/build/cmake 31 | ${zstd_SOURCE_DIR}/../zstd-build 32 | EXCLUDE_FROM_ALL) 33 | endif() 34 | -------------------------------------------------------------------------------- /native/src/seal/util/common.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #ifndef __STDC_WANT_LIB_EXT1__ 5 | #define __STDC_WANT_LIB_EXT1__ 1 6 | #endif 7 | 8 | #include "seal/util/common.h" 9 | #include 10 | 11 | #if (SEAL_SYSTEM == SEAL_SYSTEM_WINDOWS) 12 | #include 13 | #endif 14 | 15 | using namespace std; 16 | 17 | namespace seal 18 | { 19 | namespace util 20 | { 21 | void seal_memzero(void *data, size_t size) 22 | { 23 | #if (SEAL_SYSTEM == SEAL_SYSTEM_WINDOWS) 24 | SecureZeroMemory(data, size); 25 | #elif defined(SEAL_USE_MEMSET_S) 26 | if (size > 0U && memset_s(data, static_cast(size), 0, static_cast(size)) != 0) 27 | { 28 | throw runtime_error("error calling memset_s"); 29 | } 30 | #elif defined(SEAL_USE_EXPLICIT_BZERO) 31 | explicit_bzero(data, size); 32 | #elif defined(SEAL_USE_EXPLICIT_MEMSET) 33 | explicit_memset(data, 0, size); 34 | #else 35 | volatile seal_byte *data_ptr = reinterpret_cast(data); 36 | while (size--) 37 | { 38 | *data_ptr++ = seal_byte{}; 39 | } 40 | #endif 41 | } 42 | } // namespace util 43 | } // namespace seal 44 | -------------------------------------------------------------------------------- /native/src/seal/util/hash.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/blake2.h" 7 | #include "seal/util/common.h" 8 | #include "seal/util/defines.h" 9 | #include 10 | #include 11 | #include 12 | 13 | namespace seal 14 | { 15 | namespace util 16 | { 17 | class HashFunction 18 | { 19 | public: 20 | HashFunction() = delete; 21 | 22 | static constexpr std::size_t hash_block_uint64_count = 4; 23 | 24 | static constexpr std::size_t hash_block_byte_count = hash_block_uint64_count * bytes_per_uint64; 25 | 26 | using hash_block_type = std::array; 27 | 28 | static constexpr hash_block_type hash_zero_block{ { 0, 0, 0, 0 } }; 29 | 30 | inline static void hash(const std::uint64_t *input, std::size_t uint64_count, hash_block_type &destination) 31 | { 32 | if (blake2b(&destination, hash_block_byte_count, input, uint64_count * bytes_per_uint64, nullptr, 0) != 33 | 0) 34 | { 35 | throw std::runtime_error("blake2b failed"); 36 | } 37 | } 38 | }; 39 | } // namespace util 40 | } // namespace seal 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ***English:*** 2 | 3 | # FHE GPU Acceleration Library 4 | **This project is based on the framework of [Microsoft SEAL library](https://github.com/microsoft/SEAL), and the original interface is completely retained** 5 | 6 | Users do not need to modify the code as you are using the original SEAL, and can directly benifit from this project for GPU acceleration calculation. 7 | 8 | ## Acceleration 9 | 10 | More details and test results can be found in our paper. 11 | 12 | ## Preparation 13 | 1. Install CUDA 14 | Download and install CUDA 11.7 15 | https://developer.nvidia.com/cuda-11-7-0-download-archive 16 | 17 | 2. Install CMake 18 | Install CMake 3.18.3 19 | 20 | ## Compilation 21 | Execute in the root directory 22 | ``` 23 | cmake -S . -B build 24 | cd build 25 | make 26 | ``` 27 | 28 | Test: 29 | ``` 30 | cd build/bin 31 | ./sealexamples 32 | ``` 33 | 34 | ***中文:*** 35 | # FHE GPU加速库 36 | **该项目基于[微软SEAL库](https://github.com/microsoft/SEAL)框架进行完成,完全保留原有接口** 37 | 38 | 用户无需修改原有使用SEAL时的代码,可以直接使用本项目进行GPU加速计算。 39 | 40 | 41 | ## 加速效果 42 | 加速效果和细节在我们的论文中可详细查阅。 43 | 44 | ## 准备操作 45 | 1. 安装CUDA 46 | 下载安装11.7版本CUDA 47 | https://developer.nvidia.com/cuda-11-7-0-download-archive 48 | 2. 安装CMake 49 | 安装3.18.3版本cmake 50 | 51 | 52 | ## 编译方法 53 | 根目录下执行 54 | ``` 55 | cmake -S . -B build 56 | cd build 57 | make 58 | ``` 59 | 60 | 测试: 61 | ``` 62 | cd build/bin 63 | ./sealexamples 64 | ``` 65 | -------------------------------------------------------------------------------- /cmake/functions.iOS.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # This cache-initialization file will be used to enable functions when cross-compiling 5 | # for iOS, as check_cxx_source_runs fails for cross-compilers. 6 | 7 | set(SEAL_MEMSET_S_FOUND_EXITCODE 8 | "0" 9 | CACHE STRING "Result from TRY_RUN" FORCE) 10 | 11 | set(SEAL_MEMSET_S_FOUND_EXITCODE__TRYRUN_OUTPUT 12 | "" 13 | CACHE STRING "Output from TRY_RUN" FORCE) 14 | 15 | set(SEAL_ARM64_EXITCODE 16 | "0" 17 | CACHE STRING "Result from TRY_RUN" FORCE) 18 | 19 | set (SEAL_ARM64_EXITCODE__TRYRUN_OUTPUT 20 | "" 21 | CACHE STRING "Output from TRY_RUN" FORCE) 22 | 23 | set(SEAL___BUILTIN_CLZLL_FOUND_EXITCODE 24 | "0" 25 | CACHE STRING "Result from TRY_RUN" FORCE) 26 | 27 | set(SEAL___BUILTIN_CLZLL_FOUND_EXITCODE__TRYRUN_OUTPUT 28 | "" 29 | CACHE STRING "Output from TRY_RUN" FORCE) 30 | 31 | set(SEAL__ADDCARRY_U64_FOUND_EXITCODE 32 | "0" 33 | CACHE STRING "Result from TRY_RUN" FORCE) 34 | 35 | set(SEAL__ADDCARRY_U64_FOUND_EXITCODE__TRYRUN_OUTPUT 36 | "" 37 | CACHE STRING "Output from TRY_RUN" FORCE) 38 | 39 | set(SEAL__SUBBORROW_U64_FOUND_EXITCODE 40 | "0" 41 | CACHE STRING "Result from TRY_RUN" FORCE) 42 | 43 | set(SEAL__SUBBORROW_U64_FOUND_EXITCODE__TRYRUN_OUTPUT 44 | "" 45 | CACHE STRING "Output from TRY_RUN" FORCE) 46 | -------------------------------------------------------------------------------- /native/src/seal/version.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/defines.h" 7 | #include 8 | 9 | namespace seal 10 | { 11 | /** 12 | Holds IDEA SEAL_GPU version information. A SEALVersion contains four values: 13 | 14 | 1. The major version number; 15 | 2. The minor version number; 16 | 3. The patch version number; 17 | 4. The tweak version number. 18 | 19 | Two versions of the library with the same major and minor versions are fully 20 | compatible with each other. They are guaranteed to have the same public API. 21 | Changes in the patch version number indicate totally internal changes, such 22 | as bug fixes that require no changes to the public API. The tweak version 23 | number is currently not used, and is expected to be 0. 24 | */ 25 | struct SEALVersion 26 | { 27 | /** 28 | Holds the major version number. 29 | */ 30 | std::uint8_t major = SEAL_VERSION_MAJOR; 31 | 32 | /** 33 | Holds the minor version number. 34 | */ 35 | std::uint8_t minor = SEAL_VERSION_MINOR; 36 | 37 | /** 38 | Holds the patch version number. 39 | */ 40 | std::uint8_t patch = SEAL_VERSION_PATCH; 41 | 42 | std::uint8_t tweak = 0; 43 | }; 44 | } // namespace seal 45 | -------------------------------------------------------------------------------- /native/src/seal/util/scalingvariant.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/context.cuh" 7 | #include "seal/memorymanager.h" 8 | #include "seal/plaintext.cuh" 9 | #include "seal/util/iterator.h" 10 | #include 11 | 12 | namespace seal 13 | { 14 | namespace util 15 | { 16 | void add_plain_without_scaling_variant( 17 | const Plaintext &plain, const SEALContext::ContextData &context_data, RNSIter destination); 18 | 19 | void sub_plain_without_scaling_variant( 20 | const Plaintext &plain, const SEALContext::ContextData &context_data, RNSIter destination); 21 | 22 | void multiply_add_plain_with_scaling_variant( 23 | const Plaintext &plain, const SEALContext::ContextData &context_data, RNSIter destination); 24 | 25 | void multiply_add_plain_with_scaling_variant_cuda( 26 | const Plaintext &plain, const SEALContext::ContextData &context_data, uint64_t *destination); 27 | 28 | void multiply_sub_plain_with_scaling_variant( 29 | const Plaintext &plain, const SEALContext::ContextData &context_data, RNSIter destination); 30 | 31 | void multiply_sub_plain_with_scaling_variant_cuda( 32 | const Plaintext &plain, const SEALContext::ContextData &context_data, uint64_t *destination); 33 | } // namespace util 34 | } // namespace seal 35 | -------------------------------------------------------------------------------- /cmake/ExternalZLIB.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. Licensed under the MIT license. 2 | 3 | FetchContent_Declare( 4 | zlib 5 | GIT_REPOSITORY https://github.com/madler/zlib.git 6 | GIT_TAG 04f42ceca40f73e2978b50e93806c2a18c1281fc # 1.2.13 7 | ) 8 | FetchContent_GetProperties(zlib) 9 | if(NOT zlib_POPULATED) 10 | FetchContent_Populate(zlib) 11 | 12 | set(SKIP_INSTALL_ALL ON CACHE BOOL "" FORCE) 13 | mark_as_advanced(AMD64) 14 | mark_as_advanced(ASM686) 15 | mark_as_advanced(EXECUTABLE_OUTPUT_PATH) 16 | mark_as_advanced(CMAKE_INSTALL_PREFIX) 17 | mark_as_advanced(INSTALL_BIN_DIR) 18 | mark_as_advanced(INSTALL_INC_DIR) 19 | mark_as_advanced(INSTALL_LIB_DIR) 20 | mark_as_advanced(INSTALL_MAN_DIR) 21 | mark_as_advanced(INSTALL_PKGCONFIG_DIR) 22 | mark_as_advanced(LIBRARY_OUTPUT_PATH) 23 | mark_as_advanced(CMAKE_BACKWARDS_COMPATIBILITY) 24 | mark_as_advanced(ZLIB_BUILD_STATIC) 25 | mark_as_advanced(SKIP_INSTALL_ALL) 26 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_ZLIB) 27 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_ZLIB) 28 | 29 | # ZLIB has no VERSION given to project(), needs to suppress CMP0048 warning 30 | set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS TRUE CACHE INTERNAL "Suppress CMP0048 warning" FORCE) 31 | add_subdirectory( 32 | ${zlib_SOURCE_DIR} 33 | ${zlib_SOURCE_DIR}/../zlib-build 34 | EXCLUDE_FROM_ALL) 35 | endif() 36 | -------------------------------------------------------------------------------- /native/tests/seal/util/hash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/util/hash.h" 5 | #include 6 | #include "gtest/gtest.h" 7 | 8 | using namespace seal::util; 9 | using namespace std; 10 | 11 | namespace sealtest 12 | { 13 | namespace util 14 | { 15 | namespace 16 | { 17 | void hash(uint64_t value, HashFunction::hash_block_type &destination) 18 | { 19 | HashFunction::hash(&value, 1, destination); 20 | } 21 | } // namespace 22 | 23 | TEST(HashTest, Hash) 24 | { 25 | uint64_t input[3]{ 0, 0, 0 }; 26 | HashFunction::hash_block_type hash1, hash2; 27 | hash(0, hash1); 28 | 29 | HashFunction::hash(input, 0, hash2); 30 | ASSERT_TRUE(hash1 != hash2); 31 | 32 | HashFunction::hash(input, 1, hash2); 33 | ASSERT_TRUE(hash1 == hash2); 34 | 35 | HashFunction::hash(input, 2, hash2); 36 | ASSERT_TRUE(hash1 != hash2); 37 | 38 | hash(0x123456, hash1); 39 | hash(0x023456, hash2); 40 | ASSERT_TRUE(hash1 != hash2); 41 | 42 | input[0] = 0x123456; 43 | input[1] = 1; 44 | hash(0x123456, hash1); 45 | HashFunction::hash(input, 2, hash2); 46 | ASSERT_TRUE(hash1 != hash2); 47 | } 48 | } // namespace util 49 | } // namespace sealtest 50 | -------------------------------------------------------------------------------- /cmake/EnableCXX17.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | set(SEAL_USE_STD_BYTE OFF) 5 | set(SEAL_USE_SHARED_MUTEX OFF) 6 | set(SEAL_USE_IF_CONSTEXPR OFF) 7 | set(SEAL_USE_MAYBE_UNUSED OFF) 8 | set(SEAL_USE_NODISCARD OFF) 9 | set(SEAL_USE_STD_FOR_EACH_N OFF) 10 | set(SEAL_LANG_FLAG "-std=c++14") 11 | if(SEAL_USE_CXX17) 12 | set(SEAL_USE_STD_BYTE ON) 13 | set(SEAL_USE_SHARED_MUTEX ON) 14 | set(SEAL_USE_IF_CONSTEXPR ON) 15 | set(SEAL_USE_MAYBE_UNUSED ON) 16 | set(SEAL_USE_NODISCARD ON) 17 | set(SEAL_USE_STD_FOR_EACH_N ON) 18 | set(SEAL_LANG_FLAG "-std=c++17") 19 | endif() 20 | 21 | # In some non-MSVC compilers std::for_each_n is not available even when compiling as C++17 22 | if(SEAL_USE_STD_FOR_EACH_N) 23 | cmake_push_check_state(RESET) 24 | set(CMAKE_REQUIRED_QUIET TRUE) 25 | 26 | if(NOT MSVC) 27 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -O0 ${SEAL_LANG_FLAG}") 28 | check_cxx_source_compiles(" 29 | #include 30 | int main() { 31 | int a[1]{ 0 }; 32 | volatile auto fun = std::for_each_n(a, 1, [](auto b) {}); 33 | return 0; 34 | }" 35 | USE_STD_FOR_EACH_N 36 | ) 37 | if(NOT USE_STD_FOR_EACH_N EQUAL 1) 38 | set(SEAL_USE_STD_FOR_EACH_N OFF) 39 | endif() 40 | unset(USE_STD_FOR_EACH_N CACHE) 41 | endif() 42 | 43 | cmake_pop_check_state() 44 | endif() 45 | -------------------------------------------------------------------------------- /native/src/seal/util/config.h.in: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #define SEAL_VERSION "@SEAL_VERSION@" 7 | #define SEAL_VERSION_MAJOR @SEAL_VERSION_MAJOR@ 8 | #define SEAL_VERSION_MINOR @SEAL_VERSION_MINOR@ 9 | #define SEAL_VERSION_PATCH @SEAL_VERSION_PATCH@ 10 | 11 | // Are we in debug mode? 12 | #cmakedefine SEAL_DEBUG 13 | 14 | // C++17 features 15 | #cmakedefine SEAL_USE_STD_BYTE 16 | #cmakedefine SEAL_USE_ALIGNED_ALLOC 17 | #cmakedefine SEAL_USE_SHARED_MUTEX 18 | #cmakedefine SEAL_USE_IF_CONSTEXPR 19 | #cmakedefine SEAL_USE_MAYBE_UNUSED 20 | #cmakedefine SEAL_USE_NODISCARD 21 | #cmakedefine SEAL_USE_STD_FOR_EACH_N 22 | 23 | // Security 24 | #cmakedefine SEAL_THROW_ON_TRANSPARENT_CIPHERTEXT 25 | #cmakedefine SEAL_USE_GAUSSIAN_NOISE 26 | #cmakedefine SEAL_DEFAULT_PRNG @SEAL_DEFAULT_PRNG@ 27 | #cmakedefine SEAL_AVOID_BRANCHING 28 | 29 | // Intrinsics 30 | #cmakedefine SEAL_USE_INTRIN 31 | #cmakedefine SEAL_USE__UMUL128 32 | #cmakedefine SEAL_USE__BITSCANREVERSE64 33 | #cmakedefine SEAL_USE___BUILTIN_CLZLL 34 | #cmakedefine SEAL_USE___INT128 35 | #cmakedefine SEAL_USE__ADDCARRY_U64 36 | #cmakedefine SEAL_USE__SUBBORROW_U64 37 | 38 | // Zero memory functions 39 | #cmakedefine SEAL_USE_EXPLICIT_BZERO 40 | #cmakedefine SEAL_USE_EXPLICIT_MEMSET 41 | #cmakedefine SEAL_USE_MEMSET_S 42 | 43 | // Third-party dependencies 44 | #cmakedefine SEAL_USE_MSGSL 45 | #cmakedefine SEAL_USE_ZLIB 46 | #cmakedefine SEAL_USE_ZSTD 47 | #cmakedefine SEAL_USE_INTEL_HEXL 48 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 使用Ubuntu 18.04作为基础镜像 2 | FROM ubuntu:18.04 3 | 4 | # 设置环境变量以避免在安装过程中出现交互式提示 5 | ENV DEBIAN_FRONTEND=noninteractive 6 | 7 | # 安装基本工具和依赖 8 | RUN apt-get update && apt-get install -y \ 9 | build-essential \ 10 | software-properties-common \ 11 | git \ 12 | wget \ 13 | curl \ 14 | ca-certificates \ 15 | lsb-release \ 16 | gnupg \ 17 | && rm -rf /var/lib/apt/lists/* 18 | 19 | # 安装CMake 3.18.3 20 | RUN wget https://github.com/Kitware/CMake/releases/download/v3.18.3/cmake-3.18.3-Linux-x86_64.sh \ 21 | && chmod +x cmake-3.18.3-Linux-x86_64.sh \ 22 | && ./cmake-3.18.3-Linux-x86_64.sh --skip-license --prefix=/usr/local \ 23 | && rm cmake-3.18.3-Linux-x86_64.sh 24 | 25 | # 添加NVIDIA GPG key 26 | RUN curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub | apt-key add - 27 | 28 | # 添加NVIDIA CUDA repository 29 | RUN echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" > /etc/apt/sources.list.d/cuda.list 30 | 31 | # 安装CUDA 11.7 32 | RUN apt-get update && apt-get install -y --no-install-recommends \ 33 | cuda-toolkit-11-7 \ 34 | && rm -rf /var/lib/apt/lists/* 35 | 36 | # 设置CUDA环境变量 37 | ENV CUDA_HOME=/usr/local/cuda 38 | ENV PATH=$CUDA_HOME/bin:$PATH 39 | ENV LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH 40 | 41 | # 复制当前git库的代码到容器中 42 | COPY . /my_project 43 | 44 | # 设置工作目录 45 | WORKDIR /my_project 46 | 47 | # 编译代码 48 | # 假设你的项目有一个名为build.sh的脚本来编译项目 49 | RUN chmod +x build.sh && ./build.sh 50 | 51 | # 设置默认命令 52 | CMD ["bash"] 53 | -------------------------------------------------------------------------------- /native/tests/seal/randomtostd.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/randomgen.h" 5 | #include "seal/randomtostd.h" 6 | #include 7 | #include 8 | #include "gtest/gtest.h" 9 | 10 | using namespace seal; 11 | using namespace std; 12 | 13 | namespace sealtest 14 | { 15 | TEST(RandomToStandard, RandomToStandardGenerate) 16 | { 17 | shared_ptr generator(UniformRandomGeneratorFactory::DefaultFactory()->create()); 18 | RandomToStandardAdapter rand(generator); 19 | ASSERT_TRUE(rand.generator() == generator); 20 | ASSERT_EQ(static_cast(0), rand.min()); 21 | ASSERT_EQ(static_cast(UINT32_MAX), rand.max()); 22 | bool lower_half = false; 23 | bool upper_half = false; 24 | bool even = false; 25 | bool odd = false; 26 | for (int i = 0; i < 50; i++) 27 | { 28 | uint32_t value = rand(); 29 | if (value < UINT32_MAX / 2) 30 | { 31 | lower_half = true; 32 | } 33 | else 34 | { 35 | upper_half = true; 36 | } 37 | if ((value % 2) == 0) 38 | { 39 | even = true; 40 | } 41 | else 42 | { 43 | odd = true; 44 | } 45 | } 46 | ASSERT_TRUE(lower_half); 47 | ASSERT_TRUE(upper_half); 48 | ASSERT_TRUE(even); 49 | ASSERT_TRUE(odd); 50 | } 51 | } // namespace sealtest 52 | -------------------------------------------------------------------------------- /native/tests/seal/util/clipnormal.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/randomgen.h" 5 | #include "seal/randomtostd.h" 6 | #include "seal/util/clipnormal.h" 7 | #include 8 | #include 9 | #include "gtest/gtest.h" 10 | 11 | using namespace seal::util; 12 | using namespace seal; 13 | using namespace std; 14 | 15 | namespace sealtest 16 | { 17 | namespace util 18 | { 19 | TEST(ClipNormal, ClipNormalGenerate) 20 | { 21 | shared_ptr generator(UniformRandomGeneratorFactory::DefaultFactory()->create()); 22 | RandomToStandardAdapter rand(generator); 23 | ClippedNormalDistribution dist(50.0, 10.0, 20.0); 24 | 25 | ASSERT_EQ(50.0, dist.mean()); 26 | ASSERT_EQ(10.0, dist.standard_deviation()); 27 | ASSERT_EQ(20.0, dist.max_deviation()); 28 | ASSERT_EQ(30.0, dist.min()); 29 | ASSERT_EQ(70.0, dist.max()); 30 | double average = 0; 31 | double stddev = 0; 32 | for (int i = 0; i < 100; ++i) 33 | { 34 | double value = dist(rand); 35 | average += value; 36 | stddev += (value - 50.0) * (value - 50.0); 37 | ASSERT_TRUE(value >= 30.0 && value <= 70.0); 38 | } 39 | average /= 100; 40 | stddev /= 100; 41 | stddev = sqrt(stddev); 42 | ASSERT_TRUE(average >= 40.0 && average <= 60.0); 43 | ASSERT_TRUE(stddev >= 5.0 && stddev <= 15.0); 44 | } 45 | } // namespace util 46 | } // namespace sealtest 47 | -------------------------------------------------------------------------------- /native/examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | cmake_minimum_required(VERSION 3.13) 5 | 6 | set(CMAKE_CUDA_STANDARD 17) 7 | set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc") 8 | project(SEALExamples VERSION 4.1.1 LANGUAGES CXX CUDA) 9 | 10 | find_package(CUDA REQUIRED) 11 | include_directories(${CUDA_INCLUDE_DIRS}) 12 | # If not called from root CMakeLists.txt 13 | if(NOT DEFINED SEAL_BUILD_EXAMPLES) 14 | set(SEAL_BUILD_EXAMPLES ON) 15 | 16 | # Import IDEA SEAL_GPU 17 | find_package(SEAL 4.1.1 EXACT REQUIRED) 18 | 19 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) 20 | endif() 21 | 22 | if(SEAL_BUILD_EXAMPLES) 23 | add_executable(sealexamples) 24 | target_sources(sealexamples 25 | PRIVATE 26 | # ${CMAKE_CURRENT_LIST_DIR}/examples.cpp 27 | # ${CMAKE_CURRENT_LIST_DIR}/1_bfv_basics.cpp 28 | # ${CMAKE_CURRENT_LIST_DIR}/2_encoders.cpp 29 | # ${CMAKE_CURRENT_LIST_DIR}/3_levels.cpp 30 | # ${CMAKE_CURRENT_LIST_DIR}/4_ckks_basics.cpp 31 | # ${CMAKE_CURRENT_LIST_DIR}/5_rotation.cpp 32 | ${CMAKE_CURRENT_LIST_DIR}/6_performance.cpp 33 | ) 34 | 35 | 36 | if(TARGET SEAL::seal) 37 | target_link_libraries(sealexamples PRIVATE SEAL::seal) 38 | message(STATUS "Using SEAL::seal") 39 | elseif(TARGET SEAL::seal_shared) 40 | set_target_properties(sealexamples PROPERTIES 41 | CUDA_SEPARABLE_COMPILATION ON 42 | POSITION_INDEPENDENT_CODE ON 43 | ) 44 | 45 | target_link_libraries(sealexamples PRIVATE SEAL::seal_shared "/usr/local/cuda-11.7/lib64/libcudart.so.11.0") 46 | else() 47 | message(FATAL_ERROR "Cannot find target SEAL::seal or SEAL::seal_shared") 48 | endif() 49 | endif() 50 | -------------------------------------------------------------------------------- /native/examples/build/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/liqirui/SEAL/native/examples 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | # Set default install directory permissions. 41 | if(NOT DEFINED CMAKE_OBJDUMP) 42 | set(CMAKE_OBJDUMP "/usr/bin/objdump") 43 | endif() 44 | 45 | if(CMAKE_INSTALL_COMPONENT) 46 | set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt") 47 | else() 48 | set(CMAKE_INSTALL_MANIFEST "install_manifest.txt") 49 | endif() 50 | 51 | string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT 52 | "${CMAKE_INSTALL_MANIFEST_FILES}") 53 | file(WRITE "/home/liqirui/SEAL/native/examples/build/${CMAKE_INSTALL_MANIFEST}" 54 | "${CMAKE_INSTALL_MANIFEST_CONTENT}") 55 | -------------------------------------------------------------------------------- /native/src/seal/util/common.cuh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "helper.cuh" 3 | #include "ntt_60bit.cuh" 4 | #include "ntt_helper.cuh" 5 | 6 | namespace seal 7 | { 8 | namespace util 9 | { 10 | template || is_uint64_v>> 11 | __device__ inline constexpr T reverse_bits_kernel(T operand) noexcept 12 | { 13 | T temp_result; 14 | if (sizeof(T) == sizeof(std::uint32_t)) 15 | { 16 | operand = (((operand & T(0xaaaaaaaa)) >> 1) | ((operand & T(0x55555555)) << 1)); 17 | operand = (((operand & T(0xcccccccc)) >> 2) | ((operand & T(0x33333333)) << 2)); 18 | operand = (((operand & T(0xf0f0f0f0)) >> 4) | ((operand & T(0x0f0f0f0f)) << 4)); 19 | operand = (((operand & T(0xff00ff00)) >> 8) | ((operand & T(0x00ff00ff)) << 8)); 20 | return static_cast(operand >> 16) | static_cast(operand << 16); 21 | } 22 | else if (sizeof(T) == sizeof(std::uint64_t)) 23 | { 24 | return static_cast(reverse_bits_kernel(static_cast(operand >> 32))) | 25 | (static_cast(reverse_bits_kernel(static_cast(operand & T(0xFFFFFFFF)))) 26 | << 32); 27 | } 28 | } 29 | 30 | template || is_uint64_v>> 31 | __device__ inline T reverse_bits_kernel(T operand, int bit_count) 32 | { 33 | int bits_per_byte = 8; 34 | return (bit_count == 0) 35 | ? T(0) 36 | : reverse_bits_kernel(operand) >> (sizeof(T) * static_cast(bits_per_byte) - 37 | static_cast(bit_count)); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /cmake/ExternalBenchmark.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | FetchContent_Declare( 5 | benchmark 6 | GIT_REPOSITORY https://github.com/google/benchmark.git 7 | GIT_TAG d572f4777349d43653b21d6c2fc63020ab326db2 # 1.7.1 8 | ) 9 | FetchContent_GetProperties(benchmark) 10 | 11 | if(NOT benchmark) 12 | FetchContent_Populate(benchmark) 13 | 14 | set(LLVMAR_EXECUTABLE ${CMAKE_AR}) 15 | set(LLVMNM_EXECUTABLE ${CMAKE_NM}) 16 | set(LLVMRANLIB_EXECUTABLE ${CMAKE_RANLIB}) 17 | set(LLVM_FILECHECK_EXE ${CMAKE_CXX_COMPILER_AR}/../FileCheck) 18 | set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE) 19 | set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE) 20 | set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) 21 | set(BENCHMARK_ENABLE_LTO OFF CACHE BOOL "" FORCE) 22 | mark_as_advanced(LIBRT) 23 | mark_as_advanced(LLVM_FILECHECK_EXE) 24 | mark_as_advanced(BENCHMARK_BUILD_32_BITS) 25 | mark_as_advanced(BENCHMARK_DOWNLOAD_DEPENDENCIES) 26 | mark_as_advanced(BENCHMARK_ENABLE_ASSEMBLY_TESTS) 27 | mark_as_advanced(BENCHMARK_ENABLE_EXCEPTIONS) 28 | mark_as_advanced(BENCHMARK_ENABLE_GTEST_TESTS) 29 | mark_as_advanced(BENCHMARK_ENABLE_INSTALL) 30 | mark_as_advanced(BENCHMARK_ENABLE_LTO) 31 | mark_as_advanced(BENCHMARK_ENABLE_TESTING) 32 | mark_as_advanced(BENCHMARK_USE_LIBCXX) 33 | mark_as_advanced(FETCHCONTENT_SOURCE_DIR_BENCHMARK) 34 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_BENCHMARK) 35 | 36 | if(NOT WIN32) 37 | # Google Benchmark contains unsafe conversions so force -Wno-conversion temporarily 38 | set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) 39 | set(CMAKE_CXX_FLAGS "${OLD_CMAKE_CXX_FLAGS} -Wno-conversion") 40 | endif() 41 | 42 | add_subdirectory( 43 | ${benchmark_SOURCE_DIR} 44 | ${THIRDPARTY_BINARY_DIR}/benchmark-src 45 | EXCLUDE_FROM_ALL) 46 | 47 | if(NOT WIN32) 48 | set(CMAKE_CXX_FLAGS ${OLD_CMAKE_CXX_FLAGS}) 49 | endif() 50 | endif() 51 | -------------------------------------------------------------------------------- /native/src/seal/util/gpu_data.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "seal/util/common.h" 4 | #include "seal/util/helper.cuh" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace seal 11 | { 12 | class GPUData 13 | { 14 | public: 15 | GPUData() : data_(nullptr) {} 16 | 17 | GPUData(size_t size) { 18 | d_capacity_ = size; 19 | allocate_gpu(&rawPtr_, d_capacity_); 20 | }; 21 | 22 | GPUData(uint64_t* data, size_t size) { 23 | d_capacity_ = size; 24 | allocate_gpu(&rawPtr_, d_capacity_); 25 | cudaMemcpy(rawPtr_, data, d_capacity_ * sizeof(uint64_t), cudaMemcpyHostToDevice); 26 | } 27 | 28 | ~GPUData() { 29 | if (data_ != nullptr) { 30 | deallocate_gpu(&rawPtr_, d_capacity_); 31 | } 32 | } 33 | 34 | 35 | uint64_t* data() { 36 | return data_.get(); 37 | } 38 | 39 | uint64_t* data() const { 40 | return data_.get(); 41 | } 42 | 43 | void setData(std::shared_ptr newData) { 44 | data_ = newData; // 使用复制构造函数,确保共享所有权 45 | } 46 | 47 | size_t capacity() { 48 | return d_capacity_; 49 | } 50 | 51 | void alloc(size_t size) { 52 | if (size > d_capacity_) { 53 | release(); 54 | d_capacity_ = size; 55 | allocate_gpu(&rawPtr_, d_capacity_); 56 | } 57 | } 58 | 59 | void release() { 60 | deallocate_gpu(&rawPtr_, d_capacity_); 61 | d_capacity_ = 0; 62 | } 63 | 64 | std::shared_ptr data_ = nullptr; 65 | size_t d_capacity_ = 0; 66 | 67 | private: 68 | uint64_t* rawPtr_ = data_.get(); 69 | }; 70 | } -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/CMakeError.log: -------------------------------------------------------------------------------- 1 | Performing C++ SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output: 2 | Change Dir: /home/liqirui/SEAL/native/examples/build/CMakeFiles/CMakeTmp 3 | 4 | Run Build Command(s):/usr/bin/make cmTC_7972a/fast && /usr/bin/make -f CMakeFiles/cmTC_7972a.dir/build.make CMakeFiles/cmTC_7972a.dir/build 5 | make[1]: Entering directory '/home/liqirui/SEAL/native/examples/build/CMakeFiles/CMakeTmp' 6 | Building CXX object CMakeFiles/cmTC_7972a.dir/src.cxx.o 7 | /usr/bin/c++ -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_7972a.dir/src.cxx.o -c /home/liqirui/SEAL/native/examples/build/CMakeFiles/CMakeTmp/src.cxx 8 | Linking CXX executable cmTC_7972a 9 | /home/cmake-3.18.3-Linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7972a.dir/link.txt --verbose=1 10 | /usr/bin/c++ -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_7972a.dir/src.cxx.o -o cmTC_7972a 11 | /usr/local/bin/ld: CMakeFiles/cmTC_7972a.dir/src.cxx.o: in function `main': 12 | src.cxx:(.text+0x2d): undefined reference to `pthread_create' 13 | /usr/local/bin/ld: src.cxx:(.text+0x39): undefined reference to `pthread_detach' 14 | /usr/local/bin/ld: src.cxx:(.text+0x45): undefined reference to `pthread_cancel' 15 | /usr/local/bin/ld: src.cxx:(.text+0x56): undefined reference to `pthread_join' 16 | /usr/local/bin/ld: src.cxx:(.text+0x6a): undefined reference to `pthread_atfork' 17 | collect2: error: ld returned 1 exit status 18 | CMakeFiles/cmTC_7972a.dir/build.make:105: recipe for target 'cmTC_7972a' failed 19 | make[1]: *** [cmTC_7972a] Error 1 20 | make[1]: Leaving directory '/home/liqirui/SEAL/native/examples/build/CMakeFiles/CMakeTmp' 21 | Makefile:140: recipe for target 'cmTC_7972a/fast' failed 22 | make: *** [cmTC_7972a/fast] Error 2 23 | 24 | 25 | Source file was: 26 | #include 27 | 28 | void* test_func(void* data) 29 | { 30 | return data; 31 | } 32 | 33 | int main(void) 34 | { 35 | pthread_t thread; 36 | pthread_create(&thread, NULL, test_func, NULL); 37 | pthread_detach(thread); 38 | pthread_cancel(thread); 39 | pthread_join(thread, NULL); 40 | pthread_atfork(NULL, NULL, NULL); 41 | pthread_exit(NULL); 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.c: -------------------------------------------------------------------------------- 1 | # 1 "CMakeCUDACompilerId.cu" 2 | # 64 "CMakeCUDACompilerId.cu" 3 | extern const char *info_compiler; 4 | 5 | extern const char *info_simulate; 6 | # 305 "CMakeCUDACompilerId.cu" 7 | static const char info_version[50]; 8 | # 332 "CMakeCUDACompilerId.cu" 9 | static const char info_simulate_version[41]; 10 | # 352 "CMakeCUDACompilerId.cu" 11 | extern const char *info_platform; 12 | extern const char *info_arch; 13 | 14 | 15 | 16 | 17 | extern const char *info_language_dialect_default; 18 | # 64 "CMakeCUDACompilerId.cu" 19 | const char *info_compiler = ((const char *)"INFO:compiler[NVIDIA]"); 20 | 21 | const char *info_simulate = ((const char *)"INFO:simulate[GNU]"); 22 | # 305 "CMakeCUDACompilerId.cu" 23 | static const char info_version[50] = {((char)73),((char)78),((char)70),((char)79),((char)58),((char)99),((char)111),((char)109),((char)112),((char)105),((char)108),((char)101),((char)114),((char)95),((char)118),((char)101),((char)114),((char)115),((char)105),((char)111),((char)110),((char)91),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)49),((char)49),((char)46),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)55),((char)46),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)54),((char)52),((char)93),((char)0)}; 24 | # 332 "CMakeCUDACompilerId.cu" 25 | static const char info_simulate_version[41] = {((char)73),((char)78),((char)70),((char)79),((char)58),((char)115),((char)105),((char)109),((char)117),((char)108),((char)97),((char)116),((char)101),((char)95),((char)118),((char)101),((char)114),((char)115),((char)105),((char)111),((char)110),((char)91),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)56),((char)46),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)48),((char)50),((char)93),((char)0)}; 26 | # 352 "CMakeCUDACompilerId.cu" 27 | const char *info_platform = ((const char *)"INFO:platform[Linux]"); 28 | const char *info_arch = ((const char *)"INFO:arch[]"); 29 | 30 | 31 | 32 | 33 | const char *info_language_dialect_default = ((const char *)"INFO:dialect_default[14]"); 34 | -------------------------------------------------------------------------------- /native/bench/keygen.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/seal.h" 5 | #include "seal/util/rlwe.h" 6 | #include "bench.h" 7 | 8 | using namespace benchmark; 9 | using namespace sealbench; 10 | using namespace seal; 11 | using namespace std; 12 | 13 | /** 14 | This file defines benchmarks for KeyGen-related HE primitives. 15 | */ 16 | 17 | namespace seal 18 | { 19 | struct KeyGenerator::KeyGeneratorPrivateHelper 20 | { 21 | static void generate_sk(KeyGenerator *keygen) 22 | { 23 | return keygen->generate_sk(); 24 | } 25 | }; 26 | } // namespace seal 27 | 28 | namespace sealbench 29 | { 30 | void bm_keygen_secret(State &state, shared_ptr bm_env) 31 | { 32 | KeyGenerator keygen(bm_env->context()); 33 | for (auto _ : state) 34 | { 35 | KeyGenerator::KeyGeneratorPrivateHelper::generate_sk(&keygen); 36 | } 37 | } 38 | 39 | void bm_keygen_public(State &state, shared_ptr bm_env) 40 | { 41 | shared_ptr keygen = bm_env->keygen(); 42 | PublicKey pk; 43 | for (auto _ : state) 44 | { 45 | keygen->create_public_key(pk); 46 | } 47 | } 48 | 49 | void bm_keygen_relin(State &state, shared_ptr bm_env) 50 | { 51 | shared_ptr keygen = bm_env->keygen(); 52 | RelinKeys rlk; 53 | for (auto _ : state) 54 | { 55 | keygen->create_relin_keys(rlk); 56 | } 57 | } 58 | 59 | void bm_keygen_galois(State &state, shared_ptr bm_env) 60 | { 61 | shared_ptr keygen = bm_env->keygen(); 62 | GaloisKeys glk; 63 | size_t slot_count = bm_env->parms().poly_modulus_degree() >> 1; 64 | 65 | auto random_one_step = [&]() { 66 | vector res; 67 | res.emplace_back(rand() & static_cast(slot_count - 1)); 68 | return res; 69 | }; 70 | 71 | for (auto _ : state) 72 | { 73 | keygen->create_galois_keys({ random_one_step() }, glk); 74 | } 75 | } 76 | } // namespace sealbench 77 | -------------------------------------------------------------------------------- /native/tests/seal/secretkey.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/context.cuh" 5 | #include "seal/keygenerator.h" 6 | #include "seal/modulus.h" 7 | #include "seal/secretkey.cuh" 8 | #include "gtest/gtest.h" 9 | 10 | using namespace seal; 11 | using namespace std; 12 | 13 | namespace sealtest 14 | { 15 | TEST(SecretKeyTest, SaveLoadSecretKey) 16 | { 17 | auto save_load_secret_key = [](scheme_type scheme) { 18 | stringstream stream; 19 | { 20 | EncryptionParameters parms(scheme); 21 | parms.set_poly_modulus_degree(64); 22 | parms.set_plain_modulus(1 << 6); 23 | parms.set_coeff_modulus(CoeffModulus::Create(64, { 60 })); 24 | 25 | SEALContext context(parms, false, sec_level_type::none); 26 | KeyGenerator keygen(context); 27 | 28 | SecretKey sk = keygen.secret_key(); 29 | ASSERT_TRUE(sk.parms_id() == context.key_parms_id()); 30 | sk.save(stream); 31 | 32 | SecretKey sk2; 33 | sk2.load(context, stream); 34 | 35 | ASSERT_TRUE(sk.data() == sk2.data()); 36 | ASSERT_TRUE(sk.parms_id() == sk2.parms_id()); 37 | } 38 | { 39 | EncryptionParameters parms(scheme); 40 | parms.set_poly_modulus_degree(256); 41 | parms.set_plain_modulus(1 << 20); 42 | parms.set_coeff_modulus(CoeffModulus::Create(256, { 30, 40 })); 43 | 44 | SEALContext context(parms, false, sec_level_type::none); 45 | KeyGenerator keygen(context); 46 | 47 | SecretKey sk = keygen.secret_key(); 48 | ASSERT_TRUE(sk.parms_id() == context.key_parms_id()); 49 | sk.save(stream); 50 | 51 | SecretKey sk2; 52 | sk2.load(context, stream); 53 | 54 | ASSERT_TRUE(sk.data() == sk2.data()); 55 | ASSERT_TRUE(sk.parms_id() == sk2.parms_id()); 56 | } 57 | }; 58 | 59 | save_load_secret_key(scheme_type::bfv); 60 | save_load_secret_key(scheme_type::bgv); 61 | } 62 | } // namespace sealtest 63 | -------------------------------------------------------------------------------- /native/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | cmake_minimum_required(VERSION 3.13) 5 | 6 | project(SEALTest VERSION 4.1.1 LANGUAGES CXX C) 7 | 8 | # If not called from root CMakeLists.txt 9 | if(NOT DEFINED SEAL_BUILD_TESTS) 10 | set(SEAL_BUILD_TESTS ON) 11 | 12 | # Import IDEA SEAL_GPU 13 | find_package(SEAL 4.1.1 EXACT REQUIRED) 14 | 15 | # Must define these variables and include macros 16 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${OUTLIB_PATH}) 17 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) 18 | set(SEAL_THIRDPARTY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../thirdparty) 19 | set(THIRDPARTY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/thirdparty) 20 | include(FetchContent) 21 | mark_as_advanced(FETCHCONTENT_BASE_DIR) 22 | mark_as_advanced(FETCHCONTENT_FULLY_DISCONNECTED) 23 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED) 24 | mark_as_advanced(FETCHCONTENT_QUIET) 25 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../cmake) 26 | include(SEALMacros) 27 | else() 28 | set(THIRDPARTY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../thirdparty) 29 | endif() 30 | 31 | if(NOT DEFINED SEAL_BUILD_DEPS) 32 | # [option] SEAL_BUILD_DEPS (default: ON) 33 | # Download and build missing dependencies, throw error if disabled. 34 | set(SEAL_BUILD_DEPS_OPTION_STR "Automatically download and build unmet dependencies") 35 | option(SEAL_BUILD_DEPS ${SEAL_BUILD_DEPS_OPTION_STR} ON) 36 | endif() 37 | 38 | # if SEAL_BUILD_TESTS is ON, use GoogleTest 39 | if(SEAL_BUILD_TESTS) 40 | if(SEAL_BUILD_DEPS) 41 | seal_fetch_thirdparty_content(ExternalGTest) 42 | add_library(GTest::gtest ALIAS gtest) 43 | else() 44 | find_package(GTest 1 CONFIG) 45 | if(NOT GTest_FOUND) 46 | message(FATAL_ERROR "GoogleTest: not found") 47 | else() 48 | message(STATUS "GoogleTest: found") 49 | endif() 50 | endif() 51 | 52 | add_executable(sealtest "") 53 | 54 | add_subdirectory(seal) 55 | 56 | if(TARGET SEAL::seal) 57 | target_link_libraries(sealtest PRIVATE SEAL::seal GTest::gtest) 58 | elseif(TARGET SEAL::seal_shared) 59 | target_link_libraries(sealtest PRIVATE SEAL::seal_shared GTest::gtest) 60 | else() 61 | message(FATAL_ERROR "Cannot find target SEAL::seal or SEAL::seal_shared") 62 | endif() 63 | endif() 64 | -------------------------------------------------------------------------------- /native/src/seal/randomtostd.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/randomgen.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace seal 13 | { 14 | /** 15 | A simple wrapper class to implement C++ UniformRandomBitGenerator type properties 16 | for a given polymorphic UniformRandomGenerator instance. The resulting object can 17 | be used as a randomness source in C++ standard random number distribution classes, 18 | such as std::uniform_int_distribution, std::normal_distribution, or any of the 19 | standard RandomNumberEngine classes. 20 | */ 21 | class RandomToStandardAdapter 22 | { 23 | public: 24 | using result_type = std::uint32_t; 25 | 26 | /** 27 | Creates a new RandomToStandardAdapter backed by a given UniformRandomGenerator. 28 | 29 | @param[in] generator A backing UniformRandomGenerator instance 30 | @throws std::invalid_argument if generator is null 31 | */ 32 | RandomToStandardAdapter(std::shared_ptr generator) : generator_(generator) 33 | { 34 | if (!generator_) 35 | { 36 | throw std::invalid_argument("generator cannot be null"); 37 | } 38 | } 39 | 40 | /** 41 | Returns a new random number from the backing UniformRandomGenerator. 42 | */ 43 | SEAL_NODISCARD inline result_type operator()() 44 | { 45 | return generator_->generate(); 46 | } 47 | 48 | /** 49 | Returns the backing UniformRandomGenerator. 50 | */ 51 | SEAL_NODISCARD inline auto generator() const noexcept 52 | { 53 | return generator_; 54 | } 55 | 56 | /** 57 | Returns the smallest possible output value. 58 | */ 59 | SEAL_NODISCARD inline static constexpr result_type min() noexcept 60 | { 61 | return std::numeric_limits::min(); 62 | } 63 | 64 | /** 65 | Returns the largest possible output value. 66 | */ 67 | SEAL_NODISCARD static constexpr result_type max() noexcept 68 | { 69 | return std::numeric_limits::max(); 70 | } 71 | 72 | private: 73 | std::shared_ptr generator_; 74 | }; 75 | } // namespace seal 76 | -------------------------------------------------------------------------------- /cmake/CheckCXXIntrinsicsSpecific.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | if(SEAL_USE_INTRIN) 5 | cmake_push_check_state(RESET) 6 | set(CMAKE_REQUIRED_QUIET TRUE) 7 | if(NOT MSVC) 8 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -O0 ${SEAL_LANG_FLAG}") 9 | endif() 10 | 11 | if(MSVC) 12 | # Check for presence of _umul128 13 | check_cxx_source_runs(" 14 | #include <${SEAL_INTRIN_HEADER}> 15 | int main() { 16 | unsigned long long a = 0, b = 0; 17 | unsigned long long c; 18 | volatile unsigned long long d; 19 | d = _umul128(a, b, &c); 20 | return 0; 21 | }" 22 | SEAL__UMUL128_FOUND 23 | ) 24 | 25 | # Check for _BitScanReverse64 26 | check_cxx_source_runs(" 27 | #include <${SEAL_INTRIN_HEADER}> 28 | int main() { 29 | unsigned long a = 0, b = 0; 30 | volatile unsigned char res = _BitScanReverse64(&a, b); 31 | return 0; 32 | }" 33 | SEAL__BITSCANREVERSE64_FOUND 34 | ) 35 | else() 36 | # Check for presence of __int128 37 | set(CMAKE_EXTRA_INCLUDE_FILES ${SEAL_INTRIN_HEADER}) 38 | check_type_size("__int128" INT128 LANGUAGE CXX) 39 | if(INT128 EQUAL 16) 40 | set(SEAL___INT128_FOUND ON) 41 | else() 42 | set(SEAL___INT128_FOUND OFF) 43 | endif() 44 | 45 | # Check for __builtin_clzll 46 | check_cxx_source_runs(" 47 | int main() { 48 | volatile auto res = __builtin_clzll(0); 49 | return 0; 50 | }" 51 | SEAL___BUILTIN_CLZLL_FOUND 52 | ) 53 | endif() 54 | 55 | # Check for _addcarry_u64 56 | check_cxx_source_runs(" 57 | #include <${SEAL_INTRIN_HEADER}> 58 | int main() { 59 | unsigned long long a; 60 | volatile auto res = _addcarry_u64(0,0,0,&a); 61 | return 0; 62 | }" 63 | SEAL__ADDCARRY_U64_FOUND 64 | ) 65 | 66 | # Check for _subborrow_u64 67 | check_cxx_source_runs(" 68 | #include <${SEAL_INTRIN_HEADER}> 69 | int main() { 70 | unsigned long long a; 71 | volatile auto res = _subborrow_u64(0,0,0,&a); 72 | return 0; 73 | }" 74 | SEAL__SUBBORROW_U64_FOUND 75 | ) 76 | 77 | cmake_pop_check_state() 78 | endif() 79 | -------------------------------------------------------------------------------- /native/tests/seal/memorymanager.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/dynarray.h" 5 | #include "seal/memorymanager.h" 6 | #include "seal/util/pointer.h" 7 | #include "seal/util/uintcore.h" 8 | #include "gtest/gtest.h" 9 | 10 | using namespace seal; 11 | using namespace seal::util; 12 | using namespace std; 13 | 14 | namespace sealtest 15 | { 16 | TEST(MemoryPoolHandleTest, MemoryPoolHandleConstructAssign) 17 | { 18 | MemoryPoolHandle pool; 19 | ASSERT_FALSE(pool); 20 | pool = MemoryPoolHandle::Global(); 21 | ASSERT_TRUE(&static_cast(pool) == global_variables::global_memory_pool.get()); 22 | pool = MemoryPoolHandle::New(); 23 | ASSERT_FALSE(&pool.operator seal::util::MemoryPool &() == global_variables::global_memory_pool.get()); 24 | MemoryPoolHandle pool2 = MemoryPoolHandle::New(); 25 | ASSERT_FALSE(pool == pool2); 26 | 27 | pool = pool2; 28 | ASSERT_TRUE(pool == pool2); 29 | pool = MemoryPoolHandle::Global(); 30 | ASSERT_FALSE(pool == pool2); 31 | pool2 = MemoryPoolHandle::Global(); 32 | ASSERT_TRUE(pool == pool2); 33 | } 34 | 35 | TEST(MemoryPoolHandleTest, MemoryPoolHandleAllocate) 36 | { 37 | MemoryPoolHandle pool = MemoryPoolHandle::New(); 38 | ASSERT_TRUE(0LL == pool.alloc_byte_count()); 39 | { 40 | auto ptr(allocate_uint(5, pool)); 41 | ASSERT_TRUE(5LL * bytes_per_uint64 == pool.alloc_byte_count()); 42 | } 43 | 44 | pool = MemoryPoolHandle::New(); 45 | ASSERT_TRUE(0LL * bytes_per_uint64 == pool.alloc_byte_count()); 46 | { 47 | auto ptr(allocate_uint(5, pool)); 48 | ASSERT_TRUE(5LL * bytes_per_uint64 == pool.alloc_byte_count()); 49 | 50 | ptr = allocate_uint(8, pool); 51 | ASSERT_TRUE(13LL * bytes_per_uint64 == pool.alloc_byte_count()); 52 | 53 | auto ptr2(allocate_uint(2, pool)); 54 | ASSERT_TRUE(15LL * bytes_per_uint64 == pool.alloc_byte_count()); 55 | } 56 | } 57 | 58 | TEST(MemoryPoolHandleTest, UseCount) 59 | { 60 | MemoryPoolHandle pool = MemoryPoolHandle::New(); 61 | ASSERT_EQ(1L, pool.use_count()); 62 | { 63 | DynArray arr(pool); 64 | ASSERT_EQ(2L, pool.use_count()); 65 | DynArray arr2(pool); 66 | ASSERT_EQ(3L, pool.use_count()); 67 | } 68 | ASSERT_EQ(1L, pool.use_count()); 69 | } 70 | } // namespace sealtest 71 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.27.8/CMakeCCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_COMPILER "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc.exe") 2 | set(CMAKE_C_COMPILER_ARG1 "") 3 | set(CMAKE_C_COMPILER_ID "GNU") 4 | set(CMAKE_C_COMPILER_VERSION "13.1.0") 5 | set(CMAKE_C_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_C_COMPILER_WRAPPER "") 7 | set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "17") 8 | set(CMAKE_C_EXTENSIONS_COMPUTED_DEFAULT "ON") 9 | set(CMAKE_C_COMPILE_FEATURES "") 10 | set(CMAKE_C90_COMPILE_FEATURES "") 11 | set(CMAKE_C99_COMPILE_FEATURES "") 12 | set(CMAKE_C11_COMPILE_FEATURES "") 13 | set(CMAKE_C17_COMPILE_FEATURES "") 14 | set(CMAKE_C23_COMPILE_FEATURES "") 15 | 16 | set(CMAKE_C_PLATFORM_ID "MinGW") 17 | set(CMAKE_C_SIMULATE_ID "") 18 | set(CMAKE_C_COMPILER_FRONTEND_VARIANT "GNU") 19 | set(CMAKE_C_SIMULATE_VERSION "") 20 | 21 | 22 | 23 | 24 | set(CMAKE_AR "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ar.exe") 25 | set(CMAKE_C_COMPILER_AR "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc-ar.exe") 26 | set(CMAKE_RANLIB "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ranlib.exe") 27 | set(CMAKE_C_COMPILER_RANLIB "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc-ranlib.exe") 28 | set(CMAKE_LINKER "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ld.exe") 29 | set(CMAKE_MT "") 30 | set(CMAKE_TAPI "CMAKE_TAPI-NOTFOUND") 31 | set(CMAKE_COMPILER_IS_GNUCC 1) 32 | set(CMAKE_C_COMPILER_LOADED 1) 33 | set(CMAKE_C_COMPILER_WORKS ) 34 | set(CMAKE_C_ABI_COMPILED ) 35 | 36 | set(CMAKE_C_COMPILER_ENV_VAR "CC") 37 | 38 | set(CMAKE_C_COMPILER_ID_RUN 1) 39 | set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m) 40 | set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC) 41 | set(CMAKE_C_LINKER_PREFERENCE 10) 42 | set(CMAKE_C_LINKER_DEPFILE_SUPPORTED ) 43 | 44 | # Save compiler ABI information. 45 | set(CMAKE_C_SIZEOF_DATA_PTR "") 46 | set(CMAKE_C_COMPILER_ABI "") 47 | set(CMAKE_C_BYTE_ORDER "") 48 | set(CMAKE_C_LIBRARY_ARCHITECTURE "") 49 | 50 | if(CMAKE_C_SIZEOF_DATA_PTR) 51 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}") 52 | endif() 53 | 54 | if(CMAKE_C_COMPILER_ABI) 55 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}") 56 | endif() 57 | 58 | if(CMAKE_C_LIBRARY_ARCHITECTURE) 59 | set(CMAKE_LIBRARY_ARCHITECTURE "") 60 | endif() 61 | 62 | set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "") 63 | if(CMAKE_C_CL_SHOWINCLUDES_PREFIX) 64 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}") 65 | endif() 66 | 67 | 68 | 69 | 70 | 71 | set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES "") 72 | set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "") 73 | set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "") 74 | set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 75 | -------------------------------------------------------------------------------- /native/src/seal/util/croots.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/util/croots.h" 5 | #include 6 | 7 | using namespace std; 8 | 9 | namespace seal 10 | { 11 | namespace util 12 | { 13 | // Required for C++14 compliance: static constexpr member variables are not necessarily inlined so need to 14 | // ensure symbol is created. 15 | constexpr double ComplexRoots::PI_; 16 | 17 | ComplexRoots::ComplexRoots(size_t degree_of_roots, MemoryPoolHandle pool) 18 | : degree_of_roots_(degree_of_roots), pool_(move(pool)) 19 | { 20 | #ifdef SEAL_DEBUG 21 | int power = util::get_power_of_two(degree_of_roots_); 22 | if (power < 0) 23 | { 24 | throw invalid_argument("degree_of_roots must be a power of two"); 25 | } 26 | else if (power < 3) 27 | { 28 | throw invalid_argument("degree_of_roots must be at least 8"); 29 | } 30 | #endif 31 | roots_ = allocate>(degree_of_roots_ / 8 + 1, pool_); 32 | 33 | // Generate 1/8 of all roots. 34 | // Alternatively, choose from precomputed high-precision roots in files. 35 | for (size_t i = 0; i <= degree_of_roots_ / 8; i++) 36 | { 37 | roots_[i] = 38 | polar(1.0, 2 * PI_ * static_cast(i) / static_cast(degree_of_roots_)); 39 | } 40 | } 41 | 42 | SEAL_NODISCARD complex ComplexRoots::get_root(size_t index) const 43 | { 44 | index &= degree_of_roots_ - 1; 45 | auto mirror = [](complex a) { 46 | return complex{ a.imag(), a.real() }; 47 | }; 48 | 49 | // This express the 8-fold symmetry of all n-th roots. 50 | if (index <= degree_of_roots_ / 8) 51 | { 52 | return roots_[index]; 53 | } 54 | else if (index <= degree_of_roots_ / 4) 55 | { 56 | return mirror(roots_[degree_of_roots_ / 4 - index]); 57 | } 58 | else if (index <= degree_of_roots_ / 2) 59 | { 60 | return -conj(get_root(degree_of_roots_ / 2 - index)); 61 | } 62 | else if (index <= 3 * degree_of_roots_ / 4) 63 | { 64 | return -get_root(index - degree_of_roots_ / 2); 65 | } 66 | else 67 | { 68 | return conj(get_root(degree_of_roots_ - index)); 69 | } 70 | } 71 | } // namespace util 72 | } // namespace seal 73 | -------------------------------------------------------------------------------- /native/tests/seal/publickey.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/context.cuh" 5 | #include "seal/keygenerator.h" 6 | #include "seal/modulus.h" 7 | #include "seal/publickey.h" 8 | #include "gtest/gtest.h" 9 | 10 | using namespace seal; 11 | using namespace std; 12 | 13 | namespace sealtest 14 | { 15 | TEST(PublicKeyTest, SaveLoadPublicKey) 16 | { 17 | auto save_load_public_key = [](scheme_type scheme) { 18 | stringstream stream; 19 | { 20 | EncryptionParameters parms(scheme); 21 | parms.set_poly_modulus_degree(64); 22 | parms.set_plain_modulus(1 << 6); 23 | parms.set_coeff_modulus(CoeffModulus::Create(64, { 60 })); 24 | 25 | SEALContext context(parms, false, sec_level_type::none); 26 | KeyGenerator keygen(context); 27 | 28 | PublicKey pk; 29 | keygen.create_public_key(pk); 30 | ASSERT_TRUE(pk.parms_id() == context.key_parms_id()); 31 | pk.save(stream); 32 | 33 | PublicKey pk2; 34 | pk2.load(context, stream); 35 | 36 | ASSERT_EQ(pk.data().dyn_array().size(), pk2.data().dyn_array().size()); 37 | for (size_t i = 0; i < pk.data().dyn_array().size(); i++) 38 | { 39 | ASSERT_EQ(pk.data().data()[i], pk2.data().data()[i]); 40 | } 41 | ASSERT_TRUE(pk.parms_id() == pk2.parms_id()); 42 | } 43 | { 44 | EncryptionParameters parms(scheme); 45 | parms.set_poly_modulus_degree(256); 46 | parms.set_plain_modulus(1 << 20); 47 | parms.set_coeff_modulus(CoeffModulus::Create(256, { 30, 40 })); 48 | 49 | SEALContext context(parms, false, sec_level_type::none); 50 | KeyGenerator keygen(context); 51 | 52 | PublicKey pk; 53 | keygen.create_public_key(pk); 54 | ASSERT_TRUE(pk.parms_id() == context.key_parms_id()); 55 | pk.save(stream); 56 | 57 | PublicKey pk2; 58 | pk2.load(context, stream); 59 | 60 | ASSERT_EQ(pk.data().dyn_array().size(), pk2.data().dyn_array().size()); 61 | for (size_t i = 0; i < pk.data().dyn_array().size(); i++) 62 | { 63 | ASSERT_EQ(pk.data().data()[i], pk2.data().data()[i]); 64 | } 65 | ASSERT_TRUE(pk.parms_id() == pk2.parms_id()); 66 | } 67 | }; 68 | 69 | save_load_public_key(scheme_type::bfv); 70 | save_load_public_key(scheme_type::bgv); 71 | } 72 | } // namespace sealtest 73 | -------------------------------------------------------------------------------- /native/src/seal/util/clipnormal.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/defines.h" 7 | #include 8 | #include 9 | 10 | namespace seal 11 | { 12 | namespace util 13 | { 14 | class ClippedNormalDistribution 15 | { 16 | public: 17 | using result_type = double; 18 | 19 | using param_type = ClippedNormalDistribution; 20 | 21 | ClippedNormalDistribution(result_type mean, result_type standard_deviation, result_type max_deviation); 22 | 23 | template 24 | SEAL_NODISCARD inline result_type operator()(RNG &engine, const param_type &parm) noexcept 25 | { 26 | param(parm); 27 | return operator()(engine); 28 | } 29 | 30 | template 31 | SEAL_NODISCARD inline result_type operator()(RNG &engine) noexcept 32 | { 33 | result_type mean = normal_.mean(); 34 | while (true) 35 | { 36 | result_type value = normal_(engine); 37 | result_type deviation = std::abs(value - mean); 38 | if (deviation <= max_deviation_) 39 | { 40 | return value; 41 | } 42 | } 43 | } 44 | 45 | SEAL_NODISCARD inline result_type mean() const noexcept 46 | { 47 | return normal_.mean(); 48 | } 49 | 50 | SEAL_NODISCARD inline result_type standard_deviation() const noexcept 51 | { 52 | return normal_.stddev(); 53 | } 54 | 55 | SEAL_NODISCARD inline result_type max_deviation() const noexcept 56 | { 57 | return max_deviation_; 58 | } 59 | 60 | SEAL_NODISCARD inline result_type min() const noexcept 61 | { 62 | return normal_.mean() - max_deviation_; 63 | } 64 | 65 | SEAL_NODISCARD inline result_type max() const noexcept 66 | { 67 | return normal_.mean() + max_deviation_; 68 | } 69 | 70 | SEAL_NODISCARD inline param_type param() const noexcept 71 | { 72 | return *this; 73 | } 74 | 75 | inline void param(const param_type &parm) noexcept 76 | { 77 | *this = parm; 78 | } 79 | 80 | inline void reset() noexcept 81 | { 82 | normal_.reset(); 83 | } 84 | 85 | private: 86 | std::normal_distribution normal_; 87 | 88 | result_type max_deviation_; 89 | }; 90 | } // namespace util 91 | } // namespace seal 92 | -------------------------------------------------------------------------------- /native/src/seal/util/msvc.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #if SEAL_COMPILER == SEAL_COMPILER_MSVC 7 | 8 | // Require Visual Studio 2017 version 15.3 or newer 9 | #if (_MSC_VER < 1911) 10 | #error "Microsoft Visual Studio 2017 version 15.3 or newer required" 11 | #endif 12 | 13 | // Read in config.h 14 | #include "seal/util/config.h" 15 | 16 | // In Visual Studio redefine std::byte (seal_byte) 17 | #undef SEAL_USE_STD_BYTE 18 | 19 | // In Visual Studio for now we disable the use of std::shared_mutex 20 | #undef SEAL_USE_SHARED_MUTEX 21 | 22 | // Are we compiling with C++17 or newer 23 | #if (_MSVC_LANG >= 201703L) 24 | 25 | // Use `if constexpr' 26 | #define SEAL_USE_IF_CONSTEXPR 27 | 28 | // Use [[maybe_unused]] 29 | #define SEAL_USE_MAYBE_UNUSED 30 | 31 | // Use [[nodiscard]] 32 | #define SEAL_USE_NODISCARD 33 | 34 | #else 35 | 36 | #ifdef SEAL_USE_IF_CONSTEXPR 37 | #pragma message("Disabling `if constexpr` based on _MSVC_LANG value " SEAL_STRINGIZE( \ 38 | _MSVC_LANG) ": undefining SEAL_USE_IF_CONSTEXPR") 39 | #undef SEAL_USE_IF_CONSTEXPR 40 | #endif 41 | 42 | #ifdef SEAL_USE_MAYBE_UNUSED 43 | #pragma message("Disabling `[[maybe_unused]]` based on _MSVC_LANG value " SEAL_STRINGIZE( \ 44 | _MSVC_LANG) ": undefining SEAL_USE_MAYBE_UNUSED") 45 | #undef SEAL_USE_MAYBE_UNUSED 46 | #endif 47 | 48 | #ifdef SEAL_USE_NODISCARD 49 | #pragma message("Disabling `[[nodiscard]]` based on _MSVC_LANG value " SEAL_STRINGIZE( \ 50 | _MSVC_LANG) ": undefining SEAL_USE_NODISCARD") 51 | #undef SEAL_USE_NODISCARD 52 | #endif 53 | #endif 54 | 55 | #ifdef SEAL_USE_ALIGNED_ALLOC 56 | #define SEAL_MALLOC(size) static_cast(_aligned_malloc((size), 64)) 57 | #define SEAL_FREE(ptr) _aligned_free(ptr) 58 | #endif 59 | 60 | // X64 61 | #ifdef _M_X64 62 | 63 | #ifdef SEAL_USE_INTRIN 64 | #include 65 | 66 | #ifdef SEAL_USE__UMUL128 67 | #pragma intrinsic(_umul128) 68 | #define SEAL_MULTIPLY_UINT64_HW64(operand1, operand2, hw64) _umul128(operand1, operand2, hw64); 69 | 70 | #define SEAL_MULTIPLY_UINT64(operand1, operand2, result128) result128[0] = _umul128(operand1, operand2, result128 + 1); 71 | #endif 72 | 73 | #ifdef SEAL_USE__BITSCANREVERSE64 74 | #pragma intrinsic(_BitScanReverse64) 75 | #define SEAL_MSB_INDEX_UINT64(result, value) _BitScanReverse64(result, value) 76 | #endif 77 | 78 | #ifdef SEAL_USE__ADDCARRY_U64 79 | #pragma intrinsic(_addcarry_u64) 80 | #define SEAL_ADD_CARRY_UINT64(operand1, operand2, carry, result) _addcarry_u64(carry, operand1, operand2, result) 81 | #endif 82 | 83 | #ifdef SEAL_USE__SUBBORROW_U64 84 | #pragma intrinsic(_subborrow_u64) 85 | #define SEAL_SUB_BORROW_UINT64(operand1, operand2, borrow, result) _subborrow_u64(borrow, operand1, operand2, result) 86 | #endif 87 | 88 | #endif 89 | #else 90 | #undef SEAL_USE_INTRIN 91 | 92 | #endif //_M_X64 93 | 94 | // Force inline 95 | #define SEAL_FORCE_INLINE __forceinline 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /native/src/seal/util/ntt_helper.cuh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "seal/util/common.cuh" 4 | #include "seal/util/common.h" 5 | #include "seal/util/scalingvariant.h" 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | namespace seal 14 | { 15 | namespace util 16 | { 17 | 18 | __global__ void matrixMultiplication( 19 | uint64_t *A, uint64_t *B, uint64_t *C, int m, int n, int p, uint64_t modulu, uint64_t ratio0, 20 | uint64_t ratio1); 21 | 22 | __global__ void matrix_multi_transpose( 23 | uint64_t *A, uint64_t *B, uint64_t *C, int m, int n, int p, uint64_t modulu, int bit_count, 24 | uint64_t ratio0, uint64_t ratio1); 25 | 26 | __global__ void elementMulMatrix( 27 | uint64_t *MatA, uint64_t *MatB, uint64_t *MatC, int n1, int n2, uint64_t modulu, uint64_t *ratio1); 28 | 29 | __global__ void matrix_multi_elemul_merge(uint64_t *A, uint64_t *B, uint64_t *C,uint64_t *D, int m, int n, int p, uint64_t modulu, uint64_t ratio0, 30 | uint64_t ratio1); 31 | 32 | __global__ void matrix_multi_elemul_merge_batch(uint64_t *A, uint64_t *B, uint64_t *C, int m, int n, int p, size_t modulu_size, uint64_t *modulu, uint64_t *ratio0, 33 | uint64_t *ratio1, uint64_t *elemul_root); 34 | 35 | __global__ void matrix_multi_elemul_merge_batch_test(uint64_t *A, uint64_t *B, uint64_t *C, uint64_t *D, int m, int n, int p, size_t modulu_size, uint64_t *modulu, uint64_t *ratio0, 36 | uint64_t *ratio1, uint64_t *elemul_root); 37 | 38 | __global__ void matrix_multi_transpose_batch( 39 | uint64_t *A, uint64_t *B, uint64_t *C, int m, int n, int p, size_t modulu_size, uint64_t *modulu, int *bit_count, 40 | uint64_t *ratio0, uint64_t *ratio1); 41 | 42 | void ntt_v3(const SEALContext &context, parms_id_type parms_id, uint64_t *input, size_t modulu_size, int modulu_shift=0, cudaStream_t ntt_stream=0); 43 | 44 | void ntt_v3_key_switch(const SEALContext &context, parms_id_type parms_id, uint64_t *input, size_t modulu_size, cudaStream_t ntt_stream, int modulu_shift); 45 | 46 | void ntt_v1(const SEALContext &context, parms_id_type parms_id, uint64_t *input, size_t modulu_size, int modulu_shift=0, bool fix_root=false); 47 | 48 | void ntt_v1_single(const SEALContext &context, parms_id_type parms_id, uint64_t *input, int modulu_shift, cudaStream_t stream=0); 49 | 50 | void intt_v1_single(const SEALContext &context, parms_id_type parms_id, uint64_t *input, int modulu_shift, cudaStream_t stream=0); 51 | 52 | void ntt_v3_single(uint64_t *input, uint64_t *matrix_n1, uint64_t *matrix_n2, uint64_t *matrix_n12, uint64_t modulu, uint64_t ratio0, uint64_t ratio1, 53 | uint64_t root, int bit, std::pair split_result, uint64_t *ntt_temp); 54 | } 55 | } -------------------------------------------------------------------------------- /native/bench/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | cmake_minimum_required(VERSION 3.13) 5 | 6 | project(SEALBench VERSION 4.1.1 LANGUAGES CXX) 7 | 8 | # If not called from root CMakeLists.txt 9 | if(NOT DEFINED SEAL_BUILD_BENCH) 10 | set(SEAL_BUILD_BENCH ON) 11 | 12 | # Import SEAL_GPU 13 | find_package(SEAL 4.1.1 EXACT REQUIRED) 14 | 15 | # Must define these variables and include macros 16 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${OUTLIB_PATH}) 17 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) 18 | set(SEAL_THIRDPARTY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../thirdparty) 19 | set(THIRDPARTY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/thirdparty) 20 | include(FetchContent) 21 | mark_as_advanced(FETCHCONTENT_BASE_DIR) 22 | mark_as_advanced(FETCHCONTENT_FULLY_DISCONNECTED) 23 | mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED) 24 | mark_as_advanced(FETCHCONTENT_QUIET) 25 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../cmake) 26 | include(SEALMacros) 27 | else() 28 | set(THIRDPARTY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../thirdparty) 29 | endif() 30 | 31 | if(NOT DEFINED SEAL_BUILD_DEPS) 32 | # [option] SEAL_BUILD_DEPS (default: ON) 33 | # Download and build missing dependencies, throw error if disabled. 34 | set(SEAL_BUILD_DEPS_OPTION_STR "Automatically download and build unmet dependencies") 35 | option(SEAL_BUILD_DEPS ${SEAL_BUILD_DEPS_OPTION_STR} ON) 36 | endif() 37 | 38 | # if SEAL_BUILD_BENCH is ON, use GoogleBenchmark 39 | if(SEAL_BUILD_BENCH) 40 | if(SEAL_BUILD_DEPS) 41 | seal_fetch_thirdparty_content(ExternalBenchmark) 42 | else() 43 | find_package(benchmark REQUIRED) 44 | if(NOT benchmark_FOUND) 45 | message(FATAL_ERROR "GoogleBenchmark: not found") 46 | else() 47 | message(STATUS "GoogleBenchmark: found") 48 | endif() 49 | endif() 50 | 51 | add_executable(sealbench) 52 | # If we're targeting WASM, add the appropriate link flags 53 | if(EMSCRIPTEN) 54 | set_target_properties(sealbench PROPERTIES LINK_FLAGS "-flto -O3 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s MAXIMUM_MEMORY=4GB") 55 | endif() 56 | target_sources(sealbench 57 | PRIVATE 58 | ${CMAKE_CURRENT_LIST_DIR}/bench.cpp 59 | ${CMAKE_CURRENT_LIST_DIR}/keygen.cpp 60 | ${CMAKE_CURRENT_LIST_DIR}/ntt.cpp 61 | ${CMAKE_CURRENT_LIST_DIR}/bfv.cpp 62 | ${CMAKE_CURRENT_LIST_DIR}/bgv.cpp 63 | ${CMAKE_CURRENT_LIST_DIR}/ckks.cpp 64 | ) 65 | 66 | if(TARGET SEAL::seal) 67 | target_link_libraries(sealbench PRIVATE SEAL::seal benchmark::benchmark) 68 | elseif(TARGET SEAL::seal_shared) 69 | target_link_libraries(sealbench PRIVATE SEAL::seal_shared benchmark::benchmark) 70 | else() 71 | message(FATAL_ERROR "Cannot find target SEAL::seal or SEAL::seal_shared") 72 | endif() 73 | endif() 74 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | THIRD-PARTY SOFTWARE NOTICES AND INFORMATION 2 | 3 | This software incorporates components from the projects listed below. The original copyright notices 4 | and the licenses under which IDEA received such components are set forth below and are provided for 5 | informational purposes only. IDEA reserves all rights not expressly granted herein, whether by 6 | implication, estoppel or otherwise. 7 | 8 | This software includes parts of the BLAKE2 library (https://github.com/BLAKE2/BLAKE2). 9 | The BLAKE2 library is licensed under CC0 Universal, version 1.0. You can find a copy of this license at https://creativecommons.org/publicdomain/zero/1.0/legalcode 10 | 11 | This software includes parts of the Kyber library (https://github.com/pq-crystals/kyber). 12 | The Kyber library is licensed under CC0 Universal, version 1.0. You can find a copy of this license at https://creativecommons.org/publicdomain/zero/1.0/legalcode 13 | 14 | This software includes parts of the SEAL library(https://github.com/microsoft/SEAL). 15 | The SEAL library is licensed under MIT License. You can find a copy of this license at https://opensource.org/licenses/MIT 16 | 17 | This software includes parts of the ZSTD library, version 1.4.5 (https://github.com/facebook/zstd): 18 | ZSTD NOTICES AND INFORMATION BEGIN HERE 19 | =============================================================================== 20 | BSD License 21 | 22 | For Zstandard software 23 | 24 | Copyright (c) 2016-present, Facebook, Inc. All rights reserved. 25 | 26 | Redistribution and use in source and binary forms, with or without modification, 27 | are permitted provided that the following conditions are met: 28 | 29 | * Redistributions of source code must retain the above copyright notice, this 30 | list of conditions and the following disclaimer. 31 | 32 | * Redistributions in binary form must reproduce the above copyright notice, 33 | this list of conditions and the following disclaimer in the documentation 34 | and/or other materials provided with the distribution. 35 | 36 | * Neither the name Facebook nor the names of its contributors may be used to 37 | endorse or promote products derived from this software without specific 38 | prior written permission. 39 | 40 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 41 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 42 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 44 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 45 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 47 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 49 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | =============================================================================== 51 | END OF ZSTD NOTICES AND INFORMATION 52 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CMakeCUDACompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc") 2 | set(CMAKE_CUDA_HOST_COMPILER "") 3 | set(CMAKE_CUDA_HOST_LINK_LAUNCHER "/usr/local/bin/g++") 4 | set(CMAKE_CUDA_COMPILER_ID "NVIDIA") 5 | set(CMAKE_CUDA_COMPILER_VERSION "11.7.64") 6 | set(CMAKE_CUDA_STANDARD_COMPUTED_DEFAULT "14") 7 | set(CMAKE_CUDA_COMPILE_FEATURES "cuda_std_03;cuda_std_11;cuda_std_14;cuda_std_17") 8 | set(CMAKE_CUDA03_COMPILE_FEATURES "cuda_std_03") 9 | set(CMAKE_CUDA11_COMPILE_FEATURES "cuda_std_11") 10 | set(CMAKE_CUDA14_COMPILE_FEATURES "cuda_std_14") 11 | set(CMAKE_CUDA17_COMPILE_FEATURES "cuda_std_17") 12 | set(CMAKE_CUDA20_COMPILE_FEATURES "") 13 | 14 | set(CMAKE_CUDA_PLATFORM_ID "Linux") 15 | set(CMAKE_CUDA_SIMULATE_ID "GNU") 16 | set(CMAKE_CUDA_COMPILER_FRONTEND_VARIANT "") 17 | set(CMAKE_CUDA_SIMULATE_VERSION "8.2") 18 | 19 | 20 | 21 | set(CMAKE_CUDA_COMPILER_ENV_VAR "CUDACXX") 22 | set(CMAKE_CUDA_HOST_COMPILER_ENV_VAR "CUDAHOSTCXX") 23 | 24 | set(CMAKE_CUDA_COMPILER_LOADED 1) 25 | set(CMAKE_CUDA_COMPILER_ID_RUN 1) 26 | set(CMAKE_CUDA_SOURCE_FILE_EXTENSIONS cu) 27 | set(CMAKE_CUDA_LINKER_PREFERENCE 15) 28 | set(CMAKE_CUDA_LINKER_PREFERENCE_PROPAGATES 1) 29 | 30 | set(CMAKE_CUDA_SIZEOF_DATA_PTR "8") 31 | set(CMAKE_CUDA_COMPILER_ABI "ELF") 32 | set(CMAKE_CUDA_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 33 | 34 | if(CMAKE_CUDA_SIZEOF_DATA_PTR) 35 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_CUDA_SIZEOF_DATA_PTR}") 36 | endif() 37 | 38 | if(CMAKE_CUDA_COMPILER_ABI) 39 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CUDA_COMPILER_ABI}") 40 | endif() 41 | 42 | if(CMAKE_CUDA_LIBRARY_ARCHITECTURE) 43 | set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 44 | endif() 45 | 46 | set(CMAKE_CUDA_COMPILER_TOOLKIT_ROOT "/usr/local/cuda") 47 | set(CMAKE_CUDA_COMPILER_LIBRARY_ROOT "") 48 | 49 | set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES "/usr/local/cuda/targets/x86_64-linux/include") 50 | 51 | set(CMAKE_CUDA_HOST_IMPLICIT_LINK_LIBRARIES "") 52 | set(CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES "/usr/local/cuda/targets/x86_64-linux/lib/stubs;/usr/local/cuda/targets/x86_64-linux/lib") 53 | set(CMAKE_CUDA_HOST_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 54 | 55 | set(CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES "/usr/local/gcc-8.2/include/c++/8.2.0;/usr/local/gcc-8.2/include/c++/8.2.0/x86_64-pc-linux-gnu;/usr/local/gcc-8.2/include/c++/8.2.0/backward;/usr/local/gcc-8.2/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include;/usr/local/include;/usr/local/gcc-8.2/include;/usr/local/gcc-8.2/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include-fixed;/usr/include/x86_64-linux-gnu;/usr/include") 56 | set(CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES "stdc++;m;gcc_s;gcc;c;gcc_s;gcc") 57 | set(CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES "/usr/local/cuda/targets/x86_64-linux/lib/stubs;/usr/local/cuda/targets/x86_64-linux/lib;/usr/local/gcc-8.2/lib/gcc/x86_64-pc-linux-gnu/8.2.0;/usr/local/gcc-8.2/lib64;/lib/x86_64-linux-gnu;/lib64;/usr/lib/x86_64-linux-gnu;/usr/local/gcc-8.2/lib") 58 | set(CMAKE_CUDA_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 59 | 60 | set(CMAKE_CUDA_RUNTIME_LIBRARY_DEFAULT "STATIC") 61 | 62 | set(CMAKE_LINKER "/usr/bin/ld") 63 | set(CMAKE_AR "/usr/bin/ar") 64 | set(CMAKE_MT "") 65 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | CMakeFiles/sealexamples.dir/2_encoders.cpp.o 5 | /home/liqirui/SEAL/native/examples/2_encoders.cpp 6 | /home/liqirui/SEAL/native/examples/examples.h 7 | /usr/local/include/SEAL-4.1/gsl/assert 8 | /usr/local/include/SEAL-4.1/gsl/byte 9 | /usr/local/include/SEAL-4.1/gsl/span 10 | /usr/local/include/SEAL-4.1/gsl/span_ext 11 | /usr/local/include/SEAL-4.1/gsl/util 12 | /usr/local/include/SEAL-4.1/seal/batchencoder.h 13 | /usr/local/include/SEAL-4.1/seal/ciphertext.h 14 | /usr/local/include/SEAL-4.1/seal/ckks.h 15 | /usr/local/include/SEAL-4.1/seal/context.h 16 | /usr/local/include/SEAL-4.1/seal/decryptor.h 17 | /usr/local/include/SEAL-4.1/seal/dynarray.h 18 | /usr/local/include/SEAL-4.1/seal/encryptionparams.h 19 | /usr/local/include/SEAL-4.1/seal/encryptor.h 20 | /usr/local/include/SEAL-4.1/seal/evaluator.h 21 | /usr/local/include/SEAL-4.1/seal/galoiskeys.h 22 | /usr/local/include/SEAL-4.1/seal/keygenerator.h 23 | /usr/local/include/SEAL-4.1/seal/kswitchkeys.h 24 | /usr/local/include/SEAL-4.1/seal/memorymanager.h 25 | /usr/local/include/SEAL-4.1/seal/modulus.h 26 | /usr/local/include/SEAL-4.1/seal/plaintext.h 27 | /usr/local/include/SEAL-4.1/seal/publickey.h 28 | /usr/local/include/SEAL-4.1/seal/randomgen.h 29 | /usr/local/include/SEAL-4.1/seal/randomtostd.h 30 | /usr/local/include/SEAL-4.1/seal/relinkeys.h 31 | /usr/local/include/SEAL-4.1/seal/seal.h 32 | /usr/local/include/SEAL-4.1/seal/secretkey.h 33 | /usr/local/include/SEAL-4.1/seal/serializable.h 34 | /usr/local/include/SEAL-4.1/seal/serialization.h 35 | /usr/local/include/SEAL-4.1/seal/util/blake2.h 36 | /usr/local/include/SEAL-4.1/seal/util/clang.h 37 | /usr/local/include/SEAL-4.1/seal/util/common.h 38 | /usr/local/include/SEAL-4.1/seal/util/config.h 39 | /usr/local/include/SEAL-4.1/seal/util/croots.h 40 | /usr/local/include/SEAL-4.1/seal/util/defines.h 41 | /usr/local/include/SEAL-4.1/seal/util/dwthandler.h 42 | /usr/local/include/SEAL-4.1/seal/util/galois.h 43 | /usr/local/include/SEAL-4.1/seal/util/gcc.h 44 | /usr/local/include/SEAL-4.1/seal/util/globals.h 45 | /usr/local/include/SEAL-4.1/seal/util/hash.h 46 | /usr/local/include/SEAL-4.1/seal/util/hestdparms.h 47 | /usr/local/include/SEAL-4.1/seal/util/iterator.h 48 | /usr/local/include/SEAL-4.1/seal/util/locks.h 49 | /usr/local/include/SEAL-4.1/seal/util/mempool.h 50 | /usr/local/include/SEAL-4.1/seal/util/msvc.h 51 | /usr/local/include/SEAL-4.1/seal/util/ntt.h 52 | /usr/local/include/SEAL-4.1/seal/util/numth.h 53 | /usr/local/include/SEAL-4.1/seal/util/pointer.h 54 | /usr/local/include/SEAL-4.1/seal/util/polycore.h 55 | /usr/local/include/SEAL-4.1/seal/util/rns.h 56 | /usr/local/include/SEAL-4.1/seal/util/streambuf.h 57 | /usr/local/include/SEAL-4.1/seal/util/uintarith.h 58 | /usr/local/include/SEAL-4.1/seal/util/uintarithsmallmod.h 59 | /usr/local/include/SEAL-4.1/seal/util/uintcore.h 60 | /usr/local/include/SEAL-4.1/seal/util/ztools.h 61 | /usr/local/include/SEAL-4.1/seal/valcheck.h 62 | /usr/local/include/SEAL-4.1/seal/version.h 63 | -------------------------------------------------------------------------------- /native/src/seal/galoiskeys.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/ciphertext.cuh" 7 | #include "seal/kswitchkeys.h" 8 | #include "seal/memorymanager.h" 9 | #include "seal/util/defines.h" 10 | #include "seal/util/galois.h" 11 | #include 12 | 13 | namespace seal 14 | { 15 | /** 16 | Class to store Galois keys. 17 | 18 | @par Slot Rotations 19 | Galois keys are certain types of public keys that are needed to perform encrypted 20 | vector rotation operations on batched ciphertexts. Batched ciphertexts encrypt 21 | a 2-by-(N/2) matrix of modular integers in the BFV scheme, or an N/2-dimensional 22 | vector of complex numbers in the CKKS scheme, where N denotes the degree of the 23 | polynomial modulus. In the BFV scheme Galois keys can enable both cyclic rotations 24 | of the encrypted matrix rows, as well as row swaps (column rotations). In the CKKS 25 | scheme Galois keys can enable cyclic vector rotations, as well as a complex 26 | conjugation operation. 27 | 28 | 29 | @par Thread Safety 30 | In general, reading from GaloisKeys is thread-safe as long as no other thread is 31 | concurrently mutating it. This is due to the underlying data structure storing the 32 | Galois keys not being thread-safe. 33 | 34 | @see RelinKeys for the class that stores the relinearization keys. 35 | @see KeyGenerator for the class that generates the Galois keys. 36 | */ 37 | class GaloisKeys : public KSwitchKeys 38 | { 39 | public: 40 | /** 41 | Returns the index of a Galois key in the backing KSwitchKeys instance that 42 | corresponds to the given Galois element, assuming that it exists in the 43 | backing KSwitchKeys. 44 | 45 | @param[in] galois_elt The Galois element 46 | @throws std::invalid_argument if galois_elt is not valid 47 | */ 48 | SEAL_NODISCARD inline static std::size_t get_index(std::uint32_t galois_elt) 49 | { 50 | return util::GaloisTool::GetIndexFromElt(galois_elt); 51 | } 52 | 53 | /** 54 | Returns whether a Galois key corresponding to a given Galois element exists. 55 | 56 | @param[in] galois_elt The Galois element 57 | @throws std::invalid_argument if galois_elt is not valid 58 | */ 59 | SEAL_NODISCARD inline bool has_key(std::uint32_t galois_elt) const 60 | { 61 | std::size_t index = get_index(galois_elt); 62 | return data().size() > index && !data()[index].empty(); 63 | } 64 | 65 | /** 66 | Returns a const reference to a Galois key. The returned Galois key corresponds 67 | to the given Galois element. 68 | 69 | @param[in] galois_elt The Galois element 70 | @throws std::invalid_argument if the key corresponding to galois_elt does not exist 71 | */ 72 | SEAL_NODISCARD inline const auto &key(std::uint32_t galois_elt) const 73 | { 74 | return KSwitchKeys::data(get_index(galois_elt)); 75 | } 76 | }; 77 | } // namespace seal 78 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.27.8/CMakeCXXCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_CXX_COMPILER "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/g++.exe") 2 | set(CMAKE_CXX_COMPILER_ARG1 "") 3 | set(CMAKE_CXX_COMPILER_ID "GNU") 4 | set(CMAKE_CXX_COMPILER_VERSION "13.1.0") 5 | set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_CXX_COMPILER_WRAPPER "") 7 | set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "17") 8 | set(CMAKE_CXX_EXTENSIONS_COMPUTED_DEFAULT "ON") 9 | set(CMAKE_CXX_COMPILE_FEATURES "") 10 | set(CMAKE_CXX98_COMPILE_FEATURES "") 11 | set(CMAKE_CXX11_COMPILE_FEATURES "") 12 | set(CMAKE_CXX14_COMPILE_FEATURES "") 13 | set(CMAKE_CXX17_COMPILE_FEATURES "") 14 | set(CMAKE_CXX20_COMPILE_FEATURES "") 15 | set(CMAKE_CXX23_COMPILE_FEATURES "") 16 | 17 | set(CMAKE_CXX_PLATFORM_ID "MinGW") 18 | set(CMAKE_CXX_SIMULATE_ID "") 19 | set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "GNU") 20 | set(CMAKE_CXX_SIMULATE_VERSION "") 21 | 22 | 23 | 24 | 25 | set(CMAKE_AR "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ar.exe") 26 | set(CMAKE_CXX_COMPILER_AR "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc-ar.exe") 27 | set(CMAKE_RANLIB "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ranlib.exe") 28 | set(CMAKE_CXX_COMPILER_RANLIB "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc-ranlib.exe") 29 | set(CMAKE_LINKER "C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/ld.exe") 30 | set(CMAKE_MT "") 31 | set(CMAKE_TAPI "CMAKE_TAPI-NOTFOUND") 32 | set(CMAKE_COMPILER_IS_GNUCXX 1) 33 | set(CMAKE_CXX_COMPILER_LOADED 1) 34 | set(CMAKE_CXX_COMPILER_WORKS ) 35 | set(CMAKE_CXX_ABI_COMPILED ) 36 | 37 | set(CMAKE_CXX_COMPILER_ENV_VAR "CXX") 38 | 39 | set(CMAKE_CXX_COMPILER_ID_RUN 1) 40 | set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;m;mm;mpp;CPP;ixx;cppm;ccm;cxxm;c++m) 41 | set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC) 42 | 43 | foreach (lang C OBJC OBJCXX) 44 | if (CMAKE_${lang}_COMPILER_ID_RUN) 45 | foreach(extension IN LISTS CMAKE_${lang}_SOURCE_FILE_EXTENSIONS) 46 | list(REMOVE_ITEM CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${extension}) 47 | endforeach() 48 | endif() 49 | endforeach() 50 | 51 | set(CMAKE_CXX_LINKER_PREFERENCE 30) 52 | set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1) 53 | set(CMAKE_CXX_LINKER_DEPFILE_SUPPORTED ) 54 | 55 | # Save compiler ABI information. 56 | set(CMAKE_CXX_SIZEOF_DATA_PTR "") 57 | set(CMAKE_CXX_COMPILER_ABI "") 58 | set(CMAKE_CXX_BYTE_ORDER "") 59 | set(CMAKE_CXX_LIBRARY_ARCHITECTURE "") 60 | 61 | if(CMAKE_CXX_SIZEOF_DATA_PTR) 62 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}") 63 | endif() 64 | 65 | if(CMAKE_CXX_COMPILER_ABI) 66 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}") 67 | endif() 68 | 69 | if(CMAKE_CXX_LIBRARY_ARCHITECTURE) 70 | set(CMAKE_LIBRARY_ARCHITECTURE "") 71 | endif() 72 | 73 | set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "") 74 | if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX) 75 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}") 76 | endif() 77 | 78 | 79 | 80 | 81 | 82 | set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "") 83 | set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "") 84 | set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "") 85 | set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 86 | -------------------------------------------------------------------------------- /native/src/seal/util/globals.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/hestdparms.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace seal 13 | { 14 | class Modulus; 15 | 16 | namespace util 17 | { 18 | class MemoryPool; 19 | 20 | namespace global_variables 21 | { 22 | extern std::shared_ptr const global_memory_pool; 23 | 24 | /* 25 | For .NET Framework wrapper support (C++/CLI) we need to 26 | (1) compile the MemoryManager class as thread-unsafe because C++ 27 | mutexes cannot be brought through C++/CLI layer; 28 | (2) disable thread-safe memory pools. 29 | */ 30 | #ifndef _M_CEE 31 | extern thread_local std::shared_ptr const tls_memory_pool; 32 | #endif 33 | /** 34 | Default value for the standard deviation of the noise (error) distribution. 35 | */ 36 | constexpr double noise_standard_deviation = seal_he_std_parms_error_std_dev; 37 | 38 | constexpr double noise_distribution_width_multiplier = 6; 39 | 40 | constexpr double noise_max_deviation = noise_standard_deviation * noise_distribution_width_multiplier; 41 | 42 | /** 43 | This data structure is a key-value storage that maps degrees of the polynomial modulus 44 | to vectors of Modulus elements so that when used with the default value for the 45 | standard deviation of the noise distribution (noise_standard_deviation), the security 46 | level is at least 128 bits according to https://HomomorphicEncryption.org. This makes 47 | it easy for non-expert users to select secure parameters. 48 | */ 49 | const std::map> &GetDefaultCoeffModulus128(); 50 | 51 | /** 52 | This data structure is a key-value storage that maps degrees of the polynomial modulus 53 | to vectors of Modulus elements so that when used with the default value for the 54 | standard deviation of the noise distribution (noise_standard_deviation), the security 55 | level is at least 192 bits according to https://HomomorphicEncryption.org. This makes 56 | it easy for non-expert users to select secure parameters. 57 | */ 58 | const std::map> &GetDefaultCoeffModulus192(); 59 | 60 | /** 61 | This data structure is a key-value storage that maps degrees of the polynomial modulus 62 | to vectors of Modulus elements so that when used with the default value for the 63 | standard deviation of the noise distribution (noise_standard_deviation), the security 64 | level is at least 256 bits according to https://HomomorphicEncryption.org. This makes 65 | it easy for non-expert users to select secure parameters. 66 | */ 67 | const std::map> &GetDefaultCoeffModulus256(); 68 | } // namespace global_variables 69 | } // namespace util 70 | } // namespace seal 71 | -------------------------------------------------------------------------------- /native/src/seal/util/helper.cu: -------------------------------------------------------------------------------- 1 | #include "helper.cuh" 2 | #include "common.h" 3 | #include "seal/util/rns.cuh" 4 | 5 | namespace seal 6 | { 7 | 8 | 9 | __global__ void fillTablePsi128(uint64_t psiinv, uint64_t q, uint64_t psiinvTable[], uint64_t nbit) 10 | { 11 | int i = blockIdx.x * blockDim.x + threadIdx.x; 12 | psiinvTable[i] = modpow128(psiinv, bitReverse(i, nbit), q); 13 | } 14 | 15 | 16 | __global__ void fillTablePsi128_root(uint64_t psi, uint64_t q, uint64_t *psiTable, int nx, int ny) 17 | { 18 | int ix = blockIdx.x * blockDim.x + threadIdx.x; 19 | int iy = blockIdx.y * blockDim.y + threadIdx.y; 20 | unsigned int idx = ix * nx + iy; 21 | if (ix < nx && iy < ny) 22 | { 23 | 24 | psiTable[idx] = modpow128(psi, 2 * ix * iy + iy, q); 25 | } 26 | } 27 | 28 | __global__ void fillTablePsi128_root_n12(uint64_t psi, uint64_t q, uint64_t *psiTable, int n, int p) 29 | { 30 | int row = blockIdx.y * blockDim.y + threadIdx.y; 31 | int col = blockIdx.x * blockDim.x + threadIdx.x; 32 | unsigned int idx = row * p + col; 33 | if (row < n && col < p) 34 | { 35 | psiTable[idx] = modpow128(psi, 2 * row * col + col, q); 36 | } 37 | } 38 | 39 | __global__ void fillTablePsi128_root_n2(uint64_t psi, uint64_t q, uint64_t *psiTable, int nx, int ny) 40 | { 41 | int ix = blockIdx.x * blockDim.x + threadIdx.x; 42 | int iy = blockIdx.y * blockDim.y + threadIdx.y; 43 | unsigned int idx = ix * ny + iy; 44 | if (ix < nx && iy < ny) 45 | { 46 | psiTable[idx] = modpow128(psi, 2 * ix * iy, q); 47 | } 48 | } 49 | 50 | __global__ void print_helper(uint64_t *input, size_t size) 51 | { 52 | uint64_t index = blockIdx.x * blockDim.x + threadIdx.x; 53 | if (index < size) 54 | { 55 | printf("kernel result[%d]: %llu\n", index, input[index]); 56 | } 57 | } 58 | 59 | __global__ void set_poly_kernel(uint64_t *ori, uint64_t *dest, uint64_t coeff_count, uint64_t coeff_modulus_size) { 60 | uint64_t index = blockIdx.x * blockDim.x + threadIdx.x; 61 | while (index < coeff_count * coeff_modulus_size) { 62 | dest[index] = ori[index]; 63 | index += blockDim.x * gridDim.x; 64 | } 65 | } 66 | 67 | __global__ void set_uint_kernel(uint64_t *value, size_t uint64_count, uint64_t *result) 68 | { 69 | size_t idx = blockIdx.x * blockDim.x + threadIdx.x; 70 | if ((value == result) || !uint64_count) 71 | { 72 | return; 73 | } 74 | 75 | while (idx < uint64_count) 76 | { 77 | result[idx] = value[idx]; 78 | idx += blockDim.x * gridDim.x; 79 | } 80 | } 81 | 82 | __global__ void set_zero_poly_kernel(size_t coeff_count, size_t coeff_modulus_size, uint64_t *destination) 83 | { 84 | size_t idx = blockIdx.x * blockDim.x + threadIdx.x; 85 | while (idx < coeff_count * coeff_modulus_size) 86 | { 87 | destination[idx] = std::uint64_t(0); 88 | idx += blockDim.x * gridDim.x; 89 | } 90 | } 91 | 92 | inline void print_value(uint64_t *value, int count) 93 | { 94 | print_helper<<<1, count>>>(value, count); 95 | } 96 | 97 | } // namespace seal -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/Makefile.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | # The generator used is: 5 | set(CMAKE_DEPENDS_GENERATOR "Unix Makefiles") 6 | 7 | # The top level Makefile was generated from the following files: 8 | set(CMAKE_MAKEFILE_DEPENDS 9 | "CMakeCache.txt" 10 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeCUDAInformation.cmake" 11 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeCXXInformation.cmake" 12 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeCheckCompilerFlagCommonPatterns.cmake" 13 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeCommonLanguageInclude.cmake" 14 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeFindDependencyMacro.cmake" 15 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeGenericSystem.cmake" 16 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeInitializeConfigs.cmake" 17 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeLanguageInformation.cmake" 18 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeSystemSpecificInformation.cmake" 19 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CMakeSystemSpecificInitialize.cmake" 20 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CheckCXXSourceCompiles.cmake" 21 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CheckIncludeFileCXX.cmake" 22 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/CheckLibraryExists.cmake" 23 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Compiler/CMakeCommonCompilerMacros.cmake" 24 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Compiler/GNU-CXX.cmake" 25 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Compiler/GNU.cmake" 26 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Compiler/NVIDIA-CUDA.cmake" 27 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake" 28 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/FindPackageMessage.cmake" 29 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/FindThreads.cmake" 30 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Internal/CMakeCheckCompilerFlag.cmake" 31 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Platform/Linux-GNU-CXX.cmake" 32 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Platform/Linux-GNU.cmake" 33 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Platform/Linux.cmake" 34 | "/home/cmake-3.18.3-Linux-x86_64/share/cmake-3.18/Modules/Platform/UnixPaths.cmake" 35 | "../CMakeLists.txt" 36 | "CMakeFiles/3.18.3/CMakeCUDACompiler.cmake" 37 | "CMakeFiles/3.18.3/CMakeCXXCompiler.cmake" 38 | "CMakeFiles/3.18.3/CMakeSystem.cmake" 39 | "/usr/local/lib/cmake/SEAL-4.1/SEALConfig.cmake" 40 | "/usr/local/lib/cmake/SEAL-4.1/SEALConfigVersion.cmake" 41 | "/usr/local/lib/cmake/SEAL-4.1/SEALTargets-release.cmake" 42 | "/usr/local/lib/cmake/SEAL-4.1/SEALTargets.cmake" 43 | ) 44 | 45 | # The corresponding makefile is: 46 | set(CMAKE_MAKEFILE_OUTPUTS 47 | "Makefile" 48 | "CMakeFiles/cmake.check_cache" 49 | ) 50 | 51 | # Byproducts of CMake generate step: 52 | set(CMAKE_MAKEFILE_PRODUCTS 53 | "CMakeFiles/CMakeDirectoryInformation.cmake" 54 | ) 55 | 56 | # Dependency information for all targets: 57 | set(CMAKE_DEPEND_INFO_FILES 58 | "CMakeFiles/sealexamples.dir/DependInfo.cmake" 59 | ) 60 | -------------------------------------------------------------------------------- /native/examples/examples.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "examples.h" 5 | 6 | using namespace std; 7 | using namespace seal; 8 | 9 | int main() 10 | { 11 | cout << "IDEA SEAL_GPU version: " << SEAL_VERSION << endl; 12 | while (true) 13 | { 14 | cout << "+---------------------------------------------------------+" << endl; 15 | cout << "| The following examples should be executed while reading |" << endl; 16 | cout << "| comments in associated files in native/examples/. |" << endl; 17 | cout << "+---------------------------------------------------------+" << endl; 18 | cout << "| Examples | Source Files |" << endl; 19 | cout << "+----------------------------+----------------------------+" << endl; 20 | cout << "| 1. BFV Basics | 1_bfv_basics.cpp |" << endl; 21 | cout << "| 2. Encoders | 2_encoders.cpp |" << endl; 22 | cout << "| 3. Levels | 3_levels.cpp |" << endl; 23 | cout << "| 5. CKKS Basics | 4_ckks_basics.cpp |" << endl; 24 | cout << "| 6. Rotation | 5_rotation.cpp |" << endl; 25 | cout << "| 8. Performance Test | 6_performance.cpp |" << endl; 26 | cout << "+----------------------------+----------------------------+" << endl; 27 | 28 | /* 29 | Print how much memory we have allocated from the current memory pool. 30 | By default the memory pool will be a static global pool and the 31 | MemoryManager class can be used to change it. Most users should have 32 | little or no reason to touch the memory allocation system. 33 | */ 34 | size_t megabytes = MemoryManager::GetPool().alloc_byte_count() >> 20; 35 | cout << "[" << setw(7) << right << megabytes << " MB] " 36 | << "Total allocation from the memory pool" << endl; 37 | 38 | int selection = 0; 39 | bool valid = true; 40 | do 41 | { 42 | cout << endl << "> Run example (1 ~ 8) or exit (0): "; 43 | if (!(cin >> selection)) 44 | { 45 | valid = false; 46 | } 47 | else if (selection < 0 || selection > 8) 48 | { 49 | valid = false; 50 | } 51 | else 52 | { 53 | valid = true; 54 | } 55 | if (!valid) 56 | { 57 | cout << " [Beep~~] valid option: type 0 ~ 8" << endl; 58 | cin.clear(); 59 | cin.ignore(numeric_limits::max(), '\n'); 60 | } 61 | } while (!valid); 62 | 63 | switch (selection) 64 | { 65 | case 1: 66 | example_bfv_basics(); 67 | break; 68 | 69 | case 2: 70 | example_encoders(); 71 | break; 72 | 73 | case 3: 74 | example_levels(); 75 | break; 76 | 77 | case 4: 78 | example_ckks_basics(); 79 | break; 80 | 81 | case 5: 82 | example_rotation(); 83 | break; 84 | 85 | case 6: 86 | example_performance_test(); 87 | break; 88 | 89 | case 0: 90 | return 0; 91 | } 92 | } 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/CMakeCUDACompilerId.fatbin.c: -------------------------------------------------------------------------------- 1 | #ifndef __SKIP_INTERNAL_FATBINARY_HEADERS 2 | #include "fatbinary_section.h" 3 | #endif 4 | #define __CUDAFATBINSECTION ".nvFatBinSegment" 5 | #define __CUDAFATBINDATASECTION ".nv_fatbin" 6 | asm( 7 | ".section .nv_fatbin, \"a\"\n" 8 | ".align 8\n" 9 | "fatbinData:\n" 10 | ".quad 0x00100001ba55ed50,0x00000000000003d8,0x0000004001010002,0x0000000000000318\n" 11 | ".quad 0x0000000000000000,0x0000003400010007,0x0000000000000000,0x0000000000000011\n" 12 | ".quad 0x0000000000000000,0x0000000000000000,0x33010102464c457f,0x0000000000000007\n" 13 | ".quad 0x0000007500be0002,0x0000000000000000,0x0000000000000000,0x00000000000001d8\n" 14 | ".quad 0x0000004000340534,0x0001000500400000,0x7472747368732e00,0x747274732e006261\n" 15 | ".quad 0x746d79732e006261,0x746d79732e006261,0x78646e68735f6261,0x666e692e766e2e00\n" 16 | ".quad 0x65722e766e2e006f,0x6e6f697463612e6c,0x72747368732e0000,0x7274732e00626174\n" 17 | ".quad 0x6d79732e00626174,0x6d79732e00626174,0x646e68735f626174,0x6e692e766e2e0078\n" 18 | ".quad 0x722e766e2e006f66,0x6f697463612e6c65,0x000000000000006e,0x0000000000000000\n" 19 | ".quad 0x0000000000000000,0x0000000000000000,0x0004000300000032,0x0000000000000000\n" 20 | ".quad 0x0000000000000000,0x000000000000004b,0x222f0a1008020200,0x0000000008000000\n" 21 | ".quad 0x0000000008080000,0x0000000008100000,0x0000000008180000,0x0000000008200000\n" 22 | ".quad 0x0000000008280000,0x0000000008300000,0x0000000008380000,0x0000000008000001\n" 23 | ".quad 0x0000000008080001,0x0000000008100001,0x0000000008180001,0x0000000008200001\n" 24 | ".quad 0x0000000008280001,0x0000000008300001,0x0000000008380001,0x0000000008000002\n" 25 | ".quad 0x0000000008080002,0x0000000008100002,0x0000000008180002,0x0000000008200002\n" 26 | ".quad 0x0000000008280002,0x0000000008300002,0x0000000008380002,0x0000002c14000000\n" 27 | ".quad 0x000000000c000009,0x0000000000000000,0x0000000000000000,0x0000000000000000\n" 28 | ".quad 0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000\n" 29 | ".quad 0x0000000000000000,0x0000000300000001,0x0000000000000000,0x0000000000000000\n" 30 | ".quad 0x0000000000000040,0x0000000000000041,0x0000000000000000,0x0000000000000001\n" 31 | ".quad 0x0000000000000000,0x000000030000000b,0x0000000000000000,0x0000000000000000\n" 32 | ".quad 0x0000000000000081,0x0000000000000041,0x0000000000000000,0x0000000000000001\n" 33 | ".quad 0x0000000000000000,0x0000000200000013,0x0000000000000000,0x0000000000000000\n" 34 | ".quad 0x00000000000000c8,0x0000000000000030,0x0000000200000002,0x0000000000000008\n" 35 | ".quad 0x0000000000000018,0x7000000b00000032,0x0000000000000000,0x0000000000000000\n" 36 | ".quad 0x00000000000000f8,0x00000000000000e0,0x0000000000000000,0x0000000000000008\n" 37 | ".quad 0x0000000000000008,0x0000004801010001,0x0000000000000038,0x0000004000000036\n" 38 | ".quad 0x0000003400070007,0x0000000000000000,0x0000000000002011,0x0000000000000000\n" 39 | ".quad 0x0000000000000038,0x0000000000000000,0x762e21f000010a13,0x37206e6f69737265\n" 40 | ".quad 0x677261742e0a372e,0x32355f6d73207465,0x7365726464612e0a,0x3620657a69735f73\n" 41 | ".quad 0x0000000a0a0a0a34\n" 42 | ".text\n"); 43 | #ifdef __cplusplus 44 | extern "C" { 45 | #endif 46 | extern const unsigned long long fatbinData[125]; 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | static const __fatBinC_Wrapper_t __fatDeviceText __attribute__ ((aligned (8))) __attribute__ ((section (__CUDAFATBINSECTION)))= 54 | { 0x466243b1, 1, fatbinData, 0 }; 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/3.18.3/CompilerIdCUDA/tmp/a_dlink.fatbin.c: -------------------------------------------------------------------------------- 1 | #ifndef __SKIP_INTERNAL_FATBINARY_HEADERS 2 | #include "fatbinary_section.h" 3 | #endif 4 | #define __CUDAFATBINSECTION ".nvFatBinSegment" 5 | #define __CUDAFATBINDATASECTION ".nv_fatbin" 6 | asm( 7 | ".section .nv_fatbin, \"a\"\n" 8 | ".align 8\n" 9 | "fatbinData:\n" 10 | ".quad 0x00100001ba55ed50,0x0000000000000400,0x0000004001010002,0x00000000000003c0\n" 11 | ".quad 0x0000000000000000,0x0000003400010007,0x0000000000000000,0x0000000000000011\n" 12 | ".quad 0x0000000000000000,0x0000000000000000,0x33010102464c457f,0x0000000000000007\n" 13 | ".quad 0x0000007500be0002,0x0000000000000000,0x0000000000000000,0x0000000000000240\n" 14 | ".quad 0x0000004000340534,0x0001000600400000,0x7472747368732e00,0x747274732e006261\n" 15 | ".quad 0x746d79732e006261,0x746d79732e006261,0x78646e68735f6261,0x666e692e766e2e00\n" 16 | ".quad 0x61632e766e2e006f,0x0068706172676c6c,0x746f72702e766e2e,0x6e2e00657079746f\n" 17 | ".quad 0x63612e6c65722e76,0x732e00006e6f6974,0x0062617472747368,0x006261747274732e\n" 18 | ".quad 0x006261746d79732e,0x5f6261746d79732e,0x6e2e0078646e6873,0x2e006f666e692e76\n" 19 | ".quad 0x676c6c61632e766e,0x766e2e0068706172,0x79746f746f72702e,0x722e766e2e006570\n" 20 | ".quad 0x6f697463612e6c65,0x000000000000006e,0x0000000000000000,0x0000000000000000\n" 21 | ".quad 0x0000000000000000,0x0004000300000032,0x0000000000000000,0x0000000000000000\n" 22 | ".quad 0x000500030000004e,0x0000000000000000,0x0000000000000000,0xffffffff00000000\n" 23 | ".quad 0xfffffffe00000000,0xfffffffd00000000,0x000000000000004b,0x222f0a1008020200\n" 24 | ".quad 0x0000000008000000,0x0000000008080000,0x0000000008100000,0x0000000008180000\n" 25 | ".quad 0x0000000008200000,0x0000000008280000,0x0000000008300000,0x0000000008380000\n" 26 | ".quad 0x0000000008000001,0x0000000008080001,0x0000000008100001,0x0000000008180001\n" 27 | ".quad 0x0000000008200001,0x0000000008280001,0x0000000008300001,0x0000000008380001\n" 28 | ".quad 0x0000000008000002,0x0000000008080002,0x0000000008100002,0x0000000008180002\n" 29 | ".quad 0x0000000008200002,0x0000000008280002,0x0000000008300002,0x0000000008380002\n" 30 | ".quad 0x0000002c14000000,0x000000000c000009,0x0000000000000000,0x0000000000000000\n" 31 | ".quad 0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000\n" 32 | ".quad 0x0000000000000000,0x0000000000000000,0x0000000300000001,0x0000000000000000\n" 33 | ".quad 0x0000000000000000,0x0000000000000040,0x000000000000005d,0x0000000000000000\n" 34 | ".quad 0x0000000000000001,0x0000000000000000,0x000000030000000b,0x0000000000000000\n" 35 | ".quad 0x0000000000000000,0x000000000000009d,0x000000000000005d,0x0000000000000000\n" 36 | ".quad 0x0000000000000001,0x0000000000000000,0x0000000200000013,0x0000000000000000\n" 37 | ".quad 0x0000000000000000,0x0000000000000100,0x0000000000000048,0x0000000300000002\n" 38 | ".quad 0x0000000000000008,0x0000000000000018,0x7000000100000032,0x0000000000000000\n" 39 | ".quad 0x0000000000000000,0x0000000000000148,0x0000000000000018,0x0000000000000003\n" 40 | ".quad 0x0000000000000004,0x0000000000000008,0x7000000b0000004e,0x0000000000000000\n" 41 | ".quad 0x0000000000000000,0x0000000000000160,0x00000000000000e0,0x0000000000000000\n" 42 | ".quad 0x0000000000000008, 0x0000000000000008\n" 43 | ".text\n"); 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | extern const unsigned long long fatbinData[130]; 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | #ifdef __cplusplus 52 | extern "C" { 53 | #endif 54 | static const __fatBinC_Wrapper_t __fatDeviceText __attribute__ ((aligned (8))) __attribute__ ((section (__CUDAFATBINSECTION)))= 55 | { 0x466243b1, 2, fatbinData, (void**)__cudaPrelinkedFatbins }; 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /native/src/seal/util/ztools.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/defines.h" 7 | 8 | #if defined(SEAL_USE_ZLIB) || defined(SEAL_USE_ZSTD) 9 | #include "seal/dynarray.h" 10 | #include "seal/memorymanager.h" 11 | #include 12 | #include 13 | 14 | namespace seal 15 | { 16 | namespace util 17 | { 18 | namespace ztools 19 | { 20 | /** 21 | Compresses data in the given buffer, completes the given SEALHeader by writing in the size of the output and 22 | setting the compression mode to compr_mode_type::zlib and finally writes the SEALHeader followed by the 23 | compressed data in the given stream. 24 | 25 | @param[in] in The buffer to compress 26 | @param[in] in_size The size of the buffer to compress in bytes 27 | @param[out] header A pointer to a SEALHeader instance matching the output of the compression 28 | @param[out] out_stream The stream to write to 29 | @param[in] pool The MemoryPoolHandle pointing to a valid memory pool 30 | @throws std::invalid_argument if pool is uninitialized 31 | @throws std::logic_error if compression failed 32 | */ 33 | void zlib_write_header_deflate_buffer( 34 | DynArray &in, void *header_ptr, std::ostream &out_stream, MemoryPoolHandle pool); 35 | 36 | int zlib_deflate_array_inplace(DynArray &in, MemoryPoolHandle pool); 37 | 38 | int zlib_inflate_stream( 39 | std::istream &in_stream, std::streamoff in_size, std::ostream &out_stream, MemoryPoolHandle pool); 40 | 41 | /** 42 | Compresses data in the given buffer, completes the given SEALHeader by writing in the size of the output and 43 | setting the compression mode to compr_mode_type::zstd and finally writes the SEALHeader followed by the 44 | compressed data in the given stream. 45 | 46 | @param[in] in The buffer to compress 47 | @param[in] in_size The size of the buffer to compress in bytes 48 | @param[out] header A pointer to a SEALHeader instance matching the output of the compression 49 | @param[out] out_stream The stream to write to 50 | @param[in] pool The MemoryPoolHandle pointing to a valid memory pool 51 | @throws std::invalid_argument if pool is uninitialized 52 | @throws std::logic_error if compression failed 53 | */ 54 | void zstd_write_header_deflate_buffer( 55 | DynArray &in, void *header_ptr, std::ostream &out_stream, MemoryPoolHandle pool); 56 | 57 | unsigned zstd_deflate_array_inplace(DynArray &in, MemoryPoolHandle pool); 58 | 59 | unsigned zstd_inflate_stream( 60 | std::istream &in_stream, std::streamoff in_size, std::ostream &out_stream, MemoryPoolHandle pool); 61 | 62 | template 63 | SEAL_NODISCARD SizeT zlib_deflate_size_bound(SizeT in_size) 64 | { 65 | return util::add_safe(in_size, in_size >> 12, in_size >> 14, in_size >> 25, SizeT(17)); 66 | } 67 | 68 | template 69 | SEAL_NODISCARD SizeT zstd_deflate_size_bound(SizeT in_size) 70 | { 71 | return util::add_safe( 72 | in_size, in_size >> 8, 73 | (in_size < (SizeT(128) << 10)) ? (((SizeT(128) << 10) - in_size) >> 11) : SizeT(0)); 74 | } 75 | } // namespace ztools 76 | } // namespace util 77 | } // namespace seal 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/CMakeConfigureLog.yaml: -------------------------------------------------------------------------------- 1 | 2 | --- 3 | events: 4 | - 5 | kind: "message-v1" 6 | backtrace: 7 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineSystem.cmake:211 (message)" 8 | - "CMakeLists.txt:37 (project)" 9 | message: | 10 | The system is: Windows - 10.0.22631 - AMD64 11 | - 12 | kind: "message-v1" 13 | backtrace: 14 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:17 (message)" 15 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:64 (__determine_compiler_id_test)" 16 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCXXCompiler.cmake:126 (CMAKE_DETERMINE_COMPILER_ID)" 17 | - "CMakeLists.txt:37 (project)" 18 | message: | 19 | Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" succeeded. 20 | Compiler: C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/g++.exe 21 | Build flags: 22 | Id flags: 23 | 24 | The output was: 25 | 0 26 | 27 | 28 | Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "a.exe" 29 | 30 | The CXX compiler identification is GNU, found in: 31 | C:/Users/IDEA/Downloads/seal_gpu-dev_for_publish/seal_gpu-dev_for_publish/cmake-build-debug/CMakeFiles/3.27.8/CompilerIdCXX/a.exe 32 | 33 | - 34 | kind: "message-v1" 35 | backtrace: 36 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:17 (message)" 37 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:64 (__determine_compiler_id_test)" 38 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCCompiler.cmake:123 (CMAKE_DETERMINE_COMPILER_ID)" 39 | - "CMakeLists.txt:37 (project)" 40 | message: | 41 | Compiling the C compiler identification source file "CMakeCCompilerId.c" succeeded. 42 | Compiler: C:/Program Files/JetBrains/CLion 2023.3.4/bin/mingw/bin/gcc.exe 43 | Build flags: 44 | Id flags: 45 | 46 | The output was: 47 | 0 48 | 49 | 50 | Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "a.exe" 51 | 52 | The C compiler identification is GNU, found in: 53 | C:/Users/IDEA/Downloads/seal_gpu-dev_for_publish/seal_gpu-dev_for_publish/cmake-build-debug/CMakeFiles/3.27.8/CompilerIdC/a.exe 54 | 55 | - 56 | kind: "message-v1" 57 | backtrace: 58 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:1117 (message)" 59 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCUDACompiler.cmake:75 (CMAKE_DETERMINE_COMPILER_ID_VENDOR)" 60 | - "CMakeLists.txt:37 (project)" 61 | message: | 62 | Checking whether the CUDA compiler is NVIDIA using "" did not match "nvcc: NVIDIA \\(R\\) Cuda compiler driver": 63 | - 64 | kind: "message-v1" 65 | backtrace: 66 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCompilerId.cmake:1117 (message)" 67 | - "C:/Program Files/JetBrains/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeDetermineCUDACompiler.cmake:75 (CMAKE_DETERMINE_COMPILER_ID_VENDOR)" 68 | - "CMakeLists.txt:37 (project)" 69 | message: | 70 | Checking whether the CUDA compiler is Clang using "" did not match "(clang version)": 71 | ... 72 | -------------------------------------------------------------------------------- /native/src/seal/util/clang.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #if SEAL_COMPILER == SEAL_COMPILER_CLANG 7 | 8 | // We require clang >= 5 9 | #if (__clang_major__ < 5) || not defined(__cplusplus) 10 | #error "SEAL requires __clang_major__ >= 5" 11 | #endif 12 | 13 | // Read in config.h 14 | #include "seal/util/config.h" 15 | 16 | #ifdef SEAL_USE_ALIGNED_ALLOC 17 | #include 18 | #define SEAL_MALLOC(size) \ 19 | static_cast((((size)&63) == 0) ? ::aligned_alloc(64, (size)) : std::malloc((size))) 20 | #define SEAL_FREE(ptr) std::free(ptr) 21 | #endif 22 | 23 | // Are intrinsics enabled? 24 | #ifdef SEAL_USE_INTRIN 25 | #if defined(__aarch64__) 26 | #include 27 | #elif defined(EMSCRIPTEN) 28 | #include 29 | #else 30 | #include 31 | #endif 32 | 33 | #ifdef SEAL_USE___BUILTIN_CLZLL 34 | #define SEAL_MSB_INDEX_UINT64(result, value) \ 35 | { \ 36 | *result = 63UL - static_cast(__builtin_clzll(value)); \ 37 | } 38 | #endif 39 | 40 | #ifdef SEAL_USE___INT128 41 | __extension__ typedef __int128 int128_t; 42 | __extension__ typedef unsigned __int128 uint128_t; 43 | #define SEAL_MULTIPLY_UINT64_HW64(operand1, operand2, hw64) \ 44 | do \ 45 | { \ 46 | *hw64 = static_cast( \ 47 | ((static_cast(operand1) * static_cast(operand2)) >> 64)); \ 48 | } while (false) 49 | 50 | #define SEAL_MULTIPLY_UINT64(operand1, operand2, result128) \ 51 | do \ 52 | { \ 53 | uint128_t product = static_cast(operand1) * operand2; \ 54 | result128[0] = static_cast(product); \ 55 | result128[1] = static_cast(product >> 64); \ 56 | } while (false) 57 | 58 | #define SEAL_DIVIDE_UINT128_UINT64(numerator, denominator, result) \ 59 | do \ 60 | { \ 61 | uint128_t n, q; \ 62 | n = (static_cast(numerator[1]) << 64) | (static_cast(numerator[0])); \ 63 | q = n / denominator; \ 64 | n -= q * denominator; \ 65 | numerator[0] = static_cast(n); \ 66 | numerator[1] = 0; \ 67 | result[0] = static_cast(q); \ 68 | result[1] = static_cast(q >> 64); \ 69 | } while (false) 70 | #endif 71 | 72 | #ifdef SEAL_USE__ADDCARRY_U64 73 | #define SEAL_ADD_CARRY_UINT64(operand1, operand2, carry, result) _addcarry_u64(carry, operand1, operand2, result) 74 | #endif 75 | 76 | #ifdef SEAL_USE__SUBBORROW_U64 77 | #define SEAL_SUB_BORROW_UINT64(operand1, operand2, borrow, result) _subborrow_u64(borrow, operand1, operand2, result) 78 | #endif 79 | 80 | #endif // SEAL_USE_INTRIN 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /native/src/seal/util/uintcore.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/util/common.h" 5 | #include "seal/util/uintarith.cuh" 6 | #include "seal/util/uintcore.h" 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | namespace seal 13 | { 14 | namespace util 15 | { 16 | string uint_to_hex_string(const uint64_t *value, size_t uint64_count) 17 | { 18 | #ifdef SEAL_DEBUG 19 | if (uint64_count && !value) 20 | { 21 | throw invalid_argument("value"); 22 | } 23 | #endif 24 | // Start with a string with a zero for each nibble in the array. 25 | size_t num_nibbles = mul_safe(uint64_count, static_cast(nibbles_per_uint64)); 26 | string output(num_nibbles, '0'); 27 | 28 | // Iterate through each uint64 in array and set string with correct nibbles in hex. 29 | size_t nibble_index = num_nibbles; 30 | size_t leftmost_non_zero_pos = num_nibbles; 31 | for (size_t i = 0; i < uint64_count; i++) 32 | { 33 | uint64_t part = *value++; 34 | 35 | // Iterate through each nibble in the current uint64. 36 | for (size_t j = 0; j < nibbles_per_uint64; j++) 37 | { 38 | size_t nibble = safe_cast(part & uint64_t(0x0F)); 39 | size_t pos = --nibble_index; 40 | if (nibble != 0) 41 | { 42 | // If nibble is not zero, then update string and save this pos to determine 43 | // number of leading zeros. 44 | output[pos] = nibble_to_upper_hex(static_cast(nibble)); 45 | leftmost_non_zero_pos = pos; 46 | } 47 | part >>= 4; 48 | } 49 | } 50 | 51 | // Trim string to remove leading zeros. 52 | output.erase(0, leftmost_non_zero_pos); 53 | 54 | // Return 0 if nothing remains. 55 | if (output.empty()) 56 | { 57 | return string("0"); 58 | } 59 | 60 | return output; 61 | } 62 | 63 | string uint_to_dec_string(const uint64_t *value, size_t uint64_count, MemoryPool &pool) 64 | { 65 | #ifdef SEAL_DEBUG 66 | if (uint64_count && !value) 67 | { 68 | throw invalid_argument("value"); 69 | } 70 | #endif 71 | if (!uint64_count) 72 | { 73 | return string("0"); 74 | } 75 | auto remainder(allocate_uint(uint64_count, pool)); 76 | auto quotient(allocate_uint(uint64_count, pool)); 77 | auto base(allocate_uint(uint64_count, pool)); 78 | uint64_t *remainderptr = remainder.get(); 79 | uint64_t *quotientptr = quotient.get(); 80 | uint64_t *baseptr = base.get(); 81 | set_uint(10, uint64_count, baseptr); 82 | set_uint(value, uint64_count, remainderptr); 83 | string output; 84 | while (!is_zero_uint(remainderptr, uint64_count)) 85 | { 86 | divide_uint_inplace(remainderptr, baseptr, uint64_count, quotientptr, pool); 87 | char digit = static_cast(remainderptr[0] + static_cast('0')); 88 | output += digit; 89 | swap(remainderptr, quotientptr); 90 | } 91 | reverse(output.begin(), output.end()); 92 | 93 | // Return 0 if nothing remains. 94 | if (output.empty()) 95 | { 96 | return string("0"); 97 | } 98 | 99 | return output; 100 | } 101 | } // namespace util 102 | } // namespace seal 103 | -------------------------------------------------------------------------------- /native/src/seal/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | 5 | set(GPU_SUPPORT_OPTION_STR "Use GPU to accelerate computation") 6 | option(GPU_SUPPORT ${GPU_SUPPORT_OPTION_STR} OFF) 7 | message(STATUS "GPU_SUPPORT: ${GPU_SUPPORT}") 8 | 9 | if(GPU_SUPPORT) 10 | set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc") 11 | set(CMAKE_CUDA_STANDARD 17) 12 | set(CMAKE_CXX_STANDARD 17) 13 | endif() 14 | # Source files in this directory 15 | 16 | if(GPU_SUPPORT) 17 | 18 | set(SEAL_SOURCE_FILES ${SEAL_SOURCE_FILES} 19 | 20 | # ${CMAKE_CURRENT_LIST_DIR}/batchencoder.cu 21 | ${CMAKE_CURRENT_LIST_DIR}/batchencoder.cu 22 | ${CMAKE_CURRENT_LIST_DIR}/ciphertext.cpp 23 | ${CMAKE_CURRENT_LIST_DIR}/ckks.cu 24 | ${CMAKE_CURRENT_LIST_DIR}/context.cu 25 | ${CMAKE_CURRENT_LIST_DIR}/decryptor.cu 26 | ${CMAKE_CURRENT_LIST_DIR}/decryptor.cpp 27 | ${CMAKE_CURRENT_LIST_DIR}/encryptionparams.cpp 28 | ${CMAKE_CURRENT_LIST_DIR}/encryptor.cu 29 | ${CMAKE_CURRENT_LIST_DIR}/evaluator.cu 30 | ${CMAKE_CURRENT_LIST_DIR}/keygenerator.cu 31 | ${CMAKE_CURRENT_LIST_DIR}/kswitchkeys.cpp 32 | ${CMAKE_CURRENT_LIST_DIR}/memorymanager.cpp 33 | ${CMAKE_CURRENT_LIST_DIR}/modulus.cpp 34 | ${CMAKE_CURRENT_LIST_DIR}/plaintext.cpp 35 | ${CMAKE_CURRENT_LIST_DIR}/randomgen.cpp 36 | ${CMAKE_CURRENT_LIST_DIR}/serialization.cpp 37 | ${CMAKE_CURRENT_LIST_DIR}/valcheck.cpp 38 | ) 39 | else() 40 | set(SEAL_SOURCE_FILES ${SEAL_SOURCE_FILES} 41 | ${CMAKE_CURRENT_LIST_DIR}/batchencoder.cpp 42 | ${CMAKE_CURRENT_LIST_DIR}/ciphertext.cpp 43 | ${CMAKE_CURRENT_LIST_DIR}/ckks.cpp 44 | ${CMAKE_CURRENT_LIST_DIR}/context.cpp 45 | ${CMAKE_CURRENT_LIST_DIR}/decryptor.cpp 46 | ${CMAKE_CURRENT_LIST_DIR}/encryptionparams.cpp 47 | ${CMAKE_CURRENT_LIST_DIR}/encryptor.cpp 48 | ${CMAKE_CURRENT_LIST_DIR}/evaluator.cpp 49 | ${CMAKE_CURRENT_LIST_DIR}/keygenerator.cpp 50 | ${CMAKE_CURRENT_LIST_DIR}/kswitchkeys.cpp 51 | ${CMAKE_CURRENT_LIST_DIR}/memorymanager.cpp 52 | ${CMAKE_CURRENT_LIST_DIR}/modulus.cpp 53 | ${CMAKE_CURRENT_LIST_DIR}/plaintext.cpp 54 | ${CMAKE_CURRENT_LIST_DIR}/randomgen.cpp 55 | ${CMAKE_CURRENT_LIST_DIR}/serialization.cpp 56 | ${CMAKE_CURRENT_LIST_DIR}/valcheck.cpp 57 | ) 58 | endif() 59 | # Add header files for installation 60 | install( 61 | FILES 62 | ${CMAKE_CURRENT_LIST_DIR}/batchencoder.h 63 | ${CMAKE_CURRENT_LIST_DIR}/ciphertext.cuh 64 | ${CMAKE_CURRENT_LIST_DIR}/ckks.h 65 | ${CMAKE_CURRENT_LIST_DIR}/modulus.h 66 | ${CMAKE_CURRENT_LIST_DIR}/context.cuh 67 | ${CMAKE_CURRENT_LIST_DIR}/decryptor.h 68 | ${CMAKE_CURRENT_LIST_DIR}/dynarray.h 69 | ${CMAKE_CURRENT_LIST_DIR}/encryptionparams.cuh 70 | ${CMAKE_CURRENT_LIST_DIR}/encryptor.h 71 | ${CMAKE_CURRENT_LIST_DIR}/evaluator.h 72 | ${CMAKE_CURRENT_LIST_DIR}/galoiskeys.h 73 | ${CMAKE_CURRENT_LIST_DIR}/keygenerator.h 74 | ${CMAKE_CURRENT_LIST_DIR}/kswitchkeys.h 75 | ${CMAKE_CURRENT_LIST_DIR}/memorymanager.h 76 | ${CMAKE_CURRENT_LIST_DIR}/plaintext.cuh 77 | ${CMAKE_CURRENT_LIST_DIR}/publickey.h 78 | ${CMAKE_CURRENT_LIST_DIR}/randomgen.h 79 | ${CMAKE_CURRENT_LIST_DIR}/randomtostd.h 80 | ${CMAKE_CURRENT_LIST_DIR}/relinkeys.h 81 | ${CMAKE_CURRENT_LIST_DIR}/seal.h 82 | ${CMAKE_CURRENT_LIST_DIR}/secretkey.cuh 83 | ${CMAKE_CURRENT_LIST_DIR}/serializable.h 84 | ${CMAKE_CURRENT_LIST_DIR}/serialization.h 85 | ${CMAKE_CURRENT_LIST_DIR}/valcheck.h 86 | ${CMAKE_CURRENT_LIST_DIR}/version.h 87 | ${CMAKE_CURRENT_LIST_DIR}/gpu_memorypool.h 88 | 89 | DESTINATION 90 | ${SEAL_INCLUDES_INSTALL_DIR}/seal 91 | ) 92 | 93 | add_subdirectory(util) 94 | set(SEAL_SOURCE_FILES ${SEAL_SOURCE_FILES} PARENT_SCOPE) 95 | -------------------------------------------------------------------------------- /native/bench/ntt.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/seal.h" 5 | #include "seal/util/rlwe.h" 6 | #include "bench.h" 7 | 8 | using namespace benchmark; 9 | using namespace sealbench; 10 | using namespace seal; 11 | using namespace std; 12 | 13 | /** 14 | This file defines benchmarks for NTT-related HE primitives. 15 | */ 16 | 17 | namespace sealbench 18 | { 19 | void bm_util_ntt_forward(State &state, shared_ptr bm_env) 20 | { 21 | vector &ct = bm_env->ct(); 22 | for (auto _ : state) 23 | { 24 | state.PauseTiming(); 25 | bm_env->randomize_ct_bfv(ct[0]); 26 | 27 | state.ResumeTiming(); 28 | bm_env->evaluator()->transform_to_ntt(ct[0], ct[2]); 29 | } 30 | } 31 | 32 | void bm_util_ntt_inverse(State &state, shared_ptr bm_env) 33 | { 34 | vector &ct = bm_env->ct(); 35 | for (auto _ : state) 36 | { 37 | state.PauseTiming(); 38 | bm_env->randomize_ct_bfv(ct[0]); 39 | bm_env->evaluator()->transform_to_ntt_inplace(ct[0]); 40 | 41 | state.ResumeTiming(); 42 | bm_env->evaluator()->transform_from_ntt(ct[0], ct[2]); 43 | } 44 | } 45 | 46 | void bm_util_ntt_forward_low_level(State &state, shared_ptr bm_env) 47 | { 48 | parms_id_type parms_id = bm_env->context().first_parms_id(); 49 | auto context_data = bm_env->context().get_context_data(parms_id); 50 | const auto &small_ntt_tables = context_data->small_ntt_tables(); 51 | vector &ct = bm_env->ct(); 52 | for (auto _ : state) 53 | { 54 | state.PauseTiming(); 55 | bm_env->randomize_ct_bfv(ct[0]); 56 | 57 | state.ResumeTiming(); 58 | ntt_negacyclic_harvey(ct[0].data(), small_ntt_tables[0]); 59 | } 60 | } 61 | 62 | void bm_util_ntt_inverse_low_level(State &state, shared_ptr bm_env) 63 | { 64 | parms_id_type parms_id = bm_env->context().first_parms_id(); 65 | auto context_data = bm_env->context().get_context_data(parms_id); 66 | const auto &small_ntt_tables = context_data->small_ntt_tables(); 67 | vector &ct = bm_env->ct(); 68 | for (auto _ : state) 69 | { 70 | state.PauseTiming(); 71 | bm_env->randomize_ct_bfv(ct[0]); 72 | 73 | state.ResumeTiming(); 74 | inverse_ntt_negacyclic_harvey(ct[0].data(), small_ntt_tables[0]); 75 | } 76 | } 77 | 78 | void bm_util_ntt_forward_low_level_lazy(State &state, shared_ptr bm_env) 79 | { 80 | parms_id_type parms_id = bm_env->context().first_parms_id(); 81 | auto context_data = bm_env->context().get_context_data(parms_id); 82 | const auto &small_ntt_tables = context_data->small_ntt_tables(); 83 | vector &ct = bm_env->ct(); 84 | for (auto _ : state) 85 | { 86 | state.PauseTiming(); 87 | bm_env->randomize_ct_bfv(ct[0]); 88 | 89 | state.ResumeTiming(); 90 | ntt_negacyclic_harvey_lazy(ct[0].data(), small_ntt_tables[0]); 91 | } 92 | } 93 | 94 | void bm_util_ntt_inverse_low_level_lazy(State &state, shared_ptr bm_env) 95 | { 96 | parms_id_type parms_id = bm_env->context().first_parms_id(); 97 | auto context_data = bm_env->context().get_context_data(parms_id); 98 | const auto &small_ntt_tables = context_data->small_ntt_tables(); 99 | vector &ct = bm_env->ct(); 100 | for (auto _ : state) 101 | { 102 | state.PauseTiming(); 103 | bm_env->randomize_ct_bfv(ct[0]); 104 | 105 | state.ResumeTiming(); 106 | inverse_ntt_negacyclic_harvey_lazy(ct[0].data(), small_ntt_tables[0]); 107 | } 108 | } 109 | } // namespace sealbench 110 | -------------------------------------------------------------------------------- /native/src/seal/relinkeys.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/ciphertext.cuh" 7 | #include "seal/kswitchkeys.h" 8 | #include "seal/memorymanager.h" 9 | #include "seal/util/defines.h" 10 | #include 11 | 12 | namespace seal 13 | { 14 | /** 15 | Class to store relinearization keys. 16 | 17 | @par Relinearization 18 | Freshly encrypted ciphertexts have a size of 2, and multiplying ciphertexts 19 | of sizes K and L results in a ciphertext of size K+L-1. Unfortunately, this 20 | growth in size slows down further multiplications and increases noise growth. 21 | Relinearization is an operation that has no semantic meaning, but it reduces 22 | the size of ciphertexts back to 2. IDEA SEAL_GPU can only relinearize size 3 23 | ciphertexts back to size 2, so if the ciphertexts grow larger than size 3, 24 | there is no way to reduce their size. Relinearization requires an instance of 25 | RelinKeys to be created by the secret key owner and to be shared with the 26 | evaluator. Note that plain multiplication is fundamentally different from 27 | normal multiplication and does not result in ciphertext size growth. 28 | 29 | @par When to Relinearize 30 | Typically, one should always relinearize after each multiplications. However, 31 | in some cases relinearization should be postponed as late as possible due to 32 | its computational cost. For example, suppose the computation involves several 33 | homomorphic multiplications followed by a sum of the results. In this case it 34 | makes sense to not relinearize each product, but instead add them first and 35 | only then relinearize the sum. This is particularly important when using the 36 | CKKS scheme, where relinearization is much more computationally costly than 37 | multiplications and additions. 38 | 39 | @par Thread Safety 40 | In general, reading from RelinKeys is thread-safe as long as no other thread 41 | is concurrently mutating it. This is due to the underlying data structure 42 | storing the relinearization keys not being thread-safe. 43 | 44 | @see GaloisKeys for the class that stores the Galois keys. 45 | @see KeyGenerator for the class that generates the relinearization keys. 46 | */ 47 | class RelinKeys : public KSwitchKeys 48 | { 49 | public: 50 | /** 51 | Returns the index of a relinearization key in the backing KSwitchKeys 52 | instance that corresponds to the given secret key power, assuming that 53 | it exists in the backing KSwitchKeys. 54 | 55 | @param[in] key_power The power of the secret key 56 | @throws std::invalid_argument if key_power is less than 2 57 | */ 58 | SEAL_NODISCARD inline static std::size_t get_index(std::size_t key_power) 59 | { 60 | if (key_power < 2) 61 | { 62 | throw std::invalid_argument("key_power cannot be less than 2"); 63 | } 64 | return key_power - 2; 65 | } 66 | 67 | /** 68 | Returns whether a relinearization key corresponding to a given power of 69 | the secret key exists. 70 | 71 | @param[in] key_power The power of the secret key 72 | @throws std::invalid_argument if key_power is less than 2 73 | */ 74 | SEAL_NODISCARD inline bool has_key(std::size_t key_power) const 75 | { 76 | std::size_t index = get_index(key_power); 77 | return data().size() > index && !data()[index].empty(); 78 | } 79 | 80 | /** 81 | Returns a const reference to a relinearization key. The returned 82 | relinearization key corresponds to the given power of the secret key. 83 | 84 | @param[in] key_power The power of the secret key 85 | @throws std::invalid_argument if the key corresponding to key_power does not exist 86 | */ 87 | SEAL_NODISCARD inline auto &key(std::size_t key_power) const 88 | { 89 | return KSwitchKeys::data(get_index(key_power)); 90 | } 91 | }; 92 | } // namespace seal 93 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | # BasedOnStyle: Microsoft 4 | AccessModifierOffset: -4 5 | AlignAfterOpenBracket: AlwaysBreak 6 | AlignConsecutiveMacros: false 7 | AlignConsecutiveAssignments: false 8 | AlignConsecutiveDeclarations: false 9 | AlignEscapedNewlines: Left 10 | AlignOperands: true 11 | AlignTrailingComments: false 12 | AllowAllArgumentsOnNextLine: true 13 | AllowAllConstructorInitializersOnNextLine: true 14 | AllowAllParametersOfDeclarationOnNextLine: true 15 | AllowShortBlocksOnASingleLine: false 16 | AllowShortCaseLabelsOnASingleLine: false 17 | AllowShortFunctionsOnASingleLine: None 18 | AllowShortLambdasOnASingleLine: Inline 19 | AllowShortIfStatementsOnASingleLine: Never 20 | AllowShortLoopsOnASingleLine: false 21 | AlwaysBreakAfterDefinitionReturnType: None 22 | AlwaysBreakAfterReturnType: None 23 | AlwaysBreakBeforeMultilineStrings: false 24 | AlwaysBreakTemplateDeclarations: Yes 25 | BinPackArguments: true 26 | BinPackParameters: true 27 | BraceWrapping: 28 | AfterCaseLabel: true 29 | AfterClass: true 30 | AfterControlStatement: true 31 | AfterEnum: true 32 | AfterFunction: true 33 | AfterNamespace: true 34 | AfterObjCDeclaration: true 35 | AfterStruct: true 36 | AfterUnion: true 37 | AfterExternBlock: true 38 | BeforeCatch: true 39 | BeforeElse: true 40 | IndentBraces: false 41 | SplitEmptyFunction: false 42 | SplitEmptyRecord: false 43 | SplitEmptyNamespace: false 44 | BreakBeforeBinaryOperators: None 45 | BreakBeforeBraces: Custom # Allman 46 | BreakBeforeInheritanceComma: false 47 | BreakInheritanceList: BeforeColon 48 | BreakBeforeTernaryOperators: true 49 | BreakConstructorInitializersBeforeComma: false 50 | BreakConstructorInitializers: BeforeColon 51 | BreakAfterJavaFieldAnnotations: false 52 | BreakStringLiterals: true 53 | ColumnLimit: 120 54 | CommentPragmas: '^ IWYU pragma:' 55 | CompactNamespaces: false 56 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 57 | ConstructorInitializerIndentWidth: 4 58 | ContinuationIndentWidth: 4 59 | Cpp11BracedListStyle: false 60 | DerivePointerAlignment: false 61 | DisableFormat: false 62 | ExperimentalAutoDetectBinPacking: false 63 | FixNamespaceComments: true 64 | ForEachMacros: 65 | - foreach 66 | - Q_FOREACH 67 | - BOOST_FOREACH 68 | IncludeBlocks: Merge 69 | IncludeCategories: 70 | - Regex: '<.*>' 71 | Priority: 1 72 | - Regex: '"seal/util/.*"' 73 | Priority: -2 74 | - Regex: '"seal/.*"' 75 | Priority: -3 76 | IncludeIsMainRegex: '(Test)?$' 77 | IndentCaseLabels: false 78 | IndentPPDirectives: None 79 | IndentWidth: 4 80 | IndentWrappedFunctionNames: false 81 | JavaScriptQuotes: Leave 82 | JavaScriptWrapImports: true 83 | KeepEmptyLinesAtTheStartOfBlocks: false 84 | MacroBlockBegin: '' 85 | MacroBlockEnd: '' 86 | MaxEmptyLinesToKeep: 1 87 | NamespaceIndentation: All 88 | ObjCBinPackProtocolList: Auto 89 | ObjCBlockIndentWidth: 2 90 | ObjCSpaceAfterProperty: false 91 | ObjCSpaceBeforeProtocolList: true 92 | PenaltyBreakAssignment: 2 93 | PenaltyBreakBeforeFirstCallParameter: 19 94 | PenaltyBreakComment: 300 95 | PenaltyBreakFirstLessLess: 120 96 | PenaltyBreakString: 1000 97 | PenaltyBreakTemplateDeclaration: 10 98 | PenaltyExcessCharacter: 1000000 99 | PenaltyReturnTypeOnItsOwnLine: 1000 100 | PointerAlignment: Right 101 | ReflowComments: true 102 | SortIncludes: true 103 | SortUsingDeclarations: true 104 | SpaceAfterCStyleCast: false 105 | SpaceAfterLogicalNot: false 106 | SpaceAfterTemplateKeyword: true 107 | SpaceBeforeAssignmentOperators: true 108 | SpaceBeforeCpp11BracedList: false 109 | SpaceBeforeCtorInitializerColon: true 110 | SpaceBeforeInheritanceColon: true 111 | SpaceBeforeParens: ControlStatements 112 | SpaceBeforeRangeBasedForLoopColon: true 113 | SpaceInEmptyParentheses: false 114 | SpacesBeforeTrailingComments: 1 115 | SpacesInAngles: false 116 | SpacesInContainerLiterals: true 117 | SpacesInCStyleCastParentheses: false 118 | SpacesInParentheses: false 119 | SpacesInSquareBrackets: false 120 | Standard: Auto 121 | StatementMacros: 122 | - Q_UNUSED 123 | - QT_REQUIRE_VERSION 124 | TabWidth: 4 125 | UseTab: Never 126 | ... 127 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/Makefile2: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | #============================================================================= 10 | # Special targets provided by cmake. 11 | 12 | # Disable implicit rules so canonical targets will work. 13 | .SUFFIXES: 14 | 15 | 16 | # Disable VCS-based implicit rules. 17 | % : %,v 18 | 19 | 20 | # Disable VCS-based implicit rules. 21 | % : RCS/% 22 | 23 | 24 | # Disable VCS-based implicit rules. 25 | % : RCS/%,v 26 | 27 | 28 | # Disable VCS-based implicit rules. 29 | % : SCCS/s.% 30 | 31 | 32 | # Disable VCS-based implicit rules. 33 | % : s.% 34 | 35 | 36 | .SUFFIXES: .hpux_make_needs_suffix_list 37 | 38 | 39 | # Command-line flag to silence nested $(MAKE). 40 | $(VERBOSE)MAKESILENT = -s 41 | 42 | #Suppress display of executed commands. 43 | $(VERBOSE).SILENT: 44 | 45 | # A target that is always out of date. 46 | cmake_force: 47 | 48 | .PHONY : cmake_force 49 | 50 | #============================================================================= 51 | # Set environment variables for the build. 52 | 53 | # The shell in which to execute make rules. 54 | SHELL = /bin/sh 55 | 56 | # The CMake executable. 57 | CMAKE_COMMAND = /home/cmake-3.18.3-Linux-x86_64/bin/cmake 58 | 59 | # The command to remove a file. 60 | RM = /home/cmake-3.18.3-Linux-x86_64/bin/cmake -E rm -f 61 | 62 | # Escaping for special characters. 63 | EQUALS = = 64 | 65 | # The top-level source directory on which CMake was run. 66 | CMAKE_SOURCE_DIR = /home/liqirui/SEAL/native/examples 67 | 68 | # The top-level build directory on which CMake was run. 69 | CMAKE_BINARY_DIR = /home/liqirui/SEAL/native/examples/build 70 | 71 | #============================================================================= 72 | # Directory level rules for the build root directory 73 | 74 | # The main recursive "all" target. 75 | all: CMakeFiles/sealexamples.dir/all 76 | 77 | .PHONY : all 78 | 79 | # The main recursive "preinstall" target. 80 | preinstall: 81 | 82 | .PHONY : preinstall 83 | 84 | # The main recursive "clean" target. 85 | clean: CMakeFiles/sealexamples.dir/clean 86 | 87 | .PHONY : clean 88 | 89 | #============================================================================= 90 | # Target rules for target CMakeFiles/sealexamples.dir 91 | 92 | # All Build rule for target. 93 | CMakeFiles/sealexamples.dir/all: 94 | $(MAKE) $(MAKESILENT) -f CMakeFiles/sealexamples.dir/build.make CMakeFiles/sealexamples.dir/depend 95 | $(MAKE) $(MAKESILENT) -f CMakeFiles/sealexamples.dir/build.make CMakeFiles/sealexamples.dir/build 96 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/liqirui/SEAL/native/examples/build/CMakeFiles --progress-num=1,2 "Built target sealexamples" 97 | .PHONY : CMakeFiles/sealexamples.dir/all 98 | 99 | # Build rule for subdir invocation for target. 100 | CMakeFiles/sealexamples.dir/rule: cmake_check_build_system 101 | $(CMAKE_COMMAND) -E cmake_progress_start /home/liqirui/SEAL/native/examples/build/CMakeFiles 2 102 | $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/sealexamples.dir/all 103 | $(CMAKE_COMMAND) -E cmake_progress_start /home/liqirui/SEAL/native/examples/build/CMakeFiles 0 104 | .PHONY : CMakeFiles/sealexamples.dir/rule 105 | 106 | # Convenience name for target. 107 | sealexamples: CMakeFiles/sealexamples.dir/rule 108 | 109 | .PHONY : sealexamples 110 | 111 | # clean rule for target. 112 | CMakeFiles/sealexamples.dir/clean: 113 | $(MAKE) $(MAKESILENT) -f CMakeFiles/sealexamples.dir/build.make CMakeFiles/sealexamples.dir/clean 114 | .PHONY : CMakeFiles/sealexamples.dir/clean 115 | 116 | #============================================================================= 117 | # Special targets to cleanup operation of make. 118 | 119 | # Special rule to run CMake to check the build system integrity. 120 | # No rule that depends on this can have commands that come from listfiles 121 | # because they might be regenerated. 122 | cmake_check_build_system: 123 | $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 124 | .PHONY : cmake_check_build_system 125 | 126 | -------------------------------------------------------------------------------- /native/src/seal/util/gcc.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #if SEAL_COMPILER == SEAL_COMPILER_GCC 7 | 8 | // We require GCC >= 6 9 | #if (__GNUC__ < 6) || not defined(__cplusplus) 10 | #pragma GCC error "SEAL requires __GNUC__ >= 6" 11 | #endif 12 | 13 | // Read in config.h 14 | #include "seal/util/config.h" 15 | 16 | #if (__GNUC__ == 6) && defined(SEAL_USE_IF_CONSTEXPR) 17 | #pragma GCC error "g++-6 cannot compile IDEA SEAL_GPU as C++17; set CMake build option `SEAL_USE_CXX17' to OFF" 18 | #endif 19 | 20 | #define SEAL_FORCE_INLINE inline __attribute__((always_inline)) 21 | 22 | #ifdef SEAL_USE_ALIGNED_ALLOC 23 | #include 24 | #define SEAL_MALLOC(size) \ 25 | static_cast((((size)&63) == 0) ? ::aligned_alloc(64, (size)) : std::malloc((size))) 26 | #define SEAL_FREE(ptr) std::free(ptr) 27 | #endif 28 | 29 | // Are intrinsics enabled? 30 | #ifdef SEAL_USE_INTRIN 31 | #if defined(__aarch64__) 32 | #include 33 | #elif defined(EMSCRIPTEN) 34 | #include 35 | #else 36 | #include 37 | #endif 38 | 39 | #ifdef SEAL_USE___BUILTIN_CLZLL 40 | #define SEAL_MSB_INDEX_UINT64(result, value) \ 41 | { \ 42 | *result = 63UL - static_cast(__builtin_clzll(value)); \ 43 | } 44 | #endif 45 | 46 | #ifdef SEAL_USE___INT128 47 | __extension__ typedef __int128 int128_t; 48 | __extension__ typedef unsigned __int128 uint128_t; 49 | #define SEAL_MULTIPLY_UINT64_HW64(operand1, operand2, hw64) \ 50 | do \ 51 | { \ 52 | *hw64 = static_cast( \ 53 | ((static_cast(operand1) * static_cast(operand2)) >> 64)); \ 54 | } while (false); 55 | 56 | #define SEAL_MULTIPLY_UINT64(operand1, operand2, result128) \ 57 | do \ 58 | { \ 59 | uint128_t product = static_cast(operand1) * operand2; \ 60 | result128[0] = static_cast(product); \ 61 | result128[1] = static_cast(product >> 64); \ 62 | } while (false) 63 | 64 | #define SEAL_DIVIDE_UINT128_UINT64(numerator, denominator, result) \ 65 | do \ 66 | { \ 67 | uint128_t n, q; \ 68 | n = (static_cast(numerator[1]) << 64) | (static_cast(numerator[0])); \ 69 | q = n / denominator; \ 70 | n -= q * denominator; \ 71 | numerator[0] = static_cast(n); \ 72 | numerator[1] = 0; \ 73 | result[0] = static_cast(q); \ 74 | result[1] = static_cast(q >> 64); \ 75 | } while (false) 76 | #endif 77 | 78 | // GCC intrinsics for _addcarry_u64 is disabled, for it compiles to slower code than add_uint64_generic. 79 | //#ifdef SEAL_USE__ADDCARRY_U64 80 | //#define SEAL_ADD_CARRY_UINT64(operand1, operand2, carry, result) _addcarry_u64(carry, operand1, operand2, result) 81 | //#endif 82 | 83 | #ifdef SEAL_USE__SUBBORROW_U64 84 | #if ((__GNUC__ == 7) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ >= 8) 85 | // The inverted arguments problem was fixed in GCC-7.2 86 | // (https://patchwork.ozlabs.org/patch/784309/) 87 | #define SEAL_SUB_BORROW_UINT64(operand1, operand2, borrow, result) _subborrow_u64(borrow, operand1, operand2, result) 88 | #else 89 | // Warning: Note the inverted order of operand1 and operand2 90 | #define SEAL_SUB_BORROW_UINT64(operand1, operand2, borrow, result) _subborrow_u64(borrow, operand2, operand1, result) 91 | #endif //(__GNUC__ == 7) && (__GNUC_MINOR__ >= 2) 92 | #endif 93 | 94 | #endif // SEAL_USE_INTRIN 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /native/tests/seal/util/galois.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/context.cuh" 5 | #include "seal/memorymanager.h" 6 | #include "seal/util/galois.h" 7 | #include 8 | #include 9 | #include "gtest/gtest.h" 10 | 11 | using namespace seal; 12 | using namespace seal::util; 13 | using namespace std; 14 | 15 | namespace sealtest 16 | { 17 | namespace util 18 | { 19 | TEST(GaloisToolTest, Create) 20 | { 21 | auto pool = MemoryManager::GetPool(); 22 | ASSERT_THROW(GaloisTool galois_tool(0, pool), invalid_argument); 23 | ASSERT_THROW(GaloisTool galois_tool(18, pool), invalid_argument); 24 | ASSERT_NO_THROW(GaloisTool galois_tool(1, pool)); 25 | ASSERT_NO_THROW(GaloisTool galois_tool(17, pool)); 26 | } 27 | 28 | TEST(GaloisToolTest, EltFromStep) 29 | { 30 | auto pool = MemoryManager::GetPool(); 31 | { 32 | GaloisTool galois_tool(3, pool); 33 | ASSERT_EQ(15U, galois_tool.get_elt_from_step(0)); 34 | ASSERT_EQ(3U, galois_tool.get_elt_from_step(1)); 35 | ASSERT_EQ(3U, galois_tool.get_elt_from_step(-3)); 36 | ASSERT_EQ(9U, galois_tool.get_elt_from_step(2)); 37 | ASSERT_EQ(9U, galois_tool.get_elt_from_step(-2)); 38 | ASSERT_EQ(11U, galois_tool.get_elt_from_step(3)); 39 | ASSERT_EQ(11U, galois_tool.get_elt_from_step(-1)); 40 | } 41 | } 42 | 43 | TEST(GaloisToolTest, EltsFromSteps) 44 | { 45 | auto pool = MemoryManager::GetPool(); 46 | { 47 | GaloisTool galois_tool(3, pool); 48 | auto elts = galois_tool.get_elts_from_steps({ 0, 1, -3, 2, -2, 3, -1 }); 49 | uint32_t elts_true[7]{ 15, 3, 3, 9, 9, 11, 11 }; 50 | for (size_t i = 0; i < elts.size(); i++) 51 | { 52 | ASSERT_EQ(elts_true[i], elts[i]); 53 | } 54 | } 55 | } 56 | 57 | TEST(GaloisToolTest, EltsAll) 58 | { 59 | auto pool = MemoryManager::GetPool(); 60 | { 61 | GaloisTool galois_tool(3, pool); 62 | auto elts = galois_tool.get_elts_all(); 63 | uint32_t elts_true[5]{ 15, 3, 11, 9, 9 }; 64 | for (size_t i = 0; i < elts.size(); i++) 65 | { 66 | ASSERT_EQ(elts_true[i], elts[i]); 67 | } 68 | } 69 | } 70 | 71 | TEST(GaloisToolTest, IndexFromElt) 72 | { 73 | ASSERT_EQ(7, GaloisTool::GetIndexFromElt(15)); 74 | ASSERT_EQ(1, GaloisTool::GetIndexFromElt(3)); 75 | ASSERT_EQ(4, GaloisTool::GetIndexFromElt(9)); 76 | ASSERT_EQ(5, GaloisTool::GetIndexFromElt(11)); 77 | } 78 | 79 | TEST(GaloisToolTest, ApplyGalois) 80 | { 81 | EncryptionParameters parms(scheme_type::ckks); 82 | parms.set_poly_modulus_degree(8); 83 | parms.set_coeff_modulus({ 17 }); 84 | SEALContext context(parms, false, sec_level_type::none); 85 | auto context_data = context.key_context_data(); 86 | auto galois_tool = context_data->galois_tool(); 87 | uint64_t in[8]{ 0, 1, 2, 3, 4, 5, 6, 7 }; 88 | uint64_t out[8]; 89 | uint64_t out_true[8]{ 0, 14, 6, 1, 13, 7, 2, 12 }; 90 | galois_tool->apply_galois(in, 3, Modulus(17), out); 91 | for (size_t i = 0; i < 8; i++) 92 | { 93 | ASSERT_EQ(out_true[i], out[i]); 94 | } 95 | } 96 | 97 | TEST(GaloisToolTest, ApplyGaloisNTT) 98 | { 99 | EncryptionParameters parms(scheme_type::ckks); 100 | parms.set_poly_modulus_degree(8); 101 | parms.set_coeff_modulus({ 17 }); 102 | SEALContext context(parms, false, sec_level_type::none); 103 | auto context_data = context.key_context_data(); 104 | auto galois_tool = context_data->galois_tool(); 105 | uint64_t in[8]{ 0, 1, 2, 3, 4, 5, 6, 7 }; 106 | uint64_t out[8]; 107 | uint64_t out_true[8]{ 4, 5, 7, 6, 1, 0, 2, 3 }; 108 | const_cast(galois_tool)->apply_galois_ntt(in, 3, out); 109 | for (size_t i = 0; i < 8; i++) 110 | { 111 | ASSERT_EQ(out_true[i], out[i]); 112 | } 113 | } 114 | } // namespace util 115 | } // namespace sealtest 116 | -------------------------------------------------------------------------------- /native/tests/seal/batchencoder.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/batchencoder.h" 5 | #include "seal/context.cuh" 6 | #include "seal/keygenerator.h" 7 | #include "seal/modulus.h" 8 | #include 9 | #include "gtest/gtest.h" 10 | 11 | using namespace seal; 12 | using namespace seal::util; 13 | using namespace std; 14 | 15 | namespace sealtest 16 | { 17 | TEST(BatchEncoderTest, BatchUnbatchUIntVector) 18 | { 19 | EncryptionParameters parms(scheme_type::bfv); 20 | parms.set_poly_modulus_degree(64); 21 | parms.set_coeff_modulus(CoeffModulus::Create(64, { 60 })); 22 | parms.set_plain_modulus(257); 23 | 24 | SEALContext context(parms, false, sec_level_type::none); 25 | ASSERT_TRUE(context.first_context_data()->qualifiers().using_batching); 26 | 27 | BatchEncoder batch_encoder(context); 28 | ASSERT_EQ(64ULL, batch_encoder.slot_count()); 29 | vector plain_vec; 30 | for (size_t i = 0; i < batch_encoder.slot_count(); i++) 31 | { 32 | plain_vec.push_back(i); 33 | } 34 | 35 | Plaintext plain; 36 | batch_encoder.encode(plain_vec, plain); 37 | vector plain_vec2; 38 | batch_encoder.decode(plain, plain_vec2); 39 | ASSERT_TRUE(plain_vec == plain_vec2); 40 | 41 | for (size_t i = 0; i < batch_encoder.slot_count(); i++) 42 | { 43 | plain_vec[i] = 5; 44 | } 45 | batch_encoder.encode(plain_vec, plain); 46 | ASSERT_TRUE(plain.to_string() == "5"); 47 | batch_encoder.decode(plain, plain_vec2); 48 | ASSERT_TRUE(plain_vec == plain_vec2); 49 | 50 | vector short_plain_vec; 51 | for (size_t i = 0; i < 20; i++) 52 | { 53 | short_plain_vec.push_back(i); 54 | } 55 | batch_encoder.encode(short_plain_vec, plain); 56 | vector short_plain_vec2; 57 | batch_encoder.decode(plain, short_plain_vec2); 58 | ASSERT_EQ(20ULL, short_plain_vec.size()); 59 | ASSERT_EQ(64ULL, short_plain_vec2.size()); 60 | for (size_t i = 0; i < 20; i++) 61 | { 62 | ASSERT_EQ(short_plain_vec[i], short_plain_vec2[i]); 63 | } 64 | for (size_t i = 20; i < batch_encoder.slot_count(); i++) 65 | { 66 | ASSERT_EQ(0ULL, short_plain_vec2[i]); 67 | } 68 | } 69 | 70 | TEST(BatchEncoderTest, BatchUnbatchIntVector) 71 | { 72 | EncryptionParameters parms(scheme_type::bfv); 73 | parms.set_poly_modulus_degree(64); 74 | parms.set_coeff_modulus(CoeffModulus::Create(64, { 60 })); 75 | parms.set_plain_modulus(257); 76 | 77 | SEALContext context(parms, false, sec_level_type::none); 78 | ASSERT_TRUE(context.first_context_data()->qualifiers().using_batching); 79 | 80 | BatchEncoder batch_encoder(context); 81 | ASSERT_EQ(64ULL, batch_encoder.slot_count()); 82 | vector plain_vec; 83 | for (uint64_t i = 0; i < static_cast(batch_encoder.slot_count()); i++) 84 | { 85 | plain_vec.push_back(static_cast(i * (1 - (i & 1) * 2))); 86 | } 87 | 88 | Plaintext plain; 89 | batch_encoder.encode(plain_vec, plain); 90 | vector plain_vec2; 91 | batch_encoder.decode(plain, plain_vec2); 92 | ASSERT_TRUE(plain_vec == plain_vec2); 93 | 94 | for (size_t i = 0; i < batch_encoder.slot_count(); i++) 95 | { 96 | plain_vec[i] = -5; 97 | } 98 | batch_encoder.encode(plain_vec, plain); 99 | ASSERT_TRUE(plain.to_string() == "FC"); 100 | batch_encoder.decode(plain, plain_vec2); 101 | ASSERT_TRUE(plain_vec == plain_vec2); 102 | 103 | vector short_plain_vec; 104 | for (int64_t i = 0; i < 20; i++) 105 | { 106 | short_plain_vec.push_back(i * (int64_t(1) - (i & 1) * 2)); 107 | } 108 | batch_encoder.encode(short_plain_vec, plain); 109 | vector short_plain_vec2; 110 | batch_encoder.decode(plain, short_plain_vec2); 111 | ASSERT_EQ(20ULL, short_plain_vec.size()); 112 | ASSERT_EQ(64ULL, short_plain_vec2.size()); 113 | for (size_t i = 0; i < 20; i++) 114 | { 115 | ASSERT_EQ(short_plain_vec[i], short_plain_vec2[i]); 116 | } 117 | for (size_t i = 20; i < batch_encoder.slot_count(); i++) 118 | { 119 | ASSERT_EQ(0ULL, short_plain_vec2[i]); 120 | } 121 | } 122 | } // namespace sealtest 123 | -------------------------------------------------------------------------------- /native/src/seal/util/blake2-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | BLAKE2 reference source code package - reference C implementations 3 | 4 | Copyright 2012, Samuel Neves . You may use this under the 5 | terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at 6 | your option. The terms of these licenses can be found at: 7 | 8 | - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 9 | - OpenSSL license : https://www.openssl.org/source/license.html 10 | - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | More information about the BLAKE2 hash function can be found at 13 | https://blake2.net. 14 | */ 15 | #ifndef BLAKE2_IMPL_H 16 | #define BLAKE2_IMPL_H 17 | 18 | #include 19 | #include 20 | 21 | #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) 22 | #if defined(_MSC_VER) 23 | #define BLAKE2_INLINE __inline 24 | #elif defined(__GNUC__) 25 | #define BLAKE2_INLINE __inline__ 26 | #else 27 | #define BLAKE2_INLINE 28 | #endif 29 | #else 30 | #define BLAKE2_INLINE inline 31 | #endif 32 | 33 | static BLAKE2_INLINE uint32_t load32(const void *src) 34 | { 35 | #if defined(NATIVE_LITTLE_ENDIAN) 36 | uint32_t w; 37 | memcpy(&w, src, sizeof w); 38 | return w; 39 | #else 40 | const uint8_t *p = (const uint8_t *)src; 41 | return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); 42 | #endif 43 | } 44 | 45 | static BLAKE2_INLINE uint64_t load64(const void *src) 46 | { 47 | #if defined(NATIVE_LITTLE_ENDIAN) 48 | uint64_t w; 49 | memcpy(&w, src, sizeof w); 50 | return w; 51 | #else 52 | const uint8_t *p = (const uint8_t *)src; 53 | return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) | 54 | ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) | ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56); 55 | #endif 56 | } 57 | 58 | static BLAKE2_INLINE uint16_t load16(const void *src) 59 | { 60 | #if defined(NATIVE_LITTLE_ENDIAN) 61 | uint16_t w; 62 | memcpy(&w, src, sizeof w); 63 | return w; 64 | #else 65 | const uint8_t *p = (const uint8_t *)src; 66 | return (uint16_t)(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8)); 67 | #endif 68 | } 69 | 70 | static BLAKE2_INLINE void store16(void *dst, uint16_t w) 71 | { 72 | #if defined(NATIVE_LITTLE_ENDIAN) 73 | memcpy(dst, &w, sizeof w); 74 | #else 75 | uint8_t *p = (uint8_t *)dst; 76 | *p++ = (uint8_t)w; 77 | w >>= 8; 78 | *p++ = (uint8_t)w; 79 | #endif 80 | } 81 | 82 | static BLAKE2_INLINE void store32(void *dst, uint32_t w) 83 | { 84 | #if defined(NATIVE_LITTLE_ENDIAN) 85 | memcpy(dst, &w, sizeof w); 86 | #else 87 | uint8_t *p = (uint8_t *)dst; 88 | p[0] = (uint8_t)(w >> 0); 89 | p[1] = (uint8_t)(w >> 8); 90 | p[2] = (uint8_t)(w >> 16); 91 | p[3] = (uint8_t)(w >> 24); 92 | #endif 93 | } 94 | 95 | static BLAKE2_INLINE void store64(void *dst, uint64_t w) 96 | { 97 | #if defined(NATIVE_LITTLE_ENDIAN) 98 | memcpy(dst, &w, sizeof w); 99 | #else 100 | uint8_t *p = (uint8_t *)dst; 101 | p[0] = (uint8_t)(w >> 0); 102 | p[1] = (uint8_t)(w >> 8); 103 | p[2] = (uint8_t)(w >> 16); 104 | p[3] = (uint8_t)(w >> 24); 105 | p[4] = (uint8_t)(w >> 32); 106 | p[5] = (uint8_t)(w >> 40); 107 | p[6] = (uint8_t)(w >> 48); 108 | p[7] = (uint8_t)(w >> 56); 109 | #endif 110 | } 111 | 112 | static BLAKE2_INLINE uint64_t load48(const void *src) 113 | { 114 | const uint8_t *p = (const uint8_t *)src; 115 | return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) | 116 | ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40); 117 | } 118 | 119 | static BLAKE2_INLINE void store48(void *dst, uint64_t w) 120 | { 121 | uint8_t *p = (uint8_t *)dst; 122 | p[0] = (uint8_t)(w >> 0); 123 | p[1] = (uint8_t)(w >> 8); 124 | p[2] = (uint8_t)(w >> 16); 125 | p[3] = (uint8_t)(w >> 24); 126 | p[4] = (uint8_t)(w >> 32); 127 | p[5] = (uint8_t)(w >> 40); 128 | } 129 | 130 | static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c) 131 | { 132 | return (w >> c) | (w << (32 - c)); 133 | } 134 | 135 | static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c) 136 | { 137 | return (w >> c) | (w << (64 - c)); 138 | } 139 | 140 | /* prevents compiler optimizing out memset() */ 141 | static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) 142 | { 143 | static void *(*const volatile memset_v)(void *, int, size_t) = &memset; 144 | memset_v(v, 0, n); 145 | } 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /cmake/SEALMacros.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) IDEA Corporation. All rights reserved. 2 | # Licensed under the MIT license. 3 | 4 | # Set the C++ language version 5 | macro(seal_set_language target) 6 | if(SEAL_USE_CXX17) 7 | target_compile_features(${target} PUBLIC cxx_std_17) 8 | else() 9 | target_compile_features(${target} PUBLIC cxx_std_14) 10 | endif() 11 | endmacro() 12 | 13 | # Include a file to fetch thirdparty content 14 | macro(seal_fetch_thirdparty_content content_file) 15 | set(SEAL_FETCHCONTENT_BASE_DIR_OLD ${FETCHCONTENT_BASE_DIR}) 16 | set(FETCHCONTENT_BASE_DIR ${SEAL_THIRDPARTY_DIR} CACHE STRING "" FORCE) 17 | include(${content_file}) 18 | set(FETCHCONTENT_BASE_DIR ${SEAL_FETCHCONTENT_BASE_DIR_OLD} CACHE STRING "" FORCE) 19 | unset(SEAL_FETCHCONTENT_BASE_DIR_OLD) 20 | endmacro() 21 | 22 | # Set the VERSION property 23 | macro(seal_set_version target) 24 | set_target_properties(${target} PROPERTIES VERSION ${SEAL_VERSION}) 25 | endmacro() 26 | 27 | # Set the library filename to reflect version 28 | macro(seal_set_version_filename target) 29 | set_target_properties(${target} PROPERTIES 30 | OUTPUT_NAME ${target}-${SEAL_VERSION_MAJOR}.${SEAL_VERSION_MINOR}) 31 | endmacro() 32 | 33 | # Set the SOVERSION property 34 | macro(seal_set_soversion target) 35 | set_target_properties(${target} PROPERTIES 36 | SOVERSION ${SEAL_VERSION_MAJOR}.${SEAL_VERSION_MINOR}) 37 | endmacro() 38 | 39 | # Set include directories for build and install interfaces 40 | macro(seal_set_include_directories target) 41 | target_include_directories(${target} PUBLIC 42 | $ 43 | $) 44 | target_include_directories(${target} PUBLIC 45 | $ 46 | $) 47 | endmacro() 48 | 49 | # Link a thread library 50 | macro(seal_link_threads target) 51 | # Require thread library 52 | if(NOT TARGET Threads::Threads) 53 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 54 | set(THREADS_PREFER_PTHREAD_FLAG TRUE) 55 | find_package(Threads REQUIRED) 56 | endif() 57 | 58 | # Link Threads 59 | target_link_libraries(${target} PUBLIC Threads::Threads) 60 | endmacro() 61 | 62 | # Include target to given export 63 | macro(seal_install_target target export) 64 | install(TARGETS ${target} EXPORT ${export} 65 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 66 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 67 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 68 | endmacro() 69 | 70 | # Manually combine archives, using ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} to keep temporary files. 71 | macro(seal_combine_archives target dependency) 72 | if(MSVC) 73 | add_custom_command(TARGET ${target} POST_BUILD 74 | COMMAND lib.exe /OUT:$ $ $ 75 | DEPENDS $ $ 76 | WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) 77 | else() 78 | if(CMAKE_HOST_WIN32) 79 | get_filename_component(CXX_DIR "${CMAKE_CXX_COMPILER}" DIRECTORY) 80 | set(AR_CMD_PATH "${CXX_DIR}/llvm-ar.exe") 81 | file(TO_NATIVE_PATH "${AR_CMD_PATH}" AR_CMD_PATH) 82 | set(DEL_CMD "del") 83 | set(DEL_CMD_OPTS "") 84 | else() 85 | set(AR_CMD_PATH "ar") 86 | set(DEL_CMD "rm") 87 | set(DEL_CMD_OPTS "-rf") 88 | endif() 89 | if(EMSCRIPTEN) 90 | set(AR_CMD_PATH "emar") 91 | endif() 92 | add_custom_command(TARGET ${target} POST_BUILD 93 | COMMAND "${AR_CMD_PATH}" x $ 94 | COMMAND "${AR_CMD_PATH}" x $ 95 | COMMAND "${AR_CMD_PATH}" rcs $ *.o 96 | COMMAND ${DEL_CMD} ${DEL_CMD_OPTS} *.o 97 | WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) 98 | endif() 99 | endmacro() 100 | 101 | # Add secure compile options 102 | macro(seal_set_secure_compile_options target scope) 103 | if(MSVC) 104 | # Build debug symbols for static analysis tools 105 | target_link_options(${target} ${scope} /DEBUG) 106 | 107 | # Control Flow Guard / Spectre 108 | target_compile_options(${target} ${scope} /guard:cf) 109 | target_compile_options(${target} ${scope} /Qspectre) 110 | target_link_options(${target} ${scope} /guard:cf) 111 | target_link_options(${target} ${scope} /DYNAMICBASE) 112 | endif() 113 | endmacro() 114 | -------------------------------------------------------------------------------- /native/examples/build/CMakeFiles/sealexamples.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.18 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Disable VCS-based implicit rules. 16 | % : %,v 17 | 18 | 19 | # Disable VCS-based implicit rules. 20 | % : RCS/% 21 | 22 | 23 | # Disable VCS-based implicit rules. 24 | % : RCS/%,v 25 | 26 | 27 | # Disable VCS-based implicit rules. 28 | % : SCCS/s.% 29 | 30 | 31 | # Disable VCS-based implicit rules. 32 | % : s.% 33 | 34 | 35 | .SUFFIXES: .hpux_make_needs_suffix_list 36 | 37 | 38 | # Command-line flag to silence nested $(MAKE). 39 | $(VERBOSE)MAKESILENT = -s 40 | 41 | #Suppress display of executed commands. 42 | $(VERBOSE).SILENT: 43 | 44 | # A target that is always out of date. 45 | cmake_force: 46 | 47 | .PHONY : cmake_force 48 | 49 | #============================================================================= 50 | # Set environment variables for the build. 51 | 52 | # The shell in which to execute make rules. 53 | SHELL = /bin/sh 54 | 55 | # The CMake executable. 56 | CMAKE_COMMAND = /home/cmake-3.18.3-Linux-x86_64/bin/cmake 57 | 58 | # The command to remove a file. 59 | RM = /home/cmake-3.18.3-Linux-x86_64/bin/cmake -E rm -f 60 | 61 | # Escaping for special characters. 62 | EQUALS = = 63 | 64 | # The top-level source directory on which CMake was run. 65 | CMAKE_SOURCE_DIR = /home/liqirui/SEAL/native/examples 66 | 67 | # The top-level build directory on which CMake was run. 68 | CMAKE_BINARY_DIR = /home/liqirui/SEAL/native/examples/build 69 | 70 | # Include any dependencies generated for this target. 71 | include CMakeFiles/sealexamples.dir/depend.make 72 | 73 | # Include the progress variables for this target. 74 | include CMakeFiles/sealexamples.dir/progress.make 75 | 76 | # Include the compile flags for this target's objects. 77 | include CMakeFiles/sealexamples.dir/flags.make 78 | 79 | CMakeFiles/sealexamples.dir/2_encoders.cpp.o: CMakeFiles/sealexamples.dir/flags.make 80 | CMakeFiles/sealexamples.dir/2_encoders.cpp.o: ../2_encoders.cpp 81 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/liqirui/SEAL/native/examples/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building CXX object CMakeFiles/sealexamples.dir/2_encoders.cpp.o" 82 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -o CMakeFiles/sealexamples.dir/2_encoders.cpp.o -c /home/liqirui/SEAL/native/examples/2_encoders.cpp 83 | 84 | CMakeFiles/sealexamples.dir/2_encoders.cpp.i: cmake_force 85 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing CXX source to CMakeFiles/sealexamples.dir/2_encoders.cpp.i" 86 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -E /home/liqirui/SEAL/native/examples/2_encoders.cpp > CMakeFiles/sealexamples.dir/2_encoders.cpp.i 87 | 88 | CMakeFiles/sealexamples.dir/2_encoders.cpp.s: cmake_force 89 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling CXX source to assembly CMakeFiles/sealexamples.dir/2_encoders.cpp.s" 90 | /usr/bin/c++ $(CXX_DEFINES) $(CXX_INCLUDES) $(CXX_FLAGS) -S /home/liqirui/SEAL/native/examples/2_encoders.cpp -o CMakeFiles/sealexamples.dir/2_encoders.cpp.s 91 | 92 | # Object files for target sealexamples 93 | sealexamples_OBJECTS = \ 94 | "CMakeFiles/sealexamples.dir/2_encoders.cpp.o" 95 | 96 | # External object files for target sealexamples 97 | sealexamples_EXTERNAL_OBJECTS = 98 | 99 | bin/sealexamples: CMakeFiles/sealexamples.dir/2_encoders.cpp.o 100 | bin/sealexamples: CMakeFiles/sealexamples.dir/build.make 101 | bin/sealexamples: /usr/local/lib/libseal-4.1.a 102 | bin/sealexamples: CMakeFiles/sealexamples.dir/link.txt 103 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/liqirui/SEAL/native/examples/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking CXX executable bin/sealexamples" 104 | $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/sealexamples.dir/link.txt --verbose=$(VERBOSE) 105 | 106 | # Rule to build all files generated by this target. 107 | CMakeFiles/sealexamples.dir/build: bin/sealexamples 108 | 109 | .PHONY : CMakeFiles/sealexamples.dir/build 110 | 111 | CMakeFiles/sealexamples.dir/clean: 112 | $(CMAKE_COMMAND) -P CMakeFiles/sealexamples.dir/cmake_clean.cmake 113 | .PHONY : CMakeFiles/sealexamples.dir/clean 114 | 115 | CMakeFiles/sealexamples.dir/depend: 116 | cd /home/liqirui/SEAL/native/examples/build && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/liqirui/SEAL/native/examples /home/liqirui/SEAL/native/examples /home/liqirui/SEAL/native/examples/build /home/liqirui/SEAL/native/examples/build /home/liqirui/SEAL/native/examples/build/CMakeFiles/sealexamples.dir/DependInfo.cmake --color=$(COLOR) 117 | .PHONY : CMakeFiles/sealexamples.dir/depend 118 | 119 | -------------------------------------------------------------------------------- /native/src/seal/util/hestdparms.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #pragma once 5 | 6 | #include "seal/util/defines.h" 7 | #include 8 | 9 | namespace seal 10 | { 11 | namespace util 12 | { 13 | /** 14 | Largest allowed bit counts for coeff_modulus based on the security estimates from 15 | HomomorphicEncryption.org security standard. IDEA SEAL_GPU samples the secret key 16 | from a ternary {-1, 0, 1} distribution. 17 | */ 18 | // Ternary secret; 128 bits classical security 19 | SEAL_NODISCARD constexpr int seal_he_std_parms_128_tc(std::size_t poly_modulus_degree) noexcept 20 | { 21 | switch (poly_modulus_degree) 22 | { 23 | case std::size_t(1024): 24 | return 27; 25 | case std::size_t(2048): 26 | return 54; 27 | case std::size_t(4096): 28 | return 109; 29 | case std::size_t(8192): 30 | return 218; 31 | case std::size_t(16384): 32 | return 438; 33 | case std::size_t(32768): 34 | return 881; 35 | } 36 | return 0; 37 | } 38 | 39 | // Ternary secret; 192 bits classical security 40 | SEAL_NODISCARD constexpr int seal_he_std_parms_192_tc(std::size_t poly_modulus_degree) noexcept 41 | { 42 | switch (poly_modulus_degree) 43 | { 44 | case std::size_t(1024): 45 | return 19; 46 | case std::size_t(2048): 47 | return 37; 48 | case std::size_t(4096): 49 | return 75; 50 | case std::size_t(8192): 51 | return 152; 52 | case std::size_t(16384): 53 | return 305; 54 | case std::size_t(32768): 55 | return 611; 56 | } 57 | return 0; 58 | } 59 | 60 | // Ternary secret; 256 bits classical security 61 | SEAL_NODISCARD constexpr int seal_he_std_parms_256_tc(std::size_t poly_modulus_degree) noexcept 62 | { 63 | switch (poly_modulus_degree) 64 | { 65 | case std::size_t(1024): 66 | return 14; 67 | case std::size_t(2048): 68 | return 29; 69 | case std::size_t(4096): 70 | return 58; 71 | case std::size_t(8192): 72 | return 118; 73 | case std::size_t(16384): 74 | return 237; 75 | case std::size_t(32768): 76 | return 476; 77 | } 78 | return 0; 79 | } 80 | 81 | // Ternary secret; 128 bits quantum security 82 | SEAL_NODISCARD constexpr int seal_he_std_parms_128_tq(std::size_t poly_modulus_degree) noexcept 83 | { 84 | switch (poly_modulus_degree) 85 | { 86 | case std::size_t(1024): 87 | return 25; 88 | case std::size_t(2048): 89 | return 51; 90 | case std::size_t(4096): 91 | return 101; 92 | case std::size_t(8192): 93 | return 202; 94 | case std::size_t(16384): 95 | return 411; 96 | case std::size_t(32768): 97 | return 827; 98 | } 99 | return 0; 100 | } 101 | 102 | // Ternary secret; 192 bits quantum security 103 | SEAL_NODISCARD constexpr int seal_he_std_parms_192_tq(std::size_t poly_modulus_degree) noexcept 104 | { 105 | switch (poly_modulus_degree) 106 | { 107 | case std::size_t(1024): 108 | return 17; 109 | case std::size_t(2048): 110 | return 35; 111 | case std::size_t(4096): 112 | return 70; 113 | case std::size_t(8192): 114 | return 141; 115 | case std::size_t(16384): 116 | return 284; 117 | case std::size_t(32768): 118 | return 571; 119 | } 120 | return 0; 121 | } 122 | 123 | // Ternary secret; 256 bits quantum security 124 | SEAL_NODISCARD constexpr int seal_he_std_parms_256_tq(std::size_t poly_modulus_degree) noexcept 125 | { 126 | switch (poly_modulus_degree) 127 | { 128 | case std::size_t(1024): 129 | return 13; 130 | case std::size_t(2048): 131 | return 27; 132 | case std::size_t(4096): 133 | return 54; 134 | case std::size_t(8192): 135 | return 109; 136 | case std::size_t(16384): 137 | return 220; 138 | case std::size_t(32768): 139 | return 443; 140 | } 141 | return 0; 142 | } 143 | 144 | // Standard deviation for error distribution 145 | constexpr double seal_he_std_parms_error_std_dev = 3.2; 146 | } // namespace util 147 | } // namespace seal 148 | -------------------------------------------------------------------------------- /native/src/seal/kswitchkeys.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) IDEA Corporation. All rights reserved. 2 | // Licensed under the MIT license. 3 | 4 | #include "seal/kswitchkeys.h" 5 | #include 6 | 7 | using namespace std; 8 | using namespace seal::util; 9 | 10 | namespace seal 11 | { 12 | KSwitchKeys &KSwitchKeys::operator=(const KSwitchKeys &assign) 13 | { 14 | // Check for self-assignment 15 | if (this == &assign) 16 | { 17 | return *this; 18 | } 19 | 20 | // Copy over fields 21 | parms_id_ = assign.parms_id_; 22 | 23 | // Then copy over keys 24 | keys_.clear(); 25 | size_t keys_dim1 = assign.keys_.size(); 26 | keys_.reserve(keys_dim1); 27 | for (size_t i = 0; i < keys_dim1; i++) 28 | { 29 | size_t keys_dim2 = assign.keys_[i].size(); 30 | keys_.emplace_back(); 31 | keys_[i].reserve(keys_dim2); 32 | for (size_t j = 0; j < keys_dim2; j++) 33 | { 34 | keys_[i].emplace_back(PublicKey(pool_)); 35 | keys_[i][j] = assign.keys_[i][j]; 36 | } 37 | } 38 | 39 | return *this; 40 | } 41 | 42 | void KSwitchKeys::save_members(ostream &stream) const 43 | { 44 | auto old_except_mask = stream.exceptions(); 45 | try 46 | { 47 | // Throw exceptions on ios_base::badbit and ios_base::failbit 48 | stream.exceptions(ios_base::badbit | ios_base::failbit); 49 | 50 | uint64_t keys_dim1 = static_cast(keys_.size()); 51 | 52 | // Save the parms_id 53 | stream.write(reinterpret_cast(&parms_id_), sizeof(parms_id_type)); 54 | 55 | // Save the size of keys_ 56 | stream.write(reinterpret_cast(&keys_dim1), sizeof(uint64_t)); 57 | 58 | // Now loop again over keys_dim1 59 | for (size_t index = 0; index < keys_dim1; index++) 60 | { 61 | // Save second dimension of keys_ 62 | uint64_t keys_dim2 = static_cast(keys_[index].size()); 63 | stream.write(reinterpret_cast(&keys_dim2), sizeof(uint64_t)); 64 | 65 | // Loop over keys_dim2 and save all (or none) 66 | for (size_t j = 0; j < keys_dim2; j++) 67 | { 68 | // Save the key 69 | keys_[index][j].save(stream, compr_mode_type::none); 70 | } 71 | } 72 | } 73 | catch (const ios_base::failure &) 74 | { 75 | stream.exceptions(old_except_mask); 76 | throw runtime_error("I/O error"); 77 | } 78 | catch (...) 79 | { 80 | stream.exceptions(old_except_mask); 81 | throw; 82 | } 83 | stream.exceptions(old_except_mask); 84 | } 85 | 86 | void KSwitchKeys::load_members(const SEALContext &context, istream &stream, SEAL_MAYBE_UNUSED SEALVersion version) 87 | { 88 | // Verify parameters 89 | if (!context.parameters_set()) 90 | { 91 | throw invalid_argument("encryption parameters are not set correctly"); 92 | } 93 | 94 | // Create new keys 95 | vector> new_keys; 96 | 97 | auto old_except_mask = stream.exceptions(); 98 | try 99 | { 100 | // Throw exceptions on ios_base::badbit and ios_base::failbit 101 | stream.exceptions(ios_base::badbit | ios_base::failbit); 102 | 103 | // Read the parms_id 104 | stream.read(reinterpret_cast(&parms_id_), sizeof(parms_id_type)); 105 | 106 | // Read in the size of keys_ 107 | uint64_t keys_dim1 = 0; 108 | stream.read(reinterpret_cast(&keys_dim1), sizeof(uint64_t)); 109 | 110 | // Reserve first for dimension of keys_ 111 | new_keys.reserve(safe_cast(keys_dim1)); 112 | 113 | // Loop over the first dimension of keys_ 114 | for (size_t index = 0; index < keys_dim1; index++) 115 | { 116 | // Read the size of the second dimension 117 | uint64_t keys_dim2 = 0; 118 | stream.read(reinterpret_cast(&keys_dim2), sizeof(uint64_t)); 119 | 120 | // Don't resize; only reserve 121 | new_keys.emplace_back(); 122 | new_keys.back().reserve(safe_cast(keys_dim2)); 123 | for (size_t j = 0; j < keys_dim2; j++) 124 | { 125 | PublicKey key(pool_); 126 | key.unsafe_load(context, stream); 127 | new_keys[index].emplace_back(move(key)); 128 | } 129 | } 130 | } 131 | catch (const ios_base::failure &) 132 | { 133 | stream.exceptions(old_except_mask); 134 | throw runtime_error("I/O error"); 135 | } 136 | catch (...) 137 | { 138 | stream.exceptions(old_except_mask); 139 | throw; 140 | } 141 | stream.exceptions(old_except_mask); 142 | 143 | swap(keys_, new_keys); 144 | } 145 | } // namespace seal 146 | -------------------------------------------------------------------------------- /native/src/seal/util/helper.cuh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "seal/context.cuh" 3 | #include "seal/gpu_memorypool.h" 4 | #include "common.h" 5 | #include "seal/util/rns.cuh" 6 | #include "seal/util/uintarith.cuh" 7 | #include "seal/util/uintarithsmallmod.cuh" 8 | #include 9 | #include "kuint128.cuh" 10 | #include 11 | #include 12 | #include 13 | // 计算 a^b mod mod 14 | namespace seal{ 15 | // 计算 a^b mod mod 16 | __device__ __host__ inline uint64_t modpow128(uint64_t a, uint64_t b, uint64_t mod) 17 | { 18 | uint64_t res = 1; 19 | 20 | if (1 & b) 21 | res = a; 22 | 23 | while (b != 0) 24 | { 25 | b = b >> 1; 26 | k_uint128_t t128 = host64x2(a, a); 27 | a = (t128 % mod).low; 28 | if (b & 1) 29 | { 30 | k_uint128_t r128 = host64x2(res, a); 31 | res = (r128 % mod).low; 32 | } 33 | } 34 | return res; 35 | } 36 | 37 | __device__ __host__ inline unsigned modpow64(unsigned a, unsigned b, unsigned mod) 38 | { 39 | unsigned res = 1; 40 | 41 | if (1 & b) 42 | res = a; 43 | 44 | while (b != 0) 45 | { 46 | b = b >> 1; 47 | uint64_t t64 = (uint64_t)a * a; 48 | a = t64 % mod; 49 | if (b & 1) 50 | { 51 | uint64_t r64 = (uint64_t)a * res; 52 | res = r64 % mod; 53 | } 54 | } 55 | return res; 56 | } 57 | 58 | __device__ inline uint64_t modinv128(uint64_t a, uint64_t q) 59 | { 60 | uint64_t ainv = modpow128(a, q - 2, q); 61 | return ainv; 62 | } 63 | 64 | __device__ inline uint64_t bitReverse(uint64_t a, int bit_length) 65 | { 66 | uint64_t res = 0; 67 | 68 | for (int i = 0; i < bit_length; i++) 69 | { 70 | res <<= 1; 71 | res = (a & 1) | res; 72 | a >>= 1; 73 | } 74 | 75 | return res; 76 | } 77 | 78 | 79 | template 80 | inline void allocate_gpu(T **ptr, size_t size){ 81 | PoolManager& poolManager = PoolManager::getInstance(); 82 | GPUMemoryPool* memoryPool = poolManager.getMemoryPool(); 83 | *ptr = static_cast(memoryPool->allocate(size * sizeof(T))); 84 | // memoryPool->printPoolStatus(); 85 | } 86 | 87 | template 88 | inline void deallocate_gpu(T **ptr, size_t size){ 89 | PoolManager& poolManager = PoolManager::getInstance(); 90 | GPUMemoryPool* memoryPool = poolManager.getMemoryPool(); 91 | memoryPool->deallocate(*ptr, size*sizeof(T)); 92 | // memoryPool->printPoolStatus(); 93 | 94 | } 95 | 96 | 97 | template 98 | void allocate_gpu(std::shared_ptr& ptr, size_t size) { 99 | PoolManager& poolManager = PoolManager::getInstance(); 100 | GPUMemoryPool* memoryPool = poolManager.getMemoryPool(); 101 | uint64_t* new_d_data = nullptr; 102 | new_d_data = static_cast(memoryPool->allocate(size * sizeof(T))); // 在 GPU 上分配内存 103 | 104 | // 使用 std::shared_ptr 包装 new_d_data,确保内存管理 105 | ptr = std::shared_ptr(new_d_data, [memoryPool, size](uint64_t* p) { 106 | // 自定义删除器,用于在 shared_ptr 引用计数为 0 时释放内存 107 | memoryPool->deallocate(p, size * sizeof(T)); 108 | }); 109 | } 110 | 111 | 112 | template 113 | inline int getOptimalBlockSize(KernelFunc kernel) { 114 | int minGridSize, optimalBlockSize; 115 | 116 | // 使用cudaOccupancyMaxPotentialBlockSize计算达到最大占用率的最小网格大小和最佳线程块大小 117 | cudaOccupancyMaxPotentialBlockSize(&minGridSize, 118 | &optimalBlockSize, 119 | kernel, 120 | 0, 121 | 0); 122 | 123 | return optimalBlockSize; 124 | } 125 | 126 | inline int getClosestBlockSize(int optimalBlockSize) { 127 | std::vector blockSizes = {128, 256, 512, 1024}; 128 | 129 | // 使用lambda和标准库中的min_element函数找到最近的块大小 130 | return *std::min_element(blockSizes.begin(), blockSizes.end(), 131 | [optimalBlockSize](int a, int b) { 132 | return abs(a - optimalBlockSize) < abs(b - optimalBlockSize); 133 | }); 134 | } 135 | 136 | inline void print_value(uint64_t *value, int count); 137 | 138 | 139 | __global__ void fillTablePsi128(uint64_t psiinv, uint64_t q, uint64_t psiinvTable[], uint64_t nbit); 140 | __global__ void fillTablePsi128_root(uint64_t psi, uint64_t q, uint64_t *psiTable, int nx, int ny); 141 | __global__ void fillTablePsi128_root_n2(uint64_t psi, uint64_t q, uint64_t *psiTable, int nx, int ny); 142 | __global__ void fillTablePsi128_root_n12(uint64_t psi, uint64_t q, uint64_t *psiTable, int nx, int ny); 143 | 144 | __global__ void print_helper(uint64_t *input, size_t size); 145 | __global__ void set_poly_kernel(uint64_t *ori, uint64_t *dest, uint64_t coeff_count, uint64_t coeff_modulus_size); 146 | __global__ void set_uint_kernel(uint64_t *value, size_t uint64_count, uint64_t *result); 147 | __global__ void set_zero_poly_kernel(size_t coeff_count, size_t coeff_modulus_size, uint64_t *destination); 148 | 149 | } --------------------------------------------------------------------------------