Skip to content

Commit

Permalink
update rpow
Browse files Browse the repository at this point in the history
Make func internal for test reasons
  • Loading branch information
aritkulova committed Oct 23, 2023
1 parent 68a882d commit 94a46f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
55 changes: 31 additions & 24 deletions contracts/compound-rate-keeper/AbstractCompoundRateKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
_changeCapitalizationPeriod(capitalizationPeriod_);
}

/**
* @notice Implementation of exponentiation by squaring
* @param x_ basis, decimal number
* @param n_ power of x
* @param b_ precision of x
* @return z_ result
*/
function _rpow(uint256 x_, uint256 n_, uint256 b_) internal pure returns (uint256 z_) {
if (x_ == 0) {
if (n_ == 0) {
z_ = b_;
} else {
z_ = 0;
}
} else {
if (n_ % 2 == 0) {
z_ = b_;
} else {
z_ = x_;
}
uint256 half = b_ / 2; // for rounding

for (uint256 i = n_ / 2; i >= 1; i = i / 2) {
x_ = (x_ * x_ + half) / b_;
if (i % 2 == 1) {
z_ = (z_ * x_ + half) / b_;
}
}
}
}

/**
* @notice The private function to update the compound rate
*/
Expand Down Expand Up @@ -207,28 +238,4 @@ abstract contract AbstractCompoundRateKeeper is ICompoundRateKeeper, Initializab
function _getMaxRate() private pure returns (uint256) {
return type(uint128).max * PRECISION;
}

function _rpow(uint256 x_, uint256 n_, uint256 b_) private pure returns (uint256 z_) {
if (x_ == 0) {
if (n_ == 0) {
z_ = b_;
} else {
z_ = 0;
}
} else {
if (n_ % 2 == 0) {
z_ = b_;
} else {
z_ = x_;
}
uint256 half = b_ / 2; // for rounding

for (uint256 i = n_ / 2; i >= 1; i = i / 2) {
x_ = (x_ * x_ + half) / b_;
if (i % 2 == 1) {
z_ = (z_ * x_ + half) / b_;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ contract CompoundRateKeeperMock is OwnableCompoundRateKeeper {
_setCapitalizationRate(capitalizationRate_);
_setCapitalizationPeriod(capitalizationPeriod_);
}

function rpow(uint256 x_, uint256 n_, uint256 b_) external pure returns (uint256 z_) {
return _rpow(x_, n_, b_);
}
}
5 changes: 5 additions & 0 deletions test/compound-rate-keeper/CompoundRateKeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,10 @@ describe("CompoundRateKeeper", () => {
it("check max timestamp for 1000%", async () => {
await checkByParams(11);
});

it("check correct work of rpow function", async () => {
expect(await keeper.rpow(0, 0, 5)).to.equal(5);
expect(await keeper.rpow(0, 1, 5)).to.equal(0);
});
});
});

0 comments on commit 94a46f6

Please sign in to comment.