keygentoolshed
Version:
Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3
65 lines (52 loc) • 1.85 kB
JavaScript
import { program } from 'commander';
// Function to compute modular inverse using Extended Euclidean Algorithm
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;
}
function reconstructSecret(shares) {
const threshold = shares.length;
const secret = shares.reduce((acc, { share, index }) => {
let product = shares.reduce((prod, { index: j }) => {
if (index !== j) {
// Calculate the Lagrange basis polynomial
return (prod * (0 - j) * modInverse(index - j, 256)) % 256; // Use multiplication and modular inverse
}
return prod;
}, 1);
const term = (share * product) % 256; // Calculate the term for this share
console.log(`Share: ${share}, Index: ${index}, Term: ${term}`); // Log the term for each share
return (acc + term) % 256;
}, 0);
return secret;
}
function main() {
program
.option('--shares <shares>', 'Comma-separated list of shares in the format index:share', (value) => {
return value.split(',').map(pair => {
const [index, share] = pair.split(':').map(Number);
return { index, share };
});
});
program.parse(process.argv);
const options = program.opts();
if (!options.shares) {
console.error('Please provide shares in the format index:share.');
process.exit(1);
}
const secret = reconstructSecret(options.shares);
console.log('Reconstructed Secret:', secret);
}
main();