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

Fixed size 256-bit math library

This is a library specialized at replacing the big.Int library for math based on 256-bit types.

original repository: 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.

Ported from https://github.com/holiman/uint256 This package provides a 256-bit unsigned integer type, Uint256, and associated functions.

Constants 1

Variables 1

var ErrEmptyString, ErrSyntax, ErrRange, ErrMissingPrefix, ErrEmptyNumber, ErrLeadingZero, ErrBig256Range, ErrBadBufferLength, ErrBadEncodedLength, ErrInvalidBase, ErrInvalidBitSize

 1var (
 2	ErrEmptyString      = errors.New("empty hex string")
 3	ErrSyntax           = errors.New("invalid hex string")
 4	ErrRange            = errors.New("number out of range")
 5	ErrMissingPrefix    = errors.New("hex string without 0x prefix")
 6	ErrEmptyNumber      = errors.New("hex string \"0x\"")
 7	ErrLeadingZero      = errors.New("hex number with leading zero digits")
 8	ErrBig256Range      = errors.New("hex number > 256 bits")
 9	ErrBadBufferLength  = errors.New("bad ssz buffer length")
10	ErrBadEncodedLength = errors.New("bad ssz encoded length")
11	ErrInvalidBase      = errors.New("invalid base")
12	ErrInvalidBitSize   = errors.New("invalid bit size")
13)
source

Functions 8

func Reciprocal

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

Reciprocal computes a 320-bit value representing 1/m

Notes: - specialized for m.arr[3] != 0, hence limited to 2^192 <= m < 2^256 - returns zero if m.arr[3] == 0 - starts with a 32-bit division, refines with newton-raphson iterations

func FromDecimal

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

FromDecimal is a convenience-constructor to create an Uint from a decimal (base 10) string. Numbers larger than 256 bits are not accepted.

func FromHex

1func FromHex(hex string) (*Uint, error)
source

FromHex is a convenience-constructor to create an Uint from a hexadecimal string. The string is required to be '0x'-prefixed Numbers larger than 256 bits are not accepted.

func MustFromDecimal

1func MustFromDecimal(decimal string) *Uint
source

MustFromDecimal is a convenience-constructor to create an Uint from a decimal (base 10) string. Returns a new Uint and panics if any error occurred.

func MustFromHex

1func MustFromHex(hex string) *Uint
source

MustFromHex is a convenience-constructor to create an Uint from a hexadecimal string. Returns a new Uint and panics if any error occurred.

func NewUint

1func NewUint(val uint64) *Uint
source

NewUint returns a new initialized Uint.

func One

1func One() *Uint
source

One returns a new Uint initialized to one.

func Zero

1func Zero() *Uint
source

Zero returns a new Uint initialized to zero.

Types 1

type Uint

struct
1type Uint struct {
2	arr [4]uint64
3}
source

Uint is represented as an array of 4 uint64, in little-endian order, so that Uint[3] is the most significant, and Uint[0] is the least 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

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 whether overflow occurred

func And

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

And sets z = x & y and returns z.

func AndNot

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

AndNot sets z = x &^ 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

func Byte

method on Uint
1func (z *Uint) Byte(n *Uint) *Uint
source

Byte sets z to the value of the byte at position n, with 'z' considered as a big-endian 32-byte integer if 'n' > 32, f is set to 0 Example: f = '5', n=31 => 5

func ByteLen

method on Uint
1func (z *Uint) ByteLen() int
source

ByteLen returns the number of bytes required to represent z

func Clear

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

Clear sets z to 0

func Clone

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

Clone creates a new Uint identical to z

func Cmp

method on Uint
1func (z *Uint) Cmp(x *Uint) (r int)
source

Cmp compares z and x and returns:

Example
1-1 if z <  x
2 0 if z == x
3+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

commented out for possible overflow Div sets z to the quotient x/y for returns z. If y == 0, z is set to 0

func DivMod

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

DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0. If y == 0, both z and m are set to 0 (OBS: differs from the big.Int)

func Eq

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

Eq returns true if z == x

func Exp

method on Uint
1func (z *Uint) Exp(base, exponent *Uint) *Uint
source

Exp sets z = base**exponent mod 2**256, and returns z.

func Gt

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

Gt returns true if z > x

func GtUint64

method on Uint
1func (z *Uint) GtUint64(n uint64) bool
source

GtUint64 returns true if z is larger than n

func Gte

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

Gte returns true if z >= x

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 == 0

func Lsh

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

Lsh sets z = x << n and returns z.

func Lt

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

Lt returns true if z < x

func LtUint64

method on Uint
1func (z *Uint) LtUint64(n uint64) bool
source

LtUint64 returns true if z is smaller than n

func Lte

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

Lte returns true if z <= x

func MarshalJSON

method on Uint
1func (z *Uint) MarshalJSON() ([]byte, error)
source

