Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

Commit

Permalink
Remove redundant "namespacing" from type names
Browse files Browse the repository at this point in the history
* `Ed25519PublicKey` => `ed25519::PublicKey`
* `Ed25519Signature` => `ed25519::Signature`
* `EcdsaPublicKey` => `ecdsa::PublicKey`
* `EcdsaSignature` => `ecdsa::Signature`

These names were prefixed with the signature type previously to avoid
clashes with traits of the same name (i.e. `signatory::PublicKey` and
`signatory::Signature`) however the namespace clash can also be avoided
by using the `ed25519` and `ecdsa` module names.

This is considered a Rust best practice:

rust-lang/rfcs#356
  • Loading branch information
tony-iqlusion committed Oct 7, 2018
1 parent 5ff4c73 commit 35fd4ea
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 195 deletions.
14 changes: 5 additions & 9 deletions providers/signatory-dalek/benches/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@ extern crate signatory;
extern crate signatory_dalek;

use criterion::Criterion;
use signatory::{
ed25519::{Ed25519PublicKey, Ed25519Signature, Seed, TEST_VECTORS},
test_vector::TestVector,
Signature, Verifier,
};
use signatory::{ed25519, test_vector::TestVector, Signature, Verifier};
use signatory_dalek::{Ed25519Signer, Ed25519Verifier};

/// Test vector to use for benchmarking
const TEST_VECTOR: &TestVector = &TEST_VECTORS[4];
const TEST_VECTOR: &TestVector = &ed25519::TEST_VECTORS[4];

fn sign_ed25519(c: &mut Criterion) {
let signer = Ed25519Signer::from(&Seed::from_bytes(TEST_VECTOR.sk).unwrap());
let signer = Ed25519Signer::from(&ed25519::Seed::from_bytes(TEST_VECTOR.sk).unwrap());

c.bench_function("dalek: Ed25519 signer", move |b| {
b.iter(|| signatory::sign(&signer, TEST_VECTOR.msg).unwrap())
});
}

