Skip to content

Commit 62fd3e9

Browse files
committed
ed448-goldilocks: reuse types from ed448
1 parent f858173 commit 62fd3e9

6 files changed

Lines changed: 72 additions & 243 deletions

File tree

Cargo.lock

Lines changed: 25 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ members = [
1818

1919
[profile.dev]
2020
opt-level = 2
21-
2221
[patch.crates-io]
2322
primefield = { path = "primefield" }
2423
primeorder = { path = "primeorder" }
24+
25+
ed448 = { git = "https://github.com/RustCrypto/signatures.git", branch = "ed448/drop-signature-suffix" }

ed448-goldilocks/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This crate also includes signing and verifying of Ed448 signatures.
1919
crypto-bigint = { version = "=0.7.0-pre.4", features = ["hybrid-array"], default-features = false }
2020
crypto_signature = { version = "3.0.0-rc.0", default-features = false, features = ["digest", "rand_core"], optional = true, package = "signature" }
2121
elliptic-curve = { version = "0.14.0-rc.3", features = ["arithmetic", "bits", "hash2curve", "jwk", "pkcs8", "pem", "sec1"] }
22+
ed448 = { version = "0.0.0", default-features = false, optional = true }
2223
rand_core = { version = "0.9", default-features = false }
2324
serdect = { version = "0.3.0", optional = true }
2425
sha3 = { version = "0.11.0-rc.0", default-features = false }
@@ -27,11 +28,11 @@ zeroize = { version = "1.8", default-features = false, optional = true }
2728

2829
[features]
2930
default = ["std", "signing", "pkcs8"]
30-
alloc = ["serdect/alloc", "zeroize/alloc", "crypto_signature/alloc"]
31-
pkcs8 = ["elliptic-curve/pkcs8"]
32-
signing = ["dep:crypto_signature", "zeroize"]
33-
serde = ["dep:serdect"]
34-
std = ["serdect/default", "zeroize/default"]
31+
alloc = ["serdect/alloc", "zeroize/alloc", "crypto_signature/alloc", "ed448?/alloc"]
32+
pkcs8 = ["elliptic-curve/pkcs8", "ed448?/pkcs8"]
33+
signing = ["dep:crypto_signature", "ed448", "zeroize"]
34+
serde = ["dep:serdect", "ed448?/serde_bytes"]
35+
std = ["alloc", "serdect/default", "zeroize/default"]
3536
zeroize = ["dep:zeroize"]
3637

3738
[dev-dependencies]

ed448-goldilocks/src/curve/edwards/extended.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl TryFrom<&[u8]> for CompressedEdwardsY {
149149

150150
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
151151
let bytes = <PointBytes>::try_from(value).map_err(|_| "Invalid length")?;
152-
Self::try_from(&bytes)
152+
Ok(CompressedEdwardsY(bytes))
153153
}
154154
}
155155

@@ -174,24 +174,6 @@ impl From<&CompressedEdwardsY> for PointBytes {
174174
}
175175
}
176176

177-
impl TryFrom<PointBytes> for CompressedEdwardsY {
178-
type Error = &'static str;
179-
180-
fn try_from(value: PointBytes) -> Result<Self, Self::Error> {
181-
let pt = CompressedEdwardsY(value);
182-
let _ = Option::<EdwardsPoint>::from(pt.decompress()).ok_or("Invalid point")?;
183-
Ok(pt)
184-
}
185-
}
186-
187-
impl TryFrom<&PointBytes> for CompressedEdwardsY {
188-
type Error = &'static str;
189-
190-
fn try_from(value: &PointBytes) -> Result<Self, Self::Error> {
191-
Self::try_from(*value)
192-
}
193-
}
194-
195177
#[cfg(feature = "serde")]
196178
impl serdect::serde::Serialize for CompressedEdwardsY {
197179
fn serialize<S: serdect::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
@@ -211,6 +193,13 @@ impl<'de> serdect::serde::Deserialize<'de> for CompressedEdwardsY {
211193
}
212194
}
213195

