uint256

package module
v0.0.0-...-36778fc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 5 Imported by: 0

README

uint256

hoilman's uint256 for gno.

original code from: https://pkg.go.dev/github.com/holiman/uint256

All code in this repository is subject to the license of the original repository

Documentation

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.

Index

Constants

View Source
const (
	MaxUint64 = 1<<64 - 1
)

Variables

View Source
var (
	ErrEmptyString      = errors.New("empty hex string")
	ErrSyntax           = errors.New("invalid hex string")
	ErrRange            = errors.New("number out of range")
	ErrMissingPrefix    = errors.New("hex string without 0x prefix")
	ErrEmptyNumber      = errors.New("hex string \"0x\"")
	ErrLeadingZero      = errors.New("hex number with leading zero digits")
	ErrBig256Range      = errors.New("hex number > 256 bits")
	ErrBadBufferLength  = errors.New("bad ssz buffer length")
	ErrBadEncodedLength = errors.New("bad ssz encoded length")
	ErrInvalidBase      = errors.New("invalid base")
	ErrInvalidBitSize   = errors.New("invalid bit size")
)

Functions

func Hello

func Hello() uint64

func Reciprocal

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

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

Types

type Uint

type Uint struct {
	// contains filtered or unexported fields
}

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

func FromDecimal

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

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

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

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

func MustFromDecimal(decimal string) *Uint

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

func MustFromHex(hex string) *Uint

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

func NewUint(val uint64) *Uint

NewUint returns a new initialized Uint.

func One

func One() *Uint

One returns a new Uint initialized to one.

func Zero

func Zero() *Uint

Zero returns a new Uint initialized to zero.

func (*Uint) Add

func (z *Uint) Add(x, y *Uint) *Uint

Add sets z to the sum x+y

func (*Uint) AddOverflow

func (z *Uint) AddOverflow(x, y *Uint) (*Uint, bool)

AddOverflow sets z to the sum x+y, and returns z and whether overflow occurred

func (*Uint) And

func (z *Uint) And(x, y *Uint) *Uint

And sets z = x & y and returns z.

func (*Uint) AndNot

func (z *Uint) AndNot(x, y *Uint) *Uint

AndNot sets z = x &^ y and returns z.

func (*Uint) BitLen

func (z *Uint) BitLen() int

BitLen returns the number of bits required to represent z

func (*Uint) Byte

func (z *Uint) Byte(n *Uint) *Uint

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 (*Uint) ByteLen

func (z *Uint) ByteLen() int

ByteLen returns the number of bytes required to represent z

func (*Uint) Clear

func (z *Uint) Clear() *Uint

Clear sets z to 0

func (*Uint) Clone

func (z *Uint) Clone() *Uint

Clone creates a new Uint identical to z

func (*Uint) Cmp

func (z *Uint) Cmp(x *Uint) (r int)

Cmp compares z and x and returns:

-1 if z <  x
 0 if z == x
+1 if z >  x

func (*Uint) Dec

func (z *Uint) Dec() string

Dec returns the decimal representation of z.

func (*Uint) Div

func (z *Uint) Div(x, y *Uint) *Uint

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 (*Uint) DivMod

func (z *Uint) DivMod(x, y, m *Uint) (*Uint, *Uint)

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 (*Uint) Eq

func (z *Uint) Eq(x *Uint) bool

Eq returns true if z == x

func (*Uint) Exp

func (z *Uint) Exp(base, exponent *Uint) *Uint

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

func (*Uint) Gt

func (z *Uint) Gt(x *Uint) bool

Gt returns true if z > x

func (*Uint) GtUint64

func (z *Uint) GtUint64(n uint64) bool

GtUint64 returns true if z is larger than n

func (*Uint) Gte

func (z *Uint) Gte(x *Uint) bool

Gte returns true if z >= x

func (*Uint) IsUint64

func (z *Uint) IsUint64() bool

IsUint64 reports whether z can be represented as a uint64.

func (*Uint) IsZero

func (z *Uint) IsZero() bool

IsZero returns true if z == 0

func (*Uint) Lsh

func (z *Uint) Lsh(x *Uint, n uint) *Uint

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

func (*Uint) Lt

func (z *Uint) Lt(x *Uint) bool

Lt returns true if z < x

func (*Uint) LtUint64

func (z *Uint) LtUint64(n uint64) bool

LtUint64 returns true if z is smaller than n

func (*Uint) Lte

func (z *Uint) Lte(x *Uint) bool

Lte returns true if z <= x

func (*Uint) MarshalJSON

func (z *Uint) MarshalJSON() ([]byte, error)

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 (*Uint) MarshalText

func (z *Uint) MarshalText() ([]byte, error)

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

func (*Uint) Mod

func (z *Uint) Mod(x, y *Uint) *Uint

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 (*Uint) Mul

func (z *Uint) Mul(x, y *Uint) *Uint

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

func (*Uint) MulMod

func (z *Uint) MulMod(x, y, m *Uint) *Uint

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 (*Uint) MulOverflow

func (z *Uint) MulOverflow(x, y *Uint) (*Uint, bool)

MulOverflow sets z to the product x*y, and returns z and whether overflow occurred

func (*Uint) Neg

func (z *Uint) Neg(x *Uint) *Uint

Neg returns -x mod 2^256.

func (*Uint) Neq

func (z *Uint) Neq(x *Uint) bool

Neq returns true if z != x

func (*Uint) Not

func (z *Uint) Not(x *Uint) *Uint

Not sets z = ^x and returns z.

func (*Uint) Or

func (z *Uint) Or(x, y *Uint) *Uint

