|
| 1 | +push!(LOAD_PATH, "../src") |
| 2 | + |
| 3 | +using TFHE |
| 4 | + |
| 5 | + |
| 6 | +function encrypt() |
| 7 | + # generate a keyset |
| 8 | + minimum_lambda = 110 |
| 9 | + params = new_default_gate_bootstrapping_parameters(minimum_lambda) |
| 10 | + |
| 11 | + # generate a random key |
| 12 | + seed = UInt32[314, 1592, 657] |
| 13 | + tfhe_random_generator_setSeed(seed) |
| 14 | + |
| 15 | + key = new_random_gate_bootstrapping_secret_keyset(params) |
| 16 | + |
| 17 | + # generate encrypt the 16 bits of 2017 |
| 18 | + plaintext1 = Int16(2017) |
| 19 | + ciphertext1 = new_gate_bootstrapping_ciphertext_array(16, params) |
| 20 | + for i in 0:15 |
| 21 | + bootsSymEncrypt(ciphertext1[i+1], ((plaintext1>>i) & 1) != 0, key) |
| 22 | + end |
| 23 | + |
| 24 | + # generate encrypt the 16 bits of 42 |
| 25 | + plaintext2 = Int16(42) |
| 26 | + ciphertext2 = new_gate_bootstrapping_ciphertext_array(16, params) |
| 27 | + for i in 0:15 |
| 28 | + bootsSymEncrypt(ciphertext2[i+1], ((plaintext2>>i) & 1) != 0, key) |
| 29 | + end |
| 30 | + |
| 31 | + |
| 32 | + key, key.cloud, ciphertext1, ciphertext2 |
| 33 | +end |
| 34 | + |
| 35 | + |
| 36 | +# elementary full comparator gate that is used to compare the i-th bit: |
| 37 | +# input: ai and bi the i-th bit of a and b |
| 38 | +# lsb_carry: the result of the comparison on the lowest bits |
| 39 | +# algo: if (a==b) return lsb_carry else return b |
| 40 | +function compare_bit( |
| 41 | + result::LweSample, a::LweSample, b::LweSample, lsb_carry::LweSample, |
| 42 | + tmp::LweSample, bk::TFheGateBootstrappingCloudKeySet) |
| 43 | + bootsXNOR(tmp, a, b, bk) |
| 44 | + bootsMUX(result, tmp, lsb_carry, a, bk) |
| 45 | +end |
| 46 | + |
| 47 | +# this function compares two multibit words, and puts the max in result |
| 48 | +function minimum( |
| 49 | + result::Array{LweSample, 1}, a::Array{LweSample, 1}, b::Array{LweSample, 1}, nb_bits::Int, |
| 50 | + bk::TFheGateBootstrappingCloudKeySet) |
| 51 | + |
| 52 | + tmps = new_gate_bootstrapping_ciphertext_array(2, bk.params) |
| 53 | + |
| 54 | + # initialize the carry to 0 |
| 55 | + bootsCONSTANT(tmps[1], Int32(0), bk) |
| 56 | + # run the elementary comparator gate n times |
| 57 | + for i in 0:(nb_bits-1) |
| 58 | + compare_bit(tmps[1], a[i+1], b[i+1], tmps[0+1], tmps[1+1], bk) |
| 59 | + end |
| 60 | + |
| 61 | + # tmps[0] is the result of the comparaison: 0 if a is larger, 1 if b is larger |
| 62 | + # select the max and copy it to the result |
| 63 | + for i in 0:(nb_bits-1) |
| 64 | + bootsMUX(result[i+1], tmps[0+1], b[i+1], a[i+1], bk) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | + |
| 69 | +function process(cloud_key, ciphertext1, ciphertext2) |
| 70 | + |
| 71 | + # if necessary, the params are inside the key |
| 72 | + params = cloud_key.params |
| 73 | + |
| 74 | + # do some operations on the ciphertexts: here, we will compute the |
| 75 | + # minimum of the two |
| 76 | + result = new_gate_bootstrapping_ciphertext_array(16, params) |
| 77 | + minimum(result, ciphertext1, ciphertext2, 16, cloud_key) |
| 78 | + |
| 79 | + result |
| 80 | +end |
| 81 | + |
| 82 | + |
| 83 | +function verify(secret_key, answer) |
| 84 | + |
| 85 | + # if necessary, the params are inside the key |
| 86 | + params = secret_key.params |
| 87 | + |
| 88 | + # decrypt and rebuild the 16-bit plaintext answer |
| 89 | + int_answer = Int16(0) |
| 90 | + for i in 0:15 |
| 91 | + ai = bootsSymDecrypt(answer[i+1], secret_key) |
| 92 | + int_answer |= (ai<<i) |
| 93 | + end |
| 94 | + |
| 95 | + println("Answer: $int_answer") |
| 96 | +end |
| 97 | + |
| 98 | + |
| 99 | +secret_key, cloud_key, ciphertext1, ciphertext2 = encrypt() |
| 100 | +answer = process(cloud_key, ciphertext1, ciphertext2) |
| 101 | +verify(secret_key, answer) |
0 commit comments