196+
#[cfg(feature = "ed448")]
197+
impl From<ed448::ComponentBytes> for CompressedEdwardsY {
198+
fn from(point: ed448::ComponentBytes) -> Self {
199+
Self(point)
200+
}
201+
}
202+
214203
impl CompressedEdwardsY {
215204
/// The compressed generator point
216205
pub const GENERATOR: Self = Self([
Lines changed: 16 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,38 @@
11
use crate::*;
2-
use elliptic_curve::Group;
2+
use elliptic_curve::array::Array;
33

4-
/// Ed448 signature as defined in [RFC8032 § 5.2.5]
5-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
6-
pub struct Signature {
7-
pub(crate) r: CompressedEdwardsY,
8-
pub(crate) s: [u8; 57],
9-
}
10-
11-
impl Default for Signature {
12-
fn default() -> Self {
13-
Self {
14-
r: CompressedEdwardsY::default(),
15-
s: [0u8; 57],
16-
}
17-
}
18-
}
19-
20-
#[cfg(any(feature = "alloc", feature = "std"))]
21-
impl TryFrom<Vec<u8>> for Signature {
22-
type Error = SigningError;
23-
24-
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
25-
Self::try_from(value.as_slice())
26-
}
27-
}
28-
29-
#[cfg(any(feature = "alloc", feature = "std"))]
30-
impl TryFrom<&Vec<u8>> for Signature {
31-
type Error = SigningError;
32-
33-
fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
34-
Self::try_from(value.as_slice())
35-
}
36-
}
37-
38-
impl TryFrom<&[u8]> for Signature {
39-
type Error = SigningError;
40-
41-
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
42-
if value.len() != SIGNATURE_LENGTH {
43-
return Err(SigningError::InvalidSignatureLength);
44-
}
45-
46-
let mut bytes = [0u8; SIGNATURE_LENGTH];
47-
bytes.copy_from_slice(value);
48-
Self::from_bytes(&bytes)
49-
}
50-
}
51-
52-
#[cfg(any(feature = "alloc", feature = "std"))]
53-
impl TryFrom<Box<[u8]>> for Signature {
54-
type Error = SigningError;
55-
56-
fn try_from(value: Box<[u8]>) -> Result<Self, Self::Error> {
57-
Self::try_from(value.as_ref())
58-
}
59-
}
60-
61-
#[cfg(feature = "serde")]
62-
impl serdect::serde::Serialize for Signature {
63-
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
64-
where
65-
S: serdect::serde::Serializer,
66-
{
67-
serdect::array::serialize_hex_lower_or_bin(&self.to_bytes(), s)
68-
}
69-
}
70-
71-
#[cfg(feature = "serde")]
72-
impl<'de> serdect::serde::Deserialize<'de> for Signature {
73-
fn deserialize<D>(d: D) -> Result<Self, D::Error>
74-
where
75-
D: serdect::serde::Deserializer<'de>,
76-
{
77-
let mut bytes = [0u8; SIGNATURE_LENGTH];
78-
serdect::array::deserialize_hex_or_bin(&mut bytes, d)?;
79-
Signature::from_bytes(&bytes).map_err(serdect::serde::de::Error::custom)
80-
}
81-
}
4+
pub use ed448::Signature;
825

