QSC Post Quantum Cryptographic Library 1.0.0.6c (A6)
A post quantum secure library written in Ansi C
 
Loading...
Searching...
No Matches
memutils.h File Reference

Contains common memory-related functions implemented using SIMD instructions. More...

#include "common.h"
#include "intrinsics.h"

Go to the source code of this file.

Macros

#define QSC_MEMUTILS_MEMORY_FENCE()
 A memory fence macro.
 
#define QSC_MEMUTILS_CACHE_LINE_SIZE   64ULL
 The default cache line size.
 
#define QSC_MEMUTILS_MEMORY_PAGE_SIZE   4096ULL
 The default memory page size.
 

Functions

QSC_EXPORT_API void qsc_memutils_flush_cache_line (void *address)
 Flush a cache line.
 
QSC_EXPORT_API void qsc_memutils_prefetch_l1 (uint8_t *address, size_t length)
 Prefetch memory to L1 cache.
 
QSC_EXPORT_API void qsc_memutils_prefetch_l2 (uint8_t *address, size_t length)
 Prefetch memory to L2 cache.
 
QSC_EXPORT_API void qsc_memutils_prefetch_l3 (uint8_t *address, size_t length)
 Prefetch memory to L3 cache.
 
QSC_EXPORT_API void * qsc_memutils_malloc (size_t length)
 Allocate a block of memory.
 
QSC_EXPORT_API void * qsc_memutils_realloc (void *block, size_t length)
 Resize a block of memory.
 
QSC_EXPORT_API void qsc_memutils_alloc_free (void *block)
 Free a memory block created with malloc.
 
QSC_EXPORT_API void * qsc_memutils_aligned_alloc (int32_t align, size_t length)
 Allocate an aligned 8-bit integer array.
 
QSC_EXPORT_API void * qsc_memutils_aligned_realloc (void *block, size_t length)
 Reallocate an aligned 8-bit integer array.
 
QSC_EXPORT_API void qsc_memutils_aligned_free (void *block)
 Free an aligned memory block.
 
QSC_EXPORT_API void qsc_memutils_clear (void *output, size_t length)
 Erase a block of memory.
 
QSC_EXPORT_API bool qsc_memutils_array_uniform (const uint8_t *input, size_t length)
 Check if all array members are the same.
 
QSC_EXPORT_API bool qsc_memutils_are_equal (const uint8_t *a, const uint8_t *b, size_t length)
 Compare two byte arrays for equality.
 
QSC_EXPORT_API bool qsc_memutils_are_equal_128 (const uint8_t *a, const uint8_t *b)
 Compare two 16-byte arrays for equality.
 
QSC_EXPORT_API bool qsc_memutils_are_equal_256 (const uint8_t *a, const uint8_t *b)
 Compare two 32-byte arrays for equality.
 
QSC_EXPORT_API bool qsc_memutils_are_equal_512 (const uint8_t *a, const uint8_t *b)
 Compare two 64-byte arrays for equality.
 
QSC_EXPORT_API void qsc_memutils_copy (void *output, const void *input, size_t length)
 Copy a block of memory.
 
QSC_EXPORT_API void qsc_memutils_clmulepi64_si128 (uint64_t r[2], const uint64_t a[2], const uint64_t b[2], int32_t imm8)
 Emulate the _mm_clmulepi64_si128 intrinsic.
 
QSC_EXPORT_API void qsc_memutils_clmulepi64_si256_avx (__m128i r[4], const __m128i a[2], const __m128i b[2])
 Multiply two 256-bit field elements (each represented as two 128-bit integers) to produce a 512-bit product.
 
QSC_EXPORT_API void qsc_memutils_clmulepi64_si256 (uint64_t r[8], const uint64_t a[4], const uint64_t b[4])
 Multiply two 256-bit field elements (each represented as two 128-bit integers) to produce a 512-bit product.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_be128 (const uint8_t *a, const uint8_t *b)
 Compare two 16-byte arrays as 128-bit big-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_be256 (const uint8_t *a, const uint8_t *b)
 Compare two 32-byte arrays as 256-bit big-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_be512 (const uint8_t *a, const uint8_t *b)
 Compare two 64-byte arrays as 512-bit big-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_le128 (const uint8_t *a, const uint8_t *b)
 Compare two 16-byte arrays as 128-bit little-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_le256 (const uint8_t *a, const uint8_t *b)
 Compare two 32-byte arrays as 256-bit little-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API bool qsc_memutils_greater_than_le512 (const uint8_t *a, const uint8_t *b)
 Compare two 64-byte arrays as 512-bit little-endian integers to determine if A is greater than B.
 