fn verify_ed25519(c: &mut Criterion) {
let verifier = Ed25519Verifier::from(&Ed25519PublicKey::from_bytes(TEST_VECTOR.pk).unwrap());
let signature = Ed25519Signature::from_bytes(TEST_VECTOR.sig).unwrap();
let verifier = Ed25519Verifier::from(&ed25519::PublicKey::from_bytes(TEST_VECTOR.pk).unwrap());
let signature = ed25519::Signature::from_bytes(TEST_VECTOR.sig).unwrap();

c.bench_function("dalek: Ed25519 verifier", move |b| {
b.iter(|| verifier.verify(TEST_VECTOR.msg, &signature).unwrap())
Expand Down
55 changes: 27 additions & 28 deletions providers/signatory-dalek/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,62 @@ use ed25519_dalek::{Keypair, SecretKey};
use sha2::Sha512;

use signatory::{
ed25519,
error::{Error, ErrorKind},
generic_array::typenum::U64,
DigestSigner, DigestVerifier, Ed25519PublicKey, Ed25519Seed, Ed25519Signature, PublicKeyed,
Signature, Signer, Verifier,
DigestSigner, DigestVerifier, PublicKeyed, Signature, Signer, Verifier,
};

/// Ed25519 signature provider for ed25519-dalek
pub struct Ed25519Signer(Keypair);

impl<'a> From<&'a Ed25519Seed> for Ed25519Signer {
impl<'a> From<&'a ed25519::Seed> for Ed25519Signer {
/// Create a new DalekSigner from an unexpanded seed value
fn from(seed: &'a Ed25519Seed) -> Self {
fn from(seed: &'a ed25519::Seed) -> Self {
Ed25519Signer(keypair_from_seed(seed))
}
}

impl PublicKeyed<Ed25519PublicKey> for Ed25519Signer {
fn public_key(&self) -> Result<Ed25519PublicKey, Error> {
Ok(Ed25519PublicKey::from_bytes(self.0.public.as_bytes()).unwrap())
impl PublicKeyed<ed25519::PublicKey> for Ed25519Signer {
fn public_key(&self) -> Result<ed25519::PublicKey, Error> {
Ok(ed25519::PublicKey::from_bytes(self.0.public.as_bytes()).unwrap())
}
}

impl Signer<Ed25519Signature> for Ed25519Signer {
fn sign(&self, msg: &[u8]) -> Result<Ed25519Signature, Error> {
impl Signer<ed25519::Signature> for Ed25519Signer {
fn sign(&self, msg: &[u8]) -> Result<ed25519::Signature, Error> {
let signature = self.0.sign::<Sha512>(msg).to_bytes();
Ok(Ed25519Signature::from_bytes(&signature[..]).unwrap())
Ok(Signature::from_bytes(&signature[..]).unwrap())
}
}

/// Ed25519ph (i.e. pre-hashed) signature provider for ed25519-dalek
pub struct Ed25519PhSigner(Keypair);

impl<'a> From<&'a Ed25519Seed> for Ed25519PhSigner {
impl<'a> From<&'a ed25519::Seed> for Ed25519PhSigner {
/// Create a new DalekSigner from an unexpanded seed value
fn from(seed: &'a Ed25519Seed) -> Self {
fn from(seed: &'a ed25519::Seed) -> Self {
Ed25519PhSigner(keypair_from_seed(seed))
}
}

impl PublicKeyed<Ed25519PublicKey> for Ed25519PhSigner {
fn public_key(&self) -> Result<Ed25519PublicKey, Error> {
Ok(Ed25519PublicKey::from_bytes(self.0.public.as_bytes()).unwrap())
impl PublicKeyed<ed25519::PublicKey> for Ed25519PhSigner {
fn public_key(&self) -> Result<ed25519::PublicKey, Error> {
Ok(ed25519::PublicKey::from_bytes(self.0.public.as_bytes()).unwrap())
}
}

// TODO: tests!
impl<D> DigestSigner<D, Ed25519Signature> for Ed25519PhSigner
impl<D> DigestSigner<D, ed25519::Signature> for Ed25519PhSigner
where
D: Digest<OutputSize = U64> + Default,
{
fn sign(&self, digest: D) -> Result<Ed25519Signature, Error> {
fn sign(&self, digest: D) -> Result<ed25519::Signature, Error> {
// TODO: context support
let context: Option<&'static [u8]> = None;

let signature =
Ed25519Signature::from_bytes(&self.0.sign_prehashed(digest, context).to_bytes()[..])
.unwrap();
Signature::from_bytes(&self.0.sign_prehashed(digest, context).to_bytes()[..]).unwrap();

Ok(signature)
}
Expand All @@ -87,14 +86,14 @@ where
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ed25519Verifier(ed25519_dalek::PublicKey);

impl<'a> From<&'a Ed25519PublicKey> for Ed25519Verifier {
fn from(public_key: &'a Ed25519PublicKey) -> Self {
impl<'a> From<&'a ed25519::PublicKey> for Ed25519Verifier {
fn from(public_key: &'a ed25519::PublicKey) -> Self {
Ed25519Verifier(ed25519_dalek::PublicKey::from_bytes(public_key.as_ref()).unwrap())
}
}

impl Verifier<Ed25519Signature> for Ed25519Verifier {
fn verify(&self, msg: &[u8], sig: &Ed25519Signature) -> Result<(), Error> {
impl Verifier<ed25519::Signature> for Ed25519Verifier {
fn verify(&self, msg: &[u8], sig: &ed25519::Signature) -> Result<(), Error> {
let dalek_sig = ed25519_dalek::Signature::from_bytes(sig.as_ref()).unwrap();
self.0
.verify::<Sha512>(msg, &dalek_sig)
Expand All @@ -106,18 +105,18 @@ impl Verifier<Ed25519Signature> for Ed25519Verifier {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ed25519PhVerifier(ed25519_dalek::PublicKey);

impl<'a> From<&'a Ed25519PublicKey> for Ed25519PhVerifier {
fn from(public_key: &'a Ed25519PublicKey) -> Self {
impl<'a> From<&'a ed25519::PublicKey> for Ed25519PhVerifier {
fn from(public_key: &'a ed25519::PublicKey) -> Self {
Ed25519PhVerifier(ed25519_dalek::PublicKey::from_bytes(public_key.as_ref()).unwrap())
}
}

// TODO: tests!
impl<D> DigestVerifier<D, Ed25519Signature> for Ed25519PhVerifier
impl<D> DigestVerifier<D, ed25519::Signature> for Ed25519PhVerifier
where
D: Digest<OutputSize = U64> + Default,
{
fn verify(&self, digest: D, sig: &Ed25519Signature) -> Result<(), Error> {
fn verify(&self, digest: D, sig: &ed25519::Signature) -> Result<(), Error> {
// TODO: context support
let context: Option<&'static [u8]> = None;
let dalek_sig = ed25519_dalek::Signature::from_bytes(sig.as_ref()).unwrap();
Expand All @@ -128,7 +127,7 @@ where
}

/// Convert a Signatory seed into a Dalek keypair
fn keypair_from_seed(seed: &Ed25519Seed) -> Keypair {
fn keypair_from_seed(seed: &ed25519::Seed) -> Keypair {
let secret = SecretKey::from_bytes(seed.as_secret_slice()).unwrap();
let public = ed25519_dalek::PublicKey::from_secret::<Sha512>(&secret);
Keypair { secret, public }
Expand Down
15 changes: 7 additions & 8 deletions providers/signatory-ledger-cosval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use std::sync::Arc;
use std::sync::Mutex;

use signatory::{
ed25519::Ed25519PublicKey,
ed25519::Ed25519Signature,
ed25519::{PublicKey, Signature},
error::{Error, ErrorKind},
PublicKeyed, Signer,
};
Expand All @@ -43,25 +42,25 @@ impl Ed25519CosmosAppSigner {
}
}

impl PublicKeyed<Ed25519PublicKey> for Ed25519CosmosAppSigner {
impl PublicKeyed<PublicKey> for Ed25519CosmosAppSigner {
/// Returns the public key that corresponds cosmos validator app connected to this signer
fn public_key(&self) -> Result<Ed25519PublicKey, Error> {
fn public_key(&self) -> Result<PublicKey, Error> {
let app = self.app.lock().unwrap();

match app.public_key() {
Ok(pk) => Ok(Ed25519PublicKey(pk)),
Ok(pk) => Ok(PublicKey(pk)),
Err(err) => Err(Error::new(ErrorKind::ProviderError, Some(&err.to_string()))),
}
}
}

impl Signer<Ed25519Signature> for Ed25519CosmosAppSigner {
impl Signer<Signature> for Ed25519CosmosAppSigner {
/// c: Compute a compact, fixed-sized signature of the given amino/json vote
fn sign(&self, msg: &[u8]) -> Result<Ed25519Signature, Error> {
fn sign(&self, msg: &[u8]) -> Result<Signature, Error> {
let app = self.app.lock().unwrap();

match app.sign(&msg) {
Ok(sig) => Ok(Ed25519Signature(sig)),
Ok(sig) => Ok(Signature(sig)),
Err(err) => Err(Error::new(ErrorKind::ProviderError, Some(&err.to_string()))),
}
}
Expand Down
8 changes: 4 additions & 4 deletions providers/signatory-ring/benches/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate signatory_ring;
use criterion::Criterion;
use signatory::{
curve::nistp256::{self, FixedSignature},
ecdsa::EcdsaPublicKey,
ecdsa::PublicKey,
encoding::FromPkcs8,
generic_array::GenericArray,
test_vector::TestVector,
Expand All @@ -31,9 +31,9 @@ fn sign_ecdsa_p256(c: &mut Criterion) {

fn verify_ecdsa_p256(c: &mut Criterion) {
let signature = FixedSignature::from_bytes(TEST_VECTOR.sig).unwrap();
let verifier = P256Verifier::from(&EcdsaPublicKey::from_untagged_point(
GenericArray::from_slice(TEST_VECTOR.pk),
));
let verifier = P256Verifier::from(&PublicKey::from_untagged_point(GenericArray::from_slice(
TEST_VECTOR.pk,
)));

c.bench_function("ring: ECDSA (nistp256) verifier", move |b| {
b.iter(|| verifier.verify_sha256(TEST_VECTOR.msg, &signature).unwrap())
Expand Down
13 changes: 5 additions & 8 deletions providers/signatory-ring/benches/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@ extern crate signatory;
extern crate signatory_ring;

use criterion::Criterion;
use signatory::{
ed25519::TEST_VECTORS, test_vector::TestVector, Ed25519PublicKey, Ed25519Seed,
Ed25519Signature, Signature, Verifier,
};
use signatory::{ed25519, test_vector::TestVector, Signature, Verifier};
use signatory_ring::ed25519::{Ed25519Signer, Ed25519Verifier};

/// Test vector to use for benchmarking
const TEST_VECTOR: &TestVector = &TEST_VECTORS[4];
const TEST_VECTOR: &TestVector = &ed25519::TEST_VECTORS[4];

fn sign_ed25519(c: &mut Criterion) {
let signer = Ed25519Signer::from(&Ed25519Seed::from_bytes(TEST_VECTOR.sk).unwrap());
let signer = Ed25519Signer::from(&ed25519::Seed::from_bytes(TEST_VECTOR.sk).unwrap());

c.bench_function("ring: Ed25519 signer", move |b| {
b.iter(|| signatory::sign(&signer, TEST_VECTOR.msg).unwrap())
});
}

fn verify_ed25519(c: &mut Criterion) {
let signature = Ed25519Signature::from_bytes(TEST_VECTOR.sig).unwrap();
let verifier = Ed25519Verifier::from(&Ed25519PublicKey::from_bytes(TEST_VECTOR.pk).unwrap());
let signature = ed25519::Signature::from_bytes(TEST_VECTOR.sig).unwrap();
let verifier = Ed25519Verifier::from(&ed25519::PublicKey::from_bytes(TEST_VECTOR.pk).unwrap());

c.bench_function("ring: Ed25519 verifier", move |b| {
b.iter(|| verifier.verify(TEST_VECTOR.msg, &signature).unwrap())
Expand Down
14 changes: 7 additions & 7 deletions providers/signatory-ring/src/ecdsa/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ring::{rand::SystemRandom, signature::ECDSAKeyPair};
use signatory::encoding::pkcs8::{self, GeneratePkcs8};
use signatory::{
curve::nistp256::NistP256,
ecdsa::{Asn1Signature, EcdsaPublicKey, EcdsaSignature, FixedSignature},
ecdsa::{Asn1Signature, FixedSignature, PublicKey, Signature},
encoding::pkcs8::FromPkcs8,
error::{Error, ErrorKind::SignatureInvalid},
PublicKeyed, Sha256Signer, Sha256Verifier,
Expand Down Expand Up @@ -61,11 +61,11 @@ impl GeneratePkcs8 for P256Signer<FixedSignature<NistP256>> {
}
}

impl<S> PublicKeyed<EcdsaPublicKey<NistP256>> for P256Signer<S>
impl<S> PublicKeyed<PublicKey<NistP256>> for P256Signer<S>
where
S: EcdsaSignature + Send + Sync,
S: Signature + Send + Sync,
{
fn public_key(&self) -> Result<EcdsaPublicKey<NistP256>, Error> {
fn public_key(&self) -> Result<PublicKey<NistP256>, Error> {
Ok(self.public_key.clone())
}
}
Expand All @@ -84,10 +84,10 @@ impl Sha256Signer<FixedSignature<NistP256>> for P256Signer<FixedSignature<NistP2

/// NIST P-256 ECDSA verifier
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct P256Verifier(EcdsaPublicKey<NistP256>);
pub struct P256Verifier(PublicKey<NistP256>);

impl<'a> From<&'a EcdsaPublicKey<NistP256>> for P256Verifier {
fn from(public_key: &'a EcdsaPublicKey<NistP256>) -> Self {
impl<'a> From<&'a PublicKey<NistP256>> for P256Verifier {
fn from(public_key: &'a PublicKey<NistP256>) -> Self {
P256Verifier(public_key.clone())
}
}
Expand Down
14 changes: 7 additions & 7 deletions providers/signatory-ring/src/ecdsa/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ring::{rand::SystemRandom, signature::ECDSAKeyPair};
use signatory::encoding::pkcs8::{self, GeneratePkcs8};
use signatory::{
curve::nistp384::NistP384,
ecdsa::{Asn1Signature, EcdsaPublicKey, EcdsaSignature, FixedSignature},
ecdsa::{Asn1Signature, FixedSignature, PublicKey, Signature},
encoding::FromPkcs8,
error::{Error, ErrorKind::SignatureInvalid},
PublicKeyed, Sha384Signer, Sha384Verifier,
Expand Down Expand Up @@ -61,12 +61,12 @@ impl GeneratePkcs8 for P384Signer<FixedSignature<NistP384>> {
}
}

impl<S> PublicKeyed<EcdsaPublicKey<NistP384>> for P384Signer<S>
impl<S> PublicKeyed<PublicKey<NistP384>> for P384Signer<S>
where
S: EcdsaSignature + Send + Sync,
S: Signature + Send + Sync,
{
/// Obtain the public key which identifies this signer
fn public_key(&self) -> Result<EcdsaPublicKey<NistP384>, Error> {
fn public_key(&self) -> Result<PublicKey<NistP384>, Error> {
Ok(self.public_key.clone())
}
}
Expand All @@ -85,10 +85,10 @@ impl Sha384Signer<FixedSignature<NistP384>> for P384Signer<FixedSignature<NistP3

/// NIST P-384 ECDSA verifier
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct P384Verifier(EcdsaPublicKey<NistP384>);
pub struct P384Verifier(PublicKey<NistP384>);

impl<'a> From<&'a EcdsaPublicKey<NistP384>> for P384Verifier {
fn from(public_key: &'a EcdsaPublicKey<NistP384>) -> Self {
impl<'a> From<&'a PublicKey<NistP384>> for P384Verifier {
fn from(public_key: &'a PublicKey<NistP384>) -> Self {
P384Verifier(public_key.clone())
}
}
Expand Down
10 changes: 5 additions & 5 deletions providers/signatory-ring/src/ecdsa/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ use ring::{
};
use signatory::{
curve::WeierstrassCurve,
ecdsa::{EcdsaPublicKey, EcdsaSignature},
ecdsa::{PublicKey, Signature},
error::{Error, ErrorKind},
generic_array::{typenum::Unsigned, GenericArray},
};
use untrusted;

/// Generic ECDSA signer which is wrapped with curve and signature-specific types
pub struct EcdsaSigner<C: WeierstrassCurve, S: EcdsaSignature> {
pub struct EcdsaSigner<C: WeierstrassCurve, S: Signature> {
/// *ring* ECDSA keypair
keypair: KeyPair,

/// ECDSA public key for this signer
// *ring* does not presently keep a copy of this.
// See https://github.com/briansmith/ring/issues/672#issuecomment-404669397
pub(super) public_key: EcdsaPublicKey<C>,
pub(super) public_key: PublicKey<C>,

/// Cryptographically secure random number generator
csrng: SystemRandom,
Expand All @@ -34,7 +34,7 @@ pub struct EcdsaSigner<C: WeierstrassCurve, S: EcdsaSignature> {
impl<C, S> EcdsaSigner<C, S>
where
C: WeierstrassCurve,
S: EcdsaSignature,
S: Signature,
{
/// Create an ECDSA signer and public key from a PKCS#8
pub(super) fn new(alg: &'static SigningAlgorithm, pkcs8_bytes: &[u8]) -> Result<Self, Error> {
Expand All @@ -48,7 +48,7 @@ where
.checked_sub(<C as WeierstrassCurve>::UntaggedPointSize::to_usize())
.unwrap();

let public_key = EcdsaPublicKey::from_untagged_point(&GenericArray::from_slice(
let public_key = PublicKey::from_untagged_point(&GenericArray::from_slice(
&pkcs8_bytes[pubkey_bytes_pos..],
));

Expand Down
Loading

0 comments on commit 35fd4ea

Please sign in to comment.