\subsection{Motivation, Landscape and Current Problem}

    As computer processor clock speeds have begun to stagnate in frequency increases per year, manufacturers have begun to shift focus to multiple cores in search of greater performance.
    Supercomputers for decades have operated around the idea of clusters and massive parallelism.
    As such, there is now more than ever a need to study the effects of parallelism in computation and to be able to effectively model concurrent communicating systems as they start to become commonplace in all computational applications.

\subsection{Scope and Relevance of Research}

    The following aims to provide a visualisation of the parallel computation of multiple processes within the context of a concurrency calculus.
    This will be divided into several modular parts that apart demonstrate some challenges on successful implementation, but together form what should prove to be a useful proof-of-concept tool to aid in understanding of calculi of communicating processes.\\

    In particular, the project tackles the details of implementation of a computational calculus of communicating mobile processes.
    This implementation is then further extended to provide an interactive visualisation of computations to aid in the understanding of how such calculi operate.
    As discussed later in~\ref{subsec:pi-calculus}, the project aims to provide an improvement over the $\pi$-calculus described by~\cite{pi-calculus}, while still remaining functionally equivalent and equally expressive.

\subsection{Project Aims}

    The project seeks to provide an implementation of both the Solo Calculus described by~\cite{solo-calculus}, for which there exists an encoding of the $\pi$-calculus within itself, and also of Solo Diagrams described by~\cite{solo-diagrams}, an intuitively graphical representation of the calculus.
    These diagrams can then aid in understanding of reductions of process calculi, while maintaining a clean and simple language with strong, provable properties.
    Furthermore, the design of the Solo Calculus is such that it is fully asynchronous.
    That is, unlike in the $\pi$-calculus where processes ‘block’ while waiting for inputs/outputs, it is shown through the construction of the Solo Calculus that no such system is required --- however there still exists a way of building such a system to give the effects of the $\pi$-calculus should it be desired.
    For these reasons, the Solo Calculus is found to be an interesting alternative to the more common $\pi$-calculus.

\subsection{Overview of Dissertation Structure}

    The first section details an in-depth review of the surrounding literature and current state of the art.
    This includes an examination of various computational calculi, both concurrent and not, and an evaluation of their effectiveness for their given use-cases.
    Included also are some short examples to give a feel of how each calculus is used.\\

    Afterwards follows a short project requirements specification and investigation on technologies planned to be used in this project and justifications as to why each was chosen.\\

    Following that is then a breakdown of the development process, description of some simple algorithms involved and the pitfalls which may not be immediately obvious.
    This section attempts to show both the initial and final forms of the project and what difficulties caused this evolution.\\

    Finally, there is a short conclusion on the effectiveness of the design choices made and how a similar project could be conducted differently.\\


\section{Price response functions}\label{sec:response_functions}

In Sect. \ref{subsec:response_function_trade} we analyze the responses
functions in trade time scale and in Sect.
\ref{subsec:response_function_physical} we analyze the responses functions in
physical time scale.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Response functions on trade time scale}
\label{subsec:response_function_trade}

The price response function in trade time scale is defined as
\cite{my_paper_response_financial}
\begin{equation}\label{eq:response_functions_trade_scale_general}
    R^{\left(\textrm{t}\right)}_{i}\left(\tau\right)=\left\langle
    r^{\left(\textrm{t}\right)}_{i}\left(t-1,\tau \right)
    \varepsilon_{i}^{\left(\textrm{t}\right)}
    \left(t, n\right)\right\rangle _{T}.
\end{equation}
To compute the response functions on trade time scale, we use both, the trade
signs and the returns from the tick-by-tick original data during a week in
market time. Then, the response is averaged by the number of trades.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=\columnwidth]
    {figures/04_responses_trade_scale.png}
    \caption{Price response functions
             $R^{\left(\textrm{t}\right)}_{i}\left(\tau\right)$ versus time
             lag $\tau$ on a logarithmic scale in trade time scale for the
             years 2008 (top), 2014 (middle) and 2019 (bottom).}
    \label{fig:response_function_trade_scale}
\end{figure}

The results of Fig. \ref{fig:response_function_trade_scale} show the
price response functions of the seven foreign exchange major pairs used in the
analysis (see Table \ref{tab:majors}) for three different years. The results
found for all the years are entirely in line with price responses seen in other
financial markets, particularly with correlated financial markets. The response
functions have an initial increasing trend to a maximum, that flattens out and
saturates at some level, and eventually slowly decrease. This shape is
explained by an initial increase caused by autocorrelated transaction flow. The
flattening out is due to the market liquidity adapting to this flow and
assuring diffusive prices \cite{EMH_lillo}. For our selected pairs, a time lag
of $\tau = 10^{3} $ trades is enough to see an increase to a maximum followed
by a decrease. Thus, the trend in the price response functions is eventually
reversed. The response signal is much more noisier in the year 2008 for the
first seconds in the time lag. This behavior is because of the smaller amount
of data of the corresponding year. In general, more data was recorded in recent
years than in past years. In the three years analyzed, the more liquid currency
pairs have a smaller response in comparison with the non-liquid pairs.  The
strength of the response function varies from one year to the other. In 2008
the strength of the signal was one order of magnitude stronger than the
response in 2014, but the signals in 2014 have approximately twice the strength
of the signals of 2019. This behavior can be explained by the fact that in
recent times algorithm trading has been used intensively. Thus, many more
trades were carried out in the last years, which means, the impact of each
trade is reduced, and then the response functions tend to decrease compared
with previous years.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Response functions on physical time scale}
\label{subsec:response_function_physical}

One important detail to compute the price response function on physical time
scale is to define how the averaging of the function will be made, because the
response functions highly differ when we include or exclude
$\varepsilon^{\left(\textrm{p}\right)}_j \left( t\right) = 0$
\cite{Wang_2016_cross}. The price responses including
$\varepsilon^{\left(\textrm{p}\right)}_j \left( t\right) = 0$ are weaker than
the excluding ones due to the omission of direct influence of the lack of
trades. However, either including or excluding
$\varepsilon^{\left(\textrm{p}\right)}_j \left( t\right) = 0$ does not change
the trend of price reversion versus the time lag, but it does affect the
response function strength \cite{Wang_2016_avg}. For a deeper analysis of the
influence of the term
$\varepsilon^{\left(\textrm{p}\right)}_j \left( t\right) = 0$ in price response
functions, we suggest reviewing Refs. \cite{Wang_2016_avg,Wang_2016_cross}. We
will only take into account the price response functions excluding
$\varepsilon^{\textrm{p}}_j \left( t\right) = 0$.

We define the price response functions on physical time scale, using
the trade signs and the returns sampled in seconds from the original data on
physical time scale. The price response function on physical time scale is
defined as \cite{my_paper_response_financial}
\begin{equation}\label{eq:response_functions_time_scale_general}
    R^{\left(\textrm{p}\right)}_{i}\left(\tau\right)=\left\langle
    r^{\left(\textrm{p}\right)}_{i}\left(t-1, \tau\right)
    \varepsilon_{i}^{\left(\textrm{p}\right)} \left(t\right)\right\rangle _{P}
\end{equation}
\begin{figure}[htbp]
    \centering
    \includegraphics[width=\columnwidth]
    {figures/04_responses_physical_scale.png}
    \caption{Price response functions
             $R^{\left(\textrm{p}\right)}_{i}\left(\tau\right)$ excluding
             $\varepsilon^{\left(\textrm{p}\right)}_{i}\left(t\right) = 0$
             versus time lag $\tau$ on a logarithmic scale in physical time
             scale for the years 2008 (top), 2014 (middle) and 2019 (bottom).}
    \label{fig:response_function_physical_scale}
\end{figure}
The results shown in Fig. \ref{fig:response_function_physical_scale} are the
price response functions on physical time scale for three different years. The
results show approximately the same behavior observed in currency exchange
pairs in trade time scale, and in correlated financial markets, where we can
see that an increase to a maximum is followed by a decrease. Thus again, the
trend in the price responses is eventually reversed. An exception occurs in the
year 2008, where the response at short time lags seems to decrease, to then
start to slightly increase, and finally it decreases again.

The price response functions on physical time scale are smoother than the
responses on trade time scale. As we reduce from trade data all the returns and
trade signs in one second to one data point on physical time scale, and as this
sampling gives the same weight to every data point, the curves look smoother.

Compared with the response functions on trade time scale, the strength of the
signal of the response functions on physical time scale are similar in
magnitude in the corresponding years. Thus, the strength of the signal in 2008
for trade time scale is similar to the strength of the signal in 2008 for
physical time scale, and so on. This behavior is different from the one
presented in correlated financial markets, where the results differ about a
factor of two depending on the time scale \cite{my_paper_response_financial}.

On physical time scale, we can see that the liquid pairs have a smaller price
response compared with non-liquid pairs. The liquidity of the pairs vary
regarding the analyzed year. For the years 2008 and 2014, the most liquid pairs
are the EUR/USD and the GBP/USD. For 2019 the most liquid pairs are EUR/USD and
USD/CAD. Therefore, the price response of a foreign exchange pair with large
activity is smaller to the small impact of each trade. Also, the former year
responses have stronger signals. We consider the same argument of algorithm
trading to explain why the signals in recent years are weaker than in older
years.


\section{Introduction}

This section describes the problem that we tackle in this thesis (i.e., large-scale patterns prediction over multiple input event streams), followed by a brief overview of our proposed approach.

%For later  an introdcution/motavation  section should be added here %
\subsection{Problem Formulation}
%refine the stream and event pattern %
%define the pattern we deal formally%
% include base line 1 batch size in experemintal results%

Given a set of $K$ real-time streams of events $S = \{ s_1,s_2, ..., s_k\}$ as input, which are associated with a set of $K$  objects $O = \{ o_1, ..., o_k\}$. Where each stream $s_i=\langle e_1,e_3...,e_t,...\rangle$  is a time-ordered sequence of events, these events are connected to a single reference object $o_i \in O$,  $e_t$  refers to the current time event within the unbounded stream, we give the definition of the input event sample as follows:
\begin{definition}
Each event is defined as a tuple of attributes $e_i = (type,\tau,a_1,a_2.....,a_n,id)$:  where $type$ is the event type attribute that takes a value from a set of finite event types/symbols $\Sigma$, $\tau$ represents the timestamp of the event tuple,  the  $a_1,a_2,...,a_n$ are spatial or other contextual features (e.g., speed), these features are varying from one application to another, while the $id$ attribute connects the event tuple to the associated domain object.
\end{definition}

A user-defined pattern $\mathcal{P}$ is given in the form of a sequence of event types, while the main goal is to provide predictions about the full matches of $\mathcal{P}$ within each event stream $s_i\in S$ in real-time.

\par The setting that is considered in this thesis is described in the following:\\
  A large-scale patterns prediction over multiple input event streams system that  consists of $K=\left\vert{S}\right\vert=\left\vert{O}\right\vert$ distributed predictor nodes $n_1,n_2...,n_k$, each of which consumes an input event stream $s_i\in S$ and provide an online predication service. Each node $i \in [K]$ consumes a single event stream $s_i$ associated with a single object $o_i \in O$, in addition,  it  maintains a local prediction model $f_i$ for the user-defined pattern $\mathcal{P}$. The online predictions about the full match of  the pattern $\mathcal{P}$ in $s_i$ is provided for each new arriving event tuple, based on the current received event $ \ e_t \in s_i \ $ and the observed previous events sequence $\{e_j \in s_i \mid  j \text{ <} t\}$. In summary, we have multiple running instances of an online prediction algorithm on distributed nodes for multiple input event streams, each instance  provides online predications about a defined pattern of events. As an illustrative application domain, we consider massive event streams that describe trajectories of  moving objects, more specifically, event streams of moving vessels in the context of maritime surveillance.  As shown in Example \ref{example:maritime}.

\begin{example} %change it to example1 and sentances order%
	Let us consider a set of possible event types
	$$\Sigma=\{changeInSpeed,stopMoving,changeInTurn\}$$
