Skip to content
Johannes Roth edited this page Nov 23, 2023 · 2 revisions

Memory Alignment

When we allocate memory for poly (at least in MLDSA, probably also in MLKEM), then the memory has to be 128-byte aligned. The memory further has to be consecutive for matrices. The _gcry_mldsa_polymatrix_create() from the non-avx2 implementation allocates separate memory blocks (per row). Instead, allocate one large memory-aligned block.

Example:

polyvecl rowbuf[2];

becomes

gcry_mldsa_polyvec *rowbuf_alloc = xmalloc(2 * params->l * sizeof(gcry_mldsa_poly) + /*extra space for alignment*/256);
gcry_mldsa_polyvec *rowbuf = (gcry_mldsa_polyvec*)((uintptr_t)rowbuf_alloc + (128 - ((uintptr_t)rowbuf_alloc % 128))); // pointer to aligned memory
xfree(rowbuf_alloc);

Note: probably have to adjust pointer arithmetic s.t. arithmetic on polyvecl* works, e.g. buf + 1 has to be computed differently when it's not an array of size sizeof(polyvecl)

Clone this wiki locally