uint256

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StoreBigEndian

func StoreBigEndian(b []byte, u Uint256)

StoreBigEndian stores 256-bit value in byte slice in big-endian byte order. It panics if byte slice length is less than 32.

func StoreLittleEndian

func StoreLittleEndian(b []byte, u Uint256)

StoreLittleEndian stores 256-bit value in byte slice in little-endian byte order. It panics if byte slice length is less than 32.

Types

type Uint128

type Uint128 = uint128.Uint128

Uint128 is an unsigned 128-bit number alias.

type Uint256

type Uint256 struct {
	Lo Uint128 // lower 128-bit half
	Hi Uint128 // upper 128-bit half
}

Uint256 is an unsigned 256-bit number. All methods are immutable, works just like standard uint64.

Example (Json)

ExampleUint256_json is an example for JSON marshaling.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/Pilatuz/bigx/v2/uint256"
)

func main() {
	foo := map[string]interface{}{
		"bar": uint256.From64(12345),
	}

	buf, _ := json.Marshal(foo)
	fmt.Printf("%s", buf)
}
Output:

{"bar":"12345"}

func Add

func Add(x, y Uint256, carry uint64) (sum Uint256, carryOut uint64)

Add returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1.

func From128

func From128(v Uint128) Uint256

From128 converts 128-bit value v to a Uint256 value. Upper 128-bit half will be zero.

func From64

func From64(v uint64) Uint256

From64 converts 64-bit value v to a Uint256 value. Upper 128-bit half will be zero.

func FromBig

func FromBig(i *big.Int) Uint256

FromBig converts *big.Int to 256-bit Uint256 value ignoring overflows. If input integer is nil or negative then return Zero. If input interger overflows 256-bit then return Max.

Example

ExampleFromBig is an example for FromBig.

package main

import (
	"fmt"
	"math/big"

	"github.com/Pilatuz/bigx/v2/uint256"
)

func main() {
	fmt.Println(uint256.FromBig(nil))
	fmt.Println(uint256.FromBig(new(big.Int).SetInt64(12345)))
}
Output:

0
12345

func FromBigX

func FromBigX(i *big.Int) (Uint256, bool)

FromBigX converts *big.Int to 256-bit Uint256 value (eXtended version). Provides ok successful flag as a second return value. If input integer is negative or overflows 256-bit then ok=false. If input is nil then zero 256-bit returned.

Example

ExampleFromBigX is an example for FromBigX.

package main

import (
	"fmt"
	"math/big"

	"github.com/Pilatuz/bigx/v2/uint256"
)

func main() {
	one := new(big.Int).SetInt64(1)
	fmt.Println(uint256.FromBigX(new(big.Int).SetInt64(-1))) // => Zero()
	fmt.Println(uint256.FromBigX(one))
	fmt.Println(uint256.FromBigX(one.Lsh(one, 256))) // 2^256, overflows => Max()
}
Output:

0 false
1 true
115792089237316195423570985008687907853269984665640564039457584007913129639935 false

func LoadBigEndian

func LoadBigEndian(b []byte) Uint256

LoadBigEndian loads 256-bit value from byte slice in big-endian byte order. It panics if byte slice length is less than 32.

func LoadLittleEndian

func LoadLittleEndian(b []byte) Uint256

LoadLittleEndian loads 256-bit value from byte slice in little-endian byte order. It panics if byte slice length is less than 32.

func Max

func Max() Uint256

Max is the largest possible Uint256 value.

func Mul

func Mul(x, y Uint256) (hi, lo Uint256)

Mul returns the 512-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo.

func One

func One() Uint256

One is the lowest non-zero Uint256 value.

func Sub

func Sub(x, y Uint256, borrow uint64) (diff Uint256, borrowOut uint64)

Sub returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1.

func Zero

func Zero() Uint256

Zero is the lowest possible Uint256 value.

func (Uint256) Add

func (u Uint256) Add(v Uint256) Uint256

