narger-ef / LowMemoryFHEResNet20

Source code for the paper "Encrypted Image Classification with Low Memory Footprint using Fully Homomorphic Encryption"
https://eprint.iacr.org/2024/460
MIT License
24 stars 7 forks source link

Encouter Exception "what(): DropLastElement: Removing last element of DCRTPoly renders it invalid." during runtime. #9

Closed JerryFishY closed 3 months ago

JerryFishY commented 3 months ago

I encoutered an error during runtime.

The version of OpenFHE I am using is v1.1.3,

the command I execute is:

./LowMemoryFHEResNet20 generate_keys 1

./LowMemoryFHEResNet20 load_keys 1 input "inputs/vale.jpg"

The first command runs successfully while the second one will encouter an exception:

Encrypted ResNet20 classification started.
I am going to encrypt and classify ../inputs/vale.jpg.
terminate called after throwing an instance of 'lbcrypto::OpenFHEException'
  what():  DropLastElement: Removing last element of DCRTPoly renders it invalid.
Aborted (core dumped)
narger-ef commented 3 months ago

That's weird, I replicated the commands and I obtained:

narger@MacBook-Pro-di-Lorenzo build % ./LowMemoryFHEResNet20 generate_keys 1

Ciphertexts depth: 23, available multiplications: 7
You are using log(Q+P) = 1756
Context built, generating keys...
Generated.
Now serializing keys ...
EvalMult keys have been serialized
Crypto Context have been serialized
Public Key has been serialized
Secret Key has been serialized
Rotation keys "rotations-layer1.bin" have been serialized
Rotation keys "rotations-layer2-downsample.bin" have been serialized
Rotation keys "rotations-layer2.bin" have been serialized
Rotation keys "rotations-layer3-downsample.bin" have been serialized
Rotation keys "rotations-layer3.bin" have been serialized
Rotation keys "rotations-finallayer.bin" have been serialized
Context created correctly.

narger@MacBook-Pro-di-Lorenzo build % ./LowMemoryFHEResNet20 load_keys 1 input "inputs/vale.jpg"
Encrypted ResNet20 classification started.
I am going to encrypt and classify ../inputs/vale.jpg.
Decrypting the output...
Output: [ -0.384, -2.183, -2.889,  2.112, -1.509,  9.036, -1.412, -1.074, -0.251, -1.438 ]
The input image is classified as Dog
The index of max element is 5

Your error says that it is trying to reseale a ciphertext at the last level, I guess it is happening before the bootstrapping. I wonder if OpenFHE's last version has something different in that procedure.

Could you please:

?

Thank you

JerryFishY commented 3 months ago

Sorry for late response, it could now successfully run after changing the levels and ring size. Thanks!

yellow123Nike commented 3 months ago

I tried FHEController.cpp to uint32_tlevelsUsedBeforeBootstrap = get_relu_depth(relu_deg) + 4; but error image How to set parameters? image t'hthanks

narger-ef commented 3 months ago

The problem is that using by uint32_tlevelsUsedBeforeBootstrap = get_relu_depth(relu_deg) + 4, the modulus becomes too large and the security goes below $\lambda = 128$ bits.

I don't know why with newer versions the errorDropLastElement: Removing last element of DCRTPoly renders it invalid. pops up, so I suggest you different alternatives: