uint256

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: BSD-3-Clause Imports: 13 Imported by: 3,357

README

Fixed size 256-bit math library

This is a library specialized at replacing the big.Int library for math based on 256-bit types, used by both go-ethereum and erigon.

Go Reference codecov DeepSource

Benchmarks

Current benchmarks, with tests ending with big being the standard big.Int library, and uint256 being this library.

Current status
  • As of 2020-03-18, uint256 wins over math/big in every single case, often with orders of magnitude.
  • And as of release 0.1.0, the uint256 library is alloc-free.
  • With the 1.0.0 release, it also has 100% test coverage.
Bitwise
benchmark u256 time/op big time/op time diff u256 B/op big B/op B diff u256 allocs/op big allocs/op allocs diff
And/single 2.03ns 8.46ns -76.04% 0 0 ~ 0 0 ~
Or/single 2.03ns 10.98ns -81.51% 0 0 ~ 0 0 ~
Xor/single 2.03ns 12.19ns -83.34% 0 0 ~ 0 0 ~
Rsh/n_eq_0 5.00ns 48.11ns -89.61% 0 64 -100.00% 0 1 -100.00%
Rsh/n_gt_0 10.42ns 48.41ns -78.48% 0 64 -100.00% 0 1 -100.00%
Rsh/n_gt_64 6.94ns 52.39ns -86.76% 0 64 -100.00% 0 1 -100.00%
Rsh/n_gt_128 5.49ns 44.21ns -87.59% 0 48 -100.00% 0 1 -100.00%
Rsh/n_gt_192 3.43ns 28.71ns -88.04% 0 8 -100.00% 0 1 -100.00%
Lsh/n_eq_0 4.89ns 40.49ns -87.92% 0 64 -100.00% 0 1 -100.00%
Lsh/n_gt_0 10.14ns 53.25ns -80.96% 0 80 -100.00% 0 1 -100.00%
Lsh/n_gt_64 7.50ns 53.92ns -86.08% 0 80 -100.00% 0 1 -100.00%
Lsh/n_gt_128 5.39ns 56.86ns -90.52% 0 96 -100.00% 0 1 -100.00%
Lsh/n_gt_192 3.90ns 57.61ns -93.23% 0 96 -100.00% 0 1 -100.00%
Conversions
benchmark u256 time/op big time/op time diff u256 B/op big B/op B diff u256 allocs/op big allocs/op allocs diff
FromHexString 116.70ns 861.30ns -86.45% 32 88 -63.64% 1 3 -66.67%
FromDecimalString 7973.00ns 32850.00ns -75.73% 0 2464 -100.00% 0 77 -100.00%
Float64/Float64 2366.00ns 28483.00ns -91.69% 0 23424 -100.00% 0 510 -100.00%
EncodeHex/large 95.26ns 184.30ns -48.31% 80 140 -42.86% 1 2 -50.00%
Decimal/ToDecimal 67384.00ns 83431.00ns -19.23% 11344 31920 -64.46% 248 594 -58.25%
Decimal/ToPrettyDecimal 84208.00ns 123953.00ns -32.06% 14720 61376 -76.02% 251 1100 -77.18%
Math
benchmark u256 time/op big time/op time diff u256 B/op big B/op B diff u256 allocs/op big allocs/op allocs diff
Add/single 1.99ns 18.11ns -89.02% 0 0 ~ 0 0 ~
Sub/single 2.00ns 17.19ns -88.35% 0 0 ~ 0 0 ~
Mul/single 12.10ns 57.69ns -79.03% 0 0 ~ 0 0 ~
SDiv/large 94.64ns 642.10ns -85.26% 0 312 -100.00% 0 6 -100.00%
Sqrt/single 594.90ns 1844.00ns -67.74% 0 528 -100.00% 0 7 -100.00%
Square/single 12.49ns 56.10ns -77.74% 0 0 ~ 0 0 ~
Cmp/single 4.78ns 12.79ns -62.61% 0 0 ~ 0 0 ~
Div/small 12.91ns 48.31ns -73.28% 0 8 -100.00% 0 1 -100.00%
Div/mod64 65.77ns 111.20ns -40.85% 0 8 -100.00% 0 1 -100.00%
Div/mod128 93.67ns 301.40ns -68.92% 0 80 -100.00% 0 1 -100.00%
Div/mod192 86.52ns 263.80ns -67.20% 0 80 -100.00% 0 1 -100.00%
Div/mod256 77.17ns 252.80ns -69.47% 0 80 -100.00% 0 1 -100.00%
AddMod/small 13.84ns 48.16ns -71.26% 0 4 -100.00% 0 0 ~
AddMod/mod64 22.83ns 57.58ns -60.35% 0 11 -100.00% 0 0 ~
AddMod/mod128 48.31ns 145.00ns -66.68% 0 12 -100.00% 0 0 ~
AddMod/mod192 47.26ns 160.00ns -70.46% 0 12 -100.00% 0 0 ~
AddMod/mod256 14.44ns 143.20ns -89.92% 0 12 -100.00% 0 0 ~
MulMod/small 40.81ns 63.14ns -35.37% 0 8 -100.00% 0 1 -100.00%
MulMod/mod64 91.66ns 112.60ns -18.60% 0 48 -100.00% 0 1 -100.00%
MulMod/mod128 136.00ns 362.70ns -62.50% 0 128 -100.00% 0 2 -100.00%
MulMod/mod192 151.70ns 421.20ns -63.98% 0 144 -100.00% 0 2 -100.00%
MulMod/mod256 188.80ns 489.20ns -61.41% 0 176 -100.00% 0 2 -100.00%
Exp/small 433.70ns 7490.00ns -94.21% 0 7392 -100.00% 0 77 -100.00%
Exp/large 5145.00ns 23043.00ns -77.67% 0 18144 -100.00% 0 189 -100.00%
Mod/mod64 70.94ns 118.90ns -40.34% 0 64 -100.00% 0 1 -100.00%
Mod/small 14.82ns 46.27ns -67.97% 0 8 -100.00% 0 1 -100.00%
Mod/mod128 99.58ns 286.90ns -65.29% 0 64 -100.00% 0 1 -100.00%
Mod/mod192 95.31ns 269.30ns -64.61% 0 48 -100.00% 0 1 -100.00%
Mod/mod256 84.13ns 222.90ns -62.26% 0 8 -100.00% 0 1 -100.00%
MulOverflow/single 27.68ns 57.52ns -51.88% 0 0 ~ 0 0 ~
MulDivOverflow/small 2.83ns 22.97ns -87.69% 0 0 ~ 0 0 ~
MulDivOverflow/div64 2.85ns 23.13ns -87.66% 0 0 ~ 0 0 ~
MulDivOverflow/div128 3.14ns 31.29ns -89.96% 0 2 -100.00% 0 0 ~
MulDivOverflow/div192 3.17ns 30.28ns -89.54% 0 2 -100.00% 0 0 ~
MulDivOverflow/div256 3.54ns 35.56ns -90.06% 0 5 -100.00% 0 0 ~
Lt/small 3.12ns 10.43ns -70.06% 0 0 ~ 0 0 ~
Lt/large 3.49ns 10.32ns -66.18% 0 0 ~ 0 0 ~
Other
benchmark u256 time/op u256 B/op u256 allocs/op
SetBytes/generic 104.30ns 0 0
Sub/single/uint256_of 1.99ns 0 0
SetFromBig/overflow 3.57ns 0 0
ToBig/2words 73.84ns 64 2
SetBytes/specific 45.81ns 0 0
ScanScientific 674.10ns 0 0
SetFromBig/4words 3.82ns 0 0
ToBig/4words 68.82ns 64 2
RLPEncoding 11917.00ns 11911 255
SetFromBig/1word 2.99ns 0 0
SetFromBig/3words 4.12ns 0 0
ToBig/3words 70.12ns 64 2
ToBig/1word 75.38ns 64 2
MulMod/mod256/uint256r 77.11ns 0 0
HashTreeRoot 11.27ns 0 0
SetFromBig/2words 3.90ns 0 0

