Skip to content

Commit d32969e

Browse files
authored
x.crypto: initial addition of curve25519 module (#24748)
1 parent b9e5757 commit d32969e

6 files changed

Lines changed: 945 additions & 0 deletions

File tree

‎vlib/x/crypto/curve25519/README.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# curve25519
2+
------------
3+
This module implement Diffie-Hellman key exchange mechanism (ECDHA) based on elliptic curve
4+
known as Curve25519 in pure V.
5+
6+
## About curve25519
7+
8+
From wikipedia, Curve25519 is an elliptic curve used in elliptic-curve cryptography (ECC)
9+
offering 128 bits of security (256-bit key size) and designed for use with the elliptic
10+
curve Diffie–Hellman (ECDH) key agreement scheme.
11+
It is one of the fastest curves in ECC, and is not covered by any known patents.
12+
The reference implementation is public domain software. The original Curve25519 paper defined it as
13+
a Diffie–Hellman (DH) function.
14+
Daniel J. Bernstein has since proposed that the name "Curve25519" be used for the underlying curve,
15+
and the name "X25519" for the DH function Curve25519 is an elliptic curve
16+
that offers 128 security bits
17+
and is designed for use in the Elliptic Curve Diffie-Hellman (ECDH) key agreement key design scheme.
18+
19+
Examples
20+
--------
21+
```v
22+
import x.crypto.curve25519
23+
24+
fn main() {
25+
// For example, two peers, Alice and Bob, want to create a shared secret together
26+
//
27+
// Alice generates a private key
28+
mut alice_pvkey := curve25519.PrivateKey.new()!
29+
// Alice's PublicKey to be shared with Bob
30+
alice_pbkey := alice_pvkey.public_key()!
31+
32+
// The other peer, Bob, has a different private key
33+
mut bob_pvkey := curve25519.PrivateKey.new()!
34+
// Bob's public key to be shared with Alice
35+
bob_pbkey := bob_pvkey.public_key()!
36+
37+
// Let the two peers exchange their respective public keys
38+
//
39+
// Alice derives the shared secret, using her own private key, and the public key that Bob shared
40+
alice_shared_sec := curve25519.derive_shared_secret(mut alice_pvkey, bob_pbkey)!
41+
42+
// Bob derives the shared secret, using his own private key, and the public key that Alice shared
43+
bob_shared_sec := curve25519.derive_shared_secret(mut bob_pvkey, alice_pbkey)!
44+
45+
// the two shared secrets (derived by Alice, and derived by Bob), should be the same
46+
//
47+
println(alice_shared_sec.hex()) // 49fd4a4d0637d2413cd501b20111fc50a592dc21460e45f451c03d1fd3cef900
48+
println(bob_shared_sec.hex()) // 49fd4a4d0637d2413cd501b20111fc50a592dc21460e45f451c03d1fd3cef900
49+
dump(alice_shared_sec == bob_shared_sec) // alice_shared_sec == bob_shared_sec: true
50+
51+
assert alice_shared_sec == bob_shared_sec
52+
}
53+
```

0 commit comments

Comments
 (0)