JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q77659F
  • License MIT

Unchecked uint256 and int256 math operations for Solidity 0.8

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@0xdoublesharp/unsafe-math) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    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 unsafe-math
    yarn add -D unsafe-math

    UnsafeMath

    uint256

    import { UnsafeMath } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';
    
    contract ContractA {
      using UnsafeMath for uint256;
    
      function add(uint256 a, uint256 b) external returns (uint256) {
        return a.add(b);
      }
    
      function while(uint256 times) external {
          uint256 _iter = times;
          while (_iter != 0) {
            _iter = _iter.dec();
            int64 _int64 = int64(_iter);
            // ...
        }
      }
    
      function for(uint256 times) external {
        for (uint256 _iter; iter < times; _iter = _iter.inc()) {
            int64 _int64 = int64(_iter);
            // ...
        }
      }
    }

    int256

    import { UnsafeMath } from '@0xdoublesharp/unsafe-math/contracts/UnsafeMath.sol';
    
    contract ContractB {
      using UnsafeMath for int256;
    
      function sub(int256 a, int256 b) external returns (int256) {
        return a.sub(b);
      }
    }

    Unsafe Types

    U256

    import {U256} from '@0xdoublesharp/unsafe-math/contracts/types/U256.sol';
    
    contract ContractA {
      function add(uint256 a, uint256 b) external returns (uint256) {
          return (U256.wrap(a) - U256.wrap(b)).asUint256();
      }
    
      function while(uint256 times) external {
          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 {
        U256 _times = U256.wrap(times);
        for (U256 _iter; iter < _times; _iter = _iter.inc()) {
            uint256 _uint256 = _iter.asUint256();
            int64 _int64 = _iter.asInt64();
            // ...
        }
      }
    }

    I256

    import { I256 } from '@0xdoublesharp/unsafe-math/contracts/types/I256.sol';
    
    contract ContractB {
      function add(int256 a, int256 b) external returns (int256) {
        return (I256.wrap(a) - I256.wrap(b)).asInt256();
      }
    }

    Unsafe Type Libraries for uint256 and int256

    UnsafeU256

    import { UnsafeI256 } from '@0xdoublesharp/unsafe-math/contracts/UnsafeU256.sol';
    import { I256 } from '@0xdoublesharp/unsafe-math/contracts/types/U256.sol';
    
    contract ContractA {
      using UnsafeU256 for U256;
      using UnsafeU256 for uint256;
      using UnsafeU256 for int256;
    
      function add(int256 a, uint256 b) external returns (uint256) {
        return a.asU256().add(b).asUint256();
      }
    
      function sub(uint256 a, uint256 b) external returns (uint256) {
        return (a.asU256() - a.asU256()).asUint256();
      }
    }

    UnsafeI256

    import { UnsafeI256 } from '@0xdoublesharp/unsafe-math/contracts/UnsafeI256.sol';
    import { I256 } from '@0xdoublesharp/unsafe-math/contracts/types/I256.sol';
    
    contract ContractB {
      using UnsafeI256 for I256;
      using UnsafeI256 for uint256;
      using UnsafeI256 for int256;
    
      function add(int256 a, uint256 b) external returns (int256) {
        return a.asI256().add(int256(b)).asInt256();
      }
    
      function sub(uint256 a, uint256 b) external returns (int256) {
        return (a.asI256() - a.asI256()).asInt256();
      }
    }