diff --git a/Cargo.toml b/Cargo.toml index 414d485b5..2441bb575 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..041d3a7f4 --- /dev/null +++ b/build.rs @@ -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) +} diff --git a/cbindgen.toml b/cbindgen.toml new file mode 100644 index 000000000..299a72de4 --- /dev/null +++ b/cbindgen.toml @@ -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"] diff --git a/src/lib.rs b/src/lib.rs index c7b4153fb..a6c6d4ba9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::( + 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 + }, + } +} diff --git a/src/optimisation/atomic_pattern.rs b/src/optimisation/atomic_pattern.rs index 9a1b5d33f..8e2779770 100644 --- a/src/optimisation/atomic_pattern.rs +++ b/src/optimisation/atomic_pattern.rs @@ -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