word.h 923 Bytes
Newer Older
Martin Schläffer committed
1 2 3 4
#ifndef WORD_H_
#define WORD_H_

#include <stdint.h>
Martin Schläffer committed
5
#include <string.h>
Martin Schläffer committed
6

Martin Schläffer committed
7
typedef uint64_t uint64_t;
Martin Schläffer committed
8

Enrico Pozzobon committed
9
/* get byte from 64-bit Ascon word */
Martin Schläffer committed
10 11
#define GETBYTE(x, i) ((uint8_t)((uint64_t)(x) >> (56 - 8 * (i))))

Enrico Pozzobon committed
12
/* set byte in 64-bit Ascon word */
Martin Schläffer committed
13 14
#define SETBYTE(b, i) ((uint64_t)(b) << (56 - 8 * (i)))

Enrico Pozzobon committed
15
/* set padding byte in 64-bit Ascon word */
Martin Schläffer committed
16 17
#define PAD(i) SETBYTE(0x80, i)

Enrico Pozzobon committed
18
/* load bytes into 64-bit Ascon word */
Martin Schläffer committed
19 20 21 22 23 24
static inline uint64_t LOADBYTES(const uint8_t* bytes, int n) {
  uint64_t x = 0;
  for (int i = 0; i < n; ++i) x |= SETBYTE(bytes[i], i);
  return x;
}

Enrico Pozzobon committed
25
/* store bytes from 64-bit Ascon word */
Martin Schläffer committed
26 27 28 29
static inline void STOREBYTES(uint8_t* bytes, uint64_t x, int n) {
  for (int i = 0; i < n; ++i) bytes[i] = GETBYTE(x, i);
}

Enrico Pozzobon committed
30
/* clear bytes in 64-bit Ascon word */
Martin Schläffer committed
31 32 33 34 35 36
static inline uint64_t CLEARBYTES(uint64_t x, int n) {
  for (int i = 0; i < n; ++i) x &= ~SETBYTE(0xff, i);
  return x;
}

#endif /* WORD_H_ */