UNPKG

get-random-values-polypony

Version:

Synchronous randombytes function that works in node, the browser & react-native!

13 lines 6.78 kB
const TWO_PWR_16_DBL=65536,TWO_PWR_32_DBL=4294967296,TWO_PWR_24_DBL=16777216,TWO_PWR_24=fromInt(TWO_PWR_24_DBL),TWO_PWR_64_DBL=TWO_PWR_32_DBL*TWO_PWR_32_DBL,TWO_PWR_63_DBL=TWO_PWR_64_DBL/2,MIN_VALUE={low:0,high:-2147483648},MAX_VALUE={low:-1,high:2147483647},MAX_UNSIGNED_VALUE={low:-1,high:-1},ONE=fromInt(1),ZERO=fromInt(0),UZERO=fromInt(0,!0),TMP_COMPARE=fromInt(0),TMP_MULTI1=fromInt(0),TMP_MULTI2=fromInt(0),TMP_SUBTRACT=fromInt(0),TMP_NEGATE=fromInt(0),TMP_CONVERT_BUFFER=new ArrayBuffer(8),TMP_CONVERT_FLOAT=new Float64Array(TMP_CONVERT_BUFFER),TMP_CONVERT_INT=new Uint32Array(TMP_CONVERT_BUFFER);function fromInt(value,unsigned){return unsigned?(value>>>=0,{low:value,high:0>(0|value)?-1:0}):(value|=0,{low:value,high:0>value?-1:0})}function toNumber(long,unsigned){return unsigned?(long.high>>>0)*TWO_PWR_32_DBL+(long.low>>>0):long.high*TWO_PWR_32_DBL+(long.low>>>0)}function isZero(long){return 0===long.low&&0===long.high}function isOdd(long){return 1==(1&long.low)}function eq(a,b){return a.high===b.high&&a.low===b.low}function isNegative(long,unsigned){return!unsigned&&0>long.high}function fromFloat(float,target){return TMP_CONVERT_FLOAT[0]=float,target.low=TMP_CONVERT_INT[0],target.high=TMP_CONVERT_INT[1],target}// Ported from https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L808-L843 function add(long,other,target){// Divide each number into 4 chunks of 16 bits, and then sum the chunks. const a48=long.high>>>16,a32=65535&long.high,a16=long.low>>>16,a00=65535&long.low,b48=other.high>>>16,b32=65535&other.high,b16=other.low>>>16,b00=65535&other.low;let c00=a00+b00,c16=c00>>>16;c00&=65535,c16+=a16+b16;let c32=c16>>>16;c16&=65535,c32+=a32+b32;let c48=c32>>>16;return c32&=65535,c48+=a48+b48,c48&=65535,target.low=c16<<16|c00,target.high=c48<<16|c32,target}function not(long,target){return target.low=~long.low,target.high=~long.high,target}function xor(long,other,target){return target.low=long.low^other.low,target.high=long.high^other.high,target}function and(long,other,target){return target.low=long.low&other.low,target.high=long.high&other.high,target}function negate(long,target,unsigned){return!unsigned&&eq(long,MIN_VALUE)?copy(MIN_VALUE,target):add(not(long,TMP_NEGATE),ONE,target)}function subtract(long,subtrahend,target,unsigned){return add(long,negate(subtrahend,TMP_SUBTRACT,unsigned),target)}function compare(a,b,unsigned){if(eq(a,b))return 0;const aNeg=isNegative(a,unsigned),bNeg=isNegative(b,unsigned);return aNeg&&!bNeg?-1:!aNeg&&bNeg?1:unsigned?b.high>>>0>a.high>>>0||b.high===a.high&&b.low>>>0>a.low>>>0?-1:1:isNegative(subtract(a,b,TMP_COMPARE,unsigned),unsigned)?-1:1;// At this point the sign bits are the same // Both are positive if at least one is unsigned }function lt(a,b,unsigned){return 0>compare(a,b,unsigned)}function multiplyRaw(long,multiplier,target,unsigned){// If both longs are small, use float multiplication if(lt(long,TWO_PWR_24,unsigned)&&lt(multiplier,TWO_PWR_24,unsigned)){const numa=toNumber(long,unsigned),numb=toNumber(multiplier,unsigned);return fromNumber(numa*numb,target,unsigned),target}// Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. // We can skip products that would overflow. const a48=long.high>>>16,a32=65535&long.high,a16=long.low>>>16,a00=65535&long.low,b48=multiplier.high>>>16,b32=65535&multiplier.high,b16=multiplier.low>>>16,b00=65535&multiplier.low;let c00=a00*b00,c16=c00>>>16;c00&=65535,c16+=a16*b00;let c32=c16>>>16;c16&=65535,c16+=a00*b16,c32+=c16>>>16,c16&=65535,c32+=a32*b00;let c48=c32>>>16;return c32&=65535,c32+=a16*b16,c48+=c32>>>16,c32&=65535,c32+=a00*b32,c48+=c32>>>16,c32&=65535,c48+=a48*b00+a32*b16+a16*b32+a00*b48,c48&=65535,target.low=c16<<16|c00,target.high=c48<<16|c32,target}// Ported from: https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L161-L178 function fromNumber(value,target,unsigned){if(isNaN(value))return copy(unsigned?UZERO:ZERO,target);if(unsigned){if(0>value)return copy(UZERO,target);if(value>=TWO_PWR_64_DBL)return copy(MAX_UNSIGNED_VALUE,target)}else{if(value<=-TWO_PWR_63_DBL)return copy(MIN_VALUE,target);if(value+1>=TWO_PWR_63_DBL)return copy(MAX_VALUE,target);if(0>value)return negate(fromNumber(-value,target,unsigned),target,unsigned)}return target.low=0|value%TWO_PWR_32_DBL,target.high=0|value/TWO_PWR_32_DBL,target}// Ported from https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L865-L940 function multiply(long,multiplier,target,unsigned){return isZero(long)||isZero(multiplier)?(target.low=0,target.high=0,target):eq(long,MIN_VALUE)?copy(isOdd(multiplier)?MIN_VALUE:ZERO,target):eq(multiplier,MIN_VALUE)?copy(isOdd(long)?MIN_VALUE:ZERO,target):isNegative(long,unsigned)?(negate(long,TMP_MULTI1,unsigned),isNegative(multiplier,unsigned)?(negate(multiplier,TMP_MULTI2,unsigned),multiplyRaw(TMP_MULTI1,TMP_MULTI2,target,unsigned)):(multiplyRaw(TMP_MULTI1,multiplier,TMP_MULTI2,unsigned),negate(TMP_MULTI2,target,unsigned)),target):isNegative(multiplier,unsigned)?(negate(multiplier,TMP_MULTI1,unsigned),multiplyRaw(long,TMP_MULTI1,TMP_MULTI2,unsigned),negate(TMP_MULTI2,target,unsigned),target):multiplyRaw(long,multiplier,target,unsigned)}function copy(source,target){return target.low=source.low,target.high=source.high,target}module.exports={isZero:isZero,isOdd:isOdd,eq:eq,lt:lt,compare:compare,// Ported from https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L1157-L1172 shiftRight:function(long,numBits,target){return 0==(numBits&=63)?(target.low=long.low,target.high=long.high):32>numBits?(target.low=long.low>>>numBits|long.high<<32-numBits,target.high=long.high>>numBits):(target.low=long.high>>numBits-32,target.high=0<=long.high?0:-1),target},// Ported from https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L1207-L1219 shiftRightUnsigned:function(long,numBits,target){return 0==(numBits&=63)?(target.low=long.low,target.high=long.high):32>numBits?(target.low=long.low>>>numBits|long.high<<32-numBits,target.high=long.high>>>numBits):32===numBits?(target.low=long.high,target.high=0):(target.low=long.high>>>numBits-32,target.high=0),target},// Ported from https://github.com/dcodeIO/long.js/blob/ce11b4b2bd3ba1240a057d62018563d99db318f9/src/long.js#L1213-L1219 shiftLeft:function(long,numBits,target){return 0==(numBits&=63)?(target.low=long.low,target.high=long.high):32>numBits?(target.low=long.low<<numBits,target.high=long.high<<numBits|long.low>>>32-numBits):(target.low=0,target.high=long.low<<numBits-32),target},multiply:multiply,add:add,subtract:subtract,xor:xor,and:and,not:not,copy:copy,negate:negate,fromInt:fromInt,toNumber:toNumber,fromNumber:fromNumber,fromFloat:fromFloat};