Package Exports
- @hazae41/bitwise.wasm
Readme
bitwise.wasm
Bitwise operations for WebAssembly
npm i @hazae41/bitwise.wasmFeatures
- Reproducible building
- Pre-bundled and streamed
- Zero-copy memory slices
Algorithms
- Bits pack and unpack
- Bits xormod
Usage
Concat bits with bytes
import { BitwiseWasm, Memory, bitwise_unpack, bitwise_pack_right } from "@hazae41/bitwise.wasm";
// Wait for WASM to load
await BitwiseWasm.initBundled();
// Create a header of bits
const head_bits = new Uint8Array([0x00, 0x01, 0x00, 0x01])
// Create a body of bytes
const body_bytes = new Uint8Array(256)
crypto.getRandomValues(body_bytes)
// Pass it to WASM
using body_bytes_memory = new Memory(body_bytes)
// Unpack it
using body_bits_memory = bitwise_unpack(body_bytes_memory)
// Pass it to JS
const body_bits = body_bits_memory.bytes
// Concat both bits arrays
const full_bits = new Uint8Array(head_bits.length + body_bits.length)
full_bits.set(head_bits, 0)
full_bits.set(body_bits, head_bits.length)
// Pass it to WASM
using full_bits_memory = new Memory(full_bits)
// Pack it adding 0-padding to the right
using full_bytes_memory = bitwise_pack_right(fullBits)
// Pass it to JS
const full_bytes = full_bytes_memory.bytesXoring with mask
import { BitwiseWasm, Memory, bitwise_xor_mod } from "@hazae41/bitwise.wasm";
// Wait for WASM to load
await BitwiseWasm.initBundled();
const bytes = new Uint8Array(1024)
crypto.getRandomValues(bytes)
using bytes_memory = new Memory(bytes)
const mask = new Uint8Array(4)
crypto.getRandomValues(mask)
using mask_memory = new Memory(mask)
using xored_memory = bitwise_xor_mod(bytes_memory, mask_memory)
const xored = xored_memory.bytes
using unxored_memory = bitwise_xor_mod(xored_memory, mask_memory)
const unxored = unxored_memory.bytesBuilding
Unreproducible building
You need to install Rust
Then, install wasm-pack
cargo install wasm-packFinally, do a clean install and build
npm ci && npm run buildReproducible building
You can build the exact same bytecode using Docker, just be sure you're on a linux/amd64 host
docker compose up --buildThen check that all the files are the same using npm diff
npm diffIf the output is empty then the bytecode is the same as the one I commited
Automated checks
Each time I release a new version on GitHub, the GitHub's CI clones the GitHub repository, reproduces the build, and throws an error if the NPM release is different. If a version is present on NPM but not on GitHub, do not use it!