<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>posts on s20n blog</title><link>https://blog.s20n.dev/posts/</link><description>Recent content in posts on s20n blog</description><generator>Hugo 0.150.0</generator><language>en-us</language><lastBuildDate>Sat, 11 Oct 2025 17:49:44 +0530</lastBuildDate><atom:link href="https://blog.s20n.dev/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>Random Numbers from Hard Problems: LWE Based Toy RNG</title><link>https://blog.s20n.dev/posts/lwe-rng/</link><pubDate>Sat, 11 Oct 2025 17:49:44 +0530</pubDate><guid>https://blog.s20n.dev/posts/lwe-rng/</guid><description>
&lt;p&gt;

 &lt;h2&gt;Table of Contents&lt;/h2&gt;
 &lt;nav class="toc"&gt;
 &lt;nav id="TableOfContents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-1"&gt;Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-2"&gt;Prerequisites&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-3"&gt;Pseudo-Random Number Generators&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-4"&gt;Decision LWE Problem&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-5"&gt;Construction&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-6"&gt;Parameters&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-7"&gt;Generation Loop&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-8"&gt;Step 1: Create the LWE Sample&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-9"&gt;Step 2: Extract the Output Bit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-10"&gt;Step 3: Update the Internal State (Confusion and Diffusion)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-11"&gt;Step 4: Update the Error&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-12"&gt;Example Run&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-13"&gt;Initial Parameters&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-14"&gt;Iteration 1: Generating the First Bit&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-15"&gt;Step 1: Create the LWE Sample ($b_0$)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-16"&gt;Step 2: Extract the Output Bit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-17"&gt;Step 3: Update the Internal State ($a_1$)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-18"&gt;Step 4: Update the Error ($e_1$)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-19"&gt;Iteration 2: Generating the Second Bit&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-20"&gt;Step 1: Create the LWE Sample ($b_1$)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-21"&gt;Step 2: Extract the Output Bit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-22"&gt;Step 3: Update the Internal State ($a_2$)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-23"&gt;Step 4: Update the Error ($e_2$)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-24"&gt;Summary of Run&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-25"&gt;Interactive Demo&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-26"&gt;Closing Words&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
 &lt;/nav&gt;

&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
Introduction
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;
The mathematics of cryptography can feel very daunting, but every once
in a while you come across an idea that is so beautiful and elegant,
yet so profoundly simple.&lt;/p&gt;
&lt;p&gt;
For me, an example for this is the &lt;a href="https://en.wikipedia.org/wiki/Blum%E2%80%93Micali_algorithm"&gt;Blum-Micali construction&lt;/a&gt; for a
Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) /
Deterministic Random Bit Generator (DRBG) (which I think is a better
name as it perfectly describes the mechanism of number generation).&lt;/p&gt;
&lt;p&gt;
The Blum-Micali algorithm is the epitome of simplicity and
elegance. It creates a DRBG using purely algebraic means, using a
&lt;a href="https://en.wikipedia.org/wiki/Hard-core_predicate"&gt;hard-core predicate&lt;/a&gt; of the &lt;a href="https://en.wikipedia.org/wiki/Discrete_logarithm"&gt;discrete logarithm problem&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
While it is possible to generate cryptographically secure random
number generators from &lt;strong&gt;&lt;strong&gt;any&lt;/strong&gt;&lt;/strong&gt; one-way function using the
Goldreich-Levin construction, I want to emulate the elegance and
simplicity of the Blum-Micali algorithm. So, in this blog, I present a
toy RNG constructed in a similar fashion to the Blum-Micali DRBG but
based on the hardness of a mathematical dual of the &lt;a href="https://en.wikipedia.org/wiki/Learning_with_errors"&gt;Learning with
Errors (LWE)&lt;/a&gt; problem.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Mega Disclaimer:&lt;/strong&gt; The algorithm described in this blog is a &lt;strong&gt;toy&lt;/strong&gt;
RNG. It has not been subjected to any rigorous security analysis and
should be considered &lt;strong&gt;insecure&lt;/strong&gt; by default.&lt;/p&gt;
&lt;p&gt;
Please do not use this for any real-world application that requires
security.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
Prerequisites
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;div id="outline-container-headline-3" class="outline-3"&gt;
&lt;h3 id="headline-3"&gt;
Pseudo-Random Number Generators
&lt;/h3&gt;
&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;
&lt;p&gt;A pseudorandom number generator is an RNG algorithm that generates
random-looking numbers such that no polynomial-time algorithm would be
able to distinguish the output of the RNG from true randomness.&lt;/p&gt;
&lt;p&gt;
Usually, CSPRNGs take an initial seed value generated from a truly
random source to generate the subsequent pseudorandom output.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-4" class="outline-3"&gt;
&lt;h3 id="headline-4"&gt;
Decision LWE Problem
&lt;/h3&gt;
&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;
&lt;p&gt;From the above definition of PRNG, we know that the goal of an RNG is
to generate an output that is indistinguishable from truly random. The
Decision LWE (DLWE) problem has exactly this property.&lt;/p&gt;
&lt;p&gt;
This is the famous LWE equation&lt;/p&gt;
&lt;p&gt;
\[
b_i = a_i \cdot s + e_i \pmod q
\]&lt;/p&gt;
&lt;p&gt;
Where&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;
&lt;strong&gt;$s$&lt;/strong&gt; (secret vector)
&lt;/dt&gt;
&lt;dd&gt;A secret vector (the key) that no one knows.&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$a_i$&lt;/strong&gt; (public vector)
&lt;/dt&gt;
&lt;dd&gt;A public vector of random numbers.&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$e_i$&lt;/strong&gt; (small noise)
&lt;/dt&gt;
&lt;dd&gt;A small error value (+1 / 0 / -1) to smudge the result&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$q$&lt;/strong&gt; (prime modulus)
&lt;/dt&gt;
&lt;dd&gt;A large prime number modulus&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$b_i$&lt;/strong&gt; (output)
&lt;/dt&gt;
&lt;dd&gt;The final result, which is publicly known&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;The DLWE assumption states that the output $b_i$ of the LWE problem is
computationally indistinguishable from a uniform random number in
$\mathbb{Z}_q$.&lt;/p&gt;
&lt;p&gt;
In other words, when $s$ and $e_i$ are secret and only $a_i$ and $q$ are
known publicly, the output $b_i$ is computationally indistinguishable
from a random number between 0 and q-1.&lt;/p&gt;
&lt;p&gt;
Unfortunately, the LWE assumption requires use to get $a_i$ from a
random uniform distribution for each sample. Since a PRNG must be
entirely deterministic and reproducible from its seed, we cannot
generate a truly random $a_i$​ at each step, so we need to deviate from
the LWE problem a bit.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-2"&gt;
&lt;h2 id="headline-5"&gt;
Construction
&lt;/h2&gt;
&lt;div id="outline-text-headline-5" class="outline-text-2"&gt;
&lt;p&gt;
Our generator algorithm will be an iterative one. It will begin with a
secret state and, in each step, it will extract one pseudorandom bit
and update the state in a way that is hard to reverse. Just like the
Blum-Micali algorithm.&lt;/p&gt;
&lt;p&gt;
This design has the following components:&lt;/p&gt;
&lt;div id="outline-container-headline-6" class="outline-3"&gt;
&lt;h3 id="headline-6"&gt;
Parameters
&lt;/h3&gt;
&lt;div id="outline-text-headline-6" class="outline-text-3"&gt;
&lt;p&gt;
These are the parameters needed for the algorithm&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;
&lt;strong&gt;$q$&lt;/strong&gt; (prime modulus)
&lt;/dt&gt;
&lt;dd&gt;A large prime number that defines our
finite field \(\mathbb{Z}_q\).&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$a_0$&lt;/strong&gt; (seed vector)
&lt;/dt&gt;
&lt;dd&gt;A vector of truly random numbers in
\(\mathbb{Z}_q\). This is part of the initial seed.&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$G$&lt;/strong&gt; (generator matrix)
&lt;/dt&gt;
&lt;dd&gt;A public, invertible square matrix used
to update our secret state.&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$e_0$&lt;/strong&gt; (small noise)
&lt;/dt&gt;
&lt;dd&gt;A small error term (+1 / 0 / -1) to smudge
the answer. This is also part of the initial seed.&lt;/dd&gt;
&lt;dt&gt;
&lt;strong&gt;$s$&lt;/strong&gt; (secret seed vector)
&lt;/dt&gt;
&lt;dd&gt;A vector of truly random numbers that
initializes our secret state. This is the the final secret input
needed for entire system.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Therefore, the seed input from a true random source needed for this
algorithm is $\langle a_0, s, e_0 \rangle$.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-7" class="outline-3"&gt;
&lt;h3 id="headline-7"&gt;
Generation Loop
&lt;/h3&gt;
&lt;div id="outline-text-headline-7" class="outline-text-3"&gt;
&lt;p&gt;
To generate a stream of pseudorandom bits, we start with the initial
seed is $\langle a_0, s, e_0 \rangle$. and then repeat the following
four steps for each bit that we want to generate:&lt;/p&gt;
&lt;div id="outline-container-headline-8" class="outline-4"&gt;
&lt;h4 id="headline-8"&gt;
Step 1: Create the LWE Sample
&lt;/h4&gt;
&lt;div id="outline-text-headline-8" class="outline-text-4"&gt;
&lt;p&gt;
We use the LWE equation to combine our current secret state $s$ with
the vector $a_i$ and the current error $e_i$. This gives us a
value $b_i$ that is indistinguishable from random.&lt;/p&gt;
&lt;p&gt;
\[ b_i = a_i \cdot s + e_i \pmod q \]&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-9" class="outline-4"&gt;
&lt;h4 id="headline-9"&gt;
Step 2: Extract the Output Bit
&lt;/h4&gt;
&lt;div id="outline-text-headline-9" class="outline-text-4"&gt;
&lt;p&gt;
Just like Blum-Micali uses a hard-core predicate, we extract a single
bit from $b_i$ that is provably hard to guess. For LWE, we can use the
MSB for this purpose.&lt;/p&gt;
&lt;p&gt;
\[ \text{output\_bit}_i = \lfloor \frac{2 \cdot b_i}{q} \rfloor \]&lt;/p&gt;
&lt;p&gt;
This gives us a 0 if $b_i$ is in the lower half of \(\mathbb{Z}_q\)
and a 1 if it&amp;#39;s in the upper half.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-10" class="outline-4"&gt;
&lt;h4 id="headline-10"&gt;
Step 3: Update the Internal State (Confusion and Diffusion)
&lt;/h4&gt;
&lt;div id="outline-text-headline-10" class="outline-text-4"&gt;
&lt;p&gt;
Now, we want to update the secret state so that we can generate the
next pseudorandom bit. We will incorporate Shannon&amp;#39;s &lt;a href="https://en.wikipedia.org/wiki/Confusion_and_diffusion"&gt;Confusion and
Diffusion&lt;/a&gt; to thwart statistical attacks that can predict the next
state from the previous state(s).&lt;/p&gt;
&lt;p&gt;
Here we use a two-step process that provides &lt;strong&gt;diffusion&lt;/strong&gt; and
&lt;strong&gt;confusion&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Linear Mix (Diffusion):&lt;/strong&gt; We multiply the current state by our
generator matrix &lt;strong&gt;G&lt;/strong&gt;. This mixes all the components of the secret
vector together.

