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
9 changes: 8 additions & 1 deletion elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
extern crate std;

pub mod error;
pub mod oid;
pub mod ops;
pub mod secret_key;

Expand All @@ -30,7 +31,7 @@ pub mod secret_key;
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
pub mod weierstrass;

pub use self::{error::Error, secret_key::SecretKey};
pub use self::{error::Error, oid::ObjectIdentifier, secret_key::SecretKey};
pub use generic_array::{self, typenum::consts};
pub use subtle;

Expand Down Expand Up @@ -72,6 +73,12 @@ pub trait Arithmetic: Curve {
type AffinePoint: ConditionallySelectable + ops::MulBase<Scalar = Self::Scalar>;
}

/// Associate an object identifier (OID) with a curve
pub trait Identifier: Curve {
/// Object Identifier (OID) for this curve
const OID: ObjectIdentifier;
}

/// Randomly generate a value.
///
/// Primarily intended for use with scalar types for a particular curve.
Expand Down
48 changes: 48 additions & 0 deletions elliptic-curve/src/oid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Object identifiers (OIDs)

use core::fmt;

/// Object identifier (OID)
pub struct ObjectIdentifier(&'static [u32]);

impl ObjectIdentifier {
/// Create a new OID
pub const fn new(nodes: &'static [u32]) -> Self {
// TODO(tarcieri): validate nodes
Self(nodes)
}
}

impl AsRef<[u32]> for ObjectIdentifier {
fn as_ref(&self) -> &[u32] {
self.0
}
}

impl fmt::Display for ObjectIdentifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, node) in self.0.iter().enumerate() {
write!(f, "{}", node)?;

if i < self.0.len() - 1 {
write!(f, ".")?;
}
}

Ok(())
}
}

#[cfg(all(test, std))]
mod tests {
use super::ObjectIdentifier;
use std::string::ToString;

const EXAMPLE_OID: ObjectIdentifier = ObjectIdentifier::new(&[1, 2, 840, 10045, 3, 1, 7]);

#[test]
fn display_test() {
let oid = EXAMPLE_OID.to_string();
assert_eq!(oid, "1.2.840.10045.3.1.7");
}
}