arnaucube
arnaucube/davinci-paperPublic Self-hosted repository
davinci-paper/v2/sections/circuits.tex
v2/sections/circuits.texRaw
  1% !TeX root = ../build/main.tex
  2
  3At the core of \davinci lie a set of arithmetic circuits that enforce the correctness of every step of the election. Each circuit corresponds to a distinct task and, taken together, they guarantee that all protocol rules are satisfied without revealing any private information. Specifically, the \emph{ballot circuit} (Section~\ref{sec:vocdoni-protocol:circuits:ballot}) ensures that encrypted votes are valid and well-formed, the \emph{verifier circuit} (Section~\ref{sec:vocdoni-protocol:circuits:verifier}) verifies the voter's proof; the \emph{aggregation circuit} (Section~\ref{sec:vocdoni-protocol:circuits:aggregation}) combines multiple authenticated votes into a single proof; the \emph{state transition circuit} (Section~\ref{sec:vocdoni-protocol:circuits:state-transition}) updates the global state root and produces the \ac{ZK-SNARK} proof that is verified on-chain; and the \textit{results circuit} (Section~\ref{sec:vocdoni-protocol:circuits:results}) proves that the election results were computed correctly. The flow of these circuits and their interactions is shown in Figure~\ref{fig:circuits-flow}. 
  4\begin{figure}[h]
  5	\centerline{\includegraphics[width=\linewidth,draft=false]{\figs/circuits-flow.pdf}}
  6	\caption{
  7		Overview of the circuit flow in \davinci. {\color{blue}\textit{Results circuit} is missing.}}
  8	\label{fig:circuits-flow}
  9\end{figure}
 10
 11For clarity, the circuits are described in this section with their full set of public inputs explicitly listed. In the actual implementation, however, we apply an optimization: rather than exposing all public inputs $\publicinputs$ individually, we compute a single commitment $h = \hlget{\Mimc}(\publicinputs)$ and use $h$ as the only public input. The proof verifier then checks that $\hlget{\Mimc}(\publicinputs) = h$, ensuring that the original public inputs are consistent while reducing both proof size and verification cost. Note that this optimization does not alter the semantics of the circuits but significantly reduces verifier complexity and on-chain verification costs. Other implementation aspects such as the exact number of constraints and the proving frameworks used are deferred to Section~\ref{sec:analysis:implementation}.
 12
 13% Old text: By structuring the process this way, we ensure that voting can be performed from any device-including smartphones and web browsers-while keeping the sequencer's computational requirements within the capabilities of accessible, CPU-based machines with 64 GiB of memory.
 14
 15\subsubsection{Ballot circuit} 
 16\label{sec:vocdoni-protocol:circuits:ballot}
 17
 18The ballot circuit (\hlset{\ballotcircuit}), illustrated in Figure~\ref{fig:circuit-ballot}, is generated locally by the voter at the time of casting a ballot. Its purpose is twofold: first, to prove that the encrypted ballot is valid and complies with the protocol rules defined by the organizer; and second, to prove that the vote identifier (\voteid) has been correctly derived. The correctness of this circuit is attested by a \ac{ZK} proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BN254 curve. We call the resulting proof \textit{ballot proof} and denote it as $\hlset{\ballotproof}$. We detail below the inputs and constraints of the ballot circuit. 
 19
 20\begin{spacing}{1.25}
 21	\vspace{0.3cm}
 22	\noi \textbf{Public inputs}
 23	\begin{itemize}
 24		\item $\public$ $\processid$: election identifier. 
 25		\item $\public$ $\ballotmode$: ballot protocol configuration.
 26		\item $\public$ $\epk$: encryption public key.
 27		\item $\public$ $\address$: voter's address.
 28		\item $\public$ $\weight$: voter's weight.
 29		\item $\public$ $\encryptedballot$: encrypted ballot. 
 30		\item $\public$ $\voteid$: vote's unique identifier. 
 31	\end{itemize}
 32	\textbf{Private inputs}
 33	\begin{itemize}								
 34		\item \private $\ballot$: voter's voting choice.
 35		\item \private $k$: random scalar.
 36	\end{itemize}
 37	\pagebreak
 38	\textbf{Constraints}
 39	\begin{itemize}								
 40		\item \emph{Vote encryption}. Correct encryption of the ballot: \[\encryptedballot = \hlget{\Enc}_{\epk}(\ballot; k).\]
 41		\item \emph{Protocol rules compliance}. The voter's ballot meets the protocol rules (see Section~\ref{sec:ballot-protocol}): 
 42		\[\text{protocolRulesCompliance}(\ballotmode, \weight, \ballot) = \true.\]
 43		\item \emph{Vote identifier}. The vote's identifier is correctly computed: 
 44		\[\voteid = \text{Hash} (\processid \conc  \address \conc k).\]
 45	\end{itemize}
 46	\vspace{-0.3cm}
 47\end{spacing}
 48
 49\begin{figure}[H]
 50	\centerline{\includegraphics[width=380pt,draft=false]{\figs/circuit1.pdf}}
 51	\caption{Ballot circuit. All public values are framed in green.}
 52	\label{fig:circuit-ballot}
 53\end{figure}
 54
 55\subsubsection{Verifier circuit}
 56\label{sec:vocdoni-protocol:circuits:verifier}
 57
 58The verifier circuit (\hlset{\verifiercircuit}), illustrated in Figure~\ref{fig:circuit-verifier}, is generated by the sequencer when processing a vote. This circuit verifies the ballot's proof generated by the voter and the correctness of their digital signature. The correctness of this circuit is attested by a ZK proof generated using the ZK-SNARK protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BLS12-377 curve. We call the resulting proof the \textit{authentication proof} and denote it as $\hlset{\authenticationproof}$. We detail below the inputs and constraints of this circuit. {\color{blue}-- Authentication of verification proof?}
 59
 60\begin{spacing}{1.25}
 61	\vspace{0.3cm}
 62	\noi \textbf{Public inputs}
 63	\begin{itemize}
 64		\item \public $\processid$: election identifier.
 65		\item \public $\ballotmode$: ballot protocol configuration.
 66		\item \public $\epk$: encryption public key.
 67		\item \public $\encryptedballot$: encrypted ballot.		
 68		\item \public $\voteid$: vote's unique identifier.
 69		\item \public $\address$: voter's address.
 70		\item \public $\weight$: voter's weight.
 71	\end{itemize}
 72	\pagebreak
 73	\textbf{Private inputs}
 74	\begin{itemize}		
 75		\item \private $\pk$: voter's public key.						
 76		\item \private $\signature$: voter's signature.
 77		\item \private $\ballotproof$: voter's ballot proof.
 78	\end{itemize}
 79	\textbf{Constraints}	
 80	\begin{itemize}
 81		\item \emph{Authentication + non-malleability.} The voter submitted and signed their vote.
 82		\begin{itemize}
 83		\renewcommand{\labelitemii}{$\circ$}
 84			\item The signature of the vote's identifier is valid for the given public key:
 85			$$ \hlget{\SVer}_{\pk}(\voteid, \signature) = \true. $$
 86			\item The address is correctly derived from the user's public key:
 87			$$\address = \hlget{\Keccak}(\pk).$$
 88		\end{itemize}
 89		\item \emph{Voter's proof verification.} The voter's submitted proof is valid for the inputs provided.
 90		\begin{itemize}
 91		\renewcommand{\labelitemii}{$\circ$}		
 92			\item Set public inputs:
 93			\begin{align*}
 94				\publicinputs = (\processid, \ballotmode, \epk, \encryptedballot, 
 95				\voteid, \weight, \address).
 96			\end{align*}
 97			\item Verify proof: 
 98				$$\hlget{\PVerify}(\ballotproof, \publicinputs) = \true.$$
 99		\end{itemize}
100	\end{itemize}
101	\vspace{-0.3cm}
102\end{spacing}
103
104\begin{figure}[H]
105	\centerline{\includegraphics[width=450pt,draft=false]{\figs/circuit2.pdf}}
106	\caption{Authentication circuit. All public values are framed in green.}
107	\label{fig:circuit-verifier}
108\end{figure}
109
110\subsubsection{Aggregation circuit}
111\label{sec:vocdoni-protocol:circuits:aggregation}
112
113The aggregation circuit (\hlset{\aggregationcircuit}), illustrated in Figure~\ref{fig:circuit-aggregate}, is generated by the sequencer to combine multiple authenticated votes into a single proof. Its purpose is to reduce verification overhead by recursively aggregating individual authentication proofs, while ensuring that all aggregated votes belong to the same election.  The batch size ($\batchsize$), i.e., the maximum number of proofs aggregated in a single execution, is a fixed parameter (currently set to 60). If fewer proofs are available, the sequencer pads the batch with dummy proofs so that the circuit always operates on a fixed-size input. The correctness of this circuit is attested by a ZK proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BW6-761 curve. We call the resulting proof the \textit{aggregation proof} and denote it as $\hlset{\aggregationproof}$. We detail below the inputs and constraints of the aggregation circuit.  
114
115\begin{spacing}{1.25}
116	\vspace{0.3cm}
117	\noi \textbf{Public inputs}
118	\begin{itemize}
119		\item \public $\{\encryptedballot_i\}_{i=1}^{\batchsize}$: set of voters' encrypted ballots.
120		\item \public $\{\voteid_i\}_{i=1}^{\batchsize}$: set of vote identifiers.
121		\item \public $\{\address_i\}_{i=1}^{\batchsize}$: set of voters' addresses.
122		\item \public $\processid$: election identifier.
123		\item \public $\ballotmode$: ballot protocol configuration.
124		\item \public $\epk$: encryption public key.
125	\end{itemize}
126	\textbf{Private inputs}
127	\begin{itemize}								
128		\item \private $\{\authenticationproof_i\}_{i=1}^{\batchsize}$: set of authentication proofs.
129	\end{itemize}
130	\textbf{Constraints} \\ \vspace{-0.4cm}		
131	\begin{itemize}
132		\item \emph{Votes aggregation.} The accumulated authentication proofs are valid. For every $i\in\{1,\dots,\batchsize\}$:
133		\begin{itemize}
134			\renewcommand{\labelitemii}{$\circ$}		
135			\item Set public inputs:
136			\begin{align*}
137				\publicinputs = (\encryptedballot_i, \voteid_i, \address_i, \weight_i, \processid, \ballotmode, \epk).
138			\end{align*}
139			\item Verify proof: 
140			$$\hlget{\PVerify}(\authenticationproof_i, \publicinputs) = \true.$$
141		\end{itemize}
142		\item \emph{Shared public inputs.} All authentication proofs are verified using the same public inputs \processid, \ballotmode, and \epk.
143	\end{itemize}
144	\vspace{-0.3cm}
145\end{spacing}
146
147\begin{figure}[h]
148	\centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit3.pdf}}
149	\caption{Aggregation circuit. All public values are framed in green.}
150	\label{fig:circuit-aggregate}
151\end{figure}
152
153\vspace{-0.5cm}
154\subsubsection{State transition circuit} 
155\label{sec:vocdoni-protocol:circuits:state-transition}
156
157The state transition circuit (\hlset{\statetransitioncircuit}), illustrated in Figure~\ref{fig:circuit-state-transition-v2}, is generated by the sequencer to update the global state of the election. Its purpose is to verify that a batch of aggregated votes has been correctly incorporated into the state Merkle tree, that overwrites and vote identifiers are handled consistently, that the accumulators of encrypted results are updated accordingly, that each included vote corresponds to an eligible voter included in the census, and that the state transition data used by the sequencer matches the data made available through Ethereum data blobs. The correctness of this circuit is attested by a \ac{ZK} proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BN254 curve, which is supported by Ethereum’s native precompiles for proof verification. We call the resulting proof the \textit{state proof} and denote it as $\hlset{\stateproof}$. This proof is submitted on-chain and verified by the process management smart contract, ensuring that every accepted state transition complies with the protocol rules.
158
159To formalize the setting, let the sequencer collect a batch of $N$ votes ($N\leq60$), of which $n$ are new vote submissions and $m = N-n$ are overwrites of previously cast ballots. Without loss of generality, we assume that the batch is ordered such that the first $n$ votes correspond to new submissions, and the remaining $m$ votes correspond to overwrites. In the following, we describe the inputs and constraints of the state transition circuit in detail.
160
161\begin{spacing}{1.25}
162	\vspace{0.3cm}
163	\noi \textbf{Public inputs}
164	\begin{itemize}
165		\item \public $\censusroot$: Merkle root of the census tree.
166		\item \public $\stateroot^\old$: current Merkle root of the state tree.
167		\item \public $\stateroot^\new$: new tree root after all state transitions are applied.
168		\item \public $\numoverwrites^\old$: current number of overwrites.
169		\item \public $\numoverwrites^\new$: number of overwrites after all state transitions are applied.
170		\item \public $\numvotes^\old$: current number of votes.
171		\item \public $\numvotes^\new$: total number of votes after all state transitions are applied.
172		\item \public $C$: commitment to the data blob.
173	\end{itemize}
174	\textbf{Private inputs}
175	\begin{itemize}			
176		\item \private $\processid$: election identifier.		
177		\item \private $\censusorigin$: census origin parameter.
178		\item \private $\ballotmode$: ballot protocol configuration.
179		\item \private $\epk$: encryption public key.
180		\item \private $\addaccumulator^\old$: current accumulator of votes (before transitions are applied).
181		\item \private $\substractaccumulator^\old$: current accumulator of votes that have been overwritten (before transitions).	\\		
182		%
183		\item \private $\{\aggregationproof_i\}_{i = 1}^{N}$: set of aggregation proofs.
184		\item \private $\{\censuspf_i\}_{i = 1}^N$: census membership proofs.
185		%
186		\item \private $\{\voteid_i\}_{i = 1}^{N}$: set of vote's identifiers.
187		\item \private $\{\encryptedballot_i\}_{i = 1}^{N}$: set of voters' encrypted ballots.
188		\item \private $\{\encryptedballot_i^\old\}_{i = n+1}^{N}$: set of all old encrypted ballots that will be overwritten.
189		\item \private $\{\address_i\}_{i = 1}^{N}$: list of voters' addresses.
190		\item \private $\{\weight_i\}_{i = 1}^{N}$: list of voters' weights. \\
191		%
192		\item \private $\merkleInclusionPf^{\processid}$: proof that $\processid$ is stored in leaf $\texttt{0x0}$ of the state tree.
193		\item \private $\merkleInclusionPf^{\censusorigin}$: proof that $\censusorigin$ is stored in leaf $\texttt{0x1}$ of the tree.
194		\item \private $\merkleInclusionPf^{\ballotmode}$: proof that $\ballotmode$ is stored in leaf $\texttt{0x2}$ of the state tree.
195		\item \private $\merkleInclusionPf^{\addaccumulator}$: proof that $\addaccumulator^\old$ is stored in leaf $\texttt{0x3}$ of the state tree.
196		\item \private $\merkleInclusionPf^{\substractaccumulator}$: proof that $\substractaccumulator^\old$ is stored in leaf $\texttt{0x4}$ of the state tree.\\
197		%
198		\item \private $\{\merkleNInclusionPf^{\encryptedballot}_i\}_{i = 1}^{n}$: proof that the leaf corresponding to a voter's $\address_i$ that submits a new vote is empty. 
199		\item \private $\{\merkleInclusionPf^{\encryptedballot}_i\}_{i = n+1}^{N}$: proof that the vote $\encryptedballot_i^\old$ is stored in the leaf corresponding to a voter's $\address$ that submits the vote overwrite $\encryptedballot_i$.
200		\item \private $\{\merkleNInclusionPf^{\voteid}_i\}_{i= 1}^{N}$: non-membership proofs of vote identifiers.\\
201		%
202		\item \private $k$: random scalar.
203		\item \private $\openproof$: KZG opening proof.
204	\end{itemize}
205	\textbf{Constraints} \\ \vspace{-0.4cm}		
206	\begin{itemize}
207		\item \emph{Aggregation verification.} The accumulated authentication proofs are valid. For every $i\in\{1,\dots,N\}$:
208		\begin{itemize}
209			\renewcommand{\labelitemii}{$\circ$}		
210			\item Set public inputs:
211			\begin{align*}
212				\publicinputs = (\weight_i, \address_i, \encryptedballot_i, \voteid_i, \ballotmode, \epk, \processid).
213			\end{align*}
214			\item Verify proof: 
215			$$\hlget{\PVerify}(\aggregationproof_i, \publicinputs) = \true.$$
216		\end{itemize}
217		\item \emph{Census membership.} Each processed vote must correspond to an eligible voter, according to the census configuration specified by the parameter \texttt{censusOrigin}. 
218		\begin{itemize}
219			\item If \texttt{censusOrigin} = CSP, then verify a signature issued by the CSP. Concretely, for all $i \in \{1,\dots,N\}$:
220			\[
221			\hlget{\SVer}_{\censusroot}\big((\address_i, \weight_i), \censuspf_i\big) = \true,
222			\]
223			where \(\censusroot\) contains the CSP public key and \(\censuspf_i\) is the eligibility signature attesting that voter \(i\) belongs to the census with the given weight. {\color{blue}Missing parameters in the message being signed, e.g. ProcessID.}
224			
225			\item Otherwise, if the census is represented by a lean \ac{IMT}, the circuit verifies a Merkle membership proof showing that the pair \((\address_i, \weight_i)\) is included in the committed census. For all $i \in \{1,\dots,N\}$:
226			\[
227			\hlget{\censusTree}.\hlget{\MTVerify}\big((\address_i, \weight_i), \censuspf_i, \censusroot\big) = \true.
228			\]
229		\end{itemize}
230		
231		%%%%%
232		\item \emph{Transition verification.} Verify that the initial state and the state transition updates are applied correctly.
233				\begin{itemize}
234					\renewcommand{\labelitemii}{$\circ$}
235					\item Verify that the inputs $\processid$, $\censusorigin$, $\ballotmode$, $\epk$, $\addaccumulator^\old$, and $\substractaccumulator^\old$ correspond to the content of the first state tree leaves.
236					\begin{align*}
237						&\hlget{\stateTree}.\hlget{\MTVerify}(\processid, \texttt{0x0}, \merkleInclusionPf^{\processid}, \stateroot^\old).	\\				
238						&\hlget{\stateTree}.\hlget{\MTVerify}(\censusorigin, \texttt{0x1}, \merkleInclusionPf^{\censusorigin}, \stateroot^\old).	\\				
239						&\hlget{\stateTree}.\hlget{\MTVerify}(\ballotmode, \texttt{0x2}, \merkleInclusionPf^{\ballotmode}, \stateroot^\old).	\\				
240						&\hlget{\stateTree}.\hlget{\MTVerify}(\epk, \texttt{0x3}, \merkleInclusionPf^{\epk}, \stateroot^\old).	\\				
241						&\hlget{\stateTree}.\hlget{\MTVerify}(\addaccumulator^\old, \texttt{0x4}, \merkleInclusionPf^{\addaccumulator}, \stateroot^\old).	\\				
242						&\hlget{\stateTree}.\hlget{\MTVerify}(\substractaccumulator^\old, \texttt{0x5}, \merkleInclusionPf^{\substractaccumulator}, \stateroot^\old).				
243					\end{align*}
244					\item Verify that none of the $\voteid_i$ for all $i\in\{1,\dots,N\}$ are initially part of the tree:
245					\[\hlget{\stateTree}.\hlget{\MTVerify}(\voteid_i, \merkleNInclusionPf^{\voteid}_i, \stateroot^\old).\] 
246					\item Ensure that new votes are not part of the tree yet. For $i = 1, \dots, n$:
247					\[\hlget{\stateTree}.\hlget{\MTVerify}(\address_i, \merkleNInclusionPf^{\encryptedballot}_i, \stateroot^\old).\] 
248					\item Ensure that overwrites do correspond to votes that were already on the tree. For $i = 1, \dots, m$:
249					\[\hlget{\stateTree}.\hlget{\MTVerify}(\encryptedballot_i^\old, \address_i, \merkleInclusionPf^{\encryptedballot}_i, \stateroot^\old).\] 
250					\item State transitions.
251					\begin{itemize}
252						\item For every new vote $i = 1, \dots, n$:
253							\begin{enumerate}
254								\item Reencrypt the ballot:
255								$\encryptedballot_i = \hlget{\ReEnc}_{\epk}(\encryptedballot_i, k).$
256								\item Refresh reencryption randomness: $k = \hlget{\Mimc}(k).$
257								\item Add reencrypted ballot to the tree: $\hlget{\stateTree}.\hlget{\MTInsert}(\encryptedballot_i, \address_i)$.
258								\item Add vote identifier to the tree: $\hlget{\stateTree}.\hlget{\MTInsert}(\voteid_i)$.
259								\item Increase votes counter: $\numvotes = \numvotes + 1$.
260								\item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot_i$.
261						\end{enumerate}
262						\item For every overwritten vote $i = n+1, \dots, N$:
263							\begin{enumerate}	\item Refresh reencryption randomness: $k = \hlget{\Mimc}(k).$
264								\item Reencrypt the ballot:
265								$\encryptedballot_i = \hlget{\ReEnc}_{\epk}(\encryptedballot_i, k).$
266								\item Overwrite the encrypted ballot: $\hlget{\stateTree}.\hlget{\MTUpdate}(\encryptedballot, \address_i)$.
267								\item Add vote identifier: $\hlget{\stateTree}.\hlget{\MTInsert}(\voteid)$.
268								\item Increase votes counter: $\numvotes = \numvotes + 1$.
269								\item Increase overwrites counter: $\numoverwrites = \numoverwrites + 1$.
270								\item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot_i$.
271								\item Aggregate the old vote to the accumulator: $\substractaccumulator = \substractaccumulator + \encryptedballot^\old_i$.
272							\end{enumerate}
273					\end{itemize}
274					\item After all previous state transitions are done, recompute new tree root:
275					\[\stateroot^\new = \hlget{\stateTree}.\hlget{\MTRoot}().\]
276					\item {\color{blue} Reencrypt a set of old leafs from the tree: first, very their Merkle tree inclusion proofs, then reencrypt them, and finally update each leaf's content.}
277				\end{itemize}
278	\item \emph{Data blob}. 
279		\begin{itemize}
280			\item From all leaves that changed after the state transitions in the $\hlget{\stateTree}$ tree, construct blob $B$.
281			%(as described in Section~{\color{blue}X})
282			\item Derive polynomial $p(x)$ from $B$.
283			\item Compute evaluation point
284					\( z = \hlget{\Poseidon}(\processid, C, \openproof) \).
285			\item Compute evaluation \(y = p(z)\).
286			\item Verify  
287			\(\hlget{\CVerify}(z, y, C, \openproof) = \true.\)\\
288		\end{itemize} 
289	\end{itemize}
290	\vspace{-0.3cm}
291\end{spacing}
292
293\begin{remark}
294	Splitting the sequencer computation into three circuits provides an important efficiency benefit. If the global $\stateroot$ changes while a sequencer is preparing a proof, only the last circuit must be re-run to reflect the updated state, avoiding the need to recompute all previous proofs.
295\end{remark} 
296
297\begin{landscape}
298	\centering
299	{\setkeys{Gin}{draft=false}%shows image even in draft mode
300		\includegraphics[width=\linewidth]{\figs/circuit4.pdf}}
301	\captionof{figure}{State transition circuit. All public values are framed in green. {\color{blue}\textbf{Missing inputs:} old encryptedBallots.}}
302	\label{fig:circuit-state-transition-v2}
303\end{landscape}
304
305\subsubsection{Results circuit}
306\label{sec:vocdoni-protocol:circuits:results}
307
308The results circuit (\hlset{\resultscircuit}) proves that... .
309
310\begin{figure}[h]
311	\centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit5.pdf}}
312	\caption{Aggregation circuit. All public values are framed in green. \color{red}Circuit is not correct.}
313	\label{fig:circuit-results}
314\end{figure}