local testimony = require("testimony") local litls = require("litls.core") local hex = litls.hex_encode local unhex = litls.hex_decode local testify = testimony.new("== LITLS X25519 ==") -- RFC 7748 Section 6.1 - X25519 test vector testify:that("X25519 ECDH (RFC 7748 6.1)", function() local alice_priv = unhex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a") local alice_pub = unhex("8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a") local bob_priv = unhex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb") local bob_pub = unhex("de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f") local expected = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742" -- Alice computes shared secret local shared_a = litls.x25519(alice_priv, bob_pub) testimony.assert_equal(expected, hex(shared_a)) -- Bob computes same shared secret local shared_b = litls.x25519(bob_priv, alice_pub) testimony.assert_equal(expected, hex(shared_b)) end) -- Base point multiplication: private key -> public key testify:that("X25519 base point multiplication (RFC 7748 6.1)", function() local alice_priv = unhex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a") local expected = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a" local pub = litls.x25519_base(alice_priv) testimony.assert_equal(expected, hex(pub)) end) -- Key generation round-trip testify:that("X25519 keygen round-trip", function() local priv_a = litls.random_bytes(32) local priv_b = litls.random_bytes(32) local pub_a = litls.x25519_base(priv_a) local pub_b = litls.x25519_base(priv_b) local shared_a = litls.x25519(priv_a, pub_b) local shared_b = litls.x25519(priv_b, pub_a) testimony.assert_equal(hex(shared_a), hex(shared_b)) end) testify:conclude()