HE on np.nparrays

Hello,

I am trying to write a script for university project which shows a possible application of Homomorphic Encryption and facing big challenges with using Concrete Numpy. In my case I am trying to subtract one image from another in np.ndarray format that are homomorphically encrypted.

My ultimate question is: “Is it possible to use Concrete-Numpy to do cv2.subtract() of 2 np.ndarrays?”

If yes, how?
If not, why? Is there a workaround?

Example code without Concrete-Numpy:

def image_subtract(image1:np.ndarray, image2:np.ndarray):

    # Subtract image2 from image1
    differenceImage = cv2.subtract(image1, image2)

    return differenceImage


image1_path = "./assets/before.jpeg"
image2_path = "./assets/after.jpeg"

image1 = cv2.imread(image1_path)
image2 = cv2.imread(image2_path)

result = image_subtract(image1, image2)

cv2.imshow("Result", result)

Here is a workable example without Homomorphic Encryption from Concrete-Numpy, but I would want to do the subtraction on 2 images that are homomorphically encrypted.

Thank you for the reply in advance.

Hi @rutkovskii,

It’s not easy to do with v0.8.0 of concrete-numpy, but in the upcoming v0.9.0, it will be very easy.

Here is the code that works with v0.8.0, but in virtual mode (i.e., simulated):

import concrete.numpy as cnp
import cv2
import numpy as np

image1_path = "./assets/before.jpeg"
image2_path = "./assets/after.jpeg"

image1 = cv2.imread(image1_path)
image2 = cv2.imread(image2_path)
result = cv2.subtract(image1, image2)

image1 = image1.astype(np.int64)
image2 = image2.astype(np.int64)

@cnp.compiler({"image1": "encrypted", "image2": "encrypted"})
def f(image1, image2):
    return (image1 - image2).clip(0, 255)

inputset = [(image1, image2)]  # you may want to add more samples to this, maybe random ones
circuit = f.compile(inputset, enable_unsafe_features=True, virtual=True)

homomorphic_result = circuit.encrypt_run_decrypt(image1, image2)
assert np.array_equal(result, homomorphic_result)

With 0.9.0, you’ll be able to remove enable_unsafe_features=True and virtual=True, and it’ll work with FHE.

Best regards
Umut

1 Like

Thank you a lot!
Will be waiting for 0.9.0!

1 Like