Struct k256::Scalar

source ·
pub struct Scalar(_);
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:
  • PrimeField - represents elements of prime fields and provides:
    • from_repr/to_repr for converting field elements from/to big integers.
    • multiplicative_generator and root_of_unity constants.
  • PrimeFieldBits - operations over field elements represented as bits (requires bits feature)

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

source

pub const ZERO: Self = _

Zero scalar.

source

pub const ONE: Self = _

Multiplicative identity.

source

pub fn is_zero(&self) -> Choice

Checks if the scalar is zero.

source

pub fn to_bytes(&self) -> FieldBytes

Returns the SEC1 encoding of this scalar.

source

pub const fn negate(&self) -> Self

Negates the scalar.

source

pub const fn add(&self, rhs: &Self) -> Self

Returns self + rhs mod n.

source

pub const fn sub(&self, rhs: &Self) -> Self

Returns self - rhs mod n.

source

pub fn mul(&self, rhs: &Scalar) -> Scalar

Modulo multiplies two scalars.

source

pub fn square(&self) -> Self

Modulo squares the scalar.

source

pub fn shr_vartime(&self, shift: usize) -> Scalar

Right shifts the scalar.

Note: not constant-time with respect to the shift parameter.

source

pub fn invert(&self) -> CtOption<Self>

Inverts the scalar.

source

pub fn generate_biased(rng: impl CryptoRng + RngCore) -> Self

Returns a (nearly) uniformly-random scalar, generated in constant time.

source

pub fn generate_vartime(rng: impl RngCore) -> Self

Returns a uniformly-random scalar, generated using rejection sampling.

Trait Implementations§

source§

impl Add<&Scalar> for &Scalar

§

type Output = Scalar

The resulting type after applying the + operator.
source§

fn add(self, other: &Scalar) -> Scalar

Performs the + operation. Read more
source§

impl Add<&Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the + operator.
source§

fn add(self, other: &Scalar) -> Scalar

Performs the + operation. Read more
source§

impl Add<Scalar> for &Scalar

§

type Output = Scalar

The resulting type after applying the + operator.
source§

fn add(self, other: Scalar) -> Scalar

Performs the + operation. Read more
source§

impl Add<Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the + operator.
source§

fn add(self, other: Scalar) -> Scalar

Performs the + operation. Read more
source§

impl AddAssign<&Scalar> for Scalar

source§

fn add_assign(&mut self, rhs: &Scalar)

Performs the += operation. Read more
source§

impl AddAssign<Scalar> for Scalar

source§

fn add_assign(&mut self, rhs: Scalar)

Performs the += operation. Read more
source§

impl Clone for Scalar

source§

fn clone(&self) -> Scalar

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl ConditionallySelectable for Scalar

source§

fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self

Select a or b according to choice. Read more
source§

fn conditional_assign(&mut self, other: &Self, choice: Choice)

Conditionally assign other to self, according to choice. Read more
source§

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 more
source§

impl ConstantTimeEq for Scalar

source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if two items are equal. Read more
source§

impl Debug for Scalar

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Scalar

source§

fn default() -> Scalar

Returns the “default value” for a type. Read more
source§

impl Field for Scalar

source§

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 random(rng: impl RngCore) -> Self

Returns an element chosen uniformly at random using a user-provided RNG.
source§

fn zero() -> Self

Returns the zero element of the field, the additive identity.
source§

fn one() -> Self

Returns the one element of the field, the multiplicative identity.
source§

fn square(&self) -> Self

Squares this element.
source§

fn double(&self) -> Self

Doubles this element.
source§

fn invert(&self) -> CtOption<Self>

Computes the multiplicative inverse of this element, failing if the element is zero.
source§

fn is_zero(&self) -> Choice

Returns true iff this element is zero.
source§

fn is_zero_vartime(&self) -> bool

Returns true iff this element is zero. Read more
source§

fn cube(&self) -> Self

Cubes this element.
source§

fn pow_vartime<S>(&self, exp: S) -> Selfwhere S: AsRef<[u64]>,

Exponentiates self by exp, where exp is a little-endian order integer exponent. Read more
source§

impl From<&Scalar> for FieldBytes

source§

fn from(scalar: &Scalar) -> Self

Converts to this type from the input type.
source§

impl From<&Scalar> for ScalarCore<Secp256k1>

source§

fn from(scalar: &Scalar) -> ScalarCore<Secp256k1>

Converts to this type from the input type.
source§

impl From<&Scalar> for U256

source§

fn from(scalar: &Scalar) -> Self

Converts to this type from the input type.
source§

impl From<&ScalarCore<Secp256k1>> for Scalar

source§

fn from(scalar: &ScalarCore<Secp256k1>) -> Scalar

Converts to this type from the input type.
source§

impl From<Scalar> for FieldBytes

source§

fn from(scalar: Scalar) -> Self

Converts to this type from the input type.
source§

impl From<Scalar> for ScalarCore<Secp256k1>

source§

fn from(scalar: Scalar) -> ScalarCore<Secp256k1>

Converts to this type from the input type.
source§

impl From<Scalar> for U256

