Skip to content

Commit

Permalink
Update sss.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
RDxR10 authored Dec 21, 2024
1 parent 9a02dab commit 374ac59
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions examples/scripts/sss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@
* @run <url>
* @group Cryptography
*
* This example demonstrates Shamir's Secret Sharing, where a secret, split into shares, allows its reconstruction only when a sufficient number of shares are combined.
* Implementation of Shamir's Secret Sharing where a secret, split into shares,
* allows its reconstruction only when a sufficient number of shares are combined.
*/

// Random Number Generation within a range
async function getSecureRandom(min: number, max: number): Promise<number> {
const range = max - min;
const buffer = new Uint8Array(4);
crypto.getRandomValues(buffer);
crypto.getRandomValues(buffer);
const randomValue = new DataView(buffer.buffer).getUint32(0);
return min + (randomValue % range);
}

// Finding the value of the polynomial at x
function evaluatePolynomial(coefficients: number[], x: number): number {
return coefficients.reduce((result, coeff, power) =>
result + coeff * Math.pow(x, power), 0);
return coefficients.reduce(
(result, coeff, power) => result + coeff * Math.pow(x, power),
0,
);
}

// Generates shares based on the secret and threshold
async function generateShares(secret: number, totalShares: number, threshold: number) {
async function generateShares(
secret: number,
totalShares: number,
threshold: number,
) {
// Generate random coefficients for the polynomial
const coefficients = [secret];
for (let i = 1; i < threshold; i++) {
coefficients.push(await getSecureRandom(1, 1000));
coefficients.push(await getSecureRandom(1, 1000));
}

const usedXValues = new Set<number>();
Expand All @@ -53,27 +60,27 @@ async function generateShares(secret: number, totalShares: number, threshold: nu
function reconstructSecret(shares: Array<{ x: number; y: number }>): number {
const secret = shares.reduce((sum, share, i) => {
let product = share.y;

for (let j = 0; j < shares.length; j++) {
if (i !== j) {
product *= shares[j].x / (shares[j].x - share.x);
}
}

return sum + product;
}, 0);

return Math.round(secret);
}



const secret = 12345;
const totalShares = 5;
const threshold = 3;

// Generate shares
const { shares, coefficients } = await generateShares(secret, totalShares, threshold);
const { shares, coefficients } = await generateShares(
secret,
totalShares,
threshold,
);
console.log("Generated Shares:", shares);

// Select random subset of shares to reconstruct the secret
Expand All @@ -82,7 +89,7 @@ while (selectedIndices.size < threshold) {
selectedIndices.add(await getSecureRandom(0, totalShares));
}

const selectedShares = Array.from(selectedIndices).map(index => shares[index]);
const selectedShares = Array.from(selectedIndices).map((index) => shares[index]);
console.log("Selected Shares for Reconstruction:", selectedShares);

// Reconstruct the secret
Expand Down

0 comments on commit 374ac59

Please sign in to comment.