-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
369 lines (313 loc) · 10.1 KB
/
tests.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import numpy as np
from scipy.special import gammainc
from scipy.fft import *
import matplotlib.pyplot as plt
from math import *
from nist_tests import testsBatteryNIST
with open("timer_random_numbers_rotxor.csv","r") as f:
data = f.read()
byteStrings = data.rstrip("\n").split("\n")
# Now numbers should have the random numbers in 8-bit strings
numbers = [int(x, 2) for x in byteStrings]
# print(byteStrings[:5])
# print(numbers[:5])
quantityBytes = len(byteStrings)
elementSize = len(byteStrings[0])
numBits = quantityBytes*elementSize
bits = []
for byte in byteStrings:
for x in byte:
bits.append(int(x))
bitsString = ''.join(byteStrings)
def vonNeumannAlgorithm(bitsArray):
length = len(bitsArray)
newArray = []
for i in range(length//2):
if (bitsArray[2*i] ^ bitsArray[2*i+1]):
newArray.append(bitsArray[2*i])
return newArray
def makeByteStringArray(bitsArray):
newArray = []
for i in range(len(bitsArray)//8):
temp = ""
for j in range(8):
temp += str(bitsArray[8*i+j])
newArray.append(temp)
return newArray
def sanityChecks(bitsArray, intArray):
numBits = len(bitsArray)
numInts = len(intArray)
propOnes = sum(bitsArray)/numBits
print("Number of bits :",numBits)
print("Proportion of ones :",propOnes, "Expected proportion :",0.5,"±",2/sqrt(numBits))
mean = sum(intArray)/numInts
print("Integers' Mean :", mean,"Standard deviation :",np.std(intArray), "Expected STD :", 255/sqrt(12))
entropyPerBit = -propOnes*log2(propOnes) - (1-propOnes)*log2(1-propOnes)
minEntropyPerByte = -log2(max(np.histogram(intArray, bins=256)[0])/numInts)
print("Shannon Entropy per bit :",entropyPerBit, "bits, Min-Entropy per byte :", minEntropyPerByte,"bits")
# Frequency monobit test
def freqMonobitTest(bitsArray):
length = len(bitsArray)
s = abs(2*bitsArray.count(1) - length)/np.sqrt(length) # The test statistic
# erfc is the Complementary Error Function, imported from math
p = erfc(s/np.sqrt(2)) # p-value
print("Frequency monobit test : ", end="")
if p>0.01:
print("passed")
else:
print("failed")
print("s = ",s," p-value = ",p)
def freqBlockTest(bitsArray, numBlocks):
length = len(bitsArray)
blockLength = length//numBlocks
bitBlocks = [bitsArray[blockLength*i : blockLength*(i+1)] for i in range(numBlocks) ]
propOnes = np.array([sum(x)/blockLength for x in bitBlocks])
chiSquared = 4*blockLength*(sum( (propOnes - 1/2)**2 ))
p = 1 - gammainc(numBlocks/2, chiSquared/2)
print("Frequency block test : ", end="")
if p>0.01:
print("passed")
else:
print("failed")
print("Chi Squared = ",chiSquared," p-value = ",p)
# Calculating Pi using random numbers and seeing accuracy
def valueOfPiTest(intArray, makePlot = True, label = ""):
length = len(intArray)
numCords = length//2
xCords = intArray[:numCords]
yCords = intArray[numCords: 2*numCords]
pointsInCircle = 0
for i in range(numCords):
if (xCords[i]**2 + yCords[i]**2 <= 255**2):
pointsInCircle += 1
piValue = 4*(pointsInCircle/numCords)
print("Calculated value of Pi : ", piValue)
print("Actual Pi : ", pi)
if makePlot:
plt.figure()
fig, ax = plt.subplots()
ax.scatter(xCords, yCords, s=0.5)
# fig = plt.gcf()
# ax = fig.gca()
arc = plt.Circle((0,0), 255, fill = False)
ax.add_patch(arc)
ax.set_xlim(0,255)
ax.set_ylim(0,255)
if label:
ax.set_title("Random points in cartesian space to estimate Pi, " + label)
else:
ax.set_title("Random points in cartesian space to estimate Pi")
# Runs Test
def runsTest(bitsArray):
length = len(bitsArray)
propOnes = sum(bitsArray)/length
prereq = bool(abs(propOnes-0.5) < 2/np.sqrt(length))
v = 1 #initializing the test statistic
if prereq:
for i in range(length-1):
if bitsArray[i] != bitsArray[i+1]:
v += 1
p = erfc((abs(v - 2*length*propOnes*(1-propOnes)))/(2*np.sqrt(2*length)*propOnes*(1-propOnes))) #p-value
# erfc is the Complementary Error Function, imported from math
print("Runs Test : ", end="")
if p>0.01:
print("passed")
else:
print("failed")
print("v = ",v," distance from expected v (scaled) = ",(abs(v - 2*length*propOnes*(1-propOnes)))/(2*np.sqrt(2*length)*propOnes*(1-propOnes))," p-value = ",p)
else:
print("Frequency test prerequisite was failed, so Runs test not run")
def longestRunsBlockTest(bitsArray):
length = len(bitsArray)
if length>=128:
if length>=6272:
if length>=75*10**4:
blockLength = 10**4
else:
blockLength = 128
else:
blockLength = 8
else:
print("Insufficient number of bits for Runs Block Test")
return
numBlocks = length//blockLength
bitBlocks = [bitsArray[blockLength*i : blockLength*(i+1)] for i in range(numBlocks) ]
propOnes = np.zeros(numBlocks)
freqArray = np.zeros(7)
for i in range(numBlocks):
bitBlock = bitBlocks[i]
propOnes[i] = sum(bitBlock)/blockLength
index = longestRunIndex(bitBlock, blockLength)
if index == -1:
print("Something went wrong, longestRunIndex returned -1")
return
freqArray[index] += 1
probArray = []
if blockLength == 8:
probArray = [0.2148, 0.3672, 0.2305, 0.1875]
elif blockLength == 128:
probArray = [0.1174, 0.2430, 0.2493, 0.1752, 0.1027, 0.1124]
elif blockLength == 10**4:
probArray = [0.0882, 0.2092, 0.2483, 0.1933, 0.1208, 0.0675, 0.0727]
chiSquared = 0
K = len(probArray)-1
N_values = {3 : 16, 5 : 49, 6 : 75}
N = N_values[K]
for i in range(K+1):
chiSquared += (freqArray[i] - N*probArray[i])**2/(N*probArray[i])
p = 1 - gammainc(K/2, chiSquared/2)
print("Longest Runs Block test : ", end="")
if p>0.01:
print("passed")
else:
print("failed")
print("Chi Squared = ", chiSquared, " p-value = ", p)
print(blockLength, K, probArray, freqArray)
def longestRunIndex(bitsArray, blockLength):
lengthOfCurrentRun = 0
lengthOfLongestRun = 0
for i in range(blockLength):
if bitsArray[i]==1:
lengthOfCurrentRun += 1
else:
if lengthOfCurrentRun>lengthOfLongestRun:
lengthOfLongestRun = lengthOfCurrentRun
lengthOfCurrentRun = 0
if lengthOfCurrentRun>lengthOfLongestRun:
lengthOfLongestRun = lengthOfCurrentRun
if blockLength == 8:
if 0<lengthOfLongestRun<4:
return lengthOfLongestRun-1
elif lengthOfLongestRun >= 4:
return 3
elif blockLength == 128:
if 0<lengthOfLongestRun<=4:
return 0
elif 4<lengthOfLongestRun<9:
return lengthOfLongestRun - 4
elif lengthOfLongestRun >= 9:
return 5
elif blockLength == 10**4:
if 0<lengthOfLongestRun<=10:
return 0
elif 10<lengthOfLongestRun<16:
return lengthOfLongestRun - 10
elif lengthOfLongestRun >= 16:
return 6
else:
print("Invalid block length entered.")
return -1
# Unecessary function
def runsInBlock(bitsArray, blockLength): # Returns the tabulated frequencies
freqArray = np.zeros(7)
lengthOfCurrentRun = 0
bitsArray.append(0)
if blockLength == 8:
for i in range(blockLength+1):
if bitsArray[i]==1:
lengthOfCurrentRun += 1
elif 0<lengthOfCurrentRun<4:
freqArray[lengthOfCurrentRun-1] += 1
elif lengthOfCurrentRun >= 4:
freqArray[3] += 1
if blockLength == 128:
for i in range(blockLength+1):
if bitsArray[i]==1:
lengthOfCurrentRun += 1
elif 0<lengthOfCurrentRun<=4:
freqArray[0] += 1
elif 4<lengthOfCurrentRun<9:
freqArray[lengthOfCurrentRun-4] += 1
elif lengthOfCurrentRun >= 9:
freqArray[5] += 1
if blockLength == 10**4:
for i in range(blockLength+1):
if bitsArray[i]==1:
lengthOfCurrentRun += 1
elif 0<lengthOfCurrentRun<=10:
freqArray[0] += 1
elif 10<lengthOfCurrentRun<16:
freqArray[lengthOfCurrentRun-10] += 1
elif lengthOfCurrentRun >= 16:
freqArray[6] += 1
return freqArray
def matrixRankTest(bitsArray):
pass
def fourierTransformTest(bitsArray):
length = len(bitsArray)
M = abs(fft(2*np.array(bitsArray) - 1)[:length//2])
T = sqrt(length*log(1/0.05))
ExpectedNumPeaks = 0.95*length/2
ActualNumPeaks = np.count_nonzero(M<T)
d = 2*(ActualNumPeaks - ExpectedNumPeaks)/sqrt(length*0.95*0.05)
p = erfc(abs(d)/sqrt(2))
print("Fourier Transform test : ", end="")
if p>0.01:
print("passed")
else:
print("failed")
print("d = ",d," p-value = ",p)
# All the tests in one function to allow importing them together.
def testsBattery(bitsArray, intArray, makePlots = True, label="", bitmapSize = 128):
sanityChecks(bitsArray, intArray)
freqMonobitTest(bitsArray)
freqBlockTest(bitsArray, 128)
runsTest(bitsArray)
# longestRunsBlockTest(bitsArray) # Not workin yet
fourierTransformTest(bitsArray)
valueOfPiTest(intArray, makePlots, label)
# matrixRankTest(bitsArray, 8) Yet to be encoded
if makePlots:
if len(bitsArray)>=(bitmapSize**2):
plt.figure()
plt.imshow(np.reshape(bitsArray[:bitmapSize**2], (bitmapSize,bitmapSize)))
if label:
plt.title("Bitmap, " + label)
else:
plt.title("Bitmap")
else:
print("Not enough numbers for Bitmap - decrease bitmap size")
plt.figure()
plt.plot([*[0],*abs(rfft(bitsArray, norm = "ortho")[1:])])
# Get rid of the 0th term because it is essentially the sum of the array
# And so is very high, but does not represent a frequency
# Note that these frequencies are not in Hz
if label:
plt.title("Discrete Fourier Transform of the bitstream, " + label)
else:
plt.title("Discrete Fourier Transform of the bitstream")
plt.figure()
plt.plot([*[0],*abs(rfft(intArray, norm = "ortho")[1:])])
if label:
plt.title("Discrete Fourier Transform of the 8-bit integers, " + label)
else:
plt.title("Discrete Fourier Transform of the 8-bit integers")
plt.figure()
plt.plot(numbers[:])
plt.grid(True)
if label:
plt.title("Time-series of 8-bit integers, " + label)
else:
plt.title("Time-series of 8-bit integers")
plt.figure()
plt.hist(intArray, bins = 256)
if label:
plt.title("Frequency histogram of the 8-bit integers, " + label)
else:
plt.title("Frequency histogram of the 8-bit integers")
# Running Tests
if __name__ == "__main__":
makePlots = True
applyVN = False
if applyVN:
vnBits = vonNeumannAlgorithm(bits)
vnByteStrings = makeByteStringArray(vnBits)
vnNumbers = [int(x, 2) for x in vnByteStrings]
print("Before Von Neumann : ")
testsBattery(bits, numbers, makePlots, "Before Von Neumann")
print("\nAfter Von Neumann : ")
testsBattery(vnBits, vnNumbers, makePlots, "After Von Neumann")
else:
testsBattery(bits, numbers, makePlots)
testsBatteryNIST(bitsString)
plt.show()