Skip to content

Commit

Permalink
Add BGV example (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloede authored Feb 13, 2024
1 parent 12ea358 commit 20ef53f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
75 changes: 75 additions & 0 deletions examples/simple_integers_bgvrns.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using OpenFHE

plaintext_modulus = 65537
multiplicative_depth = 2

parameters = CCParams{CryptoContextBGVRNS}()
SetPlaintextModulus(parameters, plaintext_modulus)
SetMultiplicativeDepth(parameters, multiplicative_depth)

cc = GenCryptoContext(parameters)
Enable(cc, PKE)
Enable(cc, KEYSWITCH)
Enable(cc, LEVELEDSHE)

keys = KeyGen(cc)
privkey = OpenFHE.private_key(keys)
pubkey = OpenFHE.public_key(keys)
EvalMultKeyGen(cc, privkey)

EvalRotateKeyGen(cc, privkey, [1, 2, -1, -2])

vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext1 = MakePackedPlaintext(cc, vectorOfInts1)
vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext2 = MakePackedPlaintext(cc, vectorOfInts2)
vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext3 = MakePackedPlaintext(cc, vectorOfInts3)

ct1 = Encrypt(cc, pubkey, plaintext1)
ct2 = Encrypt(cc, pubkey, plaintext2)
ct3 = Encrypt(cc, pubkey, plaintext3)

ct_add12 = EvalAdd(cc, ct1, ct2);
ct_add_result = EvalAdd(cc, ct_add12, ct3);

ct_mult12 = EvalMult(cc, ct1, ct2);
ct_mult_result = EvalMult(cc, ct_mult12, ct3);

ct_rot1 = EvalRotate(cc, ct1, 1);
ct_rot2 = EvalRotate(cc, ct1, 2);
ct_rot3 = EvalRotate(cc, ct1, -1);
ct_rot4 = EvalRotate(cc, ct1, -2);

pt_add_result = Plaintext()
Decrypt(cc, privkey, ct_add_result, pt_add_result)

pt_mult_result = Plaintext()
Decrypt(cc, privkey, ct_mult_result, pt_mult_result)

pt_rot1 = Plaintext()
Decrypt(cc, privkey, ct_rot1, pt_rot1)
pt_rot2 = Plaintext()
Decrypt(cc, privkey, ct_rot2, pt_rot2)
pt_rot3 = Plaintext()
Decrypt(cc, privkey, ct_rot3, pt_rot3)
pt_rot4 = Plaintext()
Decrypt(cc, privkey, ct_rot4, pt_rot4)

SetLength(pt_rot1, length(vectorOfInts1))
SetLength(pt_rot2, length(vectorOfInts1))
SetLength(pt_rot3, length(vectorOfInts1))
SetLength(pt_rot4, length(vectorOfInts1))

println("Plaintext #1: ", plaintext1)
println("Plaintext #2: ", plaintext2)
println("Plaintext #3: ", plaintext3)

println()
println("Results of homomorphic computations")
println("#1 + #2 + #3: ", pt_add_result)
println("#1 * #2 * #3: ", pt_mult_result)
println("Left rotation of #1 by 1: ", pt_rot1)
println("Left rotation of #1 by 2: ", pt_rot2)
println("Right rotation of #1 by 1: ", pt_rot3)
println("Right rotation of #1 by 2: ", pt_rot4)
2 changes: 1 addition & 1 deletion src/OpenFHE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ end
export DCRTPoly

# CCParams
export CCParams, CryptoContextBFVRNS, CryptoContextCKKSRNS
export CCParams, CryptoContextBFVRNS, CryptoContextBGVRNS, CryptoContextCKKSRNS
export GetPlaintextModulus, GetDigitSize, GetStandardDeviation, GetSecretKeyDist,
GetMaxRelinSkDeg, GetNoiseEstimate, GetDesiredPrecision, GetStatisticalSecurity,
GetNumAdversarialQueries, GetThresholdNumOfParties, GetKeySwitchTechnique,
Expand Down
4 changes: 3 additions & 1 deletion src/convenience.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ end
# Note: remember to add tests to `test/test_convenience.jl` if you add something here
for (T, str) in [
:(CCParams{<:CryptoContextBFVRNS}) => "CCParams{CryptoContextBFVRNS}()",
:(CCParams{<:CryptoContextBGVRNS}) => "CCParams{CryptoContextBGVRNS}()",
:(CCParams{<:CryptoContextCKKSRNS}) => "CCParams{CryptoContextCKKSRNS}()",
:(CryptoContextBFVRNS) => "CryptoContextBFVRNS()",
:(CryptoContextBGVRNS) => "CryptoContextBGVRNS()",
:(CryptoContextCKKSRNS) => "CryptoContextCKKSRNS()",
:(CryptoContext{DCRTPoly}) => "CryptoContext{DCRTPoly}()",
:(Ciphertext{DCRTPoly}) => "Ciphertext{DCRTPoly}()",
Expand Down Expand Up @@ -221,7 +223,7 @@ end
noise_scale_degree = 1,
level = 0)
Encode a vector of integers `value` into a BFV-packed [`Plaintext`](@ref) using the
Encode a vector of integers `value` into a BFV/BGV-packed [`Plaintext`](@ref) using the
given `crypto_context`.
Please refer to the OpenFHE documentation for details on the remaining arguments.
Expand Down
15 changes: 14 additions & 1 deletion src/documentation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ See also: [`CCParams`](@ref)
"""
CryptoContextBFVRNS

"""
CryptoContextBGVRNS
A type used as a parameter to `CCParams` to indicate that parameters for BGV-based
encryptions are to be stored.
See also: [`CCParams`](@ref)
"""
CryptoContextBGVRNS

"""
CryptoContextCKKSRNS
Expand All @@ -40,10 +50,12 @@ Type to store parameters for a generating a cryptographic context in OpenFHE usi
Use `CCParams{CryptoContextBFVRNS}()` to create a parameter store that can be used to
generate a cryptographic context for BFV-encrypted operations.
Use `CCParams{CryptoContextBGVRNS}()` to create a parameter store that can be used to
generate a cryptographic context for BGV-encrypted operations.
Use `CCParams{CryptoContextCKKSRNS}()` to create a parameter store that can be used to
generate a cryptographic context for CKKS-encrypted operations.
See also: [`CryptoContextBFVRNS`](@ref), [`CryptoContextCKKSRNS`](@ref), [`GenCryptoContext`](@ref)
See also: [`CryptoContextBFVRNS`](@ref), [`CryptoContextBGVRNS`](@ref), [`CryptoContextCKKSRNS`](@ref), [`GenCryptoContext`](@ref)
"""
CCParams

Expand Down Expand Up @@ -311,6 +323,7 @@ SetBatchSize
"""
GenCryptoContext(parameters::CCParams)::CryptoContext
GenCryptoContext(parameters::CCParams{CryptoContextBFVRNS})::CryptoContext{DCRTYPoly}
GenCryptoContext(parameters::CCParams{CryptoContextBGVRNS})::CryptoContext{DCRTYPoly}
GenCryptoContext(parameters::CCParams{CryptoContextCKKSRNS})::CryptoContext{DCRTYPoly}
Generate a crypto context from a set of `parameters`. The exact return type depends on the
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Test
@time @testset verbose=true showtiming=true "OpenFHE.jl tests" begin
include("test_auxiliary.jl")
include("test_bfv.jl")
include("test_bgv.jl")
include("test_ckks.jl")
include("test_convenience.jl")
include("test_examples.jl")
Expand Down
23 changes: 23 additions & 0 deletions test/test_bgv.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module TestBGV

using Test
using OpenFHE

# Note: Much of the basic CCParams/CryptoContext functionality is already tested in
# test_ckks.jl and test_bfv.jl.
@testset verbose=true showtiming=true "test_bgv.jl" begin

plaintext_modulus = 65537
multiplicative_depth = 2

@testset verbose=true showtiming=true "CCParams" begin
@test_nowarn CCParams{CryptoContextBGVRNS}()
parameters = CCParams{CryptoContextBGVRNS}()

@test_nowarn SetPlaintextModulus(parameters, plaintext_modulus)
@test_nowarn SetMultiplicativeDepth(parameters, multiplicative_depth)
end

end # @testset "test_bgv.jl"

end # module
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 OpenFHE
@test_nowarn include("../examples/simple_integers.jl")
end

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

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

0 comments on commit 20ef53f

Please sign in to comment.