feat(optimize_bootstrap): C interface

This commit is contained in:
rudy
2022-03-10 17:38:34 +01:00
committed by rudy-6-4
parent b7a7264257
commit e97fe3827a
5 changed files with 96 additions and 0 deletions

View File

@@ -20,5 +20,15 @@ rayon-cond = "0.2" # to avoid rayon code coloring
rayon = "1.5.1"
text-diff = "0.4.0"
[build-dependencies]
cbindgen = "0.20.0"
[lib]
crate-type= [
"staticlib", # concretecompiler
"lib", # rust
]
[[example]]
name = "v0_parameters"
crate-type= ["bin"]

34
build.rs Normal file
View File

@@ -0,0 +1,34 @@
use cbindgen::Config;
use std::env;
use std::path::{Path, PathBuf};
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let package_name = env::var("CARGO_PKG_NAME").unwrap();
let target_dir = match env::var("CARGO_TARGET_DIR") {
Ok(target) => PathBuf::from(target),
_ => PathBuf::from(crate_dir.clone()).join("target"),
};
let header_name = format!("{}.h", package_name);
let header_path = target_dir
.join("include")
.join(header_name)
.display()
.to_string();
let config = Config::from_file(Path::new(&crate_dir).join("cbindgen.toml")).unwrap();
let result = cbindgen::generate_with_config(&crate_dir, config);
let exit_code = match result {
Err(err) => {
eprintln!("{}", err);
1
}
Ok(content) => {
let _changed = content.write_to_file(&header_path);
0
}
};
std::process::exit(exit_code)
}

11
cbindgen.toml Normal file
View File

@@ -0,0 +1,11 @@
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml
# for detailed documentation of every option here.
language = "C++"
namespaces = ["concrete_optimizer"]
[parse]
include = ["optimisation"]
[export]
item_types = ["structs", "functions"]

View File

@@ -22,3 +22,43 @@ pub mod optimisation;
pub mod parameters;
pub mod security;
pub mod weight;
#[no_mangle]
pub extern "C" fn optimise_bootstrap(
precision: u64,
security_level: u64,
noise_factor: f64,
maximum_acceptable_error_probability: f64,
) -> optimisation::atomic_pattern::Solution {
let sum_size = 1;
let glwe_log_polynomial_sizes: Vec<_> = (10..=12).collect();
let glwe_dimensions: Vec<_> = (1..=1).collect();
let internal_lwe_dimensions: Vec<_> = (512..=1024).collect();
let result = optimisation::atomic_pattern::optimise_one::<u64>(
sum_size,
precision,
security_level,
noise_factor,
maximum_acceptable_error_probability,
&glwe_log_polynomial_sizes,
&glwe_dimensions,
&internal_lwe_dimensions,
None,
);
match result.best_solution {
Some(solution) => solution,
None => optimisation::atomic_pattern::Solution {
input_lwe_dimension: 0,
internal_ks_output_lwe_dimension: 0,
ks_decomposition_level_count: 0,
ks_decomposition_base_log: 0,
glwe_polynomial_size: 0,
glwe_dimension: 0,
br_decomposition_level_count: 0,
br_decomposition_base_log: 0,
complexity: 0.0,
noise_max: 0.0,
p_error: 1.0, // error probability
},
}
}

View File

@@ -43,6 +43,7 @@ const PARETO_CUTS: bool = true; // 75ms
const CROSS_PARETO_CUTS: bool = PARETO_CUTS && true; // 70ms
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Solution {
pub input_lwe_dimension: u64, //n_big
pub internal_ks_output_lwe_dimension: u64, //n_small