Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v1 source pure

arithmetic provides arithmetic operations for Uint objects. This includes basic binary operations such as addition, s...

Readme 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 *Overflow variants
  • Optimized MulDiv for 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

Overview

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.

Constants 1

Functions 11

func Reciprocal

1func Reciprocal(m *Uint) (mu [5]uint64)
source

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

1func DivRoundingUp(x, y *Uint) *Uint
source

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

1func FromDecimal(decimal string) (*Uint, error)
source

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

1func MaxUint256() *Uint
source

MaxUint256 returns the maximum 256-bit unsigned integer (2^256-1).

Returns:

  • result: a new Uint containing 2^256-1

func MulDiv

1func MulDiv(a, b, denominator *Uint) *Uint
source

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

1func MulDivRoundingUp(a, b, denominator *Uint) *Uint
source

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

1func MustFromDecimal(decimal string) *Uint
source

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

1func NewUint(val uint64) *Uint
source

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

1func NewUintFromInt64(val int64) *Uint
source

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

1func One() *Uint
source

One returns a new Uint with value 1.

Returns:

  • result: a new Uint containing one

func Zero

1func Zero() *Uint
source

Zero returns a new Uint with value 0.

Returns:

  • result: a new zero-valued Uint

Types 1

type Uint

array
1type Uint [4]uint64
source

Uint 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 Uint
1func (z *Uint) Add(x, y *Uint) *Uint
source

Add 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 Uint
1func (z *Uint) AddOverflow(x, y *Uint) (*Uint, bool)
source

AddOverflow 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 Uint
1func (z *Uint) And(x, y *Uint) *Uint
source

And 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 Uint
1func (z *Uint) AndNot(x, y *Uint) *Uint
source

AndNot 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 Uint
1func (z *Uint) BitLen() int
source

BitLen 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 Uint
1func (z *Uint) Byte(n *Uint) *Uint
source

Byte 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 Uint
1func (z *Uint) ByteLen() int
source

ByteLen 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 Uint
1func (z *Uint) Clear() *Uint
source

Clear sets z to 0 and returns z.

Returns:

  • result: z after setting it to zero

func Clone

method on Uint
1func (z *Uint) Clone() *Uint
source

Clone 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 Uint
1func (z *Uint) Cmp(x *Uint) (r int)
source

Cmp 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 Uint
1func (z *Uint) Dec() string
source

Dec returns the decimal representation of z.

Returns:

  • decimal: the base-10 representation of z

func Div

method on Uint
1func (z *Uint) Div(x, y *Uint) *Uint
source

Div 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 Uint
1func (z *Uint) DivMod(x, y, m *Uint) (*Uint, *Uint)
source

DivMod 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 Uint
1func (z *Uint) Eq(x *Uint) bool
source

Eq 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 Uint
1func (z *Uint) Gt(x *Uint) bool
source

Gt returns true if z is greater than x.

Parameters:

  • x: the comparison operand

Returns:

  • greater: true when z>x

func Gte

method on Uint
1func (z *Uint) Gte(x *Uint) bool
source

Gte 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 Uint
1func (z *Uint) Int64() int64
source

Int64 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 Uint
1func (z *Uint) IsOverflow() bool
source

IsOverflow reports whether the highest bit of z is set.

Returns:

  • overflow: true when z has bit 255 set

func IsUint64

method on Uint
1func (z *Uint) IsUint64() bool
source

IsUint64 reports whether z can be represented as a uint64.

Returns:

  • fits: true when z is representable in 64 bits

func IsZero

method on Uint
1func (z *Uint) IsZero() bool
source

IsZero returns true if z equals 0.

Returns:

  • isZero: true when all four words of z are zero

func Lsh

method on Uint
1func (z *Uint) Lsh(x *Uint, n uint) *Uint
source

Lsh 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 Uint
1func (z *Uint) Lt(x *Uint) bool
source

Lt returns true if z is less than x.

Parameters:

  • x: the comparison operand

Returns:

  • less: true when z<x

func Lte

method on Uint
1func (z *Uint) Lte(x *Uint) bool
source

Lte 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 Uint
1func (z *Uint) Mod(x, y *Uint) *Uint
source

Mod 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 Uint
1func (z *Uint) Mul(x, y *Uint) *Uint
source

Mul 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 Uint
1func (z *Uint) MulMod(x, y, m *Uint) *Uint
source

MulMod 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 Uint
1func (z *Uint) MulOverflow(x, y *Uint) (*Uint, bool)
source

MulOverflow 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 Uint
1func (z *Uint) Neg(x *Uint) *Uint
source

Neg 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 Uint
1func (z *Uint) Neq(x *Uint) bool
source

Neq 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 Uint
1func (z *Uint) Not(x *Uint) *Uint
source

Not 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 Uint
1func (z *Uint) Or(x, y *Uint) *Uint
source

Or 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 Uint
1func (z *Uint) Rsh(x *Uint, n uint) *Uint
source

