-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_openfhe_smoke.py
More file actions
108 lines (84 loc) · 2.63 KB
/
Copy pathtest_openfhe_smoke.py
File metadata and controls
108 lines (84 loc) · 2.63 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Minimal OpenFHE CKKS smoke test to verify setup before trying multiparty.
"""
import time
import openfhe
def test_basic_ckks():
"""Test basic CKKS functionality (no multiparty)."""
print("🔍 Testing basic OpenFHE CKKS...")
# Create context with conservative parameters
params = openfhe.CCParamsCKKSRNS()
params.SetSecurityLevel(openfhe.HEStd_128_classic)
params.SetRingDim(16384)
params.SetMultiplicativeDepth(1)
params.SetScalingModSize(40)
params.SetFirstModSize(50)
print("✅ Parameters set")
cc = openfhe.GenCryptoContext(params)
print("✅ Context created")
# Enable basic features
cc.Enable(openfhe.PKE)
cc.Enable(openfhe.LEVELEDSHE)
print("✅ Features enabled")
# Generate keys
kp = cc.KeyGen()
print("✅ Keys generated")
# Test data
x = [1.0, 2.0, 3.0]
scale = 2**40
pt = cc.MakeCKKSPackedPlaintext(x, scale)
print("✅ Plaintext created")
# Encrypt
ct = cc.Encrypt(kp.publicKey, pt)
print("✅ Encrypted")
# Decrypt
decrypted = cc.Decrypt(ct, kp.secretKey)
decrypted.SetLength(len(x)) # Set logical length
result = decrypted.GetRealPackedValue()
print("✅ Decrypted")
# Check result
print(f"Expected: {x}")
print(f"Result: {result[:len(x)]}")
# Verify accuracy
errors = [abs(e - r) for e, r in zip(x, result[: len(x)])]
max_error = max(errors)
print(f"Max error: {max_error:.2e}")
if max_error < 1e-3:
print("🎉 Basic CKKS test PASSED!")
return True
else:
print("❌ Basic CKKS test FAILED!")
return False
def test_import_speed():
"""Test OpenFHE import speed."""
print("🔍 Testing OpenFHE import speed...")
start = time.time()
import openfhe
import_time = time.time() - start
print(f"✅ Import took {import_time:.2f} seconds")
if import_time < 5.0:
print("🎉 Import speed OK!")
return True
else:
print("⚠️ Import is slow (possible emulation)")
return False
if __name__ == "__main__":
print("🚀 OpenFHE Smoke Test")
print("=" * 50)
# Test import speed first
import_ok = test_import_speed()
print()
# Test basic CKKS
ckks_ok = test_basic_ckks()
print()
# Summary
print("📊 Summary:")
print(f" Import speed: {'✅' if import_ok else '❌'}")
print(f" Basic CKKS: {'✅' if ckks_ok else '❌'}")
if import_ok and ckks_ok:
print("\n🎉 All tests PASSED! Ready for threshold HE.")
exit(0)
else:
print("\n❌ Some tests FAILED. Check environment setup.")
exit(1)