generic_ec/generator.rs
1use phantom_type::PhantomType;
2
3use crate::{core::*, NonZero, Point};
4
5/// Generator of curve `E`
6///
7/// Curve generator is a point on curve defined in curve specs. For some curves,
8/// generator multiplication may be optimized, so `Scalar<E> * Generator<E>`
9/// would more efficient than `Scalar<E> * Point<E>`. That's the only purpose
10/// of `Generator<E>` structure: to distinguish generator multiplication and
11/// potentially use more efficient algorithm.
12///
13/// Curve generator `Generator<E>` should be obtained by calling [`Point::generator()`].
14/// You may convert `Generator<E>` to `Point<E>` or `NonZero<Point<E>>` by calling
15/// [`.to_point()`] or [`.to_nonzero_point()`], but then multiplication may be less
16/// efficient.
17///
18/// [`Point::generator()`]: Point::generator
19/// [`.to_point()`]: Generator::to_point
20/// [`.to_nonzero_point()`]: Generator::to_nonzero_point
21#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct Generator<E: Curve>(PhantomType<E>);
23
24impl<E: Curve> Generator<E> {
25 /// Returns a point corresponding to curve generator
26 pub fn to_point(&self) -> Point<E> {
27 (*self).into()
28 }
29
30 /// Returns a non-zero point corresponding to curve generator
31 pub fn to_nonzero_point(&self) -> NonZero<Point<E>> {
32 (*self).into()
33 }
34}
35
36impl<E: Curve> From<Generator<E>> for Point<E> {
37 #[inline]
38 fn from(_: Generator<E>) -> Self {
39 // Correctness: generator has to be a point on curve free of torsion component
40 Point::from_raw_unchecked(E::Point::from(CurveGenerator))
41 }
42}
43
44impl<E: Curve> From<Generator<E>> for NonZero<Point<E>> {
45 #[inline]
46 fn from(g: Generator<E>) -> Self {
47 // Correctness: generator has to be non-zero point
48 NonZero::new_unchecked(g.into())
49 }
50}
51
52impl<E: Curve> Default for Generator<E> {
53 fn default() -> Self {
54 Self(PhantomType::new())
55 }
56}