And an example of event tuple structure that describes trajectory of a moving vessel
$$e_i=(vesselId,type,timestamp,longitude,latitude,speed)$$ such that $type \in \Sigma$
. Hence, we can define a pattern $\mathcal{P_j}$ that represents speed change followed by change in turn:  $$P_j=changeInSpeed.changeInTurn$$
\label{example:maritime}
\end{example}
The defined pattern $\mathcal{P_j}$ is monitored over each event stream $s_i$  by a  predictor nodes  $n_i$  that maintains a local prediction model $f_i$, where there is one node for each vessel's event stream.  The prediction model $f_i$ gives the ability to provide an online predictions about when the pattern will be completed in the form of an expected number of future events before a full match does occur.

 \subsection{The Proposed Approach}
 \par We aim to design and develop a scalable and distributed patterns prediction system over massive input event streams (e.g., event streams of moving objects). We  exploit the event forecasting with Pattern Markov Chains \cite{alevizos2017event} as the base prediction model (i.e., $f_i$). We propose to enable the dynamic merge of the prediction models of the input event streams, by adapting the distributed online predication protocol \cite{kamp2014communication} to synchronize the distributed  models, i.e., Markov transitions probabilities of the Pattern Markov Chain (PMC) predictors.

 \par We propose a $synchronization operation$  for the distributed Pattern Markov Chain (PMC) models based on the maximum-likelihood estimation \citep{anderson1957statistical} for the transition probabilities matrix of the underlaying Markov Chain described by
 \begin{equation}
 \label{eq:pi_estim}
 \hat{p}_{i,j}=\frac{\sum_{k \in K} n_{k,i,j}}{\sum_{k \in K} \sum_{l \in L} n_{k,i,l}}
 \end{equation}

 \par Our approach relies on enabling the collaborative learning between the prediction models of  the input event streams. By doing so, we assume that the underlying event streams belong to the same  distribution and share the same behavior (e.g., mobility patterns). We claim that assumption is reasonable in many application domains, for instance, in the context of maritime surveillance, vessels travel through defined routes by International Maritime Organization (IMO). Additionally, vessels have similar mobility patterns in specific areas such as moving with low speed and multiple turns near the ports \cite{pallotta2013vessel,liu2014knowledge}. That allows our system to dynamically construct a coherent global prediction model for all input event streams based on merging its local models. %We will empirically investigate the effect of enabling the distributed online learning over %

  \par Our proposed approach is expected to impose a speed up in the learning of the prediction models with less training data, in addition, we expect to gain an improvement of the predictive performance compared to the no-distributed  version of event forecasting with Pattern Markov Chains system.
  %Also it is adaptive to the non-stationary data streams that shows a concept drift by the continuously adjusting the predictions models based on the change of input event stream characteristics (i.e., by the continuous clustering integration with distribute online learning).%

 %An outline of our approach is given in $Algorithm$ ~\ref{alg:our_approach}.

%\begin{algorithm}[h!]

%	\caption{Our Approach}
%	check figure 4 in \cite{lee2007trajectory}
%	\label{alg:our_approach}
%\end{algorithm}
% add algorthim table describes the whole approach.

% In thesis, we will also consider another research direction concerning the extension of forecasting Markov chain to  higher order by including additional contextual features of the event such as  weather conditions of moving entities. We will empirically evaluate the effect of this extension over the prediction quality.
%

% pattern defination as sequence or regex of the events types of the input stream
% group of moving objects
% adding illistruating figure


\chapter{An Example \simplesine Simulation}
\label{sec:simplesine-sim}

In the following chapters,
we use the \simplesine model extensively as part of simulations
to illustrate \TrickHLA\ in action.
In this chapter, we introduce a non-HLA \simplesine simulation
as a way to explain the basics of how the model may be used with Trick,
and we do so without any \TrickHLA\ distractions.

% ----------
\section{\tt SIM\_simplesine\_pubsub}

In this simulation, there are two {\tt sim\_object}s:
a {\em publisher} and a {\em subscriber}.\footnote{
This publisher/subscriber terminology here is only suggestive.
There is no distributed computing going on.
Data only moves from one {\tt sim\_object} to another within a single process.
Nevertheless, the simulation is a fair way to introduce how \simplesine
data structure and functions are used in Trick \sdefine and input files.
}
The publisher generates a sine wave using the analytic equations
and periodically copies the state to the subscriber.
The subscriber propagates the state approximately between updates from
the publisher.
Based on the analytic equations, the subscriber also calculates an error
in the approximate propagation.

The motivation for having the publisher and subscriber propagate the state
differently is to illustrate a technique that is useful for HLA simulations.
The owner of some data might simulate it at a very high rate but
send updates at a lower frequency.
In between these updates, a subscriber may use an approximate method to
extrapolate the data until the next update arrives.\footnote{
  Extrapolation alternatives include doing nothing, in which case the
  data increments in discontinuous steps as the remote data arrive;
  dead reckoning, in which the data are extrapolated based on derivatives;
  or numerical integration of an approximate (lower fidelity) model, in
  which case the discontinuities on the subscriber side are hopefully
  reduced sufficiently to allow the subscriber's simulation to proceed.
}
% ----------
\section{\sdefine}
\label{sec:simplesine-pubsub-sdefine}

The {\tt SIM\_simplesine\_pubsub} \sdefine file is shown below. It consists of

