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

README.md

1.06 Kb · 42 lines

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.