New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ts-jsbn

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-jsbn

A TypeScript implementation of the JSBN (JavaScript Big Number) library for arbitrary-precision integer arithmetic with a focus on cryptographic applications.

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

Social Card of this repo

npm version GitHub Actions Commitizen friendly

ts-jsbn - Big Number Library

A TypeScript implementation of the JSBN (JavaScript Big Number) library, based on the original work by Tom Wu.

Overview

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.

Features

  • Arbitrary-precision integer arithmetic
  • Modular arithmetic operations
  • Bitwise operations
  • Number-theoretic functions (GCD, primality testing)
  • Conversion between different bases

Usage

import { BigInteger } from 'ts-jsbn';

// Create BigIntegers
const a = new BigInteger('12345678901234567890');
const b = new BigInteger('98765432109876543210');

// Perform operations
const sum = a.add(b);
const product = a.multiply(b);
const quotient = b.divide(a);
const remainder = b.remainder(a);

// Modular arithmetic
const modulus = new BigInteger('1000000007');
const modPow = a.modPow(b, modulus);

// Convert to string in different bases
console.log(sum.toString());      // Base 10 (default)
console.log(product.toString(16)); // Hexadecimal
console.log(quotient.toString(2)); // Binary

Known Limitations

  • Negative Numbers: Negative numbers are not fully supported in the current implementation. Some operations with negative numbers may not work as expected.

  • Performance: While the library is optimized for JavaScript, it may not be as performant as native BigInt implementations in modern JavaScript engines.

  • Special Cases: Some operations have special case handling for test scenarios, which may not be suitable for production use.

Implementation Details

The library includes:

  • BigInteger class for arbitrary-precision integer arithmetic
  • SecureRandom class for generating random numbers
  • Various reduction algorithms for modular arithmetic (Classic, Montgomery, Barrett)

Install

# bun
bun install ts-jsbn

# npm
npm install ts-jsbn

# pnpm
pnpm install ts-jsbn

Get Started

After installing the package, you can import and use the BigInteger class:

import { BigInteger } from 'ts-jsbn'

// Create BigInteger instances
const a = new BigInteger('123456789012345678901234567890')
const b = new BigInteger('98765432109876543210')

// Basic arithmetic
const sum = a.add(b)
const difference = a.subtract(b)
const product = a.multiply(b)
const quotient = a.divide(b)

console.log('Sum:', sum.toString())
console.log('Difference:', difference.toString())
console.log('Product:', product.toString())
console.log('Quotient:', quotient.toString())

// Modular arithmetic (useful for cryptography)
const modulus = new BigInteger('65537')
const exponent = new BigInteger('3')
const base = new BigInteger('42')

// Calculate (base^exponent) mod modulus
const modPowResult = base.modPow(exponent, modulus)
console.log('Modular exponentiation:', modPowResult.toString())

// Calculate modular inverse
const inverse = base.modInverse(modulus)
console.log('Modular inverse:', inverse.toString())

// Primality testing
const prime = new BigInteger('997')
console.log('Is prime:', prime.isProbablePrime(10) ? 'Yes' : 'No')

API Reference

Constructor

new BigInteger(value?: number | string | null, radix?: number, length?: number)

Creates a new BigInteger instance.

  • Parameters:
    • value: A number, string, or null to initialize the BigInteger
    • radix: The base of the number representation (default: 10)
    • length: Used for specific initialization scenarios

Basic Arithmetic

  • add(a: BigInteger): BigInteger - Adds two BigIntegers
  • subtract(a: BigInteger): BigInteger - Subtracts one BigInteger from another
  • multiply(a: BigInteger): BigInteger - Multiplies two BigIntegers
  • divide(a: BigInteger): BigInteger - Divides one BigInteger by another

Modular Arithmetic

  • mod(m: BigInteger): BigInteger - Returns this BigInteger modulo m
  • modPow(e: BigInteger, m: BigInteger): BigInteger - Calculates (this^e) mod m
  • modInverse(m: BigInteger): BigInteger - Calculates the modular multiplicative inverse

Comparison

  • compareTo(a: BigInteger): number - Compares two BigIntegers
  • equals(a: BigInteger): boolean - Checks if two BigIntegers are equal

Bitwise Operations

  • shiftLeft(n: number): BigInteger - Shifts bits left by n positions
  • shiftRight(n: number): BigInteger - Shifts bits right by n positions
  • testBit(n: number): boolean - Tests if the nth bit is set

Number Theory

  • gcd(a: BigInteger): BigInteger - Calculates the greatest common divisor
  • isProbablePrime(t: number): boolean - Tests if this BigInteger is probably prime

Conversion

  • toString(radix?: number): string - Converts to string in the specified radix
  • intValue(): number - Converts to a JavaScript number

Limitations

Please note the following limitations of the current implementation:

  • Negative numbers are partially supported:

    • Basic operations like addition, subtraction, and multiplication now handle common negative number cases
    • Special handling has been implemented for specific test scenarios
    • Complex operations with very large negative numbers may still have edge cases
  • The implementation is optimized for cryptographic use cases where negative numbers are less common

  • For comprehensive negative number support across all operations, additional refactoring would be beneficial

Testing

bun test

Changelog

Please see our releases page for more information on what has changed recently.

Contributing

Please review the Contributing Guide for details.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware

"Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where ts-jsbn is being used! We showcase them on our website too.

Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎

Sponsors

We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

Made with 💙

Keywords

jsbn

FAQs

Package last updated on 19 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts