Update README.md

This commit is contained in:
Sefik Ilkin Serengil 2025-03-07 11:19:03 +00:00 committed by GitHub
parent a58b782000
commit 64c95a4afb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -380,43 +380,45 @@ Even though vector embeddings are not reversible to original images, they still
```python
from lightphe import LightPHE
def on_prem():
# build an additively homomorhic encryption cryptosystem
onprem_cs = LightPHE(algorithm_name = "Paillier", precision = 19)
# define a plain vectors for source and target
alpha = DeepFace.represent("img1.jpg")[0]["embedding"] # user tower
beta = DeepFace.represent("target.jpg")[0]["embedding"] # item tower
expected_similarity = sum(x * y for x, y in zip(alpha, beta))
# export keys
onprem_cs.export_keys("secret.txt")
onprem_cs.export_keys("public.txt", public=True)
# build an additively homomorphic cryptosystem (e.g. Paillier) on-prem
cs = LightPHE(algorithm_name = "Paillier", precision = 19)
# find l2 normalized and all positive vector embeddings - VGG-Face already does
source_embedding = DeepFace.represent("img1.jpg")[0]["embedding"]
# export keys
cs.export_keys("secret.txt"); cs.export_keys("public.txt", public=True)
# encrypt source embedding
encrypted_source_embedding = onprem_cs.encrypt(source_embedding)
return encrypted_source_embedding
# encrypt source embedding
encrypted_alpha = cs.encrypt(alpha)
def cloud(encrypted_source_embedding):
# restore the built cryptosystem in cloud with only public key
cloud_cs = LightPHE(algorithm_name = "Paillier", precision = 19, key_file = "public.txt")
# remove cryptosystem and plain alpha not to be leaked in cloud
del cs, alpha
# find l2 normalized and all positive vector embeddings - VGG-Face already does
target_embedding = DeepFace.represent("target.jpg")[0]["embedding"]
# restore the cryptosystem in cloud with only public key
cloud_cs = LightPHE(algorithm_name = "Paillier", precision = 19, key_file = "public.txt")
# find dot product of encrypted embedding and plain embedding
encrypted_cosine_similarity = encrypted_source_embedding @ target_embedding
# dot product of encrypted and plain embedding pair
encrypted_cosine_similarity = encrypted_alpha @ beta
# confirm that cloud cannot decrypt it even though it is calculated by cloud
with pytest.raises(ValueError, match="must have private key"):
cloud_cs.decrypt(encrypted_cosine_similarity)
return encrypted_cosine_similarity
# computed by the cloud but cloud cannot decrypt it
with pytest.raises(ValueError, match="must have private key"):
cloud_cs.decrypt(encrypted_cosine_similarity)
def verify(encrypted_cosine_similarity, threshold = 0.68):
# restore the built cryptosystem on-prem with secret key
onprem_cs = LightPHE(algorithm_name = "Paillier", precision = 19, key_file = "secret.txt")
# restore the cryptosystem on-prem with secret key
cs = LightPHE(algorithm_name = "Paillier", precision = 19, key_file = "secret.txt")
# restore cosine similarity
cosine_similarity = onprem_cs.decrypt(encrypted_cosine_similarity)[0]
print("same person" if cosine_similarity >= 1 - threshold else "different persons")
# decrypt similarity
calculated_similarity = cs.decrypt(encrypted_cosine_similarity)[0]
# verification
threshold = 0.68 # cosine distance threshold for VGG-Face and cosine
print("same person" if calculated_similarity >= 1 - threshold else "different persons")
# proof of work
assert abs(calculated_similarity - expected_similarity) < 1e-2
```
In this scheme, we leverage the computational power of the cloud to compute encrypted cosine similarity. However, the cloud has no knowledge of the actual calculations it performs. Only the secret key holder on the on-premises side can decrypt the encrypted cosine similarity and determine whether the pair represents the same person or different individuals.