fix ecdh tests

This commit is contained in:
bunnie 2022-06-01 03:41:58 +08:00
parent 01a10ee709
commit 3647c38cb0
2 changed files with 47 additions and 11 deletions

View file

@ -76,6 +76,29 @@ impl SecKey {
} }
impl PubKey { impl PubKey {
#[cfg(test)]
fn from_bytes_uncompressed(bytes: &[u8]) -> Option<PubKey> {
use p256::elliptic_curve::sec1::FromEncodedPoint;
if let Ok(ep) = EncodedPoint::from_bytes(bytes) {
let maybe_p = PublicKey::from_encoded_point(&ep);
if bool::from(maybe_p.is_some()) {
Some(PubKey {p: maybe_p.unwrap()})
} else {
None
}
// PointP256::from_bytes_uncompressed_vartime(bytes).map(|p| PubKey { p })
} else {
None
}
}
#[cfg(test)]
fn to_bytes_uncompressed(&self, bytes: &mut [u8; 65]) {
bytes.copy_from_slice(self.p.to_encoded_point(false).as_bytes());
//self.p.to_bytes_uncompressed(bytes);
}
pub fn from_coordinates(x: &[u8; NBYTES], y: &[u8; NBYTES]) -> Option<PubKey> { pub fn from_coordinates(x: &[u8; NBYTES], y: &[u8; NBYTES]) -> Option<PubKey> {
match PublicKey::from_sec1_bytes( match PublicKey::from_sec1_bytes(
EncodedPoint::from_affine_coordinates( EncodedPoint::from_affine_coordinates(
@ -101,9 +124,12 @@ impl PubKey {
); );
} }
} }
/*
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use p256::AffinePoint;
use p256::elliptic_curve::sec1::FromEncodedPoint;
use super::super::rng256::ThreadRng256; use super::super::rng256::ThreadRng256;
use super::*; use super::*;
@ -121,7 +147,10 @@ mod test {
for _ in 0..ITERATIONS { for _ in 0..ITERATIONS {
let sk = SecKey::gensk(&mut rng); let sk = SecKey::gensk(&mut rng);
let pk = sk.genpk(); let pk = sk.genpk();
assert!(pk.p.is_valid_vartime()); // this is a recoding of the is_valid_point_vartime() call
let encoded = pk.p.to_encoded_point(false);
let maybe_affine = AffinePoint::from_encoded_point(&encoded);
assert!(bool::from(maybe_affine.is_some()));
} }
} }
@ -160,4 +189,3 @@ mod test {
// TODO: tests with invalid public shares. // TODO: tests with invalid public shares.
} }
*/

View file

@ -156,14 +156,22 @@ impl Signature {
if bytes.len() != 64 { if bytes.len() != 64 {
None None
} else { } else {
let r_nzs = NonZeroScalar::try_from(&bytes[..32]).unwrap(); let maybe_r_nzs = NonZeroScalar::try_from(&bytes[..32]);
let s_nzs = NonZeroScalar::try_from(&bytes[32..]).unwrap(); let maybe_s_nzs = NonZeroScalar::try_from(&bytes[32..]);
match P256Signature::from_scalars( if let Ok(r_nzs) = maybe_r_nzs {
FieldBytes::from(r_nzs), // r if let Ok(s_nzs) = maybe_s_nzs {
FieldBytes::from(s_nzs), // s match P256Signature::from_scalars(
) { FieldBytes::from(r_nzs), // r
Ok(sig) => Some(Signature{sig}), FieldBytes::from(s_nzs), // s
_ => None, ) {
Ok(sig) => Some(Signature{sig}),
_ => None,
}
} else {
None
}
} else {
None
} }
/* /*
let r = let r =