Add returns sum (u+v) of two 256-bit values. Wrap-around semantic is used here: Max().Add(From64(1)) == Zero()

func (Uint256) Add128

func (u Uint256) Add128(v Uint128) Uint256

Add128 returns sum u+v of 256-bit and 128-bit values. Wrap-around semantic is used here: Max().Add128(uint128.One()) == Zero()

func (Uint256) And

func (u Uint256) And(v Uint256) Uint256

And returns logical AND (u&v) of two 256-bit values.

func (Uint256) And128

func (u Uint256) And128(v Uint128) Uint256

And128 returns logical AND (u&v) of 256-bit and 128-bit values.

func (Uint256) AndNot

func (u Uint256) AndNot(v Uint256) Uint256

AndNot returns logical AND NOT (u&^v) of two 256-bit values.

func (Uint256) AndNot128

func (u Uint256) AndNot128(v Uint128) Uint256

AndNot128 returns logical AND NOT (u&v) of 256-bit and 128-bit values.

func (Uint256) Big

func (u Uint256) Big() *big.Int

Big returns 256-bit value as a *big.Int.

func (Uint256) BitLen

func (u Uint256) BitLen() int

BitLen returns the minimum number of bits required to represent 256-bit value. The result is 0 for u == 0.

func (Uint256) Cmp

func (u Uint256) Cmp(v Uint256) int

Cmp compares two 256-bit values and returns:

-1 if u <  v
 0 if u == v
+1 if u >  v

func (Uint256) Cmp128

func (u Uint256) Cmp128(v Uint128) int

Cmp128 compares 256-bit and 128-bit values and returns:

-1 if u <  v
 0 if u == v
+1 if u >  v

func (Uint256) Div

func (u Uint256) Div(v Uint256) Uint256

Div returns division (u/v) of two 256-bit values.

func (Uint256) Div128

func (u Uint256) Div128(v Uint128) Uint256

Div128 returns division (u/v) of 256-bit and 128-bit values.

func (Uint256) Div64

func (u Uint256) Div64(v uint64) Uint256

Div64 returns division (u/v) of 256-bit and 64-bit values.

func (Uint256) Equals

func (u Uint256) Equals(v Uint256) bool

Equals returns true if two 256-bit values are equal. Uint256 values can be compared directly with == operator but use of the Equals method is preferred for consistency.

func (Uint256) Equals128

func (u Uint256) Equals128(v Uint128) bool

Equals128 returns true if 256-bit value equals to a 128-bit value.

func (Uint256) Format

func (u Uint256) Format(s fmt.State, ch rune)

Format does custom formatting of 256-bit value.

Example

ExampleUint256_Format is an example for Uint256.Format.

package main

import (
	"fmt"

	"github.com/Pilatuz/bigx/v2/uint256"
)

func main() {
	fmt.Printf("%08b\n", uint256.From64(42))
	fmt.Printf("%#O\n", uint256.From64(42))
	fmt.Printf("%#x\n", uint256.Max())
}
Output:

00101010
0o52
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

func (Uint256) IsZero

func (u Uint256) IsZero() bool

IsZero returns true if stored 256-bit value is zero.

func (Uint256) LeadingZeros

func (u Uint256) LeadingZeros() int

LeadingZeros returns the number of leading zero bits. The result is 256 for u == 0.

func (Uint256) Lsh

func (u Uint256) Lsh(n uint) Uint256

Lsh returns left shift (u<<n).

func (Uint256) MarshalText

func (u Uint256) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (Uint256) Mod

func (u Uint256) Mod(v Uint256) Uint256

Mod returns modulo (u%v) of two 256-bit values.

func (Uint256) Mod128

func (u Uint256) Mod128(v Uint128) Uint128

Mod128 returns modulo (u%v) of 256-bit and 128-bit values.

func (Uint256) Mod64

func (u Uint256) Mod64(v uint64) uint64

