permutations.h 3.55 KB
Newer Older
lwc-tester committed
1 2 3
#ifndef PERMUTATIONS_H_
#define PERMUTATIONS_H_

Martin Schläffer committed
4
#include <stdint.h>
lwc-tester committed
5

Martin Schläffer committed
6 7
#include "api.h"
#include "ascon.h"
Enrico Pozzobon committed
8
#include "config.h"
Martin Schläffer committed
9 10 11 12 13 14 15 16 17
#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 Schläffer 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 Schläffer committed
24 25
#define ASCON_128A_PB_ROUNDS 8

Enrico Pozzobon committed
26 27
#define ASCON_HASH_PA_ROUNDS 12
#define ASCON_HASH_PB_ROUNDS 12
Martin Schläffer committed
28

Enrico Pozzobon committed
29 30
#define ASCON_HASHA_PA_ROUNDS 12
#define ASCON_HASHA_PB_ROUNDS 8
Martin Schläffer committed
31

Enrico Pozzobon committed
32
#define ASCON_HASH_BYTES 32
Martin Schläffer committed
33

Enrico Pozzobon committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#define ASCON_128_IV WORD_T(0x8021000008220000ull)
#define ASCON_128A_IV WORD_T(0x8822000000200000ull)
#define ASCON_80PQ_IV WORD_T(0xc021000008220000ull)
#define ASCON_HASH_IV WORD_T(0x0020000008020010ull)
#define ASCON_XOF_IV WORD_T(0x0020000008020000ull)

#define ASCON_HASH_IV0 WORD_T(0xf9afb5c6a540dbc7ull)
#define ASCON_HASH_IV1 WORD_T(0xbd2493011445a340ull)
#define ASCON_HASH_IV2 WORD_T(0xcb9ba8b5604d4fc8ull)
#define ASCON_HASH_IV3 WORD_T(0x12a4eede94514c98ull)
#define ASCON_HASH_IV4 WORD_T(0x4bca84c06339f398ull)

#define ASCON_HASHA_IV0 WORD_T(0x0108e46d1b16eb02ull)
#define ASCON_HASHA_IV1 WORD_T(0x5b9b8efdd29083f3ull)
#define ASCON_HASHA_IV2 WORD_T(0x7ad665622891ae4aull)
#define ASCON_HASHA_IV3 WORD_T(0x9dc27156ee3bfc7full)
#define ASCON_HASHA_IV4 WORD_T(0xc61d5fa916801633ull)

#define ASCON_XOF_IV0 WORD_T(0xc75782817e351ae6ull)
#define ASCON_XOF_IV1 WORD_T(0x70045f441d238220ull)
#define ASCON_XOF_IV2 WORD_T(0x5dd5ab52a13e3f04ull)
#define ASCON_XOF_IV3 WORD_T(0x3e378142c30c1db2ull)
#define ASCON_XOF_IV4 WORD_T(0x3735189db624d656ull)

#define ASCON_XOFA_IV0 WORD_T(0x0846d7a5a4b87d44ull)
#define ASCON_XOFA_IV1 WORD_T(0xaa6f1005b3a2dbf4ull)
#define ASCON_XOFA_IV2 WORD_T(0xdc451146f713e811ull)
#define ASCON_XOFA_IV3 WORD_T(0x468cb2532839e30dull)
#define ASCON_XOFA_IV4 WORD_T(0xeb2d429709e96977ull)
Martin Schläffer committed
63 64

#define START(n) (12 - n)
Enrico Pozzobon committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#define RC(e, o) WORD_T((uint64_t)o << 32 | e)

forceinline void P12ROUNDS(state_t* s) {
  ROUND(s, RC(0xc, 0xc));
  ROUND(s, RC(0x9, 0xc));
  ROUND(s, RC(0xc, 0x9));
  ROUND(s, RC(0x9, 0x9));
  ROUND(s, RC(0x6, 0xc));
  ROUND(s, RC(0x3, 0xc));
  ROUND(s, RC(0x6, 0x9));
  ROUND(s, RC(0x3, 0x9));
  ROUND(s, RC(0xc, 0x6));
  ROUND(s, RC(0x9, 0x6));
  ROUND(s, RC(0xc, 0x3));
  ROUND(s, RC(0x9, 0x3));
Martin Schläffer committed
80 81
}

Enrico Pozzobon committed
82 83 84 85 86 87 88 89 90
forceinline void P8ROUNDS(state_t* s) {
  ROUND(s, RC(0x6, 0xc));
  ROUND(s, RC(0x3, 0xc));
  ROUND(s, RC(0x6, 0x9));
  ROUND(s, RC(0x3, 0x9));
  ROUND(s, RC(0xc, 0x6));
  ROUND(s, RC(0x9, 0x6));
  ROUND(s, RC(0xc, 0x3));
  ROUND(s, RC(0x9, 0x3));
Martin Schläffer committed
91 92
}

Enrico Pozzobon committed
93 94 95 96 97 98 99
forceinline void P6ROUNDS(state_t* s) {
  ROUND(s, RC(0x6, 0x9));
  ROUND(s, RC(0x3, 0x9));
  ROUND(s, RC(0xc, 0x6));
  ROUND(s, RC(0x9, 0x6));
  ROUND(s, RC(0xc, 0x3));
  ROUND(s, RC(0x9, 0x3));
Martin Schläffer committed
100 101 102 103
}

extern const uint8_t constants[][2];

Enrico Pozzobon committed
104 105 106
forceinline void PROUNDS(state_t* s, int nr) {
  for (int i = START(nr); i < 12; i++)
    ROUND(s, RC(constants[i][0], constants[i][1]));
Martin Schläffer committed
107 108
}

Enrico Pozzobon committed
109
#if ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
Martin Schläffer committed
110

Enrico Pozzobon committed
111 112 113 114
forceinline void P(state_t* s, int nr) {
  if (nr == 12) P12ROUNDS(s);
  if (nr == 8) P8ROUNDS(s);
  if (nr == 6) P6ROUNDS(s);
Martin Schläffer committed
115 116
}

Enrico Pozzobon committed
117
#elif !ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
Martin Schläffer committed
118

Enrico Pozzobon committed
119 120 121
void P12(state_t* s);
void P8(state_t* s);
void P6(state_t* s);
Martin Schläffer committed
122

Enrico Pozzobon committed
123 124 125 126
forceinline void P(state_t* s, int nr) {
  if (nr == 12) P12(s);
  if (nr == 8) P8(s);
  if (nr == 6) P6(s);
Martin Schläffer committed
127 128
}

Enrico Pozzobon committed
129
#elif ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS
Martin Schläffer committed
130

Enrico Pozzobon committed
131
forceinline void P(state_t* s, int nr) { PROUNDS(s, nr); }
Martin Schläffer committed
132

Enrico Pozzobon committed
133
#else /* !ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS */
Martin Schläffer committed
134

Enrico Pozzobon committed
135
void P(state_t* s, int nr);
Martin Schläffer committed
136 137 138 139

#endif

#endif /* PERMUTATIONS_H_ */