QSC_EXPORT_API void qsc_memutils_move (void *output, const void *input, size_t length)
 Move a block of memory, erasing the previous location.
 
QSC_EXPORT_API void qsc_memutils_secure_erase (void *block, size_t length)
 Securely erase a block of memory.
 
QSC_EXPORT_API void qsc_memutils_secure_free (void *block, size_t length)
 Free a secure memory block.
 
QSC_EXPORT_API void * qsc_memutils_secure_malloc (size_t length)
 Allocate a secure 8-bit integer array.
 
QSC_EXPORT_API void qsc_memutils_set_value (void *output, size_t length, uint8_t value)
 Set a block of memory to a specific value.
 
QSC_EXPORT_API void qsc_memutils_xor (uint8_t *output, const uint8_t *input, size_t length)
 Bitwise XOR two blocks of memory.
 
QSC_EXPORT_API void qsc_memutils_xorv (uint8_t *output, const uint8_t value, size_t length)
 Bitwise XOR a block of memory with a byte value.
 
QSC_EXPORT_API bool qsc_memutils_zeroed (const void *input, size_t length)
 Test if an array is entirely zeroed.
 

Detailed Description

Contains common memory-related functions implemented using SIMD instructions.

This header defines utility functions for memory allocation, reallocation, secure memory operations, and memory comparisons. It provides functions to flush cache lines, prefetch memory at various cache levels, clear memory blocks, and perform constant-time comparisons.

Reference Links:

Function Documentation

◆ qsc_memutils_aligned_alloc()

QSC_EXPORT_API void * qsc_memutils_aligned_alloc ( int32_t align,
size_t length )

Allocate an aligned 8-bit integer array.

Parameters
align[int32_t] The memory alignment boundary.
length[size_t] The length of the requested block.
Returns
[void*] Returns the aligned array of bytes, or NULL on failure.

◆ qsc_memutils_aligned_free()

QSC_EXPORT_API void qsc_memutils_aligned_free ( void * block)

Free an aligned memory block.

Parameters
block[void*] A pointer to the memory block to release.

◆ qsc_memutils_aligned_realloc()

QSC_EXPORT_API void * qsc_memutils_aligned_realloc ( void * block,
size_t length )

Reallocate an aligned 8-bit integer array.

Parameters
block[void*] The current memory block.
length[size_t] The new length of the block.
Returns
[void*] Returns the aligned array of bytes, or NULL on failure.

◆ qsc_memutils_alloc_free()

QSC_EXPORT_API void qsc_memutils_alloc_free ( void * block)

Free a memory block created with malloc.

Parameters
block[void*] A pointer to the memory block to release.

◆ qsc_memutils_are_equal()

QSC_EXPORT_API bool qsc_memutils_are_equal ( const uint8_t * a,
const uint8_t * b,
size_t length )

Compare two byte arrays for equality.

Parameters
a[const uint8_t*] A pointer to the first array.
b[const uint8_t*] A pointer to the second array.
length[size_t] The number of bytes to compare.
Returns
[bool] Returns true if the arrays are equivalent.

◆ qsc_memutils_are_equal_128()

QSC_EXPORT_API bool qsc_memutils_are_equal_128 ( const uint8_t * a,
const uint8_t * b )

Compare two 16-byte arrays for equality.

Parameters
a[const uint8_t*] A pointer to the first array.
b[const uint8_t*] A pointer to the second array.
Returns
[bool] Returns true if the arrays are equivalent.

◆ qsc_memutils_are_equal_256()

QSC_EXPORT_API bool qsc_memutils_are_equal_256 ( const uint8_t * a,
const uint8_t * b )

Compare two 32-byte arrays for equality.

Parameters
a[const uint8_t*] A pointer to the first array.
b[const uint8_t*] A pointer to the second array.
Returns
[bool] Returns true if the arrays are equivalent.

◆ qsc_memutils_are_equal_512()

QSC_EXPORT_API bool qsc_memutils_are_equal_512 ( const uint8_t * a,
const uint8_t * b )

Compare two 64-byte arrays for equality.

