Skip to content

Commit

Permalink
try a fp fuzz test (Uniswap#17)
Browse files Browse the repository at this point in the history
* try a fp fuzz test

* fix compiler error

* tiny gas savings

* remove fp test

* remove fixedpoint test
  • Loading branch information
moodysalem committed Oct 22, 2020
1 parent f0bd425 commit 43287bd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fuzz-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
- name: Run echidna
run: |
echidna-test . --contract EchidnaBabylonianTest
echidna-test . --contract BabylonianEchidnaTest
6 changes: 3 additions & 3 deletions contracts/libraries/FixedPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ library FixedPoint {
}

uint8 private constant RESOLUTION = 112;
uint256 private constant Q112 = uint256(1) << RESOLUTION;
uint256 private constant Q224 = Q112 << RESOLUTION;
uint256 private constant Q112 = 0x10000000000000000000000000000;
uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000;
uint256 private constant LOWER_MASK = 0xffffffffffffffffffffffffffff; // decimal of UQ*x112 (lower 112 bits)

// encode a uint112 as a UQ112x112
Expand Down Expand Up @@ -55,7 +55,7 @@ library FixedPoint {
// reverts on overflow
function muli(uq112x112 memory self, int256 y) internal pure returns (int256) {
uint256 z = FullMath.mulDiv(self._x, uint256(y < 0 ? -y : y), Q112);
require(z < 2**255, "FixedPoint: MULI_OVERFLOW");
require(z < 2**255, 'FixedPoint: MULI_OVERFLOW');
return y < 0 ? -int256(z) : int256(z);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ pragma solidity >=0.4.0;

import '../libraries/Babylonian.sol';

contract EchidnaBabylonianTest {
uint256 num;
contract BabylonianEchidnaTest {
uint256 input;
uint256 sqrt;

function storeSqrt(uint256 num_) external {
num = num_;
sqrt = Babylonian.sqrt(num_);
function storeSqrt(uint256 input_) external {
input = input_;
sqrt = Babylonian.sqrt(input_);
}

function echidna_sqrtAlwaysLessThanMaxUint128() external view returns (bool) {
return sqrt < 2**128; // because (2**128)^2 > uint256(-1)
}

function echidna_sqrtSquaredAlwaysLteInput() external view returns (bool) {
return sqrt * sqrt <= num;
return sqrt * sqrt <= input;
}

function echidna_sqrtPlusOneSquaredAlwaysGtInput() external view returns (bool) {
uint256 next = sqrt + 1;
uint256 nextSquared = next * next;
return (nextSquared > num); /*|| ((nextSquared) / next != next)*/
return (nextSquared > input); /*|| ((nextSquared) / next != next)*/
}
}
4 changes: 2 additions & 2 deletions test/FixedPoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ describe('FixedPoint', () => {
// long division but makes fewer iterations
expect(
await fixedPoint.getGasCostOfDivuq([BigNumber.from(10).pow(10).mul(Q112)], [BigNumber.from(25).mul(Q112)])
).to.eq(1443)
).to.eq(1440)
})

it('gas cost of long division with all iterations', async () => {
Expand All @@ -301,7 +301,7 @@ describe('FixedPoint', () => {
[BigNumber.from(10).pow(10).mul(Q112)],
[BigNumber.from(3).mul(BigNumber.from(10).pow(10)).mul(Q112)]
)
).to.eq(1443)
).to.eq(1440)
})
})

Expand Down

0 comments on commit 43287bd

Please sign in to comment.