arnaucube
arnaucube/davinci-paperPublic Self-hosted repository
davinci-paper/v2/sections/state-tree.tex
v2/sections/state-tree.texRaw
 1% !TeX root = ../build/main.tex
 2
 3% \noi Merkle trees are used to create a cryptographic representation of both the voter census registry and the election state. 
 4
 5The state Merkle tree, denoted by $\hlset{\stateTree}$, represents the evolving global state of an election in \davinci. It is implemented as a \ac{SMT} of fixed depth $D = 64$, as described in Appendix~\ref{sec:cryptographic-primitives:merkle-trees}. The state tree acts as a cryptographic accumulator that compactly commits to all relevant election data, including configuration parameters, vote identifiers, and encrypted ballots.
 6By organizing the election state in a single \ac{SMT}, \davinci supports efficient membership and update proofs that can be verified inside \ac{ZK} circuits, enabling succinct and verifiable state transitions.
 7
 8The root of the state tree, denoted $\stateroot$, is committed on-chain and serves as the authoritative reference to the current election state. Each state transition performed by a sequencer updates the tree and produces a new root. The correctness of this update, namely, that it follows the protocol rules and correctly incorporates new votes, is attested by a \ac{ZK-SNARK} proof, which is verified by the $\hlget{\statetransitionverifier}$ smart contract before the new root is accepted.
 9