Or sets z = x | y and returns z.

func (*Uint) Rsh

func (z *Uint) Rsh(x *Uint, n uint) *Uint

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

func (*Uint) SRsh

func (z *Uint) SRsh(x *Uint, n uint) *Uint

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

func (*Uint) Scan

func (z *Uint) Scan(src interface{}) error

func (*Uint) Set

func (z *Uint) Set(x *Uint) *Uint

Set sets z to x and returns z.

func (*Uint) SetAllOne

func (z *Uint) SetAllOne() *Uint

SetAllOne sets all the bits of z to 1

func (*Uint) SetBytes

func (z *Uint) SetBytes(buf []byte) *Uint

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 (*Uint) SetBytes1

func (z *Uint) SetBytes1(in []byte) *Uint

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

func (*Uint) SetBytes2

func (z *Uint) SetBytes2(in []byte) *Uint

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

func (*Uint) SetBytes3

func (z *Uint) SetBytes3(in []byte) *Uint

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

func (*Uint) SetBytes4

func (z *Uint) SetBytes4(in []byte) *Uint

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

func (*Uint) SetBytes5

func (z *Uint) SetBytes5(in []byte) *Uint

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

func (*Uint) SetBytes6

func (z *Uint) SetBytes6(in []byte) *Uint

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

func (*Uint) SetBytes7

func (z *Uint) SetBytes7(in []byte) *Uint

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

func (*Uint) SetBytes8

func (z *Uint) SetBytes8(in []byte) *Uint

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

func (*Uint) SetBytes9

func (z *Uint) SetBytes9(in []byte) *Uint

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

func (*Uint) SetBytes10

func (z *Uint) SetBytes10(in []byte) *Uint

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

func (*Uint) SetBytes11

func (z *Uint) SetBytes11(in []byte) *Uint

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

func (*Uint) SetBytes12

func (z *Uint) SetBytes12(in []byte) *Uint

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

func (*Uint) SetBytes13

func (z *Uint) SetBytes13(in []byte) *Uint

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

func (*Uint) SetBytes14

func (z *Uint) SetBytes14(in []byte) *Uint

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

func (*Uint) SetBytes15

func (z *Uint) SetBytes15(in []byte) *Uint

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

func (*Uint) SetBytes16

func (z *Uint) SetBytes16(in []byte) *Uint

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

func (*Uint) SetBytes17

func (z *Uint) SetBytes17(in []byte) *Uint

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

func (*Uint) SetBytes18

func (z *Uint) SetBytes18(in []byte) *Uint

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

func (*Uint) SetBytes19

func (z *Uint) SetBytes19(in []byte) *Uint

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

func (*Uint) SetBytes20

func (z *Uint) SetBytes20(in []byte) *Uint

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

func (*Uint) SetBytes21

func (z *Uint) SetBytes21(in []byte) *Uint

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

func (*Uint) SetBytes22

func (z *Uint) SetBytes22(in []byte) *Uint

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

func (*Uint) SetBytes23

func (z *Uint) SetBytes23(in []byte) *Uint

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

func (*Uint) SetBytes24

func (z *Uint) SetBytes24(in []byte) *Uint

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

func (*Uint) SetBytes25

func (z *Uint) SetBytes25(in []byte) *Uint

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

func (*Uint) SetBytes26

func (z *Uint) SetBytes26(in []byte) *Uint

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

func (*Uint) SetBytes27

func (z *Uint) SetBytes27(in []byte) *Uint

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

func (*Uint) SetBytes28

func (z *Uint) SetBytes28(in []byte) *Uint

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

func (*Uint) SetBytes29

func (z *Uint) SetBytes29(in []byte) *Uint

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

func (*Uint) SetBytes30

func (z *Uint) SetBytes30(in []byte) *Uint

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

func (*Uint) SetBytes31

func (z *Uint) SetBytes31(in []byte) *Uint

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

func (*Uint) SetBytes32

func (z *Uint) SetBytes32(in []byte) *Uint

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

func (*Uint) SetFromDecimal

func (z *Uint) SetFromDecimal(s string) (err error)

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 (*Uint) SetFromHex

func (z *Uint) SetFromHex(hex string) error

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 (*Uint) SetOne

func (z *Uint) SetOne() *Uint

SetOne sets z to 1

func (*Uint) SetUint64

func (z *Uint) SetUint64(x uint64) *Uint

SetUint64 sets z to the value x

func (*Uint) Sgt

func (z *Uint) Sgt(x *Uint) bool

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

func (*Uint) Sign

func (z *Uint) Sign() int

Sign returns:

-1 if z <  0
 0 if z == 0
+1 if z >  0

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

func (*Uint) Sub

func (z *Uint) Sub(x, y *Uint) *Uint

Sub sets z to the difference x-y

func (*Uint) SubOverflow

func (z *Uint) SubOverflow(x, y *Uint) (*Uint, bool)

SubOverflow sets z to the difference x-y and returns z and true if the operation underflowed

func (*Uint) ToString

func (z *Uint) ToString() string

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 (*Uint) Uint64

func (z *Uint) Uint64() uint64

Uint64 returns the lower 64-bits of z

func (*Uint) Uint64WithOverflow

func (z *Uint) Uint64WithOverflow() (uint64, bool)

Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred

func (*Uint) UnmarshalJSON

func (z *Uint) UnmarshalJSON(input []byte) error

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

func (*Uint) UnmarshalText

func (z *Uint) UnmarshalText(input []byte) error

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

func (*Uint) Xor

func (z *Uint) Xor(x, y *Uint) *Uint

Xor sets z = x ^ y and returns z.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL