54a96d07ba4edd611b23d3df6aa8d6c03f14a03c8b6a7c9bb48661b09c72ecc0.json raw
1 {"ast":null,"code":"const GF = require('./galois-field');\n\n/**\n * Multiplies two polynomials inside Galois Field\n *\n * @param {Uint8Array} p1 Polynomial\n * @param {Uint8Array} p2 Polynomial\n * @return {Uint8Array} Product of p1 and p2\n */\nexports.mul = function mul(p1, p2) {\n const coeff = new Uint8Array(p1.length + p2.length - 1);\n for (let i = 0; i < p1.length; i++) {\n for (let j = 0; j < p2.length; j++) {\n coeff[i + j] ^= GF.mul(p1[i], p2[j]);\n }\n }\n return coeff;\n};\n\n/**\n * Calculate the remainder of polynomials division\n *\n * @param {Uint8Array} divident Polynomial\n * @param {Uint8Array} divisor Polynomial\n * @return {Uint8Array} Remainder\n */\nexports.mod = function mod(divident, divisor) {\n let result = new Uint8Array(divident);\n while (result.length - divisor.length >= 0) {\n const coeff = result[0];\n for (let i = 0; i < divisor.length; i++) {\n result[i] ^= GF.mul(divisor[i], coeff);\n }\n\n // remove all zeros from buffer head\n let offset = 0;\n while (offset < result.length && result[offset] === 0) offset++;\n result = result.slice(offset);\n }\n return result;\n};\n\n/**\n * Generate an irreducible generator polynomial of specified degree\n * (used by Reed-Solomon encoder)\n *\n * @param {Number} degree Degree of the generator polynomial\n * @return {Uint8Array} Buffer containing polynomial coefficients\n */\nexports.generateECPolynomial = function generateECPolynomial(degree) {\n let poly = new Uint8Array([1]);\n for (let i = 0; i < degree; i++) {\n poly = exports.mul(poly, new Uint8Array([1, GF.exp(i)]));\n }\n return poly;\n};","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}