|
| 1 | +# Copyright 2013 David Eisenstat |
| 2 | +""" |
| 3 | +Computes the tempering matrix from the MT19937 pseudorandom number generator. |
| 4 | +
|
| 5 | +>>> from cracking_rand import * |
| 6 | +>>> import random |
| 7 | +>>> prng = random.Random() |
| 8 | +>>> n = 624 |
| 9 | +>>> data = [prng.getrandbits(32) for k in range(n)] |
| 10 | +>>> seed = prng.getstate()[1][:n] |
| 11 | +>>> T = tempering_mat() |
| 12 | +>>> data == [int_from_vec(T * vec_from_int(x)) for x in seed] |
| 13 | +True |
| 14 | +""" |
| 15 | + |
| 16 | + |
| 17 | +from mat import Mat |
| 18 | +from vec import Vec |
| 19 | + |
| 20 | +#This module should use GF2.one but doesn't yet |
| 21 | + |
| 22 | +one = 1 |
| 23 | + |
| 24 | + |
| 25 | +def format_bit(b): |
| 26 | + """ |
| 27 | + Converts a bit to a string. |
| 28 | +
|
| 29 | + >>> format_bit(0) |
| 30 | + '0' |
| 31 | + >>> format_bit(one) |
| 32 | + '1' |
| 33 | + """ |
| 34 | + return '0' if b == 0 else '1' |
| 35 | + |
| 36 | + |
| 37 | +def print_vec(v): |
| 38 | + """ |
| 39 | + Prints a bit vector compactly. |
| 40 | +
|
| 41 | + >>> print_vec(Vec({0, 1, 2, 3, 4, 5, 6, 7}, {1: one, 3: one, 5: one})) |
| 42 | + 01010100 |
| 43 | + >>> print_vec(Vec({0, 1, 2, 3}, {0: one, 2: one, 3: one})) |
| 44 | + 1011 |
| 45 | + """ |
| 46 | + print(''.join(format_bit(v[k]) for k in sorted(v.D))) |
| 47 | + |
| 48 | + |
| 49 | +def print_mat(m): |
| 50 | + """ |
| 51 | + Prints a bit matrix compactly. |
| 52 | +
|
| 53 | + >>> print_mat(Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): one})) |
| 54 | + 010 |
| 55 | + 001 |
| 56 | + 000 |
| 57 | + """ |
| 58 | + D0, D1 = map(sorted, m.D) |
| 59 | + for i in D0: |
| 60 | + print(''.join(format_bit(m[(i, j)]) for j in D1)) |
| 61 | + |
| 62 | + |
| 63 | +def vec_from_int(m, n=32): |
| 64 | + """ |
| 65 | + Returns the n-bit vector specified by the integer m. |
| 66 | +
|
| 67 | + >>> print_vec(vec_from_int(0b00101010, 8)) |
| 68 | + 01010100 |
| 69 | + >>> print_vec(vec_from_int(0b1101, 4)) |
| 70 | + 1011 |
| 71 | + """ |
| 72 | + return Vec(set(range(n)), {k: one for k in range(n) if (2 ** k) & m}) |
| 73 | + |
| 74 | + |
| 75 | +def int_from_vec(v): |
| 76 | + """ |
| 77 | + Returns the integer specified by the bit vector v. |
| 78 | +
|
| 79 | + >>> int_from_vec(vec_from_int(42, 8)) |
| 80 | + 42 |
| 81 | + >>> int_from_vec(vec_from_int(13, 4)) |
| 82 | + 13 |
| 83 | + """ |
| 84 | + # TODO(david): use GF2 |
| 85 | + return sum(2 ** k for k in v.D if v[k] & 1) |
| 86 | + |
| 87 | + |
| 88 | +def left_shift(k, n=32): |
| 89 | + """ |
| 90 | + Returns the n*n matrix corresponding to the operation |
| 91 | +
|
| 92 | + lambda v: vec_from_int(int_from_vec(v) << k, n) |
| 93 | +
|
| 94 | + >>> print_mat(left_shift(2, 6)) |
| 95 | + 000000 |
| 96 | + 000000 |
| 97 | + 100000 |
| 98 | + 010000 |
| 99 | + 001000 |
| 100 | + 000100 |
| 101 | + >>> int_from_vec(left_shift(2) * vec_from_int(42)) == 42 << 2 |
| 102 | + True |
| 103 | + """ |
| 104 | + D = set(range(n)) |
| 105 | + return Mat((D, D), {(j + k, j): one for j in range(n - k)}) |
| 106 | + |
| 107 | + |
| 108 | +def right_shift(k, n=32): |
| 109 | + """ |
| 110 | + Returns the n*n matrix corresponding to the operation |
| 111 | +
|
| 112 | + lambda v: vec_from_int(int_from_vec(v) >> k, n) |
| 113 | +
|
| 114 | + >>> print_mat(right_shift(1, 4)) |
| 115 | + 0100 |
| 116 | + 0010 |
| 117 | + 0001 |
| 118 | + 0000 |
| 119 | + >>> int_from_vec(right_shift(1) * vec_from_int(13)) == 13 >> 1 |
| 120 | + True |
| 121 | + """ |
| 122 | + D = set(range(n)) |
| 123 | + return Mat((D, D), {(i, i + k): one for i in range(n - k)}) |
| 124 | + |
| 125 | + |
| 126 | +def diag(v): |
| 127 | + """ |
| 128 | + Returns the diagonal matrix specified by the vector v. |
| 129 | +
|
| 130 | + >>> print_mat(diag(vec_from_int(13, 4))) |
| 131 | + 1000 |
| 132 | + 0000 |
| 133 | + 0010 |
| 134 | + 0001 |
| 135 | + """ |
| 136 | + return Mat((v.D, v.D), {(k, k): v[k] for k in v.D}) |
| 137 | + |
| 138 | + |
| 139 | +def bitwise_and(m, n=32): |
| 140 | + """ |
| 141 | + Returns the matrix for masking an n-bit vector by the integer m. |
| 142 | +
|
| 143 | + >>> print_mat(bitwise_and(13, 4)) |
| 144 | + 1000 |
| 145 | + 0000 |
| 146 | + 0010 |
| 147 | + 0001 |
| 148 | + """ |
| 149 | + return diag(vec_from_int(m, n)) |
| 150 | + |
| 151 | + |
| 152 | +def identity_mat(n=32): |
| 153 | + """ |
| 154 | + Returns the n*n identity matrix. |
| 155 | +
|
| 156 | + >>> print_mat(identity_mat(4)) |
| 157 | + 1000 |
| 158 | + 0100 |
| 159 | + 0010 |
| 160 | + 0001 |
| 161 | + """ |
| 162 | + D = set(range(n)) |
| 163 | + return Mat((D, D), {(k, k): one for k in range(n)}) |
| 164 | + |
| 165 | + |
| 166 | +def tempering_mat(): |
| 167 | + """ |
| 168 | + Returns the matrix corresponding to the MT19937 tempering transform. |
| 169 | +
|
| 170 | + >>> print_mat(tempering_mat()) |
| 171 | + 10010000000100100010001000000100 |
| 172 | + 01000000000010000001000100000010 |
| 173 | + 00100000000001000000100000000001 |
| 174 | + 00010000000000100000010001000000 |
| 175 | + 10001001000100010010001000000000 |
| 176 | + 00000100100000001001000100000000 |
| 177 | + 00100010010001000100100010001000 |
| 178 | + 10010001001100100010010001000000 |
| 179 | + 00000000100100000001001000100010 |
| 180 | + 00100100010011001000100100010001 |
| 181 | + 00010000001000100000010000001000 |
| 182 | + 00000001000100100010001001000100 |
| 183 | + 00000100000010011000000100100010 |
| 184 | + 00000000000001001000000010010001 |
| 185 | + 00000001000000100010000001000000 |
| 186 | + 00000000000000010000000000100000 |
| 187 | + 00000000000000001000000000010000 |
| 188 | + 00100000000001000100000000001000 |
| 189 | + 00010000000100100010001000000100 |
| 190 | + 00000000000010000001000100000010 |
| 191 | + 00000000000000000000100000000001 |
| 192 | + 00000000000000100000010001000000 |
| 193 | + 10000001000100000010001000000000 |
| 194 | + 00000000100000000001000100000000 |
| 195 | + 00100000010001000100100010001000 |
| 196 | + 00010000001000100000010001000000 |
| 197 | + 00000000000100000001001000100010 |
| 198 | + 00000100000010001000100100010001 |
| 199 | + 00000000000000000000010000001000 |
| 200 | + 00000001000000100010000001000100 |
| 201 | + 00000000000000010000000000100010 |
| 202 | + 00000000000000001000000010010001 |
| 203 | + """ |
| 204 | + m = identity_mat() |
| 205 | + m += right_shift(11) * m |
| 206 | + m += bitwise_and(0x9d2c5680) * left_shift(7) * m |
| 207 | + m += bitwise_and(0xefc60000) * left_shift(15) * m |
| 208 | + m += right_shift(18) * m |
| 209 | + return m |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == '__main__': |
| 213 | + from doctest import testmod |
| 214 | + testmod() |
0 commit comments