1
0

gentables.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //+build ignore
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. var logTable = [fieldSize]int16{
  7. -1, 0, 1, 25, 2, 50, 26, 198,
  8. 3, 223, 51, 238, 27, 104, 199, 75,
  9. 4, 100, 224, 14, 52, 141, 239, 129,
  10. 28, 193, 105, 248, 200, 8, 76, 113,
  11. 5, 138, 101, 47, 225, 36, 15, 33,
  12. 53, 147, 142, 218, 240, 18, 130, 69,
  13. 29, 181, 194, 125, 106, 39, 249, 185,
  14. 201, 154, 9, 120, 77, 228, 114, 166,
  15. 6, 191, 139, 98, 102, 221, 48, 253,
  16. 226, 152, 37, 179, 16, 145, 34, 136,
  17. 54, 208, 148, 206, 143, 150, 219, 189,
  18. 241, 210, 19, 92, 131, 56, 70, 64,
  19. 30, 66, 182, 163, 195, 72, 126, 110,
  20. 107, 58, 40, 84, 250, 133, 186, 61,
  21. 202, 94, 155, 159, 10, 21, 121, 43,
  22. 78, 212, 229, 172, 115, 243, 167, 87,
  23. 7, 112, 192, 247, 140, 128, 99, 13,
  24. 103, 74, 222, 237, 49, 197, 254, 24,
  25. 227, 165, 153, 119, 38, 184, 180, 124,
  26. 17, 68, 146, 217, 35, 32, 137, 46,
  27. 55, 63, 209, 91, 149, 188, 207, 205,
  28. 144, 135, 151, 178, 220, 252, 190, 97,
  29. 242, 86, 211, 171, 20, 42, 93, 158,
  30. 132, 60, 57, 83, 71, 109, 65, 162,
  31. 31, 45, 67, 216, 183, 123, 164, 118,
  32. 196, 23, 73, 236, 127, 12, 111, 246,
  33. 108, 161, 59, 82, 41, 157, 85, 170,
  34. 251, 96, 134, 177, 187, 204, 62, 90,
  35. 203, 89, 95, 176, 156, 169, 160, 81,
  36. 11, 245, 22, 235, 122, 117, 44, 215,
  37. 79, 174, 213, 233, 230, 231, 173, 232,
  38. 116, 214, 244, 234, 168, 80, 88, 175,
  39. }
  40. const (
  41. // The number of elements in the field.
  42. fieldSize = 256
  43. // The polynomial used to generate the logarithm table.
  44. //
  45. // There are a number of polynomials that work to generate
  46. // a Galois field of 256 elements. The choice is arbitrary,
  47. // and we just use the first one.
  48. //
  49. // The possibilities are: 29, 43, 45, 77, 95, 99, 101, 105,
  50. //* 113, 135, 141, 169, 195, 207, 231, and 245.
  51. generatingPolynomial = 29
  52. )
  53. func main() {
  54. t := generateExpTable()
  55. fmt.Printf("var expTable = %#v\n", t)
  56. //t2 := generateMulTableSplit(t)
  57. //fmt.Printf("var mulTable = %#v\n", t2)
  58. low, high := generateMulTableHalf(t)
  59. fmt.Printf("var mulTableLow = %#v\n", low)
  60. fmt.Printf("var mulTableHigh = %#v\n", high)
  61. }
  62. /**
  63. * Generates the inverse log table.
  64. */
  65. func generateExpTable() []byte {
  66. result := make([]byte, fieldSize*2-2)
  67. for i := 1; i < fieldSize; i++ {
  68. log := logTable[i]
  69. result[log] = byte(i)
  70. result[log+fieldSize-1] = byte(i)
  71. }
  72. return result
  73. }
  74. func generateMulTable(expTable []byte) []byte {
  75. result := make([]byte, 256*256)
  76. for v := range result {
  77. a := byte(v & 0xff)
  78. b := byte(v >> 8)
  79. if a == 0 || b == 0 {
  80. result[v] = 0
  81. continue
  82. }
  83. logA := int(logTable[a])
  84. logB := int(logTable[b])
  85. result[v] = expTable[logA+logB]
  86. }
  87. return result
  88. }
  89. func generateMulTableSplit(expTable []byte) [256][256]byte {
  90. var result [256][256]byte
  91. for a := range result {
  92. for b := range result[a] {
  93. if a == 0 || b == 0 {
  94. result[a][b] = 0
  95. continue
  96. }
  97. logA := int(logTable[a])
  98. logB := int(logTable[b])
  99. result[a][b] = expTable[logA+logB]
  100. }
  101. }
  102. return result
  103. }
  104. func generateMulTableHalf(expTable []byte) (low [256][16]byte, high [256][16]byte) {
  105. for a := range low {
  106. for b := range low {
  107. result := 0
  108. if !(a == 0 || b == 0) {
  109. logA := int(logTable[a])
  110. logB := int(logTable[b])
  111. result = int(expTable[logA+logB])
  112. }
  113. if (b & 0xf) == b {
  114. low[a][b] = byte(result)
  115. }
  116. if (b & 0xf0) == b {
  117. high[a][b>>4] = byte(result)
  118. }
  119. }
  120. }
  121. return
  122. }