Rsh 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 Uint
1func (z *Uint) SRsh(x *Uint, n uint) *Uint
source

SRsh 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 Uint
1func (z *Uint) Set(x *Uint) *Uint
source

Set 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 Uint
1func (z *Uint) SetAllOne() *Uint
source

SetAllOne 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 Uint
1func (z *Uint) SetBytes(buf []byte) *Uint
source

SetBytes 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 Uint
1func (z *Uint) SetBytes1(in []byte) *Uint
source

SetBytes1 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 Uint
1func (z *Uint) SetBytes10(in []byte) *Uint
source

SetBytes10 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 Uint
1func (z *Uint) SetBytes11(in []byte) *Uint
source

SetBytes11 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 Uint
1func (z *Uint) SetBytes12(in []byte) *Uint
source

SetBytes12 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 Uint
1func (z *Uint) SetBytes13(in []byte) *Uint
source

SetBytes13 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 Uint
1func (z *Uint) SetBytes14(in []byte) *Uint
source

SetBytes14 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 Uint
1func (z *Uint) SetBytes15(in []byte) *Uint
source

SetBytes15 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 Uint
1func (z *Uint) SetBytes16(in []byte) *Uint
source

SetBytes16 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 Uint
1func (z *Uint) SetBytes17(in []byte) *Uint
source

SetBytes17 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 Uint
1func (z *Uint) SetBytes18(in []byte) *Uint
source

SetBytes18 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 Uint
1func (z *Uint) SetBytes19(in []byte) *Uint
source

SetBytes19 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 Uint
1func (z *Uint) SetBytes2(in []byte) *Uint
source

SetBytes2 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 Uint
1func (z *Uint) SetBytes20(in []byte) *Uint
source

SetBytes20 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 Uint
1func (z *Uint) SetBytes21(in []byte) *Uint
source

SetBytes21 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 Uint
1func (z *Uint) SetBytes22(in []byte) *Uint
source

SetBytes22 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 Uint
1func (z *Uint) SetBytes23(in []byte) *Uint
source

SetBytes23 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 Uint
1func (z *Uint) SetBytes24(in []byte) *Uint
source

SetBytes24 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 Uint
1func (z *Uint) SetBytes25(in []byte) *Uint
source

SetBytes25 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 Uint
1func (z *Uint) SetBytes26(in []byte) *Uint
source

SetBytes26 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 Uint
1func (z *Uint) SetBytes27(in []byte) *Uint
source

SetBytes27 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 Uint
1func (z *Uint) SetBytes28(in []byte) *Uint
source

SetBytes28 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 Uint
1func (z *Uint) SetBytes29(in []byte) *Uint
source

SetBytes29 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 Uint
1func (z *Uint) SetBytes3(in []byte) *Uint
source

SetBytes3 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 Uint
1func (z *Uint) SetBytes30(in []byte) *Uint
source

SetBytes30 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 Uint
1func (z *Uint) SetBytes31(in []byte) *Uint
source

SetBytes31 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 Uint
1func (z *Uint) SetBytes32(in []byte) *Uint
source

SetBytes32 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 Uint
1func (z *Uint) SetBytes4(in []byte) *Uint
source

SetBytes4 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 Uint
1func (z *Uint) SetBytes5(in []byte) *Uint
source

SetBytes5 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 Uint
1func (z *Uint) SetBytes6(in []byte) *Uint
source

SetBytes6 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 Uint
1func (z *Uint) SetBytes7(in []byte) *Uint
source

SetBytes7 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 Uint
1func (z *Uint) SetBytes8(in []byte) *Uint
source

SetBytes8 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 Uint
1func (z *Uint) SetBytes9(in []byte) *Uint
source

SetBytes9 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 Uint
1func (z *Uint) SetFromDecimal(s string) (err error)
source

SetFromDecimal 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 Uint
1func (z *Uint) SetOne() *Uint
source

SetOne sets z to 1 and returns z.

Returns:

  • result: z after setting it to one

func SetUint64

method on Uint
1func (z *Uint) SetUint64(x uint64) *Uint
source

SetUint64 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 Uint
1func (z *Uint) Sign() int
source

Sign 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 Uint
1func (z *Uint) Sub(x, y *Uint) *Uint
source

Sub 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 Uint
1func (z *Uint) SubOverflow(x, y *Uint) (*Uint, bool)
source

SubOverflow 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 Uint
1func (z *Uint) ToString() string
source

ToString 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 Uint
1func (z *Uint) Uint64() uint64
source

Uint64 returns the lower 64 bits of z as a uint64.

Returns:

  • value: the low 64 bits of z

func Uint64WithOverflow

method on Uint
1func (z *Uint) Uint64WithOverflow() (uint64, bool)
source

Uint64WithOverflow 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 Uint
1func (z *Uint) Xor(x, y *Uint) *Uint
source

Xor 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.

Imports 4

  • encoding/binary stdlib
  • errors stdlib
  • math/bits stdlib
  • strconv stdlib

Source Files 10