<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
  <title>shaoner.com</title>
  <link>https://shaoner.com/feed.xml</link>
  <description><![CDATA[My kids will speak Rust before they speak English]]></description>
  <image>
      <url>https://shaoner.com/img/logo.png</url>
      <title>shaoner.com logo</title>
      <link>https://shaoner.com</link>
  </image>
  <lastBuildDate>Fri, 09 Dec 2022 00:00:00 </lastBuildDate>
  <pubDate>Thu, 10 Nov 2022 00:00:00 +0000</pubDate>
  
<item>
  <title>Deutsch Oracle Problem</title>
  <description><![CDATA[<h2 id="intro">Intro</h2>
<p>Here is a small introduction about quantum computing and how it works. Knowing almost nothing about this part of computer science, I decided to dig into the rabbit hole and read more about it. I’m still very new at this (a noob) so I hope that this article makes some kind of sense. I found that the best way for me to understand a concept is to explain it completely so here it is!</p>
<p>In this article/tutorial, we’re going to discover qubits, why are they different from classical bits, how we can build quantum logic gates, and we’re going to solve a simple problem where quantum computers actually outperform classical computers. Pretty awesome.</p>
<p>Also the quantum world is actually hard to conceptualize usually. I read that our native languages are not fit for it, therefore it’s usually easier to see the math.</p>
<p>Let’s jump into the quantum realm.</p>
<p><img src="/img/quantum/antman.gif" alt="Ant-Man quantumania"/></p>
<h2 id="classical-bits-representation">Classical bits representation</h2>
<p>Classical bits are units that represent 2 states: 0 or 1. It’s the foundation of classical computing.</p>
<p>For the rest of the article, we’re going to use a vector notation:</p>
<ul>
<li>bit 0: <span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix}$</span> or using the Dirac vector notation: <span class="math inline">$\ket{0}$</span></li>
<li>and bit 1: <span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix}$</span> or: <span class="math inline">$\ket{1}$</span></li>
</ul>
<p>A good way to remember which bit is which, you can think of the vector as an array with 1 at the index of the bit.</p>
<p>Now there are 4 operations we can apply to a bit: Identity, Negation, Set 0, Set 1. We can actually use a matrix as a transformation operator.</p>
<h4 id="identity">Identity</h4>
<p><span class="math inline"><em>f</em>(<em>x</em>) = <em>x</em></span></p>
<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th><span class="math inline">$f(\ket{0}) = \ket{0}$</span></th>
<th><span class="math inline">$f(\ket{1}) = \ket{1}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 \\0 &amp; 1\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 \\0 &amp; 1\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="negation">Negation</h4>
<p><span class="math inline"><em>f</em>(<em>x</em>) = ¬<em>x</em></span></p>
<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th><span class="math inline">$f(\ket{0}) = \ket{1}$</span></th>
<th><span class="math inline">$f(\ket{1}) = \ket{0}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><span class="math inline">$\begin{pmatrix}0 &amp; 1 \\1 &amp; 0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}0 &amp; 1 \\1 &amp; 0\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="set-0">Set 0</h4>
<p><span class="math inline"><em>f</em>(<em>x</em>) = 0</span></p>
<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th><span class="math inline">$f(\ket{0}) = \ket{0}$</span></th>
<th><span class="math inline">$f(\ket{1}) = \ket{0}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><span class="math inline">$\begin{pmatrix}1 &amp; 1 \\0 &amp; 0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 1 \\0 &amp; 0\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="set-1">Set 1</h4>
<p><span class="math inline"><em>f</em>(<em>x</em>) = 1</span></p>
<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th><span class="math inline">$f(\ket{0}) = \ket{1}$</span></th>
<th><span class="math inline">$f(\ket{1}) = \ket{1}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><span class="math inline">$\begin{pmatrix}0 &amp; 0 \\1 &amp; 1\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}0 &amp; 0 \\1 &amp; 1\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h3 id="multiple-bits">Multiple bits</h3>
<p>To represent multiple classical bits and keep consistency with each of the operations, we can use the tensor product of multiple vectors, each vector representing a single bit.</p>
<p>For 2 bits, it looks like: <span class="math inline">$\begin{pmatrix}a\\b\end{pmatrix} \otimes \begin{pmatrix}c\\d\end{pmatrix} = \begin{pmatrix}ac\\ad\\bc\\bd\end{pmatrix}$</span></p>
<p>For 3 bits: <span class="math inline">$\begin{pmatrix}a\\b\end{pmatrix} \otimes \begin{pmatrix}c\\d\end{pmatrix} \otimes \begin{pmatrix}e\\f\end{pmatrix} = \begin{pmatrix}ace\\acf\\ade\\adf\\bce\\bcf\\bde\\bdf\end{pmatrix}$</span></p>
<p>Example:</p>
<table>
<colgroup>
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
</colgroup>
<tbody>
<tr class="odd">
<td><span class="math inline">$\ket{00} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\ket{01} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\ket{10} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\ket{11} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\0\\0\\1\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<p>And again we see that <span class="math inline">$\ket{N}$</span> is a vector with a 1 at the Nth index.</p>
<h3 id="multi-bits-gates">Multi bits gates</h3>
<h4 id="cnot-gate">CNOT gate</h4>
<p>One common gate is called the CNOT gate. It takes 2 bits in input and “returns” 2 bits. The 1st bit is called the control bit and the 2nd bit is called the target bit. It behaves like this:</p>
<ul>
<li>If the control bit is 0, the target bit is left unchanged.</li>
<li>If the control bit is 1, the target bit is flipped.</li>
</ul>
<p>It can be represented as a matrix as well: <span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix}$</span></p>
<table>
<colgroup>
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
</colgroup>
<tbody>
<tr class="odd">
<td><span class="math inline">$C(\ket{00}) = \ket{00}$</span></td>
<td><span class="math inline">$C(\ket{01}) = \ket{01}$</span></td>
<td><span class="math inline">$C(\ket{10}) = \ket{11}$</span></td>
<td><span class="math inline">$C(\ket{11}) = \ket{10}$</span></td>
</tr>
<tr class="even">
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}0\\1\\0\\0\end{pmatrix} = \begin{pmatrix}0\\1\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\0\\1\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}0\\0\\0\\1\end{pmatrix} = \begin{pmatrix}0\\0\\1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<p>We can see the 2 parts in the matrix with <span class="math inline">$\begin{pmatrix}1 &amp; 0\\0 &amp; 1\end{pmatrix}$</span> reflecting the identity part (leaving the target unchanged) and the <span class="math inline">$\begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix}$</span> reflecting the flipping part.</p>
<h2 id="qubits">Qubits</h2>
<p>Qubits are physical objets (ions, photons, nucleus, electrons, …) that can be used as units of quantum measurement. The difference with classical bits is that qubits can represent states that are neither 0 or 1, but a proportion of both 0 and 1 at the same time.</p>
<p>That weird state is called superposition.</p>
<p>And because we’re in the quantum world, it’s only when we measure a qubit that it collapses with a probability to finally be 0 or 1.</p>
<p>Now the way I understand it is with an example, if you have 3 bits, you can make 8 combinations: <code>000</code>, <code>001</code>, <code>010</code>, <code>011</code>, <code>100</code>, <code>101</code>, <code>110</code>, and <code>111</code>. However you’re only getting 1 information out of 3 bits.</p>
<p>In superposition, you can imagine that your qubits are in all those states at the same times, so 3 qubits gets 8 pieces of information, even though you can’t measure that information yet.</p>
<p>Mesuring is what makes all your qubits to collapse to a state, therefore coming back to being classical bits.</p>
<p>The power of qubits is to use superposition so that N Qubits gives you <span class="math inline">2<sup><em>N</em></sup></span> bit of information only until you measure your results. Exponential information!</p>
<p><img src="/img/quantum/unlimited_powers.gif" alt="unlimited powers gif"></p>
<p>When I started reading about Qubits, I couldn’t make sense of it but again, math is the language we need to understand it better.</p>
<h3 id="representation-of-qubits">Representation of Qubits</h3>
<p>Qubits can also be written as vectors with each element of the vector a probability, and so a qubit is represented as: <span class="math inline">$\begin{pmatrix}a\\b\end{pmatrix}$</span> with <span class="math inline">||<em>a</em>||<sup>2</sup> + ||<em>b</em>||<sup>2</sup> = 1</span> with <span class="math inline"><em>a</em>, <em>b</em> ∈ ℂ</span> and so it has the probability <span class="math inline">||<em>a</em>||<sup>2</sup></span> to be 0 and <span class="math inline">||<em>b</em>||<sup>2</sup></span> to be 1.</p>
<p>so it can also look like <span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span> or even <span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span> which works because <span class="math inline">$\left|\left|\frac{1}{\sqrt{2}}\right|\right|^2 + \left|\left|-\frac{1}{\sqrt{2}}\right|\right|^2 = 1$</span></p>
<p>Note that in both these examples, the Qubit has 50% chance to collapse to either 0 or 1! When it’s 50/50, it’s called equal superposition.</p>
<p>Our classical bits vectors are just specific qubits, with:</p>
<p><span class="math inline">$\ket{0} = \begin{pmatrix}1\\0\end{pmatrix}$</span> having the probability 100% of being a 0 and <span class="math inline">$\ket{1} = \begin{pmatrix}0\\1\end{pmatrix}$</span> having the probability 100% of being a 1</p>
<p>We now have some tools to manipulate our qubits, we can apply a CNOT to a pair of qubits or simply flip a qubit state, whatever that state is and even if we don’t know it, for example:</p>
<p><span class="math inline">$\begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix} \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span></p>
<h3 id="operations-on-qubits">Operations on qubits</h3>
<p>Note that all operations on qubits have to be <a href="https://en.wikipedia.org/wiki/Reversible_computing">reversible</a>. An operation is said to be reversible if knowing the output and the function, you’re able to know or reconstruct the input.</p>
<p>Identity and Negation are reversible, however Set-0 and Set-1 are not. Indeed, both functions are constant and don’t use the input at all, so knowing the result it’s impossible to know what the input was.</p>
<p>For constant operations, like Set-0 and Set-1, we can instead configure a gate using multiple Qubits. We’ll see how this works next in the article.</p>
<h2 id="bloch-sphere">Bloch sphere</h2>
<p>So far, we’ve been using matrixes but it also helps to visualize common Qubits on a sphere. It is called the <a href="https://en.wikipedia.org/wiki/Bloch_sphere">Bloch sphere</a></p>
<p>To make it easy, we can consider Qubits with <span class="math inline"><em>a</em>, <em>b</em> ∈ ℝ</span> and so in that case, we only need a circle (which simplifies the drawings).</p>
<p>We can use the probability to be 0 as the x coordinate and the probability to be 1 as the y coordinate. It looks like this:</p>
<div style="position: relative; width: 300px; height: 300px">
<div style="position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\-1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<p><svg width="200" height="200" style="position: absolute; top: 50px; left: 50px"> <circle cx="100" cy="100" r="80" fill="#fff" stroke="#000" stroke-width="2"/> <line x1="0" y1="100" x2="200" y2="100" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="100" y1="-100" x2="100" y2="200" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="25" y1="25" x2="175" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="175" y1="25" x2="25" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> </svg></p>
</div>
<p>We can actually navigate through the circle, for example bit flips:</p>
<div style="position: relative; width: 300px; height: 300px">
<div style="position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\-1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<p><svg width="200" height="200" style="position: absolute; top: 50px; left: 50px"> <defs> <marker id="startarrow" markerWidth="5" markerHeight="7" refX="5" refY="3.5" orient="auto"> <polygon points="5 0, 5 7, 0 3.5" fill="#f00" /> </marker> <marker id="endarrow" markerWidth="5" markerHeight="7" refX="0" refY="3.5" orient="auto" markerUnits="strokeWidth"> <polygon points="0 0, 5 3.5, 0 7" fill="#f00" /> </marker> </defs> <circle cx="100" cy="100" r="80" fill="#fff" stroke="#000" stroke-width="2"/> <line x1="0" y1="100" x2="200" y2="100" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="100" y1="-100" x2="100" y2="200" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="25" y1="25" x2="175" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="175" y1="25" x2="25" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="190" y1="90" x2="110" y2="10" stroke="#f00" stroke-width="2" marker-end="url(#endarrow)" marker-start="url(#startarrow)"/> <line x1="45" y1="45" x2="155" y2="155" stroke="#f00" stroke-width="2" marker-end="url(#endarrow)" marker-start="url(#startarrow)"/> <line x1="10" y1="110" x2="90" y2="190" stroke="#f00" stroke-width="2" marker-end="url(#endarrow)" marker-start="url(#startarrow)"/> <circle cx="170" cy="30" r="10" stroke="#f00" stroke-width="2" marker-mid="url(#startarrow)" fill="rgba(0,0,0,0)"/> <circle cx="30" cy="170" r="10" stroke="#f00" stroke-width="2" marker-mid="url(#startarrow)" fill="rgba(0,0,0,0)"/> </svg></p>
</div>
<h2 id="hadamard-gate">Hadamard gate</h2>
<p>Another gate we need before we dig into the problem we want to solve is the <a href="https://en.wikipedia.org/wiki/Hadamard_transform#Quantum_computing_applications">Hadamard gate</a></p>
<p>The hadamard gate takes a Qubit in equal superposition and turns it to a <span class="math inline">$\ket{0}$</span> or a <span class="math inline">$\ket{1}$</span>. It’s matrix representation looks like: <span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}$</span></p>
<table>
<colgroup>
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
<col style="width: 25%" />
</colgroup>
<tbody>
<tr class="odd">
<td><span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix} \begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix} \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix} \begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\-1\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix} \begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}-1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<p>and on our Bloch circle:</p>
<div style="position: relative; width: 300px; height: 300px">
<div style="position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}0\\-1\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 25px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 25px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<div style="position: absolute; top: 225px; left: 225px; width: 50px; text-align: center">
<span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span>
</div>
<p><svg width="200" height="200" style="position: absolute; top: 50px; left: 50px"> <defs> <marker id="startarrow2" markerWidth="5" markerHeight="7" refX="5" refY="3.5" orient="auto"> <polygon points="5 0, 5 7, 0 3.5" fill="#00f" /> </marker> <marker id="endarrow2" markerWidth="5" markerHeight="7" refX="0" refY="3.5" orient="auto" markerUnits="strokeWidth"> <polygon points="0 0, 5 3.5, 0 7" fill="#00f" /> </marker> </defs> <circle cx="100" cy="100" r="80" fill="#fff" stroke="#000" stroke-width="2"/> <line x1="0" y1="100" x2="200" y2="100" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="100" y1="-100" x2="100" y2="200" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="25" y1="25" x2="175" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="175" y1="25" x2="25" y2="175" stroke="#aaa" stroke-dasharray="2" stroke-width="1"/> <line x1="110" y1="25" x2="155" y2="155" stroke="#00f" stroke-width="2" marker-end="url(#endarrow2)" marker-start="url(#startarrow2)"/> <line x1="45" y1="45" x2="90" y2="175" stroke="#00f" stroke-width="2" marker-end="url(#endarrow2)" marker-start="url(#startarrow2)"/> <line x1="25" y1="105" x2="45" y2="155" stroke="#00f" stroke-width="2" marker-end="url(#endarrow2)" marker-start="url(#startarrow2)"/> <line x1="160" y1="55" x2="175" y2="100" stroke="#00f" stroke-width="2" marker-end="url(#endarrow2)" marker-start="url(#startarrow2)"/> </svg></p>
</div>
<h2 id="the-deutsch-oracle-problem">The Deutsch oracle problem</h2>
<p>We’re going to write a system with multiple gates so that we can actually find a result to a question faster than on a classical computer.</p>
<p>The question is: Given an unknown function, is this function constant (Set-0 / Set-1) or is this function variable (Identity / Negation)?</p>
<h3 id="classical-approach">Classical approach</h3>
<p>On a classical computer it could look like this:</p>
<div style="position: relative; width: 400px; height: 200px;">
<div style="position: absolute; left: 60px; top: 60px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 60px;">
<span class="math inline">$f(\ket{x})$</span>
</div>
<p><svg width="400" height="200"> <line x1="50" y1="100" x2="150" y2="100" stroke="#00f" stroke-width="2"/> <path d="M150 50 L200 50 M200 50 L200 150 M200 150 L150 150 M150 150 L150 50" stroke="#00f" stroke-width="2" fill="none" /> <text x="160" y="100" fill="#00f">?</text> <line x1="200" y1="100" x2="300" y2="100" stroke="#00f" stroke-width="2"/> </svg></p>
</div>
<p>And in order to know what the function is, we can use 2 queries:</p>
<table>
<thead>
<tr class="header">
<th>f(0)</th>
<th>f(1)</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>0</td>
<td>0</td>
<td>f = Set-0</td>
</tr>
<tr class="even">
<td>0</td>
<td>1</td>
<td>f = Identity</td>
</tr>
<tr class="odd">
<td>1</td>
<td>0</td>
<td>f = Negation</td>
</tr>
<tr class="even">
<td>1</td>
<td>1</td>
<td>f = Set-1</td>
</tr>
</tbody>
</table>
<p>Knowing if the function is constant or variable takes also 2 queries.</p>
<h3 id="quantum-approach">Quantum approach</h3>
<p>On a quantum computer, remember that our function has to be reversible so we need a way for Set-0 and Set-1 to be reversible as well first.</p>
<p>The way to do it is to actually rewire our system to use multiple qubits for all the operations: Identity, Negation, Set-0, Set-1.</p>
<p>In the real world, note that qubits are resources that are changed, so in this case, we need 2 qubits:</p>
<ul>
<li>the input qubit that is left unchanged (in blue)</li>
<li>the output qubit where the result is written (in red)</li>
</ul>
<div style="position: relative; width: 400px; height: 200px;">
<div style="position: absolute; left: 60px; top: 40px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 90px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 40px;">
<span class="math inline">$f(\ket{x})$</span>
</div>
<div style="position: absolute; left: 260px; top: 90px;">
<span class="math inline">$\ket{x}$</span>
</div>
<p><svg width="400" height="200"> <line x1="50" y1="75" x2="150" y2="75" stroke="#f00" stroke-width="2"/> <line x1="50" y1="125" x2="150" y2="125" stroke="#00f" stroke-width="2"/> <path d="M150 50 L200 50 M200 50 L200 150 M200 150 L150 150 M150 150 L150 50" stroke="#00f" stroke-width="2" fill="none" /> <text x="160" y="100" fill="#00f">?</text> <line x1="200" y1="75" x2="300" y2="75" stroke="#f00" stroke-width="2"/> <line x1="200" y1="125" x2="300" y2="125" stroke="#00f" stroke-width="2"/> </svg></p>
</div>
<p>It’s intuitive to understand that having the input qubit left unchanged gives us an information for Set-0 and Set-1 that allows us to know what the function is.</p>
<p>Let’s now write all our operations using 2 qubits with the input the MSB (most significant bit) and the output the LSB (least significant bit).</p>
<p>It is normal to be confused by this part at first but hopefully it gets clearer afterwards.</p>
<p>To recap: we want to write all our operations as reversible operations (because this is how quantum computers work) and to do this, we pass an additional “output” qubit as input to our function. This qubit is initialized to <span class="math inline">$\ket{0}$</span> and is used to write the result of the operation, while the input qubit on the other hand is left unchanged. We’ll see that using 2 qubits allows us to write reversible operations.</p>
<h4 id="set-0-1">Set-0</h4>
<div style="position: relative; width: 400px; height: 160px;">
<div style="position: absolute; left: 60px; top: 20px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 20px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 260px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<p><svg width="400" height="160"> <line x1="50" y1="50" x2="300" y2="50" stroke="#f00" stroke-width="2"/> <line x1="50" y1="100" x2="300" y2="100" stroke="#00f" stroke-width="2"/> <path d="M150 25 L200 25 M200 25 L200 125 M200 125 L150 125 M150 125 L150 25" stroke="#00f" stroke-width="2" fill="none" /> </svg></p>
</div>
<p>In this case, we have nothing to do, the output qubit is already set to 0, and we want to leave the input qubit unchanged, so we can just leave our qubits as they are.</p>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 40%" />
<col style="width: 40%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th><span class="math inline">$\ket{00}$</span></th>
<th><span class="math inline">$\ket{10}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td><span class="math inline">$f(\ket{00}) = \ket{00}$</span></td>
<td><span class="math inline">$f(\ket{10}) = \ket{10}$</span></td>
</tr>
<tr class="even">
<td>identity</td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{pmatrix} \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{pmatrix} \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="set-1-1">Set-1</h4>
<div style="position: relative; width: 400px; height: 160px;">
<div style="position: absolute; left: 60px; top: 20px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 20px;">
<span class="math inline">$\ket{1}$</span>
</div>
<div style="position: absolute; left: 260px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<p><svg width="400" height="160"> <rect x="160" y="35" width="30" height="30" fill="#f00"/> <text x="170" y="55" fill="#fff">X</text> <line x1="50" y1="50" x2="160" y2="50" stroke="#f00" stroke-width="2"/> <line x1="50" y1="100" x2="300" y2="100" stroke="#00f" stroke-width="2"/> <line x1="190" y1="50" x2="300" y2="50" stroke="#f00" stroke-width="2"/> <path d="M150 25 L200 25 M200 25 L200 125 M200 125 L150 125 M150 125 L150 25" stroke="#00f" stroke-width="2" fill="none" /> </svg></p>
</div>
<p>This time we have to flip our “output” qubit so it becomes 1, we just added a negation gate.</p>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 40%" />
<col style="width: 40%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th><span class="math inline">$\ket{00}$</span></th>
<th><span class="math inline">$\ket{10}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td><span class="math inline">$f(\ket{00}) = \ket{01}$</span></td>
<td><span class="math inline">$f(\ket{10}) = \ket{11}$</span></td>
</tr>
<tr class="even">
<td>identity</td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{pmatrix} \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{pmatrix} \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
</tr>
<tr class="odd">
<td>X</td>
<td><span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\0\\0\\1\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="identity-1">Identity</h4>
<div style="position: relative; width: 400px; height: 160px;">
<div style="position: absolute; left: 60px; top: 20px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 20px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<p><svg width="400" height="160"> <line x1="50" y1="50" x2="300" y2="50" stroke="#f00" stroke-width="2"/> <line x1="50" y1="100" x2="300" y2="100" stroke="#00f" stroke-width="2"/> <path d="M150 25 L200 25 M200 25 L200 125 M200 125 L150 125 M150 125 L150 25" stroke="#00f" stroke-width="2" fill="none" /> <circle cx="175" cy="50" r="8" fill="none" stroke-width="2" stroke="#f00"/> <line x1="175" y1="42" x2="175" y2="100" stroke="#f00" stroke-width="2"/> <circle cx="175" cy="100" r="3" fill="#f00"/> </svg></p>
</div>
<p>In this case we have to use a CNOT gate, with the input as the control bit.</p>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 40%" />
<col style="width: 40%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th><span class="math inline">$\ket{00}$</span></th>
<th><span class="math inline">$\ket{10}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td><span class="math inline">$f(\ket{00}) = \ket{00}$</span></td>
<td><span class="math inline">$f(\ket{10}) = \ket{11}$</span></td>
</tr>
<tr class="even">
<td>CNOT</td>
<td><span class="math inline">$C\left(\begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}\right) = \begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$C\left(\begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}\right) = \begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\0\\1\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<h4 id="negation-1">Negation</h4>
<div style="position: relative; width: 400px; height: 160px;">
<div style="position: absolute; left: 60px; top: 20px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<div style="position: absolute; left: 260px; top: 20px;">
<span class="math inline">$\neg{\ket{x}}$</span>
</div>
<div style="position: absolute; left: 260px; top: 70px;">
<span class="math inline">$\ket{x}$</span>
</div>
<p><svg width="400" height="160"> <line x1="50" y1="50" x2="300" y2="50" stroke="#f00" stroke-width="2"/> <line x1="50" y1="100" x2="300" y2="100" stroke="#00f" stroke-width="2"/> <path d="M150 25 L200 25 M200 25 L200 125 M200 125 L150 125 M150 125 L150 25" stroke="#00f" stroke-width="2" fill="none" /> <circle cx="165" cy="50" r="8" fill="none" stroke-width="2" stroke="#f00"/> <line x1="165" y1="42" x2="165" y2="100" stroke="#f00" stroke-width="2"/> <circle cx="165" cy="100" r="3" fill="#f00"/> <rect x="180" y="42" width="15" height="15" fill="#f00"/> <text x="184" y="54" font-size="0.8em" fill="#fff">X</text> </svg></p>
</div>
<p>And negation is pretty much same as identity with the CNOT gate, where we just flip the result by adding a negation gate.</p>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 40%" />
<col style="width: 40%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th><span class="math inline">$\ket{00}$</span></th>
<th><span class="math inline">$\ket{10}$</span></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td><span class="math inline">$f(\ket{00}) = \ket{01}$</span></td>
<td><span class="math inline">$f(\ket{10}) = \ket{10}$</span></td>
</tr>
<tr class="even">
<td>CNOT</td>
<td><span class="math inline">$C\left(\begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}\right) = \begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\\0\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$C\left(\begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix}\right) = \begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix} \begin{pmatrix}0\\0\\1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\0\\1\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
</tr>
<tr class="odd">
<td>X</td>
<td><span class="math inline">$\begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix}\begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix} \otimes \begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\\0\\0\end{pmatrix}$</span></td>
<td><span class="math inline">$\begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}0 &amp; 1\\1 &amp; 0\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix} \otimes \begin{pmatrix}1\\0\end{pmatrix} = \begin{pmatrix}0\\0\\1\\0\end{pmatrix}$</span></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr class="odd">
<td><span class="math inline">$f(\ket{00}) = \ket{01}$</span></td>
</tr>
<tr class="even">
<td><span class="math inline">$f(\ket{10}) = \ket{10}$</span></td>
</tr>
</tbody>
</table>
<h3 id="resolution">Resolution</h3>
<p>Now that we defined all the operations using 2 qubits, how can we know if a function is constant or variable using a single query?</p>
<p>The solution is to:</p>
<ol type="1">
<li>initialize 2 qubits to <span class="math inline">$\ket{0}$</span></li>
<li>apply a bit flip, so both are now <span class="math inline">$\ket{1}$</span></li>
<li>apply the Hadamard gate, so both are now in equal superposition: <span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span></li>
<li>apply the function f</li>
<li>apply the Hadamard gate again</li>
<li>measure the result:</li>
</ol>
<ul>
<li>if the function is constant, it should be <span class="math inline">$\ket{11}$</span></li>
<li>if the function is variable, it should be <span class="math inline">$\ket{01}$</span></li>
</ul>
<div style="position: relative; width: 400px; height: 200px;">
<div style="position: absolute; left: 60px; top: 40px;">
<span class="math inline">$\ket{0}$</span>
</div>
<div style="position: absolute; left: 60px; top: 90px;">
<span class="math inline">$\ket{0}$</span>
</div>
<p><svg width="400" height="200"> <line x1="50" y1="75" x2="150" y2="75" stroke="#f00" stroke-width="2"/> <line x1="50" y1="125" x2="150" y2="125" stroke="#00f" stroke-width="2"/> <path d="M150 50 L200 50 M200 50 L200 150 M200 150 L150 150 M150 150 L150 50" stroke="#00f" stroke-width="2" fill="none" /> <text x="160" y="100" fill="#00f">?</text> <line x1="200" y1="75" x2="300" y2="75" stroke="#f00" stroke-width="2"/> <line x1="200" y1="125" x2="300" y2="125" stroke="#00f" stroke-width="2"/> <rect x="90" y="68" width="15" height="15" fill="#f00"></rect> <rect x="120" y="68" width="15" height="15" fill="#0a0"></rect> <rect x="90" y="118" width="15" height="15" fill="#f00"></rect> <rect x="120" y="118" width="15" height="15" fill="#0a0"></rect> <rect x="215" y="68" width="15" height="15" fill="#0a0"></rect> <rect x="215" y="118" width="15" height="15" fill="#0a0"></rect> <rect x="250" y="68" width="45" height="15" fill="#000"></rect> <rect x="250" y="118" width="45" height="15" fill="#000"></rect> <text x="92" y="80" fill="#fff">X</text> <text x="122" y="80" fill="#fff">H</text> <text x="92" y="130" fill="#fff">X</text> <text x="122" y="130" fill="#fff">H</text> <text x="217" y="80" fill="#fff">H</text> <text x="217" y="130" fill="#fff">H</text> <text x="252" y="80" fill="#fff" font-size="0.7em">Measure</text> <text x="252" y="130" fill="#fff" font-size="0.7em">Measure</text> </svg></p>
</div>
<p>After the first 3 steps, both qubits are always in state: <span class="math inline">$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span></p>
<p>Let see what happens from each function:</p>
<h4 id="set-0-2">Set-0</h4>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 55%" />
<col style="width: 24%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th></th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>4. Apply Set-0 gate</td>
<td><span class="math inline">$f(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span></td>
<td>Input and Output are left unchanged</td>
</tr>
<tr class="even">
<td>5. Apply H gate to each qubits</td>
<td><span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span> <br> <span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>which gives us: <span class="math inline">$\ket{11}$</span></p>
<table style="width: 100%">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>
Input Qubit
</th>
<th>
Output Qubit
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
  &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
    &lt;defs&gt;
      &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
        &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
      &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
       &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
    &lt;/defs&gt;
    &lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
    &lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;110&quot; y1=&quot;25&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;</code></pre>
</td>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
 &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
   &lt;defs&gt;
     &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
       &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
     &lt;/marker&gt;
     &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
   &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
  &lt;/marker&gt;
&lt;/defs&gt;
&lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
&lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;110&quot; y1=&quot;25&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;</code></pre>
<p></svg></p>
</div>
</td>
</tr>
</tbody>
</table>
<h4 id="set-1-2">Set-1</h4>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 55%" />
<col style="width: 24%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th></th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>4. Apply Set-1 gate</td>
<td><span class="math inline">$f(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span></td>
<td>Input is left unchanged <br> Output is flipped</td>
</tr>
<tr class="even">
<td>5. Apply H to each qubits</td>
<td><span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span> <br> <span class="math inline">$H(\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\-1\end{pmatrix}$</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>which gives us: <span class="math inline">$\ket{11}$</span></p>
<blockquote>
<p>Note that <span class="math inline">$\begin{pmatrix}0\\-1\end{pmatrix} = \ket{1}$</span> simply because there is a probability <span class="math inline">|| − 1||<sup>2</sup></span> to be 1.</p>
</blockquote>
<table style="width: 100%">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>
Input Qubit
</th>
<th>
Output Qubit
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
  &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
    &lt;defs&gt;
      &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
        &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
      &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
       &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
    &lt;/defs&gt;
    &lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
    &lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;110&quot; y1=&quot;25&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;</code></pre>
</td>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
 &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
   &lt;defs&gt;
     &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
       &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
     &lt;/marker&gt;
     &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
   &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
  &lt;/marker&gt;
&lt;/defs&gt;
&lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
&lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;155&quot; y1=&quot;155&quot; x2=&quot;45&quot; y2=&quot;45&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#endarrow2)&quot;/&gt;
&lt;line x1=&quot;45&quot; y1=&quot;45&quot; x2=&quot;100&quot; y2=&quot;175&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#endarrow2)&quot;/&gt;</code></pre>
<p></svg></p>
</div>
</td>
</tr>
</tbody>
</table>
<h4 id="identity-2">Identity</h4>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 55%" />
<col style="width: 24%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th></th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>4. Apply Identity gate</td>
<td><span class="math inline">$f(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span></td>
<td><span class="math inline">$C(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix}\begin{pmatrix}\frac{1}{2}\\-\frac{1}{2}\\-\frac{1}{2}\\\frac{1}{2}\end{pmatrix} = \frac{1}{2}\begin{pmatrix}1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\\0 &amp; 0 &amp; 1 &amp; 0\end{pmatrix}\begin{pmatrix}1\\-1\\-1\\1\end{pmatrix}$</span> <br><span class="math inline">$= \frac{1}{2}\begin{pmatrix}1\\-1\\1\\-1\end{pmatrix} = \begin{pmatrix}\frac{1}{2}\\-\frac{1}{2}\\\frac{1}{2}\\-\frac{1}{2}\end{pmatrix} = \begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$</span></td>
</tr>
<tr class="even">
<td>5. Apply H to each qubits</td>
<td><span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span> <br> <span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\1\end{pmatrix}$</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>which gives us: <span class="math inline">$\ket{01}$</span></p>
<table style="width: 100%">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>
Input Qubit
</th>
<th>
Output Qubit
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
  &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
    &lt;defs&gt;
      &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
        &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
      &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
       &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
    &lt;/defs&gt;
    &lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
    &lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;155&quot; y1=&quot;50&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;
    &lt;line x1=&quot;155&quot; y1=&quot;50&quot; x2=&quot;175&quot; y2=&quot;100&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#endarrow2)&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;</code></pre>
</td>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
   &lt;defs&gt;
     &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
       &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
     &lt;/marker&gt;
     &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
   &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
  &lt;/marker&gt;
&lt;/defs&gt;
&lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
&lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;110&quot; y1=&quot;25&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;</code></pre>
<p></svg></p>
</div>
</td>
</tr>
</tbody>
</table>
<h4 id="negation-2">Negation</h4>
<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 55%" />
<col style="width: 24%" />
</colgroup>
<thead>
<tr class="header">
<th>Step</th>
<th></th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>4. Apply Identity gate</td>
<td><span class="math inline">$f(\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} \otimes \begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$</span></td>
<td>Same as previously but with an additional bit flip on the output</td>
</tr>
<tr class="even">
<td>5. Apply H to each qubits</td>
<td><span class="math inline">$H(\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}1\\0\end{pmatrix}$</span> <br> <span class="math inline">$H(\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}) = \begin{pmatrix}\frac{1}{\sqrt{2}} &amp; \frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}} &amp; -\frac{1}{\sqrt{2}}\end{pmatrix}\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}0\\-1\end{pmatrix}$</span></td>
<td></td>
</tr>
</tbody>
</table>
<p>which gives us: <span class="math inline">$\ket{01}$</span></p>
<table style="width: 100%">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>
Input Qubit
</th>
<th>
Output Qubit
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt; 
  &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
    &lt;defs&gt;
      &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
        &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
      &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
       &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
      &lt;/marker&gt;
    &lt;/defs&gt;
    &lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
    &lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
    &lt;line x1=&quot;155&quot; y1=&quot;50&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;
    &lt;line x1=&quot;155&quot; y1=&quot;50&quot; x2=&quot;175&quot; y2=&quot;100&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#endarrow2)&quot;/&gt;
  &lt;/svg&gt;
