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
- Variables
- func Hello() uint64
- func Reciprocal(m *Uint) (mu [5]uint64)
- type Uint
- func (z *Uint) Add(x, y *Uint) *Uint
- func (z *Uint) AddOverflow(x, y *Uint) (*Uint, bool)
- func (z *Uint) And(x, y *Uint) *Uint
- func (z *Uint) AndNot(x, y *Uint) *Uint
- func (z *Uint) BitLen() int
- func (z *Uint) Byte(n *Uint) *Uint
- func (z *Uint) ByteLen() int
- func (z *Uint) Clear() *Uint
- func (z *Uint) Clone() *Uint
- func (z *Uint) Cmp(x *Uint) (r int)
- func (z *Uint) Dec() string
- func (z *Uint) Div(x, y *Uint) *Uint
- func (z *Uint) DivMod(x, y, m *Uint) (*Uint, *Uint)
- func (z *Uint) Eq(x *Uint) bool
- func (z *Uint) Exp(base, exponent *Uint) *Uint
- func (z *Uint) Gt(x *Uint) bool
- func (z *Uint) GtUint64(n uint64) bool
- func (z *Uint) Gte(x *Uint) bool
- func (z *Uint) IsUint64() bool
- func (z *Uint) IsZero() bool
- func (z *Uint) Lsh(x *Uint, n uint) *Uint
- func (z *Uint) Lt(x *Uint) bool
- func (z *Uint) LtUint64(n uint64) bool
- func (z *Uint) Lte(x *Uint) bool
- func (z *Uint) MarshalJSON() ([]byte, error)
- func (z *Uint) MarshalText() ([]byte, error)
- func (z *Uint) Mod(x, y *Uint) *Uint
- func (z *Uint) Mul(x, y *Uint) *Uint
- func (z *Uint) MulMod(x, y, m *Uint) *Uint
- func (z *Uint) MulOverflow(x, y *Uint) (*Uint, bool)
- func (z *Uint) Neg(x *Uint) *Uint
- func (z *Uint) Neq(x *Uint) bool
- func (z *Uint) Not(x *Uint) *Uint
- func (z *Uint) Or(x, y *Uint) *Uint
- func (z *Uint) Rsh(x *Uint, n uint) *Uint
- func (z *Uint) SRsh(x *Uint, n uint) *Uint
- func (z *Uint) Scan(src interface{}) error
- func (z *Uint) Set(x *Uint) *Uint
- func (z *Uint) SetAllOne() *Uint
- func (z *Uint) SetBytes(buf []byte) *Uint
- func (z *Uint) SetBytes1(in []byte) *Uint
- func (z *Uint) SetBytes2(in []byte) *Uint
- func (z *Uint) SetBytes3(in []byte) *Uint
- func (z *Uint) SetBytes4(in []byte) *Uint
- func (z *Uint) SetBytes5(in []byte) *Uint
- func (z *Uint) SetBytes6(in []byte) *Uint
- func (z *Uint) SetBytes7(in []byte) *Uint
- func (z *Uint) SetBytes8(in []byte) *Uint
- func (z *Uint) SetBytes9(in []byte) *Uint
- func (z *Uint) SetBytes10(in []byte) *Uint
- func (z *Uint) SetBytes11(in []byte) *Uint
- func (z *Uint) SetBytes12(in []byte) *Uint
- func (z *Uint) SetBytes13(in []byte) *Uint
- func (z *Uint) SetBytes14(in []byte) *Uint
- func (z *Uint) SetBytes15(in []byte) *Uint
- func (z *Uint) SetBytes16(in []byte) *Uint
- func (z *Uint) SetBytes17(in []byte) *Uint
- func (z *Uint) SetBytes18(in []byte) *Uint
- func (z *Uint) SetBytes19(in []byte) *Uint
- func (z *Uint) SetBytes20(in []byte) *Uint
- func (z *Uint) SetBytes21(in []byte) *Uint
- func (z *Uint) SetBytes22(in []byte) *Uint
- func (z *Uint) SetBytes23(in []byte) *Uint
- func (z *Uint) SetBytes24(in []byte) *Uint
- func (z *Uint) SetBytes25(in []byte) *Uint
- func (z *Uint) SetBytes26(in []byte) *Uint
- func (z *Uint) SetBytes27(in []byte) *Uint
- func (z *Uint) SetBytes28(in []byte) *Uint
- func (z *Uint) SetBytes29(in []byte) *Uint
- func (z *Uint) SetBytes30(in []byte) *Uint
- func (z *Uint) SetBytes31(in []byte) *Uint
- func (z *Uint) SetBytes32(in []byte) *Uint
- func (z *Uint) SetFromDecimal(s string) (err error)
- func (z *Uint) SetFromHex(hex string) error
- func (z *Uint) SetOne() *Uint
- func (z *Uint) SetUint64(x uint64) *Uint
- func (z *Uint) Sgt(x *Uint) bool
- func (z *Uint) Sign() int
- func (z *Uint) Sub(x, y *Uint) *Uint
- func (z *Uint) SubOverflow(x, y *Uint) (*Uint, bool)
- func (z *Uint) ToString() string
- func (z *Uint) Uint64() uint64
- func (z *Uint) Uint64WithOverflow() (uint64, bool)
- func (z *Uint) UnmarshalJSON(input []byte) error
- func (z *Uint) UnmarshalText(input []byte) error
- func (z *Uint) Xor(x, y *Uint) *Uint
Constants ¶
const (
MaxUint64 = 1<<64 - 1
)
Variables ¶
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 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
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 ¶
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 (*Uint) AddOverflow ¶
AddOverflow sets z to the sum x+y, and returns z and whether overflow occurred
func (*Uint) Byte ¶
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) Div ¶
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 ¶
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) MarshalJSON ¶
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 ¶
MarshalText implements encoding.TextMarshaler MarshalText marshals using the decimal representation (compatible with big.Uint)
func (*Uint) Mod ¶
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) MulMod ¶
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 ¶
MulOverflow sets z to the product x*y, and returns z and whether overflow occurred
func (*Uint) SRsh ¶
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) SetBytes ¶
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 ¶
SetBytes1 is identical to SetBytes(in[:1]), but panics is input is too short
func (*Uint) SetBytes2 ¶
SetBytes2 is identical to SetBytes(in[:2]), but panics is input is too short
func (*Uint) SetBytes3 ¶
SetBytes3 is identical to SetBytes(in[:3]), but panics is input is too short
func (*Uint) SetBytes4 ¶
SetBytes4 is identical to SetBytes(in[:4]), but panics is input is too short
func (*Uint) SetBytes5 ¶
SetBytes5 is identical to SetBytes(in[:5]), but panics is input is too short
func (*Uint) SetBytes6 ¶
SetBytes6 is identical to SetBytes(in[:6]), but panics is input is too short
func (*Uint) SetBytes7 ¶
SetBytes7 is identical to SetBytes(in[:7]), but panics is input is too short
func (*Uint) SetBytes8 ¶
SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short
func (*Uint) SetBytes9 ¶
SetBytes9 is identical to SetBytes(in[:9]), but panics is input is too short
func (*Uint) SetBytes10 ¶
SetBytes10 is identical to SetBytes(in[:10]), but panics is input is too short
func (*Uint) SetBytes11 ¶
SetBytes11 is identical to SetBytes(in[:11]), but panics is input is too short
func (*Uint) SetBytes12 ¶
SetBytes12 is identical to SetBytes(in[:12]), but panics is input is too short
func (*Uint) SetBytes13 ¶
SetBytes13 is identical to SetBytes(in[:13]), but panics is input is too short
func (*Uint) SetBytes14 ¶
SetBytes14 is identical to SetBytes(in[:14]), but panics is input is too short
func (*Uint) SetBytes15 ¶
SetBytes15 is identical to SetBytes(in[:15]), but panics is input is too short
func (*Uint) SetBytes16 ¶
SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short
func (*Uint) SetBytes17 ¶
SetBytes17 is identical to SetBytes(in[:17]), but panics is input is too short
func (*Uint) SetBytes18 ¶
SetBytes18 is identical to SetBytes(in[:18]), but panics is input is too short
func (*Uint) SetBytes19 ¶
SetBytes19 is identical to SetBytes(in[:19]), but panics is input is too short
func (*Uint) SetBytes20 ¶
SetBytes20 is identical to SetBytes(in[:20]), but panics is input is too short
func (*Uint) SetBytes21 ¶
SetBytes21 is identical to SetBytes(in[:21]), but panics is input is too short
func (*Uint) SetBytes22 ¶
SetBytes22 is identical to SetBytes(in[:22]), but panics is input is too short
func (*Uint) SetBytes23 ¶
SetBytes23 is identical to SetBytes(in[:23]), but panics is input is too short
func (*Uint) SetBytes24 ¶
SetBytes24 is identical to SetBytes(in[:24]), but panics is input is too short
func (*Uint) SetBytes25 ¶
SetBytes25 is identical to SetBytes(in[:25]), but panics is input is too short
func (*Uint) SetBytes26 ¶
SetBytes26 is identical to SetBytes(in[:26]), but panics is input is too short
func (*Uint) SetBytes27 ¶
SetBytes27 is identical to SetBytes(in[:27]), but panics is input is too short
func (*Uint) SetBytes28 ¶
SetBytes28 is identical to SetBytes(in[:28]), but panics is input is too short
func (*Uint) SetBytes29 ¶
SetBytes29 is identical to SetBytes(in[:29]), but panics is input is too short
func (*Uint) SetBytes30 ¶
SetBytes30 is identical to SetBytes(in[:30]), but panics is input is too short
func (*Uint) SetBytes31 ¶
SetBytes31 is identical to SetBytes(in[:31]), but panics is input is too short
func (*Uint) SetBytes32 ¶
SetBytes32 sets z to the value of the big-endian 256-bit unsigned integer in.
func (*Uint) SetFromDecimal ¶
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 ¶
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) Sign ¶
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) SubOverflow ¶
SubOverflow sets z to the difference x-y and returns z and true if the operation underflowed
func (*Uint) ToString ¶
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) Uint64WithOverflow ¶
Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either - Quoted string: either hexadecimal OR decimal - Not quoted string: only decimal
func (*Uint) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. This method can unmarshal either hexadecimal or decimal. - For hexadecimal, the input _must_ be prefixed with 0x or 0X