Mersenne Twister – a Pseudo Random Number Generator and Its Variants
Total Page:16
File Type:pdf, Size:1020Kb
Mersenne Twister – A Pseudo Random Number Generator and its Variants Archana Jagannatam Abstract: Random number generators(RNG) are widely being used in number of applications, particularly simulation and cryptography. They are a critical part of many cryptographic systems such as key generation, initialization vectors, message padding, nonces and many more. This paper discusses about the Mersenne Twister(MT), a pseudo random number generator(PRNG) and its variants. It mainly emphasizes on two of its variants. SIMD-Oriented Fast Mersenne Twister(SFMT) which is a 128-bit PRNG analogous to MT making full use of its features. And the cryptographically secure CryptMT, considered to be one of the fastest stream ciphers on a CPU with SIMD operations. It also briefly discusses the theories and the choice of parameters used in the algorithms. The requirements for a PRNG to be certified as a good and cryptographically secure PRNG will be presented. 1. Introduction: Random number generators are devices that generate a series of numbers or some kind of symbols that appear random. RNG’S are used for a variety of purposes such as simulating, modeling complex phenomena, cryptography and of course ever popular for games and gambling. There are two main approaches to generating random numbers, Pseudo Random Number Generators(PRNG) and True Random Number Generators(TRNG). TRNG extracts randomness from physical phenomena like atmospheric noise, little variations in mouse movements or the time between mouse strokes and feed then in to a computer. They are mainly used in games and gambling where genuine randomness is required, as they are to slow for use in statistical and cryptographic applications. In comparison to TRNG, PRNG’s are algorithms using some kind of mathematical formula or pre-calculated tables to generate a sequence of numbers that appear random. The sequence of numbers produced is not truly random. It is completely determined by an arbitrary initial state called seed state. If a PRNG's internal state contains n bits, its period can be no longer than 2n results. PRNG’s are efficient, deterministic and periodic which makes them suitable for applications where many numbers are required and where it is useful that the same sequence can be replayed easily such as simulation and modeling. A PRNG should posses some requirements to be considered as a good PRNG, such as uniformity, independence, long period, proper initialization, unpredictability, efficiency and portability. The quality of randomness of a PRNG is measured by subjecting it to a set of statistical testes called diehard tests. Every cryptographically secure PRNG should satisfy two basic requirements, next-bit test and state compromise extensions. This paper discusses Mersenne Twister(MT) which is a PRNG and which satisfies all the requirements to be certified as a good PRNG. MT is proposed in 1997 by Makoto Matsumoto and Takuji Nishimura. It provides for fast generation of very high-quality pseudorandom numbers with a long period length which is chosen to be a Mersenne prime, high order of dimensional equidistribution, speed and reliability. Later many different variants of MT has been introduced for better speed and security for cryptographic uses. Two of the variants this paper talks about are SIMD-Oriented Fast Mersenne Twister(SFMT) and cryptographically secure CryptMT. SFMT is introduced by Mutsuo Saito and Makoto Matsumoto in 2006. It is typically a Linear Feedback Shift Register(LFSR) which generates a 128-bit pseudorandom integer at one step. SFMT is introduced to use the new features of CPU’s such as Single Instruction Multiple Data (SIMD) operations (i.e., 128bit operations) and multi-stage pipelines. CryptMT is a stream cipher which uses MT and is considered to be cryptographically secure. It was developed by Makoto Matsumoto, Hagita Mariko, Takuji Nishimura and Mutsuo Saito in 2005. CryptMT ver. 3 is the latest version. In section 2.1 of this paper we discuss the theories behind MT and its algorithm. Section 2.2 discusses the choice of parameters for MT also presents a table showing values for different parameters of 32-bit MT. Section 2.3 talks about the limitations of MT such as initialization and security, and also the modification made to overcome the limitations. Section 3 introduces SFMT and 3.1 shows the theories and algorithms for SFMT, and also contains two figures of a circuit like description of SFMT19937 and block generation scheme. Next we talk about the choice of parameters of SFMT for the calculation of period and dimension of equidistribution in section 3.2. Then in 3.3 its limitation such as recovery from 0-excess state and the improvements made are discussed. Section 4 introduces CryptMT, we discuss its first version, the theories behind it and presents a block diagram of CryptMT ver. 1. Section 4.1 talks about its latest version CryptMT ver. 3, its theories and contains a figure showing combined generator. It also shows a table containing the parameters for the speed comparison of CryptMT with other stream ciphers. Then its advantages and shortcomings are discussed in section 4.2. The appendix of the paper shows the inversive-decimation method for primitivity testing. 2. Mersenne Twister: Mersenne Twister, the name derives from the fact that it uses a period which is a Mersenne prime. It is a modification of a Twisted Generalized Feedback Shift Register(TGFSR) which takes in an incomplete array to realize a Mersenne prime as its period and uses an inversive-decimation method for primitivity testing of a characteristic polynomial of a linear recurrence with a computational complexity of O(p2) where p is the degree of the polynomial. MT has a long period of 219937-1and a 623 dimensional equidistribution up to 32-bit accuracy, generating an output which is free of long-term correlations. It is considered to be fast, as it avoids multiplications and divisions and uses the advantages of cashes and pipelines and efficient in memory use as only 624 words needed for the working area. 2.1-MT Theories: MT generates a sequence of word vectors considered as uniform pseudorandom integers between 0 and w 2 -1, where w is the dimension of row vectors over the finite binary field F2. It is based on the following recurring equation: = ⊕ u l xk+n : xk+m (xk | xk+1 )A (1) Here: Integer n is the degree of recurrence. Integer m, 1≤ m ≤ n. A is a constant w × w matrix chosen to ease the matrix multiplication shown below 0 1 0 0 1 0 A = 1 aw−1 aw−2 a0 k is 0,1,2…… xn is a row vector of a word size w which is generated with k =0. x0, x1, …., xn-1 are initial seeds. l xk+1 are lower or rightmost r bits of xk+1 . u xk are upper or leftmost w – r bits of xk . ⊕ denotes a bitwise XOR. | denotes a concatenating operation. u l ( xk | xk+1 ) is the concatenation vector obtained by concatenating the upper w - r bits of xk and the lower l r bits of xk+1 in the order. Then the matrix A is multiplied from the right by this vector. Finally bitwise ⊕ u addition is performed to add xk+m , and then generate the next vector xk+n . The multiplication of ( xk l | xk+1 )A can be performed using simple bit shift operations as shown below. x >> 1 if x = 0 xA = 0 >> ⊕ = (x 1) a if x0 1 Where x is (xw−1 , xw−2 ,..., x0 ) , a is the bottom row vector of A and ‘ >>’ denotes bitwise right shifts. Thus the calculation of the recurrence (1) is realized with bitshift, bitwise EXCLUSIVE-OR, bitwise OR, and bitwise AND operations. MT has (n-1) dimensional equidistribution, which is a good characteristic of PRNG’s. k-distribution tests are considered to be a good way to measure the randomness of a PRNG. From the definition, a periodic = sequence X : x0 , x1,..., x p−1 with period p of w -bit integers is said to have k-dimensional equidistribution property if it has the maximum period and if any kw -bit pattern equally likely occurs as k-tuple = (xl , xl+1 ,..., xl+k−1 ) for l 0,1,2,... All the overlapping k-tuples appearing in one period, then the output k- tuples are uniformly distributed over the set of possible output numbers. A largest value of such k is called the dimension of equidistribution. To improve the k-distribution to v-bit accuracy of the raw sequences generated from the recursion (1), a method called tempering is used to produce the final pseudo random number. Each generated word is multiplied by a w × w invertible matrix T from right yielding a result of tempering matrix x into z := xT . The matrix T is chosen such that binary operations can be performed as: y := x ⊕ (x >> u) y := y ⊕ ((y << s) & b) y := y ⊕ ((y << t) & c) z := y ⊕ (y >> l) where u, s, t and l are tempering bit shifts. b and c are tempering bitmasks. ‘<<’ denotes a bitwise left shift and ‘&’ denotes a bitwise AND operation. MT works in two parts: recurring and tempering. The recurring is a form of Linear Feedback Shift Register(LFSR) in which each bit of the state derives from the recursion and each bit of the output also satisfies the recurring of the bits forming the states. Figure 1 shows a high level block diagram of MT. LFSR 32-bit 32-bit …………….. 32-bit 32-bit 0 1 622 623 Tempering 19937-bit output Fig 1. Block diagram of MT The shift register is composed of 624 elements and a total of 19937 cells.