common

package
v1.17.27 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package common provides shared utilities for post-quantum crypto implementations

Package common provides shared utilities for post-quantum crypto implementations

Index

Constants

View Source
const (
	// HashLength is the expected length of the hash
	HashLength = 32
	// AddressLength is the expected length of the address
	AddressLength = 20
)

Variables

View Source
var (
	Big0   = big.NewInt(0)
	Big1   = big.NewInt(1)
	Big2   = big.NewInt(2)
	Big3   = big.NewInt(3)
	Big256 = big.NewInt(256)
	Big257 = big.NewInt(257)
)

Common big integers often used

Functions

func AllocateCombined added in v1.2.10

func AllocateCombined(sizes ...int) []byte

AllocateCombined allocates a single buffer for multiple data segments

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex returns the hexadecimal encoding of d.

func ClearBytes added in v1.2.10

func ClearBytes(b []byte)

ClearBytes securely clears sensitive data

func ConstantTimeSelect added in v1.2.10

func ConstantTimeSelect(v int, a, b []byte) []byte

ConstantTimeSelect returns a if v == 1, b if v == 0

func CopyBytes

func CopyBytes(b []byte) (copiedBytes []byte)

CopyBytes returns an exact copy of the provided bytes.

func CopyWithPadding added in v1.2.10

func CopyWithPadding(dst, src []byte, padValue byte)

CopyWithPadding copies source to destination with padding if needed

func CreateSignatureBuffer added in v1.2.10

func CreateSignatureBuffer(size int) []byte

CreateSignatureBuffer creates a signature buffer with proper initialization

func DeriveKey added in v1.2.10

func DeriveKey(seed []byte, label string, outputLen int) []byte

DeriveKey derives deterministic key material from seed

func FillRandomBytes added in v1.2.10

func FillRandomBytes(rand io.Reader, buf []byte) error

FillRandomBytes fills existing buffer with random bytes

func FromHex

func FromHex(s string) []byte

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func GenerateDeterministicBytes added in v1.2.10

func GenerateDeterministicBytes(seed []byte, length int) []byte

GenerateDeterministicBytes generates deterministic bytes from seed

func GenerateRandomBytes added in v1.2.10

func GenerateRandomBytes(rand io.Reader, size int) ([]byte, error)

GenerateRandomBytes generates random bytes with validation

func GetHasher added in v1.2.10

func GetHasher(h HashFunc) hash.Hash

GetHasher returns a hash function instance

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func IsHexAddress

func IsHexAddress(s string) bool

IsHexAddress verifies whether a string can represent a valid hex-encoded Ethereum address or not.

func Max added in v1.2.10

func Max(a, b int) int

Max returns the maximum of two integers

func Min added in v1.2.10

func Min(a, b int) int

Min returns the minimum of two integers

func SafeCopy added in v1.2.10

func SafeCopy(dst, src []byte) error

SafeCopy performs bounds-checked copy

func SecureCompare added in v1.2.10

func SecureCompare(a, b []byte) bool

SecureCompare performs constant-time comparison

func SplitBuffer added in v1.2.10

func SplitBuffer(buffer []byte, sizes ...int) [][]byte

SplitBuffer splits a buffer into segments of specified sizes

func ValidateBufferSize added in v1.2.10

func ValidateBufferSize(buf []byte, expectedSize int, name string) error

ValidateBufferSize checks if buffer has expected size

func ValidateMode added in v1.2.10

func ValidateMode(mode int, min, max int) error

ValidateMode checks if a mode value is within valid range

func ValidateRandomSource added in v1.2.10

func ValidateRandomSource(rand io.Reader) error

ValidateRandomSource checks if random source is not nil

func XOF added in v1.2.10

func XOF(seed []byte, outputLen int) []byte

XOF extends output using a hash function as XOF

Types

type Address

type Address [AddressLength]byte

Address represents the 20 byte address of an Ethereum account.

func BigToAddress

func BigToAddress(b *big.Int) Address

BigToAddress returns Address with byte values of b. If b is larger than len(h), b will be cropped from the left.

func BytesToAddress

func BytesToAddress(b []byte) Address

BytesToAddress returns Address with value b. If b is larger than len(h), b will be cropped from the left.

func HexToAddress

func HexToAddress(s string) Address

HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.

func (Address) Big

func (a Address) Big() *big.Int

Big converts an address to a big integer.

func (Address) Bytes

func (a Address) Bytes() []byte

Bytes gets the string representation of the underlying address.

func (Address) Cmp

func (a Address) Cmp(b Address) int

Cmp compares two addresses.

func (Address) Format

func (a Address) Format(s fmt.State, c rune)

Format implements fmt.Formatter.

func (Address) Hash

func (a Address) Hash() Hash

Hash converts an address to a hash by left-padding it with zeros.

func (Address) Hex

func (a Address) Hex() string

Hex returns an EIP55-compliant hex string representation of the address.

func (Address) MarshalText

func (a Address) MarshalText() ([]byte, error)

MarshalText returns the hex representation of a.

func (*Address) Scan

func (a *Address) Scan(src interface{}) error

Scan implements Scanner for database/sql.

func (*Address) SetBytes

func (a *Address) SetBytes(b []byte)

SetBytes sets the address to the value of b. If b is larger than len(a), b will be cropped from the left.

func (Address) String

func (a Address) String() string

String implements fmt.Stringer.

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*Address) UnmarshalText

func (a *Address) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (Address) Value

func (a Address) Value() (driver.Value, error)

Value implements valuer for database/sql.

type Hash

type Hash [HashLength]byte

Hash represents the 32 byte Keccak256 hash of arbitrary data.

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash sets byte representation of b to hash. If b is larger than len(h), b will be cropped from the left.

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.

func HexToHash

func HexToHash(s string) Hash

HexToHash sets byte representation of s to hash. If b is larger than len(h), b will be cropped from the left.

func (Hash) Big

func (h Hash) Big() *big.Int

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes gets the byte representation of the underlying hash.

func (Hash) Format

func (h Hash) Format(s fmt.State, c rune)

Format implements fmt.Formatter.

func (Hash) Generate

func (h Hash) Generate(rand *rand.Rand, size int) interface{}

Generate implements testing/quick.Generator.

func (Hash) Hex

func (h Hash) Hex() string

Hex converts a hash to a hex string.

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Hash) Scan

func (h *Hash) Scan(src interface{}) error

Scan implements Scanner for database/sql.

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

SetBytes sets the hash to the value of b. If b is larger than len(h), b will be cropped from the left.

func (Hash) String

func (h Hash) String() string

String implements the stringer interface and is used also by the logger when doing full logging into a file.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (Hash) Value

func (h Hash) Value() (driver.Value, error)

Value implements valuer for database/sql.

type HashFunc added in v1.2.10

type HashFunc int

HashFunc represents available hash functions

const (
	SHA256Hash HashFunc = iota
	SHA512Hash
	SHA3_256Hash
	SHA3_512Hash
)

Directories

Path Synopsis
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.
Package math provides integer math utilities.
Package math provides integer math utilities.

Jump to

Keyboard shortcuts

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