source§

fn from(scalar: Scalar) -> Self

Converts to this type from the input type.
source§

impl From<ScalarCore<Secp256k1>> for Scalar

source§

fn from(scalar: ScalarCore<Secp256k1>) -> Scalar

Converts to this type from the input type.
source§

impl From<u32> for Scalar

source§

fn from(k: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Scalar

source§

fn from(k: u64) -> Self

Converts to this type from the input type.
source§

impl IsHigh for Scalar

source§

fn is_high(&self) -> Choice

Is this scalar greater than or equal to n / 2?
source§

impl Mul<&Scalar> for &ProjectivePoint

§

type Output = ProjectivePoint

The resulting type after applying the * operator.
source§

fn mul(self, other: &Scalar) -> ProjectivePoint

Performs the * operation. Read more
source§

impl Mul<&Scalar> for &Scalar

§

type Output = Scalar

The resulting type after applying the * operator.
source§

fn mul(self, other: &Scalar) -> Scalar

Performs the * operation. Read more
source§

impl Mul<&Scalar> for AffinePoint

§

type Output = ProjectivePoint

The resulting type after applying the * operator.
source§

fn mul(self, scalar: &Scalar) -> ProjectivePoint

Performs the * operation. Read more
source§

impl Mul<&Scalar> for ProjectivePoint

§

type Output = ProjectivePoint

The resulting type after applying the * operator.
source§

fn mul(self, other: &Scalar) -> ProjectivePoint

Performs the * operation. Read more
source§

impl Mul<&Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the * operator.
source§

fn mul(self, other: &Scalar) -> Scalar

Performs the * operation. Read more
source§

impl Mul<Scalar> for AffinePoint

§

type Output = ProjectivePoint

The resulting type after applying the * operator.
source§

fn mul(self, scalar: Scalar) -> ProjectivePoint

Performs the * operation. Read more
source§

impl Mul<Scalar> for ProjectivePoint

§

type Output = ProjectivePoint

The resulting type after applying the * operator.
source§

fn mul(self, other: Scalar) -> ProjectivePoint

Performs the * operation. Read more
source§

impl Mul<Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the * operator.
source§

fn mul(self, other: Scalar) -> Scalar

Performs the * operation. Read more
source§

impl MulAssign<&Scalar> for ProjectivePoint

source§

fn mul_assign(&mut self, rhs: &Scalar)

Performs the *= operation. Read more
source§

impl MulAssign<&Scalar> for Scalar

source§

fn mul_assign(&mut self, rhs: &Scalar)

Performs the *= operation. Read more
source§

impl MulAssign<Scalar> for ProjectivePoint

source§

fn mul_assign(&mut self, rhs: Scalar)

Performs the *= operation. Read more
source§

impl MulAssign<Scalar> for Scalar

source§

fn mul_assign(&mut self, rhs: Scalar)

Performs the *= operation. Read more
source§

impl Neg for &Scalar

§

type Output = Scalar

The resulting type after applying the - operator.
source§

fn neg(self) -> Scalar

Performs the unary - operation. Read more
source§

impl Neg for Scalar

§

type Output = Scalar

The resulting type after applying the - operator.
source§

fn neg(self) -> Scalar

Performs the unary - operation. Read more
source§

impl PartialEq<Scalar> for Scalar

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PrimeField for Scalar

source§

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).

§

type Repr = GenericArray<u8, <<Secp256k1 as Curve>::UInt as ArrayEncoding>::ByteSize>

The prime field can be converted back and forth into this binary representation.
source§

const NUM_BITS: u32 = 256u32

How many bits are needed to represent an element of this field.
source§

const CAPACITY: u32 = 255u32

How many bits of information can be reliably stored in the field element. Read more
source§

const S: u32 = 6u32

An integer s satisfying the equation 2^s * t = modulus - 1 with t odd. Read more
source§

fn to_repr(&self) -> FieldBytes

Converts an element of the prime field into the standard byte representation for this field. Read more
source§

fn is_odd(&self) -> Choice

Returns true iff this element is odd.
source§

fn multiplicative_generator() -> Self

Returns a fixed multiplicative generator of modulus - 1 order. This element must also be a quadratic nonresidue. Read more
source§

fn root_of_unity() -> Self

Returns the 2^s root of unity. Read more
source§

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§

fn from_repr_vartime(repr: Self::Repr) -> Option<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 is_even(&self) -> Choice

Returns true iff this element is even.
source§

