gf256.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package gf256 implements arithmetic over the Galois Field GF(256).
  5. package gf256
  6. import "strconv"
  7. // A Field represents an instance of GF(256) defined by a specific polynomial.
  8. type Field struct {
  9. log [256]byte // log[0] is unused
  10. exp [510]byte
  11. }
  12. // NewField returns a new field corresponding to the polynomial poly
  13. // and generator α. The Reed-Solomon encoding in QR codes uses
  14. // polynomial 0x11d with generator 2.
  15. //
  16. // The choice of generator α only affects the Exp and Log operations.
  17. func NewField(poly, α int) *Field {
  18. if poly < 0x100 || poly >= 0x200 || reducible(poly) {
  19. panic("gf256: invalid polynomial: " + strconv.Itoa(poly))
  20. }
  21. var f Field
  22. x := 1
  23. for i := 0; i < 255; i++ {
  24. if x == 1 && i != 0 {
  25. panic("gf256: invalid generator " + strconv.Itoa(α) +
  26. " for polynomial " + strconv.Itoa(poly))
  27. }
  28. f.exp[i] = byte(x)
  29. f.exp[i+255] = byte(x)
  30. f.log[x] = byte(i)
  31. x = mul(x, α, poly)
  32. }
  33. f.log[0] = 255
  34. for i := 0; i < 255; i++ {
  35. if f.log[f.exp[i]] != byte(i) {
  36. panic("bad log")
  37. }
  38. if f.log[f.exp[i+255]] != byte(i) {
  39. panic("bad log")
  40. }
  41. }
  42. for i := 1; i < 256; i++ {
  43. if f.exp[f.log[i]] != byte(i) {
  44. panic("bad log")
  45. }
  46. }
  47. return &f
  48. }
  49. // nbit returns the number of significant in p.
  50. func nbit(p int) uint {
  51. n := uint(0)
  52. for ; p > 0; p >>= 1 {
  53. n++
  54. }
  55. return n
  56. }
  57. // polyDiv divides the polynomial p by q and returns the remainder.
  58. func polyDiv(p, q int) int {
  59. np := nbit(p)
  60. nq := nbit(q)
  61. for ; np >= nq; np-- {
  62. if p&(1<<(np-1)) != 0 {
  63. p ^= q << (np - nq)
  64. }
  65. }
  66. return p
  67. }
  68. // mul returns the product x*y mod poly, a GF(256) multiplication.
  69. func mul(x, y, poly int) int {
  70. z := 0
  71. for x > 0 {
  72. if x&1 != 0 {
  73. z ^= y
  74. }
  75. x >>= 1
  76. y <<= 1
  77. if y&0x100 != 0 {
  78. y ^= poly
  79. }
  80. }
  81. return z
  82. }
  83. // reducible reports whether p is reducible.
  84. func reducible(p int) bool {
  85. // Multiplying n-bit * n-bit produces (2n-1)-bit,
  86. // so if p is reducible, one of its factors must be
  87. // of np/2+1 bits or fewer.
  88. np := nbit(p)
  89. for q := 2; q < 1<<(np/2+1); q++ {
  90. if polyDiv(p, q) == 0 {
  91. return true
  92. }
  93. }
  94. return false
  95. }
  96. // Add returns the sum of x and y in the field.
  97. func (f *Field) Add(x, y byte) byte {
  98. return x ^ y
  99. }
  100. // Exp returns the base-α exponential of e in the field.
  101. // If e < 0, Exp returns 0.
  102. func (f *Field) Exp(e int) byte {
  103. if e < 0 {
  104. return 0
  105. }
  106. return f.exp[e%255]
  107. }
  108. // Log returns the base-α logarithm of x in the field.
  109. // If x == 0, Log returns -1.
  110. func (f *Field) Log(x byte) int {
  111. if x == 0 {
  112. return -1
  113. }
  114. return int(f.log[x])
  115. }
  116. // Inv returns the multiplicative inverse of x in the field.
  117. // If x == 0, Inv returns 0.
  118. func (f *Field) Inv(x byte) byte {
  119. if x == 0 {
  120. return 0
  121. }
  122. return f.exp[255-f.log[x]]
  123. }
  124. // Mul returns the product of x and y in the field.
  125. func (f *Field) Mul(x, y byte) byte {
  126. if x == 0 || y == 0 {
  127. return 0
  128. }
  129. return f.exp[int(f.log[x])+int(f.log[y])]
  130. }
  131. // An RSEncoder implements Reed-Solomon encoding
  132. // over a given field using a given number of error correction bytes.
  133. type RSEncoder struct {
  134. f *Field
  135. c int
  136. gen []byte
  137. lgen []byte
  138. p []byte
  139. }
  140. func (f *Field) gen(e int) (gen, lgen []byte) {
  141. // p = 1
  142. p := make([]byte, e+1)
  143. p[e] = 1
  144. for i := 0; i < e; i++ {
  145. // p *= (x + Exp(i))
  146. // p[j] = p[j]*Exp(i) + p[j+1].
  147. c := f.Exp(i)
  148. for j := 0; j < e; j++ {
  149. p[j] = f.Mul(p[j], c) ^ p[j+1]
  150. }
  151. p[e] = f.Mul(p[e], c)
  152. }
  153. // lp = log p.
  154. lp := make([]byte, e+1)
  155. for i, c := range p {
  156. if c == 0 {
  157. lp[i] = 255
  158. } else {
  159. lp[i] = byte(f.Log(c))
  160. }
  161. }
  162. return p, lp
  163. }
  164. // NewRSEncoder returns a new Reed-Solomon encoder
  165. // over the given field and number of error correction bytes.
  166. func NewRSEncoder(f *Field, c int) *RSEncoder {
  167. gen, lgen := f.gen(c)
  168. return &RSEncoder{f: f, c: c, gen: gen, lgen: lgen}
  169. }
  170. // ECC writes to check the error correcting code bytes
  171. // for data using the given Reed-Solomon parameters.
  172. func (rs *RSEncoder) ECC(data []byte, check []byte) {
  173. if len(check) < rs.c {
  174. panic("gf256: invalid check byte length")
  175. }
  176. if rs.c == 0 {
  177. return
  178. }
  179. // The check bytes are the remainder after dividing
  180. // data padded with c zeros by the generator polynomial.
  181. // p = data padded with c zeros.
  182. var p []byte
  183. n := len(data) + rs.c
  184. if len(rs.p) >= n {
  185. p = rs.p
  186. } else {
  187. p = make([]byte, n)
  188. }
  189. copy(p, data)
  190. for i := len(data); i < len(p); i++ {
  191. p[i] = 0
  192. }
  193. // Divide p by gen, leaving the remainder in p[len(data):].
  194. // p[0] is the most significant term in p, and
  195. // gen[0] is the most significant term in the generator,
  196. // which is always 1.
  197. // To avoid repeated work, we store various values as
  198. // lv, not v, where lv = log[v].
  199. f := rs.f
  200. lgen := rs.lgen[1:]
  201. for i := 0; i < len(data); i++ {
  202. c := p[i]
  203. if c == 0 {
  204. continue
  205. }
  206. q := p[i+1:]
  207. exp := f.exp[f.log[c]:]
  208. for j, lg := range lgen {
  209. if lg != 255 { // lgen uses 255 for log 0
  210. q[j] ^= exp[lg]
  211. }
  212. }
  213. }
  214. copy(check, p[len(data):])
  215. rs.p = p
  216. }