From 50c1a6b8af65d9068dba60fc8b2c86f8818515c8 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 8 May 2017 05:21:05 +0000 Subject: [PATCH] Simplified a few lines of code in constructor of ReedSolomonGenerator in all language versions. --- c/qrcodegen.c | 6 +++--- cpp/QrCode.cpp | 6 +++--- java/io/nayuki/qrcodegen/QrCode.java | 2 +- javascript/qrcodegen.js | 2 +- python/qrcodegen.py | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index a1466d0..45373e8 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -433,15 +433,15 @@ testable void calcReedSolomonGenerator(int degree, uint8_t result[]) { // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), // drop the highest term, and store the rest of the coefficients in order of descending powers. // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). - int root = 1; + uint8_t root = 1; for (int i = 0; i < degree; i++) { // Multiply the current product by (x - r^i) for (int j = 0; j < degree; j++) { - result[j] = finiteFieldMultiply(result[j], (uint8_t)root); + result[j] = finiteFieldMultiply(result[j], root); if (j + 1 < degree) result[j] ^= result[j + 1]; } - root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D) + root = finiteFieldMultiply(root, 0x02); } } diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 8448c14..579c30f 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -580,15 +580,15 @@ QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) : // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), // drop the highest term, and store the rest of the coefficients in order of descending powers. // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). - int root = 1; + uint8_t root = 1; for (int i = 0; i < degree; i++) { // Multiply the current product by (x - r^i) for (size_t j = 0; j < coefficients.size(); j++) { - coefficients.at(j) = multiply(coefficients.at(j), static_cast(root)); + coefficients.at(j) = multiply(coefficients.at(j), root); if (j + 1 < coefficients.size()) coefficients.at(j) ^= coefficients.at(j + 1); } - root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D) + root = multiply(root, 0x02); } } diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index 27befd5..0b8646c 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -811,7 +811,7 @@ public final class QrCode { if (j + 1 < coefficients.length) coefficients[j] ^= coefficients[j + 1]; } - root = (root << 1) ^ ((root >>> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D) + root = multiply(root, 0x02); } } diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 405432e..09b7ad0 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -932,7 +932,7 @@ var qrcodegen = new function() { if (j + 1 < coefficients.length) coefficients[j] ^= coefficients[j + 1]; } - root = (root << 1) ^ ((root >>> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D) + root = ReedSolomonGenerator.multiply(root, 0x02); } // Computes and returns the Reed-Solomon error correction codewords for the given sequence of data codewords. diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 7edb2b3..4a0c59a 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -779,7 +779,7 @@ class _ReedSolomonGenerator(object): self.coefficients[j] = _ReedSolomonGenerator.multiply(self.coefficients[j], root) if j + 1 < degree: self.coefficients[j] ^= self.coefficients[j + 1] - root = (root << 1) ^ ((root >> 7) * 0x11D) # Multiply by 0x02 mod GF(2^8/0x11D) + root = _ReedSolomonGenerator.multiply(root, 0x02) def get_remainder(self, data):