Skip to content

Commit

Permalink
Move the addOrSubstract function to the Galois Field prototype
Browse files Browse the repository at this point in the history
This way the polynomial (GFPoly) delegates the add/substract operation in his own field instead a external dep.
This removes a circular dependence too.
  • Loading branch information
IagoLast committed Dec 10, 2016
1 parent c72886b commit 67849b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/gf256.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ GF256.prototype.inverse = function(a) {
return this.expTable[255 - this.logTable[a]];
};

GF256.prototype.addOrSubtract = function(a, b) {
return a ^ b;
};

GF256.prototype.multiply = function(a, b) {
if (a == 0 || b == 0) {
return 0;
Expand All @@ -103,7 +107,3 @@ GF256.prototype.multiply = function(a, b) {

GF256.QR_CODE_FIELD = new GF256(0x011D);
GF256.DATA_MATRIX_FIELD = new GF256(0x012D);

GF256.addOrSubtract = function(a, b) {
return a ^ b;
};
10 changes: 4 additions & 6 deletions src/gf256poly.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
* limitations under the License.
*/

import GF256 from './gf256';

export default function GF256Poly(field, coefficients) {
if (coefficients == null || coefficients.length == 0) {
throw "System.ArgumentException";
Expand Down Expand Up @@ -74,13 +72,13 @@ GF256Poly.prototype.evaluateAt = function(a) {
// Just the sum of the coefficients
var result = 0;
for (var i = 0; i < size; i++) {
result = GF256.addOrSubtract(result, this.coefficients[i]);
result = this.field.addOrSubtract(result, this.coefficients[i]);
}
return result;
}
var result2 = this.coefficients[0];
for (var i = 1; i < size; i++) {
result2 = GF256.addOrSubtract(this.field.multiply(a, result2), this.coefficients[i]);
result2 = this.field.addOrSubtract(this.field.multiply(a, result2), this.coefficients[i]);
}
return result2;
};
Expand Down Expand Up @@ -109,7 +107,7 @@ GF256Poly.prototype.addOrSubtract = function(other) {
for (var ci = 0; ci < lengthDiff; ci++)sumDiff[ci] = largerCoefficients[ci];

for (var i = lengthDiff; i < largerCoefficients.length; i++) {
sumDiff[i] = GF256.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
sumDiff[i] = this.field.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]);
}

return new GF256Poly(this.field, sumDiff);
Expand All @@ -130,7 +128,7 @@ GF256Poly.prototype.multiply1 = function(other) {
for (var i = 0; i < aLength; i++) {
var aCoeff = aCoefficients[i];
for (var j = 0; j < bLength; j++) {
product[i + j] = GF256.addOrSubtract(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
product[i + j] = this.field.addOrSubtract(product[i + j], this.field.multiply(aCoeff, bCoefficients[j]));
}
}
return new GF256Poly(this.field, product);
Expand Down

0 comments on commit 67849b9

Please sign in to comment.