generic_ec/
as_raw.rs

1//! Accessing backend library representation of point/scalar
2//!
3//! > You don't normally need to use traits from this module
4//!
5//! `Point<E>` and `Scalar<E>` provide convenient interface for doing EC arithmetic, while wrapping
6//! `E::Point` and `E::Scalar` which actually implement EC arithmetic. You may want to access
7//! wrapped `E::Point` and `E::Scalar` if you lack some functionality that cannot be implemented
8//! on top of `Point<E>` and `Scalar<E>`.
9
10use subtle::CtOption;
11
12/// Accesses backend library representation of the point/scalar
13pub trait AsRaw
14where
15    Self: Sized,
16{
17    /// Wrapped point/scalar
18    type Raw;
19
20    /// Returns reference to wrapped value
21    fn as_raw(&self) -> &Self::Raw;
22}
23
24/// Constructs a point/scalar from its backend library representation
25pub trait TryFromRaw: AsRaw {
26    /// Wraps raw value
27    ///
28    /// Returns `None` if value doesn't meet wrapper constraints
29    fn try_from_raw(raw: Self::Raw) -> Option<Self> {
30        Self::ct_try_from_raw(raw).into()
31    }
32    /// Wraps raw value (constant time)
33    ///
34    /// Returns `None` if value doesn't meet wrapper constraints
35    fn ct_try_from_raw(raw: Self::Raw) -> CtOption<Self>;
36}
37
38/// Constructs a point/scalar from its backend library representation when conversion can be done infallibly
39pub trait FromRaw: AsRaw {
40    /// Infallibly wraps raw value
41    fn from_raw(raw: Self::Raw) -> Self;
42}