Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Unbiased and crytpgraplically safe library for generating random BigInt
A small library for generating cryptographically strong unbiasesd pseudorandom integers (bigint) within a given range (0 <= x < limit).
The implementation relies on the output from crypto.randomBytes() function in Node.JS. It is easy to modify the code for the alternative random sources.
The only simple and efficient way to generate unbiased set of random values from random byte source within an arbitrary range is as follows:
In the algorithm above, it is also important to generate the entire set of random bytes again in case of the constructed output being too big to fit into the expected range. E.g. generating only one or two most significant bytes would immediately cause a bias.
While generating a random value much bigger than the maximum output value and forcing it into the acceptable range by taking the modulus (mod limit + 1) from the value, it still almost always causes mostly insignificant but still existing bias. That is why for the cryptographic purposes a truely unbiased algirithm should always be used.
'use strict';
const bigintRnd = require('bigint-rnd');
let x;
// Always returns 0n,
x = bigintRnd(1);
// Always returns 0n or 1n,
x = bigintRnd(2n);
// Throws a RangeError, because the limit must be a positive integer.
try {
x = bigintRnd(0);
} catch (e) {
console.log(e);
}
// Returns a bigint 0 <= x < 10^100
x = bigintRnd(10n ** 100n);
// Returns a bigint 0 <= x < 65536
x = bigintRnd('0x10000');
// Returns a safe integer in number type in range
// Number.MIN_SAFE_INTEGER <= x <= Number.MAX_SAFE_INTEGER
// i.e. within the full safe integer range of the number type.
x = Number(bigintRnd(BigInt(Number.MAX_SAFE_INTEGER) -
BigInt(Number.MIN_SAFE_INTEGER) +
1n) +
BigInt(Number.MIN_SAFE_INTEGER));
Timo J. Rinne tri@iki.fi
MIT License
FAQs
Unbiased and crytpgraplically safe library for generating random BigInt
We found that bigint-rnd demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.