bigint

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package bigint contains a very lightweight and high-performance implementation of unsigned multi-precision integers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedOperandSize = errors.New("unsupported operand size")
	ErrInvalidBufferSize      = errors.New("invalid buffer sizer")
	ErrInvalidLength          = errors.New("byte length not a multiple of the word size")
	ErrMissingPrefix          = errors.New("hex string without 0x prefix")
)

Errors for bigint package.

Functions

This section is empty.

Types

type Bigint

type Bigint []Word

A Bigint is an unsigned integer x of the form

x = x[n-1]*B^(n-1) + x[n-2]*B^(n-2) + ... + x[1]*B + x[0]

with 0 <= x[i] < B and 0 <= i < n is stored in a slice of length n, with the digits x[i] as the slice elements. The length of the slice never changes and operations are only well-defined for operands of the same length.

func MustParseU384

func MustParseU384(s string) Bigint

MustParseU384 parses s as a Bigint, returning the result. The encoding of s must be the same as in UnmarshalText, it panics when the encoding is not valid.

func ParseU384

func ParseU384(s string) (Bigint, error)

ParseU384 parses s as a Bigint, returning the result. The encoding of s must be the same as in UnmarshalText.

func U384

func U384() Bigint

U384 creates a 384-bit big unsigned integer.

func (Bigint) Add

func (x Bigint) Add(y Bigint) Word

Add sets x to the sum x+y and returns the carry. The carry output is guaranteed to be 0 or 1.

func (Bigint) BytesLen

func (x Bigint) BytesLen() int

BytesLen returns the length of x in bytes.

func (Bigint) Cmp

func (x Bigint) Cmp(y Bigint) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (Bigint) MSB

func (x Bigint) MSB() uint

MSB returns the value of the most significant bit of x.

func (Bigint) MarshalText

func (x Bigint) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The encoding is the same as returned by String.

func (Bigint) Read

func (x Bigint) Read(out []byte) (n int, err error)

Read reads the big-endian byte representation of x into out and returns the number of bytes read. It returns an error when len(out) <= x.BytesLen() and the bytes cannot be written in one call.

func (Bigint) SetBytes

func (x Bigint) SetBytes(buf []byte)

SetBytes interprets buf as the bytes of a big-endian unsigned integer and sets x to that value.

func (Bigint) String

func (x Bigint) String() string

String returns the hexadecimal representation of x including leading zeroes and with the "0x" prefix.

func (Bigint) Sub

func (x Bigint) Sub(y Bigint) Word

Sub sets x to the difference x-y and returns the borrow. The borrow output is guaranteed to be 0 or 1.

func (*Bigint) UnmarshalText

func (x *Bigint) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The Bigint is expected in hexadecimal representation starting with the prefix "0x". It must include leading zeroes such that the encoded byte length matches x.BytesLen().

func (Bigint) Words

func (x Bigint) Words() []Word

Words provides raw access to x by returning its underlying little-endian Word slice.

type Word

type Word = uint32

Jump to

Keyboard shortcuts

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