summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openpgp/NEWS1
-rw-r--r--openpgp/src/types/mod.rs22
2 files changed, 23 insertions, 0 deletions
diff --git a/openpgp/NEWS b/openpgp/NEWS
index 77998d4e..967ee426 100644
--- a/openpgp/NEWS
+++ b/openpgp/NEWS
@@ -6,6 +6,7 @@
** New functionality
- AEADAlgorithm::nonce_size replaces AEADAlgorithm::iv_size
- crypto::backend
+ - Curve::field_size
- MPI::is_zero
- MPI::zero
- packet::Any
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs
index e87a0e2c..29775a16 100644
--- a/openpgp/src/types/mod.rs
+++ b/openpgp/src/types/mod.rs
@@ -347,6 +347,28 @@ impl Curve {
Unknown(_) => None,
}
}
+
+ /// Returns the curve's field size in bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// # fn main() -> sequoia_openpgp::Result<()> {
+ /// use sequoia_openpgp as openpgp;
+ /// use openpgp::types::Curve;
+ ///
+ /// assert_eq!(Curve::NistP256.field_size()?, 32);
+ /// assert_eq!(Curve::NistP384.field_size()?, 48);
+ /// assert_eq!(Curve::NistP521.field_size()?, 66);
+ /// assert_eq!(Curve::Ed25519.field_size()?, 32);
+ /// assert!(Curve::Unknown(Box::new([0x2B, 0x11])).field_size().is_err());
+ /// # Ok(()) }
+ /// ```
+ pub fn field_size(&self) -> Result<usize> {
+ self.bits()
+ .map(|bits| (bits + 7) / 8)
+ .ok_or_else(|| Error::UnsupportedEllipticCurve(self.clone()).into())
+ }
}
impl fmt::Display for Curve {