elliptic_curve/scalar.rs
1//! Scalar types.
2
3#[cfg(feature = "arithmetic")]
4mod blinded;
5#[cfg(feature = "arithmetic")]
6mod nonzero;
7mod primitive;
8
9pub use self::primitive::ScalarPrimitive;
10#[cfg(feature = "arithmetic")]
11pub use self::{blinded::BlindedScalar, nonzero::NonZeroScalar};
12
13use crypto_bigint::Integer;
14use subtle::Choice;
15
16#[cfg(feature = "arithmetic")]
17use crate::CurveArithmetic;
18
19/// Scalar field element for a particular elliptic curve.
20#[cfg(feature = "arithmetic")]
21pub type Scalar<C> = <C as CurveArithmetic>::Scalar;
22
23/// Bit representation of a scalar field element of a given curve.
24#[cfg(feature = "bits")]
25pub type ScalarBits<C> = ff::FieldBits<<Scalar<C> as ff::PrimeFieldBits>::ReprBits>;
26
27/// Instantiate a scalar from an unsigned integer without checking for overflow.
28pub trait FromUintUnchecked {
29 /// Unsigned integer type (i.e. `Curve::Uint`)
30 type Uint: Integer;
31
32 /// Instantiate scalar from an unsigned integer without checking
33 /// whether the value overflows the field modulus.
34 ///
35 /// ⚠️ WARNING!
36 ///
37 /// Incorrectly used this can lead to mathematically invalid results,
38 /// which can lead to potential security vulnerabilities.
39 ///
40 /// Use with care!
41 fn from_uint_unchecked(uint: Self::Uint) -> Self;
42}
43
44/// Is this scalar greater than n / 2?
45///
46/// # Returns
47///
48/// - For scalars 0 through n / 2: `Choice::from(0)`
49/// - For scalars (n / 2) + 1 through n - 1: `Choice::from(1)`
50pub trait IsHigh {
51 /// Is this scalar greater than or equal to n / 2?
52 fn is_high(&self) -> Choice;
53}