@bcts/rand 1.0.0-alpha.5

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.
@@ -0,0 +1,315 @@
1
+ //#region src/widening.d.ts
2
+ /**
3
+ * Wide multiplication result type - returns (low, high) parts.
4
+ * For a multiplication of two N-bit values, the result is 2N bits
5
+ * split into two N-bit parts.
6
+ */
7
+ type WideMulResult = [bigint, bigint];
8
+ /**
9
+ * Performs wide multiplication for unsigned integers.
10
+ * Returns (low, high) parts of the full-width result.
11
+ *
12
+ * This is equivalent to Rust's widening_mul for unsigned types.
13
+ */
14
+ declare function wideMul(a: bigint, b: bigint, bits: number): WideMulResult;
15
+ /**
16
+ * Wide multiplication for 8-bit unsigned integers.
17
+ * @param a - First 8-bit value
18
+ * @param b - Second 8-bit value
19
+ * @returns Tuple of (low 8 bits, high 8 bits)
20
+ */
21
+ declare function wideMulU8(a: number, b: number): [number, number];
22
+ /**
23
+ * Wide multiplication for 16-bit unsigned integers.
24
+ * @param a - First 16-bit value
25
+ * @param b - Second 16-bit value
26
+ * @returns Tuple of (low 16 bits, high 16 bits)
27
+ */
28
+ declare function wideMulU16(a: number, b: number): [number, number];
29
+ /**
30
+ * Wide multiplication for 32-bit unsigned integers.
31
+ * @param a - First 32-bit value
32
+ * @param b - Second 32-bit value
33
+ * @returns Tuple of (low 32 bits, high 32 bits) as bigints
34
+ */
35
+ declare function wideMulU32(a: number, b: number): [bigint, bigint];
36
+ /**
37
+ * Wide multiplication for 64-bit unsigned integers.
38
+ * @param a - First 64-bit value as bigint
39
+ * @param b - Second 64-bit value as bigint
40
+ * @returns Tuple of (low 64 bits, high 64 bits) as bigints
41
+ */
42
+ declare function wideMulU64(a: bigint, b: bigint): [bigint, bigint];
43
+ //#endregion
44
+ //#region src/magnitude.d.ts
45
+ /**
46
+ * Converts a signed integer to its unsigned magnitude.
47
+ * For positive numbers, returns the number unchanged.
48
+ * For negative numbers, returns the absolute value (wrapping for MIN values).
49
+ *
50
+ * This matches Rust's wrapping_abs() behavior.
51
+ */
52
+ declare function toMagnitude(value: number, bits: 8 | 16 | 32): number;
53
+ /**
54
+ * Converts a signed bigint to its unsigned magnitude for 64-bit values.
55
+ */
56
+ declare function toMagnitude64(value: bigint): bigint;
57
+ /**
58
+ * Converts an unsigned magnitude back to a signed value.
59
+ * Simply reinterprets the bits.
60
+ */
61
+ declare function fromMagnitude(magnitude: number, bits: 8 | 16 | 32): number;
62
+ /**
63
+ * Converts an unsigned 64-bit magnitude back to a signed bigint.
64
+ */
65
+ declare function fromMagnitude64(magnitude: bigint): bigint;
66
+ //#endregion
67
+ //#region src/random-number-generator.d.ts
68
+ /**
69
+ * Interface for random number generators.
70
+ * This is the TypeScript equivalent of Rust's RandomNumberGenerator trait
71
+ * which extends RngCore + CryptoRng.
72
+ *
73
+ * This is compatible with the RandomNumberGenerator Swift protocol used
74
+ * in MacOS and iOS, which is important for cross-platform testing.
75
+ */
76
+ interface RandomNumberGenerator {
77
+ /**
78
+ * Returns the next random 32-bit unsigned integer.
79
+ */
80
+ nextU32(): number;
81
+ /**
82
+ * Returns the next random 64-bit unsigned integer as a bigint.
83
+ */
84
+ nextU64(): bigint;
85
+ /**
86
+ * Fills the given Uint8Array with random bytes.
87
+ */
88
+ fillBytes(dest: Uint8Array): void;
89
+ /**
90
+ * Returns a Uint8Array of random bytes of the given size.
91
+ */
92
+ randomData(size: number): Uint8Array;
93
+ /**
94
+ * Fills the given Uint8Array with random bytes.
95
+ * Alias for fillBytes for compatibility.
96
+ */
97
+ fillRandomData(data: Uint8Array): void;
98
+ }
99
+ /**
100
+ * Returns a Uint8Array of random bytes of the given size.
101
+ */
102
+ declare function rngRandomData(rng: RandomNumberGenerator, size: number): Uint8Array;
103
+ /**
104
+ * Fills the given Uint8Array with random bytes.
105
+ */
106
+ declare function rngFillRandomData(rng: RandomNumberGenerator, data: Uint8Array): void;
107
+ /**
108
+ * Returns a random value that is less than the given upper bound.
109
+ *
110
+ * Uses Lemire's "nearly divisionless" method for generating random
111
+ * integers in an interval. For a detailed explanation, see:
112
+ * https://arxiv.org/abs/1805.10941
113
+ *
114
+ * @param rng - The random number generator to use
115
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero.
116
+ * @returns A random value in the range [0, upperBound). Every value in the range is equally likely.
117
+ */
118
+ declare function rngNextWithUpperBound(rng: RandomNumberGenerator, upperBound: bigint): bigint;
119
+ /**
120
+ * Returns a random 32-bit value that is less than the given upper bound.
121
+ * This matches Rust's behavior when called with u32 type.
122
+ *
123
+ * Uses Lemire's "nearly divisionless" method with 32-bit arithmetic.
124
+ *
125
+ * @param rng - The random number generator to use
126
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero and fit in u32.
127
+ * @returns A random u32 value in the range [0, upperBound).
128
+ */
129
+ declare function rngNextWithUpperBoundU32(rng: RandomNumberGenerator, upperBound: number): number;
130
+ /**
131
+ * Returns a random value within the specified range [start, end) using 32-bit arithmetic.
132
+ * This matches Rust's behavior when called with i32 types.
133
+ *
134
+ * @param rng - The random number generator to use
135
+ * @param start - The lower bound (inclusive) as i32
136
+ * @param end - The upper bound (exclusive) as i32
137
+ * @returns A random i32 value within the bounds
138
+ */
139
+ declare function rngNextInRangeI32(rng: RandomNumberGenerator, start: number, end: number): number;
140
+ /**
141
+ * Returns a random value within the specified range [start, end).
142
+ *
143
+ * @param rng - The random number generator to use
144
+ * @param start - The lower bound (inclusive)
145
+ * @param end - The upper bound (exclusive)
146
+ * @returns A random value within the bounds of the range
147
+ */
148
+ declare function rngNextInRange(rng: RandomNumberGenerator, start: bigint, end: bigint): bigint;
149
+ /**
150
+ * Returns a random value within the specified closed range [start, end].
151
+ *
152
+ * @param rng - The random number generator to use
153
+ * @param start - The lower bound (inclusive)
154
+ * @param end - The upper bound (inclusive)
155
+ * @returns A random value within the bounds of the range
156
+ */
157
+ declare function rngNextInClosedRange(rng: RandomNumberGenerator, start: bigint, end: bigint): bigint;
158
+ /**
159
+ * Returns a random value within the specified closed range [start, end] for i32 values.
160
+ * Convenience function that handles signed 32-bit integers.
161
+ *
162
+ * @param rng - The random number generator to use
163
+ * @param start - The lower bound (inclusive) as i32
164
+ * @param end - The upper bound (inclusive) as i32
165
+ * @returns A random i32 value within the bounds of the range
166
+ */
167
+ declare function rngNextInClosedRangeI32(rng: RandomNumberGenerator, start: number, end: number): number;
168
+ /**
169
+ * Returns a fixed-size array of random bytes.
170
+ *
171
+ * @param rng - The random number generator to use
172
+ * @param size - The size of the array to return
173
+ * @returns A Uint8Array of the specified size filled with random bytes
174
+ */
175
+ declare function rngRandomArray(rng: RandomNumberGenerator, size: number): Uint8Array;
176
+ /**
177
+ * Returns a random boolean value.
178
+ *
179
+ * @param rng - The random number generator to use
180
+ * @returns A random boolean
181
+ */
182
+ declare function rngRandomBool(rng: RandomNumberGenerator): boolean;
183
+ /**
184
+ * Returns a random 32-bit unsigned integer.
185
+ *
186
+ * @param rng - The random number generator to use
187
+ * @returns A random u32 value
188
+ */
189
+ declare function rngRandomU32(rng: RandomNumberGenerator): number;
190
+ //#endregion
191
+ //#region src/secure-random.d.ts
192
+ /**
193
+ * Generate a Uint8Array of cryptographically strong random bytes of the given size.
194
+ */
195
+ declare function randomData(size: number): Uint8Array;
196
+ /**
197
+ * Fill the given Uint8Array with cryptographically strong random bytes.
198
+ */
199
+ declare function fillRandomData(data: Uint8Array): void;
200
+ /**
201
+ * Returns the next cryptographically strong random 64-bit unsigned integer.
202
+ */
203
+ declare function nextU64(): bigint;
204
+ /**
205
+ * A random number generator that can be used as a source of
206
+ * cryptographically-strong randomness.
207
+ *
208
+ * Uses the Web Crypto API (crypto.getRandomValues) which is available
209
+ * in both browsers and Node.js >= 15.
210
+ */
211
+ declare class SecureRandomNumberGenerator implements RandomNumberGenerator {
212
+ /**
213
+ * Returns the next random 32-bit unsigned integer.
214
+ */
215
+ nextU32(): number;
216
+ /**
217
+ * Returns the next random 64-bit unsigned integer as a bigint.
218
+ */
219
+ nextU64(): bigint;
220
+ /**
221
+ * Fills the given Uint8Array with random bytes.
222
+ */
223
+ fillBytes(dest: Uint8Array): void;
224
+ /**
225
+ * Returns a Uint8Array of random bytes of the given size.
226
+ */
227
+ randomData(size: number): Uint8Array;
228
+ /**
229
+ * Fills the given Uint8Array with random bytes.
230
+ */
231
+ fillRandomData(data: Uint8Array): void;
232
+ }
233
+ //#endregion
234
+ //#region src/seeded-random.d.ts
235
+ /**
236
+ * A random number generator that can be used as a source of deterministic
237
+ * pseudo-randomness for testing purposes.
238
+ *
239
+ * Uses the Xoshiro256** algorithm, which is the same algorithm used by
240
+ * rand_xoshiro in Rust. This ensures cross-platform compatibility with
241
+ * the Rust implementation.
242
+ *
243
+ * WARNING: This is NOT cryptographically secure and should only be used
244
+ * for testing purposes.
245
+ */
246
+ declare class SeededRandomNumberGenerator implements RandomNumberGenerator {
247
+ private readonly state;
248
+ /**
249
+ * Creates a new seeded random number generator.
250
+ *
251
+ * The seed should be a 256-bit value, represented as an array of 4 64-bit
252
+ * integers (as bigints). For the output distribution to look random, the seed
253
+ * should not have any obvious patterns, like all zeroes or all ones.
254
+ *
255
+ * This is not cryptographically secure, and should only be used for
256
+ * testing purposes.
257
+ *
258
+ * @param seed - Array of 4 64-bit unsigned integers as bigints
259
+ */
260
+ constructor(seed: [bigint, bigint, bigint, bigint]);
261
+ /**
262
+ * Creates a new seeded random number generator from a seed array.
263
+ * Convenience method that accepts numbers and converts to bigints.
264
+ *
265
+ * @param seed - Array of 4 64-bit unsigned integers
266
+ */
267
+ static fromSeed(seed: [bigint, bigint, bigint, bigint]): SeededRandomNumberGenerator;
268
+ /**
269
+ * Returns the next random 64-bit unsigned integer as a bigint.
270
+ */
271
+ nextU64(): bigint;
272
+ /**
273
+ * Returns the next random 32-bit unsigned integer.
274
+ */
275
+ nextU32(): number;
276
+ /**
277
+ * Fills the given Uint8Array with random bytes.
278
+ *
279
+ * Note: This implementation matches the Rust behavior exactly -
280
+ * it uses one nextU64() call per byte (taking only the low byte),
281
+ * which matches the Swift version's behavior.
282
+ */
283
+ fillBytes(dest: Uint8Array): void;
284
+ /**
285
+ * Returns a Uint8Array of random bytes of the given size.
286
+ *
287
+ * This might not be the most efficient implementation,
288
+ * but it works the same as the Swift version.
289
+ */
290
+ randomData(size: number): Uint8Array;
291
+ /**
292
+ * Fills the given Uint8Array with random bytes.
293
+ */
294
+ fillRandomData(data: Uint8Array): void;
295
+ }
296
+ /**
297
+ * The standard test seed used across all Blockchain Commons implementations.
298
+ */
299
+ declare const TEST_SEED: [bigint, bigint, bigint, bigint];
300
+ /**
301
+ * Creates a seeded random number generator with a fixed seed.
302
+ * This is useful for reproducible testing across different platforms.
303
+ */
304
+ declare function makeFakeRandomNumberGenerator(): SeededRandomNumberGenerator;
305
+ /**
306
+ * Creates a Uint8Array of random data with a fixed seed.
307
+ * This is useful for reproducible testing.
308
+ *
309
+ * @param size - The number of bytes to generate
310
+ * @returns A Uint8Array of pseudo-random bytes
311
+ */
312
+ declare function fakeRandomData(size: number): Uint8Array;
313
+ //#endregion
314
+ export { type RandomNumberGenerator, SecureRandomNumberGenerator, SeededRandomNumberGenerator, TEST_SEED, type WideMulResult, 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 };
315
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/widening.ts","../src/magnitude.ts","../src/random-number-generator.ts","../src/secure-random.ts","../src/seeded-random.ts"],"sourcesContent":[],"mappings":";;AAQA;AAQA;AAcA;AAWA;AAWgB,KA5CJ,aAAA,GA4Cc,CAAA,MAAA,EAAA,MAAA,CAAA;AAa1B;;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+BA;AASA;AAeA;AAoCA;EAiCgB,WAAA,CAAA,IAAA,EAAA,CAAA,MAAiB,EAAA,MAAM,EAAA,MAAA,EAAA,MAAA,CAAA;EAyBvB;AAyBhB;AA8BA;AAgCA;AAYA;AAUA;2DE1L2D;;;AD/D3D;EASgB,OAAA,CAAA,CAAA,EAAA,MAAc;EAQd;AAchB;;EA4B4B,OAAA,CAAA,CAAA,EAAA,MAAA;EAOL;;;;;;ACjCvB;EA8B2D,SAAA,CAAA,IAAA,EAyBzC,UAzByC,CAAA,EAAA,IAAA;EAyBzC;;;;;AA+BlB;EAWgB,UAAA,CAAA,IAAA,EAAA,MAAA,CAAA,EA9BY,UA8BiB;EAW7B;;;uBA9BO;;;;;cAQV;;;;;iBAWG,6BAAA,CAAA,GAAiC;;;;;;;;iBAWjC,cAAA,gBAA8B"}
@@ -0,0 +1,315 @@
1
+ //#region src/widening.d.ts
2
+ /**
3
+ * Wide multiplication result type - returns (low, high) parts.
4
+ * For a multiplication of two N-bit values, the result is 2N bits
5
+ * split into two N-bit parts.
6
+ */
7
+ type WideMulResult = [bigint, bigint];
8
+ /**
9
+ * Performs wide multiplication for unsigned integers.
10
+ * Returns (low, high) parts of the full-width result.
11
+ *
12
+ * This is equivalent to Rust's widening_mul for unsigned types.
13
+ */
14
+ declare function wideMul(a: bigint, b: bigint, bits: number): WideMulResult;
15
+ /**
16
+ * Wide multiplication for 8-bit unsigned integers.
17
+ * @param a - First 8-bit value
18
+ * @param b - Second 8-bit value
19
+ * @returns Tuple of (low 8 bits, high 8 bits)
20
+ */
21
+ declare function wideMulU8(a: number, b: number): [number, number];
22
+ /**
23
+ * Wide multiplication for 16-bit unsigned integers.
24
+ * @param a - First 16-bit value
25
+ * @param b - Second 16-bit value
26
+ * @returns Tuple of (low 16 bits, high 16 bits)
27
+ */
28
+ declare function wideMulU16(a: number, b: number): [number, number];
29
+ /**
30
+ * Wide multiplication for 32-bit unsigned integers.
31
+ * @param a - First 32-bit value
32
+ * @param b - Second 32-bit value
33
+ * @returns Tuple of (low 32 bits, high 32 bits) as bigints
34
+ */
35
+ declare function wideMulU32(a: number, b: number): [bigint, bigint];
36
+ /**
37
+ * Wide multiplication for 64-bit unsigned integers.
38
+ * @param a - First 64-bit value as bigint
39
+ * @param b - Second 64-bit value as bigint
40
+ * @returns Tuple of (low 64 bits, high 64 bits) as bigints
41
+ */
42
+ declare function wideMulU64(a: bigint, b: bigint): [bigint, bigint];
43
+ //#endregion
44
+ //#region src/magnitude.d.ts
45
+ /**
46
+ * Converts a signed integer to its unsigned magnitude.
47
+ * For positive numbers, returns the number unchanged.
48
+ * For negative numbers, returns the absolute value (wrapping for MIN values).
49
+ *
50
+ * This matches Rust's wrapping_abs() behavior.
51
+ */
52
+ declare function toMagnitude(value: number, bits: 8 | 16 | 32): number;
53
+ /**
54
+ * Converts a signed bigint to its unsigned magnitude for 64-bit values.
55
+ */
56
+ declare function toMagnitude64(value: bigint): bigint;
57
+ /**
58
+ * Converts an unsigned magnitude back to a signed value.
59
+ * Simply reinterprets the bits.
60
+ */
61
+ declare function fromMagnitude(magnitude: number, bits: 8 | 16 | 32): number;
62
+ /**
63
+ * Converts an unsigned 64-bit magnitude back to a signed bigint.
64
+ */
65
+ declare function fromMagnitude64(magnitude: bigint): bigint;
66
+ //#endregion
67
+ //#region src/random-number-generator.d.ts
68
+ /**
69
+ * Interface for random number generators.
70
+ * This is the TypeScript equivalent of Rust's RandomNumberGenerator trait
71
+ * which extends RngCore + CryptoRng.
72
+ *
73
+ * This is compatible with the RandomNumberGenerator Swift protocol used
74
+ * in MacOS and iOS, which is important for cross-platform testing.
75
+ */
76
+ interface RandomNumberGenerator {
77
+ /**
78
+ * Returns the next random 32-bit unsigned integer.
79
+ */
80
+ nextU32(): number;
81
+ /**
82
+ * Returns the next random 64-bit unsigned integer as a bigint.
83
+ */
84
+ nextU64(): bigint;
85
+ /**
86
+ * Fills the given Uint8Array with random bytes.
87
+ */
88
+ fillBytes(dest: Uint8Array): void;
89
+ /**
90
+ * Returns a Uint8Array of random bytes of the given size.
91
+ */
92
+ randomData(size: number): Uint8Array;
93
+ /**
94
+ * Fills the given Uint8Array with random bytes.
95
+ * Alias for fillBytes for compatibility.
96
+ */
97
+ fillRandomData(data: Uint8Array): void;
98
+ }
99
+ /**
100
+ * Returns a Uint8Array of random bytes of the given size.
101
+ */
102
+ declare function rngRandomData(rng: RandomNumberGenerator, size: number): Uint8Array;
103
+ /**
104
+ * Fills the given Uint8Array with random bytes.
105
+ */
106
+ declare function rngFillRandomData(rng: RandomNumberGenerator, data: Uint8Array): void;
107
+ /**
108
+ * Returns a random value that is less than the given upper bound.
109
+ *
110
+ * Uses Lemire's "nearly divisionless" method for generating random
111
+ * integers in an interval. For a detailed explanation, see:
112
+ * https://arxiv.org/abs/1805.10941
113
+ *
114
+ * @param rng - The random number generator to use
115
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero.
116
+ * @returns A random value in the range [0, upperBound). Every value in the range is equally likely.
117
+ */
118
+ declare function rngNextWithUpperBound(rng: RandomNumberGenerator, upperBound: bigint): bigint;
119
+ /**
120
+ * Returns a random 32-bit value that is less than the given upper bound.
121
+ * This matches Rust's behavior when called with u32 type.
122
+ *
123
+ * Uses Lemire's "nearly divisionless" method with 32-bit arithmetic.
124
+ *
125
+ * @param rng - The random number generator to use
126
+ * @param upperBound - The upper bound for the randomly generated value. Must be non-zero and fit in u32.
127
+ * @returns A random u32 value in the range [0, upperBound).
128
+ */
129
+ declare function rngNextWithUpperBoundU32(rng: RandomNumberGenerator, upperBound: number): number;
130
+ /**
131
+ * Returns a random value within the specified range [start, end) using 32-bit arithmetic.
132
+ * This matches Rust's behavior when called with i32 types.
133
+ *
134
+ * @param rng - The random number generator to use
135
+ * @param start - The lower bound (inclusive) as i32
136
+ * @param end - The upper bound (exclusive) as i32
137
+ * @returns A random i32 value within the bounds
138
+ */
139
+ declare function rngNextInRangeI32(rng: RandomNumberGenerator, start: number, end: number): number;
140
+ /**
141
+ * Returns a random value within the specified range [start, end).
142
+ *
143
+ * @param rng - The random number generator to use
144
+ * @param start - The lower bound (inclusive)
145
+ * @param end - The upper bound (exclusive)
146
+ * @returns A random value within the bounds of the range
147
+ */
148
+ declare function rngNextInRange(rng: RandomNumberGenerator, start: bigint, end: bigint): bigint;
149
+ /**
150
+ * Returns a random value within the specified closed range [start, end].
151
+ *
152
+ * @param rng - The random number generator to use
153
+ * @param start - The lower bound (inclusive)
154
+ * @param end - The upper bound (inclusive)
155
+ * @returns A random value within the bounds of the range
156
+ */
157
+ declare function rngNextInClosedRange(rng: RandomNumberGenerator, start: bigint, end: bigint): bigint;
158
+ /**
159
+ * Returns a random value within the specified closed range [start, end] for i32 values.
160
+ * Convenience function that handles signed 32-bit integers.
161
+ *
162
+ * @param rng - The random number generator to use
163
+ * @param start - The lower bound (inclusive) as i32
164
+ * @param end - The upper bound (inclusive) as i32
165
+ * @returns A random i32 value within the bounds of the range
166
+ */
167
+ declare function rngNextInClosedRangeI32(rng: RandomNumberGenerator, start: number, end: number): number;
168
+ /**
169
+ * Returns a fixed-size array of random bytes.
170
+ *
171
+ * @param rng - The random number generator to use
172
+ * @param size - The size of the array to return
173
+ * @returns A Uint8Array of the specified size filled with random bytes
174
+ */
175
+ declare function rngRandomArray(rng: RandomNumberGenerator, size: number): Uint8Array;
176
+ /**
177
+ * Returns a random boolean value.
178
+ *
179
+ * @param rng - The random number generator to use
180
+ * @returns A random boolean
181
+ */
182
+ declare function rngRandomBool(rng: RandomNumberGenerator): boolean;
183
+ /**
184
+ * Returns a random 32-bit unsigned integer.
185
+ *
186
+ * @param rng - The random number generator to use
187
+ * @returns A random u32 value
188
+ */
189
+ declare function rngRandomU32(rng: RandomNumberGenerator): number;
190
+ //#endregion
191
+ //#region src/secure-random.d.ts
192
+ /**
193
+ * Generate a Uint8Array of cryptographically strong random bytes of the given size.
194
+ */
195
+ declare function randomData(size: number): Uint8Array;
196
+ /**
197
+ * Fill the given Uint8Array with cryptographically strong random bytes.
198
+ */
199
+ declare function fillRandomData(data: Uint8Array): void;
200
+ /**
201
+ * Returns the next cryptographically strong random 64-bit unsigned integer.
202
+ */
203
+ declare function nextU64(): bigint;
204
+ /**
205
+ * A random number generator that can be used as a source of
206
+ * cryptographically-strong randomness.
207
+ *
208
+ * Uses the Web Crypto API (crypto.getRandomValues) which is available
209
+ * in both browsers and Node.js >= 15.
210
+ */
211
+ declare class SecureRandomNumberGenerator implements RandomNumberGenerator {
212
+ /**
213
+ * Returns the next random 32-bit unsigned integer.
214
+ */
215
+ nextU32(): number;
216
+ /**
217
+ * Returns the next random 64-bit unsigned integer as a bigint.
218
+ */
219
+ nextU64(): bigint;
220
+ /**
221
+ * Fills the given Uint8Array with random bytes.
222
+ */
223
+ fillBytes(dest: Uint8Array): void;
224
+ /**
225
+ * Returns a Uint8Array of random bytes of the given size.
226
+ */
227
+ randomData(size: number): Uint8Array;
228
+ /**
229
+ * Fills the given Uint8Array with random bytes.
230
+ */
231
+ fillRandomData(data: Uint8Array): void;
232
+ }
233
+ //#endregion
234
+ //#region src/seeded-random.d.ts
235
+ /**
236
+ * A random number generator that can be used as a source of deterministic
237
+ * pseudo-randomness for testing purposes.
238
+ *
239
+ * Uses the Xoshiro256** algorithm, which is the same algorithm used by
240
+ * rand_xoshiro in Rust. This ensures cross-platform compatibility with
241
+ * the Rust implementation.
242
+ *
243
+ * WARNING: This is NOT cryptographically secure and should only be used
244
+ * for testing purposes.
245
+ */
246
+ declare class SeededRandomNumberGenerator implements RandomNumberGenerator {
247
+ private readonly state;
248
+ /**
249
+ * Creates a new seeded random number generator.
250
+ *
251
+ * The seed should be a 256-bit value, represented as an array of 4 64-bit
252
+ * integers (as bigints). For the output distribution to look random, the seed
253
+ * should not have any obvious patterns, like all zeroes or all ones.
254
+ *
255
+ * This is not cryptographically secure, and should only be used for
256
+ * testing purposes.
257
+ *
258
+ * @param seed - Array of 4 64-bit unsigned integers as bigints
259
+ */
260
+ constructor(seed: [bigint, bigint, bigint, bigint]);
261
+ /**
262
+ * Creates a new seeded random number generator from a seed array.
263
+ * Convenience method that accepts numbers and converts to bigints.
264
+ *
265
+ * @param seed - Array of 4 64-bit unsigned integers
266
+ */
267
+ static fromSeed(seed: [bigint, bigint, bigint, bigint]): SeededRandomNumberGenerator;
268
+ /**
269
+ * Returns the next random 64-bit unsigned integer as a bigint.
270
+ */
271
+ nextU64(): bigint;
272
+ /**
273
+ * Returns the next random 32-bit unsigned integer.
274
+ */
275
+ nextU32(): number;
276
+ /**
277
+ * Fills the given Uint8Array with random bytes.
278
+ *
279
+ * Note: This implementation matches the Rust behavior exactly -
280
+ * it uses one nextU64() call per byte (taking only the low byte),
281
+ * which matches the Swift version's behavior.
282
+ */
283
+ fillBytes(dest: Uint8Array): void;
284
+ /**
285
+ * Returns a Uint8Array of random bytes of the given size.
286
+ *
287
+ * This might not be the most efficient implementation,
288
+ * but it works the same as the Swift version.
289
+ */
290
+ randomData(size: number): Uint8Array;
291
+ /**
292
+ * Fills the given Uint8Array with random bytes.
293
+ */
294
+ fillRandomData(data: Uint8Array): void;
295
+ }
296
+ /**
297
+ * The standard test seed used across all Blockchain Commons implementations.
298
+ */
299
+ declare const TEST_SEED: [bigint, bigint, bigint, bigint];
300
+ /**
301
+ * Creates a seeded random number generator with a fixed seed.
302
+ * This is useful for reproducible testing across different platforms.
303
+ */
304
+ declare function makeFakeRandomNumberGenerator(): SeededRandomNumberGenerator;
305
+ /**
306
+ * Creates a Uint8Array of random data with a fixed seed.
307
+ * This is useful for reproducible testing.
308
+ *
309
+ * @param size - The number of bytes to generate
310
+ * @returns A Uint8Array of pseudo-random bytes
311
+ */
312
+ declare function fakeRandomData(size: number): Uint8Array;
313
+ //#endregion
314
+ export { type RandomNumberGenerator, SecureRandomNumberGenerator, SeededRandomNumberGenerator, TEST_SEED, type WideMulResult, 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 };
315
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/widening.ts","../src/magnitude.ts","../src/random-number-generator.ts","../src/secure-random.ts","../src/seeded-random.ts"],"sourcesContent":[],"mappings":";;AAQA;AAQA;AAcA;AAWA;AAWgB,KA5CJ,aAAA,GA4Cc,CAAA,MAAA,EAAA,MAAA,CAAA;AAa1B;;;;ACxDA;AA2BA;AAegB,iBDnCA,OAAA,CCmCa,CAAA,EAAA,MAAA,EAAA,CAAA,EAAA,MAAA,EAAA,IAAA,EAAA,MAAA,CAAA,EDnCgC,aCmChC;AAc7B;;;;AC/CA;;AAmB4B,iBFPZ,SAAA,CEOY,CAAA,EAAA,MAAA,EAAA,CAAA,EAAA,MAAA,CAAA,EAAA,CAAA,MAAA,EAAA,MAAA,CAAA;;;AAY5B;AASA;AAeA;AAoCA;AAiCgB,iBFrGA,UAAA,CEqGiB,CAAM,EAAA,MAAA,EAAA,CAAA,EAAA,MAAA,CAAA,EAAqB,CAAA,MAAA,EAAA,MAAA,CAAA;AAyB5D;AAyBA;AA8BA;AAgCA;AAYA;AAUA;iBFhOgB,UAAA;;;AGzBhB;AASA;AAQA;AAcA;AAqBkB,iBHdF,UAAA,CGcE,CAAA,EAAA,MAAA,EAAA,CAAA,EAAA,MAAA,CAAA,EAAA,CAAA,MAAA,EAAA,MAAA,CAAA;;;;AHvElB;AAQA;AAcA;AAWA;AAWA;AAaA;iBCxDgB,WAAA;;;AAAhB;AA2BgB,iBAAA,aAAA,CAAa,KAAA,EAAA,MAAA,CAAA,EAAA,MAAA;AAe7B;AAcA;;;iBAdgB,aAAA;ACjChB;;;AAyBuB,iBDsBP,eAAA,CCtBO,SAAA,EAAA,MA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