Skip to main content

MathLibs

Git Source Author: @oscarsernarosero @mpetersoCode55 @cirsteve current implementation is using the FixedPointMathLib library from Solady and the Solidity_Uint512 library Wrapper functions to act as an abstraction layer between Equations and the Math library we’re using.

State Variables

WAD

uint256 constant WAD = FixedPointMathLib.WAD;

Functions

divideToWAD

expects the parameters to not have been multiplied by WAD, and the result will be a WAD number Provides an abstraction layer for division between liquidity-altbc and the underlying math library
function divideToWAD(uint256 numerator, uint256 denominator) internal pure returns (uint256 result);
Parameters
NameTypeDescription
numeratoruint256the numerator for the division
denominatoruint256the denominator for the division
Returns
NameTypeDescription
resultuint256of the division operation

uncheckedMultiply

expects the parameters to not have been multiplied by WAD, and the result will be a WAD number Provides an abstraction layer for multiplication between liquidity-altbc and the underlying math library
function uncheckedMultiply(uint256 x, uint256 y) internal pure returns (uint256 result);
Parameters
NameTypeDescription
xuint256one of the factors for the multiplication
yuint256the other factors for the multiplication
Returns
NameTypeDescription
resultuint256of the multiplication operation

powWad

function powWad(int256 x, int256 y) internal pure returns (int256);

convertToWAD

Converts a raw number to a WAD number
function convertToWAD(uint256 value) internal pure returns (uint256 result);
Parameters
NameTypeDescription
valueuint256The number to be converted
Returns
NameTypeDescription
resultuint256resulting WAD number

convertToRaw

Converts a WAD number to a raw number
function convertToRaw(uint256 value) internal pure returns (uint256 result);
Parameters
NameTypeDescription
valueuint256The number to be converted
Returns
NameTypeDescription
resultuint256resulting raw number

mul256x256

Calculates the product of two uint256 Used the chinese remainder theoreme
function mul256x256(uint256 a, uint256 b) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
auint256A uint256 representing the first factor
buint256A uint256 representing the second factor
Returns
NameTypeDescription
r0uint256The result as an uint512. r0 contains the lower bits
r1uint256The higher bits of the result

mul512x256

Calculates the product of two uint512 and uint256 Used the chinese remainder theoreme
function mul512x256(uint256 a0, uint256 a1, uint256 b) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing lower bits of the first factor
a1uint256A uint256 representing higher bits of the first factor
buint256A uint256 representing the second factor
Returns
NameTypeDescription
r0uint256The result as an uint512. r0 contains the lower bits
r1uint256The higher bits of the result

mul512x256In768

Calculates the product of a uint512 and uint256. The result is a uint768. Used the chinese remainder theoreme
function mul512x256In768(uint256 a0, uint256 a1, uint256 b)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first factor
a1uint256A uint256 representing the higher bits of the first factor
buint256A uint256 representing the second factor
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The higher bits of the result
r2uint256The highest bits of the result

mul512x512In1024

Calculates the product of two uint512. The result is a uint1024. Used the chinese remainder theoreme
function mul512x512In1024(uint256 a0, uint256 a1, uint256 b0, uint256 b1)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2, uint256 r3);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first factor
a1uint256A uint256 representing the higher bits of the first factor
b0uint256A uint256 representing the lower bits of the second factor
b1uint256A uint256 representing the higher bits of the second factor
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The high bits of the result
r2uint256The higher bits of the result
r3uint256The highest bits of the result

mulMod256x256

Calculates the product and remainder of two uint256 Used the chinese remainder theoreme
function mulMod256x256(uint256 a, uint256 b, uint256 c) internal pure returns (uint256 r0, uint256 r1, uint256 r2);
Parameters
NameTypeDescription
auint256A uint256 representing the first factor
buint256A uint256 representing the second factor
cuint256
Returns
NameTypeDescription
r0uint256The result as an uint512. r0 contains the lower bits
r1uint256The higher bits of the result
r2uint256The remainder

add512x512

Calculates the difference of two uint512
function add512x512(uint256 a0, uint256 a1, uint256 b0, uint256 b1) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first addend
a1uint256A uint256 representing the higher bits of the first addend
b0uint256A uint256 representing the lower bits of the seccond addend
b1uint256A uint256 representing the higher bits of the seccond addend
Returns
NameTypeDescription
r0uint256The result as an uint512. r0 contains the lower bits
r1uint256The higher bits of the result

