utils

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 12 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.6.0

func All[T any](slice []T, f func(T) bool) bool

All returns true if all elements match the given predicate

func BigIntToFelt

func BigIntToFelt(big *big.Int) *felt.Felt

BigIntToFelt converts a big integer to a felt.Felt.

Parameters: - big: the big integer to convert Returns: - *felt.Felt: the converted value

func BigToHex added in v0.4.6

func BigToHex(in *big.Int) string

BigToHex converts a big integer to its hexadecimal representation.

Parameters: - in: the big integer to be converted Returns: - string: the hexadecimal representation

func BytesToBig added in v0.4.6

func BytesToBig(bytes []byte) *big.Int

BytesToBig converts a byte slice to a big.Int.

Parameters: - bytes: the byte slice to be converted Returns: - *big.Int: the converted value

func ComputeFact added in v0.4.6

func ComputeFact(programHash *big.Int, programOutputs []*big.Int) *big.Int

ComputeFact computes the factorial of a given number.

Parameters: - programHash: a pointer to a big.Int representing the program hash - programOutputs: a slice of pointers to big.Int representing the program outputs Returns: - *big.Int: a pointer to a big.Int representing the computed factorial

func FeltArrToBigIntArr added in v0.4.5

func FeltArrToBigIntArr(f []*felt.Felt) []*big.Int

FeltArrToBigIntArr converts an array of Felt objects to an array of big.Int objects.

Parameters: - f: the array of Felt objects to convert Returns: - []*big.Int: the array of big.Int objects

func FeltToBigInt

func FeltToBigInt(f *felt.Felt) *big.Int

FeltToBigInt converts a Felt value to a *big.Int.

Parameters: - f: the Felt value to convert Returns: - *big.Int: the converted value

func Filter added in v0.6.0

func Filter[T any](slice []T, f func(T) bool) []T

func Flatten added in v0.6.0

func Flatten[T any](sl ...[]T) []T

func FmtKecBytes added in v0.4.6

func FmtKecBytes(in *big.Int, rolen int) (buf []byte)

FmtKecBytes formats the given big.Int as a byte slice (Keccak hash) with a specified length.

The function appends the bytes of the big.Int to a buffer and returns it. If the length of the buffer is less than the specified length, the function pads the buffer with zeros.

Parameters: - in: the big.Int to be formatted - rolen: the length of the buffer Returns: buf: the formatted buffer

func GetSelectorFromName added in v0.4.6

func GetSelectorFromName(funcName string) *big.Int

GetSelectorFromName generates a selector from a given function name.

Parameters: - funcName: the name of the function Returns: - *big.Int: the selector TODO: this is used by the signer. Should it return a felt?

func GetSelectorFromNameFelt added in v0.4.6

func GetSelectorFromNameFelt(funcName string) *felt.Felt

GetSelectorFromNameFelt returns a *felt.Felt based on the given function name.

Parameters: - funcName: the name of the function Returns: - *felt.Felt: the *felt.Felt

func HexArrToFelt

func HexArrToFelt(hexArr []string) ([]*felt.Felt, error)

HexArrToFelt converts an array of hexadecimal strings to an array of felt objects.

The function iterates over each element in the hexArr array and calls the HexToFelt function to convert each hexadecimal value to a felt object. If any error occurs during the conversion, the function will return nil and the corresponding error. Otherwise, it appends the converted felt object to the feltArr array. Finally, the function returns the feltArr array containing all the converted felt objects.

Parameters: - hexArr: an array of strings representing hexadecimal values Returns: - []*felt.Felt: an array of *felt.Felt objects, or nil if there was - error: an error if any

func HexToBN added in v0.4.6

func HexToBN(hexString string) *big.Int

HexToBN converts a hexadecimal string to a big.Int. trim "0x" prefix(if exists)

Parameters: - hexString: the hexadecimal string to be converted Returns: - *big.Int: the converted value

func HexToBytes added in v0.4.6

func HexToBytes(hexString string) ([]byte, error)

HexToBytes converts a hexadecimal string to a byte slice. trim "0x" prefix(if exists)

Parameters: - hexString: the hexadecimal string to be converted Returns: - []byte: the converted value - error: an error if any

func HexToFelt

func HexToFelt(hex string) (*felt.Felt, error)

HexToFelt converts a hexadecimal string to a *felt.Felt object.

Parameters: - hex: the input hexadecimal string to be converted. Returns: - *felt.Felt: a *felt.Felt object - error: if conversion fails

func HexToShortStr added in v0.4.6

func HexToShortStr(hexStr string) string