&lt;/div&gt;</code></pre>
</td>
<td style="text-align: center">
<div style="position: relative; width: 300px; height: 300px; display: inline-block">
<pre><code>  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: 0; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 0; left: calc(25px + 200px / 2); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(50% - 25px); left: calc(100% - 50px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}1\\0\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: calc(100% - 50px); left: calc(50% - 25px); width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}0\\-1\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 25px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
  &lt;div style=&quot;position: absolute; top: 225px; left: 25px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}-\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;div style=&quot;position: absolute; top: 225px; left: 225px; width: 50px; text-align: center&quot;&gt;$\begin{pmatrix}\frac{1}{\sqrt{2}}\\-\frac{1}{\sqrt{2}}\end{pmatrix}$&lt;/div&gt;
 &lt;svg width=&quot;200&quot; height=&quot;200&quot; style=&quot;position: absolute; top: 50px; left: 50px&quot;&gt;
   &lt;defs&gt;
     &lt;marker id=&quot;startarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;5&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot;&gt;
       &lt;polygon points=&quot;5 0, 5 7, 0 3.5&quot; fill=&quot;#00f&quot; /&gt;
     &lt;/marker&gt;
     &lt;marker id=&quot;endarrow2&quot; markerWidth=&quot;5&quot; markerHeight=&quot;7&quot; refX=&quot;0&quot; refY=&quot;3.5&quot; orient=&quot;auto&quot; markerUnits=&quot;strokeWidth&quot;&gt;
   &lt;polygon points=&quot;0 0, 5 3.5, 0 7&quot; fill=&quot;#00f&quot; /&gt;
  &lt;/marker&gt;
&lt;/defs&gt;
&lt;circle cx=&quot;100&quot; cy=&quot;100&quot; r=&quot;80&quot; fill=&quot;#fff&quot; stroke=&quot;#000&quot; stroke-width=&quot;2&quot;/&gt;
&lt;line x1=&quot;0&quot; y1=&quot;100&quot; x2=&quot;200&quot; y2=&quot;100&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;100&quot; y1=&quot;-100&quot; x2=&quot;100&quot; y2=&quot;200&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;25&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;25&quot; y2=&quot;175&quot; stroke=&quot;#aaa&quot; stroke-dasharray=&quot;2&quot; stroke-width=&quot;1&quot;/&gt;
&lt;line x1=&quot;45&quot; y1=&quot;45&quot; x2=&quot;155&quot; y2=&quot;155&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-start=&quot;url(#startarrow2)&quot;/&gt;
&lt;line x1=&quot;45&quot; y1=&quot;45&quot; x2=&quot;100&quot; y2=&quot;175&quot; stroke=&quot;#00f&quot; stroke-width=&quot;2&quot; marker-end=&quot;url(#endarrow2)&quot;/&gt;</code></pre>
<p></svg></p>
</div>
</td>
</tr>
</tbody>
</table>
<h3 id="recap">Recap</h3>
<ul>
<li>Using our system with Set-0 and Set-1 operations, we end up with <span class="math inline">$\ket{11}$</span> as the output</li>
<li>Using our system with Identity and Negation operations, we end up with <span class="math inline">$\ket{01}$</span> as the output</li>
</ul>
<p>For each those cases, it took us 1 single query to the operation as opposed to 2 queries on a classic computer.</p>
<p><img src="/img/quantum/dwight.gif" alt="Dwight victory gif"/></p>
<p>The <a href="https://en.wikipedia.org/wiki/Deutsch%E2%80%93Jozsa_algorithm">Deutsch–Jozsa algorithm</a> generalizes the idea using functions that take N-bit input and generate single 0 or 1 bit, i.e. <span class="math inline"><em>f</em>{0, 1}<sup><em>n</em></sup> → {0, 1}</span></p>
]]></description>
  <link>https://shaoner.com/2022/11/10/deutsch-oracle-problem.html</link>
  <guid>/2022/11/10/deutsch-oracle-problem.html</guid>
  <pubDate>Thu, 10 Nov 2022 00:00:00 +0000</pubDate>
</item>


