Debian Codegraph @

skein/threefish/threefish_test.go

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