shamirs-secret-sharing-ts
Version:
A simple implementation of Shamir's Secret Sharing configured to use a finite field in GF(2^8) with 128 bit padding
29 lines (28 loc) • 945 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const table_1 = require("./table");
const constants_1 = require("./constants");
function lagrange(x, p) {
const n = constants_1.MAX_SHARES;
let product = 0;
let sum = 0;
for (let i = 0; i < p[0].length; ++i) {
if (p[1][i]) {
product = table_1.logs[p[1][i]];
for (let j = 0; j < p[0].length; ++j) {
// m != j
if (i !== j) {
if (x === p[0][j]) {
product = -1;
break;
}
const a = table_1.logs[x ^ p[0][j]] - table_1.logs[p[0][i] ^ p[0][j]];
product = (product + a + n) % n;
}
}
sum = -1 === sum ? sum : sum ^ table_1.exps[product];
}
}
return sum;
}
exports.lagrange = lagrange;