2019-08-06 03:49:11 +00:00
|
|
|
use ssz::{Decode, DecodeError, Encode};
|
|
|
|
|
|
|
|
use super::{PublicKey, BLS_PUBLIC_KEY_BYTE_SIZE};
|
|
|
|
|
|
|
|
bytes_struct!(
|
|
|
|
PublicKeyBytes,
|
|
|
|
PublicKey,
|
|
|
|
BLS_PUBLIC_KEY_BYTE_SIZE,
|
2019-11-05 04:46:52 +00:00
|
|
|
"public key"
|
2019-08-06 03:49:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
use ssz::ssz_encode;
|
|
|
|
|
|
|
|
use super::super::Keypair;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_valid_public_key() {
|
|
|
|
let keypair = Keypair::random();
|
|
|
|
|
|
|
|
let bytes = ssz_encode(&keypair.pk);
|
|
|
|
let public_key_bytes = PublicKeyBytes::from_bytes(&bytes).unwrap();
|
|
|
|
let public_key: Result<PublicKey, _> = (&public_key_bytes).try_into();
|
|
|
|
assert!(public_key.is_ok());
|
|
|
|
assert_eq!(keypair.pk, public_key.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-08-30 05:49:06 +00:00
|
|
|
#[cfg(not(feature = "fake_crypto"))]
|
2019-08-06 03:49:11 +00:00
|
|
|
pub fn test_invalid_public_key() {
|
|
|
|
let mut public_key_bytes = [0; BLS_PUBLIC_KEY_BYTE_SIZE];
|
|
|
|
public_key_bytes[0] = 255; //a_flag1 == b_flag1 == c_flag1 == 1 and x1 = 0 shouldn't be allowed
|
|
|
|
let public_key_bytes = PublicKeyBytes::from_bytes(&public_key_bytes[..]);
|
|
|
|
assert!(public_key_bytes.is_ok());
|
|
|
|
|
|
|
|
let public_key: Result<PublicKey, _> = public_key_bytes.as_ref().unwrap().try_into();
|
|
|
|
assert!(public_key.is_err());
|
|
|
|
}
|
|
|
|
}
|