From db2331d9124c2a284a5ea6e381f455816de8e38b Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Fri, 17 Jan 2025 12:06:10 +0100 Subject: [PATCH 1/2] Add curve-specific aliases Signed-off-by: Denis Varlakov --- generic-ec/src/lib.rs | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/generic-ec/src/lib.rs b/generic-ec/src/lib.rs index 893685a..323d401 100644 --- a/generic-ec/src/lib.rs +++ b/generic-ec/src/lib.rs @@ -253,4 +253,80 @@ pub mod curves { #[cfg(feature = "curve-stark")] #[cfg_attr(docsrs, doc(cfg(feature = "curve-stark")))] pub use generic_ec_curves::Stark; + + macro_rules! create_aliases { + ($(#[$attr:meta] $mod:ident: $curve:ident),+$(,)?) => {$( + /// Aliases for [` + #[doc = stringify!($curve)] + /// `] curve + /// + /// This module provides type aliases to [`Point`](crate::Point), [`Scalar`](crate::Scalar), and other types + /// instantiated with [` + #[doc = stringify!($curve)] + /// `]. It might be convenient to use this module when you don't need your code to be generic over choice + /// of curve. + /// + /// ## Example + /// The code below only works with [` + #[doc = stringify!($curve)] + /// `] curve. By using type aliases from + #[doc = concat!("[`generic_ec::curves::", stringify!($mod), "`](", stringify!($mod), ")")] + /// , we never need to deal with generic parameters. + /// + /// ```rust + #[doc = concat!("use generic_ec::curves::", stringify!($mod), "::{Point, SecretScalar};")] + /// + /// let mut rng = rand::rngs::OsRng; + /// let secret_key = SecretScalar::random(&mut rng); + /// let public_key = Point::generator() * &secret_key; + /// // ... + /// ``` + #[$attr] + pub mod $mod { + /// Alias for + #[doc = concat!("[", stringify!($curve), "](super::", stringify!($curve), ")")] + /// curve + pub type E = super::$curve; + /// Point on [` + #[doc = stringify!($curve)] + /// `](E) curve + pub type Point = crate::Point; + /// Scalar in [` + #[doc = stringify!($curve)] + /// `](E) curve large prime subgroup + pub type Scalar = crate::Scalar; + /// Secret scalar in [` + #[doc = stringify!($curve)] + /// `](E) curve large prime subgroup + pub type SecretScalar = crate::SecretScalar; + /// Point on [` + #[doc = stringify!($curve)] + /// `](E) curve encoded as bytes + pub type EncodedPoint = crate::EncodedPoint; + /// Scalar in [` + #[doc = stringify!($curve)] + /// `](E) curve large prime subgroup encoded as bytes + pub type EncodedScalar = crate::EncodedScalar; + /// Iterator over scalar coefficients in radix 16 representation of [` + #[doc = stringify!($curve)] + /// `](E) curve + pub type Radix16Iter = crate::Radix16Iter; + /// Generator of [` + #[doc = stringify!($curve)] + /// `](E) curve + pub type Generator = crate::Generator; + } + )+}; + } + + create_aliases! { + #[cfg(feature = "curve-secp256k1")] + secp256k1: Secp256k1, + #[cfg(feature = "curve-secp256r1")] + secp256r1: Secp256r1, + #[cfg(feature = "curve-stark")] + stark: Stark, + #[cfg(feature = "curve-ed25519")] + ed25519: Ed25519, + } } From e66846f5f21c19233aeecdcbe82862ce589a0051 Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Fri, 17 Jan 2025 12:07:49 +0100 Subject: [PATCH 2/2] Bump version & update changelog Signed-off-by: Denis Varlakov --- Cargo.lock | 2 +- generic-ec/CHANGELOG.md | 5 +++++ generic-ec/Cargo.toml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fabc52b..7ed0410 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -704,7 +704,7 @@ dependencies = [ [[package]] name = "generic-ec" -version = "0.4.4" +version = "0.4.5" dependencies = [ "curve25519-dalek", "digest", diff --git a/generic-ec/CHANGELOG.md b/generic-ec/CHANGELOG.md index c6e28f3..cce959f 100644 --- a/generic-ec/CHANGELOG.md +++ b/generic-ec/CHANGELOG.md @@ -1,3 +1,8 @@ +## v0.4.5 +* Add curve-specific aliases (see `generic_ec::curves::{secp256k1, secp256r1, stark, ed25519}::*`) [#52] + +[#52]: https://github.com/LFDT-Lockness/generic-ec/pull/52 + ## v0.4.4 * Fix double header menu issue on docs.rs [#49] diff --git a/generic-ec/Cargo.toml b/generic-ec/Cargo.toml index a9630b7..b9d512c 100644 --- a/generic-ec/Cargo.toml +++ b/generic-ec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "generic-ec" -version = "0.4.4" +version = "0.4.5" edition = "2021" license = "MIT OR Apache-2.0" repository = "https://github.com/LFDT-Lockness/generic-ec"