import bigInt , { BigInteger } from "big-integer" ; import { RandomHelper } from "./randomHelper" ; /**
 * Helper methods for bigints.
 */ export class BigIntHelper { private static readonly BIG_32 : BigInteger = bigInt ( 32 ) ; private static readonly BIG_32_MASK : BigInteger = bigInt ( 0xffffffff ) ; param data param byteOffset returns public static read3 ( data , byteOffset ) { const v0 = ( data [ byteOffset + 0 ] + ( data [ byteOffset + 1 ] << 8 ) + ( data [ byteOffset + 2 ] << 16 ) ) >>> 0 ; return bigInt ( v0 ) ; } param data param byteOffset returns public static read4 ( data , byteOffset ) { const v0 = ( data [ byteOffset + 0 ] + ( data [ byteOffset + 1 ] << 8 ) + ( data [ byteOffset + 2 ] << 16 ) + ( data [ byteOffset + 3 ] << 24 ) ) >>> 0 ; return bigInt ( v0 ) ; } param data param byteOffset returns public static read8 ( data , byteOffset ) { const v0 = ( data [ byteOffset + 0 ] + ( data [ byteOffset + 1 ] << 8 ) + ( data [ byteOffset + 2 ] << 16 ) + ( data [ byteOffset + 3 ] << 24 ) ) >>> 0 ; const v1 = ( data [ byteOffset + 4 ] + ( data [ byteOffset + 5 ] << 8 ) + ( data [ byteOffset + 6 ] << 16 ) + ( data [ byteOffset + 7 ] << 24 ) ) >>> 0 ; return bigInt ( v1 ) . shiftLeft ( BigIntHelper . BIG_32 ) . or ( v0 ) ; } param value param data param byteOffset public static write8 ( value , data , byteOffset ) { const v0 = Number ( value . and ( BigIntHelper . BIG_32_MASK ) ) ; const v1 = Number ( value . shiftRight ( BigIntHelper . BIG_32 ) . and ( BigIntHelper . BIG_32_MASK ) ) ; data [ byteOffset ] = v0 & 0xff ; data [ byteOffset + 1 ] = ( v0 >> 8 ) & 0xff ; data [ byteOffset + 2 ] = ( v0 >> 16 ) & 0xff ; data [ byteOffset + 3 ] = ( v0 >> 24 ) & 0xff ; data [ byteOffset + 4 ] = v1 & 0xff ; data [ byteOffset + 5 ] = ( v1 >> 8 ) & 0xff ; data [ byteOffset + 6 ] = ( v1 >> 16 ) & 0xff ; data [ byteOffset + 7 ] = ( v1 >> 24 ) & 0xff ; } returns public static random ( ) { return BigIntHelper . read8 ( RandomHelper . generate ( 8 ) , 0 ) ; } }