0.1.0-beta.1 • Published 3 years ago

deterministic-rsa-js v0.1.0-beta.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

WARNING! This project has not yet been verified to be cryptographically secure. USE AT YOUR OWN RISK!

deterministic-rsa-js

Deterministic RSA using vanilla JavaScript

Can be used to generate RSA keys based on mnemonic keys. This functionality is already possible using node-forge package however, this project is more than 3 times faster on average making it more suitable for high performance applications or real-time interactive environments.

How it works

Native BigInt, no byte array shims

The original version of this project used Rust which took ~1.3s to generate a key natively, however when compiled to WASM, it took ~15 seconds. This is a because the WASM implementation used Uint8Array as a shim for large integers which is very slow in JavaScript. In the current implementation, we use JS native BigInts which handles arbitrarily-precise integers at a machine code level. This is where most of the speed up comes from.

Fast random number generation

Use simple 32bit math and bitwise operators. Instead of generating entirely new numbers, simply pick a random bit between 1 and nBits - 3 then ^= 1n << bitShift it (lbitshift xor assign). This reduces how many times we need to run the number generation.

Native Node crypto

When running, we check if the node crypto module is available. If it is, we use crypto.checkPrime() to leverage the native C++ prime checking. Otherwise, we fallback to our JavaScript solution.

Optimized memory behavior

Preallocate buffers and variables to reduce garbage collection. We also take care to only compare and assign values of the same type in order remove performance cost of type coercion and dynamic memory allocation.

Testing

To test the node, run node test/test.js

To test in browser, open test/index.html with any browser

TODO

  • Publish to NPM
  • Try to use Node crypto for the rest of RSA generation
  • Add optimized prime checking implementation

Resources