p192 0.14.0-rc.13

Pure Rust implementation of the NIST P-192 (a.k.a. secp192r1) elliptic curve as defined in SP 800-186
Documentation
//! secp192r1 scalar field elements.
//!
//! Arithmetic implementations have been synthesized using fiat-crypto.
//!
//! # License
//!
//! Copyright (c) 2015-2020 the fiat-crypto authors
//!
//! fiat-crypto is distributed under the terms of the MIT License, the
//! Apache License (Version 2.0), and the BSD 1-Clause License;
//! users may pick which license to apply.

use crate::{NistP192, ORDER_HEX, U192};
use elliptic_curve::{
    Curve as _,
    bigint::cpubits,
    ff::PrimeField,
    scalar::{FromUintUnchecked, IsHigh},
    subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, CtOption},
};

// TODO(tarcieri): remove this when we can use `const _` to silence warnings
cpubits! {
    32 => {
        #[cfg(not(p192_backend = "bigint"))]
        #[path = "scalar/p192_scalar_32.rs"]
        #[allow(
            dead_code,
            clippy::identity_op,
            clippy::needless_lifetimes,
            clippy::unnecessary_cast,
            clippy::too_many_arguments
        )]
        mod scalar_impl;
    }
    64 => {
        #[cfg(not(p192_backend = "bigint"))]
        #[path = "scalar/p192_scalar_64.rs"]
        #[allow(
            dead_code,
            clippy::identity_op,
            clippy::needless_lifetimes,
            clippy::unnecessary_cast,
            clippy::too_many_arguments
        )]
        mod scalar_impl;
    }
}

#[cfg(not(p192_backend = "bigint"))]
use self::scalar_impl::*;

#[cfg(feature = "serde")]
use {
    elliptic_curve::ScalarValue,
    serdect::serde::{Deserialize, Serialize, de, ser},
};

#[cfg(doc)]
use core::ops::{Add, Mul, Neg, Sub};

primefield::monty_field_params! {
    name: ScalarParams,
    modulus: ORDER_HEX,
    uint: U192,
    byte_order: primefield::ByteOrder::BigEndian,
    multiplicative_generator: 3,
    doc: "Montgomery parameters for the NIST P-192 scalar modulus `n`."
}

primefield::monty_field_element! {
    name: Scalar,
    params: ScalarParams,
    uint: U192,
    doc: "Element in the NIST P-192 scalar field modulo `n`."
}

#[cfg(p192_backend = "bigint")]
primefield::monty_field_arithmetic! {
    name: Scalar,
    params: ScalarParams,
    uint: U192
}

#[cfg(not(p192_backend = "bigint"))]
primefield::fiat_monty_field_arithmetic! {
    name: Scalar,
    params: ScalarParams,
    uint: U192,
    non_mont: fiat_p192_scalar_non_montgomery_domain_field_element,
    mont: fiat_p192_scalar_montgomery_domain_field_element,
    from_mont: fiat_p192_scalar_from_montgomery,
    to_mont: fiat_p192_scalar_to_montgomery,
    add: fiat_p192_scalar_add,
    sub: fiat_p192_scalar_sub,
    mul: fiat_p192_scalar_mul,
    neg: fiat_p192_scalar_opp,
    square: fiat_p192_scalar_square,
    divstep_precomp: fiat_p192_scalar_divstep_precomp,
    divstep: fiat_p192_scalar_divstep,
    msat: fiat_p192_scalar_msat,
    selectnz: fiat_p192_scalar_selectznz
}

primefield::monty_field_reduce! {
    name: Scalar,
    params: ScalarParams,
    uint: U192,
}

elliptic_curve::scalar_impls!(NistP192, Scalar);

impl AsRef<Scalar> for Scalar {
    fn as_ref(&self) -> &Scalar {
        self
    }
}

impl FromUintUnchecked for Scalar {
    type Uint = U192;

    fn from_uint_unchecked(uint: Self::Uint) -> Self {
        Self::from_uint_unchecked(uint)
    }
}

impl IsHigh for Scalar {
    fn is_high(&self) -> Choice {
        const MODULUS_SHR1: U192 = NistP192::ORDER.as_ref().shr_vartime(1);
        self.to_canonical().ct_gt(&MODULUS_SHR1)
    }
}

#[cfg(feature = "serde")]
impl Serialize for Scalar {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        ScalarValue::from(self).serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Scalar {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        Ok(ScalarValue::deserialize(deserializer)?.into())
    }
}

#[cfg(test)]
mod tests {
    use super::{Scalar, U192};

    #[cfg(not(p192_backend = "bigint"))]
    use super::{
        ScalarParams, fiat_p192_scalar_montgomery_domain_field_element, fiat_p192_scalar_msat,
        fiat_p192_scalar_non_montgomery_domain_field_element, fiat_p192_scalar_to_montgomery,
    };

    primefield::test_primefield!(Scalar, U192);

    #[cfg(not(p192_backend = "bigint"))]
    primefield::test_fiat_monty_field_arithmetic!(
        name: Scalar,
        params: ScalarParams,
        uint: U192,
        non_mont: fiat_p192_scalar_non_montgomery_domain_field_element,
        mont: fiat_p192_scalar_montgomery_domain_field_element,
        to_mont: fiat_p192_scalar_to_montgomery,
        msat: fiat_p192_scalar_msat
    );
}