@bcts/rand 1.0.0-alpha.10

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.
package/dist/index.mjs ADDED
@@ -0,0 +1,478 @@
1
+ //#region src/widening.ts
2
+ /**
3
+ * Performs wide multiplication for unsigned integers.
4
+ * Returns (low, high) parts of the full-width result.
5
+ *
6
+ * This is equivalent to Rust's widening_mul for unsigned types.
7
+ */
8
+ function wideMul(a, b, bits) {
9
+ const mask = (1n << BigInt(bits)) - 1n;
10
+ const wide = (a & mask) * (b & mask);
11
+ return [wide & mask, wide >> BigInt(bits)];
12
+ }
13
+ /**
14
+ * Wide multiplication for 8-bit unsigned integers.
15
+ * @param a - First 8-bit value
16
+ * @param b - Second 8-bit value
17
+ * @returns Tuple of (low 8 bits, high 8 bits)
18
+ */
19
+ function wideMulU8(a, b) {
20
+ const wide = (a & 255) * (b & 255);
21
+ return [wide & 255, wide >> 8 & 255];
22
+ }
23
+ /**
24
+ * Wide multiplication for 16-bit unsigned integers.
25
+ * @param a - First 16-bit value
26
+ * @param b - Second 16-bit value
27
+ * @returns Tuple of (low 16 bits, high 16 bits)
28
+ */
29
+ function wideMulU16(a, b) {
30
+ const wide = (a & 65535) * (b & 65535);
31
+ return [wide & 65535, wide >>> 16 & 65535];
32
+ }
33
+ /**
34
+ * Wide multiplication for 32-bit unsigned integers.
35
+ * @param a - First 32-bit value
36
+ * @param b - Second 32-bit value
37
+ * @returns Tuple of (low 32 bits, high 32 bits) as bigints
38
+ */
39
+ function wideMulU32(a, b) {
40
+ const wide = BigInt(a >>> 0) * BigInt(b >>> 0);
41
+ return [wide & 4294967295n, wide >> 32n];
42
+ }
43
+ /**
44
+ * Wide multiplication for 64-bit unsigned integers.
45
+ * @param a - First 64-bit value as bigint
46
+ * @param b - Second 64-bit value as bigint
47
+ * @returns Tuple of (low 64 bits, high 64 bits) as bigints
48
+ */
49
+ function wideMulU64(a, b) {
50
+ const mask64 = 18446744073709551615n;
51
+ const wide = (a & mask64) * (b & mask64);
52
+ return [wide & mask64, wide >> 64n];
53
+ }
54
+
55
+ //#endregion
56
+ //#region src/magnitude.ts
57
+ /**
58
+ * Converts a signed integer to its unsigned magnitude.
59
+ * For positive numbers, returns the number unchanged.
60
+ * For negative numbers, returns the absolute value (wrapping for MIN values).
61
+ *
62
+ * This matches Rust's wrapping_abs() behavior.
63
+ */
64
+ function toMagnitude(value, bits) {
65
+ switch (bits) {
66
+ case 8: {
67
+ const i8Value = value << 24 >> 24;
68
+ return Math.abs(i8Value) & 255;
69
+ }
70
+ case 16: {
71
+ const i16Value = value << 16 >> 16;
72
+ return Math.abs(i16Value) & 65535;
73
+ }
74
+ case 32: {
75
+ const i32Value = value | 0;
76
+ if (i32Value === -2147483648) return 2147483648;
77
+ return Math.abs(i32Value) >>> 0;
78
+ }
79
+ }
80
+ }
81
+ /**
82
+ * Converts a signed bigint to its unsigned magnitude for 64-bit values.
83
+ */
84
+ function toMagnitude64(value) {
85
+ const mask = 18446744073709551615n;
86
+ if (value < 0n) return -value & mask;
87
+ return value & mask;
88
+ }
89
+ /**
90
+ * Converts an unsigned magnitude back to a signed value.
91
+ * Simply reinterprets the bits.
92
+ */
93
+ function fromMagnitude(magnitude, bits) {
94
+ switch (bits) {
95
+ case 8: return magnitude << 24 >> 24;
96
+ case 16: return magnitude << 16 >> 16;
97
+ case 32: return magnitude | 0;
98
+ }
99
+ }
100
+ /**
101
+ * Converts an unsigned 64-bit magnitude back to a signed bigint.
102
+ */
103
+ function fromMagnitude64(magnitude) {
104
+ const mask = 18446744073709551615n;
105
+ const signBit = 1n << 63n;
106
+ const maskedMag = magnitude & mask;
107
+ if ((maskedMag & signBit) !== 0n) return maskedMag - (1n << 64n);
108
+ return maskedMag;
109
+ }
110
+
111
+ //#endregion
112
+ //#region src/random-number-generator.ts
113
+ /**
114
+ * Returns a Uint8Array of random bytes of the given size.
115
+ */
116
+ function rngRandomData(rng, size) {
117
+ const data = new Uint8Array(size);
118
+ rng.fillRandomData(data);
119
+ return data;
120
+ }
121
+ /**
122
+ * Fills the given Uint8Array with random bytes.
123
+ */
124
+ function rngFillRandomData(rng, data) {
125
+ rng.fillRandomData(data);
126
+ }
127
+ /**
128
+ * Returns a random value that is less than the given upper bound.
129
+ *
130
+ * Uses Lemire's "nearly divisionless" method for generating random
131
+ * integers in an interval. For a detailed explanation, see:
132
+ * https://arxiv.org/abs/1805.10941
133
+ *
134
+ * @param rng - The random number generator to use
135
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero.
136
+ * @returns A random value in the range [0, upperBound). Every value in the range is equally likely.
137
+ */
138
+ function rngNextWithUpperBound(rng, upperBound) {
139
+ if (upperBound === 0n) throw new Error("upperBound must be non-zero");
140
+ const bitmask = 18446744073709551615n;
141
+ let random = rng.nextU64() & bitmask;
142
+ let m = wideMulU64(random, upperBound);
143
+ if (m[0] < upperBound) {
144
+ const t = (bitmask + 1n - upperBound & bitmask) % upperBound;
145
+ while (m[0] < t) {
146
+ random = rng.nextU64() & bitmask;
147
+ m = wideMulU64(random, upperBound);
148
+ }
149
+ }
150
+ return m[1];
151
+ }
152
+ /**
153
+ * Returns a random 32-bit value that is less than the given upper bound.
154
+ * This matches Rust's behavior when called with u32 type.
155
+ *
156
+ * Uses Lemire's "nearly divisionless" method with 32-bit arithmetic.
157
+ *
158
+ * @param rng - The random number generator to use
159
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero and fit in u32.
160
+ * @returns A random u32 value in the range [0, upperBound).
161
+ */
162
+ function rngNextWithUpperBoundU32(rng, upperBound) {
163
+ if (upperBound === 0) throw new Error("upperBound must be non-zero");
164
+ const upperBoundU32 = upperBound >>> 0;
165
+ const bitmask = 4294967295;
166
+ let random = Number(rng.nextU64() & BigInt(bitmask));
167
+ let m = wideMulU32(random, upperBoundU32);
168
+ if (Number(m[0]) < upperBoundU32) {
169
+ const t = (bitmask + 1 - upperBoundU32 >>> 0) % upperBoundU32;
170
+ while (Number(m[0]) < t) {
171
+ random = Number(rng.nextU64() & BigInt(bitmask));
172
+ m = wideMulU32(random, upperBoundU32);
173
+ }
174
+ }
175
+ return Number(m[1]);
176
+ }
177
+ /**
178
+ * Returns a random value within the specified range [start, end) using 32-bit arithmetic.
179
+ * This matches Rust's behavior when called with i32 types.
180
+ *
181
+ * @param rng - The random number generator to use
182
+ * @param start - The lower bound (inclusive) as i32
183
+ * @param end - The upper bound (exclusive) as i32
184
+ * @returns A random i32 value within the bounds
185
+ */
186
+ function rngNextInRangeI32(rng, start, end) {
187
+ if (start >= end) throw new Error("start must be less than end");
188
+ const startI32 = start | 0;
189
+ const delta = toMagnitude((end | 0) - startI32, 32);
190
+ if (delta === 4294967295) return rng.nextU32() | 0;
191
+ return startI32 + rngNextWithUpperBoundU32(rng, delta) | 0;
192
+ }
193
+ /**
194
+ * Returns a random value within the specified range [start, end).
195
+ *
196
+ * @param rng - The random number generator to use
197
+ * @param start - The lower bound (inclusive)
198
+ * @param end - The upper bound (exclusive)
199
+ * @returns A random value within the bounds of the range
200
+ */
201
+ function rngNextInRange(rng, start, end) {
202
+ if (start >= end) throw new Error("start must be less than end");
203
+ const delta = end - start;
204
+ if (delta === 18446744073709551615n) return rng.nextU64();
205
+ return start + rngNextWithUpperBound(rng, delta);
206
+ }
207
+ /**
208
+ * Returns a random value within the specified closed range [start, end].
209
+ *
210
+ * @param rng - The random number generator to use
211
+ * @param start - The lower bound (inclusive)
212
+ * @param end - The upper bound (inclusive)
213
+ * @returns A random value within the bounds of the range
214
+ */
215
+ function rngNextInClosedRange(rng, start, end) {
216
+ if (start > end) throw new Error("start must be less than or equal to end");
217
+ const delta = end - start;
218
+ if (delta === 18446744073709551615n) return rng.nextU64();
219
+ return start + rngNextWithUpperBound(rng, delta + 1n);
220
+ }
221
+ /**
222
+ * Returns a random value within the specified closed range [start, end] for i32 values.
223
+ * Convenience function that handles signed 32-bit integers.
224
+ *
225
+ * @param rng - The random number generator to use
226
+ * @param start - The lower bound (inclusive) as i32
227
+ * @param end - The upper bound (inclusive) as i32
228
+ * @returns A random i32 value within the bounds of the range
229
+ */
230
+ function rngNextInClosedRangeI32(rng, start, end) {
231
+ if (start > end) throw new Error("start must be less than or equal to end");
232
+ const startI32 = start | 0;
233
+ const delta = toMagnitude((end | 0) - startI32, 32);
234
+ if (delta === 4294967295) return rng.nextU32() | 0;
235
+ return startI32 + rngNextWithUpperBoundU32(rng, delta + 1) | 0;
236
+ }
237
+ /**
238
+ * Returns a fixed-size array of random bytes.
239
+ *
240
+ * @param rng - The random number generator to use
241
+ * @param size - The size of the array to return
242
+ * @returns A Uint8Array of the specified size filled with random bytes
243
+ */
244
+ function rngRandomArray(rng, size) {
245
+ const data = new Uint8Array(size);
246
+ rng.fillRandomData(data);
247
+ return data;
248
+ }
249
+ /**
250
+ * Returns a random boolean value.
251
+ *
252
+ * @param rng - The random number generator to use
253
+ * @returns A random boolean
254
+ */
255
+ function rngRandomBool(rng) {
256
+ return (rng.nextU32() & 1) === 0;
257
+ }
258
+ /**
259
+ * Returns a random 32-bit unsigned integer.
260
+ *
261
+ * @param rng - The random number generator to use
262
+ * @returns A random u32 value
263
+ */
264
+ function rngRandomU32(rng) {
265
+ return rng.nextU32();
266
+ }
267
+
268
+ //#endregion
269
+ //#region src/secure-random.ts
270
+ /**
271
+ * Detects the crypto API available in the current environment.
272
+ * Works in both Node.js and browser environments.
273
+ */
274
+ function getCrypto() {
275
+ if (typeof globalThis !== "undefined" && globalThis.crypto !== null && globalThis.crypto !== void 0) return globalThis.crypto;
276
+ if (typeof globalThis.crypto !== "undefined") return globalThis.crypto;
277
+ throw new Error("No crypto API available in this environment");
278
+ }
279
+ /**
280
+ * Generate a Uint8Array of cryptographically strong random bytes of the given size.
281
+ */
282
+ function randomData(size) {
283
+ const data = new Uint8Array(size);
284
+ fillRandomData(data);
285
+ return data;
286
+ }
287
+ /**
288
+ * Fill the given Uint8Array with cryptographically strong random bytes.
289
+ */
290
+ function fillRandomData(data) {
291
+ getCrypto().getRandomValues(data);
292
+ }
293
+ /**
294
+ * Returns the next cryptographically strong random 64-bit unsigned integer.
295
+ */
296
+ function nextU64() {
297
+ const data = new Uint8Array(8);
298
+ fillRandomData(data);
299
+ return new DataView(data.buffer).getBigUint64(0, true);
300
+ }
301
+ /**
302
+ * A random number generator that can be used as a source of
303
+ * cryptographically-strong randomness.
304
+ *
305
+ * Uses the Web Crypto API (crypto.getRandomValues) which is available
306
+ * in both browsers and Node.js >= 15.
307
+ */
308
+ var SecureRandomNumberGenerator = class {
309
+ /**
310
+ * Returns the next random 32-bit unsigned integer.
311
+ */
312
+ nextU32() {
313
+ const data = new Uint8Array(4);
314
+ fillRandomData(data);
315
+ return new DataView(data.buffer).getUint32(0, true) >>> 0;
316
+ }
317
+ /**
318
+ * Returns the next random 64-bit unsigned integer as a bigint.
319
+ */
320
+ nextU64() {
321
+ return nextU64();
322
+ }
323
+ /**
324
+ * Fills the given Uint8Array with random bytes.
325
+ */
326
+ fillBytes(dest) {
327
+ fillRandomData(dest);
328
+ }
329
+ /**
330
+ * Returns a Uint8Array of random bytes of the given size.
331
+ */
332
+ randomData(size) {
333
+ return randomData(size);
334
+ }
335
+ /**
336
+ * Fills the given Uint8Array with random bytes.
337
+ */
338
+ fillRandomData(data) {
339
+ fillRandomData(data);
340
+ }
341
+ };
342
+
343
+ //#endregion
344
+ //#region src/seeded-random.ts
345
+ /**
346
+ * Rotate left for 64-bit bigint
347
+ */
348
+ function rotl(x, k) {
349
+ return (x << BigInt(k) | x >> BigInt(64 - k)) & 18446744073709551615n;
350
+ }
351
+ /**
352
+ * Xoshiro256** PRNG implementation
353
+ * This is the same algorithm used by rand_xoshiro in Rust
354
+ */
355
+ function xoshiro256StarStar(state) {
356
+ const mask = 18446744073709551615n;
357
+ const result = rotl(state.s1 * 5n & mask, 7) * 9n & mask;
358
+ const t = state.s1 << 17n & mask;
359
+ state.s2 ^= state.s0;
360
+ state.s3 ^= state.s1;
361
+ state.s1 ^= state.s2;
362
+ state.s0 ^= state.s3;
363
+ state.s2 ^= t;
364
+ state.s3 = rotl(state.s3, 45);
365
+ return result;
366
+ }
367
+ /**
368
+ * A random number generator that can be used as a source of deterministic
369
+ * pseudo-randomness for testing purposes.
370
+ *
371
+ * Uses the Xoshiro256** algorithm, which is the same algorithm used by
372
+ * rand_xoshiro in Rust. This ensures cross-platform compatibility with
373
+ * the Rust implementation.
374
+ *
375
+ * WARNING: This is NOT cryptographically secure and should only be used
376
+ * for testing purposes.
377
+ */
378
+ var SeededRandomNumberGenerator = class SeededRandomNumberGenerator {
379
+ state;
380
+ /**
381
+ * Creates a new seeded random number generator.
382
+ *
383
+ * The seed should be a 256-bit value, represented as an array of 4 64-bit
384
+ * integers (as bigints). For the output distribution to look random, the seed
385
+ * should not have any obvious patterns, like all zeroes or all ones.
386
+ *
387
+ * This is not cryptographically secure, and should only be used for
388
+ * testing purposes.
389
+ *
390
+ * @param seed - Array of 4 64-bit unsigned integers as bigints
391
+ */
392
+ constructor(seed) {
393
+ this.state = {
394
+ s0: seed[0] & 18446744073709551615n,
395
+ s1: seed[1] & 18446744073709551615n,
396
+ s2: seed[2] & 18446744073709551615n,
397
+ s3: seed[3] & 18446744073709551615n
398
+ };
399
+ }
400
+ /**
401
+ * Creates a new seeded random number generator from a seed array.
402
+ * Convenience method that accepts numbers and converts to bigints.
403
+ *
404
+ * @param seed - Array of 4 64-bit unsigned integers
405
+ */
406
+ static fromSeed(seed) {
407
+ return new SeededRandomNumberGenerator(seed);
408
+ }
409
+ /**
410
+ * Returns the next random 64-bit unsigned integer as a bigint.
411
+ */
412
+ nextU64() {
413
+ return xoshiro256StarStar(this.state);
414
+ }
415
+ /**
416
+ * Returns the next random 32-bit unsigned integer.
417
+ */
418
+ nextU32() {
419
+ return Number(this.nextU64() & 4294967295n) >>> 0;
420
+ }
421
+ /**
422
+ * Fills the given Uint8Array with random bytes.
423
+ *
424
+ * Note: This implementation matches the Rust behavior exactly -
425
+ * it uses one nextU64() call per byte (taking only the low byte),
426
+ * which matches the Swift version's behavior.
427
+ */
428
+ fillBytes(dest) {
429
+ for (let i = 0; i < dest.length; i++) dest[i] = Number(this.nextU64() & 255n);
430
+ }
431
+ /**
432
+ * Returns a Uint8Array of random bytes of the given size.
433
+ *
434
+ * This might not be the most efficient implementation,
435
+ * but it works the same as the Swift version.
436
+ */
437
+ randomData(size) {
438
+ const data = new Uint8Array(size);
439
+ for (let i = 0; i < size; i++) data[i] = Number(this.nextU64() & 255n);
440
+ return data;
441
+ }
442
+ /**
443
+ * Fills the given Uint8Array with random bytes.
444
+ */
445
+ fillRandomData(data) {
446
+ this.fillBytes(data);
447
+ }
448
+ };
449
+ /**
450
+ * The standard test seed used across all Blockchain Commons implementations.
451
+ */
452
+ const TEST_SEED = [
453
+ 17295166580085024720n,
454
+ 422929670265678780n,
455
+ 5577237070365765850n,
456
+ 7953171132032326923n
457
+ ];
458
+ /**
459
+ * Creates a seeded random number generator with a fixed seed.
460
+ * This is useful for reproducible testing across different platforms.
461
+ */
462
+ function makeFakeRandomNumberGenerator() {
463
+ return new SeededRandomNumberGenerator(TEST_SEED);
464
+ }
465
+ /**
466
+ * Creates a Uint8Array of random data with a fixed seed.
467
+ * This is useful for reproducible testing.
468
+ *
469
+ * @param size - The number of bytes to generate
470
+ * @returns A Uint8Array of pseudo-random bytes
471
+ */
472
+ function fakeRandomData(size) {
473
+ return makeFakeRandomNumberGenerator().randomData(size);
474
+ }
475
+
476
+ //#endregion
477
+ export { SecureRandomNumberGenerator, SeededRandomNumberGenerator, TEST_SEED, fakeRandomData, fillRandomData, fromMagnitude, fromMagnitude64, makeFakeRandomNumberGenerator, nextU64, randomData, rngFillRandomData, rngNextInClosedRange, rngNextInClosedRangeI32, rngNextInRange, rngNextInRangeI32, rngNextWithUpperBound, rngNextWithUpperBoundU32, rngRandomArray, rngRandomBool, rngRandomData, rngRandomU32, toMagnitude, toMagnitude64, wideMul, wideMulU16, wideMulU32, wideMulU64, wideMulU8 };
478
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["TEST_SEED: [bigint, bigint, bigint, bigint]"],"sources":["../src/widening.ts","../src/magnitude.ts","../src/random-number-generator.ts","../src/secure-random.ts","../src/seeded-random.ts"],"sourcesContent":["// The below is so we don't have to use #![feature(bigint_helper_methods)]\n// Ported from bc-rand-rust/src/widening.rs\n\n/**\n * Wide multiplication result type - returns (low, high) parts.\n * For a multiplication of two N-bit values, the result is 2N bits\n * split into two N-bit parts.\n */\nexport type WideMulResult = [bigint, bigint];\n\n/**\n * Performs wide multiplication for unsigned integers.\n * Returns (low, high) parts of the full-width result.\n *\n * This is equivalent to Rust's widening_mul for unsigned types.\n */\nexport function wideMul(a: bigint, b: bigint, bits: number): WideMulResult {\n const mask = (1n << BigInt(bits)) - 1n;\n const wide = (a & mask) * (b & mask);\n const low = wide & mask;\n const high = wide >> BigInt(bits);\n return [low, high];\n}\n\n/**\n * Wide multiplication for 8-bit unsigned integers.\n * @param a - First 8-bit value\n * @param b - Second 8-bit value\n * @returns Tuple of (low 8 bits, high 8 bits)\n */\nexport function wideMulU8(a: number, b: number): [number, number] {\n const wide = (a & 0xff) * (b & 0xff);\n return [wide & 0xff, (wide >> 8) & 0xff];\n}\n\n/**\n * Wide multiplication for 16-bit unsigned integers.\n * @param a - First 16-bit value\n * @param b - Second 16-bit value\n * @returns Tuple of (low 16 bits, high 16 bits)\n */\nexport function wideMulU16(a: number, b: number): [number, number] {\n const wide = (a & 0xffff) * (b & 0xffff);\n return [wide & 0xffff, (wide >>> 16) & 0xffff];\n}\n\n/**\n * Wide multiplication for 32-bit unsigned integers.\n * @param a - First 32-bit value\n * @param b - Second 32-bit value\n * @returns Tuple of (low 32 bits, high 32 bits) as bigints\n */\nexport function wideMulU32(a: number, b: number): [bigint, bigint] {\n const aBig = BigInt(a >>> 0);\n const bBig = BigInt(b >>> 0);\n const wide = aBig * bBig;\n return [wide & 0xffffffffn, wide >> 32n];\n}\n\n/**\n * Wide multiplication for 64-bit unsigned integers.\n * @param a - First 64-bit value as bigint\n * @param b - Second 64-bit value as bigint\n * @returns Tuple of (low 64 bits, high 64 bits) as bigints\n */\nexport function wideMulU64(a: bigint, b: bigint): [bigint, bigint] {\n const mask64 = 0xffffffffffffffffn;\n const wide = (a & mask64) * (b & mask64);\n return [wide & mask64, wide >> 64n];\n}\n","// Ported from bc-rand-rust/src/magnitude.rs\n\n/**\n * Converts a signed integer to its unsigned magnitude.\n * For positive numbers, returns the number unchanged.\n * For negative numbers, returns the absolute value (wrapping for MIN values).\n *\n * This matches Rust's wrapping_abs() behavior.\n */\nexport function toMagnitude(value: number, bits: 8 | 16 | 32): number {\n switch (bits) {\n case 8: {\n // i8 to u8: wrapping_abs\n const i8Value = (value << 24) >> 24; // Sign extend to i8\n return Math.abs(i8Value) & 0xff;\n }\n case 16: {\n // i16 to u16: wrapping_abs\n const i16Value = (value << 16) >> 16; // Sign extend to i16\n return Math.abs(i16Value) & 0xffff;\n }\n case 32: {\n // i32 to u32: wrapping_abs\n const i32Value = value | 0; // Force to i32\n // Handle MIN_VALUE specially (wrapping behavior)\n if (i32Value === -2147483648) {\n return 2147483648; // Returns as positive\n }\n return Math.abs(i32Value) >>> 0; // Convert to unsigned\n }\n }\n}\n\n/**\n * Converts a signed bigint to its unsigned magnitude for 64-bit values.\n */\nexport function toMagnitude64(value: bigint): bigint {\n const mask = 0xffffffffffffffffn;\n // Handle negative values\n if (value < 0n) {\n // Wrapping absolute value\n const absValue = -value;\n return absValue & mask;\n }\n return value & mask;\n}\n\n/**\n * Converts an unsigned magnitude back to a signed value.\n * Simply reinterprets the bits.\n */\nexport function fromMagnitude(magnitude: number, bits: 8 | 16 | 32): number {\n switch (bits) {\n case 8:\n return (magnitude << 24) >> 24; // Sign extend from u8 to i8\n case 16:\n return (magnitude << 16) >> 16; // Sign extend from u16 to i16\n case 32:\n return magnitude | 0; // Reinterpret as i32\n }\n}\n\n/**\n * Converts an unsigned 64-bit magnitude back to a signed bigint.\n */\nexport function fromMagnitude64(magnitude: bigint): bigint {\n const mask = 0xffffffffffffffffn;\n const signBit = 1n << 63n;\n const maskedMag = magnitude & mask;\n if ((maskedMag & signBit) !== 0n) {\n // Negative value - convert from two's complement\n return maskedMag - (1n << 64n);\n }\n return maskedMag;\n}\n","// Ported from bc-rand-rust/src/random_number_generator.rs\n\nimport { wideMulU32, wideMulU64 } from \"./widening.js\";\nimport {\n toMagnitude,\n toMagnitude64 as _toMagnitude64,\n fromMagnitude as _fromMagnitude,\n fromMagnitude64 as _fromMagnitude64,\n} from \"./magnitude.js\";\n\n/**\n * Interface for random number generators.\n * This is the TypeScript equivalent of Rust's RandomNumberGenerator trait\n * which extends RngCore + CryptoRng.\n *\n * This is compatible with the RandomNumberGenerator Swift protocol used\n * in MacOS and iOS, which is important for cross-platform testing.\n */\nexport interface RandomNumberGenerator {\n /**\n * Returns the next random 32-bit unsigned integer.\n */\n nextU32(): number;\n\n /**\n * Returns the next random 64-bit unsigned integer as a bigint.\n */\n nextU64(): bigint;\n\n /**\n * Fills the given Uint8Array with random bytes.\n */\n fillBytes(dest: Uint8Array): void;\n\n /**\n * Returns a Uint8Array of random bytes of the given size.\n */\n randomData(size: number): Uint8Array;\n\n /**\n * Fills the given Uint8Array with random bytes.\n * Alias for fillBytes for compatibility.\n */\n fillRandomData(data: Uint8Array): void;\n}\n\n/**\n * Returns a Uint8Array of random bytes of the given size.\n */\nexport function rngRandomData(rng: RandomNumberGenerator, size: number): Uint8Array {\n const data = new Uint8Array(size);\n rng.fillRandomData(data);\n return data;\n}\n\n/**\n * Fills the given Uint8Array with random bytes.\n */\nexport function rngFillRandomData(rng: RandomNumberGenerator, data: Uint8Array): void {\n rng.fillRandomData(data);\n}\n\n/**\n * Returns a random value that is less than the given upper bound.\n *\n * Uses Lemire's \"nearly divisionless\" method for generating random\n * integers in an interval. For a detailed explanation, see:\n * https://arxiv.org/abs/1805.10941\n *\n * @param rng - The random number generator to use\n * @param upperBound - The upper bound for the randomly generated value. Must be non-zero.\n * @returns A random value in the range [0, upperBound). Every value in the range is equally likely.\n */\nexport function rngNextWithUpperBound(rng: RandomNumberGenerator, upperBound: bigint): bigint {\n if (upperBound === 0n) {\n throw new Error(\"upperBound must be non-zero\");\n }\n\n // We use Lemire's \"nearly divisionless\" method for generating random\n // integers in an interval. For a detailed explanation, see:\n // https://arxiv.org/abs/1805.10941\n\n const bitmask = 0xffffffffffffffffn; // u64 max\n let random = rng.nextU64() & bitmask;\n let m = wideMulU64(random, upperBound);\n\n if (m[0] < upperBound) {\n // t = (0 - upperBound) % upperBound\n const negUpperBound = (bitmask + 1n - upperBound) & bitmask;\n const t = negUpperBound % upperBound;\n while (m[0] < t) {\n random = rng.nextU64() & bitmask;\n m = wideMulU64(random, upperBound);\n }\n }\n\n return m[1];\n}\n\n/**\n * Returns a random 32-bit value that is less than the given upper bound.\n * This matches Rust's behavior when called with u32 type.\n *\n * Uses Lemire's \"nearly divisionless\" method with 32-bit arithmetic.\n *\n * @param rng - The random number generator to use\n * @param upperBound - The upper bound for the randomly generated value. Must be non-zero and fit in u32.\n * @returns A random u32 value in the range [0, upperBound).\n */\nexport function rngNextWithUpperBoundU32(rng: RandomNumberGenerator, upperBound: number): number {\n if (upperBound === 0) {\n throw new Error(\"upperBound must be non-zero\");\n }\n\n const upperBoundU32 = upperBound >>> 0;\n const bitmask = 0xffffffff;\n\n // Get random and mask to 32 bits (matches Rust behavior)\n let random = Number(rng.nextU64() & BigInt(bitmask));\n let m = wideMulU32(random, upperBoundU32);\n\n if (Number(m[0]) < upperBoundU32) {\n // t = (0 - upperBound) % upperBound (wrapping subtraction)\n const t = ((bitmask + 1 - upperBoundU32) >>> 0) % upperBoundU32;\n while (Number(m[0]) < t) {\n random = Number(rng.nextU64() & BigInt(bitmask));\n m = wideMulU32(random, upperBoundU32);\n }\n }\n\n return Number(m[1]);\n}\n\n/**\n * Returns a random value within the specified range [start, end) using 32-bit arithmetic.\n * This matches Rust's behavior when called with i32 types.\n *\n * @param rng - The random number generator to use\n * @param start - The lower bound (inclusive) as i32\n * @param end - The upper bound (exclusive) as i32\n * @returns A random i32 value within the bounds\n */\nexport function rngNextInRangeI32(rng: RandomNumberGenerator, start: number, end: number): number {\n if (start >= end) {\n throw new Error(\"start must be less than end\");\n }\n\n const startI32 = start | 0;\n const endI32 = end | 0;\n const delta = toMagnitude(endI32 - startI32, 32);\n\n if (delta === 0xffffffff) {\n return rng.nextU32() | 0;\n }\n\n const random = rngNextWithUpperBoundU32(rng, delta);\n return (startI32 + random) | 0;\n}\n\n/**\n * Returns a random value within the specified range [start, end).\n *\n * @param rng - The random number generator to use\n * @param start - The lower bound (inclusive)\n * @param end - The upper bound (exclusive)\n * @returns A random value within the bounds of the range\n */\nexport function rngNextInRange(rng: RandomNumberGenerator, start: bigint, end: bigint): bigint {\n if (start >= end) {\n throw new Error(\"start must be less than end\");\n }\n\n const delta = end - start;\n\n // If delta covers the entire range, just return a random value\n const maxU64 = 0xffffffffffffffffn;\n if (delta === maxU64) {\n return rng.nextU64();\n }\n\n const random = rngNextWithUpperBound(rng, delta);\n return start + random;\n}\n\n/**\n * Returns a random value within the specified closed range [start, end].\n *\n * @param rng - The random number generator to use\n * @param start - The lower bound (inclusive)\n * @param end - The upper bound (inclusive)\n * @returns A random value within the bounds of the range\n */\nexport function rngNextInClosedRange(\n rng: RandomNumberGenerator,\n start: bigint,\n end: bigint,\n): bigint {\n if (start > end) {\n throw new Error(\"start must be less than or equal to end\");\n }\n\n const delta = end - start;\n\n // If delta covers the entire range, just return a random value\n const maxU64 = 0xffffffffffffffffn;\n if (delta === maxU64) {\n return rng.nextU64();\n }\n\n const random = rngNextWithUpperBound(rng, delta + 1n);\n return start + random;\n}\n\n/**\n * Returns a random value within the specified closed range [start, end] for i32 values.\n * Convenience function that handles signed 32-bit integers.\n *\n * @param rng - The random number generator to use\n * @param start - The lower bound (inclusive) as i32\n * @param end - The upper bound (inclusive) as i32\n * @returns A random i32 value within the bounds of the range\n */\nexport function rngNextInClosedRangeI32(\n rng: RandomNumberGenerator,\n start: number,\n end: number,\n): number {\n if (start > end) {\n throw new Error(\"start must be less than or equal to end\");\n }\n\n // Convert to i32\n const startI32 = start | 0;\n const endI32 = end | 0;\n\n // Calculate delta as u32 magnitude\n const delta = toMagnitude(endI32 - startI32, 32);\n\n // If delta covers the entire u32 range, just return a random value\n if (delta === 0xffffffff) {\n return rng.nextU32() | 0;\n }\n\n const random = rngNextWithUpperBoundU32(rng, delta + 1);\n return (startI32 + random) | 0;\n}\n\n/**\n * Returns a fixed-size array of random bytes.\n *\n * @param rng - The random number generator to use\n * @param size - The size of the array to return\n * @returns A Uint8Array of the specified size filled with random bytes\n */\nexport function rngRandomArray(rng: RandomNumberGenerator, size: number): Uint8Array {\n const data = new Uint8Array(size);\n rng.fillRandomData(data);\n return data;\n}\n\n/**\n * Returns a random boolean value.\n *\n * @param rng - The random number generator to use\n * @returns A random boolean\n */\nexport function rngRandomBool(rng: RandomNumberGenerator): boolean {\n return (rng.nextU32() & 1) === 0;\n}\n\n/**\n * Returns a random 32-bit unsigned integer.\n *\n * @param rng - The random number generator to use\n * @returns A random u32 value\n */\nexport function rngRandomU32(rng: RandomNumberGenerator): number {\n return rng.nextU32();\n}\n","// Ported from bc-rand-rust/src/secure_random.rs\n\nimport type { RandomNumberGenerator } from \"./random-number-generator.js\";\n\n/**\n * Detects the crypto API available in the current environment.\n * Works in both Node.js and browser environments.\n */\nfunction getCrypto(): Crypto {\n // Browser environment\n if (\n typeof globalThis !== \"undefined\" &&\n globalThis.crypto !== null &&\n globalThis.crypto !== undefined\n ) {\n return globalThis.crypto;\n }\n // Node.js environment - globalThis.crypto is also available in Node.js >= 15\n if (typeof globalThis.crypto !== \"undefined\") {\n return globalThis.crypto;\n }\n throw new Error(\"No crypto API available in this environment\");\n}\n\n/**\n * Generate a Uint8Array of cryptographically strong random bytes of the given size.\n */\nexport function randomData(size: number): Uint8Array {\n const data = new Uint8Array(size);\n fillRandomData(data);\n return data;\n}\n\n/**\n * Fill the given Uint8Array with cryptographically strong random bytes.\n */\nexport function fillRandomData(data: Uint8Array): void {\n const crypto = getCrypto();\n crypto.getRandomValues(data);\n}\n\n/**\n * Returns the next cryptographically strong random 64-bit unsigned integer.\n */\nexport function nextU64(): bigint {\n const data = new Uint8Array(8);\n fillRandomData(data);\n const view = new DataView(data.buffer);\n return view.getBigUint64(0, true); // little-endian\n}\n\n/**\n * A random number generator that can be used as a source of\n * cryptographically-strong randomness.\n *\n * Uses the Web Crypto API (crypto.getRandomValues) which is available\n * in both browsers and Node.js >= 15.\n */\nexport class SecureRandomNumberGenerator implements RandomNumberGenerator {\n /**\n * Returns the next random 32-bit unsigned integer.\n */\n nextU32(): number {\n const data = new Uint8Array(4);\n fillRandomData(data);\n const view = new DataView(data.buffer);\n return view.getUint32(0, true) >>> 0; // little-endian, unsigned\n }\n\n /**\n * Returns the next random 64-bit unsigned integer as a bigint.\n */\n nextU64(): bigint {\n return nextU64();\n }\n\n /**\n * Fills the given Uint8Array with random bytes.\n */\n fillBytes(dest: Uint8Array): void {\n fillRandomData(dest);\n }\n\n /**\n * Returns a Uint8Array of random bytes of the given size.\n */\n randomData(size: number): Uint8Array {\n return randomData(size);\n }\n\n /**\n * Fills the given Uint8Array with random bytes.\n */\n fillRandomData(data: Uint8Array): void {\n fillRandomData(data);\n }\n}\n","// Ported from bc-rand-rust/src/seeded_random.rs\n\nimport type { RandomNumberGenerator } from \"./random-number-generator.js\";\n\n/**\n * Xoshiro256** state\n * Based on xoshiro256** 1.0 by David Blackman and Sebastiano Vigna\n * https://prng.di.unimi.it/\n */\ninterface Xoshiro256State {\n s0: bigint;\n s1: bigint;\n s2: bigint;\n s3: bigint;\n}\n\n/**\n * Rotate left for 64-bit bigint\n */\nfunction rotl(x: bigint, k: number): bigint {\n const mask = 0xffffffffffffffffn;\n return ((x << BigInt(k)) | (x >> BigInt(64 - k))) & mask;\n}\n\n/**\n * Xoshiro256** PRNG implementation\n * This is the same algorithm used by rand_xoshiro in Rust\n */\nfunction xoshiro256StarStar(state: Xoshiro256State): bigint {\n const mask = 0xffffffffffffffffn;\n\n // result = rotl(s1 * 5, 7) * 9\n const result = (rotl((state.s1 * 5n) & mask, 7) * 9n) & mask;\n\n // t = s1 << 17\n const t = (state.s1 << 17n) & mask;\n\n // Update state\n state.s2 ^= state.s0;\n state.s3 ^= state.s1;\n state.s1 ^= state.s2;\n state.s0 ^= state.s3;\n\n state.s2 ^= t;\n state.s3 = rotl(state.s3, 45);\n\n return result;\n}\n\n/**\n * A random number generator that can be used as a source of deterministic\n * pseudo-randomness for testing purposes.\n *\n * Uses the Xoshiro256** algorithm, which is the same algorithm used by\n * rand_xoshiro in Rust. This ensures cross-platform compatibility with\n * the Rust implementation.\n *\n * WARNING: This is NOT cryptographically secure and should only be used\n * for testing purposes.\n */\nexport class SeededRandomNumberGenerator implements RandomNumberGenerator {\n private readonly state: Xoshiro256State;\n\n /**\n * Creates a new seeded random number generator.\n *\n * The seed should be a 256-bit value, represented as an array of 4 64-bit\n * integers (as bigints). For the output distribution to look random, the seed\n * should not have any obvious patterns, like all zeroes or all ones.\n *\n * This is not cryptographically secure, and should only be used for\n * testing purposes.\n *\n * @param seed - Array of 4 64-bit unsigned integers as bigints\n */\n constructor(seed: [bigint, bigint, bigint, bigint]) {\n this.state = {\n s0: seed[0] & 0xffffffffffffffffn,\n s1: seed[1] & 0xffffffffffffffffn,\n s2: seed[2] & 0xffffffffffffffffn,\n s3: seed[3] & 0xffffffffffffffffn,\n };\n }\n\n /**\n * Creates a new seeded random number generator from a seed array.\n * Convenience method that accepts numbers and converts to bigints.\n *\n * @param seed - Array of 4 64-bit unsigned integers\n */\n static fromSeed(seed: [bigint, bigint, bigint, bigint]): SeededRandomNumberGenerator {\n return new SeededRandomNumberGenerator(seed);\n }\n\n /**\n * Returns the next random 64-bit unsigned integer as a bigint.\n */\n nextU64(): bigint {\n return xoshiro256StarStar(this.state);\n }\n\n /**\n * Returns the next random 32-bit unsigned integer.\n */\n nextU32(): number {\n return Number(this.nextU64() & 0xffffffffn) >>> 0;\n }\n\n /**\n * Fills the given Uint8Array with random bytes.\n *\n * Note: This implementation matches the Rust behavior exactly -\n * it uses one nextU64() call per byte (taking only the low byte),\n * which matches the Swift version's behavior.\n */\n fillBytes(dest: Uint8Array): void {\n for (let i = 0; i < dest.length; i++) {\n dest[i] = Number(this.nextU64() & 0xffn);\n }\n }\n\n /**\n * Returns a Uint8Array of random bytes of the given size.\n *\n * This might not be the most efficient implementation,\n * but it works the same as the Swift version.\n */\n randomData(size: number): Uint8Array {\n const data = new Uint8Array(size);\n for (let i = 0; i < size; i++) {\n data[i] = Number(this.nextU64() & 0xffn);\n }\n return data;\n }\n\n /**\n * Fills the given Uint8Array with random bytes.\n */\n fillRandomData(data: Uint8Array): void {\n this.fillBytes(data);\n }\n}\n\n/**\n * The standard test seed used across all Blockchain Commons implementations.\n */\nexport const TEST_SEED: [bigint, bigint, bigint, bigint] = [\n 17295166580085024720n,\n 422929670265678780n,\n 5577237070365765850n,\n 7953171132032326923n,\n];\n\n/**\n * Creates a seeded random number generator with a fixed seed.\n * This is useful for reproducible testing across different platforms.\n */\nexport function makeFakeRandomNumberGenerator(): SeededRandomNumberGenerator {\n return new SeededRandomNumberGenerator(TEST_SEED);\n}\n\n/**\n * Creates a Uint8Array of random data with a fixed seed.\n * This is useful for reproducible testing.\n *\n * @param size - The number of bytes to generate\n * @returns A Uint8Array of pseudo-random bytes\n */\nexport function fakeRandomData(size: number): Uint8Array {\n return makeFakeRandomNumberGenerator().randomData(size);\n}\n"],"mappings":";;;;;;;AAgBA,SAAgB,QAAQ,GAAW,GAAW,MAA6B;CACzE,MAAM,QAAQ,MAAM,OAAO,KAAK,IAAI;CACpC,MAAM,QAAQ,IAAI,SAAS,IAAI;AAG/B,QAAO,CAFK,OAAO,MACN,QAAQ,OAAO,KAAK,CACf;;;;;;;;AASpB,SAAgB,UAAU,GAAW,GAA6B;CAChE,MAAM,QAAQ,IAAI,QAAS,IAAI;AAC/B,QAAO,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ClD,OAAK,QAAQ;GACX,IAAI,KAAK,KAAK;GACd,IAAI,KAAK,KAAK;GACd,IAAI,KAAK,KAAK;GACd,IAAI,KAAK,KAAK;GACf;;;;;;;;CASH,OAAO,SAAS,MAAqE;AACnF,SAAO,IAAI,4BAA4B,KAAK;;;;;CAM9C,UAAkB;AAChB,SAAO,mBAAmB,KAAK,MAAM;;;;;CAMvC,UAAkB;AAChB,SAAO,OAAO,KAAK,SAAS,GAAG,YAAY,KAAK;;;;;;;;;CAUlD,UAAU,MAAwB;AAChC,OAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,IAC/B,MAAK,KAAK,OAAO,KAAK,SAAS,GAAG,KAAM;;;;;;;;CAU5C,WAAW,MAA0B;EACnC,MAAM,OAAO,IAAI,WAAW,KAAK;AACjC,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IACxB,MAAK,KAAK,OAAO,KAAK,SAAS,GAAG,KAAM;AAE1C,SAAO;;;;;CAMT,eAAe,MAAwB;AACrC,OAAK,UAAU,KAAK;;;;;;AAOxB,MAAaA,YAA8C;CACzD;CACA;CACA;CACA;CACD;;;;;AAMD,SAAgB,gCAA6D;AAC3E,QAAO,IAAI,4BAA4B,UAAU;;;;;;;;;AAUnD,SAAgB,eAAe,MAA0B;AACvD,QAAO,+BAA+B,CAAC,WAAW,KAAK"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@bcts/rand",
3
+ "version": "1.0.0-alpha.10",
4
+ "type": "module",
5
+ "description": "Blockchain Commons Random Number Utilities for TypeScript",
6
+ "license": "BSD-2-Clause-Patent",
7
+ "author": "Leonardo Custodio <leonardo@custodio.me>",
8
+ "homepage": "https://bcts.dev",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leonardocustodio/bcts",
12
+ "directory": "packages/rand"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/leonardocustodio/bcts/issues"
16
+ },
17
+ "main": "dist/index.cjs",
18
+ "module": "dist/index.mjs",
19
+ "types": "dist/index.d.mts",
20
+ "browser": "dist/index.iife.js",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.mts",
24
+ "import": "./dist/index.mjs",
25
+ "require": "./dist/index.cjs",
26
+ "default": "./dist/index.mjs"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "src",
32
+ "README.md"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsdown",
36
+ "dev": "tsdown --watch",
37
+ "test": "vitest run",
38
+ "test:watch": "vitest",
39
+ "lint": "eslint 'src/**/*.ts'",
40
+ "lint:fix": "eslint 'src/**/*.ts' --fix",
41
+ "typecheck": "tsc --noEmit",
42
+ "clean": "rm -rf dist",
43
+ "docs": "typedoc",
44
+ "prepublishOnly": "npm run clean && npm run build && npm test"
45
+ },
46
+ "keywords": [
47
+ "random",
48
+ "rng",
49
+ "blockchain-commons",
50
+ "cryptography",
51
+ "testing",
52
+ "security"
53
+ ],
54
+ "engines": {
55
+ "node": ">=18.0.0"
56
+ },
57
+ "devDependencies": {
58
+ "@bcts/eslint": "^0.1.0",
59
+ "@bcts/tsconfig": "^0.1.0",
60
+ "@eslint/js": "^9.39.2",
61
+ "@types/node": "^25.0.3",
62
+ "eslint": "^9.39.2",
63
+ "ts-node": "^10.9.2",
64
+ "tsdown": "^0.18.3",
65
+ "typedoc": "^0.28.15",
66
+ "typescript": "^5.9.3",
67
+ "vitest": "^4.0.16"
68
+ }
69
+ }
package/src/index.ts ADDED
@@ -0,0 +1,57 @@
1
+ // Blockchain Commons Random Number Utilities
2
+ // Ported from bc-rand-rust
3
+ //
4
+ // `bc-rand` exposes a uniform API for the random number primitives used
5
+ // in higher-level Blockchain Commons projects, including a cryptographically
6
+ // strong random number generator `SecureRandomNumberGenerator` and a
7
+ // deterministic random number generator `SeededRandomNumberGenerator`.
8
+ //
9
+ // These primitive random number generators implement the `RandomNumberGenerator`
10
+ // interface to produce random numbers compatible with the RandomNumberGenerator
11
+ // Swift protocol used in MacOS and iOS, which is important when using the
12
+ // deterministic random number generator for cross-platform testing.
13
+
14
+ // Widening multiplication utilities
15
+ export {
16
+ wideMul,
17
+ wideMulU8,
18
+ wideMulU16,
19
+ wideMulU32,
20
+ wideMulU64,
21
+ type WideMulResult,
22
+ } from "./widening.js";
23
+
24
+ // Magnitude conversion utilities
25
+ export { toMagnitude, toMagnitude64, fromMagnitude, fromMagnitude64 } from "./magnitude.js";
26
+
27
+ // Random number generator interface and utilities
28
+ export {
29
+ type RandomNumberGenerator,
30
+ rngRandomData,
31
+ rngFillRandomData,
32
+ rngNextWithUpperBound,
33
+ rngNextWithUpperBoundU32,
34
+ rngNextInRange,
35
+ rngNextInRangeI32,
36
+ rngNextInClosedRange,
37
+ rngNextInClosedRangeI32,
38
+ rngRandomArray,
39
+ rngRandomBool,
40
+ rngRandomU32,
41
+ } from "./random-number-generator.js";
42
+
43
+ // Secure random number generator (cryptographically strong)
44
+ export {
45
+ SecureRandomNumberGenerator,
46
+ randomData,
47
+ fillRandomData,
48
+ nextU64,
49
+ } from "./secure-random.js";
50
+
51
+ // Seeded random number generator (deterministic, for testing)
52
+ export {
53
+ SeededRandomNumberGenerator,
54
+ TEST_SEED,
55
+ makeFakeRandomNumberGenerator,
56
+ fakeRandomData,
57
+ } from "./seeded-random.js";