Helping out

If you're interested in low-level algorithms and/or doing optimizations for shaving off nanoseconds, then this is certainly for you!

Implementation work

Choose an operation, and optimize the s**t out of it!

A few rules, though, to help your PR get approved:

  • Do not optimize for 'best-case'/'most common case' at the expense of worst-case.
  • We'll hold off on go assembly for a while, until the algos and interfaces are finished in a 'good enough' first version. After that, it's assembly time.
Doing benchmarks

To do a simple benchmark for everything, do

go test -run - -bench . -benchmem

To see the difference between a branch and master, for a particular benchmark, do

git checkout master
go test -run - -bench Benchmark_Lsh -benchmem -count=10 > old.txt

git checkout opt_branch
go test -run - -bench Benchmark_Lsh -benchmem -count=10 > new.txt

benchstat old.txt new.txt

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyString      = errors.New("empty hex string")
	ErrSyntax           = errors.New("invalid hex string")
	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")
)

Functions

func Reciprocal added in v1.2.1

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

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

Types

type Int

type Int [4]uint64

Int is represented as an array of 4 uint64, in little-endian order, so that Int[3] is the most significant, and Int[0] is the least significant

func FromBig

func FromBig(b *big.Int) (*Int, bool)

FromBig is a convenience-constructor from big.Int. Returns a new Int and whether overflow occurred. OBS: If b is `nil`, this method returns `nil, false`

