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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ecdsa = ["arithmetic", "digest", "ecdsa-core/sign", "ecdsa-core/verify"]
expose-field = ["arithmetic"]
jwk = ["elliptic-curve/jwk"]
keccak256 = ["digest", "sha3"]
pem = ["elliptic-curve/pem", "pkcs8"]
pem = ["elliptic-curve/pem", "ecdsa-core/pem", "pkcs8"]
pkcs8 = ["elliptic-curve/pkcs8"]
serde = ["ecdsa-core/serde", "elliptic-curve/serde", "sec1/serde"]
sha256 = ["digest", "sha2"]
Expand Down
45 changes: 44 additions & 1 deletion k256/src/arithmetic/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ use elliptic_curve::{
sec1::{self, FromEncodedPoint, ToEncodedPoint},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
zeroize::DefaultIsZeroes,
AffineArithmetic, AffineXCoordinate, DecompressPoint,
AffineArithmetic, AffineXCoordinate, DecompressPoint, Error, Result,
};

impl AffineArithmetic for Secp256k1 {
type AffinePoint = AffinePoint;
}

#[cfg(feature = "serde")]
use elliptic_curve::serde::{de, ser, Deserialize, Serialize};

/// A point on the secp256k1 curve in affine coordinates.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
Expand Down Expand Up @@ -214,6 +217,22 @@ impl ToEncodedPoint<Secp256k1> for AffinePoint {
}
}

impl TryFrom<EncodedPoint> for AffinePoint {
type Error = Error;

fn try_from(point: EncodedPoint) -> Result<AffinePoint> {
AffinePoint::try_from(&point)
}
}

impl TryFrom<&EncodedPoint> for AffinePoint {
type Error = Error;

fn try_from(point: &EncodedPoint) -> Result<AffinePoint> {
Option::from(AffinePoint::from_encoded_point(point)).ok_or(Error)
}
}

impl From<AffinePoint> for EncodedPoint {
/// Returns the SEC1 compressed encoding of this point.
fn from(affine_point: AffinePoint) -> EncodedPoint {
Expand Down Expand Up @@ -249,6 +268,30 @@ impl Neg for AffinePoint {
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for AffinePoint {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
self.to_encoded_point(true).serialize(serializer)
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for AffinePoint {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
EncodedPoint::deserialize(deserializer)?
.try_into()
.map_err(de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::AffinePoint;
Expand Down
2 changes: 1 addition & 1 deletion k256/src/arithmetic/field/field_10x26.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Field element modulo the curve internal modulus using 32-bit limbs.
//! Ported from https://github.com/bitcoin-core/secp256k1
//! Inspired by the implementation in <https://github.com/bitcoin-core/secp256k1>

use crate::FieldBytes;
use elliptic_curve::{
Expand Down
2 changes: 1 addition & 1 deletion k256/src/arithmetic/field/field_5x52.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Field element modulo the curve internal modulus using 32-bit limbs.
//! Ported from https://github.com/bitcoin-core/secp256k1
//! Inspired by the implementation in <https://github.com/bitcoin-core/secp256k1>

use crate::FieldBytes;
use elliptic_curve::{
Expand Down
43 changes: 43 additions & 0 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use elliptic_curve::{
#[cfg(feature = "bits")]
use {crate::ScalarBits, elliptic_curve::group::ff::PrimeFieldBits};

#[cfg(feature = "serde")]
use elliptic_curve::serde::{de, ser, Deserialize, Serialize};

#[cfg(test)]
use num_bigint::{BigUint, ToBigUint};

Expand Down Expand Up @@ -366,6 +369,24 @@ impl From<ScalarCore<Secp256k1>> for Scalar {
}
}

impl From<&ScalarCore<Secp256k1>> for Scalar {
fn from(scalar: &ScalarCore<Secp256k1>) -> Scalar {
Scalar(*scalar.as_uint())
}
}

impl From<Scalar> for ScalarCore<Secp256k1> {
fn from(scalar: Scalar) -> ScalarCore<Secp256k1> {
ScalarCore::from(&scalar)
}
}

impl From<&Scalar> for ScalarCore<Secp256k1> {
fn from(scalar: &Scalar) -> ScalarCore<Secp256k1> {
ScalarCore::new(scalar.0).unwrap()
}
}

impl IsHigh for Scalar {
fn is_high(&self) -> Choice {
self.0.ct_gt(&FRAC_MODULUS_2)
Expand Down Expand Up @@ -574,6 +595,28 @@ impl From<&Scalar> for FieldBytes {
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for Scalar {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
ScalarCore::from(self).serialize(serializer)
}
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for Scalar {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
Ok(ScalarCore::deserialize(deserializer)?.into())
}
}

#[cfg(test)]
mod tests {
use super::Scalar;
Expand Down
38 changes: 36 additions & 2 deletions k256/src/ecdsa/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use crate::pkcs8::{self, DecodePublicKey};
#[cfg(feature = "pem")]
use core::str::FromStr;

#[cfg(all(feature = "pem", feature = "serde"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pem", feature = "serde"))))]
use elliptic_curve::serde::{de, ser, Deserialize, Serialize};

/// ECDSA/secp256k1 verification key (i.e. public key)
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -144,9 +148,17 @@ impl From<&AffinePoint> for VerifyingKey {
}
}

impl From<ecdsa_core::VerifyingKey<Secp256k1>> for VerifyingKey {
fn from(verifying_key: ecdsa_core::VerifyingKey<Secp256k1>) -> VerifyingKey {
VerifyingKey {
inner: verifying_key,
}
}
}

impl From<&VerifyingKey> for EncodedPoint {
fn from(verify_key: &VerifyingKey) -> EncodedPoint {
verify_key.to_encoded_point(true)
fn from(verifying_key: &VerifyingKey) -> EncodedPoint {
verifying_key.to_encoded_point(true)
}
}

Expand Down Expand Up @@ -188,6 +200,28 @@ impl FromStr for VerifyingKey {
}
}

#[cfg(all(feature = "pem", feature = "serde"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pem", feature = "serde"))))]
impl Serialize for VerifyingKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
self.inner.serialize(serializer)
}
}

#[cfg(all(feature = "pem", feature = "serde"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pem", feature = "serde"))))]
impl<'de> Deserialize<'de> for VerifyingKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
ecdsa_core::VerifyingKey::<Secp256k1>::deserialize(deserializer).map(Into::into)
}
}

#[cfg(test)]
mod tests {
use super::VerifyingKey;
Expand Down