trinary

package
v1.0.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2019 License: MIT Imports: 5 Imported by: 107

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// TryteToTritsLUT is a Look-up-table for Trytes to Trits conversion.
	TryteToTritsLUT = [][]int8{
		{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}, {-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},
	}
)

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 TrailingZeros

func TrailingZeros(trits Trits) int64

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) (bytes []byte)

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 is only defined for hashes (81 Trytes). It returns 48 bytes.

func ValidTrit

func ValidTrit(t int8) bool

ValidTrit returns true if t is a valid trit.

func ValidTrits

func ValidTrits(t Trits) error

ValidTrits returns true if t is valid trits.

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 Trits, err error)

BytesToTrits unpacks an array of bytes into an array of trits

func IntToTrits

func IntToTrits(value int64) Trits

IntToTrits converts int64 to trits.

func MustTrytesToTrits

func MustTrytesToTrits(trytes Trytes) Trits

MustTrytesToTrits converts a slice of trytes into trits.

func NewTrits

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

NewTrits casts Trits and checks its validity.

func PadTrits

func PadTrits(trits Trits, size int) Trits

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) (Trytes, error)

BytesToTrytes converts bytes to Trytes. Returns an error if the bytes slice is not 48 in length.

func MustTritsToTrytes

func MustTritsToTrytes(trits Trits) Trytes

MustTritsToTrytes converts a slice of trits into trytes. Panics if len(t)%3!=0

func NewTrytes

func NewTrytes(s string) (Trytes, error)

NewTrytes casts to Trytes and checks its validity.

func Pad

func Pad(trytes Trytes, size int) Trytes

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