utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RandReader io.Reader = rand.Reader

Functions

func ConstantTimeEqual

func ConstantTimeEqual(a, b []byte) bool

ConstantTimeEqual compares two byte slices in constant time. It returns true if the slices are equal, false otherwise. This function leaks only the length of the slices.

func ConstantTimeSelect

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

ConstantTimeSelect returns a if condition is 1, b if condition is 0. condition must be 0 or 1. a and b must have the same length.

func HashConcat

func HashConcat(inputs ...[]byte) []byte

HashConcat computes the SHA3-256 hash of the concatenation of multiple byte slices. Each slice is prefixed with its length (4 bytes, little-endian) to ensure unique encoding.

func HashWithDomain

func HashWithDomain(domain string, data []byte) []byte

HashWithDomain computes a domain-separated SHA3-256 hash. It prefixes the data with the length of the domain string and the domain string itself. This prevents collisions between different uses of the hash function. Panics if domain is longer than 255 bytes.

func RandomInt

func RandomInt(max int) (int, error)

RandomInt generates a cryptographically secure random integer in [0, max). It uses rejection sampling to ensure a uniform distribution.

func SHA3256

func SHA3256(input []byte) []byte

SHA3256 computes the SHA3-256 cryptographic hash of the input. It returns a 32-byte hash.

func SampleGaussianVector

func SampleGaussianVector(seed []byte, n int, sigma float64) []int32

SampleGaussianVector samples a vector of integers from a discrete Gaussian distribution. It uses the Box-Muller transform on uniform random bytes generated from a seed via SHAKE256. The result is rounded to the nearest integer.

func SampleVectorZq

func SampleVectorZq(seed []byte, n, q int) []int32

SampleVectorZq samples a uniform random vector in Z_q^n. It uses rejection sampling on bytes generated from a seed via SHAKE256. This ensures the distribution is uniform modulo q.

func SecureRandomBytes

func SecureRandomBytes(n int) ([]byte, error)

SecureRandomBytes generates n cryptographically secure random bytes. It uses crypto/rand, which relies on the operating system's CSPRNG.

func Shake256

func Shake256(input []byte, outputLen int) []byte

Shake256 computes the SHAKE256 extendable output function (XOF). It takes an input byte slice and generates an output of the specified length. This is used for generating pseudo-random bytes from a seed.

func Shake256Into

func Shake256Into(input []byte, output []byte)

Shake256Into computes SHAKE256 and writes the output into the provided buffer.

func Shake256WithDomain

func Shake256WithDomain(domain string, data []byte, outputLen int) []byte

Shake256WithDomain computes SHAKE256 with domain separation. It works like HashWithDomain but produces an output of arbitrary length. Panics if domain is longer than 255 bytes.

func ValidateSeedEntropy

func ValidateSeedEntropy(seed []byte) error

ValidateSeedEntropy checks if a seed has sufficient entropy. It performs basic statistical tests to reject obviously weak seeds (e.g., all zeros, sequential). This is a sanity check, not a rigorous randomness test.

func Zeroize

func Zeroize(b []byte)

Zeroize overwrites a byte slice with zeros. This is used to clear sensitive data from memory. Uses runtime.KeepAlive to prevent compiler optimization from eliminating the stores.

func ZeroizeInt8

func ZeroizeInt8(s []int8)

ZeroizeInt8 overwrites an int8 slice with zeros.

func ZeroizeInt32

func ZeroizeInt32(s []int32)

ZeroizeInt32 overwrites an int32 slice with zeros. Uses runtime.KeepAlive to prevent compiler optimization from eliminating the stores.

Types

This section is empty.

Jump to

Keyboard shortcuts

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