Skip to content

Commit 4c79d32

Browse files
ai-mannamalaidsuponitskiy-duality
authored andcommitted
- update __len__ in plaintext to map to GetLength() to make the API more pythonic
- use more Python APIs
1 parent db494dd commit 4c79d32

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/lib/bindings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ void bind_encodings(py::module &m)
10411041
py::arg("sf"))
10421042
.def("GetSchemeID", &PlaintextImpl::GetSchemeID,
10431043
ptx_GetSchemeID_docs)
1044+
.def("__len__", &PlaintextImpl::GetLength,
1045+
ptx_GetLength_docs)
10441046
.def("GetLength", &PlaintextImpl::GetLength,
10451047
ptx_GetLength_docs)
10461048
.def("GetSchemeID", &PlaintextImpl::GetSchemeID,

src/lib/binfhe_bindings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ void bind_binfhe_keys(py::module &m) {
140140
py::class_<LWEPrivateKeyImpl, std::shared_ptr<LWEPrivateKeyImpl>>(
141141
m, "LWEPrivateKey")
142142
.def(py::init<>())
143+
.def("__len__", &LWEPrivateKeyImpl::GetLength)
143144
.def("GetLength", &LWEPrivateKeyImpl::GetLength)
144145
.def(py::self == py::self)
145146
.def(py::self != py::self);
@@ -148,6 +149,7 @@ void bind_binfhe_ciphertext(py::module &m) {
148149
py::class_<LWECiphertextImpl, std::shared_ptr<LWECiphertextImpl>>(
149150
m, "LWECiphertext")
150151
.def(py::init<>())
152+
.def("__len__", &LWECiphertextImpl::GetLength)
151153
.def("GetLength", &LWECiphertextImpl::GetLength)
152154
.def("GetModulus", &GetLWECiphertextModulusWrapper)
153155
.def(py::self == py::self)

tests/test_boolean.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44

55
## Sample Program: Step 1: Set CryptoContext
6+
@pytest.mark.parametrize("context",[TOY,MEDIUM,STD128])
67
@pytest.mark.parametrize("a", [0, 1])
78
@pytest.mark.parametrize("b", [0, 1])
8-
def test_boolean_AND(a, b):
9+
def test_boolean_AND(context,a, b):
910
cc = BinFHEContext()
1011

1112
"""
@@ -14,13 +15,13 @@ def test_boolean_AND(a, b):
1415
MEDIUM corresponds to the level of more than 100 bits for both quantum and
1516
classical computer attacks
1617
"""
17-
cc.GenerateBinFHEContext(STD128, GINX)
18+
cc.GenerateBinFHEContext(context, GINX)
1819

1920
## Sample Program: Step 2: Key Generation
2021

2122
# Generate the secret key
2223
sk = cc.KeyGen()
23-
24+
assert sk.GetLength() == len(sk)
2425
print("Generating the bootstrapping keys...\n")
2526

2627
# Generate the bootstrapping keys (refresh and switching keys)
@@ -37,6 +38,8 @@ def test_boolean_AND(a, b):
3738
ct1 = cc.Encrypt(sk, a)
3839
ct2 = cc.Encrypt(sk, b)
3940

41+
assert ct1.GetLength() == len(ct1)
42+
4043
# Sample Program: Step 4: Evaluation
4144

4245
# Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR

tests/test_serial_cc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def test_serial_cryptocontext_str(mode):
6464
# First plaintext vector is encoded
6565
vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
6666
plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
67-
67+
assert len(plaintext1) == plaintext1.GetLength()
68+
assert len(plaintext1) == 12
69+
6870
# Second plaintext vector is encoded
6971
vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
7072
plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)

0 commit comments

Comments
 (0)