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
, andinvert
operations- Bounds for
Add
,Sub
,Mul
, andNeg
(as well as*Assign
equivalents) - Bounds for
ConditionallySelectable
from thesubtle
crate
PrimeField
- represents elements of prime fields and provides:from_repr
/to_repr
for converting field elements from/to big integers.multiplicative_generator
androot_of_unity
constants.
PrimeFieldBits
- operations over field elements represented as bits (requiresbits
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
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: usize) -> Scalar
pub fn shr_vartime(&self, shift: usize) -> Scalar
Right shifts the scalar.
Note: not constant-time with respect to the shift
parameter.
Sourcepub fn generate_biased(rng: &mut impl CryptoRngCore) -> Self
pub fn generate_biased(rng: &mut impl CryptoRngCore) -> Self
Returns a (nearly) uniformly-random scalar, generated in constant time.
Sourcepub fn generate_vartime(rng: &mut impl RngCore) -> Self
pub fn generate_vartime(rng: &mut impl RngCore) -> Self
Returns a uniformly-random scalar, generated using rejection sampling.
Trait Implementations§
Source§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 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 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
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 random(rng: impl RngCore) -> Self
fn random(rng: impl RngCore) -> Self
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 is_zero_vartime(&self) -> bool
fn is_zero_vartime(&self) -> bool
Source§impl From<&Scalar> for FieldBytes
impl From<&Scalar> for FieldBytes
Source§impl From<Scalar> for FieldBytes
impl From<Scalar> for FieldBytes
Source§impl FromUintUnchecked for Scalar
impl FromUintUnchecked for Scalar
Source§impl Invert for Scalar
impl Invert for Scalar
Source§fn invert_vartime(&self) -> CtOption<Self>
fn invert_vartime(&self) -> CtOption<Self>
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.
Source§impl Mul<&Scalar> for &ProjectivePoint
impl Mul<&Scalar> for &ProjectivePoint
Source§type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.Source§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 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<&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 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 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§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 = 255u32
const CAPACITY: u32 = 255u32
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 = GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>
type Repr = GenericArray<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 Reduce<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
impl Reduce<Uint<crypto_bigint::::uint::U256::{constant#0}>> for Scalar
Source§type Bytes = GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>
type Bytes = GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>
Reduce::reduce_bytes
.Source§fn reduce_bytes(bytes: &FieldBytes) -> Self
fn reduce_bytes(bytes: &FieldBytes) -> Self
Source§impl Reduce<Uint<crypto_bigint::::uint::U512::{constant#0}>> for Scalar
impl Reduce<Uint<crypto_bigint::::uint::U512::{constant#0}>> for Scalar
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§fn reduce_nonzero_bytes(bytes: &FieldBytes) -> Self
fn reduce_nonzero_bytes(bytes: &FieldBytes) -> 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§fn reduce_nonzero_bytes(bytes: &WideBytes) -> Self
fn reduce_nonzero_bytes(bytes: &WideBytes) -> 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 SignPrimitive<Secp256k1> for Scalar
impl SignPrimitive<Secp256k1> for Scalar
Source§fn try_sign_prehashed<K>(
&self,
k: K,
z: &FieldBytes,
) -> Result<(Signature, Option<RecoveryId>), Error>
fn try_sign_prehashed<K>( &self, k: K, z: &FieldBytes, ) -> Result<(Signature, Option<RecoveryId>), Error>
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, rhs: &Scalar)
fn sub_assign(&mut self, rhs: &Scalar)
-=
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 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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)
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.