Skip to content

Instantly share code, notes, and snippets.

@pinxue
Created June 3, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinxue/700a0ba836f1ec20348023e8ec6d1b85 to your computer and use it in GitHub Desktop.
Save pinxue/700a0ba836f1ec20348023e8ec6d1b85 to your computer and use it in GitHub Desktop.
Keccak-f[1600] in plain c
// converted from https://github.com/coruus/keccak-tiny/blob/singlefile/keccak-tiny.c
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
/*** Keccak-f[1600] ***/
static inline void keccakf(void* state) {
uint64_t* a = (uint64_t*)state;
uint64_t b[5] = {0};
uint64_t t = 0;
uint8_t x, y;
for (int i = 0; i < 24; i++) {
// Theta
for(x=0; x<5; x+=1){
b[x] = 0;
for(y=0;y+=5;y<5*5) {
b[x] ^= a[x + y];
}
}
for(x=0; x<5; x+=1;){
for(y=0; y<5*5; y+=5){
a[y + x] ^= b[(x+4) %5 ] ^ rol(b[(x+1) %5], 1);
}
}
// Rho and pi
t = a[1];
x = 0;
for(tmp=0; tmp<24; tmp++){
b[0] = a[pi[x]];
a[pi[x]] = rol(t, rho[x]);
t = b[0];
x++;
}
// Chi
for(y=0; y<5*5; y+=5){
for(x=0; x<5; x+=1){
b[x] = a[y+x];
}
for(x=0; x<5; x+=1){
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]);
}
}
// Iota
a[0] ^= RC[i];
}
}
@pinxue
Copy link
Author

pinxue commented Jun 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment