Skip to content

Commit 492e7db

Browse files
committed
Curve25519
1 parent 61b6c5c commit 492e7db

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

docs/Runtime Environment/Crypto.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,51 @@ print(base64.encode(sig)) --> un1g04+cwG8WxYDpSlj4PO/hsTqSITgYKycRuR+m3AE6ypLyUC
541541
print(crypto.verify(msg, "rsa-sha256", pub, sig)) --> true
542542
```
543543

544+
---
545+
## Curve25519
546+
### `crypto.generatekeypair`
547+
Generates a keypair for Curve25519.
548+
#### Parameters
549+
1. `mode` — Must be "curve25519".
550+
551+
Returns two strings: the public key and the private key, each 32 bytes.
552+
553+
```pluto
554+
local crypto = require "pluto:crypto"
555+
local pub, priv = crypto.generatekeypair("curve25519")
556+
print(priv:tohex())
557+
print(pub:tohex())
558+
assert(crypto.derive("curve25519", priv) == pub)
559+
```
560+
561+
### `crypto.derive`
562+
Derives a Curve25519 public key from a private key.
563+
#### Parameters
564+
1. `mode` — Must be "curve25519".
565+
2. `key` — The 32-byte private key.
566+
567+
Returns the corresponding public key.
568+
569+
### `crypto.x25519`
570+
Performs an X25519 Diffie-Hellman exchange on Curve25519.
571+
#### Parameters
572+
1. `private` — The local 32-byte private key.
573+
2. `public` — The peer's 32-byte public key.
574+
575+
Returns the 32-byte shared secret.
576+
577+
```pluto
578+
local crypto = require "pluto:crypto"
579+
580+
local alice_priv <const> = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a":fromhex()
581+
local alice_pub <const> = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a":fromhex()
582+
local bob_priv <const> = "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb":fromhex()
583+
local bob_pub <const> = "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f":fromhex()
584+
585+
print(crypto.x25519(alice_priv, bob_pub):tohex()) --> 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
586+
print(crypto.x25519(bob_priv, alice_pub):tohex()) --> 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
587+
```
588+
544589
---
545590
## Miscellaneous
546591
### `crypto.decompress`

0 commit comments

Comments
 (0)