\begin{itemize}
  \item{
    Some {\tt \#define} statements that set relevant frequencies for
    state propagation and data copying.
  }
  \item{
    The {\tt sim\_object}s -- the usual Trick {\tt sys} object and
    publisher and subscriber objects.
  }
  \item{
    An {\tt integrate} statement which enables Trick numerical integration
    for the subscriber.
  }
\end{itemize}

\begin{lstlisting}[caption={{\tt SIM\_simplesine\_pubsub} \sdefine file},label={list:SIM-pubsub-sdefine}]
#define PROPAGATE_TIMESTEP 0.25
#define COPY_TIMESTEP 5.0

sim_object
{
  sim_services/include: EXECUTIVE exec (sim_services/include/executive.d) ;

  (automatic) sim_services/input_processor:
    input_processor( INPUT_PROCESSOR* IP = &sys.exec.ip ) ;
} sys ;

sim_object
{
  simplesine: simplesine_T simplesine (simplesine/data/simplesine.d);

  (initialization) simplesine:
    simplesine_calc(
      simplesine_T* P = &publisher.simplesine,
      double t = sys.exec.out.time );

  // Propagate the state using the analytic equations.
  (PROPAGATE_TIMESTEP, scheduled) simplesine:
    simplesine_calc(
      simplesine_T* P = &publisher.simplesine,
      double t = sys.exec.out.time );

  // Copy the propagated state to the subscriber.
  (COPY_TIMESTEP, scheduled) simplesine:
    simplesine_copyState(
      simplesine_state_T* fromP = &publisher.simplesine.state,
      simplesine_state_T* toP = &subscriber.simplesine.state );
} publisher ;

sim_object
{
  sim_services/include: INTEGRATOR integ (simplesine/data/integ.d);
  simplesine: simplesine_T simplesine (simplesine/data/simplesine.d);
  simplesine: simplesine_T err (simplesine/data/simplesine.d);

  (initialization) simplesine:
    simplesine_calc(
      simplesine_T* P = &subscriber.simplesine,
      double t = sys.exec.out.time );

  // deriv/integ jobs to propagate the state differential equation
  (derivative) simplesine:
    simplesine_deriv( simplesine_T* p = &subscriber.simplesine );
  (integration) simplesine:
    simplesine_integ(
      INTEGRATOR* I = &subscriber.integ,
      simplesine_T* p = &subscriber.simplesine );

  // calculate the error between the integrated state and the true state
  (PROPAGATE_TIMESTEP, scheduled) simplesine:
    simplesine_calcError(
      double t = sys.exec.out.time,
      simplesine_T* s = &subscriber.simplesine,
      simplesine_state_T* err = &subscriber.err.state );
} subscriber ;

integrate (PROPAGATE_TIMESTEP) subscriber;
\end{lstlisting}
% ----------
\section{Input Files}

The input files for this simulation are located in the {\tt RUN\_1} directory
and are summarized below.

\begin{itemize}
\item{
  {\tt input\_noCopy\_noInteg}.
  In this input file,
  the publisher never sends data to the subscriber, and
  the subscriber does not propagate its local state.
  The motivation here is to illustrate that nothing really happens
  on the subscriber side until the publisher sends data.
}
\item{
  {\tt input\_noInteg}.
  This input file illustrates the arrival of data at the subscriber from
  the publisher.
  By not propagating the subscriber state, the discrete arrival of
  updates is readily evident.
  In some simulations in the subsequent chapter,
  we will use this technique to illustrate the arrival of HLA data.
}
\item{
  {\tt input}.
  This file illustrates the simulation running as it is intended:
  the publisher sends data periodically to the subscriber,
  and the subscriber integrates those data in between updates.
  In this case the harmonical oscillator is so simple that the numerical
  integration is a very good approximation to the true system.
}
\end{itemize}

The first two files illustrate how to disable specific Trick jobs from
the input file using the {\tt JOB} directive.
In {\tt input\_noCopy\_noInteg}, the publisher's
publisher-to-subscriber copy job is disabled
as well as the subscriber's numerical integration jobs.
In {\tt input\_noInteg}, the subscriber's numerical integration jobs are
disabled.
The input files are shown below.

\begin{lstlisting}[caption={{\tt SIM\_simplesine\_pubsub} input file, {\tt input\_noCopy\_noInteg}},label={list:SIM-pubsub-input-noCopy-noInteg}]
#include "S_default.dat"
#include "Log_data/states.d"
#include "Modified_data/realtime.d"
#include "Modified_data/publisher.d"
#include "Modified_data/subscriber.d"

JOB publisher.simplesine_copyState(&publisher.simplesine) = Off;

JOB subscriber.simplesine_deriv(&subscriber.simplesine) = Off;
JOB subscriber.simplesine_integ(&subscriber.integ) = Off;

stop = 32.5;
\end{lstlisting}

\begin{lstlisting}[caption={{\tt SIM\_simplesine\_pubsub} input file, {\tt input\_noInteg}},label={list:SIM-pubsub-input-noInteg}]
#include "S_default.dat"
#include "Log_data/states.d"
#include "Modified_data/realtime.d"
#include "Modified_data/publisher.d"
#include "Modified_data/subscriber.d"

JOB publisher.simplesine_copyState(&publisher.simplesine) = Off;

stop = 32.5;
\end{lstlisting}

\begin{lstlisting}[caption={{\tt SIM\_simplesine\_pubsub} input file, {\tt input}},label={list:SIM-pubsub-input}]
#include "S_default.dat"
#include "Log_data/states.d"
#include "Modified_data/realtime.d"
#include "Modified_data/publisher.d"
#include "Modified_data/subscriber.d"

stop = 32.5;
\end{lstlisting}

% ----------
\section{Output}

Output from the simulation with {\tt input\_noCopy\_noInteg}
is shown in Figure~\ref{fig:SIM-pubsub-input-noCopy-noInteg}.
The plot shows the evolution of the sine wave for approximately
ten cycles.
The publisher state ($x(t)$ and $\dot{x}(t)$) evolve as expected.
The subscriber state is flatlined at its initial conditions,
since data never arrive from the publisher and the subscriber's
numerical propagation is disabled.

\begin{figure}[b]
  \begin{center}
    \includegraphics[width=4.5in]{TrickHLAUser-prelim-SIM-pubsub-input-noCopy-noInteg.png}
  \end{center}
\caption{Output from {\tt SIM\_simplesine\_pubsub} using input file {\tt input\_noCopy\_noInteg}}
\label{fig:SIM-pubsub-input-noCopy-noInteg}
\end{figure}

Output from the simulation with {\tt input\_noInteg}
is shown in Figure~\ref{fig:SIM-pubsub-input-noInteg}.
It clearly shows the discrete transfer of data from the publisher to the
subscriber.
Between the data updates, the subscriber state remains constant,
since there is still no numerical intregration on the subscriber side.
This manifests itself in errors which grow significantly until the next
update arrives, at which point the errors reset to zero.

\begin{figure}[b]
  \begin{center}
    \includegraphics[width=4.5in]{TrickHLAUser-prelim-SIM-pubsub-input-noInteg.png}
  \end{center}
\caption{Output from {\tt SIM\_simplesine\_pubsub} using input file {\tt input\_noInteg}}
\label{fig:SIM-pubsub-input-noInteg}
\end{figure}

Output from the simulation with {\tt input}
is shown in Figure~\ref{fig:SIM-pubsub-input}.
In this case, the subscriber state is ``smoothed'' in between data updates,
since the subscriber numerical integration has been enabled.
Note that there are still errors which grow between updates;
however, in this case the magnitude of those error has diminished by several
orders of magnitude.

\begin{figure}[b]
  \begin{center}
    \includegraphics[width=4.5in]{TrickHLAUser-prelim-SIM-pubsub-input.png}
  \end{center}
\caption{Output from {\tt SIM\_simplesine\_pubsub} using input file {\tt input}}
\label{fig:SIM-pubsub-input}
\end{figure}

%
% print the figures before moving to the next chapter
%
\clearpage


\newpage\section{Data | Empirical results}\label{sec:results}

\subsection{Subsection}

Some citation: \citet{Bowman:1997}.

A table:
\begin{table}[H]
	\begin{center}
		\begin{tabular}{l|rrrr||rr}\hline\hline
			&\multicolumn{4}{c}{L\'evy}&\multicolumn{2}{c}{Gaussian} \\
			\hline
			& $b_0$ & $b_1$ & $a_1$ & $a_2$ & $a_1$ & $a_2$\\
			\hline
			Estimate & 1.000 & 74.268 & $-0.192$ &  0.638& $0.686$ &  $-0.154$\\
			Std. error & 10.158 & 0.288 & 0.204 & 0.604 & 0.021 &  0.025\\
			\hline\hline
		\end{tabular}
		\caption{CARMA(2,1) Coefficient estimates of the CARMA-L\'{e}vy process), CARMA(2,0) of the Gaussian process} %\label{tab:CARMAcoef}
	\end{center}
\end{table}

\subsection{More subsections}
Link/refer to figures: Figure \ref{fig:DRP}.

More citation: \cite{Weron:2008}


\section{An Interface for the Theory of General Inductive Types}
\label{sec:outp-induct-types}

To give at least one complete example, we include here an
unabridged output for the theory of inductive types shown in
Figure~\ref{fig:wtype}.

\sourcefile{wtype.mli}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "cie.tex"
%%% End:


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{9}
\newcommand{\CommonDocsPath}{../../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge Digital Logic}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large A Simple Processor}
~\\

Figure~\ref{fig:fig1} shows a digital system that contains a number of 16-bit registers,
a multiplexer, an adder/subtracter, and a control unit (finite state machine).  Information
is input to this system via the 16-bit {\it DIN} input, which is loaded into the {\it IR}
register. Data can be transferred through the 16-bit wide multiplexer from one register
in the system to another, such as from register {\it IR} into one of the {\it general
purpose} registers $r0, \ldots, r7$.  The multiplexer's output is called {\it Buswires}
in the figure because the term {\it bus} is often used for wiring that allows data to be
transferred from one location in a system to another. The FSM controls the {\it Select}
lines of the multiplexer, which allows any of its inputs to be transferred to any register
that is connected to the bus wires.

~\\
The system can perform different operations in each clock cycle, as governed by the FSM.
It determines when particular data is placed onto the bus wires and controls which of the
registers is to be loaded with this data. For example, if the FSM selects $r0$ as the output of the
bus multiplexer and also asserts $A_{in}$, then the contents of register $r0$ will be loaded on the
next active clock edge into register {\it A}.

~\\
Addition or subtraction of signed numbers is performed by using the multiplexer to first
place one 16-bit number onto the bus wires, and then loading this number into register {\it A}.
Once this is done, a second 16-bit number is placed onto the bus, the adder/subtracter
performs the required operation, and the result is loaded into register {\it G}. The
data in {\it G} can then be transferred via the multiplexer to one of the other registers,
as required.

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale = 0.8]{figures/figure1.pdf}
	\end{center}
	\caption{A digital system.}
	\label{fig:fig1}
\end{figure}

\newpage
\noindent
A system like the one in Figure~\ref{fig:fig1} is often called a {\it processor}. It
executes operations specified in the
form of {\it instructions}. Table~\ref{tab:instructions} lists the instructions that this
processor supports. The left column shows the name of an instruction and its operands.
The meaning of the syntax {\it rX} $\leftarrow$ {\it Op2} is that the second operand,
{\it Op2}, is loaded into register {\it rX}. The operand {\it Op2} can be either a
register, {\it rY}, or {\it immediate data}, \#{\it D}.

\begin{table}[H]
\begin{center}
\begin{tabular}{rl|c}
   \multicolumn{2}{c|}{Instruction} & Function performed \\ \hline
   \rule[0.01in]{0in}{0.15in}{\it mv} & {\it rX}, $Op2$ & {\it rX} $\leftarrow Op2$ \\
		  \rule[-0.075in]{0in}{0.2in}{\it mvt} & {\it rX,} \#{\it D} & {\it rX$_{15-8}$} $\leftarrow$ {\it D$_{15-8}$}\\
   \rule[-0.075in]{0in}{0.2in}{\it add} & {\it rX}, $Op2$ & {\it rX} $\leftarrow$ {\it rX} + $Op2$ \\
   \rule[-0.075in]{0in}{0.2in}{\it sub} & {\it rX}, $Op2$ & {\it rX} $\leftarrow$ {\it rX} $-$ $Op2$ \\
\end{tabular}
\caption{Instructions performed in the processor.}
\label{tab:instructions}
\end{center}
\end{table}

\noindent
Instructions are loaded from the external input {\it DIN}, and stored into the {\it IR} register,
using the connection indicated in Figure~\ref{fig:fig1}. Each instruction is {\it encoded} using
a 16-bit format. If $Op2$ specifies a register, then the instruction encoding is
\texttt{III0XXX000000YYY}, where \texttt{III} specifies the instruction, \texttt{XXX} gives
the {\it rX} register, and \texttt{YYY} gives the {\it rY} register. If $Op2$ specifies
immediate data \#{\it D}, then the encoding is
\texttt{III1XXXDDDDDDDDD}, where the 9-bit field \texttt{DDDDDDDDD} represents the constant data.
Although only two bits are needed to encode our four instructions, we are using three bits because
other instructions will be added to the processor later. Assume that \texttt{III} $= 000$ for
the {\it mv} instruction, $001$ for {\it mvt}, $010$ for {\it add}, and $011$ for {\it sub}.

~\\
The {\it mv} instruction ({\it move}) copies the contents of one register into
another, using the syntax \texttt{mv} \texttt{rX,rY}. It can also be used to initialize a
register with immediate data, as in \texttt{mv} \texttt{rX,\#D}.  Since the data {\it D}
is represented inside the encoded instruction using only nine bits, the processor has to
{\it zero-extend} the data, as in \texttt{0000000D$_{8-0}$}, before loading it into
register~{\it rX}.  The {\it mvt} instruction ({\it move top}) is used to initialize the
most-significant byte of a register.  For {\it mvt}, only eight bits of the {\it D} field in
the instruction are used, so that \texttt{mvt} \texttt{rX,\#D} loads the value
\texttt{D$_{15-8}$00000000} into {\it rX}. As an example, to load register $r0$ with the
value \texttt{0xFF00}, you would use the instruction \texttt{mvt r0,\#0xFF00}.  The instruction
\texttt{add} \texttt{rX,rY} produces the sum {\it rX} $+$ {\it rY} and loads the result
into {\it rX}. The instruction \texttt{add} \texttt{rX,\#D} produces the
sum {\it rX} $+$ {\it D}, where {\it D} is zero-extended to 16 bits, and saves the result
in {\it rX}. Similarly, the {\it sub} instruction generates
either {\it rX} $-$ {\it rY}, or {\it rX} $-$ \#{\it D} and loads the result into {\it rX}.

~\\
Some instructions, such as an {\it add} or {\it sub}, take a few clock cycles to complete,
because multiple transfers have to be performed across the bus. The finite state machine in the
processor ``steps through'' such instructions, asserting the control signals needed in
successive clock cycles until the instruction has completed.  The processor starts executing
the instruction on the {\it DIN} input when the {\it Run} signal is asserted and the processor
asserts the {\it Done} output when the instruction is finished.  Table~\ref{tab:control_signals}
indicates the control signals from Figure~\ref{fig:fig1} that have to be
asserted in each time step to implement the instructions in Table~\ref{tab:instructions}.  The
only control signal asserted in time step $T_0$, for all instructions, is {\it IR}$_{in}$.
The meaning of {\it Select = rY} or {\it IR} in the table is that the multiplexer selects
either register {\it rY} or the immediate data in {\it IR}, depending on the value of $Op2$.
For the {\it mv} instruction, when {\it IR} is selected the multiplexer outputs
\texttt{0000000DDDDDDDDD}, and for {\it mvt} the multiplexer outputs \texttt{DDDDDDDD00000000}.
Only signals from Figure~\ref{fig:fig1} that have to be asserted in each time
step are listed in Table~\ref{tab:instructions}; all other signals are not asserted. The
meaning of {\it AddSub} in step $T_2$ of the {\it sub} instruction is that this signal is set
to 1, and this setting causes the adder/subtracter unit to perform subtraction using
2's-complement arithmetic.

~\\
The processor in Figure~\ref{fig:fig1} can perform various tasks by using a sequence of
instructions. For example, the sequence below loads the number 28 into register $r0$ and then
calculates, in register $r1$, the 2's complement value $-28$.

\begin{minipage}[t]{15 cm}
\begin{lstlisting}
       mv    r0, #28        // original number = 28
       mvt   r1, #0xFF00
       add   r1, #0x00FF    // r1 = 0xFFFF
       sub   r1, r0         // r1 = 1's-complement of r0
       add   r1, #1         // r1 = 2's-complement of r0 = -28
\end{lstlisting}
\end{minipage}

\begin{table}[H]
\begin{center}
\begin{tabular}{r|c|c|c|c|}
\multicolumn{1}{c}{~} & \multicolumn{1}{c}{$T_0$} & \multicolumn{1}{c}{$T_1$} & \multicolumn{1}{c}{$T_2$} & \multicolumn{1}{c}{$T_3$} \rule[-0.075in]{0in}{0.25in}\\ \cline{2-5}
{\it mv~} & {\it IR}$_{in}$ & \rule[-0.075in]{0in}{0.25in}{\it Select} = {\it rY} or {\it IR}, &  &  \\
~ & ~ & {\it rX$_{in}$}, {\it Done} &  &  \\ \cline{2-5}
{\it mvt~} & {\it IR}$_{in}$ & \rule[-0.075in]{0in}{0.25in}{\it Select} = {\it IR}, &  &  \\
~ & ~ & {\it rX$_{in}$}, {\it Done} &  &  \\ \cline{2-5}
\rule[-0.075in]{0in}{0.25in}{\it add~} & {\it IR}$_{in}$ & {\it Select} = {\it rX}, & {\it Select} = {\it rY} or {\it IR}, & {\it Select = G}, {\it rX$_{in}$}, \\
~ & ~ & {\it A$_{in}$} &  {\it G$_{in}$} & {\it Done} \\
\cline{2-5}
\rule[-0.075in]{0in}{0.25in}{\it sub~} & {\it IR}$_{in}$ & {\it Select} = {\it rX}, & {\it Select} = {\it rY} or {\it IR}, & {\it Select = G}, {\it rX$_{in}$}, \\
~ & ~ & {\it A$_{in}$} &  {\it AddSub}, {\it G$_{in}$} & {\it Done} \\
\cline{2-5}
\end{tabular}
\caption{Control signals asserted in each instruction/time step.}
\label{tab:control_signals}
\end{center}
\end{table}

\section*{Part I}
\addcontentsline{toc}{1}{Part I}
Implement the processor shown in Figure~\ref{fig:fig1} using VHDL code, as follows:
\begin{enumerate}
\item Make a new folder for this part of the exercise.
Part of the VHDL code for the processor is shown in parts $a$ to $c$ of
Figure~\ref{fig:fig2}, and a more complete version of the code is provided with this exercise,
in a file named {\it proc.vhd}. You can modify this code to suit your own coding style
if desired---the provided code is just a suggested solution. Fill in the missing parts of
the VHDL code to complete the design of the processor.

\lstset{language=VHDL,numbers=none,escapechar=|}
\begin{figure}[h]
\begin{center}
\begin{minipage}[t]{15 cm}
\begin{lstlisting}[name=proc]
ENTITY proc IS
    PORT ( DIN                 : IN  STD_LOGIC_VECTOR(15 DOWNTO 0);
           Resetn, Clock, Run  : IN  STD_LOGIC;
           Done                : BUFFER  STD_LOGIC);
END proc;

ARCHITECTURE Behavior OF proc IS
    |$\ldots$| declare components

    TYPE State_type IS (T0, T1, T2, T3);
    SIGNAL Tstep_Q, Tstep_D: State_type;
    |$\ldots$|
    CONSTANT mv : STD_LOGIC_VECTOR(2 DOWNTO 0) := "000";
    CONSTANT mvt : STD_LOGIC_VECTOR(2 DOWNTO 0) := "001";
    CONSTANT add : STD_LOGIC_VECTOR(2 DOWNTO 0) := "010";
    CONSTANT sub : STD_LOGIC_VECTOR(2 DOWNTO 0) := "011";
    CONSTANT Sel_R0 : STD_LOGIC_VECTOR(3 DOWNTO 0) := "0000";
    |$\ldots$|
    CONSTANT Sel_R7 : STD_LOGIC_VECTOR(3 DOWNTO 0) := "0111";
    CONSTANT Sel_G : STD_LOGIC_VECTOR(3 DOWNTO 0) := "1000";
    CONSTANT Sel_D : STD_LOGIC_VECTOR(3 DOWNTO 0) := "1001";
    CONSTANT Sel_D8 : STD_LOGIC_VECTOR(3 DOWNTO 0) := "1010" ;
             -- Sel_D is immediate data, Sel_D8 is immediate data << 8
BEGIN
\end{lstlisting}
\end{minipage}
\caption{Skeleton VHDL code for the processor. (Part $a$)}
\label{fig:fig2}
\end{center}
\end{figure}

\begin{center}
\begin{minipage}[t]{15 cm}
\begin{lstlisting}[name=proc]
    III <= IR(15 DOWNTO 13);
    IMM <= IR(12);
    rX <= IR(11 DOWNTO 9);
    rY <= IR(2 DOWNTO 0);
    decX: dec3to8 PORT MAP (rX, Xreg);

    statetable: PROCESS(Tstep_Q, Run, Done)
    BEGIN
        CASE Tstep_Q IS
            WHEN T0 =>    -- data is loaded into IR in this time step
                IF Run = '0' THEN Tstep_D <= T0;
                ELSE Tstep_D <= T1; END IF;
            WHEN T1 =>
            |$\ldots$|
        END CASE;
    END PROCESS;

    controlsignals: PROCESS (Tstep_Q, III, IMM, Xreg, rX, rY)
    BEGIN
        Done <= '0'; Ain <= '0'; |$\ldots$| default values for signals
        CASE Tstep_Q IS
            WHEN T0 => -- store DIN in IR as long as Tstep_Q = 0
                IRin <= '1';
            WHEN T1 => -- define signals in time step T1
                CASE III IS
                    WHEN mv =>
                        IF IMM = '0' THEN Sel <= '0' & rY;
                        ELSE Sel <= Sel_D; END IF;
                        Rin <= Xreg;
                        Done <= '1';
                    WHEN mvt =>
                    |$\ldots$|
                END CASE;
            WHEN T2 => -- define signals in time step T2
                CASE III IS
                    |$\ldots$|
                END CASE;
            WHEN T3 => -- define signals in time step T3
                |$\ldots$|
        END CASE;
    END PROCESS;

    fsmflipflops: PROCESS (Clock, Resetn, Tstep_D)
    BEGIN
        IF (Resetn = '0') THEN
            |$\ldots$|
    reg_0:  regn PORT MAP (BusWires, Rin(0), Clock, R0);
    reg_1:  regn PORT MAP (BusWires, Rin(1), Clock, R1);
    |$\ldots$|
    reg_7:  regn PORT MAP (BusWires, Rin(7), Clock, R7);
    |$\ldots$| instantiate other registers |and| the adder/subtracter unit
\end{lstlisting}
\end{minipage}
\end{center}

\begin{center}
Figure 2: Skeleton VHDL code for the processor. (Part $b$)
\end{center}

\begin{center}
\begin{minipage}[t]{15 cm}
\begin{lstlisting}[name=proc]
    -- define the internal bus
    busmux: PROCESS (Sel, R0, R1, R2, R3, R4, R5, R6, R7, G, IR)
    BEGIN
        CASE Sel IS
            WHEN Sel_R0 => BusWires <= R0;
            WHEN Sel_R1 => BusWires <= R1;
            |$\ldots$|
            WHEN Sel_R7 => BusWires <= R7;
            WHEN Sel_G => BusWires <= G;
            WHEN Sel_D => BusWires <= "0000000" & IR(8 DOWNTO 0);
            WHEN Sel_D8 => BusWires <= IR(7 DOWNTO 0) & "00000000";
            WHEN OTHERS => BusWires <= (OTHERS => '-');
        END CASE;
    END PROCESS;
END Behavior;

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY dec3to8 IS
    PORT ( W   : IN   STD_LOGIC_VECTOR(2 DOWNTO 0);
           Y   : OUT  STD_LOGIC_VECTOR(0 TO 7));
END dec3to8;

ARCHITECTURE Behavior OF dec3to8 IS
BEGIN
    PROCESS (W)
    BEGIN
        CASE W IS
            WHEN "000" => Y <= "10000000";
            WHEN "001" => Y <= "01000000";
            WHEN "010" => Y <= "00100000";
            |$\ldots$|
            WHEN "110" => Y <= "00000010";
            WHEN "111" => Y <= "00000001";
            WHEN OTHERS => Y <= "00000000";
        END CASE;
    END PROCESS;
END Behavior;
\end{lstlisting}
\end{minipage}
\end{center}

\begin{center}
Figure 2: Skeleton VHDL code for the processor. (Part $c$)
\end{center}

~\\
\item Set up the required subfolder and files so that your VHDL code can be compiled and
simulated using the ModelSim Simulator to verify that your processor works properly.
An example result produced by using {\it ModelSim} for a correctly-designed circuit
is given in Figure~\ref{fig:fig3}.  It shows the value \texttt{0x101C} being loaded into {\it IR}
from {\it DIN} at time 30 ns. This pattern represents the instruction \texttt{mv r0,\#28},
where the immediate value $D = 28$ (\texttt{0x1C}) is loaded into $r0$ on the clock edge at 50 ns.
The simulation results then show the instruction \texttt{mvt~r1,\#0xFF00} at 70 ns,
\texttt{add r0,\#0xFF} at 110 ns, and \texttt{sub r1,r0} at 190 ns.

You should perform a thorough simulation of your processor with the ModelSim simulator. A
sample VHDL testbench file, {\it testbench.vht}, execution script, {\it testbench.tcl},
and waveform file, {\it wave.do} are provided along with this exercise.
\end{enumerate}
\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=.95]{figures/figure3.png}
	\end{center}
	\caption{Simulation results for the processor.}
	\label{fig:fig3}
\end{figure}
\section*{Part II}
\addcontentsline{toc}{2}{Part II}
In this part we will implement the circuit depicted in Figure~\ref{fig:fig4}, in which a
memory unit and counter are connected to the processor. The
counter is used to read the contents of successive locations in the memory, and
this data is provided to the processor as a stream of instructions. To simplify the
design and testing of this circuit we have used separate clock signals, {\it PClock}
and {\it MClock}, for the processor and memory. Do the following:

\begin{enumerate}
\item A Quartus project file is provided along with this part of the exercise.  Use the
Quartus software to open this project, which is called {\it part2.qpf}.
\item A sample top-level VHDL file that instantiates the processor, memory unit, and
counter is shown in Figure~\ref{fig:procmem}. This code is provided in a file named
{\it part2.vhd}; it is the top-level file for the Quartus project {\it part2.qpf}. The
code instantiates a memory unit called {\it inst\_mem}. You have to create a VHDL file
that represents this memory unit by using the Quartus software, as described below.

~\\
\begin{figure}[H]
	\begin{center}
		\includegraphics[]{figures/figure4.pdf}
	\end{center}
	\caption{Connecting the processor to a memory unit and counter.}
	\label{fig:fig4}
\end{figure}
\newpage
\lstset{language=VHDL,numbers=none,escapechar=|}
\begin{figure}[h]
\begin{center}
\begin{minipage}[t]{15 cm}
\begin{lstlisting}[name=proc]
ENTITY part2 IS
PORT ( KEY   : IN   STD_LOGIC_VECTOR(1 DOWNTO 0);
       SW    : IN   STD_LOGIC_VECTOR(9 DOWNTO 0);
       LEDR  : OUT  STD_LOGIC_VECTOR(9 DOWNTO 0));
END part2;

ARCHITECTURE Behavior OF part2 IS
   |$\ldots$| declare components and signals
BEGIN
   Resetn <= SW(0);
   MClock <= KEY(0);
   PClock <= KEY(1);
   Run <= SW(9);
   U1: proc PORT MAP (DIN, Resetn, PClock, Run, Done);
   LEDR(0) <= Done;
   LEDR(9) <= Run;

   U2: inst_mem PORT MAP (pc, MClock, DIN);
   U3: count5 PORT MAP (Resetn, MClock, pc);
END Behavior;
|$\ldots$|
ENTITY count5 IS
PORT ( Resetn, Clock   : IN   STD_LOGIC;
       Q               : OUT  STD_LOGIC_VECTOR(4 DOWNTO 0));
END count5;

ARCHITECTURE Behavior OF count5 IS
   SIGNAL Count : STD_LOGIC_VECTOR(4 DOWNTO 0);
BEGIN
   PROCESS (Clock, Resetn)
   BEGIN
         IF (Resetn = '0') THEN
            Count <= "00000";
         ELSIF (rising_edge(Clock)) THEN
            Count <= Count + '1';
         END IF;
   END PROCESS;
   Q <= Count;
END Behavior;
\end{lstlisting}
\end{minipage}
\caption{VHDL code for the top-level entity.}
\label{fig:procmem}
\end{center}
\end{figure}

\item
A diagram of the memory unit that you need to create is depicted in Figure~\ref{fig:fig_ROM}.
Since this memory unit has only a read port, and no write port, it is called a {\it synchronous
read-only memory (synchronous ROM)}. Note that the memory unit includes a register for
synchronously loading addresses. This register is required due to the design of the memory
resources in the Intel FPGA chip.

Use the Quartus IP Catalog tool to create the memory unit, by clicking on
{\sf Tools} $>$ {\sf IP Catalog} in the Quartus software. In the IP Catalog window
choose the {\it ROM:~1-PORT} unit,
which is found under the {\sf Basic Functions $>$  On Chip Memory} category.
Select {\sf VHDL} as the type of output file to create, and give the file the name
{\it inst\_mem.vhd}.
Follow through the provided dialogue to create a memory that has one 16-bit
wide read data port and is 32 words deep. Figures~\ref{fig:fig5} and ~\ref{fig:fig6} show the
relevant pages and how to properly configure the memory.

\begin{figure}[t]
	\begin{center}
		\includegraphics[]{figures/figure_ROM.pdf}
	\end{center}
	\caption{The 32 {\sf x} 16 ROM with address register.}
	\label{fig:fig_ROM}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=1.0]{figures/figure5.png}
	\end{center}
	\caption{{Specifying memory size.}}
	\label{fig:fig5}
\end{figure}

To place processor instructions into the memory, you need to specify {\it initial values}
that should be stored in the memory when your circuit is programmed into the FPGA chip.
This can be done by initializing the memory using the contents of a {\it memory initialization
file (MIF)}. The appropriate screen is illustrated in Figure~\ref{fig:fig7}. We have specified
a file named {\it inst\_mem.mif}, which then has to be created in the folder that
contains the Quartus project. Clicking \texttt{Next} two more times will advance to the
\texttt{Summary} screen, which lists the names of files that will be created for the memory IP.
You should select {\it only} the VHDL file {\it inst\_mem.vhd}. Make sure that none of the
other types of files are selected, and then click \texttt{Finish}.

An example of a memory initialization file is given in
Figure~\ref{fig:fig_MIF}. Note that comments (\% $\ldots$ \%) are included in this file as a way of
documenting the meaning of the provided instructions.  Set the contents of
your {\it MIF} file such that it provides enough processor instructions to test your circuit.

\item The code in Figure~\ref{fig:procmem}, and the Quartus project, includes the necessary
port names and pin location assignments to implement the circuit on a DE-series board.
The switch {\it SW}$_{9}$ drives the processor's {\it Run} input, {\it SW}$_0$ is
connected to {\it Resetn}, {\it KEY}$_0$ to {\it MClock}, and {\it KEY}$_1$ to {\it PClock}.
The Run signal is displayed on {\it LEDR}$_{0}$ and {\it Done} is connected to {\it LEDR}$_{9}$.
\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=1.0]{figures/figure6.png}
	\end{center}
	\caption{Specifying which memory ports are registered.}
	\label{fig:fig6}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=1.0]{figures/figure7.png}
	\end{center}
	\caption{Specifying a memory initialization file (MIF).}
	\label{fig:fig7}
\end{figure}

\item Use the ModelSim Simulator to test your VHDL code. Ensure
that instructions are read properly out of the ROM and executed by the processor. An example
of simulation results produced using ModelSim with the MIF file from
Figure~\ref{fig:fig_MIF} is shown in Figure~\ref{fig:fig_sim2}. The corresponding ModelSim
setup files are provided along with this exercise.
\item Once your simulations show a properly-working circuit, you may wish to download
it into a DE-series board. The functionality of the circuit on the board can be tested by
toggling the switches and observing the LEDs. Since the circuit's clock inputs are controlled
by pushbutton switches, it is possible to step through the execution of instructions and
observe the behavior of the circuit.
\end{enumerate}

\begin{figure}[H]
\begin{center}
\begin{minipage}[t]{12.5 cm}
\begin{tabbing}
{\bf DEPTH} = 32;\\
{\bf WIDTH} = 16;\\
{\bf ADDRESS\_RADIX} = HEX;\\
{\bf DATA\_RADIX} = BIN;\\
{\bf CONTENT}\\
{\bf BEGIN}\\
00	:	0001000000011100;~~~~~~\=\%~~mv  \=r0, \#0xFF00~~\=\% \kill
00	:	0001000000011100;	\>\% mv \>r0, \#28\>\%\\
01	:	0011001011111111; \>\% mvt \>r1, \#0xFF00\>\%\\
02	:	0101001011111111;	\>\% add  \>r1, \#0xFF\>\%\\
03	:	0110001000000000;	\>\% sub  \>r1, r0\>\%\\
04	:	0101001000000001;	\>\% add  \>r1, \#1\>\%\\
05	:	0000000000000000;\\
$\ldots$ (some lines not shown)\\
1F :	0000000000000000;\\
{\bf END};
\end{tabbing}
\end{minipage}
\end{center}
\caption{An example memory initialization file (MIF).}
\label{fig:fig_MIF}
\end{figure}

\begin{figure}[H]
	\begin{center}
		\includegraphics[scale=.95]{figures/figure8.png}
	\end{center}
	\caption{An example simulation output using the MIF in Figure~\ref{fig:fig_MIF}.}
	\label{fig:fig_sim2}
\end{figure}

\section*{Enhanced Processor}
\addcontentsline{toc}{3}{Enhanced Processor}
It is possible to enhance the capability of the processor so that the counter in
Figure~\ref{fig:fig4} is no longer needed, and so that the processor has the ability to
perform read and write operations using memory or other devices. These enhancements involve
adding new instructions to the processor, as well as other capabilities---they are
discussed in the next lab exercise.

\end{document}


%
% Licensed to the Apache Software Foundation (ASF) under one
% or more contributor license agreements.  See the NOTICE file
% distributed with this work for additional information
% regarding copyright ownership.  The ASF licenses this file
% to you under the Apache License, Version 2.0 (the
% "License"); you may not use this file except in compliance
% with the License.  You may obtain a copy of the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing,
% software distributed under the License is distributed on an
% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
% KIND, either express or implied.  See the License for the
% specific language governing permissions and limitations
% under the License.
%
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{algorithm}
\usepackage{varwidth}
\usepackage{bm}
\usepackage[noend]{algpseudocode}
\usepackage{xspace}
\usepackage{multirow}
\usepackage{xcolor}
\DeclareRobustCommand{\NAME}{Wideskies\xspace}
\newcommand{\from}{\rightarrow}

\algnewcommand{\algorithmicgiven}{\textbf{given }}
\algnewcommand{\Given}{\algorithmicgiven}

\algnewcommand{\algorithmicselect}{\textbf{select }}
\algnewcommand{\Select}{\algorithmicselect}

\algnewcommand{\algorithmicset}{\textbf{set }}
\algnewcommand{\Set}{\algorithmicset}

\algnewcommand{\algorithmiccompute}{\textbf{compute }}
\algnewcommand{\Compute}{\algorithmiccompute}

\algnewcommand{\algorithmicgoto}{\textbf{go to}}
\algnewcommand{\Goto}[1]{\algorithmicgoto~\ref{#1}}

\newcommand{\Z}{\ensuremath{\mathbf{Z}}}
\newcommand{\zmodn}{\ensuremath{\Z/N\Z}}
\newcommand{\zmodntunits}{\ensuremath{\left(\Z/N^{2}\Z\right)^{\times}}}
\newcommand{\lcm}{\ensuremath{\text{lcm}}}

\mode<presentation> { \usetheme{default} }

\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\makeatletter
\DeclareRobustCommand*{\&}{%
  \nfss@text{%
    \fontfamily{LinuxBiolinumT-TLF}%
    \selectfont
    \symbol{`\&}%
  }%
}

\usepackage{eso-pic}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{footline}[frame number]
\newcommand\AtPagemyUpperLeft[1]{\AtPageLowerLeft{%
\put(\LenToUnit{0.9\paperwidth},\LenToUnit{0.9\paperheight}){#1}}}

\AtBeginSection[]{
  \begin{frame}
  \vfill
  \centering
  \begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
    \usebeamerfont{title}\insertsectionhead\par%
  \end{beamercolorbox}
  \vfill
  \end{frame}
}
\title[Apache Pirk Math
Walkthrough]{\includegraphics[width=2.5in,keepaspectratio]{ApachePirk_1.png}\\ \bigskip Mathematics $\&$ Algorithms}

\author{Walter Ray-Dulany}
\institute[Apache Pirk]
{
\medskip
\textit{raydulany@apache.org}
}
% I decided against a date. - Walter
\date{}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\AddToShipoutPictureFG{
  \AtPagemyUpperLeft{{\includegraphics[width=.5cm,keepaspectratio]{ApachePirkCircle.png}}}
}

\section{Introduction}

\begin{frame}
\frametitle{Pirk's Wideskies Algorithm}
  Pirk uses the Wideskies algorithm to accomplish scalable PIR.\\~\\
  This algorithm can be broken down into two distinct conceptual pieces:
    \begin{itemize}
      \item Paillier Encryption
      \item The Query-Response-Result algorithms
    \end{itemize}
    ~\\ Before we begin those however, we take a (happily brief) diversion into
    the language of the mathematics involved in this deck.
\end{frame}

\section{Language Preliminaries}
\begin{frame}
  \frametitle{Language Preliminaries}
  The Paillier scheme employs a small amount of group theoretic notation. Let's
  go over that notation briefly.
\end{frame}

\begin{frame}
  \frametitle{Language Preliminaries}
  \begin{itemize}
    \item \zmodn: This is the group of integers modulo $N$; it can be thought of
      as all numbers $0\leq k < N$, with modular addition (e.g.\ for $N=5$,
      $1+7 \equiv 3\mod N$).\\~\\ This is a group under addition.\\~\\
    \item $(\zmodn)^\times$: This is the multiplicative group of integers modulo
      $N$, also called the units of $\zmodn$. Sometimes denoted
      $(\mathbf{Z}/N\mathbf{Z})^*$, this is the set of $0\leq k < N$ that are
      relatively prime to $N$ (that is, $k$ and $N$ share no factors, or
      equivalently the greatest common denominator ($\gcd$) of $k$ and $N$ is $1$). One
      can also think of this as the set of $k\in\zmodn$ such that there exists
      a $k^{-1}\in\zmodn$ with $k\cdot k^{-1} \equiv 1\mod N$.\\~\\
      This is a group under multiplication.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Language Preliminaries}
Using the above notation, we can see that
\begin{equation*}
  \zmodntunits = \{0\leq k<N^2: \gcd(k,N^2) = 1\}.
\end{equation*}~\\
If $N$ happens to be an RSA modulus,
$N=pq$, $p$ and $q$ primes, then $\zmodntunits$ is just all numbers between $0$
(inclusive) and $N^2$ (exclusive) that are not divisible by either $p$ or
$q$.
\end{frame}

\begin{frame}
  \frametitle{Language Preliminaries}
  \begin{itemize}
    \item Order In \zmodn: The order of an element $k\in\zmodn$ is the least integer $e$
      such that $e\cdot k = 0\mod N$.
    \item Order in \zmodntunits: The order of an element $a\in\zmodntunits$ is
      the least integer $e$ such that $a^e = 1\mod N^2$.
  \end{itemize}
  In both cases, order is well defined (i.e.\ it exists and makes sense) for
  all elements of the groups.
\end{frame}

\begin{frame}
  \frametitle{Language Preliminaries}
  For a more in depth discussion of these, and closely related, terms, please
  see
  \mbox{\scriptsize \url{http://www.math.nagoya-u.ac.jp/~richard/teaching/s2015/Group_2.pdf}}
\end{frame}

\section{Paillier Encryption}
\begin{frame}
\frametitle{Paillier Encryption}
Paillier encryption is a partially homomorphic public key scheme that relies on
the function $$\mathcal{E}_g:\zmodn\times(\zmodn)^\times\rightarrow
\zmodntunits$$ given by $$\mathcal{E}_g(x,y) = g^x y^N \mod N^2,$$
$g\in\zmodntunits$. Here, $\zmodn$ is the plaintext space and $\zmodntunits$ is
the ciphertext space.\\~\\
When the order of $g$ is a non-zero multiple of $N$, $\mathcal{E}_g$ is a bijection.
\end{frame}

\begin{frame}
  \frametitle{Paillier Prerequisites}
\begin{itemize}
  \item Public key: $(N,g)$, $N$ an RSA modulus $N=pq$, $p$ and $q$ primes of
    approximately the same bit-length, and $g\in \zmodntunits$ such that the
    order of $g$ is a nonzero multiple of $N$.
  \item Private key: $\lambda(N)$, where $\lambda$ is the Carmichael function
    \begin{align*}
      \lambda(N) &= \lcm(p-1,q-1)
    \end{align*}
  that gives the exponent of $(\zmodn)^\times$.
  \item Plaintext space: \zmodn.
  \item Ciphertext space: \zmodntunits.
\end{itemize}
~\\
{\footnotesize We can also consider the pair $(p,q)$ to be the private key, as $\lambda(N)$ is
quickly and easily derived from it. Note that $\lambda(N)$ is coprime to $N$.}
\end{frame}

\begin{frame}
  \frametitle{What Do We Mean By `Homomorphic Encryption'?}
  An encryption scheme is fully homomorphic if it is a homomorphism from
  plaintext space to ciphertext space for arbitrary operations and arbitrary
  numbers of such operations. If this definition seems squishy and not very
  mathematical, that's because it is; it's hard to find a proper mathematical
  definition of this term.\\~\\

  An encryption scheme is partially homomorphic if it is a homomorphism for
  only some operations, or for only a few consecutive operations.\\~\\
\end{frame}

\begin{frame}
  \frametitle{Paillier Encryption is Homomorphic}
  Paillier encryption is a partial homomorphism between addition in \zmodn\ and
  multiplication in \zmodntunits.\\~\\

  Denote Paillier encryption by $\mathcal{E}_g$ and decryption by $\mathcal{D}_g$,
  and let $m$ and $m'\in \zmodn$. Then
    \begin{align*}
        D(\mathcal{E}(m) \mathcal{E}(m') \bmod N^2) &= (m + m') \bmod N \\
        D(\mathcal{E}(m)^k \bmod N^2) &= km \bmod N, \, k \in \mathbf{N}
    \end{align*}
    ~\\
    Note that the second equality follows immediately from the first.
\end{frame}

\section{General Paillier Algorithm}

\begin{frame}
\frametitle{Paillier Supporting Function}
Let $X=\{u<N^2 : u = 1 \mod N\}$ and let $L:X\rightarrow \zmodn$ be
defined by
\begin{equation*}L(u) = \frac{u-1}{N} \mod N.\end{equation*}
This function is well defined over \zmodntunits.
\end{frame}

\begin{frame}
\frametitle{General Paillier Encryption}
The general Paillier algorithm differs only slightly from Pirk's version.
\begin{algorithm}[H]
  \caption{General Paillier encryption and decryption.}\label{alg.paillier_encrypt_original}
  \begin{algorithmic}[1]
    \Procedure{Paillier encryption}{}
    \State \begin{varwidth}[t]{\linewidth}
      \Given \(N\), a random \(g \in \zmodntunits\) of order a nonzero\par
  multiple of $N$, and a message \(m\in\zmodn\)
      \end{varwidth}
    \State \Select a random value \(\zeta\in \left(\zmodn\right)^{\times}\)
    \State \Return \(\mathcal{E}(m) = g^m \zeta^{N}\bmod{N^{2}}\)
    \EndProcedure
  \end{algorithmic}
  \begin{algorithmic}[1]
    \Procedure{Paillier decryption}{}
    \State \Given \(N\), \(\lambda(N)\), \(g\), and ciphertext \(c \in \zmodntunits\)
    \State \Return m = \(\frac{L( c^{\lambda(N)}\bmod N^{2})}{L( g^{\lambda(N)}\bmod N^{2})}\bmod N\)
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{frame}

\begin{frame}
\frametitle{Paillier Works}
  It is a straightforward exercise to check that
  \begin{itemize}
    \item $D(\mathcal{E}(m)) = m$
    \item $D(\mathcal{E}(m)\mathcal{E}(m')\bmod N^2) = (m + m') \bmod N $
  \end{itemize}
\end{frame}

\section{Paillier As Used In \NAME}
\begin{frame}
\frametitle{Paillier As Used In \NAME}
The version of Paillier used in \NAME is a computationally simpler variant of the
full Paillier scheme that sacrifices no security over the general case.
\end{frame}

\begin{frame}
  \frametitle{Converting Between The Two}
  Pirk's simplified version of Paillier simply uses
  \begin{align*}
    g &\equiv 1 + N \mod N^2\\
    \implies L(g^{\lambda(N)} \mod N^2) &= \lambda(N),
  \end{align*}
  the proof of which is a straightforward exercise.
\end{frame}

\begin{frame}
\frametitle{Paillier As Used In \NAME}
\begin{algorithm}[H]
  \caption{Paillier encryption and decryption}\label{alg.paillier_encrypt}
  \begin{algorithmic}[1]
    \Procedure{Paillier encryption}{}
    \State \Given \(N\) and a message \(m\in\zmodn\)
    \State \Select a random value \(\zeta\in \left(\zmodn\right)^{\times}\)
    \State \Return \(\mathcal{E}(m) = (1+mN)\zeta^{N}\bmod{N^{2}}\)
    \EndProcedure
  \end{algorithmic}

  \begin{algorithmic}[1]
    \Procedure{Paillier decryption}{}
    \State \Given \(N\), \(\lambda(N)\), and a ciphertext \(c\in\zmodntunits\)
    \State \Set \(\mu = \lambda(N)^{-1}\bmod N\)
    \Comment Recall \(\gcd(\lambda(N), N) = 1\)
    \State \Set \(\hat{c} = c^{\lambda(N)}\bmod N^{2}\)
    \State\label{step.div}\Set \(\hat{m} = L(c^{\lambda(N)}\bmod N^{2})\)
    \State \Return \(\hat{m}\mu\bmod N\)
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{frame}

\begin{frame}
  \frametitle{Paillier Reference}
  For more on Paillier encryption and the (hypothesized) hard problem upon
  which it is based, see\\~\\
  \mbox{\scriptsize \url{https://pirk.incubator.apache.org/papers/1999_asiacrypt_paillier_paper.pdf}}\\~\\
  on Pirk's website.
\end{frame}

\section{Wideskies}

\begin{frame}
  \frametitle{Wideskies Parameters}
  The algorithm requires the following parameters, which are not independent
  (see the next slide).
  \begin{itemize}
    \item $N$, the Paillier modulus
    \item $B$, the bit-length of $N$
    \item $H$ (or $H_k$), a keyed hash function (with key k)
    \item $\ell$, the bit length of the output of $H$, i.e.\
      $H_k:\mathbf{Z}\rightarrow (\mathbf{Z}/2\mathbf{Z})^\ell$
    \item $\tau$, the number of search terms
    \item $\delta$, the number of bits of data returned for each search hit
    \item $b$ the chunk size, in bits, determining how data is split among
      responses.
    \item $r$, the number of responses that can be returned per query request
      period per search term
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Parameter Relationships}
  \begin{itemize}
    \item $2^{b\tau} < N$: there must be space in the modulus to hold all the
      data, even if each search term hits as often as possible.
    \item $\tau < 2^\ell$: Although the paper permits search term hash
    collisions, Pirk does not permit them. Typically $\tau \ll 2^\ell$
    \item $b|\delta$: Chunk size must evenly divide the data size
    \item $\frac{\delta}{b} | r$: the number of chunks per returned datum must
      divide the number of responses, for bandwidth efficiency.
    \item $H$: Must be pseudo-random but need not be cryptographically secure.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Public Parameters}
  All of \begin{equation*}H, \ell, N, B, \delta, b, \text{and } r\end{equation*}
  are public, that is, must be shared between the client and server.\\~\\

  Note that the fact that $2^{b\tau} < N$ gives some information on the number
  of search terms the client is using; the amount of this information can be
  decreased without bound by choosing $N$ and $\ell$ to be much larger than
  would be otherwise necessary; this necessarily causes a performance hit.
\end{frame}

\section{Wideskies Algorithm, Without Encryption}
\begin{frame}
  \frametitle{Wideskies Without Encryption?}
  The Wideskies algorithm is of sufficient complexity that it can be useful to
  go through the algorithm without the encryption and decryption steps first,
  in order to orient ourselves.\\~\\
  After, it will be straightforward to see the
  changes that using the Paillier encryption requires.
\end{frame}

\section{Query, Without Encryption}
\begin{frame}
  \frametitle{The Query Algorithm, Without Encryption}
  Let $T_0,\ldots,T_{\tau-1}\in\zmodn$ be our search terms.
  \begin{algorithm}[H]
  \caption{Query Formation Algorithm version
    1}\label{alg.plain_form_1}
\begin{algorithmic}[1]
  \State\label{step.key}Choose a random key \(k\) for \(H\).
  \State Compute \(H_{k}(T_{0}),\ldots,H_{k}(T_{\tau-1})\).
  \While{\(\mathrm{card}\left(\{H_{k}(T_{0}),\ldots,H_{k}(T_{\tau-1})\}\right)
    < \tau\)}
  \State \Goto{step.key}
  \Comment If there are hash collisions, pick a new key.
  \EndWhile
  \For{\(i=0,\ldots,2^{\ell}-1\)}
  \State\label{step.set}Set \begin{equation*}
    E_{i} = \left\{\begin{array}{l l}
    2^{jb} & \mbox{ if } i = H_{k}(T_{j}); \\
    0 & \mbox{otherwise.}
    \end{array}
    \right.
  \end{equation*}
  \EndFor
  \State\Return \(\{E_{0},\ldots,E_{2^{\ell}-1}, H, k, N\}\)
\end{algorithmic}
\end{algorithm}
\end{frame}

\begin{frame}
  \frametitle{Query Notes, Without Encryption}
  Since $\tau \ll 2^\ell$, we expect most of the $E_i$ to be zero.\\~\\
  We will typically denote $H_k(T)$ by $\mathcal{T}$ and its associated $E$ by
  $E_\mathcal{T}$. If we wish to keep track of a specific $T$ we will write
  $\mathcal{T}_j$ and $E_{\mathcal{T}_j}$.
\end{frame}

\section{Response, Without Encryption}
\begin{frame}
  \frametitle{Response Initialization, Without Encryption}
  We must initialize some values before forming the response.
  \begin{enumerate}
    \item $c_0,\ldots,c_{2^\ell - 1} = 0$, counters to keep track of the number
      of times each $E_\mathcal{T}$ has been seen.
    \item $Y_0,\ldots,Y_{r-1} = 0$, response vectors.
  \end{enumerate}
\end{frame}

\begin{frame}
  \frametitle{Response Data, Without Encryption}
  Responder information comes in pairs $(T, D)$ where $T$ is a (potential)
  search term, and $D$ is $T$'s associated response datum, which will be
  returned if $T$ is a search term.\\~\\

  We view $D$ as a $\delta$-long bit stream $(d_0,\ldots,d_{\delta-1})$, and
  break $D$ up into $\delta/b$ chunks $D_i$ as
  \begin{equation*}
    D_i = (d_{i\cdot b}, d_{i\cdot b+1},\ldots,d_{(i+1)\cdot b-1}),\ i=0,\ldots,\delta/b-1.
  \end{equation*}\\~\\
  For example, if $D=011010$ and $b=3$, then
  \begin{align*}
    D_0 &= 011\\
    D_1 &= 010
  \end{align*}
\end{frame}

\begin{frame}
  \frametitle{Response Algorithm, Without Encryption}
  \begin{algorithm}[H]
  \caption{Stream processing, plaintext version}\label{alg.plain_stream}
\begin{algorithmic}[1]
\State \textbf{Input}:  $\mathbf{T} = \{ (T,D) \}$
\For{$(T,D) \in \mathbf{T}$}
  \State Compute \(\mathcal{T} = H_{k}(T)\)
  \If{\(c_{\mathcal{T}} + \frac{\delta}{b} > r\)}\label{step.if}
  \Comment The space allocated for term \(T\) is full.
  \State \Return \label{step.return}
\Else
  \State Split \(D\) into \(b\)-bit chunks
  \(D_{0},\ldots,D_{(\delta/b)-1}\).
  \For{\(i=0,\ldots,(\delta/b)-1\)}
  \State\label{step.multiply}Set \(\mathcal{D}_{i} = D_{i}E_{\mathcal{T}}\bmod N\)
  \Comment Nonzero only if \(E_{\mathcal{T}}\neq 0\).
  \State Set \(Y_{i+c_{\mathcal{T}}} =
  Y_{i+c_{\mathcal{T}}}+\mathcal{D}_{i}\bmod N\)
  \EndFor
  \State Set \(c_{\mathcal{T}} = c_{\mathcal{T}}+(\delta/b)\)
\EndIf

\EndFor
\State{\textbf{Output}}: \(Y_{0},\ldots,Y_{r-1}\)
\end{algorithmic}
\end{algorithm}
\end{frame}

\begin{frame}
  \frametitle{Response Example, Without Encryption}
  Let's look at how the response would look on the first four $(T,D)$ pairs that
  pass through the algorithm.
\end{frame}

\begin{frame}
  \frametitle{Response Example Setup, Without Encryption}
  Suppose that among our search terms are $T$ and $T'$, with
  \begin{align*}
    H_k(T) &= j\text{ and}\\
    H_k(T') &= j'.
  \end{align*}~\\

  Suppose that $T''$, with $H_k(T'')=j''$, is \emph{not} a search term.\\~\\

  Let the responder see, in order, the pairs $(T, D^0)$, $(T', D^1)$,
  $(T'', D^2)$, $(T, D^3)$.\\~\\

  The $Y_i$ are formed by summing down the columns in following matrices.
\end{frame}

\begin{frame}
  \frametitle{Response Example Start, Without Encryption}
  No terms have yet been evaluated.
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
   {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
 {\footnotesize$j$} & 0                        & $\cdots$                         & 0                         & 0                         & $\cdots$                         & 0                         \\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
 {\footnotesize$j'$}  & 0                      & $\cdots$                           & 0                           & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & 0 & $\cdots$                           & 0         & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
  $c_{j} = 0$, $c_{j'} = 0$, $c_{j''} = 0$.
\end{frame}

\begin{frame}
  \frametitle{Response Example: First Term, Without Encryption}
  $(T, D^0)$ enters and is proccessed; hit:
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
     {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize $\bm{D^0_0} 2^{jb}$}                         & $\cdots$                         & {\footnotesize $\bm{D^0_{\delta/b-1}} 2^{jb}$}                      & 0                         & $\cdots$                         & 0                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & 0 & $\cdots$                           & 0                           & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & 0 & $\cdots$                           & 0         & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
  $\bm{c_{j} = \delta/b}$, $c_{j'} = 0$, $c_{j''} = 0$.
\end{frame}

\begin{frame}
  \frametitle{Response Example Second Term, Without Encryption}
  $(T', D^1)$ enters and is proccessed; hit:
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
     {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize ${D^0_0} 2^{jb}$}                         & $\cdots$                         & {\footnotesize ${D^0_{\delta/b-1}} 2^{jb}$}                      & 0                         & $\cdots$                         & 0                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & {\footnotesize $\bm{D^1_0} 2^{j'b}$} & $\cdots$                           & {\footnotesize $\bm{D^1_{\delta/b-1}} 2^{j'b}$}                           & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & 0 & $\cdots$                           & 0         & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
  $c_{j} = \delta/b$, $\bm{c_{j'} = \delta/b}$, $c_{j''} = 0$.
\end{frame}

\begin{frame}
  \frametitle{Response Example Third Term, Without Encryption}
  $(T'', D^2)$ enters and is proccessed; no hit:
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
     {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize ${D^0_0} 2^{jb}$}                         & $\cdots$                         & {\footnotesize ${D^0_{\delta/b-1}} 2^{jb}$}                      & 0                         & $\cdots$                         & 0                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & {\footnotesize ${D^1_0} 2^{j'b}$} & $\cdots$                           & {\footnotesize ${D^1_{\delta/b-1}} 2^{j'b}$}                           & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & {\footnotesize $\bm{D^2_0} \cdot 0$}  & $\cdots$                           & {\footnotesize $\bm{D^2_{\delta/b-1}} \cdot 0$}     & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
$c_{j} = \delta/b$, $c_{j'} = \delta/b$, $\bm{c_{j''} = \delta/b}$.
\end{frame}

\begin{frame}
  \frametitle{Response Example Fourth Term, Without Encryption}
  $(T, D^3)$ enters and is proccessed; hit:
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
     {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize ${D^0_0} 2^{jb}$}                         & $\cdots$ & {\footnotesize ${D^0_{\delta/b-1}} 2^{jb}$}                      & {\footnotesize $\bm{D^3_0} 2^{jb}$}                         & $\cdots$ & {\footnotesize $\bm{D^3_{\delta/b-1}} 2^{jb}$}                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & {\footnotesize ${D^1_0} 2^{j'b}$} & $\cdots$                           & {\footnotesize ${D^1_{\delta/b-1}} 2^{j'b}$}                           & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & {\footnotesize $\bm{D^2_0} \cdot 0$}  & $\cdots$                           & {\footnotesize $\bm{D^2_{\delta/b-1}} \cdot 0$}     & 0                      & $\cdots$                           & 0\\
    $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
  $\bm{c_{j} = 2\delta/b}$, $c_{j'} = \delta/b$, $c_{j''} = \delta/b$.
\end{frame}

\section{Result, Without Encryption}
\begin{frame}
  \frametitle{Result, Without Encryption}
  The algorithm for getting the results out of the response return is
  straightforward. To begin,\\~\\
  \begin{itemize}
    \item Write $Y_i = \sum_{k=0}^{\tau - 1} 2^{kb}P_{ki}$ in base $2^b$, where
      $P_{ki}$ is the value of the $k^{\text{th}}$ row in the
      $i^{\text{th}}$ column. Note each $P_{ki}$ is $b$-bits long, and therefore
      $Y_i < N$.
    \item $Y_i$ will have data on search term $T$ if and only if $T$ was
      seen $i+1$ times before the responder returned.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Result Algorithm, Without Encryption}
  \begin{algorithm}[H]

  \caption{Data recovery, plaintext version}\label{alg.plain_recover}
\begin{algorithmic}[1]
  \State Set \(M = 2^{j b}(2^{b}-1)\)
  \Comment $b$ $1$s left-shifted $jb$ places.
  \For{\(\eta=1,\ldots,(rb/\delta)\)}
  \Comment At most \(rb/\delta\) hits can be returned.
  \For{\(i=0,\ldots,(\delta/b)-1\)}
  \Comment Each hit uses \(\delta/b\) chunks.
  \State\label{step.mask}Set \(D_{i} = Y_{(\eta-1)(\delta/b)+i}\&M\)
  \Comment ``\(\&\)'' denotes bit-wise \texttt{AND}.
  \State\label{step.shift}Set \(D_{i} = D_{i}/2^{jb}\)
  \Comment Step \ref{step.mask} ensures \(2^{jb}\mid D_{i}\)
  \EndFor
  \State Set \(X_{\eta} = D_{0}\|D_{1}\|\ldots\|D_{(\delta/b)-1}\)
  \EndFor
  \State \Return \(X_{1},\ldots,X_{(rb/\delta)}\)
  \Comment the data corresponding to selector \(T_{j}\)
\end{algorithmic}
\end{algorithm}
\end{frame}

\section{Wideskies Algorithm, With Encryption}
\begin{frame}
  \frametitle{Adding Encryption To The Mix}
  Adding encryption is straightforward. The following slides have the
  encryption-enabled algorithms, with the differences from the earlier slides
  in bold.
\end{frame}

\section{Query, Encrypted}
\begin{frame}
  \frametitle{Query, Encrypted}
  \begin{algorithm}[H]
  \caption{Query formation, ciphertext version 1}\label{alg.cipher_form_1}
\begin{algorithmic}[1]
  \State\label{step.key_2}Choose a random key \(k\) for \(H\).
  \State Compute \(H_{k}(T_{0}),\ldots,H_{k}(T_{\tau-1})\).
  \While{\(\mathrm{card}\left(\{H_{k}(T_{0}),\ldots,H_{k}(T_{\tau-1})\}\right)
    < \tau\)}
  \State \Goto{step.key_2}
  \EndWhile
  \For{\(i=0,\ldots,2^{\ell}-1\)}
  \State Set
  {
  \bfseries\boldmath
  \begin{equation*}
    \mathcal{E}_{i} = \left\{\begin{array}{l l}
        \mathcal{E}(2^{jb}) & \mbox{ if }i=H_{k}(T_{j})\mbox{ for some
        }j\in\{0,\ldots,\tau-1\} \\
        \mathcal{E}(0) & \mbox{ otherwise.}
      \end{array}
    \right.
  \end{equation*}}
  \EndFor
  \State \Return \(\{\mathcal{E}_{0},\ldots,\mathcal{E}_{2^{\ell}-1}, H,
  k, N\}\)
\end{algorithmic}
\end{algorithm}
\end{frame}

\section{Response, Encrypted}
\begin{frame}
  \frametitle{Response Initialization, Encrypted}
  As before, we must initialize some values before forming the response.
  \begin{enumerate}
    \item $c_0,\ldots,c_{2^\ell - 1} = 0$, counters to keep track of the number
      of times each {\boldmath $\mathcal{E}_\mathcal{T}$} has been seen.
    \item {\bfseries\boldmath $Y_0,\ldots,Y_{r-1} = 1$, response vectors.}
  \end{enumerate}
\end{frame}

\begin{frame}
  \frametitle{Response, Encrypted}
  \begin{algorithm}[H]
  \caption{Stream processing, ciphertext version}\label{alg.cipher_processing}
\begin{algorithmic}[1]
\State \textbf{Input}:  $\mathbf{T} = \{ (T,D) \}$
\State \textbf{Initialize:}
\State \qquad Counters $c_i = 0 \, , \, 0 \leq i \leq (2^l -1)$
\State \qquad Paillier ciphertext values  $\mathcal{Y}_{j} = 1 \, , \, 0 \leq j \leq (r-1)$
\For{$(T,D) \in \mathbf{T}$}
  \State Compute \(\mathcal{T} = H_{k}(T)\)
  \If{\(c_{\mathcal{T}}+\frac{\delta}{b} > r\)}
  \State \Return
  \Else
  \State Split \(D\) into \(b\)-bit chunks,
  \(D=D_{0},\ldots,D_{(\delta/b)-1}\) \label{step.datachunk}
  \For{\(i=0,\ldots,(\delta/b)-1\)}
  \State {\bfseries\boldmath Set \(\mathcal{D}_{i} =
  \mathcal{E}_{\mathcal{T}}^{D_{i}}\bmod N^{2}\)}
  \State {\bfseries\boldmath Set \(\mathcal{Y}_{i+c_{\mathcal{T}}} =
  \mathcal{Y}_{i+c_{\mathcal{T}}}\mathcal{D}_{i}\bmod N^{2}\)}
  \EndFor
  \State Set \(c_{\mathcal{T}} = c_{\mathcal{T}}+(\delta/b)\)
  \EndIf
\EndFor
\State{\textbf{Output}}: \(\mathcal{Y}_{0},\ldots,\mathcal{Y}_{r-1}\)
\end{algorithmic}
\end{algorithm}
\end{frame}

\section{Result, Encrypted}
\begin{frame}
  \frametitle{Result, Encrypted (and then Decrypted)}
  Actually literally the same algorithm as before is used; the only difference
  is that we first decrypt the encrypted $\mathcal{Y}_i$.
\end{frame}

\section{Distributed Version}
\begin{frame}
  \frametitle{Distributed Version}
  The paper goes over how to do the distributed version; the change is
  straightforward, and our earlier example slides make it easy to see how it
  works.
\end{frame}

\begin{frame}
  \frametitle{Distributed Difference:\\ Unencrypted Sums, Encrypted Products}
  Recall our example matrix:
  \begin{center}
  \begin{tabular}{c  C{1.55cm}  c  C{1.55cm}  C{1.55cm}  c  C{1.55cm}  }
     {\scriptsize Index}             & $Y_0$\qquad            & $\cdots$\qquad         & $Y_{\delta/b-1}$\qquad & $Y_{\delta/b}$\qquad   & $\cdots$\qquad         & $Y_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize ${D^0_0} 2^{jb}$}                         & $\cdots$ & {\footnotesize ${D^0_{\delta/b-1}} 2^{jb}$}                      & {\footnotesize ${D^3_0} 2^{jb}$}                         & $\cdots$ & {\footnotesize ${D^3_{\delta/b-1}} 2^{jb}$}                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & {\footnotesize ${D^1_0} 2^{j'b}$} & $\cdots$                           & {\footnotesize ${D^1_{\delta/b-1}} 2^{j'b}$}                           & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & {\footnotesize $\bm{D^2_0} \cdot 0$}  & $\cdots$                           & {\footnotesize $\bm{D^2_{\delta/b-1}} \cdot 0$}     & 0                      & $\cdots$                           & 0\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Distributed Difference:\\ Unencrypted Sums, Encrypted Products}
  When we moved to an encrypted algorithm, all of the $D_i$-long sums of
  $E_i$ became
  $\mathcal{E}_i^{\mathcal{D}_i}$.\\~\\
  In the distributed version, we actually make matrix components rather than
  the fake matrix of $D_i$ in certain bit-positions we had earlier.\\~\\

  In the matrix, rows are indexed by $0 \leq \mathcal{T} \leq 2^\ell
  -1$, columns by $0 \leq j \leq r-1$.
\end{frame}

\begin{frame}
  \frametitle{Distributed Difference:\\ Unencrypted Sums, Encrypted Products}
  As before, let
  \begin{align*}
    H_k(T) &= j,\\
    H_k(T') &= j',\text{ and}\\
    H_k(T'') &= j'',
  \end{align*}
  with $T$ and $T'$ search terms and $T''$ not.\\~\\
  Notice that this time we won't simply discard the data $D_2$ from $T''$;
  we no longer multiply it by zero, but use it as the exponent of
  $\mathcal{E}_{j''}$, which is an encryption of $0$.
\end{frame}

\begin{frame}
  \frametitle{Distributed Difference:\\ Unencrypted Sums, Encrypted Products}
  The matrix in the encrypted setting:
  \begin{center}
  \begin{tabular}{c  C{1.35cm}  c  C{1.35cm}  C{1.35cm}  c  C{1.35cm}  }
     {\scriptsize Index}             & $\mathcal{Y}_0$\qquad            & $\cdots$\qquad         & $\mathcal{Y}_{\delta/b-1}$\qquad & $\mathcal{Y}_{\delta/b}$\qquad   & $\cdots$\qquad         & $\mathcal{Y}_{2\delta/b-1}$\qquad\\\toprule
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j$} & {\footnotesize $\mathcal{E}_j^{D^0_0}$} & $\cdots$ & {\footnotesize $\mathcal{E}_j^{D^0_{\delta/b-1}}$}                      & {\footnotesize $\mathcal{E}_j^{D^3_0}$}                         & $\cdots$ & {\footnotesize $\mathcal{E}_j^{D^3_{\delta/b-1}}$}                         \\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j'$}  & {\footnotesize $\mathcal{E}_{j'}^{D^1_0}$} & $\cdots$                           & {\footnotesize $\mathcal{E}_{j'}^{D^1_{\delta/b-1}}$}                           & 1                      & $\cdots$                           & 1\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\
     {\footnotesize$j''$}  & {\footnotesize $\mathcal{E}_{j''}^{D^2_0}$} & $\cdots$                           & {\footnotesize $\mathcal{E}_{j''}^{D^2_{\delta/b-1}}$}                           & 1                      & $\cdots$                           & 1\\
     $\vdots$ & \multicolumn{6}{l}{$\qquad$}\\\bottomrule
  \end{tabular}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Algorithm In Matrix Form}
  \begin{algorithm}[H]
  \caption{Responder -  Matrix Variant}\label{alg.matrix_processing}
\begin{algorithmic}[1]
  \State {\scriptsize \textbf{Input:}  $\mathbf{T} = \{ (T,D) \}$}
\State{\scriptsize  \textbf{Initialize:}}
\State{\scriptsize  \qquad Counters $c_i = 0 \, , \, 0 \leq i \leq (2^l -1)$}
\State{\scriptsize  \qquad Paillier ciphertext values  $\mathcal{Y}_{j} = 1 \, , \, 0 \leq j \leq (r-1)$}
\For{\scriptsize {$(T,D) \in \mathbf{T}$}}
\State{\scriptsize  Compute \(\mathcal{T} = H_{k}(T)\)}
\Comment{\scriptsize  View as the row index of $M: m_{\mathcal{T}, \, j}$}
  \If{\scriptsize {\(c_{\mathcal{T}}+\frac{\delta}{b} > r\)}}
  \State{\scriptsize  \Return}
\Else{\scriptsize }
  \State{\scriptsize  Split \(D\) into \(b\)-bit chunks, \(D=D_{0}\|D_{1}\|\ldots\|D_{(\delta/b)-1}\)}
  \For{\scriptsize {\(k=0,\ldots,(\delta/b)-1\)}}
  \State{\scriptsize  Set \(m_{\mathcal{T}, \, c_{\mathcal{T}}+k} = \mathcal{E}_{\mathcal{T}}^{D_{k}}\bmod N^{2}\)}
  \EndFor{\scriptsize }
  \State{\scriptsize  Set \(c_{\mathcal{T}} = c_{\mathcal{T}}+(\delta/b)\)}
 \EndIf{\scriptsize }
\EndFor{\scriptsize }
\For{\scriptsize {$0\leq j \leq (r-1)$}:}
\State{\scriptsize  \qquad \qquad $\mathcal{Y}_{j} \, = \,  \prod_{i = 0}^{2^l -1} m_{i,j}$ }
\EndFor{\scriptsize }
\State{\scriptsize {\textbf{Output}}: \(\mathcal{Y}_{0},\ldots,\mathcal{Y}_{r-1}\)}
\end{algorithmic}
\end{algorithm}
\end{frame}

\begin{frame}
  \frametitle{Distributed Algorithm}
  \begin{algorithm}[H]
  \caption{Responder -  Distributed Variant}\label{alg.dist_processing}
\begin{algorithmic}[1]
  \State {\scriptsize \textbf{Input:}  $\mathbf{T} = \{ (T,D) \}$}
\For{\scriptsize {$(T,D) \in \mathbf{T}$} in parallel}
\State{\scriptsize  Compute \(\mathcal{T} = H_{k}(T)\)}
\Comment{\scriptsize  View as the row index of $M: m_{\mathcal{T}, \, j}$}
  \State{\scriptsize  Split \(D\) into \(b\)-bit chunks, \(D=D_{0}\|D_{1}\|\ldots\|D_{(\delta/b)-1}\)}
\State{\scriptsize  Form $\mathbf{D} = \{D_k : 0 \leq k \leq (\delta/b)-1\}$}
\State{\scriptsize  \textbf{Emit} $(\mathcal{T}, \mathbf{D})$}
  \EndFor{\scriptsize }
 \For{\scriptsize  {each $\mathcal{T}$} in parallel}
\State{\scriptsize  Initialize $c_{\mathcal{T}} = 0$}
\While{\scriptsize  {$c_{\mathcal{T}} <  r$}}
\For{\scriptsize {each $(\mathcal{T}, \mathbf{D})$} }
\For{\scriptsize  {each $D_k \in \mathbf{D} \, , \, 0 \leq k \leq \ldots,(\delta/b)-1$}}
  \State{\scriptsize  Set \(m_{\mathcal{T}, \, c_{\mathcal{T}}} = \mathcal{E}_{\mathcal{T}}^{D_{k}}\bmod N^{2}\)}
\State{\scriptsize  \textbf{Emit} $(c_{\mathcal{T}}, m_{\mathcal{T}, \, c_{\mathcal{T}}})$}
\State{\scriptsize  $ c_{\mathcal{T}} = c_{\mathcal{T}} +  1$}
\EndFor{\scriptsize }
\EndFor{\scriptsize }
\EndWhile{\scriptsize }
\EndFor{\scriptsize }
\For{\scriptsize {$0\leq j \leq (r-1)$ in parallel}:}
\State{\scriptsize  \qquad \qquad $\mathcal{Y}_{j} \, = \,  \prod_{i = 0}^{2^l -1} m_{i,j}$ }
\EndFor{\scriptsize }
\State{\scriptsize {\textbf{Output}}: \(\mathcal{Y}_{0},\ldots,\mathcal{Y}_{r-1}\)}
\end{algorithmic}
\end{algorithm}
\end{frame}

\section{`Actual' Example}
\begin{frame}
  \frametitle{Actual Example Setup}
We run through the above with actual numbers.\\~\\
Let
\begin{itemize}
  \item $N = 35$, $p=5$, $q=7$, $\lambda(N) = 12$, $B=5$.
  \item $\tau$, the number of terms we'll search for, is $2$. These terms are
    $T_0 = 0$ and $T_1 = 3$.
  \item We won't specify most of $H$; only that $\ell=4$, $H(T_0) = 0110 = 6$ and
    $H(T_3) = 0010 = 2$.
  \item Our return data are $\delta=4$ bits long; let $b=2$. We limit ourselves
    to $r=4$.
  \item Let's consult an RNG to choose values of $\zeta$ for use in Paillier.
\end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Let's Consult an RNG}
  \begin{center}
  \includegraphics[width=2.5in,keepaspectratio]{random_number.png}
\end{center}
\bigskip \bigskip {\scriptsize Source:
  \url{http://imgs.xkcd.com/comics/random_number.png}, used under
  \url{http://www.xkcd.com/license.html}}
\end{frame}

\begin{frame}
  \frametitle{Actual Example Setup}
  Great, we will randomly set $\zeta = 4$ for all encryptions.
\end{frame}

\begin{frame}
  \frametitle{Example Query}
  \begin{itemize}
    \item Since $H(T_0) = 6$,
      \begin{align*}
        \mathcal{E}_6 &= \mathcal{E}(2^{0\cdot2})\\
          &= 639
      \end{align*}
    \item Similarly, since $H(T_1) = 2$,
      \begin{align*}
        \mathcal{E}_2 &= \mathcal{E}(2^{1\cdot2})\\
          &= 359.
      \end{align*}
    \item All other terms are encryptions of $0$; we will write these as $1$
      even though they would in fact be distributed across a wide array of
      values in \zmodntunits.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example Response}
  Suppose, as in our example above, that the responder inputs, in order, are
  $(T_0,D^0)$, $(T_1,D^1)$, $(5, D^2)$, and $(T_0,D^3)$, after
  which point the responder returns (perhaps another $T_0$ comes in, thus causing
  $c_{\mathcal{T}_0}$ to be greater than $r$). Here,
  \begin{align*}
    D^0 &= 0000 = (D^0_0, D^0_1) = (00, 00),\\
    D^1 &= 0110 = (D^1_0, D^1_1) = (01, 10),\\
    D^2 &= 0111 = (D^2_0, D^2_1) = (01, 11),\\
    D^3 &= 0010 = (D^3_0, D^3_1) = (00, 10),\\
  \end{align*}~\\

  Note that since $5$ is not a search term, it will result in raising an
 encrypted zero to $D^2=7$; again, we're just going to write $1$, even though the
  acutal algorithm may (will) have any encryption of $0$ instead.
\end{frame}

\begin{frame}
  \frametitle{Example Response Matrix}
  The responder forms the matrix
  \begin{center}
  \begin{tabular}{c  C{2.25cm}  C{2.25cm}  C{2.25cm}  C{2.25cm}  }
     {\scriptsize Index}             & $\mathcal{Y}_0$ & $\mathcal{Y}_1$ & $\mathcal{Y}_2$ & $\mathcal{Y}_{3}$\\\toprule
     $\vdots$ & \multicolumn{4}{l}{$\qquad$}\\
     {\footnotesize{\tiny $2$}} & {\tiny $\mathcal{E}_2^{D^1_0}\mod N^2 = 359$} & {\tiny $\mathcal{E}_2^{D^1_1}\mod N^2 = 256$}      & 1                      & 1\\
     {\tiny $\vdots$} & \multicolumn{4}{l}{{\tiny $\qquad$}}\\
     {\footnotesize{\tiny $6$}}  & {\tiny $\mathcal{E}_6^{D^0_0} \mod N^2 = 1$} & {\tiny$\mathcal{E}_6^{D^0_1}\mod N^2 = 1$} & {\tiny $\mathcal{E}_6^{D^3_0}\mod N^2 = 1$}    &  {\tiny $\mathcal{E}_6^{D^3_1}\mod N^2 = 396$} \\
     {\footnotesize{\tiny $7$}}  & 1 & 1 & 1                      & 1\\
     {\tiny $\vdots$} & \multicolumn{4}{l}{{\tiny $\qquad$}}\\\bottomrule
  \end{tabular}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Example Responses}
  The only interesting responses are $\mathcal{Y}_0 = 359$, $\mathcal{Y}_1=256$, and $\mathcal{Y}_3 = 396$ (products are taken down columns).
\end{frame}

\begin{frame}
  \frametitle{Example Result}
  We decrypt to $Y_0=0100$, $Y_1 =8 = 1000 $ and $Y_3 = 2 = 0010$, and then run through the processing algorithm:
  \begin{itemize}
    \item Data For $T_0$: $M=0011$
      \begin{itemize}
        \item $X_1 = 0$:
          \begin{itemize}
            \item $D_0 = (Y_0 \& 0011)/2^0 = 00$
            \item $D_1 = (Y_1 \& 0011)/2^0 = 00$
          \end{itemize}
        \item $X_2 = 2$:
          \begin{itemize}
            \item $D_0 = (Y_2 \& 0011)/2^0 = 00$
            \item $D_1 = (Y_3 \& 0011)/2^0 = 10$
          \end{itemize}
      \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Example Result}
  We decrypted to $Y_0 = 0100$, $Y_1 =8 = 1000 $ and $Y_3 = 2 = 0010$, and then run through the processing algorithm:
  \begin{itemize}
    \item Data For $T_1$: $M=1100$.
      \begin{itemize}
        \item $X_1 = 6$:
          \begin{itemize}
            \item $D_0 = (Y_0 \& 1100)/2^2 = 01$
            \item $D_1 = (Y_1 \& 1100)/2^2 = 10$
          \end{itemize}
        \item $X_2 = 0$:
          \begin{itemize}
            \item $D_0 = (Y_2 \& 1100)/2^2 = 00$
            \item $D_1 = (Y_3 \& 1100)/2^2 = 00$
          \end{itemize}
      \end{itemize}
  \end{itemize}
  These results are precisely the data the responder had.$^*$\\~\\
  {\tiny *: We cannot distinguish the fact that $X_2$ is a non-response
  from the possibility that $X_2$ represents an actual return of a datum $D=0$
from the responder. In practice, one must avoid using $D=0$ to eliminate this
ambiguity}
\end{frame}
\end{document}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[english]{babel} % for hyphenation dictionary
%\setdefaultlanguage{english} % polyglossia command for use with XeTeX / LuaTeX
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header2.tex}

% title information
\title{Phys 221A -- Quantum Mechanics -- Lec19}
\author{UCLA, Fall 2014}
\date{\formatdate{8}{12}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed
         % format: formatdate{dd}{mm}{yyyy}

\begin{document}
\maketitle

\section{More angular momentum}

Recall from last time that we have a complete set of commuting operators in $J_z, \v J^2$. This set has ``non-rotational'' degrees of freedom. Furthermore we have
\begin{gather}
\v J^2 = J_z^2 + \frac{1}{2} (J_+ J_- + J_- J_+), \\
[\v J^2, J_z] = 0.
\end{gather}
So we have a set of simultaneous eigenstates $\ket{a,b}$ with eigenvalues
\begin{eqn}
\v J^2 \ket{a,b} = a \ket{a,b}, \qquad
J_z \ket{a,b} = b \ket{a,b}.
\end{eqn}
Since our operators $\v J^2, J_z$ are Hermitian we know of course that $a,b \in \R$.

Now, how can we determine the eigenvalues? We can use the commutation relations
\begin{eqn}
[J_z, J_\pm] = \pm \hbar J_\pm, \qquad
[\v J^2, J_\pm] = 0
\end{eqn}
where we know that
\begin{eqn}
J_\pm \ket{a,b} \propto \ket{a, b \pm \hbar}.
\end{eqn}
So we find that
\begin{gather}
J_z (J_\pm \ket{a,b}) = (b \pm \hbar) J_\pm \ket{a,b} \\
\v J^2 (J_\pm \ket{a,b}) = a (J_\pm \ket{a,b}).
\end{gather}
Furthermore we know that
\begin{align}
a &= \matrixel[0]{a,b}{\v J^2}{a,b} \\
	&= b^2 + \matrixel[0]{a,b}{J_x^2 + J_y^2}{a,b} \\
	&\geq b^2
\end{align}
so there must be exist a maximum and minimum $b_\mathrm{max}, b_\mathrm{min}$ value of $b$. Then since
\begin{eqn}
0 = \matrixel[0]{a,b_\mathrm{max}}{J_\pm J_\mp}{a, b_\mathrm{max}}
\end{eqn}
we have
\begin{eqn}
0 = a - b_\mathrm{max}^2 - \hbar b_\mathrm{max} = a - b_\mathrm{min}^2 - \hbar b_\mathrm{min}.
\end{eqn}
so that we must have $b_\mathrm{min} = -b_\mathrm{max}$. Then due to the ladder operator nature of $J_\pm$ we have
\begin{eqn}
b_\mathrm{max} = b_\mathrm{min} + n \hbar = \frac{n\hbar}{2}, \qquad
n \in \Z
\end{eqn}
So, defining $j = b_\mathrm{max} / \hbar = n/2$ and $m = b / \hbar$, we have
\begin{gather}
a = b_\mathrm{max} (b_\mathrm{max} + \hbar) = \hbar^2 j (j+1) = 0, \frac{1}{2}, 1, \frac{3}{2}, \dots, \\
b = \hbar m = -j, -j+1, \dots, j-1, j.
\end{gather}

It is convenient and standard to denote the eigenstates by $\ket{j,m}$ instead of $\ket{a,b}$. So for each value of $j = 0, \frac{1}{2}, 1, \frac{3}{2}, \dots$ we have a ladder of $2j+1$ states
\begin{eqn}
\ket{j,j}, \ket{j,j-1}, \dots, \ket{j,-j+1}, \ket{j,-j}
\end{eqn}
each of which can be obtained from its neighbors by acting upon it with $J_\pm$, up to a multiplying factor
\begin{eqn}
J_\pm \ket{j,m} = c^\pm_{jm} \ket{j, m \pm 1}.
\end{eqn}
We can determine these factor sy working out the matrix element
\begin{align}
\abs[0]{c^\pm_{jm}}^2
	&= \matrixel[0]{j,m}{J^\dagger_\pm J_\pm}{j,m} \\
	&= \matrixel[0]{j,m}{\v J^2 - J_z^2 \mp \hbar J_z}{j,m} \\
	&= \hbar^2 (j \mp m) (j \pm m + 1),
\end{align}
thus up to an arbitrary phase we have
\begin{eqn}
c^\pm_{jm} = \hbar \sqrt{(j \mp m) (j \pm m + 1)}.
\end{eqn}
Finally, for the general matrix element of $J_\pm$ we have
\begin{eqn}
\matrixel[0]{j,m}{J_\pm}{j',m'} = c^\pm_{jm} \delta_{j'j} \delta_{m',m\pm1}.
\end{eqn}

Now, recall that for $j=1/2$ we have the usual spinor representation
\begin{eqn}
J_z = \frac{\hbar}{2} \pmat{ 1 & 0 \\ 0 & -1}.
\end{eqn}
More generally, for arbitrary $j$ we have
\begin{eqn}
J_z = \hbar
\begin{pmatrix}
j & & & & \\
& j-1 & & & \\
& & \ddots & & \\
& & & -j+1 & \\
& & & & -j
\end{pmatrix}.
\end{eqn}
To find the eigenstates for the full Hilbert space, we can follow this procedure:
\begin{enumerate}
\item ``Diagonalize'' $\v J^2$, collecting eigenkets with the largest possible $J_z$, i.e. find all $\v J^2$ eigenstates. This gives us all possible $\ket{j,j}$s.
\item Next, orthonormalize them (e.g. using Gram-Schmidt) to generate other quantum numbers $n$ unrelated to $\v J^2$, giving us kets $\ket{n,j,j}$.
\item Finally, we can obtain the full kets via
\begin{eqn}
\ket{n,j,m} = \frac{(J_-)^{j-m} \ket{n,j,j}}{c^-_{1m} c^-_{2m} \cdots c^-_{j-m,m}}.
\end{eqn}
[This looks wrong, should be $c^-_{j,j} c^-_{j,j-1} \cdots c^-_{j,j-m}$ ?]
\end{enumerate}

\end{document}