-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulti_alice.c
More file actions
66 lines (54 loc) · 2.44 KB
/
Multi_alice.c
File metadata and controls
66 lines (54 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <tfhe/tfhe.h>
#include <tfhe/tfhe_io.h>
#include <stdio.h>
int main() {
//generate a keyset
const int minimum_lambda = 110;
TFheGateBootstrappingParameterSet* params = new_default_gate_bootstrapping_parameters(minimum_lambda);
//generate a random key
uint32_t seed[] = { 314, 1592, 657 };
tfhe_random_generator_setSeed(seed,3);
TFheGateBootstrappingSecretKeySet* key = new_random_gate_bootstrapping_secret_keyset(params);
//generate encrypt the 16 bits of 2017
int16_t plaintext1 = 2;
LweSample* ciphertext1 = new_gate_bootstrapping_ciphertext_array(16, params);
for (int i=0; i<16; i++) {
bootsSymEncrypt(&ciphertext1[i], (plaintext1>>i)&1, key);
}
//generate encrypt the 16 bits of 42
int16_t plaintext2 = 2;
LweSample* ciphertext2 = new_gate_bootstrapping_ciphertext_array(16, params);
for (int i=0; i<16; i++) {
bootsSymEncrypt(&ciphertext2[i], (plaintext2>>i)&1, key);
}
//carry
int16_t plaintext3 = 0;
LweSample* ciphertext3 = new_gate_bootstrapping_ciphertext_array(16, params);
for (int i = 0; i<16; i++) {
bootsSymEncrypt(&ciphertext3[i], (plaintext3 >> i) & 1, key);
}
printf("Hi there! Today, I will ask the cloud what is the sum of %d and %d\n",plaintext1, plaintext2);
//export the secret key to file for later use
FILE* secret_key = fopen("secret.key","wb");
export_tfheGateBootstrappingSecretKeySet_toFile(secret_key, key);
fclose(secret_key);
//export the cloud key to a file (for the cloud)
FILE* cloud_key = fopen("cloud.key","wb");
export_tfheGateBootstrappingCloudKeySet_toFile(cloud_key, &key->cloud);
fclose(cloud_key);
//export the 32 ciphertexts to a file (for the cloud)
FILE* cloud_data = fopen("cloud.data","wb");
for (int i=0; i<16; i++)
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext1[i], params);
for (int i=0; i<16; i++)
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext2[i], params);
for (int i = 0; i<16; i++)
export_gate_bootstrapping_ciphertext_toFile(cloud_data, &ciphertext3[i], params);
fclose(cloud_data);
//clean up all pointers
delete_gate_bootstrapping_ciphertext_array(16, ciphertext2);
delete_gate_bootstrapping_ciphertext_array(16, ciphertext1);
delete_gate_bootstrapping_ciphertext_array(16, ciphertext3);
delete_gate_bootstrapping_secret_keyset(key);
delete_gate_bootstrapping_parameters(params);
}