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.

120 lines
3.5 KiB

  1. // Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
  2. // Use of this source code is governed by a license that can be
  3. // found in the LICENSE file.
  4. package threefish
  5. import "testing"
  6. // The UBI256, UBI512 and UBI1024 functions are tested within
  7. // the skein packages (skein, skein256 and skein1024)
  8. func testBlockSize(t *testing.T, blocksize int) {
  9. var tweak [TweakSize]byte
  10. c, err := NewCipher(&tweak, make([]byte, blocksize))
  11. if err != nil {
  12. t.Fatalf("Failed to create Threefish-%d instance: %s", blocksize*8, err)
  13. }
  14. if bs := c.BlockSize(); bs != blocksize {
  15. t.Fatalf("BlockSize() returned unexpected value: %d - expected %d", bs, blocksize)
  16. }
  17. }
  18. func TestBlockSize(t *testing.T) {
  19. testBlockSize(t, BlockSize256)
  20. testBlockSize(t, BlockSize512)
  21. testBlockSize(t, BlockSize1024)
  22. }
  23. func TestNew(t *testing.T) {
  24. badKeyLengths := []int{
  25. 0, 31, 33, 63, 65, 127, 129,
  26. }
  27. var tweak [TweakSize]byte
  28. for i, v := range badKeyLengths {
  29. _, err := NewCipher(&tweak, make([]byte, v))
  30. if err == nil {
  31. t.Fatalf("BadKey %d: NewCipher accepted inavlid key length %d", i, v)
  32. }
  33. }
  34. }
  35. func TestIncrementTweak(t *testing.T) {
  36. var tweak [3]uint64
  37. IncrementTweak(&tweak, 1)
  38. if tweak[0] != 1 {
  39. t.Fatalf("IncrementTweak failed by increment of %d", 1)
  40. }
  41. tweak[0] = ^uint64(0)
  42. IncrementTweak(&tweak, 2)
  43. if tweak[0] != 1 && tweak[1] != 1 {
  44. t.Fatalf("IncrementTweak failed by increment of %d", 2)
  45. }
  46. tweak[0] = ^uint64(0)
  47. tweak[1] = uint64(0xFFFFFFFF)
  48. IncrementTweak(&tweak, 1)
  49. if tweak[0] != 0 && tweak[1] != 0 {
  50. t.Fatalf("IncrementTweak failed by increment of %d", 1)
  51. }
  52. }
  53. // Benchmarks
  54. func benchmarkEncrypt(b *testing.B, blocksize, size int) {
  55. key := make([]byte, blocksize)
  56. var tweak [TweakSize]byte
  57. c, err := NewCipher(&tweak, key)
  58. if err != nil {
  59. b.Fatalf("Failed to create Threefish-%d instance: %s", blocksize*8, err)
  60. }
  61. n := size / blocksize
  62. buf := make([]byte, blocksize)
  63. b.SetBytes(int64(blocksize * n))
  64. b.ResetTimer()
  65. for i := 0; i < b.N; i++ {
  66. for j := 0; j < n; j++ {
  67. c.Encrypt(buf, buf)
  68. }
  69. }
  70. }
  71. func benchmarkDecrypt(b *testing.B, blocksize, size int) {
  72. key := make([]byte, blocksize)
  73. var tweak [TweakSize]byte
  74. c, err := NewCipher(&tweak, key)
  75. if err != nil {
  76. b.Fatalf("Failed to create Threefish-%d instance: %s", blocksize*8, err)
  77. }
  78. n := size / blocksize
  79. buf := make([]byte, blocksize)
  80. b.SetBytes(int64(blocksize * n))
  81. b.ResetTimer()
  82. for i := 0; i < b.N; i++ {
  83. for j := 0; j < n; j++ {
  84. c.Decrypt(buf, buf)
  85. }
  86. }
  87. }
  88. func BenchmarkEncrypt256_32(b *testing.B) { benchmarkEncrypt(b, BlockSize256, 32) }
  89. func BenchmarkEncrypt256_1024(b *testing.B) { benchmarkEncrypt(b, BlockSize256, 1024) }
  90. func BenchmarkEncrypt512_64(b *testing.B) { benchmarkEncrypt(b, BlockSize512, 64) }
  91. func BenchmarkEncrypt512_1024(b *testing.B) { benchmarkEncrypt(b, BlockSize512, 1024) }
  92. func BenchmarkEncrypt1024_128(b *testing.B) { benchmarkEncrypt(b, BlockSize1024, 128) }
  93. func BenchmarkEncrypt1024_1024(b *testing.B) { benchmarkEncrypt(b, BlockSize1024, 1024) }
  94. func BenchmarkDecrypt256_32(b *testing.B) { benchmarkDecrypt(b, BlockSize256, 32) }
  95. func BenchmarkDecrypt256_1024(b *testing.B) { benchmarkDecrypt(b, BlockSize256, 1024) }
  96. func BenchmarkDecrypt512_64(b *testing.B) { benchmarkDecrypt(b, BlockSize512, 64) }
  97. func BenchmarkDecrypt512_1024(b *testing.B) { benchmarkDecrypt(b, BlockSize512, 1024) }
  98. func BenchmarkDecrypt1024_128(b *testing.B) { benchmarkDecrypt(b, BlockSize1024, 128) }
  99. func BenchmarkDecrypt1024_1024(b *testing.B) { benchmarkDecrypt(b, BlockSize1024, 1024) }