MarshalJSON implements json.Marshaler. MarshalJSON marshals using the 'decimal string' representation. This is _not_ compatible with big.Uint: big.Uint marshals into JSON 'native' numeric format.

The JSON native format is, on some platforms, (e.g. javascript), limited to 53-bit large integer space. Thus, U256 uses string-format, which is not compatible with big.int (big.Uint refuses to unmarshal a string representation).

func MarshalText

method on Uint
1func (z *Uint) MarshalText() ([]byte, error)
source

MarshalText implements encoding.TextMarshaler MarshalText marshals using the decimal representation (compatible with big.Uint)

func Mod

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

Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, z is set to 0 (OBS: differs from the big.Uint)

func Mul

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

commented out for possible overflow Mul sets z to the product x*y

func MulMod

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

MulMod calculates the modulo-m multiplication of x and y and returns z. If m == 0, z is set to 0 (OBS: differs from the big.Int)

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 whether 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 != x

func Not

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

Not sets z = ^x and returns z.

func Or

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

Or sets z = x | y and returns z.

func Rsh

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

Rsh sets z = x >> n and returns z.

func SRsh

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

SRsh (Signed/Arithmetic right shift) considers z to be a signed integer, during right-shift and sets z = x >> n and returns z.

func Scan

method on Uint
1func (z *Uint) Scan(src any) error
source

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 all the bits of z to 1

func SetBytes

method on Uint
1func (z *Uint) SetBytes(buf []byte) *Uint
source

SetBytes interprets buf as the bytes of a big-endian unsigned integer, sets z to that value, and returns z. If buf is larger than 32 bytes, the last 32 bytes is used.

func SetBytes1

method on Uint
1func (z *Uint) SetBytes1(in []byte) *Uint
source

SetBytes1 is identical to SetBytes(in[:1]), but panics is input is too short

func SetBytes10

method on Uint
1func (z *Uint) SetBytes10(in []byte) *Uint
source

SetBytes10 is identical to SetBytes(in[:10]), but panics is input is too short

func SetBytes11

method on Uint
1func (z *Uint) SetBytes11(in []byte) *Uint
source

SetBytes11 is identical to SetBytes(in[:11]), but panics is input is too short

func SetBytes12

method on Uint
1func (z *Uint) SetBytes12(in []byte) *Uint
source

SetBytes12 is identical to SetBytes(in[:12]), but panics is input is too short

func SetBytes13

method on Uint
1func (z *Uint) SetBytes13(in []byte) *Uint
source

SetBytes13 is identical to SetBytes(in[:13]), but panics is input is too short

func SetBytes14

method on Uint
1func (z *Uint) SetBytes14(in []byte) *Uint
source

SetBytes14 is identical to SetBytes(in[:14]), but panics is input is too short

func SetBytes15

method on Uint
1func (z *Uint) SetBytes15(in []byte) *Uint
source

SetBytes15 is identical to SetBytes(in[:15]), but panics is input is too short

func SetBytes16

method on Uint
1func (z *Uint) SetBytes16(in []byte) *Uint
source

SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short

func SetBytes17

method on Uint
1func (z *Uint) SetBytes17(in []byte) *Uint
source

SetBytes17 is identical to SetBytes(in[:17]), but panics is input is too short

func SetBytes18

method on Uint
1func (z *Uint) SetBytes18(in []byte) *Uint
source

SetBytes18 is identical to SetBytes(in[:18]), but panics is input is too short

func SetBytes19

method on Uint
1func (z *Uint) SetBytes19(in []byte) *Uint
source

SetBytes19 is identical to SetBytes(in[:19]), but panics is input is too short

func SetBytes2

method on Uint
1func (z *Uint) SetBytes2(in []byte) *Uint
source

SetBytes2 is identical to SetBytes(in[:2]), but panics is input is too short

func SetBytes20

method on Uint
1func (z *Uint) SetBytes20(in []byte) *Uint
source

SetBytes20 is identical to SetBytes(in[:20]), but panics is input is too short

func SetBytes21

method on Uint
1func (z *Uint) SetBytes21(in []byte) *Uint
source

SetBytes21 is identical to SetBytes(in[:21]), but panics is input is too short

func SetBytes22

method on Uint
1func (z *Uint) SetBytes22(in []byte) *Uint
source

SetBytes22 is identical to SetBytes(in[:22]), but panics is input is too short

func SetBytes23

method on Uint
1func (z *Uint) SetBytes23(in []byte) *Uint
source

SetBytes23 is identical to SetBytes(in[:23]), but panics is input is too short

func SetBytes24

method on Uint
1func (z *Uint) SetBytes24(in []byte) *Uint
source

SetBytes24 is identical to SetBytes(in[:24]), but panics is input is too short

func SetBytes25

method on Uint
1func (z *Uint) SetBytes25(in []byte) *Uint
source

