-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
333 lines (279 loc) · 9.62 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"flag"
"fmt"
"os"
stream "github.com/notJoon/drbg/bitstream"
nist "github.com/notJoon/drbg/nist"
"github.com/jedib0t/go-pretty/table"
)
func main() {
allTests := flag.Bool("all", false, "Run all tests")
frequency := flag.Bool("frequency", false, "Run Frequency (Monobit) Test")
blockFrequency := flag.Bool("block-frequency", false, "Run Frequency Test within a Block")
blockFrequencyBlockSize := flag.Uint64("frequency-block-size", 128, "The length in bits of the substring to be tested")
runs := flag.Bool("runs", false, "Run Runs Test")
longestRun := flag.Bool("longest-run", false, "Run Test for the Longest Run of Ones in a Block")
rank := flag.Bool("rank", false, "Run Binary Matrix Rank Test")
dft := flag.Bool("dft", false, "Run Discrete Fourier Transform (Spectral) Test")
nonOverlappingTemplate := flag.Bool("non-overlapping", false, "Run Non-overlapping Template Matching Test.\nDefault template is \"000000001\" and block size is 10 bits.")
overlappingTemplate := flag.Bool("overlapping", false, "Run Overlapping Template Matching Test.\nDefault template is \"000000001\" and block size is 10 bits.")
// specifies the template B to match. Must be string of ones and zeros (e.g. "001")
templateB := flag.String("template", "000000001", "The template B to be matched (a string of ones and zeros)")
// specified the length of the substrting to test, in bits.
blockSize := flag.Uint64("block-size", 20, "The length in bits of the substring to be tested")
universal := flag.Bool("universal", false, "Run Maurer's Universal Statistical Test")
linearComplexity := flag.Bool("linear", false, "Run Linear Complexity Test. Default block size is 500 bits.")
inputSize := flag.Uint64("m", 500, "The length of the block to be tested")
serial := flag.Bool("serial", false, "Run Serial Test. Default block size is 16 bits.")
serialBlockSize := flag.Uint64("serial-size", 16, "The length in bits of the substring to be tested")
approximateEntropy := flag.Bool("entropy", false, "Run Approximate Entropy Test")
approximateEntropyBlockSize := flag.Uint64("entropy-block-size", 10, "The length in bits of the substring to be tested")
cusum := flag.Bool("cusum", false, "Run Cumulative Sums (Cusums) Test. Default mode is 0 (forward).")
mode := flag.Int("mode", 0, "The mode of the test (0 or 1)")
randomExcursions := flag.Bool("random-excursions", false, "Run Random Excursions Test")
randomExcursionsVariant := flag.Bool("random-excursions-variant", false, "Run Random Excursions Variant Test")
filename := flag.String("file", "", "File containing the random bits")
help := flag.Bool("help", false, "Show help message")
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
if *filename == "" {
fmt.Println("Error: No file specified")
os.Exit(1)
}
var (
bs *stream.BitStream
err error
)
// regulation of the bitstream
// ????
if *frequency {
bs, err = stream.FromFileWithLimit(*filename, 100)
} else {
bs, err = stream.FromFile(*filename)
}
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
// test result counters
pass, fail := 0, 0
// Draw table for test results
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"NIST Statistical Test Suite", "p-value", "Result"})
if *allTests || *frequency {
testName := "Frequency (Monobit) Test"
p_val, isRandom, err := nist.FrequencyTest(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *blockFrequency {
testName := "Frequency Test within a Block"
p_val, isRandom, err := nist.BlockFrequencyTest(bs, uint64(*blockFrequencyBlockSize))
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *runs {
testName := "Runs Test"
p_val, isRandom, err := nist.Runs(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *longestRun {
testName := "Test for the Longest Run of Ones in a Block"
p_val, isRandom, err := nist.LongestRunOfOnes(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *rank {
testName := "Binary Matrix Rank Test"
p_val, isRandom, err := nist.Rank(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *dft {
testName := "Discrete Fourier Transform (Spectral) Test"
p_val, isRandom, err := nist.DFT(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *nonOverlappingTemplate {
testName := "Non-overlapping Template Matching Test"
if *templateB == "" {
fmt.Println("Error (non-overlapping template test): template B is required for Non-overlapping Template Matching Test.\nUse -template \"001\" (or other tmeplate)")
os.Exit(1)
}
if *blockSize == 0 {
fmt.Println("Error (non-overlapping template test): block size is required for Non-overlapping Template Matching Test.\nUse -block-size 10 (or other block size)")
os.Exit(1)
}
B := make([]uint8, len(*templateB))
for i, c := range *templateB {
switch c {
case '0':
B[i] = 0
case '1':
B[i] = 1
default:
fmt.Printf("Error (non-overlapping template test): invalid character in template B: %c\n", c)
os.Exit(1)
}
}
p_value, isRandom, err := nist.NonOverlappingTemplateMatching(B, *blockSize, bs)
if err != nil {
fmt.Printf("Error (non-overlapping template test): %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_value, isRandom, &pass, &fail)
}
if *allTests || *overlappingTemplate {
testName := "Overlapping Template Matching Test"
if *templateB == "" {
fmt.Println("Error (overlapping templelate test): template B is required for Non-overlapping Template Matching Test.\nUse -template \"001\" (or other tmeplate)")
os.Exit(1)
}
if *blockSize == 0 {
fmt.Println("Error (overlapping templelate test): block size is required for Non-overlapping Template Matching Test.\nUse -block-size 10 (or other block size)")
os.Exit(1)
}
B := make([]uint8, len(*templateB))
for i, c := range *templateB {
switch c {
case '0':
B[i] = 0
case '1':
B[i] = 1
default:
fmt.Printf("Error (overlapping templelate test): invalid character in template B: %c\n", c)
os.Exit(1)
}
}
p_value, isRandom, err := nist.OverlappingTemplateMatching(B, *blockSize, bs)
if err != nil {
fmt.Printf("Error (overlapping templelate test): %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_value, isRandom, &pass, &fail)
}
if *universal {
testName := "Maurer's Universal Statistical Test"
p_val, isRandom, err := nist.UniversalRecommendedValues(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *linearComplexity {
testName := "Linear Complexity Test"
if *inputSize < 500 || *inputSize > 5000 {
fmt.Println("Error: input size must be between 500 and 5000")
os.Exit(1)
}
p_val, isRandom, err := nist.LinearComplexity(*inputSize, bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *serial {
testName := "Serial Test"
p_val, isRandom, err := nist.Serial(*serialBlockSize, bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if isRandom != nil {
pass++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Pass"})
} else {
fail++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Fail"})
}
}
if *allTests || *approximateEntropy {
testName := "Approximate Entropy Test"
p_val, isRandom, err := nist.ApproximateEntropy(*approximateEntropyBlockSize, bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *cusum {
testName := "Cumulative Sums Test"
p_val, isRandom, err := nist.CumulativeSums(*mode, bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
writeResult(t, testName, p_val, isRandom, &pass, &fail)
}
if *allTests || *randomExcursions {
testName := "Random Excursions Test"
p_val, isRandom, err := nist.RandomExcursions(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if isRandom != nil {
pass++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Pass"})
} else {
fail++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Fail"})
}
}
if *allTests || *randomExcursionsVariant {
testName := "Random Excursions Variant Test"
p_val, isRandom, err := nist.RandomExcursionsVariant(bs)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if isRandom != nil {
pass++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Pass"})
} else {
fail++
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", p_val), "Fail"})
}
}
t.AppendFooter(table.Row{"", "Total Tests", pass + fail})
t.AppendFooter(table.Row{"", "Pass", pass})
t.AppendFooter(table.Row{"", "Fail", fail})
t.Render()
}
// writeResult writes the result of a test to the table
func writeResult(t table.Writer, testName string, pValue float64, isRandom bool, pass *int, fail *int) {
result := "Fail"
if isRandom {
result = "Pass"
*pass += 1
} else {
*fail += 1
}
t.AppendRow([]interface{}{testName, fmt.Sprintf("%.2f", pValue), result})
}