You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
6.6 KiB

  1. package keccak
  2. import (
  3. "bytes"
  4. "hash"
  5. "testing"
  6. )
  7. type test struct {
  8. length int
  9. input []byte
  10. output []byte
  11. }
  12. var tests = []test{
  13. {
  14. 28,
  15. []byte{},
  16. []byte{
  17. 0xf7, 0x18, 0x37, 0x50, 0x2b, 0xa8, 0xe1, 0x08,
  18. 0x37, 0xbd, 0xd8, 0xd3, 0x65, 0xad, 0xb8, 0x55,
  19. 0x91, 0x89, 0x56, 0x02, 0xfc, 0x55, 0x2b, 0x48,
  20. 0xb7, 0x39, 0x0a, 0xbd,
  21. },
  22. }, {
  23. 28,
  24. []byte("Keccak-224 Test Hash"),
  25. []byte{
  26. 0x30, 0x04, 0x5b, 0x34, 0x94, 0x6e, 0x1b, 0x2e,
  27. 0x09, 0x16, 0x13, 0x36, 0x2f, 0xd2, 0x2a, 0xa0,
  28. 0x8e, 0x2b, 0xea, 0xfe, 0xc5, 0xe8, 0xda, 0xee,
  29. 0x42, 0xc2, 0xe6, 0x65},
  30. }, {
  31. 32,
  32. []byte{},
  33. []byte{
  34. 0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c,
  35. 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
  36. 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b,
  37. 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70,
  38. },
  39. }, {
  40. 32,
  41. []byte("Keccak-256 Test Hash"),
  42. []byte{
  43. 0xa8, 0xd7, 0x1b, 0x07, 0xf4, 0xaf, 0x26, 0xa4,
  44. 0xff, 0x21, 0x02, 0x7f, 0x62, 0xff, 0x60, 0x26,
  45. 0x7f, 0xf9, 0x55, 0xc9, 0x63, 0xf0, 0x42, 0xc4,
  46. 0x6d, 0xa5, 0x2e, 0xe3, 0xcf, 0xaf, 0x3d, 0x3c},
  47. }, {
  48. 48,
  49. []byte{},
  50. []byte{
  51. 0x2c, 0x23, 0x14, 0x6a, 0x63, 0xa2, 0x9a, 0xcf,
  52. 0x99, 0xe7, 0x3b, 0x88, 0xf8, 0xc2, 0x4e, 0xaa,
  53. 0x7d, 0xc6, 0x0a, 0xa7, 0x71, 0x78, 0x0c, 0xcc,
  54. 0x00, 0x6a, 0xfb, 0xfa, 0x8f, 0xe2, 0x47, 0x9b,
  55. 0x2d, 0xd2, 0xb2, 0x13, 0x62, 0x33, 0x74, 0x41,
  56. 0xac, 0x12, 0xb5, 0x15, 0x91, 0x19, 0x57, 0xff,
  57. },
  58. }, {
  59. 48,
  60. []byte("Keccak-384 Test Hash"),
  61. []byte{
  62. 0xe2, 0x13, 0xfd, 0x74, 0xaf, 0x0c, 0x5f, 0xf9,
  63. 0x1b, 0x42, 0x3c, 0x8b, 0xce, 0xec, 0xd7, 0x01,
  64. 0xf8, 0xdd, 0x64, 0xec, 0x18, 0xfd, 0x6f, 0x92,
  65. 0x60, 0xfc, 0x9e, 0xc1, 0xed, 0xbd, 0x22, 0x30,
  66. 0xa6, 0x90, 0x86, 0x65, 0xbc, 0xd9, 0xfb, 0xf4,
  67. 0x1a, 0x99, 0xa1, 0x8a, 0x7d, 0x9e, 0x44, 0x6e},
  68. }, {
  69. 64,
  70. []byte{},
  71. []byte{
  72. 0x0e, 0xab, 0x42, 0xde, 0x4c, 0x3c, 0xeb, 0x92,
  73. 0x35, 0xfc, 0x91, 0xac, 0xff, 0xe7, 0x46, 0xb2,
  74. 0x9c, 0x29, 0xa8, 0xc3, 0x66, 0xb7, 0xc6, 0x0e,
  75. 0x4e, 0x67, 0xc4, 0x66, 0xf3, 0x6a, 0x43, 0x04,
  76. 0xc0, 0x0f, 0xa9, 0xca, 0xf9, 0xd8, 0x79, 0x76,
  77. 0xba, 0x46, 0x9b, 0xcb, 0xe0, 0x67, 0x13, 0xb4,
  78. 0x35, 0xf0, 0x91, 0xef, 0x27, 0x69, 0xfb, 0x16,
  79. 0x0c, 0xda, 0xb3, 0x3d, 0x36, 0x70, 0x68, 0x0e,
  80. },
  81. }, {
  82. 64,
  83. []byte("Keccak-512 Test Hash"),
  84. []byte{
  85. 0x96, 0xee, 0x47, 0x18, 0xdc, 0xba, 0x3c, 0x74,
  86. 0x61, 0x9b, 0xa1, 0xfa, 0x7f, 0x57, 0xdf, 0xe7,
  87. 0x76, 0x9d, 0x3f, 0x66, 0x98, 0xa8, 0xb3, 0x3f,
  88. 0xa1, 0x01, 0x83, 0x89, 0x70, 0xa1, 0x31, 0xe6,
  89. 0x21, 0xcc, 0xfd, 0x05, 0xfe, 0xff, 0xbc, 0x11,
  90. 0x80, 0xf2, 0x63, 0xc2, 0x7f, 0x1a, 0xda, 0xb4,
  91. 0x60, 0x95, 0xd6, 0xf1, 0x25, 0x33, 0x14, 0x72,
  92. 0x4b, 0x5c, 0xbf, 0x78, 0x28, 0x65, 0x8e, 0x6a,
  93. },
  94. },
  95. }
  96. func TestKeccak(t *testing.T) {
  97. for i := range tests {
  98. var h hash.Hash
  99. switch tests[i].length {
  100. case 28:
  101. h = New224()
  102. case 32:
  103. h = New256()
  104. case 48:
  105. h = New384()
  106. case 64:
  107. h = New512()
  108. default:
  109. panic("invalid testcase")
  110. }
  111. h.Write(tests[i].input)
  112. d := h.Sum(nil)
  113. if !bytes.Equal(d, tests[i].output) {
  114. t.Errorf("testcase %d: expected %x got %x", i, tests[i].output, d)
  115. }
  116. }
  117. }
  118. type testcase struct {
  119. msg []byte
  120. output224 []byte
  121. output256 []byte
  122. output384 []byte
  123. output512 []byte
  124. }
  125. func TestKeccakShort224(t *testing.T) {
  126. h := New224()
  127. for i := range tstShort {
  128. h.Reset()
  129. h.Write(tstShort[i].msg)
  130. d := h.Sum(nil)
  131. if !bytes.Equal(d, tstShort[i].output224) {
  132. t.Errorf("testcase Short224 %d: expected %x got %x", i, tstShort[i].output224, d)
  133. }
  134. }
  135. }
  136. func TestKeccakShort256(t *testing.T) {
  137. h := New256()
  138. for i := range tstShort {
  139. h.Reset()
  140. h.Write(tstShort[i].msg)
  141. d := h.Sum(nil)
  142. if !bytes.Equal(d, tstShort[i].output256) {
  143. t.Errorf("testcase Short256 %d: expected %x got %x", i, tstShort[i].output256, d)
  144. }
  145. }
  146. }
  147. func TestKeccakShort384(t *testing.T) {
  148. h := New384()
  149. for i := range tstShort {
  150. h.Reset()
  151. h.Write(tstShort[i].msg)
  152. d := h.Sum(nil)
  153. if !bytes.Equal(d, tstShort[i].output384) {
  154. t.Errorf("testcase Short384 %d: expected %x got %x", i, tstShort[i].output384, d)
  155. }
  156. }
  157. }
  158. func TestKeccakShort512(t *testing.T) {
  159. h := New512()
  160. for i := range tstShort {
  161. h.Reset()
  162. h.Write(tstShort[i].msg)
  163. d := h.Sum(nil)
  164. if !bytes.Equal(d, tstShort[i].output512) {
  165. t.Errorf("testcase Short512 %d: expected %x got %x", i, tstShort[i].output512, d)
  166. }
  167. }
  168. }
  169. func TestKeccakLong224(t *testing.T) {
  170. h := New224()
  171. for i := range tstLong {
  172. h.Reset()
  173. h.Write(tstLong[i].msg)
  174. d := h.Sum(nil)
  175. if !bytes.Equal(d, tstLong[i].output224) {
  176. t.Errorf("testcase Long224 %d: expected %x got %x", i, tstLong[i].output224, d)
  177. }
  178. }
  179. }
  180. func TestKeccakLong256(t *testing.T) {
  181. h := New256()
  182. for i := range tstLong {
  183. h.Reset()
  184. h.Write(tstLong[i].msg)
  185. d := h.Sum(nil)
  186. if !bytes.Equal(d, tstLong[i].output256) {
  187. t.Errorf("testcase Long256 %d: expected %x got %x", i, tstLong[i].output256, d)
  188. }
  189. }
  190. }
  191. func TestKeccakLong384(t *testing.T) {
  192. h := New384()
  193. for i := range tstLong {
  194. h.Reset()
  195. h.Write(tstLong[i].msg)
  196. d := h.Sum(nil)
  197. if !bytes.Equal(d, tstLong[i].output384) {
  198. t.Errorf("testcase Long384 %d: expected %x got %x", i, tstLong[i].output384, d)
  199. }
  200. }
  201. }
  202. func TestKeccakLong512(t *testing.T) {
  203. h := New512()
  204. for i := range tstLong {
  205. h.Reset()
  206. h.Write(tstLong[i].msg)
  207. d := h.Sum(nil)
  208. if !bytes.Equal(d, tstLong[i].output512) {
  209. t.Errorf("testcase Long512 %d: expected %x got %x", i, tstLong[i].output512, d)
  210. }
  211. }
  212. }
  213. func TestSmallWrites(t *testing.T) {
  214. h := New512()
  215. tc := tstLong[len(tstLong)-1]
  216. for i := 0; i < len(tc.msg); i++ {
  217. h.Write(tc.msg[i : i+1])
  218. }
  219. d := h.Sum(nil)
  220. if !bytes.Equal(d, tc.output512) {
  221. t.Errorf("testcase small writes: expected %x got %x", tc.output512, d)
  222. }
  223. }
  224. func benchmarkHash(b *testing.B, h hash.Hash) {
  225. m := make([]byte, 4096)
  226. h.Reset()
  227. b.SetBytes(int64(len(m)))
  228. b.ReportAllocs()
  229. b.ResetTimer()
  230. for i := 0; i < b.N; i++ {
  231. h.Write(m)
  232. }
  233. }
  234. func BenchmarkKeccakf(b *testing.B) {
  235. var s [25]uint64
  236. b.SetBytes(25 * 8)
  237. for i := 0; i < b.N; i++ {
  238. keccakf(&s)
  239. }
  240. }
  241. func Benchmark224(b *testing.B) { benchmarkHash(b, New224()) }
  242. func Benchmark256(b *testing.B) { benchmarkHash(b, New256()) }
  243. func Benchmark384(b *testing.B) { benchmarkHash(b, New384()) }
  244. func Benchmark512(b *testing.B) { benchmarkHash(b, New512()) }
  245. func BenchmarkSHA3224(b *testing.B) { benchmarkHash(b, NewSHA3224()) }
  246. func BenchmarkSHA3256(b *testing.B) { benchmarkHash(b, NewSHA3256()) }
  247. func BenchmarkSHA3384(b *testing.B) { benchmarkHash(b, NewSHA3384()) }
  248. func BenchmarkSHA3512(b *testing.B) { benchmarkHash(b, NewSHA3512()) }
  249. func BenchmarkSHAKE128(b *testing.B) { benchmarkHash(b, NewSHAKE128(256)) }
  250. func BenchmarkSHAKE256(b *testing.B) { benchmarkHash(b, NewSHAKE256(256)) }