gnosis/solidity-arithmetic

Name: solidity-arithmetic

Owner: GNOSIS

Description: A solidity library for performing arithmetic.

Created: 2017-02-22 17:52:26.0

Updated: 2018-05-17 12:01:37.0

Pushed: 2017-03-09 15:16:02.0

Homepage: null

Size: 31

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

solidity-arithmetic

A solidity library for performing arithmetic.

Usage

The contract source is in the repo at contracts/Arithmetic.sol. Simply copy the contract file into your source tree and import it with:

rt "./path/to/Arithmetic.sol";

ract MyContract {
function myFunction() {
    uint a, b, d;
    // your code here
    uint result = Arithmetic.overflowResistantFraction(a, b, d);
}

library Arithmetic API
tion mul256By256(uint a, uint b)
constant
returns (uint ab32, uint ab1, uint ab0)

This function takes two unsigned 256-bit integers and multiplies them, returning a 512-bit result split into a high 256-bit limb, a middle 128-bit limb, and a low 128-bit limb.

tion div256_128By256(uint a21, uint a0, uint b)
constant
returns (uint q, uint r)

This function takes a unsigned 384-bit integer and divides it by a 256-bit integer, returning a high-bits truncated 256-bit quotient and a remainder. The 384-bit dividend is represented as a high 256-bit limb and a low 128-bit limb.

tion overflowResistantFraction(uint a, uint b, uint divisor)
returns (uint)

This function returns a 256-bit truncated a * b / divisor, where the division is integer division. The overflow from a * b is handled in a 512-bit buffer, so this method calculates the expression correctly for high values of a and b.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.