Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.01 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "mat1 = 0x8f7011ee\n",
  12. "mat2 = 0xfc78ff1f\n",
  13. "tmat = 0x3793fdff"
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": 2,
  19. "metadata": {
  20. "collapsed": false
  21. },
  22. "outputs": [],
  23. "source": [
  24. "F2 = GF(2)\n",
  25. "\n",
  26. "def int_to_f2(x):\n",
  27. " return vector([F2((x >> i) & 1) for i in range(32)])\n",
  28. "\n",
  29. "def ints_to_f2(xs):\n",
  30. " return vector([F2((xs[floor(i / 32)] >> (i % 32)) & 1) for i in range(32*len(xs))])\n",
  31. "\n",
  32. "def f2_to_int(vec):\n",
  33. " x = 0\n",
  34. " for i in range(32):\n",
  35. " x |= Integer(vec[i]) << i\n",
  36. " return x\n",
  37. "\n",
  38. "def f2_to_ints(vec):\n",
  39. " return [f2_to_int(vec[i*32:i*32+32]) for i in range(floor(len(vec) / 32))]"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 3,
  45. "metadata": {
  46. "collapsed": false
  47. },
  48. "outputs": [],
  49. "source": [
  50. "def join_matrix_yoko(mats):\n",
  51. " m = mats[0].nrows()\n",
  52. " n = mats[0].ncols()\n",
  53. " return matrix([[mats[floor(j / n)][i, j % n] for j in range(n*len(mats))] for i in range(m)])\n",
  54. "\n",
  55. "def join_matrix_tate(mats):\n",
  56. " m = mats[0].nrows()\n",
  57. " n = mats[0].ncols()\n",
  58. " return matrix([[mats[floor(i / m)][i % m, j] for j in range(n)] for i in range(m*len(mats))])"
  59. ]
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 4,
  64. "metadata": {
  65. "collapsed": false
  66. },
  67. "outputs": [],
  68. "source": [
  69. "def elem_matrix(i):\n",
  70. " mats = [Mat(GF(2), 32, 32).zero() for j in range(4)]\n",
  71. " mats[i] = Mat(GF(2), 32, 32).identity_matrix()\n",
  72. " return join_matrix_yoko(mats)\n",
  73. "\n",
  74. "def xi(x):\n",
  75. " if x:\n",
  76. " return 1\n",
  77. " else:\n",
  78. " return 0\n",
  79. "\n",
  80. "def rshift_matrix(k):\n",
  81. " return matrix([[F2(xi(j - i == k)) for j in range(32)] for i in range(32)])\n",
  82. "\n",
  83. "def lshift_matrix(k):\n",
  84. " return rshift_matrix(-k)\n",
  85. "\n",
  86. "def constant_matrix(a):\n",
  87. " m = [[F2(0) for j in range(32)] for i in range(32)]\n",
  88. " for i in range(32):\n",
  89. " m[i][0] = a[i]\n",
  90. " return matrix(m)\n",
  91. "\n",
  92. "def mask_matrix(a):\n",
  93. " return matrix([[F2(a[i] * xi(i == j)) for j in range(32)] for i in range(32)])"
  94. ]
  95. },
  96. {
  97. "cell_type": "code",
  98. "execution_count": 5,
  99. "metadata": {
  100. "collapsed": false
  101. },
  102. "outputs": [],
  103. "source": [
  104. "def temper_matrix():\n",
  105. " t0 = elem_matrix(3)\n",
  106. " t1 = elem_matrix(0) + rshift_matrix(8) * elem_matrix(2)\n",
  107. " return t0 + t1 + constant_matrix(int_to_f2(tmat)) * t1\n",
  108. "\n",
  109. "temper = temper_matrix()"
  110. ]
  111. },
  112. {
  113. "cell_type": "code",
  114. "execution_count": 6,
  115. "metadata": {
  116. "collapsed": false
  117. },
  118. "outputs": [],
  119. "source": [
  120. "def next_state_matrix():\n",
  121. " y = elem_matrix(3)\n",
  122. " x = mask_matrix(int_to_f2(0x7fffffff)) * elem_matrix(0) + elem_matrix(1) + elem_matrix(2)\n",
  123. " x += lshift_matrix(1) * x\n",
  124. " y += rshift_matrix(1) * y + x\n",
  125. " st0 = elem_matrix(1) + constant_matrix(int_to_f2(mat1)) * y\n",
  126. " st1 = elem_matrix(2) + constant_matrix(int_to_f2(mat2)) * y\n",
  127. " st2 = x + lshift_matrix(10) * y\n",
  128. " st3 = y\n",
  129. " return join_matrix_tate([st0, st1, st2, st3])\n",
  130. "\n",
  131. "next_state = next_state_matrix()"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 7,
  137. "metadata": {
  138. "collapsed": true
  139. },
  140. "outputs": [],
  141. "source": [
  142. "def genrand_matrix():\n",
  143. " r0 = temper_matrix()\n",
  144. " r1 = temper_matrix() * next_state_matrix()\n",
  145. " r2 = temper_matrix() * next_state_matrix() * next_state_matrix()\n",
  146. " r3 = temper_matrix() * next_state_matrix() * next_state_matrix() * next_state_matrix()\n",
  147. " return join_matrix_tate([r0, r1, r2, r3])\n",
  148. "\n",
  149. "genrand = genrand_matrix()"
  150. ]
  151. },
  152. {
  153. "cell_type": "code",
  154. "execution_count": 8,
  155. "metadata": {
  156. "collapsed": false
  157. },
  158. "outputs": [
  159. {
  160. "data": {
  161. "text/plain": [
  162. "['5740a11a', '3cfe1de3', 'a5083b98', '14ef46dc']"
  163. ]
  164. },
  165. "execution_count": 8,
  166. "metadata": {},
  167. "output_type": "execute_result"
  168. }
  169. ],
  170. "source": [
  171. "[x.str(16) for x in f2_to_ints(next_state * ints_to_f2([0x63B07A71, 0x5740A11A, 0x3CFE1DE3, 0x08A80987]))]"
  172. ]
  173. },
  174. {
  175. "cell_type": "code",
  176. "execution_count": 9,
  177. "metadata": {
  178. "collapsed": false
  179. },
  180. "outputs": [
  181. {
  182. "ename": "ValueError",
  183. "evalue": "matrix equation has no solutions",
  184. "output_type": "error",
  185. "traceback": [
  186. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  187. "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
  188. "\u001b[0;32m<ipython-input-9-1043d1e321cd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgenrand\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve_right\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mints_to_f2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0xd86e392e\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0xddb8564b\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0x44559e7f\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mInteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0x74a35ddf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  189. "\u001b[0;32m/projects/sage/sage-7.4/src/sage/matrix/matrix2.pyx\u001b[0m in \u001b[0;36msage.matrix.matrix2.Matrix.solve_right (/projects/sage/sage-7.4/src/build/cythonized/sage/matrix/matrix2.c:7000)\u001b[0;34m()\u001b[0m\n\u001b[1;32m 402\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrank\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnrows\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 404\u001b[0;31m \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_solve_right_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcheck\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcheck\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 405\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 406\u001b[0m \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_solve_right_nonsingular_square\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcheck_rank\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  190. "\u001b[0;32m/projects/sage/sage-7.4/src/sage/matrix/matrix2.pyx\u001b[0m in \u001b[0;36msage.matrix.matrix2.Matrix._solve_right_general (/projects/sage/sage-7.4/src/build/cythonized/sage/matrix/matrix2.c:8147)\u001b[0;34m()\u001b[0m\n\u001b[1;32m 520\u001b[0m \u001b[0;31m# Have to check that we actually solved the equation.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 521\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mX\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 522\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"matrix equation has no solutions\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 523\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 524\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  191. "\u001b[0;31mValueError\u001b[0m: matrix equation has no solutions"
  192. ]
  193. }
  194. ],
  195. "source": [
  196. "genrand.solve_right(ints_to_f2([0xd86e392e, 0xddb8564b, 0x44559e7f, 0x74a35ddf]))"
  197. ]
  198. },
  199. {
  200. "cell_type": "markdown",
  201. "metadata": {},
  202. "source": [
  203. "hoge $x$."
  204. ]
  205. }
  206. ],
  207. "metadata": {
  208. "kernelspec": {
  209. "display_name": "SageMath 7.4",
  210. "language": "",
  211. "name": "sage-7.4"
  212. },
  213. "language_info": {
  214. "codemirror_mode": {
  215. "name": "ipython",
  216. "version": 2
  217. },
  218. "file_extension": ".py",
  219. "mimetype": "text/x-python",
  220. "name": "python",
  221. "nbconvert_exporter": "python",
  222. "pygments_lexer": "ipython2",
  223. "version": "2.7.10"
  224. }
  225. },
  226. "nbformat": 4,
  227. "nbformat_minor": 0
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement