From 7abae488cdaf2fd905ca6bd8f5b360ed53406387 Mon Sep 17 00:00:00 2001 From: tmontaigu Date: Wed, 3 Jan 2024 00:43:53 +0100 Subject: [PATCH 1/3] docs(capi): fix C API example --- tfhe/docs/how_to/c_api.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tfhe/docs/how_to/c_api.md b/tfhe/docs/how_to/c_api.md index c4edceea03..2d6b2d1263 100644 --- a/tfhe/docs/how_to/c_api.md +++ b/tfhe/docs/how_to/c_api.md @@ -76,7 +76,6 @@ $ #include #include -#include #include int main(void) @@ -89,7 +88,7 @@ int main(void) // Put the builder in a default state without any types enabled config_builder_all_disabled(&builder); // Enable the uint128 type using the small LWE key for encryption - config_builder_enable_default_uint128_small(&builder); + config_builder_enable_default_integers_small(&builder); // Populate the config config_builder_build(builder, &config); @@ -104,27 +103,29 @@ int main(void) FheUint128 *lhs = NULL; FheUint128 *rhs = NULL; FheUint128 *result = NULL; + // A 128-bit unsigned integer containing value: 20 << 64 | 10 + U128 clear_lhs = { .w0 = 10, .w1 = 20 }; + // A 128-bit unsigned integer containing value: 2 << 64 | 1 + U128 clear_rhs = { .w0 = 1, .w1 = 2 }; - // Encrypt a u128 using 64 bits words, we encrypt 20 << 64 | 10 - ok = fhe_uint128_try_encrypt_with_client_key_u128(10, 20, client_key, &lhs); + ok = fhe_uint128_try_encrypt_with_client_key_u128(clear_lhs, client_key, &lhs); assert(ok == 0); - // Encrypt a u128 using words, we encrypt 2 << 64 | 1 - ok = fhe_uint128_try_encrypt_with_client_key_u128(1, 2, client_key, &rhs); + ok = fhe_uint128_try_encrypt_with_client_key_u128(clear_rhs, client_key, &rhs); assert(ok == 0); // Compute the subtraction ok = fhe_uint128_sub(lhs, rhs, &result); assert(ok == 0); - uint64_t w0, w1; + U128 clear_result; // Decrypt - ok = fhe_uint128_decrypt(result, client_key, &w0, &w1); + ok = fhe_uint128_decrypt(result, client_key, &clear_result); assert(ok == 0); // Here the subtraction allows us to compare each word - assert(w0 == 9); - assert(w1 == 18); + assert(clear_result.w0 == 9); + assert(clear_result.w1 == 18); // Destroy the ciphertexts fhe_uint128_destroy(lhs); @@ -136,4 +137,5 @@ int main(void) server_key_destroy(server_key); return EXIT_SUCCESS; } + ``` From 810d7794aedaf8acdab943513fa4266c020e66ed Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Wed, 3 Jan 2024 10:07:03 +0100 Subject: [PATCH 2/3] doc(c_api): Add an output for the users compiling the C API example --- tfhe/docs/how_to/c_api.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tfhe/docs/how_to/c_api.md b/tfhe/docs/how_to/c_api.md index 2d6b2d1263..4342d20a8d 100644 --- a/tfhe/docs/how_to/c_api.md +++ b/tfhe/docs/how_to/c_api.md @@ -68,7 +68,7 @@ $ cmake .. -DCMAKE_BUILD_TYPE=RELEASE $ make ... $ ./my-executable -Result: 2 +FHE computation successful! $ ``` @@ -135,7 +135,8 @@ int main(void) // Destroy the keys client_key_destroy(client_key); server_key_destroy(server_key); + + printf("FHE computation successful!\n"); return EXIT_SUCCESS; } - ``` From 4e3ee6d0c1a5b897291039393f5fc4d7f9de1fd9 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Wed, 3 Jan 2024 10:07:10 +0100 Subject: [PATCH 3/3] chore(c_api): add the c_api code from the docs as a test --- tfhe/c_api_tests/test_c_doc.c | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tfhe/c_api_tests/test_c_doc.c diff --git a/tfhe/c_api_tests/test_c_doc.c b/tfhe/c_api_tests/test_c_doc.c new file mode 100644 index 0000000000..5e7b42c09f --- /dev/null +++ b/tfhe/c_api_tests/test_c_doc.c @@ -0,0 +1,68 @@ +// If this test break the c_api doc needs to be updated + +#include + +#include +#include + +int main(void) +{ + int ok = 0; + // Prepare the config builder for the high level API and choose which types to enable + ConfigBuilder *builder; + Config *config; + + // Put the builder in a default state without any types enabled + config_builder_all_disabled(&builder); + // Enable the uint128 type using the small LWE key for encryption + config_builder_enable_default_integers_small(&builder); + // Populate the config + config_builder_build(builder, &config); + + ClientKey *client_key = NULL; + ServerKey *server_key = NULL; + + // Generate the keys using the config + generate_keys(config, &client_key, &server_key); + // Set the server key for the current thread + set_server_key(server_key); + + FheUint128 *lhs = NULL; + FheUint128 *rhs = NULL; + FheUint128 *result = NULL; + // A 128-bit unsigned integer containing value: 20 << 64 | 10 + U128 clear_lhs = { .w0 = 10, .w1 = 20 }; + // A 128-bit unsigned integer containing value: 2 << 64 | 1 + U128 clear_rhs = { .w0 = 1, .w1 = 2 }; + + ok = fhe_uint128_try_encrypt_with_client_key_u128(clear_lhs, client_key, &lhs); + assert(ok == 0); + + ok = fhe_uint128_try_encrypt_with_client_key_u128(clear_rhs, client_key, &rhs); + assert(ok == 0); + + // Compute the subtraction + ok = fhe_uint128_sub(lhs, rhs, &result); + assert(ok == 0); + + U128 clear_result; + // Decrypt + ok = fhe_uint128_decrypt(result, client_key, &clear_result); + assert(ok == 0); + + // Here the subtraction allows us to compare each word + assert(clear_result.w0 == 9); + assert(clear_result.w1 == 18); + + // Destroy the ciphertexts + fhe_uint128_destroy(lhs); + fhe_uint128_destroy(rhs); + fhe_uint128_destroy(result); + + // Destroy the keys + client_key_destroy(client_key); + server_key_destroy(server_key); + + printf("FHE computation successful!\n"); + return EXIT_SUCCESS; +}