diff --git a/examples/simple_integers_bgvrns.jl b/examples/simple_integers_bgvrns.jl new file mode 100644 index 0000000..885d0ac --- /dev/null +++ b/examples/simple_integers_bgvrns.jl @@ -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) diff --git a/src/OpenFHE.jl b/src/OpenFHE.jl index 020242a..c0371dd 100644 --- a/src/OpenFHE.jl +++ b/src/OpenFHE.jl @@ -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, diff --git a/src/convenience.jl b/src/convenience.jl index aba90a6..3bed2ae 100644 --- a/src/convenience.jl +++ b/src/convenience.jl @@ -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}()", @@ -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. diff --git a/src/documentation.jl b/src/documentation.jl index 8b177f8..2e4009e 100644 --- a/src/documentation.jl +++ b/src/documentation.jl @@ -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 @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 6b6eb61..564306e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") diff --git a/test/test_bgv.jl b/test/test_bgv.jl new file mode 100644 index 0000000..c3b77dc --- /dev/null +++ b/test/test_bgv.jl @@ -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 diff --git a/test/test_examples.jl b/test/test_examples.jl index e6884be..df8baea 100644 --- a/test/test_examples.jl +++ b/test/test_examples.jl @@ -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