@cloudflare/zkp-ecdsa
Version:
zkp-ecdsa: A Typescript Implementation of ZKAttest
46 lines • 1.35 kB
JavaScript
import { invMod, posMod } from '../bignum/big.js';
function eval_poly(coeff, x, m) {
let ret = BigInt(0);
for (let i = coeff.length - 1; i >= 0; i--) {
ret = posMod(coeff[i] + x * ret, m);
}
return ret;
}
export function interpolate(x, y, m) {
if (x.length != y.length) {
throw new Error('inconsistent args');
}
const n = x.length, s = [], coeff = [];
for (let i = 0; i < n; i++) {
s[i] = BigInt(0);
coeff[i] = BigInt(0);
}
s[n] = BigInt(1);
s[n - 1] = -x[0] % m;
for (let i = 1; i < n; i++) {
for (let j = n - i - 1; j < n - 1; j++) {
s[j] = (s[j] - x[i] * s[j + 1]) % m;
}
s[n - 1] = (s[n - 1] - x[i]) % m;
}
for (let i = 0; i < n; i++) {
let phi = BigInt(0);
for (let j = n; j >= 1; j--) {
phi = BigInt(j) * s[j] + x[i] * phi;
}
phi = posMod(phi, m);
const ff = invMod(phi, m) % m;
let b = BigInt(1);
for (let j = n - 1; j >= 0; j--) {
coeff[j] = posMod(coeff[j] + b * ff * y[i], m);
b = s[j] + x[i] * b;
}
}
for (let i = 0; i < n; i++) {
if (y[i] != eval_poly(coeff, x[i], m)) {
throw new Error('incorrect interpolation');
}
}
return coeff;
}
//# sourceMappingURL=interpolate.js.map