Documentation ¶
Overview ¶
Package math provides integer math utilities.
Index ¶
- Constants
- Variables
- func AbsolutePath(datadir string, filename string) string
- func AppDataDir(appName string, roaming bool) string
- func BigEndianByteAt(bigint *big.Int, n int) byte
- func BigMax(x, y *big.Int) *big.Int
- func BigMin(x, y *big.Int) *big.Int
- func BigPow(a, b int64) *big.Int
- func Byte(bigint *big.Int, padlength, n int) byte
- func Decode(input string) ([]byte, error)
- func DecodeBig(input string) (*big.Int, error)
- func DecodeUint64(input string) (uint64, error)
- func EachChildFile(directory string, process func(path string) (bool, error)) error
- func EachDirectory(directory string, process func(path string) (bool, error)) error
- func Encode(b []byte) string
- func EncodeBig(bigint *big.Int) string
- func EncodeUint64(i uint64) string
- func Exp(base, exponent *big.Int) *big.Int
- func ExpandPath(p string) string
- func FirstBitSet(v *big.Int) int
- func GenUnique() ([]byte, error)
- func GlobalBig(ctx *cli.Context, name string) *big.Int
- func HmAC(message, key []byte) []byte
- func HomeDir() string
- func IsDirExists(path string) bool
- func IsFileExists(path string) bool
- func MustDecode(input string) []byte
- func MustDecodeBig(input string) *big.Int
- func MustDecodeUint64(input string) uint64
- func MustParseBig256(s string) *big.Int
- func MustParseUint64(s string) uint64
- func PaddedBigBytes(bigint *big.Int, n int) []byte
- func ParseBig256(s string) (*big.Int, bool)
- func ParseUint64(s string) (uint64, bool)
- func ReadBits(bigint *big.Int, buf []byte)
- func S256(x *big.Int) *big.Int
- func SafeAdd(x, y uint64) (uint64, bool)
- func SafeMul(x, y uint64) (uint64, bool)
- func SafeSub(x, y uint64) (uint64, bool)
- func U256(x *big.Int) *big.Int
- func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error
- func UnmarshalFixedText(typname string, input, out []byte) error
- func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error
- type Big
- type BigFlag
- type BlockNumber
- type Bytes
- type ChainIdType
- type DirectoryFlag
- type DirectoryString
- type HexOrDecimal256
- type HexOrDecimal64
- type TextMarshaler
- type TextMarshalerFlag
- type Uint
- type Uint64
Constants ¶
const ( PendingBlockNumber = BlockNumber(-2) LatestBlockNumber = BlockNumber(-1) EarliestBlockNumber = BlockNumber(0) )
const ( MaxInt8 = 1<<7 - 1 MinInt8 = -1 << 7 MaxInt16 = 1<<15 - 1 MinInt16 = -1 << 15 MaxInt32 = 1<<31 - 1 MinInt32 = -1 << 31 MaxInt64 = 1<<63 - 1 MinInt64 = -1 << 63 MaxUint8 = 1<<8 - 1 MaxUint16 = 1<<16 - 1 MaxUint32 = 1<<32 - 1 MaxUint64 = 1<<64 - 1 )
Integer limit values.
const (
ChainIdSize = 64
)
Variables ¶
var ( TT255 = BigPow(2, 255) TT256 = BigPow(2, 256) TT256m1 = new(big.Int).Sub(TT256, big.NewInt(1)) TT63 = BigPow(2, 63) MaxBig256 = new(big.Int).Set(TT256m1) MaxBig63 = new(big.Int).Sub(TT63, big.NewInt(1)) Big1 = big.NewInt(1) Big2 = big.NewInt(2) Big3 = big.NewInt(3) Big0 = big.NewInt(0) Big32 = big.NewInt(32) Big256 = big.NewInt(256) Big257 = big.NewInt(257) )
Various big integer limit values.
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrEmptyNumber = &decError{"hex string \"0x\""} ErrLeadingZero = &decError{"hex number with leading zero digits"} ErrUint64Range = &decError{"hex number > 64 bits"} ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)} ErrBig256Range = &decError{"hex number > 256 bits"} )
Errors
Functions ¶
func AbsolutePath ¶
func AppDataDir ¶
AppDataDir returns an operating system specific directory to be used for storing application data for an application.
The appName parameter is the name of the application the data directory is being requested for. This function will prepend a period to the appName for POSIX style operating systems since that is standard practice. An empty appName or one with a single dot is treated as requesting the current directory so only "." will be returned. Further, the first character of appName will be made lowercase for POSIX style operating systems and uppercase for Mac and Windows since that is standard practice.
The roaming parameter only applies to Windows where it specifies the roaming application data profile (%APPDATA%) should be used instead of the local one (%LOCALAPPDATA%) that is used by default.
Example results:
dir := AppDataDir("myapp", false) POSIX (Linux/BSD): ~/.myapp Mac OS: $HOME/Library/Application Support/Myapp Windows: %LOCALAPPDATA%\Myapp Plan 9: $home/myapp
func BigEndianByteAt ¶
BigEndianByteAt returns the byte at position n, in Big-Endian encoding So n==0 returns the least significant byte
func Byte ¶
Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5
func DecodeBig ¶
DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.
func DecodeUint64 ¶
DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func EachChildFile ¶
EachChildFile get child fi and process ,if get error after processing stop, if get a stop flag , stop
func EachDirectory ¶
func EncodeBig ¶
EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.
func EncodeUint64 ¶
EncodeUint64 encodes i as a hex string with 0x prefix.
func Exp ¶
Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.
Courtesy @karalabe and @chfast
func ExpandPath ¶
Expands a file path 1. replace tilde with users home dir 2. expands embedded environment variables 3. cleans the path, e.g. /a/b/../c -> /a/c Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func FirstBitSet ¶
FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
func IsDirExists ¶
func IsFileExists ¶
func MustDecode ¶
MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func MustDecodeBig ¶
MustDecodeBig decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustDecodeUint64 ¶
MustDecodeUint64 decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustParseBig256 ¶
MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
func MustParseUint64 ¶
MustParseUint64 parses s as an integer and panics if the string is invalid.
func PaddedBigBytes ¶
PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.
func ParseBig256 ¶
ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func ParseUint64 ¶
ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func ReadBits ¶
ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.
func S256 ¶
S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.
S256(0) = 0 S256(1) = 1 S256(2**255) = -2**255 S256(2**256-1) = -1
func UnmarshalFixedJSON ¶
UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types.
func UnmarshalFixedText ¶
UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
func UnmarshalFixedUnprefixedText ¶
UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
Types ¶
type Big ¶
Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.
func (Big) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Big) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Big) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type BigFlag ¶
BigFlag is a command line flag that accepts 256 bit big integers in decimal or hexadecimal syntax.
type BlockNumber ¶
type BlockNumber int64
func (BlockNumber) Int64 ¶
func (bn BlockNumber) Int64() int64
func (*BlockNumber) UnmarshalJSON ¶
func (bn *BlockNumber) UnmarshalJSON(data []byte) error
UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: - "latest", "earliest" or "pending" as string arguments - the block number Returned errors: - an invalid block number error when the given argument isn't a known strings - an out of range error when the given block number is either too little or too large
type Bytes ¶
type Bytes []byte
Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".
func (Bytes) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bytes) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type ChainIdType ¶
type ChainIdType [ChainIdSize]byte
func Bytes2ChainId ¶
func Bytes2ChainId(b []byte) ChainIdType
func Hex2ChainId ¶
func Hex2ChainId(s string) ChainIdType
func (ChainIdType) Hex ¶
func (c ChainIdType) Hex() string
func (ChainIdType) MarshalText ¶
func (c ChainIdType) MarshalText() ([]byte, error)
func (*ChainIdType) SetBytes ¶
func (c *ChainIdType) SetBytes(b []byte)
func (*ChainIdType) UnmarshalJSON ¶
func (c *ChainIdType) UnmarshalJSON(input []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (*ChainIdType) UnmarshalText ¶
func (c *ChainIdType) UnmarshalText(input []byte) error
UnmarshalText implements encoding.TextUnmarshaler
type DirectoryFlag ¶
type DirectoryFlag struct { Name string Value DirectoryString Usage string }
Custom cli.Flag type which expand the received string to an absolute path. e.g. ~/.ethereum -> /home/username/.ethereum
func (DirectoryFlag) Apply ¶
func (self DirectoryFlag) Apply(set *flag.FlagSet)
called by cli library, grabs variable from environment (if in env) and adds variable to flag set for parsing.
func (DirectoryFlag) GetName ¶
func (self DirectoryFlag) GetName() string
func (*DirectoryFlag) Set ¶
func (self *DirectoryFlag) Set(value string)
func (DirectoryFlag) String ¶
func (self DirectoryFlag) String() string
type DirectoryString ¶
type DirectoryString struct {
Value string
}
Custom type which is registered in the flags library which cli uses for argument parsing. This allows us to expand Value to an absolute path when the argument is parsed
func (*DirectoryString) Set ¶
func (self *DirectoryString) Set(value string) error
func (*DirectoryString) String ¶
func (self *DirectoryString) String() string
type HexOrDecimal256 ¶
HexOrDecimal256 marshals big.Int as hex or decimal.
func (*HexOrDecimal256) MarshalText ¶
func (i *HexOrDecimal256) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (*HexOrDecimal256) UnmarshalText ¶
func (i *HexOrDecimal256) UnmarshalText(input []byte) error
UnmarshalText implements encoding.TextUnmarshaler.
type HexOrDecimal64 ¶
type HexOrDecimal64 uint64
HexOrDecimal64 marshals uint64 as hex or decimal.
func (HexOrDecimal64) MarshalText ¶
func (i HexOrDecimal64) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (*HexOrDecimal64) UnmarshalText ¶
func (i *HexOrDecimal64) UnmarshalText(input []byte) error
UnmarshalText implements encoding.TextUnmarshaler.
type TextMarshaler ¶
type TextMarshaler interface { encoding.TextMarshaler encoding.TextUnmarshaler }
func GlobalTextMarshaler ¶
func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler
GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
type TextMarshalerFlag ¶
type TextMarshalerFlag struct { Name string Value TextMarshaler Usage string }
TextMarshalerFlag wraps a TextMarshaler value.
func (TextMarshalerFlag) Apply ¶
func (f TextMarshalerFlag) Apply(set *flag.FlagSet)
func (TextMarshalerFlag) GetName ¶
func (f TextMarshalerFlag) GetName() string
func (TextMarshalerFlag) String ¶
func (f TextMarshalerFlag) String() string
type Uint ¶
type Uint uint
Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint64 ¶
type Uint64 uint64
Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint64) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint64) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler