From 6d96a08f0a8a74dcc1ce293f5da7bc04d8e8d91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20P=C3=A9r=C3=A9?= Date: Mon, 9 Dec 2024 10:44:28 +0100 Subject: [PATCH] chore(common): add 132 bits security curves --- docs/guides/configure.md | 3 ++ .../concrete/fhe/compilation/configuration.py | 14 ++++++ .../concrete/fhe/compilation/server.py | 2 + .../tests/compilation/test_configuration.py | 47 ++++++++++++++++++- .../include/concrete/curves.gen.h | 3 +- .../src/gaussian/curves_gen.rs | 19 +++++++- 6 files changed, 84 insertions(+), 4 deletions(-) diff --git a/docs/guides/configure.md b/docs/guides/configure.md index 49e7470a12..c7238d097e 100644 --- a/docs/guides/configure.md +++ b/docs/guides/configure.md @@ -218,3 +218,6 @@ When options are specified both in the `configuration` and as kwargs in the `com - Automatic scheduling behavior can be override locally by calling directly a variant of `run`: - `run_sync`: forces the fhe function to occur in the current thread, not in the background, - `run_async`: forces the fhe function to occur in a background thread, returning immediately a `Future[Value]` + +#### security_level: int = 128 +- Set the level of security used to perform the optimization of crypto-parameters. diff --git a/frontends/concrete-python/concrete/fhe/compilation/configuration.py b/frontends/concrete-python/concrete/fhe/compilation/configuration.py index 5cfe97b721..5d239c1597 100644 --- a/frontends/concrete-python/concrete/fhe/compilation/configuration.py +++ b/frontends/concrete-python/concrete/fhe/compilation/configuration.py @@ -22,6 +22,15 @@ DEFAULT_GLOBAL_P_ERROR = 1 / 100_000 +class SecurityLevel(int, Enum): + """ + Security level used to optimize the circuit parameters. + """ + + SECURITY_128_BITS = 128 + SECURITY_132_BITS = 132 + + class ParameterSelectionStrategy(str, Enum): """ ParameterSelectionStrategy, to set optimization strategy. @@ -998,6 +1007,7 @@ class Configuration: range_restriction: Optional[RangeRestriction] keyset_restriction: Optional[KeysetRestriction] auto_schedule_run: bool + security_level: SecurityLevel def __init__( self, @@ -1070,6 +1080,7 @@ def __init__( range_restriction: Optional[RangeRestriction] = None, keyset_restriction: Optional[KeysetRestriction] = None, auto_schedule_run: bool = False, + security_level: SecurityLevel = SecurityLevel.SECURITY_128_BITS, ): self.verbose = verbose self.compiler_debug_mode = compiler_debug_mode @@ -1181,6 +1192,8 @@ def __init__( self.auto_schedule_run = auto_schedule_run + self.security_level = security_level + self._validate() class Keep: @@ -1259,6 +1272,7 @@ def fork( range_restriction: Union[Keep, Optional[RangeRestriction]] = KEEP, keyset_restriction: Union[Keep, Optional[KeysetRestriction]] = KEEP, auto_schedule_run: Union[Keep, bool] = KEEP, + security_level: Union[Keep, SecurityLevel] = KEEP, ) -> "Configuration": """ Get a new configuration from another one specified changes. diff --git a/frontends/concrete-python/concrete/fhe/compilation/server.py b/frontends/concrete-python/concrete/fhe/compilation/server.py index 05f747fd5f..2a79696ea8 100644 --- a/frontends/concrete-python/concrete/fhe/compilation/server.py +++ b/frontends/concrete-python/concrete/fhe/compilation/server.py @@ -186,6 +186,8 @@ def create( if configuration.range_restriction: options.set_range_restriction(configuration.range_restriction) + options.set_security_level(configuration.security_level) + try: if configuration.compiler_debug_mode: # pragma: no cover set_llvm_debug_flag(True) diff --git a/frontends/concrete-python/tests/compilation/test_configuration.py b/frontends/concrete-python/tests/compilation/test_configuration.py index 86e3ad9f02..ec85e4b45f 100644 --- a/frontends/concrete-python/tests/compilation/test_configuration.py +++ b/frontends/concrete-python/tests/compilation/test_configuration.py @@ -5,10 +5,12 @@ import os import sys +import numpy as np import pytest from concrete import fhe -from concrete.fhe.compilation import Configuration +from concrete.fhe.compilation import Configuration, server +from concrete.fhe.compilation.configuration import SecurityLevel from ..conftest import USE_MULTI_PRECISION @@ -301,3 +303,46 @@ def test_configuration_show_bit_width_constraints_and_assignment( """.strip(), ) + + +def test_set_security_level(): + + @fhe.module() + class Module: + @fhe.function({"x": "encrypted", "y": "clear"}) + def inc(x, y): + return (x + y + 1) % 20 + + composition = fhe.Wired( + { + fhe.Wire(fhe.Output(inc, 0), fhe.AllInputs(inc)), + } + ) + + inputset = [ + (np.random.randint(1, 20, size=()), np.random.randint(1, 20, size=())) for _ in range(100) + ] + + module1 = Module.compile( + {"inc": inputset}, + security_level=SecurityLevel.SECURITY_128_BITS, + ) + + module2 = Module.compile( + {"inc": inputset}, + security_level=SecurityLevel.SECURITY_128_BITS, + ) + + module3 = Module.compile( + {"inc": inputset}, + security_level=SecurityLevel.SECURITY_132_BITS, + ) + + assert ( + module1.server.client_specs.program_info.get_keyset_info() + == module2.server.client_specs.program_info.get_keyset_info() + ) + assert ( + module1.server.client_specs.program_info.get_keyset_info() + != module3.server.client_specs.program_info.get_keyset_info() + ) diff --git a/tools/parameter-curves/concrete-security-curves-cpp/include/concrete/curves.gen.h b/tools/parameter-curves/concrete-security-curves-cpp/include/concrete/curves.gen.h index 92d7597d2f..b9350f49ea 100644 --- a/tools/parameter-curves/concrete-security-curves-cpp/include/concrete/curves.gen.h +++ b/tools/parameter-curves/concrete-security-curves-cpp/include/concrete/curves.gen.h @@ -1,5 +1,6 @@ SecurityCurve curves[] = { SecurityCurve(128, -0.025696778711484593, 2.675931372549016, 450, KeyFormat::BINARY), + SecurityCurve(132, -0.024891456582633045, 2.65734593837534, 450, KeyFormat::BINARY), }; -size_t curvesLen = 1; +size_t curvesLen = 2; diff --git a/tools/parameter-curves/concrete-security-curves-rust/src/gaussian/curves_gen.rs b/tools/parameter-curves/concrete-security-curves-rust/src/gaussian/curves_gen.rs index b173b549b3..8d1453dddb 100644 --- a/tools/parameter-curves/concrete-security-curves-rust/src/gaussian/curves_gen.rs +++ b/tools/parameter-curves/concrete-security-curves-rust/src/gaussian/curves_gen.rs @@ -1,4 +1,19 @@ use super::security_weights::SecurityWeights; -pub const SECURITY_WEIGHTS_ARRAY: [(u64, SecurityWeights); 1] = [ - (128, SecurityWeights { slope: -0.025696778711484593, bias: 2.675931372549016, minimal_lwe_dimension: 450 }), +pub const SECURITY_WEIGHTS_ARRAY: [(u64, SecurityWeights); 2] = [ + ( + 128, + SecurityWeights { + slope: -0.025696778711484593, + bias: 2.675931372549016, + minimal_lwe_dimension: 450, + }, + ), + ( + 132, + SecurityWeights { + slope: -0.024891456582633045, + bias: 2.65734593837534, + minimal_lwe_dimension: 450, + }, + ), ];