@tktb-tess/util-fns 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,4 +18,3 @@ export declare const getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
18
18
  * @returns
19
19
  */
20
20
  export declare const getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
21
- //# sourceMappingURL=baillie-psw.d.ts.map
@@ -0,0 +1,4 @@
1
+ type Brand<in out K extends symbol> = {
2
+ [key in K]: K;
3
+ };
4
+ export default Brand;
@@ -0,0 +1,46 @@
1
+ import { isEqArray, sleep, lazify, parseCSV, getHash } from './util';
2
+ import { getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare } from './math';
3
+ import { default as PCGMinimal } from './pcg-minimal';
4
+ import { bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange } from './baillie-psw';
5
+ import { default as Fraction } from './fraction';
6
+ import { base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI } from './u8arr-ext';
7
+ import { default as Brand } from './brand';
8
+ declare const Util: {
9
+ isEqArray: <T>(arr1: T[], arr2: T[]) => boolean;
10
+ sleep: (delay: number) => Promise<void>;
11
+ lazify: <ArgT extends unknown[], RetT>(func: (...args: ArgT) => RetT) => (...args: ArgT) => () => RetT;
12
+ parseCSV: (csv: string) => string[][];
13
+ getHash: (str: string, algorithm: AlgorithmIdentifier) => Promise<Uint8Array<ArrayBuffer>>;
14
+ };
15
+ declare const AdMath: {
16
+ getRndInt: (min: number, max: number) => number;
17
+ residue: (n: bigint, mod: bigint) => bigint;
18
+ toBigInt: (...nums: number[]) => bigint[];
19
+ getRandBIByBitLength: (length: number, fixed?: boolean) => bigint;
20
+ getRandBIByRange: (min: bigint, max: bigint) => bigint;
21
+ modPow: (base: bigint, power: bigint, mod: bigint) => bigint;
22
+ exEuclidean: (a: bigint, b: bigint) => {
23
+ x: bigint;
24
+ y: bigint;
25
+ gcd: bigint;
26
+ };
27
+ factorial: (n: bigint) => bigint;
28
+ rot32: (value: number, rot: number) => number;
29
+ rot32BI: (value: bigint, rot: bigint) => bigint;
30
+ jacobiSymbol: (a: bigint, n: bigint) => bigint;
31
+ isSquare: (n: bigint) => boolean;
32
+ };
33
+ declare const Prime: {
34
+ bailliePSW: (n: bigint) => boolean;
35
+ getRandPrimeByBitLength: (bitLength: number, fixed?: boolean) => bigint;
36
+ getRandPrimeByRange: (min: bigint, max: bigint) => bigint;
37
+ };
38
+ declare const U8Ext: {
39
+ base64ToU8Arr: (base64: string) => Uint8Array<ArrayBuffer>;
40
+ base64UrlToU8Arr: (base64Url: string) => Uint8Array<ArrayBuffer>;
41
+ u8ArrToBase64: (u8Arr: Uint8Array) => string;
42
+ u8ArrToBase64Url: (u8Arr: Uint8Array) => string;
43
+ BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
44
+ u8ArrToBI: (buf: Uint8Array) => bigint;
45
+ };
46
+ export { isEqArray, sleep, lazify, parseCSV, getHash, getRndInt, residue, toBigInt, getRandBIByBitLength, getRandBIByRange, modPow, exEuclidean, factorial, rot32, rot32BI, jacobiSymbol, isSquare, bailliePSW, getRandPrimeByBitLength, getRandPrimeByRange, PCGMinimal, Fraction, Util, AdMath, Prime, base64ToU8Arr, base64UrlToU8Arr, u8ArrToBase64, u8ArrToBase64Url, BItoU8Arr, u8ArrToBI, U8Ext, type Brand, };
package/dist/bundle.js ADDED
@@ -0,0 +1,492 @@
1
+ const R = new TextEncoder(), x = (n, t) => {
2
+ if (n.length !== t.length) return !1;
3
+ for (let r = 0; r < n.length; r++)
4
+ if (n[r] !== t[r]) return !1;
5
+ return !0;
6
+ }, M = (n) => new Promise((t) => {
7
+ setTimeout(() => {
8
+ t();
9
+ }, n);
10
+ }), T = (n) => (...t) => () => n(...t), L = (n) => {
11
+ const t = [];
12
+ let r = [], e = "", i = !1;
13
+ for (let s = 0; s < n.length; s++) {
14
+ const o = n[s];
15
+ o === '"' && (s === 0 || n[s - 1] !== "\\") ? i = !i : o === "," && !i ? (r.push(e.trim()), e = "") : o === `
16
+ ` && !i ? (r.push(e.trim()), t.push(r), r = [], e = "") : e += o;
17
+ }
18
+ return r.push(e.trim()), t.push(r), t;
19
+ }, _ = async (n, t) => {
20
+ const r = R.encode(n), e = await crypto.subtle.digest(t, r);
21
+ return new Uint8Array(e);
22
+ }, b = typeof process < "u" && typeof process.versions.node < "u", V = (n, t) => Math.floor(Math.random() * (t - n) + n), h = (n, t) => {
23
+ const r = n % t;
24
+ return r < 0n ? r + t : r;
25
+ }, j = (...n) => n.map((t) => BigInt(t)), p = (n, t = !1) => {
26
+ if (!Number.isFinite(n)) throw Error("`length` is not a valid number");
27
+ if (n <= 0) throw Error("`length` must be positive");
28
+ const r = Math.ceil(n / 8), e = crypto.getRandomValues(new Uint8Array(r));
29
+ let i = Array.from(e, (s) => s.toString(2).padStart(8, "0")).join("").slice(0, n);
30
+ return t && (i = i.replace(/^./, "1")), BigInt("0b" + i);
31
+ }, S = (n, t) => {
32
+ if (n >= t) throw Error("rangeError");
33
+ const r = t - n, e = r.toString(2).length, i = (() => {
34
+ for (let o = 0; o < 1e5; o++) {
35
+ const c = p(e);
36
+ if (c >= d(2n, BigInt(e), r))
37
+ return c % r;
38
+ }
39
+ throw Error("Failed to generate a random bigint");
40
+ })();
41
+ return n + i;
42
+ }, d = (n, t, r) => {
43
+ if (r < 1n) throw Error("`mod` must be positive");
44
+ if (t < 0n) throw Error("`power` must not be negative");
45
+ for (; n < 0n; ) n += r;
46
+ if (r === 1n) return 0n;
47
+ if (n % r === 1n || n % r === 0n) return n;
48
+ if (n === r - 1n) return t & 1n ? r - 1n : 1n;
49
+ let e = 1n;
50
+ for (; t > 0n; )
51
+ t & 1n && (e = e * n % r), n = n * n % r, t >>= 1n;
52
+ return e;
53
+ }, A = (n, t) => {
54
+ if (n === 0n && t === 0n) return { x: 0n, y: 0n, gcd: 0n };
55
+ if (n === 0n)
56
+ return t > 0n ? { x: 0n, y: -1n, gcd: t } : { x: 0n, y: 1n, gcd: -t };
57
+ if (t === 0n)
58
+ return n > 0n ? { x: 1n, y: 0n, gcd: n } : { x: -1n, y: 0n, gcd: -n };
59
+ let [r, e, i] = [1n, 0n, n], [s, o, c] = [0n, -1n, t];
60
+ for (; ; ) {
61
+ const u = i / c, f = i - u * c;
62
+ if (f === 0n) break;
63
+ [r, s] = [s, r - u * s], [e, o] = [o, e - u * o], [i, c] = [c, f];
64
+ }
65
+ return c < 0n && (s *= -1n, o *= -1n, c *= -1n), { x: s, y: o, gcd: c };
66
+ }, m = (n, t) => {
67
+ if (n >= t) return 1n;
68
+ const r = BigInt((t - 2n).toString(2).length), e = (t - n) / 2n;
69
+ if (r * e < 63n) {
70
+ let c = n;
71
+ for (let u = n + 2n; u < t; u += 2n)
72
+ c *= u;
73
+ return c;
74
+ }
75
+ const i = n + e | 1n, s = m(n, i), o = m(i, t);
76
+ return s * o;
77
+ }, C = (n) => {
78
+ let t = 3n, r = 1n, e = 1n;
79
+ const i = BigInt(n.toString(2).length) - 1n;
80
+ for (let s = i - 1n; s > -1n; --s) {
81
+ const o = (n >> s) + 1n | 1n;
82
+ e *= m(t, o), t = o, r *= e;
83
+ }
84
+ return r;
85
+ }, q = (n) => {
86
+ if (n < 0n) throw Error("'n' must be non-negative");
87
+ if (n === 0n) return 1n;
88
+ const t = n - BigInt(n.toString(2).match(/1/g)?.length ?? 0);
89
+ return C(n) << t;
90
+ }, D = (n, t) => BigInt.asUintN(32, n >> (t & 31n) | n << (-t & 31n)), E = (n, t) => (n >>> (t & 31) | n << (-t & 31)) >>> 0, w = (n, t) => {
91
+ if (t < 1n || t % 2n === 0n)
92
+ throw Error("`n` is invalid");
93
+ for (; n < 0n; )
94
+ n += t;
95
+ n %= t;
96
+ let r = 1n;
97
+ for (; n !== 0n; ) {
98
+ for (; n % 2n === 0n; ) {
99
+ n /= 2n;
100
+ const e = t % 8n;
101
+ (e === 3n || e === 5n) && (r *= -1n);
102
+ }
103
+ [n, t] = [t, n], n % 4n === 3n && t % 4n === 3n && (r *= -1n), n %= t;
104
+ }
105
+ return t === 1n ? r : 0n;
106
+ }, N = (n) => {
107
+ if (n < 0n) return !1;
108
+ if (n === 0n) return !0;
109
+ let t = 1n, r = n;
110
+ for (; t + 1n < r; ) {
111
+ const e = (t + r) / 2n;
112
+ e ** 2n < n ? t = e : r = e;
113
+ }
114
+ return n === t ** 2n || n === (t + 1n) ** 2n;
115
+ }, I = [0x853c49e6748fea9bn, 0xda3e39cb94b95bdbn], O = 0x5851f42d4c957f2dn;
116
+ class Y {
117
+ /**
118
+ * length = 2, `[state, increment]`
119
+ */
120
+ #t = new BigUint64Array(2);
121
+ /** シード値の配列を返す */
122
+ static getSeed() {
123
+ return crypto.getRandomValues(new BigUint64Array(2));
124
+ }
125
+ /**
126
+ * @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
127
+ */
128
+ constructor(t) {
129
+ t ? (this.#t[1] = t[1] << 1n | 1n, this.#n(), this.#t[0] = t[0], this.#n()) : (this.#t[0] = I[0], this.#t[1] = I[1]);
130
+ }
131
+ /** 内部状態を1サイクル進める */
132
+ #n() {
133
+ this.#t[0] = this.#t[0] * O + this.#t[1];
134
+ }
135
+ /** 32bit 乱数を返す (内部状態は変わらない) */
136
+ get #r() {
137
+ const t = this.#t[0];
138
+ if (!t) throw Error("empty state");
139
+ const r = t >> 59n, e = BigInt.asUintN(32, (t ^ t >> 18n) >> 27n);
140
+ return E(Number(e), Number(r));
141
+ }
142
+ /** 内部状態を1進め、乱数を返す \
143
+ * 普通はこれを使う
144
+ */
145
+ getRand() {
146
+ return this.#n(), this.#r;
147
+ }
148
+ /** `bound` 未満の乱数を返す */
149
+ getBoundedRand(t) {
150
+ if (t > 4294967296) throw Error("`bound` exceeded limit (2^32)");
151
+ const e = 4294967296 % t;
152
+ for (; ; ) {
153
+ const i = this.getRand();
154
+ if (i >= e) return i % t;
155
+ }
156
+ }
157
+ /**
158
+ * step の値だけ乱数を生成する
159
+ * @param step
160
+ * @param bound
161
+ */
162
+ *genRands(t, r) {
163
+ for (let e = 0; e < t; e++)
164
+ yield typeof r == "number" ? this.getBoundedRand(r) : this.getRand();
165
+ }
166
+ }
167
+ const $ = (n) => {
168
+ if (n <= 1n) return !1;
169
+ if (n % 2n === 0n) return n === 2n;
170
+ let t = n - 1n, r = 0n;
171
+ for (; t % 2n === 0n; )
172
+ t >>= 1n, r += 1n;
173
+ const [e, i] = [t, r];
174
+ let o = d(2n, e, n);
175
+ if (o === 1n) return !0;
176
+ for (let c = 0n; c < i; c++) {
177
+ if (o === n - 1n) return !0;
178
+ o = o * o % n;
179
+ }
180
+ return !1;
181
+ }, J = (n) => {
182
+ let t = 5n, r = w(t, n);
183
+ for (; r > 0n; ) {
184
+ if (t = t > 0n ? t + 2n : t - 2n, t *= -1n, t === -15n && N(n))
185
+ return [0n, 0n];
186
+ r = w(t, n);
187
+ }
188
+ return [t, r];
189
+ }, g = (n, t) => (n & 1n) === 1n ? h(n + t >> 1n, t) : h(n >> 1n, t), k = (n, t, r, e) => {
190
+ let i = 1n, s = r;
191
+ const o = n.toString(2).slice(1);
192
+ for (const c of o)
193
+ [i, s] = [h(i * s, t), g(s * s + e * i * i, t)], c === "1" && ([i, s] = [g(r * i + s, t), g(e * i + r * s, t)]);
194
+ return [i, s];
195
+ }, v = (n, t, r, e) => {
196
+ if (n % 2n !== 1n) throw Error("`n` must be odd");
197
+ let i = n + 1n, s = 0n;
198
+ for (; i % 2n === 0n; )
199
+ i >>= 1n, s += 1n;
200
+ const [o, c] = k(i, n, r, t);
201
+ let u = c;
202
+ if (o === 0n) return !0;
203
+ e = d(e, i, n);
204
+ for (let f = 0n; f < s; f++) {
205
+ if (u === 0n) return !0;
206
+ u = h(u * u - 2n * e, n), e = d(e, 2n, n);
207
+ }
208
+ return !1;
209
+ }, y = (n) => {
210
+ if (n <= 1n) return !1;
211
+ if (n % 2n === 0n) return n === 2n;
212
+ const t = [
213
+ 2n,
214
+ 3n,
215
+ 5n,
216
+ 7n,
217
+ 11n,
218
+ 13n,
219
+ 17n,
220
+ 19n,
221
+ 23n,
222
+ 29n,
223
+ 31n,
224
+ 37n,
225
+ 41n,
226
+ 43n,
227
+ 47n,
228
+ 53n,
229
+ 59n,
230
+ 61n,
231
+ 67n,
232
+ 71n,
233
+ 73n,
234
+ 79n,
235
+ 83n,
236
+ 89n,
237
+ 97n,
238
+ 101n
239
+ ];
240
+ for (const s of t)
241
+ if (n % s === 0n)
242
+ return n === s;
243
+ if (!$(n))
244
+ return !1;
245
+ const [r, e] = J(n);
246
+ if (e === 0n) return !1;
247
+ const i = (1n - r) / 4n;
248
+ return v(n, r, 1n, i);
249
+ }, z = (n, t) => {
250
+ if (t < 2n)
251
+ throw Error("noPrimesFound");
252
+ for (let e = 0; e < 1e5; e++) {
253
+ const i = S(n, t);
254
+ if (y(i)) return i;
255
+ }
256
+ throw Error("noPrimesFound");
257
+ }, G = (n, t = !1) => {
258
+ if (n < 2)
259
+ throw Error("noPrimesFound");
260
+ for (let e = 0; e < 1e5; e++) {
261
+ const i = p(n, t);
262
+ if (y(i)) return i;
263
+ }
264
+ throw Error("noPrimesFound");
265
+ };
266
+ class l {
267
+ #t;
268
+ #n;
269
+ /**
270
+ *
271
+ * @param numerator
272
+ * @param denominator
273
+ */
274
+ constructor(t, r) {
275
+ r === 0n ? (this.#n = 0n, this.#t = t === 0n ? 0n : t > 0n ? 1n : -1n) : r > 0n ? (this.#t = t, this.#n = r) : (this.#t = -t, this.#n = -r), this.#r();
276
+ }
277
+ /**
278
+ * generates fraction from `number` decimal using continued fraction
279
+ * @param value 値
280
+ * @param denominatorDigits 分母の桁数 (十進)
281
+ */
282
+ static fromDecimal(t, r = 20) {
283
+ if (Number.isNaN(t))
284
+ return new l(0n, 0n);
285
+ if (Math.abs(t) === 1 / 0)
286
+ return new l(t > 0 ? 1n : -1n, 0n);
287
+ const e = t < 0;
288
+ e && (t *= -1);
289
+ const i = BigInt(Math.floor(t)), s = t - Number(i);
290
+ if (s === 0)
291
+ return new l(e ? -i : i, 1n);
292
+ t = 1 / s;
293
+ let [o, c] = [1n, i], [u, f] = [0n, 1n];
294
+ for (; `${f}`.length < r + 1; ) {
295
+ const a = BigInt(Math.floor(t)), B = t - Number(a);
296
+ if ([o, c] = [c, a * c + o], [u, f] = [f, a * f + u], B === 0)
297
+ return new l(e ? -c : c, f);
298
+ t = 1 / B;
299
+ }
300
+ return new l(e ? -o : o, u);
301
+ }
302
+ /**
303
+ * reduction
304
+ */
305
+ #r() {
306
+ const { gcd: t } = A(this.#t, this.#n);
307
+ t !== 0n && (this.#t /= t, this.#n /= t);
308
+ }
309
+ /**
310
+ * returns a fraction multiplied by -1
311
+ * @returns
312
+ */
313
+ minus() {
314
+ return new l(-this.#t, this.#n);
315
+ }
316
+ /**
317
+ * returns inverse of this fraction
318
+ * @returns
319
+ */
320
+ inverse() {
321
+ return new l(this.#n, this.#t);
322
+ }
323
+ /**
324
+ * returns `this + right`
325
+ * @param right
326
+ * @returns
327
+ */
328
+ add(t) {
329
+ const r = this.#n * t.#n, e = this.#t * t.#n + t.#t * this.#n;
330
+ return new l(e, r);
331
+ }
332
+ /**
333
+ * returns `this - right`
334
+ * @param right
335
+ * @returns
336
+ */
337
+ substr(t) {
338
+ return new l(this.#t, this.#n).add(t.minus());
339
+ }
340
+ /**
341
+ * returns `this * right`
342
+ * @param right
343
+ * @returns
344
+ */
345
+ multiply(t) {
346
+ const r = this.#n * t.#n, e = this.#t * t.#t;
347
+ return new l(e, r);
348
+ }
349
+ /**
350
+ * returns `this / right`
351
+ * @param right
352
+ * @returns
353
+ */
354
+ divide(t) {
355
+ return new l(this.#t, this.#n).multiply(
356
+ t.inverse()
357
+ );
358
+ }
359
+ /**
360
+ * mediant
361
+ * @param right
362
+ * @returns
363
+ */
364
+ mediant(t) {
365
+ const r = this.#n + t.#n, e = this.#t + t.#t;
366
+ return new l(e, r);
367
+ }
368
+ /**
369
+ * `number` 型の小数に変換
370
+ * @returns
371
+ */
372
+ toDecimal() {
373
+ return Number(this.#t) / Number(this.#n);
374
+ }
375
+ /**
376
+ * 分数の文字列に変換
377
+ * @returns
378
+ */
379
+ toString() {
380
+ return this.#t === 0n && this.#n === 0n ? "NaN" : this.#t === 0n ? "0" : this.#n === 0n ? this.#t < 0n ? "-Infinity" : "Infinity" : this.#n === 1n ? `${this.#t}` : `${this.#t}/${this.#n}`;
381
+ }
382
+ valueOf() {
383
+ return this.toDecimal();
384
+ }
385
+ toJSON() {
386
+ return {
387
+ type: "Fraction",
388
+ numerator: "0x" + this.#t.toString(16),
389
+ denominator: "0x" + this.#n.toString(16)
390
+ };
391
+ }
392
+ static parse(t) {
393
+ const r = JSON.parse(t), { type: e, numerator: i, denominator: s } = r;
394
+ if (e !== "Fraction") throw Error("cannot parse");
395
+ if (typeof i == "string" && typeof s == "string") {
396
+ const o = BigInt(i), c = BigInt(s);
397
+ return new l(o, c);
398
+ } else
399
+ throw Error("cannot parse");
400
+ }
401
+ }
402
+ const H = (n) => {
403
+ let t = n.toString(16);
404
+ t.length & 1 && (t = "0" + t);
405
+ const r = [...t.match(/.{2}/g) ?? []];
406
+ return Uint8Array.from(r, (e) => Number.parseInt(e, 16));
407
+ }, W = (n) => {
408
+ const t = Array.from(n, (r) => r.toString(16).padStart(2, "0")).join("") || "00";
409
+ return BigInt("0x" + t);
410
+ }, U = (n) => {
411
+ if (b)
412
+ return Buffer.copyBytesFrom(n).toString("base64");
413
+ {
414
+ const t = Array.from(n, (r) => String.fromCharCode(r)).join("");
415
+ return btoa(t);
416
+ }
417
+ }, P = (n) => {
418
+ if (b) {
419
+ const t = Buffer.from(n, "base64");
420
+ return new Uint8Array(t.buffer, t.byteOffset, t.byteLength);
421
+ } else
422
+ return Uint8Array.from(atob(n), (t) => t.charCodeAt(0));
423
+ }, K = (n) => U(n).replaceAll(/\+/g, "-").replaceAll(/\//g, "_").replaceAll(/=*$/g, ""), X = (n) => {
424
+ const t = n.length & 3;
425
+ t > 0 && (n = n + "=".repeat(4 - t));
426
+ const r = n.replaceAll(/-/g, "+").replaceAll(/_/g, "/");
427
+ return P(r);
428
+ }, Z = {
429
+ isEqArray: x,
430
+ sleep: M,
431
+ lazify: T,
432
+ parseCSV: L,
433
+ getHash: _
434
+ }, F = {
435
+ getRndInt: V,
436
+ residue: h,
437
+ toBigInt: j,
438
+ getRandBIByBitLength: p,
439
+ getRandBIByRange: S,
440
+ modPow: d,
441
+ exEuclidean: A,
442
+ factorial: q,
443
+ rot32: E,
444
+ rot32BI: D,
445
+ jacobiSymbol: w,
446
+ isSquare: N
447
+ }, Q = {
448
+ bailliePSW: y,
449
+ getRandPrimeByBitLength: G,
450
+ getRandPrimeByRange: z
451
+ }, tt = {
452
+ base64ToU8Arr: P,
453
+ base64UrlToU8Arr: X,
454
+ u8ArrToBase64: U,
455
+ u8ArrToBase64Url: K,
456
+ BItoU8Arr: H,
457
+ u8ArrToBI: W
458
+ };
459
+ export {
460
+ F as AdMath,
461
+ H as BItoU8Arr,
462
+ l as Fraction,
463
+ Y as PCGMinimal,
464
+ Q as Prime,
465
+ tt as U8Ext,
466
+ Z as Util,
467
+ y as bailliePSW,
468
+ P as base64ToU8Arr,
469
+ X as base64UrlToU8Arr,
470
+ A as exEuclidean,
471
+ q as factorial,
472
+ _ as getHash,
473
+ p as getRandBIByBitLength,
474
+ S as getRandBIByRange,
475
+ G as getRandPrimeByBitLength,
476
+ z as getRandPrimeByRange,
477
+ V as getRndInt,
478
+ x as isEqArray,
479
+ N as isSquare,
480
+ w as jacobiSymbol,
481
+ T as lazify,
482
+ d as modPow,
483
+ L as parseCSV,
484
+ h as residue,
485
+ E as rot32,
486
+ D as rot32BI,
487
+ M as sleep,
488
+ j as toBigInt,
489
+ W as u8ArrToBI,
490
+ U as u8ArrToBase64,
491
+ K as u8ArrToBase64Url
492
+ };
@@ -0,0 +1,2 @@
1
+ (function(o,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(o=typeof globalThis<"u"?globalThis:o||self,g(o["util-fns"]={}))})(this,(function(o){"use strict";const g=new TextEncoder,R=(t,n)=>{if(t.length!==n.length)return!1;for(let e=0;e<t.length;e++)if(t[e]!==n[e])return!1;return!0},N=t=>new Promise(n=>{setTimeout(()=>{n()},t)}),T=t=>(...n)=>()=>t(...n),M=t=>{const n=[];let e=[],r="",i=!1;for(let s=0;s<t.length;s++){const c=t[s];c==='"'&&(s===0||t[s-1]!=="\\")?i=!i:c===","&&!i?(e.push(r.trim()),r=""):c===`
2
+ `&&!i?(e.push(r.trim()),n.push(e),e=[],r=""):r+=c}return e.push(r.trim()),n.push(e),n},L=async(t,n)=>{const e=g.encode(t),r=await crypto.subtle.digest(n,e);return new Uint8Array(r)},j=typeof process<"u"&&typeof process.versions.node<"u",_=(t,n)=>Math.floor(Math.random()*(n-t)+t),a=(t,n)=>{const e=t%n;return e<0n?e+n:e},C=(...t)=>t.map(n=>BigInt(n)),m=(t,n=!1)=>{if(!Number.isFinite(t))throw Error("`length` is not a valid number");if(t<=0)throw Error("`length` must be positive");const e=Math.ceil(t/8),r=crypto.getRandomValues(new Uint8Array(e));let i=Array.from(r,s=>s.toString(2).padStart(8,"0")).join("").slice(0,t);return n&&(i=i.replace(/^./,"1")),BigInt("0b"+i)},B=(t,n)=>{if(t>=n)throw Error("rangeError");const e=n-t,r=e.toString(2).length,i=(()=>{for(let c=0;c<1e5;c++){const u=m(r);if(u>=d(2n,BigInt(r),e))return u%e}throw Error("Failed to generate a random bigint")})();return t+i},d=(t,n,e)=>{if(e<1n)throw Error("`mod` must be positive");if(n<0n)throw Error("`power` must not be negative");for(;t<0n;)t+=e;if(e===1n)return 0n;if(t%e===1n||t%e===0n)return t;if(t===e-1n)return n&1n?e-1n:1n;let r=1n;for(;n>0n;)n&1n&&(r=r*t%e),t=t*t%e,n>>=1n;return r},I=(t,n)=>{if(t===0n&&n===0n)return{x:0n,y:0n,gcd:0n};if(t===0n)return n>0n?{x:0n,y:-1n,gcd:n}:{x:0n,y:1n,gcd:-n};if(n===0n)return t>0n?{x:1n,y:0n,gcd:t}:{x:-1n,y:0n,gcd:-t};let[e,r,i]=[1n,0n,t],[s,c,u]=[0n,-1n,n];for(;;){const l=i/u,h=i-l*u;if(h===0n)break;[e,s]=[s,e-l*s],[r,c]=[c,r-l*c],[i,u]=[u,h]}return u<0n&&(s*=-1n,c*=-1n,u*=-1n),{x:s,y:c,gcd:u}},b=(t,n)=>{if(t>=n)return 1n;const e=BigInt((n-2n).toString(2).length),r=(n-t)/2n;if(e*r<63n){let u=t;for(let l=t+2n;l<n;l+=2n)u*=l;return u}const i=t+r|1n,s=b(t,i),c=b(i,n);return s*c},W=t=>{let n=3n,e=1n,r=1n;const i=BigInt(t.toString(2).length)-1n;for(let s=i-1n;s>-1n;--s){const c=(t>>s)+1n|1n;r*=b(n,c),n=c,e*=r}return e},V=t=>{if(t<0n)throw Error("'n' must be non-negative");if(t===0n)return 1n;const n=t-BigInt(t.toString(2).match(/1/g)?.length??0);return W(t)<<n},q=(t,n)=>BigInt.asUintN(32,t>>(n&31n)|t<<(-n&31n)),A=(t,n)=>(t>>>(n&31)|t<<(-n&31))>>>0,y=(t,n)=>{if(n<1n||n%2n===0n)throw Error("`n` is invalid");for(;t<0n;)t+=n;t%=n;let e=1n;for(;t!==0n;){for(;t%2n===0n;){t/=2n;const r=n%8n;(r===3n||r===5n)&&(e*=-1n)}[t,n]=[n,t],t%4n===3n&&n%4n===3n&&(e*=-1n),t%=n}return n===1n?e:0n},S=t=>{if(t<0n)return!1;if(t===0n)return!0;let n=1n,e=t;for(;n+1n<e;){const r=(n+e)/2n;r**2n<t?n=r:e=r}return t===n**2n||t===(n+1n)**2n},O=[0x853c49e6748fea9bn,0xda3e39cb94b95bdbn],k=0x5851f42d4c957f2dn;class K{#n=new BigUint64Array(2);static getSeed(){return crypto.getRandomValues(new BigUint64Array(2))}constructor(n){n?(this.#n[1]=n[1]<<1n|1n,this.#t(),this.#n[0]=n[0],this.#t()):(this.#n[0]=O[0],this.#n[1]=O[1])}#t(){this.#n[0]=this.#n[0]*k+this.#n[1]}get#e(){const n=this.#n[0];if(!n)throw Error("empty state");const e=n>>59n,r=BigInt.asUintN(32,(n^n>>18n)>>27n);return A(Number(r),Number(e))}getRand(){return this.#t(),this.#e}getBoundedRand(n){if(n>4294967296)throw Error("`bound` exceeded limit (2^32)");const r=4294967296%n;for(;;){const i=this.getRand();if(i>=r)return i%n}}*genRands(n,e){for(let r=0;r<n;r++)yield typeof e=="number"?this.getBoundedRand(e):this.getRand()}}const X=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;let n=t-1n,e=0n;for(;n%2n===0n;)n>>=1n,e+=1n;const[r,i]=[n,e];let c=d(2n,r,t);if(c===1n)return!0;for(let u=0n;u<i;u++){if(c===t-1n)return!0;c=c*c%t}return!1},Y=t=>{let n=5n,e=y(n,t);for(;e>0n;){if(n=n>0n?n+2n:n-2n,n*=-1n,n===-15n&&S(t))return[0n,0n];e=y(n,t)}return[n,e]},p=(t,n)=>(t&1n)===1n?a(t+n>>1n,n):a(t>>1n,n),Z=(t,n,e,r)=>{let i=1n,s=e;const c=t.toString(2).slice(1);for(const u of c)[i,s]=[a(i*s,n),p(s*s+r*i*i,n)],u==="1"&&([i,s]=[p(e*i+s,n),p(r*i+e*s,n)]);return[i,s]},F=(t,n,e,r)=>{if(t%2n!==1n)throw Error("`n` must be odd");let i=t+1n,s=0n;for(;i%2n===0n;)i>>=1n,s+=1n;const[c,u]=Z(i,t,e,n);let l=u;if(c===0n)return!0;r=d(r,i,t);for(let h=0n;h<s;h++){if(l===0n)return!0;l=a(l*l-2n*r,t),r=d(r,2n,t)}return!1},w=t=>{if(t<=1n)return!1;if(t%2n===0n)return t===2n;const n=[2n,3n,5n,7n,11n,13n,17n,19n,23n,29n,31n,37n,41n,43n,47n,53n,59n,61n,67n,71n,73n,79n,83n,89n,97n,101n];for(const s of n)if(t%s===0n)return t===s;if(!X(t))return!1;const[e,r]=Y(t);if(r===0n)return!1;const i=(1n-e)/4n;return F(t,e,1n,i)},D=(t,n)=>{if(n<2n)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=B(t,n);if(w(i))return i}throw Error("noPrimesFound")},$=(t,n=!1)=>{if(t<2)throw Error("noPrimesFound");for(let r=0;r<1e5;r++){const i=m(t,n);if(w(i))return i}throw Error("noPrimesFound")};class f{#n;#t;constructor(n,e){e===0n?(this.#t=0n,this.#n=n===0n?0n:n>0n?1n:-1n):e>0n?(this.#n=n,this.#t=e):(this.#n=-n,this.#t=-e),this.#e()}static fromDecimal(n,e=20){if(Number.isNaN(n))return new f(0n,0n);if(Math.abs(n)===1/0)return new f(n>0?1n:-1n,0n);const r=n<0;r&&(n*=-1);const i=BigInt(Math.floor(n)),s=n-Number(i);if(s===0)return new f(r?-i:i,1n);n=1/s;let[c,u]=[1n,i],[l,h]=[0n,1n];for(;`${h}`.length<e+1;){const P=BigInt(Math.floor(n)),J=n-Number(P);if([c,u]=[u,P*u+c],[l,h]=[h,P*h+l],J===0)return new f(r?-u:u,h);n=1/J}return new f(r?-c:c,l)}#e(){const{gcd:n}=I(this.#n,this.#t);n!==0n&&(this.#n/=n,this.#t/=n)}minus(){return new f(-this.#n,this.#t)}inverse(){return new f(this.#t,this.#n)}add(n){const e=this.#t*n.#t,r=this.#n*n.#t+n.#n*this.#t;return new f(r,e)}substr(n){return new f(this.#n,this.#t).add(n.minus())}multiply(n){const e=this.#t*n.#t,r=this.#n*n.#n;return new f(r,e)}divide(n){return new f(this.#n,this.#t).multiply(n.inverse())}mediant(n){const e=this.#t+n.#t,r=this.#n+n.#n;return new f(r,e)}toDecimal(){return Number(this.#n)/Number(this.#t)}toString(){return this.#n===0n&&this.#t===0n?"NaN":this.#n===0n?"0":this.#t===0n?this.#n<0n?"-Infinity":"Infinity":this.#t===1n?`${this.#n}`:`${this.#n}/${this.#t}`}valueOf(){return this.toDecimal()}toJSON(){return{type:"Fraction",numerator:"0x"+this.#n.toString(16),denominator:"0x"+this.#t.toString(16)}}static parse(n){const e=JSON.parse(n),{type:r,numerator:i,denominator:s}=e;if(r!=="Fraction")throw Error("cannot parse");if(typeof i=="string"&&typeof s=="string"){const c=BigInt(i),u=BigInt(s);return new f(c,u)}else throw Error("cannot parse")}}const v=t=>{let n=t.toString(16);n.length&1&&(n="0"+n);const e=[...n.match(/.{2}/g)??[]];return Uint8Array.from(e,r=>Number.parseInt(r,16))},z=t=>{const n=Array.from(t,e=>e.toString(16).padStart(2,"0")).join("")||"00";return BigInt("0x"+n)},E=t=>{if(j)return Buffer.copyBytesFrom(t).toString("base64");{const n=Array.from(t,e=>String.fromCharCode(e)).join("");return btoa(n)}},U=t=>{if(j){const n=Buffer.from(t,"base64");return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}else return Uint8Array.from(atob(t),n=>n.charCodeAt(0))},G=t=>E(t).replaceAll(/\+/g,"-").replaceAll(/\//g,"_").replaceAll(/=*$/g,""),H=t=>{const n=t.length&3;n>0&&(t=t+"=".repeat(4-n));const e=t.replaceAll(/-/g,"+").replaceAll(/_/g,"/");return U(e)},Q={isEqArray:R,sleep:N,lazify:T,parseCSV:M,getHash:L},x={getRndInt:_,residue:a,toBigInt:C,getRandBIByBitLength:m,getRandBIByRange:B,modPow:d,exEuclidean:I,factorial:V,rot32:A,rot32BI:q,jacobiSymbol:y,isSquare:S},nn={bailliePSW:w,getRandPrimeByBitLength:$,getRandPrimeByRange:D},tn={base64ToU8Arr:U,base64UrlToU8Arr:H,u8ArrToBase64:E,u8ArrToBase64Url:G,BItoU8Arr:v,u8ArrToBI:z};o.AdMath=x,o.BItoU8Arr=v,o.Fraction=f,o.PCGMinimal=K,o.Prime=nn,o.U8Ext=tn,o.Util=Q,o.bailliePSW=w,o.base64ToU8Arr=U,o.base64UrlToU8Arr=H,o.exEuclidean=I,o.factorial=V,o.getHash=L,o.getRandBIByBitLength=m,o.getRandBIByRange=B,o.getRandPrimeByBitLength=$,o.getRandPrimeByRange=D,o.getRndInt=_,o.isEqArray=R,o.isSquare=S,o.jacobiSymbol=y,o.lazify=T,o.modPow=d,o.parseCSV=M,o.residue=a,o.rot32=A,o.rot32BI=q,o.sleep=N,o.toBigInt=C,o.u8ArrToBI=z,o.u8ArrToBase64=E,o.u8ArrToBase64Url=G,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
@@ -1,4 +1,3 @@
1
- declare const __fraction_brand: unique symbol;
2
1
  export type FractionData = {
3
2
  type: 'Fraction';
4
3
  numerator: string;
@@ -6,48 +5,58 @@ export type FractionData = {
6
5
  };
7
6
  export default class Fraction {
8
7
  #private;
9
- readonly [__fraction_brand]: unknown;
8
+ /**
9
+ *
10
+ * @param numerator
11
+ * @param denominator
12
+ */
10
13
  constructor(numerator: bigint, denominator: bigint);
11
14
  /**
12
- * `number` 型から構成
15
+ * generates fraction from `number` decimal using continued fraction
13
16
  * @param value 値
14
17
  * @param denominatorDigits 分母の桁数 (十進)
15
18
  */
16
19
  static fromDecimal(value: number, denominatorDigits?: number): Fraction;
17
20
  /**
18
- * -1
21
+ * returns a fraction multiplied by -1
19
22
  * @returns
20
23
  */
21
24
  minus(): Fraction;
22
25
  /**
23
- * 逆数
26
+ * returns inverse of this fraction
24
27
  * @returns
25
28
  */
26
29
  inverse(): Fraction;
27
30
  /**
28
- * 加法 (非破壊的)
31
+ * returns `this + right`
29
32
  * @param right
30
33
  * @returns
31
34
  */
32
35
  add(right: Fraction): Fraction;
33
36
  /**
34
- * 減法
37
+ * returns `this - right`
35
38
  * @param right
36
39
  * @returns
37
40
  */
38
41
  substr(right: Fraction): Fraction;
39
42
  /**
40
- * 乗法
43
+ * returns `this * right`
41
44
  * @param right
42
45
  * @returns
43
46
  */
44
47
  multiply(right: Fraction): Fraction;
45
48
  /**
46
- * 除法
49
+ * returns `this / right`
47
50
  * @param right
48
51
  * @returns
49
52
  */
50
53
  divide(right: Fraction): Fraction;
54
+ /**
55
+ * mediant
56
+ * @param right
57
+ * @returns
58
+ */
59
+ mediant(right: Fraction): Fraction;
51
60
  /**
52
61
  * `number` 型の小数に変換
53
62
  * @returns
@@ -62,5 +71,3 @@ export default class Fraction {
62
71
  toJSON(): FractionData;
63
72
  static parse(text: string): Fraction;
64
73
  }
65
- export {};
66
- //# sourceMappingURL=fraction.d.ts.map
package/dist/math.d.ts CHANGED
@@ -87,4 +87,3 @@ export declare const jacobiSymbol: (a: bigint, n: bigint) => bigint;
87
87
  * @returns
88
88
  */
89
89
  export declare const isSquare: (n: bigint) => boolean;
90
- //# sourceMappingURL=math.d.ts.map
@@ -1,20 +1,14 @@
1
- declare const __pcg_brand: unique symbol;
2
1
  /**
3
2
  * PCG-XSH-RR (Permuted congruential generator) 乱数のクラス
4
3
  */
5
4
  export default class PCGMinimal {
6
5
  #private;
7
- readonly [__pcg_brand]: unknown;
8
6
  /** シード値の配列を返す */
9
7
  static getSeed(): BigUint64Array<ArrayBuffer>;
10
8
  /**
11
9
  * @param seeds 64bit整数の配列 (長さ2以上), 省略した場合, 常に同じ値によって初期化される
12
10
  */
13
11
  constructor(seeds?: BigUint64Array<ArrayBuffer>);
14
- /** 内部状態を1サイクル進める */
15
- step(): void;
16
- /** 32bit 乱数を返す (内部状態は変わらない) */
17
- get value(): number;
18
12
  /** 内部状態を1進め、乱数を返す \
19
13
  * 普通はこれを使う
20
14
  */
@@ -28,5 +22,3 @@ export default class PCGMinimal {
28
22
  */
29
23
  genRands(step: number, bound?: number): Generator<number, void, unknown>;
30
24
  }
31
- export {};
32
- //# sourceMappingURL=pcg-minimal.d.ts.map
@@ -0,0 +1,7 @@
1
+ declare const BItoU8Arr: (n: bigint) => Uint8Array<ArrayBuffer>;
2
+ declare const u8ArrToBI: (buf: Uint8Array) => bigint;
3
+ declare const u8ArrToBase64: (u8Arr: Uint8Array) => string;
4
+ declare const base64ToU8Arr: (base64: string) => Uint8Array<ArrayBuffer>;
5
+ declare const u8ArrToBase64Url: (u8Arr: Uint8Array) => string;
6
+ declare const base64UrlToU8Arr: (base64Url: string) => Uint8Array<ArrayBuffer>;
7
+ export { BItoU8Arr, u8ArrToBI, u8ArrToBase64, base64ToU8Arr, u8ArrToBase64Url, base64UrlToU8Arr, };