Documentation ¶
Index ¶
- Variables
- func Reciprocal(m *Int) (mu [5]uint64)
- type Int
- func (z *Int) Abs(x *Int) *Int
- func (z *Int) Add(x, y *Int) *Int
- func (z *Int) AddMod(x, y, m *Int) *Int
- func (z *Int) AddOverflow(x, y *Int) (*Int, bool)
- func (z *Int) AddUint64(x *Int, y uint64) *Int
- func (z *Int) And(x, y *Int) *Int
- func (z *Int) BitLen() int
- func (z *Int) Byte(n *Int) *Int
- func (z *Int) ByteLen() int
- func (z *Int) Bytes() []byte
- func (z *Int) Bytes20() [20]byte
- func (z *Int) Bytes32() [32]byte
- func (z *Int) Clear() *Int
- func (z *Int) Clone() *Int
- func (z *Int) Cmp(x *Int) (r int)
- func (z *Int) CmpBig(x *big.Int) (r int)
- func (z *Int) CmpUint64(x uint64) int
- func (z *Int) Dec() string
- func (z *Int) Div(x, y *Int) *Int
- func (z *Int) DivMod(x, y, m *Int) (*Int, *Int)
- func (z *Int) EncodeRLP(w io.Writer) error
- func (z *Int) Eq(x *Int) bool
- func (z *Int) Exp(base, exponent *Int) *Int
- func (z *Int) ExtendSign(x, byteNum *Int) *Int
- func (z *Int) Float64() float64
- func (z *Int) Format(s fmt.State, ch rune)
- func (z *Int) Gt(x *Int) bool
- func (z *Int) GtUint64(n uint64) bool
- func (z *Int) HashTreeRoot() ([32]byte, error)
- func (z *Int) Hex() string
- func (z *Int) IntoBig(b **big.Int)
- func (z *Int) IsUint64() bool
- func (z *Int) IsZero() bool
- func (z *Int) Log10() uint
- func (z *Int) Lsh(x *Int, n uint) *Int
- func (z *Int) Lt(x *Int) bool
- func (z *Int) LtUint64(n uint64) bool
- func (z *Int) MarshalJSON() ([]byte, error)
- func (z *Int) MarshalSSZ() ([]byte, error)
- func (z *Int) MarshalSSZAppend(dst []byte) ([]byte, error)
- func (z *Int) MarshalSSZInto(dst []byte) ([]byte, error)
- func (z *Int) MarshalText() ([]byte, error)
- func (z *Int) Mod(x, y *Int) *Int
- func (z *Int) Mul(x, y *Int) *Int
- func (z *Int) MulDivOverflow(x, y, d *Int) (*Int, bool)
- func (z *Int) MulMod(x, y, m *Int) *Int
- func (z *Int) MulModWithReciprocal(x, y, m *Int, mu *[5]uint64) *Int
- func (z *Int) MulOverflow(x, y *Int) (*Int, bool)
- func (z *Int) Neg(x *Int) *Int
- func (z *Int) Not(x *Int) *Int
- func (z *Int) Or(x, y *Int) *Int
- func (z *Int) PaddedBytes(n int) []byte
- func (z *Int) PrettyDec(separator byte) string
- func (z *Int) PutUint256(dest []byte)
- func (z *Int) Rsh(x *Int, n uint) *Int
- func (z *Int) SDiv(n, d *Int) *Int
- func (z *Int) SMod(x, y *Int) *Int
- func (z *Int) SRsh(x *Int, n uint) *Int
- func (dst *Int) Scan(src interface{}) error
- func (z *Int) Set(x *Int) *Int
- func (z *Int) SetAllOne() *Int
- func (z *Int) SetBytes(buf []byte) *Int
- func (z *Int) SetBytes1(in []byte) *Int
- func (z *Int) SetBytes10(in []byte) *Int
- func (z *Int) SetBytes11(in []byte) *Int
- func (z *Int) SetBytes12(in []byte) *Int
- func (z *Int) SetBytes13(in []byte) *Int
- func (z *Int) SetBytes14(in []byte) *Int
- func (z *Int) SetBytes15(in []byte) *Int
- func (z *Int) SetBytes16(in []byte) *Int
- func (z *Int) SetBytes17(in []byte) *Int
- func (z *Int) SetBytes18(in []byte) *Int
- func (z *Int) SetBytes19(in []byte) *Int
- func (z *Int) SetBytes2(in []byte) *Int
- func (z *Int) SetBytes20(in []byte) *Int
- func (z *Int) SetBytes21(in []byte) *Int
- func (z *Int) SetBytes22(in []byte) *Int
- func (z *Int) SetBytes23(in []byte) *Int
- func (z *Int) SetBytes24(in []byte) *Int
- func (z *Int) SetBytes25(in []byte) *Int
- func (z *Int) SetBytes26(in []byte) *Int
- func (z *Int) SetBytes27(in []byte) *Int
- func (z *Int) SetBytes28(in []byte) *Int
- func (z *Int) SetBytes29(in []byte) *Int
- func (z *Int) SetBytes3(in []byte) *Int
- func (z *Int) SetBytes30(in []byte) *Int
- func (z *Int) SetBytes31(in []byte) *Int
- func (z *Int) SetBytes32(in []byte) *Int
- func (z *Int) SetBytes4(in []byte) *Int
- func (z *Int) SetBytes5(in []byte) *Int
- func (z *Int) SetBytes6(in []byte) *Int
- func (z *Int) SetBytes7(in []byte) *Int
- func (z *Int) SetBytes8(in []byte) *Int
- func (z *Int) SetBytes9(in []byte) *Int
- func (z *Int) SetFromBig(b *big.Int) bool
- func (z *Int) SetFromDecimal(s string) (err error)
- func (z *Int) SetFromHex(hex string) error
- func (z *Int) SetOne() *Int
- func (z *Int) SetUint64(x uint64) *Int
- func (z *Int) Sgt(x *Int) bool
- func (z *Int) Sign() int
- func (*Int) SizeSSZ() int
- func (z *Int) Slt(x *Int) bool
- func (z *Int) Sqrt(x *Int) *Int
- func (z *Int) String() string
- func (z *Int) Sub(x, y *Int) *Int
- func (z *Int) SubOverflow(x, y *Int) (*Int, bool)
- func (z *Int) SubUint64(x *Int, y uint64) *Int
- func (z *Int) ToBig() *big.Int
- func (z *Int) Uint64() uint64
- func (z *Int) Uint64WithOverflow() (uint64, bool)
- func (z *Int) UnmarshalJSON(input []byte) error
- func (z *Int) UnmarshalSSZ(buf []byte) error
- func (z *Int) UnmarshalText(input []byte) error
- func (src *Int) Value() (driver.Value, error)
- func (z *Int) WriteToArray20(dest *[20]byte)
- func (z *Int) WriteToArray32(dest *[32]byte)
- func (z *Int) WriteToSlice(dest []byte)
- func (z *Int) Xor(x, y *Int) *Int
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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
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
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
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
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
MustFromHex is a convenience-constructor to create an Int from a hexadecimal string. Returns a new Int and panics if any error occurred.
func (*Int) Abs ¶
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) AddMod ¶
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 ¶
AddOverflow sets z to the sum x+y, and returns z and whether overflow occurred
func (*Int) AddUint64 ¶ added in v1.2.0
AddUint64 sets z to x + y, where y is a uint64, and returns z
func (*Int) 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, z is set to 0 Example: z=5, n=31 => 5
func (*Int) CmpBig ¶ added in v1.2.3
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
CmpUint64 compares z and x and returns:
-1 if z < x 0 if z == x +1 if z > x
func (*Int) DivMod ¶ added in v1.2.1
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
EncodeRLP implements the rlp.Encoder interface from go-ethereum and writes the RLP encoding of z to w.
func (*Int) ExtendSign ¶ added in v1.0.0
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
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 ¶
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) HashTreeRoot ¶ added in v1.2.2
HashTreeRoot implements the fastssz.HashRoot interface's non-dependent part.
func (*Int) IntoBig ¶ added in v1.3.1
IntoBig sets a provided big.Int to the value of z. Sets `nil` if z is nil (thus the double pointer).
func (*Int) Log10 ¶ added in v1.2.3
Log10 returns the log in base 10, floored to nearest integer. **OBS** This method returns '0' for '0', not `-Inf`.
func (*Int) MarshalJSON ¶ added in v1.2.2
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
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
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
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
MarshalText implements encoding.TextMarshaler MarshalText marshals using the decimal representation (compatible with big.Int)
func (*Int) 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.Int)
func (*Int) MulDivOverflow ¶ added in v1.2.1
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 ¶
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
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
MulOverflow sets z to the product x*y, and returns z and whether overflow occurred
func (*Int) PaddedBytes ¶
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
PrettyDec returns the decimal representation of z, with thousands-separators.
func (*Int) PutUint256 ¶ added in v1.3.2
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) SDiv ¶ added in v1.0.0
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
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
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
Scan implements the database/sql Scanner interface. It decodes a string, because that is what postgres uses for its numeric type
func (*Int) 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. This operation is semantically equivalent to `FromBig(new(big.Int).SetBytes(buf))`
func (*Int) SetBytes10 ¶ added in v1.1.0
func (*Int) SetBytes11 ¶ added in v1.1.0
func (*Int) SetBytes12 ¶ added in v1.1.0
func (*Int) SetBytes13 ¶ added in v1.1.0
func (*Int) SetBytes14 ¶ added in v1.1.0
func (*Int) SetBytes15 ¶ added in v1.1.0
func (*Int) SetBytes16 ¶ added in v1.1.0
SetBytes16 is identical to SetBytes(in[:16]), but panics is input is too short
func (*Int) SetBytes17 ¶ added in v1.1.0
func (*Int) SetBytes18 ¶ added in v1.1.0
func (*Int) SetBytes19 ¶ added in v1.1.0
func (*Int) SetBytes20 ¶ added in v1.1.0
func (*Int) SetBytes21 ¶ added in v1.1.0
func (*Int) SetBytes22 ¶ added in v1.1.0
func (*Int) SetBytes23 ¶ added in v1.1.0
func (*Int) SetBytes24 ¶ added in v1.1.0
SetBytes16 is identical to SetBytes(in[:24]), but panics is input is too short
func (*Int) SetBytes25 ¶ added in v1.1.0
func (*Int) SetBytes26 ¶ added in v1.1.0
func (*Int) SetBytes27 ¶ added in v1.1.0
func (*Int) SetBytes28 ¶ added in v1.1.0
func (*Int) SetBytes29 ¶ added in v1.1.0
func (*Int) SetBytes30 ¶ added in v1.1.0
func (*Int) SetBytes31 ¶ added in v1.1.0
func (*Int) SetBytes32 ¶ added in v1.1.0
func (*Int) SetBytes8 ¶ added in v1.1.0
SetBytes8 is identical to SetBytes(in[:8]), but panics is input is too short
func (*Int) SetFromBig ¶
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
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
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) 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 (*Int) SizeSSZ ¶ added in v1.2.2
SizeSSZ implements the fastssz.Marshaler interface and returns the byte size of the 256 bit int.
func (*Int) Sqrt ¶ added in v1.2.1
Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z.
func (*Int) SubOverflow ¶
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
SubUint64 set z to the difference x - y, where y is a uint64, and returns z
func (*Int) Uint64WithOverflow ¶
Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred
func (*Int) UnmarshalJSON ¶ added in v1.1.1
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
UnmarshalSSZ implements the fastssz.Unmarshaler interface and parses an encoded integer into the local struct.
func (*Int) UnmarshalText ¶ added in v1.1.1
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
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 ¶
WriteToArray20 writes the last 20 bytes of z to the destination array, including zero-bytes
func (*Int) WriteToArray32 ¶
WriteToArray32 writes all 32 bytes of z to the destination array, including zero-bytes
func (*Int) WriteToSlice ¶
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