Parameters
a[const uint8_t*] A pointer to the first array.
b[const uint8_t*] A pointer to the second array.
Returns
[bool] Returns true if the arrays are equivalent.

◆ qsc_memutils_array_uniform()

QSC_EXPORT_API bool qsc_memutils_array_uniform ( const uint8_t * input,
size_t length )

Check if all array members are the same.

Parameters
input[const uint8_t*] A pointer to the array to test.
length[size_t] The number of bytes in the array.
Returns
[bool] Returns true if array members are all equivalent.

◆ qsc_memutils_clear()

QSC_EXPORT_API void qsc_memutils_clear ( void * output,
size_t length )

Erase a block of memory.

Parameters
output[void*] A pointer to the memory block to erase.
length[size_t] The number of bytes to erase.

◆ qsc_memutils_clmulepi64_si128()

QSC_EXPORT_API void qsc_memutils_clmulepi64_si128 ( uint64_t r[2],
const uint64_t a[2],
const uint64_t b[2],
int32_t imm8 )

Emulate the _mm_clmulepi64_si128 intrinsic.

Parameters
r[uint64_t[2]] Output array of two 64-bit integers representing the 128-bit result.
a[const uint64_t[2]] Input 128-bit operand, represented as an array of two 64-bit values.
b[const uint64_t[2]] Input 128-bit operand, represented as an array of two 64-bit values.
imm8[int32_t] Controls which 64-bit halves to use.

◆ qsc_memutils_clmulepi64_si256()

QSC_EXPORT_API void qsc_memutils_clmulepi64_si256 ( uint64_t r[8],
const uint64_t a[4],
const uint64_t b[4] )

Multiply two 256-bit field elements (each represented as two 128-bit integers) to produce a 512-bit product.

Parameters
r[uint64_t[8]] 512-bit product (r[0] = least-significant 64 bits, r[7] = most-significant 64 bits).
a[const uint64_t[4]] First 256-bit operand (a[0] = lower 64 bits, a[3] = upper 64 bits).
b[const uint64_t[4]] Second 256-bit operand (same ordering).

◆ qsc_memutils_clmulepi64_si256_avx()

QSC_EXPORT_API void qsc_memutils_clmulepi64_si256_avx ( __m128i r[4],
const __m128i a[2],
const __m128i b[2] )

Multiply two 256-bit field elements (each represented as two 128-bit integers) to produce a 512-bit product.

Parameters
r[__m128i[4]] 512-bit product (r[0] = least-significant 128 bits, r[3] = most-significant 128 bits).
a[const __m128i[2]] First 256-bit operand (a[0] = lower 128 bits, a[1] = upper 128 bits).
b[const __m128i[2]] Second 256-bit operand (same ordering).

◆ qsc_memutils_copy()

QSC_EXPORT_API void qsc_memutils_copy ( void * output,
const void * input,
size_t length )

Copy a block of memory.

Parameters
output[void*] A pointer to the destination array.
input[const void*] A pointer to the source array.
length[size_t] The number of bytes to copy.

◆ qsc_memutils_flush_cache_line()

QSC_EXPORT_API void qsc_memutils_flush_cache_line ( void * address)

Flush a cache line.

Parameters
address[void*] The memory address.

◆ qsc_memutils_greater_than_be128()

QSC_EXPORT_API bool qsc_memutils_greater_than_be128 ( const uint8_t * a,
const uint8_t * b )

Compare two 16-byte arrays as 128-bit big-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_greater_than_be256()

QSC_EXPORT_API bool qsc_memutils_greater_than_be256 ( const uint8_t * a,
const uint8_t * b )

Compare two 32-byte arrays as 256-bit big-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_greater_than_be512()

QSC_EXPORT_API bool qsc_memutils_greater_than_be512 ( const uint8_t * a,
const uint8_t * b )

Compare two 64-byte arrays as 512-bit big-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_greater_than_le128()

QSC_EXPORT_API bool qsc_memutils_greater_than_le128 ( const uint8_t * a,
const uint8_t * b )

Compare two 16-byte arrays as 128-bit little-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_greater_than_le256()

QSC_EXPORT_API bool qsc_memutils_greater_than_le256 ( const uint8_t * a,
const uint8_t * b )

Compare two 32-byte arrays as 256-bit little-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_greater_than_le512()

