In my application, I'm receiving CryptoContext objects from multiple origins, along with a EvalMultKey.
When "ingesting" (through DeserializeEvalMultKey) the EvalMultKeys, it seems earlier ingested keys are overridden.
A minimal example demonstrating the issue:
from openfhe import *
# NOTE:
# If running locally, you may want to replace the "hardcoded" datafolder with
# the datafolder location below which gets the current working directory
datafolder = 'demoData'
# Save-Load locations for keys
multKeyLocation1 = datafolder + '/key_mult1.txt'
multKeyLocation2 = datafolder + '/key_mult2.txt'
# Generate CC params
# Settings are arbitrarily chosen
parameters = CCParamsCKKSRNS()
parameters.SetMultiplicativeDepth(5)
parameters.SetScalingModSize(40)
parameters.SetBatchSize(32)
# Generate CC
cc = GenCryptoContext(parameters)
cc.Enable(PKE)
cc.Enable(KEYSWITCH)
cc.Enable(LEVELEDSHE)
# ... with keypair
kp1 = cc.KeyGen()
cc.EvalMultKeyGen(kp1.secretKey)
# Generate an second, identical, context
cc2 = GenCryptoContext(parameters)
cc2.Enable(PKE)
cc2.Enable(KEYSWITCH)
cc2.Enable(LEVELEDSHE)
# ... with different keypair
kp2 = cc2.KeyGen()
cc2.EvalMultKeyGen(kp2.secretKey)
# Encrypt some data, ONLY UNDER KP1
vec1 = [1.0, 2.0, 3.0, 4.0]
vec2 = [12.5, 13.5, 14.5, 15.5]
p1 = cc.MakeCKKSPackedPlaintext(vec1)
p2 = cc.MakeCKKSPackedPlaintext(vec2)
c1 = cc.Encrypt(kp1.publicKey, p1)
c2 = cc.Encrypt(kp1.publicKey, p2)
# Serialize the relinearlization keys of both contexts
assert CryptoContext.SerializeEvalMultKey(multKeyLocation1, BINARY, kp1.secretKey.GetKeyTag())
assert CryptoContext.SerializeEvalMultKey(multKeyLocation2, BINARY, kp2.secretKey.GetKeyTag())
# Release context, i.e. drop the relinearization keys from memory
cc.ClearEvalMultKeys()
ReleaseAllContexts()
# Deserialize the first relinearization keys
assert CryptoContext.DeserializeEvalMultKey(multKeyLocation1, BINARY)
cc.EvalMult(c1, c2) # this works, no problem
# Now, we deserialize the second key.
assert CryptoContext.DeserializeEvalMultKey(multKeyLocation2, BINARY)
cc.EvalMult(c1, c2) # raises RuntimeError
The error raised:
RuntimeError: cryptocontext.cpp:l.183:GetEvalMultKeyVector(): Call EvalMultKeyGen() to have EvalMultKey available for ID [...]
Note here that c1 and c2 were encrypted under cc. Before deserializing the EvalMultKey for cc2, there is no problem executing cc.EvalMult on these ciphertexts, but afterwards the key seems to be gone.
To further support the claim that the keys are OVERRIDDEN, I present the following code.
In my application, I'm receiving
CryptoContext
objects from multiple origins, along with aEvalMultKey
. When "ingesting" (throughDeserializeEvalMultKey
) theEvalMultKey
s, it seems earlier ingested keys are overridden.A minimal example demonstrating the issue:
The error raised:
Note here that
c1
andc2
were encrypted undercc
. Before deserializing theEvalMultKey
forcc2
, there is no problem executingcc.EvalMult
on these ciphertexts, but afterwards the key seems to be gone.To further support the claim that the keys are OVERRIDDEN, I present the following code.
When replace the statement that raised an exception with this code and execute, I see that the second and third hash that are printed are identical:
This suggests that the second key is the only one stored.
All was executed using the latest
main
branches ofopenfhe-development
andopenfhe-python
.Am I doing something wrong? Any clue how I should resolve this?