Documentation
¶
Index ¶
- Constants
- Variables
- func CheckLength(length, maxAllowed int) error
- func CheckPositive(value int, name string) error
- func ConstantTimeEqual(a, b []byte) bool
- func ConstantTimeSelect(condition int, a, b []byte) []byte
- func HashConcat(inputs ...[]byte) []byte
- func HashWithDomain(domain string, data []byte) []byte
- func RandomInt(max int) (int, error)
- func SHA3256(input []byte) []byte
- func SafeMakeByteSlice(count, maxAllowed int) ([]byte, error)
- func SafeMakeInt32Slice(count, maxAllowed int) ([]int32, error)
- func SafeMakeIntSlice(count, maxAllowed int) ([]int, error)
- func SafeMultiply(a, b int) (int, error)
- func SafeMultiply3(a, b, c int) (int, error)
- func SafeReadLength(data []byte, offset, maxAllowed int) (length int, newOffset int, err error)
- func SampleGaussianVector(seed []byte, n int, sigma float64) []int32
- func SampleVectorZq(seed []byte, n, q int) []int32
- func SecureRandomBytes(n int) ([]byte, error)
- func Shake256(input []byte, outputLen int) []byte
- func Shake256Into(input []byte, output []byte)
- func Shake256WithDomain(domain string, data []byte, outputLen int) []byte
- func ValidateSeedEntropy(seed []byte) error
- func ValidateSliceAccess(data []byte, offset, size int) error
- func Zeroize(b []byte)
- func ZeroizeInt32(s []int32)
- func ZeroizeInt8(s []int8)
Constants ¶
const ( // MaxVectorLength is the maximum allowed length for vectors (e.g., SLSS secret, TDD factors). MaxVectorLength = 1 << 20 // 1M elements // MaxMatrixElements is the maximum allowed number of elements in a matrix. MaxMatrixElements = 1 << 24 // 16M elements // MaxTensorElements is the maximum allowed number of elements in a tensor. MaxTensorElements = 1 << 26 // 64M elements // MaxFactorCount is the maximum allowed number of factors (e.g., TDD rank). MaxFactorCount = 1000 // MaxMessageSize is the maximum allowed message size in bytes. MaxMessageSize = 1 << 20 // 1MB // MaxPayloadLength is the maximum allowed payload length for serialized data. MaxPayloadLength = 1 << 28 // 256MB // MaxWalkLength is the maximum allowed walk length for EGRW. MaxWalkLength = 1 << 16 // 64K steps )
Maximum allowed lengths for various data types to prevent DoS via large allocations.
const ( // MaxHashConcatInputSize prevents integer overflow and collision attacks in HashConcat. // Each input must be <= 100MB. This provides safe encoding while remaining reasonable. MaxHashConcatInputSize = 100 * 1024 * 1024 )
Variables ¶
var ( // ErrOverflow indicates an integer overflow occurred. ErrOverflow = errors.New("integer overflow") // ErrExceedsLimit indicates a value exceeds the allowed limit. ErrExceedsLimit = errors.New("value exceeds allowed limit") // ErrInvalidLength indicates an invalid length value. ErrInvalidLength = errors.New("invalid length") )
var RandReader io.Reader = rand.Reader
Functions ¶
func CheckLength ¶ added in v1.0.1
CheckLength validates that length is within [0, maxAllowed].
func CheckPositive ¶ added in v1.0.1
CheckPositive validates that value is > 0.
func ConstantTimeEqual ¶
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 ¶
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 ¶
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. SECURITY: Validates input sizes to prevent integer overflow and hash collisions.
func HashWithDomain ¶
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 ¶
RandomInt generates a cryptographically secure random integer in [0, max). It uses rejection sampling to ensure a uniform distribution.
func SHA3256 ¶
SHA3256 computes the SHA3-256 cryptographic hash of the input. It returns a 32-byte hash.
func SafeMakeByteSlice ¶ added in v1.0.1
SafeMakeByteSlice creates a byte slice with bounds checking.
func SafeMakeInt32Slice ¶ added in v1.0.1
SafeMakeInt32Slice creates an int32 slice with bounds checking. Returns error if count is negative, exceeds maxAllowed, or would cause overflow.
func SafeMakeIntSlice ¶ added in v1.0.1
SafeMakeIntSlice creates an int slice with bounds checking.
func SafeMultiply ¶ added in v1.0.1
SafeMultiply multiplies two non-negative integers and returns an error if overflow occurs.
func SafeMultiply3 ¶ added in v1.0.1
SafeMultiply3 multiplies three non-negative integers and returns an error if overflow occurs.
func SafeReadLength ¶ added in v1.0.1
SafeReadLength reads a uint32 length from data at offset, validates it, and returns the value. Returns error if not enough bytes available or length exceeds maxAllowed.
func SampleGaussianVector ¶
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 ¶
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 ¶
SecureRandomBytes generates n cryptographically secure random bytes. It uses crypto/rand, which relies on the operating system's CSPRNG.
func Shake256 ¶
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 ¶
Shake256Into computes SHAKE256 and writes the output into the provided buffer.
func Shake256WithDomain ¶
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 ¶
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 ValidateSliceAccess ¶ added in v1.0.1
ValidateSliceAccess checks that accessing data[offset:offset+size] is safe.
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 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.