Open in a new tab

ML-KEM-768 encapsulation, an ML-DSA-65 signature, and the hybrid X-Wing (X25519 + ML-KEM-768) and ML-DSA-65 + Ed25519 composite variants, with every key, ciphertext and signature size shown.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Data that must stay confidential or verifiable for years: the NIST post-quantum standards, and the hybrid forms that keep a classical guarantee while the new ones mature. Sizes are the practical constraint, so the page prints them.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html and main.js next to each other and open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Prefer the hybrids for anything shipped today; pick ML-KEM-1024 / ML-DSA-87 through the sibling instances when you need the higher level.

What it shows

  • ml_kem.ml_kem768: keygen(), encapsulate(publicKey) → { cipherText, sharedSecret }, decapsulate(cipherText, secretKey).
  • ml_dsa.ml_dsa65 and hybridSign.mldsa65_ed25519: keygen(), sign(secretKey, message), verify(publicKey, signature, message) — the same shape, so the page runs both through one loop.
  • hybridKem.xwing (X-Wing) with the same KEM shape as ML-KEM.
  • A tampered message fails verification in every scheme; a tampered KEM ciphertext yields a different secret (implicit rejection), never an error.
  • Key generation blocks the main thread for a moment: the page paints "computing…" first and times the work with fw's clock.

Code

main.js

javascriptApache-2.074 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { ml_kem } from '@awacloud/fw/crypto/pkc/ml_kem.js';
import { ml_dsa } from '@awacloud/fw/crypto/pkc/ml_dsa.js';
import { hybridKem } from '@awacloud/fw/crypto/pkc/hybridKem.js';
import { hybridSign } from '@awacloud/fw/crypto/pkc/hybridSign.js';
import { clock } from '@awacloud/fw/io/timing/clock.js';

fw.runtime.registerAllDeep([ml_kem, ml_dsa, hybridKem, hybridSign, clock]);
export const KEM = fw.runtime.resolve('ml_kem').ml_kem768;
export const DSA = fw.runtime.resolve('ml_dsa').ml_dsa65;
const XWING = fw.runtime.resolve('hybridKem').xwing;
const HYBRID_SIG = fw.runtime.resolve('hybridSign').mldsa65_ed25519;
const CLOCK = fw.runtime.resolve('clock');
const encoder = new TextEncoder();

/** @typedef {{ scheme: string, publicKey: number, secretKey: number, payload: number, ok: boolean }} Row */
/** @param {string} scheme @param {any} keys @param {Uint8Array} payload @param {boolean} ok @returns {Row} */
const row = (scheme, keys, payload, ok) => (
    { scheme, publicKey: keys.publicKey.length, secretKey: keys.secretKey.length, payload: payload.length, ok });

/**
 * A KEM round trip: the sender encapsulates to the public key, the receiver
 * decapsulates the ciphertext; both hold the same 32-byte shared secret.
 * @param {string} scheme @param {any} kem @returns {Row}
 */
export function exchange(scheme, kem) {
    const keys = kem.keygen();
    const { cipherText, sharedSecret } = kem.encapsulate(keys.publicKey);
    const received = kem.decapsulate(cipherText, keys.secretKey);
    const same = received !== false && received.length === 32 && received.every((b, i) => b === sharedSecret[i]);
    return row(scheme, keys, cipherText, same);
}

/**
 * Sign, verify, and check that a changed message fails. Both schemes take
 * the key first: sign(secretKey, message), verify(publicKey, signature, message).
 * @param {string} message @returns {Row[]}
 */
export function signatures(message) {
    const [msg, changed] = [message, `${message}!`].map((m) => encoder.encode(m));
    return [['ML-DSA-65 (FIPS 204)', DSA], ['ML-DSA-65 + Ed25519 composite', HYBRID_SIG]].map(([name, scheme]) => {
        const keys = scheme.keygen();
        const sig = scheme.sign(keys.secretKey, msg);
        const ok = scheme.verify(keys.publicKey, sig, msg) && !scheme.verify(keys.publicKey, sig, changed);
        return row(name, keys, sig, ok);
    });
}

/** @param {string} message @returns {Row[]} */
export const demo = (message) => [
    exchange('ML-KEM-768 (FIPS 203)', KEM), exchange('X-Wing (X25519 + ML-KEM-768)', XWING), ...signatures(message)];

const rows = /** @type {HTMLElement} */ (document.getElementById('rows'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
const message = /** @type {HTMLInputElement} */ (document.getElementById('message'));

function update() {
    const t0 = CLOCK.monotonic();
    const result = demo(message.value);
    rows.replaceChildren(...result.map((r) => {
        const tr = document.createElement('tr');
        for (const cell of [r.scheme, r.publicKey, r.secretKey, r.payload, r.ok ? 'round trip held' : 'FAILED']) {
            tr.appendChild(document.createElement('td')).textContent = String(cell);
        }
        return tr;
    }));
    status.textContent = `${result.length} schemes in ${CLOCK.format(CLOCK.since(t0))}, pure JavaScript`;
}

message.addEventListener('change', update);
// Key generation blocks the main thread for a moment: paint the page first.
status.textContent = 'computing…';
requestAnimationFrame(() => setTimeout(update));

index.html

htmlApache-2.029 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Post-quantum key exchange and signatures</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Post-quantum key exchange and signatures</h1>
    <label for="message">Message to sign</label>
    <input id="message" type="text" value="release 1.4.0" autocomplete="off">
    <p id="status" aria-live="polite">working…</p>
    <table>
      <caption>Sizes in bytes, and whether the round trip held</caption>
      <thead><tr><th scope="col">Scheme</th><th scope="col">Public key</th><th scope="col">Secret key</th>
        <th scope="col">Ciphertext / signature</th><th scope="col">Result</th></tr></thead>
      <tbody id="rows"></tbody>
    </table>
  </main>
</body>
</html>