pub struct Scalar { /* private fields */ }Expand description
Represents a non-zero scalar in the range [1, n) where n is the order
of the secp256k1 curve. A Scalar can be:
- added, negated, subtracted, and multiplied with other
Scalarinstances. - added, negated, subtracted, and multiplied with
MaybeScalar. - multiplied with
Point. - multiplied with
MaybePoint.
…using the normal Rust arithemtic operators +, - and *. Such operations
are commutative, i.e. a * b = b * a and a + b = b + a in call cases.
Depending on the types involved in an operation, certain operators will produce
different result types which should be handled depending on your use case. For
instance, adding two Scalars results in a MaybeScalar, because the two
Scalars may be additive inverses of each other and their output would result
in MaybeScalar::Zero when taken mod n.
Implementations§
Source§impl Scalar
impl Scalar
Sourcepub fn half_order() -> Scalar
pub fn half_order() -> Scalar
Returns half of the curve order n, specifically n >> 1.
Sourcepub fn max() -> Scalar
pub fn max() -> Scalar
Returns a valid Scalar with the maximum possible value less
than the curve order, n - 1.
Sourcepub fn is_high(&self) -> Choice
pub fn is_high(&self) -> Choice
Returns subtle::Choice::from(1) if this scalar is strictly greater
than half the curve order; i.e if self > (n >> 1).
This is used to reduce malleability of ECDSA signatures, whose s values
could be considered valid if they are either s or n - s. Converting
the s value using Scalar::to_low and checking it using
Scalar::is_high upon verification fixes this ambiguity.
Beware that leaking timing information about this bit may expose a bit of information about the scalar.
Sourcepub fn to_low(self) -> Scalar
pub fn to_low(self) -> Scalar
If self.is_high(), this returns -self. Otherwise, returns
the scalar unchanged.
This is used to reduce malleability of ECDSA signatures, whose s values
could be considered valid if they are either s or n - s. Converting
the s value using Scalar::to_low and checking it using
Scalar::is_high upon verification fixes this ambiguity.
Sourcepub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Scalar
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Scalar
Generates a new random scalar from the given CSPRNG.
Sourcepub fn serialize(&self) -> [u8; 32]
pub fn serialize(&self) -> [u8; 32]
Serializes the scalar to a big-endian byte array representation.
§Warning
Use cautiously. Non-constant time operations on these bytes could reveal secret key material.
Sourcepub fn from_slice(bytes: &[u8]) -> Result<Self, InvalidScalarBytes>
pub fn from_slice(bytes: &[u8]) -> Result<Self, InvalidScalarBytes>
Parses a non-zero scalar in the range [1, n) from a given byte slice,
which must be exactly 32-byte long and must represent the scalar in
big-endian format.
Sourcepub fn from_hex(hex: &str) -> Result<Self, InvalidScalarString>
pub fn from_hex(hex: &str) -> Result<Self, InvalidScalarString>
Parses a Scalar from a 32-byte hex string representation.
Sourcepub fn base_point_mul(&self) -> Point
pub fn base_point_mul(&self) -> Point
Multiplies the secp256k1 base point by this scalar. This is how public keys (points) are derived from private keys (scalars). Since this scalar is non-zero, the point derived from base-point multiplication is also guaranteed to be valid.
Sourcepub fn negate_if(self, parity: Choice) -> Scalar
pub fn negate_if(self, parity: Choice) -> Scalar
Negates the scalar in constant-time if the given parity bit is a 1.
Sourcepub fn invert(self) -> Scalar
pub fn invert(self) -> Scalar
Inverts a scalar modulo the curve order n in constant time. This
outputs a scalar such that self * self.inverse() == Scalar::one() for
all non-zero scalars.
Sourcepub fn reduce_from(z_bytes: &[u8; 32]) -> Self
pub fn reduce_from(z_bytes: &[u8; 32]) -> Self
Converts a 32-byte array into a Scalar by interpreting it as a big-endian
integer z and returning (z % (n-1)) + 1, where n is the secp256k1
curve order. This always returns a valid non-zero scalar in the range [1, n).
All operations are constant-time, except if z works out to be zero.
The probability that z_bytes represents an integer z larger than the
curve order is only about 1 in 2^128, but nonetheless this function makes a
best-effort attempt to parse all inputs in constant time and reduce them to
an integer in the range [1, n).
Trait Implementations§
Source§impl Add<MaybeScalar> for Scalar
impl Add<MaybeScalar> for Scalar
Source§type Output = MaybeScalar
type Output = MaybeScalar
+ operator.Source§impl Add<Scalar> for MaybeScalar
impl Add<Scalar> for MaybeScalar
Source§impl AddAssign<Scalar> for MaybeScalar
impl AddAssign<Scalar> for MaybeScalar
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
Conditionally selects one of two scalars in constant time. No timing information about the value of either scalar will be leaked.
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 ConstantTimeGreater for Scalar
impl ConstantTimeGreater for Scalar
Source§impl ConstantTimeLess for Scalar
impl ConstantTimeLess for Scalar
Source§impl Debug for Scalar
This implementation was duplicated from the secp256k1 crate, because
k256::NonZeroScalar doesn’t implement Debug.
impl Debug for Scalar
This implementation was duplicated from the secp256k1 crate, because
k256::NonZeroScalar doesn’t implement Debug.
Source§impl<'de> Deserialize<'de> for Scalar
impl<'de> Deserialize<'de> for Scalar
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Div<Scalar> for G
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
impl Div<Scalar> for G
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
Source§impl Div<Scalar> for MaybePoint
impl Div<Scalar> for MaybePoint
Source§impl Div<Scalar> for MaybeScalar
impl Div<Scalar> for MaybeScalar
Source§impl Div<Scalar> for Point
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
impl Div<Scalar> for Point
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
Source§impl Div for Scalar
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
impl Div for Scalar
To divide by rhs, we simply multiply by rhs.inverse(), because rhs.inverse()
is algebraically the same as 1 / rhs.
Source§impl DivAssign<Scalar> for MaybePoint
impl DivAssign<Scalar> for MaybePoint
Source§fn div_assign(&mut self, rhs: Scalar)
fn div_assign(&mut self, rhs: Scalar)
/= operation. Read moreSource§impl DivAssign<Scalar> for MaybeScalar
impl DivAssign<Scalar> for MaybeScalar
Source§fn div_assign(&mut self, rhs: Scalar)
fn div_assign(&mut self, rhs: Scalar)
/= operation. Read moreSource§impl DivAssign<Scalar> for Point
impl DivAssign<Scalar> for Point
Source§fn div_assign(&mut self, rhs: Scalar)
fn div_assign(&mut self, rhs: Scalar)
/= operation. Read moreSource§impl DivAssign for Scalar
impl DivAssign for Scalar
Source§fn div_assign(&mut self, rhs: Scalar)
fn div_assign(&mut self, rhs: Scalar)
/= operation. Read moreSource§impl From<NonZeroScalar<Secp256k1>> for Scalar
impl From<NonZeroScalar<Secp256k1>> for Scalar
Source§fn from(nz_scalar: NonZeroScalar) -> Self
fn from(nz_scalar: NonZeroScalar) -> Self
Source§impl From<Scalar> for MaybeScalar
impl From<Scalar> for MaybeScalar
Source§fn from(scalar: Scalar) -> Self
fn from(scalar: Scalar) -> Self
Converts the scalar into a MaybeScalar::Valid instance.
Source§impl From<Scalar> for NonZeroScalar
impl From<Scalar> for NonZeroScalar
Source§impl Mul<MaybePoint> for Scalar
impl Mul<MaybePoint> for Scalar
Source§type Output = MaybePoint
type Output = MaybePoint
* operator.Source§impl Mul<MaybeScalar> for Scalar
impl Mul<MaybeScalar> for Scalar
Source§type Output = MaybeScalar
type Output = MaybeScalar
* operator.Source§impl Mul<Scalar> for MaybePoint
impl Mul<Scalar> for MaybePoint
Source§impl Mul<Scalar> for MaybeScalar
impl Mul<Scalar> for MaybeScalar
Source§impl MulAssign<Scalar> for MaybePoint
impl MulAssign<Scalar> for MaybePoint
Source§fn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*= operation. Read moreSource§impl MulAssign<Scalar> for MaybeScalar
impl MulAssign<Scalar> for MaybeScalar
Source§fn mul_assign(&mut self, rhs: Scalar)
fn mul_assign(&mut self, rhs: Scalar)
*= operation. Read moreSource§impl MulAssign<Scalar> for Point
impl MulAssign<Scalar> for Point
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 Product for Scalar
This implementation allows iterators of Scalar
to be multiplied together with Iterator::product.
impl Product for Scalar
This implementation allows iterators of Scalar
to be multiplied together with Iterator::product.
Since all scalars in the iterator are guaranteed to be non-zero, the resulting product is also guaranteed to be non-zero.
use secp::Scalar;
let scalars = [
Scalar::two(),
Scalar::two(),
Scalar::two(),
];
let expected = "0000000000000000000000000000000000000000000000000000000000000008"
.parse::<Scalar>()
.unwrap();
assert_eq!(scalars.into_iter().product::<Scalar>(), expected);Returns Scalar::one() if the iterator is empty.
Source§impl Sub<MaybeScalar> for Scalar
impl Sub<MaybeScalar> for Scalar
Source§type Output = MaybeScalar
type Output = MaybeScalar
- operator.Source§impl Sub<Scalar> for MaybeScalar
impl Sub<Scalar> for MaybeScalar
Source§impl SubAssign<Scalar> for MaybeScalar
impl SubAssign<Scalar> for MaybeScalar
Source§fn sub_assign(&mut self, rhs: Scalar)
fn sub_assign(&mut self, rhs: Scalar)
-= operation. Read moreSource§impl TryFrom<&[u8]> for Scalar
impl TryFrom<&[u8]> for Scalar
Source§fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
Attempts to parse a 32-byte slice as a scalar in the range [1, n)
in constant time, where n is the curve order.
Returns InvalidScalarBytes if the integer represented by the bytes
is greater than or equal to the curve order, or if the bytes are all zero.
Fails if bytes.len() != 32.
Source§type Error = InvalidScalarBytes
type Error = InvalidScalarBytes
Source§impl TryFrom<&[u8; 32]> for Scalar
impl TryFrom<&[u8; 32]> for Scalar
Source§fn try_from(bytes: &[u8; 32]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8; 32]) -> Result<Self, Self::Error>
Attempts to parse a 32-byte array as a scalar in the range [1, n)
in constant time, where n is the curve order.
Returns InvalidScalarBytes if the integer represented by the bytes
is greater than or equal to the curve order, or if the bytes are all zero.
Source§type Error = InvalidScalarBytes
type Error = InvalidScalarBytes
Source§impl TryFrom<[u8; 32]> for Scalar
impl TryFrom<[u8; 32]> for Scalar
Source§fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error>
fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error>
Attempts to parse a 32-byte array as a scalar in the range [1, n)
in constant time, where n is the curve order.
Returns InvalidScalarBytes if the integer represented by the bytes
is greater than or equal to the curve order, or if the bytes are all zero.
Source§type Error = InvalidScalarBytes
type Error = InvalidScalarBytes
Source§impl TryFrom<GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
impl TryFrom<GenericArray<u8, <Secp256k1 as Curve>::FieldBytesSize>> for Scalar
Source§fn try_from(bytes: FieldBytes) -> Result<Self, Self::Error>
fn try_from(bytes: FieldBytes) -> Result<Self, Self::Error>
Attempts to parse a 32-byte array as a scalar in the range [1, n)
in constant time, where n is the curve order.
Returns InvalidScalarBytes if the integer represented by the bytes
is greater than or equal to the curve order.
Source§type Error = InvalidScalarBytes
type Error = InvalidScalarBytes
Source§impl TryFrom<MaybeScalar> for Scalar
impl TryFrom<MaybeScalar> for Scalar
Source§fn try_from(maybe_scalar: MaybeScalar) -> Result<Self, Self::Error>
fn try_from(maybe_scalar: MaybeScalar) -> Result<Self, Self::Error>
Converts the MaybeScalar into a Result<Scalar, ZeroScalarError>,
returning Ok(Scalar) if the scalar is a valid non-zero number,
or Err(ZeroScalarError) if maybe_scalar == MaybeScalar::Zero.
Source§type Error = ZeroScalarError
type Error = ZeroScalarError
Source§impl TryFrom<u128> for Scalar
Converts any unsigned integer number into a Scalar.
Returns ZeroScalarError if the integer is zero.
impl TryFrom<u128> for Scalar
Converts any unsigned integer number into a Scalar.
Returns ZeroScalarError if the integer is zero.