-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfv_test.py
More file actions
40 lines (32 loc) · 1004 Bytes
/
bfv_test.py
File metadata and controls
40 lines (32 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# brew install gcc
# export CC=/opt/homebrew/bin/gcc-15
# export CXX=/opt/homebrew/bin/g++-15
#
# pip install Pyfhel
#
# python3 bfv_test.py
import numpy as np
from Pyfhel import Pyfhel
# 1. Initialize Pyfhel and generate keys
HE = Pyfhel()
# n is the degree of polynomial (affects security and speed)
# t is the plaintext modulus
HE.contextGen(scheme='bfv', n=2**14, t_bits=20)
HE.keyGen()
# 2. Encrypt two integers
# Pyfhel works with NumPy arrays for batching
integer1 = np.array([10], dtype=np.int64)
integer2 = np.array([5], dtype=np.int64)
ctxt1 = HE.encryptInt(integer1)
ctxt2 = HE.encryptInt(integer2)
# 3. Perform FHE Operations
# Addition: 10 + 5 = 15
ctxt_sum = ctxt1 + ctxt2
# Multiplication: 10 * 5 = 50
# Note: In FHE, multiplication increases 'noise' significantly
ctxt_prod = ctxt1 * ctxt2
# 4. Decrypt the results
res_sum = HE.decryptInt(ctxt_sum)
res_prod = HE.decryptInt(ctxt_prod)
print(f"Encrypted Sum (10+5): {res_sum[0]}")
print(f"Encrypted Product (10*5): {res_prod[0]}")