Skip to content
Permalink
Browse files

Python 2.7+ and 3.0+ compatibility added

  • Loading branch information...
dj-on-github committed Jun 17, 2018
1 parent fbf00e2 commit 3181ac54eff5e55c81d5d3e1de177df2ab82dbdc
@@ -19,6 +19,8 @@
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import copy

MATRIX_FORWARD_ELIMINATION = 0
@@ -29,7 +31,7 @@ def print_matrix(matrix):
#print "len matrix = ",str(len(matrix))
#for line in matrix:
# print line
for i in xrange(len(matrix)):
for i in range(len(matrix)):
#print "Line %d" % i
line = matrix[i]
#print "Line %d = %s" % (i,str(line))
@@ -43,7 +45,7 @@ def print_matrix(matrix):
astr += "]"
else:
astr = astr + "\n"
print astr
print(astr)
#print "END PRINT MATRIX"


@@ -52,10 +54,10 @@ def row_echelon(M,Q,matrix,blknum):

pivotstartrow = 0
pivotstartcol = 0
for i in xrange(Q):
for i in range(Q):
# find pivotrow
found = False
for k in xrange(pivotstartrow,Q):
for k in range(pivotstartrow,Q):
if lm[k][pivotstartcol] == 1:
found = True
pivotrow = k
@@ -67,7 +69,7 @@ def row_echelon(M,Q,matrix,blknum):
lm[pivotrow],lm[pivotstartrow] = lm[pivotstartrow],lm[pivotrow]

# eliminate lower triangle column
for j in xrange(pivotstartrow+1,Q):
for j in range(pivotstartrow+1,Q):
if lm[j][pivotstartcol]==1:
lm[j] = [x ^ y for x,y in zip(lm[pivotstartrow],lm[j])]

@@ -81,7 +83,7 @@ def row_echelon(M,Q,matrix,blknum):
def rank(M,Q,matrix,blknum):
lm = row_echelon(M,Q,matrix,blknum)
rank = 0
for i in xrange(Q):
for i in range(Q):
nonzero = False
for bit in lm[i]:
if bit == 1:
@@ -95,7 +97,7 @@ def computeRank(M, Q, matrix):

localmatrix = copy.deepcopy(matrix)
# FORWARD APPLICATION OF ELEMENTARY ROW OPERATIONS
for i in xrange(m-1):
for i in range(m-1):
if ( localmatrix[i][i] == 1 ):
localmatrix = perform_elementary_row_operations(MATRIX_FORWARD_ELIMINATION, i, M, Q, localmatrix)
else: # localmatrix[i][i] = 0
@@ -106,7 +108,7 @@ def computeRank(M, Q, matrix):


# BACKWARD APPLICATION OF ELEMENTARY ROW OPERATIONS
for i in xrange(m-1,0,-1):
for i in range(m-1,0,-1):
#for ( i=m-1; i>0; i-- ) {
if ( localmatrix[i][i] == 1 ):
localmatrix = perform_elementary_row_operations(MATRIX_BACKWARD_ELIMINATION, i, M, Q, localmatrix)
@@ -128,17 +130,17 @@ def perform_elementary_row_operations(flag, i, M, Q, A):
k = 0

if ( flag == MATRIX_FORWARD_ELIMINATION ):
for j in xrange(i+1,M):
for j in range(i+1,M):
#for ( j=i+1; j<M; j++ )
if ( A[j][i] == 1 ):
for k in xrange(i,Q):
for k in range(i,Q):
#for ( k=i; k<Q; k++ )
A[j][k] = (A[j][k] + A[i][k]) % 2
else:
#for ( j=i-1; j>=0; j-- )
for j in xrange(i-1,-1,-1):
for j in range(i-1,-1,-1):
if ( A[j][i] == 1 ):
for k in xrange(Q):
for k in range(Q):
#for ( k=0; k<Q; k++ )
A[j][k] = (A[j][k] + A[i][k]) % 2

@@ -181,10 +183,10 @@ def determine_rank(m, M, Q, A):
# DETERMINE RANK, THAT IS, COUNT THE NUMBER OF NONZERO ROWS

