1
//! See [`PublicKeyExt`].
2

            
3
use curve25519_dalek::{montgomery::MontgomeryPoint, ristretto::RistrettoPoint};
4
use opaque_ke::keypair::PublicKey;
5

            
6
/// Utility trait to help convert and compare [`opaque_ke::keypair::PublicKey`]
7
/// to `[u8; 33]`.
8
pub(crate) trait PublicKeyExt {
9
	/// Convert [`opaque_ke::keypair::PublicKey`] to `[u8; 33]`.
10
	fn into_array(self) -> [u8; 33];
11

            
12
	/// Convert [`opaque_ke::keypair::PublicKey`] reference to `[u8; 33]`.
13
	fn to_array(&self) -> [u8; 33];
14

            
15
	/// Compare [`opaque_ke::keypair::PublicKey`] to `[u8; 33]`.
16
	fn is_array(&self, key: [u8; 33]) -> bool;
17
}
18

            
19
impl PublicKeyExt for PublicKey<RistrettoPoint> {
20
167
	fn into_array(self) -> [u8; 33] {
21
167
		Self::to_array(&self)
22
167
	}
23

            
24
421
	fn to_array(&self) -> [u8; 33] {
25
421
		let mut key = [0; 33];
26
421
		key[..32].copy_from_slice(self);
27
421

            
28
421
		key
29
421
	}
30

            
31
84
	fn is_array(&self, key: [u8; 33]) -> bool {
32
84
		&key[..32] == self.as_slice()
33
84
	}
34
}
35

            
36
impl PublicKeyExt for PublicKey<MontgomeryPoint> {
37
144
	fn into_array(self) -> [u8; 33] {
38
144
		Self::to_array(&self)
39
144
	}
40

            
41
360
	fn to_array(&self) -> [u8; 33] {
42
360
		let mut key = [0; 33];
43
360
		key[..32].copy_from_slice(self);
44
360

            
45
360
		key
46
360
	}
47

            
48
72
	fn is_array(&self, key: [u8; 33]) -> bool {
49
72
		&key[..32] == self.as_slice()
50
72
	}
51
}
52

            
53
#[cfg(feature = "p256")]
54
impl PublicKeyExt for PublicKey<super::p256::P256> {
55
144
	fn into_array(self) -> [u8; 33] {
56
144
		(**self).into()
57
144
	}
58

            
59
216
	fn to_array(&self) -> [u8; 33] {
60
216
		AsRef::<[u8; 33]>::as_ref(&***self).to_owned()
61
216
	}
62

            
63
72
	fn is_array(&self, key: [u8; 33]) -> bool {
64
72
		key == self.as_slice()
65
72
	}
66
}