utils

package
v0.0.0-...-59e7cf7 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(a int) int

func AllReturnEqual

func AllReturnEqual[T, U any](fs func(T) U, args []T) (U, error)

Iterates the function on all the given arguments and return an error if one is not equal to the first one. Panics if given an empty array.

func ApplyModulusBls12377

func ApplyModulusBls12377(b string) string

Apply the modulus

func ApplyModulusBn254

func ApplyModulusBn254(b string) string

Apply the modulus of the BLS12-377 scalar field

func AsBigEndian32Bytes

func AsBigEndian32Bytes(x int) (res [32]byte)

Format an integer as a big-endian uint256

func BigsToBytes

func BigsToBytes(ins []*big.Int) []byte

func BigsToInts

func BigsToInts(ints []*big.Int) []int

func BytesEqual

func BytesEqual(expected, actual []byte) error

BytesEqual between byte slices a,b a readable error message would show in case of inequality TODO error options: block size, check forwards or backwards etc

func Copy

func Copy[T any](dst []frontend.Variable, src []T) (n int)

func Digest

func Digest(src io.Reader) (string, error)

Digest computes the SHA256 Digest of the contents of file and prepends a "0x" byte to it. Callers are responsible for closing the file. The reliance on SHA256 is motivated by the fact that we use the sum checksum for the verifier key to identify which verifier contract to use.

func DivCeil

func DivCeil(a, b int) int

DivCeil for int a, b

func DivExact

func DivExact(a, b int) int

DivExact for int a, b. Panics if b does not divide a exactly.

func FillRange

func FillRange[T constraints.Integer](dst []T, start T)

func FmtInt32Bytes

func FmtInt32Bytes(v int) [32]byte

func FmtIntHex32Bytes

func FmtIntHex32Bytes(v int) string

Format an integer as a 32 bytes hex string

func FmtUint32Bytes

func FmtUint32Bytes(v uint) [32]byte

func GCD

func GCD[T ~int](a, b T) T

GCD calculates GCD of a and b by Euclidian algorithm.

func HexConcat

func HexConcat(v ...string) string

Concatenate hex strings

func HexDecodeString

func HexDecodeString(s string) ([]byte, error)

func HexEncodeToString

func HexEncodeToString(b []byte) string

func HexHashHex

func HexHashHex(v ...string) string

Parse one or more hex string into a byte array, hash it and return the result as an hexstring. If several hex string are passed, what is hashed is the concatenation of the strings and the hasher is implictly updated only once. The hash function is Keccak.

func HexHashUint64

func HexHashUint64(v ...uint64) string

Encode the uint64 into an hexstring representing it as a u256 in bigendian form

func IsPowerOfTwo

func IsPowerOfTwo[T ~int](n T) bool

Return true if n is a power of two

func Ite

func Ite[T any](cond bool, ifSo, ifNot T) T

func Join

func Join[T any](ts ...[]T) []T

Join joins a set of slices by appending them into a new array. It can also be used to flatten a double array.

func KeccakHash

func KeccakHash(stream []byte) []byte

Compute the keccak of a stream of bytes. Returns the hex string.

func Log2Ceil

func Log2Ceil(a int) int

Log2Ceil computes the ceiled value of Log2

func Log2Floor

func Log2Floor(a int) int

Log2Floor computes the floored value of Log2

func Max

func Max[T constraints.Ordered](s ...T) T

func Min

func Min[T constraints.Ordered](s ...T) T

func NewElementFromBytes

func NewElementFromBytes[T emulated.FieldParams](api frontend.API, bytes []frontend.Variable) *emulated.Element[T]

NewElementFromBytes range checks the bytes and gives a reduced field element

func NextPowerOfTwo

func NextPowerOfTwo[T ~int64 | ~uint64 | ~uintptr | ~int | ~uint](in T) T

NextPowerOfTwo returns the next power of two for the given number. It returns the number itself if it's a power of two. As an edge case, zero returns zero.

