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
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| Name | Type | Description |
|---|---|---|
numerator | uint256 | the numerator for the division |
denominator | uint256 | the denominator for the division |
| Name | Type | Description |
|---|---|---|
result | uint256 | of 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| Name | Type | Description |
|---|---|---|
x | uint256 | one of the factors for the multiplication |
y | uint256 | the other factors for the multiplication |
| Name | Type | Description |
|---|---|---|
result | uint256 | of the multiplication operation |
powWad
convertToWAD
Converts a raw number to a WAD number| Name | Type | Description |
|---|---|---|
value | uint256 | The number to be converted |
| Name | Type | Description |
|---|---|---|
result | uint256 | resulting WAD number |
convertToRaw
Converts a WAD number to a raw number| Name | Type | Description |
|---|---|---|
value | uint256 | The number to be converted |
| Name | Type | Description |
|---|---|---|
result | uint256 | resulting raw number |
mul256x256
Calculates the product of two uint256 Used the chinese remainder theoreme| Name | Type | Description |
|---|---|---|
a | uint256 | A uint256 representing the first factor |
b | uint256 | A uint256 representing the second factor |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The result as an uint512. r0 contains the lower bits |
r1 | uint256 | The higher bits of the result |
mul512x256
Calculates the product of two uint512 and uint256 Used the chinese remainder theoreme| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing lower bits of the first factor |
a1 | uint256 | A uint256 representing higher bits of the first factor |
b | uint256 | A uint256 representing the second factor |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The result as an uint512. r0 contains the lower bits |
r1 | uint256 | The higher bits of the result |
mul512x256In768
Calculates the product of a uint512 and uint256. The result is a uint768. Used the chinese remainder theoreme| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first factor |
a1 | uint256 | A uint256 representing the higher bits of the first factor |
b | uint256 | A uint256 representing the second factor |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The higher bits of the result |
r2 | uint256 | The highest bits of the result |
mul512x512In1024
Calculates the product of two uint512. The result is a uint1024. Used the chinese remainder theoreme| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first factor |
a1 | uint256 | A uint256 representing the higher bits of the first factor |
b0 | uint256 | A uint256 representing the lower bits of the second factor |
b1 | uint256 | A uint256 representing the higher bits of the second factor |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The high bits of the result |
r2 | uint256 | The higher bits of the result |
r3 | uint256 | The highest bits of the result |
mulMod256x256
Calculates the product and remainder of two uint256 Used the chinese remainder theoreme| Name | Type | Description |
|---|---|---|
a | uint256 | A uint256 representing the first factor |
b | uint256 | A uint256 representing the second factor |
c | uint256 |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The result as an uint512. r0 contains the lower bits |
r1 | uint256 | The higher bits of the result |
r2 | uint256 | The remainder |
add512x512
Calculates the difference of two uint512| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first addend |
a1 | uint256 | A uint256 representing the higher bits of the first addend |
b0 | uint256 | A uint256 representing the lower bits of the seccond addend |
b1 | uint256 | A uint256 representing the higher bits of the seccond addend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The result as an uint512. r0 contains the lower bits |
r1 | uint256 | The higher bits of the result |
add768x768
Calculates the sum of two uint768. The result is a uint768.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first addend |
a1 | uint256 | A uint256 representing the higher bits of the first addend |
a2 | uint256 | A uint256 representing the highest bits of the first addend |
b0 | uint256 | A uint256 representing the lower bits of the second addend |
b1 | uint256 | A uint256 representing the higher bits of the second addend |
b2 | uint256 | A uint256 representing the highest bits of the second addend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The higher bits of the result |
r2 | uint256 | The highest bits of the result |
add1024x1024
Calculates the sum of two Uint1024. The result is a Uint1024.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first addend |
a1 | uint256 | A uint256 representing the high bits of the first addend |
a2 | uint256 | A uint256 representing the higher bits of the first addend |
a3 | uint256 | A uint256 representing the highest bits of the first addend |
b0 | uint256 | A uint256 representing the lower bits of the second addend |
b1 | uint256 | A uint256 representing the high bits of the second addend |
b2 | uint256 | A uint256 representing the higher bits of the second addend |
b3 | uint256 | A uint256 representing the highest bits of the second addend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The high bits of the result |
r2 | uint256 | The higher bits of the result |
r3 | uint256 | The highest bits of the result |
sub512x512
Calculates the difference of two uint512| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the minuend |
a1 | uint256 | A uint256 representing the higher bits of the minuend |
b0 | uint256 | A uint256 representing the lower bits of the subtrahend |
b1 | uint256 | A uint256 representing the higher bits of the subtrahend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The result as an uint512. r0 contains the lower bits |
r1 | uint256 | The higher bits of the result |
sub768x768
Calculates the difference of two uint768. The result is a uint768.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the minuend |
a1 | uint256 | A uint256 representing the higher bits of the minuend |
a2 | uint256 | A uint256 representing the highest bits of the minuend |
b0 | uint256 | A uint256 representing the lower bits of the subtrahend |
b1 | uint256 | A uint256 representing the higher bits of the subtrahend |
b2 | uint256 | A uint256 representing the highest bits of the subtrahend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The higher bits of the result |
r2 | uint256 | The highest bits of the result |
sub1024x1024
Calculates the difference of two Uint1024. The result is a Uint1024.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the minuend |
a1 | uint256 | A uint256 representing the high bits of the minuend |
a2 | uint256 | A uint256 representing the higher bits of the minuend |
a3 | uint256 | A uint256 representing the highest bits of the minuend |
b0 | uint256 | A uint256 representing the lower bits of the subtrahend |
b1 | uint256 | A uint256 representing the high bits of the subtrahend |
b2 | uint256 | A uint256 representing the higher bits of the subtrahend |
b3 | uint256 | A uint256 representing the highest bits of the subtrahend |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The high bits of the result |
r2 | uint256 | The higher bits of the result |
r3 | uint256 | The 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| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the nominator |
a1 | uint256 | A uint256 representing the high bits of the nominator |
b | uint256 | A uint256 representing the denominator |
rem | uint256 | A 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 |
| Name | Type | Description |
|---|---|---|
r | uint256 | The 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| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the nominator |
a1 | uint256 | A uint256 representing the high bits of the nominator |
b | uint256 | A uint256 representing the denominator |
| Name | Type | Description |
|---|---|---|
r | uint256 | The 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| Name | Type | Description |
|---|---|---|
x | uint256 | The uint256 number for which to calculate the square root |
| Name | Type | Description |
|---|---|---|
s | uint256 | The 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| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the input |
a1 | uint256 | A uint256 representing the high bits of the input |
| Name | Type | Description |
|---|---|---|
s | uint256 | The 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| Name | Type | Description |
|---|---|---|
x0 | uint256 | lower bits of x |
x1 | uint256 | higher bits of y |
y0 | uint256 | lower bits of y |
y1 | uint256 | higher bits of y |
| Name | Type | Description |
|---|---|---|
gt | bool | boolean. True if x > y |
eq512
x == y Tells if x is equal to y where x and y are 512 bit numbers| Name | Type | Description |
|---|---|---|
x0 | uint256 | lower bits of x |
x1 | uint256 | higher bits of y |
y0 | uint256 | lower bits of y |
y1 | uint256 | higher bits of y |
| Name | Type | Description |
|---|---|---|
eq | bool | boolean. True if x = y |
ge512
x >= y Tells if x is greater or equal than y where x and y are 512 bit numbers| Name | Type | Description |
|---|---|---|
x0 | uint256 | lower bits of x |
x1 | uint256 | higher bits of y |
y0 | uint256 | lower bits of y |
y1 | uint256 | higher bits of y |
| Name | Type | Description |
|---|---|---|
ge | bool | boolean. True if x >= y |
lt512
x < y Tells if x is less than y where x and y are 512 bit numbers| Name | Type | Description |
|---|---|---|
x0 | uint256 | lower bits of x |
x1 | uint256 | higher bits of y |
y0 | uint256 | lower bits of y |
y1 | uint256 | higher bits of y |
| Name | Type | Description |
|---|---|---|
lt | bool | boolean. 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| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the minuend |
a1 | uint256 | A uint256 representing the high bits of the minuend |
a2 | uint256 | A uint256 representing the higher bits of the minuend |
b0 | uint256 | A uint256 representing the lower bits of the subtrahend |
b1 | uint256 | A uint256 representing the high bits of the subtrahend |
b2 | uint256 | A uint256 representing the higher bits of the subtrahend |
| Name | Type | Description |
|---|---|---|
<none> | bool | Returns 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| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the minuend |
a1 | uint256 | A uint256 representing the high bits of the minuend |
a2 | uint256 | A uint256 representing the higher bits of the minuend |
a3 | uint256 | A uint256 representing the highest bits of the minuend |
b0 | uint256 | A uint256 representing the lower bits of the subtrahend |
b1 | uint256 | A uint256 representing the high bits of the subtrahend |
b2 | uint256 | A uint256 representing the higher bits of the subtrahend |
b3 | uint256 | A uint256 representing the highest bits of the subtrahend |
| Name | Type | Description |
|---|---|---|
<none> | bool | Returns 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the numerator |
a1 | uint256 | A uint256 representing the high bits of the numerator |
n | uint8 | the power of 2 that the division will be carried out by (demominator = 2**n). |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The higher bits of the result |
remainder | uint256 | of 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the numerator |
a1 | uint256 | A uint256 representing the high bits of the numerator |
b | uint256 | A uint256 representing the denominator |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the numerator |
a1 | uint256 | A uint256 representing the high bits of the numerator |
b0 | uint256 | A uint256 representing thehigh bits of the denominator |
b1 | uint256 | A uint256 representing the low bits of the denominator |
| Name | Type | Description |
|---|---|---|
result | uint256 | result |
div768x256
Used long division Calculates the division of a uint768 by a uint256. The result is a uint768.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lower bits of the first factor |
a1 | uint256 | A uint256 representing the middle bits of the first factor |
a2 | uint256 | A uint256 representing the higher bits of the first factor |
b | uint256 | A uint256 representing the divisor |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The middle bits of the result |
r2 | uint256 | The 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the numerator |
a1 | uint256 | A uint256 representing the middle bits of the numerator |
a2 | uint256 | A uint256 representing the high bits of the numerator |
n | uint8 | the power of 2 that the division will be carried out by (demominator = 2**n). |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The middle bits of the result |
r2 | uint256 | The higher bits of the result |
remainder | uint256 | of 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the lowest bits of a |
a1 | uint256 | A uint256 representing the mid-lower bits of a |
a2 | uint256 | A uint256 representing the mid-higher bits of a |
a3 | uint256 | A uint256 representing the highest bits of a |
b0 | uint256 | A uint256 representing the lower bits of b |
b1 | uint256 | A uint256 representing the higher bits of b |
rem0 | uint256 | A uint256 representing the lower bits of the remainder of the division |
rem1 | uint256 | A uint256 representing the higher bits of the remainder of the division |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The 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.| Name | Type | Description |
|---|---|---|
a0 | uint256 | A uint256 representing the low bits of the numerator |
a1 | uint256 | A uint256 representing the high bits of the numerator |
| Name | Type | Description |
|---|---|---|
r0 | uint256 | The lower bits of the result |
r1 | uint256 | The highre bits of the result |
lnWAD2Negative
| Name | Type | Description |
|---|---|---|
x | uint256 | the number to take the natural log of. Expressed as a WAD ** 2 |
| Name | Type | Description |
|---|---|---|
result | uint256 | the 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| Name | Type | Description |
|---|---|---|
x | uint256 | the number to be divided |
| Name | Type | Description |
|---|---|---|
precisionSlashingFactor | uint256 | the number of WADs needed to be divided to get to 0 |