spec/math/random.bend
bend-collections-laws-crypto@1.0.0.0 · 150 lines · 23 definitions
1import Base
2import bend-collections-laws-crypto@1.0.0.0/spec/lib/common.bend as C
3import bend-collections-laws-crypto@1.0.0.0/src/math/u64.bend as W
4import bend-collections-laws-crypto@1.0.0.0/src/math/w64.bend as X
5import bend-collections-laws-crypto@1.0.0.0/src/math/f64.bend as F
6import bend-collections-laws-crypto@1.0.0.0/src/math/random/rand.bend as R
7import bend-collections-laws-crypto@1.0.0.0/src/math/random/chacha8.bend as C8
8import bend-collections-laws-crypto@1.0.0.0/src/math/random/chacha8/block.bend as B
9import bend-collections-laws-crypto@1.0.0.0/src/math/random/pcg.bend as P
10import bend-collections-laws-crypto@1.0.0.0/spec/math/w64.bend as SW
11import bend-collections-laws-crypto@1.0.0.0/spec/math/f64.bend as SF
12import bend-collections-laws-crypto@1.0.0.0/spec/math/random/source.bend as SRC
13import bend-collections-laws-crypto@1.0.0.0/spec/math/random/chacha8rand.bend as SC
14import bend-collections-laws-crypto@1.0.0.0/spec/math/random/pcg.bend as SP
15import bend-collections-laws-crypto@1.0.0.0/spec/math/random/rand.bend as SR
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63def map_outputs(+n: Nat, m: Maybe<&2, C8.ChaCha8>) -> Maybe<&2, List<&2, W.U64>>:
64 match m:
65 case None{}:
66 None{}
67 case Some{g}:
68 Some{SRC.outputs(~C8.ChaCha8, ~C8.next, n, g)}
69
70def map_stream(+n: Nat, m: Maybe<&2, List<&2, U32>>) -> Maybe<&2, List<&2, W.U64>>:
71 match m:
72 case None{}:
73 None{}
74 case Some{kl}:
75 Some{SC.stream(n, kl)}
76
77def ChaCha8.stream(+n: Nat, +k: B.Key) -> Type:
78 {SRC.outputs(~C8.ChaCha8, ~C8.next, n, C8.of_key(k)) == SC.stream(n, SC.key_list(k)) : List<&2, W.U64>}
79
80def ChaCha8.seeded(+n: Nat, +seed: List<&2, U32>) -> Type:
81 {map_outputs(n, C8.new(seed)) == map_stream(n, SC.key_of_seed(seed)) : Maybe<&2, List<&2, W.U64>>}
82
83def value128(p: P.PCG) -> Nat:
84 match p:
85 case P.P{hi, lo}:
86 Nat.add(SW.value(lo), C.shift(64n, SW.value(hi)))
87
88def PCG.step(+mh: W.U64, +ml: W.U64, +ih: W.U64, +il: W.U64, +p: P.PCG) -> Type:
89 {value128(P.step_with(mh, ml, ih, il, p)) == SP.lcg(value128(P.P{mh, ml}), value128(P.P{ih, il}), value128(p)) : Nat}
90
91
92
93
94def PCG.output(+cm: W.U64, +hi: W.U64, +lo: W.U64, +t: Nat, +ht: {t == SP.dxsm1(SW.value(cm), SW.value(hi)) : Nat}) -> Type:
95 {SW.value(P.dxsm_with(cm, hi, lo)) == SP.dxsm3(t, SW.value(lo)) : Nat}
96
97def PCG.constants(+p: P.PCG) -> Type:
98 {P.next(p) == P.out(P.step_with(P.mul_hi(), P.mul_lo(), P.inc_hi(), P.inc_lo(), p)) : W.U64 & P.PCG}
99
100def val_pair(-S: Data, r: W.U64 & S) -> Nat & S:
101 (x, s) = r
102 (SW.value(x), s)
103
104def Uint64n.value(~S: Data, ~next: S -> W.U64 & S, +s: S, +n: W.U64) -> Type:
105 {val_pair(S, R.uint64n(~S, ~next, s, n)) == SR.below(~S, ~next, 127n, SW.value(n), s) : Nat & S}
106
107def Uint64n.lt(~S: Data, ~next: S -> W.U64 & S, +s: S, +n: W.U64, +hn: {X.is_zero(n) == False{} : Bool}) -> Type:
108 {Nat.is_lt(SW.value(R.fst(W.U64, S, R.uint64n(~S, ~next, s, n))), SW.value(n)) == True{} : Bool}
109
110def Lemire.unbiased(+w: Nat, +n: Nat, +k: Nat, +hn: {Nat.is_lt(0n, n) == True{} : Bool}, +hw: {C.fits(w, n) == True{} : Bool}, +hk: {Nat.is_lt(k, n) == True{} : Bool}) -> Type:
111 {SR.count(w, n, k, C.shift(w, 1n)) == Nat.div(C.shift(w, 1n), n) : Nat}
112
113def Shuffle.permutation(~A: Data, ~V: Data, ~rel: A -> V -> Bool, ~S: Data, ~next: S -> W.U64 & S, +s: S, +xs: List<&2, A>, +v: V) -> Type:
114 {SR.occurrences(~A, ~V, ~rel, v, R.fst(List<&2, A>, S, R.shuffle(~A, ~S, ~next, s, xs))) == SR.occurrences(~A, ~V, ~rel, v, xs) : Nat}
115
116def Perm.permutation(~S: Data, ~next: S -> W.U64 & S, +s: S, +n: Nat, +i: Nat) -> Type:
117 {SR.occurrences(~Nat, ~Nat, ~Nat.is_eq, i, R.fst(List<&2, Nat>, S, R.perm(~S, ~next, s, n))) == SR.b2n(Nat.is_lt(i, n)) : Nat}
118
119
120
121
122def Float64.value(+x: W.U64, +n: Nat, +hn: {C.low(53n, SW.value(x)) == n : Nat}, +xv: Nat, +hx: {Nat.sub(SF.zb(), 53n) == xv : Nat}) -> Type:
123 {R.to_float(x) == SF.round(False{}, n, xv) : F.F64}
124
125def Float64.lt_one(+x: W.U64) -> Type:
126 {F.lt(R.to_float(x), F.one()) == True{} : Bool}
127
128
129
130
131def x0(~S: Data, ~next: S -> W.U64 & S, +s: S) -> Nat:
132 SW.value(SRC.fst64(S, next(s)))
133
134def Uint32.value(~S: Data, ~next: S -> W.U64 & S, +s: S) -> Type:
135 {U32.to_nat(R.fst(U32, S, R.uint32(~S, ~next, s))) == C.high(32n, x0(~S, ~next, s)) : Nat}
136
137def Int64.value(~S: Data, ~next: S -> W.U64 & S, +s: S) -> Type:
138 {SW.value(R.fst(W.U64, S, R.int64(~S, ~next, s))) == C.low(63n, x0(~S, ~next, s)) : Nat}
139
140def Int32.value(~S: Data, ~next: S -> W.U64 & S, +s: S) -> Type:
141 {U32.to_nat(R.fst(U32, S, R.int32(~S, ~next, s))) == C.high(33n, x0(~S, ~next, s)) : Nat}
142
143def Uint32n.lt(~S: Data, ~next: S -> W.U64 & S, +s: S, +n: U32, +hn: {U32.is_zero(n) == False{} : Bool}) -> Type:
144 {Nat.is_lt(U32.to_nat(R.fst(U32, S, R.uint32n(~S, ~next, s, n))), U32.to_nat(n)) == True{} : Bool}
145
146def Intn.lt(~S: Data, ~next: S -> W.U64 & S, +s: S, +n: Nat, +hn: {Nat.is_lt(0n, n) == True{} : Bool}, +hw: {C.fits(64n, n) == True{} : Bool}) -> Type:
147 {Nat.is_lt(R.fst(Nat, S, R.intn(~S, ~next, s, n)), n) == True{} : Bool}
148
149def IntRange.bounds(~S: Data, ~next: S -> W.U64 & S, +s: S, +lo: Nat, +hi: Nat, +h: {Nat.is_lt(lo, hi) == True{} : Bool}, +hw: {C.fits(64n, Nat.sub(hi, lo)) == True{} : Bool}) -> Type:
150 {Bool.and(Nat.is_le(lo, R.fst(Nat, S, R.int_range(~S, ~next, s, lo, hi))), Nat.is_lt(R.fst(Nat, S, R.int_range(~S, ~next, s, lo, hi)), hi)) == True{} : Bool}