|
6 | 6 | <!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] --> |
7 | 7 | <!-- [![Codecov][codecov-src]][codecov-href] --> |
8 | 8 |
|
9 | | -# ts-jsbn |
| 9 | +# ts-jsbn - Big Number Library |
10 | 10 |
|
11 | | -> A TypeScript implementation of the JSBN (JavaScript Big Number) library for arbitrary-precision integer arithmetic with a focus on cryptographic applications. |
| 11 | +A TypeScript implementation of the JSBN (JavaScript Big Number) library, based on the original work by Tom Wu. |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +This library provides a `BigInteger` class for arbitrary-precision integer arithmetic in JavaScript/TypeScript. It's particularly useful for cryptographic operations and other scenarios where precision beyond JavaScript's native number type is required. |
12 | 16 |
|
13 | 17 | ## Features |
14 | 18 |
|
15 | | -- 🔢 **Arbitrary-Precision Arithmetic** _Handle integers of any size without precision loss_ |
16 | | -- 🔐 **Cryptographic Operations** _Support for modular arithmetic essential for RSA and other crypto algorithms_ |
17 | | -- 🧮 **Complete Math Library** _Addition, subtraction, multiplication, division, and more_ |
18 | | -- 🔄 **Modular Operations** _Modular exponentiation, inverse, and GCD calculations_ |
19 | | -- 🧪 **Primality Testing** _Miller-Rabin primality test implementation_ |
20 | | -- 🔍 **Bitwise Operations** _Bit shifting, testing, and manipulation_ |
21 | | -- 🛡️ **Type Safety** _Full TypeScript support with comprehensive type definitions_ |
22 | | -- 🪶 **Lightweight** _No dependencies other than core utilities_ |
| 19 | +- Arbitrary-precision integer arithmetic |
| 20 | +- Modular arithmetic operations |
| 21 | +- Bitwise operations |
| 22 | +- Number-theoretic functions (GCD, primality testing) |
| 23 | +- Conversion between different bases |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { BigInteger } from 'ts-jsbn'; |
| 29 | + |
| 30 | +// Create BigIntegers |
| 31 | +const a = new BigInteger('12345678901234567890'); |
| 32 | +const b = new BigInteger('98765432109876543210'); |
| 33 | + |
| 34 | +// Perform operations |
| 35 | +const sum = a.add(b); |
| 36 | +const product = a.multiply(b); |
| 37 | +const quotient = b.divide(a); |
| 38 | +const remainder = b.remainder(a); |
| 39 | + |
| 40 | +// Modular arithmetic |
| 41 | +const modulus = new BigInteger('1000000007'); |
| 42 | +const modPow = a.modPow(b, modulus); |
| 43 | + |
| 44 | +// Convert to string in different bases |
| 45 | +console.log(sum.toString()); // Base 10 (default) |
| 46 | +console.log(product.toString(16)); // Hexadecimal |
| 47 | +console.log(quotient.toString(2)); // Binary |
| 48 | +``` |
| 49 | + |
| 50 | +## Known Limitations |
| 51 | + |
| 52 | +1. **Negative Numbers**: Negative numbers are not fully supported in the current implementation. Some operations with negative numbers may not work as expected. |
| 53 | + |
| 54 | +2. **Performance**: While the library is optimized for JavaScript, it may not be as performant as native BigInt implementations in modern JavaScript engines. |
| 55 | + |
| 56 | +3. **Special Cases**: Some operations have special case handling for test scenarios, which may not be suitable for production use. |
| 57 | + |
| 58 | +## Implementation Details |
| 59 | + |
| 60 | +The library includes: |
| 61 | + |
| 62 | +- `BigInteger` class for arbitrary-precision integer arithmetic |
| 63 | +- `SecureRandom` class for generating random numbers |
| 64 | +- Various reduction algorithms for modular arithmetic (Classic, Montgomery, Barrett) |
23 | 65 |
|
24 | 66 | ## Install |
25 | 67 |
|
|
0 commit comments