Giter Club home page Giter Club logo

sp800_22_tests's Introduction

sp800_22_tests

A python implementation of the SP800-22 Rev 1a PRNG test suite.

The NIST STS-2.1.2 implementation of the SP800-22 Rev1a tests has some problems. It tends to crash a lot and give the wrong result.

This implementation provides a separate python file, one for each test and a program to read a binary data file and send it to each of the tests. The summary results are output at the end.

In the example below a 1 Mibibit uniform random binary file is generated with djenrandom (https://github.com/dj-on-github/djenrandom) and run through the test.

$ djenrandom -b -k 128 > megabitrand.bin
$ ./sp800_22_tests.py megabitrand.bin
Tests of Distinguishability from Random
TEST: monobit_test
  Ones count   = 523619
  Zeroes count = 524957
  PASS
  P=0.191334792562

[ Lots of per test output ]

SUMMARY
-------
monobit_test                             0.191334792562   PASS
frequency_within_block_test              0.866040491238   PASS
runs_test                                0.45073315513    PASS
longest_run_ones_in_a_block_test         0.464001170438   PASS
binary_matrix_rank_test                  0.405449005115   PASS
dft_test                                 0.852965102607   PASS
non_overlapping_template_matching_test   1.0              PASS
overlapping_template_matching_test       0.91976483935    PASS
maurers_universal_test                   0.998884573989   PASS
linear_complexity_test                   0.0406698787073  PASS
serial_test                              0.895646559993   PASS
approximate_entropy_test                 0.895690030562   PASS
cumulative_sums_test                     0.18603533058    PASS
random_excursion_test                    0.151908516968   PASS
random_excursion_variant_test            0.019160041631   PASS

Next we create a biased data file, where the probability of a 1 is 40% and a 0 is 60% and run it through the test.

$ djenrandom -b -k 128 -m biased --bias=0.4 > biased_megabitrand.bin
$ ./sp800_22_tests.py biased_megabitrand.bin
Tests of Distinguishability from Random
TEST: monobit_test
  Ones count   = 420122
  Zeroes count = 628454
  FAIL

[ Lots of per test output ]

SUMMARY
-------
monobit_test                             0.0                FAIL
frequency_within_block_test              0.0                FAIL
runs_test                                0.0                FAIL
longest_run_ones_in_a_block_test         1.81518103863e-81  FAIL
binary_matrix_rank_test                  0.22156738316      PASS
dft_test                                 3.56787241447e-17  FAIL
non_overlapping_template_matching_test   0.759928185368     PASS
overlapping_template_matching_test       5.31335556946e-91  FAIL
maurers_universal_test                   0.910613044038     PASS
linear_complexity_test                   0.317575269575     PASS
serial_test                              0.0                FAIL
approximate_entropy_test                 0.0                FAIL
cumulative_sums_test                     0.0                FAIL
random_excursion_test                    9.15178471457e-06  FAIL
random_excursion_variant_test            0.121267812518     PASS

Next we create some serially correlated data and run it through the tests.

$ djenrandom -b -k 128 -m correlated --correlation=-0.2 > correlated_megrandom.bin
$ ./sp800_22_tests.py correlated_megabitrand.bin
Tests of Distinguishability from Random
TEST: monobit_test
  Ones count   = 524242
  Zeroes count = 524334
  PASS
  P=0.928411381275

[ Lots of per test output ]

SUMMARY
-------
monobit_test                             0.928411381275     PASS
frequency_within_block_test              0.99598493834      PASS
runs_test                                0.0                FAIL
longest_run_ones_in_a_block_test         3.45886379519e-81  FAIL
binary_matrix_rank_test                  0.379259346315     PASS
dft_test                                 8.67506291511e-16  FAIL
non_overlapping_template_matching_test   0.999999863695     PASS
overlapping_template_matching_test       1.1953875512e-90   FAIL
maurers_universal_test                   0.923207164612     PASS
linear_complexity_test                   0.47696777353      PASS
serial_test                              0.0                FAIL
approximate_entropy_test                 0.0                FAIL
cumulative_sums_test                     0.853890590584     PASS
random_excursion_test                    7.63411305092e-09  FAIL
random_excursion_variant_test            0.0662033926665    PASS

Next we create some data with both bias and serial correlation from an Ornstein–Uhlenbeck process and run it through the tool

$ djenrandom -b -k 128 -m sums -l 0.2 -r 0.3 > sums_megrandom.bin
$ ./sp800_22_tests.py sums_megrandom.bin
Tests of Distinguishability from Random
TEST: monobit_test
  Ones count   = 419429
  Zeroes count = 629147
  FAIL
  P=0.0

[ Lots of per test output ]

SUMMARY
-------
monobit_test                             0.0                FAIL
frequency_within_block_test              0.0                FAIL
runs_test                                0.0                FAIL
longest_run_ones_in_a_block_test         2.32811341972e-159 FAIL
binary_matrix_rank_test                  0.702984863193     PASS
dft_test                                 7.91957168353e-06  FAIL
non_overlapping_template_matching_test   0.949847572332     PASS
overlapping_template_matching_test       1.16046343793e-137 FAIL
maurers_universal_test                   0.90191137295      PASS
linear_complexity_test                   0.16038668337      PASS
serial_test                              0.0                FAIL
approximate_entropy_test                 0.0                FAIL
cumulative_sums_test                     0.0                FAIL
random_excursion_test                    2.20517667303e-09  FAIL
random_excursion_variant_test            0.0                FAIL

sp800_22_tests's People

Contributors

dj-on-github avatar greendow avatar intelentropyreport avatar recvfrom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sp800_22_tests's Issues

Typo in Monobit test ?

success = (p >= 0.1) return (success,p,None)

Monobit test checks against 0.1. It should be 0.01 right ?

Another failure I see is if you pass same values as test. For example
bits = np.random.randint(1, size=1000)

Traceback (most recent call last):
File "randomness_check.py", line 103, in
success,p,plist = func(bits)
File "/Users/karthik/sp800_22_tests/sp800_22_approximate_entropy_test.py", line 73, in approximate_entropy_test
sum += Ci[i]*math.log((Ci[i]/10.0))
ValueError: math domain error

random bit generator and random number generator

I have question about random bit and random number :
I know random bit is necessary for random number, but I dont know if random number is ok random bit is ok? I want to test random bit generator in Linux.

longest_run_ones_in_a_block_test fails with small dataset

bits = np.random.randint(2, size=120)

For small dataset, longest_run_ones_in_a_block_test doesn't fail correctly and causes run to stop

`def longest_run_ones_in_a_block_test(bits):
n = len(bits)

if n < 128:
    #return 0 [Current]
    return (None,None,None)`

any way to run with python2.6?!

Hi,
any way to run with python2.6?! I use Python 2.6.6 on centos 6

./sp800_22_tests.py /tmp/megabitrand.bin
Tests of Distinguishability from Random
TEST: monobit_test
Ones count = 523749
Zeroes count = 524827
Traceback (most recent call last):
File "./sp800_22_tests.py", line 132, in
(success,p,plist) = func(bits)
File "/root/git/sp800_22_tests/sp800_22_monobit_test.py", line 44, in monobit_test
p = math.erfc(float(s)/(math.sqrt(float(n)) * math.sqrt(2.0)))
AttributeError: 'module' object has no attribute 'erfc'

Cannot generate random numbers

Hi,

I tried to do the NIST randomnes test and have downloaded the source file for both sp test and djenrandom, and compiled it using the makefile. When I type the command:
./djenrandom -b -k 128 > megabitrand.bin

The system outputs an error:

Failed to open YAML output file for writing
: No such file or directory

I am not sure if there any thing worng. Could you give me any advice? Thanks

number of bits

Hi! Is the code specifically defined for 8-bit format, it shouldn't work for less bits?

About Overlapping Template Matching Test

in sp800_22_overlapping_template_matching_test.py
line 52 : M = 1062
line 54 : print("Insufficient data. %d bit provided. 1,028,016 bits required" % len(bits))
but in NIST SP 800-22 Revision 1a, 2.8.2 Function Call :
M The length in bits of a substring of ε to be tested. M has been set to 1032 in the test code.

So I think it should be :
line 52 : M = 1032
line 54 : print("Insufficient data. %d bit provided. 998,976 bits required" % len(bits))

About DFT TEST

Hi dj, Thank you for converting sp800-22 to python code。
I also implement DFT test using python ,and get the same p_value as yours. But I am confused is why the test results of bits "1001010011" inside the sp800-22ra1 document is 0.029523, our implement get 0.818545808382.

Thanks.

About sp800_22_monobit_test

hi

Thank you for converting sp800-22 to python code.

I am using this program very usefully.

The p-value criterion of sp800_22_monobit_test.py is set to 0.1.

Is not 0.01 correct?

about binary_matrix_rank_test

SUMMARY

monobit_test 0.581114294834 PASS
frequency_within_block_test 0.747793618708 PASS
runs_test 0.842033492763 PASS
longest_run_ones_in_a_block_test 0.863705090529 PASS
binary_matrix_rank_test 0.0359437277529 FAIL

in the source code:

p = math.e **(-chisq/2.0)
success = (p >= 0.1)

why 0.1,i think maybe 0.01?

Memory Error

Hey,
I am running in to a problem when trying to execute the randomness tests.
My Setup: 16GB RAM; Fedora 29; random-bits-file is about 100MB in size; swap-partion: 8GB; Python 2.7.15 installed; Python 3.7.2 installed
Reproducing steps in Fedora

  • Start the test with pyhton3 sp800_22_tests.py random-bit-file
    Test runs with about 40% RAM allocated. When coming to the DFT-test the allocation increases to the maximum. See sp800-22_memorry_error.txt and sp800-22_memorry_error2.txt for details.

  • Note that calling the tests with the "old" python 2.7 results in a simple Killed message during the runs_test: sp800-22_killed.txt

fails on 1 mb of zeros

I've created 1 megabyte of zeros as dd if=/dev/zero of=zeros.bin bs=1024 count=1024

and when run sp800_22_tests.py on this file it fails like that:

Traceback (most recent call last):
  File "sp800_22_tests.py", line 139, in <module>
    (success,p,plist) = func(bits)
  File "/Users/catap/src/sp800_22_tests/sp800_22_frequency_within_block_test.py", line 74, in frequency_within_block_test
    p = gammaincc((num_of_blocks/2.0),float(chisq)/2.0)
  File "/Users/catap/src/sp800_22_tests/gamma_functions.py", line 67, in gammaincc
    return upper_incomplete_gamma(a,x)/gamma(a)
  File "/Users/catap/src/sp800_22_tests/gamma_functions.py", line 16, in upper_incomplete_gamma
    result = ((x**a) * (e**(-x)))/upper_incomplete_gamma(a,x,d=d+1)
OverflowError: (34, 'Result too large')

Why a bad random number collection sample can pass the test?

Hi ! Thank you for sharing your programs on GitHub ! It's a useful tool.
In the first, I suggest that sp800_22_tests programs be indicated to use in Python 2.7 in a prominent position of the web page on GitHub. They can't be run in Python 2.6. After a slight transform, they can be run in Python 3. The steps of transform are:
Transform 'print' to the function form, that is, 'print( )' in all python files ;
Transform 'xrange' to 'range' in all python files;
In file 'sp800_22_dft_test.py', Line 37: mags = abs(fs)[:n/2] # Compute magnitudes of first half of sequence
Transform 'n/2' to 'n//2', that is, mags = abs(fs)[:n//2]
In file 'sp800_22_tests.py', Line 36: byte = ord(bytech)
Transform 'ord(bytech)' to 'bytech', that is, byte = bytech
After finishing the above modification, I can run the programs successfully in Python 3.6.5.
Next, NIST provides a tool on web site: https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/sts-2_1_2.zip
In the zip file there are some random number samples. Especially, in \sts-2.1.2\data there are three binary data sample files: BBS.dat, data_bad_rng, data.sha1. I tested the three files by python programs. MemoryError was reported while testing BBS.data file, maybe the file size is too big. The file data.sha1 can pass the test. To my surprise, the file data.bad_rng can pass the test. In my opinion, data.bad_rng is a negative example to demonstrate bad randomness. I wonder why such a bad random number collection sample given by NIST can pass the test. Thanks a lot !

file is opened but not closed ?

In the file 'sp800_22_tests.py' :

def read_bits_from_file(filename,bigendian):
bitlist = list()
if filename == None:
f = sys.stdin
else:
f = open(filename, "rb")

A file seems to be opened, but I cannot find the place where it is closed. Is this a small bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.