return uintArray
}
-export function bigintBitLength (int: bigint): bigint {
- let bitLength = 1n
- while (int > 1n || int < -1n) {
- bitLength++
- int >>= 1n
- }
- return bitLength
-}
-
export function bigintByteLength (int: bigint): number {
let byteLength = 0
while (int > 0n || int < -1n) {
throw new TypeError(`can't convert string to BigInt`)
}
-export function bigintRandom (max: bigint = 0xFFFFFFFFFFFFFFFFn): bigint {
- if (typeof max !== 'bigint' || max < 1n) {
- throw new TypeError('Invalid max value')
- }
- const randomUint8Array = new Uint8Array(bigintByteLength(max))
- const mask = (1n << bigintBitLength(max)) - 1n
- let output = 0n
- do {
- output = 0n
- crypto.getRandomValues(randomUint8Array)
- output = BigInt(randomUint8Array[0])
- for (let i = 1; i < randomUint8Array.length; i++) {
- output <<= 8n
- output += BigInt(randomUint8Array[i])
- }
- output &= mask
- } while (output > max)
- return output
-}
-
export function bigintToHex (int: bigint, length: unknown = 0): string {
if (typeof length !== 'number') {
throw new TypeError('invalid length')