<item>
  <title>The Halting problem</title>
  <description><![CDATA[<p>An interesting <a href="https://en.wikipedia.org/wiki/Decision_problem">decision problem</a> I’ve read about is called the Halting problem.</p>
<h2 id="decision-problem">Decision problem</h2>
<p>In short, a decision problem is basically a question regarding some input(s) that is answered by yes or no (true or false).</p>
<p>For example: Given 3 integers <code>x</code>, <code>y</code> and <code>z</code>, is the addition equals to 100?</p>
<p>This decision problem has an effective solution to find the correct answer, no matter the input, it is <strong>decidable</strong>.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> eq_100(x: <span class="bu">int</span>, y: <span class="bu">int</span>, z: <span class="bu">int</span>) <span class="op">-&gt;</span> <span class="bu">bool</span>:</span>
<span id="cb1-2"><a href="#cb1-2"></a>    <span class="cf">return</span> (x <span class="op">+</span> y <span class="op">+</span> z) <span class="op">==</span> <span class="dv">100</span></span></code></pre></div>
<p>Ok, that’s not very impressive. As opposed to decidable, some decision problems are said to be <strong>non decidable</strong> i.e. there is no existing method to guarantee a correct answer for any inputs. This is the case for the halting problem.</p>
<h2 id="the-halting-problem">The halting problem</h2>
<p>The question is: Is it possible for a program to determine if another program given an input will eventually halt (or loop forever).</p>
<p>Note that it’s a decision problem because we have a question regarding some inputs (a program <code>P</code> and an input <code>I</code>) that is answered by yes or no (<code>P</code> will halt or not).</p>
<p>For this problem to be decidable, we need to find a program that finds the correct answer no matter the program it is given. I can tell you the answer right now, it is not decidable, there is no such program.</p>
<p>Alan Turing actually solved it in a clever way by using <em>reductio ad absurdum</em>.</p>
<p>Suppose we have a program <code>R</code> that takes 2 inputs: a program <code>P</code> and another input <code>I</code>, and is able to tell us if <code>P(I)</code> is going to halt or not.</p>
<p><img alt="r drawing" src="/img/misc/the-halting-problem/r.png"></p>
<p>Now, imagine if we create a program <code>R'</code> that gives its 2 inputs to <code>R</code> and:</p>
<ul>
<li>if the result of <code>R(P, I)</code> is true i.e. <code>P</code> with <code>I</code> will halt, then it loops forever.</li>
<li>if the result of <code>R(P, I)</code> is false i.e. <code>P</code> with <code>I</code> will not halt, then it returns true.</li>
</ul>
<p>something like this:</p>
<p><img alt="r' drawing" src="/img/misc/the-halting-problem/r2.png"></p>
<p>What happens if give our program <code>R'</code> to <code>R'</code>?</p>
<p><img alt="r' drawing" src="/img/misc/the-halting-problem/r3.png"></p>
<p><code>R'</code> will give <code>R'</code> to <code>R</code> and so we have 2 cases:</p>
<ul>
<li>if <code>R'</code> is supposed to halt, <code>R</code> will return true, which will make <code>R'</code> loop forever</li>
<li>if <code>R'</code> is not supposed to halt, <code>R</code> will return false, which will make <code>R'</code> halts and return true</li>
</ul>
<p>In both cases, we have a paradox: when <code>R</code> tells us that <code>R'</code> is supposed to halt, it loops forever and vice versa.</p>
<p>We started with the assumption that <code>R</code> would be able to tell us whether a program is going to halt or not and we ended up with a paradox, therefore the assumption was bad.</p>
<h3 id="an-example-with-some-python">An example with some python</h3>
<p>Same story, let’s assume that there is a function <code>is_halting</code> that takes 2 arguments:</p>
<ul>
<li>the content of a source file <code>src</code> that is given an input. A source could be: <code>src = "print('args:', arg)"</code></li>
<li>another input <code>arg</code></li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw">def</span> is_halting(src: <span class="bu">str</span>, arg: Any) <span class="op">-&gt;</span> <span class="bu">bool</span>:</span>
<span id="cb2-2"><a href="#cb2-2"></a>    <span class="co"># ...</span></span>
<span id="cb2-3"><a href="#cb2-3"></a>    <span class="cf">return</span> result</span></code></pre></div>
<p>When this function is executed, it either returns <code>True</code> or <code>False</code> depending on whether <code>src</code> associated with <code>arg</code> will halt or not.</p>
<p><img alt="is_halting drawing" src="/img/misc/the-halting-problem/is_halting.png"></p>
<p>Now, let’s make a program <code>paradox.py</code> that wraps our function.</p>
<p>Basically, we want to make a program that gives some source code and an argument to <code>is_halting</code> and if it returns <code>True</code>, it loops forever, else -if it is not supposed to halt-, we will print True. It could look like:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># ...</span></span>
<span id="cb3-2"><a href="#cb3-2"></a>src <span class="op">=</span> sys.argv[<span class="dv">1</span>]</span>
<span id="cb3-3"><a href="#cb3-3"></a>arg <span class="op">=</span> sys.argv[<span class="dv">2</span>]</span>
<span id="cb3-4"><a href="#cb3-4"></a></span>
<span id="cb3-5"><a href="#cb3-5"></a>result <span class="op">=</span> is_halting(src, arg)</span>
<span id="cb3-6"><a href="#cb3-6"></a><span class="cf">if</span> result <span class="op">==</span> <span class="va">True</span>:</span>
<span id="cb3-7"><a href="#cb3-7"></a>    <span class="co"># loop forever</span></span>
<span id="cb3-8"><a href="#cb3-8"></a>    <span class="cf">while</span> <span class="va">True</span>:</span>
<span id="cb3-9"><a href="#cb3-9"></a>        <span class="cf">pass</span></span>
<span id="cb3-10"><a href="#cb3-10"></a><span class="cf">else</span>:</span>
<span id="cb3-11"><a href="#cb3-11"></a>    <span class="bu">print</span>(<span class="va">True</span>)</span></code></pre></div>
<p><img alt="paradox.py drawing" src="/img/misc/the-halting-problem/paradox_py.png"></p>
<p>Finally, let see what happens when we give paradox.py to itself:</p>
<ul>
<li>If paradox.py is supposed to be halting, we will loop forever, meaning <code>paradox.py</code> is not halting. Oops.</li>
<li>If paradox.py is <strong>not</strong> supposed to halt, we will get an answer, meaning it is actually halting. Oops, I did it again.</li>
</ul>
<p><img alt="paradox.py drawing given itself as inputs" src="/img/misc/the-halting-problem/paradox_py2.png"></p>
<p>We can then conclude that there is function <code>is_halting</code> that can gives a correct result.</p>
]]></description>
  <link>https://shaoner.com/2022/09/20/the-halting-problem.html</link>
  <guid>/2022/09/20/the-halting-problem.html</guid>
  <pubDate>Tue, 20 Sep 2022 00:00:00 +0000</pubDate>
</item>


<item>
  <title>Page fault</title>
  <description><![CDATA[<p>I recently reopened an old project of mine, and by reading my own code and comments I re-explored what a page fault is and wanted to write it down in here!</p>
<h2 id="virtual-address-space">Virtual address space</h2>
<p>To understand a page fault, it’s necessary to understand how the memory is organized first.</p>
<h3 id="process-memory">Process memory</h3>
<p>When multiple processes run at the same time, we need to ensure they are isolated from one another. We don’t want a process A being able to read or write a memory value from process B.</p>
<p>These addresses also need to be relative and not absolute. Imagine process A requests 4096 bytes so it could use <code>0x0000</code> to <code>0x0fff</code> and process B needs 8192 bytes so from <code>0x1000</code> to <code>0x2fff</code>. Now process A terminates and process C requests 8092 bytes but it can’t fit in process A old space so we now have a tiny gap and we just created memory fragmentation. At some point we may run out of memory.</p>
<p>To deal with these issues, each process has its own address space which depends on the architecture (for ex: <span class="math inline">2<sup>32</sup></span> addresses for a 32-bit architecture)</p>
<p><em>Notes</em>:</p>
<ul>
<li>The physical memory doesn’t have to be as big as the address space.</li>
<li>The OS reserves some of the virtual memory for itself.</li>
</ul>
<h3 id="pages">Pages</h3>
<p>When the CPU needs to access an address, the address is first sent to an MMU (<a href="https://en.wikipedia.org/wiki/Memory_management_unit">Memory Management Unit</a>) which translates it to a physical address and then sends the address on the memory bus.</p>
<p>The virtual address space is split into fixed memory range called pages. The physical address space is also split into pages called page frames. Usually virtual pages and page frames are the same size.</p>
<p>This allows to load an entire contiguous range of memory instead of a single value.</p>
<p>The OS keeps a mapping between virtual page / page frame that it shares with the MMU in order to translate a virtual address into a physical address. The MMU extracts from the address:</p>
<ul>
<li>a page index</li>
<li>an offset</li>
</ul>
<p>The MMU can then use the page index to retrieve the page frame from the page table and adds the offset back to get the physical address.</p>
<h3 id="page-table">Page table</h3>
<h3 id="page-table-entry">Page table entry</h3>
<p>A page table entry contains more than just the page frame number:</p>
<p><img src="/img/kernel/page-fault/page_table_entry.jpg"></p>
<p>with:</p>
<ul>
<li>Present/Absent: If this bit is 0, it means the page isn’t mapped (i.e. a page fault if it is accessed)</li>
<li>Protection: It reflects the permission of the page, read/write or read only, etc. It can be 1 bit or 3 bits for more granular permissions.</li>
<li>Modified: If a page is written (as being used), this bit is automatically set to 1. It allows the OS to decide if the page needs to be written to disk or if it’s still clean since the last save.</li>
<li>Referenced: Sets if a page is referenced for writing or reading. It also helps the OS to decide on which page is less used.</li>
<li>Caching disabled: Help to decide whether the cache for this page should be disabled.</li>
</ul>
<h3 id="mapping">Mapping</h3>
<p>For example: If there are 16 virtual pages to represent address from 0 to 65536. Each address could be made of 4 bits to represent the virtual page index and 12 bits for the offset, which gives us 4096 possible offsets so each page should be 4096. In this case, address 24976 (= 0x6190) can be seen as 4 bits, 0x6, for the virtual page number and 12 bits, 0x190 = 400, for the offset.</p>
<p><img src="/img/kernel/page-fault/virtual_mapping.jpg"></p>
<p>Now accessing address <code>24976</code> is at virtual page 6. Virtual page 6 starts at address 24576 (<span class="math inline">4096<em>x</em>6</span>) and the address has an offset of 400 so the MMU translates it to page frame 3 with offset 400 so it becomes <span class="math inline">8192 + 400 = 8592</span></p>
<p>However, if you try accessing virtual address <code>28772</code> (virtual page 7), it’s not mapped, in that case it causes the CPU to <a href="https://en.wikipedia.org/wiki/Trap_(computing)">trap</a> the OS, which is called a <strong>page-fault</strong>.</p>
<h2 id="page-fault-management">Page-fault management</h2>
<p>When the OS receives a page-fault from the CPU, it needs to remap the virtual page to a page-frame. It selects a virtual page that is less used (using the Modified and Referenced bits), saves the physical page, retrieves the referenced page (previously unmapped) and loads it into the page frame. Finally, it updates the page table and restarts the CPU instruction that traps.</p>
<p>There are 2 types of page-fault:</p>
<ul>
<li>soft: the page can be retrieved from memory cache</li>
<li>hard: the page needs to be copied from disk (much slower)</li>
</ul>
]]></description>
  <link>https://shaoner.com/2021/11/28/page-fault.html</link>
  <guid>/2021/11/28/page-fault.html</guid>
  <pubDate>Sun, 28 Nov 2021 00:00:00 +0000</pubDate>
</item>


