Catalog npm
Back to packageRELEASE DETAILS

0.0.13

npm

@0xdoublesharp/unsafe-math

Unchecked uint256 and int256 math operations for Solidity 0.8

i256int256mathsolidityu256uint256uncheckedunchecked-mathunsafe-math
Published9 Apr 2023
Direct dependencies36
Transitive dependenciesNot calculated

README

Current package documentation

Unsafe Math for Solidity 0.8+

This library and custom user types allow for the use of unchecked math operations when appropriate. Gas savings can be realized when it is already known that the results will not overflow.

Install

npm install -D @0xdoublesharp/unsafe-math
yarn add -D @0xdoublesharp/unsafe-math

Library UnsafeMath

The UnsafeMath library provides unchecked math operations for uint256 and int256 types with Solidity 0.8.0+. It also provides interoperability and conversion for the U256 and I256 user defined types.

Type uint256

Use the UnsafeMath library for uint256 types to perform unchecked math operations on two unsigned integers.

API

using UnsafeMath for uint256;

  • Math
    • _uint256.add(uint256 _addend): add a uint256 to a uint256
    • _uint256.sub(uint256 _subtrahend): subtract a uint256 from a uint256
    • _uint256.inc(): increment a uint256
    • _uint256.dec(): decrement a uint256
    • _uint256.mul(uint256 _multiplier): multiply a uint256 by a uint256
    • _uint256.div(uint256 _divisor): divide a uint256 from a uint256
    • _uint256.mod(uint256 _divisor): get the modulus of uint256s
    • _uint256.exp(uint256 _exponent): raise a uint256 by an exponent
Example
import { UnsafeMath } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';

contract ContractA {
  using UnsafeMath for uint256;

  function add(uint256 a, uint256 b) external pure returns (uint256) {
    return a.add(b);
  }

  function while(uint256 times) external pure {
    uint256 _iter = times;
    while (_iter != 0) {
      _iter = _iter.dec();
      int64 _int64 = int64(_iter);
      // ...
    }
  }

  function for(uint256 times) external pure {
    for (uint256 _iter; _iter < times; _iter = _iter.inc()) {
      int64 _int64 = int64(_iter);
      // ...
    }
  }
}

Type int256

Use the UnsafeMath library for int256 types to perform unchecked math operations on two signed integers. Note that exp() is not available for signed integers.

API

using UnsafeMath for int256;

  • Math
    • _int256.add(int256 _addend): add an int256 to an int256
    • _int256.sub(int256 _subtrahend): subtract an int256 from an int256
    • _int256.inc(): increment an int256 value
    • _int256.dec(): decrement an int256
    • _int256.mul(int256 _multiplier): multiply an int256 by an int256
    • _int256.div(int256 _divisor): divide an int256 from an int256
    • _int256.mod(int256 _divisor): get the modulus of int256s
Example
import { UnsafeMath } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';

contract ContractB {
  using UnsafeMath for int256;

  function sub(int256 a, int256 b) external pure returns (int256) {
    return a.sub(b);
  }
}

User Defined Type U256

Used with U256 to access helper methods for interacting with uint256 types and functionality not available in overloaded operators, such as bit shifting and exponential operations.

API

