spec: Impl more traits for NonZero types

Also fixes their Default impls to use "1" as the default.
This commit is contained in:
Jack Grigg 2021-06-10 19:16:08 +01:00
parent 57f84c3eea
commit 37326df1ab
1 changed files with 35 additions and 2 deletions

View File

@ -19,9 +19,15 @@ mod prf_expand;
pub(crate) use prf_expand::PrfExpand; pub(crate) use prf_expand::PrfExpand;
/// A Pallas point that is guaranteed to not be the identity. /// A Pallas point that is guaranteed to not be the identity.
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct NonIdentityPallasPoint(pallas::Point); pub(crate) struct NonIdentityPallasPoint(pallas::Point);
impl Default for NonIdentityPallasPoint {
fn default() -> Self {
NonIdentityPallasPoint(pallas::Point::generator())
}
}
impl ConditionallySelectable for NonIdentityPallasPoint { impl ConditionallySelectable for NonIdentityPallasPoint {
fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self { fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
NonIdentityPallasPoint(pallas::Point::conditional_select(&a.0, &b.0, choice)) NonIdentityPallasPoint(pallas::Point::conditional_select(&a.0, &b.0, choice))
@ -44,9 +50,30 @@ impl Deref for NonIdentityPallasPoint {
} }
/// An integer in [1..q_P]. /// An integer in [1..q_P].
#[derive(Clone, Copy, Debug)]
pub(crate) struct NonZeroPallasBase(pallas::Base); pub(crate) struct NonZeroPallasBase(pallas::Base);
impl Default for NonZeroPallasBase {
fn default() -> Self {
NonZeroPallasBase(pallas::Base::one())
}
}
impl ConditionallySelectable for NonZeroPallasBase {
fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self {
NonZeroPallasBase(pallas::Base::conditional_select(&a.0, &b.0, choice))
}
}
impl NonZeroPallasBase { impl NonZeroPallasBase {
pub(crate) fn from_bytes(bytes: &[u8; 32]) -> CtOption<Self> {
pallas::Base::from_bytes(bytes).and_then(NonZeroPallasBase::from_base)
}
pub(crate) fn from_base(b: pallas::Base) -> CtOption<Self> {
CtOption::new(NonZeroPallasBase(b), !b.ct_is_zero())
}
/// Constructs a wrapper for a base field element that is guaranteed to be non-zero. /// Constructs a wrapper for a base field element that is guaranteed to be non-zero.
/// ///
/// # Panics /// # Panics
@ -59,9 +86,15 @@ impl NonZeroPallasBase {
} }
/// An integer in [1..r_P]. /// An integer in [1..r_P].
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug)]
pub(crate) struct NonZeroPallasScalar(pallas::Scalar); pub(crate) struct NonZeroPallasScalar(pallas::Scalar);
impl Default for NonZeroPallasScalar {
fn default() -> Self {
NonZeroPallasScalar(pallas::Scalar::one())
}
}
impl From<NonZeroPallasBase> for NonZeroPallasScalar { impl From<NonZeroPallasBase> for NonZeroPallasScalar {
fn from(s: NonZeroPallasBase) -> Self { fn from(s: NonZeroPallasBase) -> Self {
NonZeroPallasScalar::guaranteed(mod_r_p(s.0)) NonZeroPallasScalar::guaranteed(mod_r_p(s.0))