uint256 source pure
arithmetic provides arithmetic operations for Uint objects. This includes basic binary operations such as addition, s...
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
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.
1
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)8
func Reciprocal
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
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
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
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
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
NewUint returns a new initialized Uint.
func One
One returns a new Uint initialized to one.
func Zero
Zero returns a new Uint initialized to zero.
1
type Uint
structUint 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 UintAdd sets z to the sum x+y
func AddOverflow
method on UintAddOverflow sets z to the sum x+y, and returns z and whether overflow occurred
func And
method on UintAnd sets z = x & y and returns z.
func AndNot
method on UintAndNot sets z = x &^ y and returns z.
func BitLen
method on UintBitLen returns the number of bits required to represent z
func Byte
method on UintByte 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 UintByteLen returns the number of bytes required to represent z
func Clear
method on UintClear sets z to 0
func Clone
method on UintClone creates a new Uint identical to z
func Cmp
method on Uintfunc Dec
method on UintDec returns the decimal representation of z.
func Div
method on Uintcommented 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 UintDivMod 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 UintEq returns true if z == x
func Exp
method on UintExp sets z = base**exponent mod 2**256, and returns z.
func Gt
method on UintGt returns true if z > x
func GtUint64
method on UintGtUint64 returns true if z is larger than n
func Gte
method on UintGte returns true if z >= x
func IsUint64
method on UintIsUint64 reports whether z can be represented as a uint64.
func IsZero
method on UintIsZero returns true if z == 0
func Lsh
method on UintLsh sets z = x << n and returns z.
func Lt
method on UintLt returns true if z < x
func LtUint64
method on UintLtUint64 returns true if z is smaller than n
func Lte
method on UintLte returns true if z <= x
func MarshalJSON
method on UintMarshalJSON 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 UintMarshalText implements encoding.TextMarshaler MarshalText marshals using the decimal representation (compatible with big.Uint)
func Mod
method on UintMod 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 Uintcommented out for possible overflow Mul sets z to the product x*y
func MulMod
method on UintMulMod 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 UintMulOverflow sets z to the product x*y, and returns z and whether overflow occurred
func Neg
method on UintNeg returns -x mod 2^256.
func Neq
method on UintNeq returns true if z != x
func Not
method on UintNot sets z = ^x and returns z.
func Or
method on UintOr sets z = x | y and returns z.
func Rsh
method on UintRsh sets z = x >> n and returns z.
func SRsh
method on UintSRsh (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 Uintfunc Set
method on UintSet sets z to x and returns z.
func SetAllOne
method on UintSetAllOne sets all the bits of z to 1
func SetBytes
method on UintSetBytes 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 UintSetBytes1 is identical to SetBytes(in[:1]), but panics is input is too short
func SetBytes10
method on UintSetBytes10 is identical to SetBytes(in[:10]), but panics is input is too short
func SetBytes11
method on UintSetBytes11 is identical to SetBytes(in[:11]), but panics is input is too short
func SetBytes12
method on UintSetBytes12 is identical to SetBytes(in[:12]), but panics is input is too short
func SetBytes13
method on UintSetBytes13 is identical to SetBytes(in[:13]), but panics is input is too short
func SetBytes14
method on UintSetBytes14 is identical to SetBytes(in[:14]), but panics is input is too short
func SetBytes15
method on UintSetBytes15 is identical to SetBytes(in[:15]), but panics is input is too short
func SetBytes16
method on UintSetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short
func SetBytes17
method on UintSetBytes17 is identical to SetBytes(in[:17]), but panics is input is too short
func SetBytes18
method on UintSetBytes18 is identical to SetBytes(in[:18]), but panics is input is too short
func SetBytes19
method on UintSetBytes19 is identical to SetBytes(in[:19]), but panics is input is too short
func SetBytes2
method on UintSetBytes2 is identical to SetBytes(in[:2]), but panics is input is too short
func SetBytes20
method on UintSetBytes20 is identical to SetBytes(in[:20]), but panics is input is too short
func SetBytes21
method on UintSetBytes21 is identical to SetBytes(in[:21]), but panics is input is too short
func SetBytes22
method on UintSetBytes22 is identical to SetBytes(in[:22]), but panics is input is too short
func SetBytes23
method on UintSetBytes23 is identical to SetBytes(in[:23]), but panics is input is too short
func SetBytes24
method on UintSetBytes24 is identical to SetBytes(in[:24]), but panics is input is too short
func SetBytes25
method on UintSetBytes25 is identical to SetBytes(in[:25]), but panics is input is too short
func SetBytes26
method on UintSetBytes26 is identical to SetBytes(in[:26]), but panics is input is too short
func SetBytes27
method on UintSetBytes27 is identical to SetBytes(in[:27]), but panics is input is too short
func SetBytes28
method on UintSetBytes28 is identical to SetBytes(in[:28]), but panics is input is too short
func SetBytes29
method on UintSetBytes29 is identical to SetBytes(in[:29]), but panics is input is too short
func SetBytes3
method on UintSetBytes3 is identical to SetBytes(in[:3]), but panics is input is too short
func SetBytes30
method on UintSetBytes30 is identical to SetBytes(in[:30]), but panics is input is too short
func SetBytes31
method on UintSetBytes31 is identical to SetBytes(in[:31]), but panics is input is too short
func SetBytes32
method on UintSetBytes32 sets z to the value of the big-endian 256-bit unsigned integer in.
func SetBytes4
method on UintSetBytes4 is identical to SetBytes(in[:4]), but panics is input is too short
func SetBytes5
method on UintSetBytes5 is identical to SetBytes(in[:5]), but panics is input is too short
func SetBytes6
method on UintSetBytes6 is identical to SetBytes(in[:6]), but panics is input is too short
func SetBytes7
method on UintSetBytes7 is identical to SetBytes(in[:7]), but panics is input is too short
func SetBytes8
method on UintSetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short
func SetBytes9
method on UintSetBytes9 is identical to SetBytes(in[:9]), but panics is input is too short
func SetFromDecimal
method on UintSetFromDecimal 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 UintSetFromHex 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 UintSetOne sets z to 1
func SetUint64
method on UintSetUint64 sets z to the value x
func Sgt
method on UintSgt interprets z and x as signed integers, and returns true if z > x
func Sign
method on Uintfunc String
method on UintToString 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 UintSub sets z to the difference x-y
func SubOverflow
method on UintSubOverflow sets z to the difference x-y and returns z and true if the operation underflowed
func Uint64
method on UintUint64 returns the lower 64-bits of z
func Uint64WithOverflow
method on UintUint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred
func UnmarshalJSON
method on UintUnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either - Quoted string: either hexadecimal OR decimal - Not quoted string: only decimal
func UnmarshalText
method on UintUnmarshalText 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 UintXor sets z = x ^ y and returns z.
5
- encoding/binary stdlib
- errors stdlib
- math/bits stdlib
- strconv stdlib
- strings stdlib