夜綱
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # xoroshiro128+の状態遷移の逆算の考察 ## はじめに この記事は[Pokémon Past Generation Advent Calender 2019](https://adventar.org/calendars/4435) 一日目の記事です. ## なんぞこれ? - 某所で話題になっている疑似乱数生成器"xoroshiro128+"の状態遷移の逆関数を求めてみようという趣旨の記事です. ## 逆関数が求まると何がうれしいの? - 初期seedを逆算したりできて色々と便利. - 線形代数の計算を実用的な場面で行えるので楽しい(過程が). ## xoroshiroの状態遷移の概要 - xoroshiroは"xor","rotate","shift","rotate"の略らしいです. xorshiftの改良版だとか. - xorとビットシフトは乱数調整で遊んでいる人ならよく知っているはずなので省略しますが, rotateはあんまりなじみがないかもしれません. 普通のビットシフトは端からあふれ出たビットは捨てられますが, rotateは逆の端からニュッと出てきます. - `1011`を左に2bit shiftをかけると`1100`になりますが, rotateだと`1110`になります. - xoroshito128+は128bitの内部状態を64bit×2で持ち, 64bitの乱数値を返してくれます. - 内部状態を64bit整数`X`,`Y`で持っているとすると, `X+Y`で乱数を取得します. MTみたいなtemperingは無い様子. - 状態遷移は以下の通りの処理で行われます. ```cpp= X = rotl(X, 24) ^ (X^Y) ^ ((X^Y) << 16) Y = rotl(X^Y, 37) ``` ## 方針 - [これ](http://yatsuna-827.hatenadiary.com/entry/2017/12/26/050359)と全く一緒. xorとshiftしか使ってない乱数生成器なら全部同じ方針で解けると思います. ## 実際に計算してみる (以下常体) `rotl(X,n)` =: $R_l(n) \in M_{64}({\bf F}_2),$ `<<16` =: $L_{16} \in M_{64}({\bf F}_2)$とおいて, $x_n,y_n \in {\bf F}_2^{64}$を内部状態`X,Y`とする. $\begin{align} \begin{bmatrix} x_{n+1} \\ y_{n+1} \end{bmatrix} &= \begin{bmatrix} R_l(24)x_n + x_n+y_n+L_{16}(x_n+y_n)\\ R_l(37)(x_y+y_n) \end{bmatrix} \\ &= \begin{bmatrix} (R_l(24)+E+L_{16})x_n+(E+L_{16})y_n\\ R_l(37)x_y+R_l(37)y_n \end{bmatrix} \\ &= \begin{bmatrix} R_l(24)+E+L_{16} & E+L_{16} \\ R_l(37) & R_l(37) \end{bmatrix} \begin{bmatrix} x_n \\ y_n \end{bmatrix}. \end{align}$ $R_l(n) \in GL_{64}({\bf F}_2)$で, $R_l(n)^{-1} = R_l(64-n)$であり, $L'_{16}:=E+L_{16}$についても, ${\bf F}_2^{64}$においては$b=a+(a<<n) \iff a=b+(a<<n)$であることに注意すると, $x,y$の第 $i$ bitをそれぞれ$x_i,y_i$とするとき $x_i = \begin{cases} y_i \quad (1\leq i<n) \\ y_i+x_{i-n} \quad (n \leq i \leq 64) \end{cases}$ であることから, 可逆であることがわかる. あとは頑張れば逆行列が出る. 簡単のため$R_l(24)=A, L'_{16}=B, R_l(37)=C$とおくと, $\begin{bmatrix} x_{n+1} \\ y_{n+1} \end{bmatrix}= \begin{bmatrix} A+B & B \\ C & C \end{bmatrix}\begin{bmatrix}x_n \\ y_n \end{bmatrix}$ と書けてとてもうれしい. これの逆行列を求める. $\begin{bmatrix} A+B & B \\ C & C \end{bmatrix}\begin{bmatrix} X_1 & X_2 \\ X_3 & X_4 \end{bmatrix}= \begin{bmatrix} E & 0 \\ 0 & E \end{bmatrix}$ とすると, $\begin{align} (A+B)X_1+BX_3 &= E\\ (A+B)X_2+BX_4 &= 0\\ CX_1+CX_3 &= 0\\ CX_2+CX_4 &= E \end{align}$ の4本の式が立つ. $A,B,C \in M_{64}({\bf F}_2)$なので$-X=X$であることと, 乗法が非可換であることに注意してこれを解いていく. 下の2本の式より $X_3=X_1 \\ X_4=C^{-1}+X_1.$ これを上の2本にそれぞれ代入していく. まず1本目から $\begin{align} (A+B)X_1+BX_1 &= E \\ AX_1+BX_1+BX_1 &= E \\ AX_1 &= E \\ X_1 &= A^{-1}. \end{align}$ よって, $X_1=X_3=A^{-1}.$ 次に2本目から $\begin{align} (A+B)X_2+B(C^{-1}+X_2) &= 0 \\ AX_2+BX_2+BC^{-1}+BX_2 &= 0 \\ AX_2+BC^{-1} &= 0 \\ AX_2 &= BC^{-1} \\ X_2 &= A^{-1}BC^{-1}. \end{align}$ よって$X_2=A^{-1}BC^{-1}, X_4 = A^{-1}BC^{-1}+C^{-1}.$ 以上から, $\begin{align} \begin{bmatrix} A+B & B \\ C & C \end{bmatrix}^{-1} &= \begin{bmatrix} A^{-1} & A^{-1}BC^{-1} \\ A^{-1} & A^{-1}BC^{-1}+C^{-1} \end{bmatrix} \\ &= \begin{bmatrix} R_l(24)^{-1} & R_l(24)^{-1}L'_{16}R_l(37)^{-1} \\ R_l(24)^{-1} & R_l(24)^{-1}L'_{16}R_l(37)^{-1}+R_l(37)^{-1} \end{bmatrix} \\ &= \begin{bmatrix} R_l(40) & R_l(40)L'_{16}R_l(27) \\ R_l(40) & R_l(40)L'_{16}R_l(27)+R_l(27) \end{bmatrix}. \end{align}$ ($L'_{16}$の可逆性必要なかった……) すなわち, $\begin{bmatrix} x_n \\ y_n \end{bmatrix}= \begin{bmatrix} R_l(40) & R_l(40)L'_{16}R_l(27) \\ R_l(40) & R_l(40)L'_{16}R_l(27)+R_l(27) \end{bmatrix}\begin{bmatrix}x_{n+1} \\ y_{n+1} \end{bmatrix}$ $\begin{align} x_n &= R_l(40)x_{n+1}+R_l(40)(E+L_{16})R_l(27)y_{n+1}\\ &= R_l(40)x_{n+1}+R_l(3)y_{n+1}+R_l(40)L_{16}R_l(27)y_{n+1} \\ y_n &= R_l(40)x_{n+1}+R_l(40)(E+L_{16})R_l(27)y_{n+1}+R_l(27)y_{n+1}\\ &= R_l(40)x_{n+1}+R_l(3)y_{n+1}+R_l(40)L_{16}R_l(27)y_{n+1}+R_l(27)y_{n+1}. \end{align}$ 以上から, ```cpp= X = rotl(X,40) ^ rotl(Y,3) ^ rotl((rotl(Y,27)<<16),40) Y = rotl(X,40) ^ rotl(Y,3) ^ rotl((rotl(Y,27)<<16),40) ^ rotl(Y,27) ``` で逆算ができる. ![](https://i.imgur.com/7X6PN01.png) (https://gist.github.com/yatsuna827/ea985d2ce4d34da1e35ec4adf1862050) ## さいごに 明日も私の記事ですね. まだたくさん空きがあるので参加お待ちしています.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully