Documentation
¶
Overview ¶
Package int256: Fixed-size signed integer math library represented as a 256-bit absolute value plus a sign bit. Copyright (c) 2023 Trịnh Đức Bảo Linh(Kevin) Copyright 2018-2020 uint256 Authors Copyright (c) 2026 0xsimulacra SPDX-License-Identifier: MIT AND BSD-3-Clause
Package int256: Fixed-size signed integer math library represented as a 256-bit absolute value plus a sign bit. Copyright 2018-2020 uint256 Authors Copyright (c) 2026 0xsimulacra SPDX-License-Identifier: BSD-3-Clause
Package int256: Fixed-size signed integer math library represented as a 256-bit absolute value plus a sign bit. Copyright (c) 2023 Trịnh Đức Bảo Linh(Kevin) Copyright 2018-2020 uint256 Authors Copyright (c) 2026 0xsimulacra SPDX-License-Identifier: MIT AND BSD-3-Clause
Package int256: Fixed-size signed integer math library represented as a 256-bit absolute value plus a sign bit. Copyright (c) 2023 Trịnh Đức Bảo Linh(Kevin) Copyright (c) 2026 0xsimulacra SPDX-License-Identifier: MIT AND BSD-3-Clause
Index ¶
- Variables
- type Int
- func CondRef(c bool, x, y *Int) *Int
- func FromBig(x *big.Int) (*Int, bool)
- func FromDecimal(decimal string) (*Int, error)
- func FromHex(hex string) (*Int, error)
- func FromUInt256(x *uint256.Int) *Int
- func MustFromBig(x *big.Int) *Int
- func MustFromDecimal(decimal string) *Int
- func MustFromHex(hex string) *Int
- func New() *Int
- func NewInt(x int64) *Int
- func (z *Int) Abs() *uint256.Int
- func (z *Int) AbsInt() *Int
- func (z *Int) Add(x, y *Int) *Int
- func (z *Int) And(x, y *Int) *Int
- func (z *Int) Clone() *Int
- func (z *Int) Cmp(x *Int) (r int)
- func (z *Int) CmpU(x *uint256.Int) int
- func (z *Int) Dec() string
- func (z *Int) Div(x, y *Int) *Int
- func (z *Int) Eq(x *Int) bool
- func (z *Int) Exp(x, y, m *Int) *Int
- func (z *Int) Hex() string
- func (z *Int) InPlaceNegate() *Int
- func (z *Int) Int64() int64
- func (z *Int) IsZero() bool
- func (z *Int) Lsh(x *Int, n uint) *Int
- func (z *Int) MarshalJSON() ([]byte, error)
- func (z *Int) MarshalText() ([]byte, error)
- func (z *Int) MostSignificantBit() uint8
- func (z *Int) Mul(x, y *Int) *Int
- func (z *Int) MulDivOverflow(x, y, d *Int) (*Int, bool)
- func (z *Int) Negate() *Int
- func (z *Int) Or(x, y *Int) *Int
- func (z *Int) Quo(x, y *Int) *Int
- func (z *Int) Relu() *Int
- func (z *Int) Rem(x, y *Int) *Int
- func (z *Int) Rsh(x *Int, n uint) *Int
- func (z *Int) Set(x *Int) *Int
- func (z *Int) SetFromDecimal(decimal string) (err error)
- func (z *Int) SetFromHex(hex string) error
- func (z *Int) SetInt64(x int64) *Int
- func (z *Int) SetString(s string) (*Int, error)
- func (z *Int) SetUint64(x uint64) *Int
- func (z *Int) Sign() int
- func (z *Int) Signed(a, b *Int) *Int
- func (z *Int) SignedMaxAbs(a, b, c *Int) *Int
- func (z *Int) Sqrt(x *Int) *Int
- func (z *Int) String() string
- func (z *Int) Sub(x, y *Int) *Int
- func (z *Int) TempAbs() *uint256.Int
- func (z *Int) ToBig() *big.Int
- func (z *Int) UnmarshalJSON(input []byte) error
- func (z *Int) UnmarshalText(input []byte) error
- func (z *Int) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var (
ErrMultipleSignAtStart = errors.New("multiple sign at the beginning")
)
Functions ¶
This section is empty.
Types ¶
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int Fixed-size signed integer math library represented as a 256-bit absolute value plus a sign bit.
This is not Solidity/EVM int256 two's-complement arithmetic: bit 255 is not the sign bit, values are not interpreted as 256-bit EVM words, and the sign is stored separately in neg.
func FromDecimal ¶
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 ¶
FromHex is a convenience-constructor to create an Int from a hexadecimal string. The string is required to be '0x' or "-0x"-prefixed Numbers larger than 256 bits are not accepted.
func FromUInt256 ¶
FromUInt256 create a int256.Int from a uint256.Int
func MustFromBig ¶
func MustFromDecimal ¶
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 ¶
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) CmpU ¶
CmpU compares z (Int) and x(Uint) and returns:
-1 if z < x 0 if z == x +1 if z > x
func (*Int) Exp ¶
Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z. If m == nil or m == 0, z = x**y unless y <= 0 then z = 1. If m != 0, y < 0, and x and m are not relatively prime, z is unchanged and nil is returned.
Modular exponentiation of inputs of a particular size is not a cryptographically constant-time operation.
func (*Int) InPlaceNegate ¶
InPlaceNegate transform the sign of z to its opposite
func (*Int) MarshalJSON ¶
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) MarshalText ¶
MarshalText implements encoding.TextMarshaler MarshalText marshals using the decimal representation
func (*Int) MostSignificantBit ¶
MostSignificantBit return the most significant bit of z, ignoring the bit or the sign
func (*Int) MulDivOverflow ¶
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) Quo ¶
Quo sets z to the quotient x/y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Quo implements truncated division (like Go); see QuoRem for more details.
func (*Int) Rem ¶
Rem sets z to the remainder x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Rem implements truncated modulus (like Go); see QuoRem for more details.
func (*Int) SetFromDecimal ¶
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"
func (*Int) 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", "0X", "-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", - negative value should be prefixed with "-" like "-0x0"
func (*Int) SignedMaxAbs ¶
SignedMaxAbs take the maximum of abs of a and b and return z with that value but the sign of c
func (*Int) Sqrt ¶
Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z. It panics if x is negative.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. UnmarshalJSON accepts either - Quoted string: either hexadecimal OR decimal - For hexadecimal, the input _must_ be prefixed with -0x, -0X, 0x or 0X - Not quoted string: only decimal
func (*Int) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. This method can unmarshal either hexadecimal or decimal. - For hexadecimal, the input _must_ be prefixed with -0x, -0X, 0x or 0X
func (*Int) Value ¶
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