BigMathVault
this is an optimized version mainly created by taking Fluid vault's codebase into consideration so it's use is limited for other cases.
State Variables
COEFFICIENT_SIZE_DEBT_FACTOR
uint256 private constant COEFFICIENT_SIZE_DEBT_FACTOR = 35;
EXPONENT_SIZE_DEBT_FACTOR
uint256 private constant EXPONENT_SIZE_DEBT_FACTOR = 15;
COEFFICIENT_MAX_DEBT_FACTOR
uint256 private constant COEFFICIENT_MAX_DEBT_FACTOR = (1 << COEFFICIENT_SIZE_DEBT_FACTOR) - 1;
EXPONENT_MAX_DEBT_FACTOR
uint256 private constant EXPONENT_MAX_DEBT_FACTOR = (1 << EXPONENT_SIZE_DEBT_FACTOR) - 1;
DECIMALS_DEBT_FACTOR
uint256 private constant DECIMALS_DEBT_FACTOR = 16384;
MAX_MASK_DEBT_FACTOR
uint256 internal constant MAX_MASK_DEBT_FACTOR = (1 << (COEFFICIENT_SIZE_DEBT_FACTOR + EXPONENT_SIZE_DEBT_FACTOR)) - 1;
PRECISION
uint256 internal constant PRECISION = 64;
TWO_POWER_64
uint256 internal constant TWO_POWER_64 = 1 << PRECISION;
TWO_POWER_69_MINUS_1
uint256 internal constant TWO_POWER_69_MINUS_1 = (1 << 69) - 1;
COEFFICIENT_PLUS_PRECISION
uint256 private constant COEFFICIENT_PLUS_PRECISION = COEFFICIENT_SIZE_DEBT_FACTOR + PRECISION;
COEFFICIENT_PLUS_PRECISION_MINUS_1
uint256 private constant COEFFICIENT_PLUS_PRECISION_MINUS_1 = COEFFICIENT_PLUS_PRECISION - 1;
TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1
uint256 private constant TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1 = (1 << COEFFICIENT_PLUS_PRECISION_MINUS_1) - 1;
TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1_MINUS_1
uint256 private constant TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1_MINUS_1 =
(1 << (COEFFICIENT_PLUS_PRECISION_MINUS_1 - 1)) - 1;
Functions
mulDivNormal
multiplies a normal
number with a bigNumber1
and then divides by bigNumber2
.
*For vault's use case MUST always:
- bigNumbers have exponent size 15 bits
- bigNumbers have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision) so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.
- bigNumber1 (debt factor) always have exponent >= 1 & <= 16384
- bigNumber2 (connection factor) always have exponent >= 1 & <= 32767 (15 bits)
- bigNumber2 always >= bigNumber1 (connection factor can never be < base branch debt factor)
- as a result of previous points, numbers must never be 0
- normal is positionRawDebt and is always within 10000 and type(int128).max*
function mulDivNormal(uint256 normal, uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256);
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | normal * bigNumber1 / bigNumber2 |
mulDivBigNumber
multiplies a bigNumber
with normal number1
and then divides by TWO_POWER_64
.
*For vault's use case (calculating new branch debt factor after liquidation):
- number1 is debtFactor, intialized as TWO_POWER_64 and reduced from there, hence it's always <= TWO_POWER_64 and always > 0.
- bigNumber is branch debt factor, which starts as ((X35 << 15) | (1 << 14)) and reduces from there.
- bigNumber must have have exponent size 15 bits and be >= 1 & <= 16384
- bigNumber must have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision) so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.*
function mulDivBigNumber(uint256 bigNumber, uint256 number1) internal pure returns (uint256 result);
Parameters
Name | Type | Description |
---|---|---|
bigNumber | uint256 | Coefficient |
number1 | uint256 | normal number. |
Returns
Name | Type | Description |
---|---|---|
result | uint256 | bigNumber * number1 / TWO_POWER_64. |
mulBigNumber
multiplies a bigNumber1
with another bigNumber2
.
*For vault's use case (calculating connection factor of merged branches userTickDebtFactor * connectionDebtFactor *... connectionDebtFactor):
- bigNumbers must have have exponent size 15 bits and be >= 1 & <= 32767
- bigNumber must have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision) so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.*
sum of exponents from bigNumber1
bigNumber2
should be > 16384.
e.g. res = bigNumber1 * bigNumber2 = (coe1, exp1) * (coe2, exp2) >> decimal
= (coe1coe2>>overflow, exp1+exp2+overflow-decimal)*
function mulBigNumber(uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
bigNumber1 | uint256 | BigNumber format with coefficient and exponent. |
bigNumber2 | uint256 | BigNumber format with coefficient and exponent. |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | BigNumber format with coefficient and exponent |
divBigNumber
divides a bigNumber1
by bigNumber2
.
*For vault's use case (calculating connectionFactor_ = baseBranchDebtFactor / currentBranchDebtFactor) bigNumbers MUST always:
- have exponent size 15 bits and be >= 1 & <= 16384
- have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision) so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.
- as a result of previous points, numbers must never be 0 e.g. res = bigNumber1 / bigNumber2 = (coe1, exp1) / (coe2, exp2) << decimal = ((coe1<<precision_)/coe2, exp1+decimal-exp2-precision_)*
function divBigNumber(uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256);
Parameters
Name | Type | Description |
---|---|---|
bigNumber1 | uint256 | BigNumber format with coefficient and exponent |
bigNumber2 | uint256 | BigNumber format with coefficient and exponent |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | BigNumber format with coefficient and exponent Returned connection factor can only ever be >= baseBranchDebtFactor (c = x*100/y with both x,y > 0 & x,y <= 100: c can only ever be >= x) |
- State Variables
- COEFFICIENT_SIZE_DEBT_FACTOR
- EXPONENT_SIZE_DEBT_FACTOR
- COEFFICIENT_MAX_DEBT_FACTOR
- EXPONENT_MAX_DEBT_FACTOR
- DECIMALS_DEBT_FACTOR
- MAX_MASK_DEBT_FACTOR
- PRECISION
- TWO_POWER_64
- TWO_POWER_69_MINUS_1
- COEFFICIENT_PLUS_PRECISION
- COEFFICIENT_PLUS_PRECISION_MINUS_1
- TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1
- TWO_POWER_COEFFICIENT_PLUS_PRECISION_MINUS_1_MINUS_1
- Functions