func FromDecimal added in v1.2.2

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

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

func FromHex added in v1.1.1

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

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

func MustFromBig added in v1.2.2

func MustFromBig(b *big.Int) *Int

MustFromBig is a convenience-constructor from big.Int. Returns a new Int and panics if overflow occurred. OBS: If b is `nil`, this method does _not_ panic, but instead returns `nil`

func MustFromDecimal added in v1.2.2

func MustFromDecimal(decimal string) *Int

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

func MustFromHex added in v1.2.2

func MustFromHex(hex string) *Int

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

func NewInt

func NewInt(val uint64) *Int

NewInt returns a new initialized Int.

func (*Int) Abs

func (z *Int) Abs(x *Int) *Int

Abs interprets x as a two's complement signed number, and sets z to the absolute value

Abs(0)        = 0
Abs(1)        = 1
Abs(2**255)   = -2**255
Abs(2**256-1) = -1

func (*Int) Add

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

Add sets z to the sum x+y

func (*Int) AddMod

func (z *Int) AddMod(x, y, m *Int) *Int

AddMod sets z to the sum ( x+y ) mod m, and returns z. If m == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Int) AddOverflow

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

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

func (*Int) AddUint64 added in v1.2.0

func (z *Int) AddUint64(x *Int, y uint64) *Int

AddUint64 sets z to x + y, where y is a uint64, and returns z

func (*Int) And

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

And sets z = x & y and returns z.

func (*Int) BitLen

func (z *Int) BitLen() int

BitLen returns the number of bits required to represent z

func (*Int) Byte

func (z *Int) Byte(n *Int) *Int

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, z is set to 0 Example: z=5, n=31 => 5

func (*Int) ByteLen

func (z *Int) ByteLen() int

ByteLen returns the number of bytes required to represent z

func (*Int) Bytes

func (z *Int) Bytes() []byte

Bytes returns the value of z as a big-endian byte slice.

func (*Int) Bytes20

func (z *Int) Bytes20() [20]byte

Bytes20 returns the value of z as a 20-byte big-endian array.

func (*Int) Bytes32

func (z *Int) Bytes32() [32]byte

Bytes32 returns the value of z as a 32-byte big-endian array.

func (*Int) Clear

func (z *Int) Clear() *Int

Clear sets z to 0

func (*Int) Clone

func (z *Int) Clone() *Int

Clone creates a new Int identical to z

func (*Int) Cmp

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

Cmp compares z and x and returns:

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

func (*Int) CmpBig added in v1.2.3

func (z *Int) CmpBig(x *big.Int) (r int)

CmpBig compares z and x and returns:

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

func (*Int) CmpUint64 added in v1.2.3

func (z *Int) CmpUint64(x uint64) int

CmpUint64 compares z and x and returns:

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

func (*Int) Dec added in v1.2.2

func (z *Int) Dec() string

Dec returns the decimal representation of z.

func (*Int) Div

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

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

func (*Int) DivMod added in v1.2.1

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

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 (*Int) EncodeRLP added in v1.1.0

func (z *Int) EncodeRLP(w io.Writer) error

EncodeRLP implements the rlp.Encoder interface from go-ethereum and writes the RLP encoding of z to w.

func (*Int) Eq

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

Eq returns true if z == x

func (*Int) Exp

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

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

func (*Int) ExtendSign added in v1.0.0

func (z *Int) ExtendSign(x, byteNum *Int) *Int

