{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "afc512ca",
   "metadata": {},
   "source": [
    "## Speck Simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "efb373e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as pyplot\n",
    "import numpy as np\n",
    "from scipy.stats import pearsonr\n",
    "import random\n",
    "from operator import xor\n",
    "\n",
    "# Fast implementation of the Hamming weight for 64 bit values\n",
    "# See book: A Hacker's delight\n",
    "def popcount(x):\n",
    "    x -= (x >> 1) & 0x5555555555555555\n",
    "    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)\n",
    "    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f\n",
    "    return ((x * 0x0101010101010101) & 0xffffffffffffffff ) >> 56"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "306edb76",
   "metadata": {},
   "outputs": [],
   "source": [
    "# not sure if the hamming weight model w0rks for Speck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "7654d5a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "NUM_ROUNDS = 22\n",
    "BLOCK_SIZE = 32\n",
    "KEY_SIZE = 64\n",
    "WORD_SIZE = 16\n",
    "\n",
    "\n",
    "# SHIFTs for SPECK\n",
    "ALPHA = 7\n",
    "BETA = 2\n",
    "\n",
    "mod_mask = (2 ** WORD_SIZE) -1\n",
    "mod_mask_sub = (2 ** WORD_SIZE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "5c3ade39",
   "metadata": {},
   "outputs": [],
   "source": [
    "def bytesToWords16(b):       \n",
    "    return [(b >> (x * WORD_SIZE)) & mod_mask for x in\n",
    "                      range(0, math.ceil(KEY_SIZE // WORD_SIZE))]\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "953621aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    " The 16bit Speck roundfunction\n",
    "'''\n",
    "def ER16(x, y, k):\n",
    "\n",
    "    rs_x = ((x << (16 - ALPHA)) + (x >> ALPHA)) & mod_mask\n",
    "\n",
    "    add_sxy = (rs_x + y) & mod_mask\n",
    "\n",
    "    new_x = k ^ add_sxy\n",
    "\n",
    "    ls_y = ((y >> (16 - BETA)) + (y << BETA)) & mod_mask\n",
    "\n",
    "    new_y = new_x ^ ls_y\n",
    "\n",
    "    return new_x, new_y\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b7a7d5d",
   "metadata": {},
   "source": [
    "## Running the key schedule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "15e86ae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "key_schedule = bytesToWords16(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "93e4fb4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "The 16 bit python key schedule\n",
    "\n",
    "void Speck128256KeySchedule(u64 K[],u64 rk[])\n",
    "{\n",
    "    u64 i,D=K[3],C=K[2],B=K[1],A=K[0];\n",
    "    for(i=0;i<33;){\n",
    "        rk[i]=A; ER64(B,A,i++);\n",
    "        rk[i]=A; ER64(C,A,i++);\n",
    "        rk[i]=A; ER64(D,A,i++);\n",
    "    }\n",
    "    rk[i]=A;\n",
    "}\n",
    "'''\n",
    "def key_schedule(k):\n",
    "\n",
    "    D=k[3]\n",
    "    C=k[2]\n",
    "    B=k[1]\n",
    "    A=k[0]\n",
    "    out = []\n",
    "    i = 0\n",
    "    while i < 21:\n",
    "        out.append(A)\n",
    "        B, A = ER16(B, A, i)\n",
    "        i += 1\n",
    "        out.append(A)\n",
    "        C, A = ER16(C, A, i)\n",
    "        i+= 1\n",
    "        out.append(A)\n",
    "        D, A = ER16(D, A, i)\n",
    "        i+= 1\n",
    "    out.append(A)\n",
    "    return out\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0061be29",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e25af6e8",
   "metadata": {},
   "source": [
    "## Running tests to verify the output is still fine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "1dd758a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert key_schedule(l_schedule) == [0x708,0xf32, 0x2bf1,0x8035,0xa48e,0x8482, 0x74ee, 0xf589, 0xb396, 0xb231, 0xdab2, 0x57bc, 0x704e,0x9947,0xe2d2, 0xea6a, 0x4ebe, 0xdd24, 0x6b64, 0x3ab1, 0x1c57, 0x7bde]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1553c623",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b1da8b8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f090c78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d7a82e9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "973fb6fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}