Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/ed25519-dalek/tests/x25519.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / ed25519-dalek / tests / x25519.rs` 80 lines ยท 3266 bytes Raw Blame History 1 //! Tests for converting Ed25519 keys into X25519 (Montgomery form) keys. 2 3 use curve25519_dalek::scalar::{clamp_integer, Scalar}; 4 use `ed25519`_dalek::SigningKey; 5 use hex_literal::hex; 6 use sha2::{Digest, Sha512}; 7 use x25519_dalek::{PublicKey as XPublicKey, StaticSecret as XStaticSecret}; 8 9 /// Tests that X25519 Diffie-Hellman works when using keys converted from Ed25519. 10 // TODO: generate test vectors using another implementation of Ed25519->X25519 11 #[test] 12 fn `ed25519`_to_x25519_dh() { 13 // Keys from RFC8032 test vectors (from section 7.1) 14 let ed_secret_key_a = hex!("`9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60`"); 15 let ed_secret_key_b = hex!("`4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb`"); 16 17 let ed_signing_key_a = SigningKey::from_bytes(&ed_secret_key_a); 18 let ed_signing_key_b = SigningKey::from_bytes(&ed_secret_key_b); 19 20 // Create an x25519 static secret from the `ed25519` signing key 21 let scalar_bytes_a = ed_signing_key_a.to_scalar_bytes(); 22 let scalar_bytes_b = ed_signing_key_b.to_scalar_bytes(); 23 let x_static_secret_a = XStaticSecret::from(scalar_bytes_a); 24 let x_static_secret_b = XStaticSecret::from(scalar_bytes_b); 25 26 // Compute the secret scalars too 27 let scalar_a = ed_signing_key_a.to_scalar(); 28 let scalar_b = ed_signing_key_b.to_scalar(); 29 30 // Compare the scalar bytes to the first 32 bytes of SHA-512(secret_key). We have to clamp and 31 // reduce the SHA-512 output because that's what the spec does before using the scalars for 32 // anything. 33 assert_eq!(scalar_bytes_a, &Sha512::digest(ed_secret_key_a)[..32]); 34 assert_eq!(scalar_bytes_b, &Sha512::digest(ed_secret_key_b)[..32]); 35 36 // Compare the scalar with the clamped and reduced scalar bytes 37 assert_eq!( 38 scalar_a, 39 Scalar::from_bytes_mod_order(clamp_integer(scalar_bytes_a)) 40 ); 41 assert_eq!( 42 scalar_b, 43 Scalar::from_bytes_mod_order(clamp_integer(scalar_bytes_b)) 44 ); 45 46 let x_public_key_a = XPublicKey::from(&x_static_secret_a); 47 let x_public_key_b = XPublicKey::from(&x_static_secret_b); 48 assert_eq!( 49 x_public_key_a.to_bytes(), 50 hex!("`d85e07ec22b0ad881537c2f44d662d1a143cf830c57aca4305d85c7a90f6b62e`") 51 ); 52 assert_eq!( 53 x_public_key_b.to_bytes(), 54 hex!("`25c704c594b88afc00a76b69d1ed2b984d7e22550f3ed0802d04fbcd07d38d47`") 55 ); 56 57 // Test the claim made in the comments of SigningKey::to_scalar_bytes, i.e., that the resulting 58 // scalar is a valid private key for the x25519 pubkey represented by 59 // `sk.verifying_key().to_montgomery()` 60 assert_eq!( 61 ed_signing_key_a.verifying_key().to_montgomery().as_bytes(), 62 x_public_key_a.as_bytes() 63 ); 64 assert_eq!( 65 ed_signing_key_b.verifying_key().to_montgomery().as_bytes(), 66 x_public_key_b.as_bytes() 67 ); 68 69 // Check that Diffie-Hellman works 70 let expected_shared_secret = 71 hex!("`5166f24a6918368e2af831a4affadd97af0ac326bdf143596c045967cc00230e`"); 72 assert_eq!( 73 x_static_secret_a.diffie_hellman(&x_public_key_b).to_bytes(), 74 expected_shared_secret, 75 ); 76 assert_eq!( 77 x_static_secret_b.diffie_hellman(&x_public_key_a).to_bytes(), 78 expected_shared_secret, 79 ); 80 }