From 2c8dbce3e07a64b78547d1585c0e0f99b71f1084 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Thu, 23 Dec 2021 15:53:23 +0100 Subject: [PATCH] chore: fix auto-merged PR --- .github/workflows/continuous-integration.yaml | 1 + Makefile | 3 +- concrete/numpy/compile.py | 9 ++-- tests/conftest.py | 13 +++-- tests/numpy/test_compile.py | 49 +++++++++++++++---- 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/.github/workflows/continuous-integration.yaml b/.github/workflows/continuous-integration.yaml index 608a5eee1..e7d9004ef 100644 --- a/.github/workflows/continuous-integration.yaml +++ b/.github/workflows/continuous-integration.yaml @@ -292,6 +292,7 @@ jobs: echo "Conformance failed, check logs" exit 1 fi + # Taring the docs allows for much faster upload speed (from ~3min worst case to ~2s best case) - name: Tar docs artifacts if: ${{ steps.conformance.outcome == 'success' && !cancelled() }} run: | diff --git a/Makefile b/Makefile index babf5da0f..cfd49ff4d 100644 --- a/Makefile +++ b/Makefile @@ -245,7 +245,8 @@ release_docker: upgrade_py_deps: ./script/make_utils/upgrade_deps.sh -# Keeping this target as it proved useful before the package was stabilized +# Keeping this target as it proved useful before we had a proper package, allowed to run code that +# pytest-codeblocks was failing to execute if not installed as a pip package. # This is done by hand as pytest-codeblocks was failing with our native extensions. # See refused PR on the project here: https://github.com/nschloe/pytest-codeblocks/pull/58 # Test code blocks using a custom python script in the documentation diff --git a/concrete/numpy/compile.py b/concrete/numpy/compile.py index 4346e63fc..fbcfe2f49 100644 --- a/concrete/numpy/compile.py +++ b/concrete/numpy/compile.py @@ -643,13 +643,14 @@ def _compile_op_graph_to_fhe_circuit_internal( # Add MLIR representation as an artifact compilation_artifacts.add_final_operation_graph_mlir(mlir_result) - if ( - _COMPILE_FHE_INSECURE_KEY_CACHE_DIR is not None - and not compilation_configuration.use_insecure_key_cache + if _COMPILE_FHE_INSECURE_KEY_CACHE_DIR is not None and not ( + compilation_configuration.use_insecure_key_cache + and compilation_configuration.enable_unsafe_features ): raise RuntimeError( f"Unable to use insecure key cache {_COMPILE_FHE_INSECURE_KEY_CACHE_DIR} " - f"as use_insecure_key_cache is not set to True in compilation_configuration" + "as use_insecure_key_cache or enable_unsafe_features are not set to True in" + "compilation_configuration" ) # Compile the MLIR representation diff --git a/tests/conftest.py b/tests/conftest.py index 3db254159..f65a83170 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -77,7 +77,7 @@ def get_keyring_dir_from_session_or_default( @pytest.fixture def default_keyring_path(): - """fixture to get test keyring dir""" + """Fixture to get test keyring dir.""" return DEFAULT_KEYRING_PATH @@ -85,12 +85,15 @@ def default_keyring_path(): original_compilation_config_init = CompilationConfiguration.__init__ -def monkeypatched_compilation_configuration_init_for_codeblocks(self, *args, **kwargs): +def monkeypatched_compilation_configuration_init_for_codeblocks( + self: CompilationConfiguration, *args, **kwargs +): """Monkeypatched compilation configuration init for codeblocks tests.""" original_compilation_config_init(self, *args, **kwargs) self.dump_artifacts_on_unexpected_failures = False + self.enable_unsafe_features = True # This is for our tests only, never use that in prod self.treat_warnings_as_errors = True - self.use_insecure_key_cache = True + self.use_insecure_key_cache = True # This is for our tests only, never use that in prod def pytest_sessionstart(session: pytest.Session): @@ -349,6 +352,7 @@ def default_compilation_configuration(): """Return the default test compilation configuration""" return CompilationConfiguration( dump_artifacts_on_unexpected_failures=False, + enable_unsafe_features=True, # This is for our tests only, never use that in prod treat_warnings_as_errors=True, use_insecure_key_cache=True, # This is for our tests only, never use that in prod ) @@ -399,7 +403,8 @@ def check_is_good_execution_impl( # Enabled only when we have a circuit that's using the maximum possible bit width allow_relaxed_tests_passing = max_bit_width == ACCEPTABLE_MAXIMAL_BITWIDTH_FROM_CONCRETE_LIB - # Increased with compiler accuracy which dropped + # FIXME: https://github.com/zama-ai/concretefhe-internal/issues/1255 + # Increased with compiler accuracy which dropped, make sure to remove once accuracy improves nb_tries = 10 # Prepare the bool array to record if cells were properly computed diff --git a/tests/numpy/test_compile.py b/tests/numpy/test_compile.py index d1f3e7058..5bf5fb5b5 100644 --- a/tests/numpy/test_compile.py +++ b/tests/numpy/test_compile.py @@ -1979,16 +1979,21 @@ def test_compile_with_random_inputset(default_compilation_configuration): ) -def test_fail_compile_with_random_inputset(default_compilation_configuration): +def test_fail_compile_with_random_inputset(): """Test function for failed compile with random input set""" + compilation_configuration = CompilationConfiguration( + dump_artifacts_on_unexpected_failures=False, + treat_warnings_as_errors=True, + ) + with pytest.raises(ValueError): try: compile_numpy_function_into_op_graph_and_measure_bounds( lambda x: x + 1, {"x": EncryptedScalar(UnsignedInteger(3))}, inputset="unsupported", - compilation_configuration=default_compilation_configuration, + compilation_configuration=compilation_configuration, ) except Exception as error: expected = ( @@ -2004,7 +2009,7 @@ def test_fail_compile_with_random_inputset(default_compilation_configuration): lambda x: x + 1, {"x": EncryptedScalar(UnsignedInteger(3))}, inputset="random", - compilation_configuration=default_compilation_configuration, + compilation_configuration=compilation_configuration, ) except Exception as error: expected = ( @@ -2151,8 +2156,36 @@ def test_compile_and_run_correctness_with_negative_results( assert check_equality_modulo(compiler_engine.run(*args), function(*args), modulus) -def test_compile_improper_use_of_insecure_key_cache(default_keyring_path): - """Test the case where the key cache is used with wrong compilation configuration""" +@pytest.mark.parametrize( + "compilation_configuration", + [ + CompilationConfiguration( + dump_artifacts_on_unexpected_failures=False, + enable_unsafe_features=False, + use_insecure_key_cache=False, + ), + CompilationConfiguration( + dump_artifacts_on_unexpected_failures=False, + enable_unsafe_features=True, + use_insecure_key_cache=False, + ), + CompilationConfiguration( + dump_artifacts_on_unexpected_failures=False, + enable_unsafe_features=False, + use_insecure_key_cache=True, + ), + ], +) +def test_compile_improper_use_of_insecure_key_cache( + default_keyring_path, compilation_configuration +): + """Test the case where the key cache is used with wrong compilation configuration. + + DO NOT USE INSECURE KEY CACHE FOR NORMAL PRODUCTION WORK + + This is a test to check we properly fail for users trying to incorrectly use the insecure key + cache (to reuse keys across compilations). This allows to speed up tests A LOT but should not be + used in normal prod environment /!\\ DANGER /!\\.""" def f(x): return x + 42 @@ -2162,13 +2195,11 @@ def test_compile_improper_use_of_insecure_key_cache(default_keyring_path): default_keyring_path ) - compilation_configuration = CompilationConfiguration() - compilation_configuration.use_insecure_key_cache = False - with pytest.raises( RuntimeError, match="Unable to use insecure key cache .* " - "as use_insecure_key_cache is not set to True in compilation_configuration", + "as use_insecure_key_cache or enable_unsafe_features are not set to True in" + "compilation_configuration", ): _ = compile_numpy_function( f,