Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions elliptic-curve/src/weierstrass/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ where
pub fn into_bytes(self) -> GenericArray<u8, UncompressedPointSize<C>> {
self.bytes
}

/// Get the x-coordinate of this curve point
pub(crate) fn x(&self) -> &ScalarBytes<C> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something of a misuse of the Scalar part of ScalarBytes here... perhaps it should be renamed to ElementBytes to reflect the Curve::ElementSize rename (from Curve::ScalarSize)

GenericArray::from_slice(&self.bytes[1..(C::ElementSize::to_usize() + 1)])
}

/// Get the y-coordinate of this curve point
pub(crate) fn y(&self) -> &ScalarBytes<C> {
GenericArray::from_slice(&self.bytes[(C::ElementSize::to_usize() + 1)..])
}
}

impl<C: Curve> AsRef<[u8]> for UncompressedPoint<C>
Expand Down
9 changes: 9 additions & 0 deletions elliptic-curve/src/weierstrass/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ where
PublicKey::Uncompressed(UncompressedPoint::from_bytes(tagged_bytes).unwrap())
}

/// Compress this [`PublicKey`].
///
/// If the key is already compressed, this is a no-op.
pub fn compress(&mut self) {
if let PublicKey::Uncompressed(point) = self {
*self = CompressedPoint::from_affine_coords(point.x(), point.y()).into();
}
}

/// Obtain public key as a byte array reference
#[inline]
pub fn as_bytes(&self) -> &[u8] {
Expand Down