Struct Scalar
pub struct Scalar(/* private fields */);Expand description
Scalars are elements in the finite field modulo n.
§Trait impls
Much of the important functionality of scalars is provided by traits from
the ff crate, which is re-exported as
p256::elliptic_curve::ff:
Field- represents elements of finite fields and provides:Field::random- generate a random scalardouble,square, andinvertoperations- Bounds for
Add,Sub,Mul, andNeg(as well as*Assignequivalents) - Bounds for
ConditionallySelectablefrom thesubtlecrate
PrimeField- represents elements of prime fields and provides:from_repr/to_reprfor converting field elements from/to big integers.multiplicative_generatorandroot_of_unityconstants.
PrimeFieldBits- operations over field elements represented as bits (requiresbitsfeature)
Please see the documentation for the relevant traits for more information.
§serde support
When the serde feature of this crate is enabled, the Serialize and
Deserialize traits are impl’d for this type.
The serialization is a fixed-width big endian encoding. When used with textual formats, the binary data is encoded as hexadecimal.
Implementations§
§impl Scalar
impl Scalar
pub fn to_bytes(&self) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
pub fn to_bytes(&self) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
Returns the SEC1 encoding of this scalar.
pub const fn shr_vartime(&self, shift: usize) -> Scalar
pub const fn shr_vartime(&self, shift: usize) -> Scalar
Right shifts the scalar.
Note: not constant-time with respect to the shift parameter.
pub fn invert(&self) -> CtOption<Scalar>
pub fn invert(&self) -> CtOption<Scalar>
Returns the multiplicative inverse of self, if self is non-zero
pub const fn pow_vartime(&self, exp: &[u64]) -> Scalar
pub const fn pow_vartime(&self, exp: &[u64]) -> Scalar
Exponentiates self by exp, where exp is a little-endian order integer
exponent.
Trait Implementations§
§impl AddAssign<&Scalar> for Scalar
impl AddAssign<&Scalar> for Scalar
§fn add_assign(&mut self, rhs: &Scalar)
fn add_assign(&mut self, rhs: &Scalar)
+= operation. Read more§impl AddAssign for Scalar
impl AddAssign for Scalar
§fn add_assign(&mut self, rhs: Scalar)
fn add_assign(&mut self, rhs: Scalar)
+= operation. Read more§impl ConditionallySelectable for Scalar
impl ConditionallySelectable for Scalar
Source§fn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Source§fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
self and other if choice == 1; otherwise,
reassign both unto themselves. Read more§impl ConstantTimeEq for Scalar
impl ConstantTimeEq for Scalar
§impl Field for Scalar
impl Field for Scalar
§fn sqrt(&self) -> CtOption<Scalar>
fn sqrt(&self) -> CtOption<Scalar>
Tonelli-Shank’s algorithm for q mod 16 = 1 https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5)
§fn random(rng: impl RngCore) -> Scalar
fn random(rng: impl RngCore) -> Scalar
§fn invert(&self) -> CtOption<Scalar>
fn invert(&self) -> CtOption<Scalar>
§fn is_zero_vartime(&self) -> bool
fn is_zero_vartime(&self) -> bool
§fn pow_vartime<S>(&self, exp: S) -> Self
fn pow_vartime<S>(&self, exp: S) -> Self
§impl From<&Scalar> for GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
impl From<&Scalar> for GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§fn from(
scalar: &Scalar,
) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
fn from( scalar: &Scalar, ) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§impl From<&Scalar> for ScalarPrimitive<NistP256>
impl From<&Scalar> for ScalarPrimitive<NistP256>
§fn from(scalar: &Scalar) -> ScalarPrimitive<NistP256>
fn from(scalar: &Scalar) -> ScalarPrimitive<NistP256>
§impl From<&ScalarPrimitive<NistP256>> for Scalar
impl From<&ScalarPrimitive<NistP256>> for Scalar
§fn from(scalar: &ScalarPrimitive<NistP256>) -> Scalar
fn from(scalar: &ScalarPrimitive<NistP256>) -> Scalar
§impl From<Scalar> for GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
impl From<Scalar> for GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§fn from(scalar: Scalar) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
fn from(scalar: Scalar) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§impl From<Scalar> for ScalarPrimitive<NistP256>
impl From<Scalar> for ScalarPrimitive<NistP256>
§fn from(scalar: Scalar) -> ScalarPrimitive<NistP256>
fn from(scalar: Scalar) -> ScalarPrimitive<NistP256>
§impl From<ScalarPrimitive<NistP256>> for Scalar
impl From<ScalarPrimitive<NistP256>> for Scalar
§fn from(scalar: ScalarPrimitive<NistP256>) -> Scalar
fn from(scalar: ScalarPrimitive<NistP256>) -> Scalar
§impl FromUintUnchecked for Scalar
impl FromUintUnchecked for Scalar
§type Uint = Uint<crypto_bigint::::uint::U256::{constant#0}>
type Uint = Uint<crypto_bigint::::uint::U256::{constant#0}>
Curve::Uint)§fn from_uint_unchecked(uint: <Scalar as FromUintUnchecked>::Uint) -> Scalar
fn from_uint_unchecked(uint: <Scalar as FromUintUnchecked>::Uint) -> Scalar
§impl Invert for Scalar
impl Invert for Scalar
§fn invert_vartime(&self) -> CtOption<Scalar>
fn invert_vartime(&self) -> CtOption<Scalar>
Fast variable-time inversion using Stein’s algorithm.
Returns none if the scalar is zero.
https://link.springer.com/article/10.1007/s13389-016-0135-4
⚠️ WARNING!
This method should not be used with (unblinded) secret scalars, as its variable-time operation can potentially leak secrets through sidechannels.
§impl MulAssign<&Scalar> for Scalar
impl MulAssign<&Scalar> for Scalar
§fn mul_assign(&mut self, rhs: &Scalar)
fn mul_assign(&mut self, rhs: &Scalar)
*= operation. Read more§impl MulAssign for Scalar
impl MulAssign for Scalar
§fn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*= operation. Read more§impl Ord for Scalar
impl Ord for Scalar
§impl PartialOrd for Scalar
impl PartialOrd for Scalar
§impl PrimeField for Scalar
impl PrimeField for Scalar
§fn from_repr(
bytes: GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>,
) -> CtOption<Scalar>
fn from_repr( bytes: GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>, ) -> CtOption<Scalar>
Attempts to parse the given byte array as an SEC1-encoded scalar.
Returns None if the byte array does not contain a big-endian integer in the range [0, p).
§const MODULUS: &'static str = ORDER_HEX
const MODULUS: &'static str = ORDER_HEX
§const CAPACITY: u32 = 255
const CAPACITY: u32 = 255
§const MULTIPLICATIVE_GENERATOR: Scalar
const MULTIPLICATIVE_GENERATOR: Scalar
modulus - 1 order. This element must also be
a quadratic nonresidue. Read more§const ROOT_OF_UNITY: Scalar
const ROOT_OF_UNITY: Scalar
2^s root of unity. Read more§const ROOT_OF_UNITY_INV: Scalar
const ROOT_OF_UNITY_INV: Scalar
Self::ROOT_OF_UNITY.§type Repr = GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
type Repr = GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§fn to_repr(&self) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
fn to_repr(&self) -> GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
§fn from_str_vartime(s: &str) -> Option<Self>
fn from_str_vartime(s: &str) -> Option<Self>
§fn from_repr_vartime(repr: Self::Repr) -> Option<Self>
fn from_repr_vartime(repr: Self::Repr) -> Option<Self>
§impl Reduce<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
impl Reduce<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
§type Bytes = GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
type Bytes = GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>
Reduce::reduce_bytes.§fn reduce(w: Uint<crypto_bigint::::uint::U256::{constant#0}>) -> Scalar
fn reduce(w: Uint<crypto_bigint::::uint::U256::{constant#0}>) -> Scalar
§fn reduce_bytes(
bytes: &GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>,
) -> Scalar
fn reduce_bytes( bytes: &GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>, ) -> Scalar
§impl ReduceNonZero<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
impl ReduceNonZero<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
§fn reduce_nonzero(w: Uint<crypto_bigint::::uint::U256::{constant#0}>) -> Scalar
fn reduce_nonzero(w: Uint<crypto_bigint::::uint::U256::{constant#0}>) -> Scalar
§fn reduce_nonzero_bytes(
bytes: &GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>,
) -> Scalar
fn reduce_nonzero_bytes( bytes: &GenericArray<u8, <NistP256 as Curve>::FieldBytesSize>, ) -> Scalar
§impl ShrAssign<usize> for Scalar
impl ShrAssign<usize> for Scalar
§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>= operation. Read more§impl SignPrimitive<NistP256> for Scalar
Available on crate feature ecdsa only.
impl SignPrimitive<NistP256> for Scalar
ecdsa only.§fn try_sign_prehashed<K>(
&self,
k: K,
z: &GenericArray<u8, <C as Curve>::FieldBytesSize>,
) -> Result<(Signature<C>, Option<RecoveryId>), Error>
fn try_sign_prehashed<K>( &self, k: K, z: &GenericArray<u8, <C as Curve>::FieldBytesSize>, ) -> Result<(Signature<C>, Option<RecoveryId>), Error>
§fn try_sign_prehashed_rfc6979<D>(
&self,
z: &GenericArray<u8, <C as Curve>::FieldBytesSize>,
ad: &[u8],
) -> Result<(Signature<C>, Option<RecoveryId>), Error>where
Self: From<ScalarPrimitive<C>> + Invert<Output = CtOption<Self>>,
D: Digest<OutputSize = <C as Curve>::FieldBytesSize> + BlockSizeUser + FixedOutput + FixedOutputReset,
fn try_sign_prehashed_rfc6979<D>(
&self,
z: &GenericArray<u8, <C as Curve>::FieldBytesSize>,
ad: &[u8],
) -> Result<(Signature<C>, Option<RecoveryId>), Error>where
Self: From<ScalarPrimitive<C>> + Invert<Output = CtOption<Self>>,
D: Digest<OutputSize = <C as Curve>::FieldBytesSize> + BlockSizeUser + FixedOutput + FixedOutputReset,
§impl SubAssign<&Scalar> for Scalar
impl SubAssign<&Scalar> for Scalar
§fn sub_assign(&mut self, rhs: &Scalar)
fn sub_assign(&mut self, rhs: &Scalar)
-= operation. Read more§impl SubAssign for Scalar
impl SubAssign for Scalar
§fn sub_assign(&mut self, rhs: Scalar)
fn sub_assign(&mut self, rhs: Scalar)
-= operation. Read moreimpl Copy for Scalar
impl DefaultIsZeroes for Scalar
impl Eq for Scalar
Auto Trait Implementations§
impl Freeze for Scalar
impl RefUnwindSafe for Scalar
impl Send for Scalar
impl Sync for Scalar
impl Unpin for Scalar
impl UnwindSafe for Scalar
Blanket Implementations§
§impl<const N: usize, T> BatchInvert<[T; N]> for T
impl<const N: usize, T> BatchInvert<[T; N]> for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ConditionallyNegatable for T
impl<T> ConditionallyNegatable for T
Source§fn conditional_negate(&mut self, choice: Choice)
fn conditional_negate(&mut self, choice: Choice)
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more