Update README.md

phe snippet prettier
This commit is contained in:
Sefik Ilkin Serengil 2025-03-14 16:42:30 +00:00 committed by GitHub
parent 46a2ca4c3c
commit c7c6739c5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -303,14 +303,14 @@ Even though vector embeddings are not reversible to original images, they still
```python ```python
from lightphe import LightPHE from lightphe import LightPHE
# build an additively homomorphic cryptosystem (e.g. Paillier) on-prem
cs = LightPHE(algorithm_name = "Paillier", precision = 19)
# define plain vectors for source and target # define plain vectors for source and target
alpha = DeepFace.represent("img1.jpg")[0]["embedding"] alpha = DeepFace.represent("img1.jpg")[0]["embedding"]
beta = DeepFace.represent("target.jpg")[0]["embedding"] beta = DeepFace.represent("target.jpg")[0]["embedding"]
# build an additively homomorphic cryptosystem (e.g. Paillier) on-prem # encrypt source embedding on-prem - private key not required
cs = LightPHE(algorithm_name = "Paillier", precision = 19)
# encrypt source embedding on-prem
encrypted_alpha = cs.encrypt(alpha) encrypted_alpha = cs.encrypt(alpha)
# dot product of encrypted & plain embedding in cloud - private key not required # dot product of encrypted & plain embedding in cloud - private key not required
@ -321,6 +321,9 @@ calculated_similarity = cs.decrypt(encrypted_cosine_similarity)[0]
# verification # verification
print("same person" if calculated_similarity >= 1 - threshold else "different persons") print("same person" if calculated_similarity >= 1 - threshold else "different persons")
# proof of work
assert abs(calculated_similarity - sum(x * y for x, y in zip(alpha, beta))) < 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. That's the **magic** of homomorphic encryption! 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. Check out [`LightPHE`](https://github.com/serengil/LightPHE) library to find out more about partially homomorphic encryption. 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. That's the **magic** of homomorphic encryption! 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. Check out [`LightPHE`](https://github.com/serengil/LightPHE) library to find out more about partially homomorphic encryption.