Add tests for the new SharedSecret::new_with_hash() function

This commit is contained in:
Elichai Turkel 2019-11-10 00:04:05 +02:00
parent af8fa21a20
commit f80428258c
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 30 additions and 0 deletions

View File

@ -180,6 +180,36 @@ mod tests {
assert_eq!(sec1, sec2);
assert!(sec_odd != sec2);
}
#[test]
fn ecdh_with_hash() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
let (sk2, pk2) = s.generate_keypair(&mut thread_rng());
let sec1 = SharedSecret::new_with_hash(&pk1, &sk2, |x,_| x.into());
let sec2 = SharedSecret::new_with_hash(&pk2, &sk1, |x,_| x.into());
let sec_odd = SharedSecret::new_with_hash(&pk1, &sk1, |x,_| x.into());
assert_eq!(sec1, sec2);
assert_ne!(sec_odd, sec2);
}
#[test]
fn ecdh_with_hash_callback() {
let s = Secp256k1::signing_only();
let (sk1, pk1) = s.generate_keypair(&mut thread_rng());
let expect_result: [u8; 64] = [123; 64];
let mut x_out = [0u8; 32];
let mut y_out = [0u8; 32];
let result = SharedSecret::new_with_hash(&pk1, &sk1, | x, y | {
x_out = x;
y_out = y;
expect_result.into()
});
assert_eq!(&expect_result[..], &result[..]);
assert_ne!(x_out, [0u8; 32]);
assert_ne!(y_out, [0u8; 32]);
}
}
#[cfg(all(test, feature = "unstable"))]