/
main.ts
79 lines (64 loc) · 1.27 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
Secret,
_wyp,
make_secret,
wy2gau,
wy2u01,
wy2u0k,
wyhash,
wyrand,
wytrand,
} from "./internal";
export * as internal from "./internal";
const textEncoder = new TextEncoder();
export class Wyhash {
constructor(
private readonly seed: bigint,
private readonly secret: Secret = _wyp,
) {}
static makeSecret(seed: bigint) {
return make_secret(seed);
}
hash(data: string | Uint8Array) {
if (typeof data == "string") {
data = textEncoder.encode(data);
}
return wyhash(data, this.seed, this.secret);
}
}
export abstract class WyRNG {
abstract next(): bigint;
next01() {
return wy2u01(this.next());
}
nextGaussian() {
return wy2gau(this.next());
}
nextRange(minOrMax: bigint, max?: bigint) {
if (max == undefined) {
max = minOrMax;
minOrMax = 0n;
}
return minOrMax + wy2u0k(this.next(), max);
}
}
export class Wyrand extends WyRNG {
private s: { state: bigint };
constructor(seed: bigint) {
super();
this.s = { state: seed };
}
next() {
return wyrand(this.s);
}
}
export class Wytrand extends WyRNG {
private s: { state: bigint };
constructor(seed: bigint) {
super();
this.s = { state: seed };
}
next() {
return wytrand(this.s);
}
}