ExtendSign extends length of two’s complement signed integer, sets z to

  • x if byteNum > 30
  • x interpreted as a signed number with sign-bit at (byteNum*8+7), extended to the full 256 bits

and returns z.

func (*Int) Float64 added in v1.2.3

func (z *Int) Float64() float64

Float64 returns the float64 value nearest to x.

Note: The `big.Float` version of `Float64` also returns an 'Accuracy', indicating whether the value was too small or too large to be represented by a `float64`. However, the `uint256` type is unable to represent values out of scope (|x| < math.SmallestNonzeroFloat64 or |x| > math.MaxFloat64), therefore this method does not return any accuracy.

func (*Int) Format

func (z *Int) Format(s fmt.State, ch rune)

Format implements fmt.Formatter. It accepts the formats 'b' (binary), 'o' (octal with 0 prefix), 'O' (octal with 0o prefix), 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). Also supported are the full suite of package fmt's format flags for integral types, including '+' and ' ' for sign control, '#' for leading zero in octal and for hexadecimal, a leading "0x" or "0X" for "%#x" and "%#X" respectively, specification of minimum digits precision, output field width, space or zero padding, and '-' for left or right justification.

func (*Int) Gt

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

Gt returns true if z > x

func (*Int) GtUint64

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

GtUint64 returns true if z is larger than n

func (*Int) HashTreeRoot added in v1.2.2

func (z *Int) HashTreeRoot() ([32]byte, error)

HashTreeRoot implements the fastssz.HashRoot interface's non-dependent part.

func (*Int) Hex

func (z *Int) Hex() string

Hex encodes z in 0x-prefixed hexadecimal form.

func (*Int) IntoBig added in v1.3.1

func (z *Int) IntoBig(b **big.Int)

IntoBig sets a provided big.Int to the value of z. Sets `nil` if z is nil (thus the double pointer).

func (*Int) IsUint64

func (z *Int) IsUint64() bool

IsUint64 reports whether z can be represented as a uint64.

func (*Int) IsZero

func (z *Int) IsZero() bool

IsZero returns true if z == 0

func (*Int) Log10 added in v1.2.3

func (z *Int) Log10() uint

Log10 returns the log in base 10, floored to nearest integer. **OBS** This method returns '0' for '0', not `-Inf`.

func (*Int) Lsh

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

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

func (*Int) Lt

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

Lt returns true if z < x

func (*Int) LtUint64

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

LtUint64 returns true if z is smaller than n

func (*Int) MarshalJSON added in v1.2.2

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

MarshalJSON implements json.Marshaler. MarshalJSON marshals using the 'decimal string' representation. This is _not_ compatible with big.Int: big.Int 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.Int refuses to unmarshal a string representation).

func (*Int) MarshalSSZ added in v1.2.2

func (z *Int) MarshalSSZ() ([]byte, error)

MarshalSSZ implements the fastssz.Marshaler interface and returns the integer marshalled into a newly allocated byte slice.

func (*Int) MarshalSSZAppend added in v1.3.0

func (z *Int) MarshalSSZAppend(dst []byte) ([]byte, error)

MarshalSSZAppend _almost_ implements the fastssz.Marshaler (see below) interface. Originally, this method was named `MarshalSSZTo`, and ostensibly implemented the interface. However, it was noted (https://github.com/holiman/uint256/issues/170) that the actual implementation did not match the intended semantics of the interface: it inserted the data instead of appending.

Therefore, the `MarshalSSZTo` has been removed: to force users into making a choice:

  • Use `MarshalSSZAppend`: this is the method that appends to the destination buffer, and returns a potentially newly allocated buffer. This method will become `MarshalSSZTo` in some future release.
  • Or use `MarshalSSZInto`: this is the original method which places the data into the destination buffer, without ever reallocating.

fastssz.Marshaler interface:

 https://github.com/ferranbt/fastssz/blob/main/interface.go#L4
	type Marshaler interface {
		MarshalSSZTo(dst []byte) ([]byte, error)
		MarshalSSZ() ([]byte, error)
		SizeSSZ() int
	}

func (*Int) MarshalSSZInto added in v1.3.0

func (z *Int) MarshalSSZInto(dst []byte) ([]byte, error)

MarshalSSZInto is the first attempt to implement the fastssz.Marshaler interface, but which does not obey the intended semantics. See MarshalSSZAppend and - https://github.com/holiman/uint256/pull/171 - https://github.com/holiman/uint256/issues/170 @deprecated

func (*Int) MarshalText added in v1.1.1

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

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

func (*Int) Mod

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

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

func (*Int) Mul

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

Mul sets z to the product x*y

func (*Int) MulDivOverflow added in v1.2.1

func (z *Int) MulDivOverflow(x, y, d *Int) (*Int, bool)

MulDivOverflow calculates (x*y)/d with full precision, returns z and whether overflow occurred in multiply process (result does not fit to 256-bit). computes 512-bit multiplication and 512 by 256 division.

func (*Int) MulMod

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

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 (*Int) MulModWithReciprocal added in v1.2.1

func (z *Int) MulModWithReciprocal(x, y, m *Int, mu *[5]uint64) *Int

MulModWithReciprocal calculates the modulo-m multiplication of x and y and returns z, using the reciprocal of m provided as the mu parameter. Use uint256.Reciprocal to calculate mu from m. If m == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Int) MulOverflow added in v1.1.0

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

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

