| 1 | // Copyright © 2025 blackshirt. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | // |
| 5 | // Usage example of the `curve25519` module |
| 6 | import x.crypto.curve25519 |
| 7 | |
| 8 | fn test_curve25519_shared_secret_key_exchange() ! { |
| 9 | // Alice generates a private key |
| 10 | mut alice_pvkey := curve25519.PrivateKey.new()! |
| 11 | // Alice's PublicKey to be shared with Bob |
| 12 | alice_pbkey := alice_pvkey.public_key()! |
| 13 | |
| 14 | // The other peer, Bob, has a different private key |
| 15 | mut bob_pvkey := curve25519.PrivateKey.new()! |
| 16 | // Bob's public key to be shared |
| 17 | bob_pbkey := bob_pvkey.public_key()! |
| 18 | |
| 19 | // Let the two peers exchange their respective public keys |
| 20 | // |
| 21 | // Alice derives the shared secret, using her own private key, and the public key that Bob shared |
| 22 | alice_shared_sec := curve25519.derive_shared_secret(mut alice_pvkey, bob_pbkey)! |
| 23 | |
| 24 | // Bob derives the shared secret, using his own private key, and the public key that Alice shared |
| 25 | bob_shared_sec := curve25519.derive_shared_secret(mut bob_pvkey, alice_pbkey)! |
| 26 | |
| 27 | // the two shared secrets (derived by Alice, and derived by Bob), should be the same |
| 28 | // |
| 29 | assert alice_shared_sec == bob_shared_sec |
| 30 | } |
| 31 | |