PackedUint128Math

Git Sourcearrow-up-right

This library contains functions to encode and decode two uint128 into a single bytes32 and interact with the encoded bytes32.

State Variables

OFFSET

uint256 private constant OFFSET = 128;

MASK_128

uint256 private constant MASK_128 = 0xffffffffffffffffffffffffffffffff;

MASK_128_PLUS_ONE

uint256 private constant MASK_128_PLUS_ONE = MASK_128 + 1;

Functions

encode

Encodes two uint128 into a single bytes32

Parameters

Name
Type
Description

x1

uint128

The first uint128

x2

uint128

The second uint128

Returns

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: x2

encodeFirst

Encodes a uint128 into a single bytes32 as the first uint128

Parameters

Name
Type
Description

x1

uint128

The uint128

Returns

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: empty

encodeSecond

Encodes a uint128 into a single bytes32 as the second uint128

Parameters

Name
Type
Description

x2

uint128

The uint128

Returns

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: empty [128 - 256[: x2

encode

Encodes a uint128 into a single bytes32 as the first or second uint128

Parameters

Name
Type
Description

x

uint128

The uint128

first

bool

Whether to encode as the first or second uint128

Returns

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: if first: [0 - 128[: x [128 - 256[: empty else: [0 - 128[: empty [128 - 256[: x

decode

Decodes a bytes32 into two uint128

Parameters

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: x1 [128 - 256[: x2

Returns

Name
Type
Description

x1

uint128

The first uint128

x2

uint128

The second uint128

decodeX

Decodes a bytes32 into a uint128 as the first uint128

Parameters

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: x [128 - 256[: any

Returns

Name
Type
Description

x

uint128

The first uint128

decodeY

Decodes a bytes32 into a uint128 as the second uint128

Parameters

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: [0 - 128[: any [128 - 256[: y

Returns

Name
Type
Description

y

uint128

The second uint128

decode

Decodes a bytes32 into a uint128 as the first or second uint128

Parameters

Name
Type
Description

z

bytes32

The encoded bytes32 as follows: if first: [0 - 128[: x1 [128 - 256[: empty else: [0 - 128[: empty [128 - 256[: x2

first

bool

Whether to decode as the first or second uint128

Returns

Name
Type
Description

x

uint128

The decoded uint128

add

Adds two encoded bytes32, reverting on overflow on any of the uint128

Parameters

Name
Type
Description

x

bytes32

The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y

bytes32

The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2

Returns

Name
Type
Description

z

bytes32

The sum of x and y encoded as follows: [0 - 128[: x1 + y1 [128 - 256[: x2 + y2

add

Adds an encoded bytes32 and two uint128, reverting on overflow on any of the uint128

Parameters

Name
Type
Description

x

bytes32

The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y1

uint128

The first uint128

y2

uint128

The second uint128

Returns

Name
Type
Description

<none>

bytes32

z The sum of x and y encoded as follows: [0 - 128[: x1 + y1 [128 - 256[: x2 + y2

sub

Subtracts two encoded bytes32, reverting on underflow on any of the uint128

Parameters

Name
Type
Description

x

bytes32

The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y

bytes32

The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2

Returns

Name
Type
Description

z

bytes32

The difference of x and y encoded as follows: [0 - 128[: x1 - y1 [128 - 256[: x2 - y2

sub

Subtracts an encoded bytes32 and two uint128, reverting on underflow on any of the uint128

Parameters

Name
Type
Description

x

bytes32

The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y1

uint128

The first uint128

y2

uint128

The second uint128

Returns

Name
Type
Description

<none>

bytes32

z The difference of x and y encoded as follows: [0 - 128[: x1 - y1 [128 - 256[: x2 - y2

lt

Returns whether any of the uint128 of x is strictly greater than the corresponding uint128 of y

Parameters

Name
Type
Description

x

bytes32

The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y

bytes32

The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2

Returns

Name
Type
Description

<none>

bool

x1 < y1

gt

Returns whether any of the uint128 of x is strictly greater than the corresponding uint128 of y

Parameters

Name
Type
Description

x

bytes32

The first bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

y

bytes32

The second bytes32 encoded as follows: [0 - 128[: y1 [128 - 256[: y2

Returns

Name
Type
Description

<none>

bool

x1 < y1

scalarMulDivBasisPointRoundDown

Multiplies an encoded bytes32 by a uint128 then divides the result by 10_000, rounding down The result can't overflow as the multiplier needs to be smaller or equal to 10_000

Parameters

Name
Type
Description

x

bytes32

The bytes32 encoded as follows: [0 - 128[: x1 [128 - 256[: x2

multiplier

uint128

The uint128 to multiply by (must be smaller or equal to 10_000)

Returns

Name
Type
Description

z

bytes32

The product of x and multiplier encoded as follows: [0 - 128[: floor((x1 * multiplier) / 10_000) [128 - 256[: floor((x2 * multiplier) / 10_000)

Errors

PackedUint128Math__AddOverflow

PackedUint128Math__SubUnderflow

PackedUint128Math__MultiplierTooLarge

Last updated