|
| 1 | +/** |
| 2 | + * Quantum-Safe Signature Replacement Example |
| 3 | + * |
| 4 | + * Demonstrates how post-quantum signatures (ML-DSA) can replace the ECDSA |
| 5 | + * signature primitive used in Bitcoin and other cryptocurrencies. |
| 6 | + * |
| 7 | + * NOTE: This is a simplified simulation of the signature layer only. A real |
| 8 | + * blockchain migration would also require changes to address derivation, |
| 9 | + * transaction serialization, fee economics, and consensus rules. This example |
| 10 | + * focuses on the cryptographic primitive swap: ECDSA -> ML-DSA. |
| 11 | + * |
| 12 | + * Run: node examples/quantum-safe-wallet.mjs |
| 13 | + */ |
| 14 | + |
| 15 | +import { ml_dsa65 } from 'fips-crypto/auto'; |
| 16 | +import { createHash } from 'crypto'; |
| 17 | + |
| 18 | +// --- Helpers --- |
| 19 | + |
| 20 | +function sha256(data) { |
| 21 | + return createHash('sha256').update(data).digest('hex'); |
| 22 | +} |
| 23 | + |
| 24 | +/** Derive an address from a public key (simplified: truncated SHA-256). */ |
| 25 | +function deriveAddress(publicKey) { |
| 26 | + return sha256(publicKey).slice(0, 40); |
| 27 | +} |
| 28 | + |
| 29 | +function createTransaction(from, to, amount, nonce, timestamp) { |
| 30 | + const tx = { from, to, amount, nonce, timestamp }; |
| 31 | + const encoded = new TextEncoder().encode(JSON.stringify(tx)); |
| 32 | + return { ...tx, hash: sha256(encoded), encoded }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Validate a signed transaction the way a blockchain node would: |
| 37 | + * 1. Verify the signature against the signer's public key |
| 38 | + * 2. Verify the signer's public key derives the claimed sender address |
| 39 | + */ |
| 40 | +async function validateTransaction(tx, signature, signerPublicKey) { |
| 41 | + // Step 1: Signature must be valid |
| 42 | + const sigValid = await ml_dsa65.verify(signerPublicKey, tx.encoded, signature); |
| 43 | + if (!sigValid) return { valid: false, reason: 'invalid signature' }; |
| 44 | + |
| 45 | + // Step 2: Signer must own the sender address |
| 46 | + const derivedAddr = deriveAddress(signerPublicKey); |
| 47 | + if (derivedAddr !== tx.from) return { valid: false, reason: 'signer does not match sender address' }; |
| 48 | + |
| 49 | + return { valid: true, reason: 'ok' }; |
| 50 | +} |
| 51 | + |
| 52 | +// ============================================================================= |
| 53 | +// Step 1: Create two wallets (Alice and Bob) |
| 54 | +// ============================================================================= |
| 55 | + |
| 56 | +console.log('=== Quantum-Safe Signature Replacement Demo ===\n'); |
| 57 | + |
| 58 | +console.log('Creating wallets...'); |
| 59 | +const alice = await ml_dsa65.keygen(); |
| 60 | +const bob = await ml_dsa65.keygen(); |
| 61 | + |
| 62 | +const aliceAddr = deriveAddress(alice.publicKey); |
| 63 | +const bobAddr = deriveAddress(bob.publicKey); |
| 64 | + |
| 65 | +console.log(` Alice: 0x${aliceAddr}`); |
| 66 | +console.log(` Public key: ${alice.publicKey.length} bytes (ML-DSA-65)`); |
| 67 | +console.log(` Bob: 0x${bobAddr}`); |
| 68 | +console.log(` Public key: ${bob.publicKey.length} bytes (ML-DSA-65)`); |
| 69 | + |
| 70 | +// ============================================================================= |
| 71 | +// Step 2: Alice signs a transaction to send funds to Bob |
| 72 | +// ============================================================================= |
| 73 | + |
| 74 | +console.log('\n--- Transaction Signing ---\n'); |
| 75 | + |
| 76 | +const tx1 = createTransaction(aliceAddr, bobAddr, 2.5, 1, 1711900000000); |
| 77 | +console.log(`Transaction: Alice -> Bob, 2.5 coins`); |
| 78 | +console.log(` TX hash: ${tx1.hash}`); |
| 79 | + |
| 80 | +const sig1 = await ml_dsa65.sign(alice.secretKey, tx1.encoded); |
| 81 | +console.log(` Signature: ${sig1.length} bytes (ML-DSA-65)`); |
| 82 | +console.log(` (Bitcoin ECDSA would be ~71 bytes; ML-DSA-65 is ${sig1.length} bytes)`); |
| 83 | +console.log(` (Tradeoff: larger signatures, but quantum-safe)`); |
| 84 | + |
| 85 | +// ============================================================================= |
| 86 | +// Step 3: Validator verifies signature AND sender-address binding |
| 87 | +// ============================================================================= |
| 88 | + |
| 89 | +console.log('\n--- Validator Verification ---\n'); |
| 90 | + |
| 91 | +const result1 = await validateTransaction(tx1, sig1, alice.publicKey); |
| 92 | +console.log(` Signature valid: ${result1.valid}`); |
| 93 | +console.log(` Address binding: signer key derives 0x${aliceAddr.slice(0, 8)}... = tx.from`); |
| 94 | +console.log(' Transaction accepted into mempool'); |
| 95 | + |
| 96 | +// ============================================================================= |
| 97 | +// Step 4: Simulate a block with multiple transactions |
| 98 | +// ============================================================================= |
| 99 | + |
| 100 | +console.log('\n--- Block Simulation ---\n'); |
| 101 | + |
| 102 | +const transactions = [ |
| 103 | + { signer: alice, fromAddr: aliceAddr, toAddr: bobAddr, amount: 1.0, nonce: 2 }, |
| 104 | + { signer: bob, fromAddr: bobAddr, toAddr: aliceAddr, amount: 0.3, nonce: 1 }, |
| 105 | + { signer: alice, fromAddr: aliceAddr, toAddr: bobAddr, amount: 0.7, nonce: 3 }, |
| 106 | +]; |
| 107 | + |
| 108 | +const signedTxs = []; |
| 109 | +for (const t of transactions) { |
| 110 | + const tx = createTransaction(t.fromAddr, t.toAddr, t.amount, t.nonce, 1711900000000); |
| 111 | + const sig = await ml_dsa65.sign(t.signer.secretKey, tx.encoded); |
| 112 | + signedTxs.push({ tx, sig, signerPk: t.signer.publicKey }); |
| 113 | +} |
| 114 | + |
| 115 | +console.log(`Block contains ${signedTxs.length} transactions:`); |
| 116 | + |
| 117 | +let allValid = true; |
| 118 | +for (const { tx, sig, signerPk } of signedTxs) { |
| 119 | + const { valid, reason } = await validateTransaction(tx, sig, signerPk); |
| 120 | + console.log(` ${tx.hash.slice(0, 16)}... ${tx.from.slice(0, 8)}->${tx.to.slice(0, 8)} ${tx.amount} coins [${valid ? 'VALID' : 'REJECTED: ' + reason}]`); |
| 121 | + if (!valid) allValid = false; |
| 122 | +} |
| 123 | +console.log(`\nBlock validation: ${allValid ? 'ALL VALID' : 'REJECTED'}`); |
| 124 | + |
| 125 | +// ============================================================================= |
| 126 | +// Step 5: Tamper detection — modified transaction is rejected |
| 127 | +// ============================================================================= |
| 128 | + |
| 129 | +console.log('\n--- Tamper Detection ---\n'); |
| 130 | + |
| 131 | +// Sign the original transaction |
| 132 | +const originalTx = createTransaction(aliceAddr, bobAddr, 2.5, 1, 1711900000000); |
| 133 | +const originalSig = await ml_dsa65.sign(alice.secretKey, originalTx.encoded); |
| 134 | + |
| 135 | +// Attacker rebuilds the transaction with a different amount but same metadata |
| 136 | +const tamperedTx = createTransaction(aliceAddr, bobAddr, 2500, 1, 1711900000000); |
| 137 | +console.log('Attacker changes amount from 2.5 to 2500 (same nonce, same timestamp)...'); |
| 138 | + |
| 139 | +const tamperedResult = await validateTransaction(tamperedTx, originalSig, alice.publicKey); |
| 140 | +console.log(` Tampered TX valid: ${tamperedResult.valid} (${tamperedResult.reason})`); |
| 141 | + |
| 142 | +// Original still verifies |
| 143 | +const originalResult = await validateTransaction(originalTx, originalSig, alice.publicKey); |
| 144 | +console.log(` Original TX valid: ${originalResult.valid}`); |
| 145 | + |
| 146 | +// ============================================================================= |
| 147 | +// Step 6: Wrong signer is rejected by address binding |
| 148 | +// ============================================================================= |
| 149 | + |
| 150 | +console.log('\n--- Address Binding Check ---\n'); |
| 151 | + |
| 152 | +// Bob tries to sign a transaction claiming to be from Alice's address |
| 153 | +const forgedTx = createTransaction(aliceAddr, bobAddr, 100, 99, 1711900000000); |
| 154 | +const forgedSig = await ml_dsa65.sign(bob.secretKey, forgedTx.encoded); |
| 155 | +const forgedResult = await validateTransaction(forgedTx, forgedSig, bob.publicKey); |
| 156 | +console.log('Bob signs a TX claiming to be from Alice...'); |
| 157 | +console.log(` Valid: ${forgedResult.valid} (${forgedResult.reason})`); |
| 158 | + |
| 159 | +// ============================================================================= |
| 160 | +// Summary |
| 161 | +// ============================================================================= |
| 162 | + |
| 163 | +console.log('\n=== Summary ===\n'); |
| 164 | +console.log('This demo shows ML-DSA-65 (FIPS 204) replacing the ECDSA signature'); |
| 165 | +console.log('primitive. In a real blockchain migration, additional protocol-level'); |
| 166 | +console.log('changes (address format, serialization, consensus rules) would also'); |
| 167 | +console.log('be needed.\n'); |
| 168 | +console.log('Key points:'); |
| 169 | +console.log(" - ECDSA is vulnerable to Shor's algorithm on a quantum computer"); |
| 170 | +console.log(' - ML-DSA resists all known quantum attacks'); |
| 171 | +console.log(' - Same keygen -> sign -> verify workflow'); |
| 172 | +console.log(` - Tradeoff: larger keys (${alice.publicKey.length}B vs 33B) and signatures (${sig1.length}B vs 71B)`); |
0 commit comments