Hello all, I think I have found a bug in the openfhe-numpy library that errors out for scalar subtraction. Here is the minimal script to reproduce this error:
import numpy as np
from openfhe import *
import openfhe_numpy as onp
# Cryptographic setup
mult_depth = 4
params = CCParamsCKKSRNS()
cc = GenCryptoContext(params)
params.SetMultiplicativeDepth(mult_depth)
cc.Enable(PKESchemeFeature.PKE)
cc.Enable(PKESchemeFeature.LEVELEDSHE)
cc.Enable(PKESchemeFeature.ADVANCEDSHE)
keys = cc.KeyGen()
ring_dim = cc.GetRingDimension()
batch_size = ring_dim // 2
print(f"\nCKKS ring dimension: {ring_dim}")
print(f"Available slots: {batch_size}")
# Sample input vectors
vector = [1.0, 2.0, 3.0, 4.0, 5.0]
ct_v = onp.array(
cc=cc,
data=vector,
batch_size=batch_size,
order=onp.ROW_MAJOR,
mode="tile",
fhe_type="C",
public_key=keys.publicKey,
)
# Scalar value
scalar = 2.0
print(f"Vector: {vector}")
print(f"Scalar: {scalar}")
# Add scalar to vector
ct_v_add = ct_v + scalar
res_add = ct_v_add.decrypt(keys.secretKey, unpack_type="original")
print(f"Result (add): {res_add}")
# Subtract scalar from vector
ct_v_sub = ct_v + (-scalar)
res_sub_true = ct_v_sub.decrypt(keys.secretKey, unpack_type="original")
print(f"Result (sub true): {res_sub_true}")
# Subtract scalar from vector
ct_v_sub = ct_v - scalar # errors out
res_sub = ct_v_sub.decrypt(keys.secretKey, unpack_type="original")
print(f"Result (sub): {res_sub}")
I have prepared a fix for this in my local fork. But I am unable to create a PR on the parent repo.
Let me know if there is any other way to contribute.