Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple_integers.jl example + more wrappings #35

Merged
merged 6 commits into from
Feb 12, 2024
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
75 changes: 75 additions & 0 deletions examples/simple_integers.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{CryptoContextBFVRNS}()
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)
21 changes: 15 additions & 6 deletions src/OpenFHE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ end
export DCRTPoly

# CCParams
export CCParams, CryptoContextCKKSRNS
export SetMultiplicativeDepth, SetScalingModSize, SetBatchSize, SetSecretKeyDist,
SetSecurityLevel, SetRingDim, SetScalingTechnique, SetFirstModSize,
SetNumLargeDigits, SetKeySwitchTechnique
export CCParams, CryptoContextBFVRNS, CryptoContextCKKSRNS
export GetPlaintextModulus, GetDigitSize, GetStandardDeviation, GetSecretKeyDist,
GetMaxRelinSkDeg, GetNoiseEstimate, GetDesiredPrecision, GetStatisticalSecurity,
GetNumAdversarialQueries, GetThresholdNumOfParties, GetKeySwitchTechnique,
GetScalingTechnique, GetBatchSize, GetFirstModSize, GetNumLargeDigits,
GetMultiplicativeDepth, GetScalingModSize, GetSecurityLevel, GetRingDim,
GetEvalAddCount, GetKeySwitchCount, GetMultiHopModSize,
SetPlaintextModulus, SetDigitSize, SetStandardDeviation, SetSecretKeyDist,
SetMaxRelinSkDeg, SetNoiseEstimate, SetDesiredPrecision, SetStatisticalSecurity,
SetNumAdversarialQueries, SetThresholdNumOfParties, SetKeySwitchTechnique,
SetScalingTechnique, SetBatchSize, SetFirstModSize, SetNumLargeDigits,
SetMultiplicativeDepth, SetScalingModSize, SetSecurityLevel, SetRingDim,
SetEvalAddCount, SetKeySwitchCount, SetMultiHopModSize