83-
impl Signature {
84-
/// Converts [`Signature`] to a byte array.
85-
pub fn to_bytes(&self) -> [u8; SIGNATURE_LENGTH] {
86-
let mut bytes = [0u8; SIGNATURE_LENGTH];
87-
bytes[..57].copy_from_slice(self.r.as_bytes());
88-
bytes[57..].copy_from_slice(&self.s);
89-
bytes
90-
}
91-
92-
/// Converts a byte array to a [`Signature`].
93-
pub fn from_bytes(bytes: &[u8; SIGNATURE_LENGTH]) -> Result<Self, SigningError> {
94-
let mut r = [0u8; SECRET_KEY_LENGTH];
95-
r.copy_from_slice(&bytes[..SECRET_KEY_LENGTH]);
96-
let mut s = [0u8; SECRET_KEY_LENGTH];
97-
s.copy_from_slice(&bytes[SECRET_KEY_LENGTH..]);
98-
99-
let r = CompressedEdwardsY(r);
100-
101-
let big_r = r.decompress();
102-
if big_r.is_none().into() {
103-
return Err(SigningError::InvalidSignatureRComponent);
104-
}
105-
106-
let big_r = big_r.expect("big_r is not none");
107-
if big_r.is_identity().into() {
108-
return Err(SigningError::InvalidSignatureRComponent);
109-
}
110-
111-
if s[56] != 0x00 {
112-
return Err(SigningError::InvalidSignatureSComponent);
113-
}
114-
let s_bytes = ScalarBytes::from(s);
115-
let ss = Scalar::from_canonical_bytes(&s_bytes);
116-
117-
if ss.is_none().into() {
118-
return Err(SigningError::InvalidSignatureSComponent);
119-
}
120-
let sc = ss.expect("ss is not none");
121-
if sc.is_zero().into() {
122-
return Err(SigningError::InvalidSignatureSComponent);
123-
}
124-
125-
Ok(Self { r, s })
126-
}
127-
128-
/// The `r` value of the signature.
129-
pub fn r(&self) -> CompressedEdwardsY {
130-
self.r
131-
}
132-
133-
/// The `s` value of the signature.
134-
pub fn s(&self) -> &[u8; SECRET_KEY_LENGTH] {
135-
&self.s
136-
}
6+
pub(crate) struct InnerSignature {
7+
pub(crate) r: EdwardsPoint,
8+
pub(crate) s: Scalar,
1379
}
13810

13911
impl From<InnerSignature> for Signature {
14012
fn from(inner: InnerSignature) -> Self {
14113
let mut s = [0u8; SECRET_KEY_LENGTH];
14214
s.copy_from_slice(&inner.s.to_bytes_rfc_8032());
143-
Self {
144-
r: inner.r.compress(),
145-
s,
146-
}
15+
Self::from_components(inner.r.compress(), s)
14716
}
14817
}
14918

150-
impl TryFrom<Signature> for InnerSignature {
19+
impl TryFrom<&Signature> for InnerSignature {
15120
type Error = SigningError;
15221

153-
fn try_from(signature: Signature) -> Result<Self, Self::Error> {
154-
let s_bytes = ScalarBytes::try_from(&signature.s[..]).expect("invalid length");
155-
let s = Option::from(Scalar::from_canonical_bytes(&s_bytes))
22+
fn try_from(signature: &Signature) -> Result<Self, Self::Error> {
23+
let s_bytes: &Array<u8, _> = (signature.s_bytes()).into();
24+
let s = Option::from(Scalar::from_canonical_bytes(s_bytes))
15625
.ok_or(SigningError::InvalidSignatureSComponent)?;
157-
let r = Option::from(signature.r.decompress())
26+
let r = Option::from(CompressedEdwardsY::from(*signature.r_bytes()).decompress())
15827
.ok_or(SigningError::InvalidSignatureRComponent)?;
15928
Ok(Self { r, s })
16029
}
16130
}
16231

163-
pub(crate) struct InnerSignature {
164-
pub(crate) r: EdwardsPoint,
165-
pub(crate) s: Scalar,
166-
}
167-
168-
#[cfg(feature = "serde")]
169-
#[test]
170-
fn serialization() {
171-
use rand_chacha::ChaCha8Rng;
172-
use rand_core::SeedableRng;
173-
174-
let mut rng = ChaCha8Rng::from_seed([0u8; 32]);
175-
let signing_key = super::SigningKey::generate(&mut rng);
176-
let signature = signing_key.sign_raw(b"Hello, World!");
177-
178-
let bytes = serde_bare::to_vec(&signature).unwrap();
179-
let signature2: Signature = serde_bare::from_slice(&bytes).unwrap();
180-
assert_eq!(signature, signature2);
32+
impl TryFrom<Signature> for InnerSignature {
33+
type Error = SigningError;
18134

182-
let string = serde_json::to_string(&signature).unwrap();
183-
let signature3: Signature = serde_json::from_str(&string).unwrap();
184-
assert_eq!(signature, signature3);
35+
fn try_from(signature: Signature) -> Result<Self, Self::Error> {
36+
Self::try_from(&signature)
37+
}
18538
}

0 commit comments

Comments
 (0)