Coverage Report

Created: 2020-12-02 17:02

/libfido2/fuzz/prng.c
Line
Count
Source
1
/* 
2
   A C-program for MT19937, with initialization improved 2002/1/26.
3
   Coded by Takuji Nishimura and Makoto Matsumoto.
4
5
   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
6
   All rights reserved.                          
7
8
   Redistribution and use in source and binary forms, with or without
9
   modification, are permitted provided that the following conditions
10
   are met:
11
12
     1. Redistributions of source code must retain the above copyright
13
        notice, this list of conditions and the following disclaimer.
14
15
     2. Redistributions in binary form must reproduce the above copyright
16
        notice, this list of conditions and the following disclaimer in the
17
        documentation and/or other materials provided with the distribution.
18
19
     3. The names of its contributors may not be used to endorse or promote 
20
        products derived from this software without specific prior written 
21
        permission.
22
23
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35
36
   Any feedback is very welcome.
37
   http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
38
   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
39
*/
40
41
#include <assert.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include "mutator_aux.h"
45
46
#define init_genrand prng_init
47
#define genrand_int32 prng_uint32
48
49
/* Period parameters */  
50
15.8M
#define N 624
51
7.53M
#define M 397
52
1.57M
#define MATRIX_A 0x9908b0dfUL   /* constant vector a */
53
5.51M
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
54
5.51M
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
55
56
int prng_up = 0;
57
static unsigned long mt[N]; /* the array for the state vector  */
58
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
59
60
/* initializes mt[N] with a seed */
61
void init_genrand(unsigned long s)
62
8.34k
{
63
8.34k
    mt[0]= s & 0xffffffffUL;
64
5.20M
    for (mti=1; mti<N; mti++) {
65
5.19M
        mt[mti] = 
66
5.19M
            (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) +
67
5.19M
              (unsigned long)mti); 
68
5.19M
        /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
69
5.19M
        /* In the previous versions, MSBs of the seed affect   */
70
5.19M
        /* only MSBs of the array mt[].                        */
71
5.19M
        /* 2002/01/09 modified by Makoto Matsumoto             */
72
5.19M
        mt[mti] &= 0xffffffffUL;
73
5.19M
        /* for >32 bit machines */
74
5.19M
    }
75
8.34k
    prng_up = 1;
76
8.34k
}
77
78
/* generates a random number on [0,0xffffffff]-interval */
79
unsigned long genrand_int32(void)
80
1.57M
{
81
1.57M
    unsigned long y;
82
1.57M
    static unsigned long mag01[2]={0x0UL, MATRIX_A};
83
1.57M
    /* mag01[x] = x * MATRIX_A  for x=0,1 */
84
1.57M
85
1.57M
    if (mti >= N) { /* generate N words at one time */
86
8.84k
        int kk;
87
8.84k
88
8.84k
        assert(mti != N+1);
89
8.84k
90
2.01M
        for (kk=0;kk<N-M;kk++) {
91
2.00M
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
92
2.00M
            mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
93
2.00M
        }
94
3.51M
        for (;kk<N-1;kk++) {
95
3.50M
            y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
96
3.50M
            mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
97
3.50M
        }
98
8.84k
        y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
99
8.84k
        mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
100
8.84k
101
8.84k
        mti = 0;
102
8.84k
    }
103
1.57M
  
104
1.57M
    y = mt[mti++];
105
1.57M
106
1.57M
    /* Tempering */
107
1.57M
    y ^= (y >> 11);
108
1.57M
    y ^= (y << 7) & 0x9d2c5680UL;
109
1.57M
    y ^= (y << 15) & 0xefc60000UL;
110
1.57M
    y ^= (y >> 18);
111
1.57M
112
1.57M
    return y;
113
1.57M
}