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 census membership; 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.}
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 \textbf{Constraints}
38 \begin{itemize}
39 \item \emph{Vote encryption}. Correct encryption of the ballot: \[\encryptedballot = \hlget{\Enc}_{\epk}(\ballot; k).\]
40 \item \emph{Protocol rules compliance}. The voter's ballot meets the protocol rules (see Section~\ref{sec:ballot-protocol}):
41 \[\text{protocolRulesCompliance}(\ballotmode, \weight, \ballot) = \true.\]
42 \item \emph{Vote identifier}. The vote's identifier is correctly computed:
43 \[\voteid = \text{Hash} (\processid \conc \address \conc k).\]
44 \end{itemize}
45 \vspace{-0.3cm}
46\end{spacing}
47
48\begin{figure}[H]
49 \centerline{\includegraphics[width=380pt,draft=false]{\figs/circuit1.pdf}}
50 \caption{Ballot circuit. All public values are framed in green.}
51 \label{fig:circuit-ballot}
52\end{figure}
53
54\subsubsection{Verifier circuit}
55\label{sec:vocdoni-protocol:circuits:verifier}
56
57The 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 it also validates the voter’s eligibility in the census 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?}
58
59\begin{spacing}{1.25}
60 \vspace{0.3cm}
61 \noi \textbf{Public inputs}
62 \begin{itemize}
63 \item \public $\processid$: election identifier.
64 \item \public $\ballotmode$: ballot protocol configuration.
65 \item \public $\epk$: encryption public key.
66 \item \public $\encryptedballot$: encrypted ballot.
67 \item \public $\voteid$: vote's unique identifier.
68 \item \public $\address$: voter's address.
69 \item \public $\weight$: voter's weight.
70 \end{itemize}
71 \textbf{Private inputs}
72 \begin{itemize}
73 \item \private $\pk$: voter's public key.
74 \item \private $\signature$: voter's signature.
75 \item \private $\ballotproof$: voter's ballot proof.
76 \end{itemize}
77 \textbf{Constraints}
78 \begin{itemize}
79 \item \emph{Authentication + non-malleability.} The voter submitted and signed their vote.
80 \begin{itemize}
81 \renewcommand{\labelitemii}{$\circ$}
82 \item The signature of the vote's identifier is valid for the given public key:
83 $$ \hlget{\SVer}_{\pk}(\voteid, \signature) = \true. $$
84 \item The address is correctly derived from the user's public key:
85 $$\address = \hlget{\Keccak}(\pk).$$
86 \end{itemize}
87 \item \emph{Voter's proof verification.} The voter's submitted proof is valid for the inputs provided.
88 \begin{itemize}
89 \renewcommand{\labelitemii}{$\circ$}
90 \item Set public inputs:
91 \begin{align*}
92 \publicinputs = (\processid, \ballotmode, \epk, \encryptedballot,
93 \voteid, \weight, \address).
94 \end{align*}
95 \item Verify proof:
96 $$\hlget{\PVerify}(\ballotproof, \publicinputs) = \true.$$
97 \end{itemize}
98 \end{itemize}
99 \vspace{-0.3cm}
100\end{spacing}
101
102\begin{figure}[H]
103 \centerline{\includegraphics[width=450pt,draft=false]{\figs/circuit2.pdf}}
104 \caption{Authentication circuit. All public values are framed in green.}
105 \label{fig:circuit-verifier}
106\end{figure}
107
108\subsubsection{Aggregation circuit}
109\label{sec:vocdoni-protocol:circuits:aggregation}
110
111The 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.
112
113\begin{spacing}{1.25}
114 \vspace{0.3cm}
115 \noi \textbf{Public inputs}
116 \begin{itemize}
117 \item \public $\{\encryptedballot_i\}_{i=1}^{\batchsize}$: set of voters' encrypted ballots.
118 \item \public $\{\voteid_i\}_{i=1}^{\batchsize}$: set of vote identifiers.
119 \item \public $\{\address_i\}_{i=1}^{\batchsize}$: set of voters' addresses.
120 \item \public $\processid$: election identifier.
121 \item \public $\ballotmode$: ballot protocol configuration.
122 \item \public $\epk$: encryption public key.
123 \end{itemize}
124 \textbf{Private inputs}
125 \begin{itemize}
126 \item \private $\{\authenticationproof_i\}_{i=1}^{\batchsize}$: set of authentication proofs.
127 \end{itemize}
128 \textbf{Constraints} \\ \vspace{-0.4cm}
129 \begin{itemize}
130 \item \emph{Votes aggregation.} The accumulated authentication proofs are valid. For every $i\in\{1,\dots,\batchsize\}$:
131 \begin{itemize}
132 \renewcommand{\labelitemii}{$\circ$}
133 \item Set public inputs:
134 \begin{align*}
135 \publicinputs = (\encryptedballot_i, \voteid_i, \address_i, \weight_i, \processid, \ballotmode, \epk).
136 \end{align*}
137 \item Verify proof:
138 $$\hlget{\PVerify}(\authenticationproof_i, \publicinputs) = \true.$$
139 \end{itemize}
140 \item \emph{Shared public inputs.} All authentication proofs are verified using the same public inputs \processid, \ballotmode, and \epk.
141 \end{itemize}
142 \vspace{-0.3cm}
143\end{spacing}
144
145\begin{figure}[h]
146 \centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit3.pdf}}
147 \caption{Aggregation circuit. All public values are framed in green.}
148 \label{fig:circuit-aggregate}
149\end{figure}
150
151\vspace{-0.5cm}
152\subsubsection{State transition circuit}
153\label{sec:vocdoni-protocol:circuits:state-transition}
154
155The 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, and that the accumulators of encrypted results are updated accordingly {\color{blue}[+census membership +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.
156
157To 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.
158
159\begin{spacing}{1.25}
160 \vspace{0.3cm}
161 \noi \textbf{Public inputs}
162 \begin{itemize}
163 \item \public $\censusroot$: Merkle root of the census tree.
164 \item \public $\stateroot^\old$: current Merkle root of the state tree.
165 \item \public $\stateroot^\new$: new tree root after all state transitions are applied.
166 \item \public $\numoverwrites^\old$: current number of overwrites.
167 \item \public $\numoverwrites^\new$: number of overwrites after all state transitions are applied.
168 \item \public $\numvotes^\old$: current number of votes.
169 \item \public $\numvotes^\new$: total number of votes after all state transitions are applied.
170 \item \public $C$: commitment to the data blob.
171 \end{itemize}
172 \textbf{Private inputs}
173 \begin{itemize}
174 \item \private $\processid$: election identifier.
175 \item \private $\censusorigin$: census origin parameter.
176 \item \private $\ballotmode$: ballot protocol configuration.
177 \item \private $\epk$: encryption public key.
178 \item \private $\addaccumulator^\old$: current accumulator of votes (before transitions are applied).
179 \item \private $\substractaccumulator^\old$: current accumulator of votes that have been overwritten (before transitions). \\
180 %
181 \item \private $\{\aggregationproof_i\}_{i = 1}^{N}$: set of aggregation proofs.
182 \item \private $\{\censuspf_i\}_{i = 1}^N$: census membership proofs.
183 %
184 \item \private $\{\voteid_i\}_{i = 1}^{N}$: set of vote's identifiers.
185 \item \private $\{\encryptedballot_i\}_{i = 1}^{N}$: set of voters' encrypted ballots.
186 \item \private $\{\encryptedballot_i^\old\}_{i = n+1}^{N}$: set of all old encrypted ballots that will be overwritten.
187 \item \private $\{\address_i\}_{i = 1}^{N}$: list of voters' addresses.
188 \item \private $\{\weight_i\}_{i = 1}^{N}$: list of voters' weights. \\
189 %
190 \item \private $\merkleInclusionPf^{\processid}$: proof that $\processid$ is stored in leaf $\texttt{0x0}$ of the state tree.
191 \item \private $\merkleInclusionPf^{\censusorigin}$: proof that $\censusorigin$ is stored in leaf $\texttt{0x1}$ of the tree.
192 \item \private $\merkleInclusionPf^{\ballotmode}$: proof that $\ballotmode$ is stored in leaf $\texttt{0x2}$ of the state tree.
193 \item \private $\merkleInclusionPf^{\addaccumulator}$: proof that $\addaccumulator^\old$ is stored in leaf $\texttt{0x3}$ of the state tree.
194 \item \private $\merkleInclusionPf^{\substractaccumulator}$: proof that $\substractaccumulator^\old$ is stored in leaf $\texttt{0x4}$ of the state tree.\\
195 %
196 \item \private $\{\merkleNInclusionPf^{\encryptedballot}_i\}_{i = 1}^{n}$: proof that the leaf corresponding to a voter's $\address$ that submits a new vote is empty.
197 \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$.
198 \item \private $\{\merkleNInclusionPf^{\voteid}_i\}_{i= 1}^{N}$: non-membership proofs of vote identifiers.\\
199 %
200 \item \private $k$: random scalar.
201 \item \private $\openproof$: KZG opening proof.
202 \end{itemize}
203 \textbf{Constraints} \\ \vspace{-0.4cm}
204 \begin{itemize}
205 \item \emph{Aggregation verification.} The accumulated authentication proofs are valid. For every $i\in\{1,\dots,N\}$:
206 \begin{itemize}
207 \renewcommand{\labelitemii}{$\circ$}
208 \item Set public inputs:
209 \begin{align*}
210 \publicinputs = (\weight_i, \address_i, \encryptedballot_i, \voteid_i, \ballotmode, \epk, \processid).
211 \end{align*}
212 \item Verify proof:
213 $$\hlget{\PVerify}(\aggregationproof_i, \publicinputs) = \true.$$
214 \end{itemize}
215 \item \emph{Census membership.} The voter's address and weight are part of the census. For every $i\in\{1,\dots,N\}$:
216 $$ \hlget{\censusTree}.\hlget{\MTVerify}\left((\weight_i, \address_i), {\color{blue}{path??}}, \censuspf_i, \censusroot\right) = \true. $$
217 \item \emph{Transition verification.} Verify that the initial state and the state transition updates are applied correctly.
218 \begin{itemize}
219 \renewcommand{\labelitemii}{$\circ$}
220 \item Verify that the inputs $\processid$, $\censusorigin$, $\ballotmode$, $\epk$, $\addaccumulator^\old$, and $\substractaccumulator^\old$ correspond to the content of the first state tree leaves.
221 \begin{align*}
222 &\hlget{\stateTree}.\hlget{\MTVerify}(\processid, \texttt{0x0}, \merkleInclusionPf^{\processid}, \stateroot^\old). \\
223 &\hlget{\stateTree}.\hlget{\MTVerify}(\censusorigin, \texttt{0x1}, \merkleInclusionPf^{\censusorigin}, \stateroot^\old). \\
224 &\hlget{\stateTree}.\hlget{\MTVerify}(\ballotmode, \texttt{0x2}, \merkleInclusionPf^{\ballotmode}, \stateroot^\old). \\
225 &\hlget{\stateTree}.\hlget{\MTVerify}(\epk, \texttt{0x3}, \merkleInclusionPf^{\epk}, \stateroot^\old). \\
226 &\hlget{\stateTree}.\hlget{\MTVerify}(\addaccumulator^\old, \texttt{0x4}, \merkleInclusionPf^{\addaccumulator}, \stateroot^\old). \\
227 &\hlget{\stateTree}.\hlget{\MTVerify}(\substractaccumulator^\old, \texttt{0x5}, \merkleInclusionPf^{\substractaccumulator}, \stateroot^\old).
228 \end{align*}
229 \item Verify that none of the $\voteid_i$ for all $i\in\{1,\dots,N\}$ are initially part of the tree:
230 \[\hlget{\stateTree}.\hlget{\MTVerify}(\voteid_i, \merkleNInclusionPf^{\voteid}_i, \stateroot^\old).\]
231 \item Ensure that new votes are not part of the tree yet. For $i = 1, \dots, n$:
232 \[\hlget{\stateTree}.\hlget{\MTVerify}(\textsc{\color{blue}EMPTY}, \address_i, \merkleNInclusionPf^{\encryptedballot}_i, \stateroot^\old).\]
233 \item Ensure that overwrites do correspond to votes that were already on the tree. For $i = n+1, \dots, N$:
234 \[\hlget{\stateTree}.\hlget{\MTVerify}(\encryptedballot_i^\old, \address_i, \merkleInclusionPf^{\encryptedballot}_i, \stateroot^\old).\]
235 \item State transitions.
236 \begin{itemize}
237 \item For every new vote $i = 1, \dots, n$:
238 \begin{enumerate}
239 \item Add encrypted ballot to the tree: $\hlget{\stateTree}.\hlget{\MTInsert}(\encryptedballot_i, \address_i)$.
240 \item Add vote identifier: $\hlget{\stateTree}.\hlget{\MTInsert}(\voteid)$.
241 \item Increase votes counter: $\numvotes = \numvotes + 1$.
242 \item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot_i$.
243 \end{enumerate}
244 \item For every overwritten vote $i = n+1, \dots, N$:
245 \begin{enumerate}
246 \item Overwrite the encrypted ballot: $\hlget{\stateTree}.\hlget{\MTUpdate}(\encryptedballot, \address_i)$.
247 \item Add vote identifier: $\hlget{\stateTree}.\hlget{\MTInsert}(\voteid)$.
248 \item Increase votes counter: $\numvotes = \numvotes + 1$.
249 \item Increase overwrites counter: $\numoverwrites = \numoverwrites + 1$.
250 \item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot_i$.
251 \item Aggregate the old vote to the accumulator: $\substractaccumulator = \substractaccumulator + \encryptedballot^\old_i$.
252 \end{enumerate}
253 \end{itemize}
254 \item After all previous state transitions are done, recompute new tree root:
255 \[\stateroot^\new = \hlget{\stateTree}.\hlget{\MTRoot}().\]
256 \item Reencrypt all leafs of the tree (both old and new ones). {\color{blue}New are ok, but for those that are old, also verify MTProofs of membership? -- YES}.
257 \[newLeafContent = \hlget{\ReEnc}_{\epk}(\encryptedballot_i, k), \quad k = Hash(k).\]
258 \end{itemize}
259 \item \emph{Data blob}.
260 \begin{itemize}
261 \item Construct blob $B$ (as described in Section~X).
262 \item Derive polynomial $p(x)$ from $B$.
263 \item Compute evaluation point
264 \( z = \hlget{\Poseidon}(\processid, C, \openproof) \).
265 \item Compute evaluation \(y = p(z)\).
266 \item Verify
267 \(\hlget{\CVerify}(z, y, C, \openproof) = \true.\)\\
268 \end{itemize}
269 \end{itemize}
270 \vspace{-0.3cm}
271\end{spacing}
272
273{\color{blue}Mention that an advantage of splitting the circuits of the sequencers in 3 is that if the stateRoot changes in the meantime, the sequencer only needs to rerun the last circuit and not all three of them.}\\
274
275\martai{\numvotes and \numoverwrites are part of the smart contract state.}
276
277\begin{landscape}
278 \centering
279 {\setkeys{Gin}{draft=false}%shows image even in draft mode
280 \includegraphics[width=\linewidth]{\figs/circuit4.pdf}}
281 \captionof{figure}{State transition circuit. All public values are framed in green. {\color{blue}\textbf{Missing inputs:} old encryptedBallots.}}
282 \label{fig:circuit-state-transition-v2}
283\end{landscape}
284
285\subsubsection{Results circuit}
286\label{sec:vocdoni-protocol:circuits:results}
287
288The results circuit (\hlset{\resultscircuit}) proves that... .
289
290\begin{figure}[h]
291 \centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit5.pdf}}
292 \caption{Aggregation circuit. All public values are framed in green. \color{red}Circuit is not correct.}
293 \label{fig:circuit-results}
294\end{figure}