/* Copyright (C) 2021 SCARV project <info@scarv.org>
 *
 * Use of this source code is restricted per the MIT license, a copy of which 
 * can be found at https://opensource.org/licenses/MIT (or should be included 
 * as LICENSE.txt within the associated archive or repository).
 */

#include "photon.h"

uint8_t M[ 8 ][ 8 ] = {
  { 0x2, 0x4, 0x2, 0xB, 0x2, 0x8, 0x5, 0x6 },
  { 0xC, 0x9, 0x8, 0xD, 0x7, 0x7, 0x5, 0x2 },
  { 0x4, 0x4, 0xD, 0xD, 0x9, 0x4, 0xD, 0x9 },
  { 0x1, 0x6, 0x5, 0x1, 0xC, 0xD, 0xF, 0xE },
  { 0xF, 0xC, 0x9, 0xD, 0xE, 0x5, 0xE, 0xD },
  { 0x9, 0xE, 0x5, 0xF, 0x4, 0xC, 0x9, 0x6 },
  { 0xC, 0x2, 0x2, 0xA, 0x3, 0x1, 0x1, 0xE },
  { 0xF, 0x1, 0xD, 0xA, 0x5, 0xA, 0x2, 0x3 },
};

uint8_t SBOX[ 16 ] = {
  0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 
  0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2
};

uint8_t GF2N_MUL( uint8_t x, uint8_t y ) {
  uint8_t t = 0;

  for( int i = 3; i >= 0; i-- ) {
    t = t << 1;

    if( ( t >> 4 ) & 1 ) {
      t = t ^ 0x13;
    }

    if( ( y >> i ) & 1 ) {
      t = t ^    x;
    }
  }

  return t;
}
