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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OpenFHE_jll = "a2687184-f17b-54bc-b2bb-b849352af807"
CxxWrap = "0.17.3"
Preferences = "1.4"
UUIDs = "1"
openfhe_julia_int128_jll = "=0.6.1"
openfhe_julia_jll = "=0.6.1"
openfhe_julia_int128_jll = "=0.6.2"
openfhe_julia_jll = "=0.6.2"
OpenFHE_jll = "1.5.0 - 1.5.1"
julia = "1.10"
2 changes: 1 addition & 1 deletion src/OpenFHE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export GenCryptoContext, Enable, GetKeyGenLevel, SetKeyGenLevel, GetEncodingPara
Relinearize, RelinearizeInPlace, EvalRotate, EvalRotateKeyGen, ComposedEvalMult, Rescale,
RescaleInPlace, ModReduce, ModReduceInPlace, EvalSin, EvalCos, EvalLogistic,
EvalDivide, EvalSumKeyGen, ClearEvalSumKeys, EvalSum, EvalBootstrapSetup, EvalBootstrapKeyGen,
ClearEvalAutomorphismKeys, EvalBootstrap, Compress
EvalBootstrapPrecompute, ClearEvalAutomorphismKeys, EvalBootstrap, Compress

# CryptoContextFactory
export CryptoContextFactory
Expand Down
17 changes: 17 additions & 0 deletions src/convenience.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ for (WrappedT, fun) in [
:(CryptoContext{DCRTPoly}) => :EvalSum,
:(CryptoContext{DCRTPoly}) => :EvalBootstrapSetup,
:(CryptoContext{DCRTPoly}) => :EvalBootstrapKeyGen,
:(CryptoContext{DCRTPoly}) => :EvalBootstrapPrecompute,
:(CryptoContext{DCRTPoly}) => :EvalBootstrap,
:(CryptoContext{DCRTPoly}) => :Compress,
:(Plaintext) => :GetScalingFactor,
Expand Down Expand Up @@ -307,6 +308,22 @@ function EvalBootstrap(context::CxxWrap.CxxWrapCore.CxxRef{OpenFHE.CryptoContext
EvalBootstrap(context, ciphertext, num_iterations, precision)
end

"""
EvalBootstrapPrecompute(crypto_context::CryptoContext; slots = 0)

Precomputes and stores the plaintexts for encoding and decoding used in bootstrapping, if not already done in EvalBootstrapSetup.
Supported only in CKKS.

# Arguments
- `slots::Unsigned`: The number of slots to be bootstrapped.

See also: [`CryptoContext`](@ref), [`EvalBootstrapSetup`](@ref), [`EvalBootstrap`](@ref)
"""
function EvalBootstrapPrecompute(context::CxxWrap.CxxWrapCore.CxxRef{OpenFHE.CryptoContextImpl{OpenFHE.DCRTPoly}};
Comment thread
Tom-Finke marked this conversation as resolved.
slots = 0)
EvalBootstrapPrecompute(context, slots)
end

"""
GetCryptoContext(object::Union{Ciphertext})

Expand Down
148 changes: 148 additions & 0 deletions test/test_ckks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,154 @@ ReleaseAllContexts()
@test GetContextCount() == 0
end


@testset verbose=true showtiming=true "EvalBootstrap" begin
# This is essentially OpenFHE.jl/examples/simple_ckks_bootstrapping.jl as a unit test

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;

if OpenFHE.get_native_int() == 128
rescale_technique = FIXEDAUTO;
dcrt_bits = 78;
first_modulus = 89;
end

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)

EvalBootstrapSetup(cc; level_budget)

key_pair = KeyGen(cc)
pubkey = OpenFHE.public_key(key_pair)
privkey = OpenFHE.private_key(key_pair)
EvalMultKeyGen(cc, privkey);
EvalBootstrapKeyGen(cc, privkey, num_slots);

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

# We start with a depleted ciphertext that has used up all of its levels.
ptxt = MakeCKKSPackedPlaintext(cc, x; scale_degree = 1, level = depth - 1)

SetLength(ptxt, encoded_length)

ciphertext = Encrypt(cc, pubkey, ptxt)


# Perform the bootstrapping operation. The goal is to increase the number of levels
# remaining for HE computation.
ciphertext_after = EvalBootstrap(cc, ciphertext)

@test GetLevel(ciphertext_after) < GetLevel(ciphertext) # test that we actually reduced the levels by bootstrapping
result = Plaintext()
Decrypt(cc, privkey, ciphertext_after, result);
SetLength(result, encoded_length)
@test GetRealPackedValue(result) ≈ x rtol=1e-4
end

@testset verbose=true showtiming=true "EvalBootstrapPrecompute" begin
# Same test as before, but with a separate precompute step

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;

if OpenFHE.get_native_int() == 128
rescale_technique = FIXEDAUTO;
dcrt_bits = 78;
first_modulus = 89;
end

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)

EvalBootstrapSetup(cc; level_budget, precompute=false)
EvalBootstrapPrecompute(cc)

key_pair = KeyGen(cc)
pubkey = OpenFHE.public_key(key_pair)
privkey = OpenFHE.private_key(key_pair)
EvalMultKeyGen(cc, privkey);
EvalBootstrapKeyGen(cc, privkey, num_slots);

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

# We start with a depleted ciphertext that has used up all of its levels.
ptxt = MakeCKKSPackedPlaintext(cc, x; scale_degree = 1, level = depth - 1)

SetLength(ptxt, encoded_length)

ciphertext = Encrypt(cc, pubkey, ptxt)


# Perform the bootstrapping operation. The goal is to increase the number of levels
# remaining for HE computation.
ciphertext_after = EvalBootstrap(cc, ciphertext)

@test GetLevel(ciphertext_after) < GetLevel(ciphertext) # test that we actually reduced the levels by bootstrapping
result = Plaintext()
Decrypt(cc, privkey, ciphertext_after, result);
SetLength(result, encoded_length)
@test GetRealPackedValue(result) ≈ x rtol=1e-4
end

ClearEvalMultKeys("42")
ClearEvalMultKeys(cc)
ClearEvalMultKeys()
Expand Down
Loading