Skip to content
This repository was archived by the owner on May 2, 2025. It is now read-only.

Commit bf33742

Browse files
committed
Initial working port
1 parent e35d601 commit bf33742

File tree

10 files changed

+3010
-2
lines changed

10 files changed

+3010
-2
lines changed

examples/tutorial.jl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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)

src/TFHE.jl

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,74 @@
11
module TFHE
22

3-
# package code goes here
3+
#=
4+
tfhe_core.h
5+
tfhe_garbage_collector.h
6+
tfhe_generic_streams.h
7+
tfhe_generic_templates.h
8+
tfhe_io.h
49
5-
end # module
10+
tfhe.h
11+
tfhe_garbage_collector.cpp
12+
tfhe_generic_streams.cpp
13+
tfhe_io.cpp
14+
=#
15+
16+
include("numeric-functions.jl")
17+
export tfhe_random_generator_setSeed
18+
19+
include("polynomials.jl")
20+
21+
include("lwe.jl")
22+
export LweSample
23+
24+
include("tlwe.jl")
25+
26+
include("tgsw.jl")
27+
28+
include("lwe-bootstrapping.jl")
29+
30+
include("tfhe-gate-bootstrapping.jl")
31+
export new_default_gate_bootstrapping_parameters
32+
export new_random_gate_bootstrapping_secret_keyset
33+
export new_gate_bootstrapping_ciphertext_array
34+
export bootsSymEncrypt
35+
export bootsSymDecrypt
36+
37+
export TFheGateBootstrappingCloudKeySet
38+
39+
include("boot-gates.jl")
40+
export bootsXNOR
41+
export bootsCONSTANT
42+
export bootsMUX
43+
export bootsAND
44+
export bootsNOT
45+
46+
47+
import Base: ==
48+
49+
50+
function recursive_eq(x1, x2)
51+
if typeof(x1) != typeof(x2)
52+
return false
53+
end
54+
fs = fieldnames(x1)
55+
if length(fs) > 0
56+
all(recursive_eq(getfield(x1, f), getfield(x2, f)) for f in fs)
57+
else
58+
(x1 == x2)
59+
end
60+
end
61+
62+
==(a::IntPolynomial, b::IntPolynomial) = recursive_eq(a, b)
63+
==(a::TorusPolynomial, b::TorusPolynomial) = recursive_eq(a, b)
64+
==(a::TGswSample, b::TGswSample) = recursive_eq(a, b)
65+
==(a::TLweSample, b::TLweSample) = recursive_eq(a, b)
66+
==(a::LweSample, b::LweSample) = recursive_eq(a, b)
67+
==(a::LweBootstrappingKey, b::LweBootstrappingKey) = recursive_eq(a, b)
68+
==(a::LweBootstrappingKeyFFT, b::LweBootstrappingKeyFFT) = recursive_eq(a, b)
69+
==(a::TGswSampleFFT, b::TGswSampleFFT) = recursive_eq(a, b)
70+
==(a::TLweSampleFFT, b::TLweSampleFFT) = recursive_eq(a, b)
71+
==(a::LagrangeHalfCPolynomial, b::LagrangeHalfCPolynomial) = recursive_eq(a, b)
72+
73+
74+
end

0 commit comments

Comments
 (0)