using UnsafeMath for U256;

  • Math
    • _U256.add(uint256 _addend): add a U256 to a uint256
    • _U256.sub(uint256 _subtrahend): subtract a U256 from a uint256
    • _U256.inc(): increment a U256
    • _U256.dec(): decrement a U256
    • _U256.mul(uint256 _multiplier): multiply a U256 by a uint256
    • _U256.div(uint256 _divisor): divide a uint256 from a U256
    • _U256.mod(uint256 _divisor): get the modulus of U256 and uint256
    • _U256.exp(uint256 _exponent): raise a U256 by an exponent
  • Comparison
    • _U256.eq(uint256 _compare): compare U256 and uint256 equality
    • _U256.neq(uint256 _compare): compare U256 and uint256 non-equality
    • _U256.lt(uint256 _compare): compare U256 and uint256 less than uint256
    • _U256.lte(uint256 _compare): compare U256 less than or equal to uint256
    • _U256.gt(uint256 _compare: compare U256 greater than uint256
    • _U256.gte(uint256 _compare): compare U256 greater than or equal to uint256
  • Logical
    • _U256.and(uint256 _operator): bitwise AND on U256 and uint256
    • _U256.or(uint256 _operator): bitwise OR on U256 and uint256
    • _U256.xor(uint256 _operator): bitwise XOR on U256 and uint256
    • _U256.not(): bitwise NOT on U256
    • _U256.rshift(U256 _shift): right shift U256 by U256
    • _U256.lshift(U256 _shift): right shift U256 by U256
    • _U256.rshift(uint256 _shift): right shift U256 by uint256
    • _U256.lshift(uint256 _shift): right shift U256 by uint256
  • As Unsigned Integer
    • _U256.asUint8(): U256 to uint8
    • _U256.asUint16(): U256 to uint16
    • _U256.asUint32(): U256 to uint32
    • _U256.asUint64(): U256 to uint64
    • _U256.asUint128(): U256 to uint128
    • _U256.asUint256(): U256 to uint256
  • As Signed Integer
    • _U256.asInt8(): U256 to int8
    • _U256.asInt16(): U256 to int16
    • _U256.asInt32(): U256 to int32
    • _U256.asInt64(): U256 to int64
    • _U256.asInt128(): U256 to int128
    • _U256.asInt256(): U256 to int256
  • As U256
    • _uint256.asU256(): convert a uint256 to U256
    • _int256.asU256(): convert an int256 to U256
Example
import { UnsafeMath, U256 } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';

contract ContractA {
  using UnsafeMath for U256;
  using UnsafeMath for uint256;
  using UnsafeMath for int256;

  function add(int256 a, uint256 b) external pure returns (uint256) {
    return a.asU256().add(b).asUint256();
  }

  function sub(uint256 a, uint256 b) external pure returns (uint256) {
    return (a.asU256() - b.asU256()).asUint256();
  }
}

User Defined Type I256

Use with I256 to access helper methods for interacting with int256 types and functionality not available in overloaded operators.

API

using UnsafeMath for I256;

  • Math
    • _I256.add(uint256 _addend): add an I256 to a int256
    • _I256.sub(uint256 _subtrahend): subtract an I256 from a int256
    • _I256.inc(): increment an I256
    • _I256.dec(): decrement an I256
    • _I256.mul(uint256 _multiplier): multiply an I256 by a int256
    • _I256.div(uint256 _divisor): divide a int256 from an I256
    • _I256.mod(uint256 _divisor): get the modulus of U256 and uint256
  • Comparison
    • _I256.eq(uint256 _compare): compare U256 and uint256 equality
    • _I256.neq(uint256 _compare): compare U256 and uint256 non-equality
    • _I256.lt(uint256 _compare): compare U256 and uint256 less than uint256
    • _I256.lte(uint256 _compare): compare U256 less than or equal to uint256
    • _I256.gt(uint256 _compare: compare U256 greater than uint256
    • _I256.gte(uint256 _compare): compare U256 greater than or equal to uint256
  • Logical
    • _I256.and(uint256 _operator): bitwise AND on U256 and uint256
    • _I256.or(uint256 _operator): bitwise OR on U256 and uint256
    • _I256.xor(uint256 _operator): bitwise XOR on U256 and uint256
    • _I256.not(): bitwise NOT on U256
  • As Unsigned Integer
    • _I256.asUint8(): U256 to uint8
    • _I256.asUint16(): U256 to uint16
    • _I256.asUint32(): U256 to uint32
    • _I256.asUint64(): U256 to uint64
    • _I256.asUint128(): U256 to uint128
    • _I256.asUint256(): U256 to uint256
  • As Signed Integer
    • _I256.asInt8(): U256 to int8
    • _I256.asInt16(): U256 to int16
    • _I256.asInt32(): U256 to int32
    • _I256.asInt64(): U256 to int64
    • _I256.asInt128(): U256 to int128
    • _I256.asInt256(): U256 to int256
  • As I256
    • _uint256.asI256(): convert a int256 to I256
    • _int256.asI256(): convert an int256 to I256
Example
import { UnsafeMath, I256 } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';

contract ContractB {
  using UnsafeMath for I256;
  using UnsafeMath for uint256;
  using UnsafeMath for int256;

  function add(int256 a, uint256 b) external pure returns (int256) {
    I256 _a = a.asI256();
    return _a.add(int256(b)).asInt256();
  }

  function sub(uint256 a, uint256 b) external pure returns (int256) {
    I256 _a = a.asI256();
    I256 _b = b.asI256();
    return (a - b).asInt256();
  }
}

User Defined Types: U256 and I256

User defined types with operator overloads are also provided to take advantage of the latest Solidity features. Use U256 for unchecked unsigned integer operations, or I256 for unchecked signed integer operations. Helper methods are also provided for interoperating with uint256 and int256 via the UnsafeMath library.

The overloads also provide comparison and bitwise operators where available.

User Defined Type U256

The U256 user defined type can perform unchecked math operations using overloaded operators.

API

  • Math
    • _u256 + _u256: add a U256 to a U256
    • _u256 - _u256: subtract a U256 from a U256
    • _u256 * _u256: multiply a U256 by a U256
    • _u256 / _u256: divide a U256 from a U256
    • _u256 % _u256: get the modulus of U256s
  • Comparison
    • _u256 == _u256: compare U256 equality
    • _u256 != _u256: compare U256 non-equality
    • _u256 < _u256: compare U256 less than U256
    • _u256 <= _u256: compare U256 less than or equal to U256
    • _u256 > _u256: compare U256 greater than U256
    • _u256 >= _u256: compare U256 greater than or equal to U256
  • Logical
    • _u256 & _u256: bitwise AND on U256
    • _u256 | _u256: bitwise OR on U256
    • _u256 ^ _u256: bitwise XOR on U256
    • ~_u256: bitwise NOT on U256
Example
import { U256 } from '@0xdoublesharp/unsafe-math/contracts/types/U256.sol';

contract ContractA {
  function add(uint256 a, uint256 b) external pure returns (uint256) {
      return (U256.wrap(a) - U256.wrap(b)).asUint256();
  }

  function while(uint256 times) external pure {
    U256 _iter = U256.wrap(times);
    while (_iter.neq(0)) {
      _iter = _iter.dec();
      uint256 _uint256 = _iter.asUint256();
      int64 _int64 = _iter.asInt64();
      // ...
    }
  }

  function for(uint256 times) external pure {
    U256 _times = U256.wrap(times);
    for (U256 _iter; _iter < _times; _iter = _iter.inc()) {
      uint256 _uint256 = _iter.asUint256();
      int64 _int64 = _iter.asInt64();
      // ...
    }
  }
}

User Definited Type I256

The I256 user defined type can perform unchecked math operations using overloaded operators.

API

  • Math
    • _i256 + _i256: add a I256 to a I256
    • _i256 - _i256: subtract a I256 from a I256
    • _i256 * _i256: multiply a I256 by a I256
    • _i256 / _i256: divide a I256 from a I256
    • _i256 % _i256: get the modulus of I256s
  • Comparison
    • _i256 == _i256: compare I256 equality
    • _i256 != _i256: compare I256 non-equality
    • _i256 < _i256: compare I256 less than I256
    • _i256 <= _i256: compare I256 less than or equal to I256
    • _i256 > _i256: compare I256 greater than I256
    • _i256 >= _i256: compare I256 greater than or equal to I256
  • Logical
    • _i256 & _i256: bitwise AND on I256
    • _i256 | _i256: bitwise OR on I256
    • _i256 ^ _i256: bitwise XOR on I256
    • ~_i256: bitwise NOT on I256
Example
import { I256 } from '@0xdoublesharp/unsafe-math/contracts/types/I256.sol';

contract ContractB {
  function add(int256 a, int256 b) external pure returns (int256) {
    return (I256.wrap(a) - I256.wrap(b)).asInt256();
  }
}

Gas Usage Optimization Disabled

See GAS_REPORT.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 36

Native requirements
36 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@nomicfoundation/hardhat-chai-matchers
Environment
{
  "local_name": "@nomicfoundation/hardhat-chai-matchers",
  "alias": false,
  "peer_metadata": null
}
^1.0.6development
@nomicfoundation/hardhat-network-helpers
Environment
{
  "local_name": "@nomicfoundation/hardhat-network-helpers",
  "alias": false,
  "peer_metadata": null
}
^1.0.8development
@nomiclabs/hardhat-ethers
Environment
{
  "local_name": "@nomiclabs/hardhat-ethers",
  "alias": false,
  "peer_metadata": null
}
2.2.2development
@nomiclabs/hardhat-etherscan
Environment
{
  "local_name": "@nomiclabs/hardhat-etherscan",
  "alias": false,
  "peer_metadata": null
}
3.1.7development
@nomiclabs/hardhat-solhint
Environment
{
  "local_name": "@nomiclabs/hardhat-solhint",
  "alias": false,
  "peer_metadata": null
}
^3.0.0development
@typechain/ethers-v5
Environment
{
  "local_name": "@typechain/ethers-v5",
  "alias": false,
  "peer_metadata": null
}
^10.2.0development
@typechain/hardhat
Environment
{
  "local_name": "@typechain/hardhat",
  "alias": false,
  "peer_metadata": null
}
^6.1.5development
@types/chai
Environment
{
  "local_name": "@types/chai",
  "alias": false,
  "peer_metadata": null
}
^4.3.4development
@types/mocha
Environment
{
  "local_name": "@types/mocha",
  "alias": false,
  "peer_metadata": null
}
^10.0.1development
@types/node
Environment
{
  "local_name": "@types/node",
  "alias": false,
  "peer_metadata": null
}
^18.15.11development
@typescript-eslint/eslint-plugin
Environment
{
  "local_name": "@typescript-eslint/eslint-plugin",
  "alias": false,
  "peer_metadata": null
}
^5.57.1development
@typescript-eslint/parser
Environment
{
  "local_name": "@typescript-eslint/parser",
  "alias": false,
  "peer_metadata": null
}
^5.57.1development
chai
Environment
{
  "local_name": "chai",
  "alias": false,
  "peer_metadata": null
}
4.3.7development
dotenv
Environment
{
  "local_name": "dotenv",
  "alias": false,
  "peer_metadata": null
}
16.0.3development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
8.37.0development
eslint-config-prettier
Environment
{
  "local_name": "eslint-config-prettier",
  "alias": false,
  "peer_metadata": null
}
8.8.0development
hardhat
Environment
{
  "local_name": "hardhat",
  "alias": false,
  "peer_metadata": null
}
^2.13.0development
hardhat-abi-exporter
Environment
{
  "local_name": "hardhat-abi-exporter",
  "alias": false,
  "peer_metadata": null
}
^2.10.1development
hardhat-contract-sizer
Environment
{
  "local_name": "hardhat-contract-sizer",
  "alias": false,
  "peer_metadata": null
}
^2.8.0development
hardhat-deploy
Environment
{
  "local_name": "hardhat-deploy",
  "alias": false,
  "peer_metadata": null
}
^0.11.25development
hardhat-deploy-ethers
Environment
{
  "local_name": "hardhat-deploy-ethers",
  "alias": false,
  "peer_metadata": null
}
^0.3.0-beta.13development
hardhat-gas-reporter
Environment
{
  "local_name": "hardhat-gas-reporter",
  "alias": false,
  "peer_metadata": null
}
^1.0.9development
hardhat-spdx-license-identifier
Environment
{
  "local_name": "hardhat-spdx-license-identifier",
  "alias": false,
  "peer_metadata": null
}
^2.1.0development
hardhat-storage-layout
Environment
{
  "local_name": "hardhat-storage-layout",
  "alias": false,
  "peer_metadata": null
}
^0.1.7development
hardhat-tracer
Environment
{
  "local_name": "hardhat-tracer",
  "alias": false,
  "peer_metadata": null
}
^2.2.2development
hardhat-watcher
Environment
{
  "local_name": "hardhat-watcher",
  "alias": false,
  "peer_metadata": null
}
^2.5.0development
prettier
Environment
{
  "local_name": "prettier",
  "alias": false,
  "peer_metadata": null
}
2.8.7development
prettier-plugin-solidity
Environment
{
  "local_name": "prettier-plugin-solidity",
  "alias": false,
  "peer_metadata": null
}
1.1.3development
solhint
Environment
{
  "local_name": "solhint",
  "alias": false,
  "peer_metadata": null
}
3.4.1development
solhint-plugin-prettier
Environment
{
  "local_name": "solhint-plugin-prettier",
  "alias": false,
  "peer_metadata": null
}
^0.0.5development
solidity-coverage
Environment
{
  "local_name": "solidity-coverage",
  "alias": false,
  "peer_metadata": null
}
0.8.2development
ts-generator
Environment
{
  "local_name": "ts-generator",
  "alias": false,
  "peer_metadata": null
}
^0.1.1development
ts-node
Environment
{
  "local_name": "ts-node",
  "alias": false,
  "peer_metadata": null
}
~10.9.1development
tslint
Environment
{
  "local_name": "tslint",
  "alias": false,
  "peer_metadata": null
}
~6.1.3development
typechain
Environment
{
  "local_name": "typechain",
  "alias": false,
  "peer_metadata": null
}
^8.1.1development
typescript
Environment
{
  "local_name": "typescript",
  "alias": false,
  "peer_metadata": null
}
5.0.3development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-Vaa83ZpfXzA/4HdL9a8Xy1IJHJIsadexu94sU2MMJafL3Nr5bJGfXO2QAW+6IUJYGCy87v01eY75ah/md+1G+Q==",
  "shasum": "62a689934e99341ab29f2d4a6d238d9c1e25c1b1",
  "tarball": "https://registry.npmjs.org/@0xdoublesharp/unsafe-math/-/unsafe-math-0.0.13.tgz",
  "fileCount": 7,
  "unpackedSize": 25080,
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEQCID47Tzr8qi+cjhHtT9JZAv4DRYAnyNGE67AU56/fv75TAiB/Y/9za2Q7IjFNtN3ya4RA4pOdQDvJ1ZbPJtJc3h8i9A=="
    }
  ],
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkMlqTACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqGyQ/+NhV+xrJmY/sJl4VSjQxMlalubXD2+OfSS6S5Xthtc9/mARk/\r\nw+kD971WNR+QCbVRMwYCQewSLFkronBmZ+n7FEwIW8Ji5Ce0Pi/j5g6mtHnW\r\nQuXBzS4mu+s+YXTCn4UmlvgyRAq5Z8PvqtWrW96FH/l0RViIGWSRYzhQhdXN\r\na8nFvL+mB2bGSXL+yBbSrjkI7uQx3UT+nwLW9fp8VGFPd8pBuPnyoVhF/+OC\r\nmWDuUeI8ddayvgDpda8s4a8uV0ZLuUBJIveOqecK748anvtkgPjS5b+7jBN0\r\nRN8H96V6XI6I3MOMaCNTrdY0xqUgOedFBv57JE47MIpYHWs/iBbVTLeevFWS\r\nT69yJDXFQzPOpdFI4s1tCGmqe2UApr2OFh4yKGzInHFkV0OZ2jWtL1oVywMJ\r\noe9DHrGLGTIQeUA1Qpu+NfZagEJ/LSo+XybY+nQfaVDjDhKUzcCvijMJRIhx\r\n2jTLrnr7deTMrAo9vC9Z0UQX2EDXxJrsKbvRZEBYk6KTM3QjuWvp9aCgI9HK\r\nBWRpkTn6K0HERZhdOO+a2TpkVEipatXlFqzrw93aSSg8gVJL5zYvvuF2UJ3y\r\nED07fylKkk6nMwwd6YQxpR+OBXnNFiNwQB5JBsKHfkh79gyNjkZk6Tn7wsZy\r\n02jDxj3jj+7efkJVMTaW29B6lLIMkW1FGTI=\r\n=ZpEJ\r\n-----END PGP SIGNATURE-----\r\n"
}

Repository

PENDING
doublesharp/unsafe-math

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 23:06 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:aa850f11df970b98535f3a45190aff6afc04bea9fa5fcfcd3e41ee95325683cd

Other collected versions

0.0.1612 Apr0.0.1512 Apr0.0.1412 Apr0.0.1224 Mar0.0.1124 Mar0.0.1024 MarView all collected versions →