Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Aug 23, 2023
2 parents dc08ab4 + 9a40a16 commit f10ffa7
Show file tree
Hide file tree
Showing 11 changed files with 1,421 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: cargo
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
hex = { version = "0.4.3", features = ["serde"] }
pqcrypto-kyber = { version = "0.7.6", default-features = false }
libcrux-pqclean = { version = "*", path = "sys/pqclean" }

# Benchmarking "RustCrypto"
chacha20poly1305 = "0.10"
Expand Down
37 changes: 32 additions & 5 deletions benches/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod util;
use util::*;

macro_rules! impl_comp {
($fun:ident, $libcrux:expr, $rust_crypto:ty, $openssl:expr) => {
($fun:ident, $libcrux:expr, $rust_crypto:ty, $openssl:expr, $pqclean:expr) => {
// Comparing libcrux performance for different payload sizes and other implementations.
fn $fun(c: &mut Criterion) {
const PAYLOAD_SIZES: [usize; 1] = [1024 * 1024 * 10];
Expand Down Expand Up @@ -65,6 +65,29 @@ macro_rules! impl_comp {
)
},
);

if stringify!($fun) != "Sha3_224" {
group.bench_with_input(
BenchmarkId::new("PQClean", fmt(*payload_size)),
payload_size,
|b, payload_size| {
b.iter_batched(
|| randombytes(*payload_size),
|payload| {
let mut digest = [0; libcrux::digest::digest_size($libcrux)];
unsafe {
$pqclean(
digest.as_mut_ptr(),
payload.as_ptr() as _,
payload.len(),
)
};
},
BatchSize::SmallInput,
)
},
);
}
}
}
};
Expand All @@ -74,25 +97,29 @@ impl_comp!(
Sha3_224,
Algorithm::Sha3_224,
sha3::Sha3_224,
MessageDigest::sha3_224()
MessageDigest::sha3_224(),
libcrux_pqclean::sha3_256 // This is wrong, but it's not actually used.
);
impl_comp!(
Sha3_256,
Algorithm::Sha3_256,
sha3::Sha3_256,
MessageDigest::sha3_256()
MessageDigest::sha3_256(),
libcrux_pqclean::sha3_256
);
impl_comp!(
Sha3_384,
Algorithm::Sha3_384,
sha3::Sha3_384,
MessageDigest::sha3_384()
MessageDigest::sha3_384(),
libcrux_pqclean::sha3_384
);
impl_comp!(
Sha3_512,
Algorithm::Sha3_512,
sha3::Sha3_512,
MessageDigest::sha3_512()
MessageDigest::sha3_512(),
libcrux_pqclean::sha3_512
);

fn benchmarks(c: &mut Criterion) {
Expand Down
2 changes: 2 additions & 0 deletions sys/pqclean/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
Cargo.lock
11 changes: 11 additions & 0 deletions sys/pqclean/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "libcrux-pqclean"
version = "0.1.0"
edition = "2021"

[dependencies]

[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
bindgen = "0.66"
fs_extra = "1.2"
5 changes: 5 additions & 0 deletions sys/pqclean/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# PQClean sys

A simple sys crate to benchmark pqclean clode that is not exposed to Rust, like SHA3.

The code is taken from https://github.com/PQClean/PQClean/commit/ef80f51badac27b78f8e0ef26f23cbd71ca0f283.
98 changes: 98 additions & 0 deletions sys/pqclean/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::{env, path::Path};

fn copy_files(home_path: &Path, out_path: &Path) {
let mut options = fs_extra::dir::CopyOptions::new();
options.overwrite = true;
fs_extra::dir::copy(home_path.join("c"), out_path, &options).unwrap();
}

#[cfg(not(windows))]
fn create_bindings(home_dir: &Path) {
let c_dir = home_dir.join("c");
let clang_args = vec![format!("-I{}", c_dir.display())];

let bindings = bindgen::Builder::default()
// Header to wrap headers
.header("c/fips202.h")
// Set include paths for headers
.clang_args(clang_args)
// Include the things we want
.allowlist_function("shake.*")
.allowlist_function("sha3.*")
.allowlist_type("sha3.*")
.allowlist_type("shake.*")
.allowlist_type("SHAKE.*")
.allowlist_type("SHA3.*")
.allowlist_var("SHAKE.*")
.allowlist_var("SHA3.*")
// Disable tests to avoid warnings and keep it portable
.layout_tests(false)
// Generate bindings
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.use_core()
.generate()
.expect("Unable to generate bindings");

let home_bindings = home_dir.join("src/bindings.rs");
bindings
.write_to_file(home_bindings)
.expect("Couldn't write bindings!");
}

#[cfg(windows)]
fn create_bindings(_: &Path) {}

fn compile_files(library_name: &str, files: &[String], out_path: &Path, args: &[String]) {
let c_dir = out_path.join("c");

let mut build = cc::Build::new();
build
.files(files.iter().map(|fname| c_dir.join(fname)))
.warnings_into_errors(true)
.no_default_flags(true);

build.include(c_dir.join("include"));
build.flag("-O3").flag("-c");
for arg in args {
build.flag(arg);
}

build.compile(library_name);
}

fn build(out_path: &Path) {
let files = vec!["fips202.c".to_string()];
let args = vec![];
compile_files("libpqclean.a", &files, out_path, &args);
}

pub fn main() -> Result<(), u8> {
// Get ENV variables
let home_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let home_path = Path::new(&home_dir);
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);

// Moving C/ASM code to output to make build easier.
copy_files(home_path, out_path);

// Build the C/ASM files
build(out_path);

// Set library name to look up
let library_name = "pqclean";

// Set re-run trigger for all of s
println!("cargo:rerun-if-changed=cs");

// Generate new bindings. This is a no-op on Windows.
create_bindings(home_path);

// Link hacl library.
let mode = "static";
println!("cargo:rustc-link-lib={}={}", mode, library_name);
println!("cargo:rustc-link-search=native={}", out_path.display());
println!("cargo:lib={}", out_path.display());

Ok(())
}
Loading

0 comments on commit f10ffa7

Please sign in to comment.