node-qrcode-lite/lib/core/galois-field.js
Vincenzo Greco 77064f5e5e Refactor/core ()
* Split core lib into multiple files

* Refactor data encoding methods

* Refactor data masking process

* Improve qr code generation process

* Increase minimum required node version to 0.10

* Add linter

* Add tests and tests coverage

* Update travis config to fix compilation issues

* Add examples folder

* Add missing license tag in package.json

* Update build script and add sourcemap support

* Publish only strictly needed files on npm

* Update readme
2016-12-16 23:45:08 +01:00

72 lines
1.9 KiB
JavaScript

var Buffer = require('../utils/buffer')
var EXP_TABLE = new Buffer(512)
var LOG_TABLE = new Buffer(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 () {
var x = 1
for (var 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 (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]]
}