Taken from : https://github.com/protolambda/zrnt/blob/v0.13.2/eth2/util/math/math_util.go#L58 The function panics if the input is more than 2**62 as this causes overflow

func Panic

func Panic(msg string, args ...any)

func Partition

func Partition[T any, I constraints.Integer](s []T, index []I) [][]T

func PositiveMod

func PositiveMod[T ~int](a, n T) T

PositiveMod returns the positive modulus

func RangeSlice

func RangeSlice[T constraints.Integer](length int, startingPoints ...T) []T

func ReadAllJsonFiles

func ReadAllJsonFiles(dirPath string) (map[string][]byte, error)

func ReadFromFile

func ReadFromFile(path string, to io.ReaderFrom) error

func ReadFromJSON

func ReadFromJSON(path string, v interface{}) error

func ReduceBytes

func ReduceBytes[T emulated.FieldParams](api frontend.API, bytes []frontend.Variable) []frontend.Variable

ReduceBytes reduces given bytes modulo a given field. As a side effect, the "bytes" are range checked

func RegisterHints

func RegisterHints()

func RepeatSlice

func RepeatSlice[T any](s []T, n int) []T

RepeatSlice returns the concatenation of `s` with itself `n` times

func Require

func Require(cond bool, msg string, args ...any)

func RightPad

func RightPad[T any](s []T, n int) []T

RightPad copies `s` and returns a vector padded up to length `n`. The padding value is T's default. The padding value. The function panics if len(s) > n and returns a copy of s if len(s) == n.

func RightPadWith

func RightPadWith[T any](s []T, n int, padWith T) []T

RightPadWith copies `s` and returns a vector padded up to length `n` using `padWith` as a filling value. The function panics if len(s) < n and returns a copy of s if len(s) == n.

func Sha2SumHexOf

func Sha2SumHexOf(w io.WriterTo) string

Returns a SHA256 checksum of the given asset. TODO @gbotrel merge with Digest Sha2SumHexOf returns a SHA256 checksum of the given asset.

func ToBytes

func ToBytes(api frontend.API, x frontend.Variable) [32]frontend.Variable

func ToInt

func ToInt[T ~uint | ~uint64 | ~int64](i T) int

ToInt converts a uint, uint64 or int64 to an int, panicking on overflow. Due to its use of generics, it is inefficient to use in loops than run a "cryptographic" number of iterations. Use type-specific functions in such cases.

func ToUint16

func ToUint16[T ~int | ~uint](i T) uint16

func ToUint64

func ToUint64[T constraints.Signed](i T) uint64

ToUint64 converts a signed integer into a uint64, panicking on negative values. Due to its use of generics, it is inefficient to use in loops than run a "cryptographic" number of iterations. Use type-specific functions in such cases.

func ToVariableSlice

func ToVariableSlice[X any](s []X) []frontend.Variable

func ValidateHexString

func ValidateHexString(joinToErr *error, x, errWrapper string, size int)

Appends a wrapped error to `err` if x is not a valid hexstring. The err wrapper must be a string of the form "err message : %w". Size if the expected length of the string. Set to -1 to ignore the length. The length refers to the number of encoded bytes, not the number of characters.

func ValidateTimestamps

func ValidateTimestamps(joinToErr *error, timestamps ...uint)

Validate that the timestamps are increasing.

func WrapErrsAlphabetically

func WrapErrsAlphabetically(errs []error) error

func WriteToFile

func WriteToFile(path string, from io.WriterTo) error

func WriteToJSON

func WriteToJSON(path string, v interface{}) error

func WriterstoEqual

func WriterstoEqual(expected, actual io.WriterTo) error

Types

type BytesEqualError

type BytesEqualError struct {
	Index int
	// contains filtered or unexported fields
}

func (*BytesEqualError) Error

func (e *BytesEqualError) Error() string

type Range

type Range struct {
	Min, Max int
}

Directories

Path Synopsis
Package csvtraces provides a way to read and write traces in CSV format.
Package csvtraces provides a way to read and write traces in CSV format.

Jump to

Keyboard shortcuts

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