Point

Struct Point 

Source
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

Source

pub fn generator() -> Point

Returns the secp256k1 generator base point G.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn to_even_y(self) -> Self

Returns a new point with the Y-coordinate coerced flipped to be even.

Source

pub fn to_odd_y(self) -> Self

Returns a new point with the Y-coordinate coerced flipped to be odd.

Source

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.

Source

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.

Source

pub fn sum<T>(points: impl IntoIterator<Item = T>) -> MaybePoint
where T: Borrow<Point>,

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn negate_if(self, parity: Choice) -> Point

Negates the point in constant-time if the given parity bit is a 1.

Trait Implementations§

Source§

impl Add<G> for Point

Source§

type Output = MaybePoint

The resulting type after applying the + operator.
Source§

fn add(self, rhs: G) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<MaybePoint> for Point

Source§

type Output = MaybePoint

The resulting type after applying the + operator.
Source§

fn add(self, rhs: MaybePoint) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Point> for G

Source§

type Output = MaybePoint

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Point) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Point> for MaybePoint

Source§

type Output = MaybePoint

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Point) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Point

Point + Point

Source§

type Output = MaybePoint

The resulting type after applying the + operator.
Source§

fn add(self, other: Point) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<Point> for MaybePoint

Source§

fn add_assign(&mut self, rhs: Point)

Performs the += operation. Read more
Source§

impl AsRef<PublicKey> for Point

Source§

fn as_ref(&self) -> &PublicKey

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Point

Source§

fn clone(&self) -> Point

Returns a duplicate 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 Point

Source§

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)

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 Debug for Point

Source§

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

Formats the point into a string like "Point(025fa83ed...)".

Source§

impl<'de> Deserialize<'de> for Point

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Point

Source§

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

Serializes and displays the point as a compressed point in hex format.

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.

Source§

type Output = Point

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Scalar) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<Scalar> for Point

Source§

fn div_assign(&mut self, rhs: Scalar)

Performs the /= operation. Read more
Source§

impl From<(XOnlyPublicKey, Parity)> for Point

Source§

fn from((xonly, parity): (XOnlyPublicKey, Parity)) -> Self

Converts an X-only public key with a given parity into a Point.

Source§

impl From<Point> for [u8; 33]

Source§

fn from(point: Point) -> Self

Serializes the point to DER-compressed format.

Source§

impl From<Point> for [u8; 65]

Source§

fn from(point: Point) -> Self

Serializes the point to DER-uncompressed format.

Source§

impl From<Point> for (XOnlyPublicKey, Parity)

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for AffinePoint

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for EncodedPoint

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for MaybePoint

Source§

fn from(point: Point) -> MaybePoint

Converts the point into a MaybePoint::Valid instance.

Source§

impl From<Point> for PublicKey

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for PublicKey

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<Point> for XOnlyPublicKey

Source§

fn from(point: Point) -> Self

Converts to this type from the input type.
Source§

impl From<PublicKey<Secp256k1>> for Point

Source§

fn from(pubkey: PublicKey) -> Self

Converts to this type from the input type.
Source§

impl From<PublicKey> for Point

Source§

fn from(pubkey: PublicKey) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Point

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a point from a compressed or uncompressed DER encoded hex string. The input string should be either 33 or 65 bytes, hex-encoded.

Source§

type Err = InvalidPointString

The associated error which can be returned from parsing.
Source§

impl Hash for Point

Need to implement this manually because k256::PublicKey does not implement Hash.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl LowerHex for Point

Source§

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

Formats the Point as a DER-compressed hex string in lower case.

Source§

impl Mul<MaybeScalar> for Point

Source§

type Output = MaybePoint

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: MaybeScalar) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Point> for MaybeScalar

Source§

type Output = MaybePoint

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Point) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Point> for Scalar

Scalar * Point

Source§

type Output = Point

The resulting type after applying the * operator.
Source§

fn mul(self, point: Point) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Scalar> for Point

Point * Scalar

Source§

type Output = Point

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: Scalar) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<Scalar> for Point

Source§

fn mul_assign(&mut self, rhs: Scalar)

Performs the *= operation. Read more
Source§

impl Neg for Point

-Point

Source§

type Output = Point

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Point

Source§

fn cmp(&self, other: &Point) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Point

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Point

Source§

fn partial_cmp(&self, other: &Point) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Point

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub<G> for Point

Source§

type Output = MaybePoint

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: G) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<MaybePoint> for Point

Source§

type Output = MaybePoint

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: MaybePoint) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Point> for G

Source§

type Output = MaybePoint

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Point) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Point> for MaybePoint

Source§

type Output = MaybePoint

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Point) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Point

Source§

type Output = MaybePoint

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Point) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<Point> for MaybePoint

Source§

fn sub_assign(&mut self, rhs: Point)

Performs the -= operation. Read more
Source§

impl TryFrom<&[u8]> for Point

Source§

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

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

impl TryFrom<&[u8; 33]> for Point

Source§

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

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

impl TryFrom<&[u8; 65]> for Point

Source§

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

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

impl TryFrom<[u8; 33]> for Point

Source§

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

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

impl TryFrom<[u8; 65]> for Point

Source§

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

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

impl TryFrom<AffinePoint> for Point

Source§

type Error = InfinityPointError

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

fn try_from(affine_point: AffinePoint) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<EncodedPoint<<Secp256k1 as Curve>::FieldBytesSize>> for Point

Source§

type Error = InvalidPointBytes

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

fn try_from(encoded_point: EncodedPoint) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<MaybePoint> for Point

Source§

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.

Source§

type Error = InfinityPointError

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

impl UpperHex for Point

Source§

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

Formats the Point as a DER-compressed hex string in upper case.

Source§

impl Copy for Point

Source§

impl Eq for Point

Source§

impl StructuralPartialEq for Point

Auto Trait Implementations§

§

impl Freeze for Point

§

impl RefUnwindSafe for Point

§

impl Send for Point

§

impl Sync for Point

§

impl Unpin for Point

§

impl UnwindSafe for Point

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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