const EXP_TABLE = new Uint8Array(512) const LOG_TABLE = new Uint8Array(256) /** * Precompute the log and anti-log tables for faster computation later * * For each possible value in the galois field 2^8, we will pre-compute * the logarithm and anti-logarithm (exponential) of this value * * ref {@link https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders#Introduction_to_mathematical_fields} */ ;(function initTables () { let x = 1 for (let i = 0; i < 255; i++) { EXP_TABLE[i] = x LOG_TABLE[x] = i x <<= 1 // multiply by 2 // The QR code specification says to use byte-wise modulo 100011101 arithmetic. // This means that when a number is 256 or larger, it should be XORed with 0x11D. if (x & 0x100) { // similar to x >= 256, but a lot faster (because 0x100 == 256) x ^= 0x11D } } // Optimization: double the size of the anti-log table so that we don't need to mod 255 to // stay inside the bounds (because we will mainly use this table for the multiplication of // two GF numbers, no more). // @see {@link mul} for (let i = 255; i < 512; i++) { EXP_TABLE[i] = EXP_TABLE[i - 255] } }()) /** * Returns log value of n inside Galois Field * * @param {Number} n * @return {Number} */ exports.log = function log (n) { if (n < 1) throw new Error('log(' + n + ')') return LOG_TABLE[n] } /** * Returns anti-log value of n inside Galois Field * * @param {Number} n * @return {Number} */ exports.exp = function exp (n) { return EXP_TABLE[n] } /** * Multiplies two number inside Galois Field * * @param {Number} x * @param {Number} y * @return {Number} */ exports.mul = function mul (x, y) { if (x === 0 || y === 0) return 0 // should be EXP_TABLE[(LOG_TABLE[x] + LOG_TABLE[y]) % 255] if EXP_TABLE wasn't oversized // @see {@link initTables} return EXP_TABLE[LOG_TABLE[x] + LOG_TABLE[y]] }