-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfg_test.go
78 lines (71 loc) · 1.46 KB
/
lfg_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
package crazy
import (
"bytes"
"crypto/rand"
"testing"
)
// LFG.Seed() is trivial so I'm not going to bother.
func TestLFGSeed(t *testing.T) {
lfg := NewLFG()
lfg.SeedIV(nil)
lfg.SeedIV([]byte{lfgK: 0})
lfg.SeedIV([]byte{8 * lfgK: 0})
lfg.SeedIV([]byte{9 * lfgK: 0})
}
func TestLFGSeedConsistency(t *testing.T) {
iv := make([]byte, 128)
lfg := NewLFG()
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*lfgK; i++ {
rand.Read(iv)
lfg.SeedIV(iv)
lfg.Read(x)
lfg.SeedIV(iv)
lfg.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
}
}
func TestLFGSave(t *testing.T) {
b := bytes.Buffer{}
lfg := CryptoSeeded(NewLFG(), lfgK).(*LFG)
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*lfgK; i++ {
lfg.Save(&b)
lfg.Read(x)
lfg.Restore(&b)
lfg.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
b.Reset()
}
}
func TestLFGCopy(t *testing.T) {
lfg := CryptoSeeded(NewLFG(), lfgK).(*LFG)
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*lfgK; i++ {
cp := lfg.Copy()
lfg.Read(x)
cp.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
}
}
func BenchmarkLFG(b *testing.B) {
lfg := CryptoSeeded(NewLFG(), lfgK).(*LFG)
f := func(p []byte) func(b *testing.B) {
return func(b *testing.B) {
b.SetBytes(int64(len(p)))
for n := 0; n < b.N; n++ {
lfg.Read(p)
}
}
}
b.Run("8", f(make([]byte, 8)))
b.Run("K", f(make([]byte, 1<<10)))
b.Run("M", f(make([]byte, 1<<25)))
b.Run("G", f(make([]byte, 1<<30)))
}