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

int256 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 with overflow detection.

Features

  • Fixed 256-bit size (predictable gas costs)
  • Two's complement representation
  • Overflow detection on all operations
  • AMM-optimized functions
  • Range: -(2^255) to 2^255-1

Usage

 1import i256 "gno.land/p/gnoswap/int256"
 2
 3// Create values
 4a := i256.NewInt(100)
 5b := i256.MustFromDecimal("-1000")
 6
 7// Arithmetic with overflow detection
 8result, overflow := new(i256.Int).AddOverflow(a, b)
 9if overflow {
10    // Handle overflow
11}

Implementation

Built on uint256 for underlying arithmetic.

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, with arithmetic operations that detect overflow.

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.

Critical operations like Add, Sub, and Mul return overflow flags, enabling safe handling of edge cases in financial calculations.

Functions 8

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).

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

func Add

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

func AddOverflow

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

func And

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

func BitLen

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

func Clear

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

func Clone

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

func Cmp

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

func Div

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

func Eq

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

func Gt

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

func Gte

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

func Int64

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

func IsInt64

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

func IsNeg

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

func IsOne

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

func IsZero

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

func Lsh

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

func Lt

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

func Lte

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

func Mul

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

func MulOverflow

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

func Neg

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

func Neq

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

func Not

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

func Or

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

func Rem

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

func Rsh

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

func Set

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

func SetBytes32

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

func SetInt64

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

func SetOne

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

func SetString

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

func SetUint64

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

func Sign

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

func Sub

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

func SubOverflow

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

func Uint64

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

func Xor

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

Imports 6

Source Files 6