1
0

galois_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Unit tests for Galois
  3. *
  4. * Copyright 2015, Klaus Post
  5. * Copyright 2015, Backblaze, Inc.
  6. */
  7. package reedsolomon
  8. import (
  9. "bytes"
  10. "testing"
  11. )
  12. func TestAssociativity(t *testing.T) {
  13. for i := 0; i < 256; i++ {
  14. a := byte(i)
  15. for j := 0; j < 256; j++ {
  16. b := byte(j)
  17. for k := 0; k < 256; k++ {
  18. c := byte(k)
  19. x := galAdd(a, galAdd(b, c))
  20. y := galAdd(galAdd(a, b), c)
  21. if x != y {
  22. t.Fatal("add does not match:", x, "!=", y)
  23. }
  24. x = galMultiply(a, galMultiply(b, c))
  25. y = galMultiply(galMultiply(a, b), c)
  26. if x != y {
  27. t.Fatal("multiply does not match:", x, "!=", y)
  28. }
  29. }
  30. }
  31. }
  32. }
  33. func TestIdentity(t *testing.T) {
  34. for i := 0; i < 256; i++ {
  35. a := byte(i)
  36. b := galAdd(a, 0)
  37. if a != b {
  38. t.Fatal("Add zero should yield same result", a, "!=", b)
  39. }
  40. b = galMultiply(a, 1)
  41. if a != b {
  42. t.Fatal("Mul by one should yield same result", a, "!=", b)
  43. }
  44. }
  45. }
  46. func TestInverse(t *testing.T) {
  47. for i := 0; i < 256; i++ {
  48. a := byte(i)
  49. b := galSub(0, a)
  50. c := galAdd(a, b)
  51. if c != 0 {
  52. t.Fatal("inverse sub/add", c, "!=", 0)
  53. }
  54. if a != 0 {
  55. b = galDivide(1, a)
  56. c = galMultiply(a, b)
  57. if c != 1 {
  58. t.Fatal("inverse div/mul", c, "!=", 1)
  59. }
  60. }
  61. }
  62. }
  63. func TestCommutativity(t *testing.T) {
  64. for i := 0; i < 256; i++ {
  65. a := byte(i)
  66. for j := 0; j < 256; j++ {
  67. b := byte(j)
  68. x := galAdd(a, b)
  69. y := galAdd(b, a)
  70. if x != y {
  71. t.Fatal(x, "!= ", y)
  72. }
  73. x = galMultiply(a, b)
  74. y = galMultiply(b, a)
  75. if x != y {
  76. t.Fatal(x, "!= ", y)
  77. }
  78. }
  79. }
  80. }
  81. func TestDistributivity(t *testing.T) {
  82. for i := 0; i < 256; i++ {
  83. a := byte(i)
  84. for j := 0; j < 256; j++ {
  85. b := byte(j)
  86. for k := 0; k < 256; k++ {
  87. c := byte(k)
  88. x := galMultiply(a, galAdd(b, c))
  89. y := galAdd(galMultiply(a, b), galMultiply(a, c))
  90. if x != y {
  91. t.Fatal(x, "!= ", y)
  92. }
  93. }
  94. }
  95. }
  96. }
  97. func TestExp(t *testing.T) {
  98. for i := 0; i < 256; i++ {
  99. a := byte(i)
  100. power := byte(1)
  101. for j := 0; j < 256; j++ {
  102. x := galExp(a, j)
  103. if x != power {
  104. t.Fatal(x, "!=", power)
  105. }
  106. power = galMultiply(power, a)
  107. }
  108. }
  109. }
  110. func TestGalois(t *testing.T) {
  111. // These values were copied output of the Python code.
  112. if galMultiply(3, 4) != 12 {
  113. t.Fatal("galMultiply(3, 4) != 12")
  114. }
  115. if galMultiply(7, 7) != 21 {
  116. t.Fatal("galMultiply(7, 7) != 21")
  117. }
  118. if galMultiply(23, 45) != 41 {
  119. t.Fatal("galMultiply(23, 45) != 41")
  120. }
  121. // Test slices (>16 entries to test assembler)
  122. in := []byte{0, 1, 2, 3, 4, 5, 6, 10, 50, 100, 150, 174, 201, 255, 99, 32, 67, 85}
  123. out := make([]byte, len(in))
  124. galMulSlice(25, in, out, false, false)
  125. expect := []byte{0x0, 0x19, 0x32, 0x2b, 0x64, 0x7d, 0x56, 0xfa, 0xb8, 0x6d, 0xc7, 0x85, 0xc3, 0x1f, 0x22, 0x7, 0x25, 0xfe}
  126. if 0 != bytes.Compare(out, expect) {
  127. t.Errorf("got %#v, expected %#v", out, expect)
  128. }
  129. galMulSlice(177, in, out, false, false)
  130. expect = []byte{0x0, 0xb1, 0x7f, 0xce, 0xfe, 0x4f, 0x81, 0x9e, 0x3, 0x6, 0xe8, 0x75, 0xbd, 0x40, 0x36, 0xa3, 0x95, 0xcb}
  131. if 0 != bytes.Compare(out, expect) {
  132. t.Errorf("got %#v, expected %#v", out, expect)
  133. }
  134. if galExp(2, 2) != 4 {
  135. t.Fatal("galExp(2, 2) != 4")
  136. }
  137. if galExp(5, 20) != 235 {
  138. t.Fatal("galExp(5, 20) != 235")
  139. }
  140. if galExp(13, 7) != 43 {
  141. t.Fatal("galExp(13, 7) != 43")
  142. }
  143. }