Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VerifyingKey::to_edwards #624

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions ed25519-dalek/src/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ impl VerifyingKey {
pub fn to_montgomery(&self) -> MontgomeryPoint {
self.point.to_montgomery()
}

/// Return this verifying key in Edwards form.
pub fn to_edwards(&self) -> EdwardsPoint {
self.point
}
randombit marked this conversation as resolved.
Show resolved Hide resolved
}

impl Verifier<ed25519::Signature> for VerifyingKey {
Expand Down Expand Up @@ -563,6 +568,12 @@ impl TryFrom<&[u8]> for VerifyingKey {
}
}

impl From<VerifyingKey> for EdwardsPoint {
fn from(vk: VerifyingKey) -> EdwardsPoint {
vk.point
}
}

#[cfg(all(feature = "alloc", feature = "pkcs8"))]
impl pkcs8::EncodePublicKey for VerifyingKey {
fn to_public_key_der(&self) -> pkcs8::spki::Result<pkcs8::Document> {
Expand Down
23 changes: 23 additions & 0 deletions ed25519-dalek/tests/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,29 @@ mod integrations {
assert_eq!(v, "Second public key");
assert_eq!(m.len(), 2usize);
}

#[test]
fn montgomery_and_edwards_conversion() {
let mut rng = rand::rngs::OsRng;
let signing_key = SigningKey::generate(&mut rng);
let verifying_key = signing_key.verifying_key();

let ed = verifying_key.to_edwards();

// Check that to_edwards and From return same result:
assert_eq!(ed, curve25519_dalek::EdwardsPoint::from(verifying_key));

// The verifying key serialization is simply the compressed Edwards point
assert_eq!(verifying_key.to_bytes(), ed.compress().0);

// Check that modulo sign, to_montgomery().to_edwards() returns the original point
let monty = verifying_key.to_montgomery();
let via_monty0 = monty.to_edwards(0).unwrap();
let via_monty1 = monty.to_edwards(1).unwrap();

assert!(via_monty0 != via_monty1);
assert!(ed == via_monty0 || ed == via_monty1);
}
}

#[cfg(all(test, feature = "serde"))]
Expand Down