Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(capi): fix C API example #752

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tfhe/c_api_tests/test_c_doc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// If this test break the c_api doc needs to be updated

#include <tfhe.h>

#include <assert.h>
#include <stdio.h>

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;
}
25 changes: 14 additions & 11 deletions tfhe/docs/how_to/c_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ $ cmake .. -DCMAKE_BUILD_TYPE=RELEASE
$ make
...
$ ./my-executable
Result: 2
FHE computation successful!
$
```

```c
#include <tfhe.h>

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>

int main(void)
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -134,6 +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;
}
```
Loading