Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

524 řádky
24KB

  1. // Package watermark implements spread-spectrum audio watermarking for fm-rds-tx.
  2. //
  3. // # Design
  4. //
  5. // The watermark is injected into the audio L/R signal (Fix B) before stereo
  6. // encoding, so it survives FM broadcast and receiver demodulation intact.
  7. // The payload is Reed-Solomon encoded (Fix C) for robust recovery even when
  8. // individual bits have high error rates due to noise and audio masking.
  9. //
  10. // # Parameters
  11. //
  12. // - PN sequence: 2048-chip LFSR-13 (seed 0x1ACE)
  13. // - Payload: 8 bytes (SHA-256[:8] of key) → RS(16,8) → 16 bytes → 128 bits
  14. // - Frame period: ~5.5 s at 228 kHz composite (repeats ~11×/min)
  15. // - Injection: -48 dBFS on audio L+R before stereo encode (gated on audio level)
  16. // - Spreading gain: 33 dB. RS erasure corrects up to 8 of 16 byte symbols.
  17. //
  18. // # Recovery (cmd/wmdecode)
  19. //
  20. // 1. Record FM receiver audio output as mono WAV (48 kHz preferred).
  21. // 2. Phase search: slide a single-bit PN template across [0, samplesPerBit)
  22. // to find chip-aligned sample offset (coarse-fine search).
  23. // 3. Extract 128 bit correlations at found phase, averaged over all frames.
  24. // 4. Frame sync: try all 128 cyclic rotations of the bit sequence,
  25. // RS-decode each; the rotation that succeeds gives the frame alignment.
  26. // 5. Sort bits by |correlation| (confidence). Mark weakest as erasures.
  27. // 6. RS erasure-decode → 8 payload bytes → compare against known keys.
  28. package watermark
  29. import (
  30. "crypto/sha256"
  31. )
  32. const (
  33. // pnChips is the spreading factor — PN chips per data bit at composite rate.
  34. // Spreading gain = 10·log10(2048) = 33.1 dB.
  35. pnChips = 2048
  36. // rsDataBytes is the number of payload bytes before RS encoding.
  37. rsDataBytes = 8
  38. // rsCheckBytes is the number of RS parity bytes. With 8 check bytes the
  39. // code corrects up to 4 errors or up to 8 erasures per 16-byte codeword.
  40. rsCheckBytes = 8
  41. // rsTotalBytes is the full RS codeword length.
  42. rsTotalBytes = rsDataBytes + rsCheckBytes // 16
  43. // payloadBits is the total number of BPSK bits per watermark frame.
  44. payloadBits = rsTotalBytes * 8 // 128
  45. // Level is the audio injection amplitude per channel (-48 dBFS).
  46. // At typical audio levels this is completely inaudible.
  47. Level = 0.004
  48. // CompositeRate is the sample rate at which the watermark was embedded.
  49. // The recovery tool uses this to compute fractional chip indices.
  50. CompositeRate = 228000
  51. )
  52. // RecordingRate is the canonical recording rate used for chip-rate Bresenham stepping.
  53. // The embedder advances chips at this rate, so the decoder at this rate sees
  54. // exactly pnChips samples per bit with no fractional-stepping errors.
  55. const RecordingRate = 48000
  56. // Embedder continuously embeds a watermark into audio L/R samples.
  57. // Not thread-safe: call NextSample from the single DSP goroutine only.
  58. type Embedder struct {
  59. codeword [rsTotalBytes]byte // RS-encoded payload, 16 bytes
  60. chipIdx int // chip position within current bit (0..pnChips-1)
  61. bitIdx int // current bit in codeword (0..127)
  62. symbol int8 // BPSK symbol for current bit: +1 or -1
  63. accum int // Bresenham accumulator for chip-rate stepping
  64. // Audio-level gate: mutes watermark during silence to prevent audibility.
  65. gateGain float64 // smooth ramp 0.0 (muted) → 1.0 (open)
  66. gateThreshold float64 // audio level below which gate closes
  67. gateRampUp float64 // per-sample increment when opening (~5ms)
  68. gateRampDown float64 // per-sample decrement when closing (~5ms)
  69. gateEnabled bool
  70. }
  71. // NewEmbedder creates an Embedder for the given license key.
  72. // The key's SHA-256 hash (first 8 bytes) is RS-encoded and embedded.
  73. // An empty key embeds a null payload (still watermarks, just anonymous).
  74. func NewEmbedder(key string) *Embedder {
  75. var data [rsDataBytes]byte
  76. if key != "" {
  77. h := sha256.Sum256([]byte(key))
  78. copy(data[:], h[:rsDataBytes])
  79. }
  80. e := &Embedder{gateGain: 1.0}
  81. e.codeword = rsEncode(data)
  82. e.loadSymbol()
  83. return e
  84. }
  85. // NextSample returns the watermark amplitude for one composite sample.
  86. // Add this value to both audio.Frame.L and audio.Frame.R before stereo encoding.
  87. //
  88. // The chip index advances using Bresenham stepping at RecordingRate/CompositeRate,
  89. // so each chip occupies exactly CompositeRate/RecordingRate composite samples on
  90. // average. A decoder recording at RecordingRate (48 kHz) sees exactly pnChips
  91. // samples per data bit, enabling simple integer-stride correlation.
  92. func (e *Embedder) NextSample() float64 {
  93. chip := float64(pnSequence[e.chipIdx])
  94. sample := Level * float64(e.symbol) * chip * e.gateGain
  95. // Bresenham: advance chip once per RecordingRate/CompositeRate composite samples.
  96. e.accum += RecordingRate
  97. if e.accum >= CompositeRate {
  98. e.accum -= CompositeRate
  99. e.chipIdx++
  100. if e.chipIdx >= pnChips {
  101. e.chipIdx = 0
  102. e.bitIdx = (e.bitIdx + 1) % payloadBits
  103. e.loadSymbol()
  104. }
  105. }
  106. return sample
  107. }
  108. // loadSymbol sets e.symbol from the current bit in the codeword (MSB first).
  109. func (e *Embedder) loadSymbol() {
  110. byteIdx := e.bitIdx / 8
  111. bitPos := uint(7 - (e.bitIdx % 8))
  112. if (e.codeword[byteIdx]>>bitPos)&1 == 0 {
  113. e.symbol = 1
  114. } else {
  115. e.symbol = -1
  116. }
  117. }
  118. // PayloadHex returns the RS-encoded codeword as hex for logging.
  119. func (e *Embedder) PayloadHex() string {
  120. const hx = "0123456789abcdef"
  121. out := make([]byte, rsTotalBytes*2)
  122. for i, b := range e.codeword {
  123. out[i*2] = hx[b>>4]
  124. out[i*2+1] = hx[b&0xf]
  125. }
  126. return string(out)
  127. }
  128. // EnableGate activates audio-level gating with asymmetric ramp times.
  129. // threshold is the linear audio amplitude below which the watermark is muted
  130. // (e.g. 0.01 ≈ -40 dBFS). compositeRate is needed to compute ramp speed.
  131. // Attack (open) is fast (5ms) so the watermark starts immediately with audio.
  132. // Release (close) is slow (200ms) to keep the watermark running through normal
  133. // inter-word and inter-phrase gaps — only extended silence mutes.
  134. func (e *Embedder) EnableGate(threshold, compositeRate float64) {
  135. attackSamples := compositeRate * 0.005 // 5ms open
  136. releaseSamples := compositeRate * 0.200 // 200ms close
  137. if attackSamples < 1 {
  138. attackSamples = 1
  139. }
  140. if releaseSamples < 1 {
  141. releaseSamples = 1
  142. }
  143. e.gateThreshold = threshold
  144. e.gateRampUp = 1.0 / attackSamples
  145. e.gateRampDown = 1.0 / releaseSamples
  146. e.gateEnabled = true
  147. }
  148. // SetAudioLevel updates the gate state based on the current audio amplitude.
  149. // Call once per sample before NextSample. absLevel should be the absolute
  150. // mono audio level (pre- or post-pre-emphasis, either works).
  151. func (e *Embedder) SetAudioLevel(absLevel float64) {
  152. if !e.gateEnabled {
  153. return
  154. }
  155. if absLevel > e.gateThreshold {
  156. e.gateGain += e.gateRampUp
  157. if e.gateGain > 1.0 {
  158. e.gateGain = 1.0
  159. }
  160. } else {
  161. e.gateGain -= e.gateRampDown
  162. if e.gateGain < 0.0 {
  163. e.gateGain = 0.0
  164. }
  165. }
  166. }
  167. // --- RS(16,8) over GF(2^8) — GF poly 0x11d, fcr=0, generator=2 ---
  168. // These routines are used by the embedder (encode) and the recovery tool (decode).
  169. func gfMul(a, b byte) byte {
  170. if a == 0 || b == 0 {
  171. return 0
  172. }
  173. return gfExp[(int(gfLog[a])+int(gfLog[b]))%255]
  174. }
  175. func gfInv(a byte) byte {
  176. if a == 0 {
  177. return 0
  178. }
  179. return gfExp[255-int(gfLog[a])]
  180. }
  181. func gfPow(a byte, n int) byte {
  182. if a == 0 {
  183. return 0
  184. }
  185. return gfExp[(int(gfLog[a])*n)%255]
  186. }
  187. // rsEncode encodes 8 data bytes into a 16-byte RS codeword.
  188. func rsEncode(data [rsDataBytes]byte) [rsTotalBytes]byte {
  189. var work [rsTotalBytes]byte
  190. copy(work[:rsDataBytes], data[:])
  191. // Polynomial long division by the generator polynomial.
  192. for i := 0; i < rsDataBytes; i++ {
  193. fb := work[i]
  194. if fb != 0 {
  195. for j := 1; j <= rsCheckBytes; j++ {
  196. work[i+j] ^= gfMul(rsGen[j], fb)
  197. }
  198. }
  199. }
  200. var cw [rsTotalBytes]byte
  201. copy(cw[:rsDataBytes], data[:])
  202. copy(cw[rsDataBytes:], work[rsDataBytes:])
  203. return cw
  204. }
  205. // RSDecode recovers 8 data bytes from a (possibly corrupted) 16-byte codeword.
  206. // erasurePositions lists the byte indices (0..15) of symbols with low confidence
  207. // that should be treated as erasures. Up to 8 erasures can be corrected.
  208. // Returns (data, true) on success, (zero, false) on decoding failure.
  209. func RSDecode(recv [rsTotalBytes]byte, erasurePositions []int) ([rsDataBytes]byte, bool) {
  210. // Step 1: compute syndromes.
  211. var S [rsCheckBytes]byte
  212. for i := 0; i < rsCheckBytes; i++ {
  213. var acc byte
  214. for _, c := range recv {
  215. acc = gfMul(acc, gfPow(2, i)) ^ c
  216. }
  217. S[i] = acc
  218. }
  219. // Step 2: if no erasures and all syndromes zero, no errors.
  220. hasErr := false
  221. for _, s := range S {
  222. if s != 0 {
  223. hasErr = true
  224. break
  225. }
  226. }
  227. if !hasErr && len(erasurePositions) == 0 {
  228. // Valid codeword, no errors, no erasures.
  229. var out [rsDataBytes]byte
  230. copy(out[:], recv[:rsDataBytes])
  231. return out, true
  232. }
  233. if hasErr && len(erasurePositions) == 0 {
  234. // Errors present but no erasure positions supplied — cannot correct.
  235. // BUG FIX: previously fell through to ne==0 check and returned wrong
  236. // data as correct. Now correctly signals failure so the caller can
  237. // retry with erasure positions.
  238. return [rsDataBytes]byte{}, false
  239. }
  240. // Step 3: erasure locator polynomial Γ(x) = ∏(1 - α^(e_j)·x).
  241. gamma := []byte{1}
  242. for _, pos := range erasurePositions {
  243. // multiply gamma by (1 + α^pos · x)
  244. alpha := gfPow(2, pos)
  245. next := make([]byte, len(gamma)+1)
  246. for j, g := range gamma {
  247. next[j] ^= g
  248. next[j+1] ^= gfMul(g, alpha)
  249. }
  250. gamma = next
  251. }
  252. // Step 4: modified syndrome T(x) = S(x)·Γ(x).
  253. t := make([]byte, rsCheckBytes)
  254. for i := 0; i < rsCheckBytes; i++ {
  255. var acc byte
  256. for j := 0; j < len(gamma) && j <= i; j++ {
  257. if i-j < rsCheckBytes {
  258. acc ^= gfMul(gamma[j], S[i-j])
  259. }
  260. }
  261. t[i] = acc
  262. }
  263. // Step 5: compute error magnitudes using Forney's formula.
  264. // For erasure-only decoding (no additional errors), the error locator
  265. // is Γ itself. Evaluate omega = T mod x^(ne) where ne = len(erasures).
  266. ne := len(erasurePositions)
  267. if ne == 0 {
  268. // Should not be reachable: handled above. Fail safely.
  269. return [rsDataBytes]byte{}, false
  270. }
  271. if ne > rsCheckBytes {
  272. return [rsDataBytes]byte{}, false
  273. }
  274. result := recv
  275. for _, pos := range erasurePositions {
  276. xi := gfPow(2, pos)
  277. // Evaluate omega at xi^-1.
  278. xiInv := gfInv(xi)
  279. var omega byte
  280. for j := 0; j < ne && j < rsCheckBytes; j++ {
  281. omega ^= gfMul(t[j], gfPow(xiInv, j))
  282. }
  283. // Formal derivative of gamma at xi^-1 (only odd-degree terms survive in GF(2)).
  284. var gammaPrime byte
  285. for j := 1; j < len(gamma); j += 2 {
  286. gammaPrime ^= gfMul(gamma[j], gfPow(xiInv, j-1))
  287. }
  288. if gammaPrime == 0 {
  289. return [rsDataBytes]byte{}, false
  290. }
  291. magnitude := gfMul(omega, gfInv(gammaPrime))
  292. result[pos] ^= magnitude
  293. }
  294. // Verify syndromes after correction.
  295. for i := 0; i < rsCheckBytes; i++ {
  296. var acc byte
  297. for _, c := range result {
  298. acc = gfMul(acc, gfPow(2, i)) ^ c
  299. }
  300. if acc != 0 {
  301. return [rsDataBytes]byte{}, false
  302. }
  303. }
  304. var out [rsDataBytes]byte
  305. copy(out[:], result[:rsDataBytes])
  306. return out, true
  307. }
  308. // KeyMatchesPayload returns true if SHA-256(key)[:8] matches payload.
  309. func KeyMatchesPayload(key string, payload [rsDataBytes]byte) bool {
  310. h := sha256.Sum256([]byte(key))
  311. var expected [rsDataBytes]byte
  312. copy(expected[:], h[:rsDataBytes])
  313. return expected == payload
  314. }
  315. // Constants exported for the recovery tool.
  316. const (
  317. PnChips = pnChips
  318. PayloadBits = payloadBits
  319. RsDataBytes = rsDataBytes
  320. RsTotalBytes = rsTotalBytes
  321. RsCheckBytes = rsCheckBytes
  322. )
  323. // CorrelateAt returns the correlation of received samples at the given bit
  324. // position. recRate is the WAV sample rate.
  325. //
  326. // At recRate = RecordingRate (48000 Hz) the chip stride is exactly 1 — the
  327. // embedder was designed for this rate. At other rates the chip index is
  328. // scaled proportionally (still works with enough frame averaging).
  329. func CorrelateAt(samples []float64, bitStart int, recRate float64) float64 {
  330. // Samples per bit at the canonical recording rate.
  331. // At RecordingRate: samplesPerBit = pnChips (integer, perfect).
  332. // At other rates: scale proportionally.
  333. samplesPerBit := int(float64(pnChips) * recRate / float64(RecordingRate))
  334. if samplesPerBit < 1 {
  335. samplesPerBit = 1
  336. }
  337. n := samplesPerBit
  338. if bitStart+n > len(samples) {
  339. n = len(samples) - bitStart
  340. }
  341. var acc float64
  342. for i := 0; i < n; i++ {
  343. // Map recording-rate sample index to chip index.
  344. chipIdx := int(float64(i)*float64(RecordingRate)/recRate) % pnChips
  345. acc += samples[bitStart+i] * float64(pnSequence[chipIdx])
  346. }
  347. return acc
  348. }
  349. // pnSequence is the 2048-chip LFSR-13 spreading code (seed 0x1ACE).
  350. var pnSequence = [pnChips]int8{
  351. 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1,
  352. -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1,
  353. -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1,
  354. 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1,
  355. -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, 1, 1,
  356. -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1,
  357. 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1,
  358. -1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, -1,
  359. -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1,
  360. -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1,
  361. 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1,
  362. -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1,
  363. -1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1,
  364. 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1,
  365. -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1,
  366. -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1,
  367. 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1,
  368. -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1,
  369. -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1,
  370. 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1,
  371. -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1,
  372. -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1,
  373. -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1,
  374. -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1,
  375. 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1,
  376. -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1,
  377. 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1,
  378. 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1,
  379. 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1,
  380. 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, -1,
  381. -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1,
  382. 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1,
  383. -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1,
  384. -1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, 1,
  385. -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1,
  386. 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1,
  387. -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, -1,
  388. 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1,
  389. 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, 1,
  390. -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1,
  391. -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1,
  392. 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1,
  393. 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,
  394. 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1,
  395. 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1,
  396. -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, 1,
  397. 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1,
  398. 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
  399. -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1,
  400. 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1,
  401. -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1,
  402. 1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1,
  403. 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  404. 1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1,
  405. 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, 1,
  406. 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, 1,
  407. 1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1,
  408. 1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1,
  409. -1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1,
  410. -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1,
  411. 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  412. -1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1,
  413. 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1,
  414. 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1,
  415. -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1,
  416. -1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1,
  417. 1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1,
  418. 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1,
  419. -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1,
  420. -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  421. 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1,
  422. -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  423. -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1,
  424. 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1,
  425. -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1,
  426. 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  427. -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1,
  428. -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1,
  429. -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1,
  430. -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1,
  431. -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1,
  432. 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1,
  433. -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1,
  434. 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  435. 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1,
  436. 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1,
  437. 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1,
  438. -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1,
  439. -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1,
  440. -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1,
  441. 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1,
  442. -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1,
  443. -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1,
  444. -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1,
  445. -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1,
  446. 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1,
  447. 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1,
  448. -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1,
  449. -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1,
  450. 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1,
  451. -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1,
  452. -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1,
  453. -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1,
  454. -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1,
  455. 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1,
  456. -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1,
  457. -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1,
  458. -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1,
  459. -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1,
  460. -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1,
  461. 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1,
  462. -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1,
  463. 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1,
  464. 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1,
  465. 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1,
  466. -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1,
  467. 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1,
  468. -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1,
  469. -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1,
  470. -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1,
  471. 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1,
  472. -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1,
  473. -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1,
  474. 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1,
  475. -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1,
  476. 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1,
  477. -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1,
  478. 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  479. }
  480. // GF(2^8) tables with primitive polynomial 0x11d.
  481. var gfExp = [512]byte{1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 1, 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76, 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157, 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70, 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95, 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253, 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217, 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129, 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133, 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168, 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230, 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227, 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130, 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81, 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18, 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44, 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 1, 2}
  482. var gfLog = [256]byte{0, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, 29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166, 6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136, 54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64, 30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61, 202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87, 7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24, 227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46, 55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97, 242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162, 31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246, 108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90, 203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175}
  483. var rsGen = [9]byte{1, 255, 11, 81, 54, 239, 173, 200, 24}
  484. // rsGen is the RS(16,8) generator polynomial coefficients (fcr=0).