10
11\paragraph{Namespaced key space.} 
12
13The state tree operates over keys in the range $[0, 2^D-1] \subset \mathbb{F}_p$ and uses a SNARK-friendly hash function (\hlget{\Poseidon}, see Appendix~\ref{sec:cryptographic-primitives:hash}) to enable efficient in-circuit verification. To prevent collisions between different categories of data stored in the state, the 64-bit key space is partitioned into three disjoint numeric namespaces using a fixed thershold $N$. We define \( N = 2^{D-1}\), 
14which we use to split the address space into a lower region for configuration parameters and encrypted ballot storage,  and an upper region strictly reserved for vote identifiers. This strict separation ensures that configuration entries, vote identifiers, and ballots never collide. %The structure of the tree is depicted in Fig.~\ref{fig:mt-state}.
15%the 64-bit key space is partitioned into disjoint numeric namespaces by leveraging the most significant bit (MSB) of the address, effectively splitting the space exactly in half at the threshold N. 
16
17
18%\begin{figure}[h]
19%	\centerline{\includegraphics[width=450pt,draft=false]{\figs/mt-state-mermaid}}
20%	\caption{Structure of the state Merkle tree. The first leaves are reserved for global election parameters while subsequent leaves store vote identifiers and encrypted ballots.}
21%	\label{fig:mt-state}
22%\end{figure}
23
24\paragraph{Configuration namespace.}
25
26The lowest indices of the tree are reserved for a small set of process parameters. Most are configuration values fixed at election setup and never modified thereafter
27(\processid, \ballotmode, \epk, \censusorigin); the remaining entry is a mutable accumulator (\encresults) updated on every state transition. In all cases these keys
28occupy deterministic, well-known positions and use a negligible portion of the address space. The default entries are:
29%The lowest indices of the tree are reserved for a small set of process parameters and accumulators. Their positions are deterministic and occupy a negligible portion of the address space. Typical entries include:
30\begin{itemize}
31	\item \texttt{0x0}: the election identifier (\processid).
32	%\footnote{The index \texttt{0x1} is reserved for backward compatibility (historically used for alternative census structures).}
33	\footnote{The indices \texttt{0x1} and \texttt{0x5} are currently unused: \texttt{0x1} is
34		reserved for backward compatibility (historically used for alternative census structures),
35		and \texttt{0x5} is reserved for future use (an earlier design stored a separate subtractive
36		results accumulator there; the current design keeps a single net results accumulator at
37		\texttt{0x4}).}
38	\item \texttt{0x2}: the ballot mode configuration, which is the set of rules for validating votes (\ballotmode). 
39	\item \texttt{0x3}: the encryption public key used to encrypt the ballots (\epk).
40	\item \texttt{0x4}: the running encrypted results (\encresults). 
41%	\item \texttt{0x5}: an accumulator of the encrypted votes that have been overwritten (\substractaccumulator).
42	\item \texttt{0x6}: the census origin (\censusorigin). %and \type, a field in case the organization was using a different structure for the census than a MT.
43\end{itemize}
44Let $\configmax$ denote the largest index reserved for configuration parameters (by default, $\configmax = 15$).
45
46\paragraph{Encrypted ballot namespace.}
47
48The region immediately following the configuration parameters, the interval $[\configmax + 1, N - 1]$, is reserved for storing encrypted ballots. 
49%In contrast to vote identifiers, 
50Ballot storage locations are deterministic and derived from the voter's position in the census. Let $\idx$ denote the voter's census index (see Section~\ref{sec:vocdoni-protocol:census}), proven via a census membership proof. The storage path for the encrypted ballot is computed as
51\[
52\text{encPath} = \configmax + \idx \cdot 2^{16} + (\address \bmod 2^{16}).
53\]
54This construction assigns each eligible voter a unique, reserved leaf in the state tree. Because the index $\idx$ is authenticated by the census, a voter can only write to their own slot. This enables efficient \emph{last-vote-wins} semantics: if a voter overwrites their ballot, the new ciphertext replaces the previous one at the same path, while the tally is updated by subtracting the old contribution and adding the new one.
55
56\martai{The leaf stores the ciphertext and the address of the voter. Justify that this is to prevent CPS' and dynamic census to assign the same encPath to two different people.}
57\martai{If $idx = 0$ and $address = 0$, then $encPath = configMax$ but the lower limit should be $configMax + 1$.}
58
59\paragraph{Vote identifier namespace.}
60
61The upper portion of the tree, $[N, 2^D - 1]$ is reserved for \emph{vote identifiers} (\voteid), which allows voters to verify that their vote has been included in the election state, without revealing or linking to the corresponding encrypted ballot. Vote identifiers are derived by hashing the process identifier, the voter’s address, and fresh randomness, and then mapping the result into the allowed range by setting the most significant bit to 1:
62\[
63\voteid = N + \bigl(\hlget{\Poseidon}(\processid,\address,k) \bmod N\bigr).
64\]
65The \hlget{\statetransitioncircuit} recomputes this derivation and enforces that $N \le \voteid < 2^D$, guaranteeing that vote identifiers cannot overlap with configuration entries or ballot storage.
66
67Unlike classical nullifiers, vote identifiers are not used to prevent duplicate voting directly. Instead, correctness is enforced at the circuit level: the state transition circuit guarantees that a vote identifier can only be inserted if the corresponding leaf in the state tree is empty. This emptiness check is enforced inside the ZK circuit. Since the vote identifier depends on a fresh random value $k$, if a collision occurs (i.e., the computed leaf is already occupied), the circuit will reject the transition. In that case, the voter must resample $k$ and recompute a new vote identifier until an unused leaf is obtained. 
68As a result, collisions do not compromise correctness or liveness, but merely require retrying the identifier derivation.
69
70Note that the maximum number of vote identifiers that can be stored is bounded by the size of this namespace ($2^{D-1}$), 
71which upper-bounds the total number of votes and overwrites that can be processed throughout the election.
72%This capacity is orders of magnitude larger than any realistic deployment, ensuring that identifier exhaustion is practically impossible.
73This capacity is orders of magnitude larger than any realistic election workload: historical elections peak at $\approx 10^{9}$ votes~\cite{reuters2024india}, ensuring that identifier exhaustion is practically impossible and that collisions are exceptionally rare
74\footnote{Based on the birthday paradox, the probability of at least one collision occurring is given by $P=1 - e^{-n^2/2d}$, where $n$ is the number of vote identifiers and $d = 2^{D-1}$. For a large-scale national election of $10^8$ votes emitted, this probability is vanishingly small ($\approx0.054\%$), and for an extreme workload of $10^9$ votes, the probability of a single collision is only $\approx5.4\%$, meaning that the need to resample $k$ remains highly improbable.}.
75
76Together, the state tree provides a compact and tamper-proof commitment to the current status of the election. At the end of the election, the final state root together with the on-chain verification of all transitions serves as a complete cryptographic record of the election, encapsulating both the tally and the integrity of the entire voting procedure.