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

uint256 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 overflow detection and precise MulDiv operations.

Features

  • Fixed 256-bit size (4 uint64 values)
  • Overflow detection on all operations
  • Optimized MulDiv for precise calculations
  • String conversion (decimal, hex, binary)
  • Range: 0 to 2^256-1

Usage

 1import u256 "gno.land/p/gnoswap/uint256"
 2
 3// Create values
 4a := u256.NewUint(1000)
 5b := u256.MustFromDecimal("1000000000000000000")
 6
 7// Arithmetic with overflow detection
 8result, overflow := new(u256.Uint).AddOverflow(a, b)
 9if overflow {
10    // Handle overflow
11}
12
13// Precise MulDiv (a * b / c)
14result := u256.MulDiv(a, b, c)

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

Constants 1

Functions 11

func Reciprocal

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

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

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

DivRoundingUp calculates ceil(x / y) and returns the result. 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.

func MulDiv

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

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

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

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

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.

func NewUint

1func NewUint(val uint64) *Uint
source

NewUint returns a new Uint initialized with the given uint64 value.

func NewUintFromInt64

1func NewUintFromInt64(val int64) *Uint
source

NewUintFromInt64 returns a new Uint initialized with the given int64 value. Panics if val is negative.

func One

1func One() *Uint
source

One returns a new Uint with value 1.

func Zero

1func Zero() *Uint
source

Zero returns a new Uint with value 0.

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.

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.

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.

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.

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.

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 right (0 = least significant byte). Returns 0 if n >= 32.

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.

func Clear

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

Clear sets z to 0 and returns z.

func Clone

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

Clone returns a new 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.

func Dec

method on Uint
1func (z *Uint) Dec() string
source

Dec returns the decimal 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.

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.

func Eq

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

Eq returns true if z equals x.

func Gt

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

Gt returns true if z is greater than 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.

func Int64

method on Uint
1func (z *Uint) Int64() int64
source

Int64 returns the lower 64 bits of z as an int64.

func IsUint64

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

IsUint64 reports whether z can be represented as a uint64.

func IsZero

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

IsZero returns true if z equals 0.

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. The result is truncated to 256 bits. If n >= 256, z is set to 0.

func Lt

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

Lt returns true if z is less than 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.

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.

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.

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.

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.

func Neg

method on Uint
1func (z *Uint) Neg(x *Uint) *Uint
source

Neg returns -x mod 2^256.

func Neq

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

Neq returns true if z does not equal x.

func Not

method on Uint
1func (z *Uint) Not(x *Uint) *Uint
source

Not sets z to the bitwise NOT of x and returns z.

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.

func Rsh

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

Rsh sets z to x right-shifted by n bits and returns z. If n >= 256, z is set to 0.

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

Set sets z to x and returns z.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

func SetOne

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

SetOne sets z to 1 and returns z.

func SetUint64

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

SetUint64 sets z to the value of x and returns z.

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.

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.

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.

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.

func Uint64

method on Uint
1func (z *Uint) Uint64() uint64
source

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

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.

func Xor

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

Xor sets z to the bitwise XOR of x and y and returns z.

Imports 4

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

Source Files 11