Skip to content

Commit f799e52

Browse files
committed
fixup scalars
1 parent 2a8b634 commit f799e52

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

x448/src/lib.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#![no_std]
22

33
use ed448_goldilocks::{
4-
MontgomeryPoint,
4+
MontgomeryScalar, MontgomeryXpoint,
55
elliptic_curve::{bigint::U448, scalar::FromUintUnchecked},
66
};
77
use rand_core::{CryptoRng, RngCore};
88
use zeroize::Zeroize;
99

10-
type MontgomeryScalar = ed448_goldilocks::Scalar<ed448_goldilocks::Ed448>;
11-
1210
/// Computes a Scalar according to RFC7748
1311
/// given a byte array of length 56
1412
impl From<[u8; 56]> for Secret {
@@ -25,14 +23,14 @@ impl From<[u8; 56]> for Secret {
2523
impl From<&Secret> for PublicKey {
2624
fn from(secret: &Secret) -> PublicKey {
2725
let secret = secret.as_scalar();
28-
let point = &MontgomeryPoint::GENERATOR * &secret;
29-
PublicKey(point)
26+
let point = &MontgomeryXpoint::GENERATOR * &secret;
27+
PublicKey(point.to_affine())
3028
}
3129
}
3230

3331
/// A PublicKey is a point on Curve448.
3432
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
35-
pub struct PublicKey(MontgomeryPoint);
33+
pub struct PublicKey(MontgomeryXpoint);
3634

3735
/// A Secret is a Scalar on Curve448.
3836
#[derive(Clone, Zeroize)]
@@ -43,7 +41,7 @@ pub struct Secret([u8; 56]);
4341
/// This point is the result of a Diffie-Hellman key exchange.
4442
#[derive(Zeroize)]
4543
#[zeroize(drop)]
46-
pub struct SharedSecret(MontgomeryPoint);
44+
pub struct SharedSecret(MontgomeryXpoint);
4745

4846
impl PublicKey {
4947
/// Converts a bytes slice into a Public key
@@ -68,7 +66,7 @@ impl PublicKey {
6866

6967
// Check if the point has low order
7068
let arr = slice_to_array(bytes);
71-
let point = MontgomeryPoint(arr);
69+
let point = MontgomeryXpoint(arr);
7270

7371
Some(PublicKey(point))
7472
}
@@ -119,7 +117,7 @@ impl Secret {
119117
return None;
120118
}
121119
let shared_key = &public_key.0 * &self.as_scalar();
122-
Some(SharedSecret(shared_key))
120+
Some(SharedSecret(shared_key.to_affine()))
123121
}
124122

125123
/// Performs a Diffie-hellman key exchange once between the secret key and an external public key
@@ -159,17 +157,17 @@ fn slice_to_array(bytes: &[u8]) -> [u8; 56] {
159157
pub fn x448(scalar_bytes: [u8; 56], point_bytes: [u8; 56]) -> Option<[u8; 56]> {
160158
let point = PublicKey::from_bytes(&point_bytes)?;
161159
let scalar = Secret::from(scalar_bytes).as_scalar();
162-
Some((&point.0 * &scalar).0)
160+
Some((&point.0 * &scalar).to_affine().0)
163161
}
164162
/// An unchecked version of the x448 function defined in RFC448
165163
/// No checks are made on the points.
166164
pub fn x448_unchecked(scalar_bytes: [u8; 56], point_bytes: [u8; 56]) -> [u8; 56] {
167-
let point = MontgomeryPoint(point_bytes);
165+
let point = MontgomeryXpoint(point_bytes);
168166
let scalar = Secret::from(scalar_bytes).as_scalar();
169-
(&point * &scalar).0
167+
(&point * &scalar).to_affine().0
170168
}
171169

172-
pub const X448_BASEPOINT_BYTES: [u8; 56] = MontgomeryPoint::GENERATOR.0;
170+
pub const X448_BASEPOINT_BYTES: [u8; 56] = MontgomeryXpoint::GENERATOR.0;
173171

174172
#[cfg(test)]
175173
mod test {
@@ -182,16 +180,16 @@ mod test {
182180
fn test_low_order() {
183181
// Notice, that this is the only way to add low order points into the system
184182
// and this is not exposed to the user. The user will use `from_bytes` which will check for low order points.
185-
let bad_key_a = PublicKey(MontgomeryPoint::LOW_A);
186-
let checked_bad_key_a = PublicKey::from_bytes(&MontgomeryPoint::LOW_A.0);
183+
let bad_key_a = PublicKey(MontgomeryXpoint::LOW_A);
184+
let checked_bad_key_a = PublicKey::from_bytes(&MontgomeryXpoint::LOW_A.0);
187185
assert!(checked_bad_key_a.is_none());
188186

189-
let bad_key_b = PublicKey(MontgomeryPoint::LOW_B);
190-
let checked_bad_key_b = PublicKey::from_bytes(&MontgomeryPoint::LOW_B.0);
187+
let bad_key_b = PublicKey(MontgomeryXpoint::LOW_B);
188+
let checked_bad_key_b = PublicKey::from_bytes(&MontgomeryXpoint::LOW_B.0);
191189
assert!(checked_bad_key_b.is_none());
192190

193-
let bad_key_c = PublicKey(MontgomeryPoint::LOW_C);
194-
let checked_bad_key_c = PublicKey::from_bytes(&MontgomeryPoint::LOW_C.0);
191+
let bad_key_c = PublicKey(MontgomeryXpoint::LOW_C);
192+
let checked_bad_key_c = PublicKey::from_bytes(&MontgomeryXpoint::LOW_C.0);
195193
assert!(checked_bad_key_c.is_none());
196194

197195
let mut rng = rand::rng();
@@ -379,8 +377,8 @@ mod test {
379377
0xda, 0x8d, 0x52, 0x4d, 0xe3, 0xd6, 0x9b, 0xd9, 0xd9, 0xd6, 0x6b, 0x99, 0x7e, 0x37,
380378
];
381379

382-
let mut point = MontgomeryPoint::GENERATOR.0;
383-
let mut scalar = MontgomeryPoint::GENERATOR.0;
380+
let mut point = MontgomeryXpoint::GENERATOR.0;
381+
let mut scalar = MontgomeryXpoint::GENERATOR.0;
384382
let mut result = [0u8; 56];
385383

386384
// Iterate 1 time then check value on 1st iteration

0 commit comments

Comments
 (0)