rust_sike/isogeny/
publickey.rs

1//! Public key material
2
3use crate::ff::FiniteField;
4
5/// Public key
6///
7/// The public key is a curve, but can be represented as a triple of points, of which only
8/// the x-coordinate is stored.
9#[derive(Clone)]
10pub struct PublicKey<K: FiniteField> {
11    /// First point
12    pub x1: K,
13
14    /// Second point
15    pub x2: K,
16
17    /// Third point
18    pub x3: K,
19}
20
21impl<K: FiniteField + std::fmt::Debug> std::fmt::Debug for PublicKey<K> {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        write!(f, "{:?}, {:?}, {:?}", self.x1, self.x2, self.x3)
24    }
25}
26
27impl<K: FiniteField> PublicKey<K> {
28    /// Converts the public key to a sequence of bytes (for each point)
29    pub fn into_bytes(self) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
30        (
31            self.x1.into_bytes(),
32            self.x2.into_bytes(),
33            self.x3.into_bytes(),
34        )
35    }
36
37    /// Creates a new public key for given three points (represented as bytes)
38    pub fn from_bytes(part1: &[u8], part2: &[u8], part3: &[u8]) -> Result<Self, String> {
39        Ok(Self {
40            x1: K::from_bytes(part1)?,
41            x2: K::from_bytes(part2)?,
42            x3: K::from_bytes(part3)?,
43        })
44    }
45}
46
47impl<K: FiniteField> std::cmp::PartialEq for PublicKey<K> {
48    fn eq(&self, other: &Self) -> bool {
49        self.x1.equals(&other.x1) && self.x2.equals(&other.x2) && self.x3.equals(&other.x3)
50    }
51}