<item>
  <title>Field element division with big numbers in Rust</title>
  <description><![CDATA[<h2 id="introduction">Introduction</h2>
<p>Yo everybody, now that we have addition and multiplication, we’re going to need division.</p>
<p>Elliptic curve point addition requires field element addition, substraction, multiplication, square and division. Substraction is very similar to addition and square is very similar to multiplication so I won’t cover them. Modular division is a different story.</p>
<p>So we need to calculate <span class="math inline"><em>a</em>/<em>b</em></span> with <span class="math inline"><em>b</em> ≠ 0</span>. Like in normal division, field division is the inverse of multiplication therefore:</p>
<p><span class="math inline"><em>a</em>/<em>b</em> = <em>a</em> ⋅ 1/<em>b</em> = <em>a</em> ⋅ <em>b</em><sup>−1</sup></span></p>
<p>if we can get the modular inverse of b, we can then multiply it by a.</p>
<p>I’m going to evaluate 2 main methods:</p>
<ul>
<li>Fermat little theorem</li>
<li>Binary GCD</li>
</ul>
<h2 id="fermat-little-theorem">Fermat little theorem</h2>
<p>Fermat little theorem demonstrates that when P is prime:</p>
<p><span class="math inline"><em>x</em><sup>−1</sup> ≡ <em>x</em><sup><em>P</em> − 2</sup> mod  <em>P</em></span></p>
<p>Because we already have the multiplication working, we could indeed handle exponentiation. Of course, we don’t have to do <span class="math inline"><em>P</em> − 2 − 1</span> multiplications of x as we can store intermediary results and it can be done with some multiplications and squaring to have <a href="https://en.wikipedia.org/wiki/Exponentiation_by_squaring#Basic_method">fast exponentiation</a>.</p>
<p>It’s definitely interesting and multiplications on a 64 bits x86 CPU are quite fast but most of my research showed that GCD is a good way to support multiple platforms.</p>
<p>The intent of this lib is to be part of a blockchain node and I like that you could run a node from a raspberry pi.</p>
<p>It actually seems that Parity uses <a href="https://github.com/paritytech/libsecp256k1/blob/3c2a3c2b1cbdfa37555e3d9de3b4e5114693901c/core/src/field.rs#L1546">a similar approach</a> as for now.</p>
<h2 id="extended-euclidean-arithmetic">Extended euclidean arithmetic</h2>
<p>Extended euclidean arithmetic extends the Euclidean arithmetic - duh - so we can compute the modular inverse.</p>
<h3 id="gcd">GCD</h3>
<p>While reading on how to deal with modular inverse, I found out an interesting method using the GCD (greatest common divisor).</p>
<p>Commonly, the Euclidean algorithm we learn at school looks like this:</p>
<p></p>
<p>And if we want the GCD of two numbers a and b, we just iterate until <span class="math inline"><em>r</em><sub><em>k</em> + 1</sub> = 0</span> and we have <span class="math inline"><em>r</em><sub><em>k</em></sub> = <em>g</em><em>c</em><em>d</em>(<em>a</em>,<em>b</em>)</span></p>
<p>Example with <span class="math inline"><em>a</em> = 2023, <em>b</em> = 714</span>: </p>
<p>Thanks and bye.</p>
<h3 id="bézouts-identity">Bézout’s identity</h3>
<p>Ok great but how does it help finding the modular inverse?</p>
<p>Well now we have to use <a href="https://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity">Bézout’s identity</a>, quoting wikipedia:</p>
<blockquote>
<p>Let a and b be integers with greatest common divisor d. Then there exist integers x and y such that ax + by = d. More generally, the integers of the form ax + by are exactly the multiples of d. </p>
</blockquote>
<p>It allows us to write this:</p>
<p><span class="math inline"><em>a</em><em>x</em> + <em>b</em><em>y</em> = <em>g</em><em>c</em><em>d</em>(<em>a</em>,<em>b</em>)</span></p>
<p>It becomes useful when a and b are co-prime because in this case:</p>
<p><span class="math inline"><em>a</em><em>x</em> + <em>b</em><em>y</em> = 1</span></p>
<p><span class="math inline"><em>a</em><em>x</em> = <em>b</em>(−<em>y</em>) + 1</span></p>
<p>Exactly like we would write an euclidean division: <span class="math inline"><em>a</em>′ = <em>b</em><em>q</em> + <em>r</em></span> with <span class="math inline"><em>a</em>′ = <em>a</em><em>x</em></span></p>
<p>And we can deduce: <span class="math inline"><em>a</em><em>x</em> ≡ 1 mod  <em>b</em></span>, therefore x is the multiplicative inverse of a mod b, and this is exactly what we want.</p>
<h3 id="extended-euclidean-algorithm">Extended Euclidean algorithm</h3>
<p><em>How to calculate x and y?</em></p>
<p>The Extended Euclidean algorithm is actually about finding Bézout coefficients x and y:</p>
<p></p>
<p>It gives us: <span class="math inline"><em>g</em><em>c</em><em>d</em>(<em>a</em>,<em>b</em>) = <em>r</em><sub><em>k</em></sub> = <em>a</em><em>x</em><sub><em>k</em></sub> + <em>b</em><em>y</em><sub><em>k</em></sub></span></p>
<h3 id="application-for-p">Application for P</h3>
<p>If a and b are co-prime, <span class="math inline"><em>a</em><em>x</em> ≡ 1 mod  <em>b</em></span></p>
<p>Well, our P is prime itself so we already know that a and P are co-prime, i.e. <span class="math inline"><em>a</em><em>x</em> ≡ 1 mod  <em>P</em></span> and we can calculate x thanks to the extended euclidean algo.</p>
<p>In python it looks like:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a>P <span class="op">=</span> <span class="dv">2</span><span class="op">**</span><span class="dv">256</span> <span class="op">-</span> <span class="dv">2</span><span class="op">**</span><span class="dv">32</span> <span class="op">-</span> <span class="dv">977</span></span>
<span id="cb1-2"><a href="#cb1-2"></a></span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="kw">def</span> modinv(a, m):</span>
<span id="cb1-4"><a href="#cb1-4"></a>    r0 <span class="op">=</span> a</span>
<span id="cb1-5"><a href="#cb1-5"></a>    r1 <span class="op">=</span> m</span>
<span id="cb1-6"><a href="#cb1-6"></a>    x <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb1-7"><a href="#cb1-7"></a>    y <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb1-8"><a href="#cb1-8"></a>    i <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb1-9"><a href="#cb1-9"></a>    <span class="cf">while</span> r1 <span class="op">!=</span> <span class="dv">0</span>:</span>
<span id="cb1-10"><a href="#cb1-10"></a>        q <span class="op">=</span> r0 <span class="op">//</span> r1</span>
<span id="cb1-11"><a href="#cb1-11"></a>        tmp <span class="op">=</span> r0 <span class="op">-</span> q <span class="op">*</span> r1</span>
<span id="cb1-12"><a href="#cb1-12"></a>        tmp2 <span class="op">=</span> x <span class="op">-</span> q <span class="op">*</span> y</span>
<span id="cb1-13"><a href="#cb1-13"></a>        r0 <span class="op">=</span> r1</span>
<span id="cb1-14"><a href="#cb1-14"></a>        r1 <span class="op">=</span> tmp</span>
<span id="cb1-15"><a href="#cb1-15"></a>        x <span class="op">=</span> y</span>
<span id="cb1-16"><a href="#cb1-16"></a>        y <span class="op">=</span> tmp2</span>
<span id="cb1-17"><a href="#cb1-17"></a>        i <span class="op">+=</span> <span class="dv">1</span></span>
<span id="cb1-18"><a href="#cb1-18"></a>    <span class="cf">if</span> x <span class="op">&lt;</span> <span class="dv">0</span>:</span>
<span id="cb1-19"><a href="#cb1-19"></a>        x <span class="op">+=</span> m</span>
<span id="cb1-20"><a href="#cb1-20"></a>    <span class="cf">return</span> x0</span></code></pre></div>
<p>Now if we want <span class="math inline"><em>a</em>/<em>b</em></span> we can do:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>a <span class="op">*</span> modinv(b, P)</span></code></pre></div>
<p>Another way is to modify the function so that x0 is initialized with the dividend directly, it works the same way because x0 is not involved in the loop condition and we only subtract values to it.</p>
<p>For now, it’s unclear if it’s better and a simple modinv and multiplication though.</p>
<p>Anyway…</p>
<h2 id="tadam">TADAM</h2>
<p><img alt="Winner gif" src="/img/crypto/modular-division/winner.gif"></p>
<p>Okay, that’s cool but coming back to Rust, we cannot actually use our field element type here because we need signedness and all the operations without a reduction to P.</p>
<p>We need a new type that handles signedness, multiplication, integer division, addition and substraction. This type would need at least 257 bits: 1 bit for the sign, 256 bits to represent numbers close to P.</p>
<p>After dealing with our field element type, this seems easy enough except for the integer division. Indeed, if we want to compute a quotient of two big numbers. There are multiple interesting algorithms to deal with this but a simpler method is to use the binary GCD algorithm.</p>
<p>Please follow me, it’s this way:</p>
<p><img alt="Down gif" src="/img/crypto/modular-division/down.gif"></p>
<h2 id="extended-binary-gcd">Extended Binary GCD</h2>
<p>The interesting part of this GCD algorithm is that it allows us to compute the GCD with only divisions by 2, so only right shifts.</p>
<p>The algorithm is described as follow <a href="https://en.wikipedia.org/wiki/Binary_GCD_algorithm">on wikipedia</a>:</p>
<p></p>
<h3 id="implementation">Implementation</h3>
<p>Taking the definition, it can be written this way:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">def</span> bin_gcd(u, v):</span>
<span id="cb3-2"><a href="#cb3-2"></a>    <span class="cf">if</span> u <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb3-3"><a href="#cb3-3"></a>        <span class="cf">return</span> v</span>
<span id="cb3-4"><a href="#cb3-4"></a>    <span class="cf">if</span> v <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb3-5"><a href="#cb3-5"></a>        <span class="cf">return</span> u</span>
<span id="cb3-6"><a href="#cb3-6"></a>    k <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb3-7"><a href="#cb3-7"></a>    <span class="cf">while</span> u <span class="op">!=</span> v:</span>
<span id="cb3-8"><a href="#cb3-8"></a>        <span class="cf">if</span> u <span class="op">&amp;</span> <span class="bn">0x1</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb3-9"><a href="#cb3-9"></a>            <span class="cf">if</span> v <span class="op">&amp;</span> <span class="bn">0x1</span> <span class="op">!=</span> <span class="dv">0</span>:</span>
<span id="cb3-10"><a href="#cb3-10"></a>                <span class="co"># gcd(2u, v) = gcd(u, v)</span></span>
<span id="cb3-11"><a href="#cb3-11"></a>                u <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb3-12"><a href="#cb3-12"></a>            <span class="cf">else</span>:</span>
<span id="cb3-13"><a href="#cb3-13"></a>                <span class="co"># gcd(2u, 2v) = 2 * gcd(u, v)</span></span>
<span id="cb3-14"><a href="#cb3-14"></a>                u <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb3-15"><a href="#cb3-15"></a>                v <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb3-16"><a href="#cb3-16"></a>                k <span class="op">&lt;&lt;=</span> <span class="dv">1</span></span>
<span id="cb3-17"><a href="#cb3-17"></a>        <span class="cf">elif</span> v <span class="op">&amp;</span> <span class="bn">0x1</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb3-18"><a href="#cb3-18"></a>            <span class="co"># gcd(u, 2v) = gcd(u, v)</span></span>
<span id="cb3-19"><a href="#cb3-19"></a>            v <span class="op">=</span> v <span class="op">&gt;&gt;</span> <span class="dv">1</span></span>
<span id="cb3-20"><a href="#cb3-20"></a>        <span class="cf">else</span>:</span>
<span id="cb3-21"><a href="#cb3-21"></a>            <span class="co"># gcd(u, v) = gcd(|u - v|, min(u, v))</span></span>
<span id="cb3-22"><a href="#cb3-22"></a>            <span class="cf">if</span> u <span class="op">&gt;</span> v:</span>
<span id="cb3-23"><a href="#cb3-23"></a>                u <span class="op">=</span> (u <span class="op">-</span> v)</span>
<span id="cb3-24"><a href="#cb3-24"></a>            <span class="cf">else</span>:</span>
<span id="cb3-25"><a href="#cb3-25"></a>                tmp <span class="op">=</span> u</span>
<span id="cb3-26"><a href="#cb3-26"></a>                u <span class="op">=</span> (v <span class="op">-</span> u)</span>
<span id="cb3-27"><a href="#cb3-27"></a>                v <span class="op">=</span> tmp</span>
<span id="cb3-28"><a href="#cb3-28"></a>    <span class="cf">return</span> u <span class="op">*</span> k</span></code></pre></div>
<h3 id="binary-mod-inverse">Binary mod inverse</h3>
<p>Now this is a very basic implementation but in our case we want to integrate x such as <span class="math inline"><em>u</em><em>x</em> ≡ 1 mod  <em>P</em></span> to create a modinv function.</p>
<p>Some interesting things to notice before writing a modinv version:</p>
<ul>
<li>the factor <code>k</code> isn’t necessary anymore, as it doesn’t play a role in the calculation, it’s just used to compute the GCD which would be 1 in our case</li>
<li>all the divisions by 2 are exact</li>
</ul>
<p>Refactoring our function, we can integrate x and y like we did before:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> div2(x, m):</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> x <span class="op">&amp;</span> <span class="dv">1</span>:</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>        x <span class="op">+=</span> m</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> x <span class="op">&gt;&gt;</span> <span class="dv">1</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> modinv(a, m):</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>    u <span class="op">=</span> a</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a>    v <span class="op">=</span> m</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a>    x <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a>    y <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a>    <span class="cf">while</span> u <span class="op">!=</span> v:</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a>        <span class="cf">if</span> (u <span class="op">&amp;</span> <span class="bn">0x01</span>) <span class="op">==</span> <span class="bn">0x00</span>:</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a>            u <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a>            x <span class="op">=</span> div2(x, m)</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a>        <span class="cf">elif</span> (v <span class="op">&amp;</span> <span class="bn">0x01</span>) <span class="op">==</span> <span class="bn">0x00</span>:</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a>            v <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a>            y <span class="op">=</span> div2(y, m)</span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a>        <span class="cf">elif</span> u <span class="op">&gt;</span> v:</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a>            u <span class="op">=</span> (u <span class="op">-</span> v) <span class="op">&gt;&gt;</span> <span class="dv">1</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a>            x <span class="op">=</span> div2(x <span class="op">-</span> y, m)</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a>        <span class="cf">else</span>:</span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a>            v <span class="op">=</span> (v <span class="op">-</span> u) <span class="op">&gt;&gt;</span> <span class="dv">1</span></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a>            y <span class="op">=</span> div2(y <span class="op">-</span> x, m)</span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> y</span></code></pre></div>
<p>Note that we have a new <code>div2</code> function because to compute an exact division, we want the dividend to be even. If it’s is odd, we can just add the -prime- moduli to it. Some possible optimizations will be covered in a next article.</p>
<p>It can be further simplified by just switching <span class="math inline"><em>u</em></span> and <span class="math inline"><em>v</em></span> when <span class="math inline"><em>u</em> &lt; <em>v</em></span>:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">def</span> modinv_classic(a, m):</span>
<span id="cb5-2"><a href="#cb5-2"></a>    u <span class="op">=</span> a</span>
<span id="cb5-3"><a href="#cb5-3"></a>    v <span class="op">=</span> m</span>
<span id="cb5-4"><a href="#cb5-4"></a>    x <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb5-5"><a href="#cb5-5"></a>    y <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb5-6"><a href="#cb5-6"></a>    <span class="cf">while</span> u <span class="op">!=</span> <span class="dv">0</span>:</span>
<span id="cb5-7"><a href="#cb5-7"></a>        <span class="cf">if</span> (u <span class="op">&amp;</span> <span class="bn">0x01</span>) <span class="op">==</span> <span class="bn">0x00</span>:</span>
<span id="cb5-8"><a href="#cb5-8"></a>            u <span class="op">&gt;&gt;=</span> <span class="dv">1</span></span>
<span id="cb5-9"><a href="#cb5-9"></a>            x <span class="op">=</span> mdiv2(x, m)</span>
<span id="cb5-10"><a href="#cb5-10"></a>        <span class="cf">else</span>:</span>
<span id="cb5-11"><a href="#cb5-11"></a>            <span class="cf">if</span> u <span class="op">&lt;</span> v:</span>
<span id="cb5-12"><a href="#cb5-12"></a>                tmp <span class="op">=</span> u</span>
<span id="cb5-13"><a href="#cb5-13"></a>                u <span class="op">=</span> v</span>
<span id="cb5-14"><a href="#cb5-14"></a>                v <span class="op">=</span> tmp</span>
<span id="cb5-15"><a href="#cb5-15"></a>                tmp <span class="op">=</span> x</span>
<span id="cb5-16"><a href="#cb5-16"></a>                x <span class="op">=</span> y</span>
<span id="cb5-17"><a href="#cb5-17"></a>                y <span class="op">=</span> tmp</span>
<span id="cb5-18"><a href="#cb5-18"></a>            u <span class="op">=</span> (u <span class="op">-</span> v) <span class="op">&gt;&gt;</span> <span class="dv">1</span></span>
<span id="cb5-19"><a href="#cb5-19"></a>            x <span class="op">=</span> div2(x <span class="op">-</span> y, m)</span>
<span id="cb5-20"><a href="#cb5-20"></a>    <span class="cf">return</span> y</span></code></pre></div>
<h2 id="lets-rust">Let’s rust</h2>
<h3 id="recap">Recap</h3>
<p>A quick recap, we want to add a modinv function to our field element type.</p>
<p>For this, we need to create a modified binary gcd function that calculates the modular inverse of a value x. This function needs to work with signed numbers. Thus, we need to create a signed integer type.</p>
<h3 id="scalar">Scalar</h3>
<p>We want to represent numbers on 256 bits + 1 bit for the sign. However as we actually do multiple additions and substraction, it could be interesting to keep a carry and even more interesting to use the same carry-save tricks we use on our field element type.</p>
<h4 id="modinv">modinv</h4>
<p>This time <code>modinv</code> isn’t optimized specifically for <span class="math inline"><em>P</em></span>, instead we can pass a modulo m just so we can re-use the type:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb6-1"><a href="#cb6-1"></a><span class="at">#[</span>derive<span class="at">(</span><span class="bu">Clone</span><span class="op">,</span> <span class="bu">Copy</span><span class="op">,</span> <span class="bu">PartialEq</span><span class="op">,</span> <span class="bu">Eq</span><span class="at">)]</span></span>
<span id="cb6-2"><a href="#cb6-2"></a><span class="kw">pub</span> <span class="kw">struct</span> Scalar <span class="op">{</span></span>
<span id="cb6-3"><a href="#cb6-3"></a>    <span class="kw">pub</span> d<span class="op">:</span> [<span class="dt">u64</span><span class="op">;</span> <span class="dv">5</span>]</span>
<span id="cb6-4"><a href="#cb6-4"></a><span class="op">}</span></span>
<span id="cb6-5"><a href="#cb6-5"></a></span>
<span id="cb6-6"><a href="#cb6-6"></a><span class="kw">impl</span> Scalar <span class="op">{</span></span>
<span id="cb6-7"><a href="#cb6-7"></a>    <span class="co">// ...</span></span>
<span id="cb6-8"><a href="#cb6-8"></a>    <span class="kw">fn</span> div2_mod(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> m<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">{</span></span>
<span id="cb6-9"><a href="#cb6-9"></a>        <span class="kw">if</span> <span class="op">!</span><span class="kw">self</span><span class="op">.</span>is_even() <span class="op">{</span></span>
<span id="cb6-10"><a href="#cb6-10"></a>            <span class="kw">self</span><span class="op">.</span>add_assign(m)<span class="op">;</span></span>
<span id="cb6-11"><a href="#cb6-11"></a>        <span class="op">}</span></span>
<span id="cb6-12"><a href="#cb6-12"></a>        <span class="kw">self</span><span class="op">.</span>div2()</span>
<span id="cb6-13"><a href="#cb6-13"></a>    <span class="op">}</span></span>
<span id="cb6-14"><a href="#cb6-14"></a></span>
<span id="cb6-15"><a href="#cb6-15"></a>    <span class="kw">pub</span> <span class="kw">fn</span> modinv(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> m<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">{</span></span>
<span id="cb6-16"><a href="#cb6-16"></a>        <span class="kw">let</span> <span class="kw">mut</span> b <span class="op">=</span> <span class="op">*</span>m<span class="op">;</span></span>
<span id="cb6-17"><a href="#cb6-17"></a>        <span class="kw">let</span> <span class="kw">mut</span> x <span class="op">=</span> <span class="dt">Self</span><span class="pp">::</span>from_u64(<span class="dv">1u64</span>)<span class="op">;</span></span>
<span id="cb6-18"><a href="#cb6-18"></a>        <span class="kw">let</span> <span class="kw">mut</span> y <span class="op">=</span> <span class="dt">Self</span><span class="pp">::</span>from_u64(<span class="dv">0u64</span>)<span class="op">;</span></span>
<span id="cb6-19"><a href="#cb6-19"></a></span>
<span id="cb6-20"><a href="#cb6-20"></a>        <span class="kw">while</span> <span class="op">!</span><span class="kw">self</span><span class="op">.</span>is_zero() <span class="op">{</span></span>
<span id="cb6-21"><a href="#cb6-21"></a>            <span class="kw">if</span> <span class="kw">self</span><span class="op">.</span>is_even() <span class="op">{</span></span>
<span id="cb6-22"><a href="#cb6-22"></a>                <span class="kw">self</span><span class="op">.</span>div2()<span class="op">;</span></span>
<span id="cb6-23"><a href="#cb6-23"></a>                x<span class="op">.</span>div2_mod(m)<span class="op">;</span></span>
<span id="cb6-24"><a href="#cb6-24"></a>            <span class="op">}</span> <span class="kw">else</span> <span class="op">{</span></span>
<span id="cb6-25"><a href="#cb6-25"></a>                <span class="kw">if</span> <span class="op">*</span><span class="kw">self</span> <span class="op">&lt;</span> b <span class="op">{</span></span>
<span id="cb6-26"><a href="#cb6-26"></a>                    <span class="pp">mem::</span>swap(<span class="kw">self</span><span class="op">,</span> <span class="op">&amp;</span><span class="kw">mut</span> b)<span class="op">;</span></span>
<span id="cb6-27"><a href="#cb6-27"></a>                    <span class="pp">mem::</span>swap(<span class="op">&amp;</span><span class="kw">mut</span> x<span class="op">,</span> <span class="op">&amp;</span><span class="kw">mut</span> y)<span class="op">;</span></span>
<span id="cb6-28"><a href="#cb6-28"></a>                <span class="op">}</span></span>
<span id="cb6-29"><a href="#cb6-29"></a>                <span class="op">*</span><span class="kw">self</span> <span class="op">-=</span> b<span class="op">;</span></span>
<span id="cb6-30"><a href="#cb6-30"></a>                <span class="kw">self</span><span class="op">.</span>div2()<span class="op">;</span></span>
<span id="cb6-31"><a href="#cb6-31"></a>                x <span class="op">-=</span> y<span class="op">;</span></span>
<span id="cb6-32"><a href="#cb6-32"></a>                x<span class="op">.</span>div2_mod(m)<span class="op">;</span></span>
<span id="cb6-33"><a href="#cb6-33"></a>            <span class="op">}</span></span>
<span id="cb6-34"><a href="#cb6-34"></a>        <span class="op">}</span></span>
<span id="cb6-35"><a href="#cb6-35"></a>        y<span class="op">.</span>normalize(m)<span class="op">;</span></span>
<span id="cb6-36"><a href="#cb6-36"></a>        <span class="op">*</span><span class="kw">self</span> <span class="op">=</span> y<span class="op">;</span></span>
<span id="cb6-37"><a href="#cb6-37"></a>    <span class="op">}</span></span>
<span id="cb6-38"><a href="#cb6-38"></a><span class="op">}</span></span></code></pre></div>
<p>It works exactly the same way as the python function. The only trick is to define the <code>div2</code> function as a shift by 1. I didn’t go much into details about the other functions implementation because it mainly depends on the limb representation of the Scalar but if we forget about carry optimization, <a href="https://github.com/alexlren/astrel_secp256k1/blob/71f553bace9bede731355d54a3d5c06cb6dfd41d/src/modinv.rs">it looks like this</a>.</p>
<p>Now for the carry optim version, we could simply implement something similar to our field element, and keep 1 bit per limb for the sign but that’s out of scope.</p>
<h4 id="field-element-division">Field element division</h4>
<p>We can simply define our field element <code>inverse</code>:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb7-1"><a href="#cb7-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> to_scalar(<span class="op">&amp;</span><span class="kw">self</span>) <span class="op">-&gt;</span> Scalar <span class="op">{</span></span>
<span id="cb7-2"><a href="#cb7-2"></a>        <span class="kw">let</span> d0 <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="op">&gt;&gt;</span> <span class="dv">0</span>) <span class="op">|</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="op">&lt;&lt;</span> <span class="dv">52</span>)<span class="op">;</span></span>
<span id="cb7-3"><a href="#cb7-3"></a>        <span class="kw">let</span> d1 <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="op">&gt;&gt;</span> <span class="dv">12</span>) <span class="op">|</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="op">&lt;&lt;</span> <span class="dv">40</span>)<span class="op">;</span></span>
<span id="cb7-4"><a href="#cb7-4"></a>        <span class="kw">let</span> d2 <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="op">&gt;&gt;</span> <span class="dv">24</span>) <span class="op">|</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="op">&lt;&lt;</span> <span class="dv">28</span>)<span class="op">;</span></span>
<span id="cb7-5"><a href="#cb7-5"></a>        <span class="kw">let</span> d3 <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="op">&gt;&gt;</span> <span class="dv">36</span>) <span class="op">|</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">4</span>] <span class="op">&lt;&lt;</span> <span class="dv">16</span>)<span class="op">;</span></span>
<span id="cb7-6"><a href="#cb7-6"></a></span>
<span id="cb7-7"><a href="#cb7-7"></a>        <span class="pp">Scalar::</span>new(<span class="dv">0</span><span class="op">,</span> d3<span class="op">,</span> d2<span class="op">,</span> d1<span class="op">,</span> d0)</span>
<span id="cb7-8"><a href="#cb7-8"></a>    <span class="op">}</span></span>
<span id="cb7-9"><a href="#cb7-9"></a></span>
<span id="cb7-10"><a href="#cb7-10"></a>    <span class="kw">pub</span> <span class="kw">fn</span> from_scalar(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> n<span class="op">:</span> <span class="op">&amp;</span>Scalar) <span class="op">{</span></span>
<span id="cb7-11"><a href="#cb7-11"></a>        <span class="kw">let</span> d0 <span class="op">=</span> n<span class="op">.</span>d[<span class="dv">0</span>] <span class="op">&amp;</span> <span class="dv">0x000fffffffffffff</span><span class="op">;</span></span>
<span id="cb7-12"><a href="#cb7-12"></a>        <span class="kw">let</span> d1 <span class="op">=</span> n<span class="op">.</span>d[<span class="dv">0</span>] <span class="op">&gt;&gt;</span> <span class="dv">52</span> <span class="op">|</span> (n<span class="op">.</span>d[<span class="dv">1</span>] <span class="op">&amp;</span> <span class="dv">0x000000ffffffffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">12</span><span class="op">;</span></span>
<span id="cb7-13"><a href="#cb7-13"></a>        <span class="kw">let</span> d2 <span class="op">=</span> n<span class="op">.</span>d[<span class="dv">1</span>] <span class="op">&gt;&gt;</span> <span class="dv">40</span> <span class="op">|</span> (n<span class="op">.</span>d[<span class="dv">2</span>] <span class="op">&amp;</span> <span class="dv">0x000000000fffffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">24</span><span class="op">;</span></span>
<span id="cb7-14"><a href="#cb7-14"></a>        <span class="kw">let</span> d3 <span class="op">=</span> n<span class="op">.</span>d[<span class="dv">2</span>] <span class="op">&gt;&gt;</span> <span class="dv">28</span> <span class="op">|</span> (n<span class="op">.</span>d[<span class="dv">3</span>] <span class="op">&amp;</span> <span class="dv">0x000000000000ffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">36</span><span class="op">;</span></span>
<span id="cb7-15"><a href="#cb7-15"></a>        <span class="kw">let</span> d4 <span class="op">=</span> n<span class="op">.</span>d[<span class="dv">3</span>] <span class="op">&gt;&gt;</span> <span class="dv">16</span><span class="op">;</span></span>
<span id="cb7-16"><a href="#cb7-16"></a></span>
<span id="cb7-17"><a href="#cb7-17"></a>        <span class="kw">self</span><span class="op">.</span>d <span class="op">=</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4]<span class="op">;</span></span>
<span id="cb7-18"><a href="#cb7-18"></a>    <span class="op">}</span></span>
<span id="cb7-19"><a href="#cb7-19"></a></span>
<span id="cb7-20"><a href="#cb7-20"></a>    <span class="kw">pub</span> <span class="kw">fn</span> inverse(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span>) <span class="op">{</span></span>
<span id="cb7-21"><a href="#cb7-21"></a>        <span class="kw">self</span><span class="op">.</span>reduce()<span class="op">;</span></span>
<span id="cb7-22"><a href="#cb7-22"></a>        <span class="kw">let</span> <span class="kw">mut</span> n <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>to_scalar()<span class="op">;</span></span>
<span id="cb7-23"><a href="#cb7-23"></a>        n<span class="op">.</span>modinv(<span class="op">&amp;</span>P)<span class="op">;</span></span>
<span id="cb7-24"><a href="#cb7-24"></a>        <span class="kw">self</span><span class="op">.</span>from_scalar(<span class="op">&amp;</span>n)<span class="op">;</span></span>
<span id="cb7-25"><a href="#cb7-25"></a>    <span class="op">}</span></span></code></pre></div>
<p>and even some syntactic sugar for the division itself:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1"></a>    <span class="kw">pub</span> div_assign(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> rhs<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">{</span></span>
<span id="cb8-2"><a href="#cb8-2"></a>        <span class="kw">let</span> n <span class="op">=</span> <span class="op">*</span>rhs<span class="op">;</span></span>
<span id="cb8-3"><a href="#cb8-3"></a>        n<span class="op">.</span>inverse()<span class="op">;</span></span>
<span id="cb8-4"><a href="#cb8-4"></a>        <span class="kw">return</span> <span class="kw">self</span><span class="op">.</span>mul_assign(n)<span class="op">;</span></span>
<span id="cb8-5"><a href="#cb8-5"></a>    <span class="op">}</span></span></code></pre></div>
<p>Alright, time to go to bed everyone.</p>
]]></description>
  <link>https://shaoner.com/2021/09/15/field-element-division.html</link>
  <guid>/2021/09/15/field-element-division.html</guid>
  <pubDate>Wed, 15 Sep 2021 00:00:00 +0000</pubDate>
