Formal Verification and Code-Generation of Mersenne ...

19
Formal Verification and Code- Generation of Mersenne- Twister Algorithm About Mersenne- Twister Overview of the project Presentations and equivalence Irreducibility implies Long Period Inversive- decimation Code generation Conclusion and future work Formal Verification and Code-Generation of Mersenne-Twister Algorithm Takafumi Saikawa and Kazunari Tanaka and Kensaku Tanaka December 7, 2020 1 / 19

Transcript of Formal Verification and Code-Generation of Mersenne ...

Page 1: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Formal Verification and Code-Generation ofMersenne-Twister Algorithm

Takafumi Saikawa and Kazunari Tanaka and KensakuTanaka

December 7, 2020

1 / 19

Page 2: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Mersenne-Twister (Matsumotoand Nishimura, 1998)

Our work is based on the original work by Matsumoto andNishimura:• Mersenne-Twister is a pseudo-random number generator

• Long-period : 219937 − 1• Good stochastic properties, e.g., 623-distribution

• Two presentations: algebraic and pseudocode

• Their equivalence is implicit• Proof of long-period property

• Reduce the property to the irreducibility of a polynomial• Use “inversive-decimation” to show the irreducibility

2 / 19

Page 3: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Overview of the project

• Orange: finished formalizations

• Blue: the last remaining part for the long-period property3 / 19

Page 4: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Linear-algebraic presentation

A =

1

1. . .

1

a0 a1 a2 · · · aw−1

S =

(1r

1w−r

)A

B =

1w1w

. . .

1w 1w. . .

1w−rS

4 / 19

Page 5: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Linear-algebraic presentation

(Lemma mulBE in cycle.v)

xB =

xnwxn−1w

xn−2w...

x2w

x1w

x0w−r

T

1w1w

. . .

1w 1w. . .

1w−rS

=

xmw + x1w

(1r

)A

+(x0w−r 0

)A

xnw . . . x2w x1

w−r

The linear recurrence : xmw + x1

w

(1r

)A +

(x0w−r 0

)A.

5 / 19

Page 6: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Pseudocode presentation

(Definition next_random_state in mt.v)

u := 1..10..0 ; (w-r) ones and r zeroes

ll := 0..01..1 ; (w-r) zeroes and r ones

i := 0

xx[0],..,xx[n-1] := "initial words, not all-zero"

LOOP:

y := (xx[i] AND u) OR (xx[(i+1) mod n] AND ll)

xx[i] := xx[(i+m) mod n] XOR (y >> 1)

XOR (if LSB(y) = 0 then 0 else aa)

OUTPUT xx[i]

i := (i+1) mod n

GOTO LOOP

6 / 19

Page 7: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Pseudocode presentation

7 / 19

Page 8: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Equivalence and data structures

Fnw−r2 × N<n

state_of_array

〈x , i〉7→〈xB, (i+1) modn〉

array_of_state

next_random_state

• Fnw−r2 × N<n 3 〈x , i〉 is a pair of a state vector and the

number of multiplications by B• state_of_array and array_of_state are inverses to

each other.• The equivalence (Lemma next_random_stateE): for any state σ,

next_random_state(σ) =

state_of_array((array_of_state(σ))B)8 / 19

Page 9: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Data structures in CoqFnw−r

2 × N<n:

Record vector_with_counter :=

{

vector ∈ F nw−r2 ;

counter ∈ N;

_ : counter < n;

}.

:

Record valid_random_state :=

{

〈σ, k〉 ∈ (list N) ×N;

_ : size (σ) == n;

_ : k < n;

_ : ∀i < n, i < size(σ)⇒ σ[i ] < 2w;

_ : The lower r bits of σ[k] are 0;}.

9 / 19

Page 10: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Irreducibility implies Long-period

Lemma (irreducibleP)

Let x ∈ F2[X ]/ϕ(X ). If we assume x2 6= x , the following areequivalent.

1 ϕ(X ) is irreducible (i.e. primitive).

2 X 2 6≡ϕ(X ) X and X 2nw−r ≡ϕ(X ) X .

Lemma (cycleB_dvdP)

Assume that the characteristic polynomialϕB(X )(= det(XI − B)) of B is irreducible. Then for anyq ∈ N>0, the following are equivalent.

1 Bq = B.

2 q − 1 is divided by 2nw−r − 1.

Lemma (pm)

2624∗32−31 − 1 = 219937 − 1 is a prime.10 / 19

Page 11: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Proof technique: Quotientstructure

F2[X ] F2[X ]/ϕB(X )

π

repr

• We need to deal with the quotient of the polynomial ringF2[X ] by the ideal (ϕB(X )).

• MathComp provides the construction of quotient ringsfor given ideals.

• We want further structures: of vector space and field.

11 / 19

Page 12: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Proof technique: Quotientstructure

F2[X ] F2[X ]/ϕB(X )

π

repr

We had to prove algebraic facts in addition to MathComp,e.g.:• Lemma pi_linear : the canonical surjectionF2[X ]

π−→ F2[X ]/ϕB(X ) is linear.• Lemma QphiI_field : the quotient F2[X ]/ϕB(X ) is a

field.• Lemma QphiIX_full and Lemma QphiIX_free :F2[X ]/ϕB(X ) as a vector space has 1,X ,X 2, . . . ,X nw−r

as its basis.• Constructivist’s note: the explicit form of an inverse

element is given by Euclidean algorithm.12 / 19

Page 13: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Inversive-decimation

13 / 19

Page 14: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Inversive-decimation

14 / 19

Page 15: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Inversive-decimation

15 / 19

Page 16: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Inversive-decimation

16 / 19

Page 17: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Inversive-decimation

• Remaining tasks:• Infinite-dimensional vector space• Binding the algorithm and the proof• The current version algorithm is not practical in Coq.

17 / 19

Page 18: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Code Generation

• Mersenne-Twister algorithm consists of binary arithmeticoperations.

• BinNat library → a word of C.

• N.lxor → ˆ (lxor operation)

• N.succ → + 1

18 / 19

Page 19: Formal Verification and Code-Generation of Mersenne ...

FormalVerificationand Code-

Generation ofMersenne-

TwisterAlgorithm

AboutMersenne-Twister

Overview ofthe project

Presentationsandequivalence

Irreducibilityimplies LongPeriod

Inversive-decimation

Codegeneration

Conclusionand futurework

Conclusion and future work

• Our next plan is the Blue part, completing the long-period.• The Red parts are future directions. 19 / 19