<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Interested in Cryptography]]></description><link>https://caglos.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 16:16:55 GMT</lastBuildDate><atom:link href="https://caglos.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mathematical Cryptography II]]></title><description><![CDATA[Group Theory
A group is a set G combined with a binary operation * that satisfies the following four properties:

Identity Law

There is an e ∈ G such that e \ a = a * e = a for every a ∈ G*.

Inverse Law

For every a ∈ G there is a (unique) a^−1 ∈ G...]]></description><link>https://caglos.hashnode.dev/mathematical-cryptography-ii</link><guid isPermaLink="true">https://caglos.hashnode.dev/mathematical-cryptography-ii</guid><category><![CDATA[Cryptography]]></category><category><![CDATA[Mathematics]]></category><dc:creator><![CDATA[Çağla Çelik]]></dc:creator><pubDate>Mon, 08 Apr 2024 13:20:48 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-group-theory">Group Theory</h2>
<p>A <strong>group</strong> is a set <strong><em>G</em></strong> combined with a binary operation * that satisfies the following four properties:</p>
<ol>
<li><strong>Identity Law</strong></li>
</ol>
<p>There is an <em>e ∈ G</em> such that <em>e \</em> a = a * e = a for every a ∈ G*.</p>
<ol start="2">
<li><strong>Inverse Law</strong></li>
</ol>
<p>For every a ∈ G there is a (unique) a^−1 ∈ G satisfying;</p>
<p><em>a \</em> a^−1 = a^−1 * a = e.*</p>
<ol start="3">
<li><strong>Associative Law</strong></li>
</ol>
<p><em>a \</em> (b * c) = (a * b) * c for all a, b, c ∈ G.*</p>
<ol start="4">
<li><strong>Commutative Law</strong></li>
</ol>
<p><em>a \</em> b = b * a for all a, b ∈ G*</p>
<p><strong>Addition</strong></p>
<ul>
<li><p>Set: All integers <em>{... -2, -1, 0, 1, 2, ...}</em></p>
</li>
<li><p>Operation: +</p>
</li>
<li><p>Identity Element: 0</p>
</li>
<li><p>Inverse Element: for every <em>a ∈ G</em>, its inverse is <em>-a</em></p>
</li>
</ul>
<p><strong>Modular Arithmetic Addition</strong></p>
<ul>
<li><p>Set: Integers modulo <em>n: {0, 1, 2, 3..., n-1}</em></p>
</li>
<li><p>Operation: <em>+ (mod n)</em></p>
</li>
<li><p>Identity Element: 0</p>
</li>
<li><p>Inverse Element: For each <em>a</em>, an integer b such that; <em>a + b = 0 (mod n)</em></p>
</li>
</ul>
<p><strong>Matrix Group Multiplication</strong></p>
<ul>
<li><p>Set: All invertible 2 X 2 matrices over <em>R</em> or <em>C</em></p>
</li>
<li><p>Operation: Matrix multiplication</p>
</li>
<li><p>Identity Element: <em>Identity matrix = I</em></p>
</li>
<li><p>Inverse Element: For a <em>matrix</em> <em>A</em>, its matrix inverse such that; <em>A x A^-1 = I</em></p>
</li>
</ul>
<p>If the operation is commutative, the group is called an <strong>Abelian</strong> or <strong>Commutative</strong> group.</p>
<p>Groups can be finite or infinite. The <strong>order</strong> of a group is the <strong>number of its elements.</strong></p>
<h3 id="heading-what-is-subgroup">What is Subgroup ?</h3>
<p>Let <strong>H ⊂ G a subset. We'll say that "H is a subgroup of G" if;</strong></p>
<ol>
<li><p><em>e ∈ H</em></p>
</li>
<li><p>H is <strong>closed under * operation</strong>;</p>
</li>
</ol>
<p><strong><em>h1, h2 ∈ H</em></strong> <em>⇾</em> <strong><em>h1 \</em> h2 ∈ H*</strong></p>
<ol start="3">
<li>H is <strong>closed under taking inverses;</strong></li>
</ol>
<p><strong><em>h ∈ H</em></strong> <em>⇾</em> <strong><em>h^-1 ∈ H</em></strong></p>
<p>As an example, let's take:</p>
<p><em>g ∈ G, g != e</em></p>
<p><em>|G| &lt;</em> <strong><em>∞</em></strong> (G is a <strong>finite group</strong>)</p>
<p><em>H = {g^k : k ∈ N} = {g, g^2, g^3, g^4, ...}</em></p>
<p><strong>If <em>H</em> is a subgroup of <em>G</em>, it should validate 3 rules above.</strong></p>
<p><strong>Proof of 1st condition</strong></p>
<p><em>G</em> is a finite, so <em>H</em> is also a <strong>finite set</strong>, since <em>H</em> is a subset of <em>G</em>.</p>
<p><em>H = {g, g^2, g^3, g^4, ...}</em></p>
<p>The powers of the g's can not be all different because of <em>g^{n+1} = g^n \</em> g^1 = e * g^1 = g^1<em>. There <em>*must</em></em> be elements like:</p>
<p><em>R = { m ∈ N:</em> <strong><em>g^m = g^j</em></strong> <em>for some</em> <strong><em>1 ≤ j &lt; m</em></strong> <em>}</em></p>
<p>So we can define <em>H</em> like:</p>
<p><em>H = {g, g^2, g^3, g^4, g^j, ... g^(m-1)}</em></p>
<p><em>( N</em>ext element of this set is <em>g^m = g^j</em> )</p>
<p><em>g^m \</em> (g^j)^-1 = e<em>  
</em>g^(m- j) = e*</p>
<p>since j &lt; m, (m - j) <em>∈ N</em></p>
<p><em>e ∈ H</em></p>
<p><strong>Proof of 2nd condition</strong></p>
<p><em>g^l \</em> g^m = g^(l + m)*</p>
<p><strong>Proof of 3rd condition</strong></p>
<p><em>For g^(m- j) = e, we multiply both sides by g;</em></p>
<p><em>g^(m- j) \</em> g = g*</p>
<p><em>g^(m- j + 1) = g</em></p>
<p>m - j + 1 <em>∈ R</em></p>
<p>According to this equation, m must be the smallest element of <em>R</em>. So;</p>
<p><em>m ≤ (m - j + 1) ⇾ j ≤ 1 &amp;&amp; 1 ≤ j &lt; m</em></p>
<p><em>j = 1</em></p>
<p><em>H = {g, g^2, g^3, ... g^(m-1)}</em></p>
<p>Last element's power and also element number of H is (m-1)</p>
<p><strong><em>g^(m- j) = e ∈ H</em></strong></p>
<h3 id="heading-references">References</h3>
<p><a target="_blank" href="https://github.com/isislovecruft/library--/blob/master/cryptography%20%26%20mathematics/An%20Introduction%20to%20Mathematical%20Cryptography%20(2014)%20-%20Hoffstein%2C%20Pipher%2C%20Silverman.pdf">An Introduction to Mathematical Cryptography</a></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=UIhhs38IAGM&amp;list=PLFX2cij7c2PynTNWDBzmzaD6ij170ILbQ&amp;index=5">Cyclic Subgroups</a></p>
]]></content:encoded></item><item><title><![CDATA[Mathematical Cryptography I]]></title><description><![CDATA[Simple substitution ciphers
Simple substitution ciphers are a foundational cryptographic method where each letter in the plaintext is replaced by a letter from a shuffled alphabet. This method is straightforward yet introduces the basic concept of en...]]></description><link>https://caglos.hashnode.dev/mathematical-cryptography-i</link><guid isPermaLink="true">https://caglos.hashnode.dev/mathematical-cryptography-i</guid><category><![CDATA[Cryptography]]></category><category><![CDATA[Mathematics]]></category><dc:creator><![CDATA[Çağla Çelik]]></dc:creator><pubDate>Thu, 28 Mar 2024 11:23:04 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-simple-substitution-ciphers"><strong>Simple substitution ciphers</strong></h3>
<p>Simple substitution ciphers are a foundational cryptographic method where <strong>each letter in the plaintext is replaced by a letter from a shuffled alphabet</strong>. This method is straightforward yet introduces the <strong>basic concept of encrypting a message</strong> to make it unreadable to unintended recipients.</p>
<p><strong>How It Works</strong></p>
<ul>
<li><p>The cipher operates by <strong>creating a one-to-one mapping between the standard alphabet and a mixed alphabet.</strong> Each letter from the plaintext is substituted according to this mapping to produce the ciphertext.</p>
</li>
<li><p>For instance, if the letter 'A' maps to 'L', 'B' to 'M', and so on, the word "HELLO" might be encrypted as <em>"XYZZY"</em> given a specific mapping.</p>
</li>
</ul>
<p><strong>Historical Context: The Caesar Cipher</strong></p>
<ul>
<li><p>Caesar cipher is an <strong>early form of simple substitution cipher</strong>. In a Caesar cipher, the alphabet is shifted a fixed number of places. For example, with a shift of three, 'A' becomes 'D', 'B' becomes 'E', and so forth.</p>
</li>
<li><p>This cipher is named after Julius Caesar, who is historically noted to have used it for confidential communications.</p>
</li>
</ul>
<p><strong>Vulnerabilities: Frequency Analysis</strong></p>
<ul>
<li><p>A significant part of the discussion on simple substitution ciphers involves their vulnerability to frequency analysis. This method of cryptanalysis leverages the fact that <strong>certain letters and combinations of letters appear more frequently than others in a given language</strong>.</p>
</li>
<li><p>For example, <strong>in English, 'E' is the most common letter</strong>, and pairs like <strong>'TH' are common bigram</strong>. By analyzing the frequency of letters and letter pairs in the ciphertext, a cryptanalyst can begin to <strong>deduce the original mappings</strong> and decrypt the message.</p>
</li>
</ul>
<p>Despite their vulnerability, simple substitution ciphers played a crucial role in the early development of cryptographic methods. They introduced the principle of altering plaintext to secure its contents, laying the groundwork for more complex and secure ciphers. The ease with which these ciphers can be broken underscores the need for advancements in cryptographic techniques, leading to the development of more sophisticated encryption algorithms.</p>
<h3 id="heading-divisibility"><strong>Divisibility</strong></h3>
<p>At its core, the concept of divisibility is straightforward yet powerful. An integer <em>b</em> <strong>is said to divide another integer</strong><code>a</code><strong>if there exists an integer</strong> <em>c</em> <strong>such that</strong> <em>a = bc</em>.</p>
<p><strong>Ex</strong>: We have <em>847 | 485331</em>, since <em>485331 = 847 · 573</em>. On the other hand, <em>355 !/ 259943</em>, since when we try to divide 259943 by 355, we get a remainder of <em>83</em>. More precisely, <em>259943 = 355 · 732 + 83</em>, so <em>259943</em> is not an exact multiple of <em>355</em>.</p>
<p>This simple relationship forms the backbone of much more complex mathematical structures encountered in cryptography.</p>
<h3 id="heading-greatest-common-divisor-gcd"><strong>Greatest Common Divisor (GCD)</strong></h3>
<p>The greatest common divisor of <code>a</code> and <code>b</code> is, as its name suggests, <strong>the largest positive integer</strong><code>d</code><strong>such that</strong><code>d | a</code><strong>and</strong><code>d | b</code>. The greatest common divisor of <code>a</code> and <code>b</code> is denoted <code>gcd(a, b</code>). If there is no possibility of confusion, it is also sometimes denoted by <code>(a, b)</code>. If <code>a</code> and <code>b</code> are both 0, then <code>gcd(a, b)</code> is <strong>NOT DEFINED</strong>.</p>
<p><strong>Ex</strong>: Calculation of <em>gcd (748, 2024)</em></p>
<p>Not Efficient Approach: Making lists of all the positive divisors of 748 and of 2024.</p>
<p>Divisors of <em>748 = {1, 2, 4, 11, 17, 22, 34,</em> <strong><em>44*</em></strong>, 68, 187, 374, 748},*</p>
<p>Divisors of <em>2024 = {1, 2, 4, 8, 11, 22, 23,</em> <strong><em>44*</em></strong>, 46, 88, 92, 184, 253, 506, 1012, 2024}.*</p>
<p>Efficient Approach: Division with remainder, which is simply the method of <strong>“long division”</strong>. Thus, if <code>a</code> and <code>b</code> are positive integers and if you attempt to divide <code>a</code> by <code>b</code>, you will get a quotient <code>q</code> and a remainder <code>r</code>, where the remainder <code>r</code> is smaller than <code>b</code>.</p>
<p><strong>Finding GCD with Division</strong></p>
<ol>
<li><p>According to the definition, we can say <code>a = bq + r</code> and <code>0 ≤ r &lt; b</code>. The values <code>q</code> and <code>r</code> are unique for given <code>a</code> and <code>b</code>.</p>
</li>
<li><p>Start by dividing <code>a</code> by <code>b</code> to get the remainder <code>r</code>. Any common divisor of <code>a</code> and <code>b</code> is also a divisor of <code>r</code>, and any common divisor of <code>b</code> and <code>r</code> is a divisor of <code>a</code>. This means the GCD of <em>a</em> and <em>b</em> can be equated to the GCD of <code>b</code> and <code>r</code>. So;</p>
<p> <em>gcd(a, b) = gcd(b, r)</em></p>
</li>
<li><p>Repeat the process, dividing <code>b</code> by <code>r</code> to get another quotient and remainder, reducing the remainder each time until <code>r = 0</code>. The final value <em>gcd(s, 0) = s</em> is equal to the <em>gcd(a, b).</em></p>
</li>
</ol>
<p>We illustrate with an example and then describe the general method, which goes by the name Euclidean algorithm.</p>
<p><em>2024 = 748 · 2 + 528</em></p>
<p><em>748 = 528 · 1 + 220</em></p>
<p><em>528 = 220 · 2 + 88</em></p>
<p><em>220 = 88 · 2 + 44 ⇾ gcd(2024, 748) = 44</em></p>
<p><em>88 = 44 · 2 + 0</em></p>
<p><strong>The Euclidean Algorithm</strong></p>
<p>Let a and b be positive integers with <em>a ≥ b</em>. The following algorithm computes <em>gcd(a, b)</em> in a finite number of steps.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711458627666/8168ac33-f2ae-4fb1-8b7b-807ecd6d35c0.png" alt /></p>
<h3 id="heading-the-discrete-logarithm-problem"><strong>The Discrete Logarithm Problem</strong></h3>
<p>The DLP arises in the context of finite fields, particularly <code>Fp</code>​, which is a field with a prime number of elements <em>p</em>. For a given prime <em>p</em> and a primitive element <code>g</code> in <code>Fp</code>​, the DLP involves finding an exponent <code>x</code> such that:</p>
<p><em>g ^ x ≡ h ( mod p )</em></p>
<p>Here, <code>g</code> is a primitive root for <code>Fp</code>​, and <code>h</code> is a nonzero element of <code>Fp</code>​. The exponent <code>x</code> that satisfies this equation is known as the discrete logarithm of <code>h</code> the base <code>g</code>, denoted by;</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711462420848/de51c39f-02c5-4ca8-8c1f-f4b0bb97216a.png" alt class="image--center mx-auto" /></p>
<p><strong>Mathematical Representation</strong></p>
<ul>
<li>Primitive Element <code>g</code>: A primitive element <code>g</code> of <code>Fp</code>​ is such that every nonzero element of <code>Fp</code>​ can be expressed as a power of <em>g</em>. By Fermat's little theorem;</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711462601073/7d2466c8-49e8-423d-9545-b2a198dda039.png" alt class="image--center mx-auto" /></p>
<p>and no smaller power of <code>g</code> equals 1.</p>
<ul>
<li>Discrete Logarithm <code>x</code>: The discrete logarithm problem seeks the integer <code>x</code> satisfying;</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711463283479/379f15e3-49e9-488e-a984-1fffe0ac930d.png" alt class="image--center mx-auto" /></p>
<ul>
<li>If such an <code>x</code> exists, there are actually infinitely many solutions, since;</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711463629303/b2a4e389-ef8f-4a93-b9ce-2313caef9db3.png" alt class="image--center mx-auto" /></p>
<p>for any integer <code>k</code>, due to the periodic nature implied by Fermat’s little theorem.</p>
<p>The discrete logarithm <code>x</code> is thus defined modulo <em>p−1</em>, acknowledging the cyclic nature of the powers of <code>g</code> in <code>Fp</code>​.</p>
<h3 id="heading-diffie-hellman-key-exchange">Diffie-Hellman Key Exchange</h3>
<p><strong>Alice and Bob need to securely share a secret key over an insecure channel monitored by Eve.</strong> Diffie and Hellman proposed using the discrete logarithm problem's complexity as a possible solution to this challenge.</p>
<p><strong>Mathematical Representation of the Algorithm</strong></p>
<ol>
<li><strong>Public Parameter Creation</strong></li>
</ol>
<p>Alice and Bob start by choosing a <strong>large prime number</strong><code>p</code> and a nonzero <strong>integer</strong><code>g</code><strong>modulo</strong><code>p</code> and share them in public.</p>
<ol start="2">
<li><strong>Private Computations</strong></li>
</ol>
<p>Next, Alice selects a secret integer <code>a</code>, and Bob chooses a secret integer <code>b</code>. They use their secret integers to compute:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711985199098/064c2700-db03-466c-8512-300e5eb3347b.png" alt class="image--center mx-auto" /></p>
<ol start="3">
<li><strong>Public Exchange of Values</strong></li>
</ol>
<p>Alice sends to Bob ⇾ A,</p>
<p>Bob sends to Alice ⇾ B</p>
<ol start="4">
<li><strong>Further Private Computations</strong></li>
</ol>
<p>Bob and Alice again use their secret integers to compute:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711985712518/3e7ab179-9883-4eec-a01e-ee03bf05856c.png" alt class="image--center mx-auto" /></p>
<p>The values that they compute are the same. (A′ = B′)</p>
<p>Current guidelines suggest that Alice and Bob choose a prime <code>p</code> having approximately 1000 bits (i.e., p ≈ 21000) and an element <code>g</code> whose order is prime and approximately p/2. Then Eve will face a truly difficult task. However, Eve can solve the DLP (Discrete Logarithm Problem), then she can compute Alice and Bob’s secret exponents <code>a</code> and <code>b</code> from the intercepted values <code>A</code> and <code>B</code>, and then it is easy for her to compute their shared key gab. (Needs to compute only one of a and b.) But the converse is less clear. Suppose that Eve has an algorithm that efficiently solves the DHP (Diffie-Hellman Problem). Can she use it to also efficiently solve the DLP?</p>
<h3 id="heading-the-elgamal-public-key-cryptosystem">The ElGamal Public Key Cryptosystem</h3>
<p>Although Diffie-Hellman algorithm provides a method of publicly sharing a secret random key, it does not enough to being a public key cryptosystem. The most natural development of a public key cryptosystem following the Diffie–Hellman is a system described by Taher ElGamal in 1985. The ElGamal public key encryption algorithm is based on the DLP and is closely related to Diffie–Hellman key exchange.</p>
<p><strong>Mathematical Representation</strong></p>
<ol>
<li><strong>Public Parameter Creation</strong></li>
</ol>
<p>A trusted party chooses and publishes a large prime p and an element g (instead of Alice and Bob) modulo p of large (prime) order.</p>
<ol start="2">
<li><strong>Key Creation</strong></li>
</ol>
<p>2.1 Alice chose private key <em>1 =&lt; a =&lt; p-1.</em></p>
<p>2.2 Computes <em>A = g ^ a (mod p).</em></p>
<p>2.3 Alice publishes the public key <em>A</em>.</p>
<ol start="3">
<li><strong>Encryption</strong></li>
</ol>
<p>3.1 Bob chooses plaintext <em>m</em>.</p>
<p>3.2 Bob chooses random temporary key <em>k</em>.</p>
<p>3.3 Uses Alice's public key A to compute <em>c1 = g ^ k (mod p)</em></p>
<p>and <em>c2 = m A^k (mod p).</em></p>
<p>3.4 Bob sends ciphertext <em>(c1, c2)</em> to Alice.</p>
<ol start="4">
<li><strong>Decryption</strong></li>
</ol>
<p>Alice compute <code>m</code> with:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1711988954698/db3e3dd8-8bd0-4a59-9e01-30a0a286f4f0.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-how-hard-is-the-dlp">How hard is the DLP ?</h2>
<p>The discrete logarithm problem is a bit like solving a puzzle, where you need to figure out a special number, called an exponent. Imagine you have a specific operation you can do with numbers, and you want to find out how many times you need to repeat this operation on a base number <em>g</em> to get another number <em>h</em>. This is tough because you have to guess and check a lot of possibilities, especially when the numbers involved are massive.</p>
<ol>
<li><p><strong>Guess and Check - Not Practical</strong></p>
<p> If you tried to solve this by simply trying every single possibility one by one, you might end up trying an enormous number of times, especially if the range of possible numbers is huge.</p>
</li>
<li><p><strong>Fast Exponentiation - Still Not Practical</strong></p>
<p> This method starts to feel slow when the numbers get really large, because you still have to do a lot of guesses.</p>
</li>
</ol>
<h2 id="heading-shankss-babystep-giantstep-algorithm">Shanks's BabyStep-GiantStep Algorithm</h2>
<p>This method is designed to solve the DLP more efficiently than simply trying brute force.</p>
<p><strong>How it works ?</strong></p>
<ol>
<li>Divide and Conquer</li>
</ol>
<p>Create a 2 smaller list of possibilities.</p>
<p>Find the exponent x solves the DLP equation.</p>
<ol start="2">
<li>Setting Up Lists</li>
</ol>
<p>Baby Steps: Store the powers of <em>g</em> in a list. <em>(g, g^2, g^3, ...)</em></p>
<p>Giant Steps: Calculate powers of <em>h</em> multiplied by inverse powers of <em>g</em> raised to large increments and store them in another list. <em>(h.g^-n, h.g^-2n, ...)</em></p>
<ol start="3">
<li>Finding a match</li>
</ol>
<p>Looks for a common element between two lists.</p>
<ul>
<li>"Finding a common element" means two calculation methods agree, revealing the solution x. (collision)</li>
</ul>
<ol start="4">
<li>Efficiency</li>
</ol>
<p>This method significantly reduces the number of operations compared to brute force.</p>
<p>In a group of size 10,000, Shanks's Algorithm needs only around 200 steps to find the answer (200^2 = 10.000) but a brute force would require all 10,000 options.</p>
<h2 id="heading-the-chinese-remainder-theorem-crt">The Chinese Remainder Theorem (CRT)</h2>
<p>Imagine you're trying to figure out a number that, when divided by 3, leaves a remainder of 2, and when divided by 5, leaves a remainder of 1. <strong>Doing this by checking each number one by one is so boring and requires a lot of time.</strong> The CRT exists to solve this kind of problem <strong>quickly</strong> and <strong>efficiently</strong> by <strong>finding a number that works for all conditions(aka puzzles) without having to test every single possibility</strong>.</p>
<h3 id="heading-simple-example">Simple Example:</h3>
<p><strong>Puzzle 1:</strong> Event that occurs every 7 days (weekly on a Monday), and today is Monday. Today could be represented as:</p>
<p><em>x = 0 (mod 7)</em></p>
<p><strong>Puzzle 2:</strong> A second event that happens every 5 days, and today it's happening too. Today could be represented as:</p>
<p><em>x = 0 (mod 5)</em></p>
<p>What are the days that both events will happen again on the same day?</p>
<p><strong>CRT comes to play</strong></p>
<p>The CRT tells that both events will align every <em>7×5=35</em> days. So, the next time both events happen on the same day will be 35 days from today.</p>
<h3 id="heading-mathematical-representation">Mathematical Representation</h3>
<p>Looking for an integer x that simultaneously solves both of the puzzles</p>
<p>below:</p>
<ul>
<li><p><em>x = 1 (mod 5)</em></p>
</li>
<li><p><em>x = 9 (mod 11)</em></p>
</li>
</ul>
<p><strong>For the first puzzle, we can say:</strong></p>
<p><em>x = 1 + 5y (y ∈ Z)</em></p>
<p><strong>For the second one;</strong></p>
<p><em>x = 1 + 5y = 9 (mod 11) then, 5y = 8 (mod 11)</em></p>
<p><strong>Finding modulo inverse:</strong></p>
<p><em>(a x b = 1 (mod m))</em></p>
<p>To solve <em>5y = 8 (mod 11)</em>, we need the inverse of 5 modulo 11. This inverse exists because <strong><em>gcd(5, 11) = 1.</em></strong></p>
<p>5 x b = 1 (mod 11).</p>
<p>Because the modulus is so small that we can find it by trial and error.</p>
<p><em>5x9 = 45 = 1 (mod 11)</em></p>
<p>9 is the modular inverse of 5 modulo 11.</p>
<p><strong>Using the Inverse to Solve for <em>y:</em></strong></p>
<p>Given the equation above:</p>
<p><em>5y = 8 (mod 11)</em></p>
<h4 id="heading-multiply-both-sides-by-the-inverse-of-5-modulo-11"><strong>Multiply Both Sides by the Inverse of 5 Modulo 11:</strong></h4>
<p>Multiply both sides of the equation by 9:</p>
<p><em>9×5y = 9×8 (mod 11)</em></p>
<h4 id="heading-simplify-the-equation-amp-solving-y"><strong>Simplify the Equation &amp; Solving y:</strong></h4>
<p>Because <strong>9 x 5 = 1 (mod 11)</strong>, the equation simplifies</p>
<p><em>y = 72 (mod 11) ⇾ y</em></p>
<p><strong>Finding x with y:</strong></p>
<p>Since;</p>
<p><em>x = 1 + 5y</em></p>
<p><em>1 + 5x6 = 31</em></p>
<h3 id="heading-solving-puzzles-with-composite-moduli">Solving puzzles with composite moduli</h3>
<p>Let's find x such that: <em>x^2 = 197 (mod 437)</em></p>
<p>(437 is a <strong>composite number</strong> because 437 = 19 × 23. <strong>Both 19 and 23 are prime numbers</strong>.)</p>
<p><strong>Step 1.</strong></p>
<p><strong>Factor the modulus</strong></p>
<p><em>437 = 19 × 23</em></p>
<p><strong>Step 2.</strong></p>
<p><strong>Apply CRT</strong></p>
<p>Since 437 can be factored into two relatively prime numbers, we can split the original congruence into two separate congruences based on these prime factors:</p>
<ol>
<li><p><em>x^2 = 197 = 7 (mod19)</em></p>
</li>
<li><p><em>x^2 = 197 = 13 (mod 23)</em></p>
</li>
</ol>
<h4 id="heading-step-3">Step 3.</h4>
<h4 id="heading-simplify-the-congruences">Simplify the Congruences</h4>
<ol>
<li><em>x^2 = 7 (mod 19) and x^2 = 197 = 13 (mod 23)</em></li>
</ol>
<p>Because 19 (mod4) = 3, 23 (mod  4)=3;</p>
<p><em>x^2 = 64 ⇾ x = 8 | -8</em></p>
<p><em>x^2 = 36 ⇾ x = 6 | -6</em></p>
<p><strong>Step 4 with choosing positive solutions 8 and 6.</strong></p>
<p><strong>Apply the CRT Again</strong></p>
<p><em>x = 8 (mod 19) and x = 6 (mod 23);</em></p>
<p>Inverse modulo of 6 and 23:</p>
<p><em>x = 23k + 6 = 8 (mod 19)</em></p>
<p><em>23k = 2 (mod 19)</em></p>
<p><em>k = 5</em></p>
<p>Multiply both sides of equation with 5:</p>
<p><em>5 x 23k = 2 x 5 (mod 19) ⇾ k = 10</em></p>
<p>Finally:</p>
<p><em>x = 23 x 10 + 6 = 236 | - 236 for x^2 = 197 (mod 437) equation.</em></p>
<p>If the modulus were prime, there would be only these "two" square roots. However, since <em>437 = 19 · 23</em> is composite, there are two others. In order to find them, we replace one of 8 and 6 with its negative. This leads to the values <em>x = 144 and x = 293.</em></p>
<p><strong>This means that 197 has 4 square roots modulo 437.</strong></p>
<h3 id="heading-references">References</h3>
<p><a target="_blank" href="https://github.com/isislovecruft/library--/blob/master/cryptography%20%26%20mathematics/An%20Introduction%20to%20Mathematical%20Cryptography%20(2014)%20-%20Hoffstein%2C%20Pipher%2C%20Silverman.pdf">An Introduction to Mathematical Cryptography</a></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=za9azzh4v9A">The Discrete Logarithm Problem</a></p>
<p><a target="_blank" href="https://sites.math.rutgers.edu/~greenfie/gs2004/euclid.html">The Euclidean Algorithm</a></p>
]]></content:encoded></item><item><title><![CDATA[Proxies]]></title><description><![CDATA[Using proxies correctly and securely is a difficult task that requires deep knowledge of the proxy pattern, Solidity, and the EVM.
What ?
A proxy contract is a contract which delegates call to another contract.
Why ?
Smart contracts are typically imm...]]></description><link>https://caglos.hashnode.dev/proxies</link><guid isPermaLink="true">https://caglos.hashnode.dev/proxies</guid><category><![CDATA[Blockchain]]></category><category><![CDATA[Smart Contracts]]></category><category><![CDATA[upgradable smart contracts]]></category><category><![CDATA[proxy]]></category><category><![CDATA[openzeppelin]]></category><dc:creator><![CDATA[Çağla Çelik]]></dc:creator><pubDate>Wed, 25 Oct 2023 14:37:44 GMT</pubDate><content:encoded><![CDATA[<p><strong>Using proxies correctly and securely is a difficult task that requires deep knowledge of the proxy pattern, Solidity, and the EVM.</strong></p>
<p><strong>What ?</strong></p>
<p>A proxy contract is a contract which delegates call to another contract.</p>
<p><strong>Why ?</strong></p>
<p>Smart contracts are typically immutable, but some degree of mutability is necessary for bug fixes and product improvements in blockchain-based software.</p>
<p><strong>Proxy Patterns</strong></p>
<p>In a proxy architecture pattern, a Proxy contract serves as an intermediary for all message calls, directing them to the most recent deployed contract logic. Upgrades involve deploying a new contract version and updating the Proxy to point to the new contract address.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244221525/37464f93-fc28-4a41-a17d-e2876c5eed1d.png" alt class="image--center mx-auto" /></p>
<p><strong>Open Zeppelin’s Proxy Patterns</strong></p>
<ul>
<li><p>Inherited Storage</p>
</li>
<li><p>Eternal Storage</p>
</li>
<li><p>Unstructured Storage</p>
</li>
</ul>
<p>All three patterns <strong>use low-level delegatecalls.</strong> In Solidity, the <code>delegatecall</code> function is available, but it only <strong>provides a success or failure status</strong> and <strong>doesn't enable management</strong> of the returned data.</p>
<ul>
<li><p>When one contract delegates a call to another, the code of the second contract runs within the first contract's context. This ensures that msg.value and msg.sender values are retained, and any storage changes affect the first contract's storage.</p>
</li>
<li><p>Zeppelin's shared <a target="_blank" href="https://github.com/OpenZeppelin/openzeppelin-labs/blob/master/upgradeability_using_eternal_storage/contracts/Proxy.sol"><strong>Proxy</strong> <strong>contract</strong></a> has its own <code>delegatecall</code> function, which returns the value produced by calling the logic contract.</p>
</li>
<li><p>How to manage storage allocation ? With separate contracts for storage and logic, there's a risk of overwriting storage slots. For instance, if the Proxy contract stores the latest logic contract address in a slot, and the logic contract isn't aware of it, it could unintentionally <strong>overwrite critical Proxy data</strong>.</p>
</li>
<li><p>How to ensure that the logic contract does not overwrite state variables that are used in the proxy for upgradeability ?</p>
</li>
</ul>
<p><strong>INHERITED STORAGE PATTERN</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244275900/91f1c827-3357-45cd-ada5-ade54a267e82.png" alt class="image--center mx-auto" /></p>
<p>This approach requires the logic contract to adopt the same storage structure as the proxy contract. This ensures both contracts store the necessary proxy state variables.</p>
<p>During exploration, the idea of a <strong>Registry</strong> <strong>contract</strong> emerged to manage different logic contract versions. To upgrade, you register a new logic contract version in the Registry and instruct the proxy to switch to it. Importantly, the <strong>Registry doesn't impact the storage mechanism</strong> and can be implemented alongside any of the storage patterns.</p>
<p><strong>Registry</strong> <strong>contract</strong>: Recording information about a contract in a special record-keeping contract called the "Registry." This helps the system keep track of different versions of contracts and enables upgrades.</p>
<p><strong>How to Initialize</strong></p>
<ol>
<li><p>Deploy a Registry contract</p>
</li>
<li><p>Deploy an initial version of your contract (v1). Make sure it inherits the Upgradeable contract</p>
</li>
<li><p>Register the address of your initial version to the Registry</p>
</li>
<li><p>Ask the Registry contract to create an UpgradeabilityProxy instance</p>
</li>
<li><p>Call your UpgradeabilityProxy to upgrade to the initial version of the contract</p>
</li>
</ol>
<p><strong>How to Upgrade</strong></p>
<ol>
<li><p>Deploy a new version of your contract (v2) that inherits from your initial version to make sure it keeps the storage structure of the proxy and the one in the initial version of your contract.</p>
</li>
<li><p>Register the new version of your contract to the Registry</p>
</li>
<li><p>Call your UpgradeabilityProxy instance to upgrade to the new registered version.</p>
</li>
</ol>
<h4 id="heading-eternal-storage-pattern">ETERNAL STORAGE PATTERN</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244322603/7bc5c11a-23a1-4325-a3f0-d01d40aff97d.png" alt class="image--center mx-auto" /></p>
<p>In this pattern, there are three important parts:</p>
<ol>
<li><p><strong>Storage Contract</strong>: This contract <strong>defines where data is stored</strong>. Both the <strong>proxy and logic contract</strong> <strong>use this storage</strong> structure to store their data.</p>
</li>
<li><p><strong>Logic Contract</strong>: This is <strong>where the main code and functionality</strong> of your smart contract reside. It <strong>interacts</strong> <strong>with</strong> the data stored in the <strong>Storage Contract</strong>.</p>
</li>
<li><p><strong>Proxy</strong>: The Proxy is like a gateway that <strong>directs calls to the Logic Contract</strong>. It can also introduce its own data <strong>without worrying about conflicts</strong>.</p>
</li>
</ol>
<p><strong>All versions of the Logic Contract should use the same structure defined in the Storage Contract for storing data.</strong></p>
<p><strong>“Proxy Owner”</strong> is just an address that has special privileges.</p>
<p>Only the Proxy Owner can do two things:</p>
<ul>
<li><p><strong>Upgrade</strong>: They can change which Logic Contract the Proxy points to, effectively upgrading the contract's functionality.</p>
</li>
<li><p><strong>Transfer Ownership</strong>: They can transfer ownership of the Proxy to another address, which means giving control over the contract to someone else.</p>
</li>
</ul>
<p>This ensures that only trusted entities have the power to make important changes to the contract, like upgrades or handing over control.</p>
<p><strong>How to Initialize</strong></p>
<ol>
<li><p>Deploy an <strong>EternalStorageProxy</strong> instance</p>
</li>
<li><p>Deploy an initial version of your contract (v1)</p>
</li>
<li><p>Call your EternalStorageProxy instance to upgrade to the address of your initial version</p>
</li>
<li><p>If your logic contract relies on its constructor to set up some initial state, that would have to be redone after its linked to the proxy since the proxy’s storage doesn’t know about those values. <strong>EternalStorageProxy</strong> has a function <code>upgradeToAndCall</code> specifically to <strong>call some function on your Logic Contract to redo the setup after the proxy upgrades to it.</strong></p>
</li>
</ol>
<p><strong>How to Upgrade</strong></p>
<ol>
<li><p>Deploy a new version of your contract (v2) making sure it holds the eternal storage structure.</p>
</li>
<li><p>Call your EternalStorageProxy instance to upgrade to the new version.</p>
</li>
</ol>
<h4 id="heading-unstructured-storage-pattern">UNSTRUCTURED STORAGE PATTERN</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244340774/71bcb897-6d40-42fe-81a6-69936fdeb2c7.png" alt class="image--center mx-auto" /></p>
<p>Similar to Inherited Storage, however, <strong>you don't need the logic contract</strong> to inherit specific state variables for this purpose.</p>
<p>In the proxy contract, you <strong>define a constant variable</strong>. When this variable is <strong>hashed</strong>, it generates a random storage position. <strong>This storage position is where you'll store the address of the logic contract</strong> that the proxy should use for its operations.</p>
<p><strong>The proxy contract essentially uses this hashed variable to determine where in storage it should find the address of the logic contract.</strong></p>
<p><code>bytes32 private constant implementationPosition =  keccak256("org.zeppelinos.proxy.implementation");</code></p>
<p>Because <strong>constant state variables don't use storage slots</strong>, there is <strong>extremely little chance of collision</strong> of this storage slot being used by something else defined in the logic contract.</p>
<p>This pattern allows for upgrades without each new version having to be aware of the proxy's storage structure. It simplifies the process and makes it more flexible. However, it does require that each new version inherits the storage variables from the previous one to ensure they know where the data is stored.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244368424/5b9999cf-7d80-4397-88e2-79f4ff46a0fc.png" alt class="image--center mx-auto" /></p>
<p><strong>How to Initialize</strong></p>
<ol>
<li><p>Deploy an <strong>OwnedUpgradeabilityProxy</strong> instance</p>
</li>
<li><p>Deploy an initial version of your contract (v1)</p>
</li>
<li><p>Call your OwnedUpgradeabilityProxy instance to upgrade to the address of your initial version</p>
</li>
<li><p>If your logic contract relies on its constructor to set up some initial state, that would have to be redone after it's linked to the proxy, since the proxy’s storage doesn’t know about those values. OwnedUpgradeabilityProxy has a function <code>upgradeToAndCall</code> specifically to call some function on your logic contract to redo the setup after the proxy upgrades to it.</p>
</li>
</ol>
<p><strong>How to Upgrade</strong></p>
<ol>
<li><p>Deploy a new version of your contract (v2) making sure it inherits the state variable structures used in previous versions.</p>
</li>
<li><p>Call your OwnedUpgradeabilityProxy instance to upgrade to the address of your new contract version.</p>
</li>
</ol>
<p><strong>IMPORTANT NOTES</strong></p>
<p><strong>Reinitialize Logic Contracts After Proxy Upgrade</strong></p>
<p>If your logic contract <strong>relies on its constructor to set up initial values,</strong> you need to <strong>redo this setup after the proxy upgrades to your logic contract. After upgrading the proxy,</strong> you run an <code>initialize</code> function on the logic contract to set up any necessary initial states.</p>
<p><strong>Compatibility Issue</strong></p>
<p>It <strong>isn’t safe</strong> to <strong>simply add a state variable</strong> because it "<strong>shifts down</strong>" <strong>all the state variables below in the inheritance chain.</strong> This makes the storage layouts incompatible.</p>
<p><strong>Contract Interaction</strong></p>
<p>When you're working with upgradeable smart contracts, you always interact with the contract instances created from the proxy, not with the underlying logic contract. This separation is crucial for upgradeability.</p>
<p><strong>Direct Logic Contract Interaction</strong></p>
<p>While you primarily use contract instances, it's <strong>possible</strong> for a malicious actor to <strong>send transactions directly to the logic contract.</strong> However, this doesn't pose a threat because any changes made to the state of the logic contract <strong>won't affect your contract instances.</strong> Your project doesn't rely on the storage of the logic contracts.</p>
<p><strong>Exception - Selfdestruct</strong></p>
<p>There's an exception to this rule. If a direct call to the logic contract <strong>triggers</strong> a <code>selfdestruct</code> operation, it <strong>will destroy the logic contract.</strong> This action would cause all your contract instances to delegate all calls to an address with no code, effectively breaking them.</p>
<p><strong>Similar Effect with Delegatecall</strong></p>
<p>The same negative effect can occur <strong>if the logic contract contains</strong> a <code>delegatecall</code> operation. If the contract can be manipulated to <code>delegatecall</code> into a malicious contract that contains a <code>selfdestruct</code>, it will lead to the <strong>destruction of the calling contract.</strong></p>
<p><strong>Prohibition</strong></p>
<p>To avoid these issues, it's <strong>crucial not to use either the</strong> <code>selfdestruct</code> <strong>operation or</strong> <code>delegatecall</code> <strong>in your contracts</strong>. These operations can inadvertently <strong>lead to the destruction of contracts</strong> and break your project's functionality.</p>
<p><strong>Avoid Initial Values in Field Declarations</strong></p>
<p>Make sure that <strong>all initial values are set in an initializer</strong> function; <strong>otherwise,</strong> any upgradeable <strong>instances will not have these fields set</strong>.</p>
<p><code>contract MyContract is Initializable {</code></p>
<p><code>uint256 public hasInitialValue;</code></p>
<p><code>function initialize() public initializer {</code></p>
<p><code>hasInitialValue = 42;</code></p>
<p><code>}</code></p>
<p><code>}</code></p>
<p><strong>You Can Define Constant Variables</strong></p>
<p>Because the compiler does not reserve a storage slot for these variables.</p>
<p><code>contract MyContract {</code></p>
<p><code>uint256 public constant hasInitialValue = 42;</code></p>
<p><code>}</code></p>
<p><strong>Don’t Forget to Lock</strong></p>
<p>Do not leave an implementation contract uninitialized. An uninitialized implementation contract can be taken over by an attacker, which may impact the proxy. To prevent the implementation contract from being used, you should invoke the <code>_disableInitializers</code> function in the constructor to automatically lock it when it is deployed.</p>
<h3 id="heading-you-can-not"><strong>YOU CAN NOT</strong></h3>
<ul>
<li><p><strong>Change the order in which the contract STATE VARIABLES declared, nor their type.</strong></p>
</li>
<li><p><strong>Introduce a new variable before existing ones</strong></p>
</li>
<li><p><strong>Remove an existing variable</strong></p>
</li>
<li><p>Add new variables to base contracts.</p>
</li>
</ul>
<p>If you rename a variable, then it will <strong>keep the same value</strong> as before after upgrading.</p>
<p>If you <strong>remove a variable</strong> from the end of the contract, note that the <strong>storage will not be cleared</strong>.</p>
<h3 id="heading-references"><strong>REFERENCES</strong></h3>
<p><a target="_blank" href="https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable">Writing upgradeable contracts</a></p>
<p><a target="_blank" href="https://docs.soliditylang.org/en/v0.4.21/miscellaneous.html#layout-of-state-variables-in-storage">Layout of state variables in storage</a></p>
<p><a target="_blank" href="https://blog.openzeppelin.com/the-transparent-proxy-pattern">The transparent proxy pattern</a></p>
<p><a target="_blank" href="https://blog.openzeppelin.com/proxy-patterns">Proxy patterns</a></p>
<p><a target="_blank" href="https://blog.openzeppelin.com/smart-contract-upgradeability-using-eternal-storage">Smart contract upgradeability using eternal storage</a></p>
<p><a target="_blank" href="https://docs.openzeppelin.com/contracts/4.x/api/proxy#UUPSUpgradeable">UUPS Upgradeable</a></p>
<p><a target="_blank" href="https://eips.ethereum.org/EIPS/eip-1967">EIP-1967</a></p>
<p><a target="_blank" href="https://medium.com/nomic-foundation-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357">Malicious backdoors in Ethereum proxies</a></p>
<p><a target="_blank" href="https://github.com/OpenZeppelin/openzeppelin-labs/tree/master/upgradeability_ownership">Upgradeability ownership</a></p>
]]></content:encoded></item><item><title><![CDATA[Cryptographic Foundations for Blockchain]]></title><description><![CDATA[Hash Functions
A hash function is a keyless algorithm that transforms an input, which can be of varying lengths and representations, into a fixed-size binary string of a specific length, known as the hash message digest.
Given a hash function, suppos...]]></description><link>https://caglos.hashnode.dev/cryptographic-foundations-for-blockchain</link><guid isPermaLink="true">https://caglos.hashnode.dev/cryptographic-foundations-for-blockchain</guid><category><![CDATA[Blockchain]]></category><category><![CDATA[Blockchain technology]]></category><category><![CDATA[Cryptography]]></category><dc:creator><![CDATA[Çağla Çelik]]></dc:creator><pubDate>Sun, 08 Oct 2023 19:30:58 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-hash-functions"><strong>Hash Functions</strong></h2>
<p>A hash function is a <strong>keyless</strong> algorithm that transforms an input, which can be of varying lengths and representations, into a <strong>fixed-size binary string</strong> of a specific length, known as the hash message digest.</p>
<p>Given a hash function, suppose its output is always a binary string of length k. Then the size of the output space is 2^k.</p>
<p>A good hash function has two properties:</p>
<ul>
<li><p>It should be very fast to compute.</p>
</li>
<li><p>It must reduce the chance of ‘collisions’ over the expected input space.</p>
</li>
</ul>
<p>To minimize collisions, a hash function <strong>must distribute its output uniformly</strong> over the output space. (looks like random)</p>
<p>A potential drawback of such hashes is that there could be many collisions (md4, md5, SHA1)</p>
<p>Ex: Hash tables</p>
<p><a target="_blank" href="https://cs.nju.edu.cn/daihp/ns_course/07HaipengDai_Hash&amp;MessageDigests.pdf">Hash &amp; Message Digests</a></p>
<p><a target="_blank" href="https://csrc.nist.gov/projects/hash-functions">Hash Functions</a></p>
<h2 id="heading-cryptographic-hash-functions"><strong>Cryptographic Hash Functions</strong></h2>
<p>It is wanted for a hash function to behave like a <strong>random</strong> function.</p>
<p>In order for a hash function to be called a cryptographic hash function, it has to satisfy the following properties for security:</p>
<p><strong>Resistance</strong></p>
<ol>
<li>Preimage Resistance</li>
</ol>
<ul>
<li><p>Given an output y, it should be hard to find any input x such that: H(x) = y (One-way)</p>
</li>
<li><p>A generic attack would require around 2^n operations</p>
</li>
</ul>
<ol>
<li>Second Preimage Resistance</li>
</ol>
<ul>
<li><p>Given an output y and input x such that H(x) = y, It should be hard to find <strong>another input x'</strong> such that H(x') = y</p>
</li>
<li><p><strong>Theoretically, finding x and x' for the same output is always possible. Because the output size of y is FIXED, but input can be ANYTHING.</strong></p>
</li>
<li><p>A generic attack would require around 2^n operations</p>
</li>
</ul>
<ol>
<li>Collision Resistance</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792162828/bcc023b6-2e4a-445b-b40d-ec95d23bd012.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>It should be hard to find two inputs x and x' such that H(x) = H(x')</p>
</li>
<li><p>A generic attack would require around 2^(n/2) operations due to Birthday Paradox.</p>
</li>
</ul>
<p><strong>NOTE: Birthday Paradox</strong></p>
<p>How many people need to be in a room before it is more likely than not at least two people share a birthday?</p>
<p>p &gt;= 1/2 when t &gt;= 23</p>
<p>We have a collision with p &gt; 1/2</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792521619/d137f932-8bc5-43c3-b589-d83a2e3eb385.png" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=Jn2s1BSMQyM">Understanding Birthday Paradox</a></p>
<p>NOTE: A hash function’s <strong>security</strong> is determined by how easy it is to find <strong>hash collisions</strong>.</p>
<p>Regular hash functions are effective at reducing collisions when inputs are random, but they can be weak against intentional collision attacks. Cryptographic hash functions, the choice for blockchains, excel in resisting collisions even against determined attackers.</p>
<p><a target="_blank" href="https://portswigger.net/daily-swig/researchers-demonstrate-practical-break-of-sha-1-hash-function#:~:text=The%20attack%20%E2%80%93%20put%20together%20using,hash%20value%20or%20message%20digest.">Practical Break of SHA-1</a></p>
<p><a target="_blank" href="https://emn178.github.io/online-tools/sha256.html">SHA256 Playground</a></p>
<p><strong>Every cryptographic hash function is a hash function. However, not every hash function is a cryptographic hash function.</strong></p>
<h2 id="heading-symmetric-encryption-aka-secret-key-encryption"><strong>Symmetric Encryption aka Secret Key Encryption</strong></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792611918/3cf3ef83-b4d3-4ff9-ac38-458e40318750.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Only one secret key for encoding and decoding</p>
</li>
<li><p>Oldest and best-known way within cryptographic algorithms</p>
</li>
<li><p>Fast</p>
</li>
<li><p>Not scalable</p>
</li>
<li><p>Easy to execute and manage (one secret key)</p>
</li>
<li><p>Someone can steal/reach the key</p>
</li>
<li><p>Blowfish, AES, RC5, DES (Data Encryption Standard)</p>
</li>
<li><p>DES → broken because of the <strong>short key → Triple DES</strong> (multi-layered version of DES)</p>
</li>
</ul>
<p><strong>AES (Advance Encryption Standard)</strong></p>
<ul>
<li><p>Uses <a target="_blank" href="https://www.youtube.com/watch?v=x28K21mLcbA&amp;t=439s">block cipher</a></p>
</li>
<li><p><strong>Faster</strong> and <strong>safer</strong> according to DES</p>
</li>
<li><p>Supports 128, 192, 256-bit keys</p>
</li>
<li><p>Longer length and block size according to DES</p>
</li>
<li><p>Resist -&gt; brute force attacks</p>
</li>
</ul>
<p><strong>Key Distribution Problem</strong></p>
<ul>
<li><p>The secret keys must be transported securely.</p>
</li>
<li><p>Reason for the birth of asymmetric encryption.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792843257/7b5476d8-c48a-42af-8ef3-d6add9560d8e.jpeg" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture10.pdf">One of the solutions: Key</a></p>
<p><a target="_blank" href="https://engineering.purdue.edu/kak/compsec/NewLectures/Lecture10.pdf">Distribution Center</a></p>
<h2 id="heading-asymmetric-encryption-aka-public-key-encryption"><strong>Asymmetric Encryption aka Public Key Encryption</strong></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792879805/bf8b5bc9-c195-421b-ae75-2bb6529e20fd.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Founded in 1976</p>
</li>
<li><p>Uses different keys for encryption and decryption</p>
</li>
<li><p>Diffie-Hellman → First asymmetric encryption algorithm</p>
</li>
<li><p>Elliptic Curve Systems, RSA, Code-based Cryptosystems</p>
</li>
</ul>
<p>As a simple example</p>
<p>Postman, Cargo package, Receiver, Postbox</p>
<p>Receiver’s Address: Public key</p>
<p>Postbox’s Key owned by the receiver: Private key</p>
<p><strong>RSA (Rivest–Shamir–Adleman)</strong></p>
<p>A public-key algorithm that is used for key establishment and the generation and verification of digital signatures.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792923867/15069bf2-2795-4dc2-acbc-01da38bda92e.png" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=jm_5BZFVUD4">RSA</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792958855/8467840d-7e05-4c73-b9c7-32a7ff63a75a.png" alt class="image--center mx-auto" /></p>
<p><a target="_blank" href="https://www.researchgate.net/figure/Comparison-between-AES-DES-RSA-and-Blowfish_tbl1_307545373">Comparison between AES, DES, RSA and Blowfish</a></p>
<h3 id="heading-hash-puzzles"><strong>Hash Puzzles</strong></h3>
<p><strong>Hash puzzles</strong> are a game in which one tries to find a nonce (an integer) such that:</p>
<p>H (nonce, data) &lt; T (target difficulty level).</p>
<h3 id="heading-consensus-algorithms">Consensus Algorithms</h3>
<ul>
<li><p>Algorithms that provide data to be negotiated across distributed systems or operations.</p>
</li>
<li><p>Used to provide the “<strong>not to require trust structure”</strong> of the blockchain.</p>
</li>
</ul>
<p><strong>POW</strong> <strong>(Proof Of Work)</strong></p>
<p>A nonce that solves the hash puzzle serves as a proof-of-work.</p>
<ul>
<li><p>Used by Bitcoin</p>
</li>
<li><p>Hard to solve &amp; easy to confirm</p>
</li>
<li><p>Miners need to solve problems to add blocks</p>
</li>
<li><p>The first miner to solve the problem</p>
<p>  the block to the chain.</p>
</li>
<li><p><strong>Almost</strong> 100% protection against DDoS</p>
</li>
<li><p>Problem: The system consumes a lot of energy</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696792987077/a3b0f1ea-2ca6-400e-9bbb-f57c0c461a79.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-mining"><strong>Mining</strong></h3>
<p>Which party solves the puzzle the first?</p>
<ul>
<li><p>The process of searching for a nonce that solves the hash puzzle is called <strong>mining</strong>.</p>
</li>
<li><p>The parties competing to solve the hash puzzle are called <strong>miners</strong>.</p>
</li>
</ul>
<h3 id="heading-forks"><strong>Forks</strong></h3>
<p>“Note that the mining rate is different from the rate at which the ledger grows. Ideally, each new block should lead to the growth of the ledger. However, this is not always the case. For example, i<strong>t is possible that a miner mines a valid block but does not publish it</strong>; in this case, <strong>the ledger does not grow</strong> (this is dishonest behavior, but we must account for it nevertheless). It is also possible that two valid blocks are mined with the same parent block, in which case the ledger grows only by one block.</p>
<p><strong>This can happen if the second hash puzzle is solved before the pertinent miner hears of the previous block.</strong> <strong>After all, it takes a non-zero amount of time for a block to be communicated across the network, especially if the block has a lot of data.</strong></p>
<p>In general, the set of blocks mined at any given point in time form a directed tree, rather than a single chain. <strong>We say that the blockchain has forked when a single block has two or more children blocks.</strong> As such, there could be many forks over time.</p>
<p>What then should the users consider as “the ledger”?</p>
<p><strong>The longest chain rule</strong> states that the longest chain among all published blocks should be treated as the ledger. Thus, users should build a new block and append it to the longest chain that they currently know of.”</p>
<p><strong>The Longest Chain Rule</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696793008364/52b96e6d-544f-4ee9-b63e-56a482bd2486.png" alt class="image--center mx-auto" /></p>
<p>A <strong>chain selection rule</strong> is used to decide which chain is the "correct" chain. Bitcoin uses the "<strong>longest chain</strong>" rule, which means that whichever blockchain is the longest will be the one the rest of the nodes accept as valid and work with.</p>
<p><strong>POS (Proof Of Stake)</strong></p>
<ul>
<li><p>Instead of the computational power required to verify transactions, validators must stake their coins in 32 slots.</p>
</li>
<li><p>PoS systems do not award block rewards, and only transaction costs are given as the minter validates a block.</p>
</li>
</ul>
<p>There are two primary roles for a validator:</p>
<ol>
<li><p>checking new blocks and “attesting” to them if they are valid,</p>
</li>
<li><p>proposing new blocks when selected at random from the total validator pool.</p>
</li>
</ol>
<p><strong>Attacking the system is too costly doesn't mean it can't be attacked.</strong></p>
<p><strong>%51 Attack</strong></p>
<ul>
<li><p>Capturing 51% of the processing power in the system</p>
</li>
<li><p>Costs too much (processing power, electricity)</p>
</li>
<li><p>Hard to do on large networks like Bitcoin</p>
</li>
</ul>
<p><a target="_blank" href="https://bitnodes.io/nodes/all/">Global Bitcoin Nodes 10.08.23</a></p>
<p><a target="_blank" href="https://dci.mit.edu/51-attacks">51% Attacks by MIT</a></p>
<h2 id="heading-digital-signature-algorithmstandard-dsadss"><strong>Digital Signature Algorithm/Standard (DSA/DSS)</strong></h2>
<ul>
<li><p>Digital signatures are cryptographic equivalents of handwritten signatures, verifying the sender of a message.</p>
</li>
<li><p>Due to the sizes of signatures, the hash of the message is signed instead of the message itself.</p>
</li>
<li><p>Signatures must be verifiable.</p>
</li>
<li><p>Provides data integrity, data origin authentication and non-repudiation.</p>
</li>
<li><p>Each message's signature must be unique. Reusing signatures would compromise the system's integrity.</p>
</li>
<li><p>Signatures need to be sufficiently long for unforgeability and security.</p>
</li>
<li><p>Fingerprint, signing a document or file, email or e-state uses digital signatures.</p>
</li>
</ul>
<p>“A short string of data a user produces for a document using a <a target="_blank" href="https://ethereum.org/en/glossary/#private-key">private key</a> such that anyone with the corresponding <a target="_blank" href="https://ethereum.org/en/glossary/#public-key">public key</a>, the signature, and the document can verify that (1) the document was "signed" by the owner of that particular private key, and (2) the document was not changed after it was signed.“</p>
<p><a target="_blank" href="https://ethereum.org/en/glossary/#digital-signatures">DSA By Ethereum</a></p>
<p><a target="_blank" href="https://csrc.nist.gov/glossary/term/dsa">DSA by NIST</a></p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=Bx1v1ujsdTo">DSA/DSS</a></p>
<p><strong>Digital signature algorithms are NOT encryption algorithms.</strong></p>
<h2 id="heading-elliptic-curve"><strong>Elliptic Curve</strong></h2>
<ul>
<li><p>Used to <strong>generate the keys</strong>.</p>
</li>
<li><p>The security of elliptic curve cryptosystems relies on mathematical problems.</p>
</li>
<li><p>Elliptic curves are NOT ellipses but are named as such due to the similarity of their equations to those used in calculating ellipse circumferences.</p>
</li>
<li><p>Shorter key sizes in elliptic curve cryptosystems provide similar security to larger keys in traditional systems, offering better memory usage and performance.</p>
</li>
<li><p>Public keys and signatures are just <strong>points</strong> on an elliptic curve. If both of these points are created from the same <strong>private key</strong>, there will be a <em>geometric connection</em> between them that proves that the person who created the signatures also created the public key.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696793182943/bfe58f4c-7fa6-44a2-a4a8-0622dac5733f.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-ecdsa-elliptic-curve-digital-signature-algorithm"><strong>ECDSA (Elliptic Curve Digital Signature Algorithm)</strong></h2>
<ul>
<li>ECDSA uses an elliptic curve as the basis for a digital signature system.</li>
</ul>
<p><strong>Key Generation</strong></p>
<p>dG = Q</p>
<ul>
<li><p>Private key (d): A large randomly generated number</p>
</li>
<li><p>Public key (Q): The <strong>generator point</strong> G multiplied by this random number</p>
</li>
<li><p>G is the generator point</p>
</li>
</ul>
<p><a target="_blank" href="https://learnmeabitcoin.com/technical/images/ecdsa/point-multiply-public-key.gif">Key Generation</a></p>
<p><strong>Sign</strong></p>
<ul>
<li><p>Random number (k): This introduces an element of randomness in to our signatures, which is important for security. It means that every signature we generate will be different, even if we sign the same message twice.</p>
</li>
<li><p>Message hash (z): This is the <em>hash</em> of the message we want to sign. <a target="_blank" href="https://learnmeabitcoin.com/technical/hash-function">Hashing</a> the message gives us a small and unique fingerprint for it, and it’s more efficient to sign this fingerprint than it is to sign a large blob of data.</p>
</li>
<li><p>Private key (d): The source of a public key</p>
</li>
<li><p>The random point on the curve (r): Take the random number k and multiply it by the generator point to get a random point</p>
</li>
<li><p>A number to accompany the random point (s): This is a unique number created from a combination of the <em>message hash</em> z and d, which is also bound to the random point using r.</p>
</li>
</ul>
<p><a target="_blank" href="https://learnmeabitcoin.com/technical/images/ecdsa/point-sign.gif">Sign</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696793226455/c963759b-a038-4076-997e-99c227f32a4e.png" alt class="image--center mx-auto" /></p>
<p>Digital Signature = [r, s]</p>
<p><strong>Verify</strong></p>
<ul>
<li><p>Public key (Q): This is the public key for the person claiming to have created the signature.</p>
</li>
<li><p>Message: The data that was signed. We can hash it ourselves to get the <em>message hash</em> z.</p>
</li>
<li><p>Signature [r, s]: This is the signature created for the above message, allegedly created by the person who has the private key for the public key.</p>
</li>
</ul>
<p>Use these three pieces of data to <em>calculate two points</em> on the curve:</p>
<ul>
<li><p><strong>Point 1.</strong> Start with the <em>generator point</em> G, and multiply it by <code>inverse(s) * z</code>.</p>
</li>
<li><p><strong>Point 2.</strong> Start with the public key point Q, and multiply it by <code>inverse(s) * r</code>.</p>
</li>
</ul>
<p>Add these points together to give <strong>Point 3</strong>:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696793243610/ca85eb8d-20c0-4886-b4b3-6c252240e618.png" alt class="image--center mx-auto" /></p>
<p><strong>If this third point matches up with the random point given, the signature is valid</strong>.</p>
<p><a target="_blank" href="https://github.com/Blockchains-Princeton/COS-ECE470-fa2022">Principles of Blockchains</a></p>
]]></content:encoded></item></channel></rss>