add768x768

Calculates the sum of two uint768. The result is a uint768.
function add768x768(uint256 a0, uint256 a1, uint256 a2, uint256 b0, uint256 b1, uint256 b2)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first addend
a1uint256A uint256 representing the higher bits of the first addend
a2uint256A uint256 representing the highest bits of the first addend
b0uint256A uint256 representing the lower bits of the second addend
b1uint256A uint256 representing the higher bits of the second addend
b2uint256A uint256 representing the highest bits of the second addend
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The higher bits of the result
r2uint256The highest bits of the result

add1024x1024

Calculates the sum of two Uint1024. The result is a Uint1024.
function add1024x1024(uint256 a0, uint256 a1, uint256 a2, uint256 a3, uint256 b0, uint256 b1, uint256 b2, uint256 b3)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2, uint256 r3);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first addend
a1uint256A uint256 representing the high bits of the first addend
a2uint256A uint256 representing the higher bits of the first addend
a3uint256A uint256 representing the highest bits of the first addend
b0uint256A uint256 representing the lower bits of the second addend
b1uint256A uint256 representing the high bits of the second addend
b2uint256A uint256 representing the higher bits of the second addend
b3uint256A uint256 representing the highest bits of the second addend
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The high bits of the result
r2uint256The higher bits of the result
r3uint256The highest bits of the result

sub512x512

Calculates the difference of two uint512
function sub512x512(uint256 a0, uint256 a1, uint256 b0, uint256 b1) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the minuend
a1uint256A uint256 representing the higher bits of the minuend
b0uint256A uint256 representing the lower bits of the subtrahend
b1uint256A uint256 representing the higher bits of the subtrahend
Returns
NameTypeDescription
r0uint256The result as an uint512. r0 contains the lower bits
r1uint256The higher bits of the result

sub768x768

Calculates the difference of two uint768. The result is a uint768.
function sub768x768(uint256 a0, uint256 a1, uint256 a2, uint256 b0, uint256 b1, uint256 b2)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the minuend
a1uint256A uint256 representing the higher bits of the minuend
a2uint256A uint256 representing the highest bits of the minuend
b0uint256A uint256 representing the lower bits of the subtrahend
b1uint256A uint256 representing the higher bits of the subtrahend
b2uint256A uint256 representing the highest bits of the subtrahend
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The higher bits of the result
r2uint256The highest bits of the result

sub1024x1024

Calculates the difference of two Uint1024. The result is a Uint1024.
function sub1024x1024(uint256 a0, uint256 a1, uint256 a2, uint256 a3, uint256 b0, uint256 b1, uint256 b2, uint256 b3)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2, uint256 r3);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the minuend
a1uint256A uint256 representing the high bits of the minuend
a2uint256A uint256 representing the higher bits of the minuend
a3uint256A uint256 representing the highest bits of the minuend
b0uint256A uint256 representing the lower bits of the subtrahend
b1uint256A uint256 representing the high bits of the subtrahend
b2uint256A uint256 representing the higher bits of the subtrahend
b3uint256A uint256 representing the highest bits of the subtrahend
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The high bits of the result
r2uint256The higher bits of the result
r3uint256The highest bits of the result

divRem512x256

Calculates the division of a 512 bit unsigned integer by a 256 bit integer. It requires the remainder to be known and the result must fit in a 256 bit integer For a detailed explaination see: https://www.researchgate.net/internalation/235765881_Efficient_long_division_via_Montgomery_multiply
function divRem512x256(uint256 a0, uint256 a1, uint256 b, uint256 rem) internal pure returns (uint256 r);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the nominator
a1uint256A uint256 representing the high bits of the nominator
buint256A uint256 representing the denominator
remuint256A uint256 representing the remainder of the devision. The algorithm is cheaper to compute if the remainder is known. The remainder often be retreived cheaply using the mulmod and addmod operations
Returns
NameTypeDescription
ruint256The result as an uint256. Result must have at most 256 bit

div512x256

Calculates the division of a 512 bit unsigned integer by a 256 bit integer. It requires the result to fit in a 256 bit integer For a detailed explaination see: https://www.researchgate.net/internalation/235765881_Efficient_long_division_via_Montgomery_multiply
function div512x256(uint256 a0, uint256 a1, uint256 b) internal pure returns (uint256 r);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the nominator
a1uint256A uint256 representing the high bits of the nominator
buint256A uint256 representing the denominator
Returns
NameTypeDescription
ruint256The result as an uint256. Result must have at most 256 bit