rank = m
for i in xrange(M):
for i in range(M):
#for ( i=0; i<M; i++ ) {
allZeroes = 1
for j in xrange(Q):
for j in range(Q):
#for ( j=0; j<Q; j++) {
if ( A[i][j] == 1 ):
allZeroes = 0
@@ -195,15 +197,15 @@ def determine_rank(m, M, Q, A):

def create_matrix(M, Q):
matrix = list()
for rownum in xrange(Q):
row = [0 for x in xrange(M)]
for rownum in range(Q):
row = [0 for x in range(M)]
matrix.append(row)

return matrix

def matrix_from_bits(M,Q,bits,blknum):
m = list()
for rownum in xrange(Q):
for rownum in range(Q):
row = bits[rownum*M:(rownum+1)*M]
m.append(row)
return m[:]
@@ -20,14 +20,15 @@
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import math
#from scipy.special import gamma, gammainc, gammaincc
from gamma_functions import *

def bits_to_int(bits):
theint = 0
for i in xrange(len(bits)):
for i in range(len(bits)):
theint = (theint << 1) + bits[i]
return theint

@@ -40,48 +41,48 @@ def approximate_entropy_test(bits):
if m >3 :
m = 3

print " n = ",n
print " m = ",m
print(" n = ",n)
print(" m = ",m)

Cmi = list()
phi_m = list()
for iterm in xrange(m,m+2):
for iterm in range(m,m+2):
# Step 1
padded_bits=bits+bits[0:iterm-1]

# Step 2
counts = list()
for i in xrange(2**iterm):
for i in range(2**iterm):
#print " Pattern #%d of %d" % (i+1,2**iterm)
count = 0
for j in xrange(n):
for j in range(n):
if bits_to_int(padded_bits[j:j+iterm]) == i:
count += 1
counts.append(count)
print " Pattern %d of %d, count = %d" % (i+1,2**iterm, count)
print(" Pattern %d of %d, count = %d" % (i+1,2**iterm, count))

# step 3
Ci = list()
for i in xrange(2**iterm):
for i in range(2**iterm):
Ci.append(float(counts[i])/float(n))

Cmi.append(Ci)

# Step 4
sum = 0.0
for i in xrange(2**iterm):
if (C[i] > 0.0):
for i in range(2**iterm):
if (Ci[i] > 0.0):
sum += Ci[i]*math.log((Ci[i]/10.0))
phi_m.append(sum)
print " phi(%d) = %f" % (m,sum)
print(" phi(%d) = %f" % (m,sum))

# Step 5 - let the loop steps 1-4 complete

# Step 6
appen_m = phi_m[0] - phi_m[1]
print " AppEn(%d) = %f" % (m,appen_m)
print(" AppEn(%d) = %f" % (m,appen_m))
chisq = 2*n*(math.log(2) - appen_m)
print " ChiSquare = ",chisq
print(" ChiSquare = ",chisq)
# Step 7
p = gammaincc(2**(m-1),(chisq/2.0))

@@ -98,6 +99,6 @@ def approximate_entropy_test(bits):
1,0,0,0]
success, p, _ = approximate_entropy_test(bits)

print "success =",success
print "p = ",p
print("success =",success)
print("p = ",p)

@@ -20,6 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import math
import copy
@@ -28,19 +29,19 @@
def binary_matrix_rank_test(bits,M=32,Q=32):
n = len(bits)
N = int(math.floor(n/(M*Q))) #Number of blocks
print " Number of blocks %d" % N
print " Data bits used: %d" % (N*M*Q)
print " Data bits discarded: %d" % (n-(N*M*Q))
print(" Number of blocks %d" % N)
print(" Data bits used: %d" % (N*M*Q))
print(" Data bits discarded: %d" % (n-(N*M*Q)))

if N < 38:
print " Number of blocks must be greater than 37"
print(" Number of blocks must be greater than 37")
p = 0.0
return False,p,None

# Compute the reference probabilities for FM, FMM and remainder
r = M
product = 1.0
for i in xrange(r):
for i in range(r):
upper1 = (1.0 - (2.0**(i-Q)))
upper2 = (1.0 - (2.0**(i-M)))
lower = 1-(2.0**(i-r))
@@ -49,7 +50,7 @@ def binary_matrix_rank_test(bits,M=32,Q=32):

