Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v1 source pure

Package int256 implements 256-bit signed integer arithmetic for GnoSwap.

Readme View source

int256

256-bit signed integer arithmetic for GnoSwap.

Overview

Fixed-size 256-bit signed integer library optimized for AMM calculations. The unsuffixed Add, Sub, and Mul methods return the low 256 bits (two's-complement wrap). The AddOverflow, SubOverflow, and MulOverflow variants additionally report whether the signed operation overflowed.

Features

  • Fixed 256-bit size (predictable gas costs)
  • Two's complement representation
  • Explicit overflow detection via *Overflow variants
  • AMM-optimized functions
  • Range: -(2^255) to 2^255-1

Usage

 1package main
 2
 3import i256 "gno.land/p/gnoswap/int256/v1"
 4
 5func main() {
 6    a := i256.NewInt(100)
 7    b := i256.MustFromDecimal("-1000")
 8    result, overflow := new(i256.Int).AddOverflow(a, b)
 9    if overflow {
10        panic("signed addition overflow")
11    }
12    println(result.ToString()) // -900
13}

Implementation

The type shares a little-endian four-word representation with uint256 and interoperates with it through conversion methods, but signed arithmetic is implemented independently.

Overview

Package int256 implements 256-bit signed integer arithmetic for GnoSwap.

This package provides an Int type that represents a 256-bit signed integer using two's complement representation. It supports the full range from -(2^255) to 2^255-1. The unsuffixed Add, Sub, and Mul methods return the truncated 256-bit result; AddOverflow, SubOverflow, and MulOverflow also return a signed-overflow flag.

The implementation follows Ethereum's int256 semantics, ensuring compatibility for cross-chain DeFi protocols. Operations are optimized for common AMM calculations including tick math and price computations.

Functions 8

func FromDecimal

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

FromDecimal parses a signed decimal string into a 256-bit integer.

Parameters:

  • decimal: the signed decimal text, optionally prefixed with + or -

Returns:

  • value: a parsed signed Int, or nil when parsing fails
  • err: nil on success; otherwise an invalid-format or signed-256-bit-range error

func FromUint256

1func FromUint256(x *u256.Uint) *Int
source

FromUint256 converts a uint256 to int256. Panics if the uint256 value is greater than MaxInt256 (2^255 - 1).

Parameters:

  • x: the unsigned value to convert; values above MaxInt256 cause a panic

Returns:

  • result: a signed Int with the same value as x

func MaxInt256

1func MaxInt256() *Int
source

MaxInt256 returns the maximum signed 256-bit integer, 2^255 - 1.

Returns:

  • maximum: A fresh *Int containing the largest positive int256 value.

func MinInt256

1func MinInt256() *Int
source

MinInt256 returns the minimum signed 256-bit integer, -2^255.

Returns:

  • minimum: A fresh *Int containing the two's-complement minimum value.

func MustFromDecimal

1func MustFromDecimal(decimal string) *Int
source

MustFromDecimal parses a signed decimal string and panics on invalid input.

Parameters:

  • decimal: the signed decimal text, optionally prefixed with + or -

Returns:

  • result: a parsed signed Int; invalid or out-of-range input panics

func NewInt

1func NewInt(val int64) *Int
source

NewInt constructs a signed 256-bit integer from an int64.

Parameters:

  • val: Signed 64-bit value to sign-extend into 256 bits.

Returns:

  • value: A fresh *Int representing val.

func One

1func One() *Int
source

One returns a fresh Int representing the signed value 1.

Returns:

  • one: A mutable *Int with only its least-significant bit set.

func Zero

1func Zero() *Int
source

Zero returns a fresh Int whose 256 bits are all zero.

Returns:

  • zero: A mutable *Int representing the signed value 0.

Types 1

type Int

array
1type Int [4]uint64
source

Methods on Int

func Abs

method on Int
1func (z *Int) Abs() *u256.Uint
source

Abs returns the unsigned magnitude of z.

Returns:

  • magnitude: A *u256.Uint containing |z|; negative values are negated before conversion.

Panics for MinInt256 because NegOverflow cannot represent its positive magnitude as Int.

func Add

method on Int
1func (z *Int) Add(x, y *Int) *Int
source

Add adds x and y modulo 2^256.

Parameters:

  • x: First signed 256-bit addend.
  • y: Second signed 256-bit addend.

Returns:

  • z: The receiver containing the low 256 bits of x + y.

func AddOverflow

method on Int
1func (z *Int) AddOverflow(x, y *Int) (*Int, bool)
source

AddOverflow adds x and y modulo 2^256 and reports signed overflow.

Parameters:

  • x: First signed 256-bit addend.
  • y: Second signed 256-bit addend.

Returns:

  • z: The wrapped 256-bit sum.
  • overflow: True when x and y have the same sign but the wrapped sum has the opposite sign.

func And

method on Int
1func (z *Int) And(x, y *Int) *Int
source

And sets z to the bitwise AND of x and y.

Parameters:

  • x: First 256-bit operand.
  • y: Second 256-bit operand.

Returns:

  • z: The receiver containing x & y.

func BitLen

method on Int
1func (z *Int) BitLen() int
source

BitLen returns the number of bits needed to represent z's raw 256-bit pattern.

Returns:

  • length: Bit length of the four-limb representation; zero has length 0 and a negative value may require all 256 bits.

func Clear

method on Int
1func (z *Int) Clear() *Int
source

Clear sets every limb of z to zero.

Returns:

  • z: The receiver representing the signed value 0.

func Clone

method on Int
1func (z *Int) Clone() *Int
source

Clone returns an independent copy of z.

Returns:

  • clone: A fresh *Int with the same four limbs as z.

func Cmp

method on Int
1func (z *Int) Cmp(x *Int) int
source

Cmp compares z and x as signed two's-complement int256 values.

Parameters:

  • x: Signed 256-bit value to compare with z.

Returns:

  • comparison: -1 when z < x, 0 when z == x, and 1 when z > x.

func Div

method on Int
1func (z *Int) Div(x, y *Int) *Int
source

Div divides signed 256-bit x by y, truncating the quotient toward zero.

Parameters:

  • x: Signed 256-bit dividend.
  • y: Signed 256-bit divisor.

Returns:

  • z: The receiver containing the signed quotient x / y.

Panics if y is zero.

func Eq

method on Int
1func (z *Int) Eq(x *Int) bool
source

Eq reports whether z and x have identical 256-bit representations.

Parameters:

  • x: Int to compare with z.

Returns:

  • equal: True when all four limbs match.

func Gt

method on Int
1func (z *Int) Gt(x *Int) bool
source

Gt reports whether z is greater than x as signed int256 values.

Parameters:

  • x: Signed 256-bit value to compare with z.

Returns:

  • greater: True when z > x.

func Gte

method on Int
1func (z *Int) Gte(x *Int) bool
source

Gte reports whether z is greater than or equal to x as signed int256 values.

Parameters:

  • x: Signed 256-bit value to compare with z.

Returns:

  • greaterOrEqual: True when z >= x.

func Int64

method on Int
1func (z *Int) Int64() int64
source

Int64 converts z to int64 after checking its signed range.

Returns:

  • value: z's exact signed 64-bit value.

Panics if z is outside the signed int64 range.

func IsInt64

method on Int
1func (z *Int) IsInt64() bool
source

IsInt64 reports whether z can be represented exactly as a signed int64.

Returns:

  • fits: True when z is in [math.MinInt64, math.MaxInt64], false otherwise.

func IsMinI256

method on Int
1func (z *Int) IsMinI256() bool
source

IsMinI256 reports whether z equals MinInt256.

Returns:

  • isMinimum: True only for the bit pattern 0x8000...0000.

func IsNeg

method on Int
1func (z *Int) IsNeg() bool
source

IsNeg reports whether z has its signed two's-complement sign bit set.

Returns:

  • isNegative: True when z is negative, including MinInt256.

func IsOne

method on Int
1func (z *Int) IsOne() bool
source

IsOne reports whether z represents the signed value 1.

Returns:

  • isOne: True exactly when the low limb is 1 and all upper limbs are zero.

func IsPositive

method on Int
1func (z *Int) IsPositive() bool
source

IsPositive reports whether z is strictly greater than zero.

Returns:

  • isPositive: True when the sign bit is clear and at least one value bit is set.

func IsUint64

method on Int
1func (z *Int) IsUint64() bool
source

IsUint64 reports whether z fits in an unsigned 64-bit word.

Returns:

  • fits: True when all three upper 64-bit limbs of z are zero.

func IsZero

method on Int
1func (z *Int) IsZero() bool
source

IsZero reports whether every limb of z is zero.

Returns:

  • isZero: True exactly when z represents the signed value 0.

func Lsh

method on Int
1func (z *Int) Lsh(x *Int, n uint) *Int
source

Lsh sets z to x left-shifted by n bits, truncating to 256 bits.

Parameters:

  • x: 256-bit bit pattern to shift.
  • n: Number of bit positions to shift left; n >= 256 yields zero.

Returns:

  • z: The receiver containing (x << n) modulo 2^256.

func Lt

method on Int
1func (z *Int) Lt(x *Int) bool
source

Lt reports whether z is less than x as signed int256 values.

Parameters:

  • x: Signed 256-bit value to compare with z.

Returns:

  • less: True when z < x.

func Lte

method on Int
1func (z *Int) Lte(x *Int) bool
source

Lte reports whether z is less than or equal to x as signed int256 values.

Parameters:

  • x: Signed 256-bit value to compare with z.

Returns:

  • lessOrEqual: True when z <= x.

func Mul

method on Int
1func (z *Int) Mul(x, y *Int) *Int
source

Mul multiplies x and y modulo 2^256.

Parameters:

  • x: First signed 256-bit factor.
  • y: Second signed 256-bit factor.

Returns:

  • z: The receiver containing the low 256 bits of x * y.

func MulOverflow

method on Int
1func (z *Int) MulOverflow(x, y *Int) (*Int, bool)
source

MulOverflow multiplies x and y and reports whether the signed product exceeds int256. The returned value is still the wrapped low 256-bit product.

Parameters:

  • x: First signed 256-bit factor.
  • y: Second signed 256-bit factor.

Returns:

  • z: The low 256 bits of the signed product, with its sign restored.
  • overflow: True when the mathematical product is outside [-2^255, 2^255 - 1].

func Neg

method on Int
1func (z *Int) Neg(x *Int) *Int
source

Neg computes the two's-complement negation of x modulo 2^256.

Parameters:

  • x: 256-bit value whose bits are complemented and incremented.

Returns:

  • z: The receiver containing the wrapped negation of x.

func NegOverflow

method on Int
1func (z *Int) NegOverflow(x *Int) *Int
source

NegOverflow computes the two's-complement negation of x and rejects the one value whose positive magnitude is outside signed int256.

Parameters:

  • x: Signed 256-bit value to negate.

Returns:

  • z: The receiver containing -x.

Panics when x is MinInt256.

func Neq

method on Int
1func (z *Int) Neq(x *Int) bool
source

Neq reports whether z and x have different 256-bit representations.

Parameters:

  • x: Int to compare with z.

Returns:

  • different: True when at least one limb differs.

func Not

method on Int
1func (z *Int) Not(x *Int) *Int
source

Not sets z to the bitwise complement of x.

Parameters:

  • x: 256-bit operand whose bits are complemented.

Returns:

  • z: The receiver containing ^x.

func Or

method on Int
1func (z *Int) Or(x, y *Int) *Int
source

Or sets z to the bitwise OR of x and y.

Parameters:

  • x: First 256-bit operand.
  • y: Second 256-bit operand.

Returns:

  • z: The receiver containing x | y.

func Rem

method on Int
1func (z *Int) Rem(x, y *Int) *Int
source

Rem computes the signed remainder of x divided by y, preserving x's sign.

Parameters:

  • x: Signed 256-bit dividend.
  • y: Signed 256-bit divisor.

Returns:

  • z: The receiver containing x % y, with magnitude less than |y|.

Panics if y is zero.

func Rsh

method on Int
1func (z *Int) Rsh(x *Int, n uint) *Int
source

Rsh shifts x right by n bits using signed arithmetic semantics. Non-negative values receive zero-fill; negative values receive sign extension.

Parameters:

  • x: Signed 256-bit value to shift.
  • n: Number of bit positions to shift right.

Returns:

  • z: The receiver containing the arithmetic right shift of x.

func Set

method on Int
1func (z *Int) Set(x *Int) *Int
source

Set copies the complete 256-bit value from x into z.

Parameters:

  • x: Source Int whose four limbs are copied.

Returns:

  • z: The receiver after copying x.

func SetAllBitsOne

method on Int
1func (z *Int) SetAllBitsOne() *Int
source

SetAllBitsOne sets every bit of z to one, the two's-complement representation of -1.

Returns:

  • z: The receiver containing the all-ones 256-bit pattern.

func SetBytes32

method on Int
1func (z *Int) SetBytes32(in []byte) *Int
source

SetBytes32 loads the first 32 bytes of in as a big-endian 256-bit value.

Parameters:

  • in: Byte slice containing at least 32 bytes, with the most-significant byte first. Bytes beyond the first 32 are ignored.

Returns:

  • z: The receiver populated from the 32-byte big-endian representation.

Panics if in contains fewer than 32 bytes.

func SetInt64

method on Int
1func (z *Int) SetInt64(x int64) *Int
source

SetInt64 assigns x to z with two's-complement sign extension.

Parameters:

  • x: Signed 64-bit value to store.

Returns:

  • z: The receiver representing x as an Int.

func SetOne

method on Int
1func (z *Int) SetOne() *Int
source

SetOne sets z to the signed value 1.

Returns:

  • z: The receiver with only its least-significant bit set.

func SetString

method on Int
1func (z *Int) SetString(s string) (*Int, error)
source

SetString parses a signed decimal string into an Int.

Parameters:

  • s: the signed decimal text to parse, with at most one leading sign

Returns:

  • value: the parsed signed Int, or nil when parsing fails
  • err: nil on success; otherwise the parse or signed-256-bit-range error

func SetUint64

method on Int
1func (z *Int) SetUint64(x uint64) *Int
source

SetUint64 assigns the non-negative uint64 x to z, clearing its upper limbs.

Parameters:

  • x: Unsigned 64-bit value to store.

Returns:

  • z: The receiver representing x as a non-negative Int.

func Sign

method on Int
1func (z *Int) Sign() int
source

Sign reports the signed sign of z.

Returns:

  • sign: -1 for negative z, 0 for zero z, or 1 for positive z.

func Sub

method on Int
1func (z *Int) Sub(x, y *Int) *Int
source

Sub subtracts y from x modulo 2^256.

Parameters:

  • x: Signed 256-bit minuend.
  • y: Signed 256-bit subtrahend.

Returns:

  • z: The receiver containing the low 256 bits of x - y.

func SubOverflow

method on Int
1func (z *Int) SubOverflow(x, y *Int) (*Int, bool)
source

SubOverflow subtracts y from x modulo 2^256 and reports signed overflow.

Parameters:

  • x: Signed 256-bit minuend.
  • y: Signed 256-bit subtrahend.

Returns:

  • z: The wrapped 256-bit difference.
  • overflow: True when the mathematical signed difference is outside the int256 range.

func ToString

method on Int
1func (z *Int) ToString() string
source

ToString returns the signed decimal representation of z.

Returns:

  • decimal: the signed base-10 representation of z

func Uint64

method on Int
1func (z *Int) Uint64() uint64
source

Uint64 converts z to uint64 when its upper 192 bits are zero.

Returns:

  • value: z's exact unsigned 64-bit value.

Panics if any upper limb of z is non-zero.

func Xor

method on Int
1func (z *Int) Xor(x, y *Int) *Int
source

Xor sets z to the bitwise exclusive OR of x and y.

Parameters:

  • x: First 256-bit operand.
  • y: Second 256-bit operand.

Returns:

  • z: The receiver containing x ^ y.

Imports 6

Source Files 6