diff --git a/.gitignore b/.gitignore index 9537622..6ec34c1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ *.dat **/config.h **/adapter_output_data -# device/lib/kri_data/* # adpater/thirdparty # **/data/ diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..35fb62a --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,36 @@ +# List of Changes + +## Version 1.1.0 + +### New Features + +- Added explicit support for other parameter sets (for degree = 1024, 2048, 8192, and 16384, as well as an alternate prime set for degree = 4096) +- Adapter can now just be build once for any of the supported degrees, and degree can be specied as a command line argument +- Added scripts to [`device/scripts`](device/scripts) to automatically test configurations of the library + +### Bug Fixes + +- Added an extra index map setting to fix memory bug encountered in certain edge-case configurations of the library +- Fixed error with generated header files for special primes > 30 bits +- Fixed error with IFFT header file generation +- Fixed error with NTT tests + +### Minor API Changes + +- Scale is now automatically set for default parameters +- `set_custom_parms_ckks` now includes a parameter to set the scale + +### Other + +- Corrected minor typos in code comments +- Added description comments for some functions +- Added `const` and explicit type casting as good practice +- Added `memset` after array creation instances for when `malloc` is not used +- Removed support for degree < 1024, which was just for initial debugging +- Fixed path issue when building for an Azure Sphere A7 target +- Updated adapter [`CMakeLists.txt`](adapter/CMakeLists.txt) to use Microsoft SEAL 3.7.2 +- Other minor code changes + +## Version 1.0.0 + +- Initial commit diff --git a/README.md b/README.md index 4d22452..cf47551 100644 --- a/README.md +++ b/README.md @@ -1,111 +1,137 @@ -# SEAL-Embedded - -SEAL-Embedded is an open-source ([MIT licensed](LICENSE)) homomorphic encryption toolset for embedded devices developed by the Cryptography and Privacy Reserach Group at Microsoft. -It implements the designs of CKKS-style encoding and encryption with small code and memory footprint that is published in [this peer-reviewed paper](https://tches.iacr.org/index.php/TCHES/article/view/8991). -SEAL-Embedded is written in C and C++ and primarily designed for building high-level applications on [Azure Sphere](https://azure.microsoft.com/en-us/services/azure-sphere/)'s ARM A7 processor. -To enable wider experiments by developers and researchers, SEAL-Embedded includes configurations that target ARM M4 as well. - -## Acknowledgments - -The majority of SEAL-Embedded was developed by [Deepika Natarajan](https://github.com/dnat112) from University of Michigan during an internship at Microsoft, despite not presented in the Git history. - -## Introduction - -Homomorphic encryption allows computation on encryption data without decryption. -To understand what homomorphic encryption and Microsoft SEAL are, we refer readers to [the Introduction section of Microsoft SEAL](https://github.com/microsoft/SEAL#introduction). -Typically, a client generates a secret key and public keys; data are encrypted with the secret key or public keys; encrypted data are sent to a untrusted server for computation; encrypted results are returned to the client who then decrypts the results. -SEAL-Embedded enables embedded devices to encrypt data; it *does not* perform key generation, computation on encrypted data, or decryption. - -SEAL-Embedded consists of mainly two components: a device library ([`device/lib`](device/lib)) to build an application that encrypts data and an adapter application ([`adapter`](adapter)) for compability with Microsoft SEAL. -The simplest workflow is described as follows: -1. run adapter to generate a secret key and public keys; -1. build an application with device library and create an image of the application together with public keys for a target device; -1. run adapter on an remote server to receive and serialize encrypted data to Microsoft-SEAL-compatible format; -1. build an application with Microsoft SEAL to compute on encrypted data; -1. decrypt encrypted results with Microsoft SEAL on a safe device that has the secret key. - -## Warning - -This library is research code and is not yet intended for production use. -Use at your own risk. -Holding secret key on device's storage for asymmetric encryption is extremely dangerous, regardless of whether the storage type is persistent or not. -Use public key (asymmetric encryption), or consult a security expert. -See more information related to [SECURITY](SECURITY.md). - -## Building SEAL-Embedded - -On all platforms SEAL-Embedded is built with CMake. -We recommend using out-of-source build although in-source build works. -Below we give instructions for how to configure and build SEAL-Embedded components. - -### SEAL-Embedded Adapter - -The adapter ([`adapter`](adapter)) application automatically downloads and builds Microsoft SEAL as a dependency. -System requirements are listed in [SEAL/README.md](https://github.com/microsoft/SEAL/blob/main/README.md#requirements). - -On Unix-like systems: -```powershell -cd adapter -cmake -S . -B build -cmake --build build -./build/bin/se_adapter # run adapter -``` - -On Windows, following [this guide](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-160), use Visual Studio 2019 to open [`adapter/CMakeLists.txt`](adapter/CMakeLists.txt) as a CMake project. - -### SEAL-Embedded Device Library - -The device library comes with optional executables: unit tests ([`device/test`](device/test)) and benchmark ([`device/bench`](device/bench)). -The library itself, unit tests, and benchmark can be built on a native system (Linux or macOS) with same development environment listed in SEAL-Embedded adapter. -```powershell -cd adapter -cmake -S . -B build -DSE_BUILD_LOCAL=ON -cmake --build build -``` -This by default builds a library and unit tests. -The following options are configurable for custom builds. - -| CMake option | Values | Information | -| ------------ | ------ | ------------ | -| SE_BUILD_LOCAL | **ON** / OFF | Set to `OFF` to target an embedded device. | -| SE_BUILD_M4 | ON / **OFF** | Set to `ON` to target ARM M4, `OFF` to target ARM A7 on Azure Sphere. | -| SE_M4_IS_SPHERE | ON / **OFF** | Set to `ON` to target the real-time core on Azure Sphere, `OFF` to target other ARM M4 processors. | -| CMAKE_BUILD_TYPE | **Debug**
Release
MinSizeRel
RelWithDebInfo | Set to `Release` for the best run-time performance and the smallest code size. | -| SE_BUILD_TYPE | **Tests**
Bench
Lib | Set to `Bench` to build instead the benchmark executable, to `Lib` to build a library only. | - -To target an embedded device, the specific SDK for the target device is usually required. -Either the unit tests or the benchmark can be built and imaged on the device. -Using Azure Sphere as an example, please follow [this guide](https://docs.microsoft.com/en-us/azure-sphere/install/qs-blink-application?tabs=windows%2Ccliv2beta&pivots=visual-studio). -**Note: SEAL-Embedded Adapter must be built and executed first to generate required key and precomputation files.** -Ultimately, the users can develop their own application following [this guide](https://docs.microsoft.com/en-us/azure-sphere/install/qs-real-time-application?tabs=windows%2Ccliv2beta&pivots=visual-studio) and statically link to SEAL-Embedded device library. - -For targeting ARM M4 on Azure Sphere or other development boards, please follow related documents and tutorials. -For the convinience of verifying ARM M4 compatibility, we also provide [`device/lib/sdk_config.h`](device/lib/sdk_config.h) for an easier deployment on Nordic Semiconductor nRF5* series. - -## Contributing - -The main purpose of open sourcing SEAL-Embedded is to enable developers to build privacy-enhancing applications or services. -We welcome any suggestions or contributions to improve the usebility, efficiency, and quality of SEAL-Embedded. -To keep the development active, the `main` branch will always include the most recent changes, beyond the lastest release tag. -For contributing to Microsoft SEAL, please see [CONTRIBUTING](CONTRIBUTING.md). - -## Citing SEAL-Embedded - -To cite SEAL-Embedded in academic papers, please use the following BibTeX entries. - -```tex - @Article{sealembedded, - title = {{SEAL}-Embedded: A Homomorphic Encryption Library for the Internet of Things}, - author = {Deepika Natarajan and Wei Dai}, - journal = {{IACR} Transactions on Cryptographic Hardware and Embedded Systems}, - publisher = {Ruhr-Universit{\"a}t Bochum}, - year = 2021, - month = jul, - pages = {756--779}, - valume = 2021, - number = 3, - doi = {10.46586/tches.v2021.i3.756-779}, - issn = {2569-2925}, - note = {\url{https://tches.iacr.org/index.php/TCHES/article/view/8991}}, - } -``` +# SEAL-Embedded + +SEAL-Embedded is an open-source ([MIT licensed](LICENSE)) homomorphic encryption toolset for embedded devices developed by the Cryptography and Privacy Reserach Group at Microsoft. +It implements the designs of CKKS-style encoding and encryption with small code and memory footprint that is published in [this peer-reviewed paper](https://tches.iacr.org/index.php/TCHES/article/view/8991). +SEAL-Embedded is written in C and C++ and primarily designed for building high-level applications on [Azure Sphere](https://azure.microsoft.com/en-us/services/azure-sphere/)'s ARM A7 processor. +To enable wider experiments by developers and researchers, SEAL-Embedded includes configurations that target ARM M4 as well. + +## Acknowledgments + +The majority of SEAL-Embedded was developed by [Deepika Natarajan](https://github.com/dnat112) from University of Michigan during an internship at Microsoft, despite her not being present in the Git history. + +## Introduction + +Homomorphic encryption allows for computation on encryption data without decryption. +Typically, a client generates a secret key and public keys; data is encrypted with the secret key or public keys; encrypted data is sent to a untrusted server for computation; encrypted results are returned to the client who then decrypts the results. +Interested users of SEAL-Embedded should read [the Introduction section of Microsoft SEAL](https://github.com/microsoft/SEAL#introduction) prior to using SEAL-Embedded to learn more about homomorphic encryption and the Microsoft SEAL library. +SEAL-Embedded enables embedded devices to encrypt data; it *does not* perform key generation, computation on encrypted data, or decryption (but does include an easy-to-use `adapter` interface to perform key generation with Microsoft SEAL). + +SEAL-Embedded consists of mainly two components: a device library ([`device/lib`](device/lib)) to build an application that encrypts data, and an adapter application ([`adapter`](adapter)) for compability with Microsoft SEAL. +The simplest workflow is described as follows: +1. Run the adapter to generate a secret key and public keys +1. Build and run an application with the device library by creating an image of the application together with the public key (for asymmetric encryption) or secret key (for symmetric encryption) for a target device +1. Run the adapter on an remote server to receive and serialize encrypted data to Microsoft-SEAL-compatible format +1. Build and run an application with Microsoft SEAL to compute on encrypted data +1. Decrypt the encrypted results with Microsoft SEAL on a safe device that has the secret key + +## Warning + +This library is research code and is not yet intended for production use. +Use at your own risk. +Storing a secret key on device (i.e., using symmetric encryption) can be extremely dangerous. +Use public key (asymmetric encryption), or consult a security expert before creating a symmetric key deployment. +See more information related to [SECURITY](SECURITY.md). + +## Building SEAL-Embedded + +On all platforms, SEAL-Embedded is built with CMake. +We recommend using an out-of-source build, although in-source builds work as well. +Below we give instructions for how to configure and build SEAL-Embedded components. + +### SEAL-Embedded Adapter + +The adapter ([`adapter`](adapter)) application automatically downloads and builds Microsoft SEAL as a dependency. +System requirements are listed in [SEAL/README.md](https://github.com/microsoft/SEAL/blob/main/README.md#requirements). + +On Unix-like systems: +```powershell +cd adapter +cmake -S . -B build +cmake --build build -j +./build/bin/se_adapter # run adapter +``` + +On Windows, following [this guide](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-160), use Visual Studio 2019 to open [`adapter/CMakeLists.txt`](adapter/CMakeLists.txt) as a CMake project. + +### SEAL-Embedded Device Library + +The device library comes with optional executables: unit tests ([`device/test`](device/test)) and benchmarks ([`device/bench`](device/bench)). +The library itself, unit tests, and benchmarks can be built on a native system (Linux or macOS) with the same development environment listed in SEAL-Embedded adapter. +```powershell +cd device +cmake -S . -B build -DSE_BUILD_LOCAL=ON +cmake --build build -j +./build/bin/seal_embedded_tests # run tests locally +``` +This by default builds a library and unit tests. + +To build the library and run tests for the Azure Sphere A7 target instead: +```powershell +cd device +cmake -G Ninja -S . -B build -DSE_BUILD_LOCAL=OFF +cmake --build build -j +sh ./scripts/sphere_a7_launch_cl.sh +``` +Open another terminal and invoke gdb: +```powershell +sh ./scripts/gdb_launch_sphere_a7.sh +``` +The output should now print to the first terminal. + +The following options are configurable for custom builds (default settings are in bold). + +| CMake option | Values | Information | +| ------------ | ------ | ------------ | +| SE_BUILD_LOCAL | **ON** / OFF | Set to `OFF` to target an embedded device. | +| SE_BUILD_M4 | ON / **OFF** | Set to `ON` to target ARM M4, `OFF` to target ARM A7 on Azure Sphere. | +| SE_M4_IS_SPHERE | ON / **OFF** | Set to `ON` to target the real-time core on Azure Sphere, `OFF` to target other ARM M4 processors. | +| CMAKE_BUILD_TYPE | **Debug**
Release
MinSizeRel
RelWithDebInfo | Set to `Release` for the best run-time performance and the smallest code size. | +| SE_BUILD_TYPE | **Tests**
Bench
Lib | Set to `Bench` to build instead the benchmark executable, to `Lib` to build a library only. | + +**Note: SEAL-Embedded device code is built in Debug and Local mode by default!** +We recommend that all users of the library first ensure that the included tests and their target application run in `Debug` mode locally, for their desired memory configuration (see: [`user_defines.h`](device/lib/user_defines.h) and the SEAL-Embedded [paper](https://tches.iacr.org/index.php/TCHES/article/view/8991) for more details on memory configuration options). +After verifying that these tests pass, users should change `CMAKE_BUILD_TYPE` to `Release`, uncomment `SE_DISABLE_TESTING_CAPABILITY` in user_defines.h, and set `SE_ASSERT_TYPE` to `0` (`None`) in user_defines.h, for optimal performance. +To run benchmarks, change `SE_BUILD_TYPE` to `Bench` and uncomment `SE_ENABLE_TIMERS` in [`defines.h`](device/lib/defines.h). +Users should verify that everything runs as expected in their local development environment first before targetting an embedded device. + +**Note: SEAL-Embedded Adapter must be built and executed first to generate required key and precomputation files.** +The adapter by default generates and writes files to `device/adapter_output_files`. +If `device/adapter_output_files` does not exist, you should create this folder prior to running the adapter. +Note that the adapter is not intended to support generation or usage of multiple parameter instances at once (i.e., using the adapter to create files for degree = 1024, then using the adapter to create files for degree = 4096, will cause some of the files generated for degree = 1024 to be overwritten). + +To target an embedded device, the specific SDK for the target device is usually required. +Either the unit tests or benchmarks can be built and imaged on the device. +Using the Azure Sphere as an example, please follow [this guide](https://docs.microsoft.com/en-us/azure-sphere/install/qs-blink-application?tabs=windows%2Ccliv2beta&pivots=visual-studio). +Ultimately, users can develop their own application following [this guide](https://docs.microsoft.com/en-us/azure-sphere/install/qs-real-time-application?tabs=windows%2Ccliv2beta&pivots=visual-studio) and statically link to the SEAL-Embedded device library. + +For targeting ARM M4 on the Azure Sphere or other development boards, please follow related documents and tutorials. +Note that stack size limitations may prevent certain configurations of the library to run on an Azure Sphere M4 target. +For the convinience of verifying ARM M4 compatibility, we also provide [`device/lib/sdk_config.h`](device/lib/sdk_config.h) for an easier deployment on Nordic Semiconductor nRF5* series devices. + +## Contributing + +The main purpose of open sourcing SEAL-Embedded is to enable developers to build privacy-enhancing applications or services. +We welcome any suggestions or contributions to improve the usebility, efficiency, and quality of SEAL-Embedded. +To keep the development active, the `main` branch will always include the most recent changes, beyond the lastest release tag. +For contributing to Microsoft SEAL, please see [CONTRIBUTING](CONTRIBUTING.md). + +## Citing SEAL-Embedded + +To cite SEAL-Embedded in academic papers, please use the following BibTeX entries. + +```tex + @Article{sealembedded, + title = {{SEAL}-Embedded: A Homomorphic Encryption Library for the Internet of Things}, + author = {Deepika Natarajan and Wei Dai}, + journal = {{IACR} Transactions on Cryptographic Hardware and Embedded Systems}, + publisher = {Ruhr-Universit{\"a}t Bochum}, + year = 2021, + month = jul, + pages = {756--779}, + valume = 2021, + number = 3, + doi = {10.46586/tches.v2021.i3.756-779}, + issn = {2569-2925}, + note = {\url{https://tches.iacr.org/index.php/TCHES/article/view/8991}}, + } +``` diff --git a/SECURITY.md b/SECURITY.md index e5b7bbc..9bd2201 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -1,16 +1,13 @@ ## Correct Use of SEAL-Embedded SEAL-Embedded supports encryption with either a secret key (symmetric) or a public key (asymmetric). -A secret key is stored in device flash RAM, and is vulnerable against attacker with physiscal access to the deivce. -Once a secret key is leaked, all previous data encrypted with the secret key or a public key generated by the secret key are at risk. -Deployments of SEAL-Embedded should avoid using symmetric encryption unless has been reviewed by security and cryptography experts. +When symmetric encryption is used, the secret key must be stored in device memory and is thus vulnerable to attackers with access to the device. +Once the secret key is leaked, all previous data encrypted with the secret key (or a public key associated with the secret key) is at risk. +Therefore, deployments of SEAL-Embedded should avoid using symmetric encryption, unless the deployment has been reviewed by security and cryptography experts. ## Correct Use of Microsoft SEAL -Homomorphic encryption schemes have various and often unexpected security models that may be surprising even to cryptography experts. -In particular, decryptions of Microsoft SEAL ciphertexts should be treated as private information only available to the secret key owner, as sharing decryptions of ciphertexts may in some cases lead to leaking the secret key. -If it is absolutely necessary to share information about the decryption of a ciphertext, for example when building a protocol of some kind, the number of bits shared should be kept to a minimum, and secret keys should be rotated regularly. -Commercial applications of Microsoft SEAL should be carefully reviewed by cryptography experts who are familiar with homomorphic encryption security models. +Please see the [SECURITY.md document for Microsoft SEAL](https://github.com/microsoft/SEAL/blob/main/SECURITY.md#correct-use-of-microsoft-seal). ## Security @@ -24,7 +21,7 @@ If you believe you have found a security vulnerability in any Microsoft-owned re Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report). -If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc). +If you prefer to submit without logging in, send an email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc). You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc). @@ -50,4 +47,4 @@ We prefer all communications to be in English. Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd). - \ No newline at end of file + diff --git a/adapter/CMakeLists.txt b/adapter/CMakeLists.txt index 1126b20..acfa288 100644 --- a/adapter/CMakeLists.txt +++ b/adapter/CMakeLists.txt @@ -39,7 +39,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") endif() endif() -project(SEAL_Embedded_Adapter VERSION 1.0.0 LANGUAGES CXX) +project(SEAL_Embedded_Adapter VERSION 1.1.0 LANGUAGES CXX) if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13) cmake_policy(SET CMP0077 NEW) @@ -94,7 +94,7 @@ if(SE_ADAPTER_BUILD_DEPS) FetchContent_Declare( seal GIT_REPOSITORY https://github.com/microsoft/SEAL.git - GIT_TAG 6bfac481aae4057ea887d42eb0f24371e9b4c4f9 # 3.7.1 + GIT_TAG 79234726053c45eede688400aa219fdec0810bd8 # 3.7.2 ) FetchContent_GetProperties(seal) diff --git a/adapter/adapter.cpp b/adapter/adapter.cpp index f9a15e3..357f688 100644 --- a/adapter/adapter.cpp +++ b/adapter/adapter.cpp @@ -29,13 +29,9 @@ static string save_dir_path = string(SE_ADAPTER_FILE_OUTPUT_DIR) + "/adapter_out static string ct_str_file_path_asym = string(SE_ADAPTER_FILE_OUTPUT_DIR) + "/out_asym_api_tests"; static string ct_str_file_path_sym = string(SE_ADAPTER_FILE_OUTPUT_DIR) + "/out_sym_api_tests"; -void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALContext &context, - bool symm_enc, string ct_str_file_path, - string sk_binfilename = "") +void verify_ciphertexts(string dirpath, double scale, size_t degree, seal::SEALContext &context, + bool symm_enc, string ct_str_file_path, string sk_binfilename = "") { - // size_t n = degree; - // EncryptionParameters parms(scheme_type::ckks); - // SEALContext context = setup_seale_30bitprime_default(degree, parms); auto &parms = context.key_context_data()->parms(); size_t n = parms.poly_modulus_degree(); @@ -49,17 +45,11 @@ void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALCo Evaluator evaluator(context); CKKSEncoder encoder(context); size_t slot_count = encoder.slot_count(); - // double scale = (double)(n * n); - // double scale = pow(2, 40); - // double scale = pow(2, 30); - double scale = (n > 1024) ? pow(2, 25) : pow(2, 20); + cout << "\nNumber of slots: " << slot_count << "\n" << endl; SecretKey sk = keygen.secret_key(); - if (sk_binfilename.size() == 0) - { - sk_binfilename = dirpath + "sk_" + to_string(degree) + ".dat"; - } + if (sk_binfilename.size() == 0) sk_binfilename = dirpath + "sk_" + to_string(degree) + ".dat"; sk_bin_file_load(sk_binfilename, context, sk); Decryptor decryptor(context, sk); @@ -85,8 +75,9 @@ void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALCo // -- We know this should start out being true assert(pk_wr.is_ntt == true); - bool incl_sp = 1; // Need to read back the special prime - pk_bin_file_load(dirpath, context, pk_wr, incl_sp); + bool incl_sp = 1; // Need to read back the special prime + bool high_byte_first = 0; + pk_bin_file_load(dirpath, context, pk_wr, incl_sp, high_byte_first); print_pk("pk", pk_wr, 8, incl_sp); pk_to_non_ntt_form(context, pk_wr); @@ -111,25 +102,23 @@ void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALCo cout << " Test # " << ntest << endl; cout << "---------------------------------------------" << endl; - // -- Find the expected message from the string file. Comment out if not - // needed. + // -- Find the expected message from the string file. Comment out if not needed. vector values_orig(slot_count, 0); cout << "Reading values from file..." << endl; - filepos = poly_string_file_load(ct_str_file_path, values_orig, 1, filepos); + filepos = poly_string_file_load(ct_str_file_path, 1, values_orig, filepos); // -- Uncomment to check against expected plaintext output (assuming // corresponding lines in seal_embedded.c are also uncommented) - // vector plaintext_vals(slot_count * 2, 0); + // vector plaintext_vals(slot_count*2, 0); // cout << "Reading plaintext values from file..." << endl; - // filepos = poly_string_file_load(ct_str_file_path, plaintext_vals, 1, filepos); + // filepos = poly_string_file_load(ct_str_file_path, 1, plaintext_vals, filepos); // print_poly("\npt ", plaintext_vals, print_size); // -- Uncomment to check against expected plaintext output (assuming // corresponding lines in seal_embedded.c are also uncommented) - // vector plaintext_error_vals(slot_count * 2, 0); + // vector plaintext_error_vals(slot_count*2, 0); // cout << "Reading plaintext error values from file..." << endl; - // filepos = - // poly_string_file_load(ct_str_file_path, plaintext_error_vals, 1, filepos); + // filepos = poly_string_file_load(ct_str_file_path, 1, plaintext_error_vals, filepos); // print_poly("\npte ", plaintext_error_vals, print_size); // -- Read in the ciphertext from the string file @@ -158,6 +147,8 @@ void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALCo // bool are_equal = are_equal_poly(msg_d, values_orig, slot_count); // assert(are_equal); // if (!are_equal) nfailures++; + + // -- Debugging // Plaintext pt_test; // encoder.encode(values_orig, scale, pt_test); // print_poly("pt_test", get_pt_arr_ptr(pt_test), print_size); @@ -177,35 +168,141 @@ void verify_ciphertexts_30bitprime_default(string dirpath, size_t degree, SEALCo } } -// TODO: make this non-main. Need to have a different main? -int main() +int main(int argc, char *argv[]) { - cout << "Parameters: degree 4096, prime bit-lengths: {30, 30, 30, 19}, ntt_form, " - "scale = pow(2, 25)" - << endl; + // -- Instructions: Uncomment one of the below degrees and run + // or specify degree as a command line argument + // size_t degree = 1024; + // size_t degree = 2048; size_t degree = 4096; + // size_t degree = 8192; + // size_t degree = 16384; + + if (argc > 1) + { + std::istringstream ss(argv[1]); + size_t degree_in; + if (!(ss >> degree_in)) { std::cerr << "Invalid number: " << argv[1] << '\n'; } + else if (!ss.eof()) + { + std::cerr << "Trailing characters after number: " << argv[1] << '\n'; + } + assert(degree_in == 1024 || degree_in == 2048 || degree_in == 4096 || degree_in == 8192 || + degree_in == 16384); + degree = degree_in; + } + + double scale = pow(2, 25); + cout << "Parameters: degree " << degree << ", ntt_form, prime bit-lengths: {"; + switch (degree) + { + case 1024: + cout << "27}, scale = pow(2, 20)" << endl; + scale = pow(2, 20); + break; + case 2048: + cout << "27, 27}, scale = pow(2, 25)" << endl; + scale = pow(2, 25); + break; +#ifdef SEALE_DEFAULT_4K_27BIT + case 4096: + cout << "27, 27, 27, 28}, scale = pow(2, 20)" << endl; + scale = pow(2, 20); + break; +#else + case 4096: + cout << "30, 30, 30, 19}, scale = pow(2, 25)" << endl; + scale = pow(2, 25); + break; +#endif + case 8192: + cout << "30 (x6), 38}, scale = pow(2, 25)" << endl; + scale = pow(2, 25); + break; + case 16384: + cout << "30 (x13), 48}, scale = pow(2, 25)" << endl; + scale = pow(2, 25); + break; + // -- 32768 is technically possible, but we do not support it + // case 32768: cout << "30 (x28), 41}, scale = pow(2, 25)" << endl; + // scale = pow(2,25); + // break; + default: cout << "Please choose a valid degree." << endl; throw; + } + + /* + (From SEAL:) + A larger coeff_modulus implies a larger noise budget, hence more encrypted + computation capabilities. However, an upper bound for the total bit-length + of the coeff_modulus is determined by the poly_modulus_degree, as follows: + +----------------------------------------------------+ + | poly_modulus_degree | max coeff_modulus bit-length | + +---------------------+------------------------------+ + | 1024 | 27 | + | 2048 | 54 | + | 4096 | 109 | + | 8192 | 218 | + | 16384 | 438 | + | 32768 | 881 | + +---------------------+------------------------------+ + */ + EncryptionParameters parms(scheme_type::ckks); - SEALContext context = setup_seale_30bitprime_default(degree, parms); + seal::SEALContext context = setup_seale_prime_default(degree, parms); bool sk_generated = false, pk_generated = false; - string sk_fpath = save_dir_path + "sk_" + to_string(degree) + ".dat"; + // -- Testing + // setup_seal_api(1024, {27}, parms); + // setup_seal_api(2048, {27, 27}, parms); + // setup_seal_api(2048, {30, 24}, parms); + // setup_seal_api(4096, {27, 27, 27, 28}, parms); + // setup_seal_api(4096, {30, 30, 30, 19}, parms); + /* + { + vector bit_counts; + for(size_t i = 0; i < 6; i++) bit_counts.push_back(30); + bit_counts.push_back(38); + setup_seal_api(8192, bit_counts, parms); + } + */ + /* + { + for(size_t i = 0; i < 13; i++) bit_counts.push_back(30); + bit_counts.push_back(48); + setup_seal_api(16384, bit_counts, parms); + } + */ + /* + { + for(size_t i = 0; i < 28; i++) bit_counts.push_back(30); + bit_counts.push_back(41); + setup_seal_api(32768, bit_counts, parms); + } + */ + // string str_sk_fpath = save_dir_path + "str_sk_" + to_string(degree) + ".h"; + string sk_fpath = save_dir_path + "sk_" + to_string(degree) + ".dat"; string str_sk_fpath = save_dir_path + "str_sk.h"; string seal_sk_fpath = save_dir_path + "sk_" + to_string(degree) + "_seal" + ".dat"; string seal_pk_fpath = save_dir_path + "pk_" + to_string(degree) + "_seal" + ".dat"; SecretKey sk; - // string err_msg1 = "This option is not yet supported. Please choose another - // option."; + // string err_msg1 = "This option is not yet supported. Please choose another option."; string err_msg2 = "This is not a valid option choice. Please choose a valid option."; + bool is_sym = true; // TODO: Make this command line settable + while (1) { cout << "\nChoose an action:\n"; cout << " 0) Quit\n"; cout << " 1) Generate all objects\n"; - cout << " 2) Verify ciphertexts\n"; + if (is_sym) { cout << " 2) Verify ciphertexts (in symmetric mode) \n"; } + else + { + cout << " 2) Verify ciphertexts (in asymmetric mode)\n"; + } cout << " 3) Generate secret key, public key\n"; cout << " 4) Generate IFFT roots\n"; cout << " 5) Generate fast (a.k.a. \"lazy\") NTT roots\n"; @@ -216,17 +313,15 @@ int main() int option; cin >> option; - bool is_sym = true; // TODO: Make this command line settable - string ct_str_file_path = is_sym ? ct_str_file_path_sym : ct_str_file_path_asym; - - bool use_seal_sk_fpath = true; + bool use_seal_sk_fpath = true; // This is only used when verifying ciphertexts switch (option) { case 0: exit(0); - case 2: - verify_ciphertexts_30bitprime_default(save_dir_path, degree, context, is_sym, - ct_str_file_path); - break; + case 2: { + string ct_str_file_path = is_sym ? ct_str_file_path_sym : ct_str_file_path_asym; + verify_ciphertexts(save_dir_path, scale, degree, context, is_sym, ct_str_file_path); + } + break; case 1: [[fallthrough]]; case 3: cout << "Generating secret key..." << endl; @@ -253,7 +348,6 @@ int main() case 9: gen_save_index_map(save_dir_path, context, 0); break; default: cout << err_msg2 << endl; break; } - exit(0); } return 0; } diff --git a/adapter/convert.cpp b/adapter/convert.cpp index 0937a47..f21c3ce 100644 --- a/adapter/convert.cpp +++ b/adapter/convert.cpp @@ -9,6 +9,7 @@ #include +#include "generate.h" #include "seal/seal.h" #include "utils.h" @@ -24,15 +25,15 @@ void sk_to_ntt_form(const SEALContext &context, SecretKey &sk) // -- This is kind of clunky but we need to do it this way auto &parms_id = (sk.parms_id() == parms_id_zero) ? context.key_parms_id() : sk.parms_id(); - auto sk_context_data_ptr = context.get_context_data(parms_id); - auto &parms = sk_context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t n = parms.poly_modulus_degree(); - size_t coeff_modulus_size = coeff_modulus.size(); - auto ntt_tables_ptr = sk_context_data_ptr->small_ntt_tables(); + auto sk_context_data_ptr = context.get_context_data(parms_id); + auto &parms = sk_context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = sk_context_data_ptr->small_ntt_tables(); RNSIter sk_iter(sk.data().data(), n); - ntt_negacyclic_harvey(sk_iter, coeff_modulus_size, ntt_tables_ptr); + ntt_negacyclic_harvey(sk_iter, nprimes, ntt_tables_ptr); sk.data().parms_id() = context.key_parms_id(); assert(sk.data().is_ntt_form() == true); @@ -46,17 +47,17 @@ void sk_to_non_ntt_form(const SEALContext &context, SecretKey &sk) // -- This is kind of clunky but we need to do it this way auto &parms_id = (sk.parms_id() == parms_id_zero) ? context.key_parms_id() : sk.parms_id(); - auto sk_context_data_ptr = context.get_context_data(parms_id); - auto &parms = sk_context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = parms.poly_modulus_degree(); - auto ntt_tables_ptr = sk_context_data_ptr->small_ntt_tables(); + auto sk_context_data_ptr = context.get_context_data(parms_id); + auto &parms = sk_context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = sk_context_data_ptr->small_ntt_tables(); assert(sk_context_data_ptr == context.key_context_data()); RNSIter sk_itr(sk.data().data(), n); - inverse_ntt_negacyclic_harvey(sk_itr, coeff_modulus_size, ntt_tables_ptr); + inverse_ntt_negacyclic_harvey(sk_itr, nprimes, ntt_tables_ptr); sk.data().parms_id() = parms_id_zero; assert(sk.data().is_ntt_form() == false); @@ -66,52 +67,50 @@ void pk_to_ntt_form(const SEALContext &context, PublicKeyWrapper &pk_wr) { if (pk_wr.is_ntt == true) return; - auto pk_context_data_ptr = context.get_context_data(pk_wr.pk->parms_id()); - auto &parms = pk_context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t n = parms.poly_modulus_degree(); - size_t coeff_modulus_size = coeff_modulus.size(); - auto ntt_tables_ptr = pk_context_data_ptr->small_ntt_tables(); + auto pk_context_data_ptr = context.get_context_data(pk_wr.pk->parms_id()); + auto &parms = pk_context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = pk_context_data_ptr->small_ntt_tables(); assert(pk_context_data_ptr == context.key_context_data()); assert(pk_wr.pk->data().size() == 2); - PolyIter pk_iter(pk_wr.pk->data().data(), n, coeff_modulus_size); + PolyIter pk_iter(pk_wr.pk->data().data(), n, nprimes); ntt_negacyclic_harvey(pk_iter, pk_wr.pk->data().size(), ntt_tables_ptr); pk_wr.is_ntt = true; // assert(pk_wr.pk->data().is_ntt_form() == true); // -- Note: We cannot use pk_wr.pk->data().is_ntt_form() directly because this value - // is not - // directly changeable for ciphertexts (and therefore public keys). It is only - // changeable by calling evaluator.transform_to_ntt(), but this function - // throws an exception when you try to give it a public key. + // is not directly changeable for ciphertexts (and therefore public keys). + // It is only changeable by calling evaluator.transform_to_ntt(), but this + // function throws an exception when you try to give it a public key. } void pk_to_non_ntt_form(const SEALContext &context, PublicKeyWrapper &pk_wr) { if (pk_wr.is_ntt == false) return; - auto pk_context_data_ptr = context.get_context_data(pk_wr.pk->parms_id()); - auto &parms = pk_context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t n = parms.poly_modulus_degree(); - size_t coeff_modulus_size = coeff_modulus.size(); - auto ntt_tables_ptr = pk_context_data_ptr->small_ntt_tables(); + auto pk_context_data_ptr = context.get_context_data(pk_wr.pk->parms_id()); + auto &parms = pk_context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = pk_context_data_ptr->small_ntt_tables(); assert(pk_context_data_ptr == context.key_context_data()); assert(pk_wr.pk->data().size() == 2); - PolyIter pk_iter(pk_wr.pk->data().data(), n, coeff_modulus_size); + PolyIter pk_iter(pk_wr.pk->data().data(), n, nprimes); inverse_ntt_negacyclic_harvey(pk_iter, pk_wr.pk->data().size(), ntt_tables_ptr); pk_wr.is_ntt = false; // assert(pk_wr.pk->data().is_ntt_form() == false); // -- Note: We cannot use pk_wr.pk->data().is_ntt_form() directly because this value - // is not - // directly changeable for ciphertexts (and therefore public keys). It is only - // changeable by calling evaluator.transform_to_ntt(), but this function - // throws an exception when you try to give it a public key + // is not directly changeable for ciphertexts (and therefore public keys). + // It is only changeable by calling evaluator.transform_to_ntt(), but this + // function throws an exception when you try to give it a public key. } void ct_to_ntt_form(Evaluator &evaluator, Ciphertext &c_in) @@ -133,16 +132,16 @@ void pt_to_ntt_form(const SEALContext &context, Plaintext &pt) bool is_ntt = pt.is_ntt_form(); if (is_ntt == true) return; - // auto context_data_ptr = context.key_context_data(); - auto context_data_ptr = context.first_context_data(); - auto &parms = context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = parms.poly_modulus_degree(); - auto ntt_tables_ptr = context_data_ptr->small_ntt_tables(); + // auto context_data_ptr = context.key_context_data(); + auto context_data_ptr = context.first_context_data(); + auto &parms = context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = context_data_ptr->small_ntt_tables(); RNSIter plaintext(pt.data(), n); - ntt_negacyclic_harvey(plaintext, coeff_modulus_size, ntt_tables_ptr); + ntt_negacyclic_harvey(plaintext, nprimes, ntt_tables_ptr); pt.parms_id() = context_data_ptr->parms_id(); assert(pt.is_ntt_form() == true); @@ -153,15 +152,15 @@ void pt_to_non_ntt_form(const SEALContext &context, Plaintext &pt) bool is_ntt = pt.is_ntt_form(); if (is_ntt == false) return; - auto context_data_ptr = context.first_context_data(); - auto &parms = context_data_ptr->parms(); - auto &coeff_modulus = parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = parms.poly_modulus_degree(); - auto ntt_tables_ptr = context_data_ptr->small_ntt_tables(); + auto context_data_ptr = context.first_context_data(); + auto &parms = context_data_ptr->parms(); + auto &coeff_modulus = parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = parms.poly_modulus_degree(); + auto ntt_tables_ptr = context_data_ptr->small_ntt_tables(); RNSIter plaintext(pt.data(), n); - inverse_ntt_negacyclic_harvey(plaintext, coeff_modulus_size, ntt_tables_ptr); + inverse_ntt_negacyclic_harvey(plaintext, nprimes, ntt_tables_ptr); pt.parms_id() = parms_id_zero; assert(pt.is_ntt_form() == false); diff --git a/adapter/convert.h b/adapter/convert.h index 2f8eb17..6185b5c 100644 --- a/adapter/convert.h +++ b/adapter/convert.h @@ -4,15 +4,13 @@ /** @file convert.h -@brief Functions to convert secret keys, public keys, and ciphertexts to NTT or non-NTT -form, and compare secret key and public key instances. +@brief Functions to convert secret keys, public keys, and ciphertexts to NTT or non-NTT form, and +compare secret key and public key instances. */ #pragma once -#include - -#include "generate.h" +#include "generate.h" // PublicKeyWrapper #include "seal/seal.h" /** @@ -82,14 +80,14 @@ void pt_to_non_ntt_form(const seal::SEALContext &context, seal::Plaintext &pt); /** Compares two secret key instances to see if they match. -Note: This function may modify SecretKey instances to compare NTT and non-NTT forms of the -secret key, but should revert all changes before returning. +Note: This function may modify SecretKey instances to compare NTT and non-NTT forms of the secret +key, but should revert all changes before returning. @param[in] context SEAL context @param[in] sk1 Secret key 1 @param[in] sk2 Secret key 2 @param[in] incl_sp If true, compares "special prime" component of secret keys -@param[in] should_match If true, throws an error if sk1 != equal sk2 (debugging only) +@param[in] should_match If true, throws an error if sk1 != sk2 (debugging only) */ void compare_sk(const seal::SEALContext &context, seal::SecretKey &sk1, seal::SecretKey &sk2, bool incl_sp, bool should_match); @@ -97,8 +95,8 @@ void compare_sk(const seal::SEALContext &context, seal::SecretKey &sk1, seal::Se /** Compares two public key instances to see if they match. -Note: This function may modify PublicKeyWrapper instances to compare NTT and non-NTT forms -of the public key, but should revert all changes before returning. +Note: This function may modify PublicKeyWrapper instances to compare NTT and non-NTT forms of the +public key, but should revert all changes before returning. @param[in] context SEAL context @param[in] pk1_wr Public key 1 wrapper diff --git a/adapter/fileops.cpp b/adapter/fileops.cpp index 0560b80..3764ac6 100644 --- a/adapter/fileops.cpp +++ b/adapter/fileops.cpp @@ -19,7 +19,6 @@ using namespace std; using namespace seal; using namespace seal::util; -// #define HIGH_BYTE_FIRST // big endian // #define DEBUG_EASYMOD // See: SEAL-Embedded for explanation // =============================================================== @@ -41,20 +40,20 @@ void sk_bin_file_save(string fpath, string str_fpath, const SEALContext &context assert(sk.data().is_ntt_form() == false); } - fstream outfile(fpath.c_str(), ios::out | ios::binary | ios::trunc); - fstream outfile2; + fstream file(fpath.c_str(), ios::out | ios::binary | ios::trunc); + fstream file2; if (use_str_fpath) { - outfile2.open(str_fpath.c_str(), ios::out | ios::trunc); - outfile2 << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " - "defined(SE_DATA_FROM_CODE_DIRECT)\n"; - outfile2 << "\n#include \n\n"; + file2.open(str_fpath.c_str(), ios::out | ios::trunc); + file2 << "#pragma once\n\n#include \"defines.h\"\n\n"; + file2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " + "defined(SE_DATA_FROM_CODE_DIRECT)\n"; + file2 << "\n#include \n\n"; // -- # bytes = n vals * (2 bits / val) * (1 byte / 8 bits) size_t nbytes = n / 4; - outfile2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; - outfile2 << "// -- Secret key for polynomial ring degree = " << n << "\n"; - outfile2 << "uint8_t secret_key[" << nbytes << "] = { "; + file2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; + file2 << "// -- Secret key for polynomial ring degree = " << n << "\n"; + file2 << "uint8_t secret_key[" << nbytes << "] = { "; } for (size_t i = 0; i < n; i += 4) { @@ -62,7 +61,7 @@ void sk_bin_file_save(string fpath, string str_fpath, const SEALContext &context for (size_t j = 0; ((i + j) < n) && (j < 4); j++) { uint64_t sk_val = sk_ptr[i + j]; - if (i < 8) cout << "sk_val: " << sk_val << endl; + // if (i < 8) cout << "sk_val: " << sk_val << endl; #ifdef DEBUG_EASYMOD // -- Mapping: 0 --> 0, 1 --> 1, q-1 --> 2 uint8_t val = (sk_val > 1) ? 2 : (uint8_t)sk_val; @@ -73,21 +72,28 @@ void sk_bin_file_save(string fpath, string str_fpath, const SEALContext &context #endif byte |= val << (6 - 2 * j); } - // TODO: make this more compact? - outfile.put(static_cast(byte)); + file.put(static_cast(byte)); if (use_str_fpath) { // cout << "byte: " << to_string(byte) << endl; string next_str = ((i + 4) < n) ? ", " : "};\n"; - outfile2 << to_string(byte) + next_str; - if (!(i % 64) && i) outfile2 << "\n"; + assert((byte & 0b11) != 0b11); + assert(((byte >> 2) & 0b11) != 0b11); + assert(((byte >> 4) & 0b11) != 0b11); + assert(((byte >> 6) & 0b11) != 0b11); + if (byte < 10) + file2 << " "; + else if (byte < 100) + file2 << " "; + file2 << to_string(byte) + next_str; + if (!(i % 64)) file2 << "\n"; } } - outfile.close(); + file.close(); if (use_str_fpath) { - outfile2 << "#endif" << endl; - outfile2.close(); + file2 << "#endif" << endl; + file2.close(); } if (is_ntt) @@ -99,12 +105,12 @@ void sk_bin_file_save(string fpath, string str_fpath, const SEALContext &context void sk_bin_file_load(string fpath, const SEALContext &context, SecretKey &sk) { - auto &sk_parms = context.key_context_data()->parms(); - auto &coeff_modulus = sk_parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = sk_parms.poly_modulus_degree(); - auto sk_ptr = get_sk_arr_ptr(sk); - bool is_ntt = sk.data().is_ntt_form(); + auto &sk_parms = context.key_context_data()->parms(); + auto &coeff_modulus = sk_parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = sk_parms.poly_modulus_degree(); + auto sk_ptr = get_sk_arr_ptr(sk); + bool is_ntt = sk.data().is_ntt_form(); // -- Make sure sk is not in NTT form if (is_ntt) @@ -114,11 +120,11 @@ void sk_bin_file_load(string fpath, const SEALContext &context, SecretKey &sk) } // print_poly("sk_before ", sk_ptr, 8); - fstream outfile(fpath.c_str(), ios::in | ios::binary); + fstream file(fpath.c_str(), ios::in | ios::binary); for (size_t i = 0; i < n; i += 4) { char byte; - outfile.get(byte); + file.get(byte); // cout << "byte[" << i << "]: " << hex << unsigned(byte & 0xFF) << dec << endl; for (size_t j = 0; ((i + j) < n) && (j < 4); j++) { @@ -128,35 +134,28 @@ void sk_bin_file_load(string fpath, const SEALContext &context, SecretKey &sk) // -- Mapping: 0 --> 0, 1 --> 1, 2 --> q-1 if (val > 1) { - for (size_t k = 0; k < coeff_modulus_size; k++) - { - sk_ptr[(i + j) + k * n] = coeff_modulus[k].value() - 1; - } + for (size_t k = 0; k < nprimes; k++) + { sk_ptr[(i + j) + k * n] = coeff_modulus[k].value() - 1; } } else { - for (size_t k = 0; k < coeff_modulus_size; k++) { sk_ptr[(i + j) + k * n] = val; } + for (size_t k = 0; k < nprimes; k++) { sk_ptr[(i + j) + k * n] = val; } } #else // -- Mapping: 0 --> q-1, 1 --> 0, 2 --> 1 if (val > 0) { - for (size_t k = 0; k < coeff_modulus_size; k++) - { - sk_ptr[(i + j) + k * n] = val - 1; - } + for (size_t k = 0; k < nprimes; k++) { sk_ptr[(i + j) + k * n] = val - 1; } } else { - for (size_t k = 0; k < coeff_modulus_size; k++) - { - sk_ptr[(i + j) + k * n] = coeff_modulus[k].value() - 1; - } + for (size_t k = 0; k < nprimes; k++) + { sk_ptr[(i + j) + k * n] = coeff_modulus[k].value() - 1; } } #endif } } - outfile.close(); + file.close(); // print_poly("sk_after ", sk_ptr, n*4); // print_poly("sk_after ", sk_ptr, 8); // print_poly("sk_after ", sk_ptr + n, 8); @@ -172,66 +171,74 @@ void sk_bin_file_load(string fpath, const SEALContext &context, SecretKey &sk) } void pk_bin_file_save(string dirpath, const SEALContext &context, PublicKeyWrapper &pk_wr, - bool incl_sp, bool append) + bool incl_sp, bool high_byte_first, bool append) { - bool large_modulus = 0; - bool is_ntt = pk_wr.is_ntt; + bool is_ntt = pk_wr.is_ntt; assert(is_ntt); // Make sure we start out in ntt form assert(pk_wr.pk); - bool incl_sp_sf = false; // No need to include the special prime for the headers + size_t n = pk_wr.pk->data().poly_modulus_degree(); + size_t nprimes = pk_wr.pk->data().coeff_modulus_size(); - size_t n = pk_wr.pk->data().poly_modulus_degree(); - size_t coeff_modulus_size = pk_wr.pk->data().coeff_modulus_size(); - assert(coeff_modulus_size >= 2); + // -- No need to include the special prime for the headers + bool incl_sp_sf = (nprimes == 1) ? true : false; string fpath3 = dirpath + "str_pk_addr_array.h"; - fstream outfile3(fpath3.c_str(), ios::out | ios::trunc); + fstream file3(fpath3.c_str(), ios::out | ios::trunc); + size_t string_file_nprimes = incl_sp_sf ? nprimes : nprimes - 1; - outfile3 << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile3 << "#if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT)\n\n"; + file3 << "#pragma once\n\n#include \"defines.h\"\n\n"; + file3 << "#if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT)\n\n"; stringstream pk_addr_str; - - size_t string_file_coeff_modulus_size = - incl_sp_sf ? coeff_modulus_size : coeff_modulus_size - 1; - pk_addr_str << "ZZ* pk_prime_addr[" << string_file_coeff_modulus_size << "][2] = \n{\n"; + pk_addr_str << "ZZ* pk_prime_addr[" << string_file_nprimes << "][2] = \n{\n"; for (size_t outer = 0; outer < 2; outer++) { assert(pk_wr.pk->data().size() == 2); assert(sizeof(pk_wr.pk->data().data()[0]) == sizeof(uint64_t)); - for (size_t t = 0; t < coeff_modulus_size; t++) + for (size_t t = 0; t < nprimes; t++) { + auto q = context.key_context_data()->parms().coeff_modulus()[t].value(); + bool large_modulus = (log2(q) > 32) ? true : false; + + assert(((t <= (nprimes - 1)) && (log2(q) <= 30)) || + ((t == nprimes - 1) && (log2(q) <= 64) && (nprimes != 1))); + for (size_t k = 0; k < 2; k++) // pk0, pk1 { string fpath_common = "pk" + to_string(k) + "_"; if (pk_wr.is_ntt) fpath_common += "ntt_"; - - auto coeff_modulus = context.key_context_data()->parms().coeff_modulus()[t].value(); - fpath_common += to_string(n) + "_" + to_string(coeff_modulus); + fpath_common += to_string(n) + "_" + to_string(q); string fpath = dirpath + fpath_common + ".dat"; string fpath2 = dirpath + "str_" + fpath_common + ".h"; cout << "writing to files: " << fpath << ", " << fpath2 << endl; - if (outer == 0 && t < string_file_coeff_modulus_size) - { - outfile3 << " #include \"str_" << fpath_common + ".h\"" << endl; - } + if (outer == 0 && t < string_file_nprimes) + { file3 << " #include \"str_" << fpath_common + ".h\"" << endl; } auto open_mode_type = ios::out | ios::binary | (append ? ios::app : ios::trunc); - fstream outfile(fpath.c_str(), open_mode_type); + fstream file(fpath.c_str(), open_mode_type); - fstream outfile2(fpath2.c_str(), ios::out | ios::trunc); - size_t num_ZZ_elements = n; - outfile2 << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " - "defined(SE_DATA_FROM_CODE_DIRECT)\n"; - outfile2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; - outfile2 << "ZZ pk" + to_string(k) + "_prime" << to_string(t); - outfile2 << "[" << num_ZZ_elements << "] = { \n"; + fstream file2(fpath2.c_str(), ios::out | ios::trunc); + size_t nvals = n; + file2 << "#pragma once\n\n#include \"defines.h\"\n\n"; + if (large_modulus) + { + file2 << "// -- Note: This file uses >30-bit primes and cannot"; + file2 << " be used with the SEAL-Embedded device library." << endl; + } + file2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " + "defined(SE_DATA_FROM_CODE_DIRECT)\n"; + file2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; + if (large_modulus) + file2 << "uint64_t "; + else + file2 << "ZZ "; + file2 << "pk" + to_string(k) + "_prime" << to_string(t); + file2 << "[" << nvals << "] = { \n"; uint64_t *ptr = get_pk_arr_ptr(pk_wr, k); for (size_t i = 0; i < n; i++) @@ -241,38 +248,38 @@ void pk_bin_file_save(string dirpath, const SEALContext &context, PublicKeyWrapp for (size_t j = 0; j < stop_j; j++) // write one byte at a time { // -- & with 0xFF to prevent 'byte' from being sign extended -#ifdef HIGH_BYTE_FIRST - uint8_t byte = (data >> (56 - 8 * j)) & 0xFF; -#else - uint8_t byte = (data >> (8 * j)) & 0xFF; - outfile.put((char)byte); -#endif + uint8_t byte = 0; + if (high_byte_first) { byte = (data >> (56 - 8 * j)) & 0xFF; } + else + { + byte = (data >> (8 * j)) & 0xFF; + file.put((char)byte); + } } // -- Write to string file uint32_t val = data & 0xFFFFFFFF; string next_str = ((i + 1) < n) ? ", " : "};\n"; - outfile2 << hex << "0x" << (large_modulus ? data : val) << next_str; + file2 << std::hex << "0x" << (large_modulus ? data : val) << std::dec + << next_str; size_t row_break = large_modulus ? 4 : 8; - if (!(i % row_break)) outfile2 << "\n"; + if (!(i % row_break)) file2 << "\n"; } - outfile.close(); - outfile2 << "#endif" << endl; - outfile2.close(); + file.close(); + file2 << "#endif" << endl; + file2.close(); } - // TODO: get rid of special prime stuff since this must be included as - // part of the three primes... // -- No need to include special prime in string files - if (outer == 0 && t < string_file_coeff_modulus_size) + if (outer == 0 && t < string_file_nprimes) { pk_addr_str << " {&(pk0_prime" << to_string(t) << "[0]),"; pk_addr_str << " &(pk1_prime" << to_string(t) << "[0])}"; - if (t == string_file_coeff_modulus_size - 1) + if (t == string_file_nprimes - 1) pk_addr_str << "\n};" << endl; else pk_addr_str << "," << endl; } - if (!incl_sp && t == (coeff_modulus_size - 2)) break; + if (!incl_sp && t == (nprimes - 2)) break; } if (pk_wr.is_ntt) // Convert and write in non-ntt form @@ -280,12 +287,12 @@ void pk_bin_file_save(string dirpath, const SEALContext &context, PublicKeyWrapp pk_to_non_ntt_form(context, pk_wr); assert(pk_wr.is_ntt == false); } - if (outer == 0) { outfile3 << "\n"; } + if (outer == 0) file3 << "\n"; } - outfile3 << pk_addr_str.str(); - outfile3 << "#endif" << endl; - outfile3.close(); + file3 << pk_addr_str.str(); + file3 << "#endif" << endl; + file3.close(); if (is_ntt) { @@ -295,10 +302,9 @@ void pk_bin_file_save(string dirpath, const SEALContext &context, PublicKeyWrapp } void pk_bin_file_load(string dirpath, const SEALContext &context, PublicKeyWrapper &pk_wr, - bool incl_sp) + bool incl_sp, bool high_byte_first) { - bool is_ntt = pk_wr.is_ntt; - bool large_modulus = 0; + bool is_ntt = pk_wr.is_ntt; assert(pk_wr.pk); if (is_ntt) @@ -310,19 +316,22 @@ void pk_bin_file_load(string dirpath, const SEALContext &context, PublicKeyWrapp assert(pk_wr.pk->data().size() == 2); assert(sizeof(pk_wr.pk->data().data()[0]) == sizeof(uint64_t)); - size_t n = pk_wr.pk->data().poly_modulus_degree(); - size_t coeff_modulus_size = pk_wr.pk->data().coeff_modulus_size(); - assert(coeff_modulus_size >= 2); + size_t n = pk_wr.pk->data().poly_modulus_degree(); + size_t nprimes = pk_wr.pk->data().coeff_modulus_size(); - for (size_t t = 0; t < coeff_modulus_size; t++) + for (size_t t = 0; t < nprimes; t++) { + auto q = context.key_context_data()->parms().coeff_modulus()[t].value(); + bool large_modulus = (log2(q) > 32) ? true : false; + + assert(((t <= (nprimes - 1)) && (log2(q) <= 30)) || + ((t == nprimes - 1) && (log2(q) <= 64) && (nprimes != 1))); + for (size_t k = 0; k < 2; k++) { string fpath = dirpath + "pk" + to_string(k) + "_"; if (pk_wr.is_ntt) fpath += "ntt_"; - - auto coeff_modulus = context.key_context_data()->parms().coeff_modulus()[t].value(); - fpath += to_string(n) + "_" + to_string(coeff_modulus) + ".dat"; + fpath += to_string(n) + "_" + to_string(q) + ".dat"; cout << "reading from file: " << fpath << endl; fstream infile(fpath.c_str(), ios::in | ios::binary); @@ -338,18 +347,21 @@ void pk_bin_file_load(string dirpath, const SEALContext &context, PublicKeyWrapp infile.get(byte); // -- & with 0xFF to prevent 'byte' from being sign extended -#ifdef HIGH_BYTE_FIRST - data <<= 8; - data |= (byte & 0xFF); -#else - data |= ((uint64_t)(byte & 0xFF) << 8 * j); -#endif + if (high_byte_first) + { + data <<= 8; + data |= (byte & 0xFF); + } + else + { + data |= ((uint64_t)(byte & 0xFF) << 8 * j); + } } ptr[i + t * n] = data; } infile.close(); } - if (!incl_sp && t == (coeff_modulus_size - 2)) break; + if (!incl_sp && t == (nprimes - 2)) break; } if (is_ntt) @@ -424,12 +436,12 @@ void pk_seal_load(string fpath, const SEALContext &context, PublicKey &pk) streampos sk_string_file_load(string fpath, const SEALContext &context, SecretKey &sk) { - auto &sk_parms = context.key_context_data()->parms(); - auto &coeff_modulus = sk_parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = sk_parms.poly_modulus_degree(); - auto sk_ptr = get_sk_arr_ptr(sk); - bool is_ntt = sk.data().is_ntt_form(); + auto &sk_parms = context.key_context_data()->parms(); + auto &coeff_modulus = sk_parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = sk_parms.poly_modulus_degree(); + auto sk_ptr = get_sk_arr_ptr(sk); + bool is_ntt = sk.data().is_ntt_form(); // -- Make sure sk is not in NTT form // -- Note: this shouldn't run if we don't generate sk using keygenerator @@ -439,7 +451,7 @@ streampos sk_string_file_load(string fpath, const SEALContext &context, SecretKe assert(sk.data().is_ntt_form() == false); } - auto filepos = poly_string_file_load(fpath, sk_ptr, 1); + auto filepos = poly_string_file_load(fpath, 1, sk_ptr); for (size_t i = 0; i < n; i++) { @@ -447,26 +459,22 @@ streampos sk_string_file_load(string fpath, const SEALContext &context, SecretKe #ifdef DEBUG_EASYMOD if (val > 1) { - for (size_t j = 0; j < coeff_modulus_size; j++) - { - sk_ptr[i + j * n] = coeff_modulus[j].value() - 1; - } + for (size_t j = 0; j < nprimes; j++) + { sk_ptr[i + j * n] = coeff_modulus[j].value() - 1; } } else { - for (size_t j = 0; j < coeff_modulus_size; j++) { sk_ptr[i + j * n] = val; } + for (size_t j = 0; j < nprimes; j++) { sk_ptr[i + j * n] = val; } } #else if (val > 0) { - for (size_t j = 0; j < coeff_modulus_size; j++) { sk_ptr[i + j * n] = val - 1; } + for (size_t j = 0; j < nprimes; j++) { sk_ptr[i + j * n] = val - 1; } } else { - for (size_t j = 0; j < coeff_modulus_size; j++) - { - sk_ptr[i + j * n] = coeff_modulus[j].value() - 1; - } + for (size_t j = 0; j < nprimes; j++) + { sk_ptr[i + j * n] = coeff_modulus[j].value() - 1; } } #endif } @@ -484,14 +492,12 @@ streampos sk_string_file_load(string fpath, const SEALContext &context, SecretKe streampos ct_string_file_load(string fpath, const SEALContext &context, Evaluator &evaluator, Ciphertext &ct, streampos filepos_in) { - auto &ct_parms = context.first_context_data()->parms(); - auto &ct_parms_id = context.first_parms_id(); - size_t coeff_modulus_size = ct_parms.coeff_modulus().size(); - size_t n = ct_parms.poly_modulus_degree(); - bool is_ntt = ct.is_ntt_form(); - + auto &ct_parms = context.first_context_data()->parms(); + auto &ct_parms_id = context.first_parms_id(); + size_t ct_nprimes = ct_parms.coeff_modulus().size(); + size_t n = ct_parms.poly_modulus_degree(); + bool is_ntt = ct.is_ntt_form(); assert(is_ntt); - assert(coeff_modulus_size == 3); // -- Ciphertext has two components ct.resize(context, ct_parms_id, 2); @@ -506,17 +512,17 @@ streampos ct_string_file_load(string fpath, const SEALContext &context, Evaluato vector ct_temp_1p(2 * n); streampos filepos = filepos_in; - for (size_t j = 0; j < coeff_modulus_size; j++) + for (size_t j = 0; j < ct_nprimes; j++) { // -- Load in two components at a time - filepos = poly_string_file_load(fpath, ct_temp_1p, 2, filepos); + filepos = poly_string_file_load(fpath, 2, ct_temp_1p, filepos); auto ct_ptr = get_ct_arr_ptr(ct); // -- Set c0 and c1 terms for (size_t i = 0; i < n; i++) { - ct_ptr[i + j * n] = ct_temp_1p[i]; - ct_ptr[i + j * n + coeff_modulus_size * n] = ct_temp_1p[i + n]; + ct_ptr[i + j * n] = ct_temp_1p[i]; + ct_ptr[i + j * n + ct_nprimes * n] = ct_temp_1p[i + n]; } } print_ct(ct, 8); diff --git a/adapter/fileops.h b/adapter/fileops.h index 767aab0..f1633e7 100644 --- a/adapter/fileops.h +++ b/adapter/fileops.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "seal/seal.h" #include "utils.h" @@ -34,8 +35,8 @@ inline std::size_t size_of_file(std::fstream &file) } /** -Exits if a file is not open or on file failure. Optionally checks that the file is not -empty. If failure is detected, closes file and exits. +Exits if a file is not open or on file failure. Optionally checks that the file is not empty. +If failure is detected, closes file and exits. @param[in] file File to check @param[in] msg Message to print on exit @@ -71,18 +72,17 @@ inline void exit_on_err_file(std::fstream &file, std::string msg, bool check_siz // =============================================================== /** -Saves the secret key to a binary file. Optionally also creates a code file containing the -hard-coded values of the secret key. This file may then be compiled with the SEAL-Embedded -library. +Saves the secret key to a binary file. Optionally also creates a code file containing the hard-coded +values of the secret key. This file may then be compiled with the SEAL-Embedded library. Note: This stores the secret key in **compressed** form. -Note: This function may modify sk in order to write to file, but should revert all changes -before returning. +Note: This function may modify sk in order to write to file, but should revert all changes before +returning. @param[in] fpath Path to file to save secret key values in binary form -@param[in] str_fpath Path to file to hard-code secret key bytes in a code file for -SEAL-Embedded +@param[in] str_fpath Path to file to hard-code secret key bytes in a code file for use with + SEAL-Embedded @param[in] context SEAL context @param[in] use_str_fpath If true, hard codes secret key bytes to file at str_fpath @param[in] sk Secret key instance @@ -90,9 +90,8 @@ SEAL-Embedded void sk_bin_file_save(std::string fpath, std::string str_fpath, const seal::SEALContext &context, bool use_str_fpath, seal::SecretKey &sk); -// TODO: this currently includes the extra prime... /** -Loads the secret key from a binary file generating using 'sk_bin_file_save'. +Loads the secret key from a binary file generated using 'sk_bin_file_save'. Note: This assumes the secret key was saved in **compressed** form. @@ -102,19 +101,31 @@ Note: This assumes the secret key was saved in **compressed** form. */ void sk_bin_file_load(std::string fpath, const seal::SEALContext &context, seal::SecretKey &sk); +/** +Saves the public key to a binary file. Also creates a code file containing the hard-coded values of +the public key to compile with the SEAL-Embedded library. + +@param[in] dirpath Path to directory to save public key file +@param[in] context SEAL context +@param[in] pk_wr Public key wrapper instance +@param[in] incl_sp If true, writes bytes for special prime as well +@param[in] high_byte_first Toggle for endianness +@param[in] append If true, will append values to binary file. Otherwise, will overwrite +*/ void pk_bin_file_save(std::string dirpath, const seal::SEALContext &context, - PublicKeyWrapper &pk_wr, bool incl_sp, bool append = 0); + PublicKeyWrapper &pk_wr, bool incl_sp, bool high_byte_first, bool append = 0); /** Loads a public key from a SEAL-Embedded-formatted binary file. -@param[in] dirpath Path to directory containing public key file -@param[in] context SEAL context -@param[in, out] pk_wr Public key wrapper instance -@param[in] inc_sp If true, reads in bytes for special prime as well +@param[in] dirpath Path to directory containing public key file +@param[in] context SEAL context +@param[out] pk_wr Public key wrapper instance +@param[in] incl_sp If true, reads in bytes for special prime as well +@param[in] high_byte_first Toggle for endianness */ void pk_bin_file_load(std::string dirpath, const seal::SEALContext &context, - PublicKeyWrapper &pk_wr, bool incl_special_prime); + PublicKeyWrapper &pk_wr, bool incl_sp, bool high_byte_first); // ============================================================== // Binary file save/load @@ -124,7 +135,7 @@ void pk_bin_file_load(std::string dirpath, const seal::SEALContext &context, /** Saves a secret key to a binary file in SEAL form. -@param[in] fpath Binary file to save the secret key +@param[in] fpath Path to binary file to save the secret key @param[in] sk Secret key instance @param[in] compress If true, compresses the secret key w/ zstd before saving */ @@ -133,7 +144,7 @@ void sk_seal_save(std::string fpath, seal::SecretKey &sk, bool compress = true); /** Loads a secret key from a SEAL-formatted binary file. -@param[in] fpath Binary file containing secret key in SEAL form +@param[in] fpath Path to binary file containing secret key in SEAL form @param[in] context SEAL context @param[out] sk Secret key instance */ @@ -142,7 +153,7 @@ void sk_seal_load(std::string fpath, const seal::SEALContext &context, seal::Sec /** Saves a public key to a binary file in SEAL form. -@param[in] fpath Binary file to save the public key +@param[in] fpath Path to binary file to save the public key @param[in] pk Public key instance @param[in] compress If true, compresses the public key w/ zstd before saving */ @@ -151,59 +162,68 @@ void pk_seal_save(std::string fpath, seal::PublicKey &pk, bool compress = true); /** Loads a public key from a SEAL-formatted binary file. -@param[in] fpath Binary file containing public key in SEAL form +@param[in] fpath Path to binary file containing public key in SEAL form @param[in] context SEAL context @param[out] pk Public key instance */ void pk_seal_load(std::string fpath, const seal::SEALContext &context, seal::PublicKey &pk); // ============================================================== -// std::string file save/load +// String file save/load // (Mainly for debugging) // ============================================================== /** - */ +Load a secret key from a string file. + +@param[in] fpath Path to string file containing secret key +@param[in] context SEAL context +@param[out] sk Secret key object to store loaded values +@return Position of file pointer after reading in the secret key +*/ std::streampos sk_string_file_load(std::string fpath, const seal::SEALContext &context, seal::SecretKey &sk); -// This is not memory efficient at all -// File should be in the following format: -// This is just for testing, so can be slow -// File must follow a format similar to: -// ct0 : { x, x, x, x, x} -// ct1 : { x, x, x, x, x} -// ct0 : { x, x, x, x, x} --> w.r.t. next prime -// ct1 : { x, x, x, x, x} --> w.r.t. next prime -// ... - /** -This will call poly_string_file_load +Load a freshly encrypted ciphertext from a string file. +CT objects should be formatted as follows: + +ct0 : { x, x, x, x, x} +ct1 : { x, x, x, x, x} +ct0 : { x, x, x, x, x} --> w.r.t. next prime +ct1 : { x, x, x, x, x} --> w.r.t. next prime + +@param[in] fpath Path to string file containing values of ciphertext. +@param[in] context SEAL context +@param[in] evaluator SEAL evaluator +@param[out] ct Ciphertext object to load values into +@param[in] filepos_in Previous returned value from calling this function +@return Position of file pointer after reading in a single ciphertext */ std::streampos ct_string_file_load(std::string fpath, const seal::SEALContext &context, seal::Evaluator &evaluator, seal::Ciphertext &ct, std::streampos filepos_in = 0); -// This is just for testing, so can be slow -// std::string must follow a format similar to: -// Example file: -// ct0 : { x, x, x, x, x} -// ct1 : { x, x, x, x, x} -// ct0 : { x, x, x, x, x} -// ct1 : { x, x, x, x, x} -// ... - /** -@returns End position of ftell after reading 'num_elements' type-T elements of 'invec' -starting from pos +Load a polynomial object from a string file. +String file objects should be formatted as follows (values in [] are optional): + + : {, , ...} + +@param[in] fpath Path to string file containing values of ciphertext. +@param[in] ncomponents Number of components of the object (e.g. 2 for a fresh CT) +@param[out] vec Pointer to memory to store polynomial elements +@param[in] pos Position of file to start reading from +@returns End position of ftell after reading 'num_elements' type-T elements of 'invec' starting from +pos */ template -std::streampos poly_string_file_load(std::string fname, T *invec, std::size_t ncomponents, +std::streampos poly_string_file_load(std::string fpath, std::size_t ncomponents, T *vec, std::streampos pos = 0) { std::vector vec_temp; - std::fstream infile(fname.c_str(), std::ios::in); - std::cout << std::endl << "opening file at: " << fname << std::endl << std::endl; + std::fstream infile(fpath.c_str(), std::ios::in); + std::cout << std::endl << "opening file at: " << fpath << std::endl << std::endl; assert(infile.is_open()); char ch; @@ -232,9 +252,7 @@ std::streampos poly_string_file_load(std::string fname, T *invec, std::size_t nc T val_temp; if (std::is_same::value) - { - val_temp = static_cast(std::stod(val.c_str(), 0)); - } + { val_temp = static_cast(std::stod(val.c_str(), 0)); } else if (std::is_same::value) { val_temp = static_cast(std::strtoull(val.c_str(), 0, 10)); @@ -258,7 +276,7 @@ std::streampos poly_string_file_load(std::string fname, T *invec, std::size_t nc // -- Copy read elements into input vector at specific location std::size_t N = vec_temp.size(); // print_poly("vec_temp", vec_temp, N); - for (std::size_t i = 0; i < N; i++) { invec[i + idx * N] = vec_temp[i]; } + for (std::size_t i = 0; i < N; i++) { vec[i + idx * N] = vec_temp[i]; } idx++; } std::streampos curr_pos = infile.tellg(); @@ -268,9 +286,22 @@ std::streampos poly_string_file_load(std::string fname, T *invec, std::size_t nc return curr_pos; } +/** +Load a polynomial object from a string file. +String file objects should be formatted as follows: + + : {, , ...} + +@param[in] fpath Path to string file containing values of ciphertext. +@param[in] ncomponents Number of components of the object (e.g. 2 for a fresh CT) +@param[out] vec Vector object to store polynomial values +@param[in] pos Position of file to start reading from +@returns End position of ftell after reading 'num_elements' type-T elements of 'invec' starting from +pos +*/ template -std::streampos poly_string_file_load(std::string fname, std::vector &invec, - std::size_t ncomponents, std::streampos pos = 0) +std::streampos poly_string_file_load(std::string fpath, std::size_t ncomponents, + std::vector &vec, std::streampos pos = 0) { - return poly_string_file_load(fname, &(invec[0]), ncomponents, pos); + return poly_string_file_load(fpath, ncomponents, &(vec[0]), pos); } diff --git a/adapter/generate.cpp b/adapter/generate.cpp index a996896..9b39821 100644 --- a/adapter/generate.cpp +++ b/adapter/generate.cpp @@ -21,8 +21,24 @@ using namespace std; using namespace seal; using namespace seal::util; +uint64_t endian_flip(uint64_t a) +{ + uint64_t b1 = a & 0xFF; + uint64_t b2 = (a >> 8) & 0xFF; + uint64_t b3 = (a >> 16) & 0xFF; + uint64_t b4 = (a >> 24) & 0xFF; + uint64_t b5 = (a >> 32) & 0xFF; + uint64_t b6 = (a >> 40) & 0xFF; + uint64_t b7 = (a >> 48) & 0xFF; + uint64_t b8 = (a >> 56) & 0xFF; + + uint64_t a_back = (b1 << 56) | (b2 << 48) | (b3 << 40) | (b4 << 32) | (b5 << 24) | (b6 << 16) | + (b7 << 8) | b8; + return a_back; +} + void gen_save_secret_key(string sk_fpath, string str_sk_fpath, string seal_sk_fpath, - const SEALContext &context) + const seal::SEALContext &context) { KeyGenerator keygen(context); SecretKey sk1 = keygen.secret_key(); @@ -34,7 +50,7 @@ void gen_save_secret_key(string sk_fpath, string str_sk_fpath, string seal_sk_fp // -- Check to make sure we can read back secret key correctly. // -- First, generate a new secret key. This should be the same as the // old secret key since it will be created from keygen's copy. - bool incl_sp = 1; // include special prime + bool incl_sp = true; // include special prime SecretKey sk2 = sk1; compare_sk(context, sk1, sk2, incl_sp, true); @@ -49,18 +65,19 @@ void gen_save_secret_key(string sk_fpath, string str_sk_fpath, string seal_sk_fp } void gen_save_public_key(string dirpath, string seal_pk_fpath, string sk_fpath, - string seal_sk_fpath, const SEALContext &context, bool use_seal_sk_fpath) + string seal_sk_fpath, const seal::SEALContext &context, + bool use_seal_sk_fpath) { SecretKey sk; if (use_seal_sk_fpath) sk_seal_load(seal_sk_fpath, context, sk); else { - auto &sk_parms = context.key_context_data()->parms(); - auto &coeff_modulus = sk_parms.coeff_modulus(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t n = sk_parms.poly_modulus_degree(); - sk.data().resize(mul_safe(n, coeff_modulus_size)); + auto &sk_parms = context.key_context_data()->parms(); + auto &coeff_modulus = sk_parms.coeff_modulus(); + size_t nprimes = coeff_modulus.size(); + size_t n = sk_parms.poly_modulus_degree(); + sk.data().resize(mul_safe(n, nprimes)); sk_bin_file_load(sk_fpath, context, sk); sk.data().parms_id() = context.key_parms_id(); } @@ -77,8 +94,9 @@ void gen_save_public_key(string dirpath, string seal_pk_fpath, string sk_fpath, // -- Write the public key, prime by prime, across multiple files // Make sure to write special prime to file as well. - bool incl_sp = 1; - pk_bin_file_save(dirpath, context, pk1_wr, incl_sp); + bool incl_sp = true; + bool high_byte_first = false; + pk_bin_file_save(dirpath, context, pk1_wr, incl_sp, high_byte_first); // -- Check to make sure we can read back public key correctly. -- @@ -94,7 +112,7 @@ void gen_save_public_key(string dirpath, string seal_pk_fpath, string sk_fpath, compare_pk(context, pk1_wr, pk2_wr, incl_sp, false); // -- Read in saved public key from file - pk_bin_file_load(dirpath, context, pk2_wr, incl_sp); + pk_bin_file_load(dirpath, context, pk2_wr, incl_sp, high_byte_first); compare_pk(context, pk1_wr, pk2_wr, incl_sp, true); } @@ -104,45 +122,42 @@ void gen_save_ifft_roots(string dirpath, const SEALContext &context, bool high_b size_t n = context.key_context_data()->parms().poly_modulus_degree(); vector> ifft_roots(n); - shared_ptr complex_roots; - complex_roots = make_shared(ComplexRoots(2 * n, MemoryPoolHandle::Global())); + std::shared_ptr croots = + make_shared(ComplexRoots(2 * n, MemoryPoolHandle::Global())); int logn = static_cast(log2(n)); - bool better_order = 1; + bool better_order = true; if (better_order) { for (size_t i = 0; i < n; i++) { ifft_roots[i] = - conj(complex_roots->get_root(static_cast(reverse_bits(i - 1, logn) + 1))); + conj(croots->get_root(static_cast(reverse_bits(i - 1, logn) + 1))); } } else { for (size_t i = 0; i < n; i++) - { - ifft_roots[i] = - conj(complex_roots->get_root(static_cast(reverse_bits(i, logn)))); - } + { ifft_roots[i] = conj(croots->get_root(static_cast(reverse_bits(i, logn)))); } } string fname = dirpath + "ifft_roots_" + to_string(n) + +".dat"; - fstream outfile(fname.c_str(), ios::out | ios::binary | ios::trunc); + fstream file(fname.c_str(), ios::out | ios::binary | ios::trunc); - fstream outfile2; + fstream file2; if (string_roots) { string fname2 = dirpath + "str_ifft_roots.h"; - // string fname2 = dirpath + "str_ifft_roots_" + to_string(n) + ".h"; - outfile2.open(fname2, ios::out | ios::trunc); + file2.open(fname2, ios::out | ios::trunc); size_t num_uint64_elements = n * 2; // real part + imag part - outfile2 << "#pragma once\n\n#include \"defines.h\"\n\n#include \n\n"; - outfile2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " - "defined(SE_DATA_FROM_CODE_DIRECT)\n"; - outfile2 << "#ifdef SE_IFFT_LOAD_FULL\n"; - outfile2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; - outfile2 << "// -- IFFT roots for polynomial ring degree = " << n << "\n"; - outfile2 << "uint64_t ifft_roots_save[" << num_uint64_elements << "] = { "; + file2 << "#pragma once\n\n#include \"defines.h\"\n\n#include \n\n"; + file2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " + "defined(SE_DATA_FROM_CODE_DIRECT)\n"; + file2 << "#ifdef SE_IFFT_LOAD_FULL\n"; + file2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; + file2 << "// -- IFFT roots for polynomial ring degree = " << n << "\n"; + // -- Save using uint64_t instead of double to maintain full precision + file2 << "uint64_t ifft_roots_save[" << num_uint64_elements << "] = { "; } for (size_t i = 0; i < n; i++) { @@ -156,182 +171,221 @@ void gen_save_ifft_roots(string dirpath, const SEALContext &context, bool high_b uint64_t data = *((uint64_t *)(&data_d)); // we can only shift an int type // -- Debugging info - // if (i < 5) cout << "data_d: " << data_d << endl; - // if (i < 5) cout << "data : " << data << endl; + // if (i < 10) cout << "data_d: " << data_d << endl; + // if (i < 5) cout << "data : " << std::hex << data << std::dec << endl; for (size_t j = 0; j < 8; j++) // write one byte at a time { size_t shift_amt = (high_byte_first) ? 7 - j : j; uint8_t byte = (data >> (8 * shift_amt)) & 0xFF; - outfile.put(static_cast(byte)); + file.put(static_cast(byte)); } if (string_roots) { - // cout << "data string: " << to_string(data) << endl; string next_str = (((i + 1) < n) || !k) ? ", " : "};\n"; - outfile2 << to_string(data) + "ULL" << next_str; - if (!(i % 64) && i && k) outfile2 << "\n"; + uint64_t data_s = high_byte_first ? endian_flip(data) : data; + file2 << "0x" << std::hex << data_s << std::dec << next_str; + if (!(i % 64) && i && k) file2 << "\n"; } } } - outfile.close(); + file.close(); if (string_roots) { - outfile2 << "\n#endif\n#endif" << endl; - outfile2.close(); + file2 << "\n#endif\n#endif" << endl; + file2.close(); } } void gen_save_ntt_roots_header(string dirpath, const SEALContext &context, bool inverse) { - auto &key_parms = context.key_context_data()->parms(); - size_t n = key_parms.poly_modulus_degree(); - size_t coeff_modulus_size = key_parms.coeff_modulus().size(); - assert(coeff_modulus_size >= 2); - - string fpath = dirpath + "str_"; - if (inverse) fpath += "i"; - fpath += "ntt_roots_addr_array.h"; - fstream outfile(fpath.c_str(), ios::out | ios::trunc); - - bool incl_sp_sf = false; // No need to include the special prime for the headers + auto &key_parms = context.key_context_data()->parms(); + size_t nprimes = key_parms.coeff_modulus().size(); + size_t n = key_parms.poly_modulus_degree(); - // const bool large_primes = false; // Assume this + string ntt_str = inverse ? "intt" : "ntt"; + string ntt_str_caps = inverse ? "INTT" : "NTT"; - outfile << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile << "#if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT)\n\n"; - outfile << "#include \n\n"; + string fpath = dirpath + "str_" + ntt_str + "_roots_addr_array.h"; + fstream file(fpath.c_str(), ios::out | ios::trunc); - stringstream pk_addr_str; + // -- No need to include the special prime for the headers + bool incl_sp_sf = (nprimes == 1) ? true : false; + size_t string_file_nprimes = incl_sp_sf ? nprimes : nprimes - 1; - size_t string_file_coeff_modulus_size = - incl_sp_sf ? coeff_modulus_size : coeff_modulus_size - 1; + file << "#pragma once\n\n#include \"defines.h\"\n\n"; + file << "#if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT)\n\n"; + file << "#include \n\n"; for (size_t outer = 0; outer < 2; outer++) { - if (inverse) - { - if (outer == 0) - outfile << "#ifdef SE_INTT_REG\n"; - else - outfile << "#elif defined(SE_INTT_FAST)\n"; - } + if (outer == 0) + file << "#ifdef SE_" << ntt_str_caps << "_REG\n"; else + file << "#elif defined(SE_" << ntt_str_caps << "_FAST)\n"; + for (size_t t = 0; t < string_file_nprimes; t++) { - if (outer == 0) - outfile << "#ifdef SE_NTT_REG\n"; - else - outfile << "#elif defined(SE_NTT_FAST)\n"; + auto q = context.key_context_data()->parms().coeff_modulus()[t].value(); + assert(log2(q) <= 30); + string fpath = "str_" + ntt_str; + if (outer == 1) fpath += "_fast"; + fpath += "_roots_" + to_string(n) + "_" + to_string(q) + ".h"; + cout << "writing to file: " << dirpath + fpath << endl; + file << " #include \"" << fpath << "\"" << endl; } - for (size_t t = 0; t < string_file_coeff_modulus_size; t++) - { - string fpath_common = "roots_"; - auto coeff_modulus = context.key_context_data()->parms().coeff_modulus()[t].value(); - fpath_common += to_string(n) + "_" + to_string(coeff_modulus); - - string fpath = dirpath + "str_"; - if (inverse) fpath += "i"; - fpath += "ntt" + fpath_common + ".h"; - cout << "writing to file: " << fpath << endl; - - outfile << " #include \"str_"; - if (inverse) outfile << "i"; - outfile << "ntt_"; - if (outer) outfile << "fast_"; - outfile << fpath_common + ".h\"" << endl; - } - if (outer == 1) outfile << "#endif\n"; + if (outer == 1) file << "#endif\n"; } - - outfile << "\nZZ* "; - if (inverse) outfile << "i"; - outfile << "ntt_roots_addr[" << string_file_coeff_modulus_size << "] =\n{\n"; + file << "\nZZ* " << ntt_str << "_roots_addr[" << string_file_nprimes << "] =\n{\n"; // -- No need to include special prime in string files - for (size_t t = 0; t < string_file_coeff_modulus_size; t++) + for (size_t t = 0; t < string_file_nprimes; t++) { - outfile << " &(((ZZ*)("; - if (inverse) outfile << "i"; - outfile << "ntt_roots_save_prime" << to_string(t) << "))[0])"; - if (t == string_file_coeff_modulus_size - 1) - outfile << "\n};" << endl; + file << " &(((ZZ*)(" << ntt_str << "_roots_save_prime" << to_string(t) << "))[0])"; + if (t == string_file_nprimes - 1) + file << "\n};" << endl; else - outfile << "," << endl; + file << "," << endl; } - outfile << "\n#endif\n"; - outfile.close(); + file << "\n#endif\n"; + file.close(); } void gen_save_ntt_roots(string dirpath, const SEALContext &context, bool lazy, bool inverse, bool high_byte_first, bool string_roots) { + // -- Note: SEAL stores the NTT tables in bit-rev form auto ntt_tables_ptr = context.key_context_data()->small_ntt_tables(); auto &key_parms = context.key_context_data()->parms(); size_t n = key_parms.poly_modulus_degree(); + size_t nprimes = key_parms.coeff_modulus().size(); const bool large_primes = false; - for (size_t mod_loop = 0; mod_loop < key_parms.coeff_modulus().size(); mod_loop++) + + string ntt_str = inverse ? "intt" : "ntt"; + string ntt_str_caps = inverse ? "INTT" : "NTT"; + + for (size_t t = 0; t < nprimes; t++) { - const NTTTables *tables = &(ntt_tables_ptr[mod_loop]); - Modulus modulus = tables->modulus(); - MultiplyUIntModOperand inv_n = tables->inv_degree_modulo(); - MultiplyUIntModOperand w_n_minus_1 = tables->get_from_inv_root_powers(n - 1); - MultiplyUIntModOperand inv_n_w; - inv_n_w.set(multiply_uint_mod(inv_n.operand, w_n_minus_1, modulus), modulus); - - // -- Debugging - cout << "inv_n.operand : " << inv_n.operand << endl; - cout << "inv_n.quotient : large: " << inv_n.quotient; - cout << " small: " << upper32(inv_n.quotient) << endl; - cout << "inv_n_w.operand: " << inv_n_w.operand << endl; - cout << "inv_n.quotient : large: " << inv_n_w.quotient; - cout << " small: " << upper32(inv_n_w.quotient) << endl; + const NTTTables *tables = &(ntt_tables_ptr[t]); + assert(tables); + Modulus modulus = tables->modulus(); + + auto q = modulus.value(); + bool large_modulus = (log2(q) > 32) ? true : false; + + assert(((t <= (nprimes - 1)) && (log2(q) <= 30)) || + ((t == nprimes - 1) && (log2(q) <= 64) && (nprimes != 1))); + + /* + SEAL-Embedded needs the following constant values related to the NTT/INTT + - If NTT compute type is "on-the-fly" or "one-shot": + > w = Value of the first power of the NTT root, for each prime qi + - If INTT compute type is "on-the-fly" or "one-shot" (and on-device testing is desired): + > inv_w = Value of the first power of the INTT root, for each prime qi + - This should be equal to w^(-1) mod qi + - If INTT compute type is non-fast (and on-device testing is desired): + > inv_n = n^(-1) mod qi + > last_inv_sn = (last_inv_s * inv_n) mod qi + - Where last_inv_s is the last inverse NTT root power used in an INTT loop + (i.e., is last power stored in inv_root_powers) + */ + auto logn = seal::util::get_power_of_two(n); + cout << "logn: " << logn << endl; + auto bit_rev_1 = seal::util::reverse_bits((uint64_t)1, logn); + MultiplyUIntModOperand w = tables->get_from_root_powers(bit_rev_1); + MultiplyUIntModOperand inv_w = tables->get_from_inv_root_powers(1); + { + uint64_t inv_w_check; + if (!try_invert_uint_mod(w.operand, modulus, inv_w_check)) + throw invalid_argument("invalid modulus"); + if (inv_w.operand != inv_w_check) + { + cout << "inv_w_check: " << inv_w_check << endl; + cout << "inv_w.operand: " << inv_w.operand << endl; + throw; + } + } + MultiplyUIntModOperand inv_n = tables->inv_degree_modulo(); // n^(-1) mod qi + MultiplyUIntModOperand last_inv_s = tables->get_from_inv_root_powers(n - 1); + MultiplyUIntModOperand last_inv_sn; + last_inv_sn.set(multiply_uint_mod(inv_n.operand, last_inv_s, modulus), modulus); + { + // -- Check + uint64_t last_ii_s; + if (!try_invert_uint_mod(last_inv_s.operand, modulus, last_ii_s)) + throw invalid_argument("invalid modulus"); + + uint64_t last_inv_sn_check; + auto n_last_ii_s = multiply_uint_mod(n, last_ii_s, modulus); + if (!try_invert_uint_mod(n_last_ii_s, modulus, last_inv_sn_check)) + throw invalid_argument("invalid modulus"); + if (last_inv_sn.operand != last_inv_sn_check) + { + cout << "last_ii_s: " << last_ii_s << endl; + cout << "n_last_ii_s: " << n_last_ii_s << endl; + cout << "last_inv_sn_check: " << last_inv_sn_check << endl; + cout << "last_inv_sn.operand: " << last_inv_sn.operand << endl; + throw; + } + } + + // -- Debugging / For filling in SEAL-Embedded constants + cout << "\n--- Printing constants for n = " << n; + cout << ", q = " << q << " ---\n" << endl; + cout << "\t(w = first power of NTT root)" << endl; + cout << "\t w.operand : " << w.operand << endl; + cout << "\t w.quotient : " << upper32(w.quotient) << " (small) = "; + cout << w.quotient << " (large)\n" << endl; + cout << "\t(inv_w = w^(-1) mod qi = first power of INTT root)" << endl; + cout << "\t inv_w.operand : " << inv_w.operand << endl; + cout << "\t inv_w.quotient : " << upper32(inv_w.quotient) << " (small) = "; + cout << inv_w.quotient << " (large)\n" << endl; + cout << "\t(inv_n = n^(-1) mod qi)" << endl; + cout << "\t inv_n.operand : " << inv_n.operand << endl; + cout << "\t inv_n.quotient : " << upper32(inv_n.quotient) << " (small) = "; + cout << inv_n.quotient << " (large)\n" << endl; + cout << "\t(last_inv_sn = (last_inv_s * inv_n) mod qi)" << endl; + cout << "\t last_inv_sn.operand : " << last_inv_sn.operand << endl; + cout << "\t last_inv_sn.quotient : " << upper32(last_inv_sn.quotient) << " (small) = "; + cout << last_inv_sn.quotient << " (large)\n\n" << endl; // -- Create file - string fname = dirpath; - fname += (!inverse) ? "ntt" : "intt"; - // if (lazy) fname += "_lazy"; + string fname = dirpath + ntt_str; if (lazy) fname += "_fast"; - fname += "_roots_" + to_string(n) + "_" + to_string(modulus.value()) + ".dat"; - fstream outfile(fname.c_str(), ios::out | ios::binary | ios::trunc); + fname += "_roots_" + to_string(n) + "_" + to_string(q) + ".dat"; + fstream file(fname.c_str(), ios::out | ios::binary | ios::trunc); cout << "Writing to " << fname << endl; - fstream outfile2; + fstream file2; if (string_roots) { - string fname2 = dirpath; - fname2 += (!inverse) ? "str_ntt" : "str_intt"; - // if (lazy) fname2 += "_lazy"; + string fname2 = dirpath + "str_" + ntt_str; if (lazy) fname2 += "_fast"; - fname2 += "_roots_" + to_string(n) + "_" + to_string(modulus.value()) + ".h"; - outfile2.open(fname2, ios::out | ios::trunc); + fname2 += "_roots_" + to_string(n) + "_" + to_string(q) + ".h"; + file2.open(fname2, ios::out | ios::trunc); size_t num_elements = lazy ? n * 2 : n; // real part + imag part - outfile2 << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " - "defined(SE_DATA_FROM_CODE_DIRECT)\n"; - outfile2 << "#include \n\n"; // uint32_t + file2 << "#pragma once\n\n#include \"defines.h\"\n\n"; + file2 << "#if defined(SE_DATA_FROM_CODE_COPY) || " + "defined(SE_DATA_FROM_CODE_DIRECT)\n"; + file2 << "#include \n\n"; // uint32_t + file2 << "#ifdef SE_" << ntt_str_caps; if (lazy) - { - if (inverse) - outfile2 << "#ifdef SE_INTT_FAST\n"; - else - outfile2 << "#ifdef SE_NTT_FAST\n"; - } + file2 << "_FAST\n"; else + file2 << "_REG\n"; + if (large_modulus) { - if (inverse) - outfile2 << "#ifdef SE_INTT_REG\n"; - else - outfile2 << "#ifdef SE_NTT_REG\n"; + file2 << "// -- Note: This file uses >30-bit primes and cannot"; + file2 << " be used with the SEAL-Embedded device library." << endl; } - outfile2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; - // outfile2 << "uint64_t "; - outfile2 << "ZZ "; - if (inverse) outfile2 << "i"; - outfile2 << "ntt_"; - // if (lazy) outfile2 << "fast_"; - outfile2 << "roots_save_prime" << to_string(mod_loop); - outfile2 << "[" << num_elements << "] = { "; + file2 << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; + if (large_modulus) + file2 << "uint64_t "; + else + file2 << "ZZ "; + file2 << ntt_str << "_"; + file2 << "roots_save_prime" << to_string(t); + file2 << "[" << num_elements << "] = { "; } for (size_t i = 0; i < n; i++) @@ -344,11 +398,16 @@ void gen_save_ntt_roots(string dirpath, const SEALContext &context, bool lazy, b const MultiplyUIntModOperand w = (!inverse) ? tables->get_from_root_powers(i) : tables->get_from_inv_root_powers(i); - // -- Debugging info - if (i < 5) + size_t logn = seal::util::get_power_of_two(n); + assert(logn != -1); + size_t actual_idx = reverse_bits(i, logn); + + // -- Debugging / For filling in SEAL-Embedded constants + if (actual_idx == 1) { + if (inverse) cout << "inverse_"; cout << "root[" << i << "]: operand = " << w.operand << " , quotient = "; - if (large_primes) + if (large_modulus) cout << w.quotient << endl; else cout << upper32(w.quotient) << endl; @@ -357,28 +416,29 @@ void gen_save_ntt_roots(string dirpath, const SEALContext &context, bool lazy, b for (size_t k = 0; k < 1 + static_cast(lazy); k++) { uint64_t data = (!k) ? w.operand : w.quotient; - if (k && !large_primes) { data = static_cast(upper32(data)); } - size_t primesize = large_primes ? 8 : 4; // size in bytes - for (size_t j = 0; j < primesize; j++) // write one byte at a time + if (k && !large_modulus) { data = static_cast(upper32(data)); } + size_t primesize = large_modulus ? 8 : 4; // size in bytes + for (size_t j = 0; j < primesize; j++) // write one byte at a time { size_t shift_amt = high_byte_first ? primesize - 1 - j : j; uint8_t byte = (data >> (8 * shift_amt)) & 0xFF; - outfile.put(static_cast(byte)); + file.put(static_cast(byte)); } if (string_roots) { string next_str = (((i + 1) < n) || (!k && lazy)) ? ", " : "};\n"; - string ulong_str = large_primes ? "ULL" : ""; - outfile2 << to_string(data) + ulong_str << next_str; - if (!(i % 64) && i && !k) outfile2 << "\n"; + string ulong_str = large_modulus ? "ULL" : ""; + uint64_t data_s = high_byte_first ? endian_flip(data) : data; + file2 << to_string(data_s) + ulong_str << next_str; + if (!(i % 64) && i && !k) file2 << "\n"; } } } - outfile.close(); + file.close(); if (string_roots) { - outfile2 << "\n#endif\n#endif\n" << endl; - outfile2.close(); + file2 << "\n#endif\n#endif\n" << endl; + file2.close(); } } gen_save_ntt_roots_header(dirpath, context, inverse); @@ -391,7 +451,7 @@ Requires numbits to be at most 16. @param[in] input Value to be bit-reversed @param[in] numbits Number of bits to reverse */ -static size_t index_map_bit_rev(size_t input, size_t numbits) +static size_t bit_rev_16bits(size_t input, size_t numbits) { assert(numbits <= 16); size_t t = (((input & 0xaaaa) >> 1) | ((input & 0x5555) << 1)); @@ -408,14 +468,11 @@ void gen_save_index_map(string dirpath, const SEALContext &context, bool high_by uint64_t m = (uint64_t)n * 2; // m = 2n size_t slot_count = n / 2; // slot_count = n/2 size_t logn = static_cast(log2(n)); // number of bits to represent n - - // -- If n > 16384, cannot use uint16_t - assert(n < 16384); + assert(logn <= 16); // -- 3 generates a multiplicative group mod 2^n with order n/2 // (Note: 5 would work as well, but 3 matches SEAL and SEAL-Embedded) uint64_t gen = 3; - // uint64_t gen = 5; uint64_t pos = 1; uint16_t *index_map = (uint16_t *)calloc(n, sizeof(uint16_t)); @@ -425,10 +482,12 @@ void gen_save_index_map(string dirpath, const SEALContext &context, bool high_by size_t index1 = ((size_t)pos - 1) / 2; size_t index2 = n - index1 - 1; + assert(index1 <= 0xFFFF && index2 <= 0xFFFF); // less than max uint16_t + // -- Merge index mapping step w/ bitrev step req. for later application of - // IFFT/NTT - index_map[i] = (uint16_t)index_map_bit_rev(index1, logn); - index_map[i + slot_count] = (uint16_t)index_map_bit_rev(index2, logn); + // IFFT/NTT + index_map[i] = (uint16_t)bit_rev_16bits(index1, logn); + index_map[i + slot_count] = (uint16_t)bit_rev_16bits(index2, logn); // -- Next root pos *= gen; @@ -443,7 +502,7 @@ void gen_save_index_map(string dirpath, const SEALContext &context, bool high_by // -- Save to binary file { string fname = dirpath + "index_map_" + to_string(n) + ".dat"; - fstream outfile(fname.c_str(), ios::out | ios::binary | ios::trunc); + fstream file(fname.c_str(), ios::out | ios::binary | ios::trunc); cout << "Writing to " << fname << endl; for (size_t i = 0; i < n; i++) { @@ -451,38 +510,43 @@ void gen_save_index_map(string dirpath, const SEALContext &context, bool high_by { size_t shift_amt = (high_byte_first) ? 1 - j : j; uint8_t byte = (index_map[i] >> (8 * shift_amt)) & 0xFF; - outfile.put(static_cast(byte)); + file.put(static_cast(byte)); } } - outfile.close(); + file.close(); } // -- Hard-code in code file { string fname = dirpath + "str_index_map.h"; - fstream outfile(fname.c_str(), ios::out | ios::binary | ios::trunc); + fstream file(fname.c_str(), ios::out | ios::binary | ios::trunc); cout << "Writing to " << fname << endl; - outfile << "#pragma once\n\n#include \"defines.h\"\n\n"; - outfile << "#if defined(SE_DATA_FROM_CODE_COPY) || " - "defined(SE_DATA_FROM_CODE_DIRECT)\n"; - outfile << "#if defined(SE_INDEX_MAP_LOAD) || " - "defined(SE_INDEX_MAP_LOAD_PERSIST)\n"; - outfile << "#include \n\n"; // uint64_t ? - outfile << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; - outfile << "// -- index map indices for polynomial ring degree = " << n << "\n"; - outfile << "uint32_t index_map_store[" << n / 2 << "] = { "; + file << "#pragma once\n\n#include \"defines.h\"\n\n"; + file << "#if defined(SE_DATA_FROM_CODE_COPY) || " + "defined(SE_DATA_FROM_CODE_DIRECT)\n"; + file << "#if defined(SE_INDEX_MAP_LOAD) || " + "defined(SE_INDEX_MAP_LOAD_PERSIST) || " + "defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM)\n"; + file << "#include \n\n"; // uint64_t ? + file << "#ifdef SE_DATA_FROM_CODE_COPY\nconst\n#endif" << endl; + file << "// -- index map indices for polynomial ring degree = " << n << "\n"; + file << "uint32_t index_map_store[" << n / 2 << "] = { "; uint32_t *index_map_str_save = (uint32_t *)index_map; size_t i_stop = n / 2; // half since we are storing as uint32 instead of uint16 for (size_t i = 0; i < i_stop; i++) { string next_str = ((i + 1) < i_stop) ? ", " : "};\n"; - outfile << "0x" << hex << index_map[i] << next_str; - if (!(i % 13)) outfile << "\n"; + file << "0x" << std::hex << index_map_str_save[i] << next_str; + if (!(i % 13)) file << "\n"; } - outfile << "\n#endif\n#endif" << endl; - outfile.close(); + file << "\n#endif\n#endif" << endl; + file.close(); + } + if (index_map) + { + free(index_map); + index_map = 0; } - free(index_map); } diff --git a/adapter/generate.h b/adapter/generate.h index 10f63e3..1cf3027 100644 --- a/adapter/generate.h +++ b/adapter/generate.h @@ -10,11 +10,12 @@ pi-inverse) values. Useful for generating objects to load onto a SEAL-Embedded d #pragma once +#include + #include "seal/seal.h" /** -Wrapper struct for Public key. Required to keep track of whether public key is in NTT -form. +Wrapper struct for Public key. Required to keep track of whether public key is in NTT form. @param pk Pointer to public key @param is_ntt If true, pk is in NTT form @@ -28,7 +29,7 @@ typedef struct PublicKeyWrapper /** Returns the upper 32 bits of a 64-bit value */ -static inline std::uint32_t upper32(std::uint64_t val) +static inline uint32_t upper32(uint64_t val) { return (val >> 32) & 0xFFFFFFFF; } @@ -36,10 +37,10 @@ static inline std::uint32_t upper32(std::uint64_t val) /** Generates and saves a secret key to file. -@param[in] sk_fpath Path to file to store the secret key in SEAL-Embedded form -@param[in] str_sk_fpath Path to code file to hard-code the values of the secret key -@param[in] seal_sk_fpath Path to file to store the secret key in SEAL form -@param[in] context SEAL context +@param[in] sk_fpath Path to file to store the secret key in SEAL-Embedded form +@param[in] str_sk_fpath Path to code file to hard-code the values of the secret key +@param[in] seal_sk_fpath Path to file to store the secret key in SEAL form +@param[in] context SEAL context */ void gen_save_secret_key(std::string sk_fpath, std::string str_sk_fpath, std::string seal_sk_fpath, const seal::SEALContext &context); @@ -47,61 +48,55 @@ void gen_save_secret_key(std::string sk_fpath, std::string str_sk_fpath, std::st /** Generates and saves a public key to file. -In order to generate the public key, the key generator must read in the secret -key from file. This file may either be: 1) the file created using -SEAL-Embedded's adapter save functionality (SEAL-Embedded form). In this case, -set use_seal_sk_fpath to 0 and set sk_fpath to the path to the secret key. 2) -the file created using SEAL's stream save functionality (SEAL form). In this -case, set use_seal_sk_fpath to 1 and set seal_sk_fpath to the path to the secret -key. - -@param[in] dirpath Path to the directory to store file containing public key -in SEAL-Embedded form -@param[in] seal_pk_fpath Path to file to store public key -@param[in] sk_fpath Path to file storing secret key in SEAL-Embedded form -@param[in] seal_sk_fpath Path to file storing secret key in SEAL form -@param[in] context SEAL context -@param[in] use_seal_sk_fpath If true, use seal_sk_fpath to load secret key. Else, use -sk_fpath +In order to generate the public key, the key generator must read in the secret key from file. This +file may either be: 1) the file created using SEAL-Embedded's adapter save functionality +(SEAL-Embedded form). In this case, set use_seal_sk_fpath to 0 and set sk_fpath to the path to the +secret key. 2) the file created using SEAL's stream save functionality (SEAL form). In this case, +set use_seal_sk_fpath to 1 and set seal_sk_fpath to the path to the secret key. + +@param[in] dirpath Path to the directory to store file containing public key in + SEAL-Embedded form +@param[in] seal_pk_fpath Path to file to store public key +@param[in] sk_fpath Path to file storing secret key in SEAL-Embedded form +@param[in] seal_sk_fpath Path to file storing secret key in SEAL form +@param[in] context SEAL context +@param[in] use_seal_sk_fpath If true, use seal_sk_fpath to load secret key. Else, use sk_fpath */ void gen_save_public_key(std::string dirpath, std::string seal_pk_fpath, std::string sk_fpath, std::string seal_sk_fpath, const seal::SEALContext &context, bool use_seal_sk_fpath); /** -Generates and saves the IFFT roots to file. +Generates and saves the IFFT roots to file for use with SEAL-Embedded. -@param[in] dirpath Path to the directory to store file containing public key in -SEAL-Embedded form +@param[in] dirpath Path to the directory to store file containing ifft roots @param[in] context SEAL context -@param[in] high_byte_first If true, saves in (??)endian form -@param[in] string_roots +@param[in] high_byte_first Toggle for endianness +@param[in] string_roots If true, generate string header file too */ void gen_save_ifft_roots(std::string dirpath, const seal::SEALContext &context, bool high_byte_first, bool string_roots); /** -Generates and saves the NTT roots to file. +Generates and saves the NTT roots to file for use with SEAL-Embedded. -@param[in] dirpath Path to directory to store file containing NTT roots in -SEAL-Embedded form +@param[in] dirpath Path to directory to store file containing NTT roots @param[in] context SEAL context @param[in] lazy If true, generates "fast" NTT roots (false = regular roots) @param[in] inverse If true, generates inverse NTT roots (false = forward roots) -@param[in] high_byte_first If true, saves in (??)endian form // TODO: -@param[in] string_roots +@param[in] high_byte_first Toggle for endianness +@param[in] string_roots If true, generate string header file too */ void gen_save_ntt_roots(std::string dirpath, const seal::SEALContext &context, bool lazy, bool inverse, bool high_byte_first, bool string_roots); /** -Generates and saves the index map (i.e. pi-inverse) to file. Also generates a code file -with hard-coded index map values. +Generates and saves the index map (i.e. pi-inverse) to file for use with SEAL-Embedded. +Also generates a code file with hard-coded index map values for use with SEAL-Embedded. -@param[in] dirpath Path to directory to store file containing NTT roots in -SEAL-Embedded form +@param[in] dirpath Path to directory to store file containing index map values @param[in] context SEAL context -@param[in] high_byte_first If true, saves in (??)endian form // TODO: +@param[in] high_byte_first Toggle for endianness */ void gen_save_index_map(std::string dirpath, const seal::SEALContext &context, bool high_byte_first); diff --git a/adapter/utils.cpp b/adapter/utils.cpp index 49e54b9..2b54121 100644 --- a/adapter/utils.cpp +++ b/adapter/utils.cpp @@ -20,28 +20,73 @@ using namespace seal::util; // ---------------- Setup ------------------ -void set_modulus_testing_values(size_t nprimes, vector &vec) +void add_27bit_moduli(size_t nprimes, vector &vec) { + if (!nprimes) return; vec.clear(); // -- Note: We create the vector in reverse first switch (nprimes) { - case 8: vec.push_back(Modulus(1073643521)); [[fallthrough]]; - case 7: vec.push_back(Modulus(1073479681)); [[fallthrough]]; - case 6: vec.push_back(Modulus(1073184769)); [[fallthrough]]; - case 5: vec.push_back(Modulus(1073053697)); [[fallthrough]]; - case 4: vec.push_back(Modulus(1072857089)); [[fallthrough]]; - case 3: vec.push_back(Modulus(1072496641)); [[fallthrough]]; - case 2: vec.push_back(Modulus(1071513601)); [[fallthrough]]; - case 1: vec.push_back(Modulus(1071415297)); [[fallthrough]]; + // -- These primes are all 1 mod 32768 + case 3: vec.push_back(Modulus(134176769)); [[fallthrough]]; + case 2: vec.push_back(Modulus(134111233)); [[fallthrough]]; + case 1: vec.push_back(Modulus(134012929)); [[fallthrough]]; + // TODO: This is the SEAL default. Why? + // case 1: vec.push_back(Modulus(132120577)); [[fallthrough]]; default: break; } reverse(vec.begin(), vec.end()); } -SEALContext setup_seale_custom(size_t degree, const vector &moduli, - EncryptionParameters &parms) +void add_30bit_moduli(size_t nprimes, vector &vec) +{ + if (!nprimes) return; + + vec.clear(); + + // -- Note: We create the vector in reverse first + switch (nprimes) + { + // -- These primes are all 1 mod 32768 + /* + // -- These are for n = 32K + case 28: vec.push_back(Modulus(1073479681)); [[fallthrough]]; + case 27: vec.push_back(Modulus(1072496641)); [[fallthrough]]; + case 26: vec.push_back(Modulus(1071513601)); [[fallthrough]]; + case 25: vec.push_back(Modulus(1070727169)); [[fallthrough]]; + case 24: vec.push_back(Modulus(1069219841)); [[fallthrough]]; + case 23: vec.push_back(Modulus(1068564481)); [[fallthrough]]; + case 22: vec.push_back(Modulus(1068433409)); [[fallthrough]]; + case 21: vec.push_back(Modulus(1068236801)); [[fallthrough]]; + case 20: vec.push_back(Modulus(1065811969)); [[fallthrough]]; + case 19: vec.push_back(Modulus(1065484289)); [[fallthrough]]; + case 18: vec.push_back(Modulus(1064697857)); [[fallthrough]]; + case 17: vec.push_back(Modulus(1063452673)); [[fallthrough]]; + case 16: vec.push_back(Modulus(1063321601)); [[fallthrough]]; + case 15: vec.push_back(Modulus(1063059457)); [[fallthrough]]; + case 14: vec.push_back(Modulus(1062862849)); [[fallthrough]]; + */ + case 13: vec.push_back(Modulus(1062535169)); [[fallthrough]]; + case 12: vec.push_back(Modulus(1062469633)); [[fallthrough]]; + case 11: vec.push_back(Modulus(1061093377)); [[fallthrough]]; + case 10: vec.push_back(Modulus(1060765697)); [[fallthrough]]; + case 9: vec.push_back(Modulus(1060700161)); [[fallthrough]]; + case 8: vec.push_back(Modulus(1060175873)); [[fallthrough]]; + case 7: vec.push_back(Modulus(1058209793)); [[fallthrough]]; + case 6: vec.push_back(Modulus(1056440321)); [[fallthrough]]; + case 5: vec.push_back(Modulus(1056178177)); [[fallthrough]]; + case 4: vec.push_back(Modulus(1055260673)); [[fallthrough]]; + case 3: vec.push_back(Modulus(1054212097)); [[fallthrough]]; + case 2: vec.push_back(Modulus(1054015489)); [[fallthrough]]; + case 1: vec.push_back(Modulus(1053818881)); [[fallthrough]]; + default: break; + } + reverse(vec.begin(), vec.end()); +} + +seal::SEALContext setup_seale_custom(size_t degree, const vector &moduli, + EncryptionParameters &parms) { for (size_t i = 0; i < moduli.size() - 1; i++) { @@ -50,69 +95,83 @@ SEALContext setup_seale_custom(size_t degree, const vector &moduli, } parms.set_poly_modulus_degree(degree); parms.set_coeff_modulus(moduli); - SEALContext context(parms); + seal::SEALContext context(parms); print_parameters(context); + print_all_moduli(parms); cout << endl; return context; } -SEALContext setup_seale_30bitprime_default(size_t degree, EncryptionParameters &parms) +seal::SEALContext setup_seale_prime_default(size_t degree, EncryptionParameters &parms) { - int n_30bit_primes; - int special_prime_bitcount; + vector moduli; switch (degree) { + case 1024: add_27bit_moduli(1, moduli); break; case 2048: - n_30bit_primes = 1; - special_prime_bitcount = 24; + add_27bit_moduli(1, moduli); + moduli.push_back((CoeffModulus::Create(degree, {27}))[0]); + break; +#ifdef SEALE_DEFAULT_4K_27BIT + case 4096: + add_27bit_moduli(3, moduli); + moduli.push_back((CoeffModulus::Create(degree, {28}))[0]); break; +#else case 4096: - n_30bit_primes = 3; - special_prime_bitcount = 19; + add_30bit_moduli(3, moduli); + moduli.push_back((CoeffModulus::Create(degree, {19}))[0]); break; +#endif case 8192: - n_30bit_primes = 6; - special_prime_bitcount = 38; + add_30bit_moduli(6, moduli); + moduli.push_back((CoeffModulus::Create(degree, {38}))[0]); + break; + case 16384: + add_30bit_moduli(13, moduli); + moduli.push_back((CoeffModulus::Create(degree, {48}))[0]); + break; + default: + throw runtime_error( + "Please use a different setup function " + "(setup_seal_api or setup_seale_custom)"); break; - default: throw runtime_error("Please use a different setup function"); break; } - vector moduli; - set_modulus_testing_values(n_30bit_primes, moduli); - vector special_prime = CoeffModulus::Create(degree, {special_prime_bitcount}); - moduli.push_back(special_prime[0]); return setup_seale_custom(degree, moduli, parms); } -SEALContext setup_seal_api(size_t degree, const vector &bit_lengths, - EncryptionParameters &parms) +seal::SEALContext setup_seal_api(size_t degree, const vector &bit_lengths, + EncryptionParameters &parms) { return setup_seale_custom(degree, CoeffModulus::Create(degree, bit_lengths), parms); } // ---------------- Size functions ------------------ -size_t get_sk_num_bytes(const SecretKey &sk, const SEALContext &context, bool incl_sp) +size_t get_sk_num_bytes(const SecretKey &sk, const seal::SEALContext &context, bool incl_sp) { - auto &sk_parms = context.key_context_data()->parms(); - auto &coeff_modulus = sk_parms.coeff_modulus(); - size_t n = sk_parms.poly_modulus_degree(); - size_t coeff_modulus_size = coeff_modulus.size(); - size_t type_size = sizeof(sk.data().data()[0]); - - size_t num_primes = incl_sp ? coeff_modulus_size : coeff_modulus_size - 1; - return n * num_primes * type_size; + auto &sk_parms = context.key_context_data()->parms(); + auto &coeff_modulus = sk_parms.coeff_modulus(); + size_t n = sk_parms.poly_modulus_degree(); + size_t nprimes = coeff_modulus.size(); + size_t type_size = sizeof(sk.data().data()[0]); + assert(incl_sp || nprimes > 1); + + size_t nprimes_count = incl_sp ? nprimes : nprimes - 1; + return n * nprimes_count * type_size; } size_t get_pk_num_bytes(const PublicKey &pk, bool incl_sp) { - size_t n = pk.data().poly_modulus_degree(); - size_t coeff_modulus_size = pk.data().coeff_modulus_size(); - size_t type_size = sizeof(pk.data().data()[0]); - size_t num_components = pk.data().size(); + size_t n = pk.data().poly_modulus_degree(); + size_t nprimes = pk.data().coeff_modulus_size(); + size_t type_size = sizeof(pk.data().data()[0]); + size_t num_components = pk.data().size(); assert(num_components == 2); + assert(incl_sp || nprimes > 1); - size_t num_primes = incl_sp ? coeff_modulus_size : coeff_modulus_size - 1; - return n * num_primes * type_size * 2; + size_t nprimes_count = incl_sp ? nprimes : nprimes - 1; + return n * nprimes_count * type_size * 2; } // ---------------- Data pointers ------------------ @@ -126,9 +185,9 @@ uint64_t *get_ct_arr_ptr(Ciphertext &ct, bool second_element) { if (second_element) { - size_t n = ct.poly_modulus_degree(); - size_t coeff_modulus_size = ct.coeff_modulus_size(); - return reinterpret_cast(&(ct[n * coeff_modulus_size])); + size_t n = ct.poly_modulus_degree(); + size_t nprimes = ct.coeff_modulus_size(); + return reinterpret_cast(&(ct[n * nprimes])); } else { @@ -145,9 +204,9 @@ uint64_t *get_pk_arr_ptr(PublicKey &pk, bool second_element) { if (second_element) { - size_t n = pk.data().poly_modulus_degree(); - size_t coeff_modulus_size = pk.data().coeff_modulus_size(); - return reinterpret_cast(&(pk.data()[n * coeff_modulus_size])); + size_t n = pk.data().poly_modulus_degree(); + size_t nprimes = pk.data().coeff_modulus_size(); + return reinterpret_cast(&(pk.data()[n * nprimes])); } else { @@ -164,15 +223,15 @@ uint64_t *get_pk_arr_ptr(const PublicKeyWrapper &pk_wr, bool second_element) void clear_pk(PublicKey &pk) { - bool incl_special_prime = true; - size_t num_bytes = get_pk_num_bytes(pk, incl_special_prime); + bool incl_sp = true; + size_t num_bytes = get_pk_num_bytes(pk, incl_sp); memset(reinterpret_cast(get_pk_arr_ptr(pk)), 0, num_bytes); } -void clear_sk(const SEALContext &context, SecretKey &sk) +void clear_sk(const seal::SEALContext &context, SecretKey &sk) { - bool incl_special_prime = true; - size_t num_bytes = get_sk_num_bytes(sk, context, incl_special_prime); + bool incl_sp = true; + size_t num_bytes = get_sk_num_bytes(sk, context, incl_sp); memset(reinterpret_cast(get_sk_arr_ptr(sk)), 0, num_bytes); } @@ -187,23 +246,14 @@ bool same_pk(const PublicKeyWrapper &pk1_wr, const PublicKeyWrapper &pk2_wr, boo if (pk1_wr.is_ntt != pk2_wr.is_ntt) return false; - auto &data1 = pk1_wr.pk->data(); // This is actually the backing ciphertext - auto &data2 = pk2_wr.pk->data(); // This is actually the backing ciphertext - - // -- Compare polynomial degree - size_t n1 = data1.poly_modulus_degree(); - size_t n2 = data2.poly_modulus_degree(); - if (n1 != n2) return false; - - // -- Compare number of primes - size_t coeff_modulus_size1 = data1.coeff_modulus_size(); - size_t coeff_modulus_size2 = data2.coeff_modulus_size(); - if (coeff_modulus_size1 != coeff_modulus_size2) return false; + auto &data1 = pk1_wr.pk->data(); // backing ciphertext + auto &data2 = pk2_wr.pk->data(); // backing ciphertext - // -- Compare number of components - size_t num_components1 = data1.size(); - size_t num_components2 = data2.size(); - if (num_components1 != num_components2) return false; + // -- Compare polynomial degree, # of primes, # of components + if (data1.poly_modulus_degree() != data2.poly_modulus_degree()) return false; + if (data1.coeff_modulus_size() != data2.coeff_modulus_size()) return false; + if (data1.size() != data2.size()) return false; + assert(compare_sp || data1.coeff_modulus_size() > 1); size_t num_bytes = get_pk_num_bytes(*(pk1_wr.pk), compare_sp) / 2; cout << "num bytes: " << num_bytes << endl; @@ -238,25 +288,22 @@ bool same_pk(const PublicKeyWrapper &pk1_wr, const PublicKeyWrapper &pk2_wr, boo return same_pk1 && same_pk2; } -bool same_sk(const SecretKey &sk1, const SecretKey &sk2, const SEALContext &context, +bool same_sk(const SecretKey &sk1, const SecretKey &sk2, const seal::SEALContext &context, bool compare_sp) { - bool sk1_is_ntt = sk1.data().is_ntt_form(); - bool sk2_is_ntt = sk2.data().is_ntt_form(); + size_t num_bytes1 = get_sk_num_bytes(sk1, context); + size_t num_bytes2 = get_sk_num_bytes(sk2, context); + assert(num_bytes1 == num_bytes2); + + auto &data1 = sk1.data(); // backing plaintext + auto &data2 = sk2.data(); // backing plaintext - if (sk1_is_ntt != sk2_is_ntt) + if (data1.is_ntt_form() != data2.is_ntt_form()) { cout << "secret keys are not in the same form " << endl; return false; } - auto &data1 = sk1.data(); // This is actually the backing plaintext - auto &data2 = sk2.data(); // This is actually the backing plaintext - - size_t num_bytes1 = get_sk_num_bytes(sk1, context); - size_t num_bytes2 = get_sk_num_bytes(sk2, context); - assert(num_bytes1 == num_bytes2); - if (compare_sp) { return data1 == data2; } else { @@ -269,22 +316,30 @@ bool same_sk(const SecretKey &sk1, const SecretKey &sk2, const SEALContext &cont void print_all_moduli(EncryptionParameters &parms) { - cout << "Primes: " << endl; + cout << "Primes and const_ratio hw/lw: " << endl; for (size_t i = 0; i < parms.coeff_modulus().size(); i++) { - cout << " coeff_modulus[" << i << "]: "; - cout << parms.coeff_modulus()[i].value() << endl; + cout << " coeff_modulus["; + if (i < 10) cout << " "; + cout << i << "]: "; + cout << parms.coeff_modulus()[i].value(); + cout << " ("; + double const_ratio_double = floor(pow(2, 64) / parms.coeff_modulus()[i].value()); + uint64_t const_ratio = static_cast(const_ratio_double); + auto high_word = const_ratio >> 32; + auto low_word = const_ratio & (0xFFFFFFFF); + cout << std::hex << "hw = 0x" << high_word << ", lw = 0x" << low_word; + cout << std::dec << ")" << endl; } } void print_ct(Ciphertext &ct, size_t print_size) { - // total mod size >= 2, so ct_mod_size >=1 - size_t ct_mod_size = ct.coeff_modulus_size(); - size_t ct_size = ct.size(); // should be 2 for freshly encrypted - size_t n = ct.poly_modulus_degree(); - bool is_ntt = ct.is_ntt_form(); - assert(ct_mod_size >= 1); + size_t n = ct.poly_modulus_degree(); + size_t ct_nprimes = ct.coeff_modulus_size(); + size_t ct_size = ct.size(); // should be 2 for freshly encrypted + bool is_ntt = ct.is_ntt_form(); + assert(ct_nprimes); assert(ct_size >= 2); string ct_name_base = is_ntt ? "(ntt) ct" : " ct"; @@ -292,10 +347,10 @@ void print_ct(Ciphertext &ct, size_t print_size) for (size_t i = 0; i < ct_size; i++) { string ct_name_base_i = ct_name_base + to_string(i) + "["; - for (size_t j = 0; j < ct_mod_size; j++) + for (size_t j = 0; j < ct_nprimes; j++) { string ct_name_ij = ct_name_base_i + to_string(j) + "]"; - size_t offset = n * (ct_mod_size * i + j); + size_t offset = n * (ct_nprimes * i + j); print_poly(ct_name_ij, get_ct_arr_ptr(ct) + offset, print_size); } } @@ -306,8 +361,8 @@ void print_pk(string name, PublicKeyWrapper &pk_wr, size_t print_size, bool prin size_t n = pk_wr.pk->data().poly_modulus_degree(); size_t nprimes = pk_wr.pk->data().coeff_modulus_size(); bool is_ntt = pk_wr.is_ntt; - assert(nprimes >= 2); assert(pk_wr.pk->data().size() == 2); + assert(print_sp || nprimes > 1); string base = is_ntt ? "(ntt form) " : "(regular form) "; cout << endl; @@ -325,7 +380,7 @@ void print_pk(string name, PublicKeyWrapper &pk_wr, size_t print_size, bool prin } void print_sk_compare(string name1, SecretKey &sk1, string name2, SecretKey &sk2, - const SEALContext &context, size_t print_size, bool print_sp) + const seal::SEALContext &context, size_t print_size, bool print_sp) { auto &parms_id1 = (sk1.parms_id() == parms_id_zero) ? context.key_parms_id() : sk1.parms_id(); auto sk1_context_data_ptr = context.get_context_data(parms_id1); @@ -333,7 +388,7 @@ void print_sk_compare(string name1, SecretKey &sk1, string name2, SecretKey &sk2 size_t n = parms1.poly_modulus_degree(); size_t nprimes = parms1.coeff_modulus().size(); bool is_ntt = sk1.data().is_ntt_form(); - assert(nprimes >= 2); + assert(print_sp || nprimes > 1); auto &parms_id2 = (sk2.parms_id() == parms_id_zero) ? context.key_parms_id() : sk2.parms_id(); auto sk2_context_data_ptr = context.get_context_data(parms_id2); @@ -362,9 +417,9 @@ void print_pk_compare(string name1, const PublicKeyWrapper &pk1_wr, string name2 assert(pk1_wr.pk->data().size() == 2); assert(pk1_wr.pk->data().size() == pk2_wr.pk->data().size()); - assert(nprimes >= 2); // minimum is 2 - assert(nprimes == pk2_wr.pk->data().coeff_modulus_size()); assert(n == pk2_wr.pk->data().poly_modulus_degree()); + assert(nprimes == pk2_wr.pk->data().coeff_modulus_size()); + assert(print_sp || nprimes > 1); assert(is_ntt == pk2_wr.is_ntt); string base = is_ntt ? "(ntt form) " : "(regular form) "; diff --git a/adapter/utils.h b/adapter/utils.h index 5e0bf18..3e9b4eb 100644 --- a/adapter/utils.h +++ b/adapter/utils.h @@ -12,10 +12,17 @@ #include #include #include +#include #include "generate.h" #include "seal/seal.h" +/** +Uncomment to use the 27-bit default for n=4K instead of the 30-bit default. +Make sure to uncomment SE_DEFAULT_4K_27BIT in the device (see: user_defines.h) as well. +*/ +// #define SEALE_DEFAULT_4K_27BIT + /** Exits the program when an error is detected. @@ -34,16 +41,8 @@ inline void exit_on_err(int err, std::string msg) // ---------------- Setup ------------------ // ----------------------------------------- /** -Helper function for setup_seal. Sets default SEAL-Embedded seal::Modulus values for testing. - -@param[in] nprimes Number of primes to set -@param[in,out] vec Vector of moduli -*/ -void set_modulus_testing_values(std::size_t nprimes, std::vector &vec); - -/** -Creates SEAL context based on custom-chosen moduli. Verifies compatibility with -SEAL-Embedded (i.e., all moduli other than the special prime must be <= 30 bits). +Creates SEAL context based on custom-chosen moduli. Verifies compatibility with SEAL-Embedded (i.e., +all moduli other than the special prime must be <= 30 bits). @param[in] degree Polynomial ring degree @param[in] moduli Moduli @@ -54,25 +53,31 @@ seal::SEALContext setup_seale_custom(std::size_t degree, const std::vector -bool are_equal_poly(T *a, T *b, std::size_t n, double diff = 0.4) +bool are_equal_poly(T *a, T *b, std::size_t nvals, double diff = 0.4) { bool is_error = false; if (std::is_same::value) { - for (std::size_t i = 0; i < n; i++) + for (std::size_t i = 0; i < nvals; i++) { - double abs_val = fabs(a[i] - b[i]); + double abs_val = std::fabs(a[i] - b[i]); if (abs_val >= diff) { std::streamsize ss = std::cout.precision(); // save original precision std::cout << std::setprecision(9); std::cout << "a[" << i << "]: " << a[i] << std::endl; std::cout << "b[" << i << "]: " << b[i] << std::endl; - std::cout.precision(ss); + std::cout.precision(ss); // restore precision } if (abs_val >= diff) is_error = true; assert(!is_error); @@ -226,7 +231,7 @@ bool are_equal_poly(T *a, T *b, std::size_t n, double diff = 0.4) } else if (std::is_same::value) { - is_error = memcmp(a, b, n * sizeof(T)); + is_error = memcmp(a, b, nvals * sizeof(T)); assert(!is_error); } else @@ -238,22 +243,20 @@ bool are_equal_poly(T *a, T *b, std::size_t n, double diff = 0.4) } /** -TODO:? -Compares the first element of two polynomial objects to see if they have the same value. - -@param[in] a Object 1 -@param[in] b Object 2 -@param[in] n Number of elements in object -@param[in] diff Maximum allowed difference between values (only applicable for objects - with non-integer values) -@returns True if a and b have the same values, False otherwise +Compares two polynomial objects to see if they have the same value. + +@param[in] a Object 1 +@param[in] b Object 2 +@param[in] nvals Number of elements of a and b to compare +@param[in] diff Maximum allowed difference between values (only applicable for objects with + non-integer values) +@returns True if a and b have the same values, False otherwise */ template -bool are_equal_poly(std::vector &a, std::vector &b, std::size_t s_in, double diff = 0.4) +bool are_equal_poly(std::vector &a, std::vector &b, std::size_t nvals, double diff = 0.4) { - std::size_t s = s_in; - if (s == 0) s = b.size(); - return are_equal_poly(&(a[0]), &(b[0]), s, diff); + assert(nvals <= a.size() && nvals <= b.size()); + return are_equal_poly(&(a[0]), &(b[0]), nvals, diff); } // -------------------------------------------- @@ -325,22 +328,21 @@ void print_poly(std::string pname, T *poly, std::size_t print_size, int prec = 2 { std::streamsize ss = std::cout.precision(); // save original precision bool is_double = std::is_same::value; - if (is_double) std::cout << std::setprecision(prec); + if (is_double) { std::cout << std::setprecision(prec); } std::cout << pname << " : { "; for (std::size_t i = 0; i < print_size; i++) { std::cout << poly[i]; - if (i < (print_size - 1)) std::cout << ", "; + if (i < (print_size - 1)) { std::cout << ", "; } } std::cout << " }" << std::endl; - if (is_double) std::cout.precision(ss); // restore precision + if (is_double) { std::cout.precision(ss); } // restore precision } /** -TODO:? -Prints the first element of a polynomial object to stdout. +Prints a polynomial object to stdout. @param[in] pname Name of object @param[in] poly Polynomial object @@ -348,18 +350,20 @@ Prints the first element of a polynomial object to stdout. @param[in] prec Precision with which to print polynomial values */ template -void print_poly(std::string pname, std::vector &poly, std::size_t psize, int prec = 2) +void print_poly(std::string pname, std::vector &poly, std::size_t print_size, int prec = 2) { - print_poly(pname, &(poly[0]), psize, prec); + print_poly(pname, &(poly[0]), print_size, prec); } /** -Overloaded << operator for params_id. Prints the `parms_id' to std::ostream. +Overloaded << operator for parms_id. Prints the `parms_id' to std::ostream. (Note: This is modified from SEAL/native/examples/examples.h.) + +@param[in] parms_id parms_id object to print */ inline std::ostream &operator<<(std::ostream &stream, seal::parms_id_type parms_id) { - // Save the formatting information for std::cout. + // -- Save the formatting information for std::cout. std::ios old_fmt(nullptr); old_fmt.copyfmt(std::cout); @@ -367,15 +371,15 @@ inline std::ostream &operator<<(std::ostream &stream, seal::parms_id_type parms_ << parms_id[1] << " " << std::setw(16) << parms_id[2] << " " << std::setw(16) << parms_id[3] << " "; - // Restore the old std::cout formatting. + // -- Restore the old std::cout formatting. std::cout.copyfmt(old_fmt); return stream; } /** -Prints the parameters in a SEALContext to stdout. Modified from -SEAL/native/examples/examples.h. +Prints the parameters in a SEALContext to stdout. +(Note: This is modified from SEAL/native/examples/examples.h.) @param[in] context SEAL context */ @@ -388,15 +392,13 @@ inline void print_parameters(const seal::SEALContext &context) std::cout << "| poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << std::endl; - // Print the size of the true (product) coefficient seal::Modulus. + // -- Print the size of the true (product) coefficient modulus. std::cout << "| coeff_modulus size: "; std::cout << context_data.total_coeff_modulus_bit_count() << " ("; - auto coeff_modulus = context_data.parms().coeff_modulus(); - std::size_t coeff_modulus_size = coeff_modulus.size(); - for (std::size_t i = 0; i < coeff_modulus_size - 1; i++) - { - std::cout << coeff_modulus[i].bit_count() << " + "; - } + auto coeff_modulus = context_data.parms().coeff_modulus(); + std::size_t nprimes = coeff_modulus.size(); + for (std::size_t i = 0; i < nprimes - 1; i++) + { std::cout << coeff_modulus[i].bit_count() << " + "; } std::cout << coeff_modulus.back().bit_count(); std::cout << ") bits" << std::endl; std::cout << "\\" << std::endl; diff --git a/device/CMakeLists.txt b/device/CMakeLists.txt index 11be47c..9f7ad6d 100644 --- a/device/CMakeLists.txt +++ b/device/CMakeLists.txt @@ -14,6 +14,7 @@ option(SE_BUILD_LOCAL "Build for native instead of device" ON) option(SE_BUILD_M4 "Build for M4 instead of for A7" OFF) # -- 3. If m4, azure sphere or nrf (on = sphere) +# -- NOTE: Stack size issue with Sphere m4 prevents this deployment from running in most cases option(SE_M4_IS_SPHERE "Use Azure Sphere m4" OFF) # -- This needs to be above the 'project' line @@ -29,25 +30,28 @@ if(NOT SE_BUILD_LOCAL) endif() endif() -project(SEAL_EMBEDDED VERSION 1.0.0 LANGUAGES C ASM) +project(SEAL_EMBEDDED VERSION 1.1.0 LANGUAGES C ASM) ################# # Configuration # ################# -# Always build position-independent-code +# -- Always build position-independent-code set(CMAKE_POSITION_INDEPENDENT_CODE ON) -# Set the directory that stores output files generated by SEAL-Embedded Adapter +# -- Set the directory that stores output files generated by SEAL-Embedded Adapter set(SE_ADAPTER_FILE_OUTPUT_DIR ${CMAKE_CURRENT_LIST_DIR}/adapter_output_data CACHE STRING "Adapter file directory" FORCE) message(STATUS "Adapter file directory: ${SE_ADAPTER_FILE_OUTPUT_DIR}") -if(NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/sk_4096.dat OR - NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/index_map_4096.dat OR - NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ifft_roots_4096.dat OR - NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1071415297.dat OR - NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1071513601.dat OR - NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1072496641.dat) - message(FATAL_ERROR "Run adapter first to generate key and precomputation data") +if (NOT SE_BUILD_LOCAL AND NOT SE_BUILD_M4) + # -- Note: Comment out or modify this section if using a different deployment setting + if(NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/sk_4096.dat OR + NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/index_map_4096.dat OR + NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ifft_roots_4096.dat OR + NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1053818881.dat OR + NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1054015489.dat OR + NOT EXISTS ${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1054212097.dat) + message(FATAL_ERROR "Run adapter first to generate key and precomputation data") + endif() endif() # -- 4. Build type @@ -59,9 +63,9 @@ endif() message(STATUS "Build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}") if(CMAKE_BUILD_TYPE STREQUAL "Debug") - # In Debug mode, enable extra compiler flags. + # -- In Debug mode, enable extra compiler flags. include(CheckCXXCompilerFlag) - # For easier adding of CXX compiler flags + # -- For easier adding of CXX compiler flags function(se_enable_cxx_compiler_flag_if_supported flag) string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set) if(flag_already_set EQUAL -1) @@ -99,7 +103,7 @@ if(NOT SE_BUILD_LOCAL AND (NOT SE_BUILD_M4 OR SE_M4_IS_SPHERE)) # -- 7. Set this path to the path to the OS_HAL folder on your setup set(OS_HAL_DIR_PATH "../../mt3620_m4_software/MT3620_M4_Sample_Code/OS_HAL") - # -- 8. Set this path to the path to the Driver path in your setup + # -- 8. Set this path to the path to the driver in your setup set(MT3620_M4_DRIVER_PATH "../../mt3620_m4_software/MT3620_M4_Driver") set(MT3620_M4_BSP_PATH "../../mt3620_m4_software/MT3620_M4_BSP") @@ -108,53 +112,55 @@ if(NOT SE_BUILD_LOCAL AND (NOT SE_BUILD_M4 OR SE_M4_IS_SPHERE)) endif() endif() +set(SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE adapter_output_data CACHE STRING "Adapter file directory (build)" FORCE) + if(NOT SE_BUILD_LOCAL AND NOT SE_BUILD_M4) # -- This means we are targetting the sphere a7 # -- 9. Uncomment files corresponding to the desired config. set(RESOURCE_FILES # -- Secret key - "${SE_ADAPTER_FILE_OUTPUT_DIR}/sk_4096.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/sk_4096.dat" # -- Public key - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk0_ntt_4096_1071415297.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk0_ntt_4096_1071513601.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk0_ntt_4096_1072496641.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk1_ntt_4096_1071415297.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk1_ntt_4096_1071513601.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/pk1_ntt_4096_1072496641.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk0_ntt_4096_1053818881.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk0_ntt_4096_1054015489.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk0_ntt_4096_1054212097.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk1_ntt_4096_1053818881.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk1_ntt_4096_1054015489.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/pk1_ntt_4096_1054212097.dat" # -- Index map - "${SE_ADAPTER_FILE_OUTPUT_DIR}/index_map_4096.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/index_map_4096.dat" # -- IFFT Roots - "${SE_ADAPTER_FILE_OUTPUT_DIR}/ifft_roots_4096.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ifft_roots_4096.dat" # -- Regular NTT roots - "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1071415297.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1071513601.dat" - "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_roots_4096_1072496641.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_roots_4096_1053818881.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_roots_4096_1054015489.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_roots_4096_1054212097.dat" # -- "Fast" NTT roots - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_fast_roots_4096_1071415297.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_fast_roots_4096_1071513601.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/ntt_fast_roots_4096_1072496641.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_fast_roots_4096_1053818881.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_fast_roots_4096_1054015489.dat" + "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/ntt_fast_roots_4096_1054212097.dat" - # ---------------------------------------- - # -- The following are for testing only -- - # ---------------------------------------- + # -------------------------------------------------------- + # -- The following are for certain testing configs only -- + # -------------------------------------------------------- # -- FFT Roots - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/fft_roots_4096.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/fft_roots_4096.dat" # -- Regular INTT roots - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_roots_4096_1071415297.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_roots_4096_1071513601.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_roots_4096_1072496641.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_roots_4096_1053818881.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_roots_4096_1054015489.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_roots_4096_1054212097.dat" # -- "Fast" INTT roots - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_fast_roots_4096_1071415297.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_fast_roots_4096_1071513601.dat" - # "${SE_ADAPTER_FILE_OUTPUT_DIR}/intt_fast_roots_4096_1072496641.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_fast_roots_4096_1053818881.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_fast_roots_4096_1054015489.dat" + # "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}/intt_fast_roots_4096_1054212097.dat" ) endif() @@ -162,7 +168,7 @@ if(NOT SE_BUILD_LOCAL AND SE_BUILD_M4 AND SE_M4_IS_SPHERE) # On Sphere M4 add_compile_definitions(OSAI_BARE_METAL) add_compile_definitions(OSAI_ENABLE_DMA) - # When place CODE_REGION in FLASH instead of TCM, please enable this definition: + # -- Required when placing CODE_REGION in FLASH instead of TCM (see: Sphere documentation) add_compile_definitions(M4_ENABLE_XIP_FLASH) add_link_options(-specs=nano.specs -specs=nosys.specs) # add_link_options(-specs=nosys.specs) @@ -181,7 +187,6 @@ else() set(SPHERE_M4_SRC_FILES "") endif() - ##################################################################### # End of configuration section # ##################################################################### @@ -195,7 +200,6 @@ message(STATUS "BUILD LOCAL (SE_BUILD_LOCAL): ${SE_BUILD_LOCAL}") message(STATUS "CMAKE build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}") message(STATUS "SEAL-Embedded build type (SE_BUILD_TYPE): ${SE_BUILD_TYPE}") - if(NOT SE_BUILD_LOCAL) message(STATUS "BUILD M4 (SE_BUILD_M4): ${SE_BUILD_M4}") if(SE_BUILD_M4) @@ -278,9 +282,9 @@ endif() # ------------------------------- # --- Set defines for library --- # ------------------------------- -add_definitions(-DSE_DATA_PATH="${SE_ADAPTER_FILE_OUTPUT_DIR}") -string(LENGTH "${SE_ADAPTER_FILE_OUTPUT_DIR}" SE_ADAPTER_FILE_OUTPUT_DIR_LEN) -add_definitions(-DSE_DATA_PATH_LEN=${SE_ADAPTER_FILE_OUTPUT_DIR_LEN}) +add_definitions(-DSE_DATA_PATH="${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}") +string(LENGTH "${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE}" SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE_LEN) +add_definitions(-DSE_DATA_PATH_LEN=${SE_ADAPTER_FILE_OUTPUT_DIR_DEVICE_LEN}) if(NOT SE_BUILD_LOCAL) if(SE_BUILD_M4) diff --git a/device/bench/bench_asym.c b/device/bench/bench_asym.c index e558692..22107ae 100644 --- a/device/bench/bench_asym.c +++ b/device/bench/bench_asym.c @@ -33,20 +33,19 @@ void bench_asym(void) { // -- Config -- #ifdef SE_USE_MALLOC - size_t n = 4096; - size_t nprimes = 3; + const size_t n = 4096; + const size_t nprimes = 3; #else - size_t n = SE_DEGREE_N; - size_t nprimes = SE_NPRIMES; + const size_t n = SE_DEGREE_N; + const size_t nprimes = SE_NPRIMES; #endif - double scale = (n > 1024) ? pow(2, 25) : pow(2, 20); // ------------ Parms parms; - parms.scale = scale; - parms.pk_from_file = 1; - parms.is_asymmetric = 1; - parms.sample_s = 0; + parms.pk_from_file = true; + parms.is_asymmetric = true; + parms.sample_s = false; + parms.small_u = true; // should be this #ifdef SE_USE_MALLOC print_ckks_mempool_size(n, 0); @@ -54,6 +53,7 @@ void bench_asym(void) #else print_ckks_mempool_size(); ZZ mempool_local[MEMPOOL_SIZE]; + memset(*mempool_local, 0, MEMPOOL_SIZE * sizeof(ZZ)); ZZ *mempool = &(mempool_local[0]); #endif @@ -82,7 +82,7 @@ void bench_asym(void) print_bench_banner(bench_name, &parms); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -116,9 +116,8 @@ void bench_asym(void) #if defined(SE_BENCH_ENCRYPT) || defined(SE_BENCH_FULL) reset_start_timer(&timer); #endif - ckks_encode_encrypt_asym(&parms, conj_vals_int, u, e1, ntt_roots, pk_c0, pk_c1, 0, - ntt_u_e1_pte, - 0); // TODO: THIS INTERFACE MIGHT BE WRONG + ckks_encode_encrypt_asym(&parms, conj_vals_int, u, e1, ntt_roots, ntt_u_e1_pte, NULL, + NULL, pk_c0, pk_c1); #if defined(SE_BENCH_ENCRYPT) || defined(SE_BENCH_FULL) stop_timer(&timer); @@ -140,7 +139,11 @@ void bench_asym(void) print_bench_banner(bench_name, &parms); #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } delete_parameters(&parms); #endif } diff --git a/device/bench/bench_common.h b/device/bench/bench_common.h index 184d324..dcc5154 100644 --- a/device/bench/bench_common.h +++ b/device/bench/bench_common.h @@ -55,9 +55,7 @@ static inline void random_zzq_poly(ZZ *poly, size_t n, Modulus *q) static inline void gen_double_complex_half_vec(double complex *vec, int64_t div, size_t n) { for (size_t i = 0; i < n; i++) - { - vec[i] = (double complex)_complex(gen_double_half(div), gen_double_half(div)); - } + { vec[i] = (double complex)_complex(gen_double_half(div), gen_double_half(div)); } } static inline double gen_double_quarter(int64_t div) @@ -79,8 +77,7 @@ static inline void gen_flpt_quarter_poly(flpt *poly, int64_t div, size_t n) static inline void print_bench_banner(const char *benchmark_name, const Parms *parms) { #if defined(SE_ON_NRF5) && defined(SE_NRF5_UART_PRINTF_ENABLED) - return; // Printing banner on the NRF5 via serial doesn't work well. Just - // don't do it. + return; // Printing banner on the NRF5 via serial doesn't work well. Just don't do it. #endif printf("***************************************************\n"); printf("Running Benchmark: %s\n", benchmark_name); diff --git a/device/bench/bench_ifft.c b/device/bench/bench_ifft.c index a64e986..f314a3c 100644 --- a/device/bench/bench_ifft.c +++ b/device/bench/bench_ifft.c @@ -8,7 +8,6 @@ #include "defines.h" #ifdef SE_ENABLE_TIMERS - #include #include #include @@ -41,16 +40,11 @@ void bench_ifft(void) { - // ================================ - // Configuration - // ================================ - // PolySizeType n = 16; #ifdef SE_USE_MALLOC - PolySizeType n = 4096; + const PolySizeType n = 4096; #else - PolySizeType n = SE_DEGREE_N; + const PolySizeType n = SE_DEGREE_N; #endif - // ================================ size_t logn = get_log2(n); size_t size_mult = sizeof(double complex) / sizeof(ZZ); @@ -66,6 +60,7 @@ void bench_ifft(void) ZZ *mempool = calloc(mempool_size, sizeof(ZZ)); #else ZZ mempool[SE_DEGREE_N + IFFT_TEST_ROOTS_MEM]; + memset(&mempool, 0, (SE_DEGREE_N + IFFT_TEST_ROOTS_MEM) * sizeof(ZZ)); #endif double complex *vec = (double complex *)mempool; @@ -89,7 +84,7 @@ void bench_ifft(void) print_bench_banner(bench_name, &parms); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0; volatile float t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) @@ -133,8 +128,13 @@ void bench_ifft(void) print_bench_banner(bench_name, &parms); #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } #endif + delete_parameters(&parms); } #endif diff --git a/device/bench/bench_index_map.c b/device/bench/bench_index_map.c index 80b82dd..2603f53 100644 --- a/device/bench/bench_index_map.c +++ b/device/bench/bench_index_map.c @@ -21,11 +21,12 @@ void bench_index_map(void) { #ifdef SE_USE_MALLOC - PolySizeType n = 4096; - uint16_t *vec = calloc(n, sizeof(uint16_t)); + const PolySizeType n = 4096; + uint16_t *vec = calloc(n, sizeof(uint16_t)); #else - PolySizeType n = SE_DEGREE_N; + const PolySizeType n = SE_DEGREE_N; uint16_t vec[SE_DEGREE_N]; + memset(&vec, 0, SE_DEGREE_N * sizeof(uint16_t)); #endif uint16_t *index_map = &(vec[0]); @@ -36,7 +37,7 @@ void bench_index_map(void) print_bench_banner(bench_name, &parms); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -45,7 +46,8 @@ void bench_index_map(void) #if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_OTF) ckks_calc_index_map(&parms, index_map); -#elif defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) +#elif defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) load_index_map(&parms, index_map); #endif @@ -66,8 +68,13 @@ void bench_index_map(void) print_bench_banner(bench_name, &parms); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } #endif + delete_parameters(&parms); } #endif diff --git a/device/bench/bench_ntt.c b/device/bench/bench_ntt.c index 13ef849..faf5321 100644 --- a/device/bench/bench_ntt.c +++ b/device/bench/bench_ntt.c @@ -8,7 +8,6 @@ #include "defines.h" #if defined(SE_ENABLE_TIMERS) - #include #include @@ -43,12 +42,12 @@ void bench_ntt(void) { #ifdef SE_USE_MALLOC - PolySizeType n = 4096; + const PolySizeType n = 4096; #else - PolySizeType n = SE_DEGREE_N; + const PolySizeType n = SE_DEGREE_N; #endif - size_t vec_size = n; + const size_t vec_size = n; #ifdef SE_USE_MALLOC @@ -64,6 +63,7 @@ void bench_ntt(void) ZZ *mempool = calloc(mempool_size, sizeof(ZZ)); #else ZZ mempool[SE_DEGREE_N + NTT_TESTS_ROOTS_MEM]; + memset(&mempool, 0, (SE_DEGREE_N + NTT_TEST_ROOTS_MEM) * sizeof(ZZ)); #endif ZZ *vec = mempool; @@ -88,7 +88,7 @@ void bench_ntt(void) print_bench_banner(bench_name, &parms); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -131,8 +131,13 @@ void bench_ntt(void) print_bench_banner(bench_name, &parms); #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } #endif + delete_parameters(&parms); } #endif diff --git a/device/bench/bench_sample.c b/device/bench/bench_sample.c index a5e6ba0..0aac65f 100644 --- a/device/bench/bench_sample.c +++ b/device/bench/bench_sample.c @@ -6,9 +6,7 @@ */ #include "defines.h" - #ifdef SE_ENABLE_TIMERS - #include "bench_common.h" #include "sample.h" #include "timer.h" @@ -17,10 +15,10 @@ void bench_sample_poly_cbd(void) { #ifdef SE_USE_MALLOC - size_t n = 4096; - int8_t *vec = calloc(n, sizeof(int8_t)); + const size_t n = 4096; + int8_t *vec = calloc(n, sizeof(int8_t)); #else - size_t n = SE_DEGREE_N; + const size_t n = SE_DEGREE_N; int8_t vec[SE_DEGREE_N]; #endif @@ -32,7 +30,7 @@ void bench_sample_poly_cbd(void) prng_randomize_reset(&prng, NULL); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -47,29 +45,35 @@ void bench_sample_poly_cbd(void) } print_time_vals(bench_name, t_curr, COUNT, &t_total, &t_min, &t_max); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } #endif } void bench_sample_ternary_small(void) { - size_t n = 4096; - Parms parms; - parms.small_u = 1; - set_parms_ckks(n, 1, &parms); - #ifdef SE_USE_MALLOC - ZZ *vec = malloc(n / 4); + const size_t n = 4096; + ZZ *vec = malloc(n / 4); #else + const size_t n = SE_DEGREE_N; ZZ vec[SE_DEGREE_N / 4]; #endif + + Parms parms; + parms.small_u = 1; + set_parms_ckks(n, 1, &parms); + ZZ *poly = &(vec[0]); const char *bench_name = "sample poly ternary (small)"; print_bench_banner(bench_name, &parms); SE_PRNG prng; prng_randomize_reset(&prng, NULL); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -84,22 +88,28 @@ void bench_sample_ternary_small(void) } print_time_vals(bench_name, t_curr, COUNT, &t_total, &t_min, &t_max); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } delete_parameters(&parms); #endif } void bench_sample_uniform(void) { - size_t n = 4096; - Parms parms; - set_parms_ckks(n, 1, &parms); #ifdef SE_USE_MALLOC - ZZ *vec = calloc(n, sizeof(ZZ)); + const size_t n = 4096; + ZZ *vec = calloc(n, sizeof(ZZ)); #else + const size_t n = SE_DEGREE_N; ZZ vec[SE_DEGREE_N]; #endif + Parms parms; + set_parms_ckks(n, 1, &parms); + ZZ *poly = &(vec[0]); const char *bench_name = "sample poly uniform"; print_bench_banner(bench_name, &parms); @@ -108,7 +118,7 @@ void bench_sample_uniform(void) prng_randomize_reset(&prng, NULL); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -123,7 +133,11 @@ void bench_sample_uniform(void) } print_time_vals(bench_name, t_curr, COUNT, &t_total, &t_min, &t_max); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } delete_parameters(&parms); #endif } @@ -136,7 +150,7 @@ void bench_prng_randomize_seed(void) SE_PRNG prng; Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -154,13 +168,14 @@ void bench_prng_randomize_seed(void) void bench_prng_fill_buffer(void) { - size_t n = 4096; const char *bench_name = "prng fill buffer"; print_bench_banner(bench_name, 0); #ifdef SE_USE_MALLOC - ZZ *vec = calloc(n, sizeof(ZZ)); + const size_t n = 4096; + ZZ *vec = calloc(n, sizeof(ZZ)); #else + const size_t n = SE_DEGREE_N; ZZ vec[SE_DEGREE_N]; #endif ZZ *poly = &(vec[0]); @@ -168,7 +183,7 @@ void bench_prng_fill_buffer(void) SE_PRNG prng; Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -184,20 +199,24 @@ void bench_prng_fill_buffer(void) } print_time_vals(bench_name, t_curr, COUNT, &t_total, &t_min, &t_max); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } #endif } void bench_prng_randomize_seed_fill_buffer(void) { - size_t n = 4096; - const char *bench_name = "prng randomize + fill buffer"; print_bench_banner(bench_name, 0); #ifdef SE_USE_MALLOC - ZZ *vec = calloc(n, sizeof(ZZ)); + const size_t n = 4096; + ZZ *vec = calloc(n, sizeof(ZZ)); #else + const size_t n = SE_DEGREE_N; ZZ vec[SE_DEGREE_N]; #endif ZZ *poly = &(vec[0]); @@ -205,7 +224,7 @@ void bench_prng_randomize_seed_fill_buffer(void) SE_PRNG prng; Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -221,7 +240,11 @@ void bench_prng_randomize_seed_fill_buffer(void) } print_time_vals(bench_name, t_curr, COUNT, &t_total, &t_min, &t_max); #ifdef SE_USE_MALLOC - free(vec); + if (vec) + { + free(vec); + vec = 0; + } #endif } #endif diff --git a/device/bench/bench_sym.c b/device/bench/bench_sym.c index 1dc3382..6989642 100644 --- a/device/bench/bench_sym.c +++ b/device/bench/bench_sym.c @@ -29,23 +29,20 @@ void bench_sym(void) { - // ========================= - // Configuration - // ========================= #ifdef SE_USE_MALLOC - size_t n = 4096; - size_t nprimes = 3; + const size_t n = 4096; + const size_t nprimes = 3; #else - size_t n = SE_DEGREE_N; - size_t nprimes = SE_NPRIMES; + const size_t n = SE_DEGREE_N; + const size_t nprimes = SE_NPRIMES; #endif Parms parms; - parms.scale = (n > 1024) ? pow(2, 25) : pow(2, 20); - // ========================= - parms.is_asymmetric = 0; - parms.small_s = 1; - parms.sample_s = 0; - if (!parms.sample_s) se_assert(parms.small_s); // Make sure we didn't set this accidentally + parms.is_asymmetric = false; + parms.small_s = true; + parms.sample_s = false; + + // -- Make sure we didn't set this accidentally + if (!parms.sample_s) se_assert(parms.small_s); #ifdef SE_USE_MALLOC print_ckks_mempool_size(n, 1); @@ -53,10 +50,11 @@ void bench_sym(void) #else print_ckks_mempool_size(); ZZ mempool_local[MEMPOOL_SIZE]; + memset(&mempool_local, 0, MEMPOOL_SIZE * sizeof(ZZ)); ZZ *mempool = &(mempool_local[0]); #endif - // Get pointers + // -- Get pointers SE_PTRS se_ptrs_local; ckks_set_ptrs_sym(n, mempool, &se_ptrs_local); double complex *conj_vals = se_ptrs_local.conj_vals; @@ -85,7 +83,7 @@ void bench_sym(void) print_bench_banner(bench_name, &parms); Timer timer; - size_t COUNT = 10; + const size_t COUNT = 10; float t_total = 0, t_min = 0, t_max = 0, t_curr = 0; for (size_t b_itr = 0; b_itr < COUNT + 1; b_itr++) { @@ -150,7 +148,11 @@ void bench_sym(void) print_bench_banner(bench_name, &parms); #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } delete_parameters(&parms); #endif } diff --git a/device/bench/main.c b/device/bench/main.c index 90cec7a..6e73857 100644 --- a/device/bench/main.c +++ b/device/bench/main.c @@ -3,12 +3,15 @@ /** @file main.c + +Note: While the benchmarks can run back-to-back, on device it is best to run only one benchmark at a +time (i.e., uncomment the benchmark you want to run and uncomment all others before compiling.) */ #include "defines.h" -#include "util_print.h" #ifdef SE_ENABLE_TIMERS +#include "util_print.h" // -- Benchmarks extern void bench_index_map(void); @@ -23,31 +26,71 @@ extern void bench_sample_poly_cbd(void); extern void bench_sym(void); extern void bench_asym(void); +#ifdef SE_ON_SPHERE_M4 +#include "mt3620.h" +#include "os_hal_gpt.h" +#include "os_hal_uart.h" + +static const uint8_t uart_port_num = OS_HAL_UART_PORT0; +// static const uint8_t uart_port_num = OS_HAL_UART_ISU0; + +/** +Hook for "printf" +@param[in] character Character to print +*/ +// /* +void _putchar(char character) +{ + mtk_os_hal_uart_put_char(uart_port_num, character); + if (character == '\n') mtk_os_hal_uart_put_char(uart_port_num, '\r'); +} +// */ + +void RTCoreMain(void) +{ + // -- Init Vector Table -- + NVIC_SetupVectorTable(); + + // -- Init UART -- + mtk_os_hal_uart_ctlr_init(uart_port_num); + printf("\nUART Inited (port_num=%d)\n", uart_port_num); + + // -- Init GPT -- + // gpt0_int.gpt_cb_hdl = Gpt0Callback; + // gpt0_int.gpt_cb_data = (void *)gpt_cb_data; + mtk_os_hal_gpt_init(); +#else + int main(void) { - #ifdef SE_NRF5_UART_PRINTF_ENABLED +#ifdef SE_NRF5_UART_PRINTF_ENABLED se_setup_uart(); - #endif - se_randomness_init(); // required for nrf. does nothing if not on nrf +#endif +#endif + + printf("Beginning bench...\n"); +#ifdef SE_RAND_NRF5 + se_randomness_init(); // required for nrf +#endif // -- Uncomment benchmark to run + // -- Note: All benchmarks use n = 4K, 3 primes if malloc is used, + // SE_DEGREE_N and SE_NPRIMES otherwise + // TODO: make benchmarks easily configurable for other degrees + bench_index_map(); - // bench_ifft(); - // bench_ntt(); - // bench_prng_randomize_seed(); - // bench_prng_fill_buffer(); - // bench_prng_randomize_seed_fill_buffer(); - // bench_sample_uniform(); - // bench_sample_ternary_small(); - // bench_sample_poly_cbd(); - // bench_sym(); - #ifdef SE_DEFINE_PK_DATA + bench_ifft(); + bench_ntt(); + bench_prng_randomize_seed(); + bench_prng_fill_buffer(); + bench_prng_randomize_seed_fill_buffer(); + bench_sample_uniform(); + bench_sample_ternary_small(); + bench_sample_poly_cbd(); + bench_sym(); +#if defined(SE_USE_MALLOC) || defined(SE_DEFINE_PK_DATA) bench_asym(); - #endif +#endif printf("...done with all benchmarks!\n"); } -#else -int main(void) -{ -} #endif diff --git a/device/lib/ckks_asym.c b/device/lib/ckks_asym.c index 6d23fc3..00d8995 100644 --- a/device/lib/ckks_asym.c +++ b/device/lib/ckks_asym.c @@ -34,24 +34,24 @@ size_t ckks_get_mempool_size_asym(size_t degree) size_t n = degree; size_t mempool_size = 4 * n; // base - #ifdef SE_IFFT_OTF +#ifdef SE_IFFT_OTF mempool_size += n + n / 4 + n / 16; - #if defined(SE_NTT_ONE_SHOT) || defined(SE_NTT_REG) +#if defined(SE_NTT_ONE_SHOT) || defined(SE_NTT_REG) mempool_size += n; - #elif defined(SE_NTT_FAST) +#elif defined(SE_NTT_FAST) mempool_size += 2 * n; - #endif - #else +#endif +#else mempool_size += 4 * n; - #endif +#endif - #if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) +#if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) mempool_size += n / 2; - #endif +#endif - #ifdef SE_MEMPOOL_ALLOC_VALUES +#ifdef SE_MEMPOOL_ALLOC_VALUES mempool_size += n / 2; - #endif +#endif se_assert(mempool_size); return mempool_size; @@ -61,6 +61,7 @@ ZZ *ckks_mempool_setup_asym(size_t degree) { size_t mempool_size = ckks_get_mempool_size_asym(degree); ZZ *mempool = calloc(mempool_size, sizeof(ZZ)); + // printf("mempool_size: %zu\n", mempool_size); if (!mempool) { printf("Error! Allocation failed. Exiting...\n"); @@ -112,7 +113,7 @@ void ckks_set_ptrs_asym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) size_t total_block2_size = ifft_roots_size ? ifft_roots_size : (ntt_roots_size + n); // -- Set pi inverse based on index map type -#if defined(SE_INDEX_MAP_LOAD) +#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) se_ptrs->index_map_ptr = (uint16_t *)&(mempool[4 * n]); #elif defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) // -- If ifft, this will be + the ifft_roots size @@ -120,26 +121,23 @@ void ckks_set_ptrs_asym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) se_ptrs->index_map_ptr = (uint16_t *)&(mempool[4 * n + total_block2_size]); index_map_persist_size = n / 2; #endif - se_ptrs->ntt_pte_ptr = &(mempool[4 * n + ntt_roots_size]); + #ifdef SE_IFFT_OTF - se_ptrs->e1_ptr = - (int8_t *)&(mempool[4 * n + ntt_roots_size + n + index_map_persist_size]); - se_ptrs->ternary = - &(mempool[4 * n + ntt_roots_size + n + index_map_persist_size + n / 4]); + se_ptrs->e1_ptr = (int8_t *)&(mempool[4 * n + ntt_roots_size + n + index_map_persist_size]); + se_ptrs->ternary = &(mempool[4 * n + ntt_roots_size + n + index_map_persist_size + n / 4]); #else se_ptrs->e1_ptr = (int8_t *)&(mempool[4 * n + ntt_roots_size + n]); se_ptrs->ternary = &(mempool[4 * n + ntt_roots_size + n + n / 4]); #endif #ifdef SE_MEMPOOL_ALLOC_VALUES - #ifdef SE_IFFT_OTF - se_ptrs->values = (flpt *)&( - mempool[4 * n + total_block2_size + index_map_persist_size + n / 4 + n / 16]); - #else +#ifdef SE_IFFT_OTF se_ptrs->values = - (flpt *)&(mempool[4 * n + total_block2_size + index_map_persist_size]); - #endif + (flpt *)&(mempool[4 * n + total_block2_size + index_map_persist_size + n / 4 + n / 16]); +#else + se_ptrs->values = (flpt *)&(mempool[4 * n + total_block2_size + index_map_persist_size]); +#endif #endif size_t address_size = 4; @@ -158,9 +156,8 @@ void ckks_set_ptrs_asym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) #endif } -void gen_pk(const Parms *parms, ZZ *s_small, ZZ *ntt_roots, uint8_t *seed, - SE_PRNG *shareable_prng, ZZ *s_save, int8_t *ep_small, ZZ *ntt_ep, ZZ *pk_c0, - ZZ *pk_c1) +void gen_pk(const Parms *parms, ZZ *s_small, ZZ *ntt_roots, uint8_t *seed, SE_PRNG *shareable_prng, + ZZ *s_save, int8_t *ep_small, ZZ *ntt_ep, ZZ *pk_c0, ZZ *pk_c1) { se_assert(parms && shareable_prng); prng_randomize_reset(shareable_prng, seed); // Safe to share this prng @@ -169,12 +166,12 @@ void gen_pk(const Parms *parms, ZZ *s_small, ZZ *ntt_roots, uint8_t *seed, // pk_c1 := a // -- If pk_c0 and pk_c1 point to the same location, pk0 will overwrite pk1. // However, we need to return pk1. Therefore, must save it in extra buffer. - ckks_encode_encrypt_sym(parms, 0, ep_small, shareable_prng, s_small, ntt_ep, - ntt_roots, pk_c0, pk_c1, s_save, 0); + ckks_encode_encrypt_sym(parms, 0, ep_small, shareable_prng, s_small, ntt_ep, ntt_roots, pk_c0, + pk_c1, s_save, 0); } -void ckks_asym_init(const Parms *parms, uint8_t *seed, SE_PRNG *prng, - int64_t *conj_vals_int, ZZ *u, int8_t *e1) +void ckks_asym_init(const Parms *parms, uint8_t *seed, SE_PRNG *prng, int64_t *conj_vals_int, ZZ *u, + int8_t *e1) { se_assert(parms && prng); size_t n = parms->coeff_count; @@ -200,16 +197,14 @@ void ckks_asym_init(const Parms *parms, uint8_t *seed, SE_PRNG *prng, #ifdef SE_DEBUG_NO_ERRORS memset(e1, 0, n * sizeof(int8_t)); #else - sample_add_poly_cbd_generic_inpl_prng_16(conj_vals_int, n, - prng); // now stores [pt+e0] - sample_poly_cbd_generic_prng_16(n, prng, e1); // now stores [e1] + sample_add_poly_cbd_generic_inpl_prng_16(conj_vals_int, n, prng); // now stores [pt+e0] + sample_poly_cbd_generic_prng_16(n, prng, e1); // now stores [e1] #endif } -void ckks_encode_encrypt_asym(const Parms *parms, const int64_t *conj_vals_int, - const ZZ *u, const int8_t *e1, ZZ *ntt_roots, - ZZ *ntt_u_e1_pte, ZZ *ntt_u_save, ZZ *ntt_e1_save, - ZZ *pk_c0, ZZ *pk_c1) +void ckks_encode_encrypt_asym(const Parms *parms, const int64_t *conj_vals_int, const ZZ *u, + const int8_t *e1, ZZ *ntt_roots, ZZ *ntt_u_e1_pte, ZZ *ntt_u_save, + ZZ *ntt_e1_save, ZZ *pk_c0, ZZ *pk_c1) { se_assert(parms); #ifdef SE_DISABLE_TESTING_CAPABILITY @@ -249,7 +244,7 @@ void ckks_encode_encrypt_asym(const Parms *parms, const int64_t *conj_vals_int, #ifndef SE_DISABLE_TESTING_CAPABILITY // if (ntt_u_save) printf("ntt u save is not null\n"); - // else printf("ntt u save is null!!!!\n"); + // else printf("ntt u save is null\n"); if (ntt_u_save) memcpy(ntt_u_save, ntt_u_e1_pte, n * sizeof(ZZ)); // if (ntt_u_save) print_poly("ntt(u) (inside, ntt_u_save)", ntt_u_save, n); #endif diff --git a/device/lib/ckks_asym.h b/device/lib/ckks_asym.h index 9079ac6..aff7582 100644 --- a/device/lib/ckks_asym.h +++ b/device/lib/ckks_asym.h @@ -49,51 +49,47 @@ void ckks_set_ptrs_asym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs); /** Generates a CKKS public key from a CKKS secret key. Mainly useful for testing. -Size req: If SE_NTT_OTF is not defined, 'ntt_roots' must contain space for roots according -to NTT type chosen. If seed != NULL, seed must be SE_PRNG_SEED_BYTE_COUNT long. +Size req: If SE_NTT_OTF is not defined, 'ntt_roots' must contain space for roots according to NTT +type chosen. If seed != NULL, seed must be SE_PRNG_SEED_BYTE_COUNT long. @param[in] parms Parameters set by ckks_setup @param[in] s_small Secret key in small form -@param ntt_roots [Optional]. Scratch space to load ntt roots. Ignored if -SE_NTT_OTF is chosen. -@param[in] seed [Optional.] Seed to seed 'prng' with. +@param ntt_roots [Optional]. Scratch for ntt roots. Ignored if SE_NTT_OTF is chosen. +@param[in] seed [Optional]. Seed to seed 'prng' with. @param[in,out] shareable_prng A shareable prng instance. Will reset and update counter. -@param[out] s_save Secret key modulo the current modulus (in ntt form). Useful -for testing. +@param[out] s_save Secret key mod current modulus (in ntt form) (for testing) @param[out] ep_small Secret key error polynomial ep in small form @param[out] ntt_ep Expanded ep modulus the current modulus (in ntt form) @param[out] pk_c0 First component of public key for current modulus @param[out] pk_c1 Second component of public key for current modulus */ -void gen_pk(const Parms *parms, ZZ *s_small, ZZ *ntt_roots, uint8_t *seed, - SE_PRNG *shareable_prng, ZZ *s_save, int8_t *ep_small, ZZ *ntt_ep, ZZ *pk_c0, - ZZ *pk_c1); +void gen_pk(const Parms *parms, ZZ *s_small, ZZ *ntt_roots, uint8_t *seed, SE_PRNG *shareable_prng, + ZZ *s_save, int8_t *ep_small, ZZ *ntt_ep, ZZ *pk_c0, ZZ *pk_c1); /** -Initializes values for a single full asymmetric CKKS encryption. Samples the errors (w/o -respect to any prime), as well as the ternary polynomial 'u'. Should be called once per -encode-encrypt sequence (just after ckks_encode_base). +Initializes values for a single full asymmetric CKKS encryption. Samples the errors (w/o respect to +any prime), as well as the ternary polynomial 'u'. Should be called once per encode-encrypt sequence +(just after ckks_encode_base). Note: This function modifies (i.e. resets and re-randomizes) the prng instance. Size req: If seed != NULL, seed must be SE_PRNG_SEED_BYTE_COUNT long. @param[in] parms Parameters set by ckks_setup -@param[in] seed [Optional.] Seed to seed 'prng' with. -@param[in,out] prng PRNG instance needed to generate error and ternary -polynomials. +@param[in] seed [Optional]. Seed to seed 'prng' with. +@param[in,out] prng PRNG instance needed to generate error and ternary polynomials. @param[in,out] conj_vals_int As pointed to by conj_vals_int_ptr (n int64 values). In: ckks plaintext; Out: plaintext + e0 (in non-reduced form) @param[out] u Ternary polynomial @param[out] e1 Error e1 (in non-reduced form) */ -void ckks_asym_init(const Parms *parms, uint8_t *seed, SE_PRNG *prng, - int64_t *conj_vals_int, ZZ *u, int8_t *e1); +void ckks_asym_init(const Parms *parms, uint8_t *seed, SE_PRNG *prng, int64_t *conj_vals_int, ZZ *u, + int8_t *e1); /** -Encodes and asymmetrically encrypts a vector of values using CKKS, for the current modulus -prime. Optionally returns some additional values useful for testing, if -SE_DISABLE_TESTING_CAPABILITY is not defined. +Encodes and asymmetrically encrypts a vector of values using CKKS, for the current modulus prime. +Optionally returns some additional values useful for testing, if SE_DISABLE_TESTING_CAPABILITY is +not defined. Size req: 'ntt_roots' should have space for NTT roots according to NTT option chosen. 'ntt_u_e1_pte', 'pk_c0', and 'pk_c1' should have space for n ZZ elements. If testing, @@ -103,30 +99,25 @@ Size req: 'ntt_roots' should have space for NTT roots according to NTT option ch @param[in] conj_vals_int As set by ckks_asym_init @param[in] u As set by ckks_asym_init @param[in] e1 As set by ckks_asym_init -@param ntt_roots [Optional]. Scratch space to load ntt roots. Ignored if -SE_NTT_OTF is chosen. -@param[out] ntt_u_e1_pte Scratch space. Out: m + e0 in reduced and ntt form (useful for -testing). -@param[out] ntt_u_save [Optional, ignored if NULL]. Expanded and ntt form of u (useful -for testing) -@param[out] ntt_e1_save [Optional, ignored if NULL]. Reduced and ntt form of e1 (useful -for testing) +@param ntt_roots [Optional]. Scratch space for ntt roots. Ignored if SE_NTT_OTF is chosen. +@param[out] ntt_u_e1_pte Scratch space. Out: m + e0 in reduced and ntt form (useful for testing). +@param[out] ntt_u_save [Optional, ignored if NULL]. Expanded and ntt form of u (for testing) +@param[out] ntt_e1_save [Optional, ignored if NULL]. Reduced and ntt form of e1 (for testing) @param[out] pk_c0 First component of ciphertext @param[out] pk_c1 Second component of ciphertext */ -void ckks_encode_encrypt_asym(const Parms *parms, const int64_t *conj_vals_int, - const ZZ *u, const int8_t *e1, ZZ *ntt_roots, - ZZ *ntt_u_e1_pte, ZZ *ntt_u_save, ZZ *ntt_e1_save, - ZZ *pk_c0, ZZ *pk_c1); +void ckks_encode_encrypt_asym(const Parms *parms, const int64_t *conj_vals_int, const ZZ *u, + const int8_t *e1, ZZ *ntt_roots, ZZ *ntt_u_e1_pte, ZZ *ntt_u_save, + ZZ *ntt_e1_save, ZZ *pk_c0, ZZ *pk_c1); /** -Updates parameters to next prime in modulus switching chain for asymmetric CKKS -encryption. Also reduces ternary polynomial to next prime modulus if used in expanded form -(compressed form u will be reduced later). +Updates parameters to next prime in modulus switching chain for asymmetric CKKS encryption. Also +reduces ternary polynomial to next prime modulus if used in expanded form (compressed form u will be +reduced later). @param[in,out] parms Parameters set by ckks_setup. curr_modulus_idx will be advanced by 1 -@param[in,out] u [Optional]. Ternary polynomial for asymmetric encryption. Can be -null if 'u' is stored in compressed form. +@param[in,out] u [Optional]. Ternary polynomial for asymmetric encryption. + Can be null if 'u' is stored in compressed form. @returns 1 on success, 0 on failure (reached end of modulus chain) */ bool ckks_next_prime_asym(Parms *parms, ZZ *u); diff --git a/device/lib/ckks_common.c b/device/lib/ckks_common.c index 704f3e3..c3f2f09 100644 --- a/device/lib/ckks_common.c +++ b/device/lib/ckks_common.c @@ -51,8 +51,7 @@ void ckks_calc_index_map(const Parms *parms, uint16_t *index_map) size_t index1 = ((size_t)pos - 1) / 2; size_t index2 = n - index1 - 1; - // -- Merge index mapping step w/ bitrev step req. for later application of - // ifft/fft + // -- Merge index mapping step w/ bitrev step req. for later application of ifft/fft index_map[i] = (uint16_t)bitrev(index1, logn); index_map[i + slot_count] = (uint16_t)bitrev(index2, logn); @@ -75,6 +74,8 @@ void ckks_setup(size_t degree, size_t nprimes, uint16_t *index_map, Parms *parms ckks_calc_index_map(parms, index_map); #elif defined(SE_INDEX_MAP_LOAD_PERSIST) load_index_map(parms, index_map); +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) + if (!parms->is_asymmetric) load_index_map(parms, index_map); #endif } @@ -91,6 +92,8 @@ void ckks_setup_custom(size_t degree, size_t nprimes, const ZZ *modulus_vals, co ckks_calc_index_map(parms, index_map); #elif defined(SE_INDEX_MAP_LOAD_PERSIST) load_index_map(parms, index_map); +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) + if (!parms->is_asymmetric) load_index_map(parms, index_map); #endif } @@ -110,6 +113,12 @@ bool ckks_encode_base(const Parms *parms, const flpt *values, size_t values_len, #ifdef SE_INDEX_MAP_LOAD se_assert(index_map); load_index_map(parms, index_map); +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) + if (parms->is_asymmetric) + { + se_assert(index_map); + load_index_map(parms, index_map); + } #endif // if (index_map) print_poly_uint16("index map", index_map, n); @@ -136,7 +145,7 @@ bool ckks_encode_base(const Parms *parms, const flpt *values, size_t values_len, #endif se_assert(index1_rev < n); se_assert(index2_rev < n); - double complex val = (double complex)_complex(values[i], 0); + double complex val = (double complex)_complex((double)values[i], (double)0); conj_vals[index1_rev] = val; conj_vals[index2_rev] = val; // -- Note: conj_vals[index2_rev] should be set to conj(val), but since we @@ -261,9 +270,7 @@ void reduce_add_e_small(const Parms *parms, const int8_t *e, ZZ *out) Modulus *mod = parms->curr_modulus; for (size_t i = 0; i < n; i++) - { - add_mod_inpl(&(out[i]), ((-(ZZ)(e[i] < 0)) & mod->value) + (ZZ)e[i], mod); - } + { add_mod_inpl(&(out[i]), ((-(ZZ)(e[i] < 0)) & mod->value) + (ZZ)e[i], mod); } } #ifdef SE_USE_MALLOC diff --git a/device/lib/ckks_common.h b/device/lib/ckks_common.h index 864bd63..4dbdc76 100644 --- a/device/lib/ckks_common.h +++ b/device/lib/ckks_common.h @@ -29,8 +29,8 @@ For the following, n is the polynomial ring degree. @param c1_ptr 2nd component of a ciphertext for a particular prime @param index_map_ptr Index map values @param ntt_roots_ptr Storage for NTT roots -@param ntt_pte_ptr Used for adding the plaintext to the error. If asymmetric, this -is also used for ntt(u) and ntt(e1). +@param ntt_pte_ptr Used for adding the plaintext to the error. + If asymmetric, this is also used for ntt(u) and ntt(e1). @param e1_ptr Second error polynomial (unused in symmetric case) */ typedef struct SE_PTRS @@ -52,8 +52,8 @@ typedef struct SE_PTRS } SE_PTRS; /** -Computes the values for the index map. This corresponds to the "pi" inverse projection -symbol in the CKKS paper, merged with the bit-reversal required for the ifft/fft. +Computes the values for the index map. This corresponds to the "pi" inverse projection symbol in the +CKKS paper, merged with the bit-reversal required for the ifft/fft. Size req: 'index_map' must constain space for n uint16_t elements. @@ -63,14 +63,15 @@ Size req: 'index_map' must constain space for n uint16_t elements. void ckks_calc_index_map(const Parms *parms, uint16_t *index_map); /** -Sets the parameters according to the request polynomial ring degree. Also sets the index -map if required (based on index map option defined). This should be called once during -memory allocation to setup the parameters. +Sets the parameters according to the request polynomial ring degree. Also sets the index map if +required (based on index map option defined). This should be called once during memory allocation to +setup the parameters. -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). -Size req: If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t -elements. +Size req: If index map needs to be loaded (see 'Note' above), index_map must constain +space for n uint16_t elements. @param[in] degree Desired polynomial ring degree. @param[in] nprimes Desired number of primes @@ -80,46 +81,47 @@ elements. void ckks_setup(size_t degree, size_t nprimes, uint16_t *index_map, Parms *parms); /** -Sets the parameters according to request custom parameters. Also sets the index map if -required (based on index map option defined). This should be called once during memory -allocation to setup the parameters. If either 'modulus_vals' or 'ratios' is NULL, uses -regular (non-custom) ckks_setup instead. +Sets the parameters according to request custom parameters. Also sets the index map if required +(based on index map option defined). This should be called once during memory allocation to setup +the parameters. If either 'modulus_vals' or 'ratios' is NULL, uses regular (non-custom) ckks_setup +instead. -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). -Size req: If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t -elements. +Size req: If index map needs to be loaded (see 'Note' above), index_map must constain space for n +uint16_t elements. @param[in] degree Desired polynomial ring degree. @param[in] nprimes Desired number of primes @param[in] modulus_vals An array of nprimes type-ZZ modulus values. @param[in] ratios An array of const_ratio values for each custom modulus value -(high word, followed by low word). + (high word, followed by low word). @param[out] index_map [Optional]. Pointer to index map values buffer @param[out] parms Parameters instance */ -void ckks_setup_custom(size_t degree, size_t nprimes, const ZZ *modulus_vals, - const ZZ *ratios, uint16_t *index_map, Parms *parms); +void ckks_setup_custom(size_t degree, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, + uint16_t *index_map, Parms *parms); /** Resets the encryption parameters. Should be called once per encode-encrypt sequence to set -curr_modulus_idx back to the start of the modulus chain. Does not need to be called the -very first time after ckks_setup_parms is called, however. +curr_modulus_idx back to the start of the modulus chain. Does not need to be called the very first +time after ckks_setup_parms is called, however. @param[in,out] parms Parameters set by ckks_setup */ void ckks_reset_primes(Parms *parms); /** -CKKS encoding base (w/o respect to a particular modulus). Should be called once per -encode-encrypt sequence. Encoding can fail for certain inputs, so returns a value -indicating success or failure. +CKKS encoding base (w/o respect to a particular modulus). Should be called once per encode-encrypt +sequence. Encoding can fail for certain inputs, so returns a value indicating success or failure. -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). -Size req: 'values' can contain at most n/2 slots (i.e. n/2 ZZ values), where n is the -polynomial ring degree. 'conj_vals' must contain space for n double complex values. If -'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t elements. +Size req: 'values' can contain at most n/2 slots (i.e. n/2 ZZ values), where n is the polynomial +ring degree. 'conj_vals' must contain space for n double complex values. If index map needs to be +loaded (see 'Note' above), index_map must constain space for n uint16_t elements. @param[in] parms Parameters set by ckks_setup @param[in] values Initial message array with (up to) n/2 slots @@ -130,8 +132,7 @@ polynomial ring degree. 'conj_vals' must contain space for n double complex valu @returns True on success, False on failure */ bool ckks_encode_base(const Parms *parms, const flpt *values, size_t values_len, - uint16_t *index_map, double complex *ifft_roots, - double complex *conj_vals); + uint16_t *index_map, double complex *ifft_roots, double complex *conj_vals); /** Reduces all values in conj_vals_int modulo the current modulus and stores result in out. @@ -188,8 +189,7 @@ Prints the relative positions of various objects. @param[in] n Polynomial ring degree @param[in] sym Set to 1 if in symmetric mode */ -void se_print_relative_positions(const ZZ *st, const SE_PTRS *se_ptrs, size_t n, - bool sym); +void se_print_relative_positions(const ZZ *st, const SE_PTRS *se_ptrs, size_t n, bool sym); /** Prints the addresses of various objects. @@ -233,50 +233,57 @@ void print_ckks_mempool_size(void); // -- Calculate mempool size for no-malloc case #ifdef SE_IFFT_OTF - #if defined(SE_NTT_ONE_SHOT) || defined(SE_NTT_REG) - #define MEMPOOL_SIZE_BASE 5 * SE_DEGREE_N - #elif defined(SE_NTT_FAST) - #define MEMPOOL_SIZE_BASE 7 * SE_DEGREE_N - #else - #define MEMPOOL_SIZE_BASE 4 * SE_DEGREE_N - #endif +#if defined(SE_NTT_ONE_SHOT) || defined(SE_NTT_REG) +#define MEMPOOL_SIZE_BASE 5 * SE_DEGREE_N +#elif defined(SE_NTT_FAST) +#define MEMPOOL_SIZE_BASE 7 * SE_DEGREE_N #else - #define MEMPOOL_SIZE_BASE 8 * SE_DEGREE_N +#define MEMPOOL_SIZE_BASE 4 * SE_DEGREE_N +#endif +#else +#define MEMPOOL_SIZE_BASE 8 * SE_DEGREE_N +#endif + +#if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) || defined(SE_SK_INDEX_MAP_SHARED) +#define SE_INDEX_MAP_PERSIST_SIZE_sym SE_DEGREE_N / 2 +#else +#define SE_INDEX_MAP_PERSIST_SIZE_sym 0 #endif -#if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) - #define SE_INDEX_MAP_PERSIST_SIZE SE_DEGREE_N / 2 +#ifdef SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM +#define SE_INDEX_MAP_PERSIST_SIZE_asym 0 #else - #define SE_INDEX_MAP_PERSIST_SIZE 0 +#define SE_INDEX_MAP_PERSIST_SIZE_asym SE_INDEX_MAP_PERSIST_SIZE_sym #endif #ifdef SE_SK_PERSISTENT - #define SK_PERSIST_SIZE SE_DEGREE_N / 16 +#define SK_PERSIST_SIZE SE_DEGREE_N / 16 #else - #define SK_PERSIST_SIZE 0 +#define SK_PERSIST_SIZE 0 #endif #ifdef SE_MEMPOOL_ALLOC_VALUES - #define VALUES_ALLOC_SIZE SE_DEGREE_N / 2 +#define VALUES_ALLOC_SIZE SE_DEGREE_N / 2 #else - #define VALUES_ALLOC_SIZE 0 +#define VALUES_ALLOC_SIZE 0 #endif #define MEMPOOL_SIZE_sym \ - MEMPOOL_SIZE_BASE + SE_INDEX_MAP_PERSIST_SIZE + SK_PERSIST_SIZE + VALUES_ALLOC_SIZE + MEMPOOL_SIZE_BASE + SE_INDEX_MAP_PERSIST_SIZE_sym + SK_PERSIST_SIZE + VALUES_ALLOC_SIZE #ifdef SE_IFFT_OTF - #define MEMPOOL_SIZE_BASE_Asym \ - MEMPOOL_SIZE_BASE + SE_DEGREE_N + SE_DEGREE_N / 4 + SE_DEGREE_N / 16 +#define MEMPOOL_SIZE_BASE_Asym MEMPOOL_SIZE_BASE + SE_DEGREE_N + SE_DEGREE_N / 4 + SE_DEGREE_N / 16 +// 4n + n + n + n/4 + n/16 #else - #define MEMPOOL_SIZE_BASE_Asym MEMPOOL_SIZE_BASE +#define MEMPOOL_SIZE_BASE_Asym MEMPOOL_SIZE_BASE #endif #define MEMPOOL_SIZE_Asym \ - MEMPOOL_SIZE_BASE_Asym + SE_INDEX_MAP_PERSIST_SIZE + VALUES_ALLOC_SIZE + MEMPOOL_SIZE_BASE_Asym + SE_INDEX_MAP_PERSIST_SIZE_asym + VALUES_ALLOC_SIZE #ifdef SE_ENCRYPT_TYPE_SYMMETRIC - #define MEMPOOL_SIZE MEMPOOL_SIZE_sym +#define MEMPOOL_SIZE MEMPOOL_SIZE_sym #else - #define MEMPOOL_SIZE MEMPOOL_SIZE_Asym +#define MEMPOOL_SIZE MEMPOOL_SIZE_Asym #endif diff --git a/device/lib/ckks_sym.c b/device/lib/ckks_sym.c index c3c1267..768e657 100644 --- a/device/lib/ckks_sym.c +++ b/device/lib/ckks_sym.c @@ -43,7 +43,8 @@ size_t ckks_get_mempool_size_sym(size_t degree) mempool_size += 4 * n; #endif -#if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) +#if defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) || defined(SE_SK_INDEX_MAP_SHARED) mempool_size += n / 2; #endif @@ -63,6 +64,7 @@ ZZ *ckks_mempool_setup_sym(size_t degree) { size_t mempool_size = ckks_get_mempool_size_sym(degree); ZZ *mempool = calloc(mempool_size, sizeof(ZZ)); + // printf("mempool_size: %zu\n", mempool_size); if (!mempool) { printf("Error! Allocation failed. Exiting...\n"); @@ -73,15 +75,10 @@ ZZ *ckks_mempool_setup_sym(size_t degree) } #endif -// Note: first elements of ntt_roots will also contain partial pt -// This is a convenience function to return the correct addresses -// This can be called once during initial memory allocation and never needs to -// be called again And also load in secret key if necessary - void ckks_set_ptrs_sym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) { se_assert(mempool && se_ptrs); - size_t n = degree; + const size_t n = degree; // -- First, set everything to set size or 0 se_ptrs->conj_vals = (double complex *)mempool; @@ -122,11 +119,12 @@ void ckks_set_ptrs_sym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) // -- Set pi inverse based on index map type #if defined(SE_INDEX_MAP_LOAD) - se_ptrs->index_map_ptr = (uint16_t *)&(mempool[4 * n]); -#elif defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) + se_ptrs->index_map_ptr = (uint16_t *)(&(mempool[4 * n])); // 16n / sizeof(ZZ) = 4n +#elif defined(SE_INDEX_MAP_PERSIST) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) // -- If ifft, this will be + the ifft_roots size // else, this will be + the ntt size - se_ptrs->index_map_ptr = (uint16_t *)&(mempool[4 * n + total_block2_size]); + se_ptrs->index_map_ptr = (uint16_t *)(&(mempool[4 * n + total_block2_size])); index_map_persist_size = n / 2; #endif @@ -135,6 +133,9 @@ void ckks_set_ptrs_sym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs) se_ptrs->ternary = &(mempool[4 * n + total_block2_size + index_map_persist_size]); #elif !defined(SE_IFFT_OTF) && defined(SE_SK_PERSISTENT_ACROSS_PRIMES) se_ptrs->ternary = &(mempool[7 * n]); +#elif defined(SE_SK_INDEX_MAP_SHARED) + se_ptrs->ternary = &(mempool[4 * n]); + index_map_persist_size = n / 2; #endif #ifdef SE_MEMPOOL_ALLOC_VALUES @@ -167,6 +168,8 @@ void ckks_setup_s(const Parms *parms, uint8_t *seed, SE_PRNG *prng, ZZ *s) se_assert(prng); prng_randomize_reset(prng, seed); sample_small_poly_ternary_prng_96(parms->coeff_count, prng, s); + // -- TODO: Does not work to sample s for multi prime for now + // if s and index map share mem } else { @@ -179,12 +182,10 @@ void ckks_sym_init(const Parms *parms, uint8_t *share_seed, uint8_t *seed, SE_PR SE_PRNG *prng, int64_t *conj_vals_int) { // -- Each prng must be reset & re-randomized once per encode-encrypt sequence. - // -- 'prng_randomize_reset' will set the prng seed to a random value and the prng - // counter to 0 + // -- 'prng_randomize_reset' will set the prng seed to a random value and the prng counter to 0 // -- (If seeds are !NULL, seeds will be used to seed prng instead of a random value.) // -- The seed associated with the prng used to sample 'a' can be shared - // -- NOTE: The re-randomization is not strictly necessary if counter has not wrapped - // around + // -- NOTE: The re-randomization is not strictly necessary if counter has not wrapped around // and we share both the seed and starting counter value with the server // for the shareable part. prng_randomize_reset(shareable_prng, share_seed); // Used for 'a' @@ -209,8 +210,8 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, // ============================================================== // Generate ciphertext: (c[1], c[0]) = (a, [-a*s + m + e]_Rq) // ============================================================== - PolySizeType n = parms->coeff_count; - const Modulus *mod = parms->curr_modulus; + const PolySizeType n = parms->coeff_count; + const Modulus *mod = parms->curr_modulus; // ---------------------- // c1 = a <--- U @@ -220,12 +221,9 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, #ifndef SE_DISABLE_TESTING_CAPABILITY se_assert(conj_vals_int || ep_small); - // -- At this point, it is safe to send c1 away. This will allow us to re-use c1's - // memory. - // -- However, we may be debugging, in which case we need to store c1 somewhere for - // debugging later. - // -- Note: This provides very little memory savings overall, so isn't necessary to - // use. + // -- At this point, it is safe to send c1 away. This will allow us to re-use c1's memory. + // However, we may be debugging and need to store c1 somewhere for debugging later. + // -- Note: This method provides very little memory savings overall, so isn't necessary to use. if (c1_save) memcpy(c1_save, c1, n * sizeof(ZZ)); #endif @@ -239,8 +237,7 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, se_assert(!parms->sample_s); load_sk(parms, s_small); #elif defined(SE_SK_PERSISTENT_ACROSS_PRIMES) - // -- Note that if we are here, ifft type is not otf, which means that - // SE_REVERSE_CT_GEN_ENABLED + // -- Note that if we are here, ifft type is not otf, which means that SE_REVERSE_CT_GEN_ENABLED // cannot be defined. Therefore, we only have to check that the current // modulus is 0 to know that we are in the first prime of the modulus chain if (parms->curr_modulus_idx == 0) @@ -252,7 +249,8 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, // print_poly_small("s (small)", s_small, parms->coeff_count); // -- Expand and store s in c0 - // print_poly_small("s (small)", s_small, parms->coeff_count); + // print_poly_uint8_full("s (small)", (uint8_t*)s_small, parms->coeff_count/4); + // print_poly_small_full("s (small)", s_small, parms->coeff_count); expand_poly_ternary(s_small, parms, c0_s); // print_poly_full("s", c0_s, parms->coeff_count); // print_poly_ternary("s", c0_s, parms->coeff_count, false); @@ -289,7 +287,7 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, // -- Calculate ntt(m + e) = ntt(reduce(conj_vals_int)) = ntt(ntt_pte) // and store result in ntt_pte. Note: ntt roots (if required) should already be - // loaded from step 3. + // loaded from above ntt_inpl(parms, ntt_roots, ntt_pte); // print_poly("ntt(m + e)", ntt_pte, n); diff --git a/device/lib/ckks_sym.h b/device/lib/ckks_sym.h index ffce561..c5e63e4 100644 --- a/device/lib/ckks_sym.h +++ b/device/lib/ckks_sym.h @@ -47,43 +47,41 @@ Should only need to be called once during initial memory allocation. void ckks_set_ptrs_sym(size_t degree, ZZ *mempool, SE_PTRS *se_ptrs); /** -Sets up a CKKS secret key. For symmetric encryption, this should either be -called once at the start during memory allocation (if storing s persistantly in -working memory) or once per encode-encrypt sequence (between base and remaining -steps, if not storing s persistantly in working memory). +Sets up a CKKS secret key. For symmetric encryption, this should either be called once at the start +during memory allocation (if storing s persistently in working memory) or once per encode-encrypt +sequence (between base and remaining steps, if not storing s persistently in working memory). If parms.sample_s == 1, will internally generate the secret key (from the uniform ternary -distribution). This is mainly useful for testing. If parms.sample_s == 0, will read in -secret key from file. +distribution). This is mainly useful for testing. If parms.sample_s == 0, will read in secret key +from file. Size req: If seed is !NULL, seed must be SE_PRNG_SEED_BYTE_COUNT long. @param[in] parms Parameters set by ckks_setup -@param[in] seed [Optional.] Seed to seed 'prng' with, if prng is used. -@param[in,out] prng [Optional] PRNG instance needed to generate randomness for secret -key polynomial. Should not be shared. -@param[out] s Secret key polynomial. Must have space for n coefficients. If -'small' s is used, this must be 2 bits per coefficient. Otherwise, this must be sizeof(ZZ) -per coefficient. +@param[in] seed [Optional]. Seed to seed 'prng' with, if prng is used. +@param[in,out] prng [Optional]. PRNG instance needed to generate randomness for secret key + polynomial. Should not be shared. +@param[out] s Secret key polynomial. Must have space for n coefficients. If 'small' s is + used, this must be 2 bits per coefficient. Otherwise, this must be sizeof(ZZ) + per coefficient. */ void ckks_setup_s(const Parms *parms, uint8_t *seed, SE_PRNG *prng, ZZ *s); /** -Initializes values for a single full symmetric CKKS encryption. Samples the error (w/o -respect to any prime). Should be called once per encode-encrypt sequence (just after -ckks_encode_base). +Initializes values for a single full symmetric CKKS encryption. Samples the error (w/o respect to +any prime). Should be called once per encode-encrypt sequence (just after ckks_encode_base). Note: This function modifies (i.e. resets and re-randomizes) the prng instance. Size req: If seeds are !NULL, seeds must be SE_PRNG_SEED_BYTE_COUNT long. @param[in] parms Parameters set by ckks_setup -@param[in] share_seed_in [Optional.] Seed to seed 'shareable_prng' with. -@param[in] seed_in [Optional.] Seed to seed 'prng' with. -@param[in,out] shareable_prng PRNG instance needed to generate first component of -ciphertexts. Is safe to share. -@param[in,out] prng PRNG instance needed to generate error polynomial. Should -not be shared +@param[in] share_seed_in [Optional]. Seed to seed 'shareable_prng' with. +@param[in] seed_in [Optional]. Seed to seed 'prng' with. +@param[in,out] shareable_prng PRNG instance needed to generate first component of ciphertexts. Is + safe to share. +@param[in,out] prng PRNG instance needed to generate error polynomial. Should not be + shared. @param[in,out] conj_vals_int As pointed to by conj_vals_int_ptr (n int64 values). In: ckks pt; Out: pt + error (non-reduced) */ @@ -91,25 +89,21 @@ void ckks_sym_init(const Parms *parms, uint8_t *share_seed_in, uint8_t *seed_in, SE_PRNG *shareable_prng, SE_PRNG *prng, int64_t *conj_vals_int); /** -Encodes and symmetrically encrypts a vector of values using CKKS for the current modulus -prime. +Encodes and symmetrically encrypts a vector of values using CKKS for the current modulus prime. -Internally converts various objects to NTT form. If debugging encryption-only (assuming a -message of 0), or if calling to generate a public key, can set conj_vals_int to zero. In -this case, must set ep_small to the compessed form of the error. +Internally converts various objects to NTT form. If debugging encryption-only (assuming a message of +0), or if calling to generate a public key, can set conj_vals_int to zero. In this case, must set +ep_small to the compessed form of the error. @param[in] parms Parameters set by ckks_setup @param[in] conj_vals_int [Optional]. See description. @param[in] ep_small [Optional]. See description. For debugging only. -@param[in,out] shareable_prng PRNG instance needed to generate first component of -ciphertexts. Is safe to share. -@param ntt_pte Scratch space. Will be used to store plaintext plus error -in NTT form. +@param[in,out] shareable_prng PRNG instance needed to generate first component of ciphertexts. Is + safe to share. +@param ntt_pte Scratch space. Will be used to store pt + e (in NTT form) @param ntt_roots Scratch space. May be used to load NTT roots. -@param[out] c0_s 1st component of the ciphertext. Stores n coefficients of -size ZZ. -@param[out] c1 2nd component of the ciphertext. Stores n coefficients of -size ZZ. +@param[out] c0_s 1st component of the ciphertext. Stores n coeffs of size ZZ. +@param[out] c1 2nd component of the ciphertext. Stores n coeffs of size ZZ. @param[out] s_save [Optional]. Useful for testing. @param[out] c1_save [Optional]. Useful for testing. */ @@ -118,12 +112,12 @@ void ckks_encode_encrypt_sym(const Parms *parms, const int64_t *conj_vals_int, ZZ *ntt_pte, ZZ *ntt_roots, ZZ *c0_s, ZZ *c1, ZZ *s_save, ZZ *c1_save); /** -Updates parameters to next prime in modulus switching chain for symmetric CKKS encryption. -Also converts secret key polynomial to next prime modulus if used in expanded form -(compressed form s will be reduced later). +Updates parameters to next prime in modulus switching chain for symmetric CKKS encryption. Also +converts secret key polynomial to next prime modulus if used in expanded form (compressed form s +will be reduced later). @param[in,out] parms Parameters set by ckks_setup. curr_modulus_idx will be advanced by 1 -@param[in,out] s [Optional.] Secret key to convert to next modulus prime +@param[in,out] s [Optional]. Secret key to convert to next modulus prime @returns 1 on success, 0 on failure (reached end of modulus chain) */ bool ckks_next_prime_sym(Parms *parms, ZZ *s); diff --git a/device/lib/defines.h b/device/lib/defines.h index 4f55694..d8beb17 100644 --- a/device/lib/defines.h +++ b/device/lib/defines.h @@ -25,29 +25,29 @@ Inverse NTT type. Options 1 - 3 not guaranteed to always work. 0 = compute "on-the-fly" 1 = compute "one-shot" -2 = load +2 = load 3 = load fast */ #define SE_INTT_TYPE 0 // ============================================================== -// Configurations pertaining to benchmarking +// Configurations pertaining to benchmarking // ============================================================== /** Include timer code for benchmarking. Uncomment to use. */ -#define SE_ENABLE_TIMERS +// #define SE_ENABLE_TIMERS // ============================================================== -// Configurations pertaining to debugging +// Configurations pertaining to debugging // ============================================================== -// --- If defined, "print_poly" functions will only print PRINT_LEN_SMALL elements +// --- If defined, "print_poly" functions will only print PRINT_LEN_SMALL elements // of the requested polynomial, (unless a "full" version of "print_poly" is called) #define SE_PRINT_SMALL -#define PRINT_LEN_SMALL 8 +#define PRINT_LEN_SMALL 8 // #define SE_DEBUG_WITH_ZEROS // #define SE_DEBUG_NO_ERRORS @@ -59,9 +59,9 @@ Uncomment to use. // ============================================================== /** -Number of bytes to store the seed for the prng. +Number of bytes to store the seed for the prng. -For compressed public keys and/or compressed ciphertexts +For compressed public keys and/or compressed ciphertexts (in symmetric mode), must match prng byte count used in SEAL. */ #define SE_PRNG_SEED_BYTE_COUNT 64 @@ -75,7 +75,7 @@ Data path to use if SE_DATA_PATH is not set in CMAKE Data path length to use if SE_DATA_PATH is not set in CMAKE. Must be >= length of the SE_DATA_PATH_ define above (in bytes). -(Most compilers are ok with this calling strlen strlen, but +(Most compilers are ok with this calling strlen strlen, but some are not.) */ #define SE_DATA_PATH_LEN_ strlen(SE_DATA_PATH_) @@ -89,7 +89,7 @@ some are not.) // ============================================================== // -------------------------------------------------------------- -// Configurations dervied from user_defines.h and above. +// Configurations dervied from user_defines.h and above. // Do not modify. // -------------------------------------------------------------- @@ -104,7 +104,7 @@ some are not.) #if !defined(SE_DATA_PATH) || !defined(SE_DATA_PATH_LEN) #undef SE_DATA_PATH - #define SE_DATA_PATH SE_DATA_PATH_ + #define SE_DATA_PATH SE_DATA_PATH_ #define MAX_FPATH_SIZE SE_DATA_PATH_LEN_ + 1 + MAX_DATA_FILE_SIZE #else #define MAX_FPATH_SIZE SE_DATA_PATH_LEN + 1 + MAX_DATA_FILE_SIZE @@ -127,9 +127,9 @@ some are not.) # if (SE_RAND_TYPE == 0) // -- Do nothing #elif (SE_RAND_TYPE == 1) - #define SE_RAND_GETRANDOM + #define SE_RAND_GETRANDOM #elif (SE_RAND_TYPE == 2) - #define SE_RAND_NRF5 + #define SE_RAND_NRF5 #else #ifndef SE_CONFIG_ERROR #define SE_CONFIG_ERROR @@ -175,6 +175,8 @@ some are not.) #define SE_INDEX_MAP_LOAD #elif (SE_INDEX_MAP_TYPE == 3) #define SE_INDEX_MAP_LOAD_PERSIST +#elif (SE_INDEX_MAP_TYPE == 4) + #define SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM #else #ifndef SE_CONFIG_ERROR #define SE_CONFIG_ERROR @@ -183,7 +185,7 @@ some are not.) // ----- Secret key type # if (SE_SK_TYPE == 0) - #define SE_SK_NOT_PERSISTENT + #define SE_SK_NOT_PERSISTENT #elif (SE_SK_TYPE == 1) #define SE_SK_PERSISTENT_ACROSS_PRIMES #elif (SE_SK_TYPE == 2) @@ -198,7 +200,7 @@ some are not.) # if (SE_DATA_LOAD_TYPE == 0) // -- Do nothing #elif (SE_DATA_LOAD_TYPE == 1) - #define SE_DATA_FROM_CODE_COPY + #define SE_DATA_FROM_CODE_COPY #elif (SE_DATA_LOAD_TYPE == 2) #define SE_DATA_FROM_CODE_DIRECT #else @@ -330,8 +332,12 @@ FFT type. For now, we only support "on-the-fly" for the FFT type. #if __has_builtin(__builtin_complex) #define _complex(x, y) __builtin_complex(x, y) #else -// #define _complex(x, y) CMPLX(x, y) -#define _complex(x, y) x + y *I +#ifdef I +//#define _complex(x, y) CMPLX(x, y) +#define _complex(x, y) x + y*I +#else +#define _complex(x, y) x + y*(1.0fi) +#endif #endif #ifdef SE_USE_PREDEF_COMPLEX_FUNCS @@ -358,18 +364,18 @@ static inline double se_cimag(double complex val) typedef size_t PolySizeType; #ifdef SE_PRIMESIZE_64 -typedef uint64_t ZZ; -typedef int64_t ZZsign; // signed type -typedef double flpt; -#define PRIuZZ PRIu64 -#define PRIiZZ PRIi64 + typedef uint64_t ZZ; + typedef int64_t ZZsign; // signed type + typedef double flpt; + #define PRIuZZ PRIu64 + #define PRIiZZ PRIi64 #else -typedef uint32_t ZZ; -typedef int32_t ZZsign; // signed ZZ type -// typedef double flpt; -typedef float flpt; -#define PRIuZZ PRIu32 -#define PRIiZZ PRIi32 + typedef uint32_t ZZ; + typedef int32_t ZZsign; // signed ZZ type + // typedef double flpt; + typedef float flpt; + #define PRIuZZ PRIu32 + #define PRIiZZ PRIi32 #endif /** @@ -382,7 +388,10 @@ Size req: 'v' must contain at least n ZZ values */ static inline void clear(ZZ *v, PolySizeType n) { - memset(v, 0, n * sizeof(ZZ)); + if (v) + { + memset(v, 0, n * sizeof(ZZ)); + } } /** @@ -442,12 +451,17 @@ static inline void se_secure_zero_memset(void *v, size_t n) #undef SE_INDEX_MAP_LOAD #undef SE_INDEX_MAP_PERSIST #undef SE_INDEX_MAP_LOAD_PERSIST + #undef SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM #elif defined(SE_INDEX_MAP_LOAD) #undef SE_INDEX_MAP_PERSIST #undef SE_INDEX_MAP_LOAD_PERSIST + #undef SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM #elif defined(SE_INDEX_MAP_PERSIST) #undef SE_INDEX_MAP_LOAD_PERSIST + #undef SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM #elif defined(SE_INDEX_MAP_LOAD_PERSIST) + #undef SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) // -- Do nothing #else #define SE_INDEX_MAP_OTF @@ -523,19 +537,42 @@ static inline void se_secure_zero_memset(void *v, size_t n) #endif #endif -// -- This must be after IFFT sanity checks #ifdef SE_SK_PERSISTENT #undef SE_SK_PERSISTENT_ACROSS_PRIMES #undef SE_SK_NOT_PERSISTENT #elif defined(SE_SK_PERSISTENT_ACROSS_PRIMES) - #ifdef SE_IFFT_OTF - #undef SE_SK_PERSISTENT_ACROSS_PRIMES - #undef SE_SK_NOT_PERSISTENT - #define SE_SK_PERSISTENT - #endif + #undef SE_SK_NOT_PERSISTENT #elif defined(SE_SK_NOT_PERSISTENT) // -- Do nothing #else #define SE_SK_NOT_PERSISTENT #endif + + +// -- This must be afer sk and ntt and ifft and index map types are set +#ifdef SE_IFFT_OTF + // -- We don't have any ifft memory to work with, but we may have ntt memory + #ifdef SE_NTT_OTF + // -- We don't have any ntt memory to work with either + #if defined(SE_INDEX_MAP_LOAD) + // -- If sym, we have nowhere to load index_map, so can indicate + // memory as persistent + // -- Optimization: Index_map and SK can share memory if SK is not + // set as persistent + #if defined(SE_SK_PERSISTENT) + // -- Indicate index map load memory as persistent + #undef SE_INDEX_MAP_LOAD + #define SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM + #elif defined(SE_SK_NOT_PERSISTENT) + // -- SK can be at least persistent across primes + #undef SE_SK_NOT_PERSISTENT + #define SE_SK_PERSISTENT_ACROSS_PRIMES + #endif + #ifdef SE_SK_PERSISTENT_ACROSS_PRIMES + #define SE_SK_INDEX_MAP_SHARED + #endif + #endif + #endif +#endif + // clang-format on diff --git a/device/lib/fft.c b/device/lib/fft.c index d525473..ceb2e5e 100644 --- a/device/lib/fft.c +++ b/device/lib/fft.c @@ -4,8 +4,8 @@ /** @file fft.c -Note: All roots are mod 2n, where n is the number of elements (e.g. polynomial degree) in -the transformed vector. See the paper for more details. +Note: All roots are mod 2n, where n is the number of elements (e.g. polynomial degree) in the +transformed vector. See the paper for more details. */ #include "fft.h" @@ -14,7 +14,7 @@ the transformed vector. See the paper for more details. #include "util_print.h" #ifndef M_PI - #define M_PI 3.14159265358979323846 +#define M_PI 3.14159265358979323846 #endif /** @@ -145,6 +145,7 @@ void ifft_inpl(double complex *vec, size_t n, size_t logn, const double complex void fft_inpl(double complex *vec, size_t n, size_t logn, const double complex *roots) { + // print_poly_double_complex("vec[3603] ", &(vec[3603]), 1); size_t m = n << 1; // Degree of roots #ifdef SE_FFT_OTF SE_UNUSED(roots); @@ -184,7 +185,7 @@ void fft_inpl(double complex *vec, size_t n, size_t logn, const double complex * #if defined(SE_FFT_LOAD_FULL) || defined(SE_FFT_ONE_SHOT) size_t root_idx = 1; #endif - for (int i = 0; i < logn; i++, h *= 2, tt /= 2) // rounds + for (size_t i = 0; i < logn; i++, h *= 2, tt /= 2) // rounds { for (size_t j = 0, kstart = 0; j < h; j++, kstart += 2 * tt) // groups { diff --git a/device/lib/fft.h b/device/lib/fft.h index c296652..2118d82 100644 --- a/device/lib/fft.h +++ b/device/lib/fft.h @@ -6,8 +6,8 @@ Forward and Inverse Fast Fourier Transform. -Note: Currently, the fft is only useful for testing, since only the ifft is used in the -main algorithm. Leave both here for future use. +Note: Currently, the fft is only useful for testing, since only the ifft is used in the main +algorithm. Leave both here for future use. */ #pragma once @@ -74,8 +74,8 @@ Generates the roots for the IFFT from scratch (in bit-reversed order) Space req: 'ifft_roots' must have storage for n double complex values. -@param[in] n IFFT transform size (number of roots to generate) -@param[in] logn Minimum number of bits required to represent n (i.e. log2(n)) +@param[in] n IFFT transform size (number of roots to generate) +@param[in] logn Minimum number of bits required to represent n (i.e. log2(n)) @param[out] ifft_roots IFFT roots (in bit-reversed order) */ void calc_ifft_roots(size_t n, size_t logn, double complex *ifft_roots); @@ -87,8 +87,8 @@ void calc_ifft_roots(size_t n, size_t logn, double complex *ifft_roots); In-place Inverse Fast-Fourier Transform using the Harvey butterfly. 'roots' is ignored (and may be null) if SE_IFFT_OTF is chosen. -Note: This function does not divide the final result by n. This steps must be performed -outside of this function. +Note: This function does not divide the final result by n. This step must be performed outside of +this function. @param[in,out] vec Input/Output vector of n double complex values @param[in] n IFFT transform size (i.e. polynomial degree) diff --git a/device/lib/fileops.c b/device/lib/fileops.c index 7ca004c..c047e41 100644 --- a/device/lib/fileops.c +++ b/device/lib/fileops.c @@ -16,34 +16,35 @@ #include "util_print.h" #if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT) - #ifdef SE_DEFINE_SK_DATA - #include "str_sk.h" - #endif - #ifdef SE_DEFINE_PK_DATA - #include "str_pk_addr_array.h" - #endif - #ifdef SE_IFFT_LOAD_FULL - #include "str_ifft_roots.h" - #endif - #ifdef SE_FFT_LOAD_FULL - #include "str_fft_roots.h" - #endif - #if defined(SE_NTT_REG) || defined(SE_NTT_FAST) - #include "str_ntt_roots_addr_array.h" - #endif - #if defined(SE_INTT_REG) || defined(SE_INTT_FAST) - #include "str_intt_roots_addr_array.h" - #endif - #if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) - #include "str_index_map.h" - #endif +#ifdef SE_DEFINE_SK_DATA +#include "str_sk.h" +#endif +#ifdef SE_DEFINE_PK_DATA +#include "str_pk_addr_array.h" +#endif +#ifdef SE_IFFT_LOAD_FULL +#include "str_ifft_roots.h" +#endif +#ifdef SE_FFT_LOAD_FULL +#include "str_fft_roots.h" +#endif +#if defined(SE_NTT_REG) || defined(SE_NTT_FAST) +#include "str_ntt_roots_addr_array.h" +#endif +#if defined(SE_INTT_REG) || defined(SE_INTT_FAST) +#include "str_intt_roots_addr_array.h" +#endif +#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) +#include "str_index_map.h" +#endif #elif defined(SE_ON_SPHERE_A7) - #include - #include - #include // required for file open, close... +#include +#include +#include // required for file open, close... #else - #include - #include // required for file open, close... +#include +#include // required for file open, close... #endif #if !(defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT)) @@ -90,8 +91,7 @@ void check_ret(ssize_t ret, ssize_t bytes_expected, const char *fpath) } /** -Reads bytes stored at the specified location. On the Azure A7, this call called the -"image". +Reads bytes stored at the specified location. On the Azure A7, this call called the "image". Correctness: File located at 'fpath' must contain at least 'bytes_expected' values. Space req: 'vec' must have space for 'bytes_expected' bytes. @@ -105,11 +105,11 @@ void read_from_image(const char *fpath, size_t bytes_expected, void *vec) se_assert(fpath); se_assert(vec); se_assert(bytes_expected); - #ifdef SE_ON_SPHERE_A7 +#ifdef SE_ON_SPHERE_A7 int imageFile = Storage_OpenFileInImagePackage(fpath); - #else +#else int imageFile = open(fpath, 0); - #endif +#endif check_ret(imageFile, -1, fpath); ssize_t ret = 0; @@ -145,22 +145,22 @@ void load_sk(const Parms *parms, ZZ *s) // -- Image will always be in small form (2 bits per coeff) size_t bytes_expected = n / 4; #if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT) - #ifndef SE_DEFINE_SK_DATA +#ifndef SE_DEFINE_SK_DATA printf("Error! Sk data must be defined\n"); while (1) ; - #elif defined(SE_DATA_FROM_CODE_COPY) +#elif defined(SE_DATA_FROM_CODE_COPY) // uint8_t *sk_bytes = (uint8_t*)s; // for(size_t i = 0; i < bytes_expected; i++) sk_bytes[i] = secret_key[i]; memcpy(s, &(secret_key[0]), bytes_expected); return; - #else +#else SE_UNUSED(parms); SE_UNUSED(n); SE_UNUSED(bytes_expected); s = (ZZ *)(&(secret_key[0])); return; - #endif +#endif #else char fpath[MAX_FPATH_SIZE]; snprintf(fpath, MAX_FPATH_SIZE, "%s/sk_%zu.dat", SE_DATA_PATH, n); @@ -177,49 +177,48 @@ void load_pki(size_t i, const Parms *parms, ZZ *pki) size_t n = parms->coeff_count; size_t midx = parms->curr_modulus_idx; #if defined(SE_DATA_FROM_CODE_COPY) || defined(SE_DATA_FROM_CODE_DIRECT) - #ifndef SE_DEFINE_PK_DATA +#ifndef SE_DEFINE_PK_DATA SE_UNUSED(n); SE_UNUSED(midx); printf("Error! Pk data must be defined\n"); while (1) ; - #elif defined(SE_DATA_FROM_CODE_COPY) +#elif defined(SE_DATA_FROM_CODE_COPY) // for(size_t k = 0; k < n; k++) pki[j] = pk_addr[k]; memcpy(pki, pk_prime_addr[midx][i], n * sizeof(ZZ)); - #elif defined(SE_DATA_FROM_CODE_DIRECT) +#elif defined(SE_DATA_FROM_CODE_DIRECT) pki = pk_prime_addr[midx][i]; - #endif +#endif #else SE_UNUSED(midx); char fpath[MAX_FPATH_SIZE]; ZZ q = parms->curr_modulus->value; - #ifdef SE_NTT_NONE - snprintf(fpath, MAX_FPATH_SIZE, "%s/pk%zu_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, i, n, - q); - #else - snprintf(fpath, MAX_FPATH_SIZE, "%s/pk%zu_ntt_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, i, - n, q); - #endif +#ifdef SE_NTT_NONE + snprintf(fpath, MAX_FPATH_SIZE, "%s/pk%zu_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, i, n, q); +#else + snprintf(fpath, MAX_FPATH_SIZE, "%s/pk%zu_ntt_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, i, n, q); +#endif read_from_image(fpath, n * sizeof(ZZ), pki); #endif } -#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) +#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) void load_index_map(const Parms *parms, uint16_t *index_map) { size_t n = parms->coeff_count; - #ifdef SE_DATA_FROM_CODE_COPY +#ifdef SE_DATA_FROM_CODE_COPY memcpy(index_map, &(index_map_store[0]), n * sizeof(uint16_t)); - #elif defined(SE_DATA_FROM_CODE_DIRECT) +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(parms); SE_UNUSED(n); index_map = (uint16_t *)&(index_map_store[0]); - #else +#else char fpath[MAX_FPATH_SIZE]; snprintf(fpath, MAX_FPATH_SIZE, "%s/index_map_%zu.dat", SE_DATA_PATH, n); read_from_image(fpath, n * sizeof(uint16_t), index_map); - #endif +#endif } #endif @@ -227,24 +226,36 @@ void load_index_map(const Parms *parms, uint16_t *index_map) void load_ifft_roots(size_t n, double complex *ifft_roots) { se_assert(ifft_roots); - #ifdef SE_DATA_FROM_CODE_COPY - double *ifft_roots_double = (double *)(ifft_roots); - // -- This could be written more simply, but want to make clear - // that we are storing only 1 double at a time +#ifdef SE_DATA_FROM_CODE_COPY + /* + double *ifft_roots_double = (double *)(&(ifft_roots[0])); for (size_t i = 0; i < 2 * n; i += 2) { - ifft_roots_double[i] = (double)ifft_roots_save[i]; - ifft_roots_double[i + 1] = (double)ifft_roots_save[i + 1]; + // -- This could be written more simply, but want to make clear + // that we are storing only 1 double at a time + // uint64_t t1 = ifft_roots_save[i]; + // uint64_t *t1_p = &t1; + // double *t1_pd = (double*)(t1_p); + // double t1_d = *t1_pd; + // ifft_roots_double[i] = t1_d; + + // uint64_t t2 = ifft_roots_save[i+1]; + // uint64_t *t2_p = &t2; + // double *t2_pd = (double*)(t2_p); + // double t2_d = *t2_pd; + // ifft_roots_double[i+1] = t2_d; } - #elif defined(SE_DATA_FROM_CODE_DIRECT) + */ + memcpy(ifft_roots, ifft_roots_save, n * sizeof(double complex)); +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); ifft_roots = (double complex *)&(ifft_roots_save[0]); - #else +#else char fpath[MAX_FPATH_SIZE]; snprintf(fpath, MAX_FPATH_SIZE, "%s/ifft_roots_%zu.dat", SE_DATA_PATH, n); // printf("Retrieving ifft_roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(double complex), ifft_roots); - #endif +#endif } #endif @@ -252,7 +263,8 @@ void load_ifft_roots(size_t n, double complex *ifft_roots) void load_fft_roots(size_t n, double complex *fft_roots) { se_assert(fft_roots); - #ifdef SE_DATA_FROM_CODE_COPY +#ifdef SE_DATA_FROM_CODE_COPY + /* double *fft_roots_double = (double *)(fft_roots); // -- This could be written more simply, but want to make clear // that we are storing only 1 double at a time @@ -261,15 +273,17 @@ void load_fft_roots(size_t n, double complex *fft_roots) fft_roots_double[i] = (double)fft_roots_store[i]; fft_roots_double[i + 1] = (double)fft_roots_store[i + 1]; } - #elif defined(SE_DATA_FROM_CODE_DIRECT) + */ + memcpy(fft_roots, fft_roots_save, n * sizeof(double complex)); +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); fft_roots = (double complex *)&(fft_roots_store[0]); - #else +#else char fpath[MAX_FPATH_SIZE]; snprintf(fpath, MAX_FPATH_SIZE, "%s/fft_roots_%zu.dat", SE_DATA_PATH, n); // printf("Retrieving fft_roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(double complex), fft_roots); - #endif +#endif } #endif @@ -279,22 +293,21 @@ void load_ntt_roots(const Parms *parms, ZZ *ntt_roots) se_assert(parms && ntt_roots); size_t n = parms->coeff_count; size_t midx = parms->curr_modulus_idx; - #ifdef SE_DATA_FROM_CODE_COPY +#ifdef SE_DATA_FROM_CODE_COPY // for(size_t i = 0; i < n; i++) // { ntt_roots[i] = ntt_roots_addr[midx][i]; } memcpy(ntt_roots, ntt_roots_addr[midx], n * sizeof(ZZ)); - #elif defined(SE_DATA_FROM_CODE_DIRECT) +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); ntt_roots = ntt_roots_addr[midx]; - #else +#else SE_UNUSED(midx); ZZ q = parms->curr_modulus->value; char fpath[MAX_FPATH_SIZE]; - snprintf(fpath, MAX_FPATH_SIZE, "%s/ntt_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, - q); + snprintf(fpath, MAX_FPATH_SIZE, "%s/ntt_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, q); // printf("Retrieving ntt roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(ZZ), ntt_roots); - #endif +#endif } #endif @@ -304,22 +317,21 @@ void load_intt_roots(const Parms *parms, ZZ *intt_roots) se_assert(parms && intt_roots); size_t n = parms->coeff_count; size_t midx = parms->curr_modulus_idx; - #ifdef SE_DATA_FROM_CODE_COPY +#ifdef SE_DATA_FROM_CODE_COPY // for(size_t i = 0; i < n; i++) // { intt_roots[i] = intt_roots_addr[midx][i]; } memcpy(intt_roots, intt_roots_addr[midx], n * sizeof(ZZ)); - #elif defined(SE_DATA_FROM_CODE_DIRECT) +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); intt_roots = intt_roots_addr[midx]; - #else +#else SE_UNUSED(midx); ZZ q = parms->curr_modulus->value; char fpath[MAX_FPATH_SIZE]; - snprintf(fpath, MAX_FPATH_SIZE, "%s/intt_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, - q); + snprintf(fpath, MAX_FPATH_SIZE, "%s/intt_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, q); // printf("Retrieving inverse ntt roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(ZZ), intt_roots); - #endif +#endif } #endif @@ -329,24 +341,24 @@ void load_ntt_fast_roots(const Parms *parms, MUMO *ntt_fast_roots) se_assert(parms && ntt_fast_roots); size_t n = parms->coeff_count; size_t midx = parms->curr_modulus_idx; - #ifdef SE_DATA_FROM_CODE_COPY - for (size_t i = 0; i < n; i++) - { - ntt_fast_roots[i].operand = ntt_roots_addr[midx][2 * i]; - ntt_fast_roots[i].quotient = ntt_roots_addr[midx][2 * i + 1]; - } - #elif defined(SE_DATA_FROM_CODE_DIRECT) +#ifdef SE_DATA_FROM_CODE_COPY + // for (size_t i = 0; i < n; i++) + // { + // ntt_fast_roots[i].operand = ntt_roots_addr[midx][2 * i]; + // ntt_fast_roots[i].quotient = ntt_roots_addr[midx][2 * i + 1]; + // } + memcpy(ntt_fast_roots, ntt_roots_addr[midx], n * sizeof(MUMO)); +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); ntt_fast_roots = ntt_roots_addr[midx]; - #else +#else SE_UNUSED(midx); ZZ q = parms->curr_modulus->value; char fpath[MAX_FPATH_SIZE]; - snprintf(fpath, MAX_FPATH_SIZE, "%s/ntt_fast_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, - n, q); + snprintf(fpath, MAX_FPATH_SIZE, "%s/ntt_fast_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, q); // printf("Retrieving fast roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(MUMO), ntt_fast_roots); - #endif +#endif } #endif @@ -356,23 +368,25 @@ void load_intt_fast_roots(const Parms *parms, MUMO *intt_fast_roots) se_assert(parms && intt_fast_roots); size_t n = parms->coeff_count; size_t midx = parms->curr_modulus_idx; - #ifdef SE_DATA_FROM_CODE_COPY +#ifdef SE_DATA_FROM_CODE_COPY + /* for (size_t i = 0; i < n; i++) { intt_fast_roots[i].operand = intt_roots_addr[midx][2 * i]; intt_fast_roots[i].quotient = intt_roots_addr[midx][2 * i + 1]; } - #elif defined(SE_DATA_FROM_CODE_DIRECT) + */ + memcpy(intt_fast_roots, intt_roots_addr[midx], n * sizeof(MUMO)); +#elif defined(SE_DATA_FROM_CODE_DIRECT) SE_UNUSED(n); intt_fast_roots = intt_roots_addr[midx]; - #else +#else SE_UNUSED(midx); ZZ q = parms->curr_modulus->value; char fpath[MAX_FPATH_SIZE]; - snprintf(fpath, MAX_FPATH_SIZE, "%s/intt_fast_roots_%zu_%" PRIuZZ ".dat", - SE_DATA_PATH, n, q); + snprintf(fpath, MAX_FPATH_SIZE, "%s/intt_fast_roots_%zu_%" PRIuZZ ".dat", SE_DATA_PATH, n, q); // printf("Retrieving fast inverse ntt roots from file located at: %s\n", fpath); read_from_image(fpath, n * sizeof(MUMO), intt_fast_roots); - #endif +#endif } #endif diff --git a/device/lib/fileops.h b/device/lib/fileops.h index 2ad7cda..fda95ef 100644 --- a/device/lib/fileops.h +++ b/device/lib/fileops.h @@ -21,13 +21,13 @@ void read_from_image(const char *fpath, size_t bytes_expected, void *vec); Loads the secret key from storage, where the secret key is assumed to be stored in small (compressed) form. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the secret key -should be hard-coded in "kri_data/str_secret_key.h" in an array object called -"secret_key_store". This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the secret key should be +hard-coded in "kri_data/str_secret_key.h" in an array object called "secret_key_store". This file +can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, the secret key is assumed to be stored in -binary form in a file called "sk_.dat", where is the value of the -polynomial degree. This file can also be generated using the SEAL-Embedded adapter. +Otherwise, if this function is called, the secret key is assumed to be stored in binary form in a +file called "sk_.dat", where is the value of the polynomial degree. This file can also be +generated using the SEAL-Embedded adapter. Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 's' should contain space for 2n bits. @@ -40,49 +40,46 @@ void load_sk(const Parms *parms, ZZ *s); /** Loads (one component of) the public key from storage. -A full public key consists of two components per modulus prime in the modulus -switching chain. For example, if there are 3 primes in the modulus switching -chain, the public key would consists of 3 per-prime public keys PK0, PK1, PK2, -where each PKi conists of two polynomials {pk0, pk1}, for a total of 6 -polynomials, and therefore 6 components. - -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the public key -should be hard-coded in "kri_data/str_pk_addr_array.h", with the starting addresses -of each component stored in an array called 'pk_prime_addr' (contained in the same -file). This file can be generated using the SEAL-Embedded adapter. - -Otherwise, if this function is called, each public key component is assumed to be -stored in a separate file and in binary form. The file should be called -"pk__.dat", where is 0 or 1, is the value of the polynomial degree, -and is the value of the modulus prime for the particular public key component, -if the public key is in non-ntt form. If the public key is in NTT form, the file -should actually be called "pk_ntt__.dat". Both of these files can also be +A full public key consists of two components per modulus prime in the modulus switching chain. For +example, if there are 3 primes in the modulus switching chain, the public key would consists of 3 +per-prime public keys PK0, PK1, PK2, where each PKi conists of two polynomials {pk0, pk1}, for a +total of 6 polynomials, and therefore 6 components. + +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the public key should be +hard-coded in "kri_data/str_pk_addr_array.h", with the starting addresses of each component stored +in an array called 'pk_prime_addr' (contained in the same file). This file can be generated using +the SEAL-Embedded adapter. + +Otherwise, if this function is called, each public key component is assumed to be stored in a +separate file and in binary form. The file should be called "pk__.dat", where is 0 or +1, is the value of the polynomial degree, and is the value of the modulus prime for the +particular public key component, if the public key is in non-ntt form. If the public key is in NTT +form, the file should actually be called "pk_ntt__.dat". Both of these files can also be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'pki' should contain space -for n ZZ elements. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'pki' should contain space for n ZZ elements. -@param[in] i Requested polynomial component of the public key for - the current modulus prime +@param[in] i Requested polynomial component of the public key for the current modulus prime @param[in] parms Parameters set by ckks_setup @param[out] pki Public key component */ void load_pki(size_t i, const Parms *parms, ZZ *pki); -#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) +#if defined(SE_INDEX_MAP_LOAD) || defined(SE_INDEX_MAP_LOAD_PERSIST) || \ + defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) /** Loads the values of the index map from storage. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the index map -should be hard-coded in "kri_data/str_index_map.h" in an array object called -"index_map_store". This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the index map should be +hard-coded in "kri_data/str_index_map.h" in an array object called "index_map_store". This file can +be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, the index map is assumed to be stored in -binary form in a file called "index_map_.dat", where is the value of the -polynomial degree. This file can also be generated using the SEAL-Embedded adapter. +Otherwise, if this function is called, the index map is assumed to be stored in binary form in a +file called "index_map_.dat", where is the value of the polynomial degree. This file can also +be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'index_map' should contain space -for n uint16_t values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'index_map' should contain space for n +uint16_t values. @param[in] parms Parameters set by ckks_setup @param[out] index_map Buffer containing index map values @@ -94,16 +91,16 @@ void load_index_map(const Parms *parms, uint16_t *index_map); /** Loads the values of the IFFT roots from storage. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the ifft roots -should be hard-coded in "kri_data/str_ifft_roots.h" in an array object called -"ifft_roots_store". This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the ifft roots should be +hard-coded in "kri_data/str_ifft_roots.h" in an array object called "ifft_roots_store". This file +can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, the ifft_roots are assumed to be stored in -binary form in a file called "ifft_roots_.dat", where is the value of the -polynomial degree. This file can also be generated using the SEAL-Embedded adapter. +Otherwise, if this function is called, the ifft_roots are assumed to be stored in binary form in a +file called "ifft_roots_.dat", where is the value of the polynomial degree. This file can +also be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'ifft_roots' must contain space for -n double complex values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'ifft_roots' must contain space for n double +complex values. @param[in] n Number of roots to load (i.e. polynomial degree) @param[out] ifft_roots IFFT roots @@ -115,16 +112,16 @@ void load_ifft_roots(size_t n, double complex *ifft_roots); /** Loads the values of the FFT roots from storage. This is mainly useful for debugging. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the fft roots -should be hard-coded in "kri_data/str_fft_roots.h" in an array object called -"fft_roots_store". This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the fft roots should be +hard-coded in "kri_data/str_fft_roots.h" in an array object called "fft_roots_store". This file can +be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, the fft_roots are assumed to be stored in -binary form in a file called "fft_roots_.dat", where is the value of the -polynomial degree. This file can also be generated using the SEAL-Embedded adapter. +Otherwise, if this function is called, the fft_roots are assumed to be stored in binary form in a +file called "fft_roots_.dat", where is the value of the polynomial degree. This file can also +be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'fft_roots' must contain space for -n double complex values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'fft_roots' must contain space for n double +complex values. @param[in] n Number of roots to load (i.e. polynomial degree) @param[out] fft_roots FFT roots @@ -134,25 +131,23 @@ void load_fft_roots(size_t n, double complex *fft_roots); #ifdef SE_NTT_REG /** -Loads the values of the NTT roots (in regular form) for the current modulus prime from -storage. +Loads the values of the NTT roots (in regular form) for the current modulus prime from storage. -A full set of NTT roots consists of as many components are there are modulus primes -in the modulus switching chain. +A full set of NTT roots consists of as many components are there are modulus primes in the modulus +switching chain. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of NTT -roots should be hard-coded in "kri_data/str_ntt_roots_addr_array.h", with the starting -addresses of each component of NTT roots stored in an array called 'ntt_roots_addr' -(contained in the same file). This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of NTT roots should +be hard-coded in "kri_data/str_ntt_roots_addr_array.h", with the starting addresses of each +component of NTT roots stored in an array called 'ntt_roots_addr' (contained in the same file). This +file can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, each NTT component is assumed to be stored in a -separate file and in binary form. The file should be called "ntt_roots__.dat", -where is the value of the polynomial degree, and is the value of the modulus prime -for the particular NTT component. Both of these files can be generated using the -SEAL-Embedded adapter. +Otherwise, if this function is called, each NTT component is assumed to be stored in a separate file +and in binary form. The file should be called "ntt_roots__.dat", where is the value of the +polynomial degree, and is the value of the modulus prime for the particular NTT component. Both +of these files can be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'ntt_roots' must contain space for -n ZZ values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'ntt_roots' must contain space for n ZZ +values. @param[in] n Number of roots to load (i.e. polynomial degree) @param[out] ntt_roots NTT roots @@ -162,25 +157,23 @@ void load_ntt_roots(const Parms *parms, ZZ *ntt_roots); #ifdef SE_INTT_REG /** -Loads the values of the INTT roots (in regular form) for the current modulus prime from -storage. +Loads the values of the INTT roots (in regular form) for the current modulus prime from storage. -A full set of INTT roots consists of as many components are there are modulus primes -in the modulus switching chain. +A full set of INTT roots consists of as many components are there are modulus primes in the modulus +switching chain. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of INTT -roots should be hard-coded in "kri_data/str_intt_roots_addr_array.h", with the starting -addresses of each component of INTT roots stored in an array called 'intt_roots_addr' -(contained in the same file). This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of INTT roots should +be hard-coded in "kri_data/str_intt_roots_addr_array.h", with the starting addresses of each +component of INTT roots stored in an array called 'intt_roots_addr' (contained in the same file). +This file can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, each INTT component is assumed to be stored in a -separate file and in binary form. The file should be called "intt_roots__.dat", -where is the value of the polynomial degree, and is the value of the modulus prime -for the particular INTT component. Both of these files can be generated using the -SEAL-Embedded adapter. +Otherwise, if this function is called, each INTT component is assumed to be stored in a separate +file and in binary form. The file should be called "intt_roots__.dat", where is the value +of the polynomial degree, and is the value of the modulus prime for the particular INTT +component. Both of these files can be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'intt_roots' must contain space for -n ZZ values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'intt_roots' must contain space for n ZZ +values. @param[in] n Number of roots to load (i.e. polynomial degree) @param[out] intt_roots INTT roots @@ -190,22 +183,20 @@ void load_intt_roots(const Parms *parms, ZZ *intt_roots); #ifdef SE_NTT_FAST /** -Loads the values of the "fast" (a.k.a. "lazy") NTT roots for the current modulus prime -from storage. +Loads the values of the "fast" (a.k.a. "lazy") NTT roots for the current modulus prime from storage. -A full set of "fast" NTT roots consists of as many components are there are modulus primes -in the modulus switching chain. +A full set of "fast" NTT roots consists of as many components are there are modulus primes in the +modulus switching chain. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of NTT -roots should be hard-coded in "kri_data/str_ntt_roots_addr_array.h", with the starting -addresses of each component of NTT roots stored in an array called 'ntt_fast_roots_addr' -(contained in the same file). This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of NTT roots should +be hard-coded in "kri_data/str_ntt_roots_addr_array.h", with the starting addresses of each +component of NTT roots stored in an array called 'ntt_fast_roots_addr' (contained in the same file). +This file can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, each NTT component is assumed to be stored in a -separate file and in binary form. The file should be called "ntt_fast_roots__.dat", -where is the value of the polynomial degree, and is the value of the modulus prime -for the particular NTT component. Both of these files can be generated using the -SEAL-Embedded adapter. +Otherwise, if this function is called, each NTT component is assumed to be stored in a separate file +and in binary form. The file should be called "ntt_fast_roots__.dat", where is the value +of the polynomial degree, and is the value of the modulus prime for the particular NTT +component. Both of these files can be generated using the SEAL-Embedded adapter. Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'fast_ntt_roots' must contain space for 2n ZZ values. @@ -218,25 +209,24 @@ void load_ntt_fast_roots(const Parms *parms, MUMO *ntt_fast_roots); #ifdef SE_INTT_FAST /** -Loads the values of the "fast" (a.k.a. "lazy") INTT roots for the current modulus prime -from storage. +Loads the values of the "fast" (a.k.a. "lazy") INTT roots for the current modulus prime from +storage. -A full set of "fast" INTT roots consists of as many components are there are modulus -primes in the modulus switching chain. +A full set of "fast" INTT roots consists of as many components are there are modulus primes in the +modulus switching chain. -If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of INTT -roots should be hard-coded in "kri_data/str_intt_roots_addr_array.h", with the starting -addresses of each component of INTT roots stored in an array called 'intt_fast_roots_addr' -(contained in the same file). This file can be generated using the SEAL-Embedded adapter. +If SE_DATA_FROM_CODE_COPY or SE_DATA_FROM_CODE_DIRECT are defined, the full set of INTT roots should +be hard-coded in "kri_data/str_intt_roots_addr_array.h", with the starting addresses of each +component of INTT roots stored in an array called 'intt_fast_roots_addr' (contained in the same +file). This file can be generated using the SEAL-Embedded adapter. -Otherwise, if this function is called, each INTT component is assumed to be stored in a -separate file and in binary form. The file should be called "intt_fast_roots__.dat", -where is the value of the polynomial degree, and is the value of the modulus prime -for the particular INTT component. Both of these files can be generated using the -SEAL-Embedded adapter. +Otherwise, if this function is called, each INTT component is assumed to be stored in a separate +file and in binary form. The file should be called "intt_fast_roots__.dat", where is the +value of the polynomial degree, and is the value of the modulus prime for the particular INTT +component. Both of these files can be generated using the SEAL-Embedded adapter. -Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'intt_fast_roots' must contain -space for 2n ZZ values. +Space req: If SE_DATA_FROM_CODE_DIRECT is not defined, 'intt_fast_roots' must contain space for 2n +ZZ values. @param[in] n Number of roots to load (i.e. polynomial degree) @param[out] intt_fast_roots "Fast" INTT roots diff --git a/device/lib/intt.c b/device/lib/intt.c index 765497a..93cb2db 100644 --- a/device/lib/intt.c +++ b/device/lib/intt.c @@ -17,55 +17,10 @@ #include "uintmodarith.h" #include "util_print.h" -#if defined(SE_INTT_OTF) || defined(SE_INTT_ONE_SHOT) - -/** -Helper function to return root for certain modulus prime values if SE_INTT_OTF or -SE_INTT_ONE_SHOT is used. Implemented as a table lookup. +#ifndef SE_DISABLE_TESTING_CAPABILITY // INTT is only needed for on-device testing -@param[in] n Transform size (i.e. polynomial ring degree) -@param[in] q Modulus value -*/ -ZZ get_intt_root(size_t n, ZZ q) -{ - // TODO: Add more primes - ZZ inv_root; - switch (n) - { - case 16: - switch (q) - { - case 1071415297: inv_root = 615162833; break; - case 1071513601: inv_root = 208824698; break; - case 1072496641: inv_root = 750395333; break; - default: { - printf("Error! Need first power of root for intt, n = 16\n"); - print_zz("Modulus value", q); - exit(1); - } - } - break; - case 4096: - switch (q) - { - case 1071415297: inv_root = 710091420; break; - case 1071513601: inv_root = 711691669; break; - case 1072496641: inv_root = 85699917; break; - default: { - printf("Error! Need first power of root for intt, n = 4K\n"); - print_zz("Modulus value", q); - exit(1); - } - } - break; - default: { - printf("Error! Need first power of root for intt\n"); - print_zz("Modulus value", q); - exit(1); - } - } - return inv_root; -} +#if defined(SE_INTT_OTF) || defined(SE_INTT_ONE_SHOT) +ZZ get_intt_root(size_t n, ZZ q); // defined below #endif void intt_roots_initialize(const Parms *parms, ZZ *intt_roots) @@ -88,6 +43,7 @@ void intt_roots_initialize(const Parms *parms, ZZ *intt_roots) intt_roots[0] = 1; // Not necessary but set anyway for (size_t i = 1; i < n; i++) { + // -- For intt, the roots are not in *exactly* bit-reversed order. This is correct. intt_roots[bitrev(i - 1, logn) + 1] = power; power = mul_mod(power, inv_root, mod); } @@ -102,20 +58,19 @@ void intt_roots_initialize(const Parms *parms, ZZ *intt_roots) #ifdef SE_INTT_FAST /** -Performs a "fast" (a.k.a. "lazy") negacyclic in-place inverse NTT using the Harvey -butterfly, using "fast" INTT roots. Only used if "SE_INTT_FAST" is defined. See -SEAL_Embedded paper for a more detailed description. "Lazy"-ness refers to fact that we -here we lazily opt to reduce values only at the very end. +Performs a "fast" (a.k.a. "lazy") negacyclic in-place inverse NTT using the Harvey butterfly, using +"fast" INTT roots. Only used if "SE_INTT_FAST" is defined. See SEAL_Embedded paper for a more +detailed description. "Lazy"-ness refers to fact that we here we lazily opt to reduce values only at +the very end. @param[in] parms Parameters set by ckks_setup @param[in] intt_fast_roots INTT roots set by intt_roots_initialize -@param[in] inv_n Inverse of n mod q, where q is the value of the current -modulus prime -@param[in] inv_n_w +@param[in] inv_n Inverse of n mod q, where q = value of the current modulus prime +@param[in] last_inv_sn @param[in,out] vec Input/output polynomial of n ZZ elements */ void intt_lazy_inpl(const Parms *parms, const MUMO *intt_fast_roots, const MUMO *inv_n, - const MUMO *inv_n_w, ZZ *vec) + const MUMO *last_inv_sn, ZZ *vec) { se_assert(parms && ntt_lazy_roots && vec); size_t n = parms->coeff_count; @@ -127,6 +82,7 @@ void intt_lazy_inpl(const Parms *parms, const MUMO *intt_fast_roots, const MUMO size_t root_idx = 1; // We technically don't need to store the 0th one... // print_poly_full("vec", vec, n); + // -- Do everything except the last round for (size_t i = 0; i < (parms->logn - 1); i++, tt *= 2, h /= 2) // rounds { for (size_t j = 0, kstart = 0; j < h; j++, kstart += 2 * tt) // groups @@ -148,7 +104,13 @@ void intt_lazy_inpl(const Parms *parms, const MUMO *intt_fast_roots, const MUMO // print_poly_full("vec", vec, n); } + print_zz("tt", tt); + print_zz("h", h); + print_zz("root_idx", root_idx); + // print_poly_full("vec", vec, n); + // -- Finally, need to multiply by n^{-1} + // -- Merge this step with the last round we would have performed in the above loop. for (size_t j = 0; j < n / 2; j++) { ZZ u = vec[j]; @@ -160,29 +122,26 @@ void intt_lazy_inpl(const Parms *parms, const MUMO *intt_fast_roots, const MUMO ZZ tval1 = val1 - (two_q & (ZZ)(-(ZZsign)(val1 >= two_q))); vec[j] = mul_mod_mumo_lazy(tval1, inv_n, mod); - vec[j + n / 2] = mul_mod_mumo_lazy(val2, inv_n_w, mod); + vec[j + n / 2] = mul_mod_mumo_lazy(val2, last_inv_sn, mod); } // print_poly_full("vec", vec, n); } #else /** -Performs a negacyclic in-place inverse NTT using the Harvey butterfly. Only used if -SE_INTT_FAST is not defined. +Performs a negacyclic in-place inverse NTT using the Harvey butterfly. Only used if SE_INTT_FAST is +not defined. If SE_INTT_REG or SE_INTT_ONE_SHOT is defined, will use regular INTT computation. -Else, (SE_INTT_OTF is defined), will use truly "on-the-fly" INTT computation. In this last -case, 'intt_roots' may be null (and will be ignored). - -@param[in] parms Parameters set by ckks_setup -@param[in] intt_roots INTT roots set by intt_roots_initialize. Ignored if SE_INTT_OTF -is defined. -@param[in] inv_n Inverse of n mod q, where q is the value of the current modulus -prime -@param[in] inv_n_w -@param[in,out] vec Input/output polynomial of n ZZ elements +Else, (SE_INTT_OTF is defined), will use truly "on-the-fly" INTT computation. In this last case, +'intt_roots' may be null (and will be ignored). + +@param[in] parms Parameters set by ckks_setup +@param[in] intt_roots Roots set by intt_roots_initialize. Ignored if SE_INTT_OTF is defined. +@param[in] inv_n (n)^-1 mod q_i, where q_i is the value of the current modulus prime +@param[in] last_inv_sn +@param[in,out] vec Input/output polynomial of n ZZ elements */ -void intt_non_lazy_inpl(const Parms *parms, const ZZ *intt_roots, ZZ inv_n, ZZ inv_n_w, - ZZ *vec) +void intt_non_lazy_inpl(const Parms *parms, const ZZ *intt_roots, ZZ inv_n, ZZ last_inv_sn, ZZ *vec) { // -- See ntt.c for a more detailed explaination of algorithm @@ -195,26 +154,37 @@ void intt_non_lazy_inpl(const Parms *parms, const ZZ *intt_roots, ZZ inv_n, ZZ i size_t tt = 1; // size of butterflies size_t h = n / 2; // number of groups - #ifdef SE_INTT_OTF +#ifdef SE_INTT_OTF SE_UNUSED(intt_roots); ZZ root = get_intt_root(n, mod->value); - #elif defined(SE_INTT_ONE_SHOT) || defined(SE_INTT_REG) +#elif defined(SE_INTT_ONE_SHOT) || defined(SE_INTT_REG) size_t root_idx = 1; // We technically don't need to store the 0th one... - #endif +#endif + // -- Option 1: Do everything except the last round for (size_t i = 0; i < (logn - 1); i++, tt *= 2, h /= 2) // rounds + // -- Option 2: Do everything incluing the last round + // for (size_t i = 0; i < logn; i++, tt *= 2, h /= 2) // rounds { for (size_t j = 0, kstart = 0; j < h; j++, kstart += 2 * tt) // groups { - #ifdef SE_INTT_OTF +#ifdef SE_INTT_OTF // ZZ power = bitrev(h + j, logn); // ZZ s = exponentiate_uint_mod(root, power, mod); ZZ power = h + j; ZZ s = exponentiate_uint_mod_bitrev(root, power, logn, mod); - #elif defined(SE_INTT_ONE_SHOT) || defined(SE_INTT_REG) +#elif defined(SE_INTT_ONE_SHOT) || defined(SE_INTT_REG) se_assert(intt_roots); ZZ s = intt_roots[root_idx++]; - #endif +#endif + /* + if (i == logn - 1) + { + printf("\tinverse_tt, last j: %zu\n", j); + printf("\tinverse_tt, last h: %zu\n", h); + print_zz("\tinverse_ntt, last s", s); + } + */ for (size_t k = kstart; k < (kstart + tt); k++) // pairs { ZZ u = vec[k]; @@ -227,17 +197,28 @@ void intt_non_lazy_inpl(const Parms *parms, const ZZ *intt_roots, ZZ inv_n, ZZ i } // print_poly_full("vec", vec, n); + // print_zz("\tinverse ntt: tt", tt); + // print_zz("\tinverse ntt: h", h); +#if defined(SE_INTT_ONE_SHOT) || defined(SE_INTT_REG) + print_zz("inverse ntt: root_idx", root_idx); +#endif + // -- Finally, need to multiply by n^{-1} + // -- Merge this step with the last round we would have performed in the above loop. for (size_t i = 0; i < n / 2; i++) { - ///* ZZ u = vec[i]; ZZ v = vec[i + n / 2]; + ///* + // -- Use this if we do one less round above (Option 1), and merge with that last round vec[i] = mul_mod(add_mod(u, v, mod), inv_n, mod); - vec[i + n / 2] = mul_mod(sub_mod(u, v, mod), inv_n_w, mod); + vec[i + n / 2] = mul_mod(sub_mod(u, v, mod), last_inv_sn, mod); //*/ - // mul_mod_inpl(&(vec[i]), inv_n, mod); - // mul_mod_inpl(&(vec[i + n / 2]), inv_n, mod); + // -- This is just normal mult by n-inverse (Option 2) + /* + mul_mod_inpl(&(vec[i]), inv_n, mod); + mul_mod_inpl(&(vec[i + n / 2]), inv_n, mod); + */ } } #endif @@ -248,121 +229,263 @@ void intt_inpl(const Parms *parms, const ZZ *intt_roots, ZZ *vec) size_t n = parms->coeff_count; Modulus *mod = parms->curr_modulus; - ZZ inv_n, inv_n_w; + ZZ inv_n, last_inv_sn; + + /** + For custom primes: + Add cases for custom primes at the indicated locations. + Note that this is only required if: + 1) paramaters (specifically, primes) desired are of custom (i.e., non-default) type + 2) on-device testing is desired + + If any of the above conditions are false, no modifications to this function are needed. + Note: wolframalpha is a good tool to use to calculate constants. + */ + + // -- Note: inv_n = n^(-1) mod qi + // last_inv_sn = (s*n)^(-1) mod qi + // where s = last root in the loop (see intt code) + // -- Example calculations for n = 4096: + // 1072496641: last_inv_s = 420478808 ---> last_ii_s = 420478808^(-1) mod 1072496641 = + // 652017833 + // last_inv_sn = (last_inv_s * inv_n ) mod 1072496641 + // = (420478808 * 1072234801) mod 1072496641 = 44091776 + // check: = (last_ii_s * n )^(-1) mod 1072496641 + // = (652017833 * 4096)^(-1) mod 1072496641 = 44091776 --> Correct + // 1071513601: last_inv_s = 393998159 ---> last_ii_s = 393998159^(-1) mod 1071513601 = + // 677515442 + // last_inv_sn = (last_inv_s * inv_n ) mod 1071513601 + // = (393998159 * 1071252001) mod 1071513601 = 46399391 + // check: = (last_ii_s * n )^(-1) mod 1071513601 + // = (677515442 * 4096)^(-1) mod 1071513601 = 46399391 --> Correct + // 1071415297: last_inv_s = 476369346 ---> last_ii_s = 476369346^(-1) mod 1071415297 = + // 595045951 + // last_inv_sn = (last_inv_s * inv_n ) mod 1071415297 + // = (476369346 * 1071153721) mod 1071415297 = 953822398 + // check: = (last_ii_s * n )^(-1) mod 1071415297 + // = (595045951 * 4096)^(-1) mod 1071415297 = 953822398 --> + // Correct + // These values are also output by the SEAL-Embedded adapter - // -- Note: inv_n equals n^(-1) mod qi - // and inv_n_w equals (n * w)^(-1) mod qi - // where w = first (non-inverse) ntt root - switch (mod->value) + switch (n) { - case 1071415297: - switch (n) + case 1024: + // -- Add cases for custom primes here + se_assert(mod->value == 134012929); + inv_n = 133882057; + last_inv_sn = 133898800; + break; + case 2048: + // -- Add cases for custom primes here + se_assert(mod->value == 134012929); + inv_n = 133947493; + last_inv_sn = 66949400; + break; + case 4096: + switch (mod->value) { - // w = 595045951 - case 16: - inv_n = 1004451841; - inv_n_w = 967261469; + // -- Add cases for custom primes here + // -- 27 bit + case 134012929: + inv_n = 133980211; + last_inv_sn = 33474700; break; - case 1024: inv_n = 1070368993; inv_n_w = 601043701; - case 2048: inv_n = 1070892145; inv_n_w = 836229499; - case 4096: - inv_n = 1071153721; - inv_n_w = 953822398; + case 134111233: + inv_n = 134078491; + last_inv_sn = 87415839; break; - default: printf("Error with intt inv_n\n"); exit(1); + case 134176769: + inv_n = 134144011; + last_inv_sn = 23517844; + break; + // -- 30 bit + case 1053818881: + inv_n = 1053561601; + last_inv_sn = 543463427; + break; + case 1054015489: + inv_n = 1053758161; + last_inv_sn = 67611149; + break; + case 1054212097: + inv_n = 1053954721; + last_inv_sn = 1050429792; + break; + default: printf("Error with intt inv_n or last_inv_sn\n"); exit(1); } break; - - case 1071513601: - switch (n) + case 8192: + switch (mod->value) { - // w = 677515442 - case 16: - inv_n = 1004544001; - inv_n_w = 91594485; + // -- Add cases for custom primes here + case 1053818881: + inv_n = 1053690241; + last_inv_sn = 255177727; break; - case 1024: - inv_n = 1070467201; - inv_n_w = 46399391; + case 1054015489: + inv_n = 1053886825; + last_inv_sn = 493202170; break; - case 2048: - inv_n = 1070990401; - inv_n_w = 92798782; + case 1054212097: + inv_n = 1054083409; + last_inv_sn = 528997201; break; - case 4096: - inv_n = 1071252001; - inv_n_w = 46399391; + case 1055260673: + inv_n = 1055131857; + last_inv_sn = 794790938; break; - default: printf("Error with intt inv_n\n"); exit(1); + case 1056178177: + inv_n = 1056049249; + last_inv_sn = 417300148; + break; + case 1056440321: + inv_n = 1056311361; + last_inv_sn = 565858923; + break; + default: printf("Error with intt inv_n or last_inv_sn\n"); exit(1); } break; - - case 1072496641: - switch (n) + case 16384: + switch (mod->value) { - // w = 652017833 - case 16: - inv_n = 1005465601; - inv_n_w = 562528246; + // -- Add cases for custom primes here + case 1053818881: + inv_n = 1053754561; + last_inv_sn = 399320577; break; - case 1024: - inv_n = 1071449281; - inv_n_w = 176367104; + case 1054015489: + inv_n = 1053951157; + last_inv_sn = 246601085; break; - case 2048: - inv_n = 1071972961; - inv_n_w = 88183552; + case 1054212097: + inv_n = 1054147753; + last_inv_sn = 791604649; break; - case 4096: - inv_n = 1072234801; - inv_n_w = 44091776; + case 1055260673: + inv_n = 1055196265; + last_inv_sn = 657865204; break; - default: printf("Error with intt inv_n\n"); exit(1); + case 1056178177: + inv_n = 1056113713; + last_inv_sn = 208650074; + break; + case 1056440321: + inv_n = 1056375841; + last_inv_sn = 811149622; + break; + case 1058209793: + inv_n = 1058145205; + last_inv_sn = 471841522; + break; + case 1060175873: + inv_n = 1060111165; + last_inv_sn = 791157060; + break; + case 1060700161: + inv_n = 1060635421; + last_inv_sn = 122051856; + break; + case 1060765697: + inv_n = 1060700953; + last_inv_sn = 79059697; + break; + case 1061093377: + inv_n = 1061028613; + last_inv_sn = 82969774; + break; + case 1062469633: + inv_n = 1062404785; + last_inv_sn = 513630557; + break; + case 1062535169: + inv_n = 1062470317; + last_inv_sn = 693696473; + break; + default: printf("Error with intt inv_n or last_inv_sn\n"); exit(1); } break; default: printf("Error with intt inv_n\n"); exit(1); } + #ifdef SE_INTT_FAST se_assert(intt_roots); - MUMO inv_n_mumo, inv_n_w_mumo; - inv_n_mumo.operand = inv_n; - inv_n_w_mumo.operand = inv_n_w; + MUMO inv_n_mumo, last_inv_sn_mumo; + inv_n_mumo.operand = inv_n; + last_inv_sn_mumo.operand = last_inv_sn; switch (n) { - case 16: inv_n_mumo.quotient = 4026531840; break; + case 1024: inv_n_mumo.quotient = 4290772992; break; + case 2048: inv_n_mumo.quotient = 4292870144; break; case 4096: inv_n_mumo.quotient = 4293918720; break; + case 8192: inv_n_mumo.quotient = 4294443008; break; + case 16384: inv_n_mumo.quotient = 4294705152; break; default: printf("Error with intt inv_n\n"); exit(1); } - switch (mod->value) + switch (n) { - case 1071415297: - switch (n) + case 1024: + // -- Add cases for custom primes here + se_assert(mod->value == 134012929); + last_inv_sn_mumo.quotient = 4291309586; + break; + case 2048: + // -- Add cases for custom primes here + se_assert(mod->value == 134012929); + last_inv_sn_mumo.quotient = 2145654793; + break; + case 4096: { + switch (mod->value) { - case 16: inv_n_w_mumo.quotient = 417519972; break; - case 4096: inv_n_w_mumo.quotient = 3823574310; break; + // -- Add cases for custom primes here + // -- 27 bit + case 134012929: last_inv_sn_mumo.quotient = 1072827396; break; + case 134111233: last_inv_sn_mumo.quotient = 2799528132; break; + case 134176769: last_inv_sn_mumo.quotient = 752800738; break; + // -- 30 bit + case 1053818881: last_inv_sn_mumo.quotient = 2214951437; break; + case 1054015489: last_inv_sn_mumo.quotient = 275506078; break; + case 1054212097: last_inv_sn_mumo.quotient = 4279557800; break; default: printf("Error with intt inv_n\n"); exit(1); } break; - case 1071513601: - switch (n) + } + case 8192: + switch (mod->value) { - case 16: inv_n_w_mumo.quotient = 3927827469; break; - case 4096: inv_n_w_mumo.quotient = 185983515; break; + // -- Add cases for custom primes here + case 1053818881: last_inv_sn_mumo.quotient = 1040007929; break; + case 1054015489: last_inv_sn_mumo.quotient = 2009730608; break; + case 1054212097: last_inv_sn_mumo.quotient = 2155188395; break; + case 1055260673: last_inv_sn_mumo.quotient = 3234841563; break; + case 1056178177: last_inv_sn_mumo.quotient = 1696958455; break; + case 1056440321: last_inv_sn_mumo.quotient = 2300504363; break; default: printf("Error with intt inv_n\n"); exit(1); } break; - case 1072496641: - switch (n) + case 16384: + switch (mod->value) { - case 16: inv_n_w_mumo.quotient = 2252725395; break; - case 4096: inv_n_w_mumo.quotient = 176571868; break; + // -- Add cases for custom primes here + case 1053818881: last_inv_sn_mumo.quotient = 1627479683; break; + case 1054015489: last_inv_sn_mumo.quotient = 1004865304; break; + case 1054212097: last_inv_sn_mumo.quotient = 3225077845; break; + case 1055260673: last_inv_sn_mumo.quotient = 2677546514; break; + case 1056178177: last_inv_sn_mumo.quotient = 848479227; break; + case 1056440321: last_inv_sn_mumo.quotient = 3297735829; break; + case 1058209793: last_inv_sn_mumo.quotient = 1915068183; break; + case 1060175873: last_inv_sn_mumo.quotient = 3205122645; break; + case 1060700161: last_inv_sn_mumo.quotient = 494210097; break; + case 1060765697: last_inv_sn_mumo.quotient = 320107271; break; + case 1061093377: last_inv_sn_mumo.quotient = 335835161; break; + case 1062469633: last_inv_sn_mumo.quotient = 2076319525; break; + case 1062535169: last_inv_sn_mumo.quotient = 2804051810; break; default: printf("Error with intt inv_n\n"); exit(1); } break; - default: printf("Error with intt inv_n_w\n"); exit(1); + default: printf("Error with intt inv_n\n"); exit(1); } - intt_lazy_inpl(parms, (MUMO *)intt_roots, &inv_n_mumo, &inv_n_w_mumo, vec); + intt_lazy_inpl(parms, (MUMO *)intt_roots, &inv_n_mumo, &last_inv_sn_mumo, vec); // -- Final adjustments: compute a[j] = a[j] * n^{-1} mod q. // -- We incorporated mult by n inverse in the butterfly. Only need to reduce here. @@ -373,6 +496,113 @@ void intt_inpl(const Parms *parms, const ZZ *intt_roots, ZZ *vec) } #else // -- Note: intt_roots will be ignored if SE_INTT_OTF is defined - intt_non_lazy_inpl(parms, intt_roots, inv_n, inv_n_w, vec); + intt_non_lazy_inpl(parms, intt_roots, inv_n, last_inv_sn, vec); #endif } + +#if defined(SE_INTT_OTF) || defined(SE_INTT_ONE_SHOT) +/** +Helper function to return root for certain modulus prime values if SE_INTT_OTF or SE_INTT_ONE_SHOT +is used. Implemented as a table lookup. + +@param[in] n Transform size (i.e. polynomial ring degree) +@param[in] q Modulus value +*/ +ZZ get_intt_root(size_t n, ZZ q) +{ + /** + For custom primes: + Add cases for custom primes at the indicated locations. + Note that this is only required if: + 1) paramaters (specifically, primes) desired are of custom (i.e., non-default) type + 2) INTT type is of compute ("on-the-fly" or "one-shot") type + 3) on-device testing is desired + + If any of the above conditions are false, no modifications to this function are needed. + Note: wolframalpha is a good tool to use to calculate constants. + */ + + ZZ inv_root; // i.e. w^(-1) = first power of INTT root + switch (n) + { + case 1024: + // -- Add cases for custom primes here + se_assert(q == 134012929); + inv_root = 131483387; + break; + case 2048: + // -- Add cases for custom primes here + se_assert(q == 134012929); + inv_root = 83050288; + break; + case 4096: + switch (q) + { + // -- Add cases for custom primes here + // -- 27 bit + case 134012929: inv_root = 92230317; break; + case 134111233: inv_root = 106809024; break; + case 134176769: inv_root = 113035413; break; + // -- 30 bit + case 1053818881: inv_root = 18959119; break; + case 1054015489: inv_root = 450508648; break; + case 1054212097: inv_root = 82547477; break; + default: { + printf("Error! Need first power of root for intt, n = 4K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + case 8192: + switch (q) + { + // -- Add cases for custom primes here + case 1053818881: inv_root = 303911105; break; + case 1054015489: inv_root = 552874754; break; + case 1054212097: inv_root = 85757512; break; + case 1055260673: inv_root = 566657253; break; + case 1056178177: inv_root = 18375283; break; + case 1056440321: inv_root = 939847932; break; + default: { + printf("Error! Need first power of root for intt, n = 8K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + case 16384: + switch (q) + { + // -- Add cases for custom primes here + case 1053818881: inv_root = 232664460; break; + case 1054015489: inv_root = 752571217; break; + case 1054212097: inv_root = 797764264; break; + case 1055260673: inv_root = 572000669; break; + case 1056178177: inv_root = 174597629; break; + case 1056440321: inv_root = 252935303; break; + case 1058209793: inv_root = 440137408; break; + case 1060175873: inv_root = 309560567; break; + case 1060700161: inv_root = 351709685; break; + case 1060765697: inv_root = 759856646; break; + case 1061093377: inv_root = 729599158; break; + case 1062469633: inv_root = 677791800; break; + case 1062535169: inv_root = 943827998; break; + default: { + printf("Error! Need first power of root for intt, n = 16K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + default: { + printf("Error! Need first power of root for intt\n"); + print_zz("Modulus value", q); + exit(1); + } + } + return inv_root; +} +#endif + +#endif // if testing is not disabled diff --git a/device/lib/intt.h b/device/lib/intt.h index 184c1e6..65dbd1d 100644 --- a/device/lib/intt.h +++ b/device/lib/intt.h @@ -12,12 +12,13 @@ Inverse Number Theoretic Transform. #include "defines.h" #include "parameters.h" +#ifndef SE_DISABLE_TESTING_CAPABILITY // INTT is only needed for on-device testing /* For all "Harvey" butterfly INTTs: -The input is a polynomial of degree n in R_q, where n is assumed to be a power -of 2 and q is a prime such that q = 1 (mod 2n). The output is a vector A such -that the following hold: A[j] = a(psi**(2*bit_reverse(j) + 1)), 0 <= j < n. For -more details, see the SEAL-Embedded paper and Michael Naehrig and Patrick Longa's paper. +The input is a polynomial of degree n in R_q, where n is assumed to be a power of 2 and q is a prime +such that q = 1 (mod 2n). The output is a vector A such that the following hold: +A[j] = a(psi**(2*bit_reverse(j) + 1)), 0 <= j < n. For more details, see the SEAL-Embedded paper and +Michael Naehrig and Patrick Longa's paper. */ /** @@ -40,8 +41,8 @@ void intt_roots_initialize(const Parms *parms, ZZ *intt_roots); Performs a negacyclic inverse NTT using the Harvey butterfly. @param[in] parms Parameters set by ckks_setup -@param[in] intt_roots INTT roots set by ntt_roots_initialize. Ignored if SE_NTT_OTF -is defined. +@param[in] intt_roots Roots set by intt_roots_initialize. Ignored if SE_INTT_OTF is defined. @param[in, out] vec Input/output polynomial of n ZZ elements */ void intt_inpl(const Parms *parms, const ZZ *intt_roots, ZZ *vec); +#endif diff --git a/device/lib/modulo.h b/device/lib/modulo.h index 408c8f9..95f19cb 100644 --- a/device/lib/modulo.h +++ b/device/lib/modulo.h @@ -40,17 +40,16 @@ Req: modulus must be at most 31 bits @param[in] modulus Modulus object with 31-bit value @returns Result of input mod q in a uint32_t */ -static inline uint32_t barrett_reduce_32input_32modulus(uint32_t input, - const Modulus *modulus) +static inline uint32_t barrett_reduce_32input_32modulus(uint32_t input, const Modulus *modulus) { // -- (x = input) // -- (q = modulus->value) // -- We want to calculate [x]q (i.e. x mod q). - // we will use barrett reduction to calculate r - // where r = [x]q or [x]2q - // the formula for this is: - // r = [ [x]b - [floor(x*u/t) * q]b ]b - // where u = floor(t/q), b = 2^64, t = 2^64 + // we will use barrett reduction to calculate r + // where r = [x]q or [x]2q + // the formula for this is: + // r = [ [x]b - [floor(x*u/t) * q]b ]b + // where u = floor(t/q), b = 2^64, t = 2^64 uint32_t tmp; // -- We have modulus->const_ratio = floor(2^128/q). @@ -87,13 +86,13 @@ static inline uint32_t barrett_reduce_64input_32modulus(const uint32_t *input, { const uint32_t *const_ratio = modulus->const_ratio; - // The following code is essentially multiplying input (128-bit) - // with modulus->const_ratio (63-bit--> ~64 bits), but is optimized - // to only calculate the highest word of the 192-bit result since - // it is equivalent to the reduced result. - // (hw = high_word, lw = low_word) + // -- The following code is essentially multiplying input (128-bit) + // with modulus->const_ratio (63-bit--> ~64 bits), but is optimized + // to only calculate the highest word of the 192-bit result since + // it is equivalent to the reduced result. + // (hw = high_word, lw = low_word) - // Round 1 + // -- Round 1 uint32_t right_hw = mul_uint32_high(input[0], const_ratio[0]); uint32_t middle_temp[2]; @@ -102,16 +101,16 @@ static inline uint32_t barrett_reduce_64input_32modulus(const uint32_t *input, uint32_t middle_lw_carry = add_uint32(right_hw, middle_temp[0], &middle_lw); uint32_t middle_hw = middle_temp[1] + middle_lw_carry; - // Round 2 + // -- Round 2 uint32_t middle2_temp[2]; mul_uint32_wide(input[1], const_ratio[0], middle2_temp); uint32_t middle2_lw; uint32_t middle2_lw_carry = add_uint32(middle_lw, middle2_temp[0], &middle2_lw); - uint32_t middle2_hw = middle2_temp[1] + middle2_lw_carry; // We don't need the carry + uint32_t middle2_hw = middle2_temp[1] + middle2_lw_carry; // We don't need the carry uint32_t tmp = input[1] * const_ratio[1] + middle_hw + middle2_hw; - // Barrett subtraction + // -- Barrett subtraction tmp = input[0] - tmp * modulus->value; return shift_result(tmp, modulus->value); } @@ -158,10 +157,9 @@ static inline uint8_t mod3_uint8input(uint8_t r) // return (c & r) ^ ((~c) & t); r = (uint8_t)((r >> 4) + (r & 0xf)); // r' = r mod 3, since 2^4=1 r = (uint8_t)((r >> 2) + (r & 0x3)); // r'= r mod 3, since 2^2=1 - r = (uint8_t)((r >> 2) + - (r & 0x3)); // r'= r mod 3, since 2^2=1, reducing r to [0, 3] - int8_t t = (int8_t)(r - 3); // 0,1,2 --> 0xF?, 3 --> 0x00 - int8_t c = t >> 7; // 0xF? --> 0x01, 0x00 --> 0x00 + r = (uint8_t)((r >> 2) + (r & 0x3)); // r'= r mod 3, since 2^2=1, reducing r to [0, 3] + int8_t t = (int8_t)(r - 3); // 0,1,2 --> 0xF?, 3 --> 0x00 + int8_t c = t >> 7; // 0xF? --> 0x01, 0x00 --> 0x00 return (uint8_t)((c & r) ^ ((~c) & t)); } @@ -178,7 +176,7 @@ static inline uint8_t mod3_zzinput(uint32_t input) r = (r >> 8) + (r & 0xff); // r' = r mod 3, since 2^8=1 r = (r >> 4) + (r & 0xf); // r' = r mod 3, since 2^4=1 r = (r >> 2) + (r & 0x3); // r'= r mod 3, since 2^2=1 - r = (r >> 2) + (r & 0x3); // r'= r mod 3, since 2^2=1, reducing r to [0, 3] + r = (r >> 2) + (r & 0x3); // r'= r mod 3, since 2^2=1, reducing r to [0, 3] // int8_t t = (uint8_t)r - 3; // 0,1,2 --> 0xF?, 3 --> 0x00 // int8_t c = t >> 7; // 0xF? --> 0x01, 0x00 --> 0x00 // return (c & (uint8_t)r) ^ ((~c) & t); diff --git a/device/lib/modulus.c b/device/lib/modulus.c index 7257a1f..56545a5 100644 --- a/device/lib/modulus.c +++ b/device/lib/modulus.c @@ -24,25 +24,27 @@ bool set_modulus(const uint32_t q, Modulus *mod) { switch (q) { - // -- 30-bit ckks primes - case 1073479681: set_modulus_custom(q, 0x4, 0x004003f0, mod); return 1; - case 1073184769: set_modulus_custom(q, 0x4, 0x00881202, mod); return 1; - case 1073053697: set_modulus_custom(q, 0x4, 0x00a81b84, mod); return 1; - case 1072857089: set_modulus_custom(q, 0x4, 0x00d82d89, mod); return 1; - case 1072496641: set_modulus_custom(q, 0x4, 0x01305a4a, mod); return 1; - case 1071513601: set_modulus_custom(q, 0x4, 0x02212189, mod); return 1; - case 1071415297: set_modulus_custom(q, 0x4, 0x02393baf, mod); return 1; - - // -- Other values for testing - case 2: set_modulus_custom(q, 1UL << 31, 0, mod); return 1; - case 3: set_modulus_custom(q, 0x55555555, 0x55555555, mod); return 1; - case 10: set_modulus_custom(q, 0x19999999, 0x99999999, mod); return 1; - case 0xFFFF: set_modulus_custom(q, 0x10001, 0x00010001, mod); return 1; - case 0x10000: set_modulus_custom(q, 0x10000, 0x00000000, mod); return 1; - case 1559578058: set_modulus_custom(q, 0x2, 0xc1017e44, mod); return 1; - case 2147483647: - set_modulus_custom(q, 0x2, 0x4, mod); // Max Q - return 1; + // -- Add cases for custom primes here + + // -- 27 bit primes + case 134176769: set_modulus_custom(q, 0x20, 0x2802e03, mod); return 1; + case 134111233: set_modulus_custom(q, 0x20, 0x6814e43, mod); return 1; + case 134012929: set_modulus_custom(q, 0x20, 0xc84dfe5, mod); return 1; + + // -- 30-bit primes + case 1062535169: set_modulus_custom(q, 0x4, 0xaccdb49, mod); return 1; + case 1062469633: set_modulus_custom(q, 0x4, 0xadd3267, mod); return 1; + case 1061093377: set_modulus_custom(q, 0x4, 0xc34cf30, mod); return 1; + case 1060765697: set_modulus_custom(q, 0x4, 0xc86c0d4, mod); return 1; + case 1060700161: set_modulus_custom(q, 0x4, 0xc9725e9, mod); return 1; + case 1060175873: set_modulus_custom(q, 0x4, 0xd1a6142, mod); return 1; + case 1058209793: set_modulus_custom(q, 0x4, 0xf07a84a, mod); return 1; + case 1056440321: set_modulus_custom(q, 0x4, 0x10c52d4a, mod); return 1; + case 1056178177: set_modulus_custom(q, 0x4, 0x11074e88, mod); return 1; + case 1055260673: set_modulus_custom(q, 0x4, 0x11ef051e, mod); return 1; + case 1054212097: set_modulus_custom(q, 0x4, 0x12f85437, mod); return 1; + case 1054015489: set_modulus_custom(q, 0x4, 0x132a2218, mod); return 1; + case 1053818881: set_modulus_custom(q, 0x4, 0x135bf4ba, mod); return 1; default: printf("Modulus const ratio values not found for "); diff --git a/device/lib/modulus.h b/device/lib/modulus.h index db79faa..995b031 100644 --- a/device/lib/modulus.h +++ b/device/lib/modulus.h @@ -13,11 +13,11 @@ #include "defines.h" /** -Struct to store a modulus. 'const_ratio' can be precomputed and used later for faster -modular reduction in some cases. +Struct to store a modulus. 'const_ratio' can be precomputed and used later for faster modular +reduction in some cases. @param value Value of the modulus (aka 'q') -@param const_ratio floor(2^128/q) +@param const_ratio floor(2^64/q) */ typedef struct Modulus { @@ -26,12 +26,12 @@ typedef struct Modulus // -- Note: SEAL const_ratio is size 3 to store the remainder, // but we don't need the remainder so we can use a size 2 array - ZZ const_ratio[2]; // floor(2^128/q) + ZZ const_ratio[2]; // floor(2^64/q) } Modulus; /** -Sets up the modulus object for a particular modulus value. Useful for setting up a modulus -if const_ratio for modulus value has not been pre-computed by set_modulus' table. +Sets up the modulus object for a particular modulus value. Useful for setting up a modulus if +const_ratio for modulus value has not been pre-computed by set_modulus' table. @param[in] q Modulus value @param[in] hw High word of const_ratio for 'q' @@ -41,9 +41,9 @@ if const_ratio for modulus value has not been pre-computed by set_modulus' table void set_modulus_custom(const ZZ q, ZZ hw, ZZ lw, Modulus *mod); /** -Sets up the modulus object for a particular modulus value. Implements const_ratio set as a -table lookup. If table does not contain const_ratio for the requested modulus value, -returns a failure. In this case, set_modulus_custom should be used instead. +Sets up the modulus object for a particular modulus value. Implements const_ratio set as a table +lookup. If table does not contain const_ratio for the requested modulus value, returns a failure. In +this case, set_modulus_custom should be used instead. @param[in] q Modulus value @param[out] mod Modulus object to set diff --git a/device/lib/network.c b/device/lib/network.c index 1c6c18f..cfc4055 100644 --- a/device/lib/network.c +++ b/device/lib/network.c @@ -8,15 +8,15 @@ #include "defines.h" #ifdef SE_ON_SPHERE_A7 - #include - #include - #include - #include - #include - #include // strerror +#include +#include +#include +#include +#include +#include // strerror - #include "network.h" - #include "util_print.h" +#include "network.h" +#include "util_print.h" bool is_network_connected(void) { @@ -37,8 +37,7 @@ bool is_network_connected(void) } // -- Use a mask to get bits of the status related to internet connection - uint32_t connected = - status & Networking_InterfaceConnectionStatus_ConnectedToInternet; + uint32_t connected = status & Networking_InterfaceConnectionStatus_ConnectedToInternet; if (!connected) { printf("Error: No internet connectivity.\n"); @@ -97,7 +96,7 @@ void send_over_network(ZZ *data, size_t num_data_bytes) if (is_curl_error(&ret, "user agent")) { goto cleanup; } struct curl_slist *headers = NULL; - headers = curl_slist_append(headers, "Content-Type: vector"); + headers = curl_slist_append(headers, "Content-Type: vector"); printf("Sending the following data over the network: \n"); print_poly("data", data, num_data_bytes / sizeof(ZZ)); diff --git a/device/lib/network.h b/device/lib/network.h index 7969e3d..294bb9c 100644 --- a/device/lib/network.h +++ b/device/lib/network.h @@ -10,11 +10,10 @@ #include "defines.h" #ifdef SE_ON_SPHERE_A7 - #include +#include // static const char *url = "http://neverssl.com"; -// static const char *url = "http://httpstat.us"; -static const char *url = "http://192.168.86.30:1234/"; // Macbook +static const char *url = "http://httpstat.us"; /** Checks that the interface is connected to the internet. @@ -24,8 +23,8 @@ Checks that the interface is connected to the internet. bool is_network_connected(void); /** -Checks if there was an error with curl. Prints a message upon error detection (does not -exit upon error detection). +Checks if there was an error with curl. Prints a message upon error detection (does not exit upon +error detection). @param[in] ret Curl-function returned value to check @param[in] name A name or message to print upon error detection @@ -34,9 +33,8 @@ exit upon error detection). bool is_curl_error(void *ret, const char *name); /** -Sends 'num_data_bytes' of bytes from location pointed to by 'data' over the network -connection using cURL. Prints a message upon error detection (does not exit upon error -detection). +Sends 'num_data_bytes' of bytes from location pointed to by 'data' over the network connection using +cURL. Prints a message upon error detection (does not exit upon error detection). @param[in] data Pointer to location of data to send over the network @param[in] num_data_bytes Number of bytes of 'data' to send diff --git a/device/lib/ntt.c b/device/lib/ntt.c index 07f93c8..fc93463 100644 --- a/device/lib/ntt.c +++ b/device/lib/ntt.c @@ -18,54 +18,7 @@ #include "util_print.h" #if defined(SE_NTT_OTF) || defined(SE_NTT_ONE_SHOT) - -/** -Helper function to return root for certain modulus prime values if SE_NTT_OTF or -SE_NTT_ONE_SHOT is used. Implemented as a table lookup. - -@param[in] n Transform size (i.e. polynomial ring degree) -@param[in] q Modulus value -*/ -ZZ get_ntt_root(size_t n, ZZ q) -{ - // TODO: Add more primes - ZZ root; - switch (n) - { - case 16: - switch (q) - { - case 1071415297: root = 161442378; break; - case 1071513601: root = 113726644; break; - case 1072496641: root = 15809412; break; - default: { - printf("Error! Need first power of root, n = 16\n"); - print_zz("Modulus value", q); - exit(1); - } - } - break; - case 4096: - switch (q) - { - case 1071415297: root = 12202; break; - case 1071513601: root = 143907; break; - case 1072496641: root = 27507; break; - default: { - printf("Error! Need first power of root for ntt, n = 4K\n"); - print_zz("Modulus value", q); - exit(1); - } - } - break; - default: { - printf("Error! Need first power of root for ntt\n"); - print_zz("Modulus value", q); - exit(1); - } - } - return root; -} +ZZ get_ntt_root(size_t n, ZZ q); // defined below #endif void ntt_roots_initialize(const Parms *parms, ZZ *ntt_roots) @@ -108,10 +61,9 @@ void ntt_roots_initialize(const Parms *parms, ZZ *ntt_roots) #ifdef SE_NTT_FAST /** -Performs a "fast" (a.k.a. "lazy") negacyclic in-place NTT using the Harvey butterfly. Only -used if "SE_NTT_FAST" is defined. See SEAL_Embedded paper for a more detailed description. -"Lazy"-ness refers to fact that we here we lazily opt to reduce values only at the very -end. +Performs a "fast" (a.k.a. "lazy") negacyclic in-place NTT using the Harvey butterfly. Only used if +"SE_NTT_FAST" is defined. See SEAL_Embedded paper for a more detailed description. "Lazy"-ness +refers to fact that we here we lazily opt to reduce values only at the very end. @param[in] parms Parameters set by ckks_setup @param[in] ntt_fast_roots NTT roots set by ntt_roots_initialize @@ -158,22 +110,20 @@ void ntt_lazy_inpl(const Parms *parms, const MUMO *ntt_fast_roots, ZZ *vec) #else /** -Performs a negacyclic in-place NTT using the Harvey butterfly. Only used if SE_NTT_FAST is -not defined. +Performs a negacyclic in-place NTT using the Harvey butterfly. Only used if SE_NTT_FAST is not +defined. If SE_NTT_REG or SE_NTT_ONE_SHOT is defined, will use regular NTT computation. -Else, (SE_NTT_OTF is defined), will use truly "on-the-fly" NTT computation. In this last -case, 'ntt_roots' may be null (and will be ignored). +Else, (SE_NTT_OTF is defined), will use truly "on-the-fly" NTT computation. In this last case, +'ntt_roots' may be null (and will be ignored). @param[in] parms Parameters set by ckks_setup -@param[in] ntt_roots NTT roots set by ntt_roots_initialize. Ignored if SE_NTT_OTF is -defined. +@param[in] ntt_roots NTT roots set by ntt_roots_initialize. Ignored if SE_NTT_OTF is defined. @param[in,out] vec Input/output polynomial of n ZZ elements */ void ntt_non_lazy_inpl(const Parms *parms, const ZZ *ntt_roots, ZZ *vec) { se_assert(parms && parms->curr_modulus && vec); - size_t n = parms->coeff_count; size_t logn = parms->logn; Modulus *mod = parms->curr_modulus; @@ -182,25 +132,25 @@ void ntt_non_lazy_inpl(const Parms *parms, const ZZ *ntt_roots, ZZ *vec) size_t h = 1; size_t tt = n / 2; - #ifdef SE_NTT_OTF +#ifdef SE_NTT_OTF SE_UNUSED(ntt_roots); ZZ root = get_ntt_root(n, mod->value); - #endif +#endif - for (int i = 0; i < logn; i++, h *= 2, tt /= 2) // rounds + for (size_t i = 0; i < logn; i++, h *= 2, tt /= 2) // rounds { for (size_t j = 0, kstart = 0; j < h; j++, kstart += 2 * tt) // groups { - #ifdef SE_NTT_OTF +#ifdef SE_NTT_OTF // printf("h+j: %zu\n", h+j); // ZZ power = bitrev(h+j, logn); // ZZ s = exponentiate_uint_mod(root, power, mod); ZZ power = h + j; ZZ s = exponentiate_uint_mod_bitrev(root, power, logn, mod); - #else +#else se_assert(ntt_roots); ZZ s = ntt_roots[h + j]; - #endif +#endif // -- The Harvey butterfly. Assume val1, val2 in [0, 2p) // -- Return vec[k], vec[k+tt] in [0, 4p) for (size_t k = kstart; k < (kstart + tt); k++) // pairs @@ -237,3 +187,106 @@ void ntt_inpl(const Parms *parms, const ZZ *ntt_roots, ZZ *vec) ntt_non_lazy_inpl(parms, ntt_roots, vec); #endif } + +#if defined(SE_NTT_OTF) || defined(SE_NTT_ONE_SHOT) +/** +Helper function to return root for certain modulus prime values if SE_NTT_OTF or SE_NTT_ONE_SHOT is +used. Implemented as a table lookup. + +@param[in] n Transform size (i.e. polynomial ring degree) +@param[in] q Modulus value +*/ +ZZ get_ntt_root(size_t n, ZZ q) +{ + /** + For custom primes: + Add cases for custom primes at the indicated locations. + Note that this is only required if: + 1) paramaters (specifically, primes) desired are of custom (i.e., non-default) type + 2) NTT type is of compute ("on-the-fly" or "one-shot") type + + If any of the above conditions are false, no modifications to this file are needed. + Note: wolframalpha is a good tool to use to calculate constants. + */ + + ZZ root; // i.e. w = first power of NTT root + switch (n) + { + case 1024: + // -- Add cases for custom primes here + se_assert(q == 134012929); + root = 142143; + break; + case 2048: + // -- Add cases for custom primes here + se_assert(q == 134012929); + root = 85250; + break; + case 4096: + switch (q) + { + // -- Add cases for custom primes here + case 134012929: root = 7470; break; // 27 bit + case 134111233: root = 3856; break; // 27 bit + case 134176769: root = 24149; break; // 27 bit + case 1053818881: root = 503422; break; // 30 bit + case 1054015489: root = 16768; break; // 30 bit + case 1054212097: root = 7305; break; // 30 bit + + default: { + printf("Error! Need first power of root for ntt, n = 4K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + case 8192: + switch (q) + { + // -- Add cases for custom primes here + case 1053818881: root = 374229; break; + case 1054015489: root = 123363; break; + case 1054212097: root = 79941; break; + case 1055260673: root = 38869; break; + case 1056178177: root = 162146; break; + case 1056440321: root = 81884; break; + default: { + printf("Error! Need first power of root for ntt, n = 8K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + case 16384: // TODO: ADD A FLAG TO TURN THESE OFF? + switch (q) + { + // -- Add cases for custom primes here + case 1053818881: root = 13040; break; + case 1054015489: root = 507; break; + case 1054212097: root = 1595; break; + case 1055260673: root = 68507; break; + case 1056178177: root = 3073; break; + case 1056440321: root = 6854; break; + case 1058209793: root = 44467; break; + case 1060175873: root = 16117; break; + case 1060700161: root = 27607; break; + case 1060765697: root = 222391; break; + case 1061093377: root = 105471; break; + case 1062469633: root = 310222; break; + case 1062535169: root = 2005; break; + default: { + printf("Error! Need first power of root for ntt, n = 16K\n"); + print_zz("Modulus value", q); + exit(1); + } + } + break; + default: { + printf("Error! Need first power of root for ntt\n"); + print_zz("Modulus value", q); + exit(1); + } + } + return root; +} +#endif diff --git a/device/lib/ntt.h b/device/lib/ntt.h index a527a94..660e761 100644 --- a/device/lib/ntt.h +++ b/device/lib/ntt.h @@ -17,10 +17,10 @@ Number Theoretic Transform. /* For all "Harvey" butterfly NTTs: -The input is a polynomial of degree n in R_q, where n is assumed to be a power -of 2 and q is a prime such that q = 1 (mod 2n). The output is a vector A such -that the following hold: A[j] = a(psi**(2*bit_reverse(j) + 1)), 0 <= j < n. For -more details, see the SEAL-Embedded paper and Michael Naehrig and Patrick Longa's paper. +The input is a polynomial of degree n in R_q, where n is assumed to be a power of 2 and q is a prime +such that q = 1 (mod 2n). The output is a vector A such that the following hold: +A[j] = a(psi**(2*bit_reverse(j) + 1)), 0 <= j < n. For more details, see the SEAL-Embedded paper and +Michael Naehrig and Patrick Longa's paper. */ /** @@ -31,8 +31,8 @@ Else, if SE_NTT_REG is defined, will load regular roots from file. Else, if SE_NTT_ONE_SHOT is defined, will calculate NTT roots one by one. Else, (SE_NTT_OTF), will do nothing ('ntt_roots' can be null and will be ignored). -Space req: 'ntt_roots' should have space for 2n ZZ elements if SE_NTT_FAST is defined or n -ZZ elements if SE_NTT_ONE_SHOT or SE_NTT_REG is defined. +Space req: 'ntt_roots' should have space for 2n ZZ elements if SE_NTT_FAST is defined or n ZZ +elements if SE_NTT_ONE_SHOT or SE_NTT_REG is defined. @param[in] parms Parameters set by ckks_setup @param[out] ntt_roots NTT roots. Will be null if SE_NTT_OTF is defined. @@ -48,15 +48,14 @@ Else, (SE_NTT_OTF is defined), will use truly "on-the-fly" NTT computation. In t case, 'ntt_roots' may be null (and will be ignored). @param[in] parms Parameters set by ckks_setup -@param[in] ntt_roots NTT roots set by ntt_roots_initialize. Ignored if SE_NTT_OTF is -defined. +@param[in] ntt_roots NTT roots set by ntt_roots_initialize. Ignored if SE_NTT_OTF is defined. @param[in,out] vec Input/output polynomial of n ZZ elements */ void ntt_inpl(const Parms *parms, const ZZ *ntt_roots, ZZ *vec); /** -Polynomial multiplication for inputs already in NTT form. 'res' and 'a' may share the same -starting address (see: poly_mult_mod_ntt_form_inpl) +Polynomial multiplication for inputs already in NTT form. 'res' and 'a' may share the same starting +address (see: poly_mult_mod_ntt_form_inpl) @param[in] a Input polynomial 1, in NTT form, with n ZZ coefficients @param[in] b Input polynomial 2, in NTT form, with n ZZ coefficients @@ -64,8 +63,8 @@ starting address (see: poly_mult_mod_ntt_form_inpl) @param[in] mod Modulus @param[out] res Result polynomial, in NTT form, with n ZZ coefficients */ -static inline void poly_mult_mod_ntt_form(const ZZ *a, const ZZ *b, size_t n, - const Modulus *mod, ZZ *res) +static inline void poly_mult_mod_ntt_form(const ZZ *a, const ZZ *b, size_t n, const Modulus *mod, + ZZ *res) { // -- Inputs in NTT form can be multiplied component-wise poly_pointwise_mul_mod(a, b, n, mod, res); @@ -79,8 +78,7 @@ In-place polynomial multiplication for inputs already in NTT form. @param[in] n Number of coefficients in a and b @param[in] mod Modulus */ -static inline void poly_mult_mod_ntt_form_inpl(ZZ *a, const ZZ *b, size_t n, - const Modulus *mod) +static inline void poly_mult_mod_ntt_form_inpl(ZZ *a, const ZZ *b, size_t n, const Modulus *mod) { // -- Values in NTT form can be multiplied component-wise poly_pointwise_mul_mod_inpl(a, b, n, mod); diff --git a/device/lib/parameters.c b/device/lib/parameters.c index c8da905..4fe2131 100644 --- a/device/lib/parameters.c +++ b/device/lib/parameters.c @@ -27,9 +27,12 @@ void delete_parameters(Parms *parms) { #ifdef SE_USE_MALLOC se_assert(parms); - free(parms->moduli); - parms->moduli = NULL; - parms = NULL; + if (parms->moduli) + { + free(parms->moduli); + parms->moduli = 0; + } + parms = NULL; #else SE_UNUSED(parms); #endif @@ -94,7 +97,7 @@ Helper function to set the Parameters instance. */ static void set_params_base(size_t degree, size_t nprimes, Parms *parms) { - se_assert(degree >= 2 && degree <= 16384); + se_assert(degree >= 1024 && degree <= 16384); se_assert(is_power_of_2(degree)); se_assert(parms); se_assert(nprimes >= 1); @@ -118,11 +121,63 @@ static void set_params_base(size_t degree, size_t nprimes, Parms *parms) #endif } +/** +Helper function to set 27-bit primes + +@param[in, out] parms Parameters object to set +*/ +void set_parms_ckks_27bit_helper(Parms *parms) +{ + se_assert(parms); + se_assert(parms->nprimes >= 1); + se_assert(parms->coeff_count == 1024 || parms->coeff_count == 2048 || + parms->coeff_count == 4096); + switch (parms->nprimes) + { + // -- 27-bit primes equal to 1 mod 8192 + case 3: set_modulus(134176769, &(parms->moduli[2])); + case 2: set_modulus(134111233, &(parms->moduli[1])); + case 1: set_modulus(134012929, &(parms->moduli[0])); + } +} + +/** +Helper function to set 30-bit primes + +@param[in, out] parms Parameters object to set +*/ +void set_parms_ckks_30bit_helper(Parms *parms) +{ + se_assert(parms); + se_assert(parms->nprimes >= 1); + switch (parms->nprimes) + { + // -- 30-bits primes equal to 1 mod 65536? + // TODO: are these pragmas ignored when necessary? +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" + case 13: set_modulus(1062535169, &(parms->moduli[12])); + case 12: set_modulus(1062469633, &(parms->moduli[11])); + case 11: set_modulus(1061093377, &(parms->moduli[10])); + case 10: set_modulus(1060765697, &(parms->moduli[9])); + case 9: set_modulus(1060700161, &(parms->moduli[8])); + case 8: set_modulus(1060175873, &(parms->moduli[7])); + case 7: set_modulus(1058209793, &(parms->moduli[6])); + case 6: set_modulus(1056440321, &(parms->moduli[5])); + case 5: set_modulus(1056178177, &(parms->moduli[4])); + case 4: set_modulus(1055260673, &(parms->moduli[3])); + case 3: set_modulus(1054212097, &(parms->moduli[2])); + case 2: set_modulus(1054015489, &(parms->moduli[1])); + case 1: set_modulus(1053818881, &(parms->moduli[0])); +#pragma GCC diagnostic pop + } +} + void set_parms_ckks(size_t degree, size_t nprimes, Parms *parms) { se_assert(parms); + se_assert(nprimes >= 1); set_params_base(degree, nprimes, parms); - se_assert(nprimes < 8); // -- These precomputed values of q satisfy q = 1 (mod m), for m = 2*n and // n = polynomial degree = a power of 2. @@ -133,38 +188,54 @@ void set_parms_ckks(size_t degree, size_t nprimes, Parms *parms) // and we can use the same modulus q for all powers of 2 < m. Note that the // reverse is not true. That is, a prime q that satisfies q = 1 (mod m') // does not necessarily satisfy q = 1 (mod m). - switch (parms->nprimes) + switch (degree) { -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 7) - case 7: set_modulus(1073479681, &(parms->moduli[6])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 6) - case 6: set_modulus(1073184769, &(parms->moduli[5])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 5) - case 5: set_modulus(1073053697, &(parms->moduli[4])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 4) - case 4: set_modulus(1072857089, &(parms->moduli[3])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 3) - case 3: set_modulus(1072496641, &(parms->moduli[2])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 2) - case 2: set_modulus(1071513601, &(parms->moduli[1])); -#endif -#if (defined(SE_USE_MALLOC) || SE_NPRIMES >= 1) - case 1: set_modulus(1071415297, &(parms->moduli[0])); + // -- Add cases for custom primes here or use custom parms API + case 1024: + se_assert(parms->nprimes == 1); + set_parms_ckks_27bit_helper(parms); + parms->scale = pow(2, 20); + break; + case 2048: + se_assert(parms->nprimes == 1); + set_parms_ckks_27bit_helper(parms); + parms->scale = pow(2, 25); + break; +#ifdef SE_DEFAULT_4K_27BIT + case 4096: + se_assert(parms->nprimes <= 3); + set_parms_ckks_27bit_helper(parms); + parms->scale = pow(2, 20); + break; +#else + case 4096: + se_assert(parms->nprimes <= 3); + set_parms_ckks_30bit_helper(parms); + parms->scale = pow(2, 25); + break; #endif + case 8192: + se_assert(parms->nprimes <= 6); + set_parms_ckks_30bit_helper(parms); + parms->scale = pow(2, 25); + break; + case 16384: + se_assert(parms->nprimes <= 13); + set_parms_ckks_30bit_helper(parms); + parms->scale = pow(2, 25); + break; } + // parms.scale = pow(2, 40); + // parms.scale = n * n; } -void set_custom_parms_ckks(size_t degree, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, +void set_custom_parms_ckks(size_t degree, double scale, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, Parms *parms) { if (!modulus_vals || !ratios) { set_parms_ckks(degree, nprimes, parms); + parms->scale = scale; return; } @@ -174,4 +245,5 @@ void set_custom_parms_ckks(size_t degree, size_t nprimes, const ZZ *modulus_vals se_assert(modulus_vals[i]); // Should never be 0 set_modulus_custom(modulus_vals[i], ratios[i], ratios[i + 1], &(parms->moduli[i])); } + parms->scale = scale; } diff --git a/device/lib/parameters.h b/device/lib/parameters.h index 87101c5..b173ce8 100644 --- a/device/lib/parameters.h +++ b/device/lib/parameters.h @@ -35,9 +35,10 @@ Storage for encryption parameters. @param small_u Set to 1 to store the 'u' vector in small form while processing Note: SEAL-Embedded currently only works if this is 1 @param curr_param_direction Set to 1 to operate over primes in reverse order. Only -available if SE_REVERSE_CT_GEN_ENABLED is enabled. + available if SE_REVERSE_CT_GEN_ENABLED is enabled. @param skip_ntt_load Set to 1 to skip a load of the NTT roots (if applicable, -based on NTT option chosen.) Only available if SE_REVERSE_CT_GEN_ENABLED is enabled. + based on NTT option chosen.) Only available if + SE_REVERSE_CT_GEN_ENABLED is enabled. */ typedef struct { @@ -84,8 +85,7 @@ static inline size_t get_log2(size_t val) } /** -Releases the memory allocated for modulus chain is SE_USE_MALLOC is defined. -Does nothing otherwise. +Releases the memory allocated for modulus chain is SE_USE_MALLOC is defined. Does nothing otherwise. @param[in,out] parms Parameters instance */ @@ -107,12 +107,12 @@ Updates parms to next modulus in modulus chain. bool next_modulus(Parms *parms); /** -Sets up SEAL-Embedded parameters object with default (30-bit) moduli for requested degree -for CKKS. +Sets up SEAL-Embedded parameters object with default moduli for requested degree for CKKS. +Also sets the scale. -Req: degree must be a power of 2 (between 2 and 16384, inclusive) -Note: Moduli should be the same as the 30-bit default moduli chosen by the SEAL-Embedded -adapter (not including the special prime). +Req: degree must be a power of 2 (between 1024 and 16384, inclusive) +Note: Moduli should be the same as the default moduli chosen by the SEAL-Embedded adapter +(not including the special prime). @param[in] degree Polynomial ring degree @param[in] nprimes Number of prime moduli @@ -121,15 +121,16 @@ adapter (not including the special prime). void set_parms_ckks(size_t degree, size_t nprimes, Parms *parms); /** -Sets up SEAL-Embedded parameters object according to requested custom parameters for CKKS. -If either 'modulus_vals' or 'ratios' is NULL, uses default values for 30-bit primes. +Sets up SEAL-Embedded parameters object according to requested custom parameters for CKKS. If either +'modulus_vals' or 'ratios' is NULL, uses default values. @param[in] degree Polynomial ring degree +@param[in] scale Scale to use for encoding/decoding @param[in] nprimes Number of prime moduli @param[in] modulus_vals An array of nprimes type-ZZ modulus values. @param[in] ratios An array of const_ratio values for each custom modulus value -(high word, followed by low word). + (high word, followed by low word). @param[out] parms Parameters instance */ -void set_custom_parms_ckks(size_t degree, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, +void set_custom_parms_ckks(size_t degree, double scale, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, Parms *parms); diff --git a/device/lib/polymodarith.h b/device/lib/polymodarith.h index 85da672..4d8fa53 100644 --- a/device/lib/polymodarith.h +++ b/device/lib/polymodarith.h @@ -11,8 +11,8 @@ #include "uintmodarith.h" /** -Modular polynomial addition. 'p1' and 'res' may share the same starting address for -in-place computation. +Modular polynomial addition. 'p1' and 'res' may share the same starting address for in-place +computation. Space req: 'res' must have space for n ZZ values. @@ -22,8 +22,8 @@ Space req: 'res' must have space for n ZZ values. @param[in] mod Modulus @param[out] res Result polynomial */ -static inline void poly_add_mod(const ZZ *p1, const ZZ *p2, PolySizeType n, - const Modulus *mod, ZZ *res) +static inline void poly_add_mod(const ZZ *p1, const ZZ *p2, PolySizeType n, const Modulus *mod, + ZZ *res) { for (PolySizeType i = 0; i < n; i++) { res[i] = add_mod(p1[i], p2[i], mod); } } @@ -36,15 +36,14 @@ In-place modular polynomial addition. @param[in] n Number of elements (ZZ coefficients) in p1 and p2 @param[in] mod Modulus */ -static inline void poly_add_mod_inpl(ZZ *p1, const ZZ *p2, PolySizeType n, - const Modulus *mod) +static inline void poly_add_mod_inpl(ZZ *p1, const ZZ *p2, PolySizeType n, const Modulus *mod) { for (PolySizeType i = 0; i < n; i++) { add_mod_inpl(&(p1[i]), p2[i], mod); } } /** -Modular polynomial negation. 'p1' and 'res' may share the same starting address for -in-place computation. +Modular polynomial negation. 'p1' and 'res' may share the same starting address for in-place +computation. Space req: 'res' must have space for n ZZ values. @@ -71,8 +70,8 @@ static inline void poly_neg_mod_inpl(ZZ *p1, PolySizeType n, const Modulus *mod) } /** -Pointwise modular polynomial negation. 'p1' and 'res' may share the same starting address -for in-place computation (see: poly_pointwise_mul_mod_inpl). +Pointwise modular polynomial negation. 'p1' and 'res' may share the same starting address for +in-place computation (see: poly_pointwise_mul_mod_inpl). Space req: 'res' must have space for n ZZ values. diff --git a/device/lib/polymodmult.c b/device/lib/polymodmult.c index 87c1a12..17184cc 100644 --- a/device/lib/polymodmult.c +++ b/device/lib/polymodmult.c @@ -19,14 +19,14 @@ Polynomial modular multiplication. #include "util_print.h" /** -Helper funciton to multiply two ring polynomials using schoolbook multiplication, without -the final polynomial reduction. The result polynomial will be returned in the first n ZZ -elements pointed to by 'res'. +Helper funciton to multiply two ring polynomials using schoolbook multiplication, without the final +polynomial reduction. The result polynomial will be returned in the first n ZZ elements pointed to +by 'res'. Note: This function is *not* constant-time, and is mainly useful for testing. -Space req: 'res' must contain space for 2n ZZ elements, where each input polynomial -consists of n ZZ elements (each). +Space req: 'res' must contain space for 2n ZZ elements, where each input polynomial consists of n ZZ +elements (each). @param[in] a Input polynomial 1 @param[in] b Input polynomial 2 @@ -34,8 +34,8 @@ consists of n ZZ elements (each). @param[in] mod Modulus @param[out] res Sized-(2*sizeof(ZZ)) result of [a . b]_mod */ -void poly_mult_mod_sb_not_reduced(const ZZ *a, const ZZ *b, PolySizeType n, - const Modulus *mod, ZZ *res) +void poly_mult_mod_sb_not_reduced(const ZZ *a, const ZZ *b, PolySizeType n, const Modulus *mod, + ZZ *res) { memset(res, 0, 2 * n * sizeof(ZZ)); // This is necessary @@ -51,8 +51,7 @@ void poly_mult_mod_sb_not_reduced(const ZZ *a, const ZZ *b, PolySizeType n, /** Helper function to perform the final polynomial reduction. -Initial address of 'a' and initial address of 'res' may be the same (see: -poly_reduce_inpl). +Initial address of 'a' and initial address of 'res' may be the same (see: poly_reduce_inpl). Space req: 'res' must contain space for n ZZ elements (for 'a' with 2n coefficients) @@ -88,8 +87,7 @@ void poly_reduce_inpl(ZZ *a, PolySizeType n, const Modulus *mod) } } -void poly_mult_mod_sb(const ZZ *a, const ZZ *b, PolySizeType n, const Modulus *mod, - ZZ *res) +void poly_mult_mod_sb(const ZZ *a, const ZZ *b, PolySizeType n, const Modulus *mod, ZZ *res) { // -- Multiplication (not reduced) poly_mult_mod_sb_not_reduced(a, b, n, mod, res); diff --git a/device/lib/polymodmult.h b/device/lib/polymodmult.h index d33f873..f7d2f74 100644 --- a/device/lib/polymodmult.h +++ b/device/lib/polymodmult.h @@ -14,13 +14,14 @@ #include "modulo.h" /** -Multiplies two ring polynomials using schoolbook multiplication. The result polynomial -will be returned in the first n ZZ elements pointed to by 'res'. +TODO: Move to testing code? +Multiplies two ring polynomials using schoolbook multiplication. The result polynomial will be +returned in the first n ZZ elements pointed to by 'res'. Note: This function is *not* constant-time, and is mainly useful for testing. -Space req: 'res' must constain space for 2n ZZ elements, where each input polynomial -consists of n ZZ elements (each). +Space req: 'res' must constain space for 2n ZZ elements, where each input polynomial consists of n +ZZ elements (each). @param[in] a Input polynomial 1 @param[in] b Input polynomial 2 @@ -28,5 +29,4 @@ consists of n ZZ elements (each). @param[in] mod Modulus @param[out] res Result of [a . b]_mod (in the first n ZZ elements) */ -void poly_mult_mod_sb(const ZZ *a, const ZZ *b, PolySizeType n, const Modulus *mod, - ZZ *res); +void poly_mult_mod_sb(const ZZ *a, const ZZ *b, PolySizeType n, const Modulus *mod, ZZ *res); diff --git a/device/lib/rng.h b/device/lib/rng.h index 6e72081..0be3726 100644 --- a/device/lib/rng.h +++ b/device/lib/rng.h @@ -18,9 +18,9 @@ Pseudo-random number generator. #include "shake256/fips202.h" #ifdef SE_RAND_GETRANDOM - #include // getrandom +#include // getrandom #elif defined(SE_RAND_NRF5) - #include "nrf_crypto.h" +#include "nrf_crypto.h" #endif typedef struct SE_PRNG @@ -52,8 +52,7 @@ inline void prng_randomize_reset(SE_PRNG *prng, uint8_t *seed_in) ssize_t ret = getrandom((void *)(&(prng->seed[0])), SE_PRNG_SEED_BYTE_COUNT, 0); se_assert(ret == SE_PRNG_SEED_BYTE_COUNT); #elif defined(SE_RAND_NRF5) - ret_code_t ret_val = - nrf_crypto_rng_vector_generate(prng->seed, SE_PRNG_SEED_BYTE_COUNT); + ret_code_t ret_val = nrf_crypto_rng_vector_generate(prng->seed, SE_PRNG_SEED_BYTE_COUNT); if (ret_val != NRF_SUCCESS) { printf("Error: Something went wrong with nrf_crypto_rng_vector_generate()\n"); @@ -64,7 +63,7 @@ inline void prng_randomize_reset(SE_PRNG *prng, uint8_t *seed_in) #else // -- This should only be used for debugging!! memset(&(prng->seed[0]), 0, SE_PRNG_SEED_BYTE_COUNT); - prng->seed[0] = 1; + // prng->seed[0] = 1; #endif } @@ -93,8 +92,8 @@ inline void prng_fill_buffer(size_t byte_count, SE_PRNG *prng, void *buffer) /** Clears the values (both seed and counter) of a prng instance to 0. -Clears the bit that ties prng to a custom seed (so next prng_randomize_reset *will* -generate a random seed) +Clears the bit that ties prng to a custom seed (so next prng_randomize_reset *will* generate a +random seed) @param[in,out] prng PRNG instance to clear */ diff --git a/device/lib/sample.c b/device/lib/sample.c index c305fa4..f50fc3e 100644 --- a/device/lib/sample.c +++ b/device/lib/sample.c @@ -9,7 +9,7 @@ #include "defines.h" #include "parameters.h" -// #include "util_print.h" // TODO: Including this breaks things +#include "util_print.h" // TODO: Including this breaks things? #ifdef SE_RAND_NRF5 #include "nrf_crypto.h" @@ -116,12 +116,13 @@ void expand_poly_ternary(const ZZ *src, const Parms *parms, ZZ *dest) ZZ q = parms->curr_modulus->value; // -- Debugging - // print_poly("src", src, n); + // print_poly_ternary_full("src", src, n, true); // -- Fill in back first so we don't overwrite any values // -- Need to be careful with overflow and backwards loops for (size_t i = n; i > 0; i--) { + // printf("current idx: %zu\n", i-1); // debugging ZZ val = get_small_poly_idx_expanded(src, i - 1, q); dest[i - 1] = val; } @@ -129,6 +130,8 @@ void expand_poly_ternary(const ZZ *src, const Parms *parms, ZZ *dest) void expand_poly_ternary_inpl(ZZ *poly, const Parms *parms) { + // -- Debugging + // print_poly_ternary_full("src", poly, parms->coeff_count, true); expand_poly_ternary(poly, parms, poly); } @@ -200,10 +203,7 @@ void sample_small_poly_ternary_prng_k(size_t k, PolySizeType n, SE_PRNG *prng, Z for (size_t i = 0; i < i_stop; i++) { uint8_t rand_val = buffer[i]; - while (rand_val >= max_multiple) - { - prng_fill_buffer(1, prng, (void *)&rand_val); - } + while (rand_val >= max_multiple) { prng_fill_buffer(1, prng, (void *)&rand_val); } #ifdef DEBUG_EASYMOD uint8_t rand_ternary = rand_val % 3; #else @@ -269,8 +269,8 @@ static inline int8_t hamming_weight(uint8_t value) } /** -Helper function to get the cbd sample (from a cbd w/ stddev 3.24) from 6 random bytes -(non-modulo reduced) +Helper function to get the cbd sample (from a cbd w/ stddev 3.24) from 6 random bytes (non-modulo +reduced) @param[in] x Six random bytes @returns A (non-modulo-reduced) cbd sample @@ -300,7 +300,7 @@ void sample_poly_cbd_generic_prng_k(size_t k, PolySizeType n, SE_PRNG *prng, int for (size_t j = 0; j < n; j += k) { // -- Every 42 bits (6 bytes) generates a sample - uint8_t buffer[6 * k]; // Generate k samples at once + uint8_t buffer[6 * k]; // Generate k samples at once prng_fill_buffer(6 * k, prng, (void *)buffer); for (size_t i = 0; i < k; i++) { poly[i + j] = get_cbd_val(buffer + 6 * i); } @@ -332,13 +332,12 @@ void sample_add_poly_cbd_generic_inpl(int64_t *poly, PolySizeType n, SE_PRNG *pr } /* -void sample_add_poly_cbd_generic_inpl_prng_k(int64_t *poly, size_t k, PolySizeType n, - SE_PRNG *prng) +void sample_add_poly_cbd_generic_inpl_prng_k(int64_t *poly, size_t k, PolySizeType n, SE_PRNG *prng) { for (size_t j = 0; j < n; j += k) { // -- Every 42 bits (6 bytes) generates a sample - uint8_t buffer[6 * k]; // Generate k samples at once + uint8_t buffer[6 * k]; // Generate k samples at once prng_fill_buffer(6 * k, prng, (void *)buffer); for (size_t i = 0; i < k; i++) { poly[i + j] += get_cbd_val(buffer + 6 * i); } } diff --git a/device/lib/sample.h b/device/lib/sample.h index c07c30d..064ddc1 100644 --- a/device/lib/sample.h +++ b/device/lib/sample.h @@ -4,16 +4,17 @@ /** @file sample.h -Functions to sample a value or polynomial from a uniform, uniform ternary or centered -binomial distribution. +Functions to sample a value or polynomial from a uniform, uniform ternary or centered binomial +distribution. */ #pragma once -#include // uint64_t - #include "defines.h" #include "parameters.h" +// #include "util_print.h" // TODO: Including this breaks things? +#include // uint64_t + #include "rng.h" #include "stdio.h" @@ -152,8 +153,7 @@ Note: val_in should be either 0, 1, or 2 (which represent -1, 0, and 1, in some void set_small_poly_idx(size_t idx, uint8_t val_in, ZZ *poly); /** -Returns the value of the (idx)-th coefficient of 'poly', leaving 'poly' and result in -small form. +Returns the value of the (idx)-th coefficient of 'poly', leaving 'poly' and result in small form. @param[in] poly A ternary polynomial in small (compressed) form @param[in] idx Requested coefficient to read @@ -172,13 +172,13 @@ Returns the value of the (idx)-th coefficient of 'poly', leaving 'poly' in small ZZ get_small_poly_idx_expanded(const ZZ *poly, size_t idx, ZZ q); /** -Expands a small (compressed) ternary polynomial w.r.t. the current modulus and places the -result in `dest'. 'dest' and 'src' memory may share the same starting address, since -expansion occurs backwards (see: 'in place' version below). +Expands a small (compressed) ternary polynomial w.r.t. the current modulus and places the result in +`dest'. 'dest' and 'src' memory may share the same starting address, since expansion occurs +backwards (see: 'in place' version below). -Note: This function does not keep track of 'small_u' or 'small_s' flag. These flags must -be tracked/updated outside of this function if necessary. Space req: 'dest' must have -space for n ZZ elements. +Note: This function does not keep track of 'small_u' or 'small_s' flag. These flags must be +tracked/updated outside of this function if necessary. Space req: 'dest' must have space for n ZZ +elements. @param[in] src Source polynomial in small form @param[in] parms Parameters instance @@ -189,20 +189,18 @@ void expand_poly_ternary(const ZZ *src, const Parms *parms, ZZ *dest); /** Expands a small (compressed) ternary polynomial in place w.r.t. the current modulus. -Note: This function does not keep track of 'small_u' or 'small_s' flag. These flags must -be tracked/updated outside of this function if necessary. Space req: 'poly' must have -space for n ZZ elements. +Note: This function does not keep track of 'small_u' or 'small_s' flag. These flags must be +tracked/updated outside of this function if necessary. Space req: 'poly' must have space for n ZZ +elements. -@param[in,out] poly In: Source polynomial in small form; Out: result polynomial in -expanded form +@param[in,out] poly In: Source polynomial in small form; Out: result polynomial in expanded form @param[in] parms Parameters instance */ void expand_poly_ternary_inpl(ZZ *poly, const Parms *parms); /** -Reduces an (expanded) ternary polynomial w.r.t. the current modulus and places the result -in `dest'. 'dest' and 'src' memory may share the same starting address (see: 'in place' -version below). +Reduces an (expanded) ternary polynomial w.r.t. the current modulus and places the result in `dest'. +'dest' and 'src' memory may share the same starting address (see: 'in place' version below). Space req: 'dest' must have space for n ZZ elements. @@ -223,11 +221,10 @@ Space req: 'poly' must have space for n ZZ elements. void convert_poly_ternary_inpl(ZZ *poly, const Parms *parms); /** -Samples an (expanded) polynomial from the uniform ternary distribution over {-q-1, 0, 1}, -where q is the value of the current modulus. This function is mainly useful for testing, -since most of the time we would want to sample the polynomial in compressed form (see: -sample_small_poly_ternary). PRNG instance is used to generate randomness for 1 coefficient -at a time. +Samples an (expanded) polynomial from the uniform ternary distribution over {-q-1, 0, 1}, where q is +the value of the current modulus. This function is mainly useful for testing, since most of the time +we would want to sample the polynomial in compressed form (see: sample_small_poly_ternary). PRNG +instance is used to generate randomness for 1 coefficient at a time. Space req: 'poly' must have space for n ZZ values @@ -238,10 +235,10 @@ Space req: 'poly' must have space for n ZZ values void sample_poly_ternary(const Parms *parms, SE_PRNG *prng, ZZ *poly); /** -Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, -0, 1}, where q is the value of the current modulus. Note that this does *not* assume the -input buffer is in small form. It assumes that the input buffer has space for n uint8_t -values. PRNG instance is used to generate randomness for 1 coefficient at a time. +Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, 0, 1}, +where q is the value of the current modulus. Note that this does *not* assume the input buffer is in +small form. It assumes that the input buffer has space for n uint8_t values. PRNG instance is used +to generate randomness for 1 coefficient at a time. Space req: 'poly' must have space for n uin8_t values @@ -252,10 +249,10 @@ Space req: 'poly' must have space for n uin8_t values void sample_small_poly_ternary(PolySizeType n, SE_PRNG *prng, ZZ *poly); /** -Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, -0, 1}, where q is the value of the current modulus, while leaving the polynomial in -compressed form. PRNG instance is used to generate randomness for k coefficients at a time -(prior to any required rejection re-sampling, which occurs 1 coefficient at a time). +Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, 0, 1}, +where q is the value of the current modulus, while leaving the polynomial in compressed form. PRNG +instance is used to generate randomness for k coefficients at a time (prior to any required +rejection re-sampling, which occurs 1 coefficient at a time). Space req: 'poly' must have space for n uin8_t values @@ -267,10 +264,10 @@ Space req: 'poly' must have space for n uin8_t values // void sample_small_poly_ternary_prng_k(size_t k, PolySizeType n, SE_PRNG *prng, ZZ *poly); /** -Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, -0, 1}, where q is the value of the current modulus, while leaving the polynomial in -compressed form. PRNG instance is used to generate randomness for 96 coefficients at a -time (prior to any required rejection re-sampling, which occurs 1 coefficient at a time). +Samples a small (compressed) polynomial from the uniform ternary distribution over {-q-1, 0, 1}, +where q is the value of the current modulus, while leaving the polynomial in compressed form. PRNG +instance is used to generate randomness for 96 coefficients at a time (prior to any required +rejection re-sampling, which occurs 1 coefficient at a time). Space req: 'poly' must have space for n uin8_t values @@ -285,8 +282,8 @@ void sample_small_poly_ternary_prng_96(PolySizeType n, SE_PRNG *prng, ZZ *poly); // ------------------------------------------------------ /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced). PRNG instance is used to sample 1 coefficient at a time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced). +PRNG instance is used to sample 1 coefficient at a time. @param[in] n Number of coefficients to sample (e.g. degree of 'poly') @param[in,out] prng PRNG instance (will update counter) @@ -295,9 +292,8 @@ Samples coefficients of a polynomial from a centered binomial distribution void sample_poly_cbd_generic(PolySizeType n, SE_PRNG *prng, int8_t *poly); /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced). PRNG instance is used to generate randomness for k coefficients at a -time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced). +PRNG instance is used to generate randomness for k coefficients at a time. @param[in] k Number of coefficients to generate randomness for at once @param[in] n Total number of coefficients to sample (e.g. degree of 'poly') @@ -307,9 +303,8 @@ time. // void sample_poly_cbd_generic_prng_k(size_t k, PolySizeType n, SE_PRNG *prng, int8_t *poly); /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced). PRNG instance is used to generate randomness for 16 coefficients at -a time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced). +PRNG instance is used to generate randomness for 16 coefficients at a time. @param[in] n Number of coefficients to sample (e.g. degree of 'poly') @param[in,out] prng PRNG instance (will update counter) @@ -318,24 +313,22 @@ a time. void sample_poly_cbd_generic_prng_16(PolySizeType n, SE_PRNG *prng, int8_t *poly); /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced) and adds them in-place to the polynomial 'poly'. PRNG instance is -used to generate randomness for 1 coefficient at a time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced) and +adds them in-place to the polynomial 'poly'. PRNG instance is used to generate randomness for 1 +coefficient at a time. -@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error -polynomial +@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error polynomial @param[in] n Number of coefficients to sample (e.g. degree of 'poly') @param[in,out] prng PRNG instance (will update counter) */ void sample_add_poly_cbd_generic_inpl(int64_t *poly, PolySizeType n, SE_PRNG *prng); /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced) and adds them in-place to the polynomial 'poly'. PRNG instance is -used to generate randomness for k coefficients at a time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced) and +adds them in-place to the polynomial 'poly'. PRNG instance is used to generate randomness for k +coefficients at a time. -@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error -polynomial +@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error polynomial @param[in] k Number of coefficients to generate randomness for at once @param[in] n Total number of coefficients to sample (e.g. degree of 'poly') @param[in,out] prng PRNG instance (will update counter) @@ -344,12 +337,11 @@ polynomial // *prng); /** -Samples coefficients of a polynomial from a centered binomial distribution -(non-modulo-reduced) and adds them in-place to the polynomial 'poly'. PRNG instance is -used to generate randomness for 16 coefficients at a time. +Samples coefficients of a polynomial from a centered binomial distribution (non-modulo-reduced) and +adds them in-place to the polynomial 'poly'. PRNG instance is used to generate randomness for 16 +coefficients at a time. -@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error -polynomial +@param[in, out] poly In: Initial polynomial; Out: Initial polynomial + cbd error polynomial @param[in] n Number of coefficients to sample (e.g. degree of 'poly') @param[in,out] prng PRNG instance (will update counter) */ diff --git a/device/lib/sdk_config.h b/device/lib/sdk_config.h index e796212..be996f9 100644 --- a/device/lib/sdk_config.h +++ b/device/lib/sdk_config.h @@ -1,5009 +1,5009 @@ -/* -Origin: https://www.nordicsemi.com/Products/Development-software/nRF5-SDK/Download -*/ - -/** - * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form, except as embedded into a Nordic - * Semiconductor ASA integrated circuit in a product or a software update for - * such product, must reproduce the above copyright notice, this list of - * conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * 3. Neither the name of Nordic Semiconductor ASA nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * 4. This software, with or without modification, must only be used with a - * Nordic Semiconductor ASA integrated circuit. - * - * 5. Any software provided in binary form under this license must not be - * reverse engineered, decompiled, modified and/or disassembled. - * - * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef SDK_CONFIG_H -#define SDK_CONFIG_H -// <<< Use Configuration Wizard in Context Menu >>>\n -#ifdef USE_APP_CONFIG -#include "app_config.h" -#endif -// nRF_Crypto - -//========================================================== -// NRF_CRYPTO_ENABLED - nrf_crypto - Cryptography library. -//========================================================== -#ifndef NRF_CRYPTO_ENABLED -#define NRF_CRYPTO_ENABLED 1 -#endif -// NRF_CRYPTO_ALLOCATOR - Memory allocator - -// Choose memory allocator used by nrf_crypto. Default is alloca if possible -// or nrf_malloc otherwise. If 'User macros' are selected, the user has to -// create 'nrf_crypto_allocator.h' file that contains NRF_CRYPTO_ALLOC, -// NRF_CRYPTO_FREE, and NRF_CRYPTO_ALLOC_ON_STACK. <0=> Default <1=> User macros -// <2=> On stack (alloca) -// <3=> C dynamic memory (malloc) -// <4=> SDK Memory Manager (nrf_malloc) - -#ifndef NRF_CRYPTO_ALLOCATOR -#define NRF_CRYPTO_ALLOCATOR 0 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_ENABLED - Enable the ARM Cryptocell CC310 -// reduced backend. - -// The CC310 hardware-accelerated cryptography backend with reduced -// functionality and footprint (only available on nRF52840). -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED - Enable the secp224r1 -// elliptic curve support using CC310_BL. - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED 0 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED - Enable the secp256r1 -// elliptic curve support using CC310_BL. - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED - CC310_BL SHA-256 hash -// functionality. - -// CC310_BL backend implementation for hardware-accelerated SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED - -// nrf_cc310_bl buffers to RAM before running hash operation - -// Enabling this makes hashing of addresses in FLASH range possible. Size of -// buffer allocated for hashing is set by -// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED 0 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE - nrf_cc310_bl -// hash outputs digests in little endian Makes the nrf_cc310_bl hash -// functions output digests in little endian format. Only for use in nRF SDK -// DFU! - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE -#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE 4096 -#endif - -// NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED - Enable Interrupts while -// support using CC310 bl. - -// Select a library version compatible with the configuration. When -// interrupts are disable, a version named _noint must be used - -#ifndef NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_CC310_ENABLED - Enable the ARM Cryptocell CC310 -// backend. - -// The CC310 hardware-accelerated cryptography backend (only available on -// nRF52840). -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_CC310_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ENABLED 1 -#endif -// NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED - Enable the AES CBC mode using -// CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED - Enable the AES CTR mode using -// CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED - Enable the AES ECB mode using -// CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED - Enable the AES CBC_MAC -// mode using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED - Enable the AES CMAC mode -// using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED - Enable the AES CCM mode using -// CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED - Enable the AES CCM* mode -// using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED - Enable the CHACHA-POLY -// mode using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED - Enable the secp160r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED - Enable the secp160r2 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED - Enable the secp192r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED - Enable the secp224r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED - Enable the secp256r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED - Enable the secp384r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED - Enable the secp521r1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED - Enable the secp160k1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED - Enable the secp192k1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED - Enable the secp224k1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED - Enable the secp256k1 -// elliptic curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED - Enable the Curve25519 -// curve support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED - Enable the Ed25519 curve -// support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED - CC310 SHA-256 hash -// functionality. - -// CC310 backend implementation for hardware-accelerated SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED - CC310 SHA-512 hash -// functionality - -// CC310 backend implementation for SHA-512 (in software). - -#ifndef NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED - CC310 HMAC using SHA-256 - -// CC310 backend implementation for HMAC using hardware-accelerated SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED - CC310 HMAC using SHA-512 - -// CC310 backend implementation for HMAC using SHA-512 (in software). - -#ifndef NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED - Enable RNG support using CC310. - -#ifndef NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED - Enable Interrupts while -// support using CC310. - -// Select a library version compatible with the configuration. When -// interrupts are disable, a version named _noint must be used - -#ifndef NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED -#define NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_CIFRA_ENABLED - Enable the Cifra backend. -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_CIFRA_ENABLED -#define NRF_CRYPTO_BACKEND_CIFRA_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED - Enable the AES EAX mode using -// Cifra. - -#ifndef NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED -#define NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED - Enable the mbed TLS backend. -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED - Enable the AES CBC mode -// mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED - Enable the AES CTR mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED - Enable the AES CFB mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED - Enable the AES ECB mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED - Enable the AES CBC MAC -// mode using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED - Enable the AES CMAC mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED - Enable the AES CCM mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED - Enable the AES GCM mode -// using mbed TLS. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED - Enable secp192r1 -// (NIST 192-bit) curve - -// Enable this setting if you need secp192r1 (NIST 192-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED - Enable secp224r1 -// (NIST 224-bit) curve - -// Enable this setting if you need secp224r1 (NIST 224-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED - Enable secp256r1 -// (NIST 256-bit) curve - -// Enable this setting if you need secp256r1 (NIST 256-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED - Enable secp384r1 -// (NIST 384-bit) curve - -// Enable this setting if you need secp384r1 (NIST 384-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED - Enable secp521r1 -// (NIST 521-bit) curve - -// Enable this setting if you need secp521r1 (NIST 521-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED - Enable secp192k1 -// (Koblitz 192-bit) curve - -// Enable this setting if you need secp192k1 (Koblitz 192-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED - Enable secp224k1 -// (Koblitz 224-bit) curve - -// Enable this setting if you need secp224k1 (Koblitz 224-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED - Enable secp256k1 -// (Koblitz 256-bit) curve - -// Enable this setting if you need secp256k1 (Koblitz 256-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED - Enable bp256r1 -// (Brainpool 256-bit) curve - -// Enable this setting if you need bp256r1 (Brainpool 256-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED - Enable bp384r1 -// (Brainpool 384-bit) curve - -// Enable this setting if you need bp384r1 (Brainpool 384-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED - Enable bp512r1 -// (Brainpool 512-bit) curve - -// Enable this setting if you need bp512r1 (Brainpool 512-bit) support using -// MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED - Enable Curve25519 -// curve - -// Enable this setting if you need Curve25519 support using MBEDTLS - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED - Enable mbed TLS SHA-256 -// hash functionality. - -// mbed TLS backend implementation for SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED - Enable mbed TLS SHA-512 -// hash functionality. - -// mbed TLS backend implementation for SHA-512. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED - Enable mbed TLS HMAC -// using SHA-256. - -// mbed TLS backend implementation for HMAC using SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED - Enable mbed TLS HMAC -// using SHA-512. - -// mbed TLS backend implementation for HMAC using SHA-512. - -#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED - Enable the micro-ecc backend. -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED -#define NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED - Enable secp192r1 -// (NIST 192-bit) curve - -// Enable this setting if you need secp192r1 (NIST 192-bit) support using -// micro-ecc - -#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED -#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED - Enable secp224r1 -// (NIST 224-bit) curve - -// Enable this setting if you need secp224r1 (NIST 224-bit) support using -// micro-ecc - -#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED -#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED - Enable secp256r1 -// (NIST 256-bit) curve - -// Enable this setting if you need secp256r1 (NIST 256-bit) support using -// micro-ecc - -#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED - Enable secp256k1 -// (Koblitz 256-bit) curve - -// Enable this setting if you need secp256k1 (Koblitz 256-bit) support using -// micro-ecc - -#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED -#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED - Enable the nRF HW RNG backend. - -// The nRF HW backend provide access to RNG peripheral in nRF5x devices. -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED -#define NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED - Enable mbed TLS -// CTR-DRBG algorithm. - -// Enable mbed TLS CTR-DRBG standardized by NIST (NIST SP 800-90A Rev. 1). -// The nRF HW RNG is used as an entropy source for seeding. - -#ifndef NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED -#define NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_NRF_SW_ENABLED - Enable the legacy nRFx sw for crypto. - -// The nRF SW cryptography backend (only used in bootloader context). -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_NRF_SW_ENABLED -#define NRF_CRYPTO_BACKEND_NRF_SW_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED - nRF SW hash backend -// support for SHA-256 - -// The nRF SW backend provide access to nRF SDK legacy hash implementation -// of SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_OBERON_ENABLED - Enable the Oberon backend - -// The Oberon backend -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_OBERON_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED - Enable the CHACHA-POLY -// mode using Oberon. - -#ifndef NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED - Enable secp256r1 curve - -// Enable this setting if you need secp256r1 curve support using Oberon -// library - -#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED - Enable Curve25519 -// ECDH - -// Enable this setting if you need Curve25519 ECDH support using Oberon -// library - -#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED - Enable Ed25519 signature -// scheme - -// Enable this setting if you need Ed25519 support using Oberon library - -#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED - Oberon SHA-256 hash -// functionality - -// Oberon backend implementation for SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED - Oberon SHA-512 hash -// functionality - -// Oberon backend implementation for SHA-512. - -#ifndef NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED - Oberon HMAC using -// SHA-256 - -// Oberon backend implementation for HMAC using SHA-256. - -#ifndef NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED 1 -#endif - -// NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED - Oberon HMAC using -// SHA-512 - -// Oberon backend implementation for HMAC using SHA-512. - -#ifndef NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED -#define NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_BACKEND_OPTIGA_ENABLED - Enable the nrf_crypto Optiga Trust X -// backend. - -// Enables the nrf_crypto backend for Optiga Trust X devices. -//========================================================== -#ifndef NRF_CRYPTO_BACKEND_OPTIGA_ENABLED -#define NRF_CRYPTO_BACKEND_OPTIGA_ENABLED 0 -#endif -// NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED - Optiga backend support for RNG - -// The Optiga backend provide external chip RNG. - -#ifndef NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED -#define NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED 0 -#endif - -// NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED - Optiga backend support -// for ECC secp256r1 - -// The Optiga backend provide external chip ECC using secp256r1. - -#ifndef NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED -#define NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED 1 -#endif - -// - -// NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED - Big-endian byte order in raw -// Curve25519 data - -// Enable big-endian byte order in Curve25519 API, if set to 1. Use -// little-endian, if set to 0. - -#ifndef NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED -#define NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED 0 -#endif - -// - -// nrf_crypto_rng - RNG Configuration - -//========================================================== -// NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED - Use static memory buffers -// for context and temporary init buffer. - -// Always recommended when using the nRF HW RNG as the context and temporary -// buffers are small. Consider disabling if using the CC310 RNG in a RAM -// constrained application. In this case, memory must be provided to -// nrf_crypto_rng_init, or it can be allocated internally provided that -// NRF_CRYPTO_ALLOCATOR does not allocate memory on the stack. - -#ifndef NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED -#define NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED 1 -#endif - -// NRF_CRYPTO_RNG_AUTO_INIT_ENABLED - Initialize the RNG module -// automatically when nrf_crypto is initialized. - -// Automatic initialization is only supported with static or internally -// allocated context and temporary memory. - -#ifndef NRF_CRYPTO_RNG_AUTO_INIT_ENABLED -#define NRF_CRYPTO_RNG_AUTO_INIT_ENABLED 1 -#endif - -// -//========================================================== - -// -//========================================================== - -// nRF_Drivers - -//========================================================== -// NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver -//========================================================== -#ifndef NRFX_TIMER_ENABLED -#define NRFX_TIMER_ENABLED 1 -#endif -// NRFX_TIMER0_ENABLED - Enable TIMER0 instance - -#ifndef NRFX_TIMER0_ENABLED -#define NRFX_TIMER0_ENABLED 0 -#endif - -// NRFX_TIMER1_ENABLED - Enable TIMER1 instance - -#ifndef NRFX_TIMER1_ENABLED -#define NRFX_TIMER1_ENABLED 0 -#endif - -// NRFX_TIMER2_ENABLED - Enable TIMER2 instance - -#ifndef NRFX_TIMER2_ENABLED -#define NRFX_TIMER2_ENABLED 0 -#endif - -// NRFX_TIMER3_ENABLED - Enable TIMER3 instance - -#ifndef NRFX_TIMER3_ENABLED -#define NRFX_TIMER3_ENABLED 0 -#endif - -// NRFX_TIMER4_ENABLED - Enable TIMER4 instance - -#ifndef NRFX_TIMER4_ENABLED -#define NRFX_TIMER4_ENABLED 0 -#endif - -// NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY - Timer frequency if in Timer mode - -// <0=> 16 MHz -// <1=> 8 MHz -// <2=> 4 MHz -// <3=> 2 MHz -// <4=> 1 MHz -// <5=> 500 kHz -// <6=> 250 kHz -// <7=> 125 kHz -// <8=> 62.5 kHz -// <9=> 31.25 kHz - -#ifndef NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY -#define NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY 0 -#endif - -// NRFX_TIMER_DEFAULT_CONFIG_MODE - Timer mode or operation - -// <0=> Timer -// <1=> Counter - -#ifndef NRFX_TIMER_DEFAULT_CONFIG_MODE -#define NRFX_TIMER_DEFAULT_CONFIG_MODE 0 -#endif - -// NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH - Timer counter bit width - -// <0=> 16 bit -// <1=> 8 bit -// <2=> 24 bit -// <3=> 32 bit - -#ifndef NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH -#define NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH 0 -#endif - -// NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY -#define NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6 -#endif - -// NRFX_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRFX_TIMER_CONFIG_LOG_ENABLED -#define NRFX_TIMER_CONFIG_LOG_ENABLED 0 -#endif -// NRFX_TIMER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_TIMER_CONFIG_LOG_LEVEL -#define NRFX_TIMER_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_TIMER_CONFIG_INFO_COLOR -#define NRFX_TIMER_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_TIMER_CONFIG_DEBUG_COLOR -#define NRFX_TIMER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// - -// TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer -//========================================================== -#ifndef TIMER_ENABLED -#define TIMER_ENABLED 1 -#endif -// TIMER_DEFAULT_CONFIG_FREQUENCY - Timer frequency if in Timer mode - -// <0=> 16 MHz -// <1=> 8 MHz -// <2=> 4 MHz -// <3=> 2 MHz -// <4=> 1 MHz -// <5=> 500 kHz -// <6=> 250 kHz -// <7=> 125 kHz -// <8=> 62.5 kHz -// <9=> 31.25 kHz - -#ifndef TIMER_DEFAULT_CONFIG_FREQUENCY -#define TIMER_DEFAULT_CONFIG_FREQUENCY 0 -#endif - -// TIMER_DEFAULT_CONFIG_MODE - Timer mode or operation - -// <0=> Timer -// <1=> Counter - -#ifndef TIMER_DEFAULT_CONFIG_MODE -#define TIMER_DEFAULT_CONFIG_MODE 0 -#endif - -// TIMER_DEFAULT_CONFIG_BIT_WIDTH - Timer counter bit width - -// <0=> 16 bit -// <1=> 8 bit -// <2=> 24 bit -// <3=> 32 bit - -#ifndef TIMER_DEFAULT_CONFIG_BIT_WIDTH -#define TIMER_DEFAULT_CONFIG_BIT_WIDTH 3 -#endif - -// TIMER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - -// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef TIMER_DEFAULT_CONFIG_IRQ_PRIORITY -#define TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6 -#endif - -// TIMER0_ENABLED - Enable TIMER0 instance - -#ifndef TIMER0_ENABLED -#define TIMER0_ENABLED 1 -#endif - -// TIMER1_ENABLED - Enable TIMER1 instance - -#ifndef TIMER1_ENABLED -#define TIMER1_ENABLED 0 -#endif - -// TIMER2_ENABLED - Enable TIMER2 instance - -#ifndef TIMER2_ENABLED -#define TIMER2_ENABLED 0 -#endif - -// TIMER3_ENABLED - Enable TIMER3 instance - -#ifndef TIMER3_ENABLED -#define TIMER3_ENABLED 0 -#endif - -// TIMER4_ENABLED - Enable TIMER4 instance - -#ifndef TIMER4_ENABLED -#define TIMER4_ENABLED 0 -#endif - -//========================================================== -// NRFX_PRS_ENABLED - nrfx_prs - Peripheral Resource Sharing module -//========================================================== -#ifndef NRFX_PRS_ENABLED -#define NRFX_PRS_ENABLED 1 -#endif -// NRFX_PRS_BOX_0_ENABLED - Enables box 0 in the module. - -#ifndef NRFX_PRS_BOX_0_ENABLED -#define NRFX_PRS_BOX_0_ENABLED 0 -#endif - -// NRFX_PRS_BOX_1_ENABLED - Enables box 1 in the module. - -#ifndef NRFX_PRS_BOX_1_ENABLED -#define NRFX_PRS_BOX_1_ENABLED 0 -#endif - -// NRFX_PRS_BOX_2_ENABLED - Enables box 2 in the module. - -#ifndef NRFX_PRS_BOX_2_ENABLED -#define NRFX_PRS_BOX_2_ENABLED 0 -#endif - -// NRFX_PRS_BOX_3_ENABLED - Enables box 3 in the module. - -#ifndef NRFX_PRS_BOX_3_ENABLED -#define NRFX_PRS_BOX_3_ENABLED 0 -#endif - -// NRFX_PRS_BOX_4_ENABLED - Enables box 4 in the module. - -#ifndef NRFX_PRS_BOX_4_ENABLED -#define NRFX_PRS_BOX_4_ENABLED 1 -#endif - -// NRFX_PRS_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRFX_PRS_CONFIG_LOG_ENABLED -#define NRFX_PRS_CONFIG_LOG_ENABLED 1 -#endif -// NRFX_PRS_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_PRS_CONFIG_LOG_LEVEL -#define NRFX_PRS_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_PRS_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_PRS_CONFIG_INFO_COLOR -#define NRFX_PRS_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_PRS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_PRS_CONFIG_DEBUG_COLOR -#define NRFX_PRS_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// - -// NRFX_RNG_ENABLED - nrfx_rng - RNG peripheral driver -//========================================================== -#ifndef NRFX_RNG_ENABLED -#define NRFX_RNG_ENABLED 1 -#endif -// NRFX_RNG_CONFIG_ERROR_CORRECTION - Error correction - -#ifndef NRFX_RNG_CONFIG_ERROR_CORRECTION -#define NRFX_RNG_CONFIG_ERROR_CORRECTION 1 -#endif - -// NRFX_RNG_CONFIG_IRQ_PRIORITY - Interrupt priority - -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef NRFX_RNG_CONFIG_IRQ_PRIORITY -#define NRFX_RNG_CONFIG_IRQ_PRIORITY 6 -#endif - -// NRFX_RNG_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRFX_RNG_CONFIG_LOG_ENABLED -#define NRFX_RNG_CONFIG_LOG_ENABLED 0 -#endif -// NRFX_RNG_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_RNG_CONFIG_LOG_LEVEL -#define NRFX_RNG_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_RNG_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_RNG_CONFIG_INFO_COLOR -#define NRFX_RNG_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_RNG_CONFIG_DEBUG_COLOR -#define NRFX_RNG_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// - -// NRFX_UARTE_ENABLED - nrfx_uarte - UARTE peripheral driver -//========================================================== -#ifndef NRFX_UARTE_ENABLED -#define NRFX_UARTE_ENABLED 1 -#endif -// NRFX_UARTE0_ENABLED - Enable UARTE0 instance -#ifndef NRFX_UARTE0_ENABLED -#define NRFX_UARTE0_ENABLED 0 -#endif - -// NRFX_UARTE1_ENABLED - Enable UARTE1 instance -#ifndef NRFX_UARTE1_ENABLED -#define NRFX_UARTE1_ENABLED 0 -#endif - -// NRFX_UARTE_DEFAULT_CONFIG_HWFC - Hardware Flow Control - -// <0=> Disabled -// <1=> Enabled - -#ifndef NRFX_UARTE_DEFAULT_CONFIG_HWFC -#define NRFX_UARTE_DEFAULT_CONFIG_HWFC 0 -#endif - -// NRFX_UARTE_DEFAULT_CONFIG_PARITY - Parity - -// <0=> Excluded -// <14=> Included - -#ifndef NRFX_UARTE_DEFAULT_CONFIG_PARITY -#define NRFX_UARTE_DEFAULT_CONFIG_PARITY 0 -#endif - -// NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE - Default Baudrate - -// <323584=> 1200 baud -// <643072=> 2400 baud -// <1290240=> 4800 baud -// <2576384=> 9600 baud -// <3862528=> 14400 baud -// <5152768=> 19200 baud -// <7716864=> 28800 baud -// <8388608=> 31250 baud -// <10289152=> 38400 baud -// <15007744=> 56000 baud -// <15400960=> 57600 baud -// <20615168=> 76800 baud -// <30801920=> 115200 baud -// <61865984=> 230400 baud -// <67108864=> 250000 baud -// <121634816=> 460800 baud -// <251658240=> 921600 baud -// <268435456=> 1000000 baud - -#ifndef NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE -#define NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE 30801920 -#endif - -// NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY -#define NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY 6 -#endif - -// NRFX_UARTE_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRFX_UARTE_CONFIG_LOG_ENABLED -#define NRFX_UARTE_CONFIG_LOG_ENABLED 0 -#endif -// NRFX_UARTE_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_UARTE_CONFIG_LOG_LEVEL -#define NRFX_UARTE_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_UARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_UARTE_CONFIG_INFO_COLOR -#define NRFX_UARTE_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_UARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_UARTE_CONFIG_DEBUG_COLOR -#define NRFX_UARTE_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// - -// NRFX_UART_ENABLED - nrfx_uart - UART peripheral driver -//========================================================== -#ifndef NRFX_UART_ENABLED -#define NRFX_UART_ENABLED 1 -#endif -// NRFX_UART0_ENABLED - Enable UART0 instance -#ifndef NRFX_UART0_ENABLED -#define NRFX_UART0_ENABLED 0 -#endif - -// NRFX_UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control - -// <0=> Disabled -// <1=> Enabled - -#ifndef NRFX_UART_DEFAULT_CONFIG_HWFC -#define NRFX_UART_DEFAULT_CONFIG_HWFC 0 -#endif - -// NRFX_UART_DEFAULT_CONFIG_PARITY - Parity - -// <0=> Excluded -// <14=> Included - -#ifndef NRFX_UART_DEFAULT_CONFIG_PARITY -#define NRFX_UART_DEFAULT_CONFIG_PARITY 0 -#endif - -// NRFX_UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate - -// <323584=> 1200 baud -// <643072=> 2400 baud -// <1290240=> 4800 baud -// <2576384=> 9600 baud -// <3866624=> 14400 baud -// <5152768=> 19200 baud -// <7729152=> 28800 baud -// <8388608=> 31250 baud -// <10309632=> 38400 baud -// <15007744=> 56000 baud -// <15462400=> 57600 baud -// <20615168=> 76800 baud -// <30924800=> 115200 baud -// <61845504=> 230400 baud -// <67108864=> 250000 baud -// <123695104=> 460800 baud -// <247386112=> 921600 baud -// <268435456=> 1000000 baud - -#ifndef NRFX_UART_DEFAULT_CONFIG_BAUDRATE -#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE 30924800 -#endif - -// NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY -#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 6 -#endif - -// NRFX_UART_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRFX_UART_CONFIG_LOG_ENABLED -#define NRFX_UART_CONFIG_LOG_ENABLED 0 -#endif -// NRFX_UART_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_UART_CONFIG_LOG_LEVEL -#define NRFX_UART_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_UART_CONFIG_INFO_COLOR -#define NRFX_UART_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_UART_CONFIG_DEBUG_COLOR -#define NRFX_UART_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// - -// RNG_ENABLED - nrf_drv_rng - RNG peripheral driver - legacy layer -//========================================================== -#ifndef RNG_ENABLED -#define RNG_ENABLED 1 -#endif -// RNG_CONFIG_ERROR_CORRECTION - Error correction - -#ifndef RNG_CONFIG_ERROR_CORRECTION -#define RNG_CONFIG_ERROR_CORRECTION 1 -#endif - -// RNG_CONFIG_POOL_SIZE - Pool size -#ifndef RNG_CONFIG_POOL_SIZE -#define RNG_CONFIG_POOL_SIZE 64 -#endif - -// RNG_CONFIG_IRQ_PRIORITY - Interrupt priority - -// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef RNG_CONFIG_IRQ_PRIORITY -#define RNG_CONFIG_IRQ_PRIORITY 6 -#endif - -// - -// UART_ENABLED - nrf_drv_uart - UART/UARTE peripheral driver - legacy layer -//========================================================== -#ifndef UART_ENABLED -#define UART_ENABLED 1 -#endif -// UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control - -// <0=> Disabled -// <1=> Enabled - -#ifndef UART_DEFAULT_CONFIG_HWFC -#define UART_DEFAULT_CONFIG_HWFC 0 -#endif - -// UART_DEFAULT_CONFIG_PARITY - Parity - -// <0=> Excluded -// <14=> Included - -#ifndef UART_DEFAULT_CONFIG_PARITY -#define UART_DEFAULT_CONFIG_PARITY 0 -#endif - -// UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate - -// <323584=> 1200 baud -// <643072=> 2400 baud -// <1290240=> 4800 baud -// <2576384=> 9600 baud -// <3862528=> 14400 baud -// <5152768=> 19200 baud -// <7716864=> 28800 baud -// <10289152=> 38400 baud -// <15400960=> 57600 baud -// <20615168=> 76800 baud -// <30801920=> 115200 baud -// <61865984=> 230400 baud -// <67108864=> 250000 baud -// <121634816=> 460800 baud -// <251658240=> 921600 baud -// <268435456=> 1000000 baud - -#ifndef UART_DEFAULT_CONFIG_BAUDRATE -#define UART_DEFAULT_CONFIG_BAUDRATE 30801920 -#endif - -// UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority - -// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 - -#ifndef UART_DEFAULT_CONFIG_IRQ_PRIORITY -#define UART_DEFAULT_CONFIG_IRQ_PRIORITY 6 -#endif - -// UART_EASY_DMA_SUPPORT - Driver supporting EasyDMA - -#ifndef UART_EASY_DMA_SUPPORT -#define UART_EASY_DMA_SUPPORT 1 -#endif - -// UART_LEGACY_SUPPORT - Driver supporting Legacy mode - -#ifndef UART_LEGACY_SUPPORT -#define UART_LEGACY_SUPPORT 1 -#endif - -// UART0_ENABLED - Enable UART0 instance -//========================================================== -#ifndef UART0_ENABLED -#define UART0_ENABLED 1 -#endif -// UART0_CONFIG_USE_EASY_DMA - Default setting for using EasyDMA - -#ifndef UART0_CONFIG_USE_EASY_DMA -#define UART0_CONFIG_USE_EASY_DMA 1 -#endif - -// - -// UART1_ENABLED - Enable UART1 instance -//========================================================== -#ifndef UART1_ENABLED -#define UART1_ENABLED 0 -#endif -// - -// - -// -//========================================================== - -// nRF_Libraries - -//========================================================== -// APP_FIFO_ENABLED - app_fifo - Software FIFO implementation - -#ifndef APP_FIFO_ENABLED -#define APP_FIFO_ENABLED 1 -#endif - -// APP_UART_ENABLED - app_uart - UART driver -//========================================================== -#ifndef APP_UART_ENABLED -#define APP_UART_ENABLED 1 -#endif -// APP_UART_DRIVER_INSTANCE - UART instance used - -// <0=> 0 - -#ifndef APP_UART_DRIVER_INSTANCE -#define APP_UART_DRIVER_INSTANCE 0 -#endif - -// - -//========================================================== -// MEM_MANAGER_ENABLED - mem_manager - Dynamic memory allocator -//========================================================== -#ifndef MEM_MANAGER_ENABLED -#define MEM_MANAGER_ENABLED 1 -#endif -// MEMORY_MANAGER_SMALL_BLOCK_COUNT - Size of each memory blocks identified -// as 'small' block. <0-255> - -#ifndef MEMORY_MANAGER_SMALL_BLOCK_COUNT -#define MEMORY_MANAGER_SMALL_BLOCK_COUNT 1 -#endif - -// MEMORY_MANAGER_SMALL_BLOCK_SIZE - Size of each memory blocks identified -// as 'small' block. Size of each memory blocks identified as 'small' -// block. Memory block are recommended to be word-sized. - -#ifndef MEMORY_MANAGER_SMALL_BLOCK_SIZE -#define MEMORY_MANAGER_SMALL_BLOCK_SIZE 32 -#endif - -// MEMORY_MANAGER_MEDIUM_BLOCK_COUNT - Size of each memory blocks identified -// as 'medium' block. <0-255> - -#ifndef MEMORY_MANAGER_MEDIUM_BLOCK_COUNT -#define MEMORY_MANAGER_MEDIUM_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_MEDIUM_BLOCK_SIZE - Size of each memory blocks identified -// as 'medium' block. Size of each memory blocks identified as 'medium' -// block. Memory block are recommended to be word-sized. - -#ifndef MEMORY_MANAGER_MEDIUM_BLOCK_SIZE -#define MEMORY_MANAGER_MEDIUM_BLOCK_SIZE 256 -#endif - -// MEMORY_MANAGER_LARGE_BLOCK_COUNT - Size of each memory blocks identified -// as 'large' block. <0-255> - -#ifndef MEMORY_MANAGER_LARGE_BLOCK_COUNT -#define MEMORY_MANAGER_LARGE_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_LARGE_BLOCK_SIZE - Size of each memory blocks identified -// as 'large' block. Size of each memory blocks identified as 'large' -// block. Memory block are recommended to be word-sized. - -#ifndef MEMORY_MANAGER_LARGE_BLOCK_SIZE -#define MEMORY_MANAGER_LARGE_BLOCK_SIZE 256 -#endif - -// MEMORY_MANAGER_XLARGE_BLOCK_COUNT - Size of each memory blocks identified -// as 'extra large' block. <0-255> - -#ifndef MEMORY_MANAGER_XLARGE_BLOCK_COUNT -#define MEMORY_MANAGER_XLARGE_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_XLARGE_BLOCK_SIZE - Size of each memory blocks identified -// as 'extra large' block. Size of each memory blocks identified as 'extra -// large' block. Memory block are recommended to be word-sized. - -#ifndef MEMORY_MANAGER_XLARGE_BLOCK_SIZE -#define MEMORY_MANAGER_XLARGE_BLOCK_SIZE 1320 -#endif - -// MEMORY_MANAGER_XXLARGE_BLOCK_COUNT - Size of each memory blocks -// identified as 'extra extra large' block. <0-255> - -#ifndef MEMORY_MANAGER_XXLARGE_BLOCK_COUNT -#define MEMORY_MANAGER_XXLARGE_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_XXLARGE_BLOCK_SIZE - Size of each memory blocks -// identified as 'extra extra large' block. Size of each memory blocks -// identified as 'extra extra large' block. Memory block are recommended to be -// word-sized. - -#ifndef MEMORY_MANAGER_XXLARGE_BLOCK_SIZE -#define MEMORY_MANAGER_XXLARGE_BLOCK_SIZE 3444 -#endif - -// MEMORY_MANAGER_XSMALL_BLOCK_COUNT - Size of each memory blocks identified -// as 'extra small' block. <0-255> - -#ifndef MEMORY_MANAGER_XSMALL_BLOCK_COUNT -#define MEMORY_MANAGER_XSMALL_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_XSMALL_BLOCK_SIZE - Size of each memory blocks identified -// as 'extra small' block. Size of each memory blocks identified as 'extra -// large' block. Memory block are recommended to be word-sized. - -#ifndef MEMORY_MANAGER_XSMALL_BLOCK_SIZE -#define MEMORY_MANAGER_XSMALL_BLOCK_SIZE 64 -#endif - -// MEMORY_MANAGER_XXSMALL_BLOCK_COUNT - Size of each memory blocks -// identified as 'extra extra small' block. <0-255> - -#ifndef MEMORY_MANAGER_XXSMALL_BLOCK_COUNT -#define MEMORY_MANAGER_XXSMALL_BLOCK_COUNT 0 -#endif - -// MEMORY_MANAGER_XXSMALL_BLOCK_SIZE - Size of each memory blocks -// identified as 'extra extra small' block. Size of each memory blocks -// identified as 'extra extra small' block. Memory block are recommended to be -// word-sized. - -#ifndef MEMORY_MANAGER_XXSMALL_BLOCK_SIZE -#define MEMORY_MANAGER_XXSMALL_BLOCK_SIZE 32 -#endif - -// MEM_MANAGER_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef MEM_MANAGER_CONFIG_LOG_ENABLED -#define MEM_MANAGER_CONFIG_LOG_ENABLED 0 -#endif -// MEM_MANAGER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef MEM_MANAGER_CONFIG_LOG_LEVEL -#define MEM_MANAGER_CONFIG_LOG_LEVEL 3 -#endif - -// MEM_MANAGER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef MEM_MANAGER_CONFIG_INFO_COLOR -#define MEM_MANAGER_CONFIG_INFO_COLOR 0 -#endif - -// MEM_MANAGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef MEM_MANAGER_CONFIG_DEBUG_COLOR -#define MEM_MANAGER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// MEM_MANAGER_DISABLE_API_PARAM_CHECK - Disable API parameter checks in -// the module. - -#ifndef MEM_MANAGER_DISABLE_API_PARAM_CHECK -#define MEM_MANAGER_DISABLE_API_PARAM_CHECK 0 -#endif - -// - -// NRF_BALLOC_ENABLED - nrf_balloc - Block allocator module -//========================================================== -#ifndef NRF_BALLOC_ENABLED -#define NRF_BALLOC_ENABLED 1 -#endif -// NRF_BALLOC_CONFIG_DEBUG_ENABLED - Enables debug mode in the module. -//========================================================== -#ifndef NRF_BALLOC_CONFIG_DEBUG_ENABLED -#define NRF_BALLOC_CONFIG_DEBUG_ENABLED 0 -#endif -// NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS - Number of words used as head guard. -// <0-255> - -#ifndef NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS -#define NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS 1 -#endif - -// NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS - Number of words used as tail guard. -// <0-255> - -#ifndef NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS -#define NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS 1 -#endif - -// NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED - Enables basic checks in this -// module. - -#ifndef NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED -#define NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED 0 -#endif - -// NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED - Enables double memory free -// check in this module. - -#ifndef NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED -#define NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED 0 -#endif - -// NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED - Enables free memory -// corruption check in this module. - -#ifndef NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED -#define NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED 0 -#endif - -// NRF_BALLOC_CLI_CMDS - Enable CLI commands specific to the module - -#ifndef NRF_BALLOC_CLI_CMDS -#define NRF_BALLOC_CLI_CMDS 0 -#endif - -// - -// - -// NRF_MEMOBJ_ENABLED - nrf_memobj - Linked memory allocator module - -#ifndef NRF_MEMOBJ_ENABLED -#define NRF_MEMOBJ_ENABLED 1 -#endif - -// NRF_QUEUE_ENABLED - nrf_queue - Queue module -//========================================================== -#ifndef NRF_QUEUE_ENABLED -#define NRF_QUEUE_ENABLED 1 -#endif -// NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module - -#ifndef NRF_QUEUE_CLI_CMDS -#define NRF_QUEUE_CLI_CMDS 0 -#endif - -// - -// NRF_STRERROR_ENABLED - nrf_strerror - Library for converting error code -// to string. - -#ifndef NRF_STRERROR_ENABLED -#define NRF_STRERROR_ENABLED 1 -#endif - -// RETARGET_ENABLED - retarget - Retargeting stdio functions - -#ifndef RETARGET_ENABLED -#define RETARGET_ENABLED 1 -#endif - -// nrf_fprintf - fprintf function. - -//========================================================== -// NRF_FPRINTF_ENABLED - Enable/disable fprintf module. - -#ifndef NRF_FPRINTF_ENABLED -#define NRF_FPRINTF_ENABLED 1 -#endif - -// NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED - For each printed LF, -// function will add CR. - -#ifndef NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED -#define NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED 0 -#endif - -// NRF_FPRINTF_DOUBLE_ENABLED - Enable IEEE-754 double precision -// formatting. - -#ifndef NRF_FPRINTF_DOUBLE_ENABLED -#define NRF_FPRINTF_DOUBLE_ENABLED 0 -#endif - -// -//========================================================== - -// -//========================================================== - -// nRF_Log - -//========================================================== -// NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend -//========================================================== -#ifndef NRF_LOG_BACKEND_RTT_ENABLED -#define NRF_LOG_BACKEND_RTT_ENABLED 0 -#endif -// NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially -// processed strings. Size of the buffer is a trade-off between RAM usage -// and processing. if buffer is smaller then strings will often be -// fragmented. It is recommended to use size which will fit typical log and -// only the longer one will be fragmented. - -#ifndef NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE -#define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 64 -#endif - -// NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS - Period before retrying writing to -// RTT -#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS -#define NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS 1 -#endif - -// NRF_LOG_BACKEND_RTT_TX_RETRY_CNT - Writing to RTT retries. -// If RTT fails to accept any new data after retries -// module assumes that host is not active and on next -// request it will perform only one write attempt. -// On successful writing, module assumes that host is active -// and scheme with retry is applied again. - -#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_CNT -#define NRF_LOG_BACKEND_RTT_TX_RETRY_CNT 3 -#endif - -// - -// NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend -//========================================================== -#ifndef NRF_LOG_BACKEND_UART_ENABLED -#define NRF_LOG_BACKEND_UART_ENABLED 0 -#endif -// NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin -#ifndef NRF_LOG_BACKEND_UART_TX_PIN -#define NRF_LOG_BACKEND_UART_TX_PIN 6 -#endif - -// NRF_LOG_BACKEND_UART_BAUDRATE - Default Baudrate - -// <323584=> 1200 baud -// <643072=> 2400 baud -// <1290240=> 4800 baud -// <2576384=> 9600 baud -// <3862528=> 14400 baud -// <5152768=> 19200 baud -// <7716864=> 28800 baud -// <10289152=> 38400 baud -// <15400960=> 57600 baud -// <20615168=> 76800 baud -// <30801920=> 115200 baud -// <61865984=> 230400 baud -// <67108864=> 250000 baud -// <121634816=> 460800 baud -// <251658240=> 921600 baud -// <268435456=> 1000000 baud - -#ifndef NRF_LOG_BACKEND_UART_BAUDRATE -#define NRF_LOG_BACKEND_UART_BAUDRATE 30801920 -#endif - -// NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE - Size of buffer for partially -// processed strings. Size of the buffer is a trade-off between RAM usage -// and processing. if buffer is smaller then strings will often be -// fragmented. It is recommended to use size which will fit typical log and -// only the longer one will be fragmented. - -#ifndef NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE -#define NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE 64 -#endif - -// - -// NRF_LOG_ENABLED - nrf_log - Logger -//========================================================== -#ifndef NRF_LOG_ENABLED -#define NRF_LOG_ENABLED 0 -#endif -// Log message pool - Configuration of log message pool - -//========================================================== -// NRF_LOG_MSGPOOL_ELEMENT_SIZE - Size of a single element in the pool of -// memory objects. If a small value is set, then performance of logs -// processing is degraded because data is fragmented. Bigger value impacts -// RAM memory utilization. The size is set to fit a message with -// a timestamp and up to 2 arguments in a single memory object. - -#ifndef NRF_LOG_MSGPOOL_ELEMENT_SIZE -#define NRF_LOG_MSGPOOL_ELEMENT_SIZE 20 -#endif - -// NRF_LOG_MSGPOOL_ELEMENT_COUNT - Number of elements in the pool of memory -// objects If a small value is set, then it may lead to a deadlock in -// certain cases if backend has high latency and holds multiple messages for -// long time. Bigger value impacts RAM memory usage. - -#ifndef NRF_LOG_MSGPOOL_ELEMENT_COUNT -#define NRF_LOG_MSGPOOL_ELEMENT_COUNT 8 -#endif - -// -//========================================================== - -// NRF_LOG_ALLOW_OVERFLOW - Configures behavior when circular buffer is -// full. - -// If set then oldest logs are overwritten. Otherwise a -// marker is injected informing about overflow. - -#ifndef NRF_LOG_ALLOW_OVERFLOW -#define NRF_LOG_ALLOW_OVERFLOW 1 -#endif - -// NRF_LOG_BUFSIZE - Size of the buffer for storing logs (in bytes). - -// Must be power of 2 and multiple of 4. -// If NRF_LOG_DEFERRED = 0 then buffer size can be reduced to minimum. -// <128=> 128 -// <256=> 256 -// <512=> 512 -// <1024=> 1024 -// <2048=> 2048 -// <4096=> 4096 -// <8192=> 8192 -// <16384=> 16384 - -#ifndef NRF_LOG_BUFSIZE -#define NRF_LOG_BUFSIZE 1024 -#endif - -// NRF_LOG_CLI_CMDS - Enable CLI commands for the module. - -#ifndef NRF_LOG_CLI_CMDS -#define NRF_LOG_CLI_CMDS 0 -#endif - -// NRF_LOG_DEFAULT_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_LOG_DEFAULT_LEVEL -#define NRF_LOG_DEFAULT_LEVEL 4 -#endif - -// NRF_LOG_DEFERRED - Enable deffered logger. - -// Log data is buffered and can be processed in idle. - -#ifndef NRF_LOG_DEFERRED -#define NRF_LOG_DEFERRED 0 -#endif - -// NRF_LOG_FILTERS_ENABLED - Enable dynamic filtering of logs. - -#ifndef NRF_LOG_FILTERS_ENABLED -#define NRF_LOG_FILTERS_ENABLED 0 -#endif - -// NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED - Enable use of critical -// region for non deffered mode when flushing logs. - -// When enabled NRF_LOG_FLUSH is called from critical section when non -// deffered mode is used. Log output will never be corrupted as access to -// the log backend is exclusive but system will spend significant amount of -// time in critical section - -#ifndef NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED -#define NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED 0 -#endif - -// NRF_LOG_STR_PUSH_BUFFER_SIZE - Size of the buffer dedicated for strings -// stored using @ref NRF_LOG_PUSH. - -// <16=> 16 -// <32=> 32 -// <64=> 64 -// <128=> 128 -// <256=> 256 -// <512=> 512 -// <1024=> 1024 - -#ifndef NRF_LOG_STR_PUSH_BUFFER_SIZE -#define NRF_LOG_STR_PUSH_BUFFER_SIZE 128 -#endif - -// NRF_LOG_STR_PUSH_BUFFER_SIZE - Size of the buffer dedicated for strings -// stored using @ref NRF_LOG_PUSH. - -// <16=> 16 -// <32=> 32 -// <64=> 64 -// <128=> 128 -// <256=> 256 -// <512=> 512 -// <1024=> 1024 - -#ifndef NRF_LOG_STR_PUSH_BUFFER_SIZE -#define NRF_LOG_STR_PUSH_BUFFER_SIZE 128 -#endif - -// NRF_LOG_USES_COLORS - If enabled then ANSI escape code for colors is -// prefixed to every string -//========================================================== -#ifndef NRF_LOG_USES_COLORS -#define NRF_LOG_USES_COLORS 0 -#endif -// NRF_LOG_COLOR_DEFAULT - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_LOG_COLOR_DEFAULT -#define NRF_LOG_COLOR_DEFAULT 0 -#endif - -// NRF_LOG_ERROR_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_LOG_ERROR_COLOR -#define NRF_LOG_ERROR_COLOR 2 -#endif - -// NRF_LOG_WARNING_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_LOG_WARNING_COLOR -#define NRF_LOG_WARNING_COLOR 4 -#endif - -// - -// NRF_LOG_USES_TIMESTAMP - Enable timestamping - -// Function for getting the timestamp is provided by the user -//========================================================== -#ifndef NRF_LOG_USES_TIMESTAMP -#define NRF_LOG_USES_TIMESTAMP 0 -#endif -// NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY - Default frequency of the timestamp -// (in Hz) or 0 to use app_timer frequency. -#ifndef NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY -#define NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY 0 -#endif - -// - -// nrf_log module configuration - -//========================================================== -// nrf_log in nRF_Core - -//========================================================== -// NRF_MPU_LIB_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_MPU_LIB_CONFIG_LOG_ENABLED -#define NRF_MPU_LIB_CONFIG_LOG_ENABLED 0 -#endif -// NRF_MPU_LIB_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_MPU_LIB_CONFIG_LOG_LEVEL -#define NRF_MPU_LIB_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_MPU_LIB_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_MPU_LIB_CONFIG_INFO_COLOR -#define NRF_MPU_LIB_CONFIG_INFO_COLOR 0 -#endif - -// NRF_MPU_LIB_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_MPU_LIB_CONFIG_DEBUG_COLOR -#define NRF_MPU_LIB_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_STACK_GUARD_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_STACK_GUARD_CONFIG_LOG_ENABLED -#define NRF_STACK_GUARD_CONFIG_LOG_ENABLED 0 -#endif -// NRF_STACK_GUARD_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_STACK_GUARD_CONFIG_LOG_LEVEL -#define NRF_STACK_GUARD_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_STACK_GUARD_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_STACK_GUARD_CONFIG_INFO_COLOR -#define NRF_STACK_GUARD_CONFIG_INFO_COLOR 0 -#endif - -// NRF_STACK_GUARD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_STACK_GUARD_CONFIG_DEBUG_COLOR -#define NRF_STACK_GUARD_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// TASK_MANAGER_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef TASK_MANAGER_CONFIG_LOG_ENABLED -#define TASK_MANAGER_CONFIG_LOG_ENABLED 0 -#endif -// TASK_MANAGER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef TASK_MANAGER_CONFIG_LOG_LEVEL -#define TASK_MANAGER_CONFIG_LOG_LEVEL 3 -#endif - -// TASK_MANAGER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TASK_MANAGER_CONFIG_INFO_COLOR -#define TASK_MANAGER_CONFIG_INFO_COLOR 0 -#endif - -// TASK_MANAGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TASK_MANAGER_CONFIG_DEBUG_COLOR -#define TASK_MANAGER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// -//========================================================== - -// nrf_log in nRF_Drivers - -//========================================================== -// CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef CLOCK_CONFIG_LOG_ENABLED -#define CLOCK_CONFIG_LOG_ENABLED 0 -#endif -// CLOCK_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef CLOCK_CONFIG_LOG_LEVEL -#define CLOCK_CONFIG_LOG_LEVEL 3 -#endif - -// CLOCK_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef CLOCK_CONFIG_INFO_COLOR -#define CLOCK_CONFIG_INFO_COLOR 0 -#endif - -// CLOCK_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef CLOCK_CONFIG_DEBUG_COLOR -#define CLOCK_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// COMP_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef COMP_CONFIG_LOG_ENABLED -#define COMP_CONFIG_LOG_ENABLED 0 -#endif -// COMP_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef COMP_CONFIG_LOG_LEVEL -#define COMP_CONFIG_LOG_LEVEL 3 -#endif - -// COMP_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef COMP_CONFIG_INFO_COLOR -#define COMP_CONFIG_INFO_COLOR 0 -#endif - -// COMP_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef COMP_CONFIG_DEBUG_COLOR -#define COMP_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// GPIOTE_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef GPIOTE_CONFIG_LOG_ENABLED -#define GPIOTE_CONFIG_LOG_ENABLED 0 -#endif -// GPIOTE_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef GPIOTE_CONFIG_LOG_LEVEL -#define GPIOTE_CONFIG_LOG_LEVEL 3 -#endif - -// GPIOTE_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef GPIOTE_CONFIG_INFO_COLOR -#define GPIOTE_CONFIG_INFO_COLOR 0 -#endif - -// GPIOTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef GPIOTE_CONFIG_DEBUG_COLOR -#define GPIOTE_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// LPCOMP_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef LPCOMP_CONFIG_LOG_ENABLED -#define LPCOMP_CONFIG_LOG_ENABLED 0 -#endif -// LPCOMP_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef LPCOMP_CONFIG_LOG_LEVEL -#define LPCOMP_CONFIG_LOG_LEVEL 3 -#endif - -// LPCOMP_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef LPCOMP_CONFIG_INFO_COLOR -#define LPCOMP_CONFIG_INFO_COLOR 0 -#endif - -// LPCOMP_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef LPCOMP_CONFIG_DEBUG_COLOR -#define LPCOMP_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// MAX3421E_HOST_CONFIG_LOG_ENABLED - Enable logging in the module -//========================================================== -#ifndef MAX3421E_HOST_CONFIG_LOG_ENABLED -#define MAX3421E_HOST_CONFIG_LOG_ENABLED 0 -#endif -// MAX3421E_HOST_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef MAX3421E_HOST_CONFIG_LOG_LEVEL -#define MAX3421E_HOST_CONFIG_LOG_LEVEL 3 -#endif - -// MAX3421E_HOST_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef MAX3421E_HOST_CONFIG_INFO_COLOR -#define MAX3421E_HOST_CONFIG_INFO_COLOR 0 -#endif - -// MAX3421E_HOST_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef MAX3421E_HOST_CONFIG_DEBUG_COLOR -#define MAX3421E_HOST_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRFX_USBD_CONFIG_LOG_ENABLED - Enable logging in the module -//========================================================== -#ifndef NRFX_USBD_CONFIG_LOG_ENABLED -#define NRFX_USBD_CONFIG_LOG_ENABLED 0 -#endif -// NRFX_USBD_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRFX_USBD_CONFIG_LOG_LEVEL -#define NRFX_USBD_CONFIG_LOG_LEVEL 3 -#endif - -// NRFX_USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_USBD_CONFIG_INFO_COLOR -#define NRFX_USBD_CONFIG_INFO_COLOR 0 -#endif - -// NRFX_USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRFX_USBD_CONFIG_DEBUG_COLOR -#define NRFX_USBD_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// PDM_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef PDM_CONFIG_LOG_ENABLED -#define PDM_CONFIG_LOG_ENABLED 0 -#endif -// PDM_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef PDM_CONFIG_LOG_LEVEL -#define PDM_CONFIG_LOG_LEVEL 3 -#endif - -// PDM_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PDM_CONFIG_INFO_COLOR -#define PDM_CONFIG_INFO_COLOR 0 -#endif - -// PDM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PDM_CONFIG_DEBUG_COLOR -#define PDM_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// PPI_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef PPI_CONFIG_LOG_ENABLED -#define PPI_CONFIG_LOG_ENABLED 0 -#endif -// PPI_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef PPI_CONFIG_LOG_LEVEL -#define PPI_CONFIG_LOG_LEVEL 3 -#endif - -// PPI_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PPI_CONFIG_INFO_COLOR -#define PPI_CONFIG_INFO_COLOR 0 -#endif - -// PPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PPI_CONFIG_DEBUG_COLOR -#define PPI_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// PWM_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef PWM_CONFIG_LOG_ENABLED -#define PWM_CONFIG_LOG_ENABLED 0 -#endif -// PWM_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef PWM_CONFIG_LOG_LEVEL -#define PWM_CONFIG_LOG_LEVEL 3 -#endif - -// PWM_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PWM_CONFIG_INFO_COLOR -#define PWM_CONFIG_INFO_COLOR 0 -#endif - -// PWM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PWM_CONFIG_DEBUG_COLOR -#define PWM_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// QDEC_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef QDEC_CONFIG_LOG_ENABLED -#define QDEC_CONFIG_LOG_ENABLED 0 -#endif -// QDEC_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef QDEC_CONFIG_LOG_LEVEL -#define QDEC_CONFIG_LOG_LEVEL 3 -#endif - -// QDEC_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef QDEC_CONFIG_INFO_COLOR -#define QDEC_CONFIG_INFO_COLOR 0 -#endif - -// QDEC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef QDEC_CONFIG_DEBUG_COLOR -#define QDEC_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// RNG_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef RNG_CONFIG_LOG_ENABLED -#define RNG_CONFIG_LOG_ENABLED 0 -#endif -// RNG_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef RNG_CONFIG_LOG_LEVEL -#define RNG_CONFIG_LOG_LEVEL 3 -#endif - -// RNG_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef RNG_CONFIG_INFO_COLOR -#define RNG_CONFIG_INFO_COLOR 0 -#endif - -// RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef RNG_CONFIG_DEBUG_COLOR -#define RNG_CONFIG_DEBUG_COLOR 0 -#endif - -// RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED - Enables logging of random -// numbers. - -#ifndef RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED -#define RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED 0 -#endif - -// - -// RTC_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef RTC_CONFIG_LOG_ENABLED -#define RTC_CONFIG_LOG_ENABLED 0 -#endif -// RTC_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef RTC_CONFIG_LOG_LEVEL -#define RTC_CONFIG_LOG_LEVEL 3 -#endif - -// RTC_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef RTC_CONFIG_INFO_COLOR -#define RTC_CONFIG_INFO_COLOR 0 -#endif - -// RTC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef RTC_CONFIG_DEBUG_COLOR -#define RTC_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// SAADC_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef SAADC_CONFIG_LOG_ENABLED -#define SAADC_CONFIG_LOG_ENABLED 0 -#endif -// SAADC_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef SAADC_CONFIG_LOG_LEVEL -#define SAADC_CONFIG_LOG_LEVEL 3 -#endif - -// SAADC_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SAADC_CONFIG_INFO_COLOR -#define SAADC_CONFIG_INFO_COLOR 0 -#endif - -// SAADC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SAADC_CONFIG_DEBUG_COLOR -#define SAADC_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// SPIS_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef SPIS_CONFIG_LOG_ENABLED -#define SPIS_CONFIG_LOG_ENABLED 0 -#endif -// SPIS_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef SPIS_CONFIG_LOG_LEVEL -#define SPIS_CONFIG_LOG_LEVEL 3 -#endif - -// SPIS_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SPIS_CONFIG_INFO_COLOR -#define SPIS_CONFIG_INFO_COLOR 0 -#endif - -// SPIS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SPIS_CONFIG_DEBUG_COLOR -#define SPIS_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// SPI_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef SPI_CONFIG_LOG_ENABLED -#define SPI_CONFIG_LOG_ENABLED 0 -#endif -// SPI_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef SPI_CONFIG_LOG_LEVEL -#define SPI_CONFIG_LOG_LEVEL 3 -#endif - -// SPI_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SPI_CONFIG_INFO_COLOR -#define SPI_CONFIG_INFO_COLOR 0 -#endif - -// SPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SPI_CONFIG_DEBUG_COLOR -#define SPI_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef TIMER_CONFIG_LOG_ENABLED -#define TIMER_CONFIG_LOG_ENABLED 0 -#endif -// TIMER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef TIMER_CONFIG_LOG_LEVEL -#define TIMER_CONFIG_LOG_LEVEL 3 -#endif - -// TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TIMER_CONFIG_INFO_COLOR -#define TIMER_CONFIG_INFO_COLOR 0 -#endif - -// TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TIMER_CONFIG_DEBUG_COLOR -#define TIMER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// TWIS_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef TWIS_CONFIG_LOG_ENABLED -#define TWIS_CONFIG_LOG_ENABLED 0 -#endif -// TWIS_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef TWIS_CONFIG_LOG_LEVEL -#define TWIS_CONFIG_LOG_LEVEL 3 -#endif - -// TWIS_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TWIS_CONFIG_INFO_COLOR -#define TWIS_CONFIG_INFO_COLOR 0 -#endif - -// TWIS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TWIS_CONFIG_DEBUG_COLOR -#define TWIS_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// TWI_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef TWI_CONFIG_LOG_ENABLED -#define TWI_CONFIG_LOG_ENABLED 0 -#endif -// TWI_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef TWI_CONFIG_LOG_LEVEL -#define TWI_CONFIG_LOG_LEVEL 3 -#endif - -// TWI_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TWI_CONFIG_INFO_COLOR -#define TWI_CONFIG_INFO_COLOR 0 -#endif - -// TWI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef TWI_CONFIG_DEBUG_COLOR -#define TWI_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// UART_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef UART_CONFIG_LOG_ENABLED -#define UART_CONFIG_LOG_ENABLED 0 -#endif -// UART_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef UART_CONFIG_LOG_LEVEL -#define UART_CONFIG_LOG_LEVEL 3 -#endif - -// UART_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef UART_CONFIG_INFO_COLOR -#define UART_CONFIG_INFO_COLOR 0 -#endif - -// UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef UART_CONFIG_DEBUG_COLOR -#define UART_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// USBD_CONFIG_LOG_ENABLED - Enable logging in the module -//========================================================== -#ifndef USBD_CONFIG_LOG_ENABLED -#define USBD_CONFIG_LOG_ENABLED 0 -#endif -// USBD_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef USBD_CONFIG_LOG_LEVEL -#define USBD_CONFIG_LOG_LEVEL 3 -#endif - -// USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef USBD_CONFIG_INFO_COLOR -#define USBD_CONFIG_INFO_COLOR 0 -#endif - -// USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef USBD_CONFIG_DEBUG_COLOR -#define USBD_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// WDT_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef WDT_CONFIG_LOG_ENABLED -#define WDT_CONFIG_LOG_ENABLED 0 -#endif -// WDT_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef WDT_CONFIG_LOG_LEVEL -#define WDT_CONFIG_LOG_LEVEL 3 -#endif - -// WDT_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef WDT_CONFIG_INFO_COLOR -#define WDT_CONFIG_INFO_COLOR 0 -#endif - -// WDT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef WDT_CONFIG_DEBUG_COLOR -#define WDT_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// -//========================================================== - -// nrf_log in nRF_Libraries - -//========================================================== -// APP_BUTTON_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef APP_BUTTON_CONFIG_LOG_ENABLED -#define APP_BUTTON_CONFIG_LOG_ENABLED 0 -#endif -// APP_BUTTON_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_BUTTON_CONFIG_LOG_LEVEL -#define APP_BUTTON_CONFIG_LOG_LEVEL 3 -#endif - -// APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic -// filtering is enabled. - -// If module generates a lot of logs, initial log level can -// be decreased to prevent flooding. Severity level can be -// increased on instance basis. -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL -#define APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL 3 -#endif - -// APP_BUTTON_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_BUTTON_CONFIG_INFO_COLOR -#define APP_BUTTON_CONFIG_INFO_COLOR 0 -#endif - -// APP_BUTTON_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_BUTTON_CONFIG_DEBUG_COLOR -#define APP_BUTTON_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef APP_TIMER_CONFIG_LOG_ENABLED -#define APP_TIMER_CONFIG_LOG_ENABLED 0 -#endif -// APP_TIMER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_TIMER_CONFIG_LOG_LEVEL -#define APP_TIMER_CONFIG_LOG_LEVEL 3 -#endif - -// APP_TIMER_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic -// filtering is enabled. - -// If module generates a lot of logs, initial log level can -// be decreased to prevent flooding. Severity level can be -// increased on instance basis. -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_TIMER_CONFIG_INITIAL_LOG_LEVEL -#define APP_TIMER_CONFIG_INITIAL_LOG_LEVEL 3 -#endif - -// APP_TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_TIMER_CONFIG_INFO_COLOR -#define APP_TIMER_CONFIG_INFO_COLOR 0 -#endif - -// APP_TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_TIMER_CONFIG_DEBUG_COLOR -#define APP_TIMER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED -#define APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED 0 -#endif -// APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL -#define APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL 3 -#endif - -// APP_USBD_CDC_ACM_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_CDC_ACM_CONFIG_INFO_COLOR -#define APP_USBD_CDC_ACM_CONFIG_INFO_COLOR 0 -#endif - -// APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR -#define APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_USBD_CONFIG_LOG_ENABLED - Enable logging in the module. -//========================================================== -#ifndef APP_USBD_CONFIG_LOG_ENABLED -#define APP_USBD_CONFIG_LOG_ENABLED 0 -#endif -// APP_USBD_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_USBD_CONFIG_LOG_LEVEL -#define APP_USBD_CONFIG_LOG_LEVEL 3 -#endif - -// APP_USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_CONFIG_INFO_COLOR -#define APP_USBD_CONFIG_INFO_COLOR 0 -#endif - -// APP_USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_CONFIG_DEBUG_COLOR -#define APP_USBD_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_USBD_DUMMY_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef APP_USBD_DUMMY_CONFIG_LOG_ENABLED -#define APP_USBD_DUMMY_CONFIG_LOG_ENABLED 0 -#endif -// APP_USBD_DUMMY_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_USBD_DUMMY_CONFIG_LOG_LEVEL -#define APP_USBD_DUMMY_CONFIG_LOG_LEVEL 3 -#endif - -// APP_USBD_DUMMY_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_DUMMY_CONFIG_INFO_COLOR -#define APP_USBD_DUMMY_CONFIG_INFO_COLOR 0 -#endif - -// APP_USBD_DUMMY_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_DUMMY_CONFIG_DEBUG_COLOR -#define APP_USBD_DUMMY_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_USBD_MSC_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef APP_USBD_MSC_CONFIG_LOG_ENABLED -#define APP_USBD_MSC_CONFIG_LOG_ENABLED 0 -#endif -// APP_USBD_MSC_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_USBD_MSC_CONFIG_LOG_LEVEL -#define APP_USBD_MSC_CONFIG_LOG_LEVEL 3 -#endif - -// APP_USBD_MSC_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_MSC_CONFIG_INFO_COLOR -#define APP_USBD_MSC_CONFIG_INFO_COLOR 0 -#endif - -// APP_USBD_MSC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_MSC_CONFIG_DEBUG_COLOR -#define APP_USBD_MSC_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED - Enables logging in the -// module. -//========================================================== -#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED -#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED 0 -#endif -// APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL -#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL 3 -#endif - -// APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR -#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR 0 -#endif - -// APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR -#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_ATFIFO_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_ATFIFO_CONFIG_LOG_ENABLED -#define NRF_ATFIFO_CONFIG_LOG_ENABLED 0 -#endif -// NRF_ATFIFO_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_ATFIFO_CONFIG_LOG_LEVEL -#define NRF_ATFIFO_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level if -// dynamic filtering is enabled - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL -#define NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL 3 -#endif - -// NRF_ATFIFO_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_ATFIFO_CONFIG_INFO_COLOR -#define NRF_ATFIFO_CONFIG_INFO_COLOR 0 -#endif - -// NRF_ATFIFO_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_ATFIFO_CONFIG_DEBUG_COLOR -#define NRF_ATFIFO_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_BALLOC_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_BALLOC_CONFIG_LOG_ENABLED -#define NRF_BALLOC_CONFIG_LOG_ENABLED 0 -#endif -// NRF_BALLOC_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BALLOC_CONFIG_LOG_LEVEL -#define NRF_BALLOC_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic -// filtering is enabled. - -// If module generates a lot of logs, initial log level can -// be decreased to prevent flooding. Severity level can be -// increased on instance basis. -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL -#define NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL 3 -#endif - -// NRF_BALLOC_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BALLOC_CONFIG_INFO_COLOR -#define NRF_BALLOC_CONFIG_INFO_COLOR 0 -#endif - -// NRF_BALLOC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BALLOC_CONFIG_DEBUG_COLOR -#define NRF_BALLOC_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED -#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED 0 -#endif -// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL -#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity -// level if dynamic filtering is enabled - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL -#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR -#define NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR 0 -#endif - -// NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR -#define NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED -#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED 0 -#endif -// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL -#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level -// if dynamic filtering is enabled - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL -#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR -#define NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR 0 -#endif - -// NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR -#define NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED -#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED 0 -#endif -// NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL -#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level -// if dynamic filtering is enabled - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL -#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL 3 -#endif - -// NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR -#define NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR 0 -#endif - -// NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR -#define NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED -#define NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED 0 -#endif -// NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL -#define NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_CLI_BLE_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_BLE_UART_CONFIG_INFO_COLOR -#define NRF_CLI_BLE_UART_CONFIG_INFO_COLOR 0 -#endif - -// NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR -#define NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED -#define NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED 0 -#endif -// NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL -#define NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR -#define NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR 0 -#endif - -// NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR -#define NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_CLI_UART_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_CLI_UART_CONFIG_LOG_ENABLED -#define NRF_CLI_UART_CONFIG_LOG_ENABLED 0 -#endif -// NRF_CLI_UART_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_CLI_UART_CONFIG_LOG_LEVEL -#define NRF_CLI_UART_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_CLI_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_UART_CONFIG_INFO_COLOR -#define NRF_CLI_UART_CONFIG_INFO_COLOR 0 -#endif - -// NRF_CLI_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_CLI_UART_CONFIG_DEBUG_COLOR -#define NRF_CLI_UART_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_LIBUARTE_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_LIBUARTE_CONFIG_LOG_ENABLED -#define NRF_LIBUARTE_CONFIG_LOG_ENABLED 0 -#endif -// NRF_LIBUARTE_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_LIBUARTE_CONFIG_LOG_LEVEL -#define NRF_LIBUARTE_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_LIBUARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_LIBUARTE_CONFIG_INFO_COLOR -#define NRF_LIBUARTE_CONFIG_INFO_COLOR 0 -#endif - -// NRF_LIBUARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_LIBUARTE_CONFIG_DEBUG_COLOR -#define NRF_LIBUARTE_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_MEMOBJ_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_MEMOBJ_CONFIG_LOG_ENABLED -#define NRF_MEMOBJ_CONFIG_LOG_ENABLED 0 -#endif -// NRF_MEMOBJ_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_MEMOBJ_CONFIG_LOG_LEVEL -#define NRF_MEMOBJ_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_MEMOBJ_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_MEMOBJ_CONFIG_INFO_COLOR -#define NRF_MEMOBJ_CONFIG_INFO_COLOR 0 -#endif - -// NRF_MEMOBJ_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_MEMOBJ_CONFIG_DEBUG_COLOR -#define NRF_MEMOBJ_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_PWR_MGMT_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_PWR_MGMT_CONFIG_LOG_ENABLED -#define NRF_PWR_MGMT_CONFIG_LOG_ENABLED 0 -#endif -// NRF_PWR_MGMT_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_PWR_MGMT_CONFIG_LOG_LEVEL -#define NRF_PWR_MGMT_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_PWR_MGMT_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_PWR_MGMT_CONFIG_INFO_COLOR -#define NRF_PWR_MGMT_CONFIG_INFO_COLOR 0 -#endif - -// NRF_PWR_MGMT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_PWR_MGMT_CONFIG_DEBUG_COLOR -#define NRF_PWR_MGMT_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_QUEUE_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_QUEUE_CONFIG_LOG_ENABLED -#define NRF_QUEUE_CONFIG_LOG_ENABLED 0 -#endif -// NRF_QUEUE_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_QUEUE_CONFIG_LOG_LEVEL -#define NRF_QUEUE_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level if -// dynamic filtering is enabled - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL -#define NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL 3 -#endif - -// NRF_QUEUE_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_QUEUE_CONFIG_INFO_COLOR -#define NRF_QUEUE_CONFIG_INFO_COLOR 0 -#endif - -// NRF_QUEUE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_QUEUE_CONFIG_DEBUG_COLOR -#define NRF_QUEUE_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_SDH_ANT_LOG_ENABLED - Enable logging in SoftDevice handler (ANT) -// module. -//========================================================== -#ifndef NRF_SDH_ANT_LOG_ENABLED -#define NRF_SDH_ANT_LOG_ENABLED 0 -#endif -// NRF_SDH_ANT_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_SDH_ANT_LOG_LEVEL -#define NRF_SDH_ANT_LOG_LEVEL 3 -#endif - -// NRF_SDH_ANT_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_ANT_INFO_COLOR -#define NRF_SDH_ANT_INFO_COLOR 0 -#endif - -// NRF_SDH_ANT_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_ANT_DEBUG_COLOR -#define NRF_SDH_ANT_DEBUG_COLOR 0 -#endif - -// - -// NRF_SDH_BLE_LOG_ENABLED - Enable logging in SoftDevice handler (BLE) -// module. -//========================================================== -#ifndef NRF_SDH_BLE_LOG_ENABLED -#define NRF_SDH_BLE_LOG_ENABLED 0 -#endif -// NRF_SDH_BLE_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_SDH_BLE_LOG_LEVEL -#define NRF_SDH_BLE_LOG_LEVEL 3 -#endif - -// NRF_SDH_BLE_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_BLE_INFO_COLOR -#define NRF_SDH_BLE_INFO_COLOR 0 -#endif - -// NRF_SDH_BLE_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_BLE_DEBUG_COLOR -#define NRF_SDH_BLE_DEBUG_COLOR 0 -#endif - -// - -// NRF_SDH_LOG_ENABLED - Enable logging in SoftDevice handler module. -//========================================================== -#ifndef NRF_SDH_LOG_ENABLED -#define NRF_SDH_LOG_ENABLED 0 -#endif -// NRF_SDH_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_SDH_LOG_LEVEL -#define NRF_SDH_LOG_LEVEL 3 -#endif - -// NRF_SDH_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_INFO_COLOR -#define NRF_SDH_INFO_COLOR 0 -#endif - -// NRF_SDH_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_DEBUG_COLOR -#define NRF_SDH_DEBUG_COLOR 0 -#endif - -// - -// NRF_SDH_SOC_LOG_ENABLED - Enable logging in SoftDevice handler (SoC) -// module. -//========================================================== -#ifndef NRF_SDH_SOC_LOG_ENABLED -#define NRF_SDH_SOC_LOG_ENABLED 0 -#endif -// NRF_SDH_SOC_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_SDH_SOC_LOG_LEVEL -#define NRF_SDH_SOC_LOG_LEVEL 3 -#endif - -// NRF_SDH_SOC_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_SOC_INFO_COLOR -#define NRF_SDH_SOC_INFO_COLOR 0 -#endif - -// NRF_SDH_SOC_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SDH_SOC_DEBUG_COLOR -#define NRF_SDH_SOC_DEBUG_COLOR 0 -#endif - -// - -// NRF_SORTLIST_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_SORTLIST_CONFIG_LOG_ENABLED -#define NRF_SORTLIST_CONFIG_LOG_ENABLED 0 -#endif -// NRF_SORTLIST_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_SORTLIST_CONFIG_LOG_LEVEL -#define NRF_SORTLIST_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_SORTLIST_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SORTLIST_CONFIG_INFO_COLOR -#define NRF_SORTLIST_CONFIG_INFO_COLOR 0 -#endif - -// NRF_SORTLIST_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_SORTLIST_CONFIG_DEBUG_COLOR -#define NRF_SORTLIST_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// NRF_TWI_SENSOR_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef NRF_TWI_SENSOR_CONFIG_LOG_ENABLED -#define NRF_TWI_SENSOR_CONFIG_LOG_ENABLED 0 -#endif -// NRF_TWI_SENSOR_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef NRF_TWI_SENSOR_CONFIG_LOG_LEVEL -#define NRF_TWI_SENSOR_CONFIG_LOG_LEVEL 3 -#endif - -// NRF_TWI_SENSOR_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_TWI_SENSOR_CONFIG_INFO_COLOR -#define NRF_TWI_SENSOR_CONFIG_INFO_COLOR 0 -#endif - -// NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR -#define NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR 0s -#endif - -// - -// PM_LOG_ENABLED - Enable logging in Peer Manager and its submodules. -//========================================================== -#ifndef PM_LOG_ENABLED -#define PM_LOG_ENABLED 1 -#endif -// PM_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef PM_LOG_LEVEL -#define PM_LOG_LEVEL 3 -#endif - -// PM_LOG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PM_LOG_INFO_COLOR -#define PM_LOG_INFO_COLOR 0 -#endif - -// PM_LOG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef PM_LOG_DEBUG_COLOR -#define PM_LOG_DEBUG_COLOR 0 -#endif - -// - -// -//========================================================== - -// nrf_log in nRF_Serialization - -//========================================================== -// SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED - Enables logging in the module. -//========================================================== -#ifndef SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED -#define SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED 0 -#endif -// SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug - -#ifndef SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL -#define SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL 3 -#endif - -// SER_HAL_TRANSPORT_CONFIG_INFO_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SER_HAL_TRANSPORT_CONFIG_INFO_COLOR -#define SER_HAL_TRANSPORT_CONFIG_INFO_COLOR 0 -#endif - -// SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White - -#ifndef SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR -#define SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR 0 -#endif - -// - -// -//========================================================== - -// -//========================================================== - -// - -// NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED - nrf_log_str_formatter - -// Log string formatter - -#ifndef NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED -#define NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED 1 -#endif - -// -//========================================================== - -// nRF_Segger_RTT - -//========================================================== -// segger_rtt - SEGGER RTT - -//========================================================== -// SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer. -// Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE -// or this value is actually used. It depends on which one is bigger. - -#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP -#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 2048 -#endif - -// SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Maximum number of upstream -// buffers. -#ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS -#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2 -#endif - -// SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of downstream buffer. -#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN -#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16 -#endif - -// SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Maximum number of downstream -// buffers. -#ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS -#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2 -#endif - -// SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full. - -// The following modes are supported: -// - SKIP - Do not block, output nothing. -// - TRIM - Do not block, output as much as fits. -// - BLOCK - Wait until there is space in the buffer. -// <0=> SKIP -// <1=> TRIM -// <2=> BLOCK_IF_FIFO_FULL - -#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE -#define SEGGER_RTT_CONFIG_DEFAULT_MODE 0 -#endif - -// -//========================================================== - -// -//========================================================== - -// <<< end of configuration section >>> -#endif // SDK_CONFIG_H +/* +Origin: https://www.nordicsemi.com/Products/Development-software/nRF5-SDK/Download +*/ + +/** + * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be + * reverse engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef SDK_CONFIG_H +#define SDK_CONFIG_H +// <<< Use Configuration Wizard in Context Menu >>>\n +#ifdef USE_APP_CONFIG +#include "app_config.h" +#endif +// nRF_Crypto + +//========================================================== +// NRF_CRYPTO_ENABLED - nrf_crypto - Cryptography library. +//========================================================== +#ifndef NRF_CRYPTO_ENABLED +#define NRF_CRYPTO_ENABLED 1 +#endif +// NRF_CRYPTO_ALLOCATOR - Memory allocator + +// Choose memory allocator used by nrf_crypto. Default is alloca if possible +// or nrf_malloc otherwise. If 'User macros' are selected, the user has to +// create 'nrf_crypto_allocator.h' file that contains NRF_CRYPTO_ALLOC, +// NRF_CRYPTO_FREE, and NRF_CRYPTO_ALLOC_ON_STACK. <0=> Default <1=> User macros +// <2=> On stack (alloca) +// <3=> C dynamic memory (malloc) +// <4=> SDK Memory Manager (nrf_malloc) + +#ifndef NRF_CRYPTO_ALLOCATOR +#define NRF_CRYPTO_ALLOCATOR 0 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_ENABLED - Enable the ARM Cryptocell CC310 +// reduced backend. + +// The CC310 hardware-accelerated cryptography backend with reduced +// functionality and footprint (only available on nRF52840). +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED - Enable the secp224r1 +// elliptic curve support using CC310_BL. + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP224R1_ENABLED 0 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED - Enable the secp256r1 +// elliptic curve support using CC310_BL. + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_ECC_SECP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED - CC310_BL SHA-256 hash +// functionality. + +// CC310_BL backend implementation for hardware-accelerated SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED - +// nrf_cc310_bl buffers to RAM before running hash operation + +// Enabling this makes hashing of addresses in FLASH range possible. Size of +// buffer allocated for hashing is set by +// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_ENABLED 0 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE - nrf_cc310_bl +// hash outputs digests in little endian Makes the nrf_cc310_bl hash +// functions output digests in little endian format. Only for use in nRF SDK +// DFU! + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE +#define NRF_CRYPTO_BACKEND_CC310_BL_HASH_AUTOMATIC_RAM_BUFFER_SIZE 4096 +#endif + +// NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED - Enable Interrupts while +// support using CC310 bl. + +// Select a library version compatible with the configuration. When +// interrupts are disable, a version named _noint must be used + +#ifndef NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_BL_INTERRUPTS_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_CC310_ENABLED - Enable the ARM Cryptocell CC310 +// backend. + +// The CC310 hardware-accelerated cryptography backend (only available on +// nRF52840). +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_CC310_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ENABLED 1 +#endif +// NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED - Enable the AES CBC mode using +// CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CBC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED - Enable the AES CTR mode using +// CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CTR_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED - Enable the AES ECB mode using +// CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_ECB_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED - Enable the AES CBC_MAC +// mode using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CBC_MAC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED - Enable the AES CMAC mode +// using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CMAC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED - Enable the AES CCM mode using +// CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CCM_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED - Enable the AES CCM* mode +// using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_AES_CCM_STAR_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED - Enable the CHACHA-POLY +// mode using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_CHACHA_POLY_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED - Enable the secp160r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED - Enable the secp160r2 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160R2_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED - Enable the secp192r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP192R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED - Enable the secp224r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP224R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED - Enable the secp256r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED - Enable the secp384r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP384R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED - Enable the secp521r1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP521R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED - Enable the secp160k1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP160K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED - Enable the secp192k1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP192K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED - Enable the secp224k1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP224K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED - Enable the secp256k1 +// elliptic curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_SECP256K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED - Enable the Curve25519 +// curve support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_CURVE25519_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED - Enable the Ed25519 curve +// support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_ECC_ED25519_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED - CC310 SHA-256 hash +// functionality. + +// CC310 backend implementation for hardware-accelerated SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_HASH_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED - CC310 SHA-512 hash +// functionality + +// CC310 backend implementation for SHA-512 (in software). + +#ifndef NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_HASH_SHA512_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED - CC310 HMAC using SHA-256 + +// CC310 backend implementation for HMAC using hardware-accelerated SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_HMAC_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED - CC310 HMAC using SHA-512 + +// CC310 backend implementation for HMAC using SHA-512 (in software). + +#ifndef NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_HMAC_SHA512_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED - Enable RNG support using CC310. + +#ifndef NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_RNG_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED - Enable Interrupts while +// support using CC310. + +// Select a library version compatible with the configuration. When +// interrupts are disable, a version named _noint must be used + +#ifndef NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED +#define NRF_CRYPTO_BACKEND_CC310_INTERRUPTS_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_CIFRA_ENABLED - Enable the Cifra backend. +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_CIFRA_ENABLED +#define NRF_CRYPTO_BACKEND_CIFRA_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED - Enable the AES EAX mode using +// Cifra. + +#ifndef NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED +#define NRF_CRYPTO_BACKEND_CIFRA_AES_EAX_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED - Enable the mbed TLS backend. +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED - Enable the AES CBC mode +// mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED - Enable the AES CTR mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CTR_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED - Enable the AES CFB mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CFB_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED - Enable the AES ECB mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_ECB_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED - Enable the AES CBC MAC +// mode using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CBC_MAC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED - Enable the AES CMAC mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CMAC_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED - Enable the AES CCM mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_CCM_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED - Enable the AES GCM mode +// using mbed TLS. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_AES_GCM_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED - Enable secp192r1 +// (NIST 192-bit) curve + +// Enable this setting if you need secp192r1 (NIST 192-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED - Enable secp224r1 +// (NIST 224-bit) curve + +// Enable this setting if you need secp224r1 (NIST 224-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED - Enable secp256r1 +// (NIST 256-bit) curve + +// Enable this setting if you need secp256r1 (NIST 256-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED - Enable secp384r1 +// (NIST 384-bit) curve + +// Enable this setting if you need secp384r1 (NIST 384-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP384R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED - Enable secp521r1 +// (NIST 521-bit) curve + +// Enable this setting if you need secp521r1 (NIST 521-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP521R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED - Enable secp192k1 +// (Koblitz 192-bit) curve + +// Enable this setting if you need secp192k1 (Koblitz 192-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP192K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED - Enable secp224k1 +// (Koblitz 224-bit) curve + +// Enable this setting if you need secp224k1 (Koblitz 224-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP224K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED - Enable secp256k1 +// (Koblitz 256-bit) curve + +// Enable this setting if you need secp256k1 (Koblitz 256-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_SECP256K1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED - Enable bp256r1 +// (Brainpool 256-bit) curve + +// Enable this setting if you need bp256r1 (Brainpool 256-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED - Enable bp384r1 +// (Brainpool 384-bit) curve + +// Enable this setting if you need bp384r1 (Brainpool 384-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP384R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED - Enable bp512r1 +// (Brainpool 512-bit) curve + +// Enable this setting if you need bp512r1 (Brainpool 512-bit) support using +// MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_BP512R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED - Enable Curve25519 +// curve + +// Enable this setting if you need Curve25519 support using MBEDTLS + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_ECC_CURVE25519_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED - Enable mbed TLS SHA-256 +// hash functionality. + +// mbed TLS backend implementation for SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED - Enable mbed TLS SHA-512 +// hash functionality. + +// mbed TLS backend implementation for SHA-512. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_HASH_SHA512_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED - Enable mbed TLS HMAC +// using SHA-256. + +// mbed TLS backend implementation for HMAC using SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED - Enable mbed TLS HMAC +// using SHA-512. + +// mbed TLS backend implementation for HMAC using SHA-512. + +#ifndef NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_MBEDTLS_HMAC_SHA512_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED - Enable the micro-ecc backend. +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED +#define NRF_CRYPTO_BACKEND_MICRO_ECC_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED - Enable secp192r1 +// (NIST 192-bit) curve + +// Enable this setting if you need secp192r1 (NIST 192-bit) support using +// micro-ecc + +#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED +#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP192R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED - Enable secp224r1 +// (NIST 224-bit) curve + +// Enable this setting if you need secp224r1 (NIST 224-bit) support using +// micro-ecc + +#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED +#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP224R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED - Enable secp256r1 +// (NIST 256-bit) curve + +// Enable this setting if you need secp256r1 (NIST 256-bit) support using +// micro-ecc + +#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED - Enable secp256k1 +// (Koblitz 256-bit) curve + +// Enable this setting if you need secp256k1 (Koblitz 256-bit) support using +// micro-ecc + +#ifndef NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED +#define NRF_CRYPTO_BACKEND_MICRO_ECC_ECC_SECP256K1_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED - Enable the nRF HW RNG backend. + +// The nRF HW backend provide access to RNG peripheral in nRF5x devices. +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED +#define NRF_CRYPTO_BACKEND_NRF_HW_RNG_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED - Enable mbed TLS +// CTR-DRBG algorithm. + +// Enable mbed TLS CTR-DRBG standardized by NIST (NIST SP 800-90A Rev. 1). +// The nRF HW RNG is used as an entropy source for seeding. + +#ifndef NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED +#define NRF_CRYPTO_BACKEND_NRF_HW_RNG_MBEDTLS_CTR_DRBG_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_NRF_SW_ENABLED - Enable the legacy nRFx sw for crypto. + +// The nRF SW cryptography backend (only used in bootloader context). +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_NRF_SW_ENABLED +#define NRF_CRYPTO_BACKEND_NRF_SW_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED - nRF SW hash backend +// support for SHA-256 + +// The nRF SW backend provide access to nRF SDK legacy hash implementation +// of SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_NRF_SW_HASH_SHA256_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_OBERON_ENABLED - Enable the Oberon backend + +// The Oberon backend +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_OBERON_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED - Enable the CHACHA-POLY +// mode using Oberon. + +#ifndef NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_CHACHA_POLY_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED - Enable secp256r1 curve + +// Enable this setting if you need secp256r1 curve support using Oberon +// library + +#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_ECC_SECP256R1_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED - Enable Curve25519 +// ECDH + +// Enable this setting if you need Curve25519 ECDH support using Oberon +// library + +#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_ECC_CURVE25519_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED - Enable Ed25519 signature +// scheme + +// Enable this setting if you need Ed25519 support using Oberon library + +#ifndef NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_ECC_ED25519_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED - Oberon SHA-256 hash +// functionality + +// Oberon backend implementation for SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_HASH_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED - Oberon SHA-512 hash +// functionality + +// Oberon backend implementation for SHA-512. + +#ifndef NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_HASH_SHA512_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED - Oberon HMAC using +// SHA-256 + +// Oberon backend implementation for HMAC using SHA-256. + +#ifndef NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA256_ENABLED 1 +#endif + +// NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED - Oberon HMAC using +// SHA-512 + +// Oberon backend implementation for HMAC using SHA-512. + +#ifndef NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED +#define NRF_CRYPTO_BACKEND_OBERON_HMAC_SHA512_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_BACKEND_OPTIGA_ENABLED - Enable the nrf_crypto Optiga Trust X +// backend. + +// Enables the nrf_crypto backend for Optiga Trust X devices. +//========================================================== +#ifndef NRF_CRYPTO_BACKEND_OPTIGA_ENABLED +#define NRF_CRYPTO_BACKEND_OPTIGA_ENABLED 0 +#endif +// NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED - Optiga backend support for RNG + +// The Optiga backend provide external chip RNG. + +#ifndef NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED +#define NRF_CRYPTO_BACKEND_OPTIGA_RNG_ENABLED 0 +#endif + +// NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED - Optiga backend support +// for ECC secp256r1 + +// The Optiga backend provide external chip ECC using secp256r1. + +#ifndef NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED +#define NRF_CRYPTO_BACKEND_OPTIGA_ECC_SECP256R1_ENABLED 1 +#endif + +// + +// NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED - Big-endian byte order in raw +// Curve25519 data + +// Enable big-endian byte order in Curve25519 API, if set to 1. Use +// little-endian, if set to 0. + +#ifndef NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED +#define NRF_CRYPTO_CURVE25519_BIG_ENDIAN_ENABLED 0 +#endif + +// + +// nrf_crypto_rng - RNG Configuration + +//========================================================== +// NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED - Use static memory buffers +// for context and temporary init buffer. + +// Always recommended when using the nRF HW RNG as the context and temporary +// buffers are small. Consider disabling if using the CC310 RNG in a RAM +// constrained application. In this case, memory must be provided to +// nrf_crypto_rng_init, or it can be allocated internally provided that +// NRF_CRYPTO_ALLOCATOR does not allocate memory on the stack. + +#ifndef NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED +#define NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS_ENABLED 1 +#endif + +// NRF_CRYPTO_RNG_AUTO_INIT_ENABLED - Initialize the RNG module +// automatically when nrf_crypto is initialized. + +// Automatic initialization is only supported with static or internally +// allocated context and temporary memory. + +#ifndef NRF_CRYPTO_RNG_AUTO_INIT_ENABLED +#define NRF_CRYPTO_RNG_AUTO_INIT_ENABLED 1 +#endif + +// +//========================================================== + +// +//========================================================== + +// nRF_Drivers + +//========================================================== +// NRFX_TIMER_ENABLED - nrfx_timer - TIMER periperal driver +//========================================================== +#ifndef NRFX_TIMER_ENABLED +#define NRFX_TIMER_ENABLED 1 +#endif +// NRFX_TIMER0_ENABLED - Enable TIMER0 instance + +#ifndef NRFX_TIMER0_ENABLED +#define NRFX_TIMER0_ENABLED 0 +#endif + +// NRFX_TIMER1_ENABLED - Enable TIMER1 instance + +#ifndef NRFX_TIMER1_ENABLED +#define NRFX_TIMER1_ENABLED 0 +#endif + +// NRFX_TIMER2_ENABLED - Enable TIMER2 instance + +#ifndef NRFX_TIMER2_ENABLED +#define NRFX_TIMER2_ENABLED 0 +#endif + +// NRFX_TIMER3_ENABLED - Enable TIMER3 instance + +#ifndef NRFX_TIMER3_ENABLED +#define NRFX_TIMER3_ENABLED 0 +#endif + +// NRFX_TIMER4_ENABLED - Enable TIMER4 instance + +#ifndef NRFX_TIMER4_ENABLED +#define NRFX_TIMER4_ENABLED 0 +#endif + +// NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY - Timer frequency if in Timer mode + +// <0=> 16 MHz +// <1=> 8 MHz +// <2=> 4 MHz +// <3=> 2 MHz +// <4=> 1 MHz +// <5=> 500 kHz +// <6=> 250 kHz +// <7=> 125 kHz +// <8=> 62.5 kHz +// <9=> 31.25 kHz + +#ifndef NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY +#define NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY 0 +#endif + +// NRFX_TIMER_DEFAULT_CONFIG_MODE - Timer mode or operation + +// <0=> Timer +// <1=> Counter + +#ifndef NRFX_TIMER_DEFAULT_CONFIG_MODE +#define NRFX_TIMER_DEFAULT_CONFIG_MODE 0 +#endif + +// NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH - Timer counter bit width + +// <0=> 16 bit +// <1=> 8 bit +// <2=> 24 bit +// <3=> 32 bit + +#ifndef NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH +#define NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH 0 +#endif + +// NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority + +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY +#define NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6 +#endif + +// NRFX_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRFX_TIMER_CONFIG_LOG_ENABLED +#define NRFX_TIMER_CONFIG_LOG_ENABLED 0 +#endif +// NRFX_TIMER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_TIMER_CONFIG_LOG_LEVEL +#define NRFX_TIMER_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_TIMER_CONFIG_INFO_COLOR +#define NRFX_TIMER_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_TIMER_CONFIG_DEBUG_COLOR +#define NRFX_TIMER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// + +// TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer +//========================================================== +#ifndef TIMER_ENABLED +#define TIMER_ENABLED 1 +#endif +// TIMER_DEFAULT_CONFIG_FREQUENCY - Timer frequency if in Timer mode + +// <0=> 16 MHz +// <1=> 8 MHz +// <2=> 4 MHz +// <3=> 2 MHz +// <4=> 1 MHz +// <5=> 500 kHz +// <6=> 250 kHz +// <7=> 125 kHz +// <8=> 62.5 kHz +// <9=> 31.25 kHz + +#ifndef TIMER_DEFAULT_CONFIG_FREQUENCY +#define TIMER_DEFAULT_CONFIG_FREQUENCY 0 +#endif + +// TIMER_DEFAULT_CONFIG_MODE - Timer mode or operation + +// <0=> Timer +// <1=> Counter + +#ifndef TIMER_DEFAULT_CONFIG_MODE +#define TIMER_DEFAULT_CONFIG_MODE 0 +#endif + +// TIMER_DEFAULT_CONFIG_BIT_WIDTH - Timer counter bit width + +// <0=> 16 bit +// <1=> 8 bit +// <2=> 24 bit +// <3=> 32 bit + +#ifndef TIMER_DEFAULT_CONFIG_BIT_WIDTH +#define TIMER_DEFAULT_CONFIG_BIT_WIDTH 3 +#endif + +// TIMER_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority + +// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef TIMER_DEFAULT_CONFIG_IRQ_PRIORITY +#define TIMER_DEFAULT_CONFIG_IRQ_PRIORITY 6 +#endif + +// TIMER0_ENABLED - Enable TIMER0 instance + +#ifndef TIMER0_ENABLED +#define TIMER0_ENABLED 1 +#endif + +// TIMER1_ENABLED - Enable TIMER1 instance + +#ifndef TIMER1_ENABLED +#define TIMER1_ENABLED 0 +#endif + +// TIMER2_ENABLED - Enable TIMER2 instance + +#ifndef TIMER2_ENABLED +#define TIMER2_ENABLED 0 +#endif + +// TIMER3_ENABLED - Enable TIMER3 instance + +#ifndef TIMER3_ENABLED +#define TIMER3_ENABLED 0 +#endif + +// TIMER4_ENABLED - Enable TIMER4 instance + +#ifndef TIMER4_ENABLED +#define TIMER4_ENABLED 0 +#endif + +//========================================================== +// NRFX_PRS_ENABLED - nrfx_prs - Peripheral Resource Sharing module +//========================================================== +#ifndef NRFX_PRS_ENABLED +#define NRFX_PRS_ENABLED 1 +#endif +// NRFX_PRS_BOX_0_ENABLED - Enables box 0 in the module. + +#ifndef NRFX_PRS_BOX_0_ENABLED +#define NRFX_PRS_BOX_0_ENABLED 0 +#endif + +// NRFX_PRS_BOX_1_ENABLED - Enables box 1 in the module. + +#ifndef NRFX_PRS_BOX_1_ENABLED +#define NRFX_PRS_BOX_1_ENABLED 0 +#endif + +// NRFX_PRS_BOX_2_ENABLED - Enables box 2 in the module. + +#ifndef NRFX_PRS_BOX_2_ENABLED +#define NRFX_PRS_BOX_2_ENABLED 0 +#endif + +// NRFX_PRS_BOX_3_ENABLED - Enables box 3 in the module. + +#ifndef NRFX_PRS_BOX_3_ENABLED +#define NRFX_PRS_BOX_3_ENABLED 0 +#endif + +// NRFX_PRS_BOX_4_ENABLED - Enables box 4 in the module. + +#ifndef NRFX_PRS_BOX_4_ENABLED +#define NRFX_PRS_BOX_4_ENABLED 1 +#endif + +// NRFX_PRS_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRFX_PRS_CONFIG_LOG_ENABLED +#define NRFX_PRS_CONFIG_LOG_ENABLED 1 +#endif +// NRFX_PRS_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_PRS_CONFIG_LOG_LEVEL +#define NRFX_PRS_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_PRS_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_PRS_CONFIG_INFO_COLOR +#define NRFX_PRS_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_PRS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_PRS_CONFIG_DEBUG_COLOR +#define NRFX_PRS_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// + +// NRFX_RNG_ENABLED - nrfx_rng - RNG peripheral driver +//========================================================== +#ifndef NRFX_RNG_ENABLED +#define NRFX_RNG_ENABLED 1 +#endif +// NRFX_RNG_CONFIG_ERROR_CORRECTION - Error correction + +#ifndef NRFX_RNG_CONFIG_ERROR_CORRECTION +#define NRFX_RNG_CONFIG_ERROR_CORRECTION 1 +#endif + +// NRFX_RNG_CONFIG_IRQ_PRIORITY - Interrupt priority + +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef NRFX_RNG_CONFIG_IRQ_PRIORITY +#define NRFX_RNG_CONFIG_IRQ_PRIORITY 6 +#endif + +// NRFX_RNG_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRFX_RNG_CONFIG_LOG_ENABLED +#define NRFX_RNG_CONFIG_LOG_ENABLED 0 +#endif +// NRFX_RNG_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_RNG_CONFIG_LOG_LEVEL +#define NRFX_RNG_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_RNG_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_RNG_CONFIG_INFO_COLOR +#define NRFX_RNG_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_RNG_CONFIG_DEBUG_COLOR +#define NRFX_RNG_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// + +// NRFX_UARTE_ENABLED - nrfx_uarte - UARTE peripheral driver +//========================================================== +#ifndef NRFX_UARTE_ENABLED +#define NRFX_UARTE_ENABLED 1 +#endif +// NRFX_UARTE0_ENABLED - Enable UARTE0 instance +#ifndef NRFX_UARTE0_ENABLED +#define NRFX_UARTE0_ENABLED 0 +#endif + +// NRFX_UARTE1_ENABLED - Enable UARTE1 instance +#ifndef NRFX_UARTE1_ENABLED +#define NRFX_UARTE1_ENABLED 0 +#endif + +// NRFX_UARTE_DEFAULT_CONFIG_HWFC - Hardware Flow Control + +// <0=> Disabled +// <1=> Enabled + +#ifndef NRFX_UARTE_DEFAULT_CONFIG_HWFC +#define NRFX_UARTE_DEFAULT_CONFIG_HWFC 0 +#endif + +// NRFX_UARTE_DEFAULT_CONFIG_PARITY - Parity + +// <0=> Excluded +// <14=> Included + +#ifndef NRFX_UARTE_DEFAULT_CONFIG_PARITY +#define NRFX_UARTE_DEFAULT_CONFIG_PARITY 0 +#endif + +// NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE - Default Baudrate + +// <323584=> 1200 baud +// <643072=> 2400 baud +// <1290240=> 4800 baud +// <2576384=> 9600 baud +// <3862528=> 14400 baud +// <5152768=> 19200 baud +// <7716864=> 28800 baud +// <8388608=> 31250 baud +// <10289152=> 38400 baud +// <15007744=> 56000 baud +// <15400960=> 57600 baud +// <20615168=> 76800 baud +// <30801920=> 115200 baud +// <61865984=> 230400 baud +// <67108864=> 250000 baud +// <121634816=> 460800 baud +// <251658240=> 921600 baud +// <268435456=> 1000000 baud + +#ifndef NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE +#define NRFX_UARTE_DEFAULT_CONFIG_BAUDRATE 30801920 +#endif + +// NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority + +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY +#define NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY 6 +#endif + +// NRFX_UARTE_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRFX_UARTE_CONFIG_LOG_ENABLED +#define NRFX_UARTE_CONFIG_LOG_ENABLED 0 +#endif +// NRFX_UARTE_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_UARTE_CONFIG_LOG_LEVEL +#define NRFX_UARTE_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_UARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_UARTE_CONFIG_INFO_COLOR +#define NRFX_UARTE_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_UARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_UARTE_CONFIG_DEBUG_COLOR +#define NRFX_UARTE_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// + +// NRFX_UART_ENABLED - nrfx_uart - UART peripheral driver +//========================================================== +#ifndef NRFX_UART_ENABLED +#define NRFX_UART_ENABLED 1 +#endif +// NRFX_UART0_ENABLED - Enable UART0 instance +#ifndef NRFX_UART0_ENABLED +#define NRFX_UART0_ENABLED 0 +#endif + +// NRFX_UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control + +// <0=> Disabled +// <1=> Enabled + +#ifndef NRFX_UART_DEFAULT_CONFIG_HWFC +#define NRFX_UART_DEFAULT_CONFIG_HWFC 0 +#endif + +// NRFX_UART_DEFAULT_CONFIG_PARITY - Parity + +// <0=> Excluded +// <14=> Included + +#ifndef NRFX_UART_DEFAULT_CONFIG_PARITY +#define NRFX_UART_DEFAULT_CONFIG_PARITY 0 +#endif + +// NRFX_UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate + +// <323584=> 1200 baud +// <643072=> 2400 baud +// <1290240=> 4800 baud +// <2576384=> 9600 baud +// <3866624=> 14400 baud +// <5152768=> 19200 baud +// <7729152=> 28800 baud +// <8388608=> 31250 baud +// <10309632=> 38400 baud +// <15007744=> 56000 baud +// <15462400=> 57600 baud +// <20615168=> 76800 baud +// <30924800=> 115200 baud +// <61845504=> 230400 baud +// <67108864=> 250000 baud +// <123695104=> 460800 baud +// <247386112=> 921600 baud +// <268435456=> 1000000 baud + +#ifndef NRFX_UART_DEFAULT_CONFIG_BAUDRATE +#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE 30924800 +#endif + +// NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority + +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY +#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 6 +#endif + +// NRFX_UART_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRFX_UART_CONFIG_LOG_ENABLED +#define NRFX_UART_CONFIG_LOG_ENABLED 0 +#endif +// NRFX_UART_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_UART_CONFIG_LOG_LEVEL +#define NRFX_UART_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_UART_CONFIG_INFO_COLOR +#define NRFX_UART_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_UART_CONFIG_DEBUG_COLOR +#define NRFX_UART_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// + +// RNG_ENABLED - nrf_drv_rng - RNG peripheral driver - legacy layer +//========================================================== +#ifndef RNG_ENABLED +#define RNG_ENABLED 1 +#endif +// RNG_CONFIG_ERROR_CORRECTION - Error correction + +#ifndef RNG_CONFIG_ERROR_CORRECTION +#define RNG_CONFIG_ERROR_CORRECTION 1 +#endif + +// RNG_CONFIG_POOL_SIZE - Pool size +#ifndef RNG_CONFIG_POOL_SIZE +#define RNG_CONFIG_POOL_SIZE 64 +#endif + +// RNG_CONFIG_IRQ_PRIORITY - Interrupt priority + +// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef RNG_CONFIG_IRQ_PRIORITY +#define RNG_CONFIG_IRQ_PRIORITY 6 +#endif + +// + +// UART_ENABLED - nrf_drv_uart - UART/UARTE peripheral driver - legacy layer +//========================================================== +#ifndef UART_ENABLED +#define UART_ENABLED 1 +#endif +// UART_DEFAULT_CONFIG_HWFC - Hardware Flow Control + +// <0=> Disabled +// <1=> Enabled + +#ifndef UART_DEFAULT_CONFIG_HWFC +#define UART_DEFAULT_CONFIG_HWFC 0 +#endif + +// UART_DEFAULT_CONFIG_PARITY - Parity + +// <0=> Excluded +// <14=> Included + +#ifndef UART_DEFAULT_CONFIG_PARITY +#define UART_DEFAULT_CONFIG_PARITY 0 +#endif + +// UART_DEFAULT_CONFIG_BAUDRATE - Default Baudrate + +// <323584=> 1200 baud +// <643072=> 2400 baud +// <1290240=> 4800 baud +// <2576384=> 9600 baud +// <3862528=> 14400 baud +// <5152768=> 19200 baud +// <7716864=> 28800 baud +// <10289152=> 38400 baud +// <15400960=> 57600 baud +// <20615168=> 76800 baud +// <30801920=> 115200 baud +// <61865984=> 230400 baud +// <67108864=> 250000 baud +// <121634816=> 460800 baud +// <251658240=> 921600 baud +// <268435456=> 1000000 baud + +#ifndef UART_DEFAULT_CONFIG_BAUDRATE +#define UART_DEFAULT_CONFIG_BAUDRATE 30801920 +#endif + +// UART_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority + +// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 + +#ifndef UART_DEFAULT_CONFIG_IRQ_PRIORITY +#define UART_DEFAULT_CONFIG_IRQ_PRIORITY 6 +#endif + +// UART_EASY_DMA_SUPPORT - Driver supporting EasyDMA + +#ifndef UART_EASY_DMA_SUPPORT +#define UART_EASY_DMA_SUPPORT 1 +#endif + +// UART_LEGACY_SUPPORT - Driver supporting Legacy mode + +#ifndef UART_LEGACY_SUPPORT +#define UART_LEGACY_SUPPORT 1 +#endif + +// UART0_ENABLED - Enable UART0 instance +//========================================================== +#ifndef UART0_ENABLED +#define UART0_ENABLED 1 +#endif +// UART0_CONFIG_USE_EASY_DMA - Default setting for using EasyDMA + +#ifndef UART0_CONFIG_USE_EASY_DMA +#define UART0_CONFIG_USE_EASY_DMA 1 +#endif + +// + +// UART1_ENABLED - Enable UART1 instance +//========================================================== +#ifndef UART1_ENABLED +#define UART1_ENABLED 0 +#endif +// + +// + +// +//========================================================== + +// nRF_Libraries + +//========================================================== +// APP_FIFO_ENABLED - app_fifo - Software FIFO implementation + +#ifndef APP_FIFO_ENABLED +#define APP_FIFO_ENABLED 1 +#endif + +// APP_UART_ENABLED - app_uart - UART driver +//========================================================== +#ifndef APP_UART_ENABLED +#define APP_UART_ENABLED 1 +#endif +// APP_UART_DRIVER_INSTANCE - UART instance used + +// <0=> 0 + +#ifndef APP_UART_DRIVER_INSTANCE +#define APP_UART_DRIVER_INSTANCE 0 +#endif + +// + +//========================================================== +// MEM_MANAGER_ENABLED - mem_manager - Dynamic memory allocator +//========================================================== +#ifndef MEM_MANAGER_ENABLED +#define MEM_MANAGER_ENABLED 1 +#endif +// MEMORY_MANAGER_SMALL_BLOCK_COUNT - Size of each memory blocks identified +// as 'small' block. <0-255> + +#ifndef MEMORY_MANAGER_SMALL_BLOCK_COUNT +#define MEMORY_MANAGER_SMALL_BLOCK_COUNT 1 +#endif + +// MEMORY_MANAGER_SMALL_BLOCK_SIZE - Size of each memory blocks identified +// as 'small' block. Size of each memory blocks identified as 'small' +// block. Memory block are recommended to be word-sized. + +#ifndef MEMORY_MANAGER_SMALL_BLOCK_SIZE +#define MEMORY_MANAGER_SMALL_BLOCK_SIZE 32 +#endif + +// MEMORY_MANAGER_MEDIUM_BLOCK_COUNT - Size of each memory blocks identified +// as 'medium' block. <0-255> + +#ifndef MEMORY_MANAGER_MEDIUM_BLOCK_COUNT +#define MEMORY_MANAGER_MEDIUM_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_MEDIUM_BLOCK_SIZE - Size of each memory blocks identified +// as 'medium' block. Size of each memory blocks identified as 'medium' +// block. Memory block are recommended to be word-sized. + +#ifndef MEMORY_MANAGER_MEDIUM_BLOCK_SIZE +#define MEMORY_MANAGER_MEDIUM_BLOCK_SIZE 256 +#endif + +// MEMORY_MANAGER_LARGE_BLOCK_COUNT - Size of each memory blocks identified +// as 'large' block. <0-255> + +#ifndef MEMORY_MANAGER_LARGE_BLOCK_COUNT +#define MEMORY_MANAGER_LARGE_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_LARGE_BLOCK_SIZE - Size of each memory blocks identified +// as 'large' block. Size of each memory blocks identified as 'large' +// block. Memory block are recommended to be word-sized. + +#ifndef MEMORY_MANAGER_LARGE_BLOCK_SIZE +#define MEMORY_MANAGER_LARGE_BLOCK_SIZE 256 +#endif + +// MEMORY_MANAGER_XLARGE_BLOCK_COUNT - Size of each memory blocks identified +// as 'extra large' block. <0-255> + +#ifndef MEMORY_MANAGER_XLARGE_BLOCK_COUNT +#define MEMORY_MANAGER_XLARGE_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_XLARGE_BLOCK_SIZE - Size of each memory blocks identified +// as 'extra large' block. Size of each memory blocks identified as 'extra +// large' block. Memory block are recommended to be word-sized. + +#ifndef MEMORY_MANAGER_XLARGE_BLOCK_SIZE +#define MEMORY_MANAGER_XLARGE_BLOCK_SIZE 1320 +#endif + +// MEMORY_MANAGER_XXLARGE_BLOCK_COUNT - Size of each memory blocks +// identified as 'extra extra large' block. <0-255> + +#ifndef MEMORY_MANAGER_XXLARGE_BLOCK_COUNT +#define MEMORY_MANAGER_XXLARGE_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_XXLARGE_BLOCK_SIZE - Size of each memory blocks +// identified as 'extra extra large' block. Size of each memory blocks +// identified as 'extra extra large' block. Memory block are recommended to be +// word-sized. + +#ifndef MEMORY_MANAGER_XXLARGE_BLOCK_SIZE +#define MEMORY_MANAGER_XXLARGE_BLOCK_SIZE 3444 +#endif + +// MEMORY_MANAGER_XSMALL_BLOCK_COUNT - Size of each memory blocks identified +// as 'extra small' block. <0-255> + +#ifndef MEMORY_MANAGER_XSMALL_BLOCK_COUNT +#define MEMORY_MANAGER_XSMALL_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_XSMALL_BLOCK_SIZE - Size of each memory blocks identified +// as 'extra small' block. Size of each memory blocks identified as 'extra +// large' block. Memory block are recommended to be word-sized. + +#ifndef MEMORY_MANAGER_XSMALL_BLOCK_SIZE +#define MEMORY_MANAGER_XSMALL_BLOCK_SIZE 64 +#endif + +// MEMORY_MANAGER_XXSMALL_BLOCK_COUNT - Size of each memory blocks +// identified as 'extra extra small' block. <0-255> + +#ifndef MEMORY_MANAGER_XXSMALL_BLOCK_COUNT +#define MEMORY_MANAGER_XXSMALL_BLOCK_COUNT 0 +#endif + +// MEMORY_MANAGER_XXSMALL_BLOCK_SIZE - Size of each memory blocks +// identified as 'extra extra small' block. Size of each memory blocks +// identified as 'extra extra small' block. Memory block are recommended to be +// word-sized. + +#ifndef MEMORY_MANAGER_XXSMALL_BLOCK_SIZE +#define MEMORY_MANAGER_XXSMALL_BLOCK_SIZE 32 +#endif + +// MEM_MANAGER_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef MEM_MANAGER_CONFIG_LOG_ENABLED +#define MEM_MANAGER_CONFIG_LOG_ENABLED 0 +#endif +// MEM_MANAGER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef MEM_MANAGER_CONFIG_LOG_LEVEL +#define MEM_MANAGER_CONFIG_LOG_LEVEL 3 +#endif + +// MEM_MANAGER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef MEM_MANAGER_CONFIG_INFO_COLOR +#define MEM_MANAGER_CONFIG_INFO_COLOR 0 +#endif + +// MEM_MANAGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef MEM_MANAGER_CONFIG_DEBUG_COLOR +#define MEM_MANAGER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// MEM_MANAGER_DISABLE_API_PARAM_CHECK - Disable API parameter checks in +// the module. + +#ifndef MEM_MANAGER_DISABLE_API_PARAM_CHECK +#define MEM_MANAGER_DISABLE_API_PARAM_CHECK 0 +#endif + +// + +// NRF_BALLOC_ENABLED - nrf_balloc - Block allocator module +//========================================================== +#ifndef NRF_BALLOC_ENABLED +#define NRF_BALLOC_ENABLED 1 +#endif +// NRF_BALLOC_CONFIG_DEBUG_ENABLED - Enables debug mode in the module. +//========================================================== +#ifndef NRF_BALLOC_CONFIG_DEBUG_ENABLED +#define NRF_BALLOC_CONFIG_DEBUG_ENABLED 0 +#endif +// NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS - Number of words used as head guard. +// <0-255> + +#ifndef NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS +#define NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS 1 +#endif + +// NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS - Number of words used as tail guard. +// <0-255> + +#ifndef NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS +#define NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS 1 +#endif + +// NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED - Enables basic checks in this +// module. + +#ifndef NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED +#define NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED 0 +#endif + +// NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED - Enables double memory free +// check in this module. + +#ifndef NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED +#define NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED 0 +#endif + +// NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED - Enables free memory +// corruption check in this module. + +#ifndef NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED +#define NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED 0 +#endif + +// NRF_BALLOC_CLI_CMDS - Enable CLI commands specific to the module + +#ifndef NRF_BALLOC_CLI_CMDS +#define NRF_BALLOC_CLI_CMDS 0 +#endif + +// + +// + +// NRF_MEMOBJ_ENABLED - nrf_memobj - Linked memory allocator module + +#ifndef NRF_MEMOBJ_ENABLED +#define NRF_MEMOBJ_ENABLED 1 +#endif + +// NRF_QUEUE_ENABLED - nrf_queue - Queue module +//========================================================== +#ifndef NRF_QUEUE_ENABLED +#define NRF_QUEUE_ENABLED 1 +#endif +// NRF_QUEUE_CLI_CMDS - Enable CLI commands specific to the module + +#ifndef NRF_QUEUE_CLI_CMDS +#define NRF_QUEUE_CLI_CMDS 0 +#endif + +// + +// NRF_STRERROR_ENABLED - nrf_strerror - Library for converting error code +// to string. + +#ifndef NRF_STRERROR_ENABLED +#define NRF_STRERROR_ENABLED 1 +#endif + +// RETARGET_ENABLED - retarget - Retargeting stdio functions + +#ifndef RETARGET_ENABLED +#define RETARGET_ENABLED 1 +#endif + +// nrf_fprintf - fprintf function. + +//========================================================== +// NRF_FPRINTF_ENABLED - Enable/disable fprintf module. + +#ifndef NRF_FPRINTF_ENABLED +#define NRF_FPRINTF_ENABLED 1 +#endif + +// NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED - For each printed LF, +// function will add CR. + +#ifndef NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED +#define NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED 0 +#endif + +// NRF_FPRINTF_DOUBLE_ENABLED - Enable IEEE-754 double precision +// formatting. + +#ifndef NRF_FPRINTF_DOUBLE_ENABLED +#define NRF_FPRINTF_DOUBLE_ENABLED 0 +#endif + +// +//========================================================== + +// +//========================================================== + +// nRF_Log + +//========================================================== +// NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend +//========================================================== +#ifndef NRF_LOG_BACKEND_RTT_ENABLED +#define NRF_LOG_BACKEND_RTT_ENABLED 0 +#endif +// NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially +// processed strings. Size of the buffer is a trade-off between RAM usage +// and processing. if buffer is smaller then strings will often be +// fragmented. It is recommended to use size which will fit typical log and +// only the longer one will be fragmented. + +#ifndef NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE +#define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 64 +#endif + +// NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS - Period before retrying writing to +// RTT +#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS +#define NRF_LOG_BACKEND_RTT_TX_RETRY_DELAY_MS 1 +#endif + +// NRF_LOG_BACKEND_RTT_TX_RETRY_CNT - Writing to RTT retries. +// If RTT fails to accept any new data after retries +// module assumes that host is not active and on next +// request it will perform only one write attempt. +// On successful writing, module assumes that host is active +// and scheme with retry is applied again. + +#ifndef NRF_LOG_BACKEND_RTT_TX_RETRY_CNT +#define NRF_LOG_BACKEND_RTT_TX_RETRY_CNT 3 +#endif + +// + +// NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend +//========================================================== +#ifndef NRF_LOG_BACKEND_UART_ENABLED +#define NRF_LOG_BACKEND_UART_ENABLED 0 +#endif +// NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin +#ifndef NRF_LOG_BACKEND_UART_TX_PIN +#define NRF_LOG_BACKEND_UART_TX_PIN 6 +#endif + +// NRF_LOG_BACKEND_UART_BAUDRATE - Default Baudrate + +// <323584=> 1200 baud +// <643072=> 2400 baud +// <1290240=> 4800 baud +// <2576384=> 9600 baud +// <3862528=> 14400 baud +// <5152768=> 19200 baud +// <7716864=> 28800 baud +// <10289152=> 38400 baud +// <15400960=> 57600 baud +// <20615168=> 76800 baud +// <30801920=> 115200 baud +// <61865984=> 230400 baud +// <67108864=> 250000 baud +// <121634816=> 460800 baud +// <251658240=> 921600 baud +// <268435456=> 1000000 baud + +#ifndef NRF_LOG_BACKEND_UART_BAUDRATE +#define NRF_LOG_BACKEND_UART_BAUDRATE 30801920 +#endif + +// NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE - Size of buffer for partially +// processed strings. Size of the buffer is a trade-off between RAM usage +// and processing. if buffer is smaller then strings will often be +// fragmented. It is recommended to use size which will fit typical log and +// only the longer one will be fragmented. + +#ifndef NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE +#define NRF_LOG_BACKEND_UART_TEMP_BUFFER_SIZE 64 +#endif + +// + +// NRF_LOG_ENABLED - nrf_log - Logger +//========================================================== +#ifndef NRF_LOG_ENABLED +#define NRF_LOG_ENABLED 0 +#endif +// Log message pool - Configuration of log message pool + +//========================================================== +// NRF_LOG_MSGPOOL_ELEMENT_SIZE - Size of a single element in the pool of +// memory objects. If a small value is set, then performance of logs +// processing is degraded because data is fragmented. Bigger value impacts +// RAM memory utilization. The size is set to fit a message with +// a timestamp and up to 2 arguments in a single memory object. + +#ifndef NRF_LOG_MSGPOOL_ELEMENT_SIZE +#define NRF_LOG_MSGPOOL_ELEMENT_SIZE 20 +#endif + +// NRF_LOG_MSGPOOL_ELEMENT_COUNT - Number of elements in the pool of memory +// objects If a small value is set, then it may lead to a deadlock in +// certain cases if backend has high latency and holds multiple messages for +// long time. Bigger value impacts RAM memory usage. + +#ifndef NRF_LOG_MSGPOOL_ELEMENT_COUNT +#define NRF_LOG_MSGPOOL_ELEMENT_COUNT 8 +#endif + +// +//========================================================== + +// NRF_LOG_ALLOW_OVERFLOW - Configures behavior when circular buffer is +// full. + +// If set then oldest logs are overwritten. Otherwise a +// marker is injected informing about overflow. + +#ifndef NRF_LOG_ALLOW_OVERFLOW +#define NRF_LOG_ALLOW_OVERFLOW 1 +#endif + +// NRF_LOG_BUFSIZE - Size of the buffer for storing logs (in bytes). + +// Must be power of 2 and multiple of 4. +// If NRF_LOG_DEFERRED = 0 then buffer size can be reduced to minimum. +// <128=> 128 +// <256=> 256 +// <512=> 512 +// <1024=> 1024 +// <2048=> 2048 +// <4096=> 4096 +// <8192=> 8192 +// <16384=> 16384 + +#ifndef NRF_LOG_BUFSIZE +#define NRF_LOG_BUFSIZE 1024 +#endif + +// NRF_LOG_CLI_CMDS - Enable CLI commands for the module. + +#ifndef NRF_LOG_CLI_CMDS +#define NRF_LOG_CLI_CMDS 0 +#endif + +// NRF_LOG_DEFAULT_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_LOG_DEFAULT_LEVEL +#define NRF_LOG_DEFAULT_LEVEL 4 +#endif + +// NRF_LOG_DEFERRED - Enable deffered logger. + +// Log data is buffered and can be processed in idle. + +#ifndef NRF_LOG_DEFERRED +#define NRF_LOG_DEFERRED 0 +#endif + +// NRF_LOG_FILTERS_ENABLED - Enable dynamic filtering of logs. + +#ifndef NRF_LOG_FILTERS_ENABLED +#define NRF_LOG_FILTERS_ENABLED 0 +#endif + +// NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED - Enable use of critical +// region for non deffered mode when flushing logs. + +// When enabled NRF_LOG_FLUSH is called from critical section when non +// deffered mode is used. Log output will never be corrupted as access to +// the log backend is exclusive but system will spend significant amount of +// time in critical section + +#ifndef NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED +#define NRF_LOG_NON_DEFFERED_CRITICAL_REGION_ENABLED 0 +#endif + +// NRF_LOG_STR_PUSH_BUFFER_SIZE - Size of the buffer dedicated for strings +// stored using @ref NRF_LOG_PUSH. + +// <16=> 16 +// <32=> 32 +// <64=> 64 +// <128=> 128 +// <256=> 256 +// <512=> 512 +// <1024=> 1024 + +#ifndef NRF_LOG_STR_PUSH_BUFFER_SIZE +#define NRF_LOG_STR_PUSH_BUFFER_SIZE 128 +#endif + +// NRF_LOG_STR_PUSH_BUFFER_SIZE - Size of the buffer dedicated for strings +// stored using @ref NRF_LOG_PUSH. + +// <16=> 16 +// <32=> 32 +// <64=> 64 +// <128=> 128 +// <256=> 256 +// <512=> 512 +// <1024=> 1024 + +#ifndef NRF_LOG_STR_PUSH_BUFFER_SIZE +#define NRF_LOG_STR_PUSH_BUFFER_SIZE 128 +#endif + +// NRF_LOG_USES_COLORS - If enabled then ANSI escape code for colors is +// prefixed to every string +//========================================================== +#ifndef NRF_LOG_USES_COLORS +#define NRF_LOG_USES_COLORS 0 +#endif +// NRF_LOG_COLOR_DEFAULT - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_LOG_COLOR_DEFAULT +#define NRF_LOG_COLOR_DEFAULT 0 +#endif + +// NRF_LOG_ERROR_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_LOG_ERROR_COLOR +#define NRF_LOG_ERROR_COLOR 2 +#endif + +// NRF_LOG_WARNING_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_LOG_WARNING_COLOR +#define NRF_LOG_WARNING_COLOR 4 +#endif + +// + +// NRF_LOG_USES_TIMESTAMP - Enable timestamping + +// Function for getting the timestamp is provided by the user +//========================================================== +#ifndef NRF_LOG_USES_TIMESTAMP +#define NRF_LOG_USES_TIMESTAMP 0 +#endif +// NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY - Default frequency of the timestamp +// (in Hz) or 0 to use app_timer frequency. +#ifndef NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY +#define NRF_LOG_TIMESTAMP_DEFAULT_FREQUENCY 0 +#endif + +// + +// nrf_log module configuration + +//========================================================== +// nrf_log in nRF_Core + +//========================================================== +// NRF_MPU_LIB_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_MPU_LIB_CONFIG_LOG_ENABLED +#define NRF_MPU_LIB_CONFIG_LOG_ENABLED 0 +#endif +// NRF_MPU_LIB_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_MPU_LIB_CONFIG_LOG_LEVEL +#define NRF_MPU_LIB_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_MPU_LIB_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_MPU_LIB_CONFIG_INFO_COLOR +#define NRF_MPU_LIB_CONFIG_INFO_COLOR 0 +#endif + +// NRF_MPU_LIB_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_MPU_LIB_CONFIG_DEBUG_COLOR +#define NRF_MPU_LIB_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_STACK_GUARD_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_STACK_GUARD_CONFIG_LOG_ENABLED +#define NRF_STACK_GUARD_CONFIG_LOG_ENABLED 0 +#endif +// NRF_STACK_GUARD_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_STACK_GUARD_CONFIG_LOG_LEVEL +#define NRF_STACK_GUARD_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_STACK_GUARD_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_STACK_GUARD_CONFIG_INFO_COLOR +#define NRF_STACK_GUARD_CONFIG_INFO_COLOR 0 +#endif + +// NRF_STACK_GUARD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_STACK_GUARD_CONFIG_DEBUG_COLOR +#define NRF_STACK_GUARD_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// TASK_MANAGER_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef TASK_MANAGER_CONFIG_LOG_ENABLED +#define TASK_MANAGER_CONFIG_LOG_ENABLED 0 +#endif +// TASK_MANAGER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef TASK_MANAGER_CONFIG_LOG_LEVEL +#define TASK_MANAGER_CONFIG_LOG_LEVEL 3 +#endif + +// TASK_MANAGER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TASK_MANAGER_CONFIG_INFO_COLOR +#define TASK_MANAGER_CONFIG_INFO_COLOR 0 +#endif + +// TASK_MANAGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TASK_MANAGER_CONFIG_DEBUG_COLOR +#define TASK_MANAGER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// +//========================================================== + +// nrf_log in nRF_Drivers + +//========================================================== +// CLOCK_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef CLOCK_CONFIG_LOG_ENABLED +#define CLOCK_CONFIG_LOG_ENABLED 0 +#endif +// CLOCK_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef CLOCK_CONFIG_LOG_LEVEL +#define CLOCK_CONFIG_LOG_LEVEL 3 +#endif + +// CLOCK_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef CLOCK_CONFIG_INFO_COLOR +#define CLOCK_CONFIG_INFO_COLOR 0 +#endif + +// CLOCK_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef CLOCK_CONFIG_DEBUG_COLOR +#define CLOCK_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// COMP_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef COMP_CONFIG_LOG_ENABLED +#define COMP_CONFIG_LOG_ENABLED 0 +#endif +// COMP_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef COMP_CONFIG_LOG_LEVEL +#define COMP_CONFIG_LOG_LEVEL 3 +#endif + +// COMP_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef COMP_CONFIG_INFO_COLOR +#define COMP_CONFIG_INFO_COLOR 0 +#endif + +// COMP_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef COMP_CONFIG_DEBUG_COLOR +#define COMP_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// GPIOTE_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef GPIOTE_CONFIG_LOG_ENABLED +#define GPIOTE_CONFIG_LOG_ENABLED 0 +#endif +// GPIOTE_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef GPIOTE_CONFIG_LOG_LEVEL +#define GPIOTE_CONFIG_LOG_LEVEL 3 +#endif + +// GPIOTE_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef GPIOTE_CONFIG_INFO_COLOR +#define GPIOTE_CONFIG_INFO_COLOR 0 +#endif + +// GPIOTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef GPIOTE_CONFIG_DEBUG_COLOR +#define GPIOTE_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// LPCOMP_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef LPCOMP_CONFIG_LOG_ENABLED +#define LPCOMP_CONFIG_LOG_ENABLED 0 +#endif +// LPCOMP_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef LPCOMP_CONFIG_LOG_LEVEL +#define LPCOMP_CONFIG_LOG_LEVEL 3 +#endif + +// LPCOMP_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef LPCOMP_CONFIG_INFO_COLOR +#define LPCOMP_CONFIG_INFO_COLOR 0 +#endif + +// LPCOMP_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef LPCOMP_CONFIG_DEBUG_COLOR +#define LPCOMP_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// MAX3421E_HOST_CONFIG_LOG_ENABLED - Enable logging in the module +//========================================================== +#ifndef MAX3421E_HOST_CONFIG_LOG_ENABLED +#define MAX3421E_HOST_CONFIG_LOG_ENABLED 0 +#endif +// MAX3421E_HOST_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef MAX3421E_HOST_CONFIG_LOG_LEVEL +#define MAX3421E_HOST_CONFIG_LOG_LEVEL 3 +#endif + +// MAX3421E_HOST_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef MAX3421E_HOST_CONFIG_INFO_COLOR +#define MAX3421E_HOST_CONFIG_INFO_COLOR 0 +#endif + +// MAX3421E_HOST_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef MAX3421E_HOST_CONFIG_DEBUG_COLOR +#define MAX3421E_HOST_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRFX_USBD_CONFIG_LOG_ENABLED - Enable logging in the module +//========================================================== +#ifndef NRFX_USBD_CONFIG_LOG_ENABLED +#define NRFX_USBD_CONFIG_LOG_ENABLED 0 +#endif +// NRFX_USBD_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRFX_USBD_CONFIG_LOG_LEVEL +#define NRFX_USBD_CONFIG_LOG_LEVEL 3 +#endif + +// NRFX_USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_USBD_CONFIG_INFO_COLOR +#define NRFX_USBD_CONFIG_INFO_COLOR 0 +#endif + +// NRFX_USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRFX_USBD_CONFIG_DEBUG_COLOR +#define NRFX_USBD_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// PDM_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef PDM_CONFIG_LOG_ENABLED +#define PDM_CONFIG_LOG_ENABLED 0 +#endif +// PDM_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef PDM_CONFIG_LOG_LEVEL +#define PDM_CONFIG_LOG_LEVEL 3 +#endif + +// PDM_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PDM_CONFIG_INFO_COLOR +#define PDM_CONFIG_INFO_COLOR 0 +#endif + +// PDM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PDM_CONFIG_DEBUG_COLOR +#define PDM_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// PPI_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef PPI_CONFIG_LOG_ENABLED +#define PPI_CONFIG_LOG_ENABLED 0 +#endif +// PPI_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef PPI_CONFIG_LOG_LEVEL +#define PPI_CONFIG_LOG_LEVEL 3 +#endif + +// PPI_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PPI_CONFIG_INFO_COLOR +#define PPI_CONFIG_INFO_COLOR 0 +#endif + +// PPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PPI_CONFIG_DEBUG_COLOR +#define PPI_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// PWM_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef PWM_CONFIG_LOG_ENABLED +#define PWM_CONFIG_LOG_ENABLED 0 +#endif +// PWM_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef PWM_CONFIG_LOG_LEVEL +#define PWM_CONFIG_LOG_LEVEL 3 +#endif + +// PWM_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PWM_CONFIG_INFO_COLOR +#define PWM_CONFIG_INFO_COLOR 0 +#endif + +// PWM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PWM_CONFIG_DEBUG_COLOR +#define PWM_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// QDEC_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef QDEC_CONFIG_LOG_ENABLED +#define QDEC_CONFIG_LOG_ENABLED 0 +#endif +// QDEC_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef QDEC_CONFIG_LOG_LEVEL +#define QDEC_CONFIG_LOG_LEVEL 3 +#endif + +// QDEC_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef QDEC_CONFIG_INFO_COLOR +#define QDEC_CONFIG_INFO_COLOR 0 +#endif + +// QDEC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef QDEC_CONFIG_DEBUG_COLOR +#define QDEC_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// RNG_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef RNG_CONFIG_LOG_ENABLED +#define RNG_CONFIG_LOG_ENABLED 0 +#endif +// RNG_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef RNG_CONFIG_LOG_LEVEL +#define RNG_CONFIG_LOG_LEVEL 3 +#endif + +// RNG_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef RNG_CONFIG_INFO_COLOR +#define RNG_CONFIG_INFO_COLOR 0 +#endif + +// RNG_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef RNG_CONFIG_DEBUG_COLOR +#define RNG_CONFIG_DEBUG_COLOR 0 +#endif + +// RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED - Enables logging of random +// numbers. + +#ifndef RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED +#define RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED 0 +#endif + +// + +// RTC_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef RTC_CONFIG_LOG_ENABLED +#define RTC_CONFIG_LOG_ENABLED 0 +#endif +// RTC_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef RTC_CONFIG_LOG_LEVEL +#define RTC_CONFIG_LOG_LEVEL 3 +#endif + +// RTC_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef RTC_CONFIG_INFO_COLOR +#define RTC_CONFIG_INFO_COLOR 0 +#endif + +// RTC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef RTC_CONFIG_DEBUG_COLOR +#define RTC_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// SAADC_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef SAADC_CONFIG_LOG_ENABLED +#define SAADC_CONFIG_LOG_ENABLED 0 +#endif +// SAADC_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef SAADC_CONFIG_LOG_LEVEL +#define SAADC_CONFIG_LOG_LEVEL 3 +#endif + +// SAADC_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SAADC_CONFIG_INFO_COLOR +#define SAADC_CONFIG_INFO_COLOR 0 +#endif + +// SAADC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SAADC_CONFIG_DEBUG_COLOR +#define SAADC_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// SPIS_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef SPIS_CONFIG_LOG_ENABLED +#define SPIS_CONFIG_LOG_ENABLED 0 +#endif +// SPIS_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef SPIS_CONFIG_LOG_LEVEL +#define SPIS_CONFIG_LOG_LEVEL 3 +#endif + +// SPIS_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SPIS_CONFIG_INFO_COLOR +#define SPIS_CONFIG_INFO_COLOR 0 +#endif + +// SPIS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SPIS_CONFIG_DEBUG_COLOR +#define SPIS_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// SPI_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef SPI_CONFIG_LOG_ENABLED +#define SPI_CONFIG_LOG_ENABLED 0 +#endif +// SPI_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef SPI_CONFIG_LOG_LEVEL +#define SPI_CONFIG_LOG_LEVEL 3 +#endif + +// SPI_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SPI_CONFIG_INFO_COLOR +#define SPI_CONFIG_INFO_COLOR 0 +#endif + +// SPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SPI_CONFIG_DEBUG_COLOR +#define SPI_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef TIMER_CONFIG_LOG_ENABLED +#define TIMER_CONFIG_LOG_ENABLED 0 +#endif +// TIMER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef TIMER_CONFIG_LOG_LEVEL +#define TIMER_CONFIG_LOG_LEVEL 3 +#endif + +// TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TIMER_CONFIG_INFO_COLOR +#define TIMER_CONFIG_INFO_COLOR 0 +#endif + +// TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TIMER_CONFIG_DEBUG_COLOR +#define TIMER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// TWIS_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef TWIS_CONFIG_LOG_ENABLED +#define TWIS_CONFIG_LOG_ENABLED 0 +#endif +// TWIS_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef TWIS_CONFIG_LOG_LEVEL +#define TWIS_CONFIG_LOG_LEVEL 3 +#endif + +// TWIS_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TWIS_CONFIG_INFO_COLOR +#define TWIS_CONFIG_INFO_COLOR 0 +#endif + +// TWIS_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TWIS_CONFIG_DEBUG_COLOR +#define TWIS_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// TWI_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef TWI_CONFIG_LOG_ENABLED +#define TWI_CONFIG_LOG_ENABLED 0 +#endif +// TWI_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef TWI_CONFIG_LOG_LEVEL +#define TWI_CONFIG_LOG_LEVEL 3 +#endif + +// TWI_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TWI_CONFIG_INFO_COLOR +#define TWI_CONFIG_INFO_COLOR 0 +#endif + +// TWI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef TWI_CONFIG_DEBUG_COLOR +#define TWI_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// UART_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef UART_CONFIG_LOG_ENABLED +#define UART_CONFIG_LOG_ENABLED 0 +#endif +// UART_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef UART_CONFIG_LOG_LEVEL +#define UART_CONFIG_LOG_LEVEL 3 +#endif + +// UART_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef UART_CONFIG_INFO_COLOR +#define UART_CONFIG_INFO_COLOR 0 +#endif + +// UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef UART_CONFIG_DEBUG_COLOR +#define UART_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// USBD_CONFIG_LOG_ENABLED - Enable logging in the module +//========================================================== +#ifndef USBD_CONFIG_LOG_ENABLED +#define USBD_CONFIG_LOG_ENABLED 0 +#endif +// USBD_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef USBD_CONFIG_LOG_LEVEL +#define USBD_CONFIG_LOG_LEVEL 3 +#endif + +// USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef USBD_CONFIG_INFO_COLOR +#define USBD_CONFIG_INFO_COLOR 0 +#endif + +// USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef USBD_CONFIG_DEBUG_COLOR +#define USBD_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// WDT_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef WDT_CONFIG_LOG_ENABLED +#define WDT_CONFIG_LOG_ENABLED 0 +#endif +// WDT_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef WDT_CONFIG_LOG_LEVEL +#define WDT_CONFIG_LOG_LEVEL 3 +#endif + +// WDT_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef WDT_CONFIG_INFO_COLOR +#define WDT_CONFIG_INFO_COLOR 0 +#endif + +// WDT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef WDT_CONFIG_DEBUG_COLOR +#define WDT_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// +//========================================================== + +// nrf_log in nRF_Libraries + +//========================================================== +// APP_BUTTON_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef APP_BUTTON_CONFIG_LOG_ENABLED +#define APP_BUTTON_CONFIG_LOG_ENABLED 0 +#endif +// APP_BUTTON_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_BUTTON_CONFIG_LOG_LEVEL +#define APP_BUTTON_CONFIG_LOG_LEVEL 3 +#endif + +// APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic +// filtering is enabled. + +// If module generates a lot of logs, initial log level can +// be decreased to prevent flooding. Severity level can be +// increased on instance basis. +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL +#define APP_BUTTON_CONFIG_INITIAL_LOG_LEVEL 3 +#endif + +// APP_BUTTON_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_BUTTON_CONFIG_INFO_COLOR +#define APP_BUTTON_CONFIG_INFO_COLOR 0 +#endif + +// APP_BUTTON_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_BUTTON_CONFIG_DEBUG_COLOR +#define APP_BUTTON_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_TIMER_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef APP_TIMER_CONFIG_LOG_ENABLED +#define APP_TIMER_CONFIG_LOG_ENABLED 0 +#endif +// APP_TIMER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_TIMER_CONFIG_LOG_LEVEL +#define APP_TIMER_CONFIG_LOG_LEVEL 3 +#endif + +// APP_TIMER_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic +// filtering is enabled. + +// If module generates a lot of logs, initial log level can +// be decreased to prevent flooding. Severity level can be +// increased on instance basis. +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_TIMER_CONFIG_INITIAL_LOG_LEVEL +#define APP_TIMER_CONFIG_INITIAL_LOG_LEVEL 3 +#endif + +// APP_TIMER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_TIMER_CONFIG_INFO_COLOR +#define APP_TIMER_CONFIG_INFO_COLOR 0 +#endif + +// APP_TIMER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_TIMER_CONFIG_DEBUG_COLOR +#define APP_TIMER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED +#define APP_USBD_CDC_ACM_CONFIG_LOG_ENABLED 0 +#endif +// APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL +#define APP_USBD_CDC_ACM_CONFIG_LOG_LEVEL 3 +#endif + +// APP_USBD_CDC_ACM_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_CDC_ACM_CONFIG_INFO_COLOR +#define APP_USBD_CDC_ACM_CONFIG_INFO_COLOR 0 +#endif + +// APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR +#define APP_USBD_CDC_ACM_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_USBD_CONFIG_LOG_ENABLED - Enable logging in the module. +//========================================================== +#ifndef APP_USBD_CONFIG_LOG_ENABLED +#define APP_USBD_CONFIG_LOG_ENABLED 0 +#endif +// APP_USBD_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_USBD_CONFIG_LOG_LEVEL +#define APP_USBD_CONFIG_LOG_LEVEL 3 +#endif + +// APP_USBD_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_CONFIG_INFO_COLOR +#define APP_USBD_CONFIG_INFO_COLOR 0 +#endif + +// APP_USBD_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_CONFIG_DEBUG_COLOR +#define APP_USBD_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_USBD_DUMMY_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef APP_USBD_DUMMY_CONFIG_LOG_ENABLED +#define APP_USBD_DUMMY_CONFIG_LOG_ENABLED 0 +#endif +// APP_USBD_DUMMY_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_USBD_DUMMY_CONFIG_LOG_LEVEL +#define APP_USBD_DUMMY_CONFIG_LOG_LEVEL 3 +#endif + +// APP_USBD_DUMMY_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_DUMMY_CONFIG_INFO_COLOR +#define APP_USBD_DUMMY_CONFIG_INFO_COLOR 0 +#endif + +// APP_USBD_DUMMY_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_DUMMY_CONFIG_DEBUG_COLOR +#define APP_USBD_DUMMY_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_USBD_MSC_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef APP_USBD_MSC_CONFIG_LOG_ENABLED +#define APP_USBD_MSC_CONFIG_LOG_ENABLED 0 +#endif +// APP_USBD_MSC_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_USBD_MSC_CONFIG_LOG_LEVEL +#define APP_USBD_MSC_CONFIG_LOG_LEVEL 3 +#endif + +// APP_USBD_MSC_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_MSC_CONFIG_INFO_COLOR +#define APP_USBD_MSC_CONFIG_INFO_COLOR 0 +#endif + +// APP_USBD_MSC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_MSC_CONFIG_DEBUG_COLOR +#define APP_USBD_MSC_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED - Enables logging in the +// module. +//========================================================== +#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED +#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_ENABLED 0 +#endif +// APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL +#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_LOG_LEVEL 3 +#endif + +// APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR +#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_INFO_COLOR 0 +#endif + +// APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR +#define APP_USBD_NRF_DFU_TRIGGER_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_ATFIFO_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_ATFIFO_CONFIG_LOG_ENABLED +#define NRF_ATFIFO_CONFIG_LOG_ENABLED 0 +#endif +// NRF_ATFIFO_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_ATFIFO_CONFIG_LOG_LEVEL +#define NRF_ATFIFO_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level if +// dynamic filtering is enabled + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL +#define NRF_ATFIFO_CONFIG_LOG_INIT_FILTER_LEVEL 3 +#endif + +// NRF_ATFIFO_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_ATFIFO_CONFIG_INFO_COLOR +#define NRF_ATFIFO_CONFIG_INFO_COLOR 0 +#endif + +// NRF_ATFIFO_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_ATFIFO_CONFIG_DEBUG_COLOR +#define NRF_ATFIFO_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_BALLOC_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_BALLOC_CONFIG_LOG_ENABLED +#define NRF_BALLOC_CONFIG_LOG_ENABLED 0 +#endif +// NRF_BALLOC_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BALLOC_CONFIG_LOG_LEVEL +#define NRF_BALLOC_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL - Initial severity level if dynamic +// filtering is enabled. + +// If module generates a lot of logs, initial log level can +// be decreased to prevent flooding. Severity level can be +// increased on instance basis. +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL +#define NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL 3 +#endif + +// NRF_BALLOC_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BALLOC_CONFIG_INFO_COLOR +#define NRF_BALLOC_CONFIG_INFO_COLOR 0 +#endif + +// NRF_BALLOC_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BALLOC_CONFIG_DEBUG_COLOR +#define NRF_BALLOC_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED +#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_ENABLED 0 +#endif +// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL +#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity +// level if dynamic filtering is enabled + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL +#define NRF_BLOCK_DEV_EMPTY_CONFIG_LOG_INIT_FILTER_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR +#define NRF_BLOCK_DEV_EMPTY_CONFIG_INFO_COLOR 0 +#endif + +// NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR +#define NRF_BLOCK_DEV_EMPTY_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED +#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED 0 +#endif +// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL +#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level +// if dynamic filtering is enabled + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL +#define NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR +#define NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR 0 +#endif + +// NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR +#define NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED +#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_ENABLED 0 +#endif +// NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL +#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level +// if dynamic filtering is enabled + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL +#define NRF_BLOCK_DEV_RAM_CONFIG_LOG_INIT_FILTER_LEVEL 3 +#endif + +// NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR +#define NRF_BLOCK_DEV_RAM_CONFIG_INFO_COLOR 0 +#endif + +// NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR +#define NRF_BLOCK_DEV_RAM_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED +#define NRF_CLI_BLE_UART_CONFIG_LOG_ENABLED 0 +#endif +// NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL +#define NRF_CLI_BLE_UART_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_CLI_BLE_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_BLE_UART_CONFIG_INFO_COLOR +#define NRF_CLI_BLE_UART_CONFIG_INFO_COLOR 0 +#endif + +// NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR +#define NRF_CLI_BLE_UART_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED +#define NRF_CLI_LIBUARTE_CONFIG_LOG_ENABLED 0 +#endif +// NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL +#define NRF_CLI_LIBUARTE_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR +#define NRF_CLI_LIBUARTE_CONFIG_INFO_COLOR 0 +#endif + +// NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR +#define NRF_CLI_LIBUARTE_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_CLI_UART_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_CLI_UART_CONFIG_LOG_ENABLED +#define NRF_CLI_UART_CONFIG_LOG_ENABLED 0 +#endif +// NRF_CLI_UART_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_CLI_UART_CONFIG_LOG_LEVEL +#define NRF_CLI_UART_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_CLI_UART_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_UART_CONFIG_INFO_COLOR +#define NRF_CLI_UART_CONFIG_INFO_COLOR 0 +#endif + +// NRF_CLI_UART_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_CLI_UART_CONFIG_DEBUG_COLOR +#define NRF_CLI_UART_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_LIBUARTE_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_LIBUARTE_CONFIG_LOG_ENABLED +#define NRF_LIBUARTE_CONFIG_LOG_ENABLED 0 +#endif +// NRF_LIBUARTE_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_LIBUARTE_CONFIG_LOG_LEVEL +#define NRF_LIBUARTE_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_LIBUARTE_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_LIBUARTE_CONFIG_INFO_COLOR +#define NRF_LIBUARTE_CONFIG_INFO_COLOR 0 +#endif + +// NRF_LIBUARTE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_LIBUARTE_CONFIG_DEBUG_COLOR +#define NRF_LIBUARTE_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_MEMOBJ_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_MEMOBJ_CONFIG_LOG_ENABLED +#define NRF_MEMOBJ_CONFIG_LOG_ENABLED 0 +#endif +// NRF_MEMOBJ_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_MEMOBJ_CONFIG_LOG_LEVEL +#define NRF_MEMOBJ_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_MEMOBJ_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_MEMOBJ_CONFIG_INFO_COLOR +#define NRF_MEMOBJ_CONFIG_INFO_COLOR 0 +#endif + +// NRF_MEMOBJ_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_MEMOBJ_CONFIG_DEBUG_COLOR +#define NRF_MEMOBJ_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_PWR_MGMT_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_PWR_MGMT_CONFIG_LOG_ENABLED +#define NRF_PWR_MGMT_CONFIG_LOG_ENABLED 0 +#endif +// NRF_PWR_MGMT_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_PWR_MGMT_CONFIG_LOG_LEVEL +#define NRF_PWR_MGMT_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_PWR_MGMT_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_PWR_MGMT_CONFIG_INFO_COLOR +#define NRF_PWR_MGMT_CONFIG_INFO_COLOR 0 +#endif + +// NRF_PWR_MGMT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_PWR_MGMT_CONFIG_DEBUG_COLOR +#define NRF_PWR_MGMT_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_QUEUE_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_QUEUE_CONFIG_LOG_ENABLED +#define NRF_QUEUE_CONFIG_LOG_ENABLED 0 +#endif +// NRF_QUEUE_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_QUEUE_CONFIG_LOG_LEVEL +#define NRF_QUEUE_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL - Initial severity level if +// dynamic filtering is enabled + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL +#define NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL 3 +#endif + +// NRF_QUEUE_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_QUEUE_CONFIG_INFO_COLOR +#define NRF_QUEUE_CONFIG_INFO_COLOR 0 +#endif + +// NRF_QUEUE_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_QUEUE_CONFIG_DEBUG_COLOR +#define NRF_QUEUE_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_SDH_ANT_LOG_ENABLED - Enable logging in SoftDevice handler (ANT) +// module. +//========================================================== +#ifndef NRF_SDH_ANT_LOG_ENABLED +#define NRF_SDH_ANT_LOG_ENABLED 0 +#endif +// NRF_SDH_ANT_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_SDH_ANT_LOG_LEVEL +#define NRF_SDH_ANT_LOG_LEVEL 3 +#endif + +// NRF_SDH_ANT_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_ANT_INFO_COLOR +#define NRF_SDH_ANT_INFO_COLOR 0 +#endif + +// NRF_SDH_ANT_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_ANT_DEBUG_COLOR +#define NRF_SDH_ANT_DEBUG_COLOR 0 +#endif + +// + +// NRF_SDH_BLE_LOG_ENABLED - Enable logging in SoftDevice handler (BLE) +// module. +//========================================================== +#ifndef NRF_SDH_BLE_LOG_ENABLED +#define NRF_SDH_BLE_LOG_ENABLED 0 +#endif +// NRF_SDH_BLE_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_SDH_BLE_LOG_LEVEL +#define NRF_SDH_BLE_LOG_LEVEL 3 +#endif + +// NRF_SDH_BLE_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_BLE_INFO_COLOR +#define NRF_SDH_BLE_INFO_COLOR 0 +#endif + +// NRF_SDH_BLE_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_BLE_DEBUG_COLOR +#define NRF_SDH_BLE_DEBUG_COLOR 0 +#endif + +// + +// NRF_SDH_LOG_ENABLED - Enable logging in SoftDevice handler module. +//========================================================== +#ifndef NRF_SDH_LOG_ENABLED +#define NRF_SDH_LOG_ENABLED 0 +#endif +// NRF_SDH_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_SDH_LOG_LEVEL +#define NRF_SDH_LOG_LEVEL 3 +#endif + +// NRF_SDH_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_INFO_COLOR +#define NRF_SDH_INFO_COLOR 0 +#endif + +// NRF_SDH_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_DEBUG_COLOR +#define NRF_SDH_DEBUG_COLOR 0 +#endif + +// + +// NRF_SDH_SOC_LOG_ENABLED - Enable logging in SoftDevice handler (SoC) +// module. +//========================================================== +#ifndef NRF_SDH_SOC_LOG_ENABLED +#define NRF_SDH_SOC_LOG_ENABLED 0 +#endif +// NRF_SDH_SOC_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_SDH_SOC_LOG_LEVEL +#define NRF_SDH_SOC_LOG_LEVEL 3 +#endif + +// NRF_SDH_SOC_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_SOC_INFO_COLOR +#define NRF_SDH_SOC_INFO_COLOR 0 +#endif + +// NRF_SDH_SOC_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SDH_SOC_DEBUG_COLOR +#define NRF_SDH_SOC_DEBUG_COLOR 0 +#endif + +// + +// NRF_SORTLIST_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_SORTLIST_CONFIG_LOG_ENABLED +#define NRF_SORTLIST_CONFIG_LOG_ENABLED 0 +#endif +// NRF_SORTLIST_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_SORTLIST_CONFIG_LOG_LEVEL +#define NRF_SORTLIST_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_SORTLIST_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SORTLIST_CONFIG_INFO_COLOR +#define NRF_SORTLIST_CONFIG_INFO_COLOR 0 +#endif + +// NRF_SORTLIST_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_SORTLIST_CONFIG_DEBUG_COLOR +#define NRF_SORTLIST_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// NRF_TWI_SENSOR_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef NRF_TWI_SENSOR_CONFIG_LOG_ENABLED +#define NRF_TWI_SENSOR_CONFIG_LOG_ENABLED 0 +#endif +// NRF_TWI_SENSOR_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef NRF_TWI_SENSOR_CONFIG_LOG_LEVEL +#define NRF_TWI_SENSOR_CONFIG_LOG_LEVEL 3 +#endif + +// NRF_TWI_SENSOR_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_TWI_SENSOR_CONFIG_INFO_COLOR +#define NRF_TWI_SENSOR_CONFIG_INFO_COLOR 0 +#endif + +// NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR +#define NRF_TWI_SENSOR_CONFIG_DEBUG_COLOR 0s +#endif + +// + +// PM_LOG_ENABLED - Enable logging in Peer Manager and its submodules. +//========================================================== +#ifndef PM_LOG_ENABLED +#define PM_LOG_ENABLED 1 +#endif +// PM_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef PM_LOG_LEVEL +#define PM_LOG_LEVEL 3 +#endif + +// PM_LOG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PM_LOG_INFO_COLOR +#define PM_LOG_INFO_COLOR 0 +#endif + +// PM_LOG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef PM_LOG_DEBUG_COLOR +#define PM_LOG_DEBUG_COLOR 0 +#endif + +// + +// +//========================================================== + +// nrf_log in nRF_Serialization + +//========================================================== +// SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED - Enables logging in the module. +//========================================================== +#ifndef SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED +#define SER_HAL_TRANSPORT_CONFIG_LOG_ENABLED 0 +#endif +// SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL - Default Severity level + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug + +#ifndef SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL +#define SER_HAL_TRANSPORT_CONFIG_LOG_LEVEL 3 +#endif + +// SER_HAL_TRANSPORT_CONFIG_INFO_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SER_HAL_TRANSPORT_CONFIG_INFO_COLOR +#define SER_HAL_TRANSPORT_CONFIG_INFO_COLOR 0 +#endif + +// SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR - ANSI escape code prefix. + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White + +#ifndef SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR +#define SER_HAL_TRANSPORT_CONFIG_DEBUG_COLOR 0 +#endif + +// + +// +//========================================================== + +// +//========================================================== + +// + +// NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED - nrf_log_str_formatter - +// Log string formatter + +#ifndef NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED +#define NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED 1 +#endif + +// +//========================================================== + +// nRF_Segger_RTT + +//========================================================== +// segger_rtt - SEGGER RTT + +//========================================================== +// SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer. +// Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE +// or this value is actually used. It depends on which one is bigger. + +#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP +#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 2048 +#endif + +// SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Maximum number of upstream +// buffers. +#ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS +#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2 +#endif + +// SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of downstream buffer. +#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN +#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16 +#endif + +// SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Maximum number of downstream +// buffers. +#ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS +#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2 +#endif + +// SEGGER_RTT_CONFIG_DEFAULT_MODE - RTT behavior if the buffer is full. + +// The following modes are supported: +// - SKIP - Do not block, output nothing. +// - TRIM - Do not block, output as much as fits. +// - BLOCK - Wait until there is space in the buffer. +// <0=> SKIP +// <1=> TRIM +// <2=> BLOCK_IF_FIFO_FULL + +#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE +#define SEGGER_RTT_CONFIG_DEFAULT_MODE 0 +#endif + +// +//========================================================== + +// +//========================================================== + +// <<< end of configuration section >>> +#endif // SDK_CONFIG_H diff --git a/device/lib/seal_embedded.c b/device/lib/seal_embedded.c index 750cb29..473032f 100644 --- a/device/lib/seal_embedded.c +++ b/device/lib/seal_embedded.c @@ -222,12 +222,14 @@ bool se_encrypt(SEND_FNCT_PTR network_send_function, void *v, size_t vlen_bytes, void se_cleanup(SE_PARMS *se_parms) { + se_assert(se_parms); #ifdef SE_USE_MALLOC + se_assert(se_parms->se_ptrs); delete_parameters(se_parms->parms); // -- Free the memory pool. Note that conj_vals should always point to the start. // -- TODO: Make this better - free(se_parms->se_ptrs->conj_vals); + if (se_parms->se_ptrs->conj_vals) free(se_parms->se_ptrs->conj_vals); #endif se_parms->parms = 0; } diff --git a/device/lib/seal_embedded.h b/device/lib/seal_embedded.h index 924725e..406ff61 100644 --- a/device/lib/seal_embedded.h +++ b/device/lib/seal_embedded.h @@ -3,6 +3,14 @@ /** @file seal_embedded.h + +SEAL-Embedded offers an easy to use API for easy development. A user should call the se_setup +function once at the start of library initialization and se_encrypt once for every array of values +to encode/encrypt. 'se_cleanup' exists for completeness to free library memory, but should never +need to be called. SEAL-Embedded offers three types of setup functions for developers, providing +different degrees of library configurability. se_setup_custom allows for the most customization, +while se_setup_default uses default parameter settings. Note: Currently, SEAL-Embedded does not +support reconfiguring parameters to a different parameter set after initial setup. */ #pragma once @@ -22,6 +30,7 @@ extern "C" { /** Error code values range from 0 to -9999. Application-specific errors should use values outside this range. +TODO: Use error codes */ #define SE_SUCCESS 0 #define SE_ERR_NO_MEMORY -12 @@ -56,45 +65,17 @@ The second input parameter should represent the size of the data to send. typedef size_t (*SEND_FNCT_PTR)(void *, size_t); /** -Randomness generator function pointer -The first input parameter should represent a pointer to the buffer to store the random -values. The second input parameter should represent the size of randomness to generate. +Randomness generator function pointer. +The first input parameter should represent a pointer to the buffer to store the random values. +The second input parameter should represent the size of randomness to generate. The third input parameters can specify any required flags. */ typedef ssize_t (*RND_FNCT_PTR)(void *, size_t, unsigned int flags); -/* -(Modified from SEAL:) A larger coeff_modulus implies a larger noise budget, hence more -encrypted computation capabilities. However, an upper bound for the total bit-length of -the coeff_modulus is determined by the degree, as follows: - +--------------------------------------------------------+ - | degree | max coeff_modulus | max nprimes | max nprimes | - | | bit-length | (30 bit) | (25 bit) | - +--------------------------------------------------------+ - | 1024 | 27 | 0 | 1 - | 2048 | 54 | 1 | 2 - | 4096 | 109 | 3 | 4 - | 8192 | 218 | 7 | 8 - | 16384 | 438 | 14 | 16 - | 32768 | 881 | 29 | 32 - +--------------------------------------------------------+ -*/ - /** -SEAL-Embedded offers an easy to use API for easy development. A user should call the -se_setup function once at the start of library initialization and se_encrypt once for -every array of values to encode/encrypt. 'se_cleanup' exists for completeness to free -library memory, but should never need to be called. SEAL-Embedded offers three types of -setup functions for developers, providing different degrees of library configurability. -se_setup_custom allows for the most customization, while se_setup_default uses default -parameter settings. Note: Currently, SEAL-Embedded does not support reconfiguring -parameters to a different parameter set after initial setup. -*/ - -/** -Setups up SEAL-Embedded for a particular encryption type for a custom parameter set, -including a custom degree, number of modulus primes, modulus prime values, and scale. If -either modulus_vals or ratios is NULL, reverts to se_setup functionality. +Setups up SEAL-Embedded for a particular encryption type for a custom parameter set, including a +custom degree, number of modulus primes, modulus prime values, and scale. If either modulus_vals or +ratios is NULL, reverts to se_setup functionality. Note: This function calls calloc. @@ -102,13 +83,13 @@ Note: This function calls calloc. @param[in] nprimes Number of prime moduli @param[in] modulus_vals An array of nprimes type-ZZ modulus values. @param[in] ratios An array of const_ratio values for each custom modulus value -(high word, followed by low word). + (high word, followed by low word). @param[in] scale Scale -@param[in] enc_type Encryption type (symMETRIC or AsymMETRIC) +@param[in] enc_type Encryption type @returns A handle to the set SE_PARMS instance */ -SE_PARMS *se_setup_custom(size_t degree, size_t nprimes, const ZZ *modulus_vals, - const ZZ *ratios, double scale, EncryptType encrypt_type); +SE_PARMS *se_setup_custom(size_t degree, size_t nprimes, const ZZ *modulus_vals, const ZZ *ratios, + double scale, EncryptType encrypt_type); /** Setups up SEAL-Embedded for a particular encryption type for the requested parameter set. @@ -118,14 +99,14 @@ Note: This function calls calloc. @param[in] degree Polynomial ring degree @param[in] nprimes Number of prime moduli @param[in] scale Scale -@param[in] enc_type Encryption type (symMETRIC or AsymMETRIC) +@param[in] enc_type Encryption type @returns A handle to an SE_PARMS instance */ SE_PARMS *se_setup(size_t degree, size_t nprimes, double scale, EncryptType encrypt_type); /** -Setups up SEAL-Embedded for a particular encryption type for a parameter set of degree = -4096, a coefficient modulus of 3 30-bit primes, and a scale of pow(2,25). +Setups up SEAL-Embedded for a particular encryption type for a parameter set of degree = 4096, a +coefficient modulus of 3 30-bit primes, and a scale of pow(2,25). Note: This function calls calloc. @@ -134,16 +115,16 @@ Note: This function calls calloc. */ SE_PARMS *se_setup_default(EncryptType enc_type); -bool se_encrypt_seeded(uint8_t *shareable_seed, uint8_t *seed, - SEND_FNCT_PTR network_send_function, void *v, size_t vlen_bytes, - bool print, SE_PARMS *se_parms); +bool se_encrypt_seeded(uint8_t *shareable_seed, uint8_t *seed, SEND_FNCT_PTR network_send_function, + void *v, size_t vlen_bytes, bool print, SE_PARMS *se_parms); -bool se_encrypt(SEND_FNCT_PTR network_send_function, void *v, size_t vlen_bytes, - bool print, SE_PARMS *se_parms); +bool se_encrypt(SEND_FNCT_PTR network_send_function, void *v, size_t vlen_bytes, bool print, + SE_PARMS *se_parms); /** -Frees some library memory and resets parameters object. Should never need to be called by -the typical user. +Frees some library memory and resets parameters object. Should never need to be called by the +typical user. + @param[in] se_parms SE_PARMS instance to free */ void se_cleanup(SE_PARMS *se_parms); diff --git a/device/lib/shake256/fips202.c b/device/lib/shake256/fips202.c index eacb1ef..f5446e7 100644 --- a/device/lib/shake256/fips202.c +++ b/device/lib/shake256/fips202.c @@ -41,16 +41,14 @@ typedef struct * Description: Absorb step of Keccak; * non-incremental, starts by zeroeing the state. * - * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak - *state + * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) * - const uint8_t *m: pointer to input to be absorbed into s * - size_t mlen: length of input in bytes * - uint8_t p: domain-separation byte for different *Keccak-derived functions **************************************************/ -static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m, size_t mlen, - uint8_t p) +static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m, size_t mlen, uint8_t p) { size_t i; uint8_t t[200]; @@ -75,8 +73,8 @@ static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m, size_t mlen * Name: keccak_squeezeblocks * * Description: Squeeze step of Keccak. Squeezes full blocks of r bytes each. - * Modifies the state. Can be called multiple times to keep - *squeezing, i.e., is incremental. + * Modifies the state. Can be called multiple times to keep squeezing, i.e., is + * incremental. * * Arguments: - uint8_t *h: pointer to output blocks * - size_t nblocks: number of blocks to be squeezed (written to h) diff --git a/device/lib/shake256/keccakf1600.c b/device/lib/shake256/keccakf1600.c index 3ebfcf2..8c24196 100644 --- a/device/lib/shake256/keccakf1600.c +++ b/device/lib/shake256/keccakf1600.c @@ -32,25 +32,20 @@ static const uint64_t KeccakF_RoundConstants[NROUNDS] = { (uint64_t)0x8000000080008081ULL, (uint64_t)0x8000000000008080ULL, (uint64_t)0x0000000080000001ULL, (uint64_t)0x8000000080008008ULL}; -void KeccakF1600_StateExtractBytes(uint64_t *state, unsigned char *data, - unsigned int offset, unsigned int length) +void KeccakF1600_StateExtractBytes(uint64_t *state, unsigned char *data, unsigned int offset, + unsigned int length) { unsigned int i; for (i = 0; i < length; i++) - { - data[i] = - (unsigned char)(state[(offset + i) >> 3] >> (8 * ((offset + i) & 0x07))); - } + { data[i] = (unsigned char)(state[(offset + i) >> 3] >> (8 * ((offset + i) & 0x07))); } } -void KeccakF1600_StateXORBytes(uint64_t *state, const unsigned char *data, - unsigned int offset, unsigned int length) +void KeccakF1600_StateXORBytes(uint64_t *state, const unsigned char *data, unsigned int offset, + unsigned int length) { unsigned int i; for (i = 0; i < length; i++) - { - state[(offset + i) >> 3] ^= (uint64_t)data[i] << (8 * ((offset + i) & 0x07)); - } + { state[(offset + i) >> 3] ^= (uint64_t)data[i] << (8 * ((offset + i) & 0x07)); } } void KeccakF1600_StatePermute(uint64_t *state) diff --git a/device/lib/shake256/keccakf1600.h b/device/lib/shake256/keccakf1600.h index a50ed2c..dcd1924 100644 --- a/device/lib/shake256/keccakf1600.h +++ b/device/lib/shake256/keccakf1600.h @@ -8,10 +8,10 @@ Origin: pqm4 library (https://github.com/mupq/pqm4) commit #include -void KeccakF1600_StateExtractBytes(uint64_t *state, unsigned char *data, - unsigned int offset, unsigned int length); -void KeccakF1600_StateXORBytes(uint64_t *state, const unsigned char *data, - unsigned int offset, unsigned int length); +void KeccakF1600_StateExtractBytes(uint64_t *state, unsigned char *data, unsigned int offset, + unsigned int length); +void KeccakF1600_StateXORBytes(uint64_t *state, const unsigned char *data, unsigned int offset, + unsigned int length); void KeccakF1600_StatePermute(uint64_t *state); #endif diff --git a/device/lib/timer.c b/device/lib/timer.c index 73d555c..fd7344b 100644 --- a/device/lib/timer.c +++ b/device/lib/timer.c @@ -112,8 +112,7 @@ void start_timer(Timer *timer) } /** -Helper function to calculated the elapsed time. Should only be called after timer has been -stopped. +Helper function to calculated the elapsed time. Should only be called after timer has been stopped. @param[in] start Starting counter value @param[in] stop Ending counter value @@ -194,8 +193,7 @@ void start_timer(Timer *timer) { // nrfx_timer_event_handler_t event_handler = &se_nrf5_timer_timeout_func; nrfx_timer_event_handler_t event_handler = se_nrf5_timer_timeout_func; - // -- Note that a different frequency value would require a change to - // calc_elapsed_time + // -- Note that a different frequency value would require a change to calc_elapsed_time timer->timer_instance = &timer_instance_global; // (nrfx_timer_t)NRFX_TIMER_INSTANCE(0); timer->timer_config = &timer_config_global; // (nrfx_timer_config_t)NRFX_TIMER_DEFAULT_CONFIG; @@ -292,8 +290,7 @@ void start_timer(Timer *timer) } /** -Helper function to calculated the elapsed time. Should only be called after timer has been -stopped. +Helper function to calculated the elapsed time. Should only be called after timer has been stopped. @param[in] start Starting counter value @param[in] stop Ending counter value @@ -330,8 +327,7 @@ void start_timer(Timer *timer) } /** -Helper function to calculated the elapsed time. Should only be called after timer has been -stopped. +Helper function to calculated the elapsed time. Should only be called after timer has been stopped. @param[in] start Starting counter value @param[in] stop Ending counter value @@ -391,7 +387,7 @@ Returns the elapsed time of the timer in the requested unit of time. @param[in] unit Time unit @returns The elapsed time of the timer in the requested unit */ -float read_timer(Timer timer, TimeUnit unit) +volatile float read_timer(Timer timer, TimeUnit unit) { return timer.elapsed_time / (float)(NANO_SEC / unit); } diff --git a/device/lib/timer.h b/device/lib/timer.h index c582aa9..8e41087 100644 --- a/device/lib/timer.h +++ b/device/lib/timer.h @@ -4,8 +4,8 @@ /** @file timer.h -Timer struct and timing functions. Mainly useful for testing. We leave it as part of the -library in case we want to time internal functions. +Timer struct and timing functions. Mainly useful for testing. We leave it as part of the library in +case we want to time internal functions. */ #pragma once @@ -13,22 +13,22 @@ library in case we want to time internal functions. #include "defines.h" #ifdef SE_ENABLE_TIMERS - #include +#include - #ifdef SE_ON_SPHERE_M4 - #include "os_hal_gpt.h" +#ifdef SE_ON_SPHERE_M4 +#include "os_hal_gpt.h" // -- We set these to the clock we want to use // There are 5 GPT clocks: {0, 1, 3} --> interrupt based, {2, 4} --> free-run // GPT2 ~= 32KHz or 1Hz, GPT4 = ~= bus clock speed or (1/2)*bus clockspeed static const uint8_t global_gpt_timer_id = OS_HAL_GPT4; static const bool global_gpt_high_speed = 1; - #elif defined(SE_ON_NRF5) - #include "nrf.h" - #include "nrf_drv_timer.h" // TODO: Check if this is needed - #include "sdk_config.h" - #elif !defined(SE_ON_SPHERE_A7) - #include - #endif +#elif defined(SE_ON_NRF5) +#include "nrf.h" +#include "nrf_drv_timer.h" // TODO: Check if this is needed +#include "sdk_config.h" +#elif !defined(SE_ON_SPHERE_A7) +#include +#endif /** Struct for storing time points. @@ -36,28 +36,26 @@ Struct for storing time points. @param start Starting time @param stop End time @param elapsed_time Elapsed time (start-stop) in nanoseconds -@param timer_instance Timer instance (Used only with SE_ON_NRF5). Equal to -NRFX_TIMER_INSTANCE(0) -@param timer_config Timer config (Used only with SE_ON_NRF5). Equal to -NRFX_TIMER_DEFAULT_CONFIG +@param timer_instance Timer instance (Used only with SE_ON_NRF5). Equal to NRFX_TIMER_INSTANCE(0) +@param timer_config Timer config (Used only with SE_ON_NRF5). Equal to NRFX_TIMER_DEFAULT_CONFIG */ typedef struct Timer { - #if defined(SE_ON_SPHERE_M4) || defined(SE_ON_NRF5) +#if defined(SE_ON_SPHERE_M4) || defined(SE_ON_NRF5) uint32_t start; // Starting time uint32_t stop; // End time - #elif defined(SE_ON_SPHERE_A7) +#elif defined(SE_ON_SPHERE_A7) uint64_t start; // Starting time uint64_t stop; // End time - #else +#else struct timespec start; // Starting time struct timespec stop; // End time - #endif +#endif float elapsed_time; // Elapsed time (start-stop) in nanoseconds - #ifdef SE_ON_NRF5 +#ifdef SE_ON_NRF5 nrfx_timer_t *timer_instance; // Timer instance (= NRFX_TIMER_INSTANCE(0)) nrfx_timer_config_t *timer_config; // Timer config (= NRFX_TIMER_DEFAULT_CONFIG) - #endif +#endif } Timer; typedef enum TimeUnit { diff --git a/device/lib/uintmodarith.h b/device/lib/uintmodarith.h index 813fe54..9654f52 100644 --- a/device/lib/uintmodarith.h +++ b/device/lib/uintmodarith.h @@ -26,6 +26,7 @@ Modular addition. Correctness: (op1 + op2) <= (2q - 1). static inline ZZ add_mod(ZZ op1, ZZ op2, const Modulus *q) { ZZ q_val = q->value; + se_assert((op1 + op2) <= (2 * q_val - 1)); // -- We know that the sum can fit into a uint32: // max(uint32_t) = 2^32 - 1 @@ -49,6 +50,7 @@ In-place modular addition. Correctness: (op1 + op2) <= (2q - 1). */ static inline void add_mod_inpl(ZZ *op1, ZZ op2, const Modulus *q) { + se_assert(op1 && ((op1[0] + op2) <= (2 * q->value - 1))); op1[0] = add_mod(op1[0], op2, q); } @@ -61,6 +63,7 @@ Modular negation. */ static inline ZZ neg_mod(ZZ op, const Modulus *q) { + se_assert(op <= q->value); ZZsign non_zero = (ZZsign)(op != 0); ZZ mask = (ZZ)(-non_zero); @@ -77,6 +80,7 @@ In-place modular negation. */ static inline void neg_mod_inpl(ZZ *op, const Modulus *q) { + se_assert(op && op[0] <= q->value); op[0] = neg_mod(op[0], q); } @@ -90,6 +94,7 @@ Modular subtraction. */ static inline ZZ sub_mod(ZZ op1, ZZ op2, const Modulus *q) { + se_assert(op1 <= q->value && op2 <= q->value); ZZ negated = neg_mod(op2, q); return add_mod(op1, negated, q); } @@ -103,6 +108,7 @@ In-place modular subtraction. */ static inline void sub_mod_inpl(ZZ *op1, ZZ op2, const Modulus *q) { + se_assert(op1 && op1[0] <= q->value && op2 <= q->value); add_mod_inpl(op1, neg_mod(op2, q), q); } @@ -130,6 +136,7 @@ In-place modular multiplication using Barrett reduction. */ static inline void mul_mod_inpl(ZZ *op1, ZZ op2, const Modulus *q) { + se_assert(op1); op1[0] = mul_mod(op1[0], op2, q); } @@ -148,8 +155,8 @@ static inline ZZ mul_add_mod(ZZ op1, ZZ op2, ZZ op3, const Modulus *q) } /** -In-place modular multiplication of two values followed by a modular addition of -a third value. +In-place modular multiplication of two values followed by a modular addition of a third value. + @param[in,out] op1 In: Operand 1; Out: (op1 + (op2 * op3) mod q) mod q @param[in] op2 Operand 2 @param[in] op3 Operand 3 @@ -161,9 +168,8 @@ static inline void mul_add_mod_inpl(ZZ *op1, ZZ op2, ZZ op3, const Modulus *q) } /** -Exponentiates a ZZ-type value with respect to a modulus. Prior to exponentiation, -bit-reverses the value. May potentially be faster than calling bit-reverse first and -exponentiate_uint_mod after. +Exponentiates a ZZ-type value with respect to a modulus. Prior to exponentiation, bit-reverses the +value. May potentially be faster than calling bit-reverse first and exponentiate_uint_mod after. @param[in] operand Value to exponentiate @param[in] exponent Exponent to raise operand to @@ -241,7 +247,7 @@ static inline ZZ exponentiate_uint_mod(ZZ operand, ZZ exponent, const Modulus *m ZZ intermediate = 1; // print_zz("exponent begin", exponent); - // Initially: power = operand and intermediate = 1, product is irrelevant. + // -- Initially: power = operand and intermediate = 1, product is irrelevant. while (true) { if (exponent & 1) @@ -291,12 +297,12 @@ typedef struct MUMO } MUMO; /** -Modular mult. using a highly-optimized variant of Barrett reduction. Reduces result to [0, -2q- 1]. Correctness: q <= 63-bit, y < q. +Modular mult. using a highly-optimized variant of Barrett reduction. Reduces result to [0, 2q- 1]. +Correctness: q <= 31-bit, y < q. @param[in] x Operand 1 @param[in] y Operand 2 -@param[in] q Modulus. Must be <= 63 bits +@param[in] q Modulus. Must be <= 31 bits @returns ((x * y) mod q) or (((x * y) mod q) + q) */ static inline ZZ mul_mod_mumo_lazy(ZZ x, const MUMO *y, const Modulus *modulus) @@ -308,9 +314,9 @@ static inline ZZ mul_mod_mumo_lazy(ZZ x, const MUMO *y, const Modulus *modulus) // The formula for this is: // r = [ [x*y]b - [floor(x*u/t) * q]b ]b // = [ [op1]b - [op2 * q]b ]b - // where u = floor(y*t/q), b = 2^64, t = 2^64 + // where u = floor(y*t/q), b = 2^32, t = 2^32 - // -- This will implicitly give us [x*y]b by only returning the lower 64 bits of x*y + // -- This will implicitly give us [x*y]b by only returning the lower 32 bits of x*y ZZ op1 = x * y->operand; // -- We can get op2 = floor(x*u/t) by multiplying x with floor(u) and taking the high @@ -325,12 +331,12 @@ static inline ZZ mul_mod_mumo_lazy(ZZ x, const MUMO *y, const Modulus *modulus) } /** -Modular mult. using a highly-optimized variant of Barrett reduction. Reduces result to [0, -q). Correctness: q <= 63-bit, y < q. +Modular mult. using a highly-optimized variant of Barrett reduction. Reduces result to [0, q). +Correctness: q <= 31-bit, y < q. @param[in] x Operand 1 @param[in] y Operand 2 -@param[in] q Modulus. Must be <= 63 bits. +@param[in] q Modulus. Must be <= 31 bits. @returns (x * y) mod q */ static inline ZZ mul_mod_mumo(ZZ x, const MUMO *y, const Modulus *q) diff --git a/device/lib/uintops.h b/device/lib/uintops.h index 30ac106..b8830dd 100644 --- a/device/lib/uintops.h +++ b/device/lib/uintops.h @@ -46,7 +46,7 @@ Multiplies two ZZ-type values and returns the full sized-(2*sizeof(ZZ)) result. */ static inline void mul_uint_wide(ZZ op1, ZZ op2, ZZ *res) { - mul_uint32_wide(op1, op2, res); + return mul_uint32_wide(op1, op2, res); } /** @@ -83,8 +83,7 @@ Adds two 128-bit unsigned integers together. @param[out] res Pointer to the 128-bit result of op1 + op2 (w/o the carry) @returns The carry value */ -static inline unsigned char add_uint128(const uint64_t *op1, const uint64_t *op2, - uint64_t *res) +static inline unsigned char add_uint128(const uint64_t *op1, const uint64_t *op2, uint64_t *res) { uint64_t res_right; uint64_t carry_right = (uint64_t)add_uint64(op1[0], op2[0], (uint64_t *)&res_right); diff --git a/device/lib/user_defines.h b/device/lib/user_defines.h index e652dc5..532679d 100644 --- a/device/lib/user_defines.h +++ b/device/lib/user_defines.h @@ -32,7 +32,7 @@ Assert type. Will be force-set to 0 if in cmake release mode. 0 = none (will define NDEBUG) 1 = standard using -2 = custom. +2 = custom */ #define SE_ASSERT_TYPE 1 @@ -77,17 +77,21 @@ Note: On the Sphere M4, will not work if this is load-fast for n = 4K and in Deb 2 = load 3 = load fast */ -#define SE_NTT_TYPE 0 +#define SE_NTT_TYPE 1 /** Index map type. +If set to load (non-persist) and SK is persistent and IFFT and NTT are on the fly, will be +reset to option 4 (i.e. load persistent for symmetric, load for asymmetric) + 0 = compute "on-the-fly" -1 = compute persistant +1 = compute persistent 2 = load -3 = load persistant +3 = load persistent +4 = load persistent for symmetric, load for asymmetric */ -#define SE_INDEX_MAP_TYPE 0 +#define SE_INDEX_MAP_TYPE 1 /** Secret key type. (Ignored in asymmetric mode). @@ -99,15 +103,13 @@ memory for better performance). 1 = persistent across primes 2 = persistent */ -#define SE_SK_TYPE 0 +#define SE_SK_TYPE 2 /** Data load type. Load method for precomputed keys, roots, and index map. Note: On M4, this will be force set to 1. -TODO: Error when this is 1 and index map is loaded or ifft is loaded - 0 = from file (uses file I/O) 1 = copy from headers into separate buffer (no file I/O) 2 = load from code directly (not supported) @@ -135,7 +137,6 @@ treats complex values as two doubles (a real part followed by an imaginary part) /** Sometimes malloc is not available or stack memory allocation is more desireable. Comment out to use stack allocation instead. -TODO: Not fully working as yet to comment this out. */ #define SE_USE_MALLOC @@ -154,6 +155,12 @@ will no longer function correctly. Uncomment to use. */ // #define SE_DISABLE_TESTING_CAPABILITY +/** +Uncomment to use the 27-bit default for n=4K instead of the 30-bit default +Make sure to uncomment SEALE_DEFAULT_4K_27BIT in the adapter (see: utils.h) as well. +*/ +// #define SE_DEFAULT_4K_27BIT + // ============================================================================= // Malloc off configurations // @@ -202,7 +209,7 @@ If you are on NRF5 and want output to go to RTT, make sure RETARGET_ENABLED is s sdk_config.h (and that RTT is enabled in the configuration window) and comment out this preprocessor. -If you don not want output to go to RTT, do the opposite of above and uncomment this +If you do not want output to go to RTT, do the opposite of above and uncomment this preprocessor. */ // #define SE_NRF5_UART_PRINTF_ENABLED diff --git a/device/lib/util_print.h b/device/lib/util_print.h index 4896345..64af9ff 100644 --- a/device/lib/util_print.h +++ b/device/lib/util_print.h @@ -52,14 +52,13 @@ extern void _putchar(char character); #endif /** -The printf string for printing floating point values. Modify to change the printing -precision. +The printf string for printing floating point values. Modify to change the printing precision. */ #define SE_PRINT_PREC_STR "%0.2f" /** -The printf string for printing complex floating point values. Modify to change the -printing precision. +The printf string for printing complex floating point values. Modify to change the printing +precision. */ #define SE_PRINT_PREC_CMPLX_STR "%0.2f + %0.2fi" @@ -150,8 +149,8 @@ Prints an array of double complex values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' double complex values (or min(PRINT_LEN_SMALL, len) -double complex values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' double complex values (or min(PRINT_LEN_SMALL, len) double +complex values, if SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -204,8 +203,8 @@ Prints an array of type flpt values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' flpt values (or min(PRINT_LEN_SMALL, len) flpt -values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' flpt values (or min(PRINT_LEN_SMALL, len) flpt values, if +SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -249,8 +248,8 @@ Prints an array of doubles Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' double values (or min(PRINT_LEN_SMALL, len) double -values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' double values (or min(PRINT_LEN_SMALL, len) double values, if +SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -293,8 +292,8 @@ Prints an array of type ZZsign values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' ZZsign values (or min(PRINT_LEN_SMALL, len) ZZsign -values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' ZZsign values (or min(PRINT_LEN_SMALL, len) ZZsign values, if +SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -337,8 +336,8 @@ Prints an array of int8_t values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' int8_t values (or min(PRINT_LEN_SMALL, len) int8_t -values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' int8_t values (or min(PRINT_LEN_SMALL, len) int8_t values, if +SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -357,108 +356,108 @@ static inline void print_poly_int8(const char *name, const int8_t *a, PolySizeTy } /** -Prints an array of uint8_t values - -Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' +Prints an array of int8_t values -Size_req: 'a' must have at least 'len' uint8_t values (or min(PRINT_LEN_SMALL, len) uint8_t -values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' int8_t values @param[in] name Name of array @param[in] a Array to print @param[in] len Number of elements of 'a' to print */ -static inline void print_poly_uint8(const char *name, const uint8_t *a, PolySizeType len) +static inline void print_poly_int8_full(const char *name, const int8_t *a, PolySizeType len) { - size_t print_len = get_print_len(len); printf("%s : { ", name); - for (PolySizeType i = 0; i < print_len; i++) + for (PolySizeType i = 0; i < len; i++) { - printf("%" PRIu8, a[i]); + printf("%" PRIi8, a[i]); print_comma(i, len); } - print_end_string(print_len, len); + printf("}\n"); } /** -Prints an array of int8_t values +Prints an array of int64_t values -Size_req: 'a' must have at least 'len' int8_t values +Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' + +Size_req: 'a' must have at least 'len' int64_t values (or min(PRINT_LEN_SMALL, len) int64_t values, +if SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @param[in] len Number of elements of 'a' to print */ -static inline void print_poly_int8_full(const char *name, const int8_t *a, PolySizeType len) +static inline void print_poly_int64(const char *name, const int64_t *a, PolySizeType len) { + size_t print_len = get_print_len(len); printf("%s : { ", name); - for (PolySizeType i = 0; i < len; i++) + for (PolySizeType i = 0; i < print_len; i++) { - printf("%" PRIi8, a[i]); + printf("%" PRIi64, a[i]); print_comma(i, len); } - printf("}\n"); + print_end_string(print_len, len); } /** -Prints an array of uint8_t values +Prints an array of int64_t values -Size_req: 'a' must have at least 'len' uint8_t values +Size_req: 'a' must have at least 'len' int64_t values @param[in] name Name of array @param[in] a Array to print @param[in] len Number of elements of 'a' to print */ -static inline void print_poly_uint8_full(const char *name, const uint8_t *a, PolySizeType len) +static inline void print_poly_int64_full(const char *name, const int64_t *a, PolySizeType len) { printf("%s : { ", name); for (PolySizeType i = 0; i < len; i++) { - printf("%" PRIu8, a[i]); + printf("%" PRIi64, a[i]); print_comma(i, len); } printf("}\n"); } /** -Prints an array of int64_t values +Prints an array of uint64_t values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' int64_t values (or min(PRINT_LEN_SMALL, len) -int64_t values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' uint64_t values (or min(PRINT_LEN_SMALL, len) uint64_t +values, if SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @param[in] len Number of elements of 'a' to print */ -static inline void print_poly_int64(const char *name, const int64_t *a, PolySizeType len) +static inline void print_poly_uint64(const char *name, const uint64_t *a, PolySizeType len) { size_t print_len = get_print_len(len); printf("%s : { ", name); for (PolySizeType i = 0; i < print_len; i++) { - printf("%" PRIi64, a[i]); + printf("%" PRIu64, a[i]); print_comma(i, len); } print_end_string(print_len, len); } /** -Prints an array of int64_t values +Prints an array of uint64_t values -Size_req: 'a' must have at least 'len' int64_t values +Size_req: 'a' must have at least 'len' uint64_t values @param[in] name Name of array @param[in] a Array to print @param[in] len Number of elements of 'a' to print */ -static inline void print_poly_int64_full(const char *name, const int64_t *a, PolySizeType len) +static inline void print_poly_uint64_full(const char *name, const uint64_t *a, PolySizeType len) { printf("%s : { ", name); for (PolySizeType i = 0; i < len; i++) { - printf("%" PRIi64, a[i]); + printf("%" PRIu64, a[i]); print_comma(i, len); } printf("}\n"); @@ -469,8 +468,8 @@ Prints an array of type-ZZ values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' type-ZZ values (or min(PRINT_LEN_SMALL, len) -type-ZZ values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' type-ZZ values (or min(PRINT_LEN_SMALL, len) type-ZZ values, +if SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -581,9 +580,9 @@ Calls the corrrect print function depending on if 'a' is in small/compressed for Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: If small == 1, 'a' must have at least 2*'len' bits (or 2*min(PRINT_LEN_SMALL, -len) bits, if SE_PRINT_SMALL is defined). Else, 'a' must have at least 'len' type-ZZ -values (or min(PRINT_LEN_SMALL, len) type-ZZ values, if SE_PRINT_SMALL is defined). +Size_req: If small == 1, 'a' must have at least 2*'len' bits (or 2*min(PRINT_LEN_SMALL, len) bits, +if SE_PRINT_SMALL is defined). Else, 'a' must have at least 'len' type-ZZ values (or +min(PRINT_LEN_SMALL, len) type-ZZ values, if SE_PRINT_SMALL is defined). @param[in] name Name of array @param[in] a Array to print @@ -624,8 +623,8 @@ Prints an array of uint16_t values Note: If SE_PRINT_SMALL is defined, will only print up to PRINT_LEN_SMALL elements of 'a' -Size_req: 'a' must have at least 'len' uint16_t values (or min(PRINT_LEN_SMALL, len) -uint16_t values, if SE_PRINT_SMALL is defined) +Size_req: 'a' must have at least 'len' uint16_t values (or min(PRINT_LEN_SMALL, len) uint16_t +values, if SE_PRINT_SMALL is defined) @param[in] name Name of array @param[in] a Array to print @@ -663,11 +662,53 @@ static inline void print_poly_uint16_full(const char *name, const uint16_t *a, P printf("}\n"); } +/** +Prints an array of uint8_t values + +Size_req: 'a' must have at least 'len' uint8_t values (or min(PRINT_LEN_SMALL, len) +uint8_t values, if SE_PRINT_SMALL is defined) + +@param[in] name Name of array +@param[in] a Array to print +@param[in] len Number of elements of 'a' to print +*/ +static inline void print_poly_uint8(const char *name, const uint8_t *a, PolySizeType len) +{ + size_t print_len = get_print_len(len); + printf("%s : { ", name); + for (PolySizeType i = 0; i < print_len; i++) + { + printf("%" PRIu8, (uint8_t)a[i]); + print_comma(i, len); + } + printf("}\n"); +} + +/** +Prints an array of uint8_t values + +Size_req: 'a' must have at least 'len' uint8_t values + +@param[in] name Name of array +@param[in] a Array to print +@param[in] len Number of elements of 'a' to print +*/ +static inline void print_poly_uint8_full(const char *name, const uint8_t *a, PolySizeType len) +{ + printf("%s : { ", name); + for (PolySizeType i = 0; i < len; i++) + { + printf("%" PRIu8, (uint8_t)a[i]); + print_comma(i, len); + } + printf("}\n"); +} + /** Prints a banner with chosen configuration options. -@param[in] sym Set to 1 if in symmetric mode (will just toggle whether secret key type -option is printed) +@param[in] sym Set to 1 if in symmetric mode (will just toggle whether secret key type option is + printed) */ static inline void print_config(bool sym) { @@ -819,6 +860,9 @@ static inline void print_config(bool sym) printf("%s compute persistent (#define SE_INDEX_MAP_PERSIST)\n", index_map_str); #elif defined(SE_INDEX_MAP_LOAD_PERSIST) printf("%s load + persistent (#define SE_INDEX_MAP_LOAD_PERSIST)\n", index_map_str); +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) + printf("%s load + persistent (#define SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM)\n", + index_map_str); #else printf("%s Error! Index map type not chosen\n"); while (1) @@ -854,9 +898,7 @@ Error handler for uart, required for uart on the NRF5. static inline void uart_error_handle(app_uart_evt_t *p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) - { - APP_ERROR_HANDLER(p_event->data.error_communication); - } + { APP_ERROR_HANDLER(p_event->data.error_communication); } else if (p_event->evt_type == APP_UART_FIFO_ERROR) { APP_ERROR_HANDLER(p_event->data.error_code); diff --git a/device/scripts/app_manifest_sphere_a7.txt b/device/scripts/app_manifest_sphere_a7.txt index f5d8805..5d5023c 100644 --- a/device/scripts/app_manifest_sphere_a7.txt +++ b/device/scripts/app_manifest_sphere_a7.txt @@ -1,13 +1,13 @@ { - "SchemaVersion": 1, - "Name": "SealEmbeddedApp", - "ComponentId": "5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2", - "EntryPoint": "/bin/app", - "CmdArgs": [], - "Capabilities": { - "Gpio": [ 9 ], - "AllowedApplicationConnections": [], - "AllowedConnections": [ "www.neverssl.com", "neverssl.com", "httpstat.us"] - }, - "ApplicationType": "Default" - } + "SchemaVersion": 1, + "Name": "SealEmbeddedApp", + "ComponentId": "5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2", + "EntryPoint": "/bin/app", + "CmdArgs": [], + "Capabilities": { + "Gpio": [ 9 ], + "AllowedApplicationConnections": [], + "AllowedConnections": [ "www.neverssl.com", "neverssl.com", "httpstat.us"] + }, + "ApplicationType": "Default" +} diff --git a/device/scripts/app_manifest_sphere_m4.txt b/device/scripts/app_manifest_sphere_m4.txt index b73d60d..d57fefb 100644 --- a/device/scripts/app_manifest_sphere_m4.txt +++ b/device/scripts/app_manifest_sphere_m4.txt @@ -1,13 +1,13 @@ { - "SchemaVersion": 1, - "Name": "SealEmbeddedApp", - "ComponentId": "5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2", - "EntryPoint": "/bin/app", - "CmdArgs": [], - "Capabilities": { - "Gpio": [ 9 ], - "AllowedApplicationConnections": [], - "AllowedConnections": [ "www.neverssl.com", "neverssl.com", "httpstat.us"] - }, - "ApplicationType": "RealTimeCapable" - } + "SchemaVersion": 1, + "Name": "SealEmbeddedApp", + "ComponentId": "5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2", + "EntryPoint": "/bin/app", + "CmdArgs": [], + "Capabilities": { + "Gpio": [ 9 ], + "AllowedApplicationConnections": [], + "AllowedConnections": [ "www.neverssl.com", "neverssl.com", "httpstat.us"] + }, + "ApplicationType": "RealTimeCapable" +} diff --git a/device/scripts/change_defines.py b/device/scripts/change_defines.py new file mode 100644 index 0000000..d7b92de --- /dev/null +++ b/device/scripts/change_defines.py @@ -0,0 +1,150 @@ +#!/usr/bin/python + +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT license. + +import sys, getopt + +def file_line_rep(filedata, base_string, min_val, max_val, set_val): + #print("In file_line_rep. range: ["+str(min_val)+", "+str(max_val)+"], set_val: "+str(set_val)) + print("Setting "+base_string+" to "+str(set_val)) + + if max_val < min_val: + print("ERROR: max_val or min_val is set incorrectly") + raise + if set_val > max_val or set_val < min_val: + print("ERROR: max_val or min_val or set_val is set incorrectly") + raise + + newfiledata = [] + + for line in filedata: + #print(line) + if line.find(base_string) != -1: + #print(line) + for i in range(min_val, max_val+1): + line = line.replace(base_string+" "+str(i), base_string+" "+str(set_val)) + #print(line) + newfiledata.append(line) + + #for line in newfiledata: + # print(line) + + return newfiledata + +# ------------------------- +# Set the IFFT type +# ------------------------- +def set_ifft_define(set_val): + with open("lib/user_defines.h", "r") as file : + filedata = file.readlines() + + newfiledata = file_line_rep(filedata, "#define SE_IFFT_TYPE", 0, 2, set_val) + + with open("lib/user_defines.h", "w") as file : + file.writelines(newfiledata) + + +# ---------------------- +# Set the NTT type +# ---------------------- +def set_ntt_define(set_val): + with open("lib/user_defines.h", "r") as file : + filedata = file.readlines() + + newfiledata = file_line_rep(filedata, "#define SE_NTT_TYPE", 0, 3, set_val) + + with open("lib/user_defines.h", "w") as file : + file.writelines(newfiledata) + + +# ----------------------------- +# Set the Index Map type +# ----------------------------- +def set_index_map_define(set_val): + with open("lib/user_defines.h", "r") as file : + filedata = file.readlines() + + newfiledata = file_line_rep(filedata, "#define SE_INDEX_MAP_TYPE", 0, 4, set_val) + + with open("lib/user_defines.h", "w") as file : + file.writelines(newfiledata) + +# ----------------------------- +# Set the SK type +# ----------------------------- +def set_sk_define(set_val): + with open("lib/user_defines.h", "r") as file : + filedata = file.readlines() + + newfiledata = file_line_rep(filedata, "#define SE_SK_TYPE", 0, 2, set_val) + + with open("lib/user_defines.h", "w") as file : + file.writelines(newfiledata) + +# ----------------------------- +# Set the Data Load Type +# ----------------------------- +def set_data_load_define(set_val): + with open("lib/user_defines.h", "r") as file : + filedata = file.readlines() + + newfiledata = file_line_rep(filedata, "#define SE_DATA_LOAD_TYPE", 0, 2, set_val) + + with open("lib/user_defines.h", "w") as file : + file.writelines(newfiledata) + +def main(argv): + try: + opts, args = getopt.getopt(argv,"hi:n:m:s:d:",["ifft=", "ntt=", "index_map=", "sk=", "data_load="]) + except getopt.GetoptError: + print("change_defines.py -i -n "\ + "-m -s -d ") + sys.exit(2) + + ifft_set_val = -1 + ntt_set_val = -1 + index_map_set_val = -1 + sk_set_val = -1 + data_load_set_val = -1 + + for opt, arg in opts: + if opt == '-h': + print("change_defines.py -i -n "\ + "-m -s -d ") + sys.exit() + elif opt in ("-i", "--ifft"): + ifft_set_val = int(arg) + elif opt in ("-n", "--ntt"): + ntt_set_val = int(arg) + elif opt in ("-m", "--index_map"): + index_map_set_val = int(arg) + elif opt in ("-s", "--sk"): + sk_set_val = int(arg) + elif opt in ("-d", "--data_load"): + data_load_set_val = int(arg) + + if ifft_set_val < 0: + print("Error: Need to choose an ifft option") + sys.exit() + elif ntt_set_val < 0: + print("Error: Need to choose an ntt option") + sys.exit() + elif index_map_set_val < 0: + print("Error: Need to choose an index_map option") + sys.exit() + elif sk_set_val < 0: + print("Error: Need to choose an sk option") + sys.exit() + elif data_load_set_val < 0: + print("Error: Need to choose a data load option") + sys.exit() + + set_ifft_define(ifft_set_val) + set_ntt_define(ntt_set_val) + set_index_map_define(index_map_set_val) + set_sk_define(sk_set_val) + set_data_load_define(data_load_set_val) + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/device/scripts/gdb_launch_sphere_a7.sh b/device/scripts/gdb_launch_sphere_a7.sh index d09837b..6ac77a9 100644 --- a/device/scripts/gdb_launch_sphere_a7.sh +++ b/device/scripts/gdb_launch_sphere_a7.sh @@ -9,7 +9,7 @@ # -- Description: # This script runs the azure sphere gdb debug tool for debugging SEAL-Embedded # a SEAL-Embedded application on an azure sphere device and follows the -# instructions listen under 'Debug the Sample' at the following URL: +# instructions listed under 'Debug the Sample' at the following URL: # # https://docs.microsoft.com/en-us/azure-sphere/install/qs-blink-application # @@ -26,8 +26,12 @@ sysroot=10 SCRIPT_DIR_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" AZSPHERE_SDK_PATH="/opt/azurespheresdk" +IMAGE_DIR_PATH="./build" + +# -- Make sure we are connected +$AZSPHERE_SDK_PATH/DeviceConnection/azsphere_connect.sh # -- Lines 1, 2, 3 $AZSPHERE_SDK_PATH/Sysroots/$sysroot/tools/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-musleabi/arm-poky-linux-musleabi-gdb \ --command=$SCRIPT_DIR_PATH/commands_sphere_a7.gdb \ -./SEAL_EMBEDDED.out +$IMAGE_DIR_PATH/SEAL_EMBEDDED.out diff --git a/device/scripts/gdb_launch_sphere_m4.sh b/device/scripts/gdb_launch_sphere_m4.sh index a8a567d..cdbe896 100644 --- a/device/scripts/gdb_launch_sphere_m4.sh +++ b/device/scripts/gdb_launch_sphere_m4.sh @@ -26,7 +26,8 @@ sysroot=10 SCRIPT_DIR_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" GNU_TOOLCHAIN_PATH="${HOME}/gcc-arm-none-eabi-10.3-2021.07" +IMAGE_DIR_PATH="./build" $GNU_TOOLCHAIN_PATH/bin/arm-none-eabi-gdb \ --command=$SCRIPT_DIR_PATH/commands_sphere_m4.gdb \ -./SEAL_EMBEDDED.out +$IMAGE_DIR_PATH/SEAL_EMBEDDED.out diff --git a/device/scripts/sphere_a7_launch_cl.sh b/device/scripts/sphere_a7_launch_cl.sh index 2b620a7..fbf63e9 100644 --- a/device/scripts/sphere_a7_launch_cl.sh +++ b/device/scripts/sphere_a7_launch_cl.sh @@ -30,6 +30,7 @@ comp_id=5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2 AZSPHERE_SDK_PATH="/opt/azurespheresdk" +IMAGE_DIR_PATH="./build" SCRIPT_DIR_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" @@ -46,7 +47,7 @@ azsphere device sideload delete azsphere device app stop --component-id $comp_id # -- Load the applicaiton onto the device -azsphere device sideload deploy --image-package SEAL_EMBEDDED.imagepackage +azsphere device sideload deploy --image-package $IMAGE_DIR_PATH/SEAL_EMBEDDED.imagepackage # -- Start running the application in debug mode azsphere device app start --debug-mode --component-id $comp_id diff --git a/device/scripts/sphere_m4_launch_cl.sh b/device/scripts/sphere_m4_launch_cl.sh index c3f65b6..2dd3926 100644 --- a/device/scripts/sphere_m4_launch_cl.sh +++ b/device/scripts/sphere_m4_launch_cl.sh @@ -27,6 +27,7 @@ comp_id=5a4b43d1-a3b0-4f9f-a9c9-30a4cc52a2f2 AZSPHERE_SDK_PATH="/opt/azurespheresdk" +IMAGE_DIR_PATH="./build" SCRIPT_DIR_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" @@ -43,7 +44,7 @@ azsphere device sideload delete # azsphere device app stop --component-id $comp_id # -- Load the applicaiton onto the device -azsphere device sideload deploy --image-package SEAL_EMBEDDED.imagepackage +azsphere device sideload deploy --image-package $IMAGE_DIR_PATH/SEAL_EMBEDDED.imagepackage # -- Start running the application in debug mode azsphere device app start --debug-mode --component-id $comp_id diff --git a/device/scripts/test_all_configs.sh b/device/scripts/test_all_configs.sh new file mode 100644 index 0000000..0097512 --- /dev/null +++ b/device/scripts/test_all_configs.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT license. + +CHANGE_DEFINES_SCRIPT=./scripts/change_defines.py +BUILD_DIR=./build + +# ifft 0, 1 +# ntt 0, 1, 2, 3 +# index_map_type 0, 1, 2, 3, 4 +# sk_type 0, 1, 2 +# data_load_type 0, 1 + +# python3 $CHANGE_DEFINES_SCRIPT -i 0 -n 0 -m 0 -s 0 -d 0 +# cmake --build build -j || exit 1 +# $BUILD_DIR/bin/seal_embedded_tests +# status=$? +# echo "status = "$status +# if [ $status -ne "0" ] +# then +# echo "THERE WAS AN ERROR." +# exit +# fi +# exit + +for d in 0 1 + do + for i in 0 1 + do + for n in 0 1 2 3 + do + for m in 0 1 2 3 4 + do + for s in 0 1 + do + python3 $CHANGE_DEFINES_SCRIPT -i $i -n $n -m $m -s $s -d $d + cmake --build $BUILD_DIR -j || exit 1 + $BUILD_DIR/bin/seal_embedded_tests + status=$? + echo "status = "$status + if [ $status -ne 0 ] + then + echo "THERE WAS AN ERROR." + exit + fi + done + done + done + done + done diff --git a/device/test/api_tests.c b/device/test/api_tests.c index bed434c..c8e2299 100644 --- a/device/test/api_tests.c +++ b/device/test/api_tests.c @@ -25,8 +25,7 @@ Size req: v must be at least vlen_bytes @param[in] v Input polynomial (ciphertext) to be printed @param[in] vlen_bytes Number of bytes of v to print -@returns Then number of bytes of v that were printed (equal to vlen_bytes on -success) +@returns Then number of bytes of v that were printed (equal to vlen_bytes on success) */ size_t test_print_ciphertexts(void *v, size_t vlen_bytes) { @@ -43,8 +42,8 @@ size_t test_print_ciphertexts(void *v, size_t vlen_bytes) } /** -Helper function to tests the default SEAL-Embedded API for by printing values of a -ciphertext. If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. +Helper function to tests the default SEAL-Embedded API for by printing values of a ciphertext. +If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. On success, resulting output can be verified with the SEAL-Embedded adapter. */ void test_ckks_api_base(SE_PARMS *se_parms) @@ -60,6 +59,7 @@ void test_ckks_api_base(SE_PARMS *se_parms) flpt *v = calloc(vlen, sizeof(flpt)); #else flpt v[SE_DEGREE_N / 2]; + memset(&v, 0, SE_DEGREE_N * sizeof(flpt) / 2); se_assert(SE_DEGREE_N / 2 == se_parms->parms->coeff_count / 2); #endif @@ -79,26 +79,29 @@ void test_ckks_api_base(SE_PARMS *se_parms) /* // -- Seed for testing uint8_t share_seed[SE_PRNG_SEED_BYTE_COUNT]; - uint8_t seed[SE_PRNG_SEED_BYTE_COUNT]; + uint8_t seed[SE_PRNG_SEED_BYTE_COUNT]; memset(&(share_seed[0]), 0, SE_PRNG_SEED_BYTE_COUNT); - memset(&(seed[0]), 0, SE_PRNG_SEED_BYTE_COUNT); + memset(&( seed[0]), 0, SE_PRNG_SEED_BYTE_COUNT); share_seed[0] = 1; - seed[0] = 1; - bool ret = se_encrypt_seeded(share_seed, seed, fake_network_func, &(v[0]), - vlen * sizeof(flpt), false, se_parms); - se_assert(ret); + seed[0] = 1; + bool ret = se_encrypt_seeded(share_seed, seed, fake_network_func, &(v[0]), vlen * + sizeof(flpt), false, se_parms); se_assert(ret); */ se_encrypt(fake_network_func, &(v[0]), vlen * sizeof(flpt), false, se_parms); } #ifdef SE_USE_MALLOC - free(v); + if (v) + { + free(v); + v = 0; + } #endif delete_parameters(se_parms->parms); } /** -Tests the default SEAL-Embedded API for symmetric encryption by printing values of a -ciphertext. If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. +Tests the default SEAL-Embedded API for symmetric encryption by printing values of a ciphertext. +If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. On success, resulting output can be verified with the SEAL-Embedded adapter. */ void test_ckks_api_sym(void) @@ -119,8 +122,8 @@ void test_ckks_api_sym(void) } /** -Tests the default SEAL-Embedded API for asymmetric encryption by printing values of a -ciphertext. If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. +Tests the default SEAL-Embedded API for asymmetric encryption by printing values of a ciphertext. +If SE_DISABLE_TESTING_CAPABILITY is not defined, throws an error on failure. On success, resulting output can be verified with the SEAL-Embedded adapter. */ void test_ckks_api_asym(void) diff --git a/device/test/ckks_tests_asym.c b/device/test/ckks_tests_asym.c index e4592ec..58dd84e 100644 --- a/device/test/ckks_tests_asym.c +++ b/device/test/ckks_tests_asym.c @@ -21,32 +21,28 @@ #include "util_print.h" #if !((defined(SE_ON_SPHERE_M4) || defined(SE_ON_NRF5)) && defined(SE_ENCRYPT_TYPE_SYMMETRIC)) - /** Asymmetric test core. If debugging is enabled, throws an error if a test fails. +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) @param[in] test_message If 0, sets the message 0 */ -void test_ckks_asym_base(bool test_message) +void test_ckks_asym_base(size_t n, size_t nprimes, bool test_message) { -#ifdef SE_USE_MALLOC - const size_t n = 4096; - size_t nprimes = 3; -#else - const size_t n = SE_DEGREE_N; - size_t nprimes = SE_NPRIMES; +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N && nprimes == SE_NPRIMES); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; + if (nprimes != SE_NPRIMES) nprimes = SE_NPRIMES; #endif Parms parms; - parms.pk_from_file = 0; - parms.sample_s = 0; - parms.scale = (n > 1024) ? pow(2, 25) : pow(2, 20); - // parms.scale = pow(2, 40); - bool encode_only = 0; - // ========================= - parms.is_asymmetric = 1; - parms.small_s = 1; // try this first - parms.small_u = 1; // try this first + parms.sample_s = false; + parms.is_asymmetric = true; + parms.pk_from_file = false; // must be false to check on device + parms.small_s = true; // try this first + parms.small_u = true; // try this first + bool encode_only = false; // -- Make sure we didn't set this accidentally if (!parms.sample_s) se_assert(parms.small_s); @@ -57,6 +53,7 @@ void test_ckks_asym_base(bool test_message) #else print_ckks_mempool_size(); ZZ mempool_local[MEMPOOL_SIZE]; + memset(&mempool_local, 0, MEMPOOL_SIZE * sizeof(ZZ)); ZZ *mempool = &(mempool_local[0]); #endif @@ -94,14 +91,15 @@ void test_ckks_asym_base(bool test_message) ZZ s_vec[SE_DEGREE_N / 16]; int8_t ep_small_vec[SE_DEGREE_N]; ZZ ntt_s_save_vec[SE_DEGREE_N]; // ntt(expanded(s)) or expanded(s) + memset(&ep_small_vec, 0, SE_DEGREE_N * sizeof(ZZ) / 16); + memset(&ntt_s_save_vec, 0, SE_DEGREE_N * sizeof(ZZ) / 16); ZZ *s = &(s_vec[0]); int8_t *ep_small = &(ep_small_vec[0]); ZZ *ntt_s_save = &(ntt_s_save_vec[0]); // ntt(expanded(s)) or expanded(s) #endif #if defined(SE_USE_MALLOC) && !(defined(SE_ON_NRF5) || defined(SE_ON_SPHERE_M4)) - // -- This is too much memory for the NRF5, so just allocate as needed later in that - // case + // -- This is too much memory for the NRF5, so just allocate as needed later in that case ZZ *ntt_ep_save = calloc(n, sizeof(ZZ)); // ntt(reduced(ep)) or reduced(ep) ZZ *ntt_e1_save = calloc(n, sizeof(ZZ)); ZZ *ntt_u_save = calloc(n, sizeof(ZZ)); @@ -115,6 +113,10 @@ void test_ckks_asym_base(bool test_message) ZZ ntt_e1_save_vec[SE_DEGREE_N]; ZZ ntt_u_save_vec[SE_DEGREE_N]; ZZ temp_test_mem_vec[4 * SE_DEGREE_N]; + memset(&ntt_ep_save_vec, 0, SE_DEGREE_N * sizeof(ZZ)); + memset(&ntt_e1_save_vec, 0, SE_DEGREE_N * sizeof(ZZ)); + memset(&ntt_u_save_vec, 0, SE_DEGREE_N * sizeof(ZZ)); + memset(&temp_test_mem_vec, 0, 4 * SE_DEGREE_N * sizeof(ZZ)); ZZ *ntt_ep_save = &(ntt_ep_save_vec[0]); // ntt(reduced(ep)) or reduced(ep) ZZ *ntt_e1_save = &(ntt_e1_save_vec[0]); ZZ *ntt_u_save = &(ntt_u_save_vec[0]); @@ -133,7 +135,7 @@ void test_ckks_asym_base(bool test_message) // -- (If we are testing and sample s is set, this will also sample s) ckks_setup_s(&parms, NULL, &prng, s); size_t s_size = parms.small_s ? n / 16 : n; - if (encode_only) clear(s, s_size); // TODO: What do to here?? + if (encode_only) clear(s, s_size); for (size_t testnum = 0; testnum < 9; testnum++) { @@ -250,18 +252,29 @@ void test_ckks_asym_base(bool test_message) ZZ *pterr = ntt_u_e1_pte; #ifdef SE_ON_NRF5 - free(ntt_ep_save); - free(ntt_e1_save); - free(ntt_u_save); + if (ntt_ep_save) + { + free(ntt_ep_save); + ntt_ep_save = 0; + } + if (ntt_e1_save) + { + free(ntt_e1_save); + ntt_e1_save = 0; + } + if (ntt_u_save) + { + free(ntt_u_save); + ntt_u_save = 0; + } ZZ *temp_test_mem = calloc(4 * n, sizeof(ZZ)); #endif // -- Check that decrypt gives back the pt+err and decode gives back v. - // -- Note: This will only decode if values is non-zero. Otherwise, will - // just decrypt. - // -- Note: sizeof(max(ntt_roots, ifft_roots)) must be passed as temp memory - // to undo ifft - bool s_test_save_small = 0; // TODO: make this set better... + // -- Note: This will only decode if values is non-zero. Otherwise, will just decrypt. + // -- Note: sizeof(max(ntt_roots, ifft_roots)) must be passed as temp memory to undo + // ifft. + bool s_test_save_small = 0; check_decode_decrypt_inpl(pk_c0, pk_c1, v, vlen, ntt_s_save, s_test_save_small, pterr, index_map, &parms, temp_test_mem); @@ -271,7 +284,11 @@ void test_ckks_asym_base(bool test_message) se_assert(ret || (!ret && i + 1 == parms.nprimes)); #ifdef SE_ON_NRF5 - free(temp_test_mem); + if (temp_test_mem) + { + free(temp_test_mem); + temp_test_mem = 0; + } #endif } @@ -280,15 +297,51 @@ void test_ckks_asym_base(bool test_message) } #ifdef SE_USE_MALLOC - free(mempool); - free(s); - free(ep_small); - free(ntt_s_save); + //clang-format off + if (mempool) + { + free(mempool); + mempool = 0; + } + if (s) + { + free(s); + s = 0; + } + if (ep_small) + { + free(ep_small); + ep_small = 0; + } + if (ntt_s_save) + { + free(ntt_s_save); + ntt_s_save = 0; + } + //clang-format on #if !(defined(SE_ON_NRF5) || defined(SE_ON_SPHERE_M4)) - free(ntt_ep_save); - free(ntt_e1_save); - free(ntt_u_save); - free(temp_test_mem); + //clang-format off + if (ntt_ep_save) + { + free(ntt_ep_save); + ntt_ep_save = 0; + } + if (ntt_e1_save) + { + free(ntt_e1_save); + ntt_e1_save = 0; + } + if (ntt_u_save) + { + free(ntt_u_save); + ntt_u_save = 0; + } + if (temp_test_mem) + { + free(temp_test_mem); + temp_test_mem = 0; + } + //clang-format on #endif #endif delete_parameters(&parms); @@ -296,31 +349,51 @@ void test_ckks_asym_base(bool test_message) /** Full encode + asymmetric encrypt test + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) */ -void test_ckks_encode_encrypt_asym(void) +void test_ckks_encode_encrypt_asym(size_t n, size_t nprimes) { printf("Beginning tests for ckks encode + asymmetric encrypt...\n"); bool test_message = 1; - test_ckks_asym_base(test_message); +#ifdef SE_USE_MALLOC + test_ckks_asym_base(n, nprimes, test_message); +#else + SE_UNUSED(n); + SE_UNUSED(nprimes); + test_ckks_asym_base(SE_DEGREE_N, SE_NPRIMES, test_message); +#endif } /** Asymmetric rlwe test only (message is the all-zeros vector) + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) */ -void test_enc_zero_asym(void) +void test_enc_zero_asym(size_t n, size_t nprimes) { printf("Beginning tests for rlwe asymmetric encryption of 0...\n"); bool test_message = 0; - test_ckks_asym_base(test_message); +#ifdef SE_USE_MALLOC + test_ckks_asym_base(n, nprimes, test_message); +#else + SE_UNUSED(n); + SE_UNUSED(nprimes); + test_ckks_asym_base(SE_DEGREE_N, SE_NPRIMES, test_message); +#endif } #else void test_ckks_encode_encrypt_asym(void) { printf("Error! Did you choose the wrong configuration settings?\n"); + se_assert(0); } void test_enc_zero_asym(void) { printf("Error! Did you choose the wrong configuration settings?\n"); + se_assert(0); } #endif diff --git a/device/test/ckks_tests_common.c b/device/test/ckks_tests_common.c index 4e6428d..58e25b1 100644 --- a/device/test/ckks_tests_common.c +++ b/device/test/ckks_tests_common.c @@ -66,7 +66,7 @@ void ckks_decode(const ZZ *pt, size_t values_len, uint16_t *index_map, const Par double scale = parms->scale; double complex *res = temp; - printf("scale: %0.5f\n", scale); + // printf("scale: %0.5f\n", scale); print_poly("pt", pt, n); for (size_t i = 0; i < n; i++) @@ -75,18 +75,15 @@ void ckks_decode(const ZZ *pt, size_t values_len, uint16_t *index_map, const Par // -- Note: q, val are both unsigned, so dval = (double)(val - q) won't work. ZZ val = pt[i]; double dval = (val > q / 2) ? -(double)(q - val) : (double)val; - // if(i < 5) printf("dval: %0.6f\n", dval); - // if(i < 5) printf("dval/scale: %0.6f\n", dval/scale); // -- We scale by 1/scale here - res[i] = (double complex)_complex(dval / scale, 0); - // printf("value: %zu\n", i); + res[i] = (double complex)_complex(dval / scale, (double)0); } - print_poly_double_complex("res", res, n); + print_poly_double_complex("res ", res, n); fft_inpl(res, n, logn, NULL); - print_poly_double_complex("res", res, n); + print_poly_double_complex("res ", res, n); #ifdef SE_INDEX_MAP_OTF // -- Here we are making room for calculating the index map. Since we are just @@ -97,26 +94,24 @@ void ckks_decode(const ZZ *pt, size_t values_len, uint16_t *index_map, const Par // -- We no longer need the second half of res double *res_double = (double *)res; for (size_t i = 0; i < n; i++) { res_double[i] = se_creal(res[i]); } - print_poly_double("res double", res_double, n); + print_poly_double("res double ", res_double, n); uint16_t *index_map_ = (uint16_t *)(&(res[n / 2])); ckks_calc_index_map(parms, index_map_); for (size_t i = 0; i < values_len; i++) - { - values_decoded[i] = (flpt)(res_double[index_map_[i]]); - } + { values_decoded[i] = (flpt)(res_double[index_map_[i]]); } // print_poly_flpt("decoded", values_decoded, n); #else se_assert(index_map); - #ifdef SE_INDEX_MAP_LOAD +#ifdef SE_INDEX_MAP_LOAD // -- Load or setup here, doesn't matter, since we are just testing... load_index_map(parms, index_map); - #endif +#elif defined(SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM) + if (parms->is_asymmetric) load_index_map(parms, index_map); +#endif for (size_t i = 0; i < values_len; i++) - { - values_decoded[i] = (flpt)se_creal(res[index_map[i]]); - } + { values_decoded[i] = (flpt)se_creal(res[index_map[i]]); } #endif } @@ -138,8 +133,7 @@ void check_decode_inpl(ZZ *pt, const flpt *values, size_t values_len, uint16_t * se_assert(!err); } -void ckks_decrypt(const ZZ *c0, const ZZ *c1, const ZZ *s, bool small_s, - const Parms *parms, ZZ *pt) +void ckks_decrypt(const ZZ *c0, const ZZ *c1, const ZZ *s, bool small_s, const Parms *parms, ZZ *pt) { // -- c0 = [-a*s + e + pt]_Rq ; c1 = a // Encryption is correct if [c0 + c1*s]_Rq = ([-a*s + e + pt]_Rq) + [(a)*s]_Rq @@ -176,9 +170,9 @@ void ckks_decrypt_inpl(ZZ *c0, ZZ *c1, const ZZ *s, bool small_s, const Parms *p poly_add_mod_inpl(c0, c1, n, mod); } -void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values_len, - const ZZ *s, bool small_s, const ZZ *pte_calc, - uint16_t *index_map, const Parms *parms, ZZ *temp) +void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values_len, const ZZ *s, + bool small_s, const ZZ *pte_calc, uint16_t *index_map, + const Parms *parms, ZZ *temp) { se_assert(c0 && c1 && values && s && pte_calc && parms && temp); se_assert(!small_s); @@ -189,7 +183,7 @@ void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values print_poly("c0", c0, n); print_poly("c1", c1, n); - print_poly("s", s, n); + print_poly("s ", s, n); // -- Calculate: c0 := [c0 + c1*s] ckks_decrypt_inpl(c0, c1, s, small_s, parms); @@ -217,11 +211,11 @@ void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values // -- Then, test decode if requested if (values) { - print_poly(" c0 ", c0, n); + print_poly("c0 ", c0, n); intt_roots_initialize(parms, temp); se_assert(temp); intt_inpl(parms, temp, c0); - print_poly("pt = intt(c0) ", c0, n); + print_poly("pt = intt(c0) ", c0, n); // -- We don't need enough space for the ifft_roots in this version since we don't // care how long it takes diff --git a/device/test/ckks_tests_common.h b/device/test/ckks_tests_common.h index 1c7c8d6..90114bb 100644 --- a/device/test/ckks_tests_common.h +++ b/device/test/ckks_tests_common.h @@ -24,18 +24,19 @@ If testnum > 8, will set testnum to 8. void set_encode_encrypt_test(size_t testnum, size_t vlen, flpt *v); /** -(Pseudo) ckks decode. 'values_decoded' and 'pt' may share the same starting address for -in-place computation (see: ckks_decode_inpl) +(Pseudo) ckks decode. 'values_decoded' and 'pt' may share the same starting address for in-place +computation (see: ckks_decode_inpl) -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). Size req: 'temp' must contain space for n double complex values 'values_decoded' must contain space for n/2 flpt elements. -If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t elements. +If index map needs to be loaded (see 'Note' above), index_map must constain space for n uint16_t +elements. @param[in] pt Plaintext to decode -@param[in] values_len True number of entries in initial values message. Must be <= -n/2 +@param[in] values_len True number of entries in initial values message. Must be <= n/2 @param[in] index_map [Optional]. If passed in, can avoid 1 flash read @param[in] parms Parameters set by ckks_setup @param temp Scratch space @@ -47,15 +48,16 @@ void ckks_decode(const ZZ *pt, size_t values_len, uint16_t *index_map, const Par /** (Pseudo) in-place ckks decode. -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). Size req: 'temp' must contain space for n double complex values 'values_decoded' must contain space for n/2 flpt elements -If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t elements. +If index map needs to be loaded (see 'Note' above), index_map must constain space for n uint16_t +elements. @param[in,out] pt In: Plaintext to decode; Out: Decoded message -@param[in] values_len True number of entries in initial values message. Must be <= -n/2 +@param[in] values_len True number of entries in initial values message. Must be <= n/2 @param[in] index_map [Optional]. If passed in, can avoid 1 flash read @param[in] parms Parameters set by ckks_setup @param temp Scratch space @@ -69,15 +71,16 @@ static inline void ckks_decode_inpl(ZZ *pt, size_t values_len, uint16_t *index_m /** Checks that the pseudo-decoding of a ciphertext is correct. -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). Size req: 'temp' must contain space for n double complex values -If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t elements. +If index map needs to be loaded (see 'Note' above), index_map must constain space for n uint16_t +elements. @param[in,out] pt In: Plaintext to decode; Out: Decoded message @param[in] values Cleartext input message -@param[in] values_len True number of entries in initial values message. Must be <= -n/2 +@param[in] values_len True number of entries in initial values message. Must be <= n/2 @param[in] index_map [Optional]. If passed in, can avoid 1 flash read @param[in] parms Parameters instance @param temp Scratch space @@ -86,9 +89,9 @@ void check_decode_inpl(ZZ *pt, const flpt *values, size_t values_len, uint16_t * const Parms *parms, ZZ *temp); /** -Pseudo-decrypts a ciphertext in place (for a particular prime component). Currently only -works if 'small_s' is 0. 'c0' and 'pt' may share the same starting address for in-place -computation (see: ckks_decrypt_inpl). +Pseudo-decrypts a ciphertext in place (for a particular prime component). Currently only works if +'small_s' is 0. 'c0' and 'pt' may share the same starting address for in-place computation (see: +ckks_decrypt_inpl). Size req: 'pt' must have space for n elements @@ -99,13 +102,12 @@ Size req: 'pt' must have space for n elements @param[in] parms Parameters instance @param[out] pt Decypted result (a ckks plaintext) */ -void ckks_decrypt(const ZZ *c0, const ZZ *c1, const ZZ *s, bool small_s, - const Parms *parms, ZZ *pt); +void ckks_decrypt(const ZZ *c0, const ZZ *c1, const ZZ *s, bool small_s, const Parms *parms, + ZZ *pt); /** -Pseudo-decrypts a ciphertext in place (for a particular prime component). Currently only -works if small_s is 0. Note that this function will also corrupt the memory pointed to by -c1. +Pseudo-decrypts a ciphertext in place (for a particular prime component). Currently only works if +small_s is 0. Note that this function will also corrupt the memory pointed to by c1. @param[in,out] c0 In: First ciphertext component for this prime; Out: Decrypted result (a CKKS plaintext) @@ -117,15 +119,17 @@ c1. void ckks_decrypt_inpl(ZZ *c0, ZZ *c1, const ZZ *s, bool small_s, const Parms *parms); /** -Checks that the pseudo-decoding and pseudo-decryption of a ciphertext is correct (for a -particular prime ciphertext component). Currently only works if 'small_s' is 0. +Checks that the pseudo-decoding and pseudo-decryption of a ciphertext is correct (for a particular +prime ciphertext component). Currently only works if 'small_s' is 0. Correctness: 'small_s' must be true (and 's' must be in small form) -Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined. +Note: index_map is non-const in case SE_INDEX_MAP_LOAD is defined (or, if +SE_INDEX_MAP_LOAD_PERSIST_SYM_LOAD_ASYM is defined and asymmetric encryption is used). Size req: 'temp' must contain space for n double complex values -If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t elements. +If index map needs to be loaded (see 'Note' above), index_map must constain space for n uint16_t +elements. @param[in] c0 In: 1st component of ciphertext to check; Out: Decrypted and decoded message @@ -134,12 +138,11 @@ If 'SE_INDEX_MAP_LOAD' is defined, index_map must constain space for n uint16_t @param[in] values_len True number of entries in initial values message. Must be <= n/2 @param[in] s Secret key. Must be in expanded form! @param[in] small_s If true, secret key is in small form; Else, is in expanded form -@param[in] pte_calc Plaintext + error (in NTT form) (output by ckks_encode for -testing) +@param[in] pte_calc Plaintext + error (in NTT form) (output by ckks_encode for testing) @param[in] index_map [Optional]. If passed in, can avoid 1 flash read @param[in] parms Parameters instance @param temp Scratch space */ -void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values_len, - const ZZ *s, bool small_s, const ZZ *pte_calc, - uint16_t *index_map, const Parms *parms, ZZ *temp); +void check_decode_decrypt_inpl(ZZ *c0, ZZ *c1, const flpt *values, size_t values_len, const ZZ *s, + bool small_s, const ZZ *pte_calc, uint16_t *index_map, + const Parms *parms, ZZ *temp); diff --git a/device/test/ckks_tests_encode.c b/device/test/ckks_tests_encode.c index fcedefc..6baab19 100644 --- a/device/test/ckks_tests_encode.c +++ b/device/test/ckks_tests_encode.c @@ -22,26 +22,29 @@ #include "test_common.h" #include "util_print.h" -void test_ckks_encode(void) +/** +Test CKKS encoding + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ +void test_ckks_encode(size_t n) { -#ifdef SE_USE_MALLOC - size_t n = 4096; -#else - size_t n = SE_DEGREE_N; +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; #endif Parms parms; - parms.scale = (n > 1024) ? pow(2, 25) : pow(2, 20); - - // TODO: Make a malloc function to include test memory #ifdef SE_USE_MALLOC print_ckks_mempool_size(n, 1); ZZ *mempool = ckks_mempool_setup_sym(n); #else print_ckks_mempool_size(); ZZ mempool_local[MEMPOOL_SIZE]; + memset(&(mempool_local), 0, MEMPOOL_SIZE * sizeof(ZZ)); ZZ *mempool = &(mempool_local[0]); #endif + // TODO: Make a malloc function to include test memory // -- Get pointers SE_PTRS se_ptrs_local; @@ -60,6 +63,7 @@ void test_ckks_encode(void) ZZ *temp = calloc(n, sizeof(double complex)); #else ZZ temp[SE_DEGREE_N * sizeof(double complex) / sizeof(ZZ)]; + memset(&temp, 0, SE_DEGREE_N * sizeof(double complex)); #endif // -- Set up parameters and index_map if applicable @@ -90,8 +94,10 @@ void test_ckks_encode(void) check_decode_inpl(pt, v, vlen, index_map, &parms, temp); } #ifdef SE_USE_MALLOC - free(mempool); - free(temp); + // clang-format off + if (mempool) { free(mempool); mempool = 0; } + if (temp) { free(temp); temp = 0; } + // clang-format on #endif delete_parameters(&parms); } diff --git a/device/test/ckks_tests_sym.c b/device/test/ckks_tests_sym.c index e0ac35a..582d19b 100644 --- a/device/test/ckks_tests_sym.c +++ b/device/test/ckks_tests_sym.c @@ -26,38 +26,35 @@ /** Symmetric test core. If debugging is enabled, throws an error if a test fails. +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) @param[in] test_message If 0, sets the message 0 */ -void test_ckks_sym_base(bool test_message) +void test_ckks_sym_base(size_t n, size_t nprimes, bool test_message) { -#ifdef SE_USE_MALLOC - size_t n = 4096; - size_t nprimes = 3; -#else - size_t n = SE_DEGREE_N; - size_t nprimes = SE_NPRIMES; +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N && nprimes == SE_NPRIMES); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; + if (nprimes != SE_NPRIMES) nprimes = SE_NPRIMES; #endif Parms parms; - parms.scale = (n > 1024) ? pow(2, 25) : pow(2, 20); - // parms.scale = pow(2, 40); - // parms.scale = n * n; - bool encode_only = 0; // this only works if s is not not persistent - // ========================= - parms.is_asymmetric = 0; - parms.small_s = 1; - parms.sample_s = 0; + parms.sample_s = false; + parms.is_asymmetric = false; + parms.small_s = true; + bool encode_only = false; // this only works if s is not not persistent // -- Make sure we didn't set this accidentally if (!parms.sample_s) se_assert(parms.small_s); #ifdef SE_USE_MALLOC - print_ckks_mempool_size(n, 1); + print_ckks_mempool_size(n, true); ZZ *mempool = ckks_mempool_setup_sym(n); #else print_ckks_mempool_size(); ZZ mempool_local[MEMPOOL_SIZE]; ZZ *mempool = &(mempool_local[0]); + memset(&(mempool[0]), 0, MEMPOOL_SIZE * sizeof(ZZ)); #endif // -- Get pointers @@ -77,11 +74,12 @@ void test_ckks_sym_base(bool test_message) if (!test_message) { - memset(v, 0, vlen * sizeof(flpt)); + se_assert(v); + if (v) memset(v, 0, vlen * sizeof(flpt)); // v = 0; } -// -- Additional pointers required for testing. + // -- Additional pointers required for testing. #ifdef SE_USE_MALLOC ZZ *s_test_save = calloc(n, sizeof(ZZ)); // ntt(expanded(s)) or expanded(s) ZZ *c1_test_save = calloc(n, sizeof(ZZ)); @@ -90,12 +88,12 @@ void test_ckks_sym_base(bool test_message) ZZ s_test_save_vec[SE_DEGREE_N]; // ntt(expanded(s)) or expanded(s) ZZ c1_test_save_vec[SE_DEGREE_N]; ZZ temp_test_mem_vec[4 * SE_DEGREE_N]; - ZZ *s_test_save = &(s_test_save_vec[0]); - // printf("s_save address: %p\n", s_test_save); + memset(&s_test_save_vec, 0, SE_DEGREE_N * sizeof(ZZ)); + memset(&c1_test_save_vec, 0, SE_DEGREE_N * sizeof(ZZ)); + memset(&temp_test_mem_vec, 0, 4 * SE_DEGREE_N * sizeof(ZZ)); + ZZ *s_test_save = &(s_test_save_vec[0]); ZZ *c1_test_save = &(c1_test_save_vec[0]); ZZ *temp_test_mem = &(temp_test_mem_vec[0]); -// printf("temp_test_mem address: %p\n", temp_test_mem); -// printf("temp_test_mem address: %p\n", &(temp_test_mem[4*SE_DEGREE_N-1])); #endif SE_PRNG prng; @@ -128,7 +126,9 @@ void test_ckks_sym_base(bool test_message) // -- First, calculate m + e (not fully reduced, not in ntt form) if (test_message) { + // print_poly_uint8_full("s, init1", (uint8_t*)s, n/4); bool ret = ckks_encode_base(&parms, v, vlen, index_map, ifft_roots, conj_vals); + // print_poly_uint8_full("s, init2", (uint8_t*)s, n/4); se_assert(ret); } else @@ -138,15 +138,15 @@ void test_ckks_sym_base(bool test_message) // -- Sample error e. While sampling e, add it in place to the base message. if (!encode_only) - { - ckks_sym_init(&parms, NULL, NULL, &shareable_prng, &prng, conj_vals_int); - } + { ckks_sym_init(&parms, NULL, NULL, &shareable_prng, &prng, conj_vals_int); } for (size_t i = 0; i < parms.nprimes; i++) { print_zz("\n ***** Modulus", parms.curr_modulus->value); // -- Per prime Encode + Encrypt + // print_poly_ternary("s", s, n, true); + // print_poly_ternary_full("s", s, n, true); ckks_encode_encrypt_sym(&parms, conj_vals_int, NULL, &shareable_prng, s, ntt_pte, ntt_roots, c0, c1, s_test_save, c1_test_save); // print_poly_int64("conj_vals_int", conj_vals_int, n); @@ -162,6 +162,11 @@ void test_ckks_sym_base(bool test_message) check_decode_decrypt_inpl(c0, c1_test_save, v, vlen, s_test_save, s_test_save_small, ntt_pte, index_map, &parms, temp_test_mem); +#ifdef SE_SK_PERSISTENT_ACROSS_PRIMES + // -- Decoding corrupted this, so load it back + load_sk(&parms, s); +#endif + // -- Done checking this prime. Now try next prime if requested bool ret = ckks_next_prime_sym(&parms, s); se_assert(ret || (!ret && i + 1 == parms.nprimes)); @@ -172,43 +177,77 @@ void test_ckks_sym_base(bool test_message) } #ifdef SE_USE_MALLOC - free(mempool); - free(s_test_save); - free(c1_test_save); - free(temp_test_mem); + //clang-format off + if (mempool) + { + free(mempool); + mempool = 0; + } + if (s_test_save) + { + free(s_test_save); + s_test_save = 0; + } + if (c1_test_save) + { + free(c1_test_save); + c1_test_save = 0; + } + if (temp_test_mem) + { + free(temp_test_mem); + temp_test_mem = 0; + } + //clang-format on #endif delete_parameters(&parms); } /** Full encode + symmetric encrypt test + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) */ -void test_ckks_encode_encrypt_sym(void) +void test_ckks_encode_encrypt_sym(size_t n, size_t nprimes) { printf("Beginning tests for ckks encode + symmetric encrypt...\n"); bool test_message = 1; - test_ckks_sym_base(test_message); +#ifdef SE_USE_MALLOC + test_ckks_sym_base(n, nprimes, test_message); +#else + test_ckks_sym_base(SE_DEGREE_N, SE_NPRIMES, test_message); +#endif } /** Symmetric rlwe test only (message is the all-zeros vector) + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) */ -void test_enc_zero_sym(void) +void test_enc_zero_sym(size_t n, size_t nprimes) { printf("Beginning tests for rlwe symmetric encryption of 0...\n"); bool test_message = 0; - test_ckks_sym_base(test_message); +#ifdef SE_USE_MALLOC + test_ckks_sym_base(n, nprimes, test_message); +#else + test_ckks_sym_base(SE_DEGREE_N, SE_NPRIMES, test_message); +#endif } #else void test_ckks_encode_encrypt_sym(void) { printf("Error! Did you choose the wrong configuration settings?\n"); + se_assert(0); } void test_enc_zero_sym(void) { printf("Error! Did you choose the wrong configuration settings?\n"); + se_assert(0); } #endif diff --git a/device/test/fft_tests.c b/device/test/fft_tests.c index 4fbe655..deca8e1 100644 --- a/device/test/fft_tests.c +++ b/device/test/fft_tests.c @@ -31,8 +31,8 @@ /** Multiplies two complex-valued polynomials using schoolbook multiplication. -Space req: 'res' must constain space for n double complex elements, where each input -polynomial consists of n double complex elements (each). +Space req: 'res' must constain space for n double complex elements, where each input polynomial +consists of n double complex elements (each). @param[in] a Input polynomial 1 @param[in] b Input polynomial 2 @@ -61,11 +61,11 @@ static inline void poly_div_inpl_complex(double complex *poly, size_t n, size_t } /** -Pointwise multiplies two complex-valued polynomials. 'a' and 'res' starting address may -overlap for in-place computation (see: pointwise_mult_inpl_complex) +Pointwise multiplies two complex-valued polynomials. 'a' and 'res' starting address may overlap for +in-place computation (see: pointwise_mult_inpl_complex) -Space req: 'res' must constain space for n double complex elements, where each input -polynomial consists of n double complex elements (each). +Space req: 'res' must constain space for n double complex elements, where each input polynomial +consists of n double complex elements (each). @param[in] a Input polynomial 1 @param[in] b Input polynomial 2 @@ -92,11 +92,13 @@ static inline void pointwise_mult_inpl_complex(double complex *a, const double c for (size_t i = 0; i < n; i++) a[i] *= b[i]; } -void test_fft_mult_helper(size_t degree, double complex *v1, double complex *v2, - double complex *v_exp, double complex *temp, double complex *roots) +/** +Helper function for FFT test +*/ +void test_fft_mult_helper(size_t n, double complex *v1, double complex *v2, double complex *v_exp, + double complex *temp, double complex *roots) { - size_t n = degree; - size_t logn = (size_t)log2(degree); + size_t logn = (size_t)log2(n); // -- Correctness: ifft(fft(vec) .* fft(v2))*(1/n) = vec * vec2 print_poly_double_complex("v1 ", v1, n); @@ -178,6 +180,8 @@ void test_fft_helper(size_t degree, const double complex *v, double complex *tem #ifdef SE_IFFT_LOAD_FULL se_assert(roots); load_ifft_roots(n, roots); + print_poly_double_complex("roots ", roots, n); + // print_poly_double_complex_full("roots ", roots, n); #elif defined(SE_IFFT_ONE_SHOT) se_assert(roots); calc_ifft_roots(n, logn, roots); @@ -195,44 +199,52 @@ void test_fft_helper(size_t degree, const double complex *v, double complex *tem se_assert(!err); } -void test_fft(void) +/** +FFT test function + +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ +void test_fft(size_t n) { - // -- Note: For multiplication tests, v1 and v2 must not be - // larger than length n/2 in data and should be zero - // padded up to length n. Result vector will be length n. +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; +#endif + // -- Note: For multiplication tests, v1 and v2 must not be + // larger than length n/2 in data and should be zero + // padded up to length n. Result vector will be length n. + + // clang-format off #ifdef SE_USE_MALLOC - const size_t n = 4096; size_t ifft_roots_size = 0; #if defined(SE_IFFT_LOAD_FULL) || defined(SE_IFFT_ONE_SHOT) ifft_roots_size = n; #endif + size_t fft_roots_size = 0; #if defined(SE_FFT_LOAD_FULL) || defined(SE_FFT_ONE_SHOT) fft_roots_size = n; #endif - size_t roots_size = ifft_roots_size ? ifft_roots_size : fft_roots_size; - size_t mempool_size = 4 * n + roots_size; + + size_t roots_size = ifft_roots_size ? ifft_roots_size : fft_roots_size; + size_t mempool_size = 4*n + roots_size; double complex *mempool = calloc(mempool_size, sizeof(double complex)); #else - const size_t n = SE_DEGREE_N; - size_t roots_size = IFFT_TEST_ROOTS_MEM + FFT_TEST_ROOTS_MEM; - double complex mempool[4 * SE_DEGREE_N + IFFT_TEST_ROOTS_MEM + FFT_TEST_ROOTS_MEM]; - size_t mempool_size = 4 * n + roots_size; + double complex mempool[4*SE_DEGREE_N + IFFT_TEST_ROOTS_MEM + FFT_TEST_ROOTS_MEM]; + size_t roots_size = IFFT_TEST_ROOTS_MEM + FFT_TEST_ROOTS_MEM; + size_t mempool_size = 4*n + roots_size; + memset(&mempool, 0, mempool_size * sizeof(double complex)); #endif - size_t start_idx = 0; - double complex *v1 = &(mempool[start_idx]); - start_idx += n; - double complex *v2 = &(mempool[start_idx]); - start_idx += n; - double complex *v_exp = &(mempool[start_idx]); - start_idx += n; - double complex *temp = &(mempool[start_idx]); - start_idx += n; - double complex *roots = roots_size ? &(mempool[start_idx]) : 0; - start_idx += roots_size; + size_t start_idx = 0; + double complex *v1 = &(mempool[start_idx]); start_idx += n; + double complex *v2 = &(mempool[start_idx]); start_idx += n; + double complex *v_exp = &(mempool[start_idx]); start_idx += n; + double complex *temp = &(mempool[start_idx]); start_idx += n; + double complex *roots = roots_size ? &(mempool[start_idx]) : 0; start_idx += roots_size; se_assert(start_idx == mempool_size); + // clang-format on Parms parms; set_parms_ckks(n, 1, &parms); @@ -247,84 +259,76 @@ void test_fft(void) case 0: set_double_complex(v1, n, 1); break; // {1, 1, 1, ... } case 1: set_double_complex(v1, n, 2); break; // {2, 2, 2, ... } case 2: // {0, 1, 2, 3, ...} - for (size_t i = 0; i < n; i++) { v1[i] = (double complex)_complex(i, 0); } + for (size_t i = 0; i < n; i++) + { v1[i] = (double complex)_complex((double)i, (double)0); } break; case 3: for (size_t i = 0; i < n; i++) - { - v1[i] = (double complex)(gen_double_eighth(10), 0); - } + { v1[i] = (double complex)(gen_double_eighth(10), (double)0); } break; case 4: for (size_t i = 0; i < n; i++) - { - v1[i] = (double complex)(gen_double_quarter(100), 0); - } + { v1[i] = (double complex)(gen_double_quarter(100), (double)0); } break; case 5: for (size_t i = 0; i < n; i++) - { - v1[i] = (double complex)(gen_double_half(-100), 0); - } + { v1[i] = (double complex)(gen_double_half(-100), (double)0); } break; case 6: - for (size_t i = 0; i < n; i++) { v1[i] = (double complex)(gen_double(1000), 0); } + for (size_t i = 0; i < n; i++) + { v1[i] = (double complex)(gen_double(1000), (double)0); } break; case 7: // {1, 0, 0, ...} * {2, 2, 2, ...} = {2, 2, 2, ..., 0, 0, ...} - v1[0] = (double complex)_complex(1, 0); + v1[0] = (double complex)_complex((double)1, (double)0); set_double_complex(v2, n / 2, 2); set_double_complex(v_exp, n / 2, 2); break; case 8: // {-1, 0, 0, ...} * {2, 2, 2, ...} = {-2, -2, -2, ..., 0, 0, // ...} - v1[0] = (double complex)_complex(-1, 0); - set_double_complex(v2, n / 2, 2); - set_double_complex(v_exp, n / 2, -2); + v1[0] = (double complex)_complex((double)-1, (double)0); + set_double_complex(v2, n / 2, (flpt)2); + set_double_complex(v_exp, n / 2, (flpt)(-2)); break; case 9: // {1, 0, 0, ...} * {-2, -2, -2, ...} = {-2, -2, -2, ..., 0, 0, // ...} - v1[0] = (double complex)_complex(1, 0); - set_double_complex(v2, n / 2, -2); - set_double_complex(v_exp, n / 2, -2); + v1[0] = (double complex)_complex((double)1, (double)0); + set_double_complex(v2, n / 2, (flpt)(-2)); + set_double_complex(v_exp, n / 2, (flpt)(-2)); break; case 10: // {1, 1, 1, ...} * {2, 2, 2, ...} = {2, 4, 8, ..., 4, 2, 0} - set_double_complex(v1, n / 2, 1); - set_double_complex(v2, n / 2, 2); + set_double_complex(v1, n / 2, (flpt)1); + set_double_complex(v2, n / 2, (flpt)2); for (size_t i = 0; i < n / 2; i++) - { - v_exp[i] = (double complex)_complex(2 * (i + 1), 0); - } + { v_exp[i] = (double complex)_complex((double)(2 * (i + 1)), (double)0); } for (size_t i = 0; i < (n / 2) - 1; i++) - { - v_exp[i + n / 2] = v_exp[n / 2 - (i + 2)]; - } + { v_exp[i + n / 2] = v_exp[n / 2 - (i + 2)]; } break; case 11: for (size_t i = 0; i < n / 2; i++) { - v1[i] = (double complex)_complex(gen_double_eighth(pow(10, 1)), 0); - v2[i] = (double complex)_complex(gen_double_eighth(pow(10, 1)), 0); + v1[i] = (double complex)_complex(gen_double_eighth(pow(10, 1)), (double)0); + v2[i] = (double complex)_complex(gen_double_eighth(pow(10, 1)), (double)0); } break; case 12: for (size_t i = 0; i < n / 2; i++) { - v1[i] = (double complex)_complex(gen_double_quarter(-pow(10, 2)), 0); - v2[i] = (double complex)_complex(gen_double_quarter(-pow(10, 2)), 0); + v1[i] = (double complex)_complex(gen_double_quarter(-pow(10, 2)), (double)0); + v2[i] = (double complex)_complex(gen_double_quarter(-pow(10, 2)), (double)0); } break; case 13: for (size_t i = 0; i < n / 2; i++) { - v1[i] = (double complex)(gen_double_half(pow(10, 3)), 0); - v2[i] = (double complex)(gen_double_half(pow(10, 3)), 0); + v1[i] = (double complex)(gen_double_half(pow(10, 3)), (double)0); + v2[i] = (double complex)(gen_double_half(pow(10, 3)), (double)0); } break; case 14: for (size_t i = 0; i < n / 2; i++) { - v1[i] = (double complex)(gen_double(pow(10, 6)), 0); - v2[i] = (double complex)(gen_double(pow(10, 6)), 0); + v1[i] = (double complex)(gen_double(pow(10, 6)), (double)0); + v2[i] = (double complex)(gen_double(pow(10, 6)), (double)0); } break; } @@ -336,7 +340,11 @@ void test_fft(void) test_fft_mult_helper(n, v1, v2, 0, temp, roots); } #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } #endif } diff --git a/device/test/main.c b/device/test/main.c index d89d7f7..511802c 100644 --- a/device/test/main.c +++ b/device/test/main.c @@ -4,12 +4,15 @@ /** @file main.c -Note: Currently, you can only run one test at a time. Uncomment the test you want to run (and -uncomment all others) before compiling. Note: Not all the tests will run on the devices since tests -may consume much more memory than regular API usage. +Note: While the tests can run back-to-back, on device it is best to run only one test at a time +(i.e., uncomment the test you want to run and uncomment all others before compiling.) +Note: Not all the tests may run on the devices since tests may consume much more memory than regular +API usage. */ #include "defines.h" + +#ifndef SE_DISABLE_TESTING_CAPABILITY #include "util_print.h" extern void test_add_uint(void); @@ -17,18 +20,18 @@ extern void test_mult_uint(void); extern void test_add_mod(void); extern void test_neg_mod(void); extern void test_mul_mod(void); -extern void test_sample_poly_uniform(void); -extern void test_sample_poly_ternary(void); -extern void test_sample_poly_ternary_small(void); +extern void test_sample_poly_uniform(size_t n); +extern void test_sample_poly_ternary(size_t n); +extern void test_sample_poly_ternary_small(size_t n); extern void test_barrett_reduce(void); extern void test_barrett_reduce_wide(void); -extern void test_poly_mult_ntt(void); -extern void test_fft(void); -extern void test_enc_zero_sym(void); -extern void test_enc_zero_asym(void); -extern void test_ckks_encode(void); -extern void test_ckks_encode_encrypt_sym(void); -extern void test_ckks_encode_encrypt_asym(void); +extern void test_poly_mult_ntt(size_t n, size_t nprimes); +extern void test_fft(size_t n); +extern void test_enc_zero_sym(size_t n, size_t nprimes); +extern void test_enc_zero_asym(size_t n, size_t nprimes); +extern void test_ckks_encode(size_t n); +extern void test_ckks_encode_encrypt_sym(size_t n, size_t nprimes); +extern void test_ckks_encode_encrypt_asym(size_t n, size_t nprimes); extern void test_ckks_api_sym(void); extern void test_ckks_api_asym(void); @@ -81,36 +84,50 @@ int main(void) // printf("Beginning tests...\n"); // } printf("Beginning tests...\n"); - se_randomness_init(); // required for nrf. does nothing if not on nrf +#ifdef SE_RAND_NRF5 + se_randomness_init(); // required for nrf +#endif - // test_sample_poly_uniform(); - // test_sample_poly_ternary(); - // test_sample_poly_ternary_small(); // Only useful when SE_USE_MALLOC is defined +#ifdef SE_USE_MALLOC + // const size_t n = 1024, nprimes = 1; + // const size_t n = 2048, nprimes = 1; + const size_t n = 4096, nprimes = 3; + // const size_t n = 8192, nprimes = 6; + // const size_t n = 16384, nprimes = 13; +#else + const size_t n = SE_DEGREE_N; + const size_t nprimes = SE_NPRIMES; +#endif - // test_add_uint(); - // test_mult_uint(); + test_sample_poly_uniform(n); + test_sample_poly_ternary(n); + test_sample_poly_ternary_small(n); // Only useful when SE_USE_MALLOC is defined - // test_barrett_reduce(); - // test_barrett_reduce_wide(); + test_add_uint(); + test_mult_uint(); - // test_add_mod(); - // test_neg_mod(); - // test_mul_mod(); + test_barrett_reduce(); + test_barrett_reduce_wide(); + + test_add_mod(); + test_neg_mod(); + test_mul_mod(); // -- Note: This test sometimes takes a while to run // because it uses schoolbook multiplication - // test_poly_mult_ntt(); + // -- Comment it out unless you need to test it + // test_poly_mult_ntt(n, nprimes); - test_fft(); + test_fft(n); - // test_enc_zero_sym(); - // test_enc_zero_asym(); + test_enc_zero_sym(n, nprimes); + test_enc_zero_asym(n, nprimes); - // test_ckks_encode(); + test_ckks_encode(n); // -- Main tests - test_ckks_encode_encrypt_sym(); - // test_ckks_encode_encrypt_asym(); + test_ckks_encode_encrypt_sym(n, nprimes); + test_ckks_encode_encrypt_asym(n, nprimes); // -- Run these tests to verify api // -- Check the result with the adapter by writing output to a text file @@ -124,3 +141,5 @@ int main(void) printf("...done with all tests. All tests passed.\n"); exit(0); } + +#endif diff --git a/device/test/modulo_tests.c b/device/test/modulo_tests.c index b95b6b4..d990308 100644 --- a/device/test/modulo_tests.c +++ b/device/test/modulo_tests.c @@ -15,8 +15,8 @@ #include "util_print.h" /** -Helper function to test barret wide reduction. Note that this tests the '%' operator -too, which may not always be desirable. +Helper function to test barret wide reduction. Note that this tests the '%' operator too, which may +not always be desirable. @param input @param modulus @@ -44,8 +44,8 @@ void test_barrett_reduce_wide_helper(ZZ *input, const Modulus *modulus, ZZ res_e } /** -Helper function to test barret reduction. Note that this tests the '%' operator too, -which may not always be desirable. +Helper function to test barret reduction. Note that this tests the '%' operator too, which may not +always be desirable. @param input @param modulus @@ -59,7 +59,7 @@ void test_barrett_reduce_helper(ZZ input, const Modulus *modulus, ZZ res_exp) printf("%" PRIuZZ " mod %" PRIuZZ "\n", input, q); - // If you don't want to test '%', must comment this out + // -- If you don't want to test '%', comment this out ZZ res_default = input % q; print_zz("Result default", res_default); se_assert(res == res_default); @@ -73,58 +73,32 @@ void test_barrett_reduce(void) { printf("\n**************************************\n"); printf("Beginning tests for barrett_reduce...\n\n"); - ZZ input; - Modulus modulus; - - // modulus.value = q - // modulus.const_ratio = floor(2^128/q) - - // ------------------------------ - // Set 1: q = 2 (min value of q) - // ------------------------------ - set_modulus(2, &modulus); - - test_barrett_reduce_helper(1, &modulus, 1); // 1 = 1 (mod 2) - test_barrett_reduce_helper(2, &modulus, 0); // 2 = 0 (mod 2) - test_barrett_reduce_helper(3, &modulus, 1); // 3 = 1 (mod 2) - test_barrett_reduce_helper(MAX_ZZ, &modulus, 1); // max odd = 1 (mod 2) - test_barrett_reduce_helper(MAX_ZZ - 1, &modulus, 0); // max even = 0 (mod 2) - - input = 0xda9b246b; - - test_barrett_reduce_helper(input, &modulus, 1); // random odd = 1 (mod 2) - input--; - test_barrett_reduce_helper(input, &modulus, 0); // random even = 0 (mod 2) - - // ------------------ - // Set 2: q = max q - // ------------------ - set_modulus(MAX_Q, &modulus); - test_barrett_reduce_helper(1, &modulus, 1); // 1 % MAXQ = 1 - test_barrett_reduce_helper(2, &modulus, 2); // 2 % MAXQ = 2 - - // -- Since MAX_ZZ = MAX_Q*2 + 1, MAXZZ % MAX_q = 1 - test_barrett_reduce_helper(MAX_ZZ, &modulus, 1); - - // -- random x < q ---> x = x (mod q), 32 bit - input = 0x76774403; - test_barrett_reduce_helper(input, &modulus, input); - input = 0x958b2d91; // random > q, 32 bit - test_barrett_reduce_helper(input, &modulus, 361442706); - - // -------------------------------------------- - // Set 3: q = 1559578058 (random 31-bit number) - // -------------------------------------------- - set_modulus(0x5cf545ca, &modulus); - test_barrett_reduce_helper(1, &modulus, 1); // 1 = 1 (mod q) - test_barrett_reduce_helper(2, &modulus, 2); // 2 = 2 (mod q) - test_barrett_reduce_helper(MAX_ZZ, &modulus, 1175811179); - - // random x < q ---> x = x (mod q) - input = 0x16774403; - test_barrett_reduce_helper(input, &modulus, input); - // random > q, 32 bit - test_barrett_reduce_helper(0x958b2d91, &modulus, 949348295); + Modulus modulus; // value = q, const_ratio = floor(2^64, q) + + set_modulus(134012929, &modulus); // 27 bits + + test_barrett_reduce_helper(0, &modulus, 0); + test_barrett_reduce_helper(1, &modulus, 1); + test_barrett_reduce_helper(134012928, &modulus, 134012928); + test_barrett_reduce_helper(134012929, &modulus, 0); + test_barrett_reduce_helper(134012930, &modulus, 1); + test_barrett_reduce_helper((ZZ)(134012929) << 1, &modulus, 0); // 28 bits + test_barrett_reduce_helper((ZZ)(134012929) << 2, &modulus, 0); // 29 bits + test_barrett_reduce_helper(0x36934613, &modulus, 111543821); // random + test_barrett_reduce_helper(MAX_ZZ, &modulus, 6553567); + + set_modulus(1053818881, &modulus); // 30 bits + + test_barrett_reduce_helper(0, &modulus, 0); + test_barrett_reduce_helper(1, &modulus, 1); + test_barrett_reduce_helper(1053818880, &modulus, 1053818880); + test_barrett_reduce_helper(1053818881, &modulus, 0); + test_barrett_reduce_helper(1053818882, &modulus, 1); + test_barrett_reduce_helper((ZZ)(1053818881) << 1, &modulus, 0); // 31 bits + test_barrett_reduce_helper((ZZ)(1053818881) << 2, &modulus, 0); // 32 bits + test_barrett_reduce_helper(0x36934613, &modulus, 915621395); // random + test_barrett_reduce_helper(MAX_ZZ, &modulus, 79691771); + printf("\n... all tests for barrett_reduce passed.\n"); printf("**************************************\n"); } @@ -135,40 +109,43 @@ void test_barrett_reduce_wide(void) printf("Beginning tests for barrett_reduce_wide...\n\n"); ZZ input[2]; - Modulus modulus; - // -- q must also be > 1 ----> where is this enforced?? + Modulus modulus; // value = q, const_ratio = floor(2^64, q) - // ------------------------------ - // Set 1: q = 2 (min value of q) - // ------------------------------ - set_modulus(2, &modulus); + set_modulus(134012929, &modulus); // 27 bits - input[1] = 0; // 1 = 1 (mod 2) + input[1] = 0; + input[0] = 0; + test_barrett_reduce_wide_helper(input, &modulus, 0); + input[1] = 0; input[0] = 1; test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = 0; // 2 = 0 (mod 2) - input[0] = 2; + input[1] = 0; + input[0] = 134012928; + test_barrett_reduce_wide_helper(input, &modulus, 134012928); + input[1] = 0; + input[1] = 0; + input[0] = 134012929; test_barrett_reduce_wide_helper(input, &modulus, 0); - input[1] = 0; // 3 = 1 (mod 2) - input[0] = 3; - test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = MAX_ZZ; // max (odd) = 1 (mod 2) - input[0] = MAX_ZZ; + input[1] = 0; + input[0] = 134012930; test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = MAX_ZZ; // max (even) = 0 (mod 2) - input[0] = MAX_ZZ - 1; + input[1] = 0; + input[0] = 134012929 << 1; // 28 bits test_barrett_reduce_wide_helper(input, &modulus, 0); - - input[1] = random_zz(); - input[0] = 0xda9b246b; // random (odd) = 1 (mod 2) - test_barrett_reduce_wide_helper(input, &modulus, 1); - input[0]--; // random (even) = 0 (mod 2) + input[1] = 0; + input[0] = 134012929 << 2; // 29 bits test_barrett_reduce_wide_helper(input, &modulus, 0); + input[1] = 0; + input[0] = MAX_ZZ; + test_barrett_reduce_wide_helper(input, &modulus, 6553567); + input[1] = 0x33345624; // random + input[0] = 0x47193658; // random + test_barrett_reduce_wide_helper(input, &modulus, 77416961); + input[1] = MAX_ZZ; + input[0] = MAX_ZZ; + test_barrett_reduce_wide_helper(input, &modulus, 119980058); - // ------------- - // Set 2: q = 3 - // ------------- - set_modulus(3, &modulus); + set_modulus(1053818881, &modulus); // 30 bits input[1] = 0; input[0] = 0; @@ -176,64 +153,28 @@ void test_barrett_reduce_wide(void) input[1] = 0; input[0] = 1; test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = 456; - input[0] = 123; - test_barrett_reduce_wide_helper(input, &modulus, 0); - input[1] = MAX_ZZ; - input[0] = MAX_ZZ; + input[1] = 0; + input[0] = 1053818880; + test_barrett_reduce_wide_helper(input, &modulus, 1053818880); + input[1] = 0; + input[0] = 1053818881; test_barrett_reduce_wide_helper(input, &modulus, 0); - - // ------------- - // Set 3: max q - // ------------- - set_modulus(MAX_Q, &modulus); - - input[1] = 0; // 1 = 1 (mod q) - input[0] = 1; - test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = 0; // 2 = 2 (mod q) - input[0] = 2; - test_barrett_reduce_wide_helper(input, &modulus, 2); - - // -- Since MAX_ZZ = MAX_Q*2 + 1, MAXZZ % MAX_q = 1 input[1] = 0; - input[0] = MAX_ZZ; + input[0] = 1053818882; test_barrett_reduce_wide_helper(input, &modulus, 1); - - input[1] = MAX_ZZ; - input[0] = MAX_ZZ; - test_barrett_reduce_wide_helper(input, &modulus, 3); - - // -- random x < q ---> x = x (mod q), 32 bit input[1] = 0; - input[0] = 0x76774403; - test_barrett_reduce_wide_helper(input, &modulus, input[0]); - - input[1] = 0x958b2d91; // random > q, 32 bit - input[0] = 0x654b2d21; // random > q, 32 bit - test_barrett_reduce_wide_helper(input, &modulus, 274827334); - - // -------------------------------------------- - // Set 3: random q, 32 bit (q = 1559578058) - // -------------------------------------------- - set_modulus(0x5cf545ca, &modulus); - - input[1] = 0; // 1 = 1 (mod q) - input[0] = 1; - test_barrett_reduce_wide_helper(input, &modulus, 1); - input[1] = 0; // 2 = 2 (mod q) - input[0] = 2; - test_barrett_reduce_wide_helper(input, &modulus, 2); - input[1] = 0; // random x < q ---> x = x (mod q) - input[0] = 0x16774403; - test_barrett_reduce_wide_helper(input, &modulus, input[0]); - + input[0] = (ZZ)(1053818881) << 1; // 31 bits + test_barrett_reduce_wide_helper(input, &modulus, 0); + input[1] = 0; + input[0] = (ZZ)(1053818881) << 2; // 32 bits + test_barrett_reduce_wide_helper(input, &modulus, 0); + input[1] = 0; + input[0] = MAX_ZZ; + test_barrett_reduce_wide_helper(input, &modulus, 79691771); + input[1] = 0x33345624; // random + input[0] = 0x47193658; // random + test_barrett_reduce_wide_helper(input, &modulus, 569939669); input[1] = MAX_ZZ; input[0] = MAX_ZZ; - test_barrett_reduce_wide_helper(input, &modulus, 112593495); - input[1] = 0xd9582d91; // random > q - input[0] = 0x7b813c5b; - test_barrett_reduce_wide_helper(input, &modulus, 25773121); - printf("\n... all tests for barrett_reduce_wide passed.\n"); - printf("***************************************\n"); + test_barrett_reduce_wide_helper(input, &modulus, 159648581); } diff --git a/device/test/network_tests.c b/device/test/network_tests.c index ae6a54c..aefede1 100644 --- a/device/test/network_tests.c +++ b/device/test/network_tests.c @@ -8,17 +8,17 @@ Adapted from Sphere example */ #include "defines.h" - #ifdef SE_ON_SPHERE_A7 - #include - #include - #include - #include - #include - #include // strerror +#ifdef SE_USE_MALLOC +#include +#include +#include +#include +#include +#include // strerror - #include "network.h" - #include "util_print.h" +#include "network.h" +#include "util_print.h" void test_network_basic(void) { @@ -64,12 +64,12 @@ void test_network_basic(void) // // Then, try a POST // - size_t num_data_bytes = 1 * sizeof(ZZ); - ZZ *data = malloc(num_data_bytes); - data[0] = 5; + const size_t num_data_bytes = 1 * sizeof(ZZ); + ZZ *data = malloc(num_data_bytes); + data[0] = 5; struct curl_slist *headers = NULL; - headers = curl_slist_append(headers, "Content-Type: vector"); + headers = curl_slist_append(headers, "Content-Type: vector"); res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); if (is_curl_error(&res, "postfields")) { goto cleanup2; } @@ -84,7 +84,11 @@ void test_network_basic(void) if (is_curl_error(&res, "post")) { goto cleanup2; } cleanup2: - free(data); + if (data) + { + free(data); + data = 0; + } curl_slist_free_all(curl); cleanup: @@ -92,14 +96,18 @@ void test_network_basic(void) curl_global_cleanup(); } -void test_network(void) +void test_network(size_t len) { - size_t len = 1024; - size_t num_data_bytes = len * sizeof(ZZ); - ZZ *data = malloc(num_data_bytes); + const size_t num_data_bytes = len * sizeof(ZZ); + ZZ *data = malloc(num_data_bytes); for (size_t i = 0; i < len; i++) data[i] = (ZZ)i; send_over_network(data, num_data_bytes); - free(data); + if (data) + { + free(data); + data = 0; + } } #endif +#endif diff --git a/device/test/ntt_tests.c b/device/test/ntt_tests.c index 00dd653..5d53576 100644 --- a/device/test/ntt_tests.c +++ b/device/test/ntt_tests.c @@ -6,9 +6,7 @@ */ #include "defines.h" - #ifndef SE_NTT_NONE - #include #include #include // memset @@ -91,7 +89,11 @@ void test_poly_mult_ntt_intt_helper(const Parms *parms, const ZZ *ntt_roots, con const char *right_side_str = " [a * b]_Rq "; // -- First, make sure we can get back the original vector - memcpy(sb_res, a, n * sizeof(a[0])); // sb_res = a + se_assert(sb_res && a); + if (sb_res && a) + { + memcpy(sb_res, a, n * sizeof(a[0])); // sb_res = a + } ntt_inpl(parms, ntt_roots, sb_res); // sb_res = ntt(a) print_poly(" ntt(a) ", sb_res, n); @@ -121,20 +123,25 @@ void test_poly_mult_ntt_intt_helper(const Parms *parms, const ZZ *ntt_roots, con compare_poly(right_side_str, sb_res, left_side_str, a, n); } -void test_poly_mult_ntt(void) +/** +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +@param[in] nprimes # of modulus primes (ignored if SE_USE_MALLOC is defined) +*/ +void test_poly_mult_ntt(size_t n, size_t nprimes) { +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N && nprimes == SE_NPRIMES); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; + if (nprimes != SE_NPRIMES) nprimes = SE_NPRIMES; +#endif + printf("**********************************\n\n"); printf("Beginning tests for poly_mult_mod_ntt"); printf("....\n\n"); - // ================================ - // Configuration - // ================================ - PolySizeType n = 4096; bool intt_mult_test = true; - // ================================ Parms parms; - set_parms_ckks(n, 1, &parms); + set_parms_ckks(n, nprimes, &parms); print_test_banner("Ntt", &parms); #ifdef SE_NTT_OTF @@ -160,8 +167,11 @@ void test_poly_mult_ntt(void) #ifdef SE_USE_MALLOC ZZ *mempool = calloc(mempool_size, sizeof(ZZ)); #else + se_assert(n == SE_DEGREE_N && nprimes == SE_NPRIMES); ZZ mempool_local[4 * SE_DEGREE_N + NTT_TESTS_ROOTS_MEM + INTT_TESTS_ROOTS_MEM]; + se_assert(mempool_size == (4 * SE_DEGREE_N + NTT_TESTS_ROOTS_MEM + INTT_TESTS_ROOTS_MEM)); ZZ *mempool = &(mempool_local[0]); + memset(mempool, 0, mempool_size * sizeof(ZZ)); #endif // clang-format off @@ -212,7 +222,6 @@ void test_poly_mult_ntt(void) for (int testnum = 1; testnum < 14; testnum++) { printf("--------------- Test %d ------------------\n", testnum); - reset_primes(&parms); clear(mempool, mempool_size - ntt_roots_size - intt_roots_size); // Reset for each test switch (testnum) { @@ -298,7 +307,11 @@ void test_poly_mult_ntt(void) break; } #ifdef SE_USE_MALLOC - free(mempool); + if (mempool) + { + free(mempool); + mempool = 0; + } #endif delete_parameters(&parms); } diff --git a/device/test/sample_tests.c b/device/test/sample_tests.c index 0ea529a..583a459 100644 --- a/device/test/sample_tests.c +++ b/device/test/sample_tests.c @@ -16,12 +16,10 @@ Various tests for sampling a polynomial from a distribution. #include "test_common.h" #include "util_print.h" // printf -#ifdef SE_USE_MALLOC -#define TEST_SAMPLE_DEGREE 4096 -#else -#define TEST_SAMPLE_DEGREE SE_DEGREE_N -#endif - +/** +@param[in] s Polynomial to test +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ void test_ternary_poly_stats(ZZ *s, size_t n) { size_t num_zero = 0, num_one = 0, num_other = 0; @@ -36,19 +34,27 @@ void test_ternary_poly_stats(ZZ *s, size_t n) num_other++; } - size_t percent_zero = 100 * num_zero / n; - size_t percent_one = 100 * num_one / n; - size_t percent_other = 100 * num_other / n; + double percent_zero = 100 * (double)num_zero / n; + double percent_one = 100 * (double)num_one / n; + double percent_other = 100 * (double)num_other / n; - size_t threshold_lower = 29; - size_t threshold_upper = 37; + double threshold_lower = 29; + double threshold_upper = 37; - printf("Percent \'0\' values (should be ~33%%) : %0.1f\n", - ((double)num_zero / (double)n) * 100); - printf("Percent \'1\' values (should be ~33%%) : %0.1f\n", - ((double)num_one / (double)n) * 100); - printf("Percent \'other\' values (should be ~33%%) : %0.1f\n", - ((double)num_other / (double)n) * 100); + if (n <= 1024) + { + threshold_lower -= 2; + threshold_upper += 2; + } + else if (n >= 8192) + { + threshold_lower += 1; + threshold_upper -= 1; + } + + printf("Percent \'0\' values (should be ~33%%) : %0.1f\n", percent_zero); + printf("Percent \'1\' values (should be ~33%%) : %0.1f\n", percent_one); + printf("Percent \'other\' values (should be ~33%%) : %0.1f\n", percent_other); if (n > 64) { @@ -63,11 +69,19 @@ void test_ternary_poly_stats(ZZ *s, size_t n) // TODO: Create tests for cbd sampling -void test_sample_poly_uniform(void) +/** +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ +void test_sample_poly_uniform(size_t n) { +#ifndef SE_USE_MALLOC + // -- Sanity check + se_assert(n == SE_DEGREE_N); + if (n != SE_DEGREE_N) n = SE_DEGREE_N; +#endif + printf("\n******************************************\n"); printf("Beginning test for sample_poly_uniform...\n"); - size_t n = TEST_SAMPLE_DEGREE; Parms parms; set_parms_ckks(n, 1, &parms); ZZ q = parms.curr_modulus->value; @@ -80,13 +94,15 @@ void test_sample_poly_uniform(void) #ifdef SE_USE_MALLOC ZZ *a = calloc(n, sizeof(ZZ)); #else - ZZ a[SE_DEGREE_N * sizeof(ZZ)]; + se_assert(n == SE_DEGREE_N); + ZZ a[SE_DEGREE_N]; + memset(&a, 0, SE_DEGREE_N * sizeof(ZZ)); #endif sample_poly_uniform(&parms, &prng, a); // -- Test - size_t num_above = 0; - size_t num_below_eq = 0; + double num_above = 0; + double num_below_eq = 0; for (int i = 0; i < n; i++) { if (a[i] > q / 2) @@ -94,27 +110,54 @@ void test_sample_poly_uniform(void) else num_below_eq++; } - size_t percent_above = 100 * num_above / n; - size_t percent_below_eq = 100 * num_below_eq / n; - size_t threshold_lower = 47; - size_t threshold_upper = 53; + double percent_above = 100 * (double)num_above / n; + double percent_below_eq = 100 * (double)num_below_eq / n; + double threshold_lower = 47; + double threshold_upper = 53; + if (n <= 1024) + { + threshold_lower -= 2; + threshold_upper += 2; + } + else if (n >= 4096) + { + threshold_lower += 1; + threshold_upper -= 1; + } + + printf("Percent of values > \'q/2\' (should be ~50%%) : %0.1f\n", + ((double)num_above / (double)n) * 100); + printf("Percent of values <= \'q/2\' (should be ~50%%) : %0.1f\n", + ((double)num_below_eq / (double)n) * 100); + se_assert(percent_above > threshold_lower); se_assert(percent_below_eq < threshold_upper); print_poly_sign("sampled a", (ZZsign *)a, n); #ifdef SE_USE_MALLOC delete_parameters(&parms); - free(a); + if (a) + { + free(a); + a = 0; + } #endif printf("... done with tests for sample_poly_uniform.\n"); printf("******************************************\n"); } -void test_sample_poly_ternary(void) +/** +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ +void test_sample_poly_ternary(size_t n) { +#ifndef SE_USE_MALLOC + se_assert(n == SE_DEGREE_N); // sanity check + if (n != SE_DEGREE_N) n = SE_DEGREE_N; +#endif + printf("\n******************************************\n"); printf("Beginning test for sample_poly_ternary...\n"); - size_t n = TEST_SAMPLE_DEGREE; Parms parms; set_parms_ckks(n, 1, &parms); print_zz("q", parms.curr_modulus->value); @@ -126,6 +169,7 @@ void test_sample_poly_ternary(void) #ifdef SE_USE_MALLOC ZZ *s = malloc(n * sizeof(ZZ)); #else + se_assert(n == SE_DEGREE_N); ZZ s[SE_DEGREE_N * sizeof(ZZ)]; #endif sample_poly_ternary(&parms, &prng, s); @@ -136,26 +180,31 @@ void test_sample_poly_ternary(void) print_poly("sampled s", s, n); #ifdef SE_USE_MALLOC delete_parameters(&parms); - free(s); + if (s) + { + free(s); + s = 0; + } #endif printf("... done with tests for sample_poly_ternary.\n"); printf("******************************************\n"); } -#ifndef SE_USE_MALLOC -void test_sample_poly_ternary_small(void) +/** +@param[in] n Polynomial ring degree (ignored if SE_USE_MALLOC is defined) +*/ +void test_sample_poly_ternary_small(size_t n) { +#ifndef SE_USE_MALLOC + SE_UNUSED(n); printf("Error. This test is not runnable because SE_USE_MALLOC is not defined.\n"); -} + return; #else -void test_sample_poly_ternary_small(void) -{ printf("\n******************************************\n"); printf("Beginning test for sample_poly_ternary_small...\n"); - size_t n = TEST_SAMPLE_DEGREE; Parms parms; - set_parms_ckks(n, 3, &parms); + set_parms_ckks(n, 1, &parms); print_zz("q", parms.curr_modulus->value); printf("\n"); @@ -167,7 +216,7 @@ void test_sample_poly_ternary_small(void) // --> # of bits required = 2n // --> # of bytes required = 2n/8 = n/4 size_t s_small_nbytes = n / 4; - ZZ *s_small = calloc(s_small_nbytes, 1); + ZZ *s_small = calloc(s_small_nbytes, 1); sample_small_poly_ternary_prng_96(n, &prng, s_small); print_poly_small("s ", s_small, n); ZZ *s_small_save = malloc(s_small_nbytes); @@ -175,7 +224,7 @@ void test_sample_poly_ternary_small(void) // -- Test expansion size_t s_expanded_nbytes = n * sizeof(ZZ); - ZZ *s_expanded = calloc(s_expanded_nbytes, 1); + ZZ *s_expanded = calloc(s_expanded_nbytes, 1); expand_poly_ternary(s_small, &parms, s_expanded); print_poly("s_expanded ", s_expanded, n); test_ternary_poly_stats(s_expanded, n); @@ -207,9 +256,7 @@ void test_sample_poly_ternary_small(void) for (size_t i = 0; i < n; i++) { if (s_inplace[i] == 0 || s_inplace[i] == 1) - { - se_assert(s_inplace[i] == s_expanded[i]); - } + { se_assert(s_inplace[i] == s_expanded[i]); } else { ZZ q = parms.curr_modulus->value; @@ -227,10 +274,24 @@ void test_sample_poly_ternary_small(void) } delete_parameters(&parms); - free(s_inplace); - free(s_expanded); - free(s_small_save); + //clang-format off + if (s_inplace) + { + free(s_inplace); + s_inplace = 0; + } + if (s_expanded) + { + free(s_expanded); + s_expanded = 0; + } + if (s_small_save) + { + free(s_small_save); + s_small_save = 0; + } + //clang-format on printf("... done with tests for sample_poly_ternary_small.\n"); printf("******************************************\n"); -} #endif +} diff --git a/device/test/test_common.h b/device/test/test_common.h index d382a5f..b5eb421 100644 --- a/device/test/test_common.h +++ b/device/test/test_common.h @@ -187,9 +187,8 @@ static inline bool compare_poly_double_complex(const double complex *a, const do return 1; } } - return false; + return 0; } - // ------------------------------------------------- // Set uint // (Note that clear is already defined in defines.h) @@ -237,7 +236,7 @@ static inline void clear_double_complex(double complex *vec, size_t n) static inline void set_double_complex(double complex *vec, size_t n, flpt val) { - for (size_t i = 0; i < n; i++) vec[i] = (double complex)_complex(val, 0); + for (size_t i = 0; i < n; i++) vec[i] = (double complex)_complex((double)val, (double)0); } // ---------------------- @@ -269,25 +268,19 @@ static inline void random_zzq_poly(ZZ *poly, size_t n, Modulus *q) static inline void gen_double_complex_eighth_vec(double complex *vec, int64_t div, size_t n) { for (size_t i = 0; i < n; i++) - { - vec[i] = (double complex)(gen_double_eighth(div), gen_double_eighth(div)); - } + { vec[i] = (double complex)(gen_double_eighth(div), gen_double_eighth(div)); } } static inline void gen_double_complex_quarter_vec(double complex *vec, int64_t div, size_t n) { for (size_t i = 0; i < n; i++) - { - vec[i] = (double complex)(gen_double_quarter(div), gen_double_quarter(div)); - } + { vec[i] = (double complex)(gen_double_quarter(div), gen_double_quarter(div)); } } static inline void gen_double_complex_half_vec(double complex *vec, int64_t div, size_t n) { for (size_t i = 0; i < n; i++) - { - vec[i] = (double complex)(gen_double_half(div), gen_double_half(div)); - } + { vec[i] = (double complex)(gen_double_half(div), gen_double_half(div)); } } static inline void gen_double_complex_vec(double complex *vec, int64_t div, size_t n) diff --git a/device/test/uintmodarith_tests.c b/device/test/uintmodarith_tests.c index 8298b59..6ca2d49 100644 --- a/device/test/uintmodarith_tests.c +++ b/device/test/uintmodarith_tests.c @@ -12,21 +12,25 @@ #include "uintmodarith.h" #include "util_print.h" +// -- Note: This uses % to check void test_add_mod_helper(ZZ val1, ZZ val2, Modulus *modulus, ZZ res_exp) { // -- Recall correctness requirement: val1 + val2 < 2q-1 + se_assert(modulus && modulus->value); ZZ q = modulus->value; printf("---------------------------------\n"); for (int i = 0; i < 2; i++) { printf("( %" PRIuZZ " + %" PRIuZZ " ) %% %" PRIuZZ "\n", val1, val2, q); - ZZ res = add_mod(val1, val2, modulus); + se_assert((val1 + val2) < (((modulus->value) * 2) - 1)); + ZZ res = add_mod(val1, val2, modulus); + ZZ res_default = (val1 + val2) % q; + print_zz("Result ", res); print_zz("Result expected", res_exp); - se_assert(res == res_exp); - - ZZ res_default = (val1 + val2) % q; print_zz("Result default ", res_default); + + se_assert(res == res_exp); se_assert(res == res_default); // -- Swap val1 and val2 and try again @@ -37,6 +41,7 @@ void test_add_mod_helper(ZZ val1, ZZ val2, Modulus *modulus, ZZ res_exp) } } +// -- Note: This uses % to check void test_mul_mod_helper(ZZ val1, ZZ val2, Modulus *modulus, ZZ res_exp) { ZZ q = modulus->value; @@ -45,13 +50,17 @@ void test_mul_mod_helper(ZZ val1, ZZ val2, Modulus *modulus, ZZ res_exp) { printf("( %" PRIuZZ " * %" PRIuZZ " ) %% %" PRIuZZ "\n", val1, val2, q); ZZ res = mul_mod(val1, val2, modulus); - print_zz("Result expected", res_exp); + print_zz("Result ", res); + print_zz("Result expected", res_exp); se_assert(res == res_exp); - ZZ res_default = (val1 * val2) % q; - print_zz("Result default ", res_default); - se_assert(res == res_default); + if (val1 < 0xFFFF && val2 < 0xFFFF) + { + ZZ res_default = (val1 * val2) % q; + print_zz("Result default ", res_default); + se_assert(res == res_default); + } // -- Swap val1 and val2 and try again ZZ temp = val1; @@ -65,17 +74,23 @@ void test_neg_mod_helper(ZZ input, const Modulus *modulus, ZZ res_exp) { ZZ q = modulus->value; printf("---------------------------------\n"); - ZZ res = neg_mod(input, modulus); - printf("( -%" PRIuZZ ") %% %" PRIuZZ "\n", input, q); + se_assert(input <= q); + + // -- Recall correctness requirement: input must be < q + ZZ res = neg_mod(input, modulus); print_zz("Result ", res); print_zz("Result expected", res_exp); - se_assert(res == res_exp); // -- Don't use the remainder operator, it does not work well for modular negation - // ZZ res_default = (ZZ)((int64_t)(-input) % q); - // print_zz("Result default ", res_default); - // se_assert(res == res_default); + // -- Check the slow way + ZZ temp = input; + while (temp > q) temp -= q; + size_t res_basic = (input == 0) ? 0 : q - temp; + print_zz("Result basic ", res_basic); + + se_assert(res == res_exp); + se_assert(res == res_basic); } void test_add_mod_basic(Modulus *modulus) @@ -89,10 +104,10 @@ void test_add_mod_basic(Modulus *modulus) test_add_mod_helper(1, q, modulus, 1); // 1+q = 1 (mod q) test_add_mod_helper(1, q - 1, modulus, 0); // 1+q-1 = 0 (mod q) - test_add_mod_helper(q, q - 1, modulus, q - 1); // q+q-1 = q-1 (mod q) + test_add_mod_helper(q, q - 2, modulus, q - 2); // q+q-2 = q-2 (mod q) test_add_mod_helper(q - 1, q - 1, modulus, q - 2); // q-1+q-1 = q-2 (mod q) - test_add_mod_helper(0, 2 * q - 1, modulus, q - 1); // 0+2q-1 = q-1 (mod q) + test_add_mod_helper(0, 2 * q - 2, modulus, q - 2); // 0+2q-2 = q-2 (mod q) } void test_mul_mod_basic(Modulus *modulus) @@ -106,7 +121,7 @@ void test_mul_mod_basic(Modulus *modulus) test_mul_mod_helper(q - 1, 1, modulus, q - 1); // (q - 1)*1 % q = q - 1 test_mul_mod_helper(0, 12345, modulus, 0); // 0*x % q = 0 - // Can't really calculate expected for these... + // -- Can't really calculate expected for these... test_mul_mod_helper(1, MAX_ZZ, modulus, MAX_ZZ % q); // 1*MAX64 % q = MAX64 % q test_mul_mod_helper(1, 12345, modulus, 12345 % q); // 1*x % q = x % q @@ -127,26 +142,15 @@ void test_add_mod(void) printf("Beginning tests for add_mod...\n\n"); Modulus modulus; - // ------------------------------- - // Set 1: q = 2 (min value of q) - // ------------------------------- - set_modulus(2, &modulus); + set_modulus(134012929, &modulus); // 27 bit test_add_mod_basic(&modulus); + test_add_mod_helper(134012929 - 10, 134012929, &modulus, 134012929 - 10); + test_add_mod_helper(134012929 + 10, 134012929 - 12, &modulus, 134012929 - 2); - // ---------------- - // Set 2: q = 10 - // ---------------- - set_modulus(10, &modulus); - test_add_mod_basic(&modulus); - test_add_mod_helper(7, 7, &modulus, 4); // 7+7 % 10 = 4 - test_add_mod_helper(6, 7, &modulus, 3); // 6+7 % 10 = 3 - - // ----------------------- - // Set 3: max q - // ----------------------- - set_modulus(MAX_Q, &modulus); - test_add_mod_basic(&modulus); + set_modulus(1053818881, &modulus); // 30 bit test_add_mod_basic(&modulus); + test_add_mod_helper(1053818881 - 10, 1053818881, &modulus, 1053818881 - 10); + test_add_mod_helper(1053818881 + 10, 1053818881 - 12, &modulus, 1053818881 - 2); printf("\n...all tests for add_mod passed.\n"); printf("*******************************************\n"); @@ -158,16 +162,15 @@ void test_neg_mod(void) printf("Beginning tests for neg_mod...\n\n"); Modulus modulus; - set_modulus(2, &modulus); - test_neg_mod_basic(&modulus); - - set_modulus(0xFFFF, &modulus); + set_modulus(134012929, &modulus); // 27 bit test_neg_mod_basic(&modulus); - test_neg_mod_helper(1234, &modulus, 64301); + test_neg_mod_helper(10, &modulus, 134012929 - 10); + test_neg_mod_helper(134012929 - 10, &modulus, 10); - set_modulus(0x10000, &modulus); + set_modulus(1053818881, &modulus); // 30 bit test_neg_mod_basic(&modulus); - test_neg_mod_helper(1234, &modulus, 64302); + test_neg_mod_helper(10, &modulus, 1053818881 - 10); + test_neg_mod_helper(1053818881 - 10, &modulus, 10); printf("\n...all tests for neg_mod passed.\n"); printf("*******************************************\n"); @@ -179,31 +182,17 @@ void test_mul_mod(void) printf("Beginning tests for mul_mod...\n\n"); Modulus modulus; - // ------------------------------- - // Set 1: q = 2 (min value of q) - // ------------------------------- - set_modulus(2, &modulus); + set_modulus(134012929, &modulus); // 27 bit test_mul_mod_basic(&modulus); + test_mul_mod_helper(0x38573475, 0x83748563, &modulus, 4025350); // random - // ---------------- - // Set 2: q = 10 - // ---------------- - set_modulus(10, &modulus); - test_mul_mod_basic(&modulus); - test_mul_mod_helper(7, 7, &modulus, 9); - test_mul_mod_helper(6, 7, &modulus, 2); - - // ----------------------- - // Set 3: max q - // ----------------------- - set_modulus(MAX_Q, &modulus); + set_modulus(1053818881, &modulus); // 30 bit test_mul_mod_basic(&modulus); + test_mul_mod_helper(0x38573475, 0x83748563, &modulus, 65334256); // random printf("\n...all tests for mul_mod passed.\n"); printf("*******************************************\n"); } -// TODO: Add more test cases for above functions - // TODO: Add this test // void test_mod_mumo_helper();