### Docker Installation for LattiSense Source: https://context7.com/cipherflow-fhe/lattisense/llms.txt Installs and runs the official LattiSense Docker image, providing a pre-built SDK and toolchain for immediate development. This is the recommended method for quick setup. ```bash # Pull and run the official Docker image docker run -it ghcr.io/cipherflow-fhe/lattisense:latest # The container includes: # - Pre-built SDK and libraries # - Python compilation toolchain # - Project template ready for development ``` -------------------------------- ### Build and Run LattiSense Examples Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Steps to build the internal examples of LattiSense after enabling them during the CMake configuration. It includes generating computation graphs and running the compiled examples. ```bash mkdir build && cd build cmake .. -DLATTISENSE_BUILD_EXAMPLES=ON make -j$(nproc) # Generate computation graph cd build/examples/bfv_mult_cpu python3 bfv_mult_cpu.py # Run example ./bfv_mult_cpu ``` -------------------------------- ### CKKS Parameter Setup and Data Encoding in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md This C++ code snippet demonstrates the setup of CKKS parameters, context creation, and encoding of input data (x, w, b, mask) for homomorphic encryption. It configures parameters like polynomial modulus degree (N) and levels for secure computation. ```c++ int level = 3; int n_input_feature = 30; uint64_t N = 8192; CkksParameter param = CkksParameter::create_parameter(N); CkksContext ctx = CkksContext::create_random_context(param); double default_scale = param.get_default_scale(); vector x_mg{ 0.04207487339675331, -0.954683801149814, 0.09197705756340246, -0.27253446447507956, 0.18750564232192835, 0.5840745966505123, 0.4062792877225865, 0.4622266401590458, 0.3727272727272728, 0.21103622577927572, -0.2877059569074779, -0.7590611739745403, -0.2619328087452292, -0.45237748366635655, -0.6814087092497536, -0.29720311232613317, -0.7286363636363636, -0.3987497632127297, -0.3767096302133168, -0.6339151223691666, 0.24155104944859485, -0.716950959488273, 0.336620349619005, -0.09860401101061744, 0.20227167668229562, 0.23858311261169463, 0.13722044728434502, 0.8240549828178696, 0.19692489651094025, -0.16227207136298039}; vector w_mg{ -0.38779230675573784, -0.08020498791940865, -0.42494960644275187, -0.3011337927885834, 0.19736016953065058, -0.3452779920215878, -0.678324870145478, -0.8177783668067259, 0.15226510934692553, 0.5859673866284915, 0.01255264233893136, 0.4752989745604508, 0.05023635251466458, 0.11310208234475544, 0.5530291648269257, 0.12287678195417821, 0.3339257590342935, 0.07939103265266986, 0.5650923127926508, 0.44168413736941736, -0.5564150081657178, -0.2552746866713479, -0.544768402633023, -0.3273054244777431, -0.05454841442127498, -0.3247696994741705, -0.498143298043605, -1.092540674562078, 0.08402652360008195, 0.16040344319412192}; vector b_mg{0.430568328365614}; vector mask{1.0}; auto x_pt = ctx.encode(x_mg, level, default_scale); auto x_ct = ctx.encrypt_asymmetric(x_pt); auto w_pt = ctx.encode_ringt(w_mg, default_scale); auto b_pt = ctx.encode(b_mg, level - 1, default_scale * default_scale / param.get_q(level)); auto mask_pt = ctx.encode_ringt(mask, default_scale); auto y_ct = ctx.new_ciphertext( level - 2, default_scale * default_scale * default_scale / param.get_q(level) / param.get_q(level - 1)); ``` -------------------------------- ### Install LattiSense using Docker Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md This command pulls the latest LattiSense Docker image and runs it in interactive mode. It's the quickest way to get started without manual dependency installation. ```bash docker run -it ghcr.io/cipherflow-fhe/lattisense:latest ``` -------------------------------- ### Configure LattiSense Build with Options Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Demonstrates how to configure the LattiSense build using CMake with specific options like installation prefix and enabling example programs or GPU acceleration. ```bash cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd)/../install -DLATTISENSE_BUILD_EXAMPLES=ON cmake .. -DLATTISENSE_ENABLE_GPU=ON -DLATTISENSE_CUDA_ARCH=89 ``` -------------------------------- ### Install LattiSense SDK Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Installs the built LattiSense SDK. It can be installed to a custom directory using `CMAKE_INSTALL_PREFIX` or to the system default location. ```bash # Install to custom directory cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd)/../install make install # Or install to system (requires sudo) cmake .. make sudo make install ``` -------------------------------- ### FheTaskCpu Example Usage (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Demonstrates how to create and run an FheTaskCpu object. This example shows task instantiation, argument preparation, and execution of the FHE computation on the CPU. ```C++ // Create CPU computation task FheTaskCpu cpu_task("./cpu_project"); // Prepare input/output parameters vector cxx_args = { {"input_x", &x_ciphertext}, {"input_y", &y_ciphertext}, {"output_z", &z_ciphertext}, }; // Execute CPU computation uint64_t cpu_time = cpu_task.run(&context, cxx_args); ``` -------------------------------- ### Example: Compiling a BFV Custom Task (Python) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md An example demonstrating how to compile a custom BFV task. It shows the setup of FHE parameters, definition of ciphertext nodes and operations (like multiplication), and the subsequent call to `process_custom_task` to generate the necessary files. ```python from custom_task import * # Create and set global parameters param = Param.create_bfv_custom_param( n=8192, p=[0x7ffffffffb4001], q=[0x3fffffffef8001, 0x4000000011c001, 0x40000000120001], t=0x28001 ) set_fhe_param(param) # Define computation graph level = 3 x = BfvCiphertextNode('x', level) y = BfvCiphertextNode('y', level) z = mult_relin(x, y, 'z') # Compile task process_custom_task( input_args=[Argument('x', x), Argument('y', y)], output_args=[Argument('z', z)], output_instruction_path='examples/bfv_mult', fpga_acc=False, ) ``` -------------------------------- ### FheTaskGpu Example Usage (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Illustrates the usage of the FheTaskGpu class. This example demonstrates creating a GPU task, setting up input/output arguments, and executing the FHE computation on the GPU. ```C++ // Create GPU computation task FheTaskGpu gpu_task("./gpu_project"); // Prepare input/output parameters vector cxx_args = { {"input_x", &x_ciphertext}, {"input_y", &y_ciphertext}, {"output_z", &z_ciphertext}, }; // Execute GPU computation uint64_t gpu_time = gpu_task.run(&context, cxx_args); ``` -------------------------------- ### Add Subdirectories for CKKS Examples (CPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/CMakeLists.txt These CMake commands integrate subdirectories with examples for the CKKS (Cheon-Kim-Kim-Song) homomorphic encryption scheme, optimized for CPU. They cover multiplication, serialization, Euclidean distance, and logistic regression. ```cmake add_subdirectory(ckks_mult_cpu) add_subdirectory(ckks_mult_serialization_cpu) add_subdirectory(ckks_euclidean_distance_cpu) add_subdirectory(ckks_logistic_regression_cpu) ``` -------------------------------- ### Add Subdirectory for Benchmarking Examples (GPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/CMakeLists.txt This CMake command conditionally adds a subdirectory for GPU-based benchmarking examples, provided that LATTISENSE_ENABLE_GPU is defined. This allows for performance testing on hardware accelerators. ```cmake if(LATTISENSE_ENABLE_GPU) add_subdirectory(benchmark_gpu) endif() ``` -------------------------------- ### Add Subdirectories for BFV Examples (CPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/CMakeLists.txt These CMake commands add subdirectories containing examples for the BFV (Brakerski-Fan-Vercauteren) homomorphic encryption scheme, specifically for CPU execution. They demonstrate basic operations and polynomial manipulations. ```cmake add_subdirectory(bfv_mult_cpu) add_subdirectory(bfv_poly_7_cpu) ``` -------------------------------- ### Add Subdirectories for Benchmarking Examples (CPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/CMakeLists.txt These CMake commands add subdirectories containing CPU-based benchmarking examples for the Lattisense SDK. They are used to measure the performance of various homomorphic encryption operations. ```cmake add_subdirectory(benchmark_cpu) add_subdirectory(benchmark_convolution) ``` -------------------------------- ### Main Execution Flow in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md The main function orchestrates the three phases of the FHE computation: client setup, server computation, and client result retrieval. It demonstrates the overall workflow by calling corresponding phase functions. ```c++ int main() { auto data_0 = client_phase_0(); auto data_1 = server_phase_1(&get<1>(data_0), &get<2>(data_0), &get<3>(data_0)); client_phase_2(&get<0>(data_0), &data_1); } ``` -------------------------------- ### Installation Directories (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/CMakeLists.txt Sets up installation directories for LattiSense components using standard GNUInstallDirs. This allows users to customize the installation prefix. ```cmake # install include(GNUInstallDirs) # Use standard CMAKE_INSTALL_PREFIX (default: /usr/local on Linux) # Users can override with: cmake .. -DCMAKE_INSTALL_PREFIX=/custom/path message(STATUS "CMAKE_INSTALL_PREFIX : ${CMAKE_INSTALL_PREFIX}") # Define install directories using GNUInstallDirs standards set(LATTISENSE_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR}) set(LATTISENSE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}/lattisense) set(LATTISENSE_INSTALL_DATADIR ${CMAKE_INSTALL_DATADIR}/lattisense) set(LATTISENSE_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/LattiSense) ``` -------------------------------- ### Full CKKS Ciphertext Multiplication and Decryption Workflow (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md Provides a comprehensive C++ example of the CKKS workflow, including parameter creation, context initialization, plaintext encoding, encryption, ciphertext multiplication, task execution, decryption, and result decoding. It demonstrates the complete process from data preparation to obtaining decrypted results. ```c++ uint64_t N = 8192; CkksParameter param = CkksParameter::create_parameter(N); CkksContext context = CkksContext::create_random_context(param); int level = 3; double default_scale = param.get_default_scale(); vector x_mg({5.0, 10.0}); vector y_mg({2.0, 3.0}); CkksPlaintext x_pt = context.encode(x_mg, level, default_scale); CkksPlaintext y_pt = context.encode(y_mg, level, default_scale); CkksCiphertext x_ct = context.encrypt_asymmetric(x_pt); CkksCiphertext y_ct = context.encrypt_asymmetric(y_pt); FheTaskCpu cpu_project("examples/ckks_mult"); CkksCiphertext z_ct = context.new_ciphertext(level - 1, default_scale * default_scale / param.get_q(level)); vector cxx_args = { {"x", &x_ct}, {"y", &y_ct}, {"z", &z_ct}, }; cpu_project.run(&context, cxx_args); CkksPlaintext z_pt = context.decrypt(z_ct); vector z_mg = context.decode(z_pt); print_double_message(z_mg.data(), "z_mg", 2); // Print result: z_mg = [10.000001, 30.000002, ...] ``` -------------------------------- ### Install Python Dependencies Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Installs the necessary Python packages required for LattiSense development, typically used for the computation graph compiler. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Lattisense Targets and Libraries (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/CMakeLists.txt Installs the Lattisense target, third-party libraries (gsl, nlohmann/json), and header files for the shared build mode. It also installs documentation and a project template. This configuration is essential for users to find and link against the Lattisense library using CMake's find_package. ```cmake if(LATTISENSE_BUILD_MODE STREQUAL "SHARED") # Export target for find_package support install(TARGETS lattisense EXPORT LattiSenseTargets LIBRARY DESTINATION ${LATTISENSE_INSTALL_LIBDIR}) # Install third-party libraries install(DIRECTORY ${LATTISENSE_ROOT_DIR}/lib/gsl DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}) install(FILES ${LATTISENSE_ROOT_DIR}/lib/nlohmann/json.hpp DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/nlohmann) # Install header files install(DIRECTORY ${LATTISENSE_ROOT_DIR}/cxx_sdk_v2/ DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/cxx_sdk_v2 FILES_MATCHING PATTERN "*.h" PATTERN "precision.h" EXCLUDE) install(DIRECTORY ${LATTISENSE_ROOT_DIR}/fhe_ops_lib/ DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/fhe_ops_lib FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${LATTISENSE_ROOT_DIR}/mega_ag_runners/ DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/mega_ag_runners FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${LATTISENSE_ROOT_DIR}/common/ DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/common FILES_MATCHING PATTERN "*.h") # Install documentation and project template install(DIRECTORY ${LATTISENSE_ROOT_DIR}/examples/project_template/ DESTINATION ${LATTISENSE_INSTALL_DATADIR}/project_template PATTERN "build" EXCLUDE) install(DIRECTORY ${LATTISENSE_ROOT_DIR}/doc/ DESTINATION ${LATTISENSE_INSTALL_DATADIR}/doc PATTERN "doc_checker.py" EXCLUDE) # Install mega_ag_generator (Python frontend for computation graph generation) install(CODE "execute_process(COMMAND bash \"-c\" \"mkdir -p ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/dist ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/log && cp -r ${LATTISENSE_ROOT_DIR}/frontend ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator && sed -i 's/TRANSLATOR_DEV = True/TRANSLATOR_DEV = False/' ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/frontend/custom_task.py")") ``` -------------------------------- ### CMake Project Setup and LattiSense Integration Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/project_template/CMakeLists.txt Configures a CMake project to use C++20, find the LattiSense library, and link an executable named 'my_fhe_app' against it. This setup is essential for projects leveraging LattiSense's FHE capabilities. ```cmake cmake_minimum_required(VERSION 3.13) project(my_fhe_app) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) find_package(LattiSense REQUIRED) add_executable(my_fhe_app main.cpp) target_link_libraries(my_fhe_app PRIVATE LattiSense::lattisense) ``` -------------------------------- ### Handle Move Constructor Example Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Demonstrates the usage of the move constructor for the Handle class, transferring ownership of resources from one Handle object to another. ```c++ CkksCiphertext x2 = context.add(x0, x1); CkksCiphertext x3(std::move(x2)); ``` -------------------------------- ### CKKS Ciphertext Encoding and Scale Management (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md Demonstrates the encoding of CKKS plaintext messages using a default scale obtained from parameters. It highlights the importance of managing ciphertext scales, especially after multiplication and rescaling, to ensure decryption accuracy. The example shows how to create a new ciphertext with an adjusted scale. ```c++ double default_scale = param.get_default_scale(); ... CkksPlaintext x_pt = context.encode(x_mg, level, default_scale); CkksPlaintext y_pt = context.encode(y_mg, level, default_scale); ``` ```c++ CkksCiphertext z_ct = context.new_ciphertext(level - 1, default_scale * default_scale / param.get_q(level)); ``` -------------------------------- ### Handle Move Assignment Example Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Provides an example of using the move assignment operator for the Handle class to transfer resources between Handle objects. ```c++ CkksCiphertext x2 = context.add(x0, x1); CkksCiphertext x3 = std::move(x2); ``` -------------------------------- ### Install Lattisense Libraries and Headers (Non-Shared CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/CMakeLists.txt Installs Lattisense libraries (`cxx_sdk_v2`, `fhe_ops_lib`), the Go SDK shared object (`liblattigo.so`), GSL headers, and C++ SDK headers for non-shared build modes. It also installs documentation and the Python frontend for the mega_ag_generator, similar to the shared build mode. ```cmake else() option(LATTISENSE_BUILD_EXAMPLES "Build lattisense examples" OFF) message(STATUS "LATTISENSE_BUILD_EXAMPLES: ${LATTISENSE_BUILD_EXAMPLES}") if(LATTISENSE_BUILD_EXAMPLES) add_subdirectory(examples) endif() install(TARGETS cxx_sdk_v2 LIBRARY DESTINATION ${LATTISENSE_INSTALL_LIBDIR}) install(TARGETS fhe_ops_lib LIBRARY DESTINATION ${LATTISENSE_INSTALL_LIBDIR}) install(FILES ${LATTISENSE_ROOT_DIR}/fhe_ops_lib/lattigo/go_sdk/liblattigo.so DESTINATION ${LATTISENSE_INSTALL_LIBDIR}) install(DIRECTORY ${LATTISENSE_ROOT_DIR}/lib/gsl DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}) install(DIRECTORY ${LATTISENSE_ROOT_DIR}/cxx_sdk_v2/ DESTINATION ${LATTISENSE_INSTALL_INCLUDEDIR}/cxx_sdk_v2FILES_MATCHING PATTERN "*.h" PATTERN "precision.h" EXCLUDE) install(DIRECTORY ${LATTISENSE_ROOT_DIR}/doc/ DESTINATION ${LATTISENSE_INSTALL_DATADIR}/doc PATTERN "doc_checker.py" EXCLUDE) install(CODE "execute_process(COMMAND bash \"-c\" \"mkdir -p ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/dist ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/log && cp -r ${LATTISENSE_ROOT_DIR}/frontend ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator && sed -i 's/TRANSLATOR_DEV = True/TRANSLATOR_DEV = False/' ${CMAKE_INSTALL_PREFIX}/${LATTISENSE_INSTALL_DATADIR}/mega_ag_generator/frontend/custom_task.py")") install(DIRECTORY ${LATTISENSE_ROOT_DIR}/examples/project_template/ DESTINATION ${LATTISENSE_INSTALL_DATADIR}/project_template PATTERN "build" EXCLUDE) endif() ``` -------------------------------- ### Example Usage of CxxVectorArgument in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Demonstrates how to create and use CxxVectorArgument objects in C++. The example shows preparing FHE data (plaintexts and ciphertexts), constructing CxxVectorArgument instances for individual parameters, and also for vector-form parameters. ```c++ // Prepare input data vector x_data({5, 10}); vector y_data({2, 3}); BfvPlaintext x_pt = context.encode(x_data, level); BfvPlaintext y_pt = context.encode(y_data, level); BfvCiphertext x_ct = context.encrypt_asymmetric(x_pt); // Input ciphertext BfvCiphertext y_ct = context.encrypt_asymmetric(y_pt); // Input ciphertext BfvCiphertext z_ct = context.new_ciphertext(level); // Output ciphertext // Construct parameter vector vector cxx_args = { {"input_x", &x_ct}, {"input_y", &y_ct}, {"output_z", &z_ct}, }; // Support vector form parameters vector ct_vector = {x_ct, y_ct}; CxxVectorArgument vector_arg("ct_vector", &ct_vector); ``` -------------------------------- ### CMake Integration for LattiSense Project Source: https://context7.com/cipherflow-fhe/lattisense/llms.txt Demonstrates how to integrate the LattiSense SDK into a CMake project. It finds the installed LattiSense package and links the LattiSense library to the executable. ```cmake cmake_minimum_required(VERSION 3.13) project(my_fhe_app) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) # Find installed LattiSense SDK find_package(LattiSense REQUIRED) add_executable(my_fhe_app main.cpp) target_link_libraries(my_fhe_app PRIVATE LattiSense::lattisense) ``` -------------------------------- ### Build LattiSense from Source (CPU) Source: https://context7.com/cipherflow-fhe/lattisense/llms.txt Builds the LattiSense framework from its source code, primarily focusing on the CPU version. This process involves cloning the repository, installing Python dependencies, and using CMake for configuration and building. ```bash # Clone repository with submodules git clone --recursive https://github.com/cipherflow-fhe/lattisense.git cd lattisense # Install Python dependencies pip install -r requirements.txt # Build CPU version mkdir build && cd build cmake .. -DLATTISENSE_BUILD_EXAMPLES=ON make -j$(nproc) # Install to custom directory cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd)/../install make install ``` -------------------------------- ### Build GPU Convolution Benchmark (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/benchmark_convolution/CMakeLists.txt This CMake code conditionally defines an executable for benchmarking convolution on the GPU, provided `LATTISENSE_ENABLE_GPU` is set. It links against 'lattisense' and includes the current source directory. ```cmake if(LATTISENSE_ENABLE_GPU) add_executable(benchmark_convolution_gpu benchmark_convolution_gpu.cpp ${CONV_SOURCES} ) target_link_libraries(benchmark_convolution_gpu PRIVATE lattisense) target_include_directories(benchmark_convolution_gpu PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) endif() ``` -------------------------------- ### Set Target Properties (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/bfv_poly_7_cpu/CMakeLists.txt Configures installation and build-time runtime search paths for the 'bfv_poly_7_cpu' target. 'INSTALL_RPATH' specifies where libraries should be found after installation, and 'BUILD_WITH_INSTALL_RPATH' controls whether this path is used during the build. ```cmake set_target_properties(bfv_poly_7_cpu PROPERTIES INSTALL_RPATH "$ORIGIN/../../../lib" BUILD_WITH_INSTALL_RPATH OFF ) ``` -------------------------------- ### Build CPU Convolution Benchmark (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/benchmark_convolution/CMakeLists.txt This CMake code defines an executable for benchmarking convolution on the CPU. It links against the 'lattisense' library and includes the current source directory for headers. ```cmake add_executable(benchmark_convolution_cpu benchmark_convolution_cpu.cpp ${CONV_SOURCES} ) target_link_libraries(benchmark_convolution_cpu PRIVATE lattisense) target_include_directories(benchmark_convolution_cpu PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) ``` -------------------------------- ### Generate and Install CMake Package Config Files (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/CMakeLists.txt Generates and installs CMake package configuration files (`LattiSenseConfig.cmake` and `LattiSenseConfigVersion.cmake`) for the Lattisense project. These files enable other CMake projects to find and use Lattisense via `find_package(LattiSense)`. The version file specifies compatibility, and the config file is generated from a template. ```cmake # Generate and install CMake Config Package files # Generate version config file write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/LattiSenseConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) # Configure package config file configure_package_config_file( "${LATTISENSE_ROOT_DIR}/cmake/LattiSenseConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/LattiSenseConfig.cmake" INSTALL_DESTINATION ${LATTISENSE_INSTALL_CMAKEDIR} ) # Install CMake config files install(FILES "${CMAKE_CURRENT_BINARY_DIR}/LattiSenseConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/LattiSenseConfigVersion.cmake" DESTINATION ${LATTISENSE_INSTALL_CMAKEDIR} ) # Install exported targets install(EXPORT LattiSenseTargets FILE LattiSenseTargets.cmake NAMESPACE LattiSense:: DESTINATION ${LATTISENSE_INSTALL_CMAKEDIR} ) ``` -------------------------------- ### Add Executable for CKKS Multiplication (CPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/ckks_mult_serialization_cpu/CMakeLists.txt Adds an executable target named 'ckks_mult_serialization_cpu' which compiles the 'ckks_mult_serialization_cpu.cpp' file. This is a standard CMake command for building executables. ```cmake add_executable(ckks_mult_serialization_cpu ckks_mult_serialization_cpu.cpp) ``` -------------------------------- ### Link Lattisense Library to CKKS Multiplication Executable Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/ckks_mult_serialization_cpu/CMakeLists.txt Links the 'lattisense' library as a private dependency to the 'ckks_mult_serialization_cpu' target. This ensures that the necessary Lattisense functionalities are available during the compilation and linking of the executable. ```cmake target_link_libraries(ckks_mult_serialization_cpu PRIVATE lattisense) ``` -------------------------------- ### Check Go Environment (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/CMakeLists.txt Ensures that a compatible Go compiler (version 1.18 or higher) is installed and accessible. It finds the Go executable and reports its version, failing if Go is not found. ```cmake find_program(GO_EXECUTABLE go) if(NOT GO_EXECUTABLE) message(FATAL_ERROR "Go compiler not found!\n" "Go >= 1.18 is required to build the Lattigo cryptography library.\n" "Please install Go from https://golang.org/dl/") else() execute_process( COMMAND ${GO_EXECUTABLE} version OUTPUT_VARIABLE GO_VERSION_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE ) message(STATUS "Found Go: ${GO_VERSION_OUTPUT}") endif() ``` -------------------------------- ### Server Phase 1: Deserialization and Homomorphic Multiplication in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md The computing party deserializes the received public context and ciphertexts. It then performs a homomorphic multiplication on the ciphertexts using the public context and serializes the resulting ciphertext for transmission back to the requester. ```c++ vector server_phase_1(vector* ctx_bin, vector* x_bin, vector* y_bin) { CkksContext public_context_de = CkksContext::deserialize(ctx_bin); CkksCiphertext x_ct_de = CkksCiphertext::deserialize(x_bin); CkksCiphertext y_ct_de = CkksCiphertext::deserialize(y_bin); CkksCiphertext3 z_ct3 = public_context_de.mult(x_ct_de, y_ct_de); CkksCiphertext z_ct = public_context_de.relinearize(z_ct3); vector z_bin = z_ct.serialize(public_context_de.get_parameter()); return z_bin; } ``` -------------------------------- ### Build LattiSense SDK with GPU Support Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Builds the LattiSense SDK with GPU acceleration enabled. This requires HEonGPU to be built and installed first, and the `LATTISENSE_ENABLE_GPU` and `LATTISENSE_CUDA_ARCH` CMake options to be set. ```bash # 2. Return to SDK directory and build with GPU support cd ../.. mkdir build && cd build cmake .. -DLATTISENSE_ENABLE_GPU=ON -DLATTISENSE_CUDA_ARCH= make -j$(nproc) ``` -------------------------------- ### Create BFV Parameters and Context (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md Generates homomorphic parameters and a context for BFV ciphertext operations. Requires polynomial degree (n) and plaintext modulus (t). Outputs BfvParameter and BfvContext objects. The level parameter determines the RNS components. ```C++ uint64_t t = 0x1b4001; uint64_t n = 16384; BfvParameter param = BfvParameter::create_parameter(n, t); BfvContext context = BfvContext::create_random_context(param); int level = 3; ``` -------------------------------- ### Client Phase 2: Decryption and Decoding in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md The requester deserializes the computed ciphertext received from the server. It then uses its original, private context to decrypt the ciphertext and decode the resulting plaintext to obtain the final computation result. ```c++ void client_phase_2(CkksContext* ctx, vector* z_bin) { CkksCiphertext z_ct_de = CkksCiphertext::deserialize(z_bin); CkksPlaintext z_pt = ctx->decrypt(z_ct_de); vector z_mg = ctx->decode(z_pt); print_double_message(z_mg.data(), "z_mg", 2); // Output: z_mg = [10.000000, 29.999998, ...] } ``` -------------------------------- ### Set Target Properties for Installation Path (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/ckks_euclidean_distance_cpu/CMakeLists.txt This CMake code configures installation-related properties for the 'ckks_euclidean_distance_cpu' target. It sets the INSTALL_RPATH to a specific relative path and disables BUILD_WITH_INSTALL_RPATH. ```cmake set_target_properties(ckks_euclidean_distance_cpu PROPERTIES INSTALL_RPATH "$ORIGIN/../../../lib" BUILD_WITH_INSTALL_RPATH OFF ) ``` -------------------------------- ### Load and Execute Compiled BFV Task (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md Loads a pre-compiled FHE task for BFV ciphertext multiplication and executes it on a CPU using the LattiSense C++ API. It sets up input/output ciphertext objects and runs the computation. ```cpp FheTaskCpu project("example/bfv_mult"); BfvCiphertext z_ct = context.new_ciphertext(level); vector cxx_args = { {"x", &x_ct}, {"y", &y_ct}, {"z", &z_ct}, }; project.run(&context, cxx_args); ``` -------------------------------- ### Client Phase 0: Key Generation, Encoding, and Encryption in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md This phase handles the requester's initial steps: generating CKKS parameters, creating a context, encoding and encrypting data (x and y), and preparing public context and ciphertexts for transmission. It serializes the public context and ciphertexts into byte vectors. ```c++ tuple, vector, vector> client_phase_0() { int N = 16384; CkksParameter param = CkksParameter::create_parameter(N); CkksContext ctx = CkksContext::create_random_context(param); int level = 3; double default_scale = pow(2, 32); vector x_mg({5.0, 10.0}); vector y_mg({2.0, 3.0}); CkksPlaintext x_pt = ctx.encode(x_mg, level, default_scale); CkksPlaintext y_pt = ctx.encode(y_mg, level, default_scale); CkksCiphertext x_ct = ctx.encrypt_asymmetric(x_pt); CkksCiphertext y_ct = ctx.encrypt_asymmetric(y_pt); CkksContext public_ctx = ctx.make_public_context(); vector public_ctx_bin = public_ctx.serialize(); vector x_bin = x_ct.serialize(); vector y_bin = y_ct.serialize(); return {move(ctx), public_ctx_bin, x_bin, y_bin}; } ``` -------------------------------- ### Build and Run LattiSense Tests Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Instructions for building LattiSense with unit tests enabled and then running them. This includes generating test data using Python scripts and executing the compiled test binaries. ```bash # Build with tests enabled mkdir build && cd build cmake .. -DLATTISENSE_BUILD_TESTS=ON make -j$(nproc) # Generate test data cd ../unittests python3 test_cpu_bfv.py python3 test_cpu_ckks.py # Run tests cd ../build/unittests ./test_lattigo # underlying operators tests ./test_cpu_bfv # BFV CPU tests ./test_cpu_ckks # CKKS CPU tests ``` -------------------------------- ### CKKS Parameter and Context Management (C++) Source: https://context7.com/cipherflow-fhe/lattisense/llms.txt Demonstrates how to create and manage CKKS encryption parameters and context, including custom modulus chains and generating rotation keys. It also covers creating a public context for servers and setting up bootstrapping parameters. ```cpp #include using namespace cxx_sdk_v2; // Create CKKS parameters uint64_t N = 16384; CkksParameter param = CkksParameter::create_parameter(N); // Or with custom modulus chains std::vector Q = {0x3fffffffef8001, 0x4000000011c001, 0x40000000120001}; std::vector P = {0x7ffffffffb4001}; CkksParameter custom_param = CkksParameter::create_custom_parameter(N, Q, P); // Get default scale (power of 2 closest to q1) double default_scale = param.get_default_scale(); // Create context with random keys CkksContext context = CkksContext::create_random_context(param); // Generate rotation keys context.gen_rotation_keys(); context.gen_rotation_keys_for_rotations({1, 5, 10, 100}, false); // Create public context for server CkksContext public_ctx = context.make_public_context(); // CKKS Bootstrap support CkksBtpParameter btp_param = CkksBtpParameter::create_parameter(); CkksBtpContext btp_context = CkksBtpContext::create_random_context(btp_param); btp_context.gen_rotation_keys(); // Toy bootstrap parameters for testing (smaller, not secure) CkksBtpParameter toy_param = CkksBtpParameter::create_toy_parameter(); ``` -------------------------------- ### Initialize FHE Parameters and Set Global Parameter (Python) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/2_Platform_Overview/2_Platform_Overview.md This snippet shows how to initialize FHE parameters using a hardware-optimized default configuration and set them globally for subsequent operations in LattiSense. It requires the 'Param' and 'set_fhe_param' functions from the LattiSense library. ```python param = Param.create_bfv_default_param(n=16384) set_fhe_param(param) ``` -------------------------------- ### Compute Multiplication using CKKS (Python) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/3_Application_Development_Examples/3_Application_Development_Examples.md Sets up global parameters for the CKKS homomorphic encryption scheme for CPU execution. This snippet configures the polynomial modulus degree (n) and the coefficient modulus (q) required for CKKS computations, preparing the environment for subsequent operations. ```python # Set global parameters (custom parameters, only for CPU execution) param = Param.create_ckks_custom_param( n=8192, p=[0x7ffffffffb4001], q=[0x3fffffffef8001, 0x4000000011c001, 0x40000000120001, 0x3fffffffd08001] ) set_fhe_param(param) ``` -------------------------------- ### Manage BFV Parameters and Context in C++ Source: https://context7.com/cipherflow-fhe/lattisense/llms.txt Demonstrates how to create and manage BFV encryption parameters and context using the C++ SDK. This includes setting up standard or custom modulus chains, generating rotation keys, creating public contexts, and performing serialization/deserialization for network transmission. ```cpp #include #include using namespace cxx_sdk_v2; // Create BFV parameters uint64_t N = 16384; uint64_t t = 65537; // Plaintext modulus BfvParameter param = BfvParameter::create_parameter(N, t); // Or create with custom modulus chains std::vector Q = {0x3fffffffef8001, 0x4000000011c001, 0x40000000120001}; std::vector P = {0x7ffffffffb4001}; BfvParameter custom_param = BfvParameter::create_custom_parameter(N, t, Q, P); // Access parameter properties int n = param.get_n(); // Polynomial degree uint64_t plaintext_mod = param.get_t(); int max_level = param.get_max_level(); uint64_t q0 = param.get_q(0); // First Q modulus component // Create context with random keys BfvContext context = BfvContext::create_random_context(param); // Generate rotation keys for all power-of-2 steps context.gen_rotation_keys(); // Or generate specific rotation keys context.gen_rotation_keys_for_rotations({1, 2, 3, 5, 10}, true); // include row rotation // Create public context (without secret key) for server-side computation BfvContext public_ctx = context.make_public_context(true, true, true); // Shallow copy for multi-threading BfvContext thread_ctx = context.shallow_copy_context(); // Serialization for network transmission std::vector serialized = context.serialize_advanced(); BfvContext restored = BfvContext::deserialize_advanced(serialized); ``` -------------------------------- ### Configure Target Properties for Shared Library Installation (CMake) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/fhe_ops_lib/CMakeLists.txt This CMake code sets installation-related properties for the 'fhe_ops_lib' shared library. It configures the runtime search path (RPATH) to '$ORIGIN', disables building with RPATH during installation, and ensures that the RPATH is not used for linking path resolution. ```cmake set_target_properties(fhe_ops_lib PROPERTIES INSTALL_RPATH "$ORIGIN" BUILD_WITH_INSTALL_RPATH OFF INSTALL_RPATH_USE_LINK_PATH FALSE ) ``` -------------------------------- ### Build LattiSense SDK (CPU) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Compiles the LattiSense SDK for CPU execution. This involves creating a build directory, configuring with CMake, and then building the project using make. ```bash mkdir build && cd build cmake .. make -j$(nproc) ``` -------------------------------- ### BFV Ciphertext Functions Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Functions for BFV ciphertext objects, including getting the level. ```APIDOC ### BfvCiphertext Class BFV ciphertext class containing 2 polynomials. #### Function get_level Gets the level of a BFV ciphertext. ### Method `int get_level() const;` ``` -------------------------------- ### BFV Plaintext Functions Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Functions for BFV plaintext objects, including getting the level and printing. ```APIDOC ### BfvPlaintext Class BFV plaintext class for encryption, decryption, and ciphertext-plaintext addition. #### Function get_level Gets the level of a BFV plaintext. ### Method `int get_level() const;` #### Function print Prints the value of a BFV plaintext object. ### Method `void print() const;` ``` -------------------------------- ### Get Parameters in C++ Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Extracts homomorphic parameters from the given context. This function returns a reference to the CkksParameter object. ```c++ CkksParameter& get_parameter() override; ``` -------------------------------- ### Get CKKS Parameters (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Retrieves the homomorphic parameters associated with the current CkksContext. This provides access to the underlying cryptographic settings. ```c++ virtual const CkksParameter& get_parameter(); // Description: Get the homomorphic parameters corresponding to the context. // Parameters: None. // Return value: Homomorphic parameters. ``` -------------------------------- ### Handle Class Member Functions Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Documentation for the public member functions of the Handle template class, including destructor, get(), and is_empty(). ```APIDOC ## Handle Class Member Functions ### Description Documentation for the public member functions of the Handle template class, including destructor, get(), and is_empty(). ### Method Member Functions ### Endpoint N/A ### Parameters #### Destructor `virtual ~Handle()` - **None** #### Get internal value `const uint64_t& get() const` - **Return value**: A constant reference to the internal uint64_t value of the Handle. #### Check if empty `bool is_empty() const` - **Return value**: True if the Handle is empty, false otherwise. ### Request Example ```c++ // Get internal value example uint64_t handle_value = myHandle.get(); // Check if empty example bool empty = myHandle.is_empty(); ``` ### Response #### Success Response (Member Functions) - **get()**: Returns `const uint64_t&` - **is_empty()**: Returns `bool` #### Response Example N/A ``` -------------------------------- ### Execute Encrypted Process Graph on CPU Backend (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/2_Platform_Overview/2_Platform_Overview.md This C++ code snippet demonstrates how to execute a pre-defined Encrypted Process Graph (ERG) using the LattiSense Runtime SDK on a CPU backend. It maps runtime variables (ciphertexts and plaintexts) to the ERG's arguments and initiates the computation. The ERG is loaded from a JSON file. ```cpp #include #include #include "lattisense/bfv.h" #include "lattisense/runtime/fhe_task_cpu.h" using namespace std; using namespace lattisense::bfv; // ... inside a function, after context and ciphertext setup ... FheTaskCpu task("examples/bfv_poly_7"); vector cxx_args = { {"x", &x_ct}, {"a0", &a0_pt}, {"a", &a_pt_mul}, {"y", &y_ct}, }; task.run(&context, cxx_args); ``` -------------------------------- ### Get BFV Plaintext Level Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Retrieves the level of a BFV plaintext. This function does not take any parameters and returns an integer representing the plaintext level. ```c++ int get_level() const; ``` -------------------------------- ### Build HEonGPU for GPU Acceleration Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Steps to build the HEonGPU library, a prerequisite for enabling GPU acceleration in LattiSense. Requires specifying CUDA architecture and compiler path. ```bash # 1. Build HEonGPU cd HEonGPU mkdir build && cd build cmake .. \ -DCMAKE_CUDA_ARCHITECTURES= \ -DCMAKE_CUDA_COMPILER=/bin/nvcc \ -DCMAKE_INSTALL_PREFIX=/install make -j$(nproc) make install ``` -------------------------------- ### Build GPU Benchmark Executable with CMake Source: https://github.com/cipherflow-fhe/lattisense/blob/main/examples/benchmark_gpu/CMakeLists.txt Configures the build system to create an executable named 'benchmark_gpu' from 'benchmark_gpu.cpp' and links it against the 'lattisense' library. This is essential for running GPU performance tests. ```cmake add_executable(benchmark_gpu benchmark_gpu.cpp) target_link_libraries(benchmark_gpu PRIVATE lattisense) ``` -------------------------------- ### Clone LattiSense Repository Source: https://github.com/cipherflow-fhe/lattisense/blob/main/README.md Clones the LattiSense repository and its submodules. This is the first step for building the project from source. ```bash git clone --recursive https://github.com/cipherflow-fhe/lattisense.git cd lattisense ``` -------------------------------- ### Get Default CKKS Scale Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Retrieves the default scale associated with CKKS homomorphic parameters. The default scale is the power of 2 closest to q_1. ```c++ double get_default_scale() const; ``` -------------------------------- ### Get Homomorphic Parameters (C++) Source: https://github.com/cipherflow-fhe/lattisense/blob/main/doc/doc-en/4_API_Reference/4_API_Reference.md Retrieves the homomorphic parameters associated with the current FHE context. These parameters define the cryptographic properties and constraints of the encryption scheme. ```c++ const BfvParameter& get_parameter(); ```