SetBytes25 is identical to SetBytes(in[:25]), but panics is input is too short

func SetBytes26

method on Uint
1func (z *Uint) SetBytes26(in []byte) *Uint
source

SetBytes26 is identical to SetBytes(in[:26]), but panics is input is too short

func SetBytes27

method on Uint
1func (z *Uint) SetBytes27(in []byte) *Uint
source

SetBytes27 is identical to SetBytes(in[:27]), but panics is input is too short

func SetBytes28

method on Uint
1func (z *Uint) SetBytes28(in []byte) *Uint
source

SetBytes28 is identical to SetBytes(in[:28]), but panics is input is too short

func SetBytes29

method on Uint
1func (z *Uint) SetBytes29(in []byte) *Uint
source

SetBytes29 is identical to SetBytes(in[:29]), but panics is input is too short

func SetBytes3

method on Uint
1func (z *Uint) SetBytes3(in []byte) *Uint
source

SetBytes3 is identical to SetBytes(in[:3]), but panics is input is too short

func SetBytes30

method on Uint
1func (z *Uint) SetBytes30(in []byte) *Uint
source

SetBytes30 is identical to SetBytes(in[:30]), but panics is input is too short

func SetBytes31

method on Uint
1func (z *Uint) SetBytes31(in []byte) *Uint
source

SetBytes31 is identical to SetBytes(in[:31]), but panics is input is too short

func SetBytes32

method on Uint
1func (z *Uint) SetBytes32(in []byte) *Uint
source

SetBytes32 sets z to the value of the big-endian 256-bit unsigned integer in.

func SetBytes4

method on Uint
1func (z *Uint) SetBytes4(in []byte) *Uint
source

SetBytes4 is identical to SetBytes(in[:4]), but panics is input is too short

func SetBytes5

method on Uint
1func (z *Uint) SetBytes5(in []byte) *Uint
source

SetBytes5 is identical to SetBytes(in[:5]), but panics is input is too short

func SetBytes6

method on Uint
1func (z *Uint) SetBytes6(in []byte) *Uint
source

SetBytes6 is identical to SetBytes(in[:6]), but panics is input is too short

func SetBytes7

method on Uint
1func (z *Uint) SetBytes7(in []byte) *Uint
source

SetBytes7 is identical to SetBytes(in[:7]), but panics is input is too short

func SetBytes8

method on Uint
1func (z *Uint) SetBytes8(in []byte) *Uint
source

SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short

func SetBytes9

method on Uint
1func (z *Uint) SetBytes9(in []byte) *Uint
source

SetBytes9 is identical to SetBytes(in[:9]), but panics is input is too short

func SetFromDecimal

method on Uint
1func (z *Uint) SetFromDecimal(s string) (err error)
source

SetFromDecimal sets z from the given string, interpreted as a decimal number. OBS! This method is _not_ strictly identical to the (*big.Uint).SetString(..., 10) method. Notable differences: - This method does not accept underscore input, e.g. "100_000", - This method does not accept negative zero as valid, e.g "-0",

  • (this method does not accept any negative input as valid))

func SetFromHex

method on Uint
1func (z *Uint) SetFromHex(hex string) error
source

SetFromHex sets z from the given string, interpreted as a hexadecimal number. OBS! This method is _not_ strictly identical to the (*big.Int).SetString(..., 16) method. Notable differences: - This method _require_ "0x" or "0X" prefix. - This method does not accept zero-prefixed hex, e.g. "0x0001" - This method does not accept underscore input, e.g. "100_000", - This method does not accept negative zero as valid, e.g "-0x0",

  • (this method does not accept any negative input as valid)

func SetOne

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

SetOne sets z to 1

func SetUint64

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

SetUint64 sets z to the value x

func Sgt

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

Sgt interprets z and x as signed integers, and returns true if z > x

func Sign

method on Uint
1func (z *Uint) Sign() int
source

Sign returns:

Example
1-1 if z <  0
2 0 if z == 0
3+1 if z >  0

Where z is interpreted as a two's complement signed number

func String

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

ToString returns the decimal string representation of z. It returns an empty string if z is nil. OBS: doesn't exist from holiman's uint256

func Sub

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

Sub sets z to the difference x-y

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 the operation underflowed

func Uint64

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

Uint64 returns the lower 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 bool whether overflow occurred

func UnmarshalJSON

method on Uint
1func (z *Uint) UnmarshalJSON(input []byte) error
source

UnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either - Quoted string: either hexadecimal OR decimal - Not quoted string: only decimal

func UnmarshalText

method on Uint
1func (z *Uint) UnmarshalText(input []byte) error
source

UnmarshalText implements encoding.TextUnmarshaler. This method can unmarshal either hexadecimal or decimal. - For hexadecimal, the input _must_ be prefixed with 0x or 0X

func Xor

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

Xor sets z = x ^ y and returns z.

Imports 5

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

Source Files 17