trinary

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package trinary provides functions for validating and converting Trits and Trytes.

Index

Constants

This section is empty.

Variables

View Source
var (
	// TryteValueToTritsLUT is a lookup table to convert tryte values into trits.
	TryteValueToTritsLUT = [TryteRadix][TritsPerTryte]int8{
		{-1, -1, -1}, {0, -1, -1}, {1, -1, -1}, {-1, 0, -1}, {0, 0, -1}, {1, 0, -1},
		{-1, 1, -1}, {0, 1, -1}, {1, 1, -1}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0},
		{-1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0},
		{-1, -1, 1}, {0, -1, 1}, {1, -1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 1},
		{-1, 1, 1}, {0, 1, 1}, {1, 1, 1},
	}

	// TryteValueToTyteLUT is a lookup table to convert tryte values into trytes.
	TryteValueToTyteLUT = [TryteRadix]byte{'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
		'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'}

	// TryteToTryteValueLUT is a lookup table to convert trytes into tryte values.
	TryteToTryteValueLUT = [...]int8{
		0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
		-13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1,
	}

	// Pow27LUT is a Look-up-table for Decoding Trits to int64
	Pow27LUT = []int64{1,
		27,
		729,
		19683,
		531441,
		14348907,
		387420489,
		10460353203,
		282429536481,
		7625597484987,
		205891132094649,
		5559060566555523,
		150094635296999136,
		4052555153018976256}
)

Functions

func CanBeHash

func CanBeHash(trits Trits) bool

CanBeHash returns the validity of the trit length.

func CanTritsToTrytes

func CanTritsToTrytes(trits Trits) bool

CanTritsToTrytes returns true if t can be converted to trytes.

func DecodeInt64

func DecodeInt64(t Trits) (value int64, size uint64, err error)

DecodeInt64 decodes a slice of trits with encoding information as an int64.

func EncodedLength

func EncodedLength(value int64) uint64

EncodedLength returns the length of trits needed to encode the value + encoding information.

func MinTrits

func MinTrits(value int64) int

MinTrits returns the length of trits needed to encode the value.

func MustPutTryteTrits

func MustPutTryteTrits(trits []int8, v int8)

MustPutTryteTrits converts v in [-13,13] to its corresponding 3-trit value and writes this to trits. It panics on invalid input.

func MustTritsToBytes

func MustTritsToBytes(trits Trits) (bytes []byte)

MustTritsToBytes packs an array of trits into an array of bytes (5 packed trits in 1 byte). Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func MustTryteToTryteValue

func MustTryteToTryteValue(t byte) int8

MustTryteToTryteValue converts a tryte char t in [9A-Z] to a tryte value in [-13,13]. It panics when t is an invalid tryte.

func MustTryteValueToTryte

func MustTryteValueToTryte(v int8) byte

MustTryteValueToTryte converts the value of a tryte v in [-13,13] to a tryte char in [9A-Z]. It panics when v is an invalid value.

func MustTrytesToBytes

func MustTrytesToBytes(trytes Trytes) []byte

MustTrytesToBytes packs trytes into a slice of bytes (5 packed trits in 1 byte). Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func Sum

func Sum(a int8, b int8) int8

Sum returns the sum of two trits.

func TrailingZeros

func TrailingZeros(trits Trits) int

TrailingZeros returns the number of trailing zeros of the given trits.

func TritsEqual

func TritsEqual(a Trits, b Trits) (bool, error)

TritsEqual returns true if t and b are equal Trits.

func TritsToBytes

func TritsToBytes(trits Trits) ([]byte, error)

TritsToBytes packs an array of trits into an array of bytes (5 packed trits in 1 byte).

func TritsToInt

func TritsToInt(t Trits) int64

TritsToInt converts a slice of trits into an integer and assumes little-endian notation.

func TrytesToBytes

func TrytesToBytes(trytes Trytes) ([]byte, error)

TrytesToBytes packs trytes into a slice of bytes (5 packed trits in 1 byte).

func TrytesToInt

func TrytesToInt(t Trytes) int64

TrytesToInt converts a slice of trytes to int64.

func ValidBytesForTrits