impl Reduce<UInt<crypto_bigint::::uint::U256::{constant#0}>> for Scalar

source§

fn from_uint_reduced(w: U256) -> Self

Perform a modular reduction, returning a field element.
source§

fn from_be_bytes_reduced( bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize> ) -> Self

Interpret the given byte array as a big endian integer and perform a modular reduction.
source§

fn from_le_bytes_reduced( bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize> ) -> Self

Interpret the given byte array as a little endian integer and perform a modular reduction.
source§

fn from_be_digest_reduced<D>(digest: D) -> Selfwhere D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,

Interpret a digest as a big endian integer and perform a modular reduction.
source§

fn from_le_digest_reduced<D>(digest: D) -> Selfwhere D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,

Interpret a digest as a little endian integer and perform a modular reduction.
source§

impl Reduce<UInt<crypto_bigint::::uint::U512::{constant#0}>> for Scalar

source§

fn from_uint_reduced(w: U512) -> Self

Perform a modular reduction, returning a field element.
source§

fn from_be_bytes_reduced( bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize> ) -> Self

Interpret the given byte array as a big endian integer and perform a modular reduction.
source§

fn from_le_bytes_reduced( bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize> ) -> Self

Interpret the given byte array as a little endian integer and perform a modular reduction.
source§

fn from_be_digest_reduced<D>(digest: D) -> Selfwhere D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,

Interpret a digest as a big endian integer and perform a modular reduction.
source§

fn from_le_digest_reduced<D>(digest: D) -> Selfwhere D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,

Interpret a digest as a little endian integer and perform a modular reduction.
source§

impl ReduceNonZero<UInt<crypto_bigint::::uint::U256::{constant#0}>> for Scalar

source§

fn from_uint_reduced_nonzero(w: U256) -> Self

Perform a modular reduction, returning a field element.
source§

impl ReduceNonZero<UInt<crypto_bigint::::uint::U512::{constant#0}>> for Scalar

source§

fn from_uint_reduced_nonzero(w: U512) -> Self

Perform a modular reduction, returning a field element.
source§

impl Shr<usize> for &Scalar

§

type Output = Scalar

The resulting type after applying the >> operator.
source§

fn shr(self, rhs: usize) -> Self::Output

Performs the >> operation. Read more
source§

impl Shr<usize> for Scalar

§

type Output = Scalar

The resulting type after applying the >> operator.
source§

fn shr(self, rhs: usize) -> Self::Output

Performs the >> operation. Read more
source§

impl SignPrimitive<Secp256k1> for Scalar

source§

fn try_sign_prehashed<K>( &self, ephemeral_scalar: K, z: FieldBytes ) -> Result<(Signature, Option<RecoveryId>), Error>where K: Borrow<Scalar> + Invert<Output = CtOption<Scalar>>,

Try to sign the prehashed message. Read more
source§

fn try_sign_prehashed_rfc6979<D>( &self, z: GenericArray<u8, <<C as Curve>::UInt as ArrayEncoding>::ByteSize>, ad: &[u8] ) -> Result<(Signature<C>, Option<RecoveryId>), Error>where Self: From<ScalarCore<C>>, <C as Curve>::UInt: for<'a> From<&'a Self>, D: Digest<OutputSize = <<C as Curve>::UInt as ArrayEncoding>::ByteSize> + BlockSizeUser + FixedOutput + FixedOutputReset,

Try to sign the given message digest deterministically using the method described in RFC6979 for computing ECDSA ephemeral scalar k. Read more
source§

fn try_sign_digest_rfc6979<D>( &self, msg_digest: D, ad: &[u8] ) -> Result<(Signature<C>, Option<RecoveryId>), Error>where Self: From<ScalarCore<C>>, <C as Curve>::UInt: for<'a> From<&'a Self>, D: Digest<OutputSize = <<C as Curve>::UInt as ArrayEncoding>::ByteSize> + BlockSizeUser + FixedOutput + FixedOutputReset,

Try to sign the given digest instance using the method described in RFC6979.
source§

impl Sub<&Scalar> for &Scalar

§

type Output = Scalar

The resulting type after applying the - operator.
source§

fn sub(self, other: &Scalar) -> Scalar

Performs the - operation. Read more
source§

impl Sub<&Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the - operator.
source§

fn sub(self, other: &Scalar) -> Scalar

Performs the - operation. Read more
source§

impl Sub<Scalar> for Scalar

§

type Output = Scalar

The resulting type after applying the - operator.
source§

fn sub(self, other: Scalar) -> Scalar

Performs the - operation. Read more
source§

impl SubAssign<&Scalar> for Scalar

source§

fn sub_assign(&mut self, rhs: &Scalar)

Performs the -= operation. Read more
source§

impl SubAssign<Scalar> for Scalar

source§

fn sub_assign(&mut self, rhs: Scalar)

Performs the -= operation. Read more
source§

impl Copy for Scalar

source§

impl DefaultIsZeroes for Scalar

source§

impl Eq for Scalar

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> ConditionallyNegatable for Twhere T: ConditionallySelectable, &'a T: for<'a> Neg<Output = T>,

source§

fn conditional_negate(&mut self, choice: Choice)

Negate self if choice == Choice(1); otherwise, leave it unchanged. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<F> Invert for Fwhere F: Field,

§

type Output = CtOption<F>

Field element type
source§

fn invert(&self) -> CtOption<F>

Invert a field element.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<Z> Zeroize for Zwhere Z: DefaultIsZeroes,

source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
source§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for Twhere T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,

source§

impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for Twhere T: for<'r> GroupOps<&'r Rhs, Output>,

source§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for Twhere T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,

source§

impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for Twhere T: for<'r> ScalarMul<&'r Rhs, Output>,