Skip to content

Commit f44963a

Browse files
authored
Add NonIdentity::mul_by_generator() (#1833)
This PR adds `NonIdentity::mul_by_generator()`, which is similar to the `MulByGenerator` trait, but returns a `NonIdentity` instead of a `ProjectivePoint`. This is quite useful for getting the public key from a `NonZeroScalar` without having to go through the whole conversion dance.
1 parent 1681db5 commit f44963a

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

elliptic-curve/src/point/non_identity.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use core::ops::{Deref, Mul};
44

5-
use group::{Curve, GroupEncoding, prime::PrimeCurveAffine};
5+
use group::{Curve, Group, GroupEncoding, prime::PrimeCurveAffine};
66
use rand_core::CryptoRng;
77
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
88

@@ -73,6 +73,16 @@ where
7373
point: self.point.to_affine(),
7474
}
7575
}
76+
77+
/// Multiply by the generator of the prime-order subgroup.
78+
pub fn mul_by_generator<C: CurveArithmetic>(scalar: &NonZeroScalar<C>) -> Self
79+
where
80+
P: Group<Scalar = C::Scalar>,
81+
{
82+
Self {
83+
point: P::mul_by_generator(scalar),
84+
}
85+
}
7686
}
7787

7888
impl<P> NonIdentity<P>
@@ -195,7 +205,7 @@ where
195205
}
196206
}
197207

198-
impl<P: group::Group> Zeroize for NonIdentity<P> {
208+
impl<P: Group> Zeroize for NonIdentity<P> {
199209
fn zeroize(&mut self) {
200210
self.point = P::generator();
201211
}
@@ -204,7 +214,7 @@ impl<P: group::Group> Zeroize for NonIdentity<P> {
204214
#[cfg(all(test, feature = "dev"))]
205215
mod tests {
206216
use super::NonIdentity;
207-
use crate::dev::{AffinePoint, ProjectivePoint};
217+
use crate::dev::{AffinePoint, NonZeroScalar, ProjectivePoint, SecretKey};
208218
use group::GroupEncoding;
209219
use hex_literal::hex;
210220
use zeroize::Zeroize;
@@ -255,4 +265,18 @@ mod tests {
255265

256266
assert_eq!(point.to_point(), ProjectivePoint::Generator);
257267
}
268+
269+
#[test]
270+
fn mul_by_generator() {
271+
let scalar = NonZeroScalar::from_repr(
272+
hex!("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721").into(),
273+
)
274+
.unwrap();
275+
let point = NonIdentity::<ProjectivePoint>::mul_by_generator(&scalar);
276+
277+
let sk = SecretKey::from(scalar);
278+
let pk = sk.public_key();
279+
280+
assert_eq!(point.to_point(), pk.to_projective());
281+
}
258282
}

0 commit comments

Comments
 (0)