pub struct Scalar(/* private fields */);Available on crate feature
arithmetic only.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
p521::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.
Implementations§
Source§impl Scalar
impl Scalar
Sourcepub fn from_bytes(repr: &FieldBytes) -> CtOption<Self>
pub fn from_bytes(repr: &FieldBytes) -> CtOption<Self>
Create a Scalar from a canonical big-endian representation.
Sourcepub fn from_slice(slice: &[u8]) -> Result<Self>
pub fn from_slice(slice: &[u8]) -> Result<Self>
Decode Scalar from a big endian byte slice.
Sourcepub fn to_bytes(self) -> FieldBytes
pub fn to_bytes(self) -> FieldBytes
Returns the big-endian encoding of this Scalar.
Sourcepub const fn to_canonical(self) -> U576
Available on 32-bit only.
pub const fn to_canonical(self) -> U576
Sourcepub const fn pow_vartime(&self, exp: &[u64]) -> Self
pub const fn pow_vartime(&self, exp: &[u64]) -> Self
Returns self^exp, where exp is a little-endian integer exponent.
This operation is variable time with respect to the exponent.
If the exponent is fixed, this operation is effectively constant time.
Sourcepub const fn shr_vartime(&self, shift: usize) -> Scalar
Available on 32-bit only.
pub const fn shr_vartime(&self, shift: usize) -> Scalar
Right shifts the scalar.
Note: not constant-time with respect to the shift parameter.
Trait Implementations§
Source§impl AddAssign<&Scalar> for Scalar
impl AddAssign<&Scalar> for Scalar
Source§fn add_assign(&mut self, other: &Scalar)
fn add_assign(&mut self, other: &Scalar)
Performs the
+= operation. Read moreSource§impl AddAssign for Scalar
impl AddAssign for Scalar
Source§fn add_assign(&mut self, other: Scalar)
fn add_assign(&mut self, other: Scalar)
Performs the
+= operation. Read moreSource§impl ConditionallySelectable for Scalar
impl ConditionallySelectable for Scalar
Source§fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
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)
Conditionally swap
self and other if choice == 1; otherwise,
reassign both unto themselves. Read moreSource§impl ConstantTimeEq for Scalar
impl ConstantTimeEq for Scalar
Source§impl<'de> Deserialize<'de> for Scalar
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Scalar
Available on crate feature
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Field for Scalar
impl Field for Scalar
Source§fn random(rng: impl RngCore) -> Self
fn random(rng: impl RngCore) -> Self
Returns an element chosen uniformly at random using a user-provided RNG.
Source§fn invert(&self) -> CtOption<Self>
fn invert(&self) -> CtOption<Self>
Computes the multiplicative inverse of this element,
failing if the element is zero.
Source§fn sqrt(&self) -> CtOption<Self>
fn sqrt(&self) -> CtOption<Self>
Returns the square root of the field element, if it is
quadratic residue. Read more
Source§fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
Computes: Read more
Source§fn is_zero_vartime(&self) -> bool
fn is_zero_vartime(&self) -> bool
Returns true iff this element is zero. Read more
Source§impl From<&Scalar> for FieldBytes
impl From<&Scalar> for FieldBytes
Source§impl From<Scalar> for FieldBytes
impl From<Scalar> for FieldBytes
Source§impl From<ScalarPrimitive<NistP521>> for Scalar
impl From<ScalarPrimitive<NistP521>> for Scalar
Source§fn from(w: ScalarPrimitive<NistP521>) -> Self
fn from(w: ScalarPrimitive<NistP521>) -> Self
Converts to this type from the input type.
Source§impl FromUintUnchecked for Scalar
impl FromUintUnchecked for Scalar
Source§impl MulAssign<&Scalar> for Scalar
impl MulAssign<&Scalar> for Scalar
Source§fn mul_assign(&mut self, other: &Scalar)
fn mul_assign(&mut self, other: &Scalar)
Performs the
*= operation. Read moreSource§impl MulAssign for Scalar
impl MulAssign for Scalar
Source§fn mul_assign(&mut self, other: Scalar)
fn mul_assign(&mut self, other: Scalar)
Performs the
*= operation. Read moreSource§impl Ord for Scalar
impl Ord for Scalar
Source§impl PartialOrd for Scalar
impl PartialOrd for Scalar
Source§impl PrimeField for Scalar
impl PrimeField for Scalar
Source§const MODULUS: &'static str = "01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409"
const MODULUS: &'static str = "01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409"
Modulus of the field written as a string for debugging purposes. Read more
Source§const CAPACITY: u32 = 520u32
const CAPACITY: u32 = 520u32
How many bits of information can be reliably stored in the field element. Read more
Source§const MULTIPLICATIVE_GENERATOR: Self
const MULTIPLICATIVE_GENERATOR: Self
A fixed multiplicative generator of
modulus - 1 order. This element must also be
a quadratic nonresidue. Read moreSource§const ROOT_OF_UNITY: Self
const ROOT_OF_UNITY: Self
The
2^s root of unity. Read moreSource§const ROOT_OF_UNITY_INV: Self
const ROOT_OF_UNITY_INV: Self
Inverse of
Self::ROOT_OF_UNITY.Source§type Repr = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
type Repr = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
The prime field can be converted back and forth into this binary
representation.
Source§fn from_repr(bytes: FieldBytes) -> CtOption<Self>
fn from_repr(bytes: FieldBytes) -> CtOption<Self>
Attempts to convert a byte representation of a field element into an element of
this prime field, failing if the input is not canonical (is not smaller than the
field’s modulus). Read more
Source§fn to_repr(&self) -> FieldBytes
fn to_repr(&self) -> FieldBytes
Converts an element of the prime field into the standard byte representation for
this field. Read more
Source§fn from_str_vartime(s: &str) -> Option<Self>
fn from_str_vartime(s: &str) -> Option<Self>
Interpret a string of numbers as a (congruent) prime field element.
Does not accept unnecessary leading zeroes or a blank string. Read more
Source§impl Reduce<Uint<crypto_bigint::::uint::U576::{constant#0}>> for Scalar
impl Reduce<Uint<crypto_bigint::::uint::U576::{constant#0}>> for Scalar
Source§type Bytes = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
type Bytes = GenericArray<u8, <NistP521 as Curve>::FieldBytesSize>
Bytes used as input to
Reduce::reduce_bytes.Source§fn reduce_bytes(bytes: &FieldBytes) -> Self
fn reduce_bytes(bytes: &FieldBytes) -> Self
Interpret the given bytes as an integer and perform a modular reduction.
Source§impl ShrAssign<usize> for Scalar
impl ShrAssign<usize> for Scalar
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
Performs the
>>= operation. Read moreSource§impl SignPrimitive<NistP521> for Scalar
Available on crate features ecdsa-core and ecdsa only.
impl SignPrimitive<NistP521> for Scalar
Available on crate features
ecdsa-core and ecdsa only.Source§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>
Try to sign the prehashed message. Read more
Source§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,
Source§impl SubAssign<&Scalar> for Scalar
impl SubAssign<&Scalar> for Scalar
Source§fn sub_assign(&mut self, other: &Scalar)
fn sub_assign(&mut self, other: &Scalar)
Performs the
-= operation. Read moreSource§impl SubAssign for Scalar
impl SubAssign for Scalar
Source§fn sub_assign(&mut self, other: Scalar)
fn sub_assign(&mut self, other: Scalar)
Performs the
-= 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§
Source§impl<T> BatchInvert<[T]> for T
impl<T> BatchInvert<[T]> for T
Source§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
Mutably borrows from an owned value. Read more