Skip to content
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
93 changes: 93 additions & 0 deletions examples/simple_ckks_bootstrapping.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using SecureArithmetic
using OpenFHE

# Note: this is a significantly truncated version of the original
# `simple_ckks_bootstrapping.jl` example in OpenFHE.jl, due to some of the properties (such
# as creating a packed plaintext with additional properties) not yet being sufficiently
# generalized.
function simple_ckks_bootstrapping(context)
public_key, private_key = generate_keys(context)

init_multiplication(context, private_key)
init_bootstrapping(context, private_key)

x = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
encoded_length = length(x)

pv = PlainVector(context, x)
println("Input: ", pv)

sv = encrypt(context, public_key, pv)

# Perform the bootstrapping operation. The goal is to increase the number of levels
# remaining for HE computation.
sv_after = bootstrap!(context, sv)

result = decrypt(context, private_key, sv)
println("Output after bootstrapping \n\t", result)
end


################################################################################
println("="^80)
println("Creating OpenFHE context...")

parameters = CCParams{CryptoContextCKKSRNS}()

secret_key_distribution = UNIFORM_TERNARY
SetSecretKeyDist(parameters, secret_key_distribution)

SetSecurityLevel(parameters, HEStd_NotSet)
SetRingDim(parameters, 1 << 12)

rescale_technique = FLEXIBLEAUTO
dcrt_bits = 59
first_modulus = 60

SetScalingModSize(parameters, dcrt_bits)
SetScalingTechnique(parameters, rescale_technique)
SetFirstModSize(parameters, first_modulus)

level_budget = [4, 4]

levels_available_after_bootstrap = 10
depth = levels_available_after_bootstrap + GetBootstrapDepth(level_budget, secret_key_distribution)
SetMultiplicativeDepth(parameters, depth)

cc = GenCryptoContext(parameters)

Enable(cc, PKE)
Enable(cc, KEYSWITCH)
Enable(cc, LEVELEDSHE)
Enable(cc, ADVANCEDSHE)
Enable(cc, FHE)

ring_dimension = GetRingDimension(cc)
# This is the maximum number of slots that can be used for full packing.
num_slots = div(ring_dimension, 2)
println("CKKS scheme is using ring dimension ", ring_dimension)
println()

EvalBootstrapSetup(cc; level_budget)

context_openfhe = SecureContext(OpenFHEBackend(cc))


################################################################################
println("="^80)
println("Creating unencrypted context...")
println()

context_unencrypted = SecureContext(Unencrypted())


################################################################################
println("="^80)
println("simple_ckks_bootstrapping with an OpenFHE context")
simple_ckks_bootstrapping(context_openfhe)


################################################################################
println("="^80)
println("simple_ckks_bootstrapping with an Unencrypted context")
simple_ckks_bootstrapping(context_unencrypted)
1 change: 1 addition & 0 deletions examples/simple_real_numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ context_openfhe = SecureContext(OpenFHEBackend(cc))
################################################################################
println("="^80)
println("Creating unencrypted context...")
println()

context_unencrypted = SecureContext(Unencrypted())

Expand Down
3 changes: 2 additions & 1 deletion src/SecureArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export SecureContext, SecureVector, PlainVector
export Unencrypted, OpenFHEBackend

# Crypto operations
export generate_keys, init_multiplication, init_rotation, encrypt, decrypt
export generate_keys, init_multiplication, init_rotation, init_bootstrapping
export encrypt, decrypt, decrypt!, bootstrap!

include("types.jl")
include("openfhe.jl")
Expand Down
23 changes: 23 additions & 0 deletions src/openfhe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ function init_rotation(context::SecureContext{<:OpenFHEBackend}, private_key, sh
nothing
end

function init_bootstrapping(context::SecureContext{<:OpenFHEBackend}, private_key)
cc = get_crypto_context(context)
ring_dimension = OpenFHE.GetRingDimension(cc)
num_slots = div(ring_dimension, 2)
OpenFHE.EvalBootstrapKeyGen(cc, private_key.private_key, num_slots)

nothing
end

function PlainVector(context::SecureContext{<:OpenFHEBackend}, data::Vector{<:Real})
cc = get_crypto_context(context)
plaintext = OpenFHE.MakeCKKSPackedPlaintext(cc, data)
Expand Down Expand Up @@ -74,6 +83,20 @@ function decrypt(context::SecureContext{<:OpenFHEBackend}, private_key, secure_v
decrypt!(plain_vector, context, private_key, secure_vector)
end


function bootstrap!(context::SecureContext{<:OpenFHEBackend}, secure_vector)
cc = get_crypto_context(context)
OpenFHE.EvalBootstrap(cc, secure_vector.ciphertext)

secure_vector
end
function bootstrap!(context::SecureContext{<:OpenFHEBackend}, secure_vector)
cc = get_crypto_context(context)
OpenFHE.EvalBootstrap(cc, secure_vector.ciphertext)

secure_vector
end

function add(sv1::SecureVector{<:OpenFHEBackend}, sv2::SecureVector{<:OpenFHEBackend})
cc = get_crypto_context(sv1)
ciphertext = OpenFHE.EvalAdd(cc, sv1.ciphertext, sv2.ciphertext)
Expand Down
5 changes: 5 additions & 0 deletions src/unencrypted.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end

init_multiplication(context::SecureContext{<:Unencrypted}, private_key) = nothing
init_rotation(context::SecureContext{<:Unencrypted}, private_key, shifts) = nothing
init_bootstrapping(context::SecureContext{<:Unencrypted}, private_key) = nothing

function PlainVector(context::SecureContext{<:Unencrypted}, data::Vector{<:Real})
plain_vector = PlainVector(data, context)
Expand All @@ -25,6 +26,8 @@ end
function decrypt!(plain_vector, context::SecureContext{<:Unencrypted}, private_key,
secure_vector)
plain_vector.plaintext .= secure_vector.ciphertext

plain_vector
end

function decrypt(context::SecureContext{<:Unencrypted}, private_key, secure_vector)
Expand All @@ -33,6 +36,8 @@ function decrypt(context::SecureContext{<:Unencrypted}, private_key, secure_vect
decrypt!(plain_vector, context, private_key, secure_vector)
end

bootstrap!(context::SecureContext{<:Unencrypted}, secure_vector) = secure_vector

function add(sv1::SecureVector{<:Unencrypted}, sv2::SecureVector{<:Unencrypted})
SecureVector(sv1.ciphertext .+ sv2.ciphertext, sv1.context)
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ using SecureArithmetic
@test_nowarn include("../examples/simple_real_numbers.jl")
end

@testset verbose=true showtiming=true "examples/simple_ckks_bootstrapping.jl" begin
@test_nowarn include("../examples/simple_ckks_bootstrapping.jl")
end

end # @testset "test_examples.jl"

end # module
Expand Down