Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bn Php Laravel Package

simplito/bn-php

Arbitrary-precision math for PHP with a clean, minimal API. Provides big integer/decimal operations beyond native limits, suitable for financial calculations, cryptography, and any code needing exact large-number arithmetic and conversions.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require simplito/bn-php. This library provides arbitrary-precision integer arithmetic (BigNumber class) and is designed to be interoperable with the popular JavaScript bn.js library—especially useful in cross-language or blockchain-related projects (e.g., Ethereum). Start by importing the namespace: use Simplito\BigNum\BigNum;. A minimal usage example:

$bn = BigNum::from(123456789);
$sum = $bn->add(BigNum::from('987654321'));
echo $sum->toString(); // "1111111110"

For base conversion (e.g., hex to BN), use BigNum::from($hexString, 16).

Implementation Patterns

  • Blockchain token handling: Parse hex-encoded values from blockchain APIs (e.g., Ethereum balances in wei) using BigNum::from($hex, 16) and format output with $bn->toJSON() or $bn->toString(16).
  • Interoperable crypto primitives: When integrating with JS frontends or Web3 tools, ensure BN→BN compatibility by leveraging BigNum::from('0x123', 16) on both sides.
  • Efficient arithmetic: Use chained methods for gas optimizations:
    $fee = BigNum::from($gasLimit)->mul($gasPrice)->div(BigNum::from(1000000000));
  • Validation & comparison: Use $bn->gt($other), $bn->lt(), or $bn->eq() for conditional logic instead of casting to strings/numbers.

Gotchas and Tips

  • Hex input convention: Unlike bn.js, this library expects lowercase hex prefixes (0x) but does not require them—you can pass raw hex (e.g., '1a2b'). Omit 0x unless specified in from($str, $base) (where base=16 handles both).
  • Negative numbers: Not natively supported. Workaround: store sign separately or use two’s complement manually—common for blockchain data.
  • PHP integer limits: Avoid direct PHP arithmetic on BN values; always use BN methods to prevent overflow or precision loss.
  • Performance: BN operations are expensive—cache results in memory when computing repeatedly (e.g., in loops or recurring calculations).
  • Missing methods: Not all bn.js features are implemented (e.g., modPow, extended Euclidean algorithm). Check BigNum source methods—contribution opportunities exist.
  • Testing tip: Use PHPUnit’s assertEquals() with toString() because object comparison fails even for identical values.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4