Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

589 rindas
26KB

  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 after all audio
  6. // processing (LPF, clip, limiter) but before stereo encoding, so it
  7. // survives FM broadcast, receiver demodulation, de-emphasis, and moderate
  8. // EQ intact. The PN chip rate is limited to 12 kHz so all watermark energy
  9. // sits within 0–6 kHz — the band that survives every stage of the FM chain:
  10. //
  11. // Embedder → Stereo Encode → Composite Clip → Pilot/RDS → FM Mod
  12. // → FM Demod → De-emphasis → Stereo Decode → Audio Out → Recording
  13. //
  14. // The payload is Reed-Solomon encoded for robust recovery even when
  15. // individual bits have high error rates due to noise and audio masking.
  16. //
  17. // # Parameters
  18. //
  19. // - PN sequence: 2048-chip LFSR-13 (seed 0x1ACE)
  20. // - Payload: 8 bytes (SHA-256[:8] of key) → RS(16,8) → 16 bytes → 128 bits
  21. // - Chip clock: 12 kHz → PN bandwidth 0–6 kHz (survives de-emphasis)
  22. // - Frame period: ~21.8 s at 228 kHz composite (repeats ~2.7×/min)
  23. // - Injection: -48 dBFS on audio L+R after all processing, before stereo encode
  24. // - Spreading gain: 33 dB. RS erasure corrects up to 8 of 16 byte symbols.
  25. //
  26. // # Recovery (cmd/wmdecode)
  27. //
  28. // 1. Record FM receiver audio output as mono WAV (any sample rate ≥ 12 kHz).
  29. // 2. Phase search: energy-based coarse/fine search for chip alignment.
  30. // 3. Extract 128 bit correlations at found phase, averaged over all frames.
  31. // 4. Frame sync: try all 128 cyclic rotations of the bit sequence,
  32. // RS-decode each; the rotation that succeeds gives the frame alignment.
  33. // 5. Byte-level erasure + soft-decision bit-flipping for error correction.
  34. // 6. RS erasure-decode → 8 payload bytes → compare against known keys.
  35. package watermark
  36. import (
  37. "crypto/sha256"
  38. )
  39. const (
  40. // pnChips is the spreading factor — PN chips per data bit.
  41. // Spreading gain = 10·log10(2048) = 33.1 dB.
  42. pnChips = 2048
  43. // rsDataBytes is the number of payload bytes before RS encoding.
  44. rsDataBytes = 8
  45. // rsCheckBytes is the number of RS parity bytes. With 8 check bytes the
  46. // code corrects up to 4 errors or up to 8 erasures per 16-byte codeword.
  47. rsCheckBytes = 8
  48. // rsTotalBytes is the full RS codeword length.
  49. rsTotalBytes = rsDataBytes + rsCheckBytes // 16
  50. // payloadBits is the total number of BPSK bits per watermark frame.
  51. payloadBits = rsTotalBytes * 8 // 128
  52. // Level is the audio injection amplitude per channel (-48 dBFS).
  53. // At typical audio levels this is completely inaudible.
  54. Level = 0.040
  55. // CompositeRate is the sample rate at which the watermark is embedded.
  56. CompositeRate = 228000
  57. )
  58. // ChipRate is the effective PN chip clock rate (Hz). Determines the spectral
  59. // bandwidth of the watermark: Nyquist = ChipRate/2 = 6 kHz. This ensures
  60. // all PN energy is within the audio band that survives de-emphasis (50/75 µs),
  61. // receiver LPFs, audio codecs, speaker EQ, and even acoustic recording.
  62. //
  63. // At CompositeRate (228 kHz), each chip spans 228000/12000 = 19 samples.
  64. // At any recording rate R, each chip spans R/12000 samples.
  65. const ChipRate = 12000
  66. // RecordingRate is the canonical recording rate for test WAV output (wmtest).
  67. // Not used for chip stepping — ChipRate controls that.
  68. const RecordingRate = 48000
  69. // Embedder continuously embeds a watermark into audio L/R samples.
  70. // Not thread-safe: call NextSample from the single DSP goroutine only.
  71. type Embedder struct {
  72. codeword [rsTotalBytes]byte // RS-encoded payload, 16 bytes
  73. chipIdx int // chip position within current bit (0..pnChips-1)
  74. bitIdx int // current bit in codeword (0..127)
  75. symbol int8 // BPSK symbol for current bit: +1 or -1
  76. accum int // Bresenham accumulator for chip-rate stepping
  77. // Audio-level gate: mutes watermark during silence to prevent audibility.
  78. gateGain float64 // smooth ramp 0.0 (muted) → 1.0 (open)
  79. gateThreshold float64 // audio level below which gate closes
  80. gateRampUp float64 // per-sample increment when opening (~5ms)
  81. gateRampDown float64 // per-sample decrement when closing (~5ms)
  82. gateEnabled bool
  83. }
  84. // NewEmbedder creates an Embedder for the given license key.
  85. // The key's SHA-256 hash (first 8 bytes) is RS-encoded and embedded.
  86. // An empty key embeds a null payload (still watermarks, just anonymous).
  87. func NewEmbedder(key string) *Embedder {
  88. var data [rsDataBytes]byte
  89. if key != "" {
  90. h := sha256.Sum256([]byte(key))
  91. copy(data[:], h[:rsDataBytes])
  92. }
  93. e := &Embedder{gateGain: 1.0}
  94. e.codeword = rsEncode(data)
  95. e.loadSymbol()
  96. return e
  97. }
  98. // NextSample returns the watermark amplitude for one composite sample.
  99. // Add this value to both audio.Frame.L and audio.Frame.R before stereo encoding.
  100. //
  101. // The chip index advances using Bresenham stepping at ChipRate/CompositeRate,
  102. // so each chip occupies exactly CompositeRate/ChipRate composite samples on
  103. // average (~19 samples at 228 kHz). The PN signal bandwidth is 0–6 kHz.
  104. func (e *Embedder) NextSample() float64 {
  105. chip := float64(pnSequence[e.chipIdx])
  106. sample := Level * float64(e.symbol) * chip * e.gateGain
  107. // Bresenham: advance chip once per ChipRate/CompositeRate composite samples.
  108. e.accum += ChipRate
  109. if e.accum >= CompositeRate {
  110. e.accum -= CompositeRate
  111. e.chipIdx++
  112. if e.chipIdx >= pnChips {
  113. e.chipIdx = 0
  114. e.bitIdx = (e.bitIdx + 1) % payloadBits
  115. e.loadSymbol()
  116. }
  117. }
  118. return sample
  119. }
  120. // loadSymbol sets e.symbol from the current bit in the codeword (MSB first).
  121. func (e *Embedder) loadSymbol() {
  122. byteIdx := e.bitIdx / 8
  123. bitPos := uint(7 - (e.bitIdx % 8))
  124. if (e.codeword[byteIdx]>>bitPos)&1 == 0 {
  125. e.symbol = 1
  126. } else {
  127. e.symbol = -1
  128. }
  129. }
  130. // PayloadHex returns the RS-encoded codeword as hex for logging.
  131. func (e *Embedder) PayloadHex() string {
  132. const hx = "0123456789abcdef"
  133. out := make([]byte, rsTotalBytes*2)
  134. for i, b := range e.codeword {
  135. out[i*2] = hx[b>>4]
  136. out[i*2+1] = hx[b&0xf]
  137. }
  138. return string(out)
  139. }
  140. // EnableGate activates audio-level gating with asymmetric ramp times.
  141. // threshold is the linear audio amplitude below which the watermark is muted
  142. // (e.g. 0.01 ≈ -40 dBFS). compositeRate is needed to compute ramp speed.
  143. // Attack (open) is fast (5ms) so the watermark starts immediately with audio.
  144. // Release (close) is slow (200ms) to keep the watermark running through normal
  145. // inter-word and inter-phrase gaps — only extended silence mutes.
  146. func (e *Embedder) EnableGate(threshold, compositeRate float64) {
  147. attackSamples := compositeRate * 0.005 // 5ms open
  148. releaseSamples := compositeRate * 0.200 // 200ms close
  149. if attackSamples < 1 {
  150. attackSamples = 1
  151. }
  152. if releaseSamples < 1 {
  153. releaseSamples = 1
  154. }
  155. e.gateThreshold = threshold
  156. e.gateRampUp = 1.0 / attackSamples
  157. e.gateRampDown = 1.0 / releaseSamples
  158. e.gateEnabled = true
  159. }
  160. // SetAudioLevel updates the gate state based on the current audio amplitude.
  161. // Call once per sample before NextSample. absLevel should be the absolute
  162. // mono audio level (pre- or post-pre-emphasis, either works).
  163. func (e *Embedder) SetAudioLevel(absLevel float64) {
  164. if !e.gateEnabled {
  165. return
  166. }
  167. if absLevel > e.gateThreshold {
  168. e.gateGain += e.gateRampUp
  169. if e.gateGain > 1.0 {
  170. e.gateGain = 1.0
  171. }
  172. } else {
  173. e.gateGain -= e.gateRampDown
  174. if e.gateGain < 0.0 {
  175. e.gateGain = 0.0
  176. }
  177. }
  178. }
  179. // DiagnosticState returns internal state for debugging.
  180. type DiagnosticInfo struct {
  181. GateGain float64
  182. GateEnabled bool
  183. ChipIdx int
  184. BitIdx int
  185. Symbol int8
  186. }
  187. // DiagnosticState returns a snapshot of the embedder's internal state.
  188. func (e *Embedder) DiagnosticState() DiagnosticInfo {
  189. return DiagnosticInfo{
  190. GateGain: e.gateGain,
  191. GateEnabled: e.gateEnabled,
  192. ChipIdx: e.chipIdx,
  193. BitIdx: e.bitIdx,
  194. Symbol: e.symbol,
  195. }
  196. }
  197. // --- RS(16,8) over GF(2^8) — GF poly 0x11d, fcr=0, generator=2 ---
  198. // These routines are used by the embedder (encode) and the recovery tool (decode).
  199. func gfMul(a, b byte) byte {
  200. if a == 0 || b == 0 {
  201. return 0
  202. }
  203. return gfExp[(int(gfLog[a])+int(gfLog[b]))%255]
  204. }
  205. func gfInv(a byte) byte {
  206. if a == 0 {
  207. return 0
  208. }
  209. return gfExp[255-int(gfLog[a])]
  210. }
  211. func gfPow(a byte, n int) byte {
  212. if a == 0 {
  213. return 0
  214. }
  215. return gfExp[(int(gfLog[a])*n)%255]
  216. }
  217. // rsEncode encodes 8 data bytes into a 16-byte RS codeword.
  218. func rsEncode(data [rsDataBytes]byte) [rsTotalBytes]byte {
  219. var work [rsTotalBytes]byte
  220. copy(work[:rsDataBytes], data[:])
  221. // Polynomial long division by the generator polynomial.
  222. for i := 0; i < rsDataBytes; i++ {
  223. fb := work[i]
  224. if fb != 0 {
  225. for j := 1; j <= rsCheckBytes; j++ {
  226. work[i+j] ^= gfMul(rsGen[j], fb)
  227. }
  228. }
  229. }
  230. var cw [rsTotalBytes]byte
  231. copy(cw[:rsDataBytes], data[:])
  232. copy(cw[rsDataBytes:], work[rsDataBytes:])
  233. return cw
  234. }
  235. // RSDecode recovers 8 data bytes from a (possibly corrupted) 16-byte codeword.
  236. // erasurePositions lists the byte indices (0..15) of symbols with low confidence
  237. // that should be treated as erasures. Up to 8 erasures can be corrected.
  238. // Returns (data, true) on success, (zero, false) on decoding failure.
  239. func RSDecode(recv [rsTotalBytes]byte, erasurePositions []int) ([rsDataBytes]byte, bool) {
  240. // Step 1: compute syndromes S[i] = C(α^i) via Horner's method.
  241. // The polynomial convention is C(x) = c[0]x^15 + c[1]x^14 + … + c[15],
  242. // so byte position j contributes c[j]·(α^i)^(15-j) to S[i].
  243. var S [rsCheckBytes]byte
  244. for i := 0; i < rsCheckBytes; i++ {
  245. var acc byte
  246. for _, c := range recv {
  247. acc = gfMul(acc, gfPow(2, i)) ^ c
  248. }
  249. S[i] = acc
  250. }
  251. // Step 2: all syndromes zero → valid codeword.
  252. hasErr := false
  253. for _, s := range S {
  254. if s != 0 {
  255. hasErr = true
  256. break
  257. }
  258. }
  259. if !hasErr {
  260. var out [rsDataBytes]byte
  261. copy(out[:], recv[:rsDataBytes])
  262. return out, true
  263. }
  264. ne := len(erasurePositions)
  265. if ne == 0 || ne > rsCheckBytes {
  266. return [rsDataBytes]byte{}, false
  267. }
  268. // Step 3: Solve for error magnitudes via Vandermonde system.
  269. //
  270. // Because C(x) = c[0]x^15 + … + c[15], byte position j maps to
  271. // polynomial power (15-j). The "locator" for position j is
  272. // X_j = α^(15-j). The syndrome equation becomes:
  273. //
  274. // S[i] = Σ_k e_k · X_k^i
  275. //
  276. // This is a linear system (Vandermonde) in the unknowns e_k.
  277. // Solve by Gaussian elimination in GF(2^8).
  278. // Build locators
  279. X := make([]byte, ne)
  280. for k, pos := range erasurePositions {
  281. X[k] = gfPow(2, rsTotalBytes-1-pos)
  282. }
  283. // Augmented matrix [V | S], ne × (ne+1)
  284. mat := make([][]byte, ne)
  285. for i := range mat {
  286. mat[i] = make([]byte, ne+1)
  287. for k := 0; k < ne; k++ {
  288. mat[i][k] = gfPow(X[k], i)
  289. }
  290. mat[i][ne] = S[i]
  291. }
  292. // Gaussian elimination with partial pivoting
  293. for col := 0; col < ne; col++ {
  294. pivot := -1
  295. for row := col; row < ne; row++ {
  296. if mat[row][col] != 0 {
  297. pivot = row
  298. break
  299. }
  300. }
  301. if pivot < 0 {
  302. return [rsDataBytes]byte{}, false // singular
  303. }
  304. mat[col], mat[pivot] = mat[pivot], mat[col]
  305. inv := gfInv(mat[col][col])
  306. for j := 0; j <= ne; j++ {
  307. mat[col][j] = gfMul(mat[col][j], inv)
  308. }
  309. for row := 0; row < ne; row++ {
  310. if row == col {
  311. continue
  312. }
  313. f := mat[row][col]
  314. if f == 0 {
  315. continue
  316. }
  317. for j := 0; j <= ne; j++ {
  318. mat[row][j] ^= gfMul(f, mat[col][j])
  319. }
  320. }
  321. }
  322. // Apply corrections
  323. result := recv
  324. for k := 0; k < ne; k++ {
  325. result[erasurePositions[k]] ^= mat[k][ne]
  326. }
  327. // Verify syndromes after correction
  328. for i := 0; i < rsCheckBytes; i++ {
  329. var acc byte
  330. for _, c := range result {
  331. acc = gfMul(acc, gfPow(2, i)) ^ c
  332. }
  333. if acc != 0 {
  334. return [rsDataBytes]byte{}, false
  335. }
  336. }
  337. var out [rsDataBytes]byte
  338. copy(out[:], result[:rsDataBytes])
  339. return out, true
  340. }
  341. // KeyMatchesPayload returns true if SHA-256(key)[:8] matches payload.
  342. func KeyMatchesPayload(key string, payload [rsDataBytes]byte) bool {
  343. h := sha256.Sum256([]byte(key))
  344. var expected [rsDataBytes]byte
  345. copy(expected[:], h[:rsDataBytes])
  346. return expected == payload
  347. }
  348. // KeyToPayload returns SHA-256(key)[:8].
  349. func KeyToPayload(key string) [rsDataBytes]byte {
  350. var data [rsDataBytes]byte
  351. h := sha256.Sum256([]byte(key))
  352. copy(data[:], h[:rsDataBytes])
  353. return data
  354. }
  355. // RSEncode encodes 8 data bytes into a 16-byte RS codeword.
  356. func RSEncode(data [rsDataBytes]byte) [rsTotalBytes]byte {
  357. return rsEncode(data)
  358. }
  359. // PNChipAt returns the PN chip value at group g, bin b.
  360. func (d *STFTDetector) PNChipAt(g, b int) int8 {
  361. return d.pnChips[g][b]
  362. }
  363. // GroupBit returns the data bit index for group g.
  364. func (d *STFTDetector) GroupBit(g int) int {
  365. return d.groupToBit[g]
  366. }
  367. // Constants exported for the recovery tool and legacy tools.
  368. const (
  369. PnChips = pnChips
  370. PayloadBits = payloadBits
  371. RsDataBytes = rsDataBytes
  372. RsTotalBytes = rsTotalBytes
  373. RsCheckBytes = rsCheckBytes
  374. )
  375. // PnSequenceAt returns the PN chip value (+1.0 or -1.0) at the given index.
  376. // Used by the decoder for rate-compensated correlation.
  377. func PnSequenceAt(chipIdx int) float64 {
  378. return float64(pnSequence[chipIdx%pnChips])
  379. }
  380. // PNSequence exposes the raw PN chip values for the chip-rate decoder.
  381. var PNSequence = &pnSequence
  382. // CorrelateAt returns the correlation of received samples at the given bit
  383. // position. recRate is the WAV sample rate.
  384. //
  385. // At any recording rate, chips are mapped via ChipRate: each chip spans
  386. // recRate/ChipRate samples. At 48 kHz: 4 samples/chip, 8192 samples/bit.
  387. // At 192 kHz: 16 samples/chip, 32768 samples/bit.
  388. func CorrelateAt(samples []float64, bitStart int, recRate float64) float64 {
  389. samplesPerBit := int(float64(pnChips) * recRate / float64(ChipRate))
  390. if samplesPerBit < 1 {
  391. samplesPerBit = 1
  392. }
  393. n := samplesPerBit
  394. if bitStart+n > len(samples) {
  395. n = len(samples) - bitStart
  396. }
  397. var acc float64
  398. for i := 0; i < n; i++ {
  399. chipIdx := int(float64(i)*float64(ChipRate)/recRate) % pnChips
  400. acc += samples[bitStart+i] * float64(pnSequence[chipIdx])
  401. }
  402. return acc
  403. }
  404. // pnSequence is the 2048-chip LFSR-13 spreading code (seed 0x1ACE).
  405. var pnSequence = [pnChips]int8{
  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. 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1,
  480. -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1,
  481. 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1,
  482. -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 1,
  483. -1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1,
  484. -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1,
  485. -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1,
  486. -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1,
  487. 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, 1,
  488. -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1,
  489. 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  490. 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 1,
  491. 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1,
  492. 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1,
  493. -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1,
  494. -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1,
  495. -1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1,
  496. 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1,
  497. -1, -1, 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1,
  498. -1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1,
  499. -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1,
  500. -1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1,
  501. 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1,
  502. 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1,
  503. -1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1,
  504. -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1,
  505. 1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1,
  506. -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1,
  507. -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1,
  508. -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1,
  509. -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1,
  510. 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1,
  511. -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1,
  512. -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1,
  513. -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, -1,
  514. -1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1,
  515. -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1,
  516. 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, -1,
  517. -1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1,
  518. 1, -1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1,
  519. 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1,
  520. 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1,
  521. -1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1,
  522. 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1,
  523. -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1,
  524. -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1,
  525. -1, -1, 1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, -1, 1,
  526. 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, -1,
  527. -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1,
  528. -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1,
  529. 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1,
  530. -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1,
  531. 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1,
  532. -1, -1, 1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1,
  533. 1, 1, 1, -1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  534. }
  535. // GF(2^8) tables with primitive polynomial 0x11d.
  536. 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}
  537. 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}
  538. var rsGen = [9]byte{1, 255, 11, 81, 54, 239, 173, 200, 24}
  539. // rsGen is the RS(16,8) generator polynomial coefficients (fcr=0).