UNPKG

keygentoolshed

Version:

Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3

71 lines (55 loc) 1.85 kB
import fs from 'fs'; import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; function computeSecret(shares) { const k = shares.length; let secret = 0; for (let i = 0; i < k; i++) { const [x_i, y_i] = shares[i]; let numerator = 1; let denominator = 1; for (let j = 0; j < k; j++) { if (i !== j) { const [x_j] = shares[j]; numerator = (numerator * (-x_j)) % 256; // Modulo can be adjusted based on the secret space denominator = (denominator * (x_i - x_j)) % 256; // Modulo can be adjusted based on the secret space } } const lagrangeBasis = (numerator * modInverse(denominator, 256)) % 256; // Modulo can be adjusted based on the secret space secret = (secret + (lagrangeBasis * y_i)) % 256; // Modulo can be adjusted based on the secret space } return secret; } function modInverse(a, m) { let m0 = m, t, q; let x0 = 0, x1 = 1; if (m === 1) return 0; while (a > 1) { q = Math.floor(a / m); t = m; m = a % m; a = t; t = x0; x0 = x1 - q * x0; x1 = t; } if (x1 < 0) x1 += m0; return x1; } const argv = yargs(hideBin(process.argv)) .usage('Usage: $0 --secret [shares]') .option('secret', { alias: 's', describe: 'Specify the shares in the format x1:y1,x2:y2,...', type: 'string', demandOption: true }) .help() .argv; const sharesInput = argv.secret.split(',').map(share => { const [x, y] = share.split(':').map(Number); return [x, y]; }); // Compute the secret const secret = computeSecret(sharesInput); console.log(`Reconstructed secret: ${secret}`);