sqrt256

Calculates the square root of x, rounding down Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
function sqrt256(uint256 x) internal pure returns (uint256 s);
Parameters
NameTypeDescription
xuint256The uint256 number for which to calculate the square root
Returns
NameTypeDescription
suint256The square root as an uint256

sqrt512

Calculates the square root of a 512 bit unsigned integer, rounding down Uses the Karatsuba Square Root method. See https://hal.inria.fr/inria-00072854/document for details
function sqrt512(uint256 a0, uint256 a1) internal pure returns (uint256 s);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the input
a1uint256A uint256 representing the high bits of the input
Returns
NameTypeDescription
suint256The square root as an uint256. Result has at most 256 bit

gt512

x > y Tells if x is greater than y where x and y are 512 bit numbers
function gt512(uint256 x0, uint256 x1, uint256 y0, uint256 y1) internal pure returns (bool gt);
Parameters
NameTypeDescription
x0uint256lower bits of x
x1uint256higher bits of y
y0uint256lower bits of y
y1uint256higher bits of y
Returns
NameTypeDescription
gtboolboolean. True if x > y

eq512

x == y Tells if x is equal to y where x and y are 512 bit numbers
function eq512(uint256 x0, uint256 x1, uint256 y0, uint256 y1) internal pure returns (bool eq);
Parameters
NameTypeDescription
x0uint256lower bits of x
x1uint256higher bits of y
y0uint256lower bits of y
y1uint256higher bits of y
Returns
NameTypeDescription
eqboolboolean. True if x = y

ge512

x >= y Tells if x is greater or equal than y where x and y are 512 bit numbers
function ge512(uint256 x0, uint256 x1, uint256 y0, uint256 y1) internal pure returns (bool ge);
Parameters
NameTypeDescription
x0uint256lower bits of x
x1uint256higher bits of y
y0uint256lower bits of y
y1uint256higher bits of y
Returns
NameTypeDescription
geboolboolean. True if x >= y

lt512

x < y Tells if x is less than y where x and y are 512 bit numbers
function lt512(uint256 x0, uint256 x1, uint256 y0, uint256 y1) internal pure returns (bool lt);
Parameters
NameTypeDescription
x0uint256lower bits of x
x1uint256higher bits of y
y0uint256lower bits of y
y1uint256higher bits of y
Returns
NameTypeDescription
ltboolboolean. True if x < y

lt768

Checks the minuend(a0-a2) is greater than the right operand(b0-b2) Used as an underflow/negative result indicator for subtraction methods
function lt768(uint256 a0, uint256 a1, uint256 a2, uint256 b0, uint256 b1, uint256 b2) internal pure returns (bool);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the minuend
a1uint256A uint256 representing the high bits of the minuend
a2uint256A uint256 representing the higher bits of the minuend
b0uint256A uint256 representing the lower bits of the subtrahend
b1uint256A uint256 representing the high bits of the subtrahend
b2uint256A uint256 representing the higher bits of the subtrahend
Returns
NameTypeDescription
<none>boolReturns true if there would be an underflow/negative result

lt1024

Checks the minuend(a0-a3) is greater than the right operand(b0-b3) Used as an underflow/negative result indicator for subtraction methods
function lt1024(uint256 a0, uint256 a1, uint256 a2, uint256 a3, uint256 b0, uint256 b1, uint256 b2, uint256 b3)
    internal
    pure
    returns (bool);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the minuend
a1uint256A uint256 representing the high bits of the minuend
a2uint256A uint256 representing the higher bits of the minuend
a3uint256A uint256 representing the highest bits of the minuend
b0uint256A uint256 representing the lower bits of the subtrahend
b1uint256A uint256 representing the high bits of the subtrahend
b2uint256A uint256 representing the higher bits of the subtrahend
b3uint256A uint256 representing the highest bits of the subtrahend
Returns
NameTypeDescription
<none>boolReturns true if there would be an underflow/negative result

div512ByPowerOf2

