pub struct Point { /* private fields */ }Expand description
Represents a valid non-infinity point on the secp256k1 curve.
Internally this wraps either secp256k1::PublicKey or k256::PublicKey
depending on which feature set is enabled.
Point supports constant time arithmetic operations using addition,
subtraction, negation, and multiplication with other types in this crate.
Curve arithmetic is performed using traits from std::ops.
Implementations§
Source§impl Point
impl Point
Sourcepub fn serialize(&self) -> [u8; 33]
pub fn serialize(&self) -> [u8; 33]
Serializes the point into compressed DER encoding. This consists of a parity
byte at the beginning, which is either 0x02 (even parity) or 0x03 (odd parity),
followed by the big-endian encoding of the point’s X-coordinate.
Sourcepub fn serialize_uncompressed(&self) -> [u8; 65]
pub fn serialize_uncompressed(&self) -> [u8; 65]
Serializes the point into uncompressed DER encoding. This consists of a static tag
byte 0x04, followed by the point’s X-coordinate and Y-coordinate encoded sequentially
(X then Y) as big-endian integers.
Sourcepub fn serialize_xonly(&self) -> [u8; 32]
pub fn serialize_xonly(&self) -> [u8; 32]
Serializes the point into BIP340 X-only representation. This consists solely of the big-endian encoding of the point’s X-coordinate.
Sourcepub fn from_slice(bytes: &[u8]) -> Result<Self, InvalidPointBytes>
pub fn from_slice(bytes: &[u8]) -> Result<Self, InvalidPointBytes>
Parses a non-infinity point from a given byte slice, which can be either 33 or 65 bytes long, depending on whether it represents a compressed or uncompressed point.
Sourcepub fn from_hex(hex: &str) -> Result<Self, InvalidPointString>
pub fn from_hex(hex: &str) -> Result<Self, InvalidPointString>
Parses a non-infinity point from a given hex string, which can be in compressed or uncompressed format.
Sourcepub fn parity(&self) -> Choice
pub fn parity(&self) -> Choice
Returns subtle::Choice::from(0) if the point’s Y-coordinate is even, or
subtle::Choice::from(1) if the Y-coordinate is odd.
Sourcepub fn has_even_y(&self) -> bool
pub fn has_even_y(&self) -> bool
Returns true if the point’s Y-coordinate is even, or false if the Y-coordinate is odd.
Sourcepub fn has_odd_y(&self) -> bool
pub fn has_odd_y(&self) -> bool
Returns true if the point’s Y-coordinate is odd, or false if the Y-coordinate is even.
Sourcepub fn with_parity(self, parity: Choice) -> Self
pub fn with_parity(self, parity: Choice) -> Self
Returns a point with the same X-coordinate but with the Y-coordinate’s parity set
to the given parity, with subtle::Choice::from(1) indicating odd parity and
subtle::Choice::from(0) indicating even parity.
Sourcepub fn to_even_y(self) -> Self
pub fn to_even_y(self) -> Self
Returns a new point with the Y-coordinate coerced flipped to be even.
Sourcepub fn to_odd_y(self) -> Self
pub fn to_odd_y(self) -> Self
Returns a new point with the Y-coordinate coerced flipped to be odd.
Sourcepub fn lift_x(x_bytes: [u8; 32]) -> Result<Point, InvalidPointBytes>
pub fn lift_x(x_bytes: [u8; 32]) -> Result<Point, InvalidPointBytes>
Parses a point with even parity from a BIP340 X-only public-key serialization representation.
Every possible non-zero X-coordinate on the secp256k1 curve has exactly
two corresponding Y-coordinates: one even, and one odd. This function computes
the point for which the X-coordinate is represented by x_bytes, and the Y-coordinate
is even.
Sourcepub fn lift_x_hex(x_bytes_hex: &str) -> Result<Point, InvalidPointString>
pub fn lift_x_hex(x_bytes_hex: &str) -> Result<Point, InvalidPointString>
Parses a point from a BIP340 X-only public-key hex serialization.
Every possible non-zero X-coordinate on the secp256k1 curve has exactly
two corresponding Y-coordinates: one even, and one odd. This function computes
the point for which the X-coordinate is represented by x_bytes_hex, and the
Y-coordinate is even.
Sourcepub fn sum<T>(points: impl IntoIterator<Item = T>) -> MaybePoint
pub fn sum<T>(points: impl IntoIterator<Item = T>) -> MaybePoint
Aggregate an iterator of points together by simple summation.
The iterator item type T can be any type that borrows as a
Point, including Point itself, or &Point.
Point::sum(points) should be preferred over summing up the points
one at a time. This function offloads most of the work to libsecp256k1,
reducing overhead if the secp256k1 crate feature is enabled.
Sourcepub fn negate<C: Verification>(self, secp: &Secp256k1<C>) -> Point
pub fn negate<C: Verification>(self, secp: &Secp256k1<C>) -> Point
Negates the point, returning the point P such that self + P = MaybePoint::Infinity.
Always returns a non-infinity point.
This method uses a specific libsecp256k1 context object instead of the global
context used by the std::ops implementations.
Sourcepub fn sub<C: Verification>(
self,
secp: &Secp256k1<C>,
other: Point,
) -> MaybePoint
pub fn sub<C: Verification>( self, secp: &Secp256k1<C>, other: Point, ) -> MaybePoint
Subtracts two points, returning self - other. This computes the point P such
that self + P = other. Returns MaybePoint::Infinity if self == other.
This method uses a specific libsecp256k1 context object instead of the global
context used by the std::ops implementations.
Sourcepub fn sub_maybe<C: Verification>(
self,
secp: &Secp256k1<C>,
other: MaybePoint,
) -> MaybePoint
pub fn sub_maybe<C: Verification>( self, secp: &Secp256k1<C>, other: MaybePoint, ) -> MaybePoint
Subtracts two points, returning self - other. This computes the point P such
that self + P = other. Returns MaybePoint::Infinity if self == other.
Returns self if other == MaybePoint::Infinity.
This method uses a specific libsecp256k1 context object instead of the global
context used by the std::ops implementations.
Sourcepub fn mul<C: Verification>(self, secp: &Secp256k1<C>, scalar: Scalar) -> Point
pub fn mul<C: Verification>(self, secp: &Secp256k1<C>, scalar: Scalar) -> Point
Multiplies the point by the given scalar. Always returns a non-infinity point.
This method uses a specific libsecp256k1 context object instead of the global
context used by the std::ops implementations.
Sourcepub fn mul_maybe<C: Verification>(
self,
secp: &Secp256k1<C>,
scalar: MaybeScalar,
) -> MaybePoint
pub fn mul_maybe<C: Verification>( self, secp: &Secp256k1<C>, scalar: MaybeScalar, ) -> MaybePoint
Multiplies the point by the given scalar. Returns MaybePoint::Infinity
if scalar == MaybeScalar::Zero.
This method uses a specific libsecp256k1 context object instead of the global
context used by the std::ops implementations.
Trait Implementations§
Source§impl Add<MaybePoint> for Point
impl Add<MaybePoint> for Point
Source§type Output = MaybePoint
type Output = MaybePoint
+ operator.Source§impl Add<Point> for MaybePoint
impl Add<Point> for MaybePoint
Source§impl AddAssign<Point> for MaybePoint
impl AddAssign<Point> for MaybePoint
Source§fn add_assign(&mut self, rhs: Point)
fn add_assign(&mut self, rhs: Point)
+= operation. Read moreSource§impl ConditionallySelectable for Point
impl ConditionallySelectable for Point
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 points in constant time. No timing information about the value of either point 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<'de> Deserialize<'de> for Point
impl<'de> Deserialize<'de> for Point
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 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 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 From<Point> for AffinePoint
impl From<Point> for AffinePoint
Source§impl From<Point> for EncodedPoint
impl From<Point> for EncodedPoint
Source§impl From<Point> for MaybePoint
impl From<Point> for MaybePoint
Source§fn from(point: Point) -> MaybePoint
fn from(point: Point) -> MaybePoint
Converts the point into a MaybePoint::Valid instance.
Source§impl From<Point> for XOnlyPublicKey
impl From<Point> for XOnlyPublicKey
Source§impl Hash for Point
Need to implement this manually because k256::PublicKey does not implement Hash.
impl Hash for Point
Need to implement this manually because k256::PublicKey does not implement Hash.
Source§impl Mul<MaybeScalar> for Point
impl Mul<MaybeScalar> for Point
Source§type Output = MaybePoint
type Output = MaybePoint
* operator.Source§impl Mul<Point> for MaybeScalar
impl Mul<Point> for MaybeScalar
Source§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 Ord for Point
impl Ord for Point
Source§impl PartialOrd for Point
impl PartialOrd for Point
Source§impl Sub<MaybePoint> for Point
impl Sub<MaybePoint> for Point
Source§type Output = MaybePoint
type Output = MaybePoint
- operator.Source§impl Sub<Point> for MaybePoint
impl Sub<Point> for MaybePoint
Source§impl SubAssign<Point> for MaybePoint
impl SubAssign<Point> for MaybePoint
Source§fn sub_assign(&mut self, rhs: Point)
fn sub_assign(&mut self, rhs: Point)
-= operation. Read moreSource§impl TryFrom<&[u8]> for Point
impl TryFrom<&[u8]> for Point
Source§fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
Parses a compressed or uncompressed DER encoding of a point. See
Point::serialize and Point::serialize_uncompressed. The slice
length should be either 33 or 65 for compressed and uncompressed
encodings respectively.
Returns InvalidPointBytes if the bytes do not represent a valid
non-infinity curve point.
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<&[u8; 33]> for Point
impl TryFrom<&[u8; 33]> for Point
Source§fn try_from(bytes: &[u8; 33]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8; 33]) -> Result<Self, Self::Error>
Parses a compressed DER encoding of a point. See Point::serialize.
Returns InvalidPointBytes if the bytes do not represent a valid
non-infinity curve point.
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<&[u8; 65]> for Point
impl TryFrom<&[u8; 65]> for Point
Source§fn try_from(bytes: &[u8; 65]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8; 65]) -> Result<Self, Self::Error>
Parses an uncompressed DER encoding of a point. See Point::serialize_uncompressed.
Returns InvalidPointBytes if the bytes do not represent a valid
non-infinity curve point.
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<[u8; 33]> for Point
impl TryFrom<[u8; 33]> for Point
Source§fn try_from(bytes: [u8; 33]) -> Result<Self, Self::Error>
fn try_from(bytes: [u8; 33]) -> Result<Self, Self::Error>
Parses a compressed DER encoding of a point. See Point::serialize.
Returns InvalidPointBytes if the bytes do not represent a valid
non-infinity curve point.
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<[u8; 65]> for Point
impl TryFrom<[u8; 65]> for Point
Source§fn try_from(bytes: [u8; 65]) -> Result<Self, Self::Error>
fn try_from(bytes: [u8; 65]) -> Result<Self, Self::Error>
Parses an uncompressed DER encoding of a point. See Point::serialize_uncompressed.
Returns InvalidPointBytes if the bytes do not represent a valid
non-infinity curve point.
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<AffinePoint> for Point
impl TryFrom<AffinePoint> for Point
Source§type Error = InfinityPointError
type Error = InfinityPointError
Source§impl TryFrom<EncodedPoint<<Secp256k1 as Curve>::FieldBytesSize>> for Point
impl TryFrom<EncodedPoint<<Secp256k1 as Curve>::FieldBytesSize>> for Point
Source§type Error = InvalidPointBytes
type Error = InvalidPointBytes
Source§impl TryFrom<MaybePoint> for Point
impl TryFrom<MaybePoint> for Point
Source§fn try_from(maybe_point: MaybePoint) -> Result<Self, Self::Error>
fn try_from(maybe_point: MaybePoint) -> Result<Self, Self::Error>
Converts the MaybePoint into a Result<Point, InfinityPointError>,
returning Ok(Point) if the point is a valid non-infinity point,
or Err(InfinityPointError) if maybe_point == MaybePoint::Infinity.