Mod64 returns modulo (u%v) of 256-bit and 64-bit values.

func (Uint256) Mul

func (u Uint256) Mul(v Uint256) Uint256

Mul returns multiplication (u*v) of two 256-bit values. Wrap-around semantic is used here: Max().Mul(Max()) == From64(1).

func (Uint256) Mul128

func (u Uint256) Mul128(v Uint128) Uint256

Mul128 returns multiplication (u*v) of 256-bit and 128-bit values. Wrap-around semantic is used here: Max().Mul128(2) == Max().Sub128(1).

func (Uint256) Not

func (u Uint256) Not() Uint256

Not returns logical NOT (^u) of 256-bit value.

func (Uint256) OnesCount

func (u Uint256) OnesCount() int

OnesCount returns the number of one bits ("population count").

func (Uint256) Or

func (u Uint256) Or(v Uint256) Uint256

Or returns logical OR (u|v) of two 256-bit values.

func (Uint256) Or128

func (u Uint256) Or128(v Uint128) Uint256

Or128 returns logical OR (u|v) of 256-bit and 128-bit values.

func (Uint256) QuoRem

func (u Uint256) QuoRem(v Uint256) (Uint256, Uint256)

QuoRem returns quotient (u/v) and remainder (u%v) of two 256-bit values.

func (Uint256) QuoRem128

func (u Uint256) QuoRem128(v Uint128) (Uint256, Uint128)

QuoRem128 returns quotient (u/v) and remainder (u%v) of 256-bit and 128-bit values.

func (Uint256) QuoRem64

func (u Uint256) QuoRem64(v uint64) (q Uint256, r uint64)

QuoRem64 returns quotient (u/v) and remainder (u%v) of 256-bit and 64-bit values.

func (Uint256) Reverse

func (u Uint256) Reverse() Uint256

Reverse returns the value with bits in reversed order.

func (Uint256) ReverseBytes

func (u Uint256) ReverseBytes() Uint256

ReverseBytes returns the value with bytes in reversed order.

func (Uint256) RotateLeft

func (u Uint256) RotateLeft(k int) Uint256

RotateLeft returns the value of u rotated left by (k mod 256) bits.

func (Uint256) RotateRight

func (u Uint256) RotateRight(k int) Uint256

RotateRight returns the value of u rotated left by (k mod 256) bits.

func (Uint256) Rsh

func (u Uint256) Rsh(n uint) Uint256

Rsh returns right shift (u>>n).

func (Uint256) String

func (u Uint256) String() string

String returns the base-10 representation of 256-bit value.

Example

ExampleUint256_String is an example for Uint256.String.

package main

import (
	"fmt"

	"github.com/Pilatuz/bigx/v2/uint256"
)

func main() {
	fmt.Println(uint256.Zero())
	fmt.Println(uint256.One())
	fmt.Println(uint256.Max())
}
Output:

0
1
115792089237316195423570985008687907853269984665640564039457584007913129639935

func (Uint256) Sub

func (u Uint256) Sub(v Uint256) Uint256

Sub returns difference (u-v) of two 256-bit values. Wrap-around semantic is used here: Zero().Sub(From64(1)) == Max().

func (Uint256) Sub128

func (u Uint256) Sub128(v Uint128) Uint256

Sub128 returns difference (u-v) of 256-bit and 128-bit values. Wrap-around semantic is used here: Zero().Sub128(uint128.One()) == Max().

func (Uint256) TrailingZeros

func (u Uint256) TrailingZeros() int

TrailingZeros returns the number of trailing zero bits. The result is 256 for u == 0.

func (*Uint256) UnmarshalText

func (u *Uint256) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Uint256) Xor

func (u Uint256) Xor(v Uint256) Uint256

Xor returns logical XOR (u^v) of two 256-bit values.

func (Uint256) Xor128

func (u Uint256) Xor128(v Uint128) Uint256

Xor128 returns logical XOR (u^v) of 256-bit and 128-bit values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL