a statistical test suite for random and pseudorandom ... · a statistical test suite for random and...

131
Special Publication 800-22 Revision 1a A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications Andrew Rukhin, Juan Soto, James Nechvatal, Miles Smid, Elaine Barker, Stefan Leigh, Mark Levenson, Mark Vangel, David Banks,Alan Heckert, James Dray, San Vo Revised:April 2010 Lawrence E Bassham III

Upload: ngodang

Post on 14-Jul-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

  • Special Publication 800-22 Revision 1a

    A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications

    AndrewRukhin, JuanSoto,JamesNechvatal,Miles Smid,ElaineBarker,Stefan Leigh,MarkLevenson,Mark Vangel,DavidBanks,AlanHeckert, JamesDray,SanVo

    Revised:April 2010 LawrenceEBasshamIII

  • NIST Special Publication 800-22 Revision 1a

    A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications

    Andrew Rukhin1, Juan Soto2, James Nechvatal2, Miles Smid2, Elaine Barker2, Stefan Leigh1, Mark Levenson1, Mark Vangel1, David Banks1, Alan Heckert1, James Dray2 , San Vo2

    Revised: April 2010 Lawrence E Bassham III2

    C O M P U T E R S E C U R I T Y

    1Statistical Engineering Division2Computer Security Division Information Technology Laboratory National Institute of Standards and Technology Gaithersburg, MD 20899-8930

    Revised: April 2010

    U.S. Department of Commerce

    Gary Locke, Secretary

    National Institute of Standards and Technology

    Patrick Gallagher, Director

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    Reports on Computer Systems Technology

    The Information Technology Laboratory (ITL) at the National Institute of Standards and Technology (NIST) promotes the U.S. economy and public welfare by providing technical leadership for the nations measurement and standards infrastructure. ITL develops tests, test methods, reference data, proof of concept implementations, and technical analysis to advance the development and productive use of information technology. ITLs responsibilities include the development of technical, physical, administrative, and management standards and guidelines for the cost-effective security and privacy of sensitive unclassified information in Federal computer systems. This Special Publication 800-series reports on ITLs research, guidance, and outreach efforts in computer security and its collaborative activities with industry, government, and academic organizations.

    National Institute of Standards and Technology Special Publication 800-22 revision 1a Natl. Inst. Stand. Technol. Spec. Publ. 800-22rev1a, 131 pages (April 2010)

    Certain commercial entities, equipment, or materials may be identified in this document in order to describe an experimental procedure or concept adequately.

    Such identification is not intended to imply recommendation or endorsement by the National Institute of Standards and Technology, nor is it intended to imply that the entities, materials, or equipment are necessarily the best available for the purpose.

    ii

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    Table of Contents

    Abstract ....................................................................................................................................... 1

    1. Introduction to Random Number Testing.......................................................................1-1 1.1 General Discussion.................................................................................................. 1-1

    1.1.1 Randomness ................................................................................................1-1 1.1.2 Unpredictability.............................................................................................1-1 1.1.3 Random Number Generators (RNGs)..........................................................1-2 1.1.4 Pseudorandom Number Generators (PRNGs) ............................................1-2 1.1.5 Testing .........................................................................................................1-2 1.1.6 Considerations for Randomness, Unpredictability and Testing ...................1-5

    1.2 Definitions ................................................................................................................ 1-5 1.3 Abbreviations ........................................................................................................... 1-8 1.4 Mathematical Symbols............................................................................................. 1-8

    2. Random Number Generation Tests.................................................................................2-1 2.1 Frequency (Monobit) Test........................................................................................ 2-2

    2.1.1 Test Purpose................................................................................................2-2 2.1.2 Function Call ................................................................................................2-2 2.1.3 Test Statistic and Reference Distribution .....................................................2-2 2.1.4 Test Description ...........................................................................................2-2 2.1.5 Decision Rule (at the 1% Level) ...................................................................2-3 2.1.6 Conclusion and Interpretation of Results .....................................................2-3 2.1.7 Input Size Recommendation ........................................................................2-3 2.1.8 Example .......................................................................................................2-3

    2.2 Frequency Test within a Block ................................................................................. 2-4 2.2.1 Test Purpose................................................................................................2-4 2.2.2 Function Call ................................................................................................2-4 2.2.3 Test Statistic and Reference Distribution .....................................................2-4 2.2.4 Test Description ...........................................................................................2-4 2.2.5 Decision Rule (at the 1% Level) ...................................................................2-5 2.2.6 Conclusion and Interpretation of Results .....................................................2-5 2.2.7 Input Size Recommendation ........................................................................2-5 2.2.8 Example .......................................................................................................2-5

    2.3 Runs Test................................................................................................................. 2-5 2.3.1 Test Purpose................................................................................................2-5 2.3.2 Function Call ................................................................................................2-6 2.3.3 Test Statistic and Reference Distribution .....................................................2-6 2.3.4 Test Description ...........................................................................................2-6 2.3.5 Decision Rule (at the 1% Level) ...................................................................2-7 2.3.6 Conclusion and Interpretation of Results .....................................................2-7 2.3.7 Input Size Recommendation ........................................................................2-7 2.3.8 Example .......................................................................................................2-7

    2.4 Test for the Longest Run of Ones in a Block ........................................................... 2-7 2.4.1 Test Purpose................................................................................................2-7 2.4.2 Function Call ................................................................................................2-8 2.4.3 Test Statistic and Reference Distribution .....................................................2-8 2.4.4 Test Description ...........................................................................................2-8 2.4.5 Decision Rule (at the 1% Level) ...................................................................2-9

    iii

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.4.6 Conclusion and Interpretation of Results .....................................................2-9 2.4.7 Input Size Recommendation ........................................................................2-9 2.4.8 Example .......................................................................................................2-9

    2.5 Binary Matrix Rank Test......................................................................................... 2-10 2.5.1 Test Purpose..............................................................................................2-10 2.5.2 Function Call ..............................................................................................2-10 2.5.3 Test Statistic and Reference Distribution ...................................................2-10 2.5.4 Test Description .........................................................................................2-10 2.5.5 Decision Rule (at the 1% Level) .................................................................2-11 2.5.6 Conclusion and Interpretation of Results ...................................................2-12 2.5.7 Input Size Recommendation ......................................................................2-12 2.5.8 Example .....................................................................................................2-12

    2.6 Discrete Fourier Transform (Spectral) Test ........................................................... 2-12 2.6.1 Test Purpose..............................................................................................2-12 2.6.2 Function Call ..............................................................................................2-12 2.6.3 Test Statistic and Reference Distribution ...................................................2-13 2.6.4 Test Description .........................................................................................2-13 2.6.5 Decision Rule (at the 1% Level) .................................................................2-14 2.6.6 Conclusion and Interpretation of Results ...................................................2-14 2.6.7 Input Size Recommendation ......................................................................2-14 2.6.8 Example .....................................................................................................2-14

    2.7 Non-overlapping Template Matching Test ............................................................. 2-14 2.7.1 Test Purpose..............................................................................................2-14 2.7.2 Function Call ..............................................................................................2-14 2.7.3 Test Statistic and Reference Distribution ...................................................2-15 2.7.4 Test Description .........................................................................................2-15 2.7.5 Decision Rule (at the 1% Level) .................................................................2-16 2.7.6 Conclusion and Interpretation of Results ...................................................2-16 2.7.7 Input Size Recommendation ......................................................................2-16 2.7.8 Example .....................................................................................................2-16

    2.8 Overlapping Template Matching Test .................................................................... 2-17 2.8.1 Test Purpose..............................................................................................2-17 2.8.2 Function Call ..............................................................................................2-17 2.8.3 Test Statistic and Reference Distribution ...................................................2-17 2.8.4 Test Description .........................................................................................2-17 2.8.5 Decision Rule (at the 1% Level) .................................................................2-19 2.8.6 Conclusion and Interpretation of Results ...................................................2-19 2.8.7 Input Size Recommendation ......................................................................2-19 2.8.8 Example .....................................................................................................2-19

    2.9 Maurers Universal Statistical Test ...................................................................... 2-20 2.9.1 Test Purpose..............................................................................................2-20 2.9.2 Function Call ..............................................................................................2-20 2.9.3 Test Statistic and Reference Distribution ...................................................2-20 2.9.4 Test Description .........................................................................................2-20 2.9.5 Decision Rule (at the 1% Level) .................................................................2-23 2.9.6 Conclusion and Interpretation of Results ...................................................2-23 2.9.7 Input Size Recommendation ......................................................................2-23 2.9.8 Example .....................................................................................................2-23

    2.10 Linear Complexity Test .......................................................................................... 2-24 2.10.1 Test Purpose ..............................................................................................2-24 2.10.2 Function Call ..............................................................................................2-24

    iv

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.10.3 Test Statistic and Reference Distribution ...................................................2-24 2.10.4 Test Description .........................................................................................2-24 2.10.5 Decision Rule (at the 1% Level) .................................................................2-26 2.10.6 Conclusion and Interpretation of Results ...................................................2-26 2.10.7 Input Size Recommendation ......................................................................2-26 2.10.8 Example .....................................................................................................2-26

    2.11 Serial Test.............................................................................................................. 2-26 2.11.1 Test Purpose ..............................................................................................2-26 2.11.2 Function Call ..............................................................................................2-26 2.11.3 Test Statistic and Reference Distribution ...................................................2-27 2.11.4 Test Description .........................................................................................2-27 2.11.5 Decision Rule (at the 1% Level) .................................................................2-28 2.11.6 Conclusion and Interpretation of Results ...................................................2-28 2.11.7 Input Size Recommendation ......................................................................2-28 2.11.8 Example .....................................................................................................2-28

    2.12 Approximate Entropy Test ..................................................................................... 2-29 2.12.1 Test Purpose ..............................................................................................2-29 2.12.2 Function Call ..............................................................................................2-29 2.12.3 Test Statistic and Reference Distribution ...................................................2-29 2.12.4 Test Description .........................................................................................2-29 2.12.5 Decision Rule (at the 1% Level) .................................................................2-30 2.12.6 Conclusion and Interpretation of Results ...................................................2-30 2.12.7 Input Size Recommendation ......................................................................2-30 2.12.8 Example .....................................................................................................2-31

    2.13 Cumulative Sums (Cusum) Test ............................................................................ 2-31 2.13.1 Test Purpose ..............................................................................................2-31 2.13.2 Function Call ..............................................................................................2-31 2.13.3 Test Statistic and Reference Distribution ...................................................2-31 2.13.4 Test Description .........................................................................................2-31 2.13.5 Decision Rule (at the 1% Level) .................................................................2-33 2.13.6 Conclusion and Interpretation of Results ...................................................2-33 2.13.7 Input Size Recommendation ......................................................................2-33 2.13.8 Example .....................................................................................................2-33

    2.14 Random Excursions Test....................................................................................... 2-33 2.14.1 Test Purpose ..............................................................................................2-33 2.14.2 Function Call ..............................................................................................2-34 2.14.3 Test Statistic and Reference Distribution ...................................................2-34 2.14.4 Test Description .........................................................................................2-34 2.14.5 Decision Rule (at the 1% Level) .................................................................2-37 2.14.6 Conclusion and Interpretation of Results ...................................................2-37 2.14.7 Input Size Recommendation ......................................................................2-37 2.14.8 Example .....................................................................................................2-37

    2.15 Random Excursions Variant Test .......................................................................... 2-38 2.15.1 Test Purpose ..............................................................................................2-38 2.15.2 Function Call ..............................................................................................2-38 2.15.3 Test Statistic and Reference Distribution ...................................................2-38 2.15.4 Test Description .........................................................................................2-38 2.15.5 Decision Rule (at the 1% Level) .................................................................2-39 2.15.6 Conclusion and Interpretation of Results ...................................................2-39 2.15.7 Input Size Recommendation ......................................................................2-40 2.15.8 Example .....................................................................................................2-40

    v

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    3. Technical Description of Tests........................................................................................3-1 3.1 Frequency (Monobits) Test ...................................................................................... 3-1 3.2 Frequency Test within a Block ................................................................................. 3-1 3.3 Runs Test................................................................................................................. 3-2 3.4 Test for the Longest Run of Ones in a Block ........................................................... 3-3 3.5 Binary Matrix Rank Test........................................................................................... 3-5 3.6 Discrete Fourier Transform (Specral) Test .............................................................. 3-6 3.7 Non-Overlapping Template Matching Test .............................................................. 3-9 3.8 Overlapping Template Matching Test .................................................................... 3-12 3.9 Maurers Universal Statistical Test ...................................................................... 3-13 3.10 Linear Complexity Test .......................................................................................... 3-15 3.11 Serial Test.............................................................................................................. 3-18 3.12 Approximate Entropy Test ..................................................................................... 3-19 3.13 Cumulative Sums (Cusum) Test ............................................................................ 3-21 3.14 Random Excursions Test....................................................................................... 3-22 3.15 Random Excursions Variant Test .......................................................................... 3-24

    4. Testing Strategy and Result Interpretation ....................................................................4-1 4.1 Strategies for the Statistical Analysis of an RNG..................................................... 4-1 4.2 The Interpretation of Empirical Results.................................................................... 4-2

    4.2.1 Proportion of Sequences Passing a Test.....................................................4-2 4.2.2 Uniform Distribution of P-values...................................................................4-3

    4.3 General Recommendations and Guidelines ............................................................ 4-3 4.4 Application of Multiple Tests .................................................................................... 4-6

    5. Users Guide......................................................................................................................5-1 5.1 About the Package................................................................................................... 5-1 5.2 System Requirements.............................................................................................. 5-1 5.3 How to Get Started .................................................................................................. 5-2 5.4 Data Input and Output of Empirical Results............................................................. 5-3

    5.4.1 Data Input.....................................................................................................5-3 5.4.2 Output of Empirical Results..........................................................................5-3 5.4.3 Test Data Files .............................................................................................5-3

    5.5 Program Layout ....................................................................................................... 5-3 5.5.1 General Program..........................................................................................5-3 5.5.2 Global Parameters .......................................................................................5-4 5.5.3 Mathematical Software.................................................................................5-4

    5.6 Running the Test Code ............................................................................................ 5-5 5.7 Interpretation of Results........................................................................................... 5-7

    List of Appendices

    Appendix A Source Code ................................................................................................... A-1 A.1 Hierarchical Directory Structure...............................................................................A-1 A.2 Configuration Information ........................................................................................A-3

    Appendix B Empirical Results for Sample Data............................................................... B-1

    vi

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    Appendix C Extending the Test Suite ............................................................................... C-1 C.1 Incorporating Additional Statistical Tests................................................................ C-1 C.2 Incorporating Additional PRNGs............................................................................. C-2

    Appendix D Description of Reference Pseudorandom Number Generators................. D-1 D.1 Linear Congruential Generator (LCG) .................................................................... D-1 D.2 Quadratic Congruential Generator I (QCG-I) .......................................................... D-1 D.3 Quadratic Congruential Generator II (QCG-II) ........................................................ D-1 D.4 Cubic Congruential Generator II (CCG).................................................................. D-2 D.5 Exclusive OR Generator (XORG) ........................................................................... D-2 D.6 Modular Exponentiation Generator (MODEXPG) ................................................... D-2 D.7 Secure Hash Generator (G-SHA1) ......................................................................... D-3 D.8 Blum-Blum-Shub (BBSG) ....................................................................................... D-3 D.9 Micali-Schnorr Generator (MSG) ............................................................................ D-4 D.10 Test Results ............................................................................................................ D-5

    Appendix E Numerical Algorithm Issues...........................................................................E-1

    Appendix F Supporting Software .......................................................................................F-1 F.1 Rank Computation of Binary Matrices .....................................................................F-1 F.2 Construction of Aperiodic Templates.......................................................................F-4 F.3 Generation of the Binary Expansion of Irrational Numbers .....................................F-7

    Appendix G References...................................................................................................... G-1

    vii

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    Abstract

    This paper discusses some aspects of selecting and testing random and pseudorandom number generators. The outputs of such generators may be used in many cryptographic applications, such as the generation of key material. Generators suitable for use in cryptographic applications may need to meet stronger requirements than for other applications. In particular, their outputs must be unpredictable in the absence of knowledge of the inputs. Some criteria for characterizing and selecting appropriate generators are discussed in this document. The subject of statistical testing and its relation to cryptanalysis is also discussed, and some recommended statistical tests are provided. These tests may be useful as a first step in determining whether or not a generator is suitable for a particular cryptographic application. However, no set of statistical tests can absolutely certify a generator as appropriate for usage in a particular application, i.e., statistical testing cannot serve as a substitute for cryptanalysis. The design and cryptanalysis of generators is outside the scope of this paper.

    Key words: random number generator, hypothesis test, P-value

    Abstract-1

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    1. Introduction to Random Number Testing

    The need for random and pseudorandom numbers arises in many cryptographic applications. For example, common cryptosystems employ keys that must be generated in a random fashion. Many cryptographic protocols also require random or pseudorandom inputs at various points, e.g., for auxiliary quantities used in generating digital signatures, or for generating challenges in authentication protocols.

    This document discusses the randomness testing of random number and pseudorandom number generators that may be used for many purposes including cryptographic, modeling and simulation applications. The focus of this document is on those applications where randomness is required for cryptographic purposes. A set of statistical tests for randomness is described in this document. The National Institute of Standards and Technology (NIST) believes that these procedures are useful in detecting deviations of a binary sequence from randomness. However, a tester should note that apparent deviations from randomness may be due to either a poorly designed generator or to anomalies that appear in the binary sequence that is tested (i.e., a certain number of failures is expected in random sequences produced by a particular generator). It is up to the tester to determine the correct interpretation of the test results. Refer to Section 4 for a discussion of testing strategy and the interpretation of test results.

    1.1 General Discussion

    There are two basic types of generators used to produce random sequences: random number generators (RNGs - see Section 1.1.3) and pseudorandom number generators (PRNGs - see Section 1.1.4). For cryptographic applications, both of these generator types produce a stream of zeros and ones that may be divided into substreams or blocks of random numbers.

    1.1.1 Randomness

    A random bit sequence could be interpreted as the result of the flips of an unbiased fair coin with sides that are labeled 0 and 1, with each flip having a probability of exactly of producing a 0 or 1. Furthermore, the flips are independent of each other: the result of any previous coin flip does not affect future coin flips. The unbiased fair coin is thus the perfect random bit stream generator, since the 0 and 1 values will be randomly distributed (and [0,1] uniformly distributed). All elements of the sequence are generated independently of each other, and the value of the next element in the sequence cannot be predicted, regardless of how many elements have already been produced.

    Obviously, the use of unbiased coins for cryptographic purposes is impractical. Nonetheless, the hypothetical output of such an idealized generator of a true random sequence serves as a benchmark for the evaluation of random and pseudorandom number generators.

    1.1.2 Unpredictability

    Random and pseudorandom numbers generated for cryptographic applications should be unpredictable. In the case of PRNGs, if the seed is unknown, the next output number in the sequence should be unpredictable in spite of any knowledge of previous random numbers in the sequence. This property is known as forward unpredictability. It should also not be feasible to determine the seed from knowledge of any generated values (i.e., backward unpredictability is also required). No correlation between a seed and any value generated from that seed should be evident; each element of the sequence should appear to be the outcome of an independent random event whose probability is 1/2.

    To ensure forward unpredictability, care must be exercised in obtaining seeds. The values produced by a PRNG are completely predictable if the seed and generation algorithm are known. Since in many cases

    1-1

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    the generation algorithm is publicly available, the seed must be kept secret and should not be derivable from the pseudorandom sequence that it produces. In addition, the seed itself must be unpredictable.

    1.1.3 Random Number Generators (RNGs)

    The first type of sequence generator is a random number generator (RNG). An RNG uses a non-deterministic source (i.e., the entropy source), along with some processing function (i.e., the entropy distillation process) to produce randomness. The use of a distillation process is needed to overcome any weakness in the entropy source that results in the production of non-random numbers (e.g., the occurrence of long strings of zeros or ones). The entropy source typically consists of some physical quantity, such as the noise in an electrical circuit, the timing of user processes (e.g., key strokes or mouse movements), or the quantum effects in a semiconductor. Various combinations of these inputs may be used.

    The outputs of an RNG may be used directly as a random number or may be fed into a pseudorandom number generator (PRNG). To be used directly (i.e., without further processing), the output of any RNG needs to satisfy strict randomness criteria as measured by statistical tests in order to determine that the physical sources of the RNG inputs appear random. For example, a physical source such as electronic noise may contain a superposition of regular structures, such as waves or other periodic phenomena, which may appear to be random, yet are determined to be non-random using statistical tests.

    For cryptographic purposes, the output of RNGs needs to be unpredictable. However, some physical sources (e.g., date/time vectors) are quite predictable. These problems may be mitigated by combining outputs from different types of sources to use as the inputs for an RNG. However, the resulting outputs from the RNG may still be deficient when evaluated by statistical tests. In addition, the production of high-quality random numbers may be too time consuming, making such production undesirable when a large quantity of random numbers is needed. To produce large quantities of random numbers, pseudorandom number generators may be preferable.

    1.1.4 Pseudorandom Number Generators (PRNGs)

    The second generator type is a pseudorandom number generator (PRNG). A PRNG uses one or more inputs and generates multiple pseudorandom numbers. Inputs to PRNGs are called seeds. In contexts in which unpredictability is needed, the seed itself must be random and unpredictable. Hence, by default, a PRNG should obtain its seeds from the outputs of an RNG; i.e., a PRNG requires a RNG as a companion.

    The outputs of a PRNG are typically deterministic functions of the seed; i.e., all true randomness is confined to seed generation. The deterministic nature of the process leads to the term pseudorandom. Since each element of a pseudorandom sequence is reproducible from its seed, only the seed needs to be saved if reproduction or validation of the pseudorandom sequence is required.

    Ironically, pseudorandom numbers often appear to be more random than random numbers obtained from physical sources. If a pseudorandom sequence is properly constructed, each value in the sequence is produced from the previous value via transformations that appear to introduce additional randomness. A series of such transformations can eliminate statistical auto-correlations between input and output. Thus, the outputs of a PRNG may have better statistical properties and be produced faster than an RNG.

    1.1.5 Testing

    Various statistical tests can be applied to a sequence to attempt to compare and evaluate the sequence to a truly random sequence. Randomness is a probabilistic property; that is, the properties of a random

    1-2

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    sequence can be characterized and described in terms of probability. The likely outcome of statistical tests, when applied to a truly random sequence, is known a priori and can be described in probabilistic terms. There are an infinite number of possible statistical tests, each assessing the presence or absence of a pattern which, if detected, would indicate that the sequence is nonrandom. Because there are so many tests for judging whether a sequence is random or not, no specific finite set of tests is deemed complete. In addition, the results of statistical testing must be interpreted with some care and caution to avoid incorrect conclusions about a specific generator (see Section 4).

    A statistical test is formulated to test a specific null hypothesis (H0). For the purpose of this document, the null hypothesis under test is that the sequence being tested is random. Associated with this null hypothesis is the alternative hypothesis (Ha), which, for this document, is that the sequence is not random. For each applied test, a decision or conclusion is derived that accepts or rejects the null hypothesis, i.e., whether the generator is (or is not) producing random values, based on the sequence that was produced.

    For each test, a relevant randomness statistic must be chosen and used to determine the acceptance or rejection of the null hypothesis. Under an assumption of randomness, such a statistic has a distribution of possible values. A theoretical reference distribution of this statistic under the null hypothesis is determined by mathematical methods. From this reference distribution, a critical value is determined (typically, this value is "far out" in the tails of the distribution, say out at the 99 % point). During a test, a test statistic value is computed on the data (the sequence being tested). This test statistic value is compared to the critical value. If the test statistic value exceeds the critical value, the null hypothesis for randomness is rejected. Otherwise, the null hypothesis (the randomness hypothesis) is not rejected (i.e., the hypothesis is accepted).

    In practice, the reason that statistical hypothesis testing works is that the reference distribution and the critical value are dependent on and generated under a tentative assumption of randomness. If the randomness assumption is, in fact, true for the data at hand, then the resulting calculated test statistic value on the data will have a very low probability (e.g., 0.01 %) of exceeding the critical value. On the other hand, if the calculated test statistic value does exceed the critical value (i.e., if the low probability event does in fact occur), then from a statistical hypothesis testing point of view, the low probability event should not occur naturally. Therefore, when the calculated test statistic value exceeds the critical value, the conclusion is made that the original assumption of randomness is suspect or faulty. In this case, statistical hypothesis testing yields the following conclusions: reject H0 (randomness) and accept Ha (non-randomness).

    Statistical hypothesis testing is a conclusion-generation procedure that has two possible outcomes, either accept H0 (the data is random) or accept Ha (the data is non-random). The following 2 by 2 table relates the true (unknown) status of the data at hand to the conclusion arrived at using the testing procedure.

    TRUE SITUATION CONCLUSION

    Accept H0 Accept Ha (reject H0) Data is random (H0 is true) No error Type I error

    Data is not random (Ha is true) Type II error No error

    If the data is, in truth, random, then a conclusion to reject the null hypothesis (i.e., conclude that the data is non-random) will occur a small percentage of the time. This conclusion is called a Type I error. If the data is, in truth, non-random, then a conclusion to accept the null hypothesis (i.e., conclude that the data is actually random) is called a Type II error. The conclusions to accept H0 when the data is really random, and to reject H0 when the data is non-random, are correct.

    1-3

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    The probability of a Type I error is often called the level of significance of the test. This probability can be set prior to a test and is denoted as . For the test, is the probability that the test will indicate that the sequence is not random when it really is random. That is, a sequence appears to have non-random properties even when a good generator produced the sequence. Common values of in cryptography are about 0.01.

    The probability of a Type II error is denoted as . For the test, is the probability that the test will indicate that the sequence is random when it is not; that is, a bad generator produced a sequence that appears to have random properties. Unlike , is not a fixed value. can take on many different values because there are an infinite number of ways that a data stream can be non-random, and each different way yields a different . The calculation of the Type II error is more difficult than the calculation of because of the many possible types of non-randomness.

    One of the primary goals of the following tests is to minimize the probability of a Type II error, i.e., to minimize the probability of accepting a sequence being produced by a generator as good when the generator was actually bad. The probabilities and are related to each other and to the size n of the tested sequence in such a way that if two of them are specified, the third value is automatically determined. Practitioners usually select a sample size n and a value for (the probability of a Type I error the level of significance). Then a critical point for a given statistic is selected that will produce the smallest (the probability of a Type II error). That is, a suitable sample size is selected along with an acceptable probability of deciding that a bad generator has produced the sequence when it really is random. Then the cutoff point for acceptability is chosen such that the probability of falsely accepting a sequence as random has the smallest possible value.

    Each test is based on a calculated test statistic value, which is a function of the data. If the test statistic value is S and the critical value is t, then the Type I error probability is P(S > t || Ho is true) = P(reject Ho | H0 is true), and the Type II error probability is P(S t || H0 is false) = P(accept H0 | H0 is false). The test statistic is used to calculate a P-value that summarizes the strength of the evidence against the null hypothesis. For these tests, each P-value is the probability that a perfect random number generator would have produced a sequence less random than the sequence that was tested, given the kind of non-randomness assessed by the test. If a P-value for a test is determined to be equal to 1, then the sequence appears to have perfect randomness. A P-value of zero indicates that the sequence appears to be completely non-random. A significance level () can be chosen for the tests. If P-value , then the null hypothesis is accepted; i.e., the sequence appears to be random. If P-value < , then the null hypothesis is rejected; i.e., the sequence appears to be non-random. The parameter denotes the probability of the Type I error. Typically, is chosen in the range [0.001, 0.01].

    An of 0.001 indicates that one would expect one sequence in 1000 sequences to be rejected by the test if the sequence was random. For a P-value 0.001, a sequence would be considered to be random with a confidence of 99.9%. For a P-value < 0.001, a sequence would be considered to be non-random with a confidence of 99.9%.

    An of 0.01 indicates that one would expect 1 sequence in 100 sequences to be rejected. A P-value 0.01 would mean that the sequence would be considered to be random with a confidence of 99%. A P-value < 0.01 would mean that the conclusion was that the sequence is non-random with a confidence of 99%.

    For the examples in this document, has been chosen to be 0.01. Note that, in many cases, the parameters in the examples do not conform to the recommended values; the examples are for illustrative purposes only.

    1-4

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    1.1.6 Considerations for Randomness, Unpredictability and Testing

    The following assumptions are made with respect to random binary sequences to be tested:

    1. Uniformity: At any point in the generation of a sequence of random or pseudorandom bits, the occurrence of a zero or one is equally likely, i.e., the probability of each is exactly 1/2. The expected number of zeros (or ones) is n/2, where n = the sequence length.

    2. Scalability: Any test applicable to a sequence can also be applied to subsequences extracted at random. If a sequence is random, then any such extracted subsequence should also be random. Hence, any extracted subsequence should pass any test for randomness.

    3. Consistency: The behavior of a generator must be consistent across starting values (seeds). It is inadequate to test a PRNG based on the output from a single seed, or an RNG on the basis of an output produced from a single physical output.

    1.2 Definitions

    Term Definition Asymptotic Analysis A statistical technique that derives limiting approximations for functions

    of interest. Asymptotic Distribution The limiting distribution of a test statistic arising when n approaches

    infinity. Bernoulli Random Variable

    A random variable that takes on the value of one with probability p and the value of zero with probability 1-p.

    Binary Sequence A sequence of zeroes and ones. Binomial Distribution A random variable is binomially distributed if there is an integer n and a

    probability p such that the random variable is the number of successes in n independent Bernoulli experiments, where the probability of success in a single experiment is p. In a Bernoulli experiment, there are only two possible outcomes.

    Bit String A sequence of bits. Block A subset of a bit string. A block has a predetermined length. Central Limit Theorem For a random sample of size n from a population with mean and

    variance 2, the distribution of the sample means is approximately normal with mean and variance 2/n as the sample size increases.

    Complementary Error Function

    See Erfc.

    Confluent Hypergeometric Function

    The confluent hypergeometric function is defined as

    (a;b;z) = (b) (a)(b a)

    ezt t a1(1 t)ba1 dt0

    1 ,0 < a < b.

    Critical Value The value that is exceeded by the test statistic with a small probability (significance level). A "look-up" or calculated value of a test statistic (i.e., a test statistic value) that, by construction, has a small probability of occurring (e.g., 5 %) when the null hypothesis of randomness is true.

    Cumulative Distribution Function (CDF) F(x)

    A function giving the probability that the random variable X is less than or equal to x, for every value x. That is, F(x) = P(X x).

    1-5

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    Entropy A measure of the disorder or randomness in a closed system. The entropy of uncertainty of a random variable X with probabilities pi, , pn is

    defined to be H(X) = i n

    i 1 i p log p

    = .

    Entropy Source A physical source of information whose output either appears to be random in itself or by applying some filtering/distillation process. This output is used as input to either a RNG or PRNG.

    Erfc The complementary error function erfc(z) is defined in Section 5.5.3. This function is related to the normal cdf.

    Geometric Random Variable

    A random variable that takes the value k, a non-negative integer with probability pk(1-p). The random variable x is the number of successes before a failure in an infinite series of Bernoulli trials.

    Global Structure/Global Value

    A structure/value that is available by all routines in the test code.

    igamc The incomplete gamma function Q(a,x) is defined in Section 5.5.3. Incomplete Gamma Function

    See the definition for igamc.

    Hypothesis (Alternative) A statement Ha that an analyst will consider as true (e.g., Ha: the sequence is non-random) if and when the null hypothesis is determined to be false.

    Hypothesis (Null) A statement H0 about the assumed default condition/property of the observed sequence. For the purposes of this document, the null hypothesis H0 is that the sequence is random. If H0 is in fact true, then the reference distribution and critical values of the test statistic may be derived.

    Kolmogorov-Smirnov Test A statistical test that may be used to determine if a set of data comes from a particular probability distribution.

    Level of Significance () The probability of falsely rejecting the null hypothesis, i.e., the probability of concluding that the null hypothesis is false when the hypothesis is, in fact, true. The tester usually chooses this value; typical values are 0.05, 0.01 or 0.001; occasionally, smaller values such as 0.0001 are used. The level of significance is the probability of concluding that a sequence is non-random when it is in fact random. Synonyms: Type I error, error.

    Linear Dependence In the context of the binary rank matrix test, linear dependence refers to m-bit vectors that may be expressed as a linear combination of the linearly independent m-bit vectors.

    Maple An interactive computer algebra system that provides a complete mathematical environment for the manipulation and simplification of symbolic algebraic expressions, arbitrary extended precision mathematics, two- and three-dimensional graphics, and programming.

    MATLAB An integrated, technical computer environment that combines numeric computation, advanced graphics and visualization, and a high level programming language. MATLAB includes functions for data analysis and visualization; numeric and symbolic computation; engineering and scientific graphics; modeling, simulation and prototyping; and programming, application development and a GUI design.

    Normal (Gaussian) Distribution

    A continuous distribution whose density function is given by 2

    2 1

    22 1);( ;

    =

    x

    exf , where and are location and scale

    parameters.

    1-6

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    P-value The probability (under the null hypothesis of randomness) that the chosen test statistic will assume values that are equal to or worse than the observed test statistic value when considering the null hypothesis. The P-value is frequently called the tail probability.

    Poisson Distribution - 3.8 Poisson distributions model (some) discrete random variables. Typically, a Poisson random variable is a count of the number of rare events that occur in a certain time interval.

    Probability Density Function (PDF)

    A function that provides the "local" probability distribution of a test statistic. From a finite sample size n, a probability density function will be approximated by a histogram.

    Probability Distribution The assignment of a probability to the possible outcomes (realizations) of a random variable.

    Pseudorandom Number Generator (PRNG)

    A deterministic algorithm which, given a truly random binary sequence of length k, outputs a binary sequence of length l >> k which appears to be random. The input to the generator is called the seed, while the output is called a pseudorandom bit sequence.

    Random Number Generator (RNG)

    A mechanism that purports to generate truly random data.

    Random Binary Sequence A sequence of bits for which the probability of each bit being a 0 or 1 is . The value of each bit is independent of any other bit in the sequence, i.e., each bit is unpredictable.

    Random Variable Random variables differ from the usual deterministic variables (of science and engineering) in that random variables allow the systematic distributional assignment of probability values to each possible outcome.

    Rank (of a matrix) Refers to the rank of a matrix in linear algebra over GF(2). Having reduced a matrix into row-echelon form via elementary row operations, the number of nonzero rows, if any, are counted in order to determine the number of linearly independent rows or columns in the matrix.

    Run An uninterrupted sequence of like bits (i.e., either all zeroes or all ones). Seed The input to a pseudorandom number generator. Different seeds generate

    different pseudorandom sequences. SHA-1 The Secure Hash Algorithm defined in Federal Information Processing

    Standard 180-1. Standard Normal Cumulative Distribution Function

    See the definition in Section 5.5.3. This is the normal function for mean = 0 and variance = 1.

    Statistically Independent Events

    Two events are independent if the occurrence of one event does not affect the chances of the occurrence of the other event. The mathematical formulation of the independence of events A and B is the probability of the occurrence of both A and B being equal to the product of the probabilities of A and B (i.e., P(A and B) = P(A)P(B)).

    Statistical Test (of a Hypothesis)

    A function of the data (binary stream) which is computed and used to decide whether or not to reject the null hypothesis. A systematic statistical rule whose purpose is to generate a conclusion regarding whether the experimenter should accept or reject the null hypothesis Ho.

    Word A predefined substring consisting of a fixed pattern/template (e.g., 010, 0110).

    1-7

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    1.3 Abbreviations

    Abbreviation Definition ANSI American National Standards Institute FIPS Federal Information Processing Standard NIST National Institute of Standards and Technology RNG Random Number Generator SHA-1 Secure Hash Algorithm

    1.4 Mathematical Symbols

    In general, the following notation is used throughout this document. However, the tests in this document have been designed and described by multiple authors who may have used slightly different notation. The reader is advised to consider the notation used for each test separate from that notation used in other tests.

    Symbol Meaning x The floor function of x; for a given real positive x, x = x-g, where x

    is a non-negative integer, and 0 g < 1. The significance level. d The normalized difference between the observed and expected number of frequency

    components. See Sections 2.6 and 3.6. 2 m(obs); 22 m(obs)

    A measure of how well the observed values match the expected value. See Sections 2.11 and 3.11.

    E[ ] The expected value of a random variable. The original input string of zero and one bits to be tested. i The ith bit in the original sequence . H0 The null hypothesis; i.e., the statement that the sequence is random. log(x) The natural logarithm of x: log(x) = loge(x) = ln(x). log2(x)

    Defined as ln( 2 ) ln( x )

    , where ln is the natural logarithm.

    M The number of bits in a substring (block) being tested. N The number of M-bit blocks to be tested. n The number of bits in the stream being tested. fn The sum of the log2 distances between matching L-bit templates, i.e., the sum of the

    number of digits in the distance between L-bit templates. See Sections 2.9 and 3.9. 3.14159 unless defined otherwise for a specific test. The standard deviation of a random variable = ( ) f ( x )dxx 2 . 2 The variance of a random variable = (standard deviation)2 . sobs The observed value which is used as a statistic in the Frequency test. Sn The nth partial sum for values Xi = {-1, +1}; i.e., the sum of the first n values of Xi. The summation symbol. Standard Normal Cumulative Distribution Function (see Section 5.5.3). j The total number of times that a given state occurs in the identified cycles. See

    Section 2.15 and 3.15. Xi The elements of the string consisting of 1 that is to be tested for randomness, where

    Xi = 2i -1.

    1-8

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2 The [theoretical] chi-square distribution; used as a test statistic; also, a test statistic that follows the 2 distribution.

    2(obs) The chi-square statistic computed on the observed values. See Sections 2.2, 2.4, 2.5, 2.7, 2.8, 2.10, 2.12, 2.14, and the corresponding sections of Section 3.

    Vn The expected number of runs that would occur in a sequence of length n under an assumption of randomness See Sections 2.3 and 3.3.

    Vn(obs) The observed number of runs in a sequence of length n. See Sections 2.3 and 3.3.

    1-9

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2. Random Number Generation Tests

    The NIST Test Suite is a statistical package consisting of 15 tests that were developed to test the randomness of (arbitrarily long) binary sequences produced by either hardware or software based cryptographic random or pseudorandom number generators. These tests focus on a variety of different types of non-randomness that could exist in a sequence. Some tests are decomposable into a variety of subtests. The 15 tests are:

    1. The Frequency (Monobit) Test, 2. Frequency Test within a Block, 3. The Runs Test, 4. Tests for the Longest-Run-of-Ones in a Block, 5. The Binary Matrix Rank Test, 6. The Discrete Fourier Transform (Spectral) Test, 7. The Non-overlapping Template Matching Test, 8. The Overlapping Template Matching Test, 9. Maurer's "Universal Statistical" Test, 10. The Linear Complexity Test, 11. The Serial Test, 12. The Approximate Entropy Test, 13. The Cumulative Sums (Cusums) Test, 14. The Random Excursions Test, and 15. The Random Excursions Variant Test.

    This section (Section 2) consists of 15 subsections, one subsection for each test. Each subsection provides a high level description of the particular test. The corresponding subsections in Section 3 provide the technical details for each test.

    Section 4 provides a discussion of testing strategy and the interpretation of test results. The order of the application of the tests in the test suite is arbitrary. However, it is recommended that the Frequency test be run first, since this supplies the most basic evidence for the existence of non-randomness in a sequence, specifically, non-uniformity. If this test fails, the likelihood of other tests failing is high. (Note: The most time-consuming statistical test is the Linear Complexity test; see Sections 2.10 and 3.10).

    Section 5 provides a user's guide for setting up and running the tests, and a discussion on program layout. The statistical package includes source code and sample data sets. The test code was developed in ANSI C. Some inputs are assumed to be global values rather than calling parameters.

    A number of tests in the test suite have the standard normal and the chi-square ( 2 ) as reference distributions. If the sequence under test is in fact non-random, the calculated test statistic will fall in extreme regions of the reference distribution. The standard normal distribution (i.e., the bell-shaped curve) is used to compare the value of the test statistic obtained from the RNG with the expected value of the statistic under the assumption of randomness. The test statistic for the standard normal distribution is of the form z = (x - )/, where x is the sample test statistic value, and and 2 are the expected value and the variance of the test statistic. The 2 distribution (i.e., a left skewed curve) is used to compare the goodness-of-fit of the observed frequencies of a sample measure to the corresponding expected

    2-1

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    frequencies of the hypothesized distribution. The test statistic is of the form 2 = ((o ei )2 e ),i i where oi and ei are the observed and expected frequencies of occurrence of the measure, respectively.

    For many of the tests in this test suite, the assumption has been made that the size of the sequence length, n, is large (of the order 103 to 107). For such large sample sizes of n, asymptotic reference distributions have been derived and applied to carry out the tests. Most of the tests are applicable for smaller values of n. However, if used for smaller values of n, the asymptotic reference distributions would be inappropriate and would need to be replaced by exact distributions that would commonly be difficult to compute.

    Note: For many of the examples throughout Section 2, small sample sizes are used for illustrative purposes only, e.g., n = 10. The normal approximation is not really applicable for these examples.

    2.1 Frequency (Monobit) Test

    2.1.1 Test Purpose The focus of the test is the proportion of zeroes and ones for the entire sequence. The purpose of this test is to determine whether the number of ones and zeros in a sequence are approximately the same as would be expected for a truly random sequence. The test assesses the closeness of the fraction of ones to , that is, the number of ones and zeroes in a sequence should be about the same. All subsequent tests depend on the passing of this test.

    2.1.2 Function Call Frequency(n), where:

    n The length of the bit string.

    Additional input used by the function, but supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    2.1.3 Test Statistic and Reference Distribution sobs: The absolute value of the sum of the Xi (where, Xi = 2 - 1 = 1) in the sequence divided

    by the square root of the length of the sequence.

    The reference distribution for the test statistic is half normal (for large n). (Note: If z (where z = 2 ; see Section 3.1) is distributed as normal, then |z| is distributed as half normal.) If the sobs sequence is random, then the plus and minus ones will tend to cancel one another out so that the test statistic will be about 0. If there are too many ones or too many zeroes, then the test statistic will tend to be larger than zero.

    2.1.4 Test Description (1) Conversion to 1: The zeros and ones of the input sequence () are converted to values of 1 and

    +1 and are added together to produce Sn = X1 + X2 ++Xn , where Xi = 2i 1.

    2-2

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    For example, if = 1011010101, then n=10 and Sn = 1 + (-1) + 1 + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 = 2.

    Sn (2) Compute the test statistic sobs = n

    2For the example in this section, sobs = = .632455532.

    10

    sobs(3) Compute P-value = erfc , where erfc is the complementary error function as defined in 2

    Section 5.5.3.

    .632455532 For the example in this section, P-value = erfc = 0.527089.

    2

    2.1.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2.1.6 Conclusion and Interpretation of Results Since the P-value obtained in step 3 of Section 2.1.4 is 0.01 (i.e., P-value = 0.527089), the conclusion is that the sequence is random.

    Note that if the P-value were small (< 0.01), then this would be caused by or being large. Sn sobs Large positive values of Sn are indicative of too many ones, and large negative values of Sn are indicative of too many zeros.

    2.1.7 Input Size Recommendation It is recommended that each sequence to be tested consist of a minimum of 100 bits (i.e., n 100).

    2.1.8 Example (input) = 11001001000011111101101010100010001000010110100011

    00001000110100110001001100011001100010100010111000

    (input) n = 100

    (processing) S100 = -16

    (processing) sobs = 1.6

    (output) P-value = 0.109599

    (conclusion) Since P-value 0.01, accept the sequence as random.

    2-3

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.2 Frequency Test within a Block

    2.2.1 Test Purpose The focus of the test is the proportion of ones within M-bit blocks. The purpose of this test is to determine whether the frequency of ones in an M-bit block is approximately M/2, as would be expected under an assumption of randomness. For block size M=1, this test degenerates to test 1, the Frequency (Monobit) test.

    2.2.2 Function Call BlockFrequency(M,n), where:

    M The length of each block.

    n The length of the bit string.

    Additional input used by the function, but supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    2.2.3 Test Statistic and Reference Distribution

    2 (obs): A measure of how well the observed proportion of ones within a given M-bit block match the expected proportion (1/2).

    The reference distribution for the test statistic is a 2 distribution.

    2.2.4 Test Description n (1) Partition the input sequence into N = non-overlapping blocks. Discard any unused bits. M

    For example, if n = 10, M = 3 and = 0110011010, 3 blocks (N = 3) would be created, consisting of 011, 001 and 101. The final 0 would be discarded.

    M ( i1 )M + jj=1(2) Determine the proportion i of ones in each M-bit block using the equation = ,i M

    for 1 i N.

    For the example in this section, 1 = 2/3, 2 = 1/3, and 3 = 2/3.

    N

    (3) Compute the 2 statistic: 2(obs) = 4 M ( i - )2. i =1

    2 2 2 2 1 ) + (1 1 ) + (2 1 ) = 1 .For the example in this section, 2(obs) = 4 x 3 x ( 3 2 3 2 3 2

    2-4

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    (4) Compute P-value = igamc (N/2, 2(obs)/2) , where igamc is the incomplete gamma function for Q(a,x) as defined in Section 5.5.3.

    Note: When comparing this section against the technical description in Section 3.2, note that Q(a,x) = 1-P(a,x).

    3 1 For the example in this section, P-value = igamc = 0.801252.

    ,2 2

    2.2.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2.2.6 Conclusion and Interpretation of Results Since the P-value obtained in step 4 of Section 2.2.4 is 0.01 (i.e., P-value = 0.801252), the conclusion is that the sequence is random.

    Note that small P-values (< 0.01) would have indicated a large deviation from the equal proportion of ones and zeros in at least one of the blocks.

    2.2.7 Input Size Recommendation It is recommended that each sequence to be tested consist of a minimum of 100 bits (i.e., n 100). Note that n MN. The block size M should be selected such that M 20, M > .01n and N < 100.

    2.2.8 Example (input) = 11001001000011111101101010100010001000010110100011

    00001000110100110001001100011001100010100010111000

    (input) n = 100

    (input) M = 10

    (processing) N = 10

    (processing) 2 = 7.2

    (output) P-value = 0.706438

    (conclusion) Since P-value 0.01, accept the sequence as random.

    2.3 Runs Test

    2.3.1 Test Purpose The focus of this test is the total number of runs in the sequence, where a run is an uninterrupted sequence of identical bits. A run of length k consists of exactly k identical bits and is bounded before and after with a bit of the opposite value. The purpose of the runs test is to determine whether the number of runs of

    2-5

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    ones and zeros of various lengths is as expected for a random sequence. In particular, this test determines whether the oscillation between such zeros and ones is too fast or too slow.

    2.3.2 Function Call Runs(n), where:

    n The length of the bit string.

    Additional inputs for the function, but supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    2.3.3 Test Statistic and Reference Distribution Vn(obs): The total number of runs (i.e., the total number of zero runs + the total number of

    one-runs) across all n bits.

    The reference distribution for the test statistic is a 2 distribution.

    2.3.4 Test Description Note: The Runs test carries out a Frequency test as a prerequisite.

    j j(1) Compute the pre-test proportion of ones in the input sequence: = . n

    For example, if = 1001101011, then n=10 and = 6/10 = 3/5.

    (2) Determine if the prerequisite Frequency test is passed: If it can be shown that - 1 , then the 2 Runs test need not be performed (i.e., the test should not have been run because of a failure to pass test 1, the Frequency (Monobit) test). If the test is not applicable, then the P-value is set to 0.0000. Note that for this test, = 2 has been pre-defined in the test code.

    n

    For the example in this section, since = 2 0.63246 , then | - 1/2| = | 3/5 1/2 | = 0.1 < ,10

    and the test is not run.

    Since the observed value is within the selected bounds, the runs test is applicable.

    n1 n(3) Compute the test statistic V ( obs ) = r( k ) + 1 , where r(k)=0 if k=k+1, and r(k)=1 otherwise.

    k =1

    Since = 1 00 11 0 1 0 11, then

    V10(obs)=(1+0+1+0+1+1+1+1+0)+1=7.

    |V (obs) 2n (1 ) | (4) Compute P-value = erfc

    n . 2 2n (1 )

    2-6

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    = 0.147232.

    3 5

    13

    3 5

    7 2 10 For the example, P-value = erfc

    3

    2.3.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2.3.6 Conclusion and Interpretation of Results Since the P-value obtained in step 4 of Section 2.3.4 is 0.01 (i.e., P-value = 0.147232), the conclusion is that the sequence is random.

    Note that a large value for Vn(obs) would have indicated an oscillation in the string which is too fast; a small value would have indicated that the oscillation is too slow. (An oscillation is considered to be a change from a one to a zero or vice versa.) A fast oscillation occurs when there are a lot of changes, e.g., 010101010 oscillates with every bit. A stream with a slow oscillation has fewer runs than would be expected in a random sequence, e.g., a sequence containing 100 ones, followed by 73 zeroes, followed by 127 ones (a total of 300 bits) would have only three runs, whereas 150 runs would be expected.

    2.3.7 Input Size Recommendation It is recommended that each sequence to be tested consist of a minimum of 100 bits (i.e., n 100).

    2.3.8 Example (input) = 11001001000011111101101010100010001000010110100011

    00001000110100110001001100011001100010100010111000

    (input) n = 100

    (input) = 0.02

    (processing) = 0.42

    (processing) Vn(obs) = 52

    (output) P-value = 0.500798

    (conclusion) Since P-value 0.01, accept the sequence as random.

    2.4 Test for the Longest Run of Ones in a Block

    2.4.1 Test Purpose The focus of the test is the longest run of ones within M-bit blocks. The purpose of this test is to determine whether the length of the longest run of ones within the tested sequence is consistent with the length of the longest run of ones that would be expected in a random sequence. Note that an irregularity in the expected length of the longest run of ones implies that there is also an irregularity in the expected length of the longest run of zeroes. Therefore, only a test for ones is necessary. See Section 4.4.

    5

    1

    5

    2 2 10

    2-7

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.4.2 Function Call LongestRunOfOnes(n), where:

    n The length of the bit string.

    Additional input for the function supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    M The length of each block. The test code has been pre-set to accommodate three values for M: M = 8, M = 128 and M = 104 in accordance with the following values of sequence length, n:

    Minimum n M 128 8

    6272 128 750,000 104

    N The number of blocks; selected in accordance with the value of M.

    2.4.3 Test Statistic and Reference Distribution 2(obs): A measure of how well the observed longest run length within M-bit blocks

    matches the expected longest length within M-bit blocks.

    The reference distribution for the test statistic is a 2 distribution.

    2.4.4 Test Description (1) Divide the sequence into M-bit blocks.

    (2) Tabulate the frequencies i of the longest runs of ones in each block into categories, where each cell contains the number of runs of ones of a given length.

    For the values of M supported by the test code, the vi cells will hold the following counts:

    vi M = 8 M = 128 M = 104 v0 1 4 10 v1 2 5 11 v2 3 6 12 v3 4 7 13 v4 8 14 v5 9 15 v6 16

    For an example, see Section 2.4.8.

    2-8

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    K ( N )2 2 vi i(3) Compute ( obs ) = , where the values for i are provided in Section 3.4. The Ni=0 i values of K and N are determined by the value of M in accordance with the following table:

    M K N 8 3 16

    128 5 49 104 6 75

    For the example of 2.4.8, 2 2 2 2

    2 ( 4 16(.2148 )) ( 9 16(.3672 )) ( 3 16(.2305 )) (0 16(.1875 )) ( obs ) = + + + = 4.88260516(.2148 ) 16(.3672 ) 16(.2305 16(.1875 )

    K 2( obs ) (4) Compute P-value = igamc , . 2 2

    3 4.882605 For the example, P-value = igamc = 0.180598.

    ,2 2

    2.4.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2.4.6 Conclusion and Interpretation of Results For the example in Section 2.4.8, since the P-value 0.01 (P-value = 0.180609), the conclusion is that the sequence is random. Note that large values of 2(obs) indicate that the tested sequence has clusters of ones.

    2.4.7 Input Size Recommendation It is recommended that each sequence to be tested consists of a minimum of bits as specified in the table in Section 2.4.2.

    2.4.8 Example For the case where K = 3 and M = 8:

    (input) = 11001100000101010110110001001100111000000000001001 00110101010001000100111101011010000000110101111100 1100111001101101100010110010

    (input) n = 128

    (processing) Subblock Max-Run 11001100 01101100 11100000

    (2) (2) (3)

    Subblock Max-Run 00010101 01001100 00000010

    (1) (2) (1)

    2-9

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    01001101 (2) 01010001 (1) 00010011 (2) 11010110 (2) 10000000 (1) 11010111 (3) 11001100 (2) 11100110 (3) 11011000 (2) 10110010 (2)

    (processing) 0 = 4; 1 = 9; 2 = 3; 4 = 0; 2 = 4.882457

    (output) P-value = 0.180609

    (conclusion) Since the P-value is 0.01, accept the sequence as random.

    2.5 Binary Matrix Rank Test

    2.5.1 Test Purpose The focus of the test is the rank of disjoint sub-matrices of the entire sequence. The purpose of this test is to check for linear dependence among fixed length substrings of the original sequence. Note that this test also appears in the DIEHARD battery of tests [7].

    2.5.2 Function Call Rank(n), where:

    n The length of the bit string.

    Additional input used by the function supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    M The number of rows in each matrix. For the test suite, M has been set to 32. If other values of M are used, new approximations need to be computed.

    Q The number of columns in each matrix. For the test suite, Q has been set to 32. If other values of Q are used, new approximations need to be computed.

    2.5.3 Test Statistic and Reference Distribution 2(obs): A measure of how well the observed number of ranks of various orders match the

    expected number of ranks under an assumption of randomness.

    The reference distribution for the test statistic is a 2 distribution.

    2.5.4 Test Description n(1) Sequentially divide the sequence into MQ-bit disjoint blocks; there will exist N =

    such MQ

    blocks. Discarded bits will be reported as not being used in the computation within each block. Collect the MQ bit segments into M by Q matrices. Each row of the matrix is filled with successive Q-bit blocks of the original sequence .

    2-10

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    For example, if n = 20, M = Q = 3, and = 01011001001010101101, then partition the stream

    into N = n = 2 matrices of cardinality MQ (33 = 9). Note that the last two bits (0 and 1) 3 3

    0 1 0 0 1 0 will be discarded. The two matrices are 1 1 0 and 1 0 1 . Note that the first matrix

    0 1 0 0 1 1 consists of the first three bits in row 1, the second set of three bits in row 2, and the third set of three bits in row 3. The second matrix is similarly constructed using the next nine bits in the sequence.

    (2) Determine the binary rank ( R ) of each matrix, where = 1,..., N . The method for determining the rank is described in Appendix A.

    For the example in this section, the rank of the first matrix is 2 (R1 = 2), and the rank of the second matrix is 3 (R2 = 3).

    (3) Let FM = the number of matrices with R = M (full rank), FM-1 = the number of matrices with R = M-1 (full rank - 1), N FM - FM-1 = the number of matrices remaining.

    For the example in this section, FM = F3 = 1 (R2 has the full rank of 3), FM-1 = F2 = 1 (R1 has rank 2), and no matrix has any lower rank.

    (4) Compute 2 2 2(F 0.2888N ) (F 0.5776N ) (N F F 0.1336N )2 M M 1 M M 1 (obs) = + + .

    0.2888N 0.5776N 0.1336N

    For the example in this section, 2 2 2

    2 (1 0.2888 2) (1 0.5776 2) (2 11 0.1336 2) (obs) = + + = 0.596953. 0.2888 2 0.5776 2 0.1336 2

    2 (obs) / 2(5) Compute P value = e . Since there are 3 classes in the example, the P-value for the 2 (obs)

    example is equal to igamc1, . 2

    0.596953 For the example in this section, P-value = e 2 = 0.741948.

    2.5.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2-11

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.5.6 Conclusion and Interpretation of Results Since the P-value obtained in step 5 of Section 2.5.4 is 0.01 (P-value = 0.741948), the conclusion is that the sequence is random.

    Note that large values of 2 ( obs ) (and hence, small P-values) would have indicated a deviation of the rank distribution from that corresponding to a random sequence.

    2.5.7 Input Size Recommendation The probabilities for M = Q = 32 have been calculated and inserted into the test code. Other choices of M and Q may be selected, but the probabilities would need to be calculated. The minimum number of bits to be tested must be such that n 38MQ (i.e., at least 38 matrices are created). For M = Q = 32, each sequence to be tested should consist of a minimum of 38,912 bits.

    2.5.8 Example (input) = the first 100,000 binary digits in the expansion of e

    (input) n = 100000, M = Q = 32 (NOTE: 672 BITS WERE DISCARDED.)

    (processing) N = 97

    (processing) FM = 23, FM-1 = 60, N FM FM-1= 14

    (processing) 2 = 1.2619656

    (output) P-value = 0.532069

    (conclusion) Since P-value 0.01, accept the sequence as random.

    2.6 Discrete Fourier Transform (Spectral) Test

    2.6.1 Test Purpose The focus of this test is the peak heights in the Discrete Fourier Transform of the sequence. The purpose of this test is to detect periodic features (i.e., repetitive patterns that are near each other) in the tested sequence that would indicate a deviation from the assumption of randomness. The intention is to detect whether the number of peaks exceeding the 95 % threshold is significantly different than 5 %.

    2.6.2 Function Call DiscreteFourierTransform(n), where:

    n The length of the bit string.

    Additional input used by the function, but supplied by the testing code:

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    2-12

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.6.3 Test Statistic and Reference Distribution d: The normalized difference between the observed and the expected number of frequency

    components that are beyond the 95 % threshold.

    The reference distribution for the test statistic is the normal distribution.

    2.6.4 Test Description (1) The zeros and ones of the input sequence () are converted to values of 1 and +1 to create the

    sequence X = x1, x2, , xn, where xi = 2i 1.

    For example, if n = 10 and = 1001010011, then X = 1, -1, -1, 1, -1, 1, -1, -1, 1, 1.

    (2) Apply a Discrete Fourier transform (DFT) on X to produce: S = DFT(X). A sequence of complex variables is produced which represents periodic components of the sequence of bits at different frequencies (see Section 3.6 for a sample diagram of a DFT result).

    (3) Calculate M = modulus(S) |S'|, where S is the substring consisting of the first n/2 elements in S, and the modulus function produces a sequence of peak heights.

    1log

    (4) Compute T = n = the 95 % peak height threshold value. Under an assumption of

    0.05

    randomness, 95 % of the values obtained from the test should not exceed T.

    (5) Compute N0 = .95n/2. N0 is the expected theoretical (95 %) number of peaks (under the assumption of randomness) that are less than T.

    For the example in this section, N0 = 4.75.

    (6) Compute N1 = the actual observed number of peaks in M that are less than T.

    For the example in this section, N1 = 4.

    (N1 N0)(7) Compute d = . n(.95)(.05) /4

    (4 4.75)For the example in this section, d = = -2.176429.

    10 .95)(.05) /4 (

    d (8) Compute P-value = erfc . 2

    For the example in this section, P-value = erfc 2.176429

    = 0.029523. 2

    2-13

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2.6.5 Decision Rule (at the 1% Level) If the computed P-value is < 0.01, then conclude that the sequence is non-random. Otherwise, conclude that the sequence is random.

    2.6.6 Conclusion and Interpretation of Results Since the P-value obtained in step 8 of Section 2.6.4 is 0.01 (P-value = 0.029523), the conclusion is that the sequence is random.

    A d value that is too low would indicate that there were too few peaks (< 95%) below T, and too many peaks (more than 5%) above T.

    2.6.7 Input Size Recommendation It is recommended that each sequence to be tested consist of a minimum of 1000 bits (i.e., n 1000).

    2.6.8 Example (input) = 11001001000011111101101010100010001000010110100011

    00001000110100110001001100011001100010100010111000

    (input) n = 100

    (processing) N1 = 46

    (processing) N0 = 47.5

    (processing) d = -1.376494

    (output) P-value = 0.168669

    (conclusion) Since P-value 0.01, accept the sequence as random.

    2.7 Non-overlapping Template Matching Test

    2.7.1 Test Purpose The focus of this test is the number of occurrences of pre-specified target strings. The purpose of this test is to detect generators that produce too many occurrences of a given non-periodic (aperiodic) pattern. For this test and for the Overlapping Template Matching test of Section 2.8, an m-bit window is used to search for a specific m-bit pattern. If the pattern is not found, the window slides one bit position. If the pattern is found, the window is reset to the bit after the found pattern, and the search resumes.

    2.7.2 Function Call NonOverlappingTemplateMatching(m,n)

    m The length in bits of each template. The template is the target string.

    n The length of the entire bit string under test.

    Additional input used by the function, but supplied by the testing code:

    2-14

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    The sequence of bits as generated by the RNG or PRNG being tested; this exists as a global structure at the time of the function call; = 1, 2, , n.

    B The m-bit template to be matched; B is a string of ones and zeros (of length m) which is defined in a template library of non-periodic patterns contained within the test code.

    M The length in bits of the substring of to be tested.

    N The number of independent blocks. N has been fixed at 8 in the test code.

    2.7.3 Test Statistic and Reference Distribution 2(obs): A measure of how well the observed number of template hits matches the

    expected number of template hits (under an assumption of randomness).

    The reference distribution for the test statistic is the 2 distribution.

    2.7.4 Test Description (1) Partition the sequence into N independent blocks of length M.

    For example, if = 10100100101110010110, then n = 20. If N = 2 and M = 10, then the two blocks would be 1010010010 and 1110010110.

    (2) Let Wj (j = 1, , N) be the number of times that B (the template) occurs within the block j. Note that j = 1,,N. The search for matches proceeds by creating an m-bit window on the sequence, comparing the bits within that window against the template. If there is no match, the window slides over one bit , e.g., if m = 3 and the current window contains bits 3 to 5, then the next window will contain bits 4 to 6. If there is a match, the window slides over m bits, e.g., if the current (successful) window contains bits 3 to 5, then the next window will contain bits 6 to 8.

    For the above example, if m = 3 and the template B = 001, then the examination proceeds as follows:

    Bit Positions Block 1 Block 2

    Bits W1 Bits W2 1-3 101 0 111 0 2-4 010 0 110 0 3-5 100 0 100 0 4-6 001 (hit) Increment to 1 001 (hit) Increment to 1 5-7 Not examined Not examined 6-8 Not examined Not examined 7-9 001 Increment to 2 011 1

    8-10 010 (hit) 2 110 1

    Thus, W1 = 2, and W2 = 1.

    (3) Under an assumption of randomness, compute the theoretical mean and variance 2: 1 2m 1

    = (M-m+1)/2m 2 = M . m 2m 2 2

    2-15

  • A STATISTICAL TEST SUITE FOR RANDOM AND PSEUDORANDOM NUMBER GENERATORS FOR CRYPTOGRAPHIC APPLICATIONS

    2 1 2 3 1 For the example in this section, = (10-3+1)/23 = 1, and = 10 = 0.46875 . 3 23 2 2

    2 N (W j )2 (4) Compute ( obs ) = 2 . j=1