Skip to content

Commit

Permalink
fix typo ed25199 to ed25519 (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirahsapong authored May 3, 2024
1 parent f74a684 commit d1c4390
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
30 changes: 15 additions & 15 deletions crates/crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use ed25519_dalek::{
use jwk::Jwk;
use rand::rngs::OsRng;

pub struct Ed25199;
pub struct Ed25519;

impl Ed25199 {}
impl Ed25519 {}

impl CurveOperations for Ed25199 {
impl CurveOperations for Ed25519 {
fn generate() -> Result<Jwk, CryptoError> {
let signing_key = SigningKey::generate(&mut OsRng {});
let verifying_key = signing_key.verifying_key();
Expand Down Expand Up @@ -78,7 +78,7 @@ mod tests {

#[test]
fn test_generate_keys() {
let jwk = Ed25199::generate().unwrap();
let jwk = Ed25519::generate().unwrap();
assert_eq!(jwk.alg, "EdDSA");
assert_eq!(jwk.kty, "OKP");
assert_eq!(jwk.crv, "Ed25519");
Expand All @@ -95,43 +95,43 @@ mod tests {

#[test]
fn test_sign_and_verify() {
let jwk = Ed25199::generate().unwrap();
let jwk = Ed25519::generate().unwrap();
let payload = b"test payload";
let signature = Ed25199::sign(&jwk, payload).unwrap();
assert!(Ed25199::verify(&jwk, payload, &signature).is_ok());
let signature = Ed25519::sign(&jwk, payload).unwrap();
assert!(Ed25519::verify(&jwk, payload, &signature).is_ok());
}

#[test]
fn test_sign_with_invalid_private_key_length() {
let mut jwk = Ed25199::generate().unwrap();
let mut jwk = Ed25519::generate().unwrap();
let payload = b"another test payload";
// Alter the private key to an invalid length
jwk.d = Some(general_purpose::URL_SAFE_NO_PAD.encode(&[0u8; 31])); // one byte short

assert!(Ed25199::sign(&jwk, payload).is_err());
assert!(Ed25519::sign(&jwk, payload).is_err());
}

#[test]
fn test_verification_failure_with_invalid_public_key_length() {
let jwk = Ed25199::generate().unwrap();
let jwk = Ed25519::generate().unwrap();
let payload = b"test payload again";
let signature = Ed25199::sign(&jwk, payload).unwrap();
let signature = Ed25519::sign(&jwk, payload).unwrap();

let mut jwk_modified = jwk.clone();
// Alter the public key to an invalid length
jwk_modified.x = general_purpose::URL_SAFE_NO_PAD.encode(&[0u8; 31]); // one byte short

assert!(Ed25199::verify(&jwk_modified, payload, &signature).is_err());
assert!(Ed25519::verify(&jwk_modified, payload, &signature).is_err());
}

#[test]
fn test_verification_failure_with_modified_signature() {
let jwk = Ed25199::generate().unwrap();
let jwk = Ed25519::generate().unwrap();
let payload = b"yet another test payload";
let mut signature = Ed25199::sign(&jwk, payload).unwrap();
let mut signature = Ed25519::sign(&jwk, payload).unwrap();
// Introduce an error in the signature
signature[0] ^= 0xff;

assert!(Ed25199::verify(&jwk, payload, &signature).is_err());
assert!(Ed25519::verify(&jwk, payload, &signature).is_err());
}
}
4 changes: 2 additions & 2 deletions crates/jws/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use base64::{engine::general_purpose, Engine as _};
use crypto::{ed25519::Ed25199, secp256k1::Secp256k1, CryptoError, CurveOperations};
use crypto::{ed25519::Ed25519, secp256k1::Secp256k1, CryptoError, CurveOperations};
use dids::{
bearer::{BearerDid, BearerDidError},
document::{DocumentError, KeyIdFragment, KeySelector},
Expand Down Expand Up @@ -137,7 +137,7 @@ pub async fn verify_compact_jws(compact_jws: &str) -> Result<(), JwsError> {
.decode(&parts[2])
.map_err(|e| JwsError::DecodingError(e.to_string()))?;
match alg.as_str() {
"EdDSA" => Ed25199::verify(&public_key, &to_verify.into_bytes(), &decoded_signature),
"EdDSA" => Ed25519::verify(&public_key, &to_verify.into_bytes(), &decoded_signature),
"ES256K" => Secp256k1::verify(&public_key, &to_verify.into_bytes(), &decoded_signature),
_ => return Err(JwsError::AlgorithmNotFound(alg)),
}?;
Expand Down
18 changes: 9 additions & 9 deletions crates/keys/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crypto::{ed25519::Ed25199, secp256k1::Secp256k1, CryptoError, CurveOperations};
use crypto::{ed25519::Ed25519, secp256k1::Secp256k1, CryptoError, CurveOperations};
use jwk::{Jwk, JwkError};
use std::sync::Arc;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl Key for Jwk {
impl PublicKey for Jwk {
fn verify(&self, payload: &[u8], signature: &[u8]) -> Result<(), KeyError> {
match self.crv.as_str() {
"Ed25519" => Ed25199::verify(self, payload, signature),
"Ed25519" => Ed25519::verify(self, payload, signature),
"secp256k1" => Secp256k1::verify(self, payload, signature),
_ => return Err(KeyError::CurveNotFound),
}?;
Expand All @@ -74,7 +74,7 @@ impl PrivateKey for Jwk {

fn sign(&self, payload: &[u8]) -> Result<Vec<u8>, KeyError> {
let signature = match self.crv.as_str() {
"Ed25519" => Ed25199::sign(self, payload),
"Ed25519" => Ed25519::sign(self, payload),
"secp256k1" => Secp256k1::sign(self, payload),
_ => return Err(KeyError::CurveNotFound),
}?;
Expand All @@ -84,13 +84,13 @@ impl PrivateKey for Jwk {

#[cfg(test)]
mod tests {
use crypto::{ed25519::Ed25199, CurveOperations};
use crypto::{ed25519::Ed25519, CurveOperations};

use super::*;

#[test]
fn test_verify() {
let private_key = Ed25199::generate().unwrap();
let private_key = Ed25519::generate().unwrap();
let payload = b"hello world";
let signature = private_key.sign(payload).unwrap();

Expand All @@ -100,20 +100,20 @@ mod tests {

#[test]
fn test_verify_failure() {
let private_key = Ed25199::generate().unwrap();
let private_key = Ed25519::generate().unwrap();
let payload: &[u8] = b"hello world";
let signature = private_key.sign(payload).unwrap();

// public_key is unrelated to the private_key used to sign the payload, so it should fail
let private_key_2 = Ed25199::generate().unwrap();
let private_key_2 = Ed25519::generate().unwrap();
let public_key = private_key_2.to_public().unwrap();
let verification_warnings = public_key.verify(payload, &signature);
assert!(verification_warnings.is_err());
}

#[test]
fn test_to_public() {
let private_key = Ed25199::generate().unwrap();
let private_key = Ed25519::generate().unwrap();
let public_key = private_key.to_public().unwrap().jwk().unwrap();

assert_eq!(private_key.x, public_key.x);
Expand All @@ -125,7 +125,7 @@ mod tests {

#[test]
fn test_sign() {
let private_key = Ed25199::generate().unwrap();
let private_key = Ed25519::generate().unwrap();
let payload: &[u8] = b"hello world";
let signature = private_key.sign(payload).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/keys/src/key_manager/key_store/in_memory_key_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::key::{PrivateKey, PublicKey};
use crate::key_manager::key_store::{KeyStore, KeyStoreError};
use crypto::ed25519::Ed25199;
use crypto::ed25519::Ed25519;
use crypto::secp256k1::Secp256k1;
use crypto::{Curve, CurveOperations};
use std::collections::HashMap;
Expand All @@ -25,7 +25,7 @@ impl KeyStore for InMemoryKeyStore {
key_alias: Option<String>,
) -> Result<String, KeyStoreError> {
let private_key = Arc::new(match curve {
Curve::Ed25519 => Ed25199::generate(),
Curve::Ed25519 => Ed25519::generate(),
Curve::Secp256k1 => Secp256k1::generate(),
}?);
let key_alias = match key_alias {
Expand Down

0 comments on commit d1c4390

Please sign in to comment.