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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Define Point behavier.

use super::bytes::{self, Bytes};
use super::scalar::{Scalar, ScalarNumber};
use core::cmp::{Eq, PartialEq};
use core::fmt::Debug;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// Point trait.
pub trait DisLogPoint: Clone + Bytes + Debug {
    const SIZE: usize;

    type Scalar: ScalarNumber;

    fn zero() -> Self;

    fn one() -> Self;

    fn basepoint() -> Self;

    fn add(&self, rhs: &Self) -> Self;

    fn mul(&self, rhs: &Self::Scalar) -> Self;

    fn neg(&self) -> Self;

    fn eq(&self, o: &Self) -> bool;

    fn sub(&self, rhs: &Self) -> Self
    where
        Self: Sized,
    {
        self.add(&rhs.neg())
    }
}

/// Point.
#[derive(Debug, Clone, Eq)]
pub struct Point<P: DisLogPoint>(pub P);

impl<P: DisLogPoint> Point<P> {
    pub fn zero() -> Self {
        Point(P::zero())
    }

    pub fn one() -> Self {
        Point(P::one())
    }

    pub fn basepoint() -> Self {
        Point(P::basepoint())
    }
}

impl<P: DisLogPoint> Bytes for Point<P> {
    type OutputSize = P::OutputSize;

    fn to_bytes(&self) -> bytes::Output<Self> {
        self.0.to_bytes()
    }

    fn from_bytes(data: bytes::Output<Self>) -> Self {
        Self(P::from_bytes(data))
    }
}

impl<P: DisLogPoint> PartialEq for Point<P> {
    fn eq(&self, o: &Self) -> bool {
        self.0.eq(&o.0)
    }
}

macro_rules! impl_point {
    ($op:ident, $opf:ident, $t:ty, $lt:ty, $rt:ty) => {
        impl<'a, 'b, P: DisLogPoint> $op<$rt> for $lt {
            type Output = $t;
            fn $opf(self, rhs: $rt) -> $t {
                Point(self.0.$opf(&rhs.0))
            }
        }
    };
}

macro_rules! impl_scalar_point {
    ($op:ident, $opf:ident, $t:ty, $lt:ty, $rt:ty) => {
        impl<'a, 'b, S: ScalarNumber, P: DisLogPoint<Scalar = S>> $op<$rt> for $lt {
            type Output = $t;
            fn $opf(self, rhs: $rt) -> $t {
                Point(self.0.$opf(&rhs.0))
            }
        }
    };
}

impl_point!(Add, add, Point<P>, &'a Point<P>, &'b Point<P>);
impl_point!(Sub, sub, Point<P>, &'a Point<P>, &'b Point<P>);
impl_point!(Add, add, Point<P>, Point<P>, &'b Point<P>);
impl_point!(Sub, sub, Point<P>, Point<P>, &'b Point<P>);
impl_point!(Add, add, Point<P>, &'a Point<P>, Point<P>);
impl_point!(Sub, sub, Point<P>, &'a Point<P>, Point<P>);
impl_point!(Add, add, Point<P>, Point<P>, Point<P>);
impl_point!(Sub, sub, Point<P>, Point<P>, Point<P>);

impl_scalar_point!(Mul, mul, Point<P>, &'a Point<P>, &'b Scalar<S>);
impl_scalar_point!(Mul, mul, Point<P>, Point<P>, &'b Scalar<S>);
impl_scalar_point!(Mul, mul, Point<P>, &'a Point<P>, Scalar<S>);
impl_scalar_point!(Mul, mul, Point<P>, Point<P>, Scalar<S>);

macro_rules! impl_point_assign {
    ($op:ident, $opf:ident, $opf_a:ident, $lt:ty, $rt:ty) => {
        impl<'a, 'b, P: DisLogPoint> $op<$rt> for $lt {
            fn $opf_a(&mut self, rhs: $rt) {
                self.0 = self.0.$opf(&rhs.0)
            }
        }
    };
}

macro_rules! impl_point_scalar_assign {
    ($op:ident, $opf:ident, $opf_a:ident, $lt:ty, $rt:ty) => {
        impl<'a, 'b, P: DisLogPoint<Scalar = S>, S: ScalarNumber> $op<$rt> for $lt {
            fn $opf_a(&mut self, rhs: $rt) {
                self.0 = self.0.$opf(&rhs.0)
            }
        }
    };
}

impl_point_assign!(AddAssign, add, add_assign, Point<P>, &'b Point<P>);
impl_point_assign!(SubAssign, sub, sub_assign, Point<P>, &'b Point<P>);
impl_point_scalar_assign!(MulAssign, mul, mul_assign, Point<P>, &'b Scalar<S>);

impl<P: DisLogPoint> Neg for Point<P> {
    type Output = Point<P>;

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