UNPKG

crypto-keygen-suite

Version:

Key generation utilities for cryptographic operations. YES I RENAMED IT OMG BASES AND PROTOCOLS!!! See its folder for all <3

47 lines (38 loc) 1.77 kB
import { program } from 'commander'; // Function to generate a random polynomial of given degree with the secret as the constant term function generatePolynomial(degree, secret) { const coefficients = Array.from({ length: degree }, () => Math.floor(Math.random() * 256)); coefficients.push(secret); // The secret is the constant term return coefficients; } // Function to evaluate the polynomial at a given x value function evaluatePolynomial(coefficients, x) { return coefficients.reduce((acc, coeff, index) => { return (acc + coeff * Math.pow(x, index)) % 256; // Modulo 256 for simplicity }, 0); } // Function to distribute shares among parties function distributeShares(secret, totalShares, threshold) { const polynomial = generatePolynomial(threshold - 1, secret); const shares = []; for (let i = 1; i <= totalShares; i++) { const shareValue = evaluatePolynomial(polynomial, i); shares.push({ index: i, share: shareValue }); } return shares; } // Main function to handle command-line arguments function main() { program .requiredOption('--secret <number>', 'The secret to be shared') .requiredOption('--totalShares <number>', 'Total number of shares to create') .requiredOption('--threshold <number>', 'Minimum number of shares needed to reconstruct the secret') .parse(process.argv); const options = program.opts(); const secret = parseInt(options.secret, 10); const totalShares = parseInt(options.totalShares, 10); const threshold = parseInt(options.threshold, 10); const shares = distributeShares(secret, totalShares, threshold); console.log("Distributed Shares:", shares); } main();