HexToShortStr converts a hexadecimal string to a short string (Starknet) representation.

Parameters: - hexStr: the hexadecimal string to convert to a short string Returns: - string: a short string

func Keccak256 added in v0.4.6

func Keccak256(data ...[]byte) []byte

Keccak256 returns the Keccak-256 hash of the input data. (ref: https://github.com/ethereum/go-ethereum/blob/master/crypto/crypto.go)

Parameters: - data: a variadic parameter of type []byte representing the input data Returns: - []byte: a 32-byte hash output

func Map added in v0.6.0

func Map[T1, T2 any](slice []T1, f func(T1) T2) []T2

func MaskBits added in v0.4.6

func MaskBits(mask, wordSize int, slice []byte) (ret []byte)

MaskBits masks (excess) bits in a slice of bytes based on the given mask and wordSize.

Parameters: - mask: an integer representing the number of bits to mask - wordSize: an integer representing the size of each word in bits - slice: a slice of bytes to mask Returns: - ret: a slice of bytes with the masked bits

func SNValToBN added in v0.4.6

func SNValToBN(str string) *big.Int

SNValToBN converts a given string to a *big.Int by checking if the string contains "0x" prefix. used in string conversions when interfacing with the APIs

Parameters: - str: a string to be converted to *big.Int Returns: - *big.Int: a pointer to a big.Int representing the converted value

func SplitFactStr added in v0.4.6

func SplitFactStr(fact string) (fact_low, fact_high string)

SplitFactStr splits a given fact string into two parts (felts): fact_low and fact_high.

The function takes a fact string as input and converts it to a big number using the HexToBN function. It then converts the big number to bytes using the Bytes method. If the length of the bytes is less than 32, it pads the bytes with zeros using the bytes.Repeat method. The padded bytes are then appended to the original bytes. The function then extracts the low part of the bytes by taking the last 16 bytes and converts it to a big number using the BytesToBig function. It also extracts the high part of the bytes by taking the first 16 bytes and converts it to a big number using the BytesToBig function. Finally, it converts the low and high big numbers to hexadecimal strings using the BigToHex function and returns them.

Parameters: - fact: The fact string to be split Return types: - fact_low: The low part of the fact string in hexadecimal format - fact_high: The high part of the fact string in hexadecimal format

func StrToBig added in v0.4.6

func StrToBig(str string) *big.Int

StrToBig generates a *big.Int from a string representation.

Parameters: - str: The string to convert to a *big.Int Returns: - *big.Int: a pointer to a big.Int representing the converted value

func TestHexArrToFelt

func TestHexArrToFelt(t testing.TB, hexArr []string) []*felt.Felt

TestHexArrToFelt generates a slice of *felt.Felt from a slice of strings representing hexadecimal values.

Parameters: - t: A testing.TB interface used for test logging and error reporting - hexArr: A slice of strings representing hexadecimal values Returns: - []*felt.Felt: a slice of *felt.Felt

func TestHexToFelt

func TestHexToFelt(t testing.TB, hex string) *felt.Felt

TestHexToFelt generates a felt.Felt from a hexadecimal string.

Parameters: - t: the testing.TB object for test logging and reporting - hex: the hexadecimal string to convert to a felt.Felt Returns: - *felt.Felt: the generated felt.Felt object

func UTF8StrToBig added in v0.4.6

func UTF8StrToBig(str string) *big.Int

UTF8StrToBig converts a UTF-8 string to a big integer.

Parameters: - str: The UTF-8 string to convert to a big integer Returns: - *big.Int: a pointer to a big.Int representing the converted value

func Uint64ToFelt

func Uint64ToFelt(num uint64) *felt.Felt

Uint64ToFelt generates a new *felt.Felt from a given uint64 number.

Parameters: - num: the uint64 number to convert to a *felt.Felt Returns: - *felt.Felt: a *felt.Felt

func UnwrapJSON added in v0.7.0

func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{}, error)

Types

type KeccakState added in v0.4.6

type KeccakState interface {
	hash.Hash
	Read([]byte) (int, error)
}

KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports Read to get a variable amount of data from the hash state. Read is faster than Sum because it doesn't copy the internal state, but also modifies the internal state.

func NewKeccakState added in v0.4.6

func NewKeccakState() KeccakState

NewKeccakState returns a new instance of KeccakState. (ref: https://github.com/ethereum/go-ethereum/blob/master/crypto/crypto.go)

Parameters:

none

Returns: - KeccakState: a new instance of KeccakState

Jump to

Keyboard shortcuts

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