QSC_EXPORT_API bool qsc_memutils_greater_than_le512 ( const uint8_t * a,
const uint8_t * b )

Compare two 64-byte arrays as 512-bit little-endian integers to determine if A is greater than B.

Parameters
a[const uint8_t*] Pointer to the primary array.
b[const uint8_t*] Pointer to the comparison array.
Returns
[bool] Returns true if array A is greater than array B.

◆ qsc_memutils_malloc()

QSC_EXPORT_API void * qsc_memutils_malloc ( size_t length)

Allocate a block of memory.

Parameters
length[size_t] The length of the requested block.
Returns
[void*] Returns the aligned array of bytes, or NULL on failure.

◆ qsc_memutils_move()

QSC_EXPORT_API void qsc_memutils_move ( void * output,
const void * input,
size_t length )

Move a block of memory, erasing the previous location.

Parameters
output[void*] Pointer to the destination array.
input[const void*] Pointer to the source array.
length[size_t] The number of bytes to copy.

◆ qsc_memutils_prefetch_l1()

QSC_EXPORT_API void qsc_memutils_prefetch_l1 ( uint8_t * address,
size_t length )

Prefetch memory to L1 cache.

Parameters
address[uint8_t*] The array memory address.
length[size_t] The number of bytes to prefetch.

◆ qsc_memutils_prefetch_l2()

QSC_EXPORT_API void qsc_memutils_prefetch_l2 ( uint8_t * address,
size_t length )

Prefetch memory to L2 cache.

Parameters
address[uint8_t*] The array memory address.
length[size_t] The number of bytes to prefetch.

◆ qsc_memutils_prefetch_l3()

QSC_EXPORT_API void qsc_memutils_prefetch_l3 ( uint8_t * address,
size_t length )

Prefetch memory to L3 cache.

Parameters
address[uint8_t*] The array memory address.
length[size_t] The number of bytes to prefetch.

◆ qsc_memutils_realloc()

QSC_EXPORT_API void * qsc_memutils_realloc ( void * block,
size_t length )

Resize a block of memory.

Parameters
block[void*] The current memory block.
length[size_t] The new length of the block.
Returns
[void*] Returns the aligned array of bytes, or NULL on failure.

◆ qsc_memutils_secure_erase()

QSC_EXPORT_API void qsc_memutils_secure_erase ( void * block,
size_t length )

Securely erase a block of memory.

Parameters
block[void*] Pointer to the memory block.
length[size_t] The length of the block.

◆ qsc_memutils_secure_free()

QSC_EXPORT_API void qsc_memutils_secure_free ( void * block,
size_t length )

Free a secure memory block.

Parameters
block[void*] Pointer to the memory block.
length[size_t] The length of the requested block.

◆ qsc_memutils_secure_malloc()

QSC_EXPORT_API void * qsc_memutils_secure_malloc ( size_t length)

Allocate a secure 8-bit integer array.

Parameters
length[size_t] The length of the requested block.
Returns
[void*] Returns the secure allocated memory block, or NULL on failure.

◆ qsc_memutils_set_value()

QSC_EXPORT_API void qsc_memutils_set_value ( void * output,
size_t length,
uint8_t value )

Set a block of memory to a specific value.

Parameters
output[void*] Pointer to the destination array.
length[size_t] The number of bytes to change.
value[uint8_t] The value to set each byte.

◆ qsc_memutils_xor()

QSC_EXPORT_API void qsc_memutils_xor ( uint8_t * output,
const uint8_t * input,
size_t length )

Bitwise XOR two blocks of memory.

Parameters
output[uint8_t*] Pointer to the destination array.
input[const uint8_t*] Pointer to the source array.
length[size_t] The number of bytes to XOR.

◆ qsc_memutils_xorv()

QSC_EXPORT_API void qsc_memutils_xorv ( uint8_t * output,
const uint8_t value,
size_t length )

Bitwise XOR a block of memory with a byte value.

Parameters
output[uint8_t*] Pointer to the destination array.
value[uint8_t] The byte value.
length[size_t] The number of bytes to XOR.

◆ qsc_memutils_zeroed()

QSC_EXPORT_API bool qsc_memutils_zeroed ( const void * input,
size_t length )

Test if an array is entirely zeroed.

Parameters
input[const void*] Pointer to the input array.
length[size_t] The length of the array.
Returns
[bool] Returns true if the array is zeroed.