Skip to content

Commit

Permalink
mp-spdz-rs: build: Link ntl, libsodium, and gmp into library object
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Mar 30, 2024
1 parent 0ade49c commit 8b5ac15
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
6 changes: 3 additions & 3 deletions mp-spdz-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
1. Run `git submodule update --init --recursive` to clone the submodules in this repo.
2. Install `libsodium` & `gmp` via:
```zsh
brew install libsodium gmp
yum install libsodium gmp
apt-get install libsodium gmp
brew install libsodium gmp boost ntl
yum install libsodium gmp boost ntl
apt-get install libsodium gmp boost ntl
```
based on your local package manager.
32 changes: 28 additions & 4 deletions mp-spdz-rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ use itertools::Itertools;
// Build entrypoint
fn main() {
// Resolve the libsodium and gmp include paths
let libsodium_include = find_package("libsodium");
let gmp_include = find_package("gmp");
let libsodium_include = find_include_path("libsodium");
let gmp_include = find_include_path("gmp");
let ntl_include = find_include_path("ntl");

// Build the c++ bridge
cxx_build::bridge("src/lib.rs")
.file("src/include/MP-SPDZ/FHE/Ring_Element.cpp")
.file("src/include/MP-SPDZ/FHE/Ring.cpp")
.file("src/include/MP-SPDZ/FHE/NTL-Subs.cpp")
.file("src/include/MP-SPDZ/Math/bigint.cpp")
.include("src/include/MP-SPDZ")
.include("src/include/MP-SPDZ/deps")
.include(libsodium_include)
.include(gmp_include)
.include(ntl_include)
.define("USE_NTL", None)
.std("c++17")
.compile("mp-spdz-cxx");

// Linker args
println!("cargo:rustc-link-arg=-L{}", find_lib_path("ntl"));
println!("cargo:rustc-link-arg=-lntl");
println!("cargo:rustc-link-arg=-L{}", find_lib_path("gmp"));
println!("cargo:rustc-link-arg=-lgmp");

// Build cache flags
println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=../MP-SPDZ");
}
Expand All @@ -32,6 +44,18 @@ fn get_host_triple() -> Vec<String> {
host_triple.split('-').map(|s| s.to_string()).collect_vec()
}

/// Find the include location for a package
fn find_include_path(name: &str) -> String {
let base_path = find_package(name);
format!("{base_path}/include")
}

/// Find the lib location for a package
fn find_lib_path(name: &str) -> String {
let base_path = find_package(name);
format!("{base_path}/lib")
}

/// Resolve a package's include location
///
/// Windows is not supported
Expand Down Expand Up @@ -65,7 +89,7 @@ fn find_package_macos(name: &str) -> String {

// Parse the output from stdout
let path = parse_utf8(&output.stdout).trim().to_string();
format!("{path}/include")
path
}

/// Find a package on Linux
Expand Down
14 changes: 5 additions & 9 deletions mp-spdz-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@
#[cxx::bridge]
pub mod ffi {
struct Test {
a: i32,
b: i32,
}

unsafe extern "C++" {
include!("FHE/Ring_Element.h");
include!("FHE/Ring.h");

fn test_method();
type Ring;
fn new_ring(m: i32) -> UniquePtr<Ring>;
}
}

#[cfg(test)]
mod test {
use crate::ffi::test_method;
use super::ffi::*;

#[test]
fn test_dummy() {
test_method();
let ring = new_ring(10);
}
}

0 comments on commit 8b5ac15

Please sign in to comment.