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
k256::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.
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§
Source§impl Scalar
impl Scalar
Sourcepub fn to_bytes(&self) -> FieldBytes
pub fn to_bytes(&self) -> FieldBytes
Returns the SEC1 encoding of this scalar.
Sourcepub fn shr_vartime(&self, shift: u32) -> Scalar
pub fn shr_vartime(&self, shift: u32) -> Scalar
Right shifts the scalar.
Note: not constant-time with respect to the shift parameter.
Sourcepub fn invert(&self) -> CtOption<Self>
pub fn invert(&self) -> CtOption<Self>
Returns the multiplicative inverse of self, if self is non-zero.
Sourcepub fn invert_vartime(&self) -> CtOption<Self>
pub fn invert_vartime(&self) -> CtOption<Self>
Returns the multiplicative inverse of self in variable-time, if self is non-zero.
Sourcepub fn generate_biased_from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
pub fn generate_biased_from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self
Returns a (nearly) uniformly-random scalar, generated in constant time.
Sourcepub fn try_generate_biased_from_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Self, R::Error>
pub fn try_generate_biased_from_rng<R: TryCryptoRng + ?Sized>( rng: &mut R, ) -> Result<Self, R::Error>
Returns a (nearly) uniformly-random scalar, generated in constant time.
§Errors
Propagates R::Error on RNG failure.
Trait Implementations§
Source§impl AddAssign for Scalar
impl AddAssign for Scalar
Source§fn add_assign(&mut self, rhs: Scalar)
fn add_assign(&mut self, rhs: Scalar)
+= operation. Read moreSource§impl AddAssign<&Scalar> for Scalar
impl AddAssign<&Scalar> for Scalar
Source§fn add_assign(&mut self, rhs: &Scalar)
fn add_assign(&mut self, rhs: &Scalar)
+= 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)
self and other if choice == 1; otherwise,
reassign both unto themselves. Read moreSource§impl ConstantTimeEq for Scalar
impl ConstantTimeEq for Scalar
impl Copy for Scalar
impl DefaultIsZeroes for Scalar
impl Eq for Scalar
Source§impl Field for Scalar
impl Field for Scalar
Source§fn sqrt(&self) -> CtOption<Self>
fn sqrt(&self) -> CtOption<Self>
Tonelli-Shank’s algorithm for q mod 16 = 1 https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5)
Source§fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error>
fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error>
Source§fn invert(&self) -> CtOption<Self>
fn invert(&self) -> CtOption<Self>
Source§fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self)
Source§fn random<R>(rng: &mut R) -> Self
fn random<R>(rng: &mut R) -> Self
Source§fn is_zero_vartime(&self) -> bool
fn is_zero_vartime(&self) -> bool
impl FieldExt for Scalar
Source§impl From<&NonZeroScalar<Secp256k1>> for Scalar
impl From<&NonZeroScalar<Secp256k1>> for Scalar
Source§fn from(scalar: &NonZeroScalar) -> Self
fn from(scalar: &NonZeroScalar) -> Self
Source§impl From<&Scalar> for FieldBytes
impl From<&Scalar> for FieldBytes
Source§impl From<NonZeroScalar<Secp256k1>> for Scalar
impl From<NonZeroScalar<Secp256k1>> for Scalar
Source§fn from(scalar: NonZeroScalar) -> Self
fn from(scalar: NonZeroScalar) -> Self
Source§impl From<Scalar> for FieldBytes
impl From<Scalar> for FieldBytes
Source§impl FromUintUnchecked for Scalar
impl FromUintUnchecked for Scalar
Source§impl FromUniformBytes<64> for Scalar
impl FromUniformBytes<64> for Scalar
Source§fn from_uniform_bytes(bytes: &[u8; 64]) -> Self
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self
Source§impl Generate for Scalar
impl Generate for Scalar
Source§fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Self, R::Error>
fn try_generate_from_rng<R: TryCryptoRng + ?Sized>( rng: &mut R, ) -> Result<Self, R::Error>
TryCryptoRng. Read moreSource§fn generate_from_rng<R>(rng: &mut R) -> Self
fn generate_from_rng<R>(rng: &mut R) -> Self
CryptoRng.Source§impl Mul<&<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
impl Mul<&<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<&<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
impl Mul<&<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
impl Mul<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: &ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: &ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
impl Mul<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: &ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: &ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<&Scalar> for AffinePoint
impl Mul<&Scalar> for AffinePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
* operator.Source§impl Mul<&Scalar> for &ProjectivePoint
impl Mul<&Scalar> for &ProjectivePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
* operator.Source§impl Mul<&Scalar> for ProjectivePoint
impl Mul<&Scalar> for ProjectivePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
* operator.Source§impl Mul<<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
impl Mul<<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
impl Mul<<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
impl Mul<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
impl Mul<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
Source§type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
type Output = <Secp256k1 as CurveArithmetic>::ProjectivePoint
* operator.Source§fn mul(self, rhs: ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul(self, rhs: ProjectivePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
* operation. Read moreSource§impl Mul<Scalar> for AffinePoint
impl Mul<Scalar> for AffinePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
* operator.Source§impl Mul<Scalar> for ProjectivePoint
impl Mul<Scalar> for ProjectivePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
* operator.Source§impl MulAssign for Scalar
impl MulAssign for Scalar
Source§fn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*= operation. Read moreSource§impl MulAssign<&Scalar> for ProjectivePoint
impl MulAssign<&Scalar> for ProjectivePoint
Source§fn mul_assign(&mut self, rhs: &Scalar)
fn mul_assign(&mut self, rhs: &Scalar)
*= operation. Read moreSource§impl MulAssign<&Scalar> for Scalar
impl MulAssign<&Scalar> for Scalar
Source§fn mul_assign(&mut self, rhs: &Scalar)
fn mul_assign(&mut self, rhs: &Scalar)
*= operation. Read moreSource§impl MulAssign<Scalar> for ProjectivePoint
impl MulAssign<Scalar> for ProjectivePoint
Source§fn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*= operation. Read moreSource§impl MulVartime<&<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
impl MulVartime<&<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
Source§fn mul_vartime(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul_vartime(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<&<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
impl MulVartime<&<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
Source§fn mul_vartime(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul_vartime(self, rhs: &AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
impl MulVartime<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
Source§fn mul_vartime(
self,
rhs: &ProjectivePoint<Secp256k1>,
) -> ProjectivePoint<Secp256k1>
fn mul_vartime( self, rhs: &ProjectivePoint<Secp256k1>, ) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
impl MulVartime<&<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
Source§fn mul_vartime(
self,
rhs: &ProjectivePoint<Secp256k1>,
) -> ProjectivePoint<Secp256k1>
fn mul_vartime( self, rhs: &ProjectivePoint<Secp256k1>, ) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<&Scalar> for AffinePoint
impl MulVartime<&Scalar> for AffinePoint
Source§fn mul_vartime(self, scalar: &Scalar) -> ProjectivePoint
fn mul_vartime(self, scalar: &Scalar) -> ProjectivePoint
self by rhs in variable-time.Source§impl MulVartime<&Scalar> for &ProjectivePoint
impl MulVartime<&Scalar> for &ProjectivePoint
Source§fn mul_vartime(self, other: &Scalar) -> ProjectivePoint
fn mul_vartime(self, other: &Scalar) -> ProjectivePoint
self by rhs in variable-time.Source§impl MulVartime<&Scalar> for ProjectivePoint
impl MulVartime<&Scalar> for ProjectivePoint
Source§fn mul_vartime(self, other: &Scalar) -> ProjectivePoint
fn mul_vartime(self, other: &Scalar) -> ProjectivePoint
self by rhs in variable-time.Source§impl MulVartime<<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
impl MulVartime<<Secp256k1 as CurveArithmetic>::AffinePoint> for Scalar
Source§fn mul_vartime(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul_vartime(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
impl MulVartime<<Secp256k1 as CurveArithmetic>::AffinePoint> for &Scalar
Source§fn mul_vartime(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
fn mul_vartime(self, rhs: AffinePoint<Secp256k1>) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
impl MulVartime<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for Scalar
Source§fn mul_vartime(
self,
rhs: ProjectivePoint<Secp256k1>,
) -> ProjectivePoint<Secp256k1>
fn mul_vartime( self, rhs: ProjectivePoint<Secp256k1>, ) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
impl MulVartime<<Secp256k1 as CurveArithmetic>::ProjectivePoint> for &Scalar
Source§fn mul_vartime(
self,
rhs: ProjectivePoint<Secp256k1>,
) -> ProjectivePoint<Secp256k1>
fn mul_vartime( self, rhs: ProjectivePoint<Secp256k1>, ) -> ProjectivePoint<Secp256k1>
self by rhs in variable-time.Source§impl MulVartime<Scalar> for AffinePoint
impl MulVartime<Scalar> for AffinePoint
Source§fn mul_vartime(self, scalar: Scalar) -> ProjectivePoint
fn mul_vartime(self, scalar: Scalar) -> ProjectivePoint
self by rhs in variable-time.Source§impl MulVartime<Scalar> for ProjectivePoint
impl MulVartime<Scalar> for ProjectivePoint
Source§fn mul_vartime(self, other: Scalar) -> ProjectivePoint
fn mul_vartime(self, other: Scalar) -> ProjectivePoint
self by rhs in variable-time.Source§impl Ord for Scalar
impl Ord for Scalar
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Scalar
impl PartialOrd for Scalar
Source§impl PrimeField for Scalar
impl PrimeField for Scalar
Source§fn from_repr(bytes: FieldBytes) -> CtOption<Self>
fn from_repr(bytes: FieldBytes) -> CtOption<Self>
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).
Source§const MODULUS: &'static str = ORDER_HEX
const MODULUS: &'static str = ORDER_HEX
Source§const CAPACITY: u32 = 255
const CAPACITY: u32 = 255
Source§const MULTIPLICATIVE_GENERATOR: Self
const MULTIPLICATIVE_GENERATOR: Self
modulus - 1 order. This element must also be
a quadratic nonresidue. Read moreSource§const ROOT_OF_UNITY: Self
const ROOT_OF_UNITY: Self
2^s root of unity. Read moreSource§const ROOT_OF_UNITY_INV: Self
const ROOT_OF_UNITY_INV: Self
Self::ROOT_OF_UNITY.Source§type Repr = Array<u8, <Secp256k1 as Curve>::FieldBytesSize>
type Repr = Array<u8, <Secp256k1 as Curve>::FieldBytesSize>
Source§fn to_repr(&self) -> FieldBytes
fn to_repr(&self) -> FieldBytes
Source§fn from_str_vartime(s: &str) -> Option<Self>
fn from_str_vartime(s: &str) -> Option<Self>
Source§impl PrimeFieldExt for Scalar
impl PrimeFieldExt for Scalar
Source§const REPR_ENDIANNESS: ByteOrder = ByteOrder::BigEndian
const REPR_ENDIANNESS: ByteOrder = ByteOrder::BigEndian
ff::PrimeField::Repr.Source§fn to_be_repr(&self) -> Self::Repr
fn to_be_repr(&self) -> Self::Repr
self using a big endian representation.Source§fn to_le_repr(&self) -> Self::Repr
fn to_le_repr(&self) -> Self::Repr
self using a little endian representation.Source§impl Reduce<Array<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
impl Reduce<Array<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
Source§fn reduce(bytes: &FieldBytes) -> Self
fn reduce(bytes: &FieldBytes) -> Self
self modulo Modulus.Source§impl Reduce<Array<u8, UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Scalar
impl Reduce<Array<u8, UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Scalar
Source§impl ReduceNonZero<Array<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
impl ReduceNonZero<Array<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
Source§fn reduce_nonzero(bytes: &FieldBytes) -> Self
fn reduce_nonzero(bytes: &FieldBytes) -> Self
Source§impl ReduceNonZero<Array<u8, UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Scalar
impl ReduceNonZero<Array<u8, UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>>> for Scalar
Source§fn reduce_nonzero(bytes: &WideBytes) -> Self
fn reduce_nonzero(bytes: &WideBytes) -> Self
Source§impl ReduceNonZero<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
impl ReduceNonZero<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
Source§fn reduce_nonzero(w: &U256) -> Self
fn reduce_nonzero(w: &U256) -> Self
Source§impl ReduceNonZero<Uint<crypto_bigint::::uint::U512::{constant#0}>> for Scalar
impl ReduceNonZero<Uint<crypto_bigint::::uint::U512::{constant#0}>> for Scalar
Source§fn reduce_nonzero(w: &U512) -> Self
fn reduce_nonzero(w: &U512) -> Self
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)
>>= operation. Read moreSource§impl SubAssign for Scalar
impl SubAssign for Scalar
Source§fn sub_assign(&mut self, rhs: Scalar)
fn sub_assign(&mut self, rhs: Scalar)
-= operation. Read moreSource§impl SubAssign<&Scalar> for Scalar
impl SubAssign<&Scalar> for Scalar
Source§fn sub_assign(&mut self, rhs: &Scalar)
fn sub_assign(&mut self, rhs: &Scalar)
-= operation. Read moreSource§impl TryFrom<Scalar> for NonZeroScalar
The constant-time alternative is available at NonZeroScalar::new().
impl TryFrom<Scalar> for NonZeroScalar
The constant-time alternative is available at NonZeroScalar::new().