-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwst_test.go
91 lines (82 loc) · 1.77 KB
/
bwst_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package bwst
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"testing"
)
const test_string = `Wherever you go, I follow,
hands held across sidewalks
frozen
or slopes more slippery
called love,
life, and 1685 miles
of "I can't wait
to be there when you fall."`
func TestAbsorption(t *testing.T) {
s := []byte(test_string)
s = UnBWST(BWST(s))
if string(s) != test_string {
t.Fatal("UnBWST(BWST(s)) failed: expected a sweet poem, got gibberish")
}
s = BWST(UnBWST(s))
if string(s) != test_string {
t.Fatal("BWST(UnBWST(s)) failed: expected a sweet poem, got gibberish")
}
}
func TestAbsorptionRandom(t *testing.T) {
s := makerandombytes(1 << 15)
c := s
s = UnBWST(BWST(s))
if !bytes.Equal(c, s) {
t.Fatal("UnBWST(BWST(s)) failed: expected randomness, got gibberish")
}
s = BWST(UnBWST(s))
if !bytes.Equal(c, s) {
t.Fatal("BWST(UnBWST(s)) failed: expected randomness, got gibberish")
}
}
func BenchmarkBWST(b *testing.B) {
s := []byte(test_string)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = BWST(s)
}
}
func BenchmarkBWSTRandom(b *testing.B) {
s := makerandombytes(1 << 15)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = BWST(s)
}
}
func BenchmarkUnBWST(b *testing.B) {
s := []byte(test_string)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = UnBWST(s)
}
}
func BenchmarkUnBWSTRandom(b *testing.B) {
s := makerandombytes(1 << 15)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s = UnBWST(s)
}
}
func makerandombytes(n int) (b []byte) {
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
c, _ := aes.NewCipher(iv) // I do insist.
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
s := cipher.NewCTR(c, iv)
b = make([]byte, n)
s.XORKeyStream(b, b)
return b
}