</item>


<item>
  <title>Field element multiplication with big numbers in Rust</title>
  <description><![CDATA[<h2 id="introduction">Introduction</h2>
<p>We saw in the <a href="/2021/07/07/crypto/modular-addition.html">addition part 1</a> multiple ways to represent our field elements which is key to optimization.</p>
<p>The final version was 4 x u52 and 1 x 48 to store our 256 bits leaving the extra bits for the carry.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">pub</span> <span class="kw">struct</span> Fe <span class="op">{</span></span>
<span id="cb1-2"><a href="#cb1-2"></a>    d<span class="op">:</span> [<span class="dt">u64</span><span class="op">;</span> <span class="dv">5</span>]</span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="op">}</span></span></code></pre></div>
<h2 id="the-long-multiplication">The long multiplication</h2>
<h3 id="algo-with-4-x-u64">Algo with 4 x u64</h3>
<p>To make it easier we can start working with 4 u64 digits as we did for the addition, so <span class="math inline"><em>a</em> × <em>b</em></span>:</p>
<p></p>
<p>I labeled each column with <span class="math inline"><em>t</em><sub><em>i</em></sub></span> with <span class="math inline"><em>i</em> := 0 → 7</span> so it’s easier. And finally we also define <span class="math inline"><em>c</em><sub><em>i</em></sub></span> with <span class="math inline"><em>i</em> := 0 → 6</span> as the carries for each column.</p>
<p>Since we represent <span class="math inline"><em>a</em></span> and <span class="math inline"><em>b</em></span> with 4 digits, there are now 8 terms in our result:</p>
<p></p>
<p>Now we have an issue here, because most of these terms can exceed 128 bits while we only have a u128. For example <span class="math inline"><em>t</em><sub>1</sub></span> \eqref{eq:t_1} is <span class="math inline">64 × 64 + 64 × 64 + 64 → 129</span>, meaning we would need 129 bits.</p>
<p>Fortunately if we go back to our u52 strategy, we don’t have this issue anymore because at worse <span class="math inline"><em>t</em><sub>3</sub></span> \eqref{eq:t_3} is <span class="math inline">52 × 52 + 52 × 52 + 52 × 52 + 52 × 52 + 52 → 106</span> i.e. requires 106 bits</p>
<h3 id="algo-with-4-x-u52-u48">Algo with 4 x u52 + u48</h3>
<p>Let’s get back to our u52 baby</p>
<p><img src="/img/crypto/modular-multiplication/barney_wink.gif"></p>
<p>Now our beautiful multiplication looks more like:</p>
<p></p>
<p></p>
<p>We can also see our result R as <span class="math inline"><em>R</em> = <em>R</em><sub>1</sub> ⋅ 2<sup>256</sup> + <em>R</em><sub>0</sub></span> with:</p>
<ul>
<li><span class="math inline"><em>R</em><sub>0</sub></span> composed of the digits <span class="math inline"><em>t</em><sub>0</sub>, <em>t</em><sub>1</sub>, <em>t</em><sub>2</sub>, <em>t</em><sub>3</sub>, <em>t</em><sub>4</sub></span></li>
<li><span class="math inline"><em>R</em><sub>1</sub></span> composed of the digits <span class="math inline"><em>t</em><sub>5</sub>, <em>t</em><sub>6</sub>, <em>t</em><sub>7</sub>, <em>t</em><sub>8</sub>, <em>t</em><sub>9</sub></span></li>
</ul>
<p>Now because of the size of <span class="math inline"><em>R</em><sub>1</sub></span>, it’s actually more interesting to reduce our result modulo <span class="math inline"><em>P</em></span> directly in the multiplication as opposed to our previous <a href="/2021/07/07/crypto/modular-addition.html">add function</a> in which we could store the carry in the extra bits.</p>
<p>The final purpose is to be able to make multiplications of numbers close to <span class="math inline"><em>P</em></span> so the carry <span class="math inline"><em>R</em><sub>1</sub></span> won’t fit in our 64-bits reserved space.</p>
<p>Similarly to the addition, we have 3 cases:</p>
<ol type="1">
<li><span class="math inline"><em>R</em><sub>1</sub> = 0</span> and <span class="math inline"><em>R</em><sub>0</sub> &lt; <em>P</em></span></li>
</ol>
<p><span class="math inline"><em>R</em> = <em>R</em><sub>0</sub></span></p>
<ol start="2" type="1">
<li><span class="math inline"><em>R</em><sub>1</sub> = 0</span>, <span class="math inline"><em>P</em> &lt;  = <em>R</em><sub>0</sub> &lt; 2<sup>256</sup></span></li>
</ol>
<p><span class="math inline"><em>R</em> = <em>R</em><sub>0</sub> − <em>P</em></span></p>
<ol start="3" type="1">
<li><span class="math inline"><em>R</em><sub>1</sub> &gt; 0</span></li>
</ol>
<p><span class="math inline"><em>R</em> = <em>R</em><sub>0</sub> + <em>R</em><sub>1</sub> ⋅ (2<sup>256</sup>−<em>P</em>)</span></p>
<p>For more clarity, let’s define <span class="math inline"><em>P</em><sub>0</sub> = 2<sup>256</sup> − <em>P</em> = 2<sup>32</sup> + 977</span>, we can now have:</p>
<p></p>
<p>Cool, now we have new carries to propagate:</p>
<p></p>
<p>It’s actually way easier to see it this way:</p>
<p></p>
<h3 id="now-in-rust-ladies-and-gentlemen">NOW IN RUST LADIES AND GENTLEMEN</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> mul(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb2-2"><a href="#cb2-2"></a>        <span class="kw">const</span> M52<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x000fffffffffffffu128</span><span class="op">;</span> <span class="co">// 2^52 - 1</span></span>
<span id="cb2-3"><a href="#cb2-3"></a>        <span class="kw">const</span> M48<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x0000ffffffffffffu128</span><span class="op">;</span> <span class="co">// 2^48 - 1</span></span>
<span id="cb2-4"><a href="#cb2-4"></a>        <span class="kw">const</span> P0<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x1000003d1u128</span><span class="op">;</span> <span class="co">// 2^32 + 977</span></span>
<span id="cb2-5"><a href="#cb2-5"></a></span>
<span id="cb2-6"><a href="#cb2-6"></a>        <span class="kw">let</span> (a0<span class="op">,</span> a1<span class="op">,</span> a2<span class="op">,</span> a3<span class="op">,</span> a4) <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">4</span>])<span class="op">;</span></span>
<span id="cb2-7"><a href="#cb2-7"></a>        <span class="kw">let</span> (b0<span class="op">,</span> b1<span class="op">,</span> b2<span class="op">,</span> b3<span class="op">,</span> b4) <span class="op">=</span> (b<span class="op">.</span>d[<span class="dv">0</span>]<span class="op">,</span> b<span class="op">.</span>d[<span class="dv">1</span>]<span class="op">,</span> b<span class="op">.</span>d[<span class="dv">2</span>]<span class="op">,</span> b<span class="op">.</span>d[<span class="dv">3</span>]<span class="op">,</span> b<span class="op">.</span>d[<span class="dv">4</span>])<span class="op">;</span></span>
<span id="cb2-8"><a href="#cb2-8"></a></span>
<span id="cb2-9"><a href="#cb2-9"></a>        <span class="kw">let</span> (</span>
<span id="cb2-10"><a href="#cb2-10"></a>            <span class="kw">mut</span> t0<span class="op">,</span> <span class="kw">mut</span> t1<span class="op">,</span> <span class="kw">mut</span> t2<span class="op">,</span></span>
<span id="cb2-11"><a href="#cb2-11"></a>            <span class="kw">mut</span> t3<span class="op">,</span> <span class="kw">mut</span> t4<span class="op">,</span> <span class="kw">mut</span> t5<span class="op">,</span></span>
<span id="cb2-12"><a href="#cb2-12"></a>            <span class="kw">mut</span> t6<span class="op">,</span> <span class="kw">mut</span> t7<span class="op">,</span> <span class="kw">mut</span> t8</span>
<span id="cb2-13"><a href="#cb2-13"></a>        )<span class="op">:</span> (</span>
<span id="cb2-14"><a href="#cb2-14"></a>            <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span></span>
<span id="cb2-15"><a href="#cb2-15"></a>            <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span></span>
<span id="cb2-16"><a href="#cb2-16"></a>            <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span></span>
<span id="cb2-17"><a href="#cb2-17"></a>        )<span class="op">;</span></span>
<span id="cb2-18"><a href="#cb2-18"></a>        <span class="kw">let</span> t9<span class="op">:</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-19"><a href="#cb2-19"></a></span>
<span id="cb2-20"><a href="#cb2-20"></a>        <span class="kw">let</span> <span class="kw">mut</span> t<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-21"><a href="#cb2-21"></a>        <span class="kw">let</span> <span class="kw">mut</span> c<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-22"><a href="#cb2-22"></a></span>
<span id="cb2-23"><a href="#cb2-23"></a>        t <span class="op">=</span> a0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b0 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-24"><a href="#cb2-24"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-25"><a href="#cb2-25"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-26"><a href="#cb2-26"></a>        t <span class="op">+=</span> a0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-27"><a href="#cb2-27"></a>            a1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b0 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-28"><a href="#cb2-28"></a>        t1 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-29"><a href="#cb2-29"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-30"><a href="#cb2-30"></a>        t <span class="op">+=</span> a0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-31"><a href="#cb2-31"></a>            a1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-32"><a href="#cb2-32"></a>            a2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b0 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-33"><a href="#cb2-33"></a>        t2 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-34"><a href="#cb2-34"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-35"><a href="#cb2-35"></a>        t <span class="op">+=</span> a0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-36"><a href="#cb2-36"></a>            a1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-37"><a href="#cb2-37"></a>            a2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-38"><a href="#cb2-38"></a>            a3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b0 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-39"><a href="#cb2-39"></a>        t3 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-40"><a href="#cb2-40"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-41"><a href="#cb2-41"></a>        t <span class="op">+=</span> a0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-42"><a href="#cb2-42"></a>            a1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-43"><a href="#cb2-43"></a>            a2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-44"><a href="#cb2-44"></a>            a3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-45"><a href="#cb2-45"></a>            a4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b0 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-46"><a href="#cb2-46"></a>        t4 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-47"><a href="#cb2-47"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-48"><a href="#cb2-48"></a>        c <span class="op">=</span> t4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">&gt;&gt;</span> <span class="dv">48</span><span class="op">;</span></span>
<span id="cb2-49"><a href="#cb2-49"></a>        t4 <span class="op">&amp;=</span> M48 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-50"><a href="#cb2-50"></a></span>
<span id="cb2-51"><a href="#cb2-51"></a>        t <span class="op">+=</span> a1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-52"><a href="#cb2-52"></a>            a2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-53"><a href="#cb2-53"></a>            a3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-54"><a href="#cb2-54"></a>            a4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-55"><a href="#cb2-55"></a>        t5 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-56"><a href="#cb2-56"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-57"><a href="#cb2-57"></a>        t5 <span class="op">=</span> t5 <span class="op">&lt;&lt;</span> <span class="dv">4</span> <span class="op">|</span> c <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-58"><a href="#cb2-58"></a>        c <span class="op">=</span> t5 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-59"><a href="#cb2-59"></a>        t5 <span class="op">&amp;=</span> M52 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-60"><a href="#cb2-60"></a></span>
<span id="cb2-61"><a href="#cb2-61"></a>        t <span class="op">+=</span> a2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-62"><a href="#cb2-62"></a>            a3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-63"><a href="#cb2-63"></a>            a4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b2 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-64"><a href="#cb2-64"></a>        t6 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-65"><a href="#cb2-65"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-66"><a href="#cb2-66"></a>        t6 <span class="op">=</span> t6 <span class="op">&lt;&lt;</span> <span class="dv">4</span> <span class="op">|</span> c <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-67"><a href="#cb2-67"></a>        c <span class="op">=</span> t6 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-68"><a href="#cb2-68"></a>        t6 <span class="op">&amp;=</span> M52 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-69"><a href="#cb2-69"></a>        </span>
<span id="cb2-70"><a href="#cb2-70"></a>        t <span class="op">+=</span> a3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span></span>
<span id="cb2-71"><a href="#cb2-71"></a>            a4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-72"><a href="#cb2-72"></a>        t7 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-73"><a href="#cb2-73"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-74"><a href="#cb2-74"></a>        t7 <span class="op">=</span> t7 <span class="op">&lt;&lt;</span> <span class="dv">4</span> <span class="op">|</span> c <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-75"><a href="#cb2-75"></a>        c <span class="op">=</span> t7 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-76"><a href="#cb2-76"></a>        t7 <span class="op">&amp;=</span> M52 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-77"><a href="#cb2-77"></a></span>
<span id="cb2-78"><a href="#cb2-78"></a>        t <span class="op">+=</span> a4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> b4 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-79"><a href="#cb2-79"></a>        t8 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-80"><a href="#cb2-80"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-81"><a href="#cb2-81"></a>        t8 <span class="op">=</span> t8 <span class="op">&lt;&lt;</span> <span class="dv">4</span> <span class="op">|</span> c <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-82"><a href="#cb2-82"></a>        c <span class="op">=</span> t8 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-83"><a href="#cb2-83"></a>        t8 <span class="op">&amp;=</span> M52 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-84"><a href="#cb2-84"></a></span>
<span id="cb2-85"><a href="#cb2-85"></a>        t9 <span class="op">=</span> (t <span class="op">&lt;&lt;</span> <span class="dv">4</span> <span class="op">|</span> c) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-86"><a href="#cb2-86"></a></span>
<span id="cb2-87"><a href="#cb2-87"></a>        <span class="co">// 1st reduction R = R1 + R0 * P0</span></span>
<span id="cb2-88"><a href="#cb2-88"></a>        t <span class="op">=</span> t0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t5 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-89"><a href="#cb2-89"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-90"><a href="#cb2-90"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-91"><a href="#cb2-91"></a></span>
<span id="cb2-92"><a href="#cb2-92"></a>        t <span class="op">+=</span> t1 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t6 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-93"><a href="#cb2-93"></a>        t1 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-94"><a href="#cb2-94"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-95"><a href="#cb2-95"></a></span>
<span id="cb2-96"><a href="#cb2-96"></a>        t <span class="op">+=</span> t2 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t7 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-97"><a href="#cb2-97"></a>        t2 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-98"><a href="#cb2-98"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-99"><a href="#cb2-99"></a></span>
<span id="cb2-100"><a href="#cb2-100"></a>        t <span class="op">+=</span> t3 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t8 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-101"><a href="#cb2-101"></a>        t3 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-102"><a href="#cb2-102"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-103"><a href="#cb2-103"></a></span>
<span id="cb2-104"><a href="#cb2-104"></a>        t <span class="op">+=</span> t4 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t9 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-105"><a href="#cb2-105"></a>        t4 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-106"><a href="#cb2-106"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-107"><a href="#cb2-107"></a>        c <span class="op">=</span> (t4 <span class="op">&gt;&gt;</span> <span class="dv">48</span>) <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-108"><a href="#cb2-108"></a>        t4 <span class="op">&amp;=</span> M48 <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-109"><a href="#cb2-109"></a></span>
<span id="cb2-110"><a href="#cb2-110"></a>        c <span class="op">=</span> c <span class="op">|</span> t <span class="op">&lt;&lt;</span> <span class="dv">4</span><span class="op">;</span></span>
<span id="cb2-111"><a href="#cb2-111"></a></span>
<span id="cb2-112"><a href="#cb2-112"></a>        <span class="co">// 2nd pass</span></span>
<span id="cb2-113"><a href="#cb2-113"></a>        t <span class="op">=</span> t0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> c <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb2-114"><a href="#cb2-114"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-115"><a href="#cb2-115"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-116"><a href="#cb2-116"></a></span>
<span id="cb2-117"><a href="#cb2-117"></a>        t <span class="op">+=</span> t1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-118"><a href="#cb2-118"></a>        t1 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-119"><a href="#cb2-119"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-120"><a href="#cb2-120"></a></span>
<span id="cb2-121"><a href="#cb2-121"></a>        t <span class="op">+=</span> t2 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-122"><a href="#cb2-122"></a>        t2 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-123"><a href="#cb2-123"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-124"><a href="#cb2-124"></a></span>
<span id="cb2-125"><a href="#cb2-125"></a>        t <span class="op">+=</span> t3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-126"><a href="#cb2-126"></a>        t3 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-127"><a href="#cb2-127"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb2-128"><a href="#cb2-128"></a></span>
<span id="cb2-129"><a href="#cb2-129"></a>        t <span class="op">+=</span> t4 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb2-130"><a href="#cb2-130"></a>        t4 <span class="op">=</span> (t <span class="op">&amp;</span> M48) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb2-131"><a href="#cb2-131"></a></span>
<span id="cb2-132"><a href="#cb2-132"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> t3<span class="op">,</span> t4] <span class="op">}</span></span>
<span id="cb2-133"><a href="#cb2-133"></a>    <span class="op">}</span></span></code></pre></div>
<p>Finally let’s write a quick test:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb3-1"><a href="#cb3-1"></a>    <span class="at">#[</span>test<span class="at">]</span></span>
<span id="cb3-2"><a href="#cb3-2"></a>    <span class="kw">fn</span> it_multiply_field_elements() <span class="op">{</span></span>
<span id="cb3-3"><a href="#cb3-3"></a>        <span class="co">// A=0xfffffffffffffffffffffffffffffffffffffffffffffffffffffbfefffffc2f = p - 2^42</span></span>
<span id="cb3-4"><a href="#cb3-4"></a>        <span class="co">// B=0xfffffffffffffffffffffffffffffffffffffffffffffffffffff7fefffffc2f = p - 2^43</span></span>
<span id="cb3-5"><a href="#cb3-5"></a>        <span class="kw">let</span> a <span class="op">=</span> <span class="pp">Fe::</span>new(</span>
<span id="cb3-6"><a href="#cb3-6"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-7"><a href="#cb3-7"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-8"><a href="#cb3-8"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-9"><a href="#cb3-9"></a>            <span class="dv">0xfffffbfefffffc2fu64</span><span class="op">,</span></span>
<span id="cb3-10"><a href="#cb3-10"></a>        )<span class="op">;</span></span>
<span id="cb3-11"><a href="#cb3-11"></a>        <span class="kw">let</span> b <span class="op">=</span> <span class="pp">Fe::</span>new(</span>
<span id="cb3-12"><a href="#cb3-12"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-13"><a href="#cb3-13"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-14"><a href="#cb3-14"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb3-15"><a href="#cb3-15"></a>            <span class="dv">0xfffff7fefffffc2fu64</span><span class="op">,</span></span>
<span id="cb3-16"><a href="#cb3-16"></a>        )<span class="op">;</span></span>
<span id="cb3-17"><a href="#cb3-17"></a>        <span class="kw">let</span> r <span class="op">=</span> a<span class="op">.</span>mul(<span class="op">&amp;</span>b)<span class="op">;</span></span>
<span id="cb3-18"><a href="#cb3-18"></a>        <span class="co">// r = ((p - 2^42) * (p - 2^43)) % p</span></span>
<span id="cb3-19"><a href="#cb3-19"></a>        <span class="kw">let</span> expected <span class="op">=</span> <span class="pp">Fe::</span>new(</span>
<span id="cb3-20"><a href="#cb3-20"></a>            <span class="dv">0x0000000000000000u64</span><span class="op">,</span></span>
<span id="cb3-21"><a href="#cb3-21"></a>            <span class="dv">0x0000000000000000u64</span><span class="op">,</span></span>
<span id="cb3-22"><a href="#cb3-22"></a>            <span class="dv">0x0000000000200000u64</span><span class="op">,</span></span>
<span id="cb3-23"><a href="#cb3-23"></a>            <span class="dv">0x0000000000000000u64</span><span class="op">,</span></span>
<span id="cb3-24"><a href="#cb3-24"></a>        )<span class="op">;</span></span>
<span id="cb3-25"><a href="#cb3-25"></a>        <span class="pp">assert_eq!</span>(r<span class="op">,</span> expected)<span class="op">;</span></span>
<span id="cb3-26"><a href="#cb3-26"></a>    <span class="op">}</span></span></code></pre></div>
<p>and we now have a multiplication.</p>
<h1 id="just-like-that">JUST LIKE THAT</h1>
<p><img src="/img/crypto/modular-multiplication/booya.gif"></p>
<p>Verbose, but it works.</p>
<h3 id="optimizations">Optimizations</h3>
<h4 id="carry-saver">Carry-saver</h4>
<p>Okay, now let’s just think about it, we actually don’t have to propate the carry after the 2nd pass. Same as the addition, we can just use our carry-save storage.</p>
<p>A very simple way to prove it is to work with the worst case scenario: <span class="math inline">(<em>P</em>−1)(<em>P</em>−1)</span></p>
<p>After the 1st pass, it gives us a result <span class="math inline"><em>R</em>0 = 1000003d0fffffffffffffffffffffffffffffffffffffffffffffffefffff85dfff16f60<sub>16</sub></span> Or <span class="math inline"><em>R</em>0 = 2<sup>256</sup> × 1000003d0<sub>16</sub> + fffffffffffffffffffffffffffffffffffffffffffffffefffff85dfff16f60<sub>16</sub></span></p>
<p>Now applying the 2nd pass: </p>
<p><span class="math inline">1000003d0<sub>16</sub> × (2<sup>32</sup>+977)</span> is 65 bits so our first digit needs 66 bits. We only have 12 extra bits on the 1st digit <span class="math inline"><em>d</em><sub>0</sub></span>, so we need to propagate the carry on the 2nd digit <span class="math inline"><em>d</em><sub>1</sub></span></p>
<p>And we don’t have to go further for now, we can still work with few operations before we overflow our 320 bits storage.</p>
<p>Let’s remove the last carry propagation on t3 and t4:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1"></a>        <span class="co">// ...</span></span>
<span id="cb4-2"><a href="#cb4-2"></a>        <span class="co">// 2nd pass</span></span>
<span id="cb4-3"><a href="#cb4-3"></a>        t <span class="op">=</span> t0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> c <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb4-4"><a href="#cb4-4"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-5"><a href="#cb4-5"></a>        t <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb4-6"><a href="#cb4-6"></a></span>
<span id="cb4-7"><a href="#cb4-7"></a>        t <span class="op">+=</span> t1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-8"><a href="#cb4-8"></a>        t1 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-9"><a href="#cb4-9"></a></span>
<span id="cb4-10"><a href="#cb4-10"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> t3<span class="op">,</span> t4] <span class="op">}</span></span></code></pre></div>
<h4 id="combine-operations">Combine operations</h4>
<p>Another optimization would be to combine the operations on <span class="math inline"><em>t</em><sub>0</sub></span>, we have:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>        <span class="co">// 1st pass</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>        t <span class="op">=</span> t0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> t5 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>        <span class="co">// ...</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>        <span class="co">// 2nd pass</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>        t <span class="op">=</span> t0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> c <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>        t0 <span class="op">=</span> (t <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span></code></pre></div>
<p>Could be nice to combine those just so we save some instructions, better to do <span class="math inline"><em>t</em><sub>0</sub> + (<em>a</em>+<em>b</em>)<em>P</em><sub>0</sub></span> than <span class="math inline"><em>t</em><sub>0</sub> + <em>a</em><em>P</em><sub>0</sub> + <em>b</em><em>P</em><sub>0</sub></span></p>
<p>We can use the fact that we don’t need to propagate the carry over <span class="math inline"><em>t</em><sub>3</sub></span> and <span class="math inline"><em>t</em><sub>4</sub></span> on the 2nd pass. Therefore, we can work with them first.</p>
<h4 id="final-version">Final version</h4>
<div class="sourceCode" id="cb6"><pre class="sourceCode rust numerLines"><code class="sourceCode rust"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> mul(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>        <span class="kw">const</span> M52<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x000fffffffffffffu128</span><span class="op">;</span> <span class="co">// 2^52 - 1</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>        <span class="kw">const</span> M48<span class="op">:</span> <span class="dt">u64</span> <span class="op">=</span> <span class="dv">0x0000ffffffffffffu64</span><span class="op">;</span> <span class="co">// 2^48 - 1</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>        <span class="kw">const</span> P0<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x1000003d1u128</span><span class="op">;</span> <span class="co">// 2^32 + 977</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a>        <span class="kw">const</span> P1<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="dv">0x1000003d10u128</span><span class="op">;</span> <span class="co">// 2^32 + 977 &lt;&lt; 4</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> (a0<span class="op">,</span> a1<span class="op">,</span> a2<span class="op">,</span> a3<span class="op">,</span> a4) <span class="op">=</span> (</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a>            <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a>            <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">4</span>] <span class="kw">as</span> <span class="dt">u128</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>        )<span class="op">;</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> (b0<span class="op">,</span> b1<span class="op">,</span> b2<span class="op">,</span> b3<span class="op">,</span> b4) <span class="op">=</span> (</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a>            b<span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> b<span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> b<span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span></span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a>            b<span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">,</span> b<span class="op">.</span>d[<span class="dv">4</span>] <span class="kw">as</span> <span class="dt">u128</span></span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a>        )<span class="op">;</span></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> <span class="kw">mut</span> tx<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> <span class="kw">mut</span> cx<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> (t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> <span class="kw">mut</span> t3<span class="op">,</span> <span class="kw">mut</span> t4<span class="op">,</span> <span class="kw">mut</span> t5)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u128</span>)<span class="op">;</span></span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a>        <span class="kw">let</span> c4<span class="op">:</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t3</span></span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">=</span> a0 <span class="op">*</span> b3 <span class="op">+</span> a1 <span class="op">*</span> b2 <span class="op">+</span> a2 <span class="op">*</span> b1 <span class="op">+</span> a3 <span class="op">*</span> b0<span class="op">;</span></span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t8</span></span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">=</span> a4 <span class="op">*</span> b4<span class="op">;</span></span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t3 + t8 * P1</span></span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> (cx <span class="op">&amp;</span> M52) <span class="op">*</span> P1<span class="op">;</span></span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a>        t3 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t4</span></span>
<span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> a0 <span class="op">*</span> b4 <span class="op">+</span> a1 <span class="op">*</span> b3 <span class="op">+</span> a2 <span class="op">*</span> b2 <span class="op">+</span> a3 <span class="op">*</span> b1 <span class="op">+</span> a4 <span class="op">*</span> b0<span class="op">;</span></span>
<span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a>        <span class="co">// (c3 + t4) + (c8 + t9) * P1</span></span>
<span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> cx <span class="op">*</span> P1<span class="op">;</span></span>
<span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a>        t4 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a>        c4 <span class="op">=</span> t4 <span class="op">&gt;&gt;</span> <span class="dv">48</span><span class="op">;</span></span>
<span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a>        t4 <span class="op">&amp;=</span> M48<span class="op">;</span></span>
<span id="cb6-38"><a href="#cb6-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-39"><a href="#cb6-39" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t5</span></span>
<span id="cb6-40"><a href="#cb6-40" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">=</span> tx <span class="op">+</span> a1 <span class="op">*</span> b4 <span class="op">+</span> a2 <span class="op">*</span> b3 <span class="op">+</span> a3 <span class="op">*</span> b2 <span class="op">+</span> a4 <span class="op">*</span> b1<span class="op">;</span></span>
<span id="cb6-41"><a href="#cb6-41" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t0</span></span>
<span id="cb6-42"><a href="#cb6-42" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">=</span> a0 <span class="op">*</span> b0<span class="op">;</span></span>
<span id="cb6-43"><a href="#cb6-43" aria-hidden="true" tabindex="-1"></a>        t5 <span class="op">=</span> cx <span class="op">&amp;</span> M52<span class="op">;</span></span>
<span id="cb6-44"><a href="#cb6-44" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-45"><a href="#cb6-45" aria-hidden="true" tabindex="-1"></a>        t5 <span class="op">=</span> (t5 <span class="op">&lt;&lt;</span> <span class="dv">4</span>) <span class="op">|</span> c4 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb6-46"><a href="#cb6-46" aria-hidden="true" tabindex="-1"></a>        <span class="co">// c9 + t0 + (c4 + t5) * P0</span></span>
<span id="cb6-47"><a href="#cb6-47" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> t5 <span class="op">*</span> P0<span class="op">;</span></span>
<span id="cb6-48"><a href="#cb6-48" aria-hidden="true" tabindex="-1"></a>        t0 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-49"><a href="#cb6-49" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-50"><a href="#cb6-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-51"><a href="#cb6-51" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t1</span></span>
<span id="cb6-52"><a href="#cb6-52" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> a0 <span class="op">*</span> b1 <span class="op">+</span> a1 <span class="op">*</span> b0<span class="op">;</span></span>
<span id="cb6-53"><a href="#cb6-53" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t6</span></span>
<span id="cb6-54"><a href="#cb6-54" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">+=</span> a2 <span class="op">*</span> b4 <span class="op">+</span> a3 <span class="op">*</span> b3 <span class="op">+</span> a4 <span class="op">*</span> b2<span class="op">;</span></span>
<span id="cb6-55"><a href="#cb6-55" aria-hidden="true" tabindex="-1"></a>        <span class="co">// c0 + t1 + (c5 + t6) * P1</span></span>
<span id="cb6-56"><a href="#cb6-56" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> (cx <span class="op">&amp;</span> M52) <span class="op">*</span> P1<span class="op">;</span></span>
<span id="cb6-57"><a href="#cb6-57" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-58"><a href="#cb6-58" aria-hidden="true" tabindex="-1"></a>        t1 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-59"><a href="#cb6-59" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-60"><a href="#cb6-60" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-61"><a href="#cb6-61" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t2</span></span>
<span id="cb6-62"><a href="#cb6-62" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> a0 <span class="op">*</span> b2 <span class="op">+</span> a1 <span class="op">*</span> b1 <span class="op">+</span> a2 <span class="op">*</span> b0<span class="op">;</span></span>
<span id="cb6-63"><a href="#cb6-63" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t7</span></span>
<span id="cb6-64"><a href="#cb6-64" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">+=</span> a3 <span class="op">*</span> b4 <span class="op">+</span> a4 <span class="op">*</span> b3<span class="op">;</span></span>
<span id="cb6-65"><a href="#cb6-65" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t2 + t7 * P1</span></span>
<span id="cb6-66"><a href="#cb6-66" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> (cx <span class="op">&amp;</span> M52) <span class="op">*</span> P1<span class="op">;</span></span>
<span id="cb6-67"><a href="#cb6-67" aria-hidden="true" tabindex="-1"></a>        cx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-68"><a href="#cb6-68" aria-hidden="true" tabindex="-1"></a>        t2 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-69"><a href="#cb6-69" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-70"><a href="#cb6-70" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-71"><a href="#cb6-71" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t23</span></span>
<span id="cb6-72"><a href="#cb6-72" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> cx <span class="op">*</span> P1 <span class="op">+</span> t3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb6-73"><a href="#cb6-73" aria-hidden="true" tabindex="-1"></a>        t3 <span class="op">=</span> (tx <span class="op">&amp;</span> M52) <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-74"><a href="#cb6-74" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">&gt;&gt;=</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb6-75"><a href="#cb6-75" aria-hidden="true" tabindex="-1"></a>        <span class="co">// t24</span></span>
<span id="cb6-76"><a href="#cb6-76" aria-hidden="true" tabindex="-1"></a>        tx <span class="op">+=</span> t4 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb6-77"><a href="#cb6-77" aria-hidden="true" tabindex="-1"></a>        t4 <span class="op">=</span> tx <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb6-78"><a href="#cb6-78" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-79"><a href="#cb6-79" aria-hidden="true" tabindex="-1"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> t3<span class="op">,</span> t4] <span class="op">}</span></span>
<span id="cb6-80"><a href="#cb6-80" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span></code></pre></div>
]]></description>
  <link>https://shaoner.com/2021/07/26/field-element-multiplication.html</link>
  <guid>/2021/07/26/field-element-multiplication.html</guid>
  <pubDate>Mon, 26 Jul 2021 00:00:00 +0000</pubDate>
