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