\documentclass[11pt]{article}
\newcommand{\tuple}[1]{\ensuremath \langle #1 \rangle}
\usepackage{color}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{mathrsfs}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage[all]{xy}
\newtheorem{theorem}{Theorem}
\newtheorem{prop}{Proposition}
\newtheorem{lem}{Lemma}
\newtheorem{defn}{Definition}
\newcommand{\note}[1]{{\color{red} \bf [[#1]]}}
\newcommand{\push}[1][1]{\hskip\dimexpr #1\algorithmicindent\relax}
\begin{document}
\section{Approach}

\subsection{Keys}

Each device has: user id + password

Server login is:
hash1(user id), hash1(password)

Symmetric Crypto keys is:
hash2(user id | password)

Server has finite length queue of entries + max\_entry\_identifier +
server login key

\subsection{Entry layout}
Each entry has:
\begin{enumerate}
\item Sequence identifier
\item Random IV (if needed by crypto algorithm)
\item Encrypted payload
\end{enumerate}

Payload has:
\begin{enumerate}
\item Sequence identifier
\item Machine id (most probably something like a 64-bit random number 
that is self-generated by client)
\item HMAC of previous slot
\item Data entries
\item HMAC of current slot
\end{enumerate}

A data entry can be one of these:
\begin{enumerate}
\item All or part of a Key-value entry
\item Slot sequence entry: Machine id + last message identifier 
\newline {The purpose of this is to keep the record of the last slot 
from a certain client if a client's update has to expunge that other 
client's last entry from the queue. This is kept in the slot until 
the entry owner inserts a newer update into the queue.}
\item Queue state entry: Includes queue size \newline {The purpose 
of this is for the client to tell if the server lies about the number 
of slots in the queue, e.g. if there are 2 queue state entry in the queue, 
e.g. 50 and 70, the client knows that when it sees 50, it should expect 
at most 50 slots in the queue and after it sees 70, it should expect 
50 slots before that queue state entry slot 50 and at most 70 slots. 
The queue state entry slot 70 is counted as slot number 51 in the queue.}
\item Collision resolution entry: message identifier + machine id of a
collision winner
\newline {The purpose of this is to keep keep track of the winner of 
all the collisions until all clients have seen the particular entry.}
\end{enumerate}

\subsection{Live status}

Live status of entries:
\begin{enumerate}
\item Key-Value Entry is dead if either (a) there is a newer key-value pair or (b) it is incomplete.
	
\item Slot sequence number (of either a message version data
or user-level data) is dead if there is a newer slot from the same machine.

\item Queue state entry is dead if there is a newer queue state entry.
{In the case of queue state entries 50 and 70, this means that queue state 
entry 50 is dead and 70 is live. However, not until the number of slots reaches 
70 that queue state entry 50 will be expunged from the queue.}

\item Collision resolution entry is dead if this entry has been seen
by all clients after a collision happens.
\end{enumerate}

When data is at the end of the queue ready to expunge, if:
\begin{enumerate}
\item The key-value entry is not dead, it must be reinserted at the
beginning of the queue.

\item If the slot sequence number is not dead, then a message sequence
entry must be inserted.

\item If the queue state entry is not dead, it must be reinserted at the
beginning of the queue.
\end{enumerate}


\paragraph{Reads:}
Client sends a sequence number.  Server replies with either: all data
entries or all newer data entries.

\paragraph{Writes:}
Client sends slot, server verifies that sequence number is valid,
checks entry hash, and replies with an accept message if all checks
pass.  On success, client updates its sequence number.  On failure,
server sends updates slots to client and client validates those slots.

\paragraph{Local state on each client:}
A list of machines and the corresponding latest sequence numbers.

\paragraph{Validation procedure on client:}
\begin{enumerate}
\item Decrypt each new slot in order.
\item For each slot:
    (a) check its HMAC, and
    (b) check that the previous entry HMAC field matches the previous
    entry.
\item Check that the last-message entry for our machine matches the stored HMAC of our last message sent.
\item For all other machines, check that the latest sequence number is
at least as large (never goes backwards).
\item That the queue has a current queue state entry.
\item That the number of entries received is consistent with the size
specified in the queue state entry.
\end{enumerate}

Key-value entries can span multiple slots.  They aren't valid until
they are complete.

\subsection{Resizing Queue}
Client can make a request to resize the queue. This is done as a write that combines:
  (a) a slot with the message, and (b) a request to the server. The queue can only be expanded, never contracted; attempting to decrease the size of the queue will cause future clients to throw an error.

\subsection{Server Algorithm}
$s \in SN$ is a sequence number\\
$sv \in SV$ is a slot's value\\
$slot_s = \tuple{s, sv} \in SL \subseteq SN \times SV$ \\ \\
\textbf{State} \\
\textit{SL = set of live slots on server} \\
\textit{max = maximum number of slots (input only for resize message)} \\
\textit{n = number of slots} \\ \\
\textbf{Helper Function} \\
$MaxSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv}
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\
$MinSlot(SL_s)= \tuple{s, sv} \mid \tuple{s, sv} 
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\
$SeqN(slot_s = \tuple{s, sv})=s$ \\
$SlotVal(slot_s = \tuple{s, sv})=sv$ \\

\begin{algorithmic}[1]
\Function{GetSlot}{$s_g$}
\State \Return{$\{\tuple{s, sv} \in SL \mid s \geq s_g\}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{PutSlot}{$s_p,sv_p,max'$}
\If{$(max' \neq \emptyset)$}  \Comment{Resize}
\State $max \gets max'$
\EndIf
\State $\tuple{s_n,sv_n} \gets MaxSlot(SL)$\Comment{Last sv}
%\State $s_n \gets SeqN(\tuple{s_n,sv_n})$
\If{$(s_p = s_n + 1)$}
	\If{$n = max$}
    	\State $\tuple{s_m,sv_m} \gets MinSlot(SL)$\Comment{First sv}
		\State $SL \gets SL - \{\tuple{s_m,sv_m}\}$
	\Else \Comment{$n < max$}
		\State $n \gets n + 1$
	\EndIf
    \State $SL \gets SL \cup \{\tuple{s_p,sv_p}\}$
	\State \Return{$(true,\emptyset)$}
\Else
	\State \Return{$(false,\{\tuple{s,sv}\in SL \mid 
    s \geq s_p\})$}
\EndIf
\EndFunction
\end{algorithmic}

\subsection{Client Algorithm}
\subsubsection{Reading Slots}
\textbf{Data Entry} \\
$de$ is a data entry \\
$k$ is key of entry \\
$v$ is value of entry \\
$kv$ is a key-value entry $\tuple{k,v}$, $kv \in DE$ \\
$ss$ is a slot sequence entry $\tuple{id,s_{last}}$, 
id + last s of a machine, $ss \in DE$ \\
$qs$ is a queue state entry (contains $max$ size of queue), $qs \in DE$ \\
$cr$ is a collision resolution entry $\tuple{s_{col},id_{col}}$, 
s + id of a machine that wins a collision, $cr \in DE$ \\
$DE$ is a set of all data entries, possibly of different types, in a single message \\
$s \in SN$ is a sequence number \\
$id$ is a machine ID\\
$hmac_p$ is the HMAC value of the previous slot \\
$hmac_c$ is the HMAC value of the current slot \\
$Dat_s = \tuple{s,id,hmac_p,DE,hmac_c}$ \\
$slot_s = \tuple{s, E(Dat_s)} = 
\tuple{s, E(\tuple{s,id,hmac_p,DE,hmac_c})}$ \\ \\
\textbf{States} \\
\textit{$id_{self}$ = machine Id of this client} \\
\textit{$max_g$ = maximum number of slots (initially $max_g > 0$)} \\
\textit{m = number of slots stored on client (initially $m = 0$)} \\
\textit{$sl_{last}$ = info of last slot in queue = 
	$\tuple{s_{last},sv_{last},id_{last}}$ (initially $\emptyset$)} \\
\textit{DT = set of $\tuple{k,v}$ on client} \\
\textit{MS = associative array of $\tuple{id, s_{last}}$ of all clients on client 
(initially empty)} \\
\textit{$LV$ = live set of $kv$ entries on client, contains 
	$\tuple{kv,s}$ entries} \\
\textit{$SS_{live}$ = live set of $ss$ entries on client} \\
\textit{$CR_{live}$ = live set of $cr$ entries on client} \\
\textit{$MS_g$ = set MS to save all $\tuple{id, s_{last}}$ pairs while
traversing DT after a request to server (initially empty)} \\
\textit{SK = Secret Key} \\
\textit{$SM$ = associative array of $\tuple{s, id}$ of all slots in a previous read
(initially empty)} \\ \\
\textbf{Helper Functions} \\
$MaxSlot(SL_s)= \tuple{s, sv}$ \textit{such that} $\tuple{s, sv}
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \geq s_s$ \\
$MinSlot(SL_s)= \tuple{s, sv}$ \textit{such that} $\tuple{s, sv} 
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s \leq s_s$ \\
$Slot(SL_s,s_s)= \tuple{s, sv}$ \textit{such that} $\tuple{s, sv} 
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s = s_s$ \\
$SeqN(\tuple{s, sv})=s$ \\
$SlotVal(\tuple{s, sv})=sv$ \\
$CreateLastSL(s,sv,id)=\tuple{s,sv,id}=sl_{last}$ \\
$CreateEntLV(kv_s,s_s)=\tuple{kv_s,s_s}$ \\
$Decrypt(SK_s,sv_s)=Dat_s=\tuple{s,id,hmac_p,DE,hmac_c}$ \\
$GetSeqN(Dat_s = \tuple{s,id,hmac_p,DE,hmac_c})=s$ \\
$GetMacId(Dat_s = \tuple{s,id,hmac_p,DE,hmac_c})=id$ \\
$GetPrevHmac(Dat_s = \tuple{s,id,hmac_p,DE,hmac_c})=hmac_p$ \\
$GetCurrHmac(Dat_s = \tuple{s,id,hmac_p,DE,hmac_c})=hmac_c$ \\
$GetDatEnt(Dat_s = \tuple{s,id,hmac_p,DE,hmac_c})=DE$ \\
$GetLiveSS(SS_{live},ss_s)= ss$ \textit{such that} $ss \in SS_{live} 
\wedge \forall ss_s \in SS_{live}, ss = ss_s$ \\
$GetLiveCR(CR_{live},cr_s)= cr$ \textit{such that} $cr \in CR_{live} 
\wedge \forall cr_s \in CR_{live}, cr = cr_s$ \\
$GetLivEntLastS(LV_s,kv_s)= s$ \textit{such that} $\tuple{kv, s} \in LV_s 
\wedge \forall \tuple{kv_s, s_s} \in LV_s, kv_s = kv$ \\
$GetKV($key-value data entry$)=\tuple{k_s,v_s} = kv_s$ \\
$GetSS($slot-sequence data entry$)=\tuple{id_s,s_{s_{last}}} = ss_s$ \\
$GetQS($queue-state data entry$)=qs_s$ \\
$GetCR($collision-resolution data entry$)=\tuple{s_s,id_s} = cr_s$ \\
$GetKey(kv = \tuple{k, v})=k$ \\
$GetVal(kv = \tuple{k, v})=v$ \\
$GetId(ss = \tuple{id, s_{last}})=id$ \\
$GetSLast(ss = \tuple{id, s_{last}})=s_{last}$ \\
$GetS(cr = \tuple{s, id})=s$ \\
$GetId(cr = \tuple{s, id})=id$ \\
$GetKeyVal(DT_s,k_s)= \tuple{k, v}$ \textit{such that} $\tuple{k, v} 
\in DT_s \wedge \forall \tuple{k_s, v_s} \in DT_s, k = k_s$ \\
$MaxLastSeqN(MS_s)= s_{last}$ \textit{such that} $\tuple{id, s_{last}} \in MS_s 
\wedge \forall \tuple{id_s, s_{s_{last}}} \in MS_s, s_{last} \geq s_{s_{last}}$ \\
$MinLastSeqN(MS_s)= s_{last}$ \textit{such that} $\tuple{id, s_{last}} \in MS_s 
\wedge \forall \tuple{id_s, s_{s_{last}}} \in MS_s, s_{last} \leq s_{s_{last}}$ \\

\begin{algorithmic}[1]
\Procedure{Error}{$msg$}
\State $Print(msg)$
\State $Halt()$
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Function{ValidHmac}{$DE_s,SK_s,hmac_{stored}$}
\State $hmac_{computed} \gets Hmac(DE_s,SK_s)$
\State \Return {$hmac_{stored} = hmac_{computed}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{ValidPrevHmac}{$s_s,DE_s,hmac_{p_s},hmac_{p_{sto}}$}
\If{$s_s = 0 \land hmac_{p_s} = \emptyset$}\Comment{First slot - no previous HMAC}
	\State \Return $true$
\Else
	\State \Return {$hmac_{p_{sto}} = hmac_{p_s}$}
\EndIf
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetQueSta}{$DE_s$}
\State $de_{qs} \gets de_s$ \textit{such that} $de_s \in DE_s, 
	de_s \in D \land de_s = qs$
\If{$de_{qs} \neq \emptyset$}
	\State $qs_{ret} \gets GetQS(de_{qs})$
\Else
	\State $qs_{ret} \gets \emptyset$
\EndIf
\State \Return{$qs_{ret}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetSlotSeq}{$DE_s$}
\State $DE_{ss} \gets \{de_s | de_s \in DE_s, de_s = ss\}$
\State \Return{$DE_{ss}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetColRes}{$DE_s$}
\State $DE_{cr} \gets \{de_s | de_s \in DE_s, de_s = cr\}$
\State \Return{$DE_{cr}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{UpdateLastSeqN}{$id_s,s_s,MS_s$}
\State $s_t \gets MS_s[id_s]$
\If{$s_t = \emptyset$}
	\State $MS_s[id_s] = s_s$  \Comment{First occurrence}
\Else
	\State $MS_S[id_s] \gets max(s_t, s_s)$
\EndIf
\State \Return{$MS_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{UpdateKVLivEnt}{$LV_s,kv_s,s_s$}
\State $s_t \gets GetLivEntLastS(LV_s,kv_s)$
\If{$s_t = \emptyset$}
	\State $LV_s \gets LV_s \cup \{\tuple{kv_s,s_s}\}$\Comment{First occurrence}
\Else
	\If{$s_s > s_t$}\Comment{Update entry with a later s}
		\State $LV_s \gets (LV_s - \{\tuple{kv_s,s_t}\}) \cup 
			\{\tuple{kv_s,s_s}\}$
	\EndIf
\EndIf
\State \Return{$LV_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{AddSSLivEnt}{$SS_{s_{live}},de_s$}
\State $ss_s \gets GetSS(de_s)$
\State $ss_t \gets GetLiveSS(SS_{s_{live}},ss_s)$
\If{$ss_t = \emptyset$}
	\State $SS_{s_{live}} \gets SS_{s_{live}} \cup \{ss_s\}$\Comment{First occurrence}
\EndIf
\State \Return{$SS_{s_{live}}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{AddCRLivEnt}{$CR_{s_{live}},de_s$}
\State $cr_s \gets GetCR(de_s)$
\State $cr_t \gets GetLiveCR(CR_{s_{live}},cr_s)$
\If{$cr_t = \emptyset$}
	\State $CR_{s_{live}} \gets CR_{s_{live}} \cup \{cr_s\}$\Comment{First occurrence}
\EndIf
\State \Return{$CR_{s_{live}}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{UpdateSSLivEnt}{$SS_{s_{live}},MS_s$}
\State $s_{s_{min}} \gets MinLastSeqN(MS_s)$
\ForAll{$ss_s \in SS_{s_{live}}$}
	\State $s_{s_{last}} \gets GetSLast(ss_s)$
	\If{$s_{s_{min}} > s_{s_{last}}$}\Comment{Remove if dead}
		\State $SS_{s_{live}} \gets SS_{s_{live}} - \{ss_s\}$		
	\EndIf
\EndFor
\State \Return{$SS_{s_{live}}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{UpdateCRLivEnt}{$CR_{s_{live}},MS_s$}
\State $s_{s_{min}} \gets MinLastSeqN(MS_s)$
\ForAll{$cr_s \in CR_{s_{live}}$}
	\State $s_s \gets GetS(cr_s)$
	\If{$s_{s_{min}} > s_s$}\Comment{Remove if dead}
		\State $CR_{s_{live}} \gets CR_{s_{live}} - \{cr_s\}$	
	\EndIf
\EndFor
\State \Return{$CR_{s_{live}}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{CheckLastSeqN}{$MS_s,MS_t$}
\For {$\tuple{id, s_t}$ in $MS_t$}\Comment{Check $MS_t$ based on the newer $MS_s$}
	\State $s_s \gets MS_s[id]$
	\If{$s_s = \emptyset$}
    	\Call{Error}{'No $s$ for machine $id$'}
	\ElsIf{$id = id_{self}$ and $s_s \neq s_t$}
			\State \Call{Error}{'Invalid last $s$ for this machine'}
	\ElsIf{$id \neq id_{self}$ and $s_{s_{last}} < s_{t_{last}}$}
    	\State \Call{Error}{'Invalid last $s$ for machine $id$'}
    \Else
		\State $MS_t[id] \gets s_s$
	\EndIf
\EndFor
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{CheckCollision}{$MS_s,SM_s,cr_s$}
\If{$cr_s \neq \emptyset$}
	\State $s_s \gets GetS(cr_s)$
	\State $id_s \gets GetId(cr_s)$
	\State $s_{s_{last}} \gets GetLastSeqN(MS_s,id_s)$
	\If{$s_{s_{last}} < s_s$}
		\State $\Call{CheckColRes}{SM_s,cr_s}$
	\EndIf
\EndIf
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{CheckColRes}{$SM_s,cr_t$}\Comment{Check $id_s$ in $SM_s$}
\State $s_t \gets GetS(cr_t)$
\State $id_s \gets SM_s[s_t]$
\If{$id_s \neq id_t$}
	\State \Call{Error}{'Invalid $id$ for this slot update'}
\EndIf
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Function{StoreLastSlot}{$MS_s,sl_l,s_s,sv_s,id_s$}
\State $s_{min} \gets MinLastSeqN(MS_s)$
\If{$s_{min} \neq \emptyset \land s_{min} = s_s$}\Comment{$MS$ initially empty}
   	\State $sl_l \gets CreateLastSL(s_s,sv_s,id_s)$
\EndIf
\State \Return{$sl_l$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{UpdateDT}{$DT_s,DE_s,LV_s,s_s$}
\State $DE_{s_{kv}} \gets \{de_s | de_s \in DE_s, de_s = kv\}$
\ForAll{$de_s \in DE_s$}
	\State $kv_s \gets GetKV(de_s)$
	\State $LV_s \gets \Call{UpdateKVLivEnt}{LV_s,kv_s,s_s}$
	\State $k_s \gets GetKey(kv_s)$
	\State $\tuple{k_s,v_t} \gets GetKeyVal(DT_s,k_s)$
	\If{$\tuple{k_s,v_t} = \emptyset$}
		\State $DT_s \gets DT_s \cup \{\tuple{k_s,v_s}\}$
	\Else
		\State $DT_s \gets (DT_s - \{\tuple{k_s,v_t}\}) \cup 
			\{\tuple{k_s,v_s}\}$
	\EndIf
\EndFor
\State \Return{$DT_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{ProcessSL}{$SL_g$}
\State $MS_g \gets \emptyset$
\State $SM_{curr} \gets \emptyset$
\State $\tuple{s_{g_{min}},sv_{g_{min}}} \gets MinSlot(SL_g)$
\State $\tuple{s_{g_{max}},sv_{g_{max}}} \gets MaxSlot(SL_g)$
\For{$s_g \gets s_{g_{min}}$ \textbf{to} $s_{g_{max}}$}\Comment{Process slots 
	in $SL_g$ in order}
	\State $\tuple{s_g,sv_g} \gets Slot(SL_g,s_g)$
	\State $SM_{curr} \gets SM_{curr} \cup \{\tuple{s_g,sv_g}\}$
	\State $Dat_g \gets Decrypt(SK,sv_g)$
	\State $s_{g_{in}} \gets GetSeqN(Dat_g)$
    \If{$s_g \neq s_{g_{in}}$}
		\State \Call{Error}{'Invalid sequence number'}
	\EndIf
	\State $DE_g \gets GetDatEnt(Dat_g)$
	\State $hmac_{p_{stored}} \gets GetPrevHmac(Dat_g)$
	\If{$\neg \Call{ValidPrevHmac}{s_g,DE_g,hmac_{p_g},hmac_{p_{stored}}}$}
		\State \Call{ReportError}{'Invalid previous HMAC value'}
	\EndIf
	\State $hmac_{c_g} \gets GetCurrHmac(Dat_g)$
	\If{$\neg \Call{ValidHmac}{DE_g,SK,hmac_{c_g}}$}
		\State \Call{Error}{'Invalid current HMAC value'}
	\EndIf
	\State $hmac_{p_g} \gets Hmac(DE_g,SK)$\Comment{Update $hmac_{p_g}$ for next check}
	\State $qs_g \gets \Call{GetQueSta}{DE_g}$\Comment{Handle qs}
	\If{$qs_g \neq \emptyset \land qs_g > max_g$}
		\State $max_g \gets qs_g$
	\EndIf
    %Check for last s in Dat
	\State $id_g \gets GetMacId(Dat_g)$\Comment{Handle last s}
	\State $MS_g \gets \Call{UpdateLastSeqN}{id_g,s_g,MS_g}$
    %Check for last s in DE in Dat
	\State $DE_{g_{ss}} \gets \Call{GetSlotSeq}{DE_g}$\Comment{Handle ss}
	\If{$DE_{g_{ss}} \neq \emptyset$}
		\ForAll{$de_{g_{ss}} \in DE_{g_{ss}}$}
			\State $\tuple{id_d,s_{d_{last}}} \gets GetSS(de_{g_{ss}})$
			\State $MS_g \gets \Call{UpdateLastSeqN}{id_d,s_{d_{last}},MS_g}$
			\State $SS_{live} \gets \Call{AddSSLivEnt}{SS_{live},de_{g_{ss}}}$
		\EndFor
	\EndIf
	\State $DE_{g_{cr}} \gets \Call{GetColRes}{DE_g}$\Comment{Handle cr}
	\If{$DE_{g_{cr}} \neq \emptyset$}
		\ForAll{$de_{g_{cr}} \in DE_{g_{cr}}$}
			\State $cr_g \gets GetCR(de_{g_{cr}})$
			\State $\Call{CheckCollision}{MS,SM,cr_g}$
			\State $CR_{live} \gets \Call{AddCRLivEnt}{CR_{live},de_{g_{cr}}}$
		\EndFor
	\EndIf
	\State $sl_{last} \gets \Call{StoreLastSlot}{MS,sl_{last},s_g,sv_g,id_g}$
	\State $DT \gets \Call{UpdateDT}{DT,DE_g,LV,s_g}$
\EndFor
\State $SM \gets SM_{curr}$
\If{$m + |SL_g| \leq max_g$}\Comment{Check actual size against $max_g$}
	\State $m \gets m + |SL_g|$
\Else
	\State \Call{Error}{'Actual $SL$ size on server exceeds $max_g$'}
\EndIf
\State $\Call{CheckLastSeqN}{MS_g,MS}$
\State $\Call{UpdateSSLivEnt}{SS_{live},MS}$
\State $\Call{UpdateCRLivEnt}{CR_{live},MS}$
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{GetKVPairs}{}
\State $s_g \gets GetLastSeqN(MS,id_{self}) + 1$
\State $SL_c \gets \Call{GetSlot}{s_g}$
\State $\Call{ProcessSL}{SL_c}$\Comment{Process slots and update DT}
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetValFromKey}{$k_g$}
\State $\tuple{k_s,v_s} \gets \tuple{k,v}$ \textit{such that} $\tuple{k,v} 
	\in DT \land k = k_g$
\State \Return{$v_s$}
\EndFunction
\end{algorithmic}

\subsubsection{Writing Slots}
\textbf{States} \\
\textit{$cp$ = data entry $DE$ maximum size/capacity} \\
%\textit{$cr_p$ = saved cr entry $\tuple{s,id}$ on client if there is a collision
%(sent in the following slot)} \\
%\textit{$cr_{p_{last}}$ = saved cr entry $\tuple{s,id}$ on client if there is a 
%collision in reinserting the last slot (sent in the following slot)} \\
\textit{$ck_p$ = counter of $kv \in KV$ for putting pairs (initially 0)} \\
\textit{$ck_g$ = counter of $kv \in KV$ for getting pairs (initially 0)} \\
\textit{$cs_p$ = counter of $ss \in SS$ for putting pairs (initially 0)} \\
\textit{$cs_g$ = counter of $ss \in SS$ for getting pairs (initially 0)} \\
\textit{$cc_p$ = counter of $cr \in CR$ for putting pairs (initially 0)} \\
\textit{$cc_g$ = counter of $cr \in CR$ for getting pairs (initially 0)} \\
\textit{$hmac_{c_p}$ = the HMAC value of the current slot} \\
\textit{$hmac_{p_p}$ = the HMAC value of the previous slot 
($hmac_{p_p} = \emptyset$ for the first slot)} \\
\textit{$id_{self}$ = machine Id of this client} \\
\textit{$sl_{last}$ = info of last slot in queue = 
	$\tuple{s_{last},sv_{last},id_{last}}$ (initially $\emptyset$)} \\
\textit{$th_p$ = threshold number of dead slots for a resize to happen} \\
\textit{$m'_p$ = offset added to $max$ for resize} \\
\textit{$reinsert_{qs}$ = boolean to decide $qs$($max_g$) reinsertion} \\
\textit{$KV$ = set of $\tuple{ck, \tuple{k,v}}$ of kv entries on client} \\
\textit{$SS$ = set of $\tuple{cs, \tuple{id,s_{last}}}$ of ss entries on client} \\
\textit{$CR$ = set of $\tuple{cc, \tuple{s_{col},id_{col}}}$ of cr entries on client} \\
\textit{$SL_p$ = set of returned slots on client} \\
\textit{SK = Secret Key} \\ \\
\textbf{Helper Functions} \\
$CreateDat(s,id,hmac_p,DE,hmac_c)=Dat_s=\tuple{s,id,hmac_p,DE,hmac_c}$ \\
$CreateSS(id_s,s_{s_{last}})=\tuple{id_s,s_{s_{last}}} = ss_s$ \\
$CreateQS(max'_s)=qs_s$ \\
$CreateCR(s_s,id_s)=\tuple{s_s,id_s} = cr_s$ \\
$Encrypt(Dat_s,SK_s)=sv_s$ \\
$GetLastS(sl = \tuple{s,sv,id})=s$ \\
$GetSV(sl = \tuple{s,sv,id})=sv$ \\
$GetID(sl = \tuple{s,sv,id})=id$ \\
$GetColSeqN(SL_s,s_s)= \tuple{s, sv}$ \textit{such that} $\tuple{s, sv}
\in SL_s \wedge \forall \tuple{s_s, sv_s} \in SL_s, s = s_s$ \\
$GetKVPair(KV_s,k_s)= \tuple{ck,\tuple{k, v}}$ \textit{such that} 
$\tuple{ck,\tuple{k, v}} \in KV_s \wedge
\forall \tuple{ck_s,\tuple{k_s, v_s}} \in KV_s, k = k_s$ \\

\begin{algorithmic}[1]
\Function{PutKVPair}{$KV_s,\tuple{k_s,v_s}$}
\State $\tuple{ck_s,\tuple{k_s,v_t}} \gets GetKVPair(KV,k_s)$
\If{$\tuple{ck_s,\tuple{k_s,v_t}} = \emptyset$}
	\State $KV_s \gets KV_s \cup \{\tuple{ck_p, \tuple{k_s,v_s}}\}$
	\State $ck_p \gets ck_p + 1$
\Else
	\State $KV_s \gets (KV_s - \{\tuple{ck_s, \tuple{k_s,v_t}}\}) \cup 
	\{\tuple{ck_s, \tuple{k_s,v_s}}\}$
\EndIf
\State \Return{$KV_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{PutSSPair}{$SS_s,\tuple{id_s,s_{s_{last}}}$}\Comment{Insert a set of $ss$ entries}
\State $SS_s \gets SS_s \cup \{\tuple{cs_p, \tuple{id_s,s_{s_{last}}}}\}$
\State $cs_p \gets cs_p + 1$
\State \Return{$SS_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{PutCRPair}{$CR_s,\tuple{s_s,id_s}$}\Comment{Insert a set of $cr$ entries}
\State $CR_s \gets CR_s \cup \{\tuple{cc_p, \tuple{s_s,id_s}}\}$
\State $cc_p \gets cc_p + 1$
\State \Return{$CR_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{CheckResize}{$MS_s,th_s,max_t,m'_s$}
\State $s_{last_{min}} \gets MinLastSeqN(MS_s)$
\State $s_{last_{max}} \gets MaxLastSeqN(MS_s)$
\State $n_{live} \gets s_{last_{max}} - s_{last_{min}} + 1$\Comment{Number of live slots}
\State $n_{dead} \gets max_t - n_{live}$
\If{$n_{dead} \leq th_s$}
	\State $max'_s \gets max_t + m'_s$
\Else
	\State $max'_s \gets \emptyset$
\EndIf
\State \Return{$max'_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{CheckSLFull}{$MS_s,max_t$}\Comment{Check if $ss$ is needed}
\State $s_{last_{min}} \gets MinLastSeqN(MS_s)$
\State $s_{last_{max}} \gets MaxLastSeqN(MS_s)$
\State $n_{live} \gets s_{last_{max}} - s_{last_{min}}$\Comment{Number of live slots}
\State $n_{dead} \gets max_t - n_{live}$
\State \Return {$n_{dead} = 0$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{HandleCollision}{$SL_s,s_s$}
\State $\tuple{s_{col},sv_{col}} \gets GetColSeqN(SL_s,s_s)$
\State $Dat_{col} \gets Decrypt(SK,sv_{col})$
\State $id_{col} \gets GetMacId(Dat_{col})$
\State $cr_s \gets CreateCR(s_{col},id_{col})$
\State $\Call{ProcessSL}{SL_s}$
\State \Return{$cr_s$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{CheckLastSlot}{$sl_{s_{last}}$}
\State $s_s \gets GetLastS(sl_{s_{last}})$
\State $sv_s \gets GetSV(sl_{s_{last}})$
\State $Dat_s \gets Decrypt(SK,sv_s)$
\State $DE_s \gets GetDatEnt(Dat_s)$
\ForAll{$de_s \in DE_s$}
	\State $live \gets \Call{CheckLiveness}{s_s,de_s}$
	\If{$live$}
		\If{$de_s = kv$}
			\State $\tuple{k_s,v_s} \gets GetKV(de_s)$
			\State $KV \gets \Call{PutKVPair}{KV,\tuple{k_s,v_s}}$
		\ElsIf{$de_s = ss$}
			\State $\tuple{id_s,s_{s_{last}}} \gets GetSS(de_s)$
			\State $SS \gets \Call{PutSSPair}{SS,\tuple{id_s,s_{s_{last}}}}$
		\ElsIf{$de_s = cr$}
			\State $\tuple{s_s,id_s} \gets GetCR(de_s)$
			\State $CR \gets \Call{PutCRPair}{CR,\tuple{s_s,id_s}}$
		\ElsIf{$de_s = qs$}
			\State $reinsert_{qs} \gets true$
		\EndIf
	\EndIf
\EndFor
\EndProcedure
\end{algorithmic}

\begin{algorithmic}[1]
\Function{CheckLiveness}{$s_s,de_s$}
\State $live \gets true$
\If{$de_s = kv$}
	\State $s_l \gets GetLivEntLastS(LV,de_s)$
	\If{$s_l = \emptyset \lor s_s < s_l$}
		\State $live \gets false$
	\EndIf
\ElsIf{$de_s = ss$}
	\State $ss_s \gets GetSS(de_s)$
	\State $ss_l \gets GetLiveSS(SS_{live},ss_s)$
	\If{$ss_l = \emptyset$}
		\State $live \gets false$
	\EndIf
\ElsIf{$de_s = cr$}
	\State $cr_s \gets GetCR(de_s)$
	\State $cr_l \gets GetLiveCR(CR_{live},cr_s)$
	\If{$cr_l = \emptyset$}
		\State $live \gets false$
	\EndIf
\ElsIf{$de_s = qs$}
	\State $qs_s \gets GetQS(de_s)$
	\If{$qs_s \neq max_g$}
		\State $live \gets false$
	\EndIf
\Else
	\State \Call{Error}{'Unrecognized $de$ type'}
\EndIf
\State \Return{$live$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{CreateSlotSeq}{$sl_s$}
\State $id_s \gets GetID(sl_s)$
\State $s_{s_{last}} \gets GetLastS(sl_s)$
\State $ss_s \gets CreateSS(id_s,s_{s_{last}})$
\State \Return{$\tuple{ss_s}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{AddQueSta}{$DE_s,max'_s,cp_s$}\Comment{Insert a $qs$}
\State $DE_{ret} \gets \emptyset$
\State $qs_s \gets max'_s$
\State $DE_{ret} \gets DE_s \cup \{qs_s\}$
\State $cp_s \gets cp_s - 1$
\State \Return{$\tuple{DE_{ret},cp_s}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetKVPairs}{$DE_s,KV_s,cp_s$}
\State $DE_{ret} \gets \emptyset$
\If{$|KV_s| \leq cp_s$}\Comment{$KV$ set can span multiple slots}
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{k_s,v_s} \mid \tuple{ck_s,\tuple{k_s,v_s}} \in KV_s\}$
\Else
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{k_s,v_s} \mid \tuple{ck_s,\tuple{k_s,v_s}} \in KV_s,
		ck_g \leq ck_s < ck_g + cp_s\}$
\EndIf
\State \Return{$\tuple{DE_{ret}}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetSSPairs}{$DE_s,SS_s,cp_s$}
\State $DE_{ret} \gets \emptyset$
\If{$|SS_s| \leq cp_s$}\Comment{$SS$ set can span multiple slots}
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{id_s,s_{s_{last}}} \mid \tuple{cs_s,\tuple{id_s,s_{s_{last}}}} \in SS_s\}$
	\State $cp_s \gets cp_s - |SS_s|$
\Else
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{id_s,s_{s_{last}}} \mid \tuple{cs_s,\tuple{id_s,s_{s_{last}}}} \in SS_s,
		cs_g \leq cs_s < cs_g + cp_s\}$
	\State $cp_s \gets 0$
\EndIf
\State \Return{$\tuple{DE_{ret},cp_s}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Function{GetCRPairs}{$DE_s,CR_s,cp_s$}
\State $DE_{ret} \gets \emptyset$
\If{$|CR_s| \leq cp_s$}\Comment{$CR$ set can span multiple slots}
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{s_s,id_s} \mid \tuple{cc_s,\tuple{s_s,id_s}} \in CR_s\}$
	\State $cp_s \gets cp_s - |CR_s|$
\Else
	\State $DE_{ret} \gets DE_s \cup
    	\{\tuple{s_s,id_s} \mid \tuple{cc_s,\tuple{s_s,id_s}} \in CR_s,
		cc_g \leq cc_s < cc_g + cp_s\}$
	\State $cp_s \gets 0$
\EndIf
\State \Return{$\tuple{DE_{ret},cp_s}$}
\EndFunction
\end{algorithmic}

\begin{algorithmic}[1]
\Procedure{PutDataEntries}{$th_p,m'_p$}
\State $success_p \gets false$
\State $CR_p \gets \emptyset$
\While{$\neg success_p$}
	\State $DE_p \gets \emptyset$
	\State $s_p \gets MaxLastSeqN(MS)$
	\State $cp_p \gets cp$
	\State $max'_p \gets \Call{CheckResize}{MS,th_p,max_g,m'_p}$
	\If{$max'_p \neq \emptyset$}\Comment{Add a qs entry}
		\State $\tuple{DE_p,cp_p} \gets \Call{AddQueSta}{DE_p,max'_p,cp_p}$
		\State $reinsert_{qs} \gets false$
	\Else\Comment{Check if there is $qs$ reinsertion}
		\If{$reinsert_{qs}$}
			\State $\tuple{DE_p,cp_p} \gets \Call{AddQueSta}{DE_p,max_g,cp_p}$
			\State $reinsert_{qs} \gets false$
		\EndIf
	\EndIf
	\If{$SS \neq \emptyset$}\Comment{Add $ss$ entries}
		\State $\tuple{DE_p,cp_p} \gets \Call{GetSSPairs}{DE_p,SS,cp_p}$
	\EndIf
	\If{$CR \neq \emptyset$}\Comment{Add $cr$ entries}
		\State $\tuple{DE_p,cp_p} \gets \Call{GetCRPairs}{DE_p,CR,cp_p}$
	\EndIf
	\State $\tuple{DE_p,cp_p} \gets \Call{GetKVPairs}{DE_p,KV,cp_p}$\Comment{Add $kv$ entries}
	\State $hmac_{c_p} \gets Hmac(DE_p,SK)$
	\State $Dat_p \gets CreateDat(s_p,id_{self},hmac_{p_p},DE_p,hmac_{c_p})$
	\State $hmac_{p_p} \gets hmac_{c_p}$
	\State $sv_p \gets Encrypt(Dat_p,SK)$
	\State $\tuple{success_p,SL_p} \gets \Call{PutSlot}{s_p,sv_p,max'_p}$
	\If{$\neg success_p$}
		\State $cr_p \gets \Call{HandleCollision}{SL_p,s_p}$
		\State $\tuple{s_{p_{col}},id_{p_{col}}} \gets GetCR(cr_p)$
		\State $CR \gets \Call{PutCRPair}{CR,\tuple{s_{p_{col}},id_{p_{col}}}}$
	\EndIf
\EndWhile
\State $MS \gets \Call{UpdateLastSeqN}{id_{self},s_p,MS}$
\If{$|DE_p| = cp$}\Comment{Update set counters}
	\State $ck_g \gets ck_g + cp_p$\Comment{Middle of set}
	\State $cs_g \gets cs_g + |SS|$
	\State $cc_g \gets cc_g + |CR|$
\Else\Comment{End of set}
	\State $ck_g \gets 0$
	\State $cs_g \gets 0$
	\State $cc_g \gets 0$
\EndIf
\State $need_p \gets \Call{CheckSLFull}{MS,max_g}$
\If{$need_p$}\Comment{SL on server is full}
	\State $\Call{CheckLastSlot}{sl_{last}}$\Comment{Salvage entries from expunged slot}
	\State $ss_p \gets \Call{CreateSlotSeq}{sl_{last}}$
	\State $\tuple{id_p,s_{p_{last}}} \gets GetSS(ss_p)$
	\State $SS \gets \Call{PutSSPair}{SS,\tuple{id_p,s_{p_{last}}}}$\Comment{Add ss}
\EndIf
\EndProcedure
\end{algorithmic}

%\note{Lots of problems with PutDataEntries: (1) What happens if lose network connectivity after adding the key value pair, but before reinserting the last slot?  You probably need to create space first and then insert your data entry...  (2) What if reinsertlastslot kicks something else important out?  What if the server rejects our update because it is out of date?  At the very least, any putdataentries function w/o a loop is wrong!}

%\note{General comments...  Work on structuring things to improve readability...  This include names of functions/variables, how things are partitioned into functions, adding useful comments,...}
  
%\note{Also Missing liveness state definition in algorithm...}


\subsection{Formal Guarantees}
\subsubsection{Definitions}

\begin{defn}[Message]
A message $\mathsf{t}$, is the tuple 
\begin{center}
$\mathsf{t = \tuple{s, E(Dat_s)}}$ \\
$\mathsf{Dat_t = \tuple{s,id,hmac_p, DE,hmac_c}}$
\end{center}
containing $\mathsf{s}$ as sequence number and $\mathsf{Dat_t}$ as its 
encrypted contents. $\mathsf{Dat_t}$ consists of $\mathsf{s}$, 
$\mathsf{id}$ as machine ID of the sender, $\mathsf{hmac_p}$ as HMAC 
from a previous message, $\mathsf{DE}$ as set of data entries, and 
$\mathsf{hmac_c}$ as HMAC from message $\mathsf{t}$ respectively.
\end{defn}

\begin{defn}[Equality]
Two messages $\mathsf{t}$ and $\mathsf{u}$ are equal if their $\mathsf{s}$, 
and $\mathsf{Dat_t}$ are exactly the same.
\end{defn}

\begin{defn}[Parent]
A parent of a message $\mathsf{t}$ is the message $\mathsf{\mathscr{P}_t}$, 
unique by the correctness of HMACs in $\mathsf{Dat_t}$, such that 
$\mathsf{hmac_p(t) = hmac_c(\mathscr{P}_t)}$.
\end{defn}

\begin{defn}[Chain]
A chain of messages with length $\mathsf{n \ge 1}$ is a message sequence 
$\mathsf{R = (r_s, r_{s+1}, ..., r_{s+n-1})}$ such that for every sequence 
number $\mathsf{s < k \le s+n-1}$, $\mathsf{r_k}$ has sequence number 
$\mathsf{k}$ and is the parent of $\mathsf{r_{k-1}}$.
\end{defn}

\begin{defn}[Partial sequence]
A partial sequence $\mathsf{P}$ is a sequence of messages, no two 
with the same sequence number, that can be divided into disjoint chains.
\end{defn}

\begin{defn}[Total sequence]
A total sequence $\mathsf{T =}$ $\mathsf{(t_1, t_2, ..., t_n)}$ with 
length $\mathsf{n}$ is a chain of messages that starts at $\mathsf{s = 1}$.
\end{defn}

\begin{defn}[Path]
The path of a message $\mathsf{t}$ is the chain that starts at $\mathsf{s = 1}$ 
and whose last message is $\mathsf{t}$. The uniqueness of a path follows 
from the uniqueness of a parent.
\end{defn}

\begin{defn}[Consistency]
A partial sequence $\mathsf{P}$ is consistent with a total sequence 
$\mathsf{T}$ of length $\mathsf{n}$ if for every message $\mathsf{p \in P}$ 
with $\mathsf{s_p \leq n}$, $\mathsf{t_{s_p} = p}$. This implies that 
$\mathsf{\{p \in P | s_p \le n\}}$ is a partial sequence of $\mathsf{T}$.
\end{defn}

\begin{defn}[Transitive closure]
Transitive closure set at sequence number $\mathsf{s_n}$ is a set 
$\mathsf{\mathscr{S}}$ of clients comprising a connected component of an 
undirected graph, where two clients are connected by an edge if they both 
received the same message $\mathsf{t}$ with sequence number $\mathsf{s_t > n}$.
\end{defn}

\subsubsection{Lemmas and Proofs}

\begin{prop} 
Every client $\mathsf{J}$ who sends a message $\mathsf{t}$ 
has $\mathsf{\mathscr{P}_t}$ as its latest stored message, and 
$\mathsf{s_t = s_{\mathscr{P}_t} + 1}$. 
\end{prop}
\begin{proof} True by definition, because $J$ sets 
$\mathsf{hmac_p(t) = hmac_c(\mathscr{P}_t)}$ and 
$\mathsf{s_t = }$ $\mathsf{s_{\mathscr{P}_t + 1}}$ when a message 
is sent. 
\end{proof}

\begin{prop} If a rejected message entry is added to the set $\mathsf{CR}$ 
at sequence number $\mathsf{s}$, the message will remain in $\mathsf{CR}$ 
until every client has seen it. 
\end{prop}
\begin{proof} Every $\mathsf{CR}$ entry $\mathsf{cr}$ remains in the queue until it 
reaches the tail, and is refreshed by the next sender $\mathsf{J}$ at that time if 
$\mathsf{min(MS) > s_{cr}}$; that is, until every client has sent a message with 
sequence number greater than $\mathsf{s_{cr}}$. Because every client who sends a 
message with sequence number $\mathsf{s}$ has the state of the set $\mathsf{SL}$ at 
$\mathsf{s - 1}$, this client will have seen the message at $\mathsf{s_{cr}}$. 
\end{proof}

\begin{figure}[h]
  \centering
      \xymatrix{ & & L \\
\dots \ar[r] & q \ar[dr]_{J} \ar[r]^{K} & l_1 \ar[r] & l_2 \ar[r] & \dots \ar[r] & p \ar[r] & \dots \ar[r] & l_n = u \\
& & r_1 \ar[r] & r_2 \ar[r] & \dots \ar[r] & r_m = t \\
& & R
\save "2,3"."2,8"*+\frm{^\}}
\save "3,3"."3,6"*+\frm{_\}}
\restore
\restore
}
\caption{By Lemma 1, receiving $t$ after $u$ is impossible.}
\end{figure}

\begin{lem} Two messages are received without errors by a client $\mathsf{C}$; 
call them $\mathsf{t}$ and $\mathsf{u}$ such that $\mathsf{s_t \le s_u}$. 
Then $\mathsf{t}$ is in the path of $\mathsf{u}$. 
\end{lem}
\begin{proof}
Clearly $\mathsf{C}$ will throw an error if $\mathsf{s_t = s_u}$. So 
$\mathsf{s_t < s_u}$. Additionally, if $\mathsf{C}$ receives $\mathsf{u}$ before 
$\mathsf{t}$, this will cause it to throw an error, so $\mathsf{t}$ is received 
before $\mathsf{u}$.

Assume that $\mathsf{t}$ is not in the path of $\mathsf{u}$. Take $\mathsf{u}$ 
to be the packet of smallest sequence number for which this occurs, and 
$\mathsf{t}$ be the packet with greatest sequence number for this $\mathsf{u}$. 
We will prove that an error occurs upon receipt of $\mathsf{u}$.

Let $\mathsf{r_1}$ be the earliest member of the path of $\mathsf{t}$ that is 
not in the path of $\mathsf{u}$, and $\mathsf{q}$ be its parent. Message 
$\mathsf{q}$, the last common ancestor of $\mathsf{t}$ and $\mathsf{u}$, must exist, 
since all clients and the server were initialized with the same state. Let 
$\mathsf{l_1}$ be the successor of $\mathsf{q}$ that is in the path of $\mathsf{u}$; 
we know $\mathsf{l_1 \neq r_1}$. Let $\mathsf{R = (r_1, r_2, \dots, r_m = t)}$ be 
the distinct portion of the path of $\mathsf{t}$, and similarly let $\mathsf{L}$ 
be the distinct portion of the path of $\mathsf{l_n = u}$.

Let $\mathsf{J}$ be the client who sent $\mathsf{r_1}$; that is, such that 
$\mathsf{{id_{self}}_J = GetMacID(r_1)}$, and $\mathsf{K}$ be the client who 
sent $\mathsf{l_1}$.

We know the following three facts: 

\begin{enumerate}

\item Because no client can send two messages with the same sequence number, and 
$\mathsf{s_{r_1} = s_{l_1} = s_q + 1}$, $\mathsf{J \neq K}$.

\item To send a message $\mathsf{p}$ that is the parent of some other 
message, one must have received the parent of $\mathsf{p}$. Since 
$\mathsf{u}$ is the message with smallest sequence number received by any 
client that violates this lemma, no client receives both a message in $\mathsf{r}$ 
and a message in $\mathsf{l}$; therefore, no client sends both a message in 
$\mathsf{(r_2,...,t)}$ and a message in $\mathsf{(l_2,...,u)}$.

\item Since $\mathsf{u}$ is the message with the greatest- and least- sequence 
number that violates this lemma, $\mathsf{C}$ does not receive any message with 
sequence number strictly between $\mathsf{s_t}$ and $\mathsf{s_u}$. Because the 
$\mathsf{s_{last}}$ that $\mathsf{C}$ stores increases at every message receipt 
event, $\mathsf{C}$ also does not receive any message after $\mathsf{t}$ and before 
$\mathsf{u}$ in real time.

\end{enumerate}
There are two cases:
\begin{itemize}
\item Case 1: $\mathsf{J}$ did not send a message in $\mathsf{L}$. Then 
%$\mathsf{v_J(t) > v_J(q) = v_J(u)}$.
$\mathsf{s_{t_J} > s_{q_J} = s_{u_J}}$.
\begin{itemize}
\item Case 1.1: $\mathsf{C}$ never updates its slot sequence list $\mathsf{SS}$ 
between receiving $\mathsf{t}$ and receiving $\mathsf{u}$; this can only happen if 
$\mathsf{s_t = s_u - 1}$. Since $\mathsf{t}$ is not the parent of $\mathsf{u}$, 
$\mathsf{hmac_p(u) \neq hmac_c(t)}$, causing $\mathsf{C}$ to error.
\item Case 1.2: Case 1.1 does not occur; therefore, $\mathsf{C}$ must update 
its slot sequence list $\mathsf{SS}$ at some point between receiving $\mathsf{t}$ 
and $\mathsf{u}$. 
The latest sequence number of $\mathsf{J}$ decreases during this time, which 
means it must decrease when some message is received, which means $\mathsf{C}$ 
throws an error in the $\mathsf{CheckLastSeqN}$ subroutine.
\end{itemize}

\item Case 2: $\mathsf{J}$ sent at least one message in $\mathsf{L}$. Call the 
first one $\mathsf{p}$. We know that $\mathsf{s_p > s_{r_1}}$, since 
$\mathsf{J \neq K}$ and $\mathsf{p \neq l_1}$. Message $\mathsf{r_1}$ must be sent 
either before or after $\mathsf{p}$.
\begin{itemize}
\item Case 2.1: Client $\mathsf{J}$ sends $\mathsf{p}$, and then $\mathsf{r_1}$. 
Before sending $\mathsf{p}$, the greatest sequence number of a message that 
$\mathsf{J}$ has received, $\mathsf{{s_{last}}_J}$, must be equal to 
$\mathsf{s_p - 1 \ge s_{r_1}}$. Since $\mathsf{{s_{last}}_J}$ never decreases, 
client $\mathsf{J}$ cannot then send a message with sequence number 
$\mathsf{s_{r_1}}$, a contradiction.
\item Case 2.2: Client $\mathsf{J}$ sends $\mathsf{r_1}$, and then $\mathsf{p}$. 
Let $\mathsf{X = (r_1, \dots )}$ be the list of messages $\mathsf{J}$ sends 
starting before $\mathsf{r_1}$ and ending before $p$.
\begin{itemize}
\item Case 2.2.1: Some message in $\mathsf{X}$ was accepted. Let $\mathsf{s_{w_J}}$ 
be the greatest sequence number of the messages that client $\mathsf{J}$ sent in 
the path of message $\mathsf{w}$. In this case, before sending $\mathsf{p}$, 
$\mathsf{J}$'s value in $\mathsf{SM_J}$ for its own latest sequence number would 
be strictly greater than $\mathsf{s_{q_J}}$. If there is a sequence of messages with 
contiguous sequence numbers that $\mathsf{J}$ receives between $\mathsf{r_1}$ and 
$\mathsf{p}$, $\mathsf{J}$ throws an error for a similar reason as Case 1.1. Otherwise, 
when preparing to send $\mathsf{p}$, $\mathsf{J}$ would have received an update of its 
own latest sequence number as at most $\mathsf{s_{q_J}}$. $J$ throws an error before 
sending $\mathsf{p}$, because its own latest sequence number decreases.
\item Case 2.2.2: All messages in $\mathsf{X}$ were rejected, making $\mathsf{p}$ 
the first message of $\mathsf{J}$ that is accepted after $\mathsf{r_1}$. Note that 
$\mathsf{u}$ is the message with least sequence number that violates this lemma, and 
therefore the first one that $\mathsf{C}$ receives after $\mathsf{t}$. Therefore, 
$\mathsf{C}$ could not have seen a message after $\mathsf{t}$ with sequence number less 
than $\mathsf{s_p}$. In the $\mathsf{PutDataEntries}$ subroutine, $\mathsf{J}$ adds every 
%TODO: Check with Thomas about this part
rejected message to $\mathsf{CR(p)}$; so for every $\mathsf{s}$, $\mathsf{1 \leq s < |X|}$, 
the $\mathsf{s}$-th element of $\mathsf{X}$ will be recorded in the rejected message list
$\mathsf{CR}$ of all further elements of $\mathsf{X}$; and every element of 
$\mathsf{X}$ will be recorded in $\mathsf{CR_C(p)}$. Since every rejected message in 
$\mathsf{CR(p)}$ will be in $\mathsf{CR_C(u)}$, ${r_1}$ will be recorded in 
$\mathsf{CR_C(p)}$. When $\mathsf{C}$ receives $\mathsf{u}$, $\mathsf{C}$ will throw 
an error from the match $\mathsf{\tuple{s_q+1, id_J}}$ in $\mathsf{CR_C(p)}$.
\item Case 2.2.2.1: 
\end{itemize}
\end{itemize}

\end{itemize}
\end{proof}

\begin{lem} If there are two packets $\mathsf{t}$ and $\mathsf{u}$, with 
$\mathsf{s_t \leq s_u}$, such that $\mathsf{t}$ is in the path of $\mathsf{u}$, 
then for any message $\mathsf{p}$ with $\mathsf{s_p \leq s_t}$, iff $\mathsf{p}$ is in 
the path of $\mathsf{t}$, it is in the path of $\mathsf{u}$. 
\end{lem}

\begin{proof}
If $\mathsf{s_t = s_u}$ or $\mathsf{s_p = s_t}$, then we are done, because the two 
relevant messages are the same. If they are different messages, then:
\begin{itemize}
\item Reverse direction: The definition of $\mathsf{t}$ being in the path of 
$\mathsf{u}$ is the existence of a message sequence $\mathsf{(\dots, t, \dots, u)}$ 
such that each message except $\mathsf{u}$ is the parent of the succeeding message. 
The path of $\mathsf{u}$ must contain some message with sequence number $\mathsf{s_p}$; 
because $\mathsf{p}$ is in the path of $\mathsf{u}$, this message is $\mathsf{p}$ 
itself. The path of $\mathsf{t}$ is then the prefix of this path ending at $\mathsf{t}$, 
which clearly contains $\mathsf{p}$.

\item Forward direction: The path of $\mathsf{t}$ is a substring of the path of 
$\mathsf{u}$, so if the path of $\mathsf{t}$ contains $\mathsf{p}$, so does the path 
of $\mathsf{u}$.
\end{itemize}
\end{proof}

\begin{theorem}
Suppose that there is a transitive closure set $\mathsf{\mathscr{S}}$ of clients, 
at sequence number $\mathsf{s_n}$. Then there is some total sequence $\mathsf{T}$ of 
length $\mathsf{n}$ such that every client $\mathsf{C}$ in $\mathsf{\mathscr{S}}$ 
sees a partial sequence $\mathsf{P_C}$ consistent with $\mathsf{T}$. 
\end{theorem}

\begin{proof}

The definition of consistency of $\mathsf{P_C}$ with $\mathsf{T}$ is that every message 
$\mathsf{p \in P_C}$ with sequence number $\mathsf{s_p \le s_n}$ is equal to the message 
in that slot in $\mathsf{T}$. Let $\mathsf{C_1}$ be some client in the transitive closure 
set, with partial sequence $\mathsf{P_{C_1}}$, and let $\mathsf{u}$ be some message with 
$\mathsf{s_u > s_n}$ that $\mathsf{C_1}$ shares with another client. Then let $\mathsf{T}$ 
be the portion of the path of $\mathsf{u}$ ending at sequence number $\mathsf{s_n}$ and 
$\mathsf{t}$ be the message at that sequence number. Clearly, by Lemma 1, $\mathsf{P_{C_1}}$ 
is consistent with $\mathsf{T}$. We will show that, for every other client $\mathsf{D}$ 
with partial sequence $\mathsf{P_D}$, $\mathsf{P_D}$ has some message whose path includes 
$\mathsf{t}$. Because $\mathsf{D}$ is in the transitive closure, there is a sequence of 
clients $\mathsf{\mathscr{C} = (C_1, C_2, ..., D)}$ from $\mathsf{C_1}$ to $\mathsf{D}$, 
where each shares an edge with the next.
We prove by induction that $\mathsf{P_D}$ has a message whose path includes $\mathsf{t}$.
\begin{itemize}
\item Base case: $\mathsf{P_{C_1}}$ includes $\mathsf{u}$, whose path includes $\mathsf{t}$.

\item Inductive step: Each client in $\mathsf{\mathscr{C}}$ has a partial sequence with a message 
that includes $\mathsf{t}$ if the previous client does. Suppose $\mathsf{P_{C_k}}$ has 
a message $\mathsf{w}$ with a path that includes $\mathsf{t}$, and shares message $\mathsf{x}$ 
with $\mathsf{P_{C_{k+1}}}$ such that $\mathsf{s_x > s_n}$. By Lemma 1, $\mathsf{w}$ or 
$\mathsf{x}$, whichever has the least sequence number, is in the path of the other, and therefore 
by Lemma 2, $\mathsf{t}$ is in the path of $\mathsf{x}$.

\item Let $\mathsf{z}$ be the message of $\mathsf{D}$ whose path includes $\mathsf{t}$. 
By Lemma 1, every message in $\mathsf{P_D}$ with sequence number smaller than $\mathsf{s_w}$ 
is in the path of $\mathsf{z}$. Since $\mathsf{t}$ is in the path of $\mathsf{z}$, every 
message in $\mathsf{P_D}$ with smaller sequence number than $\mathsf{s_t = s_n}$ is in $\mathsf{T}$. 
Therefore, $\mathsf{P_D}$ is consistent with $\mathsf{T}$.

\end{itemize}
\end{proof}

\subsection{Future Work}
\paragraph{Support Messages}
  A message is dead once receiving machine sends an entry with a newer
  sequence identifier

\paragraph{Persistent data structures}
	Root object w/ fields
	Other objects can be reachable from root
	Each object has its own entries
	Dead objects correspond to dead 

\paragraph{Multiple App Sharing}

Idea is to separate subspace of entries...  Shared with other cloud...
\end{document}
