zkp-stark 0.2.1

Implementation of the STARK ZK-proof system
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Performance analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import re\n",
    "from datetime import datetime\n",
    "import numpy as np\n",
    "from mpmath import mp\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.axes_grid1 import host_subplot\n",
    "import mpl_toolkits.axisartist as AA\n",
    "import math\n",
    "import warnings\n",
    "from ipywidgets import interact, interactive, fixed, interact_manual\n",
    "plt.rcParams['figure.dpi'] = 90\n",
    "plt.rcParams['figure.figsize'] = [24.0, 16.0]\n",
    "plt.rcParams['text.latex.unicode'] = False\n",
    "plt.rcParams['text.usetex'] = False\n",
    "plt.rcParams['mathtext.fontset'] = 'stixsans'\n",
    "plt.rcParams['font.family'] = 'DejaVu Sans'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Log parsing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def events(log):\n",
    "    start = None\n",
    "    events = []\n",
    "    for line in log.split('\\n'):\n",
    "        match = re.search(r'^\\[(.+Z) TRACE [:\\w]+\\] (BEGIN|END) (.*)$', line)\n",
    "        if match:\n",
    "            (time, event, name) = match.groups()\n",
    "            time = datetime.strptime(time, '%Y-%m-%dT%H:%M:%S.%fZ')\n",
    "            if start:\n",
    "                time -= start\n",
    "                time = time.total_seconds()\n",
    "            else:\n",
    "                start = time\n",
    "                time = 0\n",
    "            begin = event == 'BEGIN'\n",
    "            events += [(time, begin, name)]\n",
    "    return events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process(events):\n",
    "    stack = []\n",
    "    spans = []\n",
    "    histogram = {}\n",
    "    last = None\n",
    "    for event in events:\n",
    "        (time, begin, name) = event\n",
    "        # Collect top of stack histogram\n",
    "        if last:\n",
    "            top_of_stack = stack[-1][2]\n",
    "            duration = time - last\n",
    "            if top_of_stack in histogram:\n",
    "                histogram[top_of_stack] += duration\n",
    "            else:\n",
    "                histogram[top_of_stack] = duration\n",
    "        last = time\n",
    "        # Compute spans\n",
    "        if begin:\n",
    "            stack += [event]\n",
    "        else:\n",
    "            (start, _, previous_name) = stack[-1]\n",
    "            stack = stack[:-1]\n",
    "            assert name == previous_name\n",
    "            spans += [(len(stack), start, time, name)]\n",
    "    histogram = {k: v for k, v in sorted(histogram.items(), key=lambda item: -item[1])}\n",
    "    print(len(spans), len(histogram))\n",
    "    return spans, histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = []\n",
    "data = []\n",
    "for i in range(10, 16):\n",
    "    i = 2**i\n",
    "    x += [i]\n",
    "    data += [process(events(open(\"../../pedersen-\" + str(i) + \".log\",\"r\").read()))]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Flame graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def flamegraph(spans):\n",
    "    labels = list(set([span[3] for span in spans]))\n",
    "    maxt = spans[-1][2]\n",
    "    colour_map = mpl.cm.rainbow(np.linspace(0, 1, len(labels)))\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(111)\n",
    "    ax.set_xlim((0,maxt))\n",
    "    ax.set_ylim((0,7))\n",
    "\n",
    "    for span in spans:\n",
    "        (depth, start, end, label) = span\n",
    "        colour = colour_map[labels.index(label)]\n",
    "        rectangle = mpl.patches.Rectangle((start, depth), end - start, 1, fc=colour, ec='white')\n",
    "        ax.add_patch(rectangle)\n",
    "        if end - start > 0.01 * maxt:\n",
    "            ax.text(start + 0.005 * maxt, depth+0.05, label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flamegraph(process(events(open(\"../../pedersen-test.log\",\"r\").read()))[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flamegraph(process(events(open(\"../../pedersen-test3.log\",\"r\").read()))[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flamegraph(data[-1][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = list(data[-1][1].keys())\n",
    "values = np.array([[hist[label] for label in labels] for (_, hist) in data]).T\n",
    "fig, ax = plt.subplots()\n",
    "y = values / np.sum(values, axis=0)\n",
    "ax.stackplot(np.log2(x), y[::-1, :], labels=labels[::-1])\n",
    "ax.set_xlim((10,15))\n",
    "ax.set_ylim((0,1))\n",
    "\n",
    "handles, labels = ax.get_legend_handles_labels()\n",
    "ax.legend(handles[::-1], labels[::-1], loc='lower left')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2**13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}