Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aPere3 committed Nov 21, 2024
1 parent c6d7fd4 commit f2e53f8
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class KeysetCache {
KeysetCache() = default;
};

Message<concreteprotocol::KeysetInfo> generate_generic_keyset_info(
Message<concreteprotocol::KeysetInfo> keysetInfoFromVirtualCircuit(
std::vector<concrete_optimizer::utils::PartitionDefinition> partitions,
bool generate_fks);
bool generate_fks, std::optional<concrete_optimizer::Options> options);

} // namespace keysets
} // namespace concretelang
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
// Exceptions. See
// https://github.com/zama-ai/concrete/blob/main/LICENSE.txt
// for license information.
#ifndef CONCRETELANG_COMMON_SECURITY_H
#define CONCRETELANG_COMMON_SECURITY_H

#ifndef CONCRETELANG_SUPPORT_V0CURVES_H_
#define CONCRETELANG_SUPPORT_V0CURVES_H_

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <vector>

namespace concrete {
namespace concretelang {
namespace security {

enum KeyFormat {
BINARY,
Expand Down Expand Up @@ -42,31 +37,16 @@ struct SecurityCurve {
/// @param polynomialSize The size of the polynom of the glwe
/// @param logQ The log of q
/// @return The secure encryption variances
double getVariance(int glweDimension, int polynomialSize, int logQ) {
auto size = glweDimension * polynomialSize;
if (size < minimalLweDimension) {
return NAN;
}
auto a = std::pow(2, (slope * size + bias) * 2);
auto b = std::pow(2, -2 * (logQ - 2));
return a > b ? a : b;
}
double getVariance(int glweDimension, int polynomialSize, int logQ);
};

#include "curves.gen.h"

/// @brief Return the security curve for a given level and a key format.
/// @param bitsOfSecurity The number of bits of security
/// @param keyFormat The format of the key
/// @return The security curve or nullptr if the curve is not found.
SecurityCurve *getSecurityCurve(int bitsOfSecurity, KeyFormat keyFormat) {
for (size_t i = 0; i < curvesLen; i++) {
if (curves[i].bits == bitsOfSecurity && curves[i].keyFormat == keyFormat)
return &curves[i];
}
return nullptr;
}
SecurityCurve *getSecurityCurve(int bitsOfSecurity, KeyFormat keyFormat);

} // namespace concrete
} // namespace security
} // namespace concretelang

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "concretelang/Support/Error.h"
#include "concretelang/Support/V0Parameters.h"
#include "concretelang/Support/logging.h"
#include <cstdint>
#include <filesystem>
#include <memory>
#include <mlir-c/Bindings/Python/Interop.h>
Expand Down Expand Up @@ -326,6 +325,122 @@ void mlir::concretelang::python::populateCompilerAPISubmodule(
.doc() = "Allow to restrict the optimizer search space to be compatible "
"with a keyset.";

// ------------------------------------------------------------------------------//
// OPTIMIZER OPTIONS //
// ------------------------------------------------------------------------------//
pybind11::class_<concrete_optimizer::Options>(m, "OptimizerOptions")
.def(
"set_security_level",
[](concrete_optimizer::Options &options, uint64_t security_level) {
options.security_level = security_level;
},
"Set option for security level.", arg("security_level"))
.def(
"set_maximum_acceptable_error_probability",
[](concrete_optimizer::Options &options,
double maximum_acceptable_error_probability) {
options.maximum_acceptable_error_probability =
maximum_acceptable_error_probability;
},
"Set option for maximum acceptable error probability.",
arg("maximum_acceptable_error_probability"))
.def(
"set_key_sharing",
[](concrete_optimizer::Options &options, bool key_sharing) {
options.key_sharing = key_sharing;
},
"Set option for key sharing.", arg("key_sharing"))
.def(
"set_multi_param_strategy_to_by_precision",
[](concrete_optimizer::Options &options) {
options.multi_param_strategy =
concrete_optimizer::MultiParamStrategy::ByPrecision;
},
"Set option for multi param strategy to by-precision.")
.def(
"set_multi_param_strategy_to_by_precision_and_norm_2",
[](concrete_optimizer::Options &options) {
options.multi_param_strategy =
concrete_optimizer::MultiParamStrategy::ByPrecisionAndNorm2;
},
"Set option for multi param strategy to by-precision-and-norm2.")
.def(
"set_default_log_norm2_woppbs",
[](concrete_optimizer::Options &options,
double default_log_norm2_woppbs) {
options.default_log_norm2_woppbs = default_log_norm2_woppbs;
},
"Set option for default log norm2 woppbs.",
arg("default_log_norm2_woppbs"))
.def(
"set_use_gpu_constraints",
[](concrete_optimizer::Options &options, bool use_gpu_constraints) {
options.use_gpu_constraints = use_gpu_constraints;
},
"Set option for use gpu constrints.", arg("use_gpu_constraints"))
.def(
"set_encoding_to_auto",
[](concrete_optimizer::Options &options) {
options.encoding = concrete_optimizer::Encoding::Auto;
},
"Set option for encoding to auto.")
.def(
"set_encoding_to_crt",
[](concrete_optimizer::Options &options) {
options.encoding = concrete_optimizer::Encoding::Crt;
},
"Set option for encoding to crt.")
.def(
"set_encoding_to_native",
[](concrete_optimizer::Options &options) {
options.encoding = concrete_optimizer::Encoding::Native;
},
"Set option for encoding to native.")
.def(
"set_cache_on_disk",
[](concrete_optimizer::Options &options, bool cache_on_disk) {
options.cache_on_disk = cache_on_disk;
},
"Set option for cache on disk.", arg("cache_on_disk"))
.def(
"set_ciphertext_modulus_log",
[](concrete_optimizer::Options &options,
uint32_t ciphertext_modulus_log) {
options.ciphertext_modulus_log = ciphertext_modulus_log;
},
"Set option for ciphertext modulus log.",
arg("ciphertext_modulus_log"))
.def(
"set_fft_precision",
[](concrete_optimizer::Options &options, uint32_t fft_precision) {
options.fft_precision = fft_precision;
},
"Set option for fft precision.", arg("fft_precision"))
.def(
"set_fft_precision",
[](concrete_optimizer::Options &options, uint32_t fft_precision) {
options.fft_precision = fft_precision;
},
"Set option for fft precision.", arg("fft_precision"))
.def(
"set_range_restriction",
[](concrete_optimizer::Options &options,
concrete_optimizer::restriction::RangeRestriction restriction) {
options.range_restriction = std::make_shared<
concrete_optimizer::restriction::RangeRestriction>(restriction);
},
"Set option for range restriction", arg("restriction"))
.def(
"set_keyset_restriction",
[](concrete_optimizer::Options &options,
concrete_optimizer::restriction::KeysetRestriction restriction) {
options.keyset_restriction = std::make_shared<
concrete_optimizer::restriction::KeysetRestriction>(
restriction);
},
"Set option for keyset restriction", arg("restriction"))
.doc() = "Options for the optimizer.";

// ------------------------------------------------------------------------------//
// COMPILATION OPTIONS //
// ------------------------------------------------------------------------------//
Expand Down Expand Up @@ -972,18 +1087,20 @@ void mlir::concretelang::python::populateCompilerAPISubmodule(
typedef Message<concreteprotocol::KeysetInfo> KeysetInfo;
pybind11::class_<KeysetInfo>(m, "KeysetInfo")
.def_static(
"generate_generic",
"generate_virtual",
[](std::vector<concrete_optimizer::utils::PartitionDefinition>
partitions,
bool generateFks) -> KeysetInfo {
bool generateFks,
std::optional<concrete_optimizer::Options> options) -> KeysetInfo {
if (partitions.size() < 2) {
throw std::runtime_error("Need at least two partition defs to "
"generate a generic keyset info.");
"generate a virtual keyset info.");
}
return ::concretelang::keysets::generate_generic_keyset_info(
partitions, generateFks);
return ::concretelang::keysets::keysetInfoFromVirtualCircuit(
partitions, generateFks, options);
},
arg("partition_defs"), arg("generate_fks"),
arg("options") = std::nullopt,
"Generate a generic keyset info for a set of partition definitions")
.def(
"secret_keys",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_mlir_library(
Keys.cpp
Keysets.cpp
Transformers.cpp
Security.cpp
Values.cpp
DEPENDS
concrete-protocol
Expand Down
Loading

0 comments on commit f2e53f8

Please sign in to comment.