Skip to content

Commit

Permalink
Move ECDH tests to the libcrux-ecdh crate
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider-bensch committed Jun 12, 2024
1 parent d0ad351 commit b119396
Show file tree
Hide file tree
Showing 5 changed files with 5,316 additions and 9 deletions.
7 changes: 7 additions & 0 deletions libcrux-ecdh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ path = "src/ecdh.rs"
[dependencies]
rand = { version = "0.8" }
libcrux-hacl = { version = "=0.0.2-pre.2", path = "../sys/hacl" }

[dev-dependencies]
libcrux = { version = "=0.0.2-pre.2", path = "../", features = ["rand"] }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
pretty_env_logger = "0.5"
24 changes: 15 additions & 9 deletions tests/p256.rs → libcrux-ecdh/tests/p256.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(target_arch = "wasm32"))]
use libcrux::drbg;
use libcrux::ecdh::{self, key_gen};
use libcrux_ecdh::{self, key_gen};
#[cfg(target_arch = "wasm32")]
use rand_core::OsRng;

Expand All @@ -12,11 +12,13 @@ fn derive_rand() {
#[cfg(target_arch = "wasm32")]
let mut rng = OsRng;

let (private_a, public_a) = key_gen(ecdh::Algorithm::P256, &mut rng).unwrap();
let (private_b, public_b) = key_gen(ecdh::Algorithm::P256, &mut rng).unwrap();
let (private_a, public_a) = key_gen(libcrux_ecdh::Algorithm::P256, &mut rng).unwrap();
let (private_b, public_b) = key_gen(libcrux_ecdh::Algorithm::P256, &mut rng).unwrap();

let shared_a = ecdh::derive(ecdh::Algorithm::P256, &public_b, &private_a).unwrap();
let shared_b = ecdh::derive(ecdh::Algorithm::P256, &public_a, &private_b).unwrap();
let shared_a =
libcrux_ecdh::derive(libcrux_ecdh::Algorithm::P256, &public_b, &private_a).unwrap();
let shared_b =
libcrux_ecdh::derive(libcrux_ecdh::Algorithm::P256, &public_a, &private_b).unwrap();
eprintln!("a = {}", hex::encode(&private_a));
eprintln!("A = {}", hex::encode(&public_a));
eprintln!("b = {}", hex::encode(&private_b));
Expand All @@ -36,15 +38,19 @@ fn derive() {
hex::decode("17e1ebef41df589d0483aa0ec4302abbe2dcc3da2e87211e09f36eb40131f304").unwrap();
let public_b = hex::decode("de9d41b163a0804b968b37ba21caec240e8191977ddf4d0594d656289a6cf96b260caee19e0e3b03bfa11361c9f02027c625a9f1ad4c832e0eb4684a8b32237b").unwrap();

let public_a_comp = ecdh::secret_to_public(ecdh::Algorithm::P256, &private_a).unwrap();
let public_a_comp =
libcrux_ecdh::secret_to_public(libcrux_ecdh::Algorithm::P256, &private_a).unwrap();
assert_eq!(public_a, public_a_comp);
let public_b_comp = ecdh::secret_to_public(ecdh::Algorithm::P256, &private_b).unwrap();
let public_b_comp =
libcrux_ecdh::secret_to_public(libcrux_ecdh::Algorithm::P256, &private_b).unwrap();
assert_eq!(public_b, public_b_comp);

let expected_shared = hex::decode("9839a5cf9b295e385e274dad44a3acf9d285bfc7ba8cfbe36c132f1c6967ab081ce2d1405f436ba09810a9c89b6a407ca3aec13519dec058d487e89520d3ac5e").unwrap();

let shared_a = ecdh::derive(ecdh::Algorithm::P256, &public_b, &private_a).unwrap();
let shared_b = ecdh::derive(ecdh::Algorithm::P256, &public_a, &private_b).unwrap();
let shared_a =
libcrux_ecdh::derive(libcrux_ecdh::Algorithm::P256, &public_b, &private_a).unwrap();
let shared_b =
libcrux_ecdh::derive(libcrux_ecdh::Algorithm::P256, &public_a, &private_b).unwrap();
eprintln!("a = {}", hex::encode(&private_a));
eprintln!("A = {}", hex::encode(&public_a));
eprintln!("b = {}", hex::encode(&private_b));
Expand Down
46 changes: 46 additions & 0 deletions libcrux-ecdh/tests/test_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![allow(dead_code)]

pub use serde::{self, de::DeserializeOwned};
pub use std::fs::File;
pub use std::io::BufReader;

use std::num::ParseIntError;

pub(crate) trait ReadFromFile {
fn from_file<T: DeserializeOwned>(file_str: &'static str) -> T {
let file = match File::open(file_str) {
Ok(f) => f,
Err(_) => panic!("Couldn't open file {file_str}."),
};
let reader = BufReader::new(file);
match serde_json::from_reader(reader) {
Ok(r) => r,
Err(e) => {
println!("{:?}", e);
panic!("Error reading file {file_str}.")
}
}
}
}

pub(crate) fn hex_str_to_bytes(val: &str) -> Vec<u8> {
let b: Result<Vec<u8>, ParseIntError> = (0..val.len())
.step_by(2)
.map(|i| u8::from_str_radix(&val[i..i + 2], 16))
.collect();
b.expect("Error parsing hex string")
}

pub(crate) fn hex_str_to_array<A>(val: &str) -> A
where
A: Default + AsMut<[u8]>,
{
let b: Result<Vec<u8>, ParseIntError> = (0..val.len())
.step_by(2)
.map(|i| u8::from_str_radix(&val[i..i + 2], 16))
.collect();
let b = b.expect("Error parsing hex string");
let mut out = A::default();
A::as_mut(&mut out).clone_from_slice(&b);
out
}
Loading

0 comments on commit b119396

Please sign in to comment.