\[ a&amp;#39;_i = G \cdot a_i \pmod q \]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-linear Transform (Confusion):&lt;/strong&gt; Here, we square each element of
the mixed vector (&lt;a href="https://en.wikipedia.org/wiki/Hadamard_product_(matrices)"&gt;Hadamard Product&lt;/a&gt;). This breaks the predictable
linearity.

\[ a_{i+1} = a&amp;#39;_i \odot a&amp;#39;_i \pmod q \]&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-11" class="outline-4"&gt;
&lt;h4 id="headline-11"&gt;
Step 4: Update the Error
&lt;/h4&gt;
&lt;div id="outline-text-headline-11" class="outline-text-4"&gt;
&lt;p&gt;
Finally, we need a new error term for the next iteration. To prevent
the output bit from leaking information about the next error, we
derive it from the low-order bits of $b_i$, which are statistically
independent from the MSB we just outputted.&lt;/p&gt;
&lt;p&gt;
\[ e_{i+1} = (b_i \pmod 3) - 1 \]&lt;/p&gt;
&lt;p&gt;
This gives us a pseudorandom choice of -1, 0, or +1 for the next round.&lt;/p&gt;
&lt;p&gt;
By repeating these steps, we can generate a long sequence of
pseudorandom bits from a single initial secret seed, $\langle a_0, s, e_0 \rangle$.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/lwe-rng/generator.png" alt="/images/lwe-rng/generator.png" title="/images/lwe-rng/generator.png" /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-12" class="outline-2"&gt;
&lt;h2 id="headline-12"&gt;
Example Run
&lt;/h2&gt;
&lt;div id="outline-text-headline-12" class="outline-text-2"&gt;
&lt;p&gt;
To better understand the algorithm, let&amp;#39;s walk through the first two
iterations of the generator with a small set of arbitrarily chosen
parameters.&lt;/p&gt;
&lt;div id="outline-container-headline-13" class="outline-3"&gt;
&lt;h3 id="headline-13"&gt;
Initial Parameters
&lt;/h3&gt;
&lt;div id="outline-text-headline-13" class="outline-text-3"&gt;
&lt;p&gt;
\[ q = 17 \]&lt;/p&gt;
&lt;p&gt;
\[ s = \begin{pmatrix} 2 &amp;amp; 8 &amp;amp; 10 &amp;amp; 1 \end{pmatrix} \]&lt;/p&gt;
&lt;p&gt;
\[ a_0 = \begin{pmatrix} 9 &amp;amp; 5 &amp;amp; 4 &amp;amp; 1 \end{pmatrix} \]&lt;/p&gt;
&lt;p&gt;
\[ e_0 = +1 \]&lt;/p&gt;
&lt;p&gt;
\[ G = \begin{pmatrix} 2 &amp;amp; 3 &amp;amp; 3 &amp;amp; 8 \\ 9 &amp;amp; 7 &amp;amp; 10 &amp;amp; 6 \\ 0 &amp;amp; 12 &amp;amp; 8 &amp;amp; 8 \\ 9 &amp;amp; 13 &amp;amp; 0 &amp;amp; 10 \end{pmatrix} \]&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-14" class="outline-3"&gt;
&lt;h3 id="headline-14"&gt;
Iteration 1: Generating the First Bit
&lt;/h3&gt;
&lt;div id="outline-text-headline-14" class="outline-text-3"&gt;
&lt;div id="outline-container-headline-15" class="outline-4"&gt;
&lt;h4 id="headline-15"&gt;
Step 1: Create the LWE Sample ($b_0$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-15" class="outline-text-4"&gt;
&lt;p&gt;
\[ b_0 = a_0 \cdot s + e_0 \pmod q \]
\[ b_0 = (9 \cdot 2 + 5 \cdot 8 + 4 \cdot 10 + 1 \cdot 1) + 1 \pmod{17} \]
\[ 99 + 1 \pmod{17} \implies 100 \pmod{17} \]&lt;/p&gt;
&lt;p&gt;
Therefore \[ b_0 = 15 \]&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-16" class="outline-4"&gt;
&lt;h4 id="headline-16"&gt;
Step 2: Extract the Output Bit
&lt;/h4&gt;
&lt;div id="outline-text-headline-16" class="outline-text-4"&gt;
&lt;p&gt;\[ output\_bit_0 = \lfloor \frac{2 \cdot 15}{17} \rfloor = \lfloor \frac{30}{17} \rfloor = 1 \]
The first output bit is &lt;strong&gt;1&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-17" class="outline-4"&gt;
&lt;h4 id="headline-17"&gt;
Step 3: Update the Internal State ($a_1$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-17" class="outline-text-4"&gt;
&lt;p&gt;First, the linear mix (diffusion) with the new matrix:
 \[
 a&amp;#39;_0 = G \cdot a_0 =
 \begin{pmatrix} 2 &amp;amp; 3 &amp;amp; 3 &amp;amp; 8 \\ 9 &amp;amp; 7 &amp;amp; 10 &amp;amp; 6 \\ 0 &amp;amp; 12 &amp;amp; 8 &amp;amp; 8 \\ 9 &amp;amp; 13 &amp;amp; 0 &amp;amp; 10 \end{pmatrix}
 \begin{pmatrix} 9 \\ 5 \\ 4 \\ 1 \end{pmatrix} = \begin{pmatrix} 53 \\ 162 \\ 100 \\ 156 \end{pmatrix}
 \pmod{17} = \begin{pmatrix} 2 \\ 9 \\ 15 \\ 3 \end{pmatrix}
 \]
Next, the non-linear transform (confusion):
 \[ a_1 = a&amp;#39;_0 \odot a&amp;#39;_0 = [2^2, 9^2, 15^2, 3^2] \pmod{17} \]
 \[ [4, 81, 225, 9] \pmod{17} \]
Our new vector is &lt;strong&gt;$a_1$ = [4, 13, 4, 9]&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-18" class="outline-4"&gt;
&lt;h4 id="headline-18"&gt;
Step 4: Update the Error ($e_1$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-18" class="outline-text-4"&gt;
&lt;p&gt;\[ e_1 = (b_0 \pmod 3) - 1 = (15 \pmod 3) - 1 = 0 - 1 = -1 \]
The next error is &lt;strong&gt;$e_1$ = -1&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-19" class="outline-3"&gt;
&lt;h3 id="headline-19"&gt;
Iteration 2: Generating the Second Bit
&lt;/h3&gt;
&lt;div id="outline-text-headline-19" class="outline-text-3"&gt;
&lt;p&gt;
Now we repeat the loop with our new state $a_1$ and error $e_1$.&lt;/p&gt;
&lt;div id="outline-container-headline-20" class="outline-4"&gt;
&lt;h4 id="headline-20"&gt;
Step 1: Create the LWE Sample ($b_1$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-20" class="outline-text-4"&gt;
&lt;p&gt;
\[ b_1 = a_1 \cdot s + e_1 \pmod q \]
\[ b_1 = (4 \cdot 2 + 13 \cdot 8 + 4 \cdot 10 + 9 \cdot 1) - 1 \pmod{17} \]
\[ 161 - 1 \pmod{17} \implies 160 \pmod{17} \]&lt;/p&gt;
&lt;p&gt;
Therefore \[ b_1 = 7 \]&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-21" class="outline-4"&gt;
&lt;h4 id="headline-21"&gt;
Step 2: Extract the Output Bit
&lt;/h4&gt;
&lt;div id="outline-text-headline-21" class="outline-text-4"&gt;
&lt;p&gt;\[ output\_bit_1 = \lfloor \frac{2 \cdot 7}{17} \rfloor = \lfloor \frac{14}{17} \rfloor = 0 \]
The second output bit is &lt;strong&gt;0&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-22" class="outline-4"&gt;
&lt;h4 id="headline-22"&gt;
Step 3: Update the Internal State ($a_2$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-22" class="outline-text-4"&gt;
&lt;p&gt;\[ a&amp;#39;_1 = G \cdot a_1 =
 \begin{pmatrix} 2 &amp;amp; 3 &amp;amp; 3 &amp;amp; 8 \\ 9 &amp;amp; 7 &amp;amp; 10 &amp;amp; 6 \\ 0 &amp;amp; 12 &amp;amp; 8 &amp;amp; 8 \\ 9 &amp;amp; 13 &amp;amp; 0 &amp;amp; 10 \end{pmatrix}
 \begin{pmatrix} 4 \\ 13 \\ 4 \\ 9 \end{pmatrix} = \begin{pmatrix} 131 \\ 221 \\ 260 \\ 295 \end{pmatrix}
 \pmod{17} = \begin{pmatrix} 12 \\ 0 \\ 5 \\ 6 \end{pmatrix} \]
 \[ a_2 = a&amp;#39;_1 \odot a&amp;#39;_1 = [12^2, 0^2, 5^2, 6^2] \pmod{17} \]
 \[ [144, 0, 25, 36] \pmod{17} \]&lt;/p&gt;
&lt;p&gt;
Our new secret state is &lt;strong&gt;$a_2$ = [8, 0, 8, 2]&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-23" class="outline-4"&gt;
&lt;h4 id="headline-23"&gt;
Step 4: Update the Error ($e_2$)
&lt;/h4&gt;
&lt;div id="outline-text-headline-23" class="outline-text-4"&gt;
&lt;p&gt;\[ e_2 = (b_1 \pmod 3) - 1 = (7 \pmod 3) - 1 = 1 - 1 = 0 \]
The next error is &lt;strong&gt;$e_2$ = 0&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-24" class="outline-3"&gt;
&lt;h3 id="headline-24"&gt;
Summary of Run
&lt;/h3&gt;
&lt;div id="outline-text-headline-24" class="outline-text-3"&gt;
&lt;p&gt;
After two iterations, the generator has produced the output bitstream
&amp;#34;10&amp;#34; and the internal state has evolved to $a_2$ = [8, 0, 8, 2] and
$e_2$ = 0.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-25" class="outline-2"&gt;
&lt;h2 id="headline-25"&gt;
Interactive Demo
&lt;/h2&gt;
&lt;div id="outline-text-headline-25" class="outline-text-2"&gt;
&lt;p&gt;
Here&amp;#39;s an interactive demo written in javascript. The &lt;a href="https://github.com/shriramters/blog.s20n.dev/blob/29abf65a25430cf1497855bf2118414f8da308a2/layouts/shortcodes/lwe_rng.html"&gt;javascript code&lt;/a&gt;
for this interactive demo is provided under &lt;a href="https://creativecommons.org/public-domain/cc0/"&gt;CC0&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
&lt;div id="lwe-rng-container" style="border: 1px solid #ccc; border-radius: 5px; padding: 1rem; margin: 1rem 0;"&gt;
 &lt;h3&gt;Interactive LWE Toy RNG&lt;/h3&gt;
 &lt;p&gt;Enter the parameters below and run the generator.&lt;/p&gt;

 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-q"&gt;Modulus (q):&lt;/label&gt;&lt;br&gt;
 &lt;input type="number" id="lwe-q" value="17" style="width: 100%; padding: 0.5rem; box-sizing: border-box;"&gt;
 &lt;/div&gt;
 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-e0"&gt;Initial Error (e₀):&lt;/label&gt;&lt;br&gt;
 &lt;input type="number" id="lwe-e0" value="1" style="width: 100%; padding: 0.5rem; box-sizing: border-box;"&gt;
 &lt;/div&gt;
 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-a0"&gt;Random Vector (a₀):&lt;/label&gt;&lt;br&gt;
 &lt;input type="text" id="lwe-a0" value="9, 5, 4, 1" style="width: 100%; padding: 0.5rem; box-sizing: border-box;"&gt;
 &lt;small&gt;Comma-separated numbers&lt;/small&gt;
 &lt;/div&gt;
 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-s"&gt;Secret Vector (s):&lt;/label&gt;&lt;br&gt;
 &lt;input type="text" id="lwe-s" value="2, 8, 10, 1" style="width: 100%; padding: 0.5rem; box-sizing: border-box;"&gt;
 &lt;small&gt;Comma-separated numbers&lt;/small&gt;
 &lt;/div&gt;
 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-g"&gt;Generator Matrix (G):&lt;/label&gt;&lt;br&gt;
 &lt;textarea id="lwe-g" rows="4" style="width: 100%; padding: 0.5rem; font-family: monospace; box-sizing: border-box;"&gt;2, 3, 3, 8
9, 7, 10, 6
0, 12, 8, 8
9, 13, 0, 10&lt;/textarea&gt;
 &lt;small&gt;Comma-separated numbers, new line for each row.&lt;/small&gt;
 &lt;/div&gt;

 &lt;div style="margin-bottom: 1rem;"&gt;
 &lt;label for="lwe-num-bits"&gt;Number of bits to generate:&lt;/label&gt;&lt;br&gt;
 &lt;input type="number" id="lwe-num-bits" value="16" style="padding: 0.5rem;"&gt;
 &lt;/div&gt;
 
 &lt;button id="runLweRngBtn" style="padding: 0.5rem 1rem; cursor: pointer;"&gt;Run Generator&lt;/button&gt;
 
 &lt;div style="margin-top: 1rem;"&gt;
 &lt;label for="lweRngOutput"&gt;Generated Bits:&lt;/label&gt;
 &lt;pre id="lweRngOutput" style="padding: 0.5rem; min-height: 3rem; font-family: monospace;"&gt;&lt;/pre&gt;
 &lt;p id="lweRngError" style="color: red;"&gt;&lt;/p&gt;
 &lt;/div&gt;
&lt;/div&gt;

&lt;script&gt;
 (function() {
 function dot(vec1, vec2) {
 let result = 0;
 for (let i = 0; i &lt; vec1.length; i++) {
 result += vec1[i] * vec2[i];
 }
 return result;
 }

 function matVecMul(mat, vec) {
 const result = [];
 for (let i = 0; i &lt; mat.length; i++) {
 result.push(dot(mat[i], vec));
 }
 return result;
 }

 function* lweRng(a0, e0, q, s, G, numBits) {
 let a_i = [...a0];
 let e_i = e0;

 for (let i = 0; i &lt; numBits; i++) {
 const dotProduct = dot(a_i, s);
 
 
 let b_i = ((dotProduct + e_i) % q + q) % q;

 const outputBit = Math.floor((2 * b_i) / q);
 yield outputBit;

 const a_prime_raw = matVecMul(G, a_i);
 const a_prime = a_prime_raw.map(val =&gt; ((val % q) + q) % q);

 a_i = a_prime.map(val =&gt; (((val * val) % q) + q) % q);

 e_i = (b_i % 3) - 1;
 }
 }

 
 const container = document.getElementById('lwe-rng-container');
 const runBtn = container.querySelector('#runLweRngBtn');
 const outputEl = container.querySelector('#lweRngOutput');
 const errorEl = container.querySelector('#lweRngError');

 
 const parseVector = (str) =&gt; str.split(',').map(n =&gt; parseInt(n.trim(), 10));
 const parseMatrix = (str) =&gt; str.split('\n').map(row =&gt; parseVector(row.trim()));

 runBtn.addEventListener('click', () =&gt; {
 try {
 
 outputEl.textContent = '';
 errorEl.textContent = '';

 
 const q = parseInt(container.querySelector('#lwe-q').value, 10);
 const s = parseVector(container.querySelector('#lwe-s').value);
 const a0 = parseVector(container.querySelector('#lwe-a0').value);
 const e0 = parseInt(container.querySelector('#lwe-e0').value, 10);
 const G = parseMatrix(container.querySelector('#lwe-g').value);
 const numBitsToGenerate = parseInt(container.querySelector('#lwe-num-bits').value, 10);

 
 if (isNaN(q) || isNaN(e0) || isNaN(numBitsToGenerate) || s.some(isNaN) || a0.some(isNaN) || G.flat().some(isNaN)) {
 throw new Error("Invalid input. Please ensure all fields contain valid numbers.");
 }
 if (s.length !== a0.length || G.length !== s.length || G.some(row =&gt; row.length !== s.length)) {
 throw new Error("Dimension mismatch. The length of 's', 'a0', and the dimensions of 'G' must be consistent.");
 }

 
 const rngGenerator = lweRng(a0, e0, q, s, G, numBitsToGenerate);
 const generatedBits = [...rngGenerator];
 
 outputEl.textContent = generatedBits.join('');

 } catch (err) {
 errorEl.textContent = `Error: ${err.message}`;
 }
 });
 })();
&lt;/script&gt;

&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-26" class="outline-2"&gt;
&lt;h2 id="headline-26"&gt;
Closing Words
&lt;/h2&gt;
&lt;div id="outline-text-headline-26" class="outline-text-2"&gt;
&lt;p&gt;
In this blog we have seen how to build a pseudorandom number generator
from the LWE problem using a Blum-Micali style construction.&lt;/p&gt;
&lt;p&gt;
I must emphasize one last time that the spirit of this article is
learning and NOT to provide an unbreakable CSPRNG. I wanted to take
the elegant ideas from the Blum-Micali algorithm and apply them to the
post-quantum LWE problem.&lt;/p&gt;
&lt;p&gt;
I invite you wholeheartedly to analyse and break this design.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The output of the confusion and diffusion step may not really be a
uniform random distribution as required by the LWE problem. What are
the implications of using this instead?&lt;/li&gt;
&lt;li&gt;Can you find any statistical biases in the algorithm?&lt;/li&gt;
&lt;li&gt;Is it possible to recover any information about the secret state
from the output bit stream?&lt;/li&gt;
&lt;li&gt;Is the error term $e$&amp;#39;s distribution in the algorithm flawed?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>GPG Setup Uncomplicated</title><link>https://blog.s20n.dev/posts/gpg-setup-uncomplicated/</link><pubDate>Sat, 27 Sep 2025 10:09:39 +0530</pubDate><guid>https://blog.s20n.dev/posts/gpg-setup-uncomplicated/</guid><description>
&lt;p&gt;
The&lt;a href="https://www.gnupg.org/documentation/manuals/gnupg/"&gt; GPG manual&lt;/a&gt; can feel a little.. &lt;em&gt;cryptic&lt;/em&gt; sometimes
&lt;em&gt;&lt;code&gt;*ba-dum-tss*&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;
Here&amp;#39;s a simple and effective guide to hit the ground
running with GnuPG.&lt;/p&gt;
&lt;p&gt;
This guide is mostly based on this great article about &lt;a href="https://musigma.blog/2021/05/09/gpg-ssh-ed25519.html"&gt;using ed25519
keys with GnuPG&lt;/a&gt;. I generated my current GPG keys following this
article a few years ago. There&amp;#39;s however, some missing stuff about how
to get gpg-agent to work with your [A] key for ssh and some other
things I have discovered over the course of using gpg-agent that I&amp;#39;d
like to share.&lt;/p&gt;
&lt;p&gt;

 &lt;h2&gt;Table of Contents&lt;/h2&gt;
 &lt;nav class="toc"&gt;
 &lt;nav id="TableOfContents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-1"&gt;Key Generation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-2"&gt;Configuring GPG for SSH&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#headline-3"&gt;Pinentry&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-4"&gt;Export the [A] key in SSH format&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-5"&gt;Troubleshooting&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-6"&gt;Exporting and Importing Subkeys&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-7"&gt;Renewing Keys&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="#headline-8"&gt;Closing Words&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
 &lt;/nav&gt;

&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
Key Generation
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;
The keys we will be generating will use ed25519 and cv25519
algorithms. These algorithms are both based on the &lt;a href="https://en.wikipedia.org/wiki/Twisted_Edwards_curve"&gt;Twisted Edwards
Curve&lt;/a&gt;. This is a special family of elliptic curves which is resistant
to &lt;a href="https://en.wikipedia.org/wiki/Elliptic-curve_cryptography#Side-channel_attacks"&gt;the side-channel attacks&lt;/a&gt; that other elliptic curves are vulnerable
to.&lt;/p&gt;
&lt;p&gt;
Basically, ed25519 is the signing algorithm and cv25519 is its
encryption counterpart.&lt;/p&gt;
&lt;p&gt;
We want to generate four keys. Specifically, 1 primary key and 3
subkeys.
These are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Certificate Key [C]&lt;/strong&gt;: Your primary key which will be used for
creating and revoking your other keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authentication Key [A]&lt;/strong&gt;: The subkey which will be used for SSH.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encryption Key [E]&lt;/strong&gt;: The subkey for encrypting and decrypting
messages (such as PGP/MIME encrypted emails).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Signature Key [S]&lt;/strong&gt;: The subkey for digitally signing messages
(such as PGP/MIME signed emails).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Let&amp;#39;s begin by generating our Certificate Key.&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --quick-generate-key &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Your Name &amp;lt;your.email@example.com&amp;gt;&amp;#39;&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;&lt;/span&gt; ed25519 cert never&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
When it prompts you to enter a password, make sure you select a
strong, memorable password that you will remember. Do not store this
password in a password manager. You need to remember this so that you
can reliably recall the password when the attacker hits you with &lt;a href="https://xkcd.com/538"&gt;the
wrench&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
The command will give the output:&lt;/p&gt;
&lt;pre class="example"&gt;
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: revocation certificate stored as &amp;#39;/home/shriram/.gnupg/openpgp-revocs.d/47BDC6DBB9460CE2471AA2258E9C8FBD1272F630.rev&amp;#39;
public and secret key created and signed.

pub ed25519 2025-09-27 [C]
 47BDC6DBB9460CE2471AA2258E9C8FBD1272F630
uid Your Name &amp;lt;your.email@example.com&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Next, copy the fingerprint of the generated key and save it to a
variable&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; export KEYFP&lt;span style="color:#f92672"&gt;=&lt;/span&gt;47BDC6DBB9460CE2471AA2258E9C8FBD1272F630 &lt;span style="color:#75715e"&gt;# the fingerprint from above&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Now, we can use this main key to generate our 3 subkeys.
This should prompt you for your main key&amp;#39;s password thrice.&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpg --quick-add-key $KEYFP ed25519 sign 1y
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpg --quick-add-key $KEYFP cv25519 encr 1y
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;gpg --quick-add-key $KEYFP ed25519 auth 1y&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
That&amp;#39;s it. We have generated the 4 keys that we need. The main key
does not expire and the three subkeys have a validity of 1 year
each. (This can be extended by editing the keys using your main key
later).&lt;/p&gt;
&lt;p&gt;
At this point, your gpg keychain should look something like this:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg -K&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="example"&gt;
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
/home/shriram/.gnupg/pubring.kbx
--------------------------------
sec ed25519 2025-09-27 [C]
 47BDC6DBB9460CE2471AA2258E9C8FBD1272F630
uid [ultimate] Your Name &amp;lt;your.email@example.com&amp;gt;
ssb ed25519 2025-09-27 [S] [expires: 2026-09-27]
ssb cv25519 2025-09-27 [E] [expires: 2026-09-27]
ssb ed25519 2025-09-27 [A] [expires: 2026-09-27]
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
Configuring GPG for SSH
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;p&gt;
In order to use your GPG [A] key for SSH, you need to switch your
system&amp;#39;s &lt;code&gt;ssh-agent&lt;/code&gt; with the &lt;code&gt;gpg-agent&lt;/code&gt; that comes with GnuPG.&lt;/p&gt;
&lt;p&gt;
To do this, open your code editor and add the following lines to your
&lt;code class="verbatim"&gt;~/.bash_profile&lt;/code&gt; or the equivalent for your shell (e.g., &lt;code class="verbatim"&gt;.zprofile&lt;/code&gt;)&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpgconf --launch gpg-agent&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Next, we need to tell GPG which key to use for SSH. We can do that by
grabbing the keygrip of our [A] key and putting it in GnuPG&amp;#39;s
&lt;code&gt;sshcontrol&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;
First, get the keygrip of your [A] key like so:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg -K --with-keygrip&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This should give you an output like:&lt;/p&gt;
&lt;pre class="example"&gt;
/home/shriram/.gnupg/pubring.kbx
--------------------------------
sec ed25519 2025-09-27 [C]
 47BDC6DBB9460CE2471AA2258E9C8FBD1272F630
 Keygrip = 0BECF2A25D3647CCE20BB8486A7426912C14399C
uid [ultimate] Your Name &amp;lt;your.email@example.com&amp;gt;
ssb ed25519 2025-09-27 [S] [expires: 2026-09-27]
 Keygrip = 809DB3A0F806CF443114651FC3D67E0C7EEC99C4
ssb cv25519 2025-09-27 [E] [expires: 2026-09-27]
 Keygrip = EB55552C8982AA5E2D3477D6B582A15BC30549D2
ssb ed25519 2025-09-27 [A] [expires: 2026-09-27]
 Keygrip = 7B761F0445CC62FFAA4D2F68416BE178ED066537
&lt;/pre&gt;
&lt;p&gt;
Copy the keygrip under the [A] key and put it in the &lt;code&gt;sshcontrol&lt;/code&gt; file
like so.&lt;/p&gt;
&lt;div class="src src-bash"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; echo &lt;span style="color:#e6db74"&gt;&amp;#34;7B761F0445CC62FFAA4D2F68416BE178ED066537&amp;#34;&lt;/span&gt; &amp;gt;&amp;gt; ~/.gnupg/sshcontrol&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Now that we have done the required setup, logout and log back in to
your desktop for the changes to take effect.&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;/strong&gt; if you are using a code editor for editing these files,
 please ensure that the file ends with a newline, otherwise GPG will
 not parse the file properly, especially the &lt;code&gt;sshcontrol&lt;/code&gt; file.&lt;/p&gt;
&lt;div id="outline-container-headline-3" class="outline-3"&gt;
&lt;h3 id="headline-3"&gt;
Pinentry
&lt;/h3&gt;
&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;
&lt;p&gt;
&lt;code&gt;gpg-agent&lt;/code&gt; needs to know which tty to use for launching the pinentry
program (the program that asks you for your GPG key&amp;#39;s password), even
if it&amp;#39;s a GUI program like pinentry-qt.&lt;/p&gt;
&lt;p&gt;
So we need to update our &lt;code&gt;.ssh/config&lt;/code&gt; file which runs every time you
try to use SSH, to tell &lt;code&gt;gpg-agent&lt;/code&gt; about the current tty.&lt;/p&gt;
&lt;p&gt;
Edit &lt;code&gt;.ssh/config&lt;/code&gt; and add this line at the top. (create the file if
it doesn&amp;#39;t exist)&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Match host * exec &amp;#34;gpg-connect-agent UPDATESTARTUPTTY /bye&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-4" class="outline-3"&gt;
&lt;h3 id="headline-4"&gt;
Export the [A] key in SSH format
&lt;/h3&gt;
&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;
&lt;p&gt;
Run the following command to export your [A] key in SSH format.&lt;/p&gt;
&lt;p&gt;
Use the email associated with your certificate key here.&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --export-ssh-key your.email@example.com&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This should give you your [A] key&amp;#39;s public component in SSH
format. for example:&lt;/p&gt;
&lt;pre class="example"&gt;
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGrNLpEGa6DxRqF+fg13PY0p/pwlBvWWcuWv8L23YXUw openpgp:0xC7BC60CC
&lt;/pre&gt;
&lt;p&gt;
You can now use this as your ssh key.&lt;/p&gt;
&lt;p&gt;
For example, you can add this to your github at
&lt;a href="https://github.com/settings/ssh/new"&gt;https://github.com/settings/ssh/new&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
Give it any name like &amp;#34;my gpg key&amp;#34;.
Once you have submitted that form after pasting that key (the entire
output of the previous command), you can test that authentication
works using the following command:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ssh -T git@github.com&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This should give you something like the following output:
(if it prompts to add the host to known_hosts, type yes and hit enter)&lt;/p&gt;
&lt;pre class="example"&gt;
The authenticity of host &amp;#39;github.com (20.207.73.82)&amp;#39; can&amp;#39;t be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added &amp;#39;github.com&amp;#39; (ED25519) to the list of known hosts.
Hi shriramters! You&amp;#39;ve successfully authenticated, but GitHub does not provide shell access.
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-3"&gt;
&lt;h3 id="headline-5"&gt;
Troubleshooting
&lt;/h3&gt;
&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;
&lt;p&gt;If you get a permission denied (maybe the pinentry window didn&amp;#39;t show
up), make sure you logged out and logged in again as mentioned in one
of the previous sections.&lt;/p&gt;
&lt;p&gt;
Ensure that your configuration files &lt;code&gt;.ssh/config&lt;/code&gt;,
&lt;code&gt;.gnupg/sshcontrol&lt;/code&gt; etc are proper and have a newline character at the
end of the file.&lt;/p&gt;
&lt;p&gt;
Pinentry issues are specific to your operating system and
desktop environment, so you may need to read the manual for the
pinentry program you are using and how to configure gpg to use it
(maybe by editing &lt;code&gt;.gnupg/gpg-agent.conf&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;
You can also get the ssh command to produce verbose output to clearly
understand the error.&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ssh -vvT git@github.com&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-6" class="outline-2"&gt;
&lt;h2 id="headline-6"&gt;
Exporting and Importing Subkeys
&lt;/h2&gt;
&lt;div id="outline-text-headline-6" class="outline-text-2"&gt;
&lt;p&gt;
If you have multiple machines and say you want to use your gpg [A] key to
access github, [E] key for encrypting emails and [S] key to sign your
commits, but you don&amp;#39;t need your [C] key there, you can export your
full [A], [E] and [S] keys for use in another machine.&lt;/p&gt;
&lt;p&gt;
The simplest way is to use:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --armor --export-secret-subkeys your.email@example.com &amp;gt; ssb-keys.key&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This will export dummy packets for the primary key, so effectively
only all your subkeys are exported.&lt;/p&gt;
&lt;p&gt;
If you don&amp;#39;t need all the subkeys, you can individually export only
the required subkeys by their fingerprints.&lt;/p&gt;
&lt;p&gt;
Like if you only need [A] and [S] key on your other machine:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg -K --with-subkey-fingerprint&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="example"&gt;
/home/shriram/.gnupg/pubring.kbx
--------------------------------
sec ed25519 2025-09-27 [C]
 47BDC6DBB9460CE2471AA2258E9C8FBD1272F630
uid [ultimate] Your Name &amp;lt;your.email@example.com&amp;gt;
ssb ed25519 2025-09-27 [S] [expires: 2027-09-27]
 055BE71F465B5D1762C3393A7D4D5B421D76D88E
ssb cv25519 2025-09-27 [E] [expires: 2027-09-27]
 D019979E67F90A344947EECDBFB31F285CEF4FE8
ssb ed25519 2025-09-27 [A] [expires: 2027-09-27]
 5CFE67C0E17E7909F6BB847D7B1ED4B2C7BC60CC
&lt;/pre&gt;
&lt;p&gt;
Now export only [A] and [S] subkeys:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --armor --export-secret-subkeys &amp;#34;&amp;lt;subkey-S-fingerprint&amp;gt;!&amp;#34; &amp;#34;&amp;lt;subkey-A-fingerprint&amp;gt;!&amp;#34; &amp;gt; a-and-s.key&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="example"&gt;
gpg --armor --export-secret-subkeys &amp;#34;055BE71F465B5D1762C3393A7D4D5B421D76D88E!&amp;#34; &amp;#34;5CFE67C0E17E7909F6BB847D7B1ED4B2C7BC60CC!&amp;#34; &amp;gt; a-and-s.key
&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;/strong&gt; the bang (!) is necessary to prevent the primary key from
being exported.&lt;/p&gt;
&lt;p&gt;
Now, copy this &lt;code class="verbatim"&gt;a-and-s.key&lt;/code&gt; file to the other machine and run the
command:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --import /path/to/a-and-s.key&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This will prompt you for pinentry and then:&lt;/p&gt;
&lt;pre class="example"&gt;
gpg: key 8E9C8FBD1272F630: public key &amp;#34;Your Name &amp;lt;your.email@example.com&amp;gt;&amp;#34; imported
gpg: To migrate &amp;#39;secring.gpg&amp;#39;, with each smartcard, run: gpg --card-status
gpg: key 8E9C8FBD1272F630: secret key imported
gpg: Total number processed: 1
gpg: imported: 1
gpg: secret keys read: 1
gpg: secret keys imported: 1
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-7" class="outline-2"&gt;
&lt;h2 id="headline-7"&gt;
Renewing Keys
&lt;/h2&gt;
&lt;div id="outline-text-headline-7" class="outline-text-2"&gt;
&lt;p&gt;
If your subkeys expired and you want to extend the validity rather
than generate new keys, you can do that like so:&lt;/p&gt;
&lt;div class="src src-text"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; gpg --edit-key your.email@example.com&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This should take you to an interactive CLI:&lt;/p&gt;
&lt;pre class="example"&gt;
gpg (GnuPG) 2.4.7; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

sec ed25519/8E9C8FBD1272F630
 created: 2025-09-27 expires: never usage: C 
 trust: ultimate validity: ultimate
ssb ed25519/7D4D5B421D76D88E
 created: 2025-09-27 expires: 2026-09-27 usage: S 
ssb cv25519/BFB31F285CEF4FE8
 created: 2025-09-27 expires: 2026-09-27 usage: E 
ssb ed25519/7B1ED4B2C7BC60CC
 created: 2025-09-27 expires: 2026-09-27 usage: A 
[ultimate] (1). Your Name &amp;lt;your.email@example.com&amp;gt;

gpg&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
We want to select all three subkeys (1, 2 and 3) to extend the expiry
date for.&lt;/p&gt;
&lt;p&gt;
At the prompt, type &lt;code&gt;key 1&lt;/code&gt; press enter, type &lt;code&gt;key 2&lt;/code&gt; press enter and
then type &lt;code&gt;key 3&lt;/code&gt; and press enter.&lt;/p&gt;
&lt;p&gt;
This will select all three keys and you will see * next to each
selected subkey.&lt;/p&gt;
&lt;pre class="example"&gt;
sec ed25519/8E9C8FBD1272F630
 created: 2025-09-27 expires: never usage: C 
 trust: ultimate validity: ultimate
ssb* ed25519/7D4D5B421D76D88E
 created: 2025-09-27 expires: 2026-09-27 usage: S 
ssb* cv25519/BFB31F285CEF4FE8
 created: 2025-09-27 expires: 2026-09-27 usage: E 
ssb* ed25519/7B1ED4B2C7BC60CC
 created: 2025-09-27 expires: 2026-09-27 usage: A 
[ultimate] (1). Your Name &amp;lt;your.email@example.com&amp;gt;

gpg&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Now, enter &lt;code&gt;expire&lt;/code&gt; to edit the expiry date and then enter how long
you want the key to be valid, for example enter &lt;code&gt;2y&lt;/code&gt; if you want it to
be valid for 2 years.&lt;/p&gt;
&lt;pre class="example"&gt;
gpg&amp;gt; expire
Are you sure you want to change the expiration time for multiple subkeys? (y/N) y
Please specify how long the key should be valid.
 0 = key does not expire
 &amp;lt;n&amp;gt; = key expires in n days
 &amp;lt;n&amp;gt;w = key expires in n weeks
 &amp;lt;n&amp;gt;m = key expires in n months
 &amp;lt;n&amp;gt;y = key expires in n years
Key is valid for? (0) 2y
Key expires at Monday 27 September 2027 09:31:50 AM IST
Is this correct? (y/N) y

sec ed25519/8E9C8FBD1272F630
 created: 2025-09-27 expires: never usage: C 
 trust: ultimate validity: ultimate
ssb* ed25519/7D4D5B421D76D88E
 created: 2025-09-27 expires: 2027-09-27 usage: S 
ssb* cv25519/BFB31F285CEF4FE8
 created: 2025-09-27 expires: 2027-09-27 usage: E 
ssb* ed25519/7B1ED4B2C7BC60CC
 created: 2025-09-27 expires: 2027-09-27 usage: A 
[ultimate] (1). Your Name &amp;lt;your.email@example.com&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Finally, enter &lt;code&gt;save&lt;/code&gt; at the prompt to save and exit.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-8" class="outline-2"&gt;
&lt;h2 id="headline-8"&gt;
Closing Words
&lt;/h2&gt;
&lt;div id="outline-text-headline-8" class="outline-text-2"&gt;
&lt;p&gt;
In this guide, we&amp;#39;ve seen how to generate an ed25519 certificate key
and ed25519 and cv25519 keys for Encryption, Authentication, Signing.&lt;/p&gt;
&lt;p&gt;
We&amp;#39;ve also seen how we can configure GPG for use with SSH and also how
to export, import and renew keys.&lt;/p&gt;
&lt;p&gt;
You should have a pretty decent configuration by now. Next, download
an email client which supports PGP/MIME and maybe &lt;a href="https://help.ubuntu.com/community/GnuPrivacyGuardHowto#Uploading_the_key_to_Ubuntu_keyserver"&gt;upload your public
key to a keyserver&lt;/a&gt; so that others can download it for sending you PGP
encrypted mail etc.&lt;/p&gt;
&lt;p&gt;
Thank you for reading!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>Keycase: Self-Hostable Cloud Based Password Manager</title><link>https://blog.s20n.dev/posts/keycase-password-manager/</link><pubDate>Sat, 27 Apr 2024 23:04:33 +0530</pubDate><guid>https://blog.s20n.dev/posts/keycase-password-manager/</guid><description>
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/banner.png" alt="/images/keycase-password-manager/banner.png" title="/images/keycase-password-manager/banner.png" /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://github.com/shriramters/keycase"&gt;Keycase&lt;/a&gt; is a browser based, self-hostable cryptographic password
manager. It is a chrome extension that lets you encrypt and store
passwords securely on firebase.&lt;/p&gt;
&lt;p&gt;
The passwords are encrypted at rest. So only the user&amp;#39;s browser is
able to decrypt them. Even firebase administrators with full
database access cannot decrypt the contents stored.&lt;/p&gt;
&lt;p&gt;
This is because all user data is encrypted with a personal RSA key
(unique to each user). This RSA key is itself encrypted and
stored along with the user record in firebase.&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
User Interface
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;
Users can sign in to keycase using Google Authentication (Domains
configurable).&lt;/p&gt;
&lt;p&gt;
Keycase requires a master password to decrypt the RSA private key of
the user which is stored in the cloud (firebase). This RSA key is used
to encrypt and decrypt user passwords.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/welcome.png" alt="/images/keycase-password-manager/welcome.png" title="/images/keycase-password-manager/welcome.png" /&gt;&lt;/p&gt;
&lt;p&gt;
Once the master password is provided, the user is able to decrypt all
their passwords stored in the cloud. All cryptography happens on the
browser using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API"&gt;Browser Crypto API&lt;/a&gt;. This ensures that your data is
only ever decrypted on your computer.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/passwords.png" alt="/images/keycase-password-manager/passwords.png" title="/images/keycase-password-manager/passwords.png" /&gt;&lt;/p&gt;
&lt;p&gt;
In the above image, we can see the user has five passwords stored. The
user can click on any of them to view details and copy the password.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/details.png" alt="/images/keycase-password-manager/details.png" title="/images/keycase-password-manager/details.png" /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
How it works
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;p&gt;
This is the sequence diagram depicting the workings of keycase. These
are the steps involved:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Login with google authentication&lt;/li&gt;
&lt;li&gt;Receive the AES encrypted RSA private key&lt;/li&gt;
&lt;li&gt;Regenerate the AES key from the user&amp;#39;s master password using PBKDF2&lt;/li&gt;
&lt;li&gt;Decrypt the RSA private key with the re-generated AES key&lt;/li&gt;
&lt;li&gt;Use the decrypted RSA private key and the RSA public keys to add,
update or delete values from the database.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/keycase-sequence.svg" alt="/images/keycase-password-manager/keycase-sequence.svg" title="/images/keycase-password-manager/keycase-sequence.svg" /&gt;&lt;/p&gt;
&lt;div id="outline-container-headline-3" class="outline-3"&gt;
&lt;h3 id="headline-3"&gt;
PBKDF2
&lt;/h3&gt;
&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/PBKDF2"&gt;Password Based Key Derivation Function 2&lt;/a&gt; is a cryptographic function
which can take a string (password) and generate an AES key out of
it. This is useful to us as we can then use this AES key to decrypt
our RSA key which can then be used to perform our operations.&lt;/p&gt;
&lt;div id="outline-container-headline-4" class="outline-4"&gt;
&lt;h4 id="headline-4"&gt;
Why not use the AES key directly
&lt;/h4&gt;
&lt;div id="outline-text-headline-4" class="outline-text-4"&gt;
&lt;p&gt;
Why do we need an RSA pair when we get a perfectly usable AES key from
PBKDF2? If the user wishes to change their master password (for any
reason), then we would have had to decrypt all the passwords and
re-encrypt them with the new AES key generated from the new password.&lt;/p&gt;
&lt;p&gt;
Using an RSA keypair eliminates the need for doing all this mass
re-encryption since we would only need to re-encrypt the RSA private
key with the new AES key.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-3"&gt;
&lt;h3 id="headline-5"&gt;
Database Design
&lt;/h3&gt;
&lt;div id="outline-text-headline-5" class="outline-text-3"&gt;
&lt;p&gt;
The database for keycase is of a rather simplistic design. We are
using Cloud Firestore, which is a Document Database like MongoDB. We
have only two collections. viz. Auth and Passwords.&lt;/p&gt;
&lt;div id="outline-container-headline-6" class="outline-4"&gt;
&lt;h4 id="headline-6"&gt;
Auth
&lt;/h4&gt;
&lt;div id="outline-text-headline-6" class="outline-text-4"&gt;
&lt;p&gt;The Auth collection is used to store the user records.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/auth-collection.png" alt="/images/keycase-password-manager/auth-collection.png" title="/images/keycase-password-manager/auth-collection.png" /&gt;&lt;/p&gt;
&lt;p&gt;
Each document represents one user record. The name of the
document is our user id. In the above image, we can see the user
record of our user with the id &lt;code&gt;QvyjDdGcgVNM3URaAPVswNHwdci1&lt;/code&gt;.
Here we&amp;#39;re storing the following details of the user:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;RSA Public Key&lt;/li&gt;
&lt;li&gt;AES Encrypted RSA Private Key&lt;/li&gt;
&lt;li&gt;Master Password Hash and Salt&lt;/li&gt;
&lt;li&gt;AES key IV&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-7" class="outline-4"&gt;
&lt;h4 id="headline-7"&gt;
Passwords
&lt;/h4&gt;
&lt;div id="outline-text-headline-7" class="outline-text-4"&gt;
&lt;p&gt;The Passwords collection is used to store all the user passwords.&lt;/p&gt;
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/keycase-password-manager/passwords-collection.png" alt="/images/keycase-password-manager/passwords-collection.png" title="/images/keycase-password-manager/passwords-collection.png" /&gt;&lt;/p&gt;
&lt;p&gt;
Each user has one document in this collection where their passwords
will be stored. In the above image you can see the 5 passwords that
were depicted in the earlier screenshot, encrypted and stored as a
list.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-8" class="outline-2"&gt;
&lt;h2 id="headline-8"&gt;
Closing Words
&lt;/h2&gt;
&lt;div id="outline-text-headline-8" class="outline-text-2"&gt;
&lt;p&gt;
Keycase is a secure, self hostable password manager for your friends
and family. It is by no means perfect. If you find any gaping
blunders, please do reach out to me. My contact information is on this
website. Please also feel free to contribute your code and make
keycase better.&lt;/p&gt;
&lt;p&gt;
Until Next Time!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>How Chordcat Works, A Chord Naming Algorithm</title><link>https://blog.s20n.dev/posts/how-chordcat-works/</link><pubDate>Sat, 06 Apr 2024 10:55:09 +0530</pubDate><guid>https://blog.s20n.dev/posts/how-chordcat-works/</guid><description>
&lt;p&gt;
&lt;img src="https://blog.s20n.dev/images/chordcat-banner.png" alt="/images/chordcat-banner.png" title="/images/chordcat-banner.png" /&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://github.com/shriramters/chordcat/"&gt;chordcat&lt;/a&gt; is a chord-naming application and a pet project of mine. It is written
in C++ and SFML.&lt;/p&gt;
&lt;p&gt;
At the heart of chordcat is the chord naming algorithm. I and my dear friend &lt;a href="https://theakash.dev"&gt;Akash&lt;/a&gt;
came up with this one afternoon and found it rather elegant. The algorithm
itself is not very complicated but the explanation might be a tad bit
lengthy. In this blog, I&amp;#39;ll try my best to explain how this algorithm works.&lt;/p&gt;
&lt;p&gt;
All the code presented in this blog is licensed under &lt;a href="https://www.gnu.org/licenses/gpl-3.0.en.html"&gt;GPL-3.0&lt;/a&gt;&lt;/p&gt;
&lt;div id="outline-container-headline-1" class="outline-2"&gt;
&lt;h2 id="headline-1"&gt;
Basic Theory
&lt;/h2&gt;
&lt;div id="outline-text-headline-1" class="outline-text-2"&gt;
&lt;p&gt;
A chord can have many names. In fact we could pick any of the notes as the root
note and come up with a name for the chord. For example, the notes &lt;code&gt;C&lt;/code&gt;, &lt;code&gt;E&lt;/code&gt; and
&lt;code&gt;G&lt;/code&gt; (The C major chord) can have any of the following names, based on what is
considered to be the root note.&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;root note&lt;/td&gt;
&lt;td&gt;chord name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;C Major&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;E Minor #5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;G&lt;/td&gt;
&lt;td&gt;G sus4 (13)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
(To the best of my knowledge, I&amp;#39;m no music theorist)&lt;/p&gt;
&lt;p&gt;
Also, it does not really matter what octave these notes are played in or whether
these notes are repeated. For all intents and purposes, we can simply take the
index of the key pressed on the keyboard (0-88 starting from A) and take the
modulo of that index with 12 to get our note.&lt;/p&gt;
&lt;p&gt;
It is also important to know that chords are built on thirds. If we were to go
up the major scale from the root note in diatonic thirds (alternating notes),
we&amp;#39;d form chords like 1, 3, 5, 7, 9, 11, 13 (Maj13)&lt;/p&gt;
&lt;p&gt;
For example, in the C Major scale (&lt;code&gt;CDEFGABC&lt;/code&gt;), we could pick the alternate
notes and form this chord (&lt;code&gt;CEGBDFA&lt;/code&gt;) which is the &lt;code&gt;CMaj13&lt;/code&gt; chord.&lt;/p&gt;
&lt;p&gt;
And that&amp;#39;s about the biggest chord you&amp;#39;ll find using this technique because the
next number after 13, i.e &amp;#34;15&amp;#34;, is the root note &lt;code&gt;C&lt;/code&gt; again.&lt;/p&gt;
&lt;p&gt;
While we were coming up with chordcat, Jake from &lt;a href="https://signalsmusicstudio.com/"&gt;signals music studio&lt;/a&gt; posted
&lt;a href="https://www.youtube.com/watch?v=2SeN9W1CIrI"&gt;this excellent video&lt;/a&gt; on how to name every chord ever. This video came
extremely in clutch when we were writing this software.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-2" class="outline-2"&gt;
&lt;h2 id="headline-2"&gt;
The Algorithm
&lt;/h2&gt;
&lt;div id="outline-text-headline-2" class="outline-text-2"&gt;
&lt;p&gt;
First, we get &lt;code&gt;indices&lt;/code&gt;, which is the list of indices of all the keys pressed on
the keyboard (0-88). As discussed in the previous section, we can simply take
the modulo of these indices with 12 and store them in a set to name our chord
(no duplicates). We&amp;#39;ll call this set &amp;#34;&lt;code&gt;notes&lt;/code&gt;&amp;#34;.&lt;/p&gt;
&lt;p&gt;
Since we now know that basically any of the played notes could be the root note
of our chord, we&amp;#39;ll need to iterate through all the notes that were played and
run the chord-naming algorithm once for each note.&lt;/p&gt;
&lt;p&gt;
This is what is done in the piece of code below. In our chord naming function,
we&amp;#39;ve got the second &lt;code&gt;for&lt;/code&gt; loop iterating through each of the notes considering
each of them the root, one at a time.&lt;/p&gt;
&lt;div class="src src-c++"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;multiset&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;Chord&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; name_that_chord(&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;vector&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;size_t&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&lt;/span&gt; indices) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; notes &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; (&lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; index : indices) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; notes.insert(index &lt;span style="color:#f92672"&gt;%&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;12&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;multiset&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;Chord&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; result &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; (&lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; root : notes) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; intervals &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; (&lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; other : notes) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	 &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (other &lt;span style="color:#f92672"&gt;==&lt;/span&gt; root)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	 &lt;span style="color:#66d9ef"&gt;continue&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	 intervals.insert(get_note_distance(root, other));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; insert_chords(root, intervals, result);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; result;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Once we consider one of the notes as the root, we can then go ahead and get the
structure of the chord by calculating the distance of each of the notes in
relation with the root note. By distance, I mean the number of semitones between
the two notes. This is stored in a set named &amp;#34;&lt;code&gt;intervals&lt;/code&gt;&amp;#34;.&lt;/p&gt;
&lt;p&gt;
Once we have the set of intervals, we can then simply lookup this set in our
database of chord names. &lt;a href="https://github.com/shriramters/chordcat/blob/main/src/chord_db.hpp"&gt;This is a rudimentary &amp;#34;database&amp;#34; of chord names I came
up with&lt;/a&gt;. These names will be our &amp;#34;base names&amp;#34;. We will have to check our
intervals set against each of these chords in our database. The missing or
additional notes will be the accidental notes in the chord (b13, #11 etc..).&lt;/p&gt;
&lt;p&gt;
Here &lt;code&gt;Chord&lt;/code&gt; is a struct representing the actual chord. We store:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;root note,&lt;/li&gt;
&lt;li&gt;extra tones (The notes present in our intervals set but not in the chord&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;database entry)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;omitted tones (The notes present in the database entry but not in our
intervals set)&lt;/li&gt;
&lt;li&gt;num_accidentals (the sum of extra and omitted tones, this is used for sorting)&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="src src-c++"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Chord&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt; root;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; sf&lt;span style="color:#f92672"&gt;::&lt;/span&gt;String base_name;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;vector&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; extra_tones;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;vector&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; omitted_tones;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; num_accidentals;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// support sorting
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;friend&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;operator&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;lt;=&amp;gt;&lt;/span&gt;(Chord &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; a, Chord &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; b) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; a.num_accidentals &lt;span style="color:#f92672"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; b.num_accidentals;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-3" class="outline-3"&gt;
&lt;h3 id="headline-3"&gt;
Sorting?
&lt;/h3&gt;
&lt;div id="outline-text-headline-3" class="outline-text-3"&gt;
&lt;p&gt;
Here is another postulate of this algorithm, the chord name that has fewer
accidentals can be considered a more proper name for the chord.&lt;/p&gt;
&lt;p&gt;
It is for this reason that we are making our chords sortable by
&lt;code&gt;num_accidentals&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;
For instance, consider the chord &lt;code&gt;CEG&lt;/code&gt;. If we consider &lt;code&gt;C&lt;/code&gt; to be the root note,
we can calculate the interval set to be &lt;code&gt;(4, 7)&lt;/code&gt; (because &lt;code&gt;E&lt;/code&gt; is 4 semitones above
&lt;code&gt;C&lt;/code&gt; and &lt;code&gt;G&lt;/code&gt; is 7 semitones above &lt;code&gt;C&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;
Running this &lt;code&gt;(4,7)&lt;/code&gt; against our database, we come up with these candidates for names:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;intervals&lt;/td&gt;
&lt;td&gt;base name&lt;/td&gt;
&lt;td&gt;omitted tones&lt;/td&gt;
&lt;td&gt;extra tones&lt;/td&gt;
&lt;td&gt;final name&lt;/td&gt;
&lt;td class="align-right"&gt;num_accidentals&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(3,7)&lt;/td&gt;
&lt;td&gt;minor&lt;/td&gt;
&lt;td&gt;(3)&lt;/td&gt;
&lt;td&gt;(4)&lt;/td&gt;
&lt;td&gt;C minor (no b3, add 3)&lt;/td&gt;
&lt;td class="align-right"&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(4,7)&lt;/td&gt;
&lt;td&gt;major&lt;/td&gt;
&lt;td&gt;nil&lt;/td&gt;
&lt;td&gt;nil&lt;/td&gt;
&lt;td&gt;C major&lt;/td&gt;
&lt;td class="align-right"&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(4,7,10)&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;(10)&lt;/td&gt;
&lt;td&gt;nil&lt;/td&gt;
&lt;td&gt;C7 (no b7)&lt;/td&gt;
&lt;td class="align-right"&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(4,7,11)&lt;/td&gt;
&lt;td&gt;maj7&lt;/td&gt;
&lt;td&gt;(11)&lt;/td&gt;
&lt;td&gt;nil&lt;/td&gt;
&lt;td&gt;Cmaj7 (no 7)&lt;/td&gt;
&lt;td class="align-right"&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;(3,7,10)&lt;/td&gt;
&lt;td&gt;min7&lt;/td&gt;
&lt;td&gt;(3, 11)&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Cmin7(no b3, no b7, add 3)&lt;/td&gt;
&lt;td class="align-right"&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
Clearly, we can see that the names with fewer accidentals are more fitting (in
our case &lt;code&gt;C major&lt;/code&gt; would be the best name for our set of notes).&lt;/p&gt;
&lt;p&gt;
These accidentals (difference between our interval set and the chord_db entry)
are calculated in our &lt;code&gt;insert_chords&lt;/code&gt; function:&lt;/p&gt;
&lt;div class="src src-c++"&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;insert_chords&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt; root, &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;unsigned&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;short&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&lt;/span&gt; intervals,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;multiset&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;Chord&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&lt;/span&gt; res) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;Chord&lt;span style="color:#f92672"&gt;&amp;gt;&lt;/span&gt; temp;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; (&lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; [name, notes] &lt;span style="color:#f92672"&gt;:&lt;/span&gt; chord_db) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Chord chord &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {};
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; chord.root &lt;span style="color:#f92672"&gt;=&lt;/span&gt; root;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; chord.base_name &lt;span style="color:#f92672"&gt;=&lt;/span&gt; name;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Can be written better using std::ranges::set_difference
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set_difference(notes.begin(), notes.end(), intervals.begin(), intervals.end(),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;back_inserter(chord.omitted_tones));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;set_difference(intervals.begin(), intervals.end(), notes.begin(), notes.end(),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;back_inserter(chord.extra_tones));
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; chord.num_accidentals &lt;span style="color:#f92672"&gt;=&lt;/span&gt; chord.extra_tones.size() &lt;span style="color:#f92672"&gt;+&lt;/span&gt; chord.omitted_tones.size();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; temp.insert(chord);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#f92672"&gt;!&lt;/span&gt;temp.empty())
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; res.insert(&lt;span style="color:#f92672"&gt;*&lt;/span&gt;temp.begin());
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-4" class="outline-3"&gt;
&lt;h3 id="headline-4"&gt;
Why multiset?
&lt;/h3&gt;
&lt;div id="outline-text-headline-4" class="outline-text-3"&gt;
&lt;p&gt;
Going back to our &lt;code&gt;name_that_chord&lt;/code&gt; function, we see that I have used a
&lt;code&gt;multiset&lt;/code&gt; to store all the candidate names instead of a regular &lt;code&gt;set&lt;/code&gt;. The
reason for this is the comparison operator &lt;code&gt;&amp;lt;=&amp;gt;&lt;/code&gt; that we defined in our &lt;code&gt;Chord&lt;/code&gt;
struct.&lt;/p&gt;
&lt;p&gt;
Since it is entirely possible that two chords can have the same number of
accidentals, it is not possible to store both of these chords in a set (if we
define the operator like that). For this reason, we have to use a &lt;code&gt;multiset&lt;/code&gt;
which allows for two or more entries to be on the same level (or in our case,
have the same number of accidentals).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-headline-5" class="outline-2"&gt;
&lt;h2 id="headline-5"&gt;
Conclusion
&lt;/h2&gt;
&lt;div id="outline-text-headline-5" class="outline-text-2"&gt;
&lt;p&gt;In this blog we&amp;#39;ve taken a look at the chord naming algorithm powering
chordcat. The algorithm has these following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Get all the pressed notes (No duplicates, each note an integer between
[0,11]).&lt;/li&gt;
&lt;li&gt;Iterate through the set of notes making each of them the root, one at a time.&lt;/li&gt;
&lt;li&gt;Get the set of intervals (distances of other notes of the set from the
current root)&lt;/li&gt;
&lt;li&gt;Take the two-way set difference of this interval set against our database of
chord names to get the number of accidentals.&lt;/li&gt;
&lt;li&gt;Sort these candidate chord names from the database by the number of
accidentals and take the top result(s).&lt;/li&gt;
&lt;li&gt;Store the top results for each root note in a &lt;code&gt;multiset&lt;/code&gt; to get our final
list of chord names.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Thank you for reading till the end. I hope that you found this interesting and I
encourage you to use this code (GPL-3.0) in your projects as well. I&amp;#39;m also sure
that I might have gotten some things wrong from a music theory
standpoint. Suggestions are always welcome. Please email them to me at s20n AT
ters.dev.&lt;/p&gt;
&lt;p&gt;
Until next time!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description></item><item><title>C++ for the Web with Drogon</title><link>https://blog.s20n.dev/posts/cpp-for-the-web-drogon/</link><pubDate>Fri, 30 Jun 2023 21:23:07 +0530</pubDate><guid>https://blog.s20n.dev/posts/cpp-for-the-web-drogon/</guid><description>&lt;p&gt;&lt;a href="https://drogon.org"&gt;Drogon&lt;/a&gt; is currently &lt;a href="https://www.techempower.com/benchmarks/#section=data-r21"&gt;the fastest web server in the world&lt;/a&gt;. If you already know a bit of C++, it wouldn&amp;rsquo;t hurt to try it out. I personally think drogon is great, however I do find its documentation somewhat lacking which is all the more reason to contribute to them. If you find that drogon is right for you, please consider &lt;a href="https://github.com/drogonframework/drogon"&gt;contributing to / sponsoring their project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this blog, we&amp;rsquo;ll see how we can set up a basic REST API using drogon and CMake.&lt;/p&gt;
&lt;p&gt;The code snippets I&amp;rsquo;ve provided apply to macOS and Linux. However, you can still follow along if you&amp;rsquo;re on windows and have worked with CMake and VS before.&lt;/p&gt;
&lt;h2 id="project-setup"&gt;Project Setup&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re like me and you don&amp;rsquo;t mind using CMake with submodules, we can set up drogon like so:&lt;/p&gt;
&lt;p&gt;Get the pre-requisite libraries installed in accordance with &lt;a href="https://github.com/drogonframework/drogon/wiki/ENG-02-Installation"&gt;the official documentation&lt;/a&gt;. For macOS, I had to install &lt;code&gt;ossp-uuid&lt;/code&gt; and &lt;code&gt;jsoncpp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Create a CMakeLists.txt in your project root.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;touch CMakeLists.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Initialize Git in your project root.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git init
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Add drogon as a submodule&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git submodule add https://github.com/drogonframework/drogon.git external/drogon
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Pull drogon&amp;rsquo;s submodules (trantor)&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;git submodule update --init --recursive
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At this point you&amp;rsquo;ve got your dependencies setup. Let&amp;rsquo;s go ahead and add some code for our basic REST API.&lt;/p&gt;
&lt;p&gt;Create a new directory for our source files&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkdir src
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And add our entrypoint&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;touch src/main.cpp
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here&amp;rsquo;s a basic &lt;code&gt;CMakeLists.txt&lt;/code&gt; file that will build our project&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-cmake" data-lang="cmake"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cmake_minimum_required(&lt;span style="color:#e6db74"&gt;VERSION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;3.25.2&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;project(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;drogontutorial&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;VERSION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;0.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;DESCRIPTION&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Drogon CMake Tutorial&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;LANGUAGES&lt;/span&gt; &lt;span style="color:#e6db74"&gt;CXX&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;# compiler flags
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_STANDARD&lt;/span&gt; &lt;span style="color:#e6db74"&gt;20&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_STANDARD_REQUIRED&lt;/span&gt; &lt;span style="color:#e6db74"&gt;ON&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;set(&lt;span style="color:#e6db74"&gt;CMAKE_CXX_FLAGS&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;${CMAKE_CXX_FLAGS} -Wall -Wextra&amp;#34;&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;add_executable(&lt;span style="color:#f92672"&gt;${&lt;/span&gt;PROJECT_NAME&lt;span style="color:#f92672"&gt;}&lt;/span&gt; &lt;span style="color:#e6db74"&gt;src/main.cpp&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;add_subdirectory(&lt;span style="color:#e6db74"&gt;external/drogon&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#960050;background-color:#1e0010"&gt;&lt;/span&gt;target_link_libraries(&lt;span style="color:#f92672"&gt;${&lt;/span&gt;PROJECT_NAME&lt;span style="color:#f92672"&gt;}&lt;/span&gt; &lt;span style="color:#e6db74"&gt;PRIVATE&lt;/span&gt; &lt;span style="color:#e6db74"&gt;drogon&lt;/span&gt;)&lt;span style="color:#960050;background-color:#1e0010"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And this will be our &lt;code&gt;src/main.cpp&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#include&lt;/span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;drogon/drogon.h&amp;gt;&lt;/span&gt;&lt;span style="color:#75715e"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;main&lt;/span&gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;using&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;namespace&lt;/span&gt; drogon;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; app().setLogPath(&lt;span style="color:#e6db74"&gt;&amp;#34;./&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .setLogLevel(trantor&lt;span style="color:#f92672"&gt;::&lt;/span&gt;Logger&lt;span style="color:#f92672"&gt;::&lt;/span&gt;kWarn)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .addListener(&lt;span style="color:#e6db74"&gt;&amp;#34;0.0.0.0&amp;#34;&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;8080&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .setThreadNum(&lt;span style="color:#ae81ff"&gt;8&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .registerHandler(&lt;span style="color:#e6db74"&gt;&amp;#34;/hello?name={name}&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; [](&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; HttpRequestPtr&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; req,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;function&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;void&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; HttpResponsePtr&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&amp;amp;&lt;/span&gt; callback,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;string&lt;span style="color:#f92672"&gt;&amp;amp;&lt;/span&gt; name)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Json&lt;span style="color:#f92672"&gt;::&lt;/span&gt;Value json;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; json[&lt;span style="color:#e6db74"&gt;&amp;#34;result&amp;#34;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;ok&amp;#34;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; json[&lt;span style="color:#e6db74"&gt;&amp;#34;message&amp;#34;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; std&lt;span style="color:#f92672"&gt;::&lt;/span&gt;string(&lt;span style="color:#e6db74"&gt;&amp;#34;hello,&amp;#34;&lt;/span&gt;) &lt;span style="color:#f92672"&gt;+&lt;/span&gt; name;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;auto&lt;/span&gt; resp &lt;span style="color:#f92672"&gt;=&lt;/span&gt; HttpResponse&lt;span style="color:#f92672"&gt;::&lt;/span&gt;newHttpJsonResponse(json);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; callback(resp);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { Get,&lt;span style="color:#e6db74"&gt;&amp;#34;LoginFilter&amp;#34;&lt;/span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; .run();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="building-and-running-the-app"&gt;Building and Running the App&lt;/h2&gt;
&lt;p&gt;So far your directory structure should look something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── CMakeLists.txt
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;├── external
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;│ └── drogon
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;└── src
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; └── main.cpp
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;4&lt;/span&gt; directories, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; files
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Go ahead and create a directory to house our build files&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkdir build &lt;span style="color:#f92672"&gt;&amp;amp;&amp;amp;&lt;/span&gt; cd build
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now lets generate the Makefile using&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;cmake ..
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and now to compile our application,&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;make -j&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;nproc&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hopefully after all this, you should find an executable called &lt;code&gt;drogontutorial&lt;/code&gt; in the build directory.&lt;/p&gt;
&lt;p&gt;let&amp;rsquo;s run this file to start our server.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;./drogontutorial
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, your server should be listening on port 8080. Test it out by sending your name to &lt;code&gt;/hello&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;curl http://localhost:8080/hello?name&lt;span style="color:#f92672"&gt;=&lt;/span&gt;yourname
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And just like that you&amp;rsquo;ve got a starter project to write your own REST APIs in C++.&lt;/p&gt;
&lt;p&gt;If you find this nice, do check out their &lt;a href="https://github.com/drogonframework/drogon/tree/master/examples"&gt;examples on github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Until next time!&lt;/p&gt;</description></item><item><title>A Shallow Dive Into Webassembly</title><link>https://blog.s20n.dev/posts/a-shallow-dive-into-webassembly/</link><pubDate>Fri, 18 Mar 2022 19:22:00 +0530</pubDate><guid>https://blog.s20n.dev/posts/a-shallow-dive-into-webassembly/</guid><description>&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/WA.png" alt="Dive into WebAssembly"&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve been following the buzz in the past few years, you must have certainly heard about &lt;a href="https://webassembly.org"&gt;WebAssembly&lt;/a&gt;, the new, low-level, general purpose programming language that can be used to compile code for the web.
Webassembly was created by Mozilla and Google to be &lt;a href="https://medium.com/@torch2424/webassembly-is-fast-a-real-world-benchmark-of-webassembly-vs-es6-d85a23f8e193"&gt;extremely fast&lt;/a&gt;, safe and to bring near native performance to code running in the browser.&lt;/p&gt;
&lt;p&gt;Webassembly has been designed with performance in mind. It has been built on top of &lt;a href="https://github.com/llvm/llvm-project"&gt;LLVM&lt;/a&gt; which means it will have access to all of LLVM’s optimizations and features.&lt;/p&gt;
&lt;p&gt;Webassembly was created by the WebAssembly Community Group in 2015. The goal of Webassembly is to provide a compilation target that has a similar level of performance as native machine code but with the high-level convenience of JavaScript. It is supported by &lt;a href="https://webassembly.org/roadmap/"&gt;all 4 of the major browser engines&lt;/a&gt; today.&lt;/p&gt;
&lt;h2 id="but-why-webassembly"&gt;But Why WebAssembly?&lt;/h2&gt;
&lt;p&gt;With the near-native performance promised by WebAssembly, It is quite possible to &lt;a href="https://www.infoq.com/news/2019/07/doom3-web-assembly-port/"&gt;run really graphically intensive games&lt;/a&gt; right in the browser.
You surely must have used &lt;a href="https://www.figma.com"&gt;Figma&lt;/a&gt; or atleast heard of it. It&amp;rsquo;s a really powerful Graphic Design app which runs flawlessly, purely on the browser. The reason for its amazing performance is that It is powered by WebAssembly.
Infact I am pretty confident that in the near future Audio/Video and Photo editing on the Web could go mainstream with such a leap in performance.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/WOW.png" alt="WOW"&gt;&lt;/p&gt;
&lt;h2 id="assemblyscript"&gt;AssemblyScript&lt;/h2&gt;
&lt;p&gt;In order to write WebAssembly Code which runs in your browser, you don&amp;rsquo;t actually need to learn WebAssembly syntax. WebAssembly is better thought of as a compilation target for other higher level languages which are easier to write code in.
In fact, if you know Typescript, you can jump right in to &lt;a href="https://www.assemblyscript.org/"&gt;AssemblyScript&lt;/a&gt; which has &lt;a href="https://www.npmjs.com/package/assemblyscript"&gt;approximately 13000 npm downloads each week&lt;/a&gt; at the time of this publication.&lt;/p&gt;
&lt;p&gt;But if you&amp;rsquo;re feeling really adventurous, you really gotta try compiling raw C++ into WebAssembly. Yes you heard me right, It is indeed possible to cause a &lt;a href="https://stackoverflow.com/questions/55583474/how-do-i-debug-an-emscripten-segmentation-fault-due-to-exceeding-the-available-d"&gt;segfault in the browser&lt;/a&gt; with your code!&lt;/p&gt;
&lt;h2 id="running-c-in-the-browser"&gt;Running C++ in the browser&lt;/h2&gt;
&lt;p&gt;With WebAssembly, it is possible to call a C++ function in the browser. You could use a toolchain like &lt;a href="https://emscripten.org"&gt;Emscripten&lt;/a&gt; to do this but it is completely possible to compile C++ directly into WebAssembly&amp;rsquo;s binary format &lt;code&gt;wasm&lt;/code&gt; by setting &lt;code&gt;wasm32&lt;/code&gt; as the target for clang++.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s write a function that does bitwise left-shifts and yields the nth power of 2&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;extern&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;C&amp;#34;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;long&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;powerOf2&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; (&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&amp;lt;&lt;/span&gt; n);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;C++ does &lt;a href="https://en.wikipedia.org/wiki/Name_mangling"&gt;Name Mangling&lt;/a&gt; which changes the name of the exported function and we won&amp;rsquo;t be able to call it from the browser if that happens. To suppress this behaviour, we need to wrap the function in &lt;code&gt;extern &amp;quot;C&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The C++ file can be compiled with the command :&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;clang++ --target&lt;span style="color:#f92672"&gt;=&lt;/span&gt;wasm32 --no-standard-libraries -Wl,--export-all -Wl,--no-entry -o power.wasm power.cpp
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--target=wasm32&lt;/code&gt; sets the target architecture to WebAssembly&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--no-standard-libraries&lt;/code&gt; because this target does not support libc++ (discussed later)&lt;/li&gt;
&lt;li&gt;&lt;code&gt; -Wl,--export-all&lt;/code&gt; exports all symbols (the function &lt;code&gt;powerOf2&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-Wl,--no-entry&lt;/code&gt; tells the compiler that there is no &lt;code&gt;main&lt;/code&gt; function&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep in mind that you&amp;rsquo;re going to need clang++ and thus need to have LLVM installed.
You could download the prebuilt packages depending on your OS or &lt;a href="https://llvm.org/docs/GettingStarted.html"&gt;build llvm from source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;once your wasm binary is generated, you can simply call that function in your webpage like so&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;meta&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;charset&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;utf-8&amp;#34;&lt;/span&gt; /&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;WASM instantiateStreaming() test&amp;lt;/&lt;span style="color:#f92672"&gt;title&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;WebAssembly&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;instantiateStreaming&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;power.wasm&amp;#34;&lt;/span&gt;)).&lt;span style="color:#a6e22e"&gt;then&lt;/span&gt;((&lt;span style="color:#a6e22e"&gt;obj&lt;/span&gt;) =&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;obj&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;instance&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;exports&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;powerOf2&lt;/span&gt;(&lt;span style="color:#ae81ff"&gt;62&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; );
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/&lt;span style="color:#f92672"&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/instantiate-streaming.html"&gt;Code Based on source: WebAssembly JS-API documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here, We are calling the &lt;code&gt;powerOf2&lt;/code&gt; function with an argument of &lt;code&gt;62&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;You can check the output in the console showing &lt;code&gt;1073741824n&lt;/code&gt; and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"&gt;&lt;code&gt;n&lt;/code&gt;
indicates&lt;/a&gt; that the number is infact a BigInt, which is right as we return a &lt;code&gt;long long&lt;/code&gt; from the C++ function.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/console-wa-output.png" alt="Console Output"&gt;&lt;/p&gt;
&lt;p&gt;As mentioned before, libc++ is not supported by the target &lt;code&gt;wasm32&lt;/code&gt;, but in case you want to use it, you could target &lt;code&gt;wasi&lt;/code&gt; &lt;a href="https://stackoverflow.com/a/67174440/11335534"&gt;refer to this answer on stackoverflow&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And That&amp;rsquo;s it! You&amp;rsquo;ve successfully run C++ in your browser.
It&amp;rsquo;s time now to port all of your legacy code over to the browser. Hope you could follow along with this. Happy memory leaks and race conditions!&lt;/p&gt;</description></item><item><title>Headless CMS vs Traditional CMS</title><link>https://blog.s20n.dev/posts/headless-cms-vs-traditional-cms/</link><pubDate>Tue, 22 Feb 2022 17:28:00 +0530</pubDate><guid>https://blog.s20n.dev/posts/headless-cms-vs-traditional-cms/</guid><description>&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/headless.png" alt="Totally Headless Nick"&gt;&lt;/p&gt;
&lt;p&gt;Today is Tue, 22/02/2022. Happy Twosday!&lt;/p&gt;
&lt;p&gt;A CMS or Content Management System is a piece of software for digital content management, it can also be used as an enterprise wide digital asset management system, which enables you to manage your digital assets (images, videos, documents and other such files). The CMS comes with a web browser based user interface that allows users to upload content, edit their media files and to control their website.&lt;/p&gt;
&lt;p&gt;Headless CMS is a new trend in the CMS world. It is a backend-only solution that provides an API to interact with the content. This type of CMS is often used by developers who want to build their own frontend and don&amp;rsquo;t want to deal with the complexity of the backend frameworks.&lt;/p&gt;
&lt;p&gt;In contrast, traditional CMSs are more common for small businesses who need a simple way to manage their website content. They are also easier for non-technical users who just want to update their website without any coding knowledge.&lt;/p&gt;
&lt;h2 id="why-do-you-need-a-cms"&gt;Why do you need a CMS?&lt;/h2&gt;
&lt;p&gt;Using a CMS abstracts away a lot of technical intricacies from the content creator.
CMSs are specifically designed so that webmasters or site managers can create, modify and organise digital resources in various ways and for various levels of functionality or to provide search engine optimisation (SEO), online marketing automation processes, online sales opportunities, or self-service tools for their customers.&lt;/p&gt;
&lt;p&gt;CMSs have changed the world of web development, it has changed the way in which websites are designed and managed. &lt;a href="https://whatcms.org/Tech_Reports"&gt;Over 15 million domains&lt;/a&gt; on the internet run on some CMS technology like Wordpress, Drupal, Joomla, Shopify to name a few.&lt;/p&gt;
&lt;h2 id="why-headless"&gt;Why Headless?&lt;/h2&gt;
&lt;p&gt;Now, you might be thinking, if it&amp;rsquo;s way easier to use a traditional CMS like wordpress, why on earth would you want to go through the ordeal of decapitating the CMS and having to write your own frontend?&lt;/p&gt;
&lt;p&gt;Well, The main benefit of Headless CMS is that it can be used to create an API for your website, which means you can have more control over your data and how it&amp;rsquo;s displayed on your site.&lt;/p&gt;
&lt;p&gt;This type of CMS also has many other benefits:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;
because there is no frontend interface, the server doesn&amp;rsquo;t have to process as much data and can therefore handle more requests at once. Headless CMSs are also faster than traditional CMSs because they don&amp;rsquo;t need to load all of the content on every page request, which saves time and resources on both server and client side.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Flexibility:&lt;/strong&gt;
You can use any frontend you want, which means developers can choose the best one for their project. This also means that you don&amp;rsquo;t have to worry about updating the frontend when there are changes in the backend.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Future-proof-ness:&lt;/strong&gt;
There is no guarantee that the epic looking theme you chose on wordpress or that wordpress itself will still be in the vogue in the future. Does that mean your content is not valuable anymore? Heck no, your content should not die with the platform and using an API like the ones in Headless CMS will ensure that the content can always be shown wherever you please.&lt;/p&gt;
&lt;h2 id="jamstack-and-the-rise-of-headless"&gt;JAMstack and the rise of Headless&lt;/h2&gt;
&lt;p&gt;JAM stands for Javascript, API and Markup. It refers to the approach of decoupling the frontend, backend and the database which you would find on a traditional website and distributing it over these three services.&lt;/p&gt;
&lt;p&gt;Basically, a Javascript Application retrieves the content from a latent Headless CMS through an API in the form of pre-rendered static markup.
This lets them condense the entire app and serve it over a CDN which leads to blazing fast performance and easily scalable applications.&lt;/p&gt;
&lt;p&gt;All this has lead to the rapid adoption of JAMstack applications through platforms like Vercel, Netlify, Github Pages etc. and along with that, brought forth a rise in popularity for Headless CMS platforms.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/jamstack.png" alt="JAM Stack"&gt;&lt;/p&gt;
&lt;h2 id="so-whats-the-catch-chief"&gt;So What&amp;rsquo;s the Catch, Chief?&lt;/h2&gt;
&lt;p&gt;Well, the main disadvantage of headless CMS is that it requires more technical knowledge than traditional CMS. This means that it might not be suitable for beginners who are just starting out with web development.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.s20n.dev/images/Awsbill.jpeg" alt="Awsbill.jpeg"&gt;&lt;/p&gt;
&lt;p&gt;Apart from the complexity, there&amp;rsquo;s a cost factor too. You know that wordpress hosting is quite affordable nowadays and the websites who provide really good offers on shared hosting are a dime a dozen. However, you&amp;rsquo;re going to have to shell out a little bit more if you are going to opt for a headless CMS solution like Strapi. Not to mention the cost of development if you are not really well versed with the tooling.&lt;/p&gt;</description></item></channel></rss>