round.h 1.9 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6
#ifndef ROUND_H_
#define ROUND_H_

#include "ascon.h"
#include "printstate.h"

Martin Schläffer committed
7 8 9 10 11 12 13 14 15 16 17 18
forceinline void LINEAR_LAYER(state_t* s, uint64_t xtemp) {
  uint64_t temp;
  temp = s->x[2] ^ ROR(s->x[2], 28 - 19);
  s->x[0] = s->x[2] ^ ROR(temp, 19);
  temp = s->x[4] ^ ROR(s->x[4], 6 - 1);
  s->x[2] = s->x[4] ^ ROR(temp, 1);
  temp = s->x[1] ^ ROR(s->x[1], 41 - 7);
  s->x[4] = s->x[1] ^ ROR(temp, 7);
  temp = s->x[3] ^ ROR(s->x[3], 61 - 39);
  s->x[1] = s->x[3] ^ ROR(temp, 39);
  temp = xtemp ^ ROR(xtemp, 17 - 10);
  s->x[3] = xtemp ^ ROR(temp, 10);
Martin Schläffer committed
19 20
}

Martin Schläffer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
forceinline void NONLINEAR_LAYER(state_t* s, word_t* xtemp, uint8_t pos) {
  uint8_t t0;
  uint8_t t1;
  uint8_t t2;
  // Based on the round description of Ascon given in the Bachelor's thesis:
  //"Optimizing Ascon on RISC-V" of Lars Jellema
  // see https://github.com/Lucus16/ascon-riscv/
  t0 = XOR8(s->b[1][pos], s->b[2][pos]);
  t1 = XOR8(s->b[0][pos], s->b[4][pos]);
  t2 = XOR8(s->b[3][pos], s->b[4][pos]);
  s->b[4][pos] = OR8(s->b[3][pos], NOT8(s->b[4][pos]));
  s->b[4][pos] = XOR8(s->b[4][pos], t0);
  s->b[3][pos] = XOR8(s->b[3][pos], s->b[1][pos]);
  s->b[3][pos] = OR8(s->b[3][pos], t0);
  s->b[3][pos] = XOR8(s->b[3][pos], t1);
  s->b[2][pos] = XOR8(s->b[2][pos], t1);
  s->b[2][pos] = OR8(s->b[2][pos], s->b[1][pos]);
  s->b[2][pos] = XOR8(s->b[2][pos], t2);
  s->b[1][pos] = AND8(s->b[1][pos], NOT8(t1));
  s->b[1][pos] = XOR8(s->b[1][pos], t2);
  s->b[0][pos] = OR8(s->b[0][pos], t2);
  (*xtemp).b[pos] = XOR8(s->b[0][pos], t0);
Martin Schläffer committed
43 44
}

Martin Schläffer committed
45
forceinline void ROUND(state_t* s, uint8_t C) {
Enrico Pozzobon committed
46
  word_t xtemp;
Martin Schläffer committed
47
  /* round constant */
Martin Schläffer committed
48
  s->b[2][0] = XOR8(s->b[2][0], C);
Martin Schläffer committed
49
  /* s-box layer */
Martin Schläffer committed
50
  for (uint8_t i = 0; i < 8; i++) NONLINEAR_LAYER(s, &xtemp, i);
Martin Schläffer committed
51
  /* linear layer */
Martin Schläffer committed
52
  LINEAR_LAYER(s, xtemp.x);
Martin Schläffer committed
53 54 55
  printstate(" round output", s);
}

Martin Schläffer committed
56 57 58 59 60 61 62 63
forceinline void PROUNDS(state_t* s, int nr) {
  int i = START(nr);
  do {
    ROUND(s, RC(i));
    i += INC;
  } while (i != END);
}

Martin Schläffer committed
64
#endif /* ROUND_H_ */