npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@azghr/debias

v0.1.0

Published

Debias a biased bitstream (e.g. from a hardware/quantum RNG) with the Von Neumann extractor and an optional XOR-fold whitener. Classical, zero deps. NOT a cryptographic RNG.

Readme

@azghr/debias

npm MIT License

Debias a biased bitstream (e.g. from a hardware/quantum RNG) with the Von Neumann extractor and an optional XOR-fold whitener. Classical, zero deps. NOT a cryptographic RNG.

The problem

Hardware and quantum RNGs emit biased bitstreams. You must debias them before use, but the classic Von Neumann extractor is subtle to implement correctly. Standalone debiasing utilities are scarce and often untested on edge cases. This fills that gap for anyone consuming hardware/QRNG feeds in research, IoT, or novelty APIs.

Install

npm install @azghr/debias
# or
pnpm add @azghr/debias
# or
yarn add @azghr/debias

Use

import { vonNeumann, bias } from "@azghr/debias";

const biasedBits = new Uint8Array([1, 1, 1, 0, 0, 0, 1, 0]);
console.log(bias(biasedBits));           // 0.125 (biased)
const debiasedBits = vonNeumann(biasedBits);
console.log(bias(debiasedBits));         // 0.0 (unbiased)

Real-world hardware QRNG processing

import { vonNeumann, bias, packBytes } from "@azghr/debias";

const qrngBits = generateFromHardwareQRNG(100000);
console.log("Raw bias:", bias(qrngBits));        // 0.201
const debiased = vonNeumann(qrngBits);
console.log("Debiased:", bias(debiased));        // 0.002
const packed = packBytes(debiased);
console.log("Size:", packed.length, "bytes");     // Efficient storage

XOR-fold for problematic sources

import { xorFold } from "@azghr/debias";

// All equal pairs (Von Neumann produces nothing)
const problematic = new Uint8Array([0, 0, 1, 1, 0, 0, 1, 1]);
const whitened = xorFold(problematic, 4);
console.log(whitened);  // [0, 0, 0, 0] - still processes data

API

vonNeumann(bits: Bits): Bits

Applies Von Neumann extractor: 01→0, 10→1, equal pairs discarded. Trailing odd bit ignored. Output length varies (≈ input * p(1-p)).

  • bits: Input bitstream (elements must be 0 or 1)
  • Returns: Debias bitstream
  • Throws: InvalidBit if invalid input

xorFold(bits: Bits, blockSize?: number): Bits

XOR-folds by reshaping into blocks and XORing corresponding positions. Weaker than Von Neumann but handles problematic sources.

  • bits: Input bitstream (elements must be 0 or 1)
  • blockSize: Block size (default: 2, must be ≥ 1)
  • Returns: Whitened bitstream
  • Throws: InvalidBit if invalid input, Error if blockSize < 1

bias(bits: Bits): number

Returns mean(bits) - 0.5 (estimates P(1) - 0.5). Returns 0 for empty input.

  • bits: Input bitstream (elements must be 0 or 1)
  • Returns: Bias measurement
  • Throws: InvalidBit if invalid input

packBytes(bits: Bits): Uint8Array / unpackBytes(bytes: Uint8Array): Bits

MSB-first bit packing/unpacking. Non-multiple-of-8 inputs padded with zeros.

  • packBytes: Returns packed bytes
  • unpackBytes: Returns 8 * bytes.length bits
  • Throws: InvalidBit if invalid input (packBytes only)

InvalidBit extends Error

Thrown when bit value is invalid. Contains readonly index: number.

Security Notice

NOT FOR CRYPTOGRAPHIC USE — assumes independent bits. For crypto, use WebCrypto API or crypto.randomBytes().

Non-goals

This does NOT: generate random numbers, handle correlated sources (use Toeplitz/hashing), estimate entropy beyond bias, or replace CSPRNGs.

TypeScript note

Full TypeScript support included. Exported Bits type is Uint8Array (elements must be 0 or 1).

License

MIT