v1 source pure
arithmetic provides arithmetic operations for Uint objects. This includes basic binary operations such as addition, s...
View source
uint256
256-bit unsigned integer arithmetic for GnoSwap.
Overview
Fixed-size 256-bit unsigned integer library optimized for AMM calculations with
precise MulDiv operations. The unsuffixed Add, Sub, and Mul methods
return the low 256 bits; the corresponding AddOverflow, SubOverflow, and
MulOverflow variants also report an overflow/underflow flag.
Features
- Fixed 256-bit size (4 uint64 values)
- Explicit overflow detection via
*Overflowvariants - Optimized
MulDivfor precise calculations - Decimal string conversion
- Range: 0 to 2^256-1
Usage
1package main
2
3import u256 "gno.land/p/gnoswap/uint256/v1"
4
5func main() {
6 a := u256.NewUint(1000)
7 b := u256.MustFromDecimal("1000000000000000000")
8 result, overflow := new(u256.Uint).AddOverflow(a, b)
9 if overflow {
10 panic("unsigned addition overflow")
11 }
12 println(result.ToString()) // 1000000000000001000
13
14 // MulDiv calculates floor(a*b/c). The denominator must be non-zero,
15 // and the quotient must fit in 256 bits; otherwise it panics.
16 c := u256.NewUint(3)
17 println(u256.MulDiv(a, b, c).ToString()) // 333333333333333333333
18}
Credits
Ported from holiman/uint256
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 for GnoSwap.
This package provides a Uint type that represents a 256-bit unsigned integer, stored as four uint64 values in little-endian order. The unsuffixed Add, Sub, and Mul methods return the low 256 bits; AddOverflow, SubOverflow, and MulOverflow additionally report an overflow/underflow flag. MulDiv and MulDivRoundingUp panic on a zero denominator or an unrepresentable result; the rounding variant also panics if adding one to its result would overflow.
The implementation is optimized for gas efficiency while maintaining compatibility with Ethereum's uint256 semantics, ensuring consistent behavior across different blockchain environments.
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 the base arithmetic methods in the uint256 package, which return low-256-bit values (or values plus overflow flags in their `*Overflow` variants), functions in this file panic on invalid inputs to maintain behavioral compatibility with the original Solidity implementation.
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
11
func Reciprocal
Reciprocal computes the 320-bit reciprocal estimate used by Barrett reduction.
The implementation is specialized for a four-word modulus with m[3] non-zero (2^192 <= m < 2^256). For a modulus whose most-significant word is zero it returns an all-zero estimate instead of running the refinement steps.
Parameters:
- m: Four-word unsigned modulus; its most-significant word selects the supported range.
Returns:
- mu: Five little-endian uint64 words containing the reciprocal estimate for m.
func DivRoundingUp
DivRoundingUp computes ceil(x / y) for unsigned 256-bit operands.
Parameters:
- x: Non-negative 256-bit dividend.
- y: Non-zero 256-bit divisor.
Returns:
- quotient: The quotient rounded toward positive infinity.
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.
Parameters:
- decimal: the decimal text representing a non-negative value within 256 bits
Returns:
- value: a new parsed Uint, or nil on error
- err: nil on success; otherwise an invalid-format or 256-bit-range error
func MaxUint256
MaxUint256 returns the maximum 256-bit unsigned integer (2^256-1).
Returns:
- result: a new Uint containing 2^256-1
func MulDiv
MulDiv computes floor((a * b) / denominator) with a full 512-bit intermediate product.
Parameters:
- a: First non-negative 256-bit multiplicand.
- b: Second non-negative 256-bit multiplicand.
- denominator: Non-zero 256-bit divisor.
Returns:
- quotient: The exact floor quotient when it fits in 256 bits.
Panics if denominator is zero or the quotient is at least 2^256.
func MulDivRoundingUp
MulDivRoundingUp computes ceil((a * b) / denominator) with a full 512-bit intermediate product.
Parameters:
- a: First non-negative 256-bit multiplicand.
- b: Second non-negative 256-bit multiplicand.
- denominator: Non-zero 256-bit divisor.
Returns:
- quotient: The ceiling quotient; it is one greater than the floor quotient exactly when the product has a non-zero remainder.
Panics if denominator is zero or rounding the result exceeds 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.
Parameters:
- decimal: the decimal text representing a non-negative value within 256 bits
Returns:
- result: a new parsed Uint; invalid or out-of-range input panics
func NewUint
NewUint returns a new Uint initialized with the given uint64 value.
Parameters:
- val: the initial uint64 value
Returns:
- result: a new Uint initialized to val
func NewUintFromInt64
NewUintFromInt64 returns a new Uint initialized with the given int64 value. Panics if val is negative.
Parameters:
- val: the initial non-negative int64 value; a negative value panics
Returns:
- result: a new Uint initialized to val
func One
One returns a new Uint with value 1.
Returns:
- result: a new Uint containing one
func Zero
Zero returns a new Uint with value 0.
Returns:
- result: a new zero-valued Uint
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.
Parameters:
- x: the first addend; its 256-bit value is added modulo 2^256
- y: the second addend; its 256-bit value is added modulo 2^256
Returns:
- result: z containing x+y modulo 2^256; any carry beyond bit 255 is discarded
func AddOverflow
method on UintAddOverflow sets z to the sum x+y and returns z and true if overflow occurred.
Parameters:
- x: the first addend
- y: the second addend
Returns:
- result: z containing x+y modulo 2^256
- overflow: true when x+y exceeds the 256-bit range
func And
method on UintAnd sets z to the bitwise AND of x and y and returns z.
Parameters:
- x: First 256-bit operand.
- y: Second 256-bit operand.
Returns:
- z: The 256-bit bitwise AND x & y, stored in the receiver.
func AndNot
method on UintAndNot sets z to x AND NOT y and returns z.
Parameters:
- x: First 256-bit operand.
- y: Operand whose bits are cleared from x.
Returns:
- z: The 256-bit bit pattern x &^ y, stored in the receiver.
func BitLen
method on UintBitLen returns the number of bits required to represent z. BitLen(0) returns 0.
Returns:
- bits: the number of significant bits in z, with zero represented by 0
func Byte
method on UintByte returns the value of the byte at position n as a Uint. Position n is counted from the left (0 = most significant byte). Returns 0 if n >= 32.
Parameters:
- n: the zero-based byte position counted from the most-significant byte; positions 32 and above yield zero
Returns:
- result: z containing the selected byte as a Uint, or zero when n is outside the 32-byte value
func ByteLen
method on UintByteLen returns the number of bytes required to represent z. ByteLen(0) returns 0.
Returns:
- bytes: the number of bytes needed to represent z, with zero represented by 0
func Clear
method on UintClear sets z to 0 and returns z.
Returns:
- result: z after setting it to zero
func Clone
method on UintClone returns a new Uint with the same value as z.
Returns:
- result: a newly allocated 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.
Parameters:
- x: the Uint value compared with z
Returns:
- r: -1 when z<x, 0 when z==x, or +1 when z>x
func Dec
method on UintDec returns the decimal representation of z.
Returns:
- decimal: the base-10 representation of z
func Div
method on UintDiv sets z to the quotient x/y and returns z. It panics if y == 0.
Parameters:
- x: the dividend
- y: the non-zero divisor; zero causes a division-by-zero panic
Returns:
- result: z containing the unsigned quotient x/y
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.
Parameters:
- x: the dividend
- y: the non-zero divisor; zero causes a division-by-zero panic
- m: the destination overwritten with x modulo y
Returns:
- quotient: z containing x/y
- remainder: m containing x%y
func Eq
method on UintEq returns true if z equals x.
Parameters:
- x: the comparison operand
Returns:
- equal: true when z and x have identical 256-bit values
func Gt
method on UintGt returns true if z is greater than x.
Parameters:
- x: the comparison operand
Returns:
- greater: true when z>x
func Gte
method on UintGte returns true if z is greater than or equal to x.
Parameters:
- x: the comparison operand
Returns:
- greaterOrEqual: true when z>=x
func Int64
method on UintInt64 returns the lower 64 bits of z as an int64.
Returns:
- value: the low 64 bits of z interpreted as int64
func IsOverflow
method on UintIsOverflow reports whether the highest bit of z is set.
Returns:
- overflow: true when z has bit 255 set
func IsUint64
method on UintIsUint64 reports whether z can be represented as a uint64.
Returns:
- fits: true when z is representable in 64 bits
func IsZero
method on UintIsZero returns true if z equals 0.
Returns:
- isZero: true when all four words of z are zero
func Lsh
method on UintLsh sets z to x left-shifted by n bits and returns z. Bits shifted beyond the 256-bit width are discarded; n >= 256 produces zero.
Parameters:
- x: 256-bit operand to shift.
- n: Number of bit positions to shift left.
Returns:
- z: The low 256 bits of x << n, stored in the receiver.
func Lt
method on UintLt returns true if z is less than x.
Parameters:
- x: the comparison operand
Returns:
- less: true when z<x
func Lte
method on UintLte returns true if z is less than or equal to x.
Parameters:
- x: the comparison operand
Returns:
- lessOrEqual: true when z<=x
func Mod
method on UintMod sets z to the modulus x%y and returns z. It panics if y == 0.
Parameters:
- x: the dividend
- y: the non-zero divisor; zero causes a modulo-by-zero panic
Returns:
- result: z containing the unsigned remainder x%y
func Mul
method on UintMul sets z to the product x*y and returns z.
Parameters:
- x: the first multiplicand
- y: the second multiplicand
Returns:
- result: z containing the low 256 bits of x*y
func MulMod
method on UintMulMod sets z to (x * y) mod m and returns z. It panics if m == 0.
Parameters:
- x: the first multiplicand
- y: the second multiplicand
- m: the non-zero modulus; zero causes a modulo-by-zero panic
Returns:
- result: z containing (x*y) modulo m
func MulOverflow
method on UintMulOverflow sets z to the product x*y and returns z and true if overflow occurred.
Parameters:
- x: the first multiplicand
- y: the second multiplicand
Returns:
- result: z containing the low 256 bits of x*y
- overflow: true when the full product needs more than 256 bits
func Neg
method on UintNeg returns -x mod 2^256.
Parameters:
- x: the value whose additive inverse modulo 2^256 is computed
Returns:
- result: z containing -x modulo 2^256
func Neq
method on UintNeq returns true if z does not equal x.
Parameters:
- x: the comparison operand
Returns:
- notEqual: true when z and x differ
func Not
method on UintNot sets z to the bitwise complement of x and returns z.
Parameters:
- x: 256-bit operand whose bits are complemented.
Returns:
- z: The 256-bit bitwise complement ^x, stored in the receiver.
func Or
method on UintOr sets z to the bitwise OR of x and y and returns z.
Parameters:
- x: First 256-bit operand.
- y: Second 256-bit operand.
Returns:
- z: The 256-bit bitwise OR x | y, stored in the receiver.
func Rsh
method on UintRsh sets z to x logically right-shifted by n bits and returns z. Zero bits are shifted in from the left, and n >= 256 produces zero.
Parameters:
- x: 256-bit operand to shift.
- n: Number of bit positions to shift right.
Returns:
- z: The 256-bit logical right shift x >> n, stored in the receiver.
func SRsh
method on UintSRsh sets z to x arithmetically right-shifted by n bits and returns z. The top bit is treated as a sign bit: negative patterns receive one-fill, while non-negative patterns use the logical right shift.
Parameters:
- x: 256-bit two's-complement bit pattern to shift.
- n: Number of bit positions to shift right.
Returns:
- z: The arithmetic right shift of x, stored in the receiver; n >= 256 yields all ones for a negative x and zero otherwise.
func Set
method on UintSet sets z to x and returns z.
Parameters:
- x: the Uint value copied into z
Returns:
- result: z after copying x
func SetAllOne
method on UintSetAllOne sets z to the maximum 256-bit value (all bits set to 1) and returns z.
Returns:
- result: z after setting every bit to one
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.
Parameters:
- buf: the big-endian byte sequence; if longer than 32 bytes, only its final 32 bytes are used
Returns:
- result: z after decoding the selected big-endian bytes
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.
Parameters:
- in: the first byte is decoded in big-endian order; it must be present
Returns:
- result: z after decoding the first byte of in
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.
Parameters:
- in: the byte sequence whose first 10 bytes are decoded in big-endian order; it must contain at least 10 bytes
Returns:
- result: z after decoding the first 10 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 11 bytes are decoded in big-endian order; it must contain at least 11 bytes
Returns:
- result: z after decoding the first 11 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 12 bytes are decoded in big-endian order; it must contain at least 12 bytes
Returns:
- result: z after decoding the first 12 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 13 bytes are decoded in big-endian order; it must contain at least 13 bytes
Returns:
- result: z after decoding the first 13 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 14 bytes are decoded in big-endian order; it must contain at least 14 bytes
Returns:
- result: z after decoding the first 14 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 15 bytes are decoded in big-endian order; it must contain at least 15 bytes
Returns:
- result: z after decoding the first 15 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 16 bytes are decoded in big-endian order; it must contain at least 16 bytes
Returns:
- result: z after decoding the first 16 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 17 bytes are decoded in big-endian order; it must contain at least 17 bytes
Returns:
- result: z after decoding the first 17 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 18 bytes are decoded in big-endian order; it must contain at least 18 bytes
Returns:
- result: z after decoding the first 18 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 19 bytes are decoded in big-endian order; it must contain at least 19 bytes
Returns:
- result: z after decoding the first 19 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 2 bytes are decoded in big-endian order; it must contain at least 2 bytes
Returns:
- result: z after decoding the first 2 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 20 bytes are decoded in big-endian order; it must contain at least 20 bytes
Returns:
- result: z after decoding the first 20 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 21 bytes are decoded in big-endian order; it must contain at least 21 bytes
Returns:
- result: z after decoding the first 21 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 22 bytes are decoded in big-endian order; it must contain at least 22 bytes
Returns:
- result: z after decoding the first 22 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 23 bytes are decoded in big-endian order; it must contain at least 23 bytes
Returns:
- result: z after decoding the first 23 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 24 bytes are decoded in big-endian order; it must contain at least 24 bytes
Returns:
- result: z after decoding the first 24 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 25 bytes are decoded in big-endian order; it must contain at least 25 bytes
Returns:
- result: z after decoding the first 25 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 26 bytes are decoded in big-endian order; it must contain at least 26 bytes
Returns:
- result: z after decoding the first 26 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 27 bytes are decoded in big-endian order; it must contain at least 27 bytes
Returns:
- result: z after decoding the first 27 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 28 bytes are decoded in big-endian order; it must contain at least 28 bytes
Returns:
- result: z after decoding the first 28 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 29 bytes are decoded in big-endian order; it must contain at least 29 bytes
Returns:
- result: z after decoding the first 29 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 3 bytes are decoded in big-endian order; it must contain at least 3 bytes
Returns:
- result: z after decoding the first 3 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 30 bytes are decoded in big-endian order; it must contain at least 30 bytes
Returns:
- result: z after decoding the first 30 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 31 bytes are decoded in big-endian order; it must contain at least 31 bytes
Returns:
- result: z after decoding the first 31 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 32 bytes are decoded in big-endian order; it must contain at least 32 bytes
Returns:
- result: z after decoding the first 32 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 4 bytes are decoded in big-endian order; it must contain at least 4 bytes
Returns:
- result: z after decoding the first 4 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 5 bytes are decoded in big-endian order; it must contain at least 5 bytes
Returns:
- result: z after decoding the first 5 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 6 bytes are decoded in big-endian order; it must contain at least 6 bytes
Returns:
- result: z after decoding the first 6 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 7 bytes are decoded in big-endian order; it must contain at least 7 bytes
Returns:
- result: z after decoding the first 7 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 8 bytes are decoded in big-endian order; it must contain at least 8 bytes
Returns:
- result: z after decoding the first 8 bytes of in in big-endian order
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.
Parameters:
- in: the byte sequence whose first 9 bytes are decoded in big-endian order; it must contain at least 9 bytes
Returns:
- result: z after decoding the first 9 bytes of in in big-endian order
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.
Parameters:
- s: the decimal text to parse; a leading + is accepted, while negatives and underscores are rejected
Returns:
- err: nil on success; otherwise the parse or range error
func SetOne
method on UintSetOne sets z to 1 and returns z.
Returns:
- result: z after setting it to one
func SetUint64
method on UintSetUint64 sets z to the value of x and returns z.
Parameters:
- x: the uint64 value assigned to z
Returns:
- result: z after setting it to x
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.
Returns:
- sign: -1, 0, or +1 according to z interpreted as a two's-complement signed value
func Sub
method on UintSub sets z to the difference x-y and returns z.
Parameters:
- x: the minuend
- y: the subtrahend
Returns:
- result: z containing x-y modulo 2^256; underflow wraps in the unsigned representation
func SubOverflow
method on UintSubOverflow sets z to the difference x-y and returns z and true if underflow occurred.
Parameters:
- x: the minuend
- y: the subtrahend
Returns:
- result: z containing x-y modulo 2^256
- underflow: true when x is less than y
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.
Returns:
- decimal: z in base 10, or an empty string when z is nil
func Uint64
method on UintUint64 returns the lower 64 bits of z as a uint64.
Returns:
- value: the low 64 bits of z
func Uint64WithOverflow
method on UintUint64WithOverflow returns the lower 64 bits of z and true if overflow occurred.
Returns:
- value: the low 64 bits of z
- overflow: true when any upper 192 bits are non-zero
func Xor
method on UintXor sets z to the bitwise exclusive OR of x and y and returns z.
Parameters:
- x: First 256-bit operand.
- y: Second 256-bit operand.
Returns:
- z: The 256-bit bitwise exclusive OR x ^ y, stored in the receiver.
4
- encoding/binary stdlib
- errors stdlib
- math/bits stdlib
- strconv stdlib