1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::scalar::Scalar;
use crate::primitive::bytes::{self, Bytes};
use crate::primitive::point::DisLogPoint;
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto;
use curve25519_dalek::traits::Identity;
use generic_array::typenum::U32;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Point(pub ristretto::RistrettoPoint);

impl Bytes for Point {
    type OutputSize = U32;

    fn from_bytes(data: bytes::Output<Self>) -> Self {
        let point = ristretto::CompressedRistretto::from_slice(&data);
        let de_point = point.decompress().unwrap();
        Self(de_point)
    }

    fn to_bytes(&self) -> bytes::Output<Self> {
        let p = self.0.compress();
        p.to_bytes().into()
    }
}

impl DisLogPoint for Point {
    const SIZE: usize = 32;

    type Scalar = Scalar;

    fn zero() -> Self {
        Self(ristretto::RistrettoPoint::identity())
    }

    fn one() -> Self {
        Self(ristretto::RistrettoPoint::identity())
    }

    fn basepoint() -> Self {
        Self(RISTRETTO_BASEPOINT_POINT)
    }

    fn add(&self, rhs: &Self) -> Self {
        Self(self.0 + rhs.0)
    }

    fn mul(&self, rhs: &Self::Scalar) -> Self {
        Self(self.0 * rhs.0)
    }

    fn neg(&self) -> Self {
        Self(-self.0)
    }

    fn eq(&self, o: &Self) -> bool {
        self.0 == o.0
    }
}