Documentation ¶
Index ¶
- func StoreBigEndian(b []byte, u Uint256)
- func StoreLittleEndian(b []byte, u Uint256)
- type Uint128
- type Uint256
- func Add(x, y Uint256, carry uint64) (sum Uint256, carryOut uint64)
- func From128(v Uint128) Uint256
- func From64(v uint64) Uint256
- func FromBig(i *big.Int) Uint256
- func FromBigX(i *big.Int) (Uint256, bool)
- func LoadBigEndian(b []byte) Uint256
- func LoadLittleEndian(b []byte) Uint256
- func Max() Uint256
- func Mul(x, y Uint256) (hi, lo Uint256)
- func One() Uint256
- func Sub(x, y Uint256, borrow uint64) (diff Uint256, borrowOut uint64)
- func Zero() Uint256
- func (u Uint256) Add(v Uint256) Uint256
- func (u Uint256) Add128(v Uint128) Uint256
- func (u Uint256) And(v Uint256) Uint256
- func (u Uint256) And128(v Uint128) Uint256
- func (u Uint256) AndNot(v Uint256) Uint256
- func (u Uint256) AndNot128(v Uint128) Uint256
- func (u Uint256) Big() *big.Int
- func (u Uint256) BitLen() int
- func (u Uint256) Cmp(v Uint256) int
- func (u Uint256) Cmp128(v Uint128) int
- func (u Uint256) Div(v Uint256) Uint256
- func (u Uint256) Div128(v Uint128) Uint256
- func (u Uint256) Div64(v uint64) Uint256
- func (u Uint256) Equals(v Uint256) bool
- func (u Uint256) Equals128(v Uint128) bool
- func (u Uint256) Format(s fmt.State, ch rune)
- func (u Uint256) IsZero() bool
- func (u Uint256) LeadingZeros() int
- func (u Uint256) Lsh(n uint) Uint256
- func (u Uint256) MarshalText() (text []byte, err error)
- func (u Uint256) Mod(v Uint256) Uint256
- func (u Uint256) Mod128(v Uint128) Uint128
- func (u Uint256) Mod64(v uint64) uint64
- func (u Uint256) Mul(v Uint256) Uint256
- func (u Uint256) Mul128(v Uint128) Uint256
- func (u Uint256) Not() Uint256
- func (u Uint256) OnesCount() int
- func (u Uint256) Or(v Uint256) Uint256
- func (u Uint256) Or128(v Uint128) Uint256
- func (u Uint256) QuoRem(v Uint256) (Uint256, Uint256)
- func (u Uint256) QuoRem128(v Uint128) (Uint256, Uint128)
- func (u Uint256) QuoRem64(v uint64) (q Uint256, r uint64)
- func (u Uint256) Reverse() Uint256
- func (u Uint256) ReverseBytes() Uint256
- func (u Uint256) RotateLeft(k int) Uint256
- func (u Uint256) RotateRight(k int) Uint256
- func (u Uint256) Rsh(n uint) Uint256
- func (u Uint256) String() string
- func (u Uint256) Sub(v Uint256) Uint256
- func (u Uint256) Sub128(v Uint128) Uint256
- func (u Uint256) TrailingZeros() int
- func (u *Uint256) UnmarshalText(text []byte) error
- func (u Uint256) Xor(v Uint256) Uint256
- func (u Uint256) Xor128(v Uint128) Uint256
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StoreBigEndian ¶
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 ¶
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 Uint256 ¶
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 ¶
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 ¶
From128 converts 128-bit value v to a Uint256 value. Upper 128-bit half will be zero.
func FromBig ¶
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 ¶
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 ¶
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 ¶
LoadLittleEndian loads 256-bit value from byte slice in little-endian byte order. It panics if byte slice length is less than 32.
func Mul ¶
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 Sub ¶
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 (Uint256) Add ¶
Add returns sum (u+v) of two 256-bit values. Wrap-around semantic is used here: Max().Add(From64(1)) == Zero()
func (Uint256) Add128 ¶
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) BitLen ¶
BitLen returns the minimum number of bits required to represent 256-bit value. The result is 0 for u == 0.
func (Uint256) Cmp ¶
Cmp compares two 256-bit values and returns:
-1 if u < v 0 if u == v +1 if u > v
func (Uint256) Cmp128 ¶
Cmp128 compares 256-bit and 128-bit values and returns:
-1 if u < v 0 if u == v +1 if u > v
func (Uint256) Equals ¶
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) Format ¶
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) LeadingZeros ¶
LeadingZeros returns the number of leading zero bits. The result is 256 for u == 0.
func (Uint256) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Uint256) Mul ¶
Mul returns multiplication (u*v) of two 256-bit values. Wrap-around semantic is used here: Max().Mul(Max()) == From64(1).
func (Uint256) Mul128 ¶
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) QuoRem128 ¶
QuoRem128 returns quotient (u/v) and remainder (u%v) of 256-bit and 128-bit values.
func (Uint256) QuoRem64 ¶
QuoRem64 returns quotient (u/v) and remainder (u%v) of 256-bit and 64-bit values.
func (Uint256) ReverseBytes ¶
ReverseBytes returns the value with bytes in reversed order.
func (Uint256) RotateLeft ¶
RotateLeft returns the value of u rotated left by (k mod 256) bits.
func (Uint256) RotateRight ¶
RotateRight returns the value of u rotated left by (k mod 256) bits.
func (Uint256) 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 ¶
Sub returns difference (u-v) of two 256-bit values. Wrap-around semantic is used here: Zero().Sub(From64(1)) == Max().
func (Uint256) Sub128 ¶
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 ¶
TrailingZeros returns the number of trailing zero bits. The result is 256 for u == 0.
func (*Uint256) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.