From 891fa1e473b15d41580645eebac1b7e9217412b0 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Tue, 11 Feb 2025 12:27:49 -0800 Subject: [PATCH 1/2] cryptocontext check on smaller native ints as well --- tests/test_cryptocontext.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/test_cryptocontext.py b/tests/test_cryptocontext.py index a104c85..0a9b4df 100644 --- a/tests/test_cryptocontext.py +++ b/tests/test_cryptocontext.py @@ -1,14 +1,26 @@ import pytest import openfhe as fhe -pytestmark = pytest.mark.skipif(fhe.get_native_int() != 128, reason="Only for NATIVE_INT=128") +@pytest.mark.skipif(fhe.get_native_int() != 128, reason="Only for NATIVE_INT=128") +@pytest.mark.parametrize("scaling", [fhe.FIXEDAUTO, fhe.FIXEDMANUAL]) +def test_ckks_context_nativeint128(scaling): + batch_size = 8 + parameters = fhe.CCParamsCKKSRNS() + parameters.SetMultiplicativeDepth(5) + parameters.SetScalingModSize(78) + parameters.SetBatchSize(batch_size) + parameters.SetScalingTechnique(scaling) + parameters.SetNumLargeDigits(2) + cc = fhe.GenCryptoContext(parameters) + assert isinstance(cc, fhe.CryptoContext) + @pytest.mark.parametrize("scaling", [fhe.FIXEDAUTO, fhe.FIXEDMANUAL]) def test_ckks_context(scaling): batch_size = 8 parameters = fhe.CCParamsCKKSRNS() parameters.SetMultiplicativeDepth(5) - parameters.SetScalingModSize(78) + parameters.SetScalingModSize(60-1) parameters.SetBatchSize(batch_size) parameters.SetScalingTechnique(scaling) parameters.SetNumLargeDigits(2) From b38cf176f2773e61a64968c1b38db070bd84ebb4 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Tue, 11 Feb 2025 12:40:47 -0800 Subject: [PATCH 2/2] - update __len__ in plaintext to map to GetLength() to make the API more pythonic - use more Python APIs --- src/lib/bindings.cpp | 2 ++ src/lib/binfhe_bindings.cpp | 2 ++ tests/test_boolean.py | 9 ++++++--- tests/test_serial_cc.py | 4 +++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/bindings.cpp b/src/lib/bindings.cpp index 1ac197f..869b30a 100644 --- a/src/lib/bindings.cpp +++ b/src/lib/bindings.cpp @@ -1041,6 +1041,8 @@ void bind_encodings(py::module &m) py::arg("sf")) .def("GetSchemeID", &PlaintextImpl::GetSchemeID, ptx_GetSchemeID_docs) + .def("__len__", &PlaintextImpl::GetLength, + ptx_GetLength_docs) .def("GetLength", &PlaintextImpl::GetLength, ptx_GetLength_docs) .def("GetSchemeID", &PlaintextImpl::GetSchemeID, diff --git a/src/lib/binfhe_bindings.cpp b/src/lib/binfhe_bindings.cpp index 9f32304..ae47c05 100644 --- a/src/lib/binfhe_bindings.cpp +++ b/src/lib/binfhe_bindings.cpp @@ -140,6 +140,7 @@ void bind_binfhe_keys(py::module &m) { py::class_>( m, "LWEPrivateKey") .def(py::init<>()) + .def("__len__", &LWEPrivateKeyImpl::GetLength) .def("GetLength", &LWEPrivateKeyImpl::GetLength) .def(py::self == py::self) .def(py::self != py::self); @@ -148,6 +149,7 @@ void bind_binfhe_ciphertext(py::module &m) { py::class_>( m, "LWECiphertext") .def(py::init<>()) + .def("__len__", &LWECiphertextImpl::GetLength) .def("GetLength", &LWECiphertextImpl::GetLength) .def("GetModulus", &GetLWECiphertextModulusWrapper) .def(py::self == py::self) diff --git a/tests/test_boolean.py b/tests/test_boolean.py index a69b9f6..df5f0b1 100644 --- a/tests/test_boolean.py +++ b/tests/test_boolean.py @@ -3,9 +3,10 @@ ## Sample Program: Step 1: Set CryptoContext +@pytest.mark.parametrize("context",[TOY,MEDIUM,STD128]) @pytest.mark.parametrize("a", [0, 1]) @pytest.mark.parametrize("b", [0, 1]) -def test_boolean_AND(a, b): +def test_boolean_AND(context,a, b): cc = BinFHEContext() """ @@ -14,13 +15,13 @@ def test_boolean_AND(a, b): MEDIUM corresponds to the level of more than 100 bits for both quantum and classical computer attacks """ - cc.GenerateBinFHEContext(STD128, GINX) + cc.GenerateBinFHEContext(context, GINX) ## Sample Program: Step 2: Key Generation # Generate the secret key sk = cc.KeyGen() - + assert sk.GetLength() == len(sk) print("Generating the bootstrapping keys...\n") # Generate the bootstrapping keys (refresh and switching keys) @@ -37,6 +38,8 @@ def test_boolean_AND(a, b): ct1 = cc.Encrypt(sk, a) ct2 = cc.Encrypt(sk, b) + assert ct1.GetLength() == len(ct1) + # Sample Program: Step 4: Evaluation # Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR diff --git a/tests/test_serial_cc.py b/tests/test_serial_cc.py index 5a1acf2..d376946 100644 --- a/tests/test_serial_cc.py +++ b/tests/test_serial_cc.py @@ -64,7 +64,9 @@ def test_serial_cryptocontext_str(mode): # First plaintext vector is encoded vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1) - + assert len(plaintext1) == plaintext1.GetLength() + assert len(plaintext1) == 12 + # Second plaintext vector is encoded vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12] plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)