func (*Int) Neg

func (z *Int) Neg(x *Int) *Int

Neg returns -x mod 2**256.

func (*Int) Not

func (z *Int) Not(x *Int) *Int

Not sets z = ^x and returns z.

func (*Int) Or

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

Or sets z = x | y and returns z.

func (*Int) PaddedBytes

func (z *Int) PaddedBytes(n int) []byte

PaddedBytes encodes a Int as a 0-padded byte slice. The length of the slice is at least n bytes. Example, z =1, n = 20 => [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]

func (*Int) PrettyDec added in v1.2.2

func (z *Int) PrettyDec(separator byte) string

PrettyDec returns the decimal representation of z, with thousands-separators.

func (*Int) PutUint256 added in v1.3.2

func (z *Int) PutUint256(dest []byte)

PutUint256 writes all 32 bytes of z to the destination slice, including zero-bytes. If dest is larger than 32 bytes, z will fill the first parts, and leave the end untouched. Note: The dest slice must be at least 32 bytes large, otherwise this method will panic. The method WriteToSlice, which is slower, should be used if the destination slice is smaller or of unknown size.

func (*Int) Rsh

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

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

func (*Int) SDiv added in v1.0.0

func (z *Int) SDiv(n, d *Int) *Int

SDiv interprets n and d as two's complement signed integers, does a signed division on the two operands and sets z to the result. If d == 0, z is set to 0

func (*Int) SMod added in v1.0.0

func (z *Int) SMod(x, y *Int) *Int

SMod interprets x and y as two's complement signed integers, sets z to (sign x) * { abs(x) modulus abs(y) } If y == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Int) SRsh added in v1.0.0

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

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

func (*Int) Scan added in v1.2.2

func (dst *Int) Scan(src interface{}) error

Scan implements the database/sql Scanner interface. It decodes a string, because that is what postgres uses for its numeric type

func (*Int) Set added in v1.0.0

func (z *Int) Set(x *Int) *Int

Set sets z to x and returns z.

func (*Int) SetAllOne

func (z *Int) SetAllOne() *Int

SetAllOne sets all the bits of z to 1

func (*Int) SetBytes

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

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. This operation is semantically equivalent to `FromBig(new(big.Int).SetBytes(buf))`

func (*Int) SetBytes1 added in v1.1.0

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

func (*Int) SetBytes10 added in v1.1.0

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

func (*Int) SetBytes11 added in v1.1.0

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

func (*Int) SetBytes12 added in v1.1.0

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

func (*Int) SetBytes13 added in v1.1.0

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

func (*Int) SetBytes14 added in v1.1.0

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

func (*Int) SetBytes15 added in v1.1.0

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

func (*Int) SetBytes16 added in v1.1.0

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

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

func (*Int) SetBytes17 added in v1.1.0

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

func (*Int) SetBytes18 added in v1.1.0

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

func (*Int) SetBytes19 added in v1.1.0

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

func (*Int) SetBytes2 added in v1.1.0

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

func (*Int) SetBytes20 added in v1.1.0

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

func (*Int) SetBytes21 added in v1.1.0

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

func (*Int) SetBytes22 added in v1.1.0

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

func (*Int) SetBytes23 added in v1.1.0

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

func (*Int) SetBytes24 added in v1.1.0

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

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