r = M-1
product = 1.0
for i in xrange(r):
for i in range(r):
upper1 = (1.0 - (2.0**(i-Q)))
upper2 = (1.0 - (2.0**(i-M)))
lower = 1-(2.0**(i-r))
@@ -61,7 +62,7 @@ def binary_matrix_rank_test(bits,M=32,Q=32):
FM = 0 # Number of full rank matrices
FMM = 0 # Number of rank -1 matrices
remainder = 0
for blknum in xrange(N):
for blknum in range(N):
block = bits[blknum*(M*Q):(blknum+1)*(M*Q)]
# Put in a matrix
matrix = gf2matrix.matrix_from_bits(M,Q,block,blknum)
@@ -81,9 +82,9 @@ def binary_matrix_rank_test(bits,M=32,Q=32):
p = math.e **(-chisq/2.0)
success = (p >= 0.01)

print " Full Rank Count = ",FM
print " Full Rank -1 Count = ",FMM
print " Remainder Count = ",remainder
print " Chi-Square = ",chisq
print(" Full Rank Count = ",FM)
print(" Full Rank -1 Count = ",FMM)
print(" Remainder Count = ",remainder)
print(" Chi-Square = ",chisq)

return (success, p, None)
@@ -20,6 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import math
#from scipy.special import gamma, gammainc, gammaincc
@@ -33,7 +34,7 @@ def p_value(n,z):
sum_a = 0.0
startk = int(math.floor((((float(-n)/z)+1.0)/4.0)))
endk = int(math.floor((((float(n)/z)-1.0)/4.0)))
for k in xrange(startk,endk+1):
for k in range(startk,endk+1):
c = (((4.0*k)+1.0)*z)/math.sqrt(n)
#d = scipy.stats.norm.cdf(c)
d = normcdf(c)
@@ -45,7 +46,7 @@ def p_value(n,z):
sum_b = 0.0
startk = int(math.floor((((float(-n)/z)-3.0)/4.0)))
endk = int(math.floor((((float(n)/z)-1.0)/4.0)))
for k in xrange(startk,endk+1):
for k in range(startk,endk+1):
c = (((4.0*k)+3.0)*z)/math.sqrt(n)
#d = scipy.stats.norm.cdf(c)
d = normcdf(c)
@@ -88,9 +89,9 @@ def cumulative_sums_test(bits):
plist = [p_forward, p_backward]

if success:
print "PASS"
print("PASS")
else:
print "FAIL: Data not random"
print("FAIL: Data not random")
return (success, None, plist)

if __name__ == "__main__":
@@ -101,6 +102,6 @@ def cumulative_sums_test(bits):
0,1,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1,0,0,0]
success, _, plist = cumulative_sums_test(bits)

print "success =",success
print "plist = ",plist
print("success =",success)
print("plist = ",plist)

@@ -1,5 +1,3 @@
import math
import numpy

# sp800_22_dft_test.py
#
@@ -21,6 +19,11 @@
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import math
import numpy
import sys

def dft_test(bits):
n = len(bits)
@@ -34,17 +37,20 @@ def dft_test(bits):
ts_np = numpy.array(ts)
fs = numpy.fft.fft(ts_np) # Compute DFT

mags = abs(fs)[:n/2] # Compute magnitudes of first half of sequence

if sys.version_info > (3,0):
mags = abs(fs)[:n//2] # Compute magnitudes of first half of sequence
else:
mags = abs(fs)[:n/2] # Compute magnitudes of first half of sequence

T = math.sqrt(math.log(1.0/0.05)*n) # Compute upper threshold
N0 = 0.95*n/2.0
print " N0 = %f" % N0
print(" N0 = %f" % N0)

N1 = 0.0 # Count the peaks above the upper theshold
for mag in mags:
if mag < T:
N1 += 1.0
print " N1 = %f" % N1
print(" N1 = %f" % N1)
d = (N1 - N0)/math.sqrt((n*0.95*0.05)/4) # Compute the P value
p = math.erfc(abs(d)/math.sqrt(2))

Oops, something went wrong.

0 comments on commit 3181ac5

Please sign in to comment.
You can’t perform that action at this time.