Skip to content

Instantly share code, notes, and snippets.

@animetosho
Created November 28, 2022 12:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save animetosho/1859ee02891e8718125011d1aead8dec to your computer and use it in GitHub Desktop.
Save animetosho/1859ee02891e8718125011d1aead8dec to your computer and use it in GitHub Desktop.
Compute Affine table for GF(2^8) multiplication with 0x11d polynomial
#include <stdio.h>
#include <x86intrin.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
__m128i polymask1 = _mm_set1_epi64x(0xff00ffffff000000ULL); // polynomial 0x11d: 0x1d = 0b11101, reverse bits -> 10111000, then replace 1->ff, 0->00
for(int val=0; val<256; val++) {
__m128i addvals1 = _mm_set_epi8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80);
__m128i valtest = _mm_set1_epi8(val);
__m128i addmask = _mm_cmpgt_epi8(_mm_setzero_si128(), valtest);
__m128i depmask1 = _mm_and_si128(addvals1, addmask);
for(int i=0; i<7; i++) {
/* rotate */
__m128i last = _mm_shuffle_epi8(depmask1, _mm_set1_epi8(0));
depmask1 = _mm_srli_epi64(depmask1, 8);
/* XOR poly */
depmask1 = _mm_xor_si128(depmask1, _mm_and_si128(polymask1, last));
valtest = _mm_add_epi8(valtest, valtest);
addmask = _mm_cmpgt_epi8(_mm_setzero_si128(), valtest);
depmask1 = _mm_xor_si128(depmask1, _mm_and_si128(addvals1, addmask));
}
// if((val & 0xf) == 0)
// printf("\n");
uint64_t ans = _mm_cvtsi128_si64(depmask1);
printf("0x%llxULL,", ans);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment