permutations.h 3.49 KB
Newer Older
Martin Schlaeffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef PERMUTATIONS_H_
#define PERMUTATIONS_H_

#include <stdint.h>

#include "api.h"
#include "ascon.h"
#include "config.h"
#include "printstate.h"
#include "round.h"

#define ASCON_128_KEYBYTES 16
#define ASCON_128A_KEYBYTES 16
#define ASCON_80PQ_KEYBYTES 20

#define ASCON_128_RATE 8
#define ASCON_128A_RATE 16
Enrico Pozzobon committed
18
#define ASCON_HASH_RATE 8
Martin Schlaeffer committed
19 20 21

#define ASCON_128_PA_ROUNDS 12
#define ASCON_128_PB_ROUNDS 6
Enrico Pozzobon committed
22 23

#define ASCON_128A_PA_ROUNDS 12
Martin Schlaeffer committed
24 25
#define ASCON_128A_PB_ROUNDS 8

Enrico Pozzobon committed
26 27 28 29 30 31
#define ASCON_HASH_PA_ROUNDS 12
#define ASCON_HASH_PB_ROUNDS 12

#define ASCON_HASHA_PA_ROUNDS 12
#define ASCON_HASHA_PB_ROUNDS 8

Martin Schlaeffer committed
32 33
#define ASCON_HASH_BYTES 32

Enrico Pozzobon committed
34 35 36 37 38 39 40
#define ASCON_128_IV WORD_T(0x80400c0600000000ull)
#define ASCON_128A_IV WORD_T(0x80800c0800000000ull)
#define ASCON_80PQ_IV WORD_T(0xa0400c0600000000ull)
#define ASCON_HASH_IV WORD_T(0x00400c0000000100ull)
#define ASCON_HASHA_IV WORD_T(0x00400c0400000100ull)
#define ASCON_XOF_IV WORD_T(0x00400c0000000000ull)
#define ASCON_XOFA_IV WORD_T(0x00400c0400000000ull)
Martin Schlaeffer committed
41 42 43 44 45 46 47

#define ASCON_HASH_IV0 WORD_T(0xee9398aadb67f03dull)
#define ASCON_HASH_IV1 WORD_T(0x8bb21831c60f1002ull)
#define ASCON_HASH_IV2 WORD_T(0xb48a92db98d5da62ull)
#define ASCON_HASH_IV3 WORD_T(0x43189921b8f8e3e8ull)
#define ASCON_HASH_IV4 WORD_T(0x348fa5c9d525e140ull)

Enrico Pozzobon committed
48 49 50 51 52 53
#define ASCON_HASHA_IV0 WORD_T(0x01470194fc6528a6ull)
#define ASCON_HASHA_IV1 WORD_T(0x738ec38ac0adffa7ull)
#define ASCON_HASHA_IV2 WORD_T(0x2ec8e3296c76384cull)
#define ASCON_HASHA_IV3 WORD_T(0xd6f6a54d7f52377dull)
#define ASCON_HASHA_IV4 WORD_T(0xa13c42a223be8d87ull)

Martin Schlaeffer committed
54 55 56 57 58 59
#define ASCON_XOF_IV0 WORD_T(0xb57e273b814cd416ull)
#define ASCON_XOF_IV1 WORD_T(0x2b51042562ae2420ull)
#define ASCON_XOF_IV2 WORD_T(0x66a3a7768ddf2218ull)
#define ASCON_XOF_IV3 WORD_T(0x5aad0a7a8153650cull)
#define ASCON_XOF_IV4 WORD_T(0x4f3e0e32539493b6ull)

Enrico Pozzobon committed
60 61 62 63 64 65
#define ASCON_XOFA_IV0 WORD_T(0x44906568b77b9832ull)
#define ASCON_XOFA_IV1 WORD_T(0xcd8d6cae53455532ull)
#define ASCON_XOFA_IV2 WORD_T(0xf7b5212756422129ull)
#define ASCON_XOFA_IV3 WORD_T(0x246885e1de0d225bull)
#define ASCON_XOFA_IV4 WORD_T(0xa8cb5ce33449973full)

Martin Schlaeffer committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#define START(n) ((3 + (n)) << 4 | (12 - (n)))
#define RC(c) WORD_T(c)

forceinline void P12ROUNDS(state_t* s) {
  ROUND(s, RC(0xf0));
  ROUND(s, RC(0xe1));
  ROUND(s, RC(0xd2));
  ROUND(s, RC(0xc3));
  ROUND(s, RC(0xb4));
  ROUND(s, RC(0xa5));
  ROUND(s, RC(0x96));
  ROUND(s, RC(0x87));
  ROUND(s, RC(0x78));
  ROUND(s, RC(0x69));
  ROUND(s, RC(0x5a));
  ROUND(s, RC(0x4b));
}

forceinline void P8ROUNDS(state_t* s) {
  ROUND(s, RC(0xb4));
  ROUND(s, RC(0xa5));
  ROUND(s, RC(0x96));
  ROUND(s, RC(0x87));
  ROUND(s, RC(0x78));
  ROUND(s, RC(0x69));
  ROUND(s, RC(0x5a));
  ROUND(s, RC(0x4b));
}

forceinline void P6ROUNDS(state_t* s) {
  ROUND(s, RC(0x96));
  ROUND(s, RC(0x87));
  ROUND(s, RC(0x78));
  ROUND(s, RC(0x69));
  ROUND(s, RC(0x5a));
  ROUND(s, RC(0x4b));
}

forceinline void PROUNDS(state_t* s, int nr) {
  for (int i = START(nr); i > 0x4a; i -= 0x0f) ROUND(s, RC(i));
}

#if ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS

forceinline void P(state_t* s, int nr) {
  if (nr == 12) P12ROUNDS(s);
  if (nr == 8) P8ROUNDS(s);
  if (nr == 6) P6ROUNDS(s);
}

#elif !ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS

void P12(state_t* s);
void P8(state_t* s);
void P6(state_t* s);

forceinline void P(state_t* s, int nr) {
  if (nr == 12) P12(s);
  if (nr == 8) P8(s);
  if (nr == 6) P6(s);
}

#elif ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS

forceinline void P(state_t* s, int nr) { PROUNDS(s, nr); }

#else /* !ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS */

void P(state_t* s, int nr);

#endif

#endif /* PERMUTATIONS_H_ */