Skip to content

Commit

Permalink
👯 Improve uint128x2 and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Apr 11, 2024
1 parent c73e32b commit 1cc38e4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion contracts/IProtocolFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

import {PROTOCOL_FUND} from "./Addresses.sol";
import {PROTOCOL_FUND} from "./addresses.sol";
import {amountAddr} from "./amountAddr.sol";

interface IProtocolFund {
Expand Down
18 changes: 11 additions & 7 deletions contracts/uint128x2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pragma solidity ^0.8.20;

type uint128x2 is uint256;

function uint128x2From(uint256 hi, uint256 lo) pure returns (uint128x2) {
return uint128x2.wrap(hi << 128 | lo);
function uint128x2From(uint256 high, uint256 low) pure returns (uint128x2) {
return uint128x2.wrap(high << 128 | low);
}

function hi128(uint128x2 n) pure returns (uint256) {
function hi(uint128x2 n) pure returns (uint256) {
return uint128x2.unwrap(n) >> 128;
}

function lo128(uint128x2 n) pure returns (uint256) {
function lo(uint128x2 n) pure returns (uint256) {
return uint128(uint128x2.unwrap(n));
}

Expand All @@ -24,14 +24,18 @@ function decLo(uint128x2 self, uint256 delta) pure returns (uint128x2) {

function inc(uint128x2 self, uint256 delta) pure returns (uint128x2) {
unchecked {
return uint128x2.wrap(uint128x2.unwrap(self) + (delta | delta << 128));
return uint128x2.wrap(uint128x2.unwrap(self) + (delta | (delta << 128)));
}
}

function dec(uint128x2 self, uint256 delta) pure returns (uint128x2) {
unchecked {
return uint128x2.wrap(uint128x2.unwrap(self) - (delta | delta << 128));
return uint128x2.wrap(uint128x2.unwrap(self) - (delta | (delta << 128)));
}
}

using {hi128, lo128, decLo, inc, dec} for uint128x2 global;
function equal(uint128x2 self, uint128x2 other) pure returns (bool) {
return uint128x2.unwrap(self) == uint128x2.unwrap(other);
}

using {hi, lo, decLo, inc, dec, equal as ==} for uint128x2 global;
32 changes: 16 additions & 16 deletions test/addresses.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

pragma solidity ^0.8.0;

import {applyL1ToL2Alias, computeCreateAddress as computeZkSyncCreateAddress} from "contracts/IZkSync.sol";
import {
PROTOCOL_FUND,
PROTOCOL_FUND_DEPLOYER,
PROTOCOL_FUND_ZKSYNC,
PROTOCOL_FUND_ZKSYNC_DEPLOYER,
KPASS_DEPLOYER,
KPASS,
KPASS_ZKSYNC,
KPASS_ZKSYNC_DEPLOYER,
KDAOL,
KDAOL_DEPLOYER,
KDAO_MAINNET,
KDAO_MAINNET_DEPLOYER,
KDAO_ZKSYNC,
KDAO_ZKSYNC_DEPLOYER,
KDAO_ZKSYNC_ALIAS,
KDAOL,
KDAOL_DEPLOYER
} from "contracts/Addresses.sol";
KDAO_ZKSYNC_DEPLOYER,
KPASS,
KPASS_DEPLOYER,
KPASS_ZKSYNC,
KPASS_ZKSYNC_DEPLOYER,
PROTOCOL_FUND,
PROTOCOL_FUND_DEPLOYER,
PROTOCOL_FUND_ZKSYNC,
PROTOCOL_FUND_ZKSYNC_DEPLOYER
} from "contracts/addresses.sol";
import {Test} from "forge-std/Test.sol";
import {computeCreateAddress as computeZkSyncCreateAddress, applyL1ToL2Alias} from "contracts/IZkSync.sol";

contract AddressesTest is Test {
function testDeployerConsistency() public pure {
assertEq(computeCreateAddress(PROTOCOL_FUND_DEPLOYER, 0), PROTOCOL_FUND);
assertEq(vm.computeCreateAddress(PROTOCOL_FUND_DEPLOYER, 0), PROTOCOL_FUND);
assertEq(computeZkSyncCreateAddress(PROTOCOL_FUND_ZKSYNC_DEPLOYER, 0), PROTOCOL_FUND_ZKSYNC);

assertEq(computeCreateAddress(KPASS_DEPLOYER, 0), KPASS);
assertEq(vm.computeCreateAddress(KPASS_DEPLOYER, 0), KPASS);
assertEq(computeZkSyncCreateAddress(KPASS_ZKSYNC_DEPLOYER, 0), KPASS_ZKSYNC);

assertEq(computeCreateAddress(KDAO_MAINNET_DEPLOYER, 0), KDAO_MAINNET);
assertEq(vm.computeCreateAddress(KDAO_MAINNET_DEPLOYER, 0), KDAO_MAINNET);
assertEq(computeZkSyncCreateAddress(KDAO_ZKSYNC_DEPLOYER, 0), KDAO_ZKSYNC);
assertEq(applyL1ToL2Alias(KDAO_MAINNET), KDAO_ZKSYNC_ALIAS);

Expand Down
27 changes: 23 additions & 4 deletions test/uint128x2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@

pragma solidity ^0.8.0;

import {Test} from "forge-std/Test.sol";
import {uint128x2, uint128x2From} from "contracts/uint128x2.sol";
import {Test} from "forge-std/Test.sol";

contract integersTest is Test {
contract uint128x2Test is Test {
function testAccessors() public pure {
uint128x2 pair = uint128x2From(1, 2);

assertEq(pair.lo128(), 2);
assertEq(pair.hi128(), 1);
assertEq(pair.lo(), 2);
assertEq(pair.hi(), 1);
}

function testIncDec() public pure {
uint128x2 pair = uint128x2From(300, 500);
pair = pair.inc(200);

assertEq(pair.hi(), 500);
assertEq(pair.lo(), 700);

pair = pair.dec(200);

assertEq(pair.hi(), 300);
assertEq(pair.lo(), 500);

uint128x2 pair2 = uint128x2From(type(uint128).max, 10);
uint128x2 pair3 = pair2.dec(10);
pair3 = pair3.inc(10);

assert(pair2 == pair3);
}
}

0 comments on commit 1cc38e4

Please sign in to comment.