falconTS/mnemonic_example.ts
2025-04-11 11:31:57 +02:00

92 lines
3.4 KiB
TypeScript

import { uint8ArrayToMnemonic, mnemonicToUint8Array, validateMnemonic, generateMnemonic } from './src/mnemonic';
import Falcon from './src/falcon';
/**
* Example demonstrating the usage of our custom mnemonic implementation
* for Falcon keys.
*/
async function mnemonicExample() {
console.log('=== Falcon Mnemonic Example ===');
// Generate a random mnemonic
const mnemonic = generateMnemonic();
console.log('Generated mnemonic:', mnemonic);
// Validate the mnemonic
const isValid = validateMnemonic(mnemonic);
console.log('Mnemonic is valid:', isValid);
// Convert mnemonic to Uint8Array (seed)
const seed = mnemonicToUint8Array(mnemonic);
console.log('Seed (first 10 bytes):', Buffer.from(seed.slice(0, 10)).toString('hex'));
// Create a Falcon instance
const falcon = new Falcon();
await falcon.init();
// Generate a key pair using the seed
await falcon.genkey(seed);
// Get the key pair
const keypair = await falcon.getKeypair();
const publicKey = keypair.pk;
const privateKey = keypair.sk;
// Sign a message
const message = 'Hello, Falcon!';
const signature = await falcon.sign(message);
// Verify the signature
const verificationResult = await falcon.verify(message, signature);
console.log('Original signature verification:', verificationResult ? 'Valid' : 'Invalid');
// Export the keys and mnemonic
console.log('Public Key:', Buffer.from(publicKey).toString('hex'));
console.log('Private Key:', Buffer.from(privateKey).toString('hex'));
console.log('Mnemonic:', mnemonic);
// Demonstrate recovery from mnemonic
console.log('\n=== Recovering from mnemonic ===');
const recoveredSeed = mnemonicToUint8Array(mnemonic);
// Create a new Falcon instance
const recoveredFalcon = new Falcon();
await recoveredFalcon.init();
// Generate a key pair using the recovered seed
await recoveredFalcon.genkey(recoveredSeed);
// Get the recovered key pair
const recoveredKeypair = await recoveredFalcon.getKeypair();
const recoveredPublicKey = recoveredKeypair.pk;
const recoveredPrivateKey = recoveredKeypair.sk;
// Verify that the recovered keys match the original
const publicKeysMatch = Buffer.from(publicKey).toString('hex') ===
Buffer.from(recoveredPublicKey).toString('hex');
const privateKeysMatch = Buffer.from(privateKey).toString('hex') ===
Buffer.from(recoveredPrivateKey).toString('hex');
console.log('Recovery successful:', publicKeysMatch && privateKeysMatch);
console.log('Public keys match:', publicKeysMatch);
console.log('Private keys match:', privateKeysMatch);
// Test signing and verification with recovered keys
console.log('\n=== Testing Recovered Keys ===');
const newMessage = 'Testing recovered keys!';
// Sign with recovered keys
const newSignature = await recoveredFalcon.sign(newMessage);
console.log('Signed message with recovered keys');
// Verify with recovered keys
const newVerificationResult = await recoveredFalcon.verify(newMessage, newSignature);
console.log('New signature verification:', newVerificationResult ? 'Valid' : 'Invalid');
// Cross-verify: verify new signature with original instance
const crossVerificationResult = await falcon.verify(newMessage, newSignature);
console.log('Cross-verification (original instance):', crossVerificationResult ? 'Valid' : 'Invalid');
}
// Run the example
mnemonicExample().catch(console.error);