</item>


<item>
  <title>Field element addition with big numbers in Rust</title>
  <description><![CDATA[<h2 id="introduction">Introduction</h2>
<p>As I started to get interested in cryptography, I quickly tried to implement private key signing and verification.</p>
<p>One of the fundamentals is to use <a href="https://en.wikipedia.org/wiki/Finite_field_arithmetic">finite fields</a> with a big prime P as the order of the field.</p>
<p>As I read mostly about <a href="https://www.secg.org/sec2-v2.pdf">secp256k1</a>, I’ll deal with:</p>
<p><span class="math inline"><em>P</em> = 2<sup>256</sup> − 2<sup>32</sup> − 2<sup>9</sup> − 2<sup>8</sup> − 2<sup>7</sup> − 2<sup>6</sup> − 2<sup>4</sup> − 1</span></p>
<p>usually shortened as:</p>
<p><span class="math inline"><em>P</em> = 2<sup>256</sup> − 2<sup>32</sup> − 977</span></p>
<p>Making a poc in python is quite easy:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a>P <span class="op">=</span> <span class="dv">2</span><span class="op">**</span><span class="dv">256</span> <span class="op">-</span> <span class="dv">2</span><span class="op">**</span><span class="dv">32</span> <span class="op">-</span> <span class="dv">977</span></span>
<span id="cb1-2"><a href="#cb1-2"></a></span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="kw">class</span> Fe:</span>
<span id="cb1-4"><a href="#cb1-4"></a>    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, val):</span>
<span id="cb1-5"><a href="#cb1-5"></a>        <span class="va">self</span>.val <span class="op">=</span> val</span>
<span id="cb1-6"><a href="#cb1-6"></a></span>
<span id="cb1-7"><a href="#cb1-7"></a>    <span class="kw">def</span> <span class="fu">__add__</span>(<span class="va">self</span>, b):</span>
<span id="cb1-8"><a href="#cb1-8"></a>        <span class="cf">return</span> <span class="va">self</span>.__class__((<span class="va">self</span>.val <span class="op">+</span> b.val) <span class="op">%</span> P)</span>
<span id="cb1-9"><a href="#cb1-9"></a></span>
<span id="cb1-10"><a href="#cb1-10"></a>    <span class="kw">def</span> <span class="fu">__mul__</span>(<span class="va">self</span>, b):</span>
<span id="cb1-11"><a href="#cb1-11"></a>        <span class="cf">return</span> <span class="va">self</span>.__class__((<span class="va">self</span>.val <span class="op">*</span> b.val) <span class="op">%</span> P)</span></code></pre></div>
<p>Here <code>self.val</code>, <code>b</code> and <code>P</code> are supposedly close to 2<sup>256</sup> which is fine because Python already supports integers of arbitrary size. Python actually <em>mallocs</em> an array of <code>uint32_t</code> and splits the original number in power of 2<sup>30</sup> when dealing with big numbers.</p>
<h2 id="field-element-representation-in-rust">Field element representation in Rust</h2>
<h3 id="the-common-approach">The common approach</h3>
<p>In C or Rust, we could imagine doing the same thing aka <span class="math inline">(<em>a</em>+<em>b</em>) mod  <em>P</em></span> but then you’d have to find an integer type that fits at least 256 bits.</p>
<p>Is that enough?</p>
<p>Not really, if you add two u256 numbers, the maximum result is <span class="math inline">2<sup>256</sup> − 1 + 2<sup>256</sup> − 1</span>, i.e. <span class="math inline">2<sup>257</sup> − 2</span> so we need to handle an extra carry bit.</p>
<p>If we want to multiply two u256 numbers, the maximum result is <span class="math inline">(2<sup>256</sup>−1)(2<sup>256</sup>−1)</span> i.e. <span class="math inline">2<sup>512</sup> − 2<sup>257</sup> + 1</span> so now we also need a u512 type.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb2-1"><a href="#cb2-1"></a><span class="co">// after implementing u256 and u512</span></span>
<span id="cb2-2"><a href="#cb2-2"></a></span>
<span id="cb2-3"><a href="#cb2-3"></a><span class="kw">const</span> P<span class="op">:</span> u256 <span class="op">=</span> (<span class="dv">2</span>u512<span class="op">.</span>pow(<span class="dv">256</span>) <span class="op">-</span> <span class="dv">0</span>x100000000u256 <span class="op">-</span> <span class="dv">0</span>x3d1u256) <span class="kw">as</span> u256<span class="op">;</span></span>
<span id="cb2-4"><a href="#cb2-4"></a></span>
<span id="cb2-5"><a href="#cb2-5"></a><span class="kw">struct</span> Fe <span class="op">{</span></span>
<span id="cb2-6"><a href="#cb2-6"></a>    val<span class="op">:</span> u256</span>
<span id="cb2-7"><a href="#cb2-7"></a><span class="op">}</span></span>
<span id="cb2-8"><a href="#cb2-8"></a></span>
<span id="cb2-9"><a href="#cb2-9"></a><span class="kw">impl</span> Fe <span class="op">{</span></span>
<span id="cb2-10"><a href="#cb2-10"></a>    <span class="kw">fn</span> new(val<span class="op">:</span> u256) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb2-11"><a href="#cb2-11"></a>        <span class="dt">Self</span> <span class="op">{</span> val <span class="op">}</span></span>
<span id="cb2-12"><a href="#cb2-12"></a>    <span class="op">}</span></span>
<span id="cb2-13"><a href="#cb2-13"></a></span>
<span id="cb2-14"><a href="#cb2-14"></a>    <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb2-15"><a href="#cb2-15"></a>        <span class="kw">let</span> tmp<span class="op">:</span> u512 <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>val <span class="kw">as</span> u512 <span class="op">+</span> b<span class="op">.</span>val <span class="kw">as</span> u512) <span class="op">%</span> (P <span class="kw">as</span> u512)<span class="op">;</span></span>
<span id="cb2-16"><a href="#cb2-16"></a></span>
<span id="cb2-17"><a href="#cb2-17"></a>        <span class="dt">Self</span> <span class="op">{</span> val<span class="op">:</span> tmp <span class="kw">as</span> u256 <span class="op">}</span></span>
<span id="cb2-18"><a href="#cb2-18"></a>    <span class="op">}</span></span>
<span id="cb2-19"><a href="#cb2-19"></a></span>
<span id="cb2-20"><a href="#cb2-20"></a>    <span class="kw">fn</span> mul(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb2-21"><a href="#cb2-21"></a>        <span class="kw">let</span> tmp<span class="op">:</span> u512 <span class="op">=</span> (a <span class="kw">as</span> u512 <span class="op">*</span> b<span class="op">.</span>val <span class="kw">as</span> u512) <span class="op">%</span> (P <span class="kw">as</span> u512)<span class="op">;</span></span>
<span id="cb2-22"><a href="#cb2-22"></a></span>
<span id="cb2-23"><a href="#cb2-23"></a>        <span class="dt">Self</span> <span class="op">{</span> val<span class="op">:</span> tmp <span class="kw">as</span> u256 <span class="op">}</span></span>
<span id="cb2-24"><a href="#cb2-24"></a>    <span class="op">}</span></span>
<span id="cb2-25"><a href="#cb2-25"></a><span class="op">}</span></span></code></pre></div>
<ol type="1">
<li>We had to create 2 new integer types with one serving only as a temporary carry storage</li>
<li>There is a lot of waste for the addition as it needs at max 257 bits, meaning meaning 255 bits are just zeros and most generally all the explicit casts waste memory.</li>
</ol>
<h3 id="integer-representation">Integer representation</h3>
<p>We haven’t talked yet about how to represent a u256, but since Rust supports u128, u64, u32, u16 and u8, we could use an array of “digits” to represent our number, just like Python does.</p>
<p>As we want to be efficient and limit the number of operations, 2 x u128 could be a good choice except we would still need to handle a carry when adding or multiplying the high or low digits of 2 u256.</p>
<p>For now, a better choice is to actually use 4 x u64 as it limits the number of instructions and we can use u128 as a temporary storage.</p>
<p>Finally, we would benefit from the x86_64 <a href="https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf#G5.88727">imul instruction</a> instruction that can do multiplication of 2 u64 as <code>64x64 -&gt; 128</code>.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">pub</span> <span class="kw">struct</span> Fe <span class="op">{</span></span>
<span id="cb3-2"><a href="#cb3-2"></a>    d<span class="op">:</span> [<span class="dt">u64</span><span class="op">;</span> <span class="dv">4</span>]</span>
<span id="cb3-3"><a href="#cb3-3"></a><span class="op">}</span></span>
<span id="cb3-4"><a href="#cb3-4"></a></span>
<span id="cb3-5"><a href="#cb3-5"></a><span class="kw">impl</span> Fe <span class="op">{</span></span>
<span id="cb3-6"><a href="#cb3-6"></a>    <span class="kw">pub</span> <span class="kw">fn</span> new(d3<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d2<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d1<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d0<span class="op">:</span> <span class="dt">u64</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb3-7"><a href="#cb3-7"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3] <span class="op">}</span></span>
<span id="cb3-8"><a href="#cb3-8"></a>    <span class="op">}</span></span>
<span id="cb3-9"><a href="#cb3-9"></a><span class="op">}</span></span></code></pre></div>
<h3 id="addition-v1">Addition v1</h3>
<p>Ok, let’s now define an <code>add</code> method (I’m not bringing additional traits yet):</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb4-1"><a href="#cb4-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb4-2"><a href="#cb4-2"></a>        <span class="kw">let</span> (d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb4-3"><a href="#cb4-3"></a>        <span class="kw">let</span> <span class="kw">mut</span> tmp<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-4"><a href="#cb4-4"></a></span>
<span id="cb4-5"><a href="#cb4-5"></a>        tmp <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-6"><a href="#cb4-6"></a>        d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-7"><a href="#cb4-7"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb4-8"><a href="#cb4-8"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-9"><a href="#cb4-9"></a>        d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-10"><a href="#cb4-10"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb4-11"><a href="#cb4-11"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-12"><a href="#cb4-12"></a>        d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-13"><a href="#cb4-13"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb4-14"><a href="#cb4-14"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb4-15"><a href="#cb4-15"></a>        d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb4-16"><a href="#cb4-16"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb4-17"><a href="#cb4-17"></a>        <span class="co">// tmp here holds the final carry</span></span>
<span id="cb4-18"><a href="#cb4-18"></a></span>
<span id="cb4-19"><a href="#cb4-19"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3] <span class="op">}</span></span>
<span id="cb4-20"><a href="#cb4-20"></a>    <span class="op">}</span></span></code></pre></div>
<p>Ok, so far we can make an addition of two u256, but we haven’t dealt with the modulo part or the final carry.</p>
<p>Actually we can define the result of <span class="math inline"><em>A</em> + <em>B</em></span> as:</p>
<p><span class="math inline"><em>A</em> + <em>B</em> = <em>R</em><sub>1</sub> ⋅ 2<sup>256</sup> + <em>R</em><sub>0</sub></span></p>
<p>with: <span class="math inline"><em>R</em><sub>0</sub> &lt; 2<sup>256</sup></span> and <span class="math inline"><em>R</em><sub>1</sub> ∈ (0,1)</span></p>
<p><em>Note: It helps to pick this form since our storage is 256 bits.</em></p>
<p>This gives us 3 cases:</p>
<h4 id="r_1-0-r_0-p-i.e.-r-p"><span class="math inline"><em>R</em><sub>1</sub> = 0, <em>R</em><sub>0</sub> &lt; <em>P</em></span> (i.e. <span class="math inline"><em>R</em> &lt; <em>P</em></span>)</h4>
<p>The result is already less than <span class="math inline"><em>P</em></span> so nothing to do:</p>
<p></p>
<h4 id="r_1-0-r_0-ge-p-i.e.-p-le-r-2256"><span class="math inline"><em>R</em><sub>1</sub> = 0, <em>R</em><sub>0</sub> ≥ <em>P</em></span> (i.e. <span class="math inline"><em>P</em> ≤ <em>R</em> &lt; 2<sup>256</sup></span>)</h4>
<p>Here the result hasn’t overflowed 2<sup>256</sup>.</p>
<p></p>
<p>It’s easy to prove:</p>
<p></p>
<h4 id="r_1-1-i.e.-r-ge-2256"><span class="math inline"><em>R</em><sub>1</sub> = 1</span> (i.e. <span class="math inline"><em>R</em> ≥ 2<sup>256</sup></span>)</h4>
<p>Here the result has overflowed. In this case:</p>
<p></p>
<p>And here is the proof:</p>
<p></p>
<p>yet:</p>
<p></p>
<p>And from \eqref{eq:1}</p>
<p><span class="math inline">2<sup>256</sup> mod  <em>P</em> = 2<sup>256</sup> − <em>P</em></span></p>
<p>so: </p>
<h3 id="addition-v2">Addition v2</h3>
<p>Let’s add the modulo part in the addition:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb5-1"><a href="#cb5-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb5-2"><a href="#cb5-2"></a>        <span class="kw">let</span> (<span class="kw">mut</span> d0<span class="op">,</span> <span class="kw">mut</span> d1<span class="op">,</span> <span class="kw">mut</span> d2<span class="op">,</span> <span class="kw">mut</span> d3)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb5-3"><a href="#cb5-3"></a>        <span class="kw">let</span> <span class="kw">mut</span> tmp<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-4"><a href="#cb5-4"></a></span>
<span id="cb5-5"><a href="#cb5-5"></a>        tmp <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-6"><a href="#cb5-6"></a>        d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-7"><a href="#cb5-7"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-8"><a href="#cb5-8"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-9"><a href="#cb5-9"></a>        d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-10"><a href="#cb5-10"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-11"><a href="#cb5-11"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-12"><a href="#cb5-12"></a>        d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-13"><a href="#cb5-13"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-14"><a href="#cb5-14"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-15"><a href="#cb5-15"></a>        d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-16"><a href="#cb5-16"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-17"><a href="#cb5-17"></a></span>
<span id="cb5-18"><a href="#cb5-18"></a>        <span class="co">// N = [d0, d1, d2, d3]</span></span>
<span id="cb5-19"><a href="#cb5-19"></a>        <span class="co">// tmp is the extra carry</span></span>
<span id="cb5-20"><a href="#cb5-20"></a>        <span class="kw">if</span> tmp <span class="op">&gt;</span> <span class="dv">0</span> <span class="op">{</span></span>
<span id="cb5-21"><a href="#cb5-21"></a>            <span class="co">// R = N + 2**32 + 977</span></span>
<span id="cb5-22"><a href="#cb5-22"></a>            tmp <span class="op">=</span> d0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> <span class="dv">0x1000003d1u128</span><span class="op">;</span></span>
<span id="cb5-23"><a href="#cb5-23"></a>            d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-24"><a href="#cb5-24"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-25"><a href="#cb5-25"></a>            tmp <span class="op">+=</span> d1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-26"><a href="#cb5-26"></a>            d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-27"><a href="#cb5-27"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-28"><a href="#cb5-28"></a>            tmp <span class="op">+=</span> d2 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-29"><a href="#cb5-29"></a>            d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-30"><a href="#cb5-30"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb5-31"><a href="#cb5-31"></a>            tmp <span class="op">+=</span> d3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb5-32"><a href="#cb5-32"></a>            d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb5-33"><a href="#cb5-33"></a>        <span class="op">}</span> <span class="kw">else</span> <span class="kw">if</span> d3 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb5-34"><a href="#cb5-34"></a>                  d2 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb5-35"><a href="#cb5-35"></a>                  d1 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb5-36"><a href="#cb5-36"></a>                  d0 <span class="op">&gt;=</span> <span class="dv">0xfffffffefffffc2fu64</span> <span class="op">{</span></span>
<span id="cb5-37"><a href="#cb5-37"></a>            <span class="co">// P &lt;= R &lt; 2^256</span></span>
<span id="cb5-38"><a href="#cb5-38"></a>            <span class="co">// R -= P</span></span>
<span id="cb5-39"><a href="#cb5-39"></a>            d0 <span class="op">-=</span> <span class="dv">0xfffffffefffffc2fu64</span><span class="op">;</span></span>
<span id="cb5-40"><a href="#cb5-40"></a>            d1 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb5-41"><a href="#cb5-41"></a>            d2 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb5-42"><a href="#cb5-42"></a>            d3 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb5-43"><a href="#cb5-43"></a>        <span class="op">}</span></span>
<span id="cb5-44"><a href="#cb5-44"></a></span>
<span id="cb5-45"><a href="#cb5-45"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3] <span class="op">}</span></span>
<span id="cb5-46"><a href="#cb5-46"></a>    <span class="op">}</span></span></code></pre></div>
<h3 id="addition-v3">Addition v3</h3>
<p>Now that works well but usually we have to deal with multiple additions in a row so this is not super optimized. Suppose we have <span class="math inline">((((<em>a</em>+<em>b</em> mod  <em>P</em>)+<em>c</em>) mod  <em>P</em>)+<em>d</em>) mod  <em>P</em></span>, it would be more efficient to do <span class="math inline">(<em>a</em>+<em>b</em>+<em>c</em>+<em>d</em>) mod  <em>P</em></span></p>
<p>In order to do this, we’d have to store the carry for later.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb6-1"><a href="#cb6-1"></a><span class="kw">pub</span> <span class="kw">struct</span> Fe <span class="op">{</span></span>
<span id="cb6-2"><a href="#cb6-2"></a>    d<span class="op">:</span> [<span class="dt">u64</span><span class="op">;</span> <span class="dv">4</span>]<span class="op">,</span> <span class="co">// digits</span></span>
<span id="cb6-3"><a href="#cb6-3"></a>    c<span class="op">:</span> <span class="dt">u64</span> <span class="co">// carry</span></span>
<span id="cb6-4"><a href="#cb6-4"></a><span class="op">}</span></span></code></pre></div>
<p>With a 64 bits for the carry, it lets us do max 63 additions before we have to reduce the result, so let’s write a reduce function and the updated add version:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb7-1"><a href="#cb7-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb7-2"><a href="#cb7-2"></a>        <span class="kw">let</span> (d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb7-3"><a href="#cb7-3"></a>        <span class="kw">let</span> <span class="kw">mut</span> tmp<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-4"><a href="#cb7-4"></a></span>
<span id="cb7-5"><a href="#cb7-5"></a>        tmp <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">0</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-6"><a href="#cb7-6"></a>        d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-7"><a href="#cb7-7"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-8"><a href="#cb7-8"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">1</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-9"><a href="#cb7-9"></a>        d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-10"><a href="#cb7-10"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-11"><a href="#cb7-11"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">2</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-12"><a href="#cb7-12"></a>        d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-13"><a href="#cb7-13"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-14"><a href="#cb7-14"></a>        tmp <span class="op">+=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>] <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-15"><a href="#cb7-15"></a>        d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-16"><a href="#cb7-16"></a>        tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-17"><a href="#cb7-17"></a></span>
<span id="cb7-18"><a href="#cb7-18"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3]<span class="op">,</span> c<span class="op">:</span> <span class="kw">self</span><span class="op">.</span>c <span class="op">+</span> b<span class="op">.</span>c <span class="op">+</span> tmp <span class="kw">as</span> <span class="dt">u64</span> <span class="op">}</span></span>
<span id="cb7-19"><a href="#cb7-19"></a>    <span class="op">}</span></span>
<span id="cb7-20"><a href="#cb7-20"></a></span>
<span id="cb7-21"><a href="#cb7-21"></a>    <span class="kw">pub</span> <span class="kw">fn</span> reduce(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span>) <span class="op">{</span></span>
<span id="cb7-22"><a href="#cb7-22"></a>        <span class="kw">let</span> (<span class="kw">mut</span> d0<span class="op">,</span> <span class="kw">mut</span> d1<span class="op">,</span> <span class="kw">mut</span> d2<span class="op">,</span> <span class="kw">mut</span> d3) <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>])<span class="op">;</span></span>
<span id="cb7-23"><a href="#cb7-23"></a>        <span class="kw">let</span> <span class="kw">mut</span> c<span class="op">:</span> <span class="dt">u128</span> <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>c <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-24"><a href="#cb7-24"></a>        <span class="kw">let</span> <span class="kw">mut</span> tmp<span class="op">:</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-25"><a href="#cb7-25"></a></span>
<span id="cb7-26"><a href="#cb7-26"></a>        <span class="kw">if</span> c <span class="op">&gt;</span> <span class="dv">0</span> <span class="op">{</span></span>
<span id="cb7-27"><a href="#cb7-27"></a>            tmp <span class="op">=</span> d0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> c <span class="op">*</span> <span class="dv">0x1000003d1u128</span><span class="op">;</span></span>
<span id="cb7-28"><a href="#cb7-28"></a>            d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-29"><a href="#cb7-29"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-30"><a href="#cb7-30"></a>            tmp <span class="op">+=</span> d1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-31"><a href="#cb7-31"></a>            d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-32"><a href="#cb7-32"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-33"><a href="#cb7-33"></a>            tmp <span class="op">+=</span> d2 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-34"><a href="#cb7-34"></a>            d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-35"><a href="#cb7-35"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-36"><a href="#cb7-36"></a>            tmp <span class="op">+=</span> d3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-37"><a href="#cb7-37"></a>            d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-38"><a href="#cb7-38"></a>            tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-39"><a href="#cb7-39"></a>            c <span class="op">=</span> tmp<span class="op">;</span></span>
<span id="cb7-40"><a href="#cb7-40"></a>            <span class="kw">if</span> c <span class="op">&gt;</span> <span class="dv">0</span> <span class="op">{</span> <span class="co">// 2nd pass</span></span>
<span id="cb7-41"><a href="#cb7-41"></a>                tmp <span class="op">=</span> d0 <span class="kw">as</span> <span class="dt">u128</span> <span class="op">+</span> c <span class="op">*</span> <span class="dv">0x1000003d1u128</span><span class="op">;</span></span>
<span id="cb7-42"><a href="#cb7-42"></a>                d0 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-43"><a href="#cb7-43"></a>                tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-44"><a href="#cb7-44"></a>                tmp <span class="op">+=</span> d1 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-45"><a href="#cb7-45"></a>                d1 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-46"><a href="#cb7-46"></a>                tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-47"><a href="#cb7-47"></a>                tmp <span class="op">+=</span> d2 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-48"><a href="#cb7-48"></a>                d2 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-49"><a href="#cb7-49"></a>                tmp <span class="op">&gt;&gt;=</span> <span class="dv">64</span><span class="op">;</span></span>
<span id="cb7-50"><a href="#cb7-50"></a>                tmp <span class="op">+=</span> d3 <span class="kw">as</span> <span class="dt">u128</span><span class="op">;</span></span>
<span id="cb7-51"><a href="#cb7-51"></a>                d3 <span class="op">=</span> tmp <span class="kw">as</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb7-52"><a href="#cb7-52"></a>            <span class="op">}</span></span>
<span id="cb7-53"><a href="#cb7-53"></a>        <span class="op">}</span> <span class="kw">else</span> <span class="kw">if</span> d3 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb7-54"><a href="#cb7-54"></a>                  d2 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb7-55"><a href="#cb7-55"></a>                  d1 <span class="op">==</span> <span class="dv">0xffffffffffffffffu64</span> <span class="op">&amp;&amp;</span></span>
<span id="cb7-56"><a href="#cb7-56"></a>                  d0 <span class="op">&gt;=</span> <span class="dv">0xfffffffefffffc2fu64</span> <span class="op">{</span></span>
<span id="cb7-57"><a href="#cb7-57"></a>            d0 <span class="op">-=</span> <span class="dv">0xfffffffefffffc2fu64</span><span class="op">;</span></span>
<span id="cb7-58"><a href="#cb7-58"></a>            d1 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb7-59"><a href="#cb7-59"></a>            d2 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb7-60"><a href="#cb7-60"></a>            d3 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb7-61"><a href="#cb7-61"></a>        <span class="op">}</span></span>
<span id="cb7-62"><a href="#cb7-62"></a></span>
<span id="cb7-63"><a href="#cb7-63"></a>        <span class="kw">self</span><span class="op">.</span>d <span class="op">=</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3]<span class="op">;</span></span>
<span id="cb7-64"><a href="#cb7-64"></a>        <span class="kw">self</span><span class="op">.</span>c <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb7-65"><a href="#cb7-65"></a>    <span class="op">}</span></span></code></pre></div>
<p>Now we can make several additions, store the carries in c and finally reduce to P at the very end.</p>
<p>Let’s also write a test:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb8-1"><a href="#cb8-1"></a><span class="at">#[</span>cfg<span class="at">(</span>test<span class="at">)]</span></span>
<span id="cb8-2"><a href="#cb8-2"></a><span class="kw">mod</span> tests <span class="op">{</span></span>
<span id="cb8-3"><a href="#cb8-3"></a>    <span class="kw">use</span> <span class="kw">super</span><span class="pp">::</span><span class="op">*;</span></span>
<span id="cb8-4"><a href="#cb8-4"></a></span>
<span id="cb8-5"><a href="#cb8-5"></a>    <span class="at">#[</span>test<span class="at">]</span></span>
<span id="cb8-6"><a href="#cb8-6"></a>    <span class="kw">fn</span> it_adds_field_elements_carry() <span class="op">{</span></span>
<span id="cb8-7"><a href="#cb8-7"></a>        <span class="kw">let</span> a <span class="op">=</span> <span class="pp">Fe::</span>new(</span>
<span id="cb8-8"><a href="#cb8-8"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-9"><a href="#cb8-9"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-10"><a href="#cb8-10"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-11"><a href="#cb8-11"></a>            <span class="dv">0xfffffffefffffc2eu64</span><span class="op">,</span></span>
<span id="cb8-12"><a href="#cb8-12"></a>        )<span class="op">;</span> <span class="co">// P - 1</span></span>
<span id="cb8-13"><a href="#cb8-13"></a>        <span class="kw">let</span> b <span class="op">=</span> <span class="op">&amp;</span>a<span class="op">;</span></span>
<span id="cb8-14"><a href="#cb8-14"></a>        <span class="kw">let</span> r <span class="op">=</span> a<span class="op">.</span>add(b)<span class="op">;</span></span>
<span id="cb8-15"><a href="#cb8-15"></a>        <span class="kw">let</span> rb <span class="op">=</span> <span class="op">&amp;</span>r<span class="op">;</span></span>
<span id="cb8-16"><a href="#cb8-16"></a>        <span class="kw">let</span> <span class="kw">mut</span> r2 <span class="op">=</span> r<span class="op">.</span>add(rb)<span class="op">;</span></span>
<span id="cb8-17"><a href="#cb8-17"></a>        <span class="co">// r2 = ((p - 1 + p - 1) + (p - 1 + p - 1))</span></span>
<span id="cb8-18"><a href="#cb8-18"></a>        <span class="co">// we overflow p 3 times, so the carry should be 3</span></span>
<span id="cb8-19"><a href="#cb8-19"></a>        <span class="pp">assert_eq!</span>(r2<span class="op">.</span>c<span class="op">,</span> <span class="dv">3</span>)<span class="op">;</span></span>
<span id="cb8-20"><a href="#cb8-20"></a>        r2<span class="op">.</span>reduce()<span class="op">;</span></span>
<span id="cb8-21"><a href="#cb8-21"></a>        <span class="co">// r2 = ((p - 1 + p - 1) + (p - 1 + p - 1)) % p</span></span>
<span id="cb8-22"><a href="#cb8-22"></a>        <span class="kw">let</span> expected <span class="op">=</span> <span class="pp">Fe::</span>new(</span>
<span id="cb8-23"><a href="#cb8-23"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-24"><a href="#cb8-24"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-25"><a href="#cb8-25"></a>            <span class="dv">0xffffffffffffffffu64</span><span class="op">,</span></span>
<span id="cb8-26"><a href="#cb8-26"></a>            <span class="dv">0xfffffffefffffc2bu64</span><span class="op">,</span></span>
<span id="cb8-27"><a href="#cb8-27"></a>        )<span class="op">;</span></span>
<span id="cb8-28"><a href="#cb8-28"></a>        <span class="pp">assert_eq!</span>(r2<span class="op">,</span> expected)<span class="op">;</span></span>
<span id="cb8-29"><a href="#cb8-29"></a>    <span class="op">}</span></span></code></pre></div>
<h3 id="addition-v4">Addition v4</h3>
<p>Another optimization would be to use a carry save strategy. If we look at the addition, we propagate the carry on each digit. It’s actually possible to reduce the storage size of each digit in order to keep bits for the carry.</p>
<p>I actually discovered it by looking at the <a href="https://github.com/bitcoin/bitcoin/blob/master/src/secp256k1/src/field_5x52.h">bitcoin implementation</a> and by <a href="https://twitter.com/shaoner/status/1400208945277456400">asking the question</a> directly to Pieter Wuille, the author of the libsecp256k1 used in bitcoin.</p>
<h4 id="how-it-works">How it works</h4>
<p>Instead of using 4 x u64 + a u64 carry, we could use 4 x u52 + 1 x u48 to represent our number (physically stored as 5 x u64) while keeping extra bits for the carry.</p>
<p>Now we don’t have to propagate the carry anymore in the addition, it will be stored in the extra bits, as we have 12 extra bits for the 4 first digits and 16 bits for the last.</p>
<p>It allows us to rewrite the addition this way:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb9-1"><a href="#cb9-1"></a>    <span class="kw">pub</span> <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb9-2"><a href="#cb9-2"></a>        <span class="kw">let</span> (d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb9-3"><a href="#cb9-3"></a></span>
<span id="cb9-4"><a href="#cb9-4"></a>        d0 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">0</span>]<span class="op">;</span></span>
<span id="cb9-5"><a href="#cb9-5"></a>        d1 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">1</span>]<span class="op">;</span></span>
<span id="cb9-6"><a href="#cb9-6"></a>        d2 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">2</span>]<span class="op">;</span></span>
<span id="cb9-7"><a href="#cb9-7"></a>        d3 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>]<span class="op">;</span></span>
<span id="cb9-8"><a href="#cb9-8"></a>        d4 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>]<span class="op">;</span></span>
<span id="cb9-9"><a href="#cb9-9"></a></span>
<span id="cb9-10"><a href="#cb9-10"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4] <span class="op">}</span></span>
<span id="cb9-11"><a href="#cb9-11"></a>    <span class="op">}</span></span></code></pre></div>
<p>We now have a 5th digit but it’s pretty easy with less instructions. We can actually do 12 additions before one of the smallest carries overflows which is plenty enough.</p>
<p>Finally, we also have to rewrite our reduce function and optionally update our <code>new</code> method so it can easily transform 4 x u64 to 4 x u52 + 1 x u48, here is the final implementation:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode numberSource rust numberLines"><code class="sourceCode rust"><span id="cb10-1"><a href="#cb10-1"></a><span class="kw">impl</span> Fe <span class="op">{</span></span>
<span id="cb10-2"><a href="#cb10-2"></a>    <span class="kw">pub</span> <span class="kw">fn</span> new(d3<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d2<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d1<span class="op">:</span> <span class="dt">u64</span><span class="op">,</span> d0<span class="op">:</span> <span class="dt">u64</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb10-3"><a href="#cb10-3"></a>        <span class="kw">let</span> (t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> t3)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb10-4"><a href="#cb10-4"></a>        <span class="kw">let</span> t4<span class="op">:</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb10-5"><a href="#cb10-5"></a></span>
<span id="cb10-6"><a href="#cb10-6"></a>        t0 <span class="op">=</span> d0 <span class="op">&amp;</span> <span class="dv">0x000fffffffffffff</span><span class="op">;</span></span>
<span id="cb10-7"><a href="#cb10-7"></a>        t1 <span class="op">=</span> d0 <span class="op">&gt;&gt;</span> <span class="dv">52</span> <span class="op">|</span> (d1 <span class="op">&amp;</span> <span class="dv">0x000000ffffffffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">12</span><span class="op">;</span> <span class="co">// 12 + 40</span></span>
<span id="cb10-8"><a href="#cb10-8"></a>        t2 <span class="op">=</span> d1 <span class="op">&gt;&gt;</span> <span class="dv">40</span> <span class="op">|</span> (d2 <span class="op">&amp;</span> <span class="dv">0x000000000fffffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">24</span><span class="op">;</span> <span class="co">// 24 + 28</span></span>
<span id="cb10-9"><a href="#cb10-9"></a>        t3 <span class="op">=</span> d2 <span class="op">&gt;&gt;</span> <span class="dv">28</span> <span class="op">|</span> (d3 <span class="op">&amp;</span> <span class="dv">0x000000000000ffff</span>) <span class="op">&lt;&lt;</span> <span class="dv">36</span><span class="op">;</span> <span class="co">// 36 + 16</span></span>
<span id="cb10-10"><a href="#cb10-10"></a>        t4 <span class="op">=</span> d3 <span class="op">&gt;&gt;</span> <span class="dv">16</span><span class="op">;</span> <span class="co">// 48</span></span>
<span id="cb10-11"><a href="#cb10-11"></a></span>
<span id="cb10-12"><a href="#cb10-12"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [t0<span class="op">,</span> t1<span class="op">,</span> t2<span class="op">,</span> t3<span class="op">,</span> t4] <span class="op">}</span></span>
<span id="cb10-13"><a href="#cb10-13"></a>    <span class="op">}</span></span>
<span id="cb10-14"><a href="#cb10-14"></a></span>
<span id="cb10-15"><a href="#cb10-15"></a>    <span class="kw">pub</span> <span class="kw">fn</span> add(<span class="op">&amp;</span><span class="kw">self</span><span class="op">,</span> b<span class="op">:</span> <span class="op">&amp;</span><span class="dt">Self</span>) <span class="op">-&gt;</span> <span class="dt">Self</span> <span class="op">{</span></span>
<span id="cb10-16"><a href="#cb10-16"></a>        <span class="kw">let</span> (d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4)<span class="op">:</span> (<span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span><span class="op">,</span> <span class="dt">u64</span>)<span class="op">;</span></span>
<span id="cb10-17"><a href="#cb10-17"></a></span>
<span id="cb10-18"><a href="#cb10-18"></a>        d0 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">0</span>]<span class="op">;</span></span>
<span id="cb10-19"><a href="#cb10-19"></a>        d1 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">1</span>]<span class="op">;</span></span>
<span id="cb10-20"><a href="#cb10-20"></a>        d2 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">2</span>]<span class="op">;</span></span>
<span id="cb10-21"><a href="#cb10-21"></a>        d3 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">3</span>]<span class="op">;</span></span>
<span id="cb10-22"><a href="#cb10-22"></a>        d4 <span class="op">=</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">4</span>] <span class="op">+</span> b<span class="op">.</span>d[<span class="dv">4</span>]<span class="op">;</span></span>
<span id="cb10-23"><a href="#cb10-23"></a></span>
<span id="cb10-24"><a href="#cb10-24"></a>        <span class="dt">Self</span> <span class="op">{</span> d<span class="op">:</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4] <span class="op">}</span></span>
<span id="cb10-25"><a href="#cb10-25"></a>    <span class="op">}</span></span>
<span id="cb10-26"><a href="#cb10-26"></a></span>
<span id="cb10-27"><a href="#cb10-27"></a>    <span class="kw">pub</span> <span class="kw">fn</span> reduce(<span class="op">&amp;</span><span class="kw">mut</span> <span class="kw">self</span>) <span class="op">{</span></span>
<span id="cb10-28"><a href="#cb10-28"></a>        <span class="kw">let</span> (<span class="kw">mut</span> d0<span class="op">,</span> <span class="kw">mut</span> d1<span class="op">,</span> <span class="kw">mut</span> d2<span class="op">,</span> <span class="kw">mut</span> d3<span class="op">,</span> <span class="kw">mut</span> d4) <span class="op">=</span> (<span class="kw">self</span><span class="op">.</span>d[<span class="dv">0</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">1</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">2</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">3</span>]<span class="op">,</span> <span class="kw">self</span><span class="op">.</span>d[<span class="dv">4</span>])<span class="op">;</span></span>
<span id="cb10-29"><a href="#cb10-29"></a>        <span class="kw">let</span> <span class="kw">mut</span> c<span class="op">:</span> <span class="dt">u64</span><span class="op">;</span></span>
<span id="cb10-30"><a href="#cb10-30"></a></span>
<span id="cb10-31"><a href="#cb10-31"></a>        c <span class="op">=</span> d4 <span class="op">&gt;&gt;</span> <span class="dv">48</span><span class="op">;</span></span>
<span id="cb10-32"><a href="#cb10-32"></a>        d4 <span class="op">&amp;=</span> <span class="dv">0x0000ffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-33"><a href="#cb10-33"></a>        d0 <span class="op">+=</span> c <span class="op">*</span> <span class="dv">0x1000003d1u64</span><span class="op">;</span></span>
<span id="cb10-34"><a href="#cb10-34"></a>        d1 <span class="op">+=</span> d0 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-35"><a href="#cb10-35"></a>        d0 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-36"><a href="#cb10-36"></a>        d2 <span class="op">+=</span> d1 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-37"><a href="#cb10-37"></a>        d1 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-38"><a href="#cb10-38"></a>        d3 <span class="op">+=</span> d2 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-39"><a href="#cb10-39"></a>        d2 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-40"><a href="#cb10-40"></a>        d4 <span class="op">+=</span> d3 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-41"><a href="#cb10-41"></a>        d3 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-42"><a href="#cb10-42"></a></span>
<span id="cb10-43"><a href="#cb10-43"></a>        <span class="kw">if</span> d4 <span class="op">&gt;</span> M48 <span class="op">||</span> (d4 <span class="op">==</span> M48 <span class="op">&amp;&amp;</span> d3 <span class="op">|</span> d2 <span class="op">|</span> d1 <span class="op">==</span> M52 <span class="op">&amp;&amp;</span> d0 <span class="op">&gt;=</span> <span class="dv">0xffffefffffc2f</span>) <span class="op">{</span></span>
<span id="cb10-44"><a href="#cb10-44"></a>            <span class="kw">if</span> d4 <span class="op">&gt;</span> M48 <span class="op">{</span></span>
<span id="cb10-45"><a href="#cb10-45"></a>                c <span class="op">=</span> d4 <span class="op">&gt;&gt;</span> <span class="dv">48</span><span class="op">;</span></span>
<span id="cb10-46"><a href="#cb10-46"></a>            <span class="op">}</span> <span class="kw">else</span> <span class="op">{</span></span>
<span id="cb10-47"><a href="#cb10-47"></a>                c <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb10-48"><a href="#cb10-48"></a>            <span class="op">}</span></span>
<span id="cb10-49"><a href="#cb10-49"></a>            d4 <span class="op">&amp;=</span> <span class="dv">0x0000ffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-50"><a href="#cb10-50"></a>            d0 <span class="op">+=</span> c <span class="op">*</span> <span class="dv">0x1000003d1u64</span><span class="op">;</span></span>
<span id="cb10-51"><a href="#cb10-51"></a>            d1 <span class="op">+=</span> d0 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-52"><a href="#cb10-52"></a>            d0 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-53"><a href="#cb10-53"></a>            d2 <span class="op">+=</span> d1 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-54"><a href="#cb10-54"></a>            d1 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-55"><a href="#cb10-55"></a>            d3 <span class="op">+=</span> d2 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-56"><a href="#cb10-56"></a>            d2 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-57"><a href="#cb10-57"></a>            d4 <span class="op">+=</span> d3 <span class="op">&gt;&gt;</span> <span class="dv">52</span><span class="op">;</span></span>
<span id="cb10-58"><a href="#cb10-58"></a>            d3 <span class="op">&amp;=</span> <span class="dv">0x000fffffffffffffu64</span><span class="op">;</span></span>
<span id="cb10-59"><a href="#cb10-59"></a>            d4 <span class="op">&amp;=</span> M48<span class="op">;</span></span>
<span id="cb10-60"><a href="#cb10-60"></a>        <span class="op">}</span></span>
<span id="cb10-61"><a href="#cb10-61"></a></span>
<span id="cb10-62"><a href="#cb10-62"></a>        <span class="kw">self</span><span class="op">.</span>d <span class="op">=</span> [d0<span class="op">,</span> d1<span class="op">,</span> d2<span class="op">,</span> d3<span class="op">,</span> d4]<span class="op">;</span></span>
<span id="cb10-63"><a href="#cb10-63"></a>    <span class="op">}</span></span>
<span id="cb10-64"><a href="#cb10-64"></a><span class="op">}</span></span></code></pre></div>
<p>We also dropped the case <span class="math inline"><em>P</em> ≤ <em>R</em> &lt; 2<sup>256</sup></span> because now this is handled by the carry itself.</p>
<p>The tradeoff comes with the multiplication / square operations because as explained by Pieter, we would have to do more digit operations in the multiplication Fe x Fe.</p>
<p>I still need to benchmark it and see if it’s worth it because Elliptic curve point addition contains almost as many field element additions as field element multiplications.</p>
<p><em>Why not 2 x u104 + 1 x 48?</em></p>
<p>In the multiplication typically, we need to handle the carries differently. Using a long multiplication, we’d have to multiply 2 u104 resulting in 208 bits.</p>
<p>Anyway, see ya my bros ✌️</p>
]]></description>
  <link>https://shaoner.com/2021/07/07/field-element-addition.html</link>
  <guid>/2021/07/07/field-element-addition.html</guid>
  <pubDate>Wed, 07 Jul 2021 00:00:00 +0000</pubDate>
</item>


<item>
  <title>Hello World!</title>
  <description><![CDATA[<h2 id="introduction">Introduction</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode numberSource python numberLines"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> hey(version):</span>
<span id="cb1-2"><a href="#cb1-2"></a>    <span class="bu">print</span>(<span class="st">&quot;Just a tiny blog </span><span class="sc">{:.1f}</span><span class="st">&quot;</span>.<span class="bu">format</span>(version))</span></code></pre></div>
<p><span class="math inline"><em>a</em> + <em>b</em> (mod  <em>P</em>)</span></p>
]]></description>
  <link>https://shaoner.com/2021/07/02/hello.html</link>
  <guid>/2021/07/02/hello.html</guid>
  <pubDate>Fri, 02 Jul 2021 00:00:00 +0000</pubDate>
</item>

</channel>
</rss>