func ValidBytesForTrits(bytes []byte) error

ValidBytesForTrits checks whether the given bytes are valid for bytes to trits conversion (5 packed trits in 1 byte).

func ValidTrit

func ValidTrit(t int8) bool

ValidTrit returns true if t is a valid trit.

func ValidTrits

func ValidTrits(trits Trits) error

ValidTrits returns true if t is valid trits (non-empty and -1, 0 or 1).

func ValidTryte

func ValidTryte(t rune) error

ValidTryte returns the validity of a tryte (must be rune A-Z or 9)

func ValidTrytes

func ValidTrytes(trytes Trytes) error

ValidTrytes returns true if t is made of valid trytes.

Types

type Hash

type Hash = Trytes

Hash represents a trinary hash

type Hashes

type Hashes = []Hash

Hashes is a slice of Hash.

type Trits

type Trits = []int8

Trits is a slice of int8. You should not use cast, use NewTrits instead to ensure the validity.

func AddTrits

func AddTrits(a Trits, b Trits) Trits

AddTrits adds a to b.

func BytesToTrits

func BytesToTrits(bytes []byte, numTrits ...int) (Trits, error)

BytesToTrits unpacks an array of bytes (5 packed trits in 1 byte) into an array of trits.

func EncodeInt64

func EncodeInt64(value int64) (t Trits, size uint64, err error)

EncodeInt64 encodes an int64 as a slice of trits with encoding information.

func IntToTrits

func IntToTrits(value int64) Trits

IntToTrits converts int64 to a slice of trits.

func MustBytesToTrits

func MustBytesToTrits(bytes []byte, numTrits ...int) Trits

MustBytesToTrits unpacks an array of bytes (5 packed trits in 1 byte) into an array of trits. Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func MustPadTrits

func MustPadTrits(trits Trits, n int) Trits

MustPadTrits pads the given trits with 0 up to the given size. Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func MustTrytesToTrits

func MustTrytesToTrits(trytes Trytes) Trits

MustTrytesToTrits converts a slice of trytes into trits. Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func NewTrits

func NewTrits(t []int8) (Trits, error)

NewTrits casts Trits and checks its validity.

func PadTrits

func PadTrits(trits Trits, n int) (Trits, error)

PadTrits pads the given trits with 0 up to the given size.

func ReverseTrits

func ReverseTrits(trits Trits) Trits

ReverseTrits reverses the given trits.

func TrytesToTrits

func TrytesToTrits(trytes Trytes) (Trits, error)

TrytesToTrits converts a slice of trytes into trits.

type Trytes

type Trytes = string

Trytes is a string of trytes. Use NewTrytes() instead of typecasting.

func BytesToTrytes

func BytesToTrytes(bytes []byte, numTrytes ...int) (Trytes, error)

BytesToTrytes unpacks a slice of bytes (5 packed trits in 1 byte) into trytes.

func IntToTrytes

func IntToTrytes(value int64, trytesCnt int) Trytes

IntToTrytes converts int64 to a slice of trytes.

func MustBytesToTrytes

func MustBytesToTrytes(bytes []byte, numTrytes ...int) Trytes

MustBytesToTrytes unpacks a slice of bytes (5 packed trits in 1 byte) into trytes. Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func MustPad

func MustPad(trytes Trytes, n int) Trytes

MustPad pads the given trytes with 9s up to the given size. Performs no validation on the provided inputs (therefore might return an invalid representation) and might panic.

func MustTritsToTrytes

func MustTritsToTrytes(trits Trits) Trytes

MustTritsToTrytes converts a slice of trits into trytes. Performs no validation on the input trits and might therefore return an invalid trytes representation (without a panic).

func NewTrytes

func NewTrytes(s string) (Trytes, error)

NewTrytes casts to Trytes and checks its validity.

func Pad

func Pad(trytes Trytes, n int) (Trytes, error)

Pad pads the given trytes with 9s up to the given size.

func TritsToTrytes

func TritsToTrytes(trits Trits) (Trytes, error)

TritsToTrytes converts a slice of trits into trytes. Returns an error if len(t)%3!=0

Jump to

Keyboard shortcuts

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