uint256 source pure
arithmetic provides arithmetic operations for Uint objects. This includes basic binary operations such as addition, s...
arithmetic provides arithmetic operations for Uint objects. This includes basic binary operations such as addition, subtraction, multiplication, division, and modulo operations as well as overflow checks, and negation. These functions are essential for numeric calculations using 256-bit unsigned integers.
bitwise contains bitwise operations for Uint instances. This file includes functions to perform bitwise AND, OR, XOR, and NOT operations, as well as bit shifting. These operations are crucial for manipulating individual bits within a 256-bit unsigned integer.
cmp (or, comparisons) includes methods for comparing Uint instances. These comparison functions cover a range of operations including equality checks, less than/greater than evaluations, and specialized comparisons such as signed greater than. These are fundamental for logical decision making based on Uint values.
conversions contains methods for converting Uint instances to other types and vice versa. This includes conversions to and from basic types such as uint64 and int32, as well as string representations and byte slices. Additionally, it covers marshaling and unmarshaling for JSON and other text formats.
Package uint256 implements 256-bit unsigned integer arithmetic.
This package provides a Uint type that represents a 256-bit unsigned integer, stored as four uint64 values in little-endian order. It includes arithmetic operations with overflow detection, which are essential for safe token calculations in DeFi protocols.
The implementation is optimized for gas efficiency while maintaining compatibility with Ethereum's uint256 semantics, ensuring consistent behavior across different blockchain environments.
All operations that may overflow return both the result and an overflow flag, allowing calling code to handle overflow conditions appropriately.
fullmath implements Uniswap V3's FullMath library.
This library provides advanced fixed-point math operations that are essential for Uniswap V3's tick math and liquidity calculations. It enables precise calculations of (a * b / denominator) with full 512-bit intermediate precision.
NOTE: Unlike other arithmetic functions in the uint256 package that return errors, functions in this file panic on invalid inputs to maintain behavioral compatibility with the original Solidity implementation which uses require() statements.
This design choice is intentional because: 1. These functions are typically used in hot paths where error handling would add overhead 2. Invalid inputs (like zero denominator) represent programming errors, not runtime conditions 3. Staying close to the Solidity implementation makes protocol porting more reliable
If you need error-returning versions, wrap these functions with appropriate error handling.
1
2
10
func Reciprocal
Reciprocal computes a 320-bit value representing 1/m
Notes: - specialized for m[3] != 0, hence limited to 2^192 <= m < 2^256 - returns zero if m[3] == 0 - starts with a 32-bit division, refines with newton-raphson iterations
func DivRoundingUp
DivRoundingUp calculates ceil(x / y) and returns the result. Panics if y is zero.
func FromDecimal
FromDecimal creates a new Uint from a decimal string. Returns an error if the number exceeds 256 bits or is invalid.
func MulDiv
MulDiv calculates (a * b) / denominator with full 512-bit intermediate precision. Panics if denominator is zero or if the result overflows 256 bits.
func MulDivRoundingUp
MulDivRoundingUp calculates ceil((a * b) / denominator) with full 512-bit intermediate precision. Panics if denominator is zero or if the result overflows 256 bits.
func MustFromDecimal
MustFromDecimal creates a new Uint from a decimal string. Panics if the string is invalid or the number exceeds 256 bits.
func NewUint
NewUint returns a new Uint initialized with the given uint64 value.
func NewUintFromInt64
NewUintFromInt64 returns a new Uint initialized with the given int64 value. Panics if val is negative.
func One
One returns a new Uint with value 1.
func Zero
Zero returns a new Uint with value 0.
1
type Uint
arrayUint represents a 256-bit unsigned integer. It is stored as an array of 4 uint64 in little-endian order, where arr[0] is the least significant and arr[3] is the most significant.
Methods on Uint
func Add
method on UintAdd sets z to the sum x+y and returns z.
func AddOverflow
method on UintAddOverflow sets z to the sum x+y and returns z and true if overflow occurred.
func And
method on UintAnd sets z to the bitwise AND of x and y and returns z.
func AndNot
method on UintAndNot sets z to x AND NOT y and returns z.
func BitLen
method on UintBitLen returns the number of bits required to represent z. BitLen(0) returns 0.
func Byte
method on UintByte returns the value of the byte at position n as a Uint. Position n is counted from the right (0 = least significant byte). Returns 0 if n >= 32.
func ByteLen
method on UintByteLen returns the number of bytes required to represent z. ByteLen(0) returns 0.
func Clear
method on UintClear sets z to 0 and returns z.
func Clone
method on UintClone returns a new Uint with the same value as z.
func Cmp
method on UintCmp compares z and x and returns -1 if z < x, 0 if z == x, or +1 if z > x.
func Dec
method on UintDec returns the decimal representation of z.
func Div
method on UintDiv sets z to the quotient x/y and returns z. It panics if y == 0.
func DivMod
method on UintDivMod sets z to the quotient x/y and m to the modulus x%y, returning the pair (z, m). It panics if y == 0.
func Eq
method on UintEq returns true if z equals x.
func Gt
method on UintGt returns true if z is greater than x.
func Gte
method on UintGte returns true if z is greater than or equal to x.
func Int64
method on UintInt64 returns the lower 64 bits of z as an int64.
func IsOverflow
method on Uintfunc IsUint64
method on UintIsUint64 reports whether z can be represented as a uint64.
func IsZero
method on UintIsZero returns true if z equals 0.
func Lsh
method on UintLsh sets z to x left-shifted by n bits and returns z. The result is truncated to 256 bits. If n >= 256, z is set to 0.
func Lt
method on UintLt returns true if z is less than x.
func Lte
method on UintLte returns true if z is less than or equal to x.
func Mod
method on UintMod sets z to the modulus x%y and returns z. It panics if y == 0.
func Mul
method on UintMul sets z to the product x*y and returns z.
func MulMod
method on UintMulMod sets z to (x * y) mod m and returns z. It panics if m == 0.
func MulOverflow
method on UintMulOverflow sets z to the product x*y and returns z and true if overflow occurred.
func Neg
method on UintNeg returns -x mod 2^256.
func Neq
method on UintNeq returns true if z does not equal x.
func Not
method on UintNot sets z to the bitwise NOT of x and returns z.
func Or
method on UintOr sets z to the bitwise OR of x and y and returns z.
func Rsh
method on UintRsh sets z to x right-shifted by n bits and returns z. If n >= 256, z is set to 0.
func SRsh
method on UintSRsh sets z to x arithmetically right-shifted by n bits and returns z. It treats x as a signed two's complement integer. For negative values, high-order bits are filled with 1s instead of 0s. If n >= 256 and x is negative, z is set to all 1s.
func Set
method on UintSet sets z to x and returns z.
func SetAllOne
method on UintSetAllOne sets z to the maximum 256-bit value (all bits set to 1) and returns z.
func SetBytes
method on UintSetBytes interprets buf as a big-endian unsigned integer and sets z to that value. If buf is larger than 32 bytes, uses only the last 32 bytes. Returns z.
func SetBytes1
method on UintSetBytes1 sets z from a 1-byte big-endian slice and returns z. Panics if input is shorter than 1 byte.
func SetBytes10
method on UintSetBytes10 sets z from a 10-byte big-endian slice and returns z. Panics if input is shorter than 10 bytes.
func SetBytes11
method on UintSetBytes11 sets z from an 11-byte big-endian slice and returns z. Panics if input is shorter than 11 bytes.
func SetBytes12
method on UintSetBytes12 sets z from a 12-byte big-endian slice and returns z. Panics if input is shorter than 12 bytes.
func SetBytes13
method on UintSetBytes13 sets z from a 13-byte big-endian slice and returns z. Panics if input is shorter than 13 bytes.
func SetBytes14
method on UintSetBytes14 sets z from a 14-byte big-endian slice and returns z. Panics if input is shorter than 14 bytes.
func SetBytes15
method on UintSetBytes15 sets z from a 15-byte big-endian slice and returns z. Panics if input is shorter than 15 bytes.
func SetBytes16
method on UintSetBytes16 sets z from a 16-byte big-endian slice and returns z. Panics if input is shorter than 16 bytes.
func SetBytes17
method on UintSetBytes17 sets z from a 17-byte big-endian slice and returns z. Panics if input is shorter than 17 bytes.
func SetBytes18
method on UintSetBytes18 sets z from an 18-byte big-endian slice and returns z. Panics if input is shorter than 18 bytes.
func SetBytes19
method on UintSetBytes19 sets z from a 19-byte big-endian slice and returns z. Panics if input is shorter than 19 bytes.
func SetBytes2
method on UintSetBytes2 sets z from a 2-byte big-endian slice and returns z. Panics if input is shorter than 2 bytes.
func SetBytes20
method on UintSetBytes20 sets z from a 20-byte big-endian slice and returns z. Panics if input is shorter than 20 bytes.
func SetBytes21
method on UintSetBytes21 sets z from a 21-byte big-endian slice and returns z. Panics if input is shorter than 21 bytes.
func SetBytes22
method on UintSetBytes22 sets z from a 22-byte big-endian slice and returns z. Panics if input is shorter than 22 bytes.
func SetBytes23
method on UintSetBytes23 sets z from a 23-byte big-endian slice and returns z. Panics if input is shorter than 23 bytes.
func SetBytes24
method on UintSetBytes24 sets z from a 24-byte big-endian slice and returns z. Panics if input is shorter than 24 bytes.
func SetBytes25
method on UintSetBytes25 sets z from a 25-byte big-endian slice and returns z. Panics if input is shorter than 25 bytes.
func SetBytes26
method on UintSetBytes26 sets z from a 26-byte big-endian slice and returns z. Panics if input is shorter than 26 bytes.
func SetBytes27
method on UintSetBytes27 sets z from a 27-byte big-endian slice and returns z. Panics if input is shorter than 27 bytes.
func SetBytes28
method on UintSetBytes28 sets z from a 28-byte big-endian slice and returns z. Panics if input is shorter than 28 bytes.
func SetBytes29
method on UintSetBytes29 sets z from a 29-byte big-endian slice and returns z. Panics if input is shorter than 29 bytes.
func SetBytes3
method on UintSetBytes3 sets z from a 3-byte big-endian slice and returns z. Panics if input is shorter than 3 bytes.
func SetBytes30
method on UintSetBytes30 sets z from a 30-byte big-endian slice and returns z. Panics if input is shorter than 30 bytes.
func SetBytes31
method on UintSetBytes31 sets z from a 31-byte big-endian slice and returns z. Panics if input is shorter than 31 bytes.
func SetBytes32
method on UintSetBytes32 sets z from a 32-byte big-endian slice and returns z. Panics if input is shorter than 32 bytes.
func SetBytes4
method on UintSetBytes4 sets z from a 4-byte big-endian slice and returns z. Panics if input is shorter than 4 bytes.
func SetBytes5
method on UintSetBytes5 sets z from a 5-byte big-endian slice and returns z. Panics if input is shorter than 5 bytes.
func SetBytes6
method on UintSetBytes6 sets z from a 6-byte big-endian slice and returns z. Panics if input is shorter than 6 bytes.
func SetBytes7
method on UintSetBytes7 sets z from a 7-byte big-endian slice and returns z. Panics if input is shorter than 7 bytes.
func SetBytes8
method on UintSetBytes8 sets z from an 8-byte big-endian slice and returns z. Panics if input is shorter than 8 bytes.
func SetBytes9
method on UintSetBytes9 sets z from a 9-byte big-endian slice and returns z. Panics if input is shorter than 9 bytes.
func SetFromDecimal
method on UintSetFromDecimal sets z from a decimal string and returns an error if invalid. Accepts an optional leading "+" sign but rejects underscores and negative values. Returns ErrBig256Range if the number exceeds 256 bits.
func SetOne
method on UintSetOne sets z to 1 and returns z.
func SetUint64
method on UintSetUint64 sets z to the value of x and returns z.
func Sign
method on UintSign returns the sign of z interpreted as a two's complement signed number. It returns -1 if z < 0, 0 if z == 0, or +1 if z > 0.
func Sub
method on UintSub sets z to the difference x-y and returns z.
func SubOverflow
method on UintSubOverflow sets z to the difference x-y and returns z and true if underflow occurred.
func ToString
method on UintToString returns the decimal string representation of z. Returns an empty string if z is nil. This method doesn't exist in holiman's uint256.
func Uint64
method on UintUint64 returns the lower 64 bits of z as a uint64.
func Uint64WithOverflow
method on UintUint64WithOverflow returns the lower 64 bits of z and true if overflow occurred.
func Xor
method on UintXor sets z to the bitwise XOR of x and y and returns z.
4
- encoding/binary stdlib
- errors stdlib
- math/bits stdlib
- strconv stdlib