Calculates the division of a 512 bit unsigned integer by a denominator which is a power of 2. It doesn’t require the result to be a uint256. very useful if a division of a 512 is expected to be also a 512.
function div512ByPowerOf2(uint256 a0, uint256 a1, uint8 n)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 remainder);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the numerator
a1uint256A uint256 representing the high bits of the numerator
nuint8the power of 2 that the division will be carried out by (demominator = 2**n).
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The higher bits of the result
remainderuint256of the division

div512x256ResultIn512

this function requires a0 to be greater than 0 and less or equal than 2**254 Calculates the division of a 512-bit unsigned integer by a 256-bit uint where the result can be a 512 bit unsigned integer.
function div512x256ResultIn512(uint256 a0, uint256 a1, uint256 b) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the numerator
a1uint256A uint256 representing the high bits of the numerator
buint256A uint256 representing the denominator
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The highre bits of the result

div512x512

Calculates the division of a 512-bit unsigned integer by a 256-bit uint where the result can be a 512 bit unsigned integer.
function div512x512(uint256 a0, uint256 a1, uint256 b0, uint256 b1) internal pure returns (uint256 result);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the numerator
a1uint256A uint256 representing the high bits of the numerator
b0uint256A uint256 representing thehigh bits of the denominator
b1uint256A uint256 representing the low bits of the denominator
Returns
NameTypeDescription
resultuint256result

div768x256

Used long division Calculates the division of a uint768 by a uint256. The result is a uint768.
function div768x256(uint256 a0, uint256 a1, uint256 a2, uint256 b)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lower bits of the first factor
a1uint256A uint256 representing the middle bits of the first factor
a2uint256A uint256 representing the higher bits of the first factor
buint256A uint256 representing the divisor
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The middle bits of the result
r2uint256The higher bits of the result

div768ByPowerOf2

Calculates the division of a 768-bit unsigned integer by a denominator which is a power of 2 less than 256.
function div768ByPowerOf2(uint256 a0, uint256 a1, uint256 a2, uint8 n)
    internal
    pure
    returns (uint256 r0, uint256 r1, uint256 r2, uint256 remainder);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the numerator
a1uint256A uint256 representing the middle bits of the numerator
a2uint256A uint256 representing the high bits of the numerator
nuint8the power of 2 that the division will be carried out by (demominator = 2**n).
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The middle bits of the result
r2uint256The higher bits of the result
remainderuint256of the division

divRem1024x512In512

it requires to previously know the remainder of the division Calculates the division a / b where a is a 1024-bit unsigned integer and b is a uint512.
function divRem1024x512In512(
    uint256 a0,
    uint256 a1,
    uint256 a2,
    uint256 a3,
    uint256 b0,
    uint256 b1,
    uint256 rem0,
    uint256 rem1
) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing the lowest bits of a
a1uint256A uint256 representing the mid-lower bits of a
a2uint256A uint256 representing the mid-higher bits of a
a3uint256A uint256 representing the highest bits of a
b0uint256A uint256 representing the lower bits of b
b1uint256A uint256 representing the higher bits of b
rem0uint256A uint256 representing the lower bits of the remainder of the division
rem1uint256A uint256 representing the higher bits of the remainder of the division
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The high bits of the result

convertToRaw512

this function requires a0 to be greater than 0 and less or equal than 2**254 Divides a 512 bit unsigned integer by WAD where the result can be a 512 bit unsigned integer.
function convertToRaw512(uint256 a0, uint256 a1) internal pure returns (uint256 r0, uint256 r1);
Parameters
NameTypeDescription
a0uint256A uint256 representing the low bits of the numerator
a1uint256A uint256 representing the high bits of the numerator
Returns
NameTypeDescription
r0uint256The lower bits of the result
r1uint256The highre bits of the result

lnWAD2Negative

function lnWAD2Negative(uint256 x) internal pure returns (uint256 result);
Parameters
NameTypeDescription
xuint256the number to take the natural log of. Expressed as a WAD ** 2
Returns
NameTypeDescription
resultuint256the ln of x multiplied by -1. Expressed as a WAD ** 2

findWADsToSlashTo0

this function tells how many WADs a number needs to be divided by to get to 0
function findWADsToSlashTo0(uint256 x) internal pure returns (uint256 precisionSlashingFactor);
Parameters
NameTypeDescription
xuint256the number to be divided
Returns
NameTypeDescription
precisionSlashingFactoruint256the number of WADs needed to be divided to get to 0