From d513671a8f22d89fd943ce39da6baff787a5ac45 Mon Sep 17 00:00:00 2001 From: John Collomosse Date: Tue, 3 Nov 2026 06:17:07 +0000 Subject: [PATCH 1/4] fix: bch validation edge case in python --- python/trustmark/bchecc.py | 12 ++++++++++-- python/trustmark/datalayer.py | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/python/trustmark/bchecc.py b/python/trustmark/bchecc.py index 90daffe..f9730b5 100644 --- a/python/trustmark/bchecc.py +++ b/python/trustmark/bchecc.py @@ -160,6 +160,7 @@ def get_ecc_bytes(self): def decode(self,data,recvecc): calc_ecc=self.encode(data) + corrected_ecc=bytearray(recvecc) self.ECCstate.errloc=[] @@ -260,6 +261,8 @@ def decode(self,data,recvecc): nroots = self.getroots(len(data),self.ECCstate.elp) + if nroots != self.ECCstate.elp.deg or not 0 <= nroots <= t: + return -1 datalen=len(data) nbits=(datalen*8)+self.ECCstate.ecc_bits @@ -273,12 +276,17 @@ def decode(self,data,recvecc): for bitflip in self.ECCstate.errloc: byte= int (bitflip / 8) bit = pow(2,(bitflip & 7)) - if bitflip < (len(data)+len(recvecc))*8: + if bitflip < (len(data)+len(corrected_ecc))*8: if byte> pad_e) != (int.from_bytes(corrected_ecc, 'big') >> pad_e): + return -1 return nroots diff --git a/python/trustmark/datalayer.py b/python/trustmark/datalayer.py index 2519201..f856ddf 100644 --- a/python/trustmark/datalayer.py +++ b/python/trustmark/datalayer.py @@ -188,6 +188,9 @@ def _decode_text(self, packet: np.array, MODE): bitflips = bch_decoder.decode(data, ecc) else: bitflips = -1 + # Byte alignment adds zero bits that were never transmitted. + if bitflips != -1 and pad_d and data[-1] & ((1 << pad_d)-1): + bitflips = -1 # print('Bitflips = %d' % bitflips) if bitflips == -1: if MODE=='text': From 66c4407add13d65fc3587336c3d621034816c6f4 Mon Sep 17 00:00:00 2001 From: John Collomosse Date: Tue, 3 Nov 2026 06:57:35 +0000 Subject: [PATCH 2/4] fix: bch validation edge case in python-hailo --- python-hailo/trustmark_hailo/bchecc.py | 12 ++++++++++-- python-hailo/trustmark_hailo/datalayer.py | 3 +++ python/CHANGELOG.md | 5 +++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python-hailo/trustmark_hailo/bchecc.py b/python-hailo/trustmark_hailo/bchecc.py index e282057..789d6f4 100644 --- a/python-hailo/trustmark_hailo/bchecc.py +++ b/python-hailo/trustmark_hailo/bchecc.py @@ -162,6 +162,7 @@ def get_ecc_bytes(self): def decode(self,data,recvecc): calc_ecc=self.encode(data) + corrected_ecc=bytearray(recvecc) self.ECCstate.errloc=[] @@ -262,6 +263,8 @@ def decode(self,data,recvecc): nroots = self.getroots(len(data),self.ECCstate.elp) + if nroots != self.ECCstate.elp.deg or not 0 <= nroots <= t: + return -1 datalen=len(data) nbits=(datalen*8)+self.ECCstate.ecc_bits @@ -275,12 +278,17 @@ def decode(self,data,recvecc): for bitflip in self.ECCstate.errloc: byte= int (bitflip / 8) bit = pow(2,(bitflip & 7)) - if bitflip < (len(data)+len(recvecc))*8: + if bitflip < (len(data)+len(corrected_ecc))*8: if byte> pad_e) != (int.from_bytes(corrected_ecc, 'big') >> pad_e): + return -1 return nroots diff --git a/python-hailo/trustmark_hailo/datalayer.py b/python-hailo/trustmark_hailo/datalayer.py index b5c0ad6..9dcc44c 100644 --- a/python-hailo/trustmark_hailo/datalayer.py +++ b/python-hailo/trustmark_hailo/datalayer.py @@ -188,6 +188,9 @@ def _decode_text(self, packet: np.array, MODE): bitflips = bch_decoder.decode(data, ecc) else: bitflips = -1 + # Byte alignment adds zero bits that were never transmitted. + if bitflips != -1 and pad_d and data[-1] & ((1 << pad_d)-1): + bitflips = -1 # print('Bitflips = %d' % bitflips) if bitflips == -1: if MODE=='text': diff --git a/python/CHANGELOG.md b/python/CHANGELOG.md index c3f269b..8a34f8e 100644 --- a/python/CHANGELOG.md +++ b/python/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -All notable changes to this project are documented in this file. +## [0.9.3] - 2026-03-11 -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +### Changed +- Validation edge case fixed in BCH error correction library ## [0.9.2] - 2026-09-08 From 73207420dd00f5bf08ec9e8a10edf9d49bfd019e Mon Sep 17 00:00:00 2001 From: John Collomosse Date: Tue, 3 Nov 2026 07:06:05 +0000 Subject: [PATCH 3/4] fix: bch validation edge case in js --- js/deps/bch_ecc.js | 709 +++++++++++++++++++++++++++++++++++++++++ js/deps/bch_ecc.min.js | 3 +- 2 files changed, 710 insertions(+), 2 deletions(-) create mode 100644 js/deps/bch_ecc.js diff --git a/js/deps/bch_ecc.js b/js/deps/bch_ecc.js new file mode 100644 index 0000000..11eb6c2 --- /dev/null +++ b/js/deps/bch_ecc.js @@ -0,0 +1,709 @@ +/*! + * BCH ECC Library + * Copyright 2024 Adobe. All rights reserved. + * Licensed under the MIT License. + * + * NOTICE: Adobe permits you to use, modify, and distribute this file in + * accordance with the terms of the Adobe license agreement accompanying it. + */ + +function uint8ToBoolean(u8array) { + const resultArray = []; + + for (let i = 0; i < u8array.length; i++) { + let c = u8array[i]; + resultArray.push((c & 0x80) > 0); + resultArray.push((c & 0x40) > 0); + resultArray.push((c & 0x20) > 0); + resultArray.push((c & 0x10) > 0); + resultArray.push((c & 0x8) > 0); + resultArray.push((c & 0x4) > 0); + resultArray.push((c & 0x2) > 0); + resultArray.push((c & 0x1) > 0); + } + return resultArray; +} + +function load4bytes(data) { + let w = BigInt(0); + if (data.length > 0) w += data[0] << BigInt(24); + if (data.length > 1) w += data[1] << BigInt(16); + if (data.length > 2) w += data[2] << BigInt(8); + if (data.length > 3) w += data[3] << BigInt(0); + return w; +} + +function loadwordfrombits(data, lim) { + // for example 8 or 32 + let w = BigInt(0); + if (lim < data.length) lim = data.length; + let pos = lim - 1; + for (let b = 0; b < lim; b++) { + if (data[b]) w += BigInt(1) << BigInt(pos); + pos--; + } + return w; +} + +function loadallwords(data, lim) { + const datalen = data.length; + let mlen = Math.floor(datalen / lim); // how many whole words + let eccbuf = []; + let offset = 0; + while (mlen > 0) { + let w = loadwordfrombits(data.slice(offset, offset + lim), lim); + eccbuf.push(w); + offset += lim; + mlen--; + } + data = data.slice(offset, data.length); + let leftdata = data.length; + if (leftdata > 0) { + let w = loadwordfrombits(data, lim); + eccbuf.push(w); + } + return eccbuf; +} + +function BCH_Encode(self, databits) { + let data = loadallwords(databits, 8); + const datalen = data.length; + const l = ceilop(self.ECCstate.m * self.ECCstate.t, 32) - 1; + let ecc = Array(self.ECCstate.ecc_bytes).fill(0); + const ecc_max_words = ceilop(31 * 64, 32); + let r = Array(ecc_max_words).fill(BigInt(0)); + + const tab0idx = 0; + const tab1idx = tab0idx + 256 * (l + 1); + const tab2idx = tab1idx + 256 * (l + 1); + const tab3idx = tab2idx + 256 * (l + 1); + + let mlen = Math.floor(datalen / 4); + let offset = 0; + while (mlen > 0) { + let w = load4bytes(data.slice(offset, offset + 4)); + w = w ^ r[0]; + const p0 = tab0idx + (l + 1) * Number((w >> BigInt(0)) & BigInt(0xff)); + const p1 = tab1idx + (l + 1) * Number((w >> BigInt(8)) & BigInt(0xff)); + const p2 = tab2idx + (l + 1) * Number((w >> BigInt(16)) & BigInt(0xff)); + const p3 = tab3idx + (l + 1) * Number((w >> BigInt(24)) & BigInt(0xff)); + + for (let i = 0; i < l; i++) { + r[i] = + r[i + 1] ^ + self.ECCstate.cyclic_tab[p0 + i] ^ + self.ECCstate.cyclic_tab[p1 + i] ^ + self.ECCstate.cyclic_tab[p2 + i] ^ + self.ECCstate.cyclic_tab[p3 + i]; + } + + r[l] = + self.ECCstate.cyclic_tab[p0 + l] ^ + self.ECCstate.cyclic_tab[p1 + l] ^ + self.ECCstate.cyclic_tab[p2 + l] ^ + self.ECCstate.cyclic_tab[p3 + l]; + mlen--; + offset += 4; + } + + data = data.slice(offset, data.length); + let leftdata = data.length; + ecc = r; + + let posn = 0; + while (leftdata) { + const tmp = data[posn]; + posn++; + let pidx = (l + 1) * Number(((ecc[0] >> BigInt(24)) ^ tmp) & BigInt(0xff)); + for (let i = 0; i < l; i++) { + ecc[i] = + (((ecc[i] << BigInt(8)) & BigInt(0xffffffff)) | + (ecc[i + 1] >> BigInt(24))) ^ + self.ECCstate.cyclic_tab[pidx]; + pidx++; + } + ecc[l] = + ((ecc[l] << BigInt(8)) & BigInt(0xffffffff)) ^ + self.ECCstate.cyclic_tab[pidx]; + leftdata--; + } + + self.ECCstate.ecc_buf = ecc; + let eccout = []; + for (let idx = 0; idx < r.length; idx++) { + const e = r[idx]; + eccout.push(Number(e >> BigInt(24)) & 0xff); + eccout.push(Number(e >> BigInt(16)) & 0xff); + eccout.push(Number(e >> BigInt(8)) & 0xff); + eccout.push(Number(e >> BigInt(0)) & 0xff); + } + + eccout = eccout.slice(0, self.ECCstate.ecc_bytes); + + return eccout; +} + +function int8_stringify(data) { + return data.map((num) => String.fromCharCode(Number(num))).join(""); +} + +function toBinaryString(num, length) { + // Convert the number to a binary string + let binaryString = num.toString(2); + + // Pad with leading zeros to the desired length + while (binaryString.length < length) { + binaryString = "0" + binaryString; + } + + return binaryString; +} + +function genBinString(dataout, datalen) { + let out = ""; + for (let i = 0; i < dataout.length; i++) { + out += toBinaryString(dataout[i], 8); + } + out = out.slice(0, datalen); + return out; +} + +function bin_stringify(data) { + return data.join(""); +} + +function deepcopy(poly) { + const newpoly = {}; + newpoly.deg = poly.deg; + newpoly.c = Array.from(poly.c); + return newpoly; +} + +function BCH_Decode(self, data, recvecc) { + const calc_ecc = BCH_Encode(self, data); + + const ecclen = recvecc.length; + const eccbuf = loadallwords(recvecc, 32); + + const eccwords = ceilop(self.ECCstate.m * self.ECCstate.t, 32); + + let sum = BigInt(0); + for (let i = 0; i < eccwords; i++) { + self.ECCstate.ecc_buf[i] = self.ECCstate.ecc_buf[i] ^ eccbuf[i]; + sum = sum | self.ECCstate.ecc_buf[i]; + } + + let dataout = loadallwords(data, 8); + if (sum === 0) { + const retdata = Object(); + retdata.data_ascii = int8_stringify(dataout); + retdata.data_binary = bin_stringify(data); + retdata.bitflips = 0; + retdata.valid = true; + return retdata; + } + + let s = self.ECCstate.ecc_bits; + const t = self.ECCstate.t; + const syn = Array(2 * t).fill(0); + + let m = s & 31; + let synbuf = self.ECCstate.ecc_buf; + + if (m) { + const newval = + synbuf[Math.floor(s / 32)] & ~BigInt(Math.pow(2, Number(32 - m)) - 1); + synbuf[Math.floor(s / 32)] = newval; + } + + let synptr = 0; + while (s > 0 || synptr === 0) { + let poly = synbuf[synptr]; + synptr += 1; + s -= 32; + while (poly) { + const i = degBigInt(self, poly); + for (let j = 0; j < 2 * t; j += 2) { + syn[j] = syn[j] ^ g_pow(self, (j + 1) * (i + s)); + } + poly = poly ^ BigInt(Math.pow(2, i)); + } + } + + for (let i = 0; i < t; i++) { + syn[2 * i + 1] = g_sqrt(self, syn[i]); + } + + const n = self.ECCstate.n; + let pp = -1; + let pd = 1; + + let pelp = Object(); + pelp.deg = 0; + pelp.c = Array(2 * t).fill(0); + pelp.c[0] = 1; + + let elp = Object(); + elp.deg = 0; + elp.c = Array(2 * t).fill(0); + elp.c[0] = 1; + + let d = syn[0]; + let elp_copy = Object(); + for (let i = 0; i < t; i++) { + if (elp.deg > t) { + break; + } + if (d) { + const k = 2 * i - pp; + elp_copy = deepcopy(elp); + const tmp = g_log(self, d) + n - g_log(self, pd); + for (let j = 0; j < pelp.deg + 1; j++) { + if (pelp.c[j]) { + const l = g_log(self, pelp.c[j]); + elp.c[j + k] = elp.c[j + k] ^ g_pow(self, tmp + l); + } + } + + const tmpDeg = pelp.deg + k; + if (tmpDeg > elp.deg) { + elp.deg = tmpDeg; + pelp = deepcopy(elp_copy); + pd = d; + pp = 2 * i; + } + } + if (i < t - 1) { + d = syn[2 * i + 2]; + for (let j = 1; j < elp.deg + 1; j++) { + d = d ^ g_mul(self, elp.c[j], syn[2 * i + 2 - j]); + } + } + } + self.ECCstate.elp = elp; + + const nroots = getroots(self, dataout.length, self.ECCstate.elp); + dataout = loadallwords(data, 8); + const recveccout = loadallwords(recvecc, 8); + const datalen = dataout.length; + const nbits = datalen * 8 + self.ECCstate.ecc_bits; + + const retdata = Object(); + retdata.bitflips = nroots; + retdata.valid = false; + + if (nroots !== self.ECCstate.elp.deg || nroots < 0 || nroots > t) { + retdata.bitflips = -1; + return retdata; + } + + for (let i = 0; i < nroots; i++) { + if (self.ECCstate.errloc[i] >= nbits) { + return retdata; // fail + } + self.ECCstate.errloc[i] = nbits - 1 - self.ECCstate.errloc[i]; + self.ECCstate.errloc[i] = + (self.ECCstate.errloc[i] & ~7) | (7 - (self.ECCstate.errloc[i] & 7)); + } + + for (let bidx = 0; bidx < self.ECCstate.errloc.length; bidx++) { + const bitflip = Number(self.ECCstate.errloc[bidx]); + + const byte = Math.floor(bitflip / 8); + const bit = Math.pow(2, bitflip & 7); + if (bitflip < (dataout.length + recveccout.length) * 8) { + if (byte < dataout.length) { + dataout[byte] = dataout[byte] ^ BigInt(bit); + } else { + recveccout[byte - dataout.length] = + recveccout[byte - dataout.length] ^ BigInt(bit); + } + } + } + + // Byte alignment adds zero bits that were never transmitted. + const padding = dataout.length * 8 - data.length; + if ( + padding && + (dataout[dataout.length - 1] & + ((BigInt(1) << BigInt(padding)) - BigInt(1))) !== + BigInt(0) + ) { + retdata.bitflips = -1; + return retdata; + } + + const ds = genBinString(dataout, data.length); + const correctedBits = Array.from(ds, (bit) => bit === "1"); + const reencodedEcc = BCH_Encode(self, correctedBits); + if ( + genBinString(reencodedEcc, self.ECCstate.ecc_bits) !== + genBinString(recveccout, self.ECCstate.ecc_bits) + ) { + retdata.bitflips = -1; + return retdata; + } + + retdata.data_ascii = int8_stringify(dataout); + retdata.data_binary = ds; + retdata.valid = true; + + return retdata; +} + +function getroots(self, k, poly) { + let roots = Array(); + + if (poly.deg > 2) { + k = k * 8 + self.ECCstate.ecc_bits; + + let rep = Array(self.ECCstate.t * 2).fill(0); + let d = poly.deg; + let l = self.ECCstate.n - g_log(self, poly.c[poly.deg]); + for (let i = 0; i < d; i++) { + if (poly.c[i]) { + rep[i] = mod(self, g_log(self, poly.c[i]) + l); + } else { + rep[i] = -1; + } + } + + rep[poly.deg] = 0; + let syn0 = g_div(self, poly.c[0], poly.c[poly.deg]); + for (let i = self.ECCstate.n - k + 1; i < self.ECCstate.n + 1; i++) { + let syn = syn0; + for (let j = 1; j < poly.deg + 1; j++) { + let m = rep[j]; + if (m >= 0) { + syn = syn ^ g_pow(self, m + j * i); + } + } + if (syn == 0) { + roots.push(self.ECCstate.n - i); + if (roots.length == poly.deg) { + break; + } + } + } + if (roots.length < poly.deg) { + self.ECCstate.errloc = []; + return -1; + } + } + + if (poly.deg == 1) { + if (poly.c[0]) { + roots.push( + mod( + self, + self.ECCstate.n - + self.ECCstate.logarithms[poly.c[0]] + + self.ECCstate.logarithms[poly.c[1]], + ), + ); + } + } + + if (poly.deg == 2) { + if (poly.c[0] && poly.c[1]) { + let l0 = self.ECCstate.logarithms[poly.c[0]]; + let l1 = self.ECCstate.logarithms[poly.c[1]]; + let l2 = self.ECCstate.logarithms[poly.c[2]]; + + let u = g_pow(self, l0 + l2 + 2 * (self.ECCstate.n - l1)); + let r = 0; + let v = u; + while (v) { + let i = deg(self, v); + r = r ^ self.ECCstate.elp_pre[i]; + v = v ^ Math.pow(2, i); + } + if ((g_sqrt(self, r) ^ r) == u) { + roots.push( + modn( + self, + 2 * self.ECCstate.n - l1 - self.ECCstate.logarithms[r] + l2, + ), + ); + roots.push( + modn( + self, + 2 * self.ECCstate.n - l1 - self.ECCstate.logarithms[r ^ 1] + l2, + ), + ); + } + } + } + + self.ECCstate.errloc = roots; + return roots.length; +} + +function BCH(t, poly) { + const retBCH = Object(); + retBCH.ECCstate = Object(); + + let tmp = poly; + let m = 0; + while (tmp >> 1) { + tmp = tmp >> 1; + m += 1; + } + + retBCH.ECCstate.m = m; + retBCH.ECCstate.t = t; + retBCH.ECCstate.poly = Object(); + + retBCH.ECCstate.n = Math.pow(2, m) - 1; + let words = ceilop(m * t, 32); + retBCH.ECCstate.ecc_bytes = ceilop(m * t, 8); + retBCH.ECCstate.cyclic_tab = new Array(words * 1024).fill(BigInt(0)); + retBCH.ECCstate.syn = new Array(2 * t).fill(0); + retBCH.ECCstate.elp = new Array(t + 1).fill(0); + retBCH.ECCstate.errloc = new Array(t).fill(0); + + let x = 1; + let k = Math.pow(2, deg(retBCH, poly)); + if (k !== Math.pow(2, retBCH.ECCstate.m)) { + return -1; + } + + retBCH.ECCstate.exponents = new Array(1 + retBCH.ECCstate.n).fill(0); + retBCH.ECCstate.logarithms = new Array(1 + retBCH.ECCstate.n).fill(0); + retBCH.ECCstate.elp_pre = new Array(1 + retBCH.ECCstate.m).fill(0); + + for (let i = 0; i < retBCH.ECCstate.n; i++) { + retBCH.ECCstate.exponents[i] = x; + retBCH.ECCstate.logarithms[x] = i; + if (i && x === 1) { + return -1; + } + x *= 2; + if (x & k) { + x = x ^ poly; + } + } + + retBCH.ECCstate.logarithms[0] = 0; + retBCH.ECCstate.exponents[retBCH.ECCstate.n] = 1; + + let n = 0; + const g = Object(); + g.deg = 0; + g.c = new Array(m * t + 1).fill(0); + let roots = new Array(retBCH.ECCstate.n + 1).fill(0); + let genpoly = new Array(ceilop(m * t + 1, 32)).fill(0); + + for (let i = 0; i < t; i++) { + let r = 2 * i + 1; + for (let j = 0; j < m; j++) { + roots[r] = 1; + r = mod(retBCH, 2 * r); + } + } + + g.deg = 0; + g.c[0] = 1; + for (let i = 0; i < retBCH.ECCstate.n; i++) { + if (roots[i]) { + let r = retBCH.ECCstate.exponents[i]; + g.c[g.deg + 1] = 1; + for (let j = g.deg; j > 0; j--) { + g.c[j] = g_mul(retBCH, g.c[j], r) ^ g.c[j - 1]; + } + g.c[0] = g_mul(retBCH, g.c[0], r); + g.deg += 1; + } + } + + n = g.deg + 1; + let i = 0; + + while (n > 0) { + let nbits; + if (n > 32) { + nbits = 32; + } else { + nbits = n; + } + + let word = BigInt(0); + for (let j = 0; j < nbits; j++) { + if (g.c[n - 1 - j]) { + word = word | BigInt(Math.pow(2, 31 - j)); + } + } + genpoly[i] = word; + i += 1; + n -= nbits; + } + + retBCH.ECCstate.ecc_bits = g.deg; + + build_cyclic(retBCH, genpoly); + + let sum = 0; + let aexp = 0; + for (let i = 0; i < m; i++) { + for (let j = 0; j < m; j++) { + sum = sum ^ g_pow(retBCH, i * Math.pow(2, j)); + } + if (sum) { + aexp = retBCH.ECCstate.exponents[i]; + break; + } + } + + x = 0; + let precomp = new Array(31).fill(0); + let remaining = m; + + while (x <= retBCH.ECCstate.n && remaining) { + let y = g_sqrt(retBCH, x) ^ x; + for (let i = 0; i < 2; i++) { + let r = g_log(retBCH, y); + if (y && r < m && !precomp[r]) { + retBCH.ECCstate.elp_pre[r] = x; + precomp[r] = 1; + remaining -= 1; + break; + } + y = y ^ aexp; + } + x += 1; + } + + return retBCH; +} + +// GALOIS OPERATORS + +function g_inv(self, a) { + return self.ECCstate.exponents[self.ECCstate.n - self.ECCstate.logarithms[a]]; +} + +function g_sqrt(self, a) { + if (a) { + return self.ECCstate.exponents[mod(self, 2 * self.ECCstate.logarithms[a])]; + } else { + return 0; + } +} + +function mod(self, v) { + if (v < self.ECCstate.n) { + return v; + } else { + return v - self.ECCstate.n; + } +} + +function ceilop(a, b) { + return Math.floor((a + b - 1) / b); +} + +function g_mul(self, a, b) { + if (a > 0 && b > 0) { + let res = mod( + self, + self.ECCstate.logarithms[a] + self.ECCstate.logarithms[b], + ); + return self.ECCstate.exponents[res]; + } else { + return 0; + } +} + +function g_div(self, a, b) { + if (a) { + return self.ECCstate.exponents[ + mod( + self, + self.ECCstate.logarithms[a] + + self.ECCstate.n - + self.ECCstate.logarithms[b], + ) + ]; + } else { + return 0; + } +} + +function modn(self, v) { + let n = self.ECCstate.n; + while (v >= n) { + v -= n; + v = (v & n) + (v >> self.ECCstate.m); + } + return v; +} + +function g_log(self, x) { + return self.ECCstate.logarithms[x]; +} + +function a_ilog(self, x) { + return mod(self, self.ECCstate.n - self.ECCstate.logarithms[x]); +} + +function g_pow(self, i) { + return self.ECCstate.exponents[modn(self, i)]; +} + +function deg(self, x) { + let count = 0; + while (x >> 1) { + x = x >> 1; + count += 1; + } + return count; +} + +function degBigInt(self, x) { + let count = 0; + while (x >> BigInt(1)) { + x = x >> BigInt(1); + count += 1; + } + return count; +} + +function build_cyclic(self, g) { + let l = ceilop(self.ECCstate.m * self.ECCstate.t, 32); + let plen = ceilop(self.ECCstate.ecc_bits + 1, 32); + let ecclen = ceilop(self.ECCstate.ecc_bits, 32); + + self.ECCstate.cyclic_tab = new Array(4 * 256 * l).fill(BigInt(0)); + + for (let i = 0; i < 256; i++) { + for (let b = 0; b < 4; b++) { + let offset = (b * 256 + i) * l; + let data = BigInt(BigInt(i) << BigInt(8 * b)); + while (data) { + let d = degBigInt(self, data); + data = data ^ (g[0] >> BigInt(31 - d)); + + for (let j = 0; j < ecclen; j++) { + let hi = 0; + let lo = 0; + if (d < 31) { + hi = BigInt(g[j] << BigInt(d + 1)) & BigInt(0xffffffff); + } else { + hi = BigInt(0); + } + if (j + 1 < plen) { + lo = g[j + 1] >> BigInt(31 - d); + } else { + lo = BigInt(0); + } + let hilo = hi | lo; + if (self.ECCstate.cyclic_tab[j + offset] == 0) { + self.ECCstate.cyclic_tab[j + offset] = BigInt(0); + } + self.ECCstate.cyclic_tab[j + offset] = + self.ECCstate.cyclic_tab[j + offset] ^ hilo; + } + } + } + } +} diff --git a/js/deps/bch_ecc.min.js b/js/deps/bch_ecc.min.js index 509456e..06d1faa 100644 --- a/js/deps/bch_ecc.min.js +++ b/js/deps/bch_ecc.min.js @@ -5,5 +5,4 @@ * * NOTICE: Adobe permits you to use, modify, and distribute this file in * accordance with the terms of the Adobe license agreement accompanying it. - */ -(function(_0x1daaab,_0x3274ab){const _0x26b5c4=a0_0x9a23,_0xd25c03=_0x1daaab();while(!![]){try{const _0x3e84cd=parseInt(_0x26b5c4(0xe3))/0x1*(-parseInt(_0x26b5c4(0xe4))/0x2)+-parseInt(_0x26b5c4(0xef))/0x3*(parseInt(_0x26b5c4(0xe7))/0x4)+parseInt(_0x26b5c4(0xd5))/0x5*(-parseInt(_0x26b5c4(0xda))/0x6)+parseInt(_0x26b5c4(0xeb))/0x7+-parseInt(_0x26b5c4(0xd3))/0x8+parseInt(_0x26b5c4(0xe2))/0x9*(-parseInt(_0x26b5c4(0xe8))/0xa)+parseInt(_0x26b5c4(0xe9))/0xb;if(_0x3e84cd===_0x3274ab)break;else _0xd25c03['push'](_0xd25c03['shift']());}catch(_0x79ab94){_0xd25c03['push'](_0xd25c03['shift']());}}}(a0_0x5c2c,0x884a5));function uint8ToBoolean(_0x237abc){const _0x143a99=a0_0x9a23,_0x17f44d=[];for(let _0x182fd7=0x0;_0x182fd7<_0x237abc[_0x143a99(0xf4)];_0x182fd7++){let _0x2b0f67=_0x237abc[_0x182fd7];_0x17f44d['push']((_0x2b0f67&0x80)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x40)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x20)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x10)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x8)>0x0),_0x17f44d['push']((_0x2b0f67&0x4)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x2)>0x0),_0x17f44d[_0x143a99(0xdc)]((_0x2b0f67&0x1)>0x0);}return _0x17f44d;}function load4bytes(_0xb43d9){const _0xe947b8=a0_0x9a23;let _0x1417cb=BigInt(0x0);if(_0xb43d9['length']>0x0)_0x1417cb+=_0xb43d9[0x0]<0x1)_0x1417cb+=_0xb43d9[0x1]<0x2)_0x1417cb+=_0xb43d9[0x2]<0x3)_0x1417cb+=_0xb43d9[0x3]<0x0){let _0x14958f=loadwordfrombits(_0x8e0a06[_0x2cf722(0xe5)](_0x93d7a3,_0x93d7a3+_0x5e1a58),_0x5e1a58);_0x486f28[_0x2cf722(0xdc)](_0x14958f),_0x93d7a3+=_0x5e1a58,_0x3bad9b--;}_0x8e0a06=_0x8e0a06[_0x2cf722(0xe5)](_0x93d7a3,_0x8e0a06[_0x2cf722(0xf4)]);let _0x314846=_0x8e0a06[_0x2cf722(0xf4)];if(_0x314846>0x0){let _0x5e4e5a=loadwordfrombits(_0x8e0a06,_0x5e1a58);_0x486f28[_0x2cf722(0xdc)](_0x5e4e5a);}return _0x486f28;}function BCH_Encode(_0x4c4102,_0x25a124){const _0x14806d=a0_0x9a23;let _0x56b676=loadallwords(_0x25a124,0x8);const _0xb8b731=_0x56b676[_0x14806d(0xf4)],_0x4b7999=ceilop(_0x4c4102['ECCstate']['m']*_0x4c4102[_0x14806d(0xf2)]['t'],0x20)-0x1;let _0x41b89d=Array(_0x4c4102['ECCstate'][_0x14806d(0xf1)])['fill'](0x0);const _0x2dd4de=ceilop(0x1f*0x40,0x20);let _0xb6cdfc=Array(_0x2dd4de)[_0x14806d(0xe6)](BigInt(0x0));const _0x27374=0x0,_0x5de881=_0x27374+0x100*(_0x4b7999+0x1),_0x5a5bfd=_0x5de881+0x100*(_0x4b7999+0x1),_0x3c5315=_0x5a5bfd+0x100*(_0x4b7999+0x1);let _0x228b84=Math[_0x14806d(0xdf)](_0xb8b731/0x4),_0x460fff=0x0;while(_0x228b84>0x0){let _0x3a40c7=load4bytes(_0x56b676[_0x14806d(0xe5)](_0x460fff,_0x460fff+0x4));_0x3a40c7=_0x3a40c7^_0xb6cdfc[0x0];const _0x965704=_0x27374+(_0x4b7999+0x1)*Number(_0x3a40c7>>BigInt(0x0)&BigInt(0xff)),_0xe7ade3=_0x5de881+(_0x4b7999+0x1)*Number(_0x3a40c7>>BigInt(0x8)&BigInt(0xff)),_0x2ae526=_0x5a5bfd+(_0x4b7999+0x1)*Number(_0x3a40c7>>BigInt(0x10)&BigInt(0xff)),_0xd66d51=_0x3c5315+(_0x4b7999+0x1)*Number(_0x3a40c7>>BigInt(0x18)&BigInt(0xff));for(let _0x2c2fd1=0x0;_0x2c2fd1<_0x4b7999;_0x2c2fd1++){_0xb6cdfc[_0x2c2fd1]=_0xb6cdfc[_0x2c2fd1+0x1]^_0x4c4102[_0x14806d(0xf2)]['cyclic_tab'][_0x965704+_0x2c2fd1]^_0x4c4102[_0x14806d(0xf2)][_0x14806d(0xde)][_0xe7ade3+_0x2c2fd1]^_0x4c4102['ECCstate'][_0x14806d(0xde)][_0x2ae526+_0x2c2fd1]^_0x4c4102[_0x14806d(0xf2)]['cyclic_tab'][_0xd66d51+_0x2c2fd1];}_0xb6cdfc[_0x4b7999]=_0x4c4102['ECCstate']['cyclic_tab'][_0x965704+_0x4b7999]^_0x4c4102[_0x14806d(0xf2)]['cyclic_tab'][_0xe7ade3+_0x4b7999]^_0x4c4102['ECCstate'][_0x14806d(0xde)][_0x2ae526+_0x4b7999]^_0x4c4102[_0x14806d(0xf2)][_0x14806d(0xde)][_0xd66d51+_0x4b7999],_0x228b84--,_0x460fff+=0x4;}_0x56b676=_0x56b676[_0x14806d(0xe5)](_0x460fff,_0x56b676[_0x14806d(0xf4)]);let _0x277b5d=_0x56b676[_0x14806d(0xf4)];_0x41b89d=_0xb6cdfc;let _0x2276bc=0x0;while(_0x277b5d){const _0x3b905e=_0x56b676[_0x2276bc];_0x2276bc++;let _0xf1490f=(_0x4b7999+0x1)*Number((_0x41b89d[0x0]>>BigInt(0x18)^_0x3b905e)&BigInt(0xff));for(let _0x407ccb=0x0;_0x407ccb<_0x4b7999;_0x407ccb++){_0x41b89d[_0x407ccb]=(_0x41b89d[_0x407ccb]<>BigInt(0x18))^_0x4c4102[_0x14806d(0xf2)][_0x14806d(0xde)][_0xf1490f],_0xf1490f++;}_0x41b89d[_0x4b7999]=_0x41b89d[_0x4b7999]<>BigInt(0x18))&0xff),_0x460c96[_0x14806d(0xdc)](Number(_0x20677a>>BigInt(0x10))&0xff),_0x460c96[_0x14806d(0xdc)](Number(_0x20677a>>BigInt(0x8))&0xff),_0x460c96[_0x14806d(0xdc)](Number(_0x20677a>>BigInt(0x0))&0xff);}return _0x460c96=_0x460c96[_0x14806d(0xe5)](0x0,_0x4c4102[_0x14806d(0xf2)]['ecc_bytes']),_0x460c96;}function int8_stringify(_0x304ba7){const _0x4bfc7c=a0_0x9a23;return _0x304ba7['map'](_0x265a1d=>String[_0x4bfc7c(0xf3)](Number(_0x265a1d)))[_0x4bfc7c(0xea)]('');}function toBinaryString(_0x2960fd,_0x1dbd77){const _0x353141=a0_0x9a23;let _0x3c8dcb=_0x2960fd['toString'](0x2);while(_0x3c8dcb[_0x353141(0xf4)]<_0x1dbd77){_0x3c8dcb='0'+_0x3c8dcb;}return _0x3c8dcb;}function genBinString(_0x289fd9,_0x22d844){const _0x491a8a=a0_0x9a23;let _0x333020='';for(let _0x4de1fd=0x0;_0x4de1fd<_0x289fd9['length'];_0x4de1fd++){_0x333020+=toBinaryString(_0x289fd9[_0x4de1fd],0x8);}return _0x333020=_0x333020[_0x491a8a(0xe5)](0x0,_0x22d844),_0x333020;}function bin_stringify(_0x4eb873){const _0x5d870c=a0_0x9a23;return _0x4eb873[_0x5d870c(0xea)]('');}function deepcopy(_0xa72c13){const _0x5ed357=a0_0x9a23,_0x2ea346={};return _0x2ea346[_0x5ed357(0xe1)]=_0xa72c13['deg'],_0x2ea346['c']=Array['from'](_0xa72c13['c']),_0x2ea346;}function BCH_Decode(_0x453df5,_0xabc0ac,_0x188b3d){const _0x47a2b2=a0_0x9a23,_0x17b354=BCH_Encode(_0x453df5,_0xabc0ac),_0x5362c8=_0x188b3d[_0x47a2b2(0xf4)],_0x50d90b=loadallwords(_0x188b3d,0x20),_0x103897=ceilop(_0x453df5[_0x47a2b2(0xf2)]['m']*_0x453df5['ECCstate']['t'],0x20);let _0x1ff81b=BigInt(0x0);for(let _0x2ca990=0x0;_0x2ca990<_0x103897;_0x2ca990++){_0x453df5[_0x47a2b2(0xf2)]['ecc_buf'][_0x2ca990]=_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xd7)][_0x2ca990]^_0x50d90b[_0x2ca990],_0x1ff81b=_0x1ff81b|_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xd7)][_0x2ca990];}let _0x597a6b=loadallwords(_0xabc0ac,0x8);if(_0x1ff81b===0x0){const _0x391cf=Object();return _0x391cf['data_ascii']=int8_stringify(_0x597a6b),_0x391cf[_0x47a2b2(0xd9)]=bin_stringify(_0xabc0ac),_0x391cf['bitflips']=0x0,_0x391cf[_0x47a2b2(0xf0)]=!![],_0x391cf;}let _0x3a359e=_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xdb)];const _0x556ebf=_0x453df5[_0x47a2b2(0xf2)]['t'],_0x5b02bd=Array(0x2*_0x556ebf)['fill'](0x0);let _0x447f2f=_0x3a359e&0x1f,_0x210d62=_0x453df5['ECCstate'][_0x47a2b2(0xd7)];if(_0x447f2f){const _0x350fd8=_0x210d62[Math[_0x47a2b2(0xdf)](_0x3a359e/0x20)]&~BigInt(Math[_0x47a2b2(0xd2)](0x2,Number(0x20-_0x447f2f))-0x1);_0x210d62[Math['floor'](_0x3a359e/0x20)]=_0x350fd8;}let _0x231871=0x0;while(_0x3a359e>0x0||_0x231871===0x0){let _0x1c86cf=_0x210d62[_0x231871];_0x231871+=0x1,_0x3a359e-=0x20;while(_0x1c86cf){const _0x5740bd=degBigInt(_0x453df5,_0x1c86cf);for(let _0x225e3e=0x0;_0x225e3e<0x2*_0x556ebf;_0x225e3e+=0x2){_0x5b02bd[_0x225e3e]=_0x5b02bd[_0x225e3e]^g_pow(_0x453df5,(_0x225e3e+0x1)*(_0x5740bd+_0x3a359e));}_0x1c86cf=_0x1c86cf^BigInt(Math['pow'](0x2,_0x5740bd));}}for(let _0x256431=0x0;_0x256431<_0x556ebf;_0x256431++){_0x5b02bd[0x2*_0x256431+0x1]=g_sqrt(_0x453df5,_0x5b02bd[_0x256431]);}const _0x1f5919=_0x453df5[_0x47a2b2(0xf2)]['n'];let _0x14589e=-0x1,_0x2fc822=0x1,_0x3fb755=Object();_0x3fb755[_0x47a2b2(0xe1)]=0x0,_0x3fb755['c']=Array(0x2*_0x556ebf)[_0x47a2b2(0xe6)](0x0),_0x3fb755['c'][0x0]=0x1;let _0x58dcc6=Object();_0x58dcc6[_0x47a2b2(0xe1)]=0x0,_0x58dcc6['c']=Array(0x2*_0x556ebf)[_0x47a2b2(0xe6)](0x0),_0x58dcc6['c'][0x0]=0x1;let _0x1121b9=_0x5b02bd[0x0],_0x2b7fc7=Object();for(let _0x253109=0x0;_0x253109<_0x556ebf;_0x253109++){if(_0x58dcc6[_0x47a2b2(0xe1)]>_0x556ebf)break;if(_0x1121b9){const _0x5ed161=0x2*_0x253109-_0x14589e;_0x2b7fc7=deepcopy(_0x58dcc6);const _0x5a864e=g_log(_0x453df5,_0x1121b9)+_0x1f5919-g_log(_0x453df5,_0x2fc822);for(let _0xafc872=0x0;_0xafc872<_0x3fb755['deg']+0x1;_0xafc872++){if(_0x3fb755['c'][_0xafc872]){const _0x431ee6=g_log(_0x453df5,_0x3fb755['c'][_0xafc872]);_0x58dcc6['c'][_0xafc872+_0x5ed161]=_0x58dcc6['c'][_0xafc872+_0x5ed161]^g_pow(_0x453df5,_0x5a864e+_0x431ee6);}}const _0x3da1f0=_0x3fb755[_0x47a2b2(0xe1)]+_0x5ed161;_0x3da1f0>_0x58dcc6['deg']&&(_0x58dcc6['deg']=_0x3da1f0,_0x3fb755=deepcopy(_0x2b7fc7),_0x2fc822=_0x1121b9,_0x14589e=0x2*_0x253109);}if(_0x253109<_0x556ebf-0x1){_0x1121b9=_0x5b02bd[0x2*_0x253109+0x2];for(let _0x3ac9b1=0x1;_0x3ac9b1<_0x58dcc6[_0x47a2b2(0xe1)]+0x1;_0x3ac9b1++){_0x1121b9=_0x1121b9^g_mul(_0x453df5,_0x58dcc6['c'][_0x3ac9b1],_0x5b02bd[0x2*_0x253109+0x2-_0x3ac9b1]);}}}_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xd8)]=_0x58dcc6;const _0x566dd=getroots(_0x453df5,_0x597a6b[_0x47a2b2(0xf4)],_0x453df5['ECCstate'][_0x47a2b2(0xd8)]);_0x597a6b=loadallwords(_0xabc0ac,0x8);const _0x59460f=loadallwords(_0x188b3d,0x8),_0x13c77c=_0x597a6b[_0x47a2b2(0xf4)],_0x5a0f1f=_0x13c77c*0x8+_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xdb)],_0x28a790=Object();_0x28a790['bitflips']=_0x566dd,_0x28a790[_0x47a2b2(0xf0)]=![];if(_0x566dd===-0x1)return _0x28a790;for(let _0x391b0e=0x0;_0x391b0e<_0x566dd;_0x391b0e++){if(_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xec)][_0x391b0e]>=_0x5a0f1f)return _0x28a790;_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xec)][_0x391b0e]=_0x5a0f1f-0x1-_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xec)][_0x391b0e],_0x453df5['ECCstate'][_0x47a2b2(0xec)][_0x391b0e]=_0x453df5[_0x47a2b2(0xf2)][_0x47a2b2(0xec)][_0x391b0e]&~0x7|0x7-(_0x453df5['ECCstate'][_0x47a2b2(0xec)][_0x391b0e]&0x7);}for(let _0x563743=0x0;_0x563743<_0x453df5['ECCstate']['errloc'][_0x47a2b2(0xf4)];_0x563743++){const _0x320367=Number(_0x453df5['ECCstate']['errloc'][_0x563743]),_0x510995=Math[_0x47a2b2(0xdf)](_0x320367/0x8),_0x39cda3=Math[_0x47a2b2(0xd2)](0x2,_0x320367&0x7);_0x320367<(_0x597a6b['length']+_0x59460f['length'])*0x8&&(_0x510995<_0x597a6b[_0x47a2b2(0xf4)]?_0x597a6b[_0x510995]=_0x597a6b[_0x510995]^BigInt(_0x39cda3):_0x59460f[_0x510995-_0x597a6b[_0x47a2b2(0xf4)]]=_0x59460f[_0x510995-_0x597a6b['length']]^BigInt(_0x39cda3));}const _0x4ace94=genBinString(_0x597a6b,_0xabc0ac[_0x47a2b2(0xf4)]);return _0x28a790[_0x47a2b2(0xd6)]=int8_stringify(_0x597a6b),_0x28a790[_0x47a2b2(0xd9)]=_0x4ace94,_0x28a790['valid']=!![],_0x28a790;}function getroots(_0x463978,_0x9cc290,_0x2bed83){const _0x320850=a0_0x9a23;let _0x44da17=Array();if(_0x2bed83['deg']>0x2){_0x9cc290=_0x9cc290*0x8+_0x463978['ECCstate'][_0x320850(0xdb)];let _0x2922fb=Array(_0x463978[_0x320850(0xf2)]['t']*0x2)['fill'](0x0),_0x2fdace=_0x2bed83[_0x320850(0xe1)],_0x1591f8=_0x463978['ECCstate']['n']-g_log(_0x463978,_0x2bed83['c'][_0x2bed83[_0x320850(0xe1)]]);for(let _0x3cbbb9=0x0;_0x3cbbb9<_0x2fdace;_0x3cbbb9++){_0x2bed83['c'][_0x3cbbb9]?_0x2922fb[_0x3cbbb9]=mod(_0x463978,g_log(_0x463978,_0x2bed83['c'][_0x3cbbb9])+_0x1591f8):_0x2922fb[_0x3cbbb9]=-0x1;}_0x2922fb[_0x2bed83['deg']]=0x0;let _0x5a6531=g_div(_0x463978,_0x2bed83['c'][0x0],_0x2bed83['c'][_0x2bed83['deg']]);for(let _0x1fdaeb=_0x463978[_0x320850(0xf2)]['n']-_0x9cc290+0x1;_0x1fdaeb<_0x463978[_0x320850(0xf2)]['n']+0x1;_0x1fdaeb++){let _0x484313=_0x5a6531;for(let _0x3fa56f=0x1;_0x3fa56f<_0x2bed83[_0x320850(0xe1)]+0x1;_0x3fa56f++){let _0x2d1d97=_0x2922fb[_0x3fa56f];_0x2d1d97>=0x0&&(_0x484313=_0x484313^g_pow(_0x463978,_0x2d1d97+_0x3fa56f*_0x1fdaeb));}if(_0x484313==0x0){_0x44da17[_0x320850(0xdc)](_0x463978[_0x320850(0xf2)]['n']-_0x1fdaeb);if(_0x44da17[_0x320850(0xf4)]==_0x2bed83['deg'])break;}}if(_0x44da17[_0x320850(0xf4)]<_0x2bed83[_0x320850(0xe1)])return _0x463978['ECCstate']['errloc']=[],-0x1;}_0x2bed83['deg']==0x1&&(_0x2bed83['c'][0x0]&&_0x44da17['push'](mod(_0x463978,_0x463978['ECCstate']['n']-_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x2bed83['c'][0x0]]+_0x463978['ECCstate'][_0x320850(0xed)][_0x2bed83['c'][0x1]])));if(_0x2bed83[_0x320850(0xe1)]==0x2){if(_0x2bed83['c'][0x0]&&_0x2bed83['c'][0x1]){let _0x2ec59a=_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x2bed83['c'][0x0]],_0x3e3fa5=_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x2bed83['c'][0x1]],_0x2088ea=_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x2bed83['c'][0x2]],_0x25dfc0=g_pow(_0x463978,_0x2ec59a+_0x2088ea+0x2*(_0x463978['ECCstate']['n']-_0x3e3fa5)),_0x10e7fd=0x0,_0x39177a=_0x25dfc0;while(_0x39177a){let _0x461f9=deg(_0x463978,_0x39177a);_0x10e7fd=_0x10e7fd^_0x463978['ECCstate'][_0x320850(0xd4)][_0x461f9],_0x39177a=_0x39177a^Math[_0x320850(0xd2)](0x2,_0x461f9);}g_sqrt(_0x463978,_0x10e7fd)^_0x10e7fd==_0x25dfc0&&(_0x44da17[_0x320850(0xdc)](modn(_0x463978,0x2*_0x463978[_0x320850(0xf2)]['n']-_0x3e3fa5-_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x10e7fd]+_0x2088ea)),_0x44da17[_0x320850(0xdc)](modn(_0x463978,0x2*_0x463978[_0x320850(0xf2)]['n']-_0x3e3fa5-_0x463978[_0x320850(0xf2)][_0x320850(0xed)][_0x10e7fd^0x1]+_0x2088ea)));}}return _0x463978['ECCstate'][_0x320850(0xec)]=_0x44da17,_0x44da17['length'];}function BCH(_0x3d0cef,_0x5090c4){const _0x1f01ef=a0_0x9a23,_0x2a908e=Object();_0x2a908e[_0x1f01ef(0xf2)]=Object();let _0x497b1a=_0x5090c4,_0x466de5=0x0;while(_0x497b1a>>0x1){_0x497b1a=_0x497b1a>>0x1,_0x466de5+=0x1;}_0x2a908e[_0x1f01ef(0xf2)]['m']=_0x466de5,_0x2a908e[_0x1f01ef(0xf2)]['t']=_0x3d0cef,_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xee)]=Object(),_0x2a908e[_0x1f01ef(0xf2)]['n']=Math[_0x1f01ef(0xd2)](0x2,_0x466de5)-0x1;let _0x3361c6=ceilop(_0x466de5*_0x3d0cef,0x20);_0x2a908e['ECCstate'][_0x1f01ef(0xf1)]=ceilop(_0x466de5*_0x3d0cef,0x8),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xde)]=new Array(_0x3361c6*0x400)['fill'](BigInt(0x0)),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xdd)]=new Array(0x2*_0x3d0cef)[_0x1f01ef(0xe6)](0x0),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xd8)]=new Array(_0x3d0cef+0x1)[_0x1f01ef(0xe6)](0x0),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xec)]=new Array(_0x3d0cef)[_0x1f01ef(0xe6)](0x0);let _0x15a3e6=0x1,_0x52bd28=Math['pow'](0x2,deg(_0x2a908e,_0x5090c4));if(_0x52bd28!==Math[_0x1f01ef(0xd2)](0x2,_0x2a908e[_0x1f01ef(0xf2)]['m']))return-0x1;_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xe0)]=new Array(0x1+_0x2a908e[_0x1f01ef(0xf2)]['n'])[_0x1f01ef(0xe6)](0x0),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xed)]=new Array(0x1+_0x2a908e[_0x1f01ef(0xf2)]['n'])[_0x1f01ef(0xe6)](0x0),_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xd4)]=new Array(0x1+_0x2a908e[_0x1f01ef(0xf2)]['m'])[_0x1f01ef(0xe6)](0x0);for(let _0xba1889=0x0;_0xba1889<_0x2a908e[_0x1f01ef(0xf2)]['n'];_0xba1889++){_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xe0)][_0xba1889]=_0x15a3e6,_0x2a908e[_0x1f01ef(0xf2)]['logarithms'][_0x15a3e6]=_0xba1889;if(_0xba1889&&_0x15a3e6===0x1)return-0x1;_0x15a3e6*=0x2,_0x15a3e6&_0x52bd28&&(_0x15a3e6=_0x15a3e6^_0x5090c4);}_0x2a908e[_0x1f01ef(0xf2)]['logarithms'][0x0]=0x0,_0x2a908e[_0x1f01ef(0xf2)]['exponents'][_0x2a908e[_0x1f01ef(0xf2)]['n']]=0x1;let _0x50aa72=0x0;const _0x304069=Object();_0x304069[_0x1f01ef(0xe1)]=0x0,_0x304069['c']=new Array(_0x466de5*_0x3d0cef+0x1)[_0x1f01ef(0xe6)](0x0);let _0x3669f3=new Array(_0x2a908e[_0x1f01ef(0xf2)]['n']+0x1)[_0x1f01ef(0xe6)](0x0),_0x36f1af=new Array(ceilop(_0x466de5*_0x3d0cef+0x1,0x20))[_0x1f01ef(0xe6)](0x0);for(let _0x201e95=0x0;_0x201e95<_0x3d0cef;_0x201e95++){let _0x3b5268=0x2*_0x201e95+0x1;for(let _0x160c71=0x0;_0x160c71<_0x466de5;_0x160c71++){_0x3669f3[_0x3b5268]=0x1,_0x3b5268=mod(_0x2a908e,0x2*_0x3b5268);}}_0x304069[_0x1f01ef(0xe1)]=0x0,_0x304069['c'][0x0]=0x1;for(let _0x3a0590=0x0;_0x3a0590<_0x2a908e[_0x1f01ef(0xf2)]['n'];_0x3a0590++){if(_0x3669f3[_0x3a0590]){let _0x3326b5=_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xe0)][_0x3a0590];_0x304069['c'][_0x304069['deg']+0x1]=0x1;for(let _0xddfeeb=_0x304069[_0x1f01ef(0xe1)];_0xddfeeb>0x0;_0xddfeeb--){_0x304069['c'][_0xddfeeb]=g_mul(_0x2a908e,_0x304069['c'][_0xddfeeb],_0x3326b5)^_0x304069['c'][_0xddfeeb-0x1];}_0x304069['c'][0x0]=g_mul(_0x2a908e,_0x304069['c'][0x0],_0x3326b5),_0x304069[_0x1f01ef(0xe1)]+=0x1;}}_0x50aa72=_0x304069[_0x1f01ef(0xe1)]+0x1;let _0x57118e=0x0;while(_0x50aa72>0x0){let _0x3b1e0b;_0x50aa72>0x20?_0x3b1e0b=0x20:_0x3b1e0b=_0x50aa72;let _0x4d2523=BigInt(0x0);for(let _0x133cc3=0x0;_0x133cc3<_0x3b1e0b;_0x133cc3++){_0x304069['c'][_0x50aa72-0x1-_0x133cc3]&&(_0x4d2523=_0x4d2523|BigInt(Math['pow'](0x2,0x1f-_0x133cc3)));}_0x36f1af[_0x57118e]=_0x4d2523,_0x57118e+=0x1,_0x50aa72-=_0x3b1e0b;}_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xdb)]=_0x304069[_0x1f01ef(0xe1)],build_cyclic(_0x2a908e,_0x36f1af);let _0x4fb602=0x0,_0x41b8ae=0x0;for(let _0x31f241=0x0;_0x31f241<_0x466de5;_0x31f241++){for(let _0x42e50d=0x0;_0x42e50d<_0x466de5;_0x42e50d++){_0x4fb602=_0x4fb602^g_pow(_0x2a908e,_0x31f241*Math[_0x1f01ef(0xd2)](0x2,_0x42e50d));}if(_0x4fb602){_0x41b8ae=_0x2a908e[_0x1f01ef(0xf2)][_0x1f01ef(0xe0)][_0x31f241];break;}}_0x15a3e6=0x0;let _0x55f528=new Array(0x1f)[_0x1f01ef(0xe6)](0x0),_0xfcab38=_0x466de5;while(_0x15a3e6<=_0x2a908e['ECCstate']['n']&&_0xfcab38){let _0x470cd2=g_sqrt(_0x2a908e,_0x15a3e6)^_0x15a3e6;for(let _0x3b8f34=0x0;_0x3b8f34<0x2;_0x3b8f34++){let _0x1b1b04=g_log(_0x2a908e,_0x470cd2);if(_0x470cd2&&_0x1b1b04<_0x466de5&&!_0x55f528[_0x1b1b04]){_0x2a908e['ECCstate'][_0x1f01ef(0xd4)][_0x1b1b04]=_0x15a3e6,_0x55f528[_0x1b1b04]=0x1,_0xfcab38-=0x1;break;}_0x470cd2=_0x470cd2^_0x41b8ae;}_0x15a3e6+=0x1;}return _0x2a908e;}function g_inv(_0x1c145e,_0x1acade){const _0x37cd35=a0_0x9a23;return _0x1c145e['ECCstate']['exponents'][_0x1c145e[_0x37cd35(0xf2)]['n']-_0x1c145e[_0x37cd35(0xf2)][_0x37cd35(0xed)][_0x1acade]];}function g_sqrt(_0x36c53d,_0x5e9d34){const _0x2e4ef1=a0_0x9a23;return _0x5e9d34?_0x36c53d[_0x2e4ef1(0xf2)][_0x2e4ef1(0xe0)][mod(_0x36c53d,0x2*_0x36c53d['ECCstate'][_0x2e4ef1(0xed)][_0x5e9d34])]:0x0;}function mod(_0x48a72b,_0x239e9e){const _0x52e625=a0_0x9a23;return _0x239e9e<_0x48a72b[_0x52e625(0xf2)]['n']?_0x239e9e:_0x239e9e-_0x48a72b['ECCstate']['n'];}function a0_0x5c2c(){const _0x3e66af=['poly','3SqgmGA','valid','ecc_bytes','ECCstate','fromCharCode','length','pow','4876144jKOIoZ','elp_pre','30770jrWtFv','data_ascii','ecc_buf','elp','data_binary','528sWkXjJ','ecc_bits','push','syn','cyclic_tab','floor','exponents','deg','9ydJtXf','300971GNQscq','6IsokUB','slice','fill','1783284AymXHX','3884870FrjNOh','29951911onbAPa','join','5065445WXYgrv','errloc','logarithms'];a0_0x5c2c=function(){return _0x3e66af;};return a0_0x5c2c();}function a0_0x9a23(_0x1e1ec8,_0xa0c001){const _0x5c2c1f=a0_0x5c2c();return a0_0x9a23=function(_0x9a232f,_0x2afc14){_0x9a232f=_0x9a232f-0xd2;let _0x2e48d1=_0x5c2c1f[_0x9a232f];return _0x2e48d1;},a0_0x9a23(_0x1e1ec8,_0xa0c001);}function ceilop(_0x20f90c,_0x379df8){const _0x97c282=a0_0x9a23;return Math[_0x97c282(0xdf)]((_0x20f90c+_0x379df8-0x1)/_0x379df8);}function g_mul(_0x409187,_0x3c43c0,_0x55a248){const _0x107ba2=a0_0x9a23;if(_0x3c43c0>0x0&&_0x55a248>0x0){let _0x19d971=mod(_0x409187,_0x409187[_0x107ba2(0xf2)][_0x107ba2(0xed)][_0x3c43c0]+_0x409187['ECCstate']['logarithms'][_0x55a248]);return _0x409187['ECCstate'][_0x107ba2(0xe0)][_0x19d971];}else return 0x0;}function g_div(_0x3934f7,_0x171baf,_0x297b36){const _0x56ba3c=a0_0x9a23;return _0x171baf?_0x3934f7['ECCstate'][_0x56ba3c(0xe0)][mod(_0x3934f7,_0x3934f7[_0x56ba3c(0xf2)]['logarithms'][_0x171baf]+_0x3934f7[_0x56ba3c(0xf2)]['n']-_0x3934f7[_0x56ba3c(0xf2)][_0x56ba3c(0xed)][_0x297b36])]:0x0;}function modn(_0x5e5e06,_0x46df31){const _0x3d832d=a0_0x9a23;let _0x4cef62=_0x5e5e06[_0x3d832d(0xf2)]['n'];while(_0x46df31>=_0x4cef62){_0x46df31-=_0x4cef62,_0x46df31=(_0x46df31&_0x4cef62)+(_0x46df31>>_0x5e5e06[_0x3d832d(0xf2)]['m']);}return _0x46df31;}function g_log(_0x4ad915,_0x335f19){const _0x5a3729=a0_0x9a23;return _0x4ad915[_0x5a3729(0xf2)][_0x5a3729(0xed)][_0x335f19];}function a_ilog(_0x2f9701,_0x1d4e12){const _0x31bb53=a0_0x9a23;return mod(_0x2f9701,_0x2f9701[_0x31bb53(0xf2)]['n']-_0x2f9701['ECCstate'][_0x31bb53(0xed)][_0x1d4e12]);}function g_pow(_0x3c916f,_0x42e307){return _0x3c916f['ECCstate']['exponents'][modn(_0x3c916f,_0x42e307)];}function deg(_0x39f2b5,_0x11a117){let _0x1758e1=0x0;while(_0x11a117>>0x1){_0x11a117=_0x11a117>>0x1,_0x1758e1+=0x1;}return _0x1758e1;}function degBigInt(_0x586985,_0x216cac){let _0x58bd37=0x0;while(_0x216cac>>BigInt(0x1)){_0x216cac=_0x216cac>>BigInt(0x1),_0x58bd37+=0x1;}return _0x58bd37;}function build_cyclic(_0x236214,_0x371165){const _0x4a7c09=a0_0x9a23;let _0x6df1dc=ceilop(_0x236214['ECCstate']['m']*_0x236214['ECCstate']['t'],0x20),_0x1be735=ceilop(_0x236214[_0x4a7c09(0xf2)]['ecc_bits']+0x1,0x20),_0x173928=ceilop(_0x236214['ECCstate'][_0x4a7c09(0xdb)],0x20);_0x236214[_0x4a7c09(0xf2)]['cyclic_tab']=new Array(0x4*0x100*_0x6df1dc)[_0x4a7c09(0xe6)](BigInt(0x0));for(let _0x21ce6c=0x0;_0x21ce6c<0x100;_0x21ce6c++){for(let _0x2af951=0x0;_0x2af951<0x4;_0x2af951++){let _0x46a5ea=(_0x2af951*0x100+_0x21ce6c)*_0x6df1dc,_0x314aa5=BigInt(BigInt(_0x21ce6c)<>BigInt(0x1f-_0x169713);for(let _0x5a9947=0x0;_0x5a9947<_0x173928;_0x5a9947++){let _0x229221=0x0,_0x307bfc=0x0;_0x169713<0x1f?_0x229221=BigInt(_0x371165[_0x5a9947]<>BigInt(0x1f-_0x169713):_0x307bfc=BigInt(0x0);let _0x40a96a=_0x229221|_0x307bfc;_0x236214[_0x4a7c09(0xf2)]['cyclic_tab'][_0x5a9947+_0x46a5ea]==0x0&&(_0x236214[_0x4a7c09(0xf2)][_0x4a7c09(0xde)][_0x5a9947+_0x46a5ea]=BigInt(0x0)),_0x236214[_0x4a7c09(0xf2)]['cyclic_tab'][_0x5a9947+_0x46a5ea]=_0x236214[_0x4a7c09(0xf2)][_0x4a7c09(0xde)][_0x5a9947+_0x46a5ea]^_0x40a96a;}}}}} + */function uint8ToBoolean(t){const n=[];for(let e=0;e0),n.push((l&64)>0),n.push((l&32)>0),n.push((l&16)>0),n.push((l&8)>0),n.push((l&4)>0),n.push((l&2)>0),n.push((l&1)>0)}return n}function load4bytes(t){let n=BigInt(0);return t.length>0&&(n+=t[0]<1&&(n+=t[1]<2&&(n+=t[2]<3&&(n+=t[3]<0;){let h=loadwordfrombits(t.slice(u,u+n),n);r.push(h),u+=n,l--}if(t=t.slice(u,t.length),t.length>0){let h=loadwordfrombits(t,n);r.push(h)}return r}function BCH_Encode(t,n){let e=loadallwords(n,8);const l=e.length,r=ceilop(t.ECCstate.m*t.ECCstate.t,32)-1;let u=Array(t.ECCstate.ecc_bytes).fill(0);const E=ceilop(1984,32);let h=Array(E).fill(BigInt(0));const i=0,a=i+256*(r+1),g=a+256*(r+1),f=g+256*(r+1);let m=Math.floor(l/4),p=0;for(;m>0;){let C=load4bytes(e.slice(p,p+4));C=C^h[0];const s=i+(r+1)*Number(C>>BigInt(0)&BigInt(255)),o=a+(r+1)*Number(C>>BigInt(8)&BigInt(255)),y=g+(r+1)*Number(C>>BigInt(16)&BigInt(255)),j=f+(r+1)*Number(C>>BigInt(24)&BigInt(255));for(let B=0;B>BigInt(24)^C)&BigInt(255));for(let o=0;o>BigInt(24))^t.ECCstate.cyclic_tab[s],s++;u[r]=u[r]<>BigInt(24))&255),d.push(Number(s>>BigInt(16))&255),d.push(Number(s>>BigInt(8))&255),d.push(Number(s>>BigInt(0))&255)}return d=d.slice(0,t.ECCstate.ecc_bytes),d}function int8_stringify(t){return t.map(n=>String.fromCharCode(Number(n))).join("")}function toBinaryString(t,n){let e=t.toString(2);for(;e.length0||w===0;){let c=p[w];for(w+=1,a-=32;c;){const b=degBigInt(t,c);for(let _=0;_<2*g;_+=2)f[_]=f[_]^g_pow(t,(_+1)*(b+a));c=c^BigInt(Math.pow(2,b))}}for(let c=0;cg);c++){if(y){const b=2*c-d;j=deepcopy(o);const _=g_log(t,y)+x-g_log(t,C);for(let A=0;Ao.deg&&(o.deg=M,s=deepcopy(j),C=y,d=2*c)}if(cg)return I.bitflips=-1,I;for(let c=0;c=O)return I;t.ECCstate.errloc[c]=O-1-t.ECCstate.errloc[c],t.ECCstate.errloc[c]=t.ECCstate.errloc[c]&-8|7-(t.ECCstate.errloc[c]&7)}for(let c=0;cc==="1"),D=BCH_Encode(t,H);return genBinString(D,t.ECCstate.ecc_bits)!==genBinString(N,t.ECCstate.ecc_bits)?(I.bitflips=-1,I):(I.data_ascii=int8_stringify(i),I.data_binary=k,I.valid=!0,I)}function getroots(t,n,e){let l=Array();if(e.deg>2){n=n*8+t.ECCstate.ecc_bits;let r=Array(t.ECCstate.t*2).fill(0),u=e.deg,E=t.ECCstate.n-g_log(t,e.c[e.deg]);for(let i=0;i=0&&(a=a^g_pow(t,f+g*i))}if(a==0&&(l.push(t.ECCstate.n-i),l.length==e.deg))break}if(l.length>1;)l=l>>1,r+=1;e.ECCstate.m=r,e.ECCstate.t=t,e.ECCstate.poly=Object(),e.ECCstate.n=Math.pow(2,r)-1;let u=ceilop(r*t,32);e.ECCstate.ecc_bytes=ceilop(r*t,8),e.ECCstate.cyclic_tab=new Array(u*1024).fill(BigInt(0)),e.ECCstate.syn=new Array(2*t).fill(0),e.ECCstate.elp=new Array(t+1).fill(0),e.ECCstate.errloc=new Array(t).fill(0);let E=1,h=Math.pow(2,deg(e,n));if(h!==Math.pow(2,e.ECCstate.m))return-1;e.ECCstate.exponents=new Array(1+e.ECCstate.n).fill(0),e.ECCstate.logarithms=new Array(1+e.ECCstate.n).fill(0),e.ECCstate.elp_pre=new Array(1+e.ECCstate.m).fill(0);for(let C=0;C0;o--)a.c[o]=g_mul(e,a.c[o],s)^a.c[o-1];a.c[0]=g_mul(e,a.c[0],s),a.deg+=1}i=a.deg+1;let m=0;for(;i>0;){let C;i>32?C=32:C=i;let s=BigInt(0);for(let o=0;o0&&e>0){let l=mod(t,t.ECCstate.logarithms[n]+t.ECCstate.logarithms[e]);return t.ECCstate.exponents[l]}else return 0}function g_div(t,n,e){return n?t.ECCstate.exponents[mod(t,t.ECCstate.logarithms[n]+t.ECCstate.n-t.ECCstate.logarithms[e])]:0}function modn(t,n){let e=t.ECCstate.n;for(;n>=e;)n-=e,n=(n&e)+(n>>t.ECCstate.m);return n}function g_log(t,n){return t.ECCstate.logarithms[n]}function a_ilog(t,n){return mod(t,t.ECCstate.n-t.ECCstate.logarithms[n])}function g_pow(t,n){return t.ECCstate.exponents[modn(t,n)]}function deg(t,n){let e=0;for(;n>>1;)n=n>>1,e+=1;return e}function degBigInt(t,n){let e=0;for(;n>>BigInt(1);)n=n>>BigInt(1),e+=1;return e}function build_cyclic(t,n){let e=ceilop(t.ECCstate.m*t.ECCstate.t,32),l=ceilop(t.ECCstate.ecc_bits+1,32),r=ceilop(t.ECCstate.ecc_bits,32);t.ECCstate.cyclic_tab=new Array(4*256*e).fill(BigInt(0));for(let u=0;u<256;u++)for(let E=0;E<4;E++){let h=(E*256+u)*e,i=BigInt(BigInt(u)<>BigInt(31-a);for(let g=0;g>BigInt(31-a):m=BigInt(0);let p=f|m;t.ECCstate.cyclic_tab[g+h]==0&&(t.ECCstate.cyclic_tab[g+h]=BigInt(0)),t.ECCstate.cyclic_tab[g+h]=t.ECCstate.cyclic_tab[g+h]^p}}}} From 09a21f65989bdf51801ec9ff0e03c30d5962a11a Mon Sep 17 00:00:00 2001 From: John Collomosse Date: Tue, 3 Nov 2026 07:36:29 +0000 Subject: [PATCH 4/4] fix: bch validation edge case in cpp/rust --- cpp/src/bch.cpp | 3 ++ js/README.md | 12 +++++ rust/src/bits.rs | 68 +++++++++++++++++++++++++++-- rust/src/bits/bch.rs | 101 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 177 insertions(+), 7 deletions(-) diff --git a/cpp/src/bch.cpp b/cpp/src/bch.cpp index 45dc94a..59f872e 100644 --- a/cpp/src/bch.cpp +++ b/cpp/src/bch.cpp @@ -135,6 +135,9 @@ class Bch { } } if (found != degree || found > t_) return false; + for (size_t i = data.size(); i < static_cast(data_length); ++i) { + if (received[i] != '0') return false; + } data = received.substr(0, data.size()); ecc = received.substr(data_length); return ecc == parity(data); diff --git a/js/README.md b/js/README.md index 97449d2..94bd7dd 100644 --- a/js/README.md +++ b/js/README.md @@ -41,6 +41,18 @@ To run the example: To use the code in your own project, simply include the two JavaScript files as usual. +## BCH library + +[`deps/bch_ecc.js`](deps/bch_ecc.js) contains the readable BCH source. The example loads +[`deps/bch_ecc.min.js`](deps/bch_ecc.min.js), generated from that source without obfuscation. +After changing the source, regenerate the minified file from the `js` directory: + +```sh +npx --yes esbuild@0.27.4 deps/bch_ecc.js --minify --target=es2020 --legal-comments=inline --outfile=deps/bch_ecc.min.js +``` + +Run the CPU-only ECC regressions against both files with `node --test test-ecc.cjs`. + ## Example output For an image containing a TrustMark watermark: diff --git a/rust/src/bits.rs b/rust/src/bits.rs index 6b8697b..69a2301 100644 --- a/rust/src/bits.rs +++ b/rust/src/bits.rs @@ -75,7 +75,8 @@ impl Bits { } // pad the input - input.push_str(&"0".repeat(data_bits - input.len() + (8 - data_bits % 8))); + let padding = (8 - data_bits % 8) % 8; + input.push_str(&"0".repeat(data_bits - input.len() + padding)); // pack the input into bytes let data: Vec = input @@ -171,8 +172,9 @@ impl Bits { let mut ecc = s[data_bits..data_bits + ecc_bits].to_string(); // pad - data.push_str(&"0".repeat(data_bits - data.len() + (8 - data_bits % 8))); - ecc.push_str(&"0".repeat(ecc_bits - ecc.len() + (8 - ecc_bits % 8))); + let data_padding = (8 - data_bits % 8) % 8; + data.push_str(&"0".repeat(data_bits - data.len() + data_padding)); + ecc.push_str(&"0".repeat(ecc_bits - ecc.len() + (8 - ecc_bits % 8) % 8)); // pack into bytes let mut data: Vec = data @@ -193,6 +195,9 @@ impl Bits { if bitflips > version.allowed_bit_flips() { return Err(Error::CorruptWatermark); } + if data_padding != 0 && data.last().unwrap() & ((1_u8 << data_padding) - 1) != 0 { + return Err(Error::CorruptWatermark); + } // unpack data and ecc let mut data: String = data @@ -394,6 +399,63 @@ mod tests { ); } + #[test] + fn noncanonical_packets_are_rejected() { + for (packet, version) in [ + ( + "0000101101110101001111100000111101110001010010000000110001111101101000001101100101010010111111011111", + Version::Bch3, + ), + ( + "1001110101000101100000011100111101010010000001100101100010001001111100010010000001101111101011000001", + Version::Bch5, + ), + ] { + assert!(matches!( + Bits::new_with_version(packet, version), + Err(Error::CorruptWatermark) + )); + assert!(matches!( + Bits::new(packet.to_owned()), + Err(Error::CorruptWatermark) + )); + } + } + + #[test] + fn python_packets_keep_the_full_correction_radius() { + let packets = [ + (Version::BchSuper, "1011010010100000000000000000000000000000001111000100010111110110000001111110001101101011000010000000"), + (Version::Bch5, "1011010010100000000000000000000000000000000000000000000000000111110111111010101111110110000101010001"), + (Version::Bch4, "1011010010100000000000000000000000000000000000000000000000000000000000011011111010010110010010000010"), + (Version::Bch3, "1011010010100000000000000000000000000000000000000000000000000000000000000000000010100100010110110011"), + ]; + for (version, packet) in packets { + let encoded = + Bits::apply_error_correction_and_schema("10110100101".to_owned(), version).unwrap(); + assert_eq!(encoded.0, packet); + let capacity = version.data_bits() as usize; + for flips in 0..=version.allowed_bit_flips() as usize { + for layout in 0..3 { + let mut corrupted = packet.as_bytes().to_vec(); + for index in 0..flips { + let location = match layout { + 0 => index, + 1 => capacity + index, + _ if index % 2 == 1 => capacity + index / 2, + _ => index / 2, + }; + corrupted[location] ^= 1; + } + let corrupted = String::from_utf8(corrupted).unwrap(); + let decoded = Bits::new_with_version(&corrupted, version).unwrap(); + assert_eq!(decoded.get_version(), version); + assert_eq!(decoded.get_data(), packet[..capacity]); + } + } + } + } + #[test] fn invalid_bitstring() { let err = Bits::apply_error_correction_and_schema("hello".to_string(), Version::Bch5) diff --git a/rust/src/bits/bch.rs b/rust/src/bits/bch.rs index 0aa9871..65b96f5 100644 --- a/rust/src/bits/bch.rs +++ b/rust/src/bits/bch.rs @@ -344,10 +344,11 @@ pub(super) fn bch_encode(ecc_state: &mut EccState, data: &[u8]) -> Vec { let tmp = data[posn]; let cyclic_tab = ecc_state.cyclic_tab.as_ref().unwrap(); posn += 1; - let pidx = (l + 1) * (((ecc[0] >> 24) ^ (tmp as u32)) & 0xff); + let mut pidx = (l + 1) * (((ecc[0] >> 24) ^ (tmp as u32)) & 0xff); for i in 0..l { ecc[i as usize] = ((ecc[i as usize] << 8) | (ecc[(i + 1) as usize] >> 24)) ^ cyclic_tab[pidx as usize]; + pidx += 1; } ecc[l as usize] = (ecc[l as usize] << 8) ^ cyclic_tab[pidx as usize]; leftdata -= 1; @@ -379,6 +380,7 @@ pub(super) fn bch_encode(ecc_state: &mut EccState, data: &[u8]) -> Vec { pub(super) fn bch_decode(ecc_state: &mut EccState, data: &mut [u8], recvecc: &[u8]) -> u8 { // calc_ecc=self.encode(data) let _calc_ecc = bch_encode(ecc_state, data); + let mut corrected_ecc = recvecc.to_vec(); // self.ECCstate.errloc=[] ecc_state.errloc = Some(vec![]); @@ -585,7 +587,7 @@ pub(super) fn bch_decode(ecc_state: &mut EccState, data: &mut [u8], recvecc: &[u // Rust version returns u32::MAX. If we try to iterate over 0..u32::MAX, we'll try to access an // element from ecc_state.errloc, which is empty. // Hence, we check for the sentinal value here. - if nroots == u32::MAX { + if nroots != elp.deg || nroots > t { return u8::MAX; } let datalen = data.len() as u32; @@ -618,15 +620,29 @@ pub(super) fn bch_decode(ecc_state: &mut EccState, data: &mut [u8], recvecc: &[u for bitflip in ecc_state.errloc.as_ref().unwrap() { let byte = bitflip / 8; let bit = 2_u32.pow(bitflip & 7); - if (*bitflip as usize) < (data.len() + recvecc.len()) * 8 { + if (*bitflip as usize) < (data.len() + corrected_ecc.len()) * 8 { if (byte as usize) < data.len() { data[byte as usize] ^= bit as u8; } else { - recvecc[(byte - data.len() as u32) as usize] ^= bit as u8; + corrected_ecc[(byte - data.len() as u32) as usize] ^= bit as u8; } } } + let reencoded_ecc = bch_encode(ecc_state, data); + let ecc_bits = ecc_state.ecc_bits.unwrap() as usize; + let whole_bytes = ecc_bits / 8; + if reencoded_ecc[..whole_bytes] != corrected_ecc[..whole_bytes] { + return u8::MAX; + } + // Trailing byte-alignment bits are not protected BCH parity. + if !ecc_bits.is_multiple_of(8) { + let mask = u8::MAX << (8 - ecc_bits % 8); + if (reencoded_ecc[whole_bytes] ^ corrected_ecc[whole_bytes]) & mask != 0 { + return u8::MAX; + } + } + nroots as u8 } @@ -984,4 +1000,81 @@ mod tests { let ecc = bch_encode(&mut ecc_state, &[133, 20, 228, 249, 11, 172, 165, 151, 0]); assert_eq!(ecc, &[115, 32, 10, 0]); } + + #[test] + fn quadratic_zero_roots_are_rejected() { + let packet = "0000101101110101001111100000111101110001010010000000110001111101101000001101100101010010111111011111"; + let pack = |bits: &str| { + bits.as_bytes() + .chunks(8) + .map(|chunk| { + chunk.iter().enumerate().fold(0_u8, |byte, (index, bit)| { + byte | ((bit - b'0') << (7 - index)) + }) + }) + .collect::>() + }; + let mut data = pack(&packet[..75]); + let ecc = pack(&packet[75..96]); + let mut ecc_state = bch_init(3, POLYNOMIAL); + assert_eq!(bch_decode(&mut ecc_state, &mut data, &ecc), u8::MAX); + assert!(ecc_state.errloc.as_ref().unwrap().is_empty()); + } + + #[test] + fn byte_payloads_keep_the_full_correction_radius() { + for radius in [8, 5, 4, 3] { + let mut ecc_state = bch_init(radius, POLYNOMIAL); + for size in [1, 5, 8] { + let original: Vec<_> = (0..size) + .map(|index| (index as u8).wrapping_mul(37) | 31) + .collect(); + let parity = bch_encode(&mut ecc_state, &original); + for flips in 0..=radius as usize { + for layout in 0..3 { + let mut data = original.clone(); + let mut ecc = parity.clone(); + for index in 0..flips { + let location = match layout { + 0 => index, + 1 => size * 8 + index, + _ if index % 2 == 1 => size * 8 + index / 2, + _ => index / 2, + }; + if location < size * 8 { + data[location / 8] ^= 1 << (7 - location % 8); + } else { + let bit = location - size * 8; + ecc[bit / 8] ^= 1 << (7 - bit % 8); + } + } + assert_eq!( + bch_decode(&mut ecc_state, &mut data, &ecc), + flips as u8, + "radius={radius} size={size} flips={flips} layout={layout}" + ); + assert_eq!(data, original); + } + } + } + } + } + + #[test] + fn ecc_byte_padding_remains_ignored_during_correction() { + for radius in [8, 5, 4, 3] { + let mut ecc_state = bch_init(radius, POLYNOMIAL); + let original = [255, 7]; + let mut parity = bch_encode(&mut ecc_state, &original); + let padding = parity.len() * 8 - ecc_state.ecc_bits.unwrap() as usize; + if padding != 0 { + *parity.last_mut().unwrap() |= ((1_u16 << padding) - 1) as u8; + } + let mut data = original; + data[0] ^= 128; + parity[0] ^= 128; + assert_eq!(bch_decode(&mut ecc_state, &mut data, &parity), 2); + assert_eq!(data, original); + } + } }