Skip to content

Commit

Permalink
add Uint128x2 to bytes32 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Apr 4, 2024
1 parent 2025086 commit e7f9dbd
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions contracts/utils/Packing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ pragma solidity ^0.8.20;
library Packing {
type Uint128x2 is bytes32;

function asUint128x2(bytes32 packed) internal pure returns (Uint128x2) {
return Uint128x2.wrap(packed);
function asUint128x2(bytes32 self) internal pure returns (Uint128x2) {
return Uint128x2.wrap(self);
}

function asBytes32(Uint128x2 self) internal pure returns (bytes32) {
return Uint128x2.unwrap(self);
}

function pack(uint128 high128, uint128 low128) internal pure returns (Uint128x2) {
return Uint128x2.wrap(bytes32(bytes16(high128)) | bytes32(uint256(low128)));
}

function split(Uint128x2 packed) internal pure returns (uint128, uint128) {
return (high(packed), low(packed));
function split(Uint128x2 self) internal pure returns (uint128, uint128) {
return (high(self), low(self));
}

function high(Uint128x2 packed) internal pure returns (uint128) {
return uint128(bytes16(Uint128x2.unwrap(packed)));
function high(Uint128x2 self) internal pure returns (uint128) {
return uint128(bytes16(Uint128x2.unwrap(self)));
}

function low(Uint128x2 packed) internal pure returns (uint128) {
return uint128(uint256(Uint128x2.unwrap(packed)));
function low(Uint128x2 self) internal pure returns (uint128) {
return uint128(uint256(Uint128x2.unwrap(self)));
}
}

0 comments on commit e7f9dbd

Please sign in to comment.