# FHECKKSRNS
export GetBootstrapDepth
Expand All @@ -45,8 +54,8 @@ export GetCryptoContext
export CryptoContext
export GenCryptoContext, Enable, GetKeyGenLevel, SetKeyGenLevel, GetEncodingParams,
GetCyclotomicOrder, GetModulus, GetRingDimension, GetRootOfUnity, KeyGen,
MakeCKKSPackedPlaintext, Encrypt, Decrypt, EvalNegate, EvalAdd, EvalSub,
EvalMultKeyGen, EvalMult, EvalSquare, EvalMultNoRelin, Relinearize,
MakePackedPlaintext, MakeCKKSPackedPlaintext, Encrypt, Decrypt, EvalNegate, EvalAdd,
EvalSub, EvalMultKeyGen, EvalMult, EvalSquare, EvalMultNoRelin, Relinearize,
RelinearizeInPlace, EvalRotate, EvalRotateKeyGen, ComposedEvalMult, Rescale,
RescaleInPlace, ModReduce, ModReduceInPlace, EvalSin, EvalCos, EvalLogistic,
EvalDivide, EvalSumKeyGen, EvalSum, EvalBootstrapSetup, EvalBootstrapKeyGen,
Expand Down
27 changes: 27 additions & 0 deletions src/convenience.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ for (WrappedT, fun) in [
:(CryptoContext{DCRTPoly}) => :GetRootOfUnity,
:(CryptoContext{DCRTPoly}) => :KeyGen,
:(CryptoContext{DCRTPoly}) => :MakeCKKSPackedPlaintext,
:(CryptoContext{DCRTPoly}) => :MakePackedPlaintext,
:(CryptoContext{DCRTPoly}) => :Encrypt,
:(CryptoContext{DCRTPoly}) => :Decrypt,
:(CryptoContext{DCRTPoly}) => :EvalNegate,
Expand Down Expand Up @@ -171,7 +172,9 @@ end
# Convenience `show` methods to hide wrapping-induced ugliness
# Note: remember to add tests to `test/test_convenience.jl` if you add something here
for (T, str) in [
:(CCParams{<:CryptoContextBFVRNS}) => "CCParams{CryptoContextBFVRNS}()",
:(CCParams{<:CryptoContextCKKSRNS}) => "CCParams{CryptoContextCKKSRNS}()",
:(CryptoContextBFVRNS) => "CryptoContextBFVRNS()",
:(CryptoContextCKKSRNS) => "CryptoContextCKKSRNS()",
:(CryptoContext{DCRTPoly}) => "CryptoContext{DCRTPoly}()",
:(Ciphertext{DCRTPoly}) => "Ciphertext{DCRTPoly}()",
Expand Down Expand Up @@ -211,6 +214,30 @@ function MakeCKKSPackedPlaintext(context::CxxWrap.CxxWrapCore.CxxRef{OpenFHE.Cry
MakeCKKSPackedPlaintext(context, CxxWrap.StdVector(value), scale_degree, level, params, num_slots)
end

"""
MakePackedPlaintext(crypto_context::CryptoContext, value::Vector{<:Integer};
noise_scale_degree = 1,
level = 0)

Encode a vector of integers `value` into a BFV-packed [`Plaintext`](@ref) using the
given `crypto_context`.
Please refer to the OpenFHE documentation for details on the remaining arguments.

See also: [`CryptoContext`](@ref), [`Plaintext`](@ref)
"""
function MakePackedPlaintext(context::CxxWrap.CxxWrapCore.CxxRef{OpenFHE.CryptoContextImpl{OpenFHE.DCRTPoly}},
value::Vector{<:Integer};
noise_scale_degree = 1,
level = 0)
MakePackedPlaintext(context, Int64.(value); noise_scale_degree, level)
end
function MakePackedPlaintext(context::CxxWrap.CxxWrapCore.CxxRef{OpenFHE.CryptoContextImpl{OpenFHE.DCRTPoly}},
value::Vector{Int64};
noise_scale_degree = 1,
level = 0)
MakePackedPlaintext(context, CxxWrap.StdVector(value), noise_scale_degree, level)
end

"""
EvalRotateKeyGen(crypto_context::CryptoContext,
private_key::PrivateKey,
Expand Down
135 changes: 102 additions & 33 deletions src/documentation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ DCRTPoly


# CCParams
"""
CryptoContextBFVRNS

A type used as a parameter to `CCParams` to indicate that parameters for BFV-based
encryptions are to be stored.

See also: [`CCParams`](@ref)
"""
CryptoContextBFVRNS

"""
CryptoContextCKKSRNS

Expand All @@ -28,28 +38,68 @@ CryptoContextCKKSRNS
Type to store parameters for a generating a cryptographic context in OpenFHE using
`GenCryptoContext`.

Use `CCParams{CryptoContextBFVRNS}()` to create a parameter store that can be used to
generate a cryptographic context for BFV-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: [`CryptoContextCKKSRNS`](@ref), [`GenCryptoContext`](@ref)
See also: [`CryptoContextBFVRNS`](@ref), [`CryptoContextCKKSRNS`](@ref), [`GenCryptoContext`](@ref)
"""
CCParams

"""
SetMultiplicativeDepth(parameters::CCParams, depth::Integer)

Set the required multiplicative `depth` for a set of `parameters`.
"""
SetMultiplicativeDepth

"""
SetScalingModSize(parameters::CCParams, modulus::Integer)

Set the scaling `modulus` for a set of `parameters`.

See also: [`SetFirstModSize`](@ref)
"""
SetScalingModSize
# FIXME: Add missing docstrings for the following functions
#
# Note: Those marked with * already have an entry for another type; in this case the
# existing docstring needs to be extended by the CCParams type
#
# Note: Those marked as `~~NAME~~` are already there; they are just retained in the list to
# allow for adding the missing docstrings in the proper order
#
# GetPlaintextModulus*
# GetDigitSize
# GetStandardDeviation
# GetSecretKeyDist
# GetMaxRelinSkDeg
# GetNoiseEstimate
# GetDesiredPrecision
# GetStatisticalSecurity
# GetNumAdversarialQueries
# GetThresholdNumOfParties
# GetKeySwitchTechnique
# GetScalingTechnique
# GetBatchSize*
# GetFirstModSize
# GetNumLargeDigits
# GetMultiplicativeDepth
# GetScalingModSize
# GetSecurityLevel
# GetRingDim
# GetEvalAddCount
# GetKeySwitchCount
# GetMultiHopModSize
#
# SetPlaintextModulus*
# SetDigitSize
# SetStandardDeviation
# ~~SetSecretKeyDist~~
# SetMaxRelinSkDeg
# SetNoiseEstimate
# SetDesiredPrecision
# SetStatisticalSecurity
# SetNumAdversarialQueries
# SetThresholdNumOfParties
# ~~SetKeySwitchTechnique~~
# ~~SetScalingTechnique~~
# SetBatchSize*
# ~~SetFirstModSize~~
# ~~SetNumLargeDigits~~
# ~~SetMultiplicativeDepth~~
# ~~SetScalingModSize~~
# ~~SetSecurityLevel~~
# ~~SetRingDim~~
# SetEvalAddCount
# SetKeySwitchCount
# SetMultiHopModSize

"""
SetSecretKeyDist(parameters::CCParams, distribution::SecretKeyDist)
Expand All @@ -61,21 +111,13 @@ See also: [`SecretKeyDist`](@ref)
SetSecretKeyDist

"""
SetSecurityLevel(parameters::CCParams, level::SecurityLevel)

Set the encryption security `level` according to the homomogrphic encryption standard for a
set of `parameters`.

See also: [`SecurityLevel`](@ref)
"""
SetSecurityLevel
SetKeySwitchTechnique(parameters::CCParams, technique::KeySwitchTechnique)

"""
SetRingDim(parameters::CCParams, dimension::Integer)
Set the key switching technique `technique` for a set of `parameters`.

Set the polynomial ring `dimension` for a set of `parameters`.
See also: [`KeySwitchTechnique`](@ref)
"""
SetRingDim
SetKeySwitchTechnique

"""
SetScalingTechnique(parameters::CCParams, technique::ScalingTechnique)
Expand Down Expand Up @@ -103,13 +145,37 @@ Set the `number` of large digits for a set of `parameters`.
SetNumLargeDigits

"""
SetKeySwitchTechnique(parameters::CCParams, technique::KeySwitchTechnique)
SetMultiplicativeDepth(parameters::CCParams, depth::Integer)

Set the key switching technique `technique` for a set of `parameters`.
Set the required multiplicative `depth` for a set of `parameters`.
"""
SetMultiplicativeDepth

See also: [`KeySwitchTechnique`](@ref)
"""
SetKeySwitchTechnique
SetScalingModSize(parameters::CCParams, modulus::Integer)

Set the scaling `modulus` for a set of `parameters`.

See also: [`SetFirstModSize`](@ref)
"""
SetScalingModSize

"""
SetSecurityLevel(parameters::CCParams, level::SecurityLevel)

Set the encryption security `level` according to the homomogrphic encryption standard for a
set of `parameters`.

See also: [`SecurityLevel`](@ref)
"""
SetSecurityLevel

"""
SetRingDim(parameters::CCParams, dimension::Integer)

Set the polynomial ring `dimension` for a set of `parameters`.
"""
SetRingDim


# FHECKKSRNS
Expand Down Expand Up @@ -230,7 +296,8 @@ SetBatchSize
# CryptoContext
"""
GenCryptoContext(parameters::CCParams)::CryptoContext
GenCryptoContext(parameters::CCParams<CryptoContextCKKSRNS>)::CryptoContext<DCRTYPoly>
GenCryptoContext(parameters::CCParams{CryptoContextBFVRNS})::CryptoContext{DCRTYPoly}
GenCryptoContext(parameters::CCParams{CryptoContextCKKSRNS})::CryptoContext{DCRTYPoly}

Generate a crypto context from a set of `parameters`. The exact return type depends on the
parameter set type.
Expand Down Expand Up @@ -334,6 +401,8 @@ EvalMultKeyGen

# EvalRotateKeyGen` is documented in `src/convenience.jl`

# `MakePackedPlaintext` is documented in `src/convenience.jl`
#
# `MakeCKKSPackedPlaintext` is documented in `src/convenience.jl`

"""
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Test

@time @testset verbose=true showtiming=true "OpenFHE.jl tests" begin
include("test_auxiliary.jl")
include("test_bfv.jl")
include("test_ckks.jl")
include("test_convenience.jl")
include("test_examples.jl")
Expand Down
Loading
Loading