func (*Int) SetBytes25 added in v1.1.0

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

func (*Int) SetBytes26 added in v1.1.0

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

func (*Int) SetBytes27 added in v1.1.0

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

func (*Int) SetBytes28 added in v1.1.0

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

func (*Int) SetBytes29 added in v1.1.0

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

func (*Int) SetBytes3 added in v1.1.0

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

func (*Int) SetBytes30 added in v1.1.0

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

func (*Int) SetBytes31 added in v1.1.0

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

func (*Int) SetBytes32 added in v1.1.0

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

func (*Int) SetBytes4 added in v1.1.0

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

func (*Int) SetBytes5 added in v1.1.0

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

func (*Int) SetBytes6 added in v1.1.0

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

func (*Int) SetBytes7 added in v1.1.0

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

func (*Int) SetBytes8 added in v1.1.0

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

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

func (*Int) SetBytes9 added in v1.1.0

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

func (*Int) SetFromBig

func (z *Int) SetFromBig(b *big.Int) bool

SetFromBig converts a big.Int to Int and sets the value to z. TODO: Ensure we have sufficient testing, esp for negative bigints.

func (*Int) SetFromDecimal added in v1.2.2

func (z *Int) 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.Int).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 (*Int) SetFromHex added in v1.2.2

func (z *Int) 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 (*Int) SetOne

func (z *Int) SetOne() *Int

SetOne sets z to 1

func (*Int) SetUint64

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

SetUint64 sets z to the value x

func (*Int) Sgt

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

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

func (*Int) Sign

func (z *Int) 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 (*Int) SizeSSZ added in v1.2.2

func (*Int) SizeSSZ() int

SizeSSZ implements the fastssz.Marshaler interface and returns the byte size of the 256 bit int.

func (*Int) Slt

func (z *Int) Slt(x *Int) bool

Slt interprets z and x as signed integers, and returns true if z < x

func (*Int) Sqrt added in v1.2.1

func (z *Int) Sqrt(x *Int) *Int

Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z.

func (*Int) String added in v1.1.1

func (z *Int) String() string

String returns the decimal encoding of b.

func (*Int) Sub

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

Sub sets z to the difference x-y

func (*Int) SubOverflow

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

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

func (*Int) SubUint64 added in v1.0.0

func (z *Int) SubUint64(x *Int, y uint64) *Int

SubUint64 set z to the difference x - y, where y is a uint64, and returns z

func (*Int) ToBig

func (z *Int) ToBig() *big.Int

ToBig returns a big.Int version of z. Return `nil` if z is nil

func (*Int) Uint64

func (z *Int) Uint64() uint64

Uint64 returns the lower 64-bits of z

func (*Int) Uint64WithOverflow

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

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

func (*Int) UnmarshalJSON added in v1.1.1

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

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

func (*Int) UnmarshalSSZ added in v1.2.2

func (z *Int) UnmarshalSSZ(buf []byte) error

UnmarshalSSZ implements the fastssz.Unmarshaler interface and parses an encoded integer into the local struct.

func (*Int) UnmarshalText added in v1.1.1

func (z *Int) 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 (*Int) Value added in v1.2.2

func (src *Int) Value() (driver.Value, error)

Value implements the database/sql/driver Valuer interface. It encodes a base 10 string. In Postgres, this will work with both integer and the Numeric/Decimal types In MariaDB/MySQL, this will work with the Numeric/Decimal types up to 65 digits, however any more and you should use either VarChar or Char(79) In SqLite, use TEXT

func (*Int) WriteToArray20

func (z *Int) WriteToArray20(dest *[20]byte)

WriteToArray20 writes the last 20 bytes of z to the destination array, including zero-bytes

func (*Int) WriteToArray32

func (z *Int) WriteToArray32(dest *[32]byte)

WriteToArray32 writes all 32 bytes of z to the destination array, including zero-bytes

func (*Int) WriteToSlice

func (z *Int) WriteToSlice(dest []byte)

WriteToSlice writes the content of z into the given byteslice. If dest is larger than 32 bytes, z will fill the first parts, and leave the end untouched. OBS! If dest is smaller than 32 bytes, only the end parts of z will be used for filling the array, making it useful for filling an Address object

func (*Int) Xor

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

Xor sets z = x ^ y and returns z.

Jump to

Keyboard shortcuts

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