pub struct Scalar(_);
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
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
sourceimpl 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: impl CryptoRng + RngCore) -> Self
pub fn generate_biased(rng: impl CryptoRng + RngCore) -> Self
Returns a (nearly) uniformly-random scalar, generated in constant time.
sourcepub fn generate_vartime(rng: impl RngCore) -> Self
pub fn generate_vartime(rng: impl RngCore) -> Self
Returns a uniformly-random scalar, generated using rejection sampling.
Trait Implementations
sourceimpl AddAssign<&Scalar> for Scalar
impl AddAssign<&Scalar> for Scalar
sourcefn add_assign(&mut self, rhs: &Scalar)
fn add_assign(&mut self, rhs: &Scalar)
+=
operation. Read moresourceimpl AddAssign<Scalar> for Scalar
impl AddAssign<Scalar> for Scalar
sourcefn add_assign(&mut self, rhs: Scalar)
fn add_assign(&mut self, rhs: Scalar)
+=
operation. Read moresourceimpl ConditionallySelectable for Scalar
impl ConditionallySelectable for Scalar
sourcefn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
sourcefn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
sourceimpl ConstantTimeEq for Scalar
impl ConstantTimeEq for Scalar
sourceimpl Field for Scalar
impl Field for Scalar
sourcefn 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)
sourcefn random(rng: impl RngCore) -> Self
fn random(rng: impl RngCore) -> Self
sourcefn invert(&self) -> CtOption<Self>
fn invert(&self) -> CtOption<Self>
fn is_zero_vartime(&self) -> bool
fn is_zero_vartime(&self) -> bool
fn cube(&self) -> Self
fn cube(&self) -> Self
fn pow_vartime<S>(&self, exp: S) -> Selfwhere
S: AsRef<[u64]>,
fn pow_vartime<S>(&self, exp: S) -> Selfwhere
S: AsRef<[u64]>,
sourceimpl From<&Scalar> for FieldBytes
impl From<&Scalar> for FieldBytes
sourceimpl From<&Scalar> for ScalarCore<Secp256k1>
impl From<&Scalar> for ScalarCore<Secp256k1>
sourcefn from(scalar: &Scalar) -> ScalarCore<Secp256k1>
fn from(scalar: &Scalar) -> ScalarCore<Secp256k1>
sourceimpl From<&ScalarCore<Secp256k1>> for Scalar
impl From<&ScalarCore<Secp256k1>> for Scalar
sourcefn from(scalar: &ScalarCore<Secp256k1>) -> Scalar
fn from(scalar: &ScalarCore<Secp256k1>) -> Scalar
sourceimpl From<Scalar> for FieldBytes
impl From<Scalar> for FieldBytes
sourceimpl From<Scalar> for ScalarCore<Secp256k1>
impl From<Scalar> for ScalarCore<Secp256k1>
sourcefn from(scalar: Scalar) -> ScalarCore<Secp256k1>
fn from(scalar: Scalar) -> ScalarCore<Secp256k1>
sourceimpl From<ScalarCore<Secp256k1>> for Scalar
impl From<ScalarCore<Secp256k1>> for Scalar
sourcefn from(scalar: ScalarCore<Secp256k1>) -> Scalar
fn from(scalar: ScalarCore<Secp256k1>) -> Scalar
sourceimpl Mul<&Scalar> for &ProjectivePoint
impl Mul<&Scalar> for &ProjectivePoint
type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.sourcefn mul(self, other: &Scalar) -> ProjectivePoint
fn mul(self, other: &Scalar) -> ProjectivePoint
*
operation. Read moresourceimpl Mul<&Scalar> for AffinePoint
impl Mul<&Scalar> for AffinePoint
type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.sourcefn mul(self, scalar: &Scalar) -> ProjectivePoint
fn mul(self, scalar: &Scalar) -> ProjectivePoint
*
operation. Read moresourceimpl Mul<&Scalar> for ProjectivePoint
impl Mul<&Scalar> for ProjectivePoint
type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.sourcefn mul(self, other: &Scalar) -> ProjectivePoint
fn mul(self, other: &Scalar) -> ProjectivePoint
*
operation. Read moresourceimpl Mul<Scalar> for AffinePoint
impl Mul<Scalar> for AffinePoint
type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.sourcefn mul(self, scalar: Scalar) -> ProjectivePoint
fn mul(self, scalar: Scalar) -> ProjectivePoint
*
operation. Read moresourceimpl Mul<Scalar> for ProjectivePoint
impl Mul<Scalar> for ProjectivePoint
type Output = ProjectivePoint
type Output = ProjectivePoint
*
operator.sourcefn mul(self, other: Scalar) -> ProjectivePoint
fn mul(self, other: Scalar) -> ProjectivePoint
*
operation. Read moresourceimpl MulAssign<&Scalar> for ProjectivePoint
impl MulAssign<&Scalar> for ProjectivePoint
sourcefn mul_assign(&mut self, rhs: &Scalar)
fn mul_assign(&mut self, rhs: &Scalar)
*=
operation. Read moresourceimpl MulAssign<&Scalar> for Scalar
impl MulAssign<&Scalar> for Scalar
sourcefn mul_assign(&mut self, rhs: &Scalar)
fn mul_assign(&mut self, rhs: &Scalar)
*=
operation. Read moresourceimpl MulAssign<Scalar> for ProjectivePoint
impl MulAssign<Scalar> for ProjectivePoint
sourcefn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*=
operation. Read moresourceimpl MulAssign<Scalar> for Scalar
impl MulAssign<Scalar> for Scalar
sourcefn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*=
operation. Read moresourceimpl PrimeField for Scalar
impl PrimeField for Scalar
sourcefn 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).
type Repr = GenericArray<u8, <<Secp256k1 as Curve>::UInt as ArrayEncoding>::ByteSize>
type Repr = GenericArray<u8, <<Secp256k1 as Curve>::UInt as ArrayEncoding>::ByteSize>
sourceconst CAPACITY: u32 = 255u32
const CAPACITY: u32 = 255u32
sourcefn to_repr(&self) -> FieldBytes
fn to_repr(&self) -> FieldBytes
sourcefn multiplicative_generator() -> Self
fn multiplicative_generator() -> Self
modulus - 1
order. This element must
also be a quadratic nonresidue. Read moresourcefn root_of_unity() -> Self
fn root_of_unity() -> Self
2^s
root of unity. Read morefn 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>
sourceimpl Reduce<UInt<_>> for Scalar
impl Reduce<UInt<_>> for Scalar
sourcefn from_uint_reduced(w: U256) -> Self
fn from_uint_reduced(w: U256) -> Self
sourcefn from_be_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
fn from_be_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
sourcefn from_le_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
fn from_le_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
sourcefn from_be_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
fn from_be_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
sourcefn from_le_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
fn from_le_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
sourceimpl Reduce<UInt<_>> for Scalar
impl Reduce<UInt<_>> for Scalar
sourcefn from_uint_reduced(w: U512) -> Self
fn from_uint_reduced(w: U512) -> Self
sourcefn from_be_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
fn from_be_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
sourcefn from_le_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
fn from_le_bytes_reduced(
bytes: GenericArray<u8, <UInt as ArrayEncoding>::ByteSize>
) -> Self
sourcefn from_be_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
fn from_be_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
sourcefn from_le_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
fn from_le_digest_reduced<D>(digest: D) -> Selfwhere
D: FixedOutput<OutputSize = <UInt as ArrayEncoding>::ByteSize>,
sourceimpl ReduceNonZero<UInt<_>> for Scalar
impl ReduceNonZero<UInt<_>> for Scalar
sourcefn from_uint_reduced_nonzero(w: U256) -> Self
fn from_uint_reduced_nonzero(w: U256) -> Self
sourceimpl ReduceNonZero<UInt<_>> for Scalar
impl ReduceNonZero<UInt<_>> for Scalar
sourcefn from_uint_reduced_nonzero(w: U512) -> Self
fn from_uint_reduced_nonzero(w: U512) -> Self
sourceimpl SignPrimitive<Secp256k1> for Scalar
Available on crate feature ecdsa-core
only.
impl SignPrimitive<Secp256k1> for Scalar
ecdsa-core
only.sourcefn try_sign_prehashed<K>(
&self,
ephemeral_scalar: K,
z: FieldBytes
) -> Result<(Signature, Option<RecoveryId>), Error>where
K: Borrow<Scalar> + Invert<Output = CtOption<Scalar>>,
fn try_sign_prehashed<K>(
&self,
ephemeral_scalar: K,
z: FieldBytes
) -> Result<(Signature, Option<RecoveryId>), Error>where
K: Borrow<Scalar> + Invert<Output = CtOption<Scalar>>,
sourceimpl SubAssign<&Scalar> for Scalar
impl SubAssign<&Scalar> for Scalar
sourcefn sub_assign(&mut self, rhs: &Scalar)
fn sub_assign(&mut self, rhs: &Scalar)
-=
operation. Read moresourceimpl SubAssign<Scalar> for Scalar
impl SubAssign<Scalar> for Scalar
sourcefn sub_assign(&mut self, rhs: Scalar)
fn sub_assign(&mut self, rhs: Scalar)
-=
operation. Read more