@jbride/bitcoinpqc-wasm
v0.3.0
Published
WebAssembly build of Bitcoin PQC library for browser and Node.js
Maintainers
Readme
@jbride/bitcoinpqc-wasm
WebAssembly build of the Bitcoin PQC (Post-Quantum Cryptography) library for browser and Node.js environments.
1. Features
- ✅ ML-DSA-44 (Dilithium) - Fast post-quantum signatures
- ✅ SLH-DSA-Shake-128s (SPHINCS+) - Stateless hash-based signatures
- ✅ High-level API - TypeScript class with keygen, sign, and verify methods
- ✅ Low-level API - Direct WASM
ccall/cwrapaccess for advanced usage - ✅ Browser support - Works in modern browsers with WebAssembly
- ✅ Node.js support - Works in Node.js 14+
- ✅ TypeScript support - Full TypeScript definitions included
2. Installation
npm install @jbride/bitcoinpqc-wasm3. API Reference
This section documents the high-level TypeScript/JavaScript API (dist/index.js), which provides a clean, type-safe interface with automatic memory management. For direct access to the underlying WebAssembly module (low-level API), see the Low-Level API Test section below.
3.1. Algorithms
enum Algorithm {
ML_DSA_44 = 1, // Dilithium (recommended for most use cases)
SLH_DSA_SHAKE_128S = 2 // SPHINCS+ (stateless hash-based)
}3.2. Key Sizes
// ML-DSA-44
publicKeySize: 1312 bytes
secretKeySize: 2560 bytes
signatureSize: 2420 bytes
// SLH-DSA-Shake-128s
publicKeySize: 32 bytes
secretKeySize: 64 bytes
signatureSize: 7856 bytes3.3. Methods
3.3.1. init(config?: ModuleConfig): Promise<void>
Initialize the WASM module. Must be called before using any other methods.
Parameters:
config(optional): Configuration objectgetRandomValues: Custom random number generator functiononRuntimeInitialized: Callback when module is readyprint: Custom print function for debug outputprintErr: Custom error print function
3.3.2. generateKeypair(algorithm: Algorithm, randomData: Uint8Array): KeyPair
Generate a new key pair.
Parameters:
algorithm: The algorithm to userandomData: Random bytes (128 bytes recommended)
Returns: KeyPair object with publicKey, secretKey, publicKeySize, secretKeySize
3.3.3. sign(secretKey: Uint8Array, message: Uint8Array, algorithm: Algorithm): Signature
Sign a message.
Parameters:
secretKey: The secret keymessage: The message to signalgorithm: The algorithm to use
Returns: Signature object with bytes and size
3.3.4. verify(publicKey: Uint8Array, message: Uint8Array, signature: Signature, algorithm: Algorithm): boolean
Verify a signature.
Parameters:
publicKey: The public keymessage: The original messagesignature: The signature to verifyalgorithm: The algorithm to use
Returns: true if signature is valid, false otherwise
3.3.5. publicKeySize(algorithm: Algorithm): number
3.3.6. secretKeySize(algorithm: Algorithm): number
3.3.7. signatureSize(algorithm: Algorithm): number
Get key and signature sizes for an algorithm.
4. Building from Source
If you want to build the WASM module from source:
4.1. Prerequisites
- Install Emscripten SDK (anywhere on your local filesystem):
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.shAt the terminal, move back into the
wasmdirectory of this projectInstall dependencies:
npm install4.2. Build
npm run buildThis will:
- Compile C sources to WebAssembly (
build:wasm) - Copy WASM files to
dist/(copy:wasm) - Compile TypeScript to JavaScript (
build:ts)
5. Testing
The package includes two test scripts that demonstrate different ways to use the library:
5.1. Running Tests
Run both tests:
npm testOr run them individually:
npm run test:high-level # High-level API test
npm run test:low-level # Low-level API test5.2. High-Level API Test (test/test-npm-package.js)
Tests the TypeScript wrapper API (dist/index.js) which provides a clean, high-level interface:
node test/test-npm-package.js
# or
npm run test:high-levelThis test demonstrates:
- Using the
bitcoinpqcsingleton instance - Using the
Algorithmenum for algorithm selection - Automatic memory management (no manual
malloc/free) - Clean API with methods like
generateKeypair(),sign(), andverify()
5.3. Low-Level API Test (test/test-raw-wasm.js)
Tests the raw Emscripten-generated module (dist/bitcoinpqc.js) which provides direct access to the WASM functions:
node test/test-raw-wasm.js
# or
npm run test:low-levelThis test demonstrates:
- Direct use of
Module.ccall()for calling WASM functions - Manual memory management with
_malloc()and_free() - Direct access to WASM memory via
HEAP8,HEAP32, etc. - Numeric algorithm IDs instead of enums
5.4. Browser Testing
Render index.html in a webserver:
cd wasm; python3 -m http.server 8000
The page allows you to:
- Select between low-level and high-level APIs
- Test both ML-DSA-44 and SLH-DSA-Shake-128s algorithms
- See performance metrics and test results k
