Skip to content

Commit

Permalink
Bump elliptic-curve to 0.7, k256 to 0.6 and ecdsa to 0.9, and adjust …
Browse files Browse the repository at this point in the history
…API usage
  • Loading branch information
fjarri committed Dec 13, 2020
1 parent 9203d9d commit a168e32
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
6 changes: 3 additions & 3 deletions umbral-pre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"
categories = ["cryptography", "no-std"]

[dependencies]
k256 = { version = "0.5", default-features = false, features = ["ecdsa", "arithmetic"] }
k256 = { version = "0.6", default-features = false, features = ["ecdsa", "arithmetic"] }
blake2 = "0.9"
sha3 = "0.9"
sha2 = "0.9"
Expand All @@ -19,11 +19,11 @@ hkdf = "0.10"

# These packages are among the dependencies of the packages above.
# Their versions should be updated when the main packages above are updated.
elliptic-curve = { version = "0.6", default-features = false, features = ["digest"] }
elliptic-curve = { version = "0.7", default-features = false, features = ["digest"] }
digest = "0.9"
generic-array = "0.14"
aead = { version = "0.3", features = ["heapless"] }
ecdsa = "0.8"
ecdsa = "0.9"
signature = "1.2"
rand_core = { version = "0.5", default-features = false, features = ["getrandom"] }
typenum = "1.12"
Expand Down
41 changes: 14 additions & 27 deletions umbral-pre/src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use core::default::Default;
use core::ops::{Add, Mul, Sub};
use digest::{BlockInput, Digest, FixedOutput, Reset, Update};
use ecdsa::{
SecretKey as BackendSecretKey, Signature as BackendSignature, SignatureSize, SigningKey,
VerifyKey,
};
use ecdsa::{Signature as BackendSignature, SignatureSize, SigningKey, VerifyingKey};
use elliptic_curve::ff::PrimeField;
use elliptic_curve::scalar::NonZeroScalar;
use elliptic_curve::sec1::{CompressedPointSize, EncodedPoint, FromEncodedPoint, ToEncodedPoint};
use elliptic_curve::{Curve, FromDigest, ProjectiveArithmetic, Scalar};
use elliptic_curve::{
Curve, FromDigest, ProjectiveArithmetic, PublicKey as BackendPublicKey, Scalar,
SecretKey as BackendSecretKey,
};
use generic_array::typenum::U32;
use generic_array::GenericArray;
use k256::Secp256k1;
Expand Down Expand Up @@ -151,7 +151,7 @@ impl SerializableToArray for CurvePoint {

fn from_array(arr: &GenericArray<u8, Self::Size>) -> Option<Self> {
let ep = EncodedPoint::<CurveType>::from_bytes(arr.as_slice()).ok()?;
let cp_opt: Option<BackendPoint> = BackendPoint::from_encoded_point(&ep).into();
let cp_opt: Option<BackendPoint> = BackendPoint::from_encoded_point(&ep);
cp_opt.map(Self)
}
}
Expand Down Expand Up @@ -206,7 +206,7 @@ impl SecretKey {
&self,
digest: impl BlockInput + FixedOutput<OutputSize = U32> + Clone + Default + Reset + Update,
) -> Signature {
let signer = SigningKey::<CurveType>::from(&self.0);
let signer = SigningKey::<CurveType>::from(self.0.clone());
Signature(signer.sign_digest_with_rng(OsRng, digest))
}
}
Expand All @@ -227,21 +227,17 @@ impl SerializableToArray for SecretKey {

/// A public key.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PublicKey(EncodedPoint<CurveType>);
pub struct PublicKey(BackendPublicKey<CurveType>);

impl PublicKey {
/// Creates a public key from a secret key.
pub fn from_secret_key(secret_key: &SecretKey) -> Self {
Self(EncodedPoint::from_secret_key(&secret_key.0, true))
Self(secret_key.0.public_key())
}

/// Returns the underlying curve point of the public key.
pub(crate) fn to_point(&self) -> CurvePoint {
// TODO: there's currently no way to get the point
// of a known valid public key without `unwrap()`.
// If there's a panic here, something is wrong with the backend ECC crate.
// Should be fixable with `elliptic_curve=0.6`
CurvePoint(BackendPoint::from_encoded_point(&self.0).unwrap())
CurvePoint(self.0.to_projective())
}

/// Verifies the signature.
Expand All @@ -250,11 +246,7 @@ impl PublicKey {
digest: impl Digest<OutputSize = U32>,
signature: &Signature,
) -> bool {
// TODO: there's currently no way to create a verifier
// from a known valid public key without `unwrap()`.
// If there's a panic here, something is wrong with the backend ECC crate.
// Should be fixable with `elliptic_curve=0.6`
let verifier = VerifyKey::from_encoded_point(&self.0).unwrap();
let verifier = VerifyingKey::from(&self.0);
verifier.verify_digest(digest, &signature.0).is_ok()
}
}
Expand All @@ -263,17 +255,12 @@ impl SerializableToArray for PublicKey {
type Size = <CurvePoint as SerializableToArray>::Size;

fn to_array(&self) -> GenericArray<u8, Self::Size> {
// EncodedPoint can be compressed or uncompressed,
// so `to_bytes()` does not have a compile-time size,
// and we have to do this conversion
// (we know that in our case it is always compressed).
*GenericArray::<u8, Self::Size>::from_slice(self.0.as_bytes())
self.to_point().to_array()
}

fn from_array(arr: &GenericArray<u8, Self::Size>) -> Option<Self> {
EncodedPoint::<CurveType>::from_bytes(arr.as_slice())
.ok()
.map(Self)
CurvePoint::from_array(&arr)
.map(|cp| Self(BackendPublicKey::<CurveType>::from_affine(cp.0.to_affine())))
}
}

Expand Down

0 comments on commit a168e32

Please sign in to comment.