<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: abhinav the builder</title>
    <description>The latest articles on DEV Community by abhinav the builder (@abhinavmir).</description>
    <link>https://dev.to/abhinavmir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F299577%2Fe803f5c5-5572-4884-9da2-8696f5da833f.png</url>
      <title>DEV Community: abhinav the builder</title>
      <link>https://dev.to/abhinavmir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhinavmir"/>
    <language>en</language>
    <item>
      <title>CTF basics!</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Thu, 11 May 2023 15:17:41 +0000</pubDate>
      <link>https://dev.to/abhinavmir/ctf-basics-5791</link>
      <guid>https://dev.to/abhinavmir/ctf-basics-5791</guid>
      <description>&lt;h2&gt;
  
  
  Quick Reference for most basic CTFs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Basic Syscalls&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;fork&lt;/code&gt; creates a new process by duplicating the calling process, while &lt;code&gt;exec&lt;/code&gt; replaces the current process image with a new process image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fork&lt;/code&gt; creates a child process that is a clone of the parent process, while &lt;code&gt;exec&lt;/code&gt; loads a new program into the current process.&lt;/li&gt;
&lt;li&gt;The child process created by &lt;code&gt;fork&lt;/code&gt; initially shares the same memory space as the parent process, while the &lt;code&gt;exec&lt;/code&gt; system call loads a new program into a new memory space.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fork&lt;/code&gt; returns the process ID of the child process to the parent process, while &lt;code&gt;exec&lt;/code&gt; does not return to the calling process.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fork&lt;/code&gt; is often used to create a new process that performs some task independently of the parent process, while &lt;code&gt;exec&lt;/code&gt; is used to start a new program or to change the behavior of the current process.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;exec&lt;/code&gt; family of system calls includes several variants, such as &lt;code&gt;execl&lt;/code&gt;, &lt;code&gt;execv&lt;/code&gt;, &lt;code&gt;execle&lt;/code&gt;, and &lt;code&gt;execvp&lt;/code&gt;, which differ in the way they accept arguments and how they search for the new program.&lt;/li&gt;
&lt;li&gt;The choice between &lt;code&gt;fork&lt;/code&gt; and &lt;code&gt;exec&lt;/code&gt; depends on the specific needs of the application and the desired behavior of the new process.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;PLTs and GOTs&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;PLT stands for Procedure Linkage Table, and it is a data structure used in dynamic linking to call functions in shared libraries.&lt;/li&gt;
&lt;li&gt;GOT stands for Global Offset Table, and it is another data structure used in dynamic linking to resolve the addresses of functions and data objects in shared libraries.&lt;/li&gt;
&lt;li&gt;When a program calls a function in a shared library, the PLT is used to redirect the call to the GOT, which then resolves the address of the function and redirects the call to the actual function code.&lt;/li&gt;
&lt;li&gt;The PLT and GOT are both created by the linker during the dynamic linking process, and they are stored in a read-only section of the program's memory.&lt;/li&gt;
&lt;li&gt;The use of PLTs and GOTs helps to reduce the size of shared libraries and allows them to be shared across multiple programs in memory, improving memory efficiency.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;x86 Stacks:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The x86 architecture has two stacks: the user stack and the kernel stack.&lt;/li&gt;
&lt;li&gt;The user stack is used to store local variables and function call parameters in user-mode programs.&lt;/li&gt;
&lt;li&gt;The kernel stack is used to store kernel-level variables and function call parameters in kernel-mode programs.&lt;/li&gt;
&lt;li&gt;The stacks grow downwards in memory, with the stack pointer pointing to the top of the stack.&lt;/li&gt;
&lt;li&gt;The stack pointer is maintained in the ESP (Extended Stack Pointer) register.&lt;/li&gt;
&lt;li&gt;Pushing data onto the stack decreases the ESP value, while popping data off the stack increases the ESP value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;x86 Registers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The x86 architecture has several types of registers, including general-purpose, segment, and control registers.&lt;/li&gt;
&lt;li&gt;General-purpose registers are used for arithmetic and logic operations, and include EAX, EBX, ECX, EDX, ESI, EDI, and EBP.&lt;/li&gt;
&lt;li&gt;The instruction pointer (IP) register stores the memory address of the next instruction to be executed.&lt;/li&gt;
&lt;li&gt;Segment registers are used to point to different segments of memory, such as code or data segments.&lt;/li&gt;
&lt;li&gt;Control registers are used to control system behavior, such as the flags register (EFLAGS), which contains status flags that indicate the result of arithmetic and logic operations.&lt;/li&gt;
&lt;li&gt;The x86 architecture also includes several special registers, such as the floating-point unit (FPU) stack and the debug registers, which are used for debugging purposes.

&lt;ul&gt;
&lt;li&gt;Rax - accumulator: arithmetic operations and function return values.&lt;/li&gt;
&lt;li&gt;Rbx - base: memory addressing and as a general-purpose register.&lt;/li&gt;
&lt;li&gt;Rcx - counter: loop iterations and function arguments.&lt;/li&gt;
&lt;li&gt;Rdx - data: Data register used for arithmetic operations and as a general-purpose register.&lt;/li&gt;
&lt;li&gt;Rsi: Source index register used for string operations and memory addressing.&lt;/li&gt;
&lt;li&gt;Rdi: Destination index register used for string operations and memory addressing.&lt;/li&gt;
&lt;li&gt;Rbp: Base pointer register used for accessing function parameters and local variables on the stack.&lt;/li&gt;
&lt;li&gt;Rsp: Stack pointer register used for pointing to the top of the stack.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Stack&lt;/th&gt;
&lt;th&gt;Heap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;Used to store function calls and values&lt;/td&gt;
&lt;td&gt;Used for dynamic memory allocation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data structure&lt;/td&gt;
&lt;td&gt;Last In First Out (LIFO)&lt;/td&gt;
&lt;td&gt;Not organized, data is randomly stored&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory allocation&lt;/td&gt;
&lt;td&gt;Automatic&lt;/td&gt;
&lt;td&gt;Manual, managed by the programmer or operating system&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory allocation/deallocation time&lt;/td&gt;
&lt;td&gt;Very fast&lt;/td&gt;
&lt;td&gt;Slower than stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size limit&lt;/td&gt;
&lt;td&gt;Fixed, smaller than heap&lt;/td&gt;
&lt;td&gt;Not fixed, larger than stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access speed&lt;/td&gt;
&lt;td&gt;Fastest&lt;/td&gt;
&lt;td&gt;Slower than stack&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Stack overflows&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most basic type of attack you must understand - do not skip! Stack overflow is a type of Buffer overflows. Every process has a memory map. Each memory map has 4 areas: Application, Heap, Libraries, Stack. RBP tells you where the stack “ends”, RSP tells you where the stack “starts”. Stack grows in one direction: either “up” or “down” - depending on convention. Every function gets a stack frame.&lt;/p&gt;

&lt;p&gt;Buffer overflow attacks occur when an attacker sends more data than a program has allocated memory for, which can overwrite adjacent memory areas, including the stack. Here is how a buffer overflow attack works on stacks using a simple C example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In C, a stack is used to store local variables and function calls. The stack grows downward from high memory addresses to low memory addresses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A buffer is a temporary storage area in memory that can hold a set amount of data. When an attacker inputs more data than the buffer can hold, it overflows into adjacent memory areas, including the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An attacker can exploit this vulnerability by injecting malicious code into the overflowed buffer, which can alter the program's control flow, hijack its execution, or execute arbitrary code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here is an example vulnerable program in C:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;vulnerable_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Input: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter input: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vulnerable_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this program, &lt;code&gt;vulnerable_function()&lt;/code&gt; copies the &lt;code&gt;input&lt;/code&gt; string into a &lt;code&gt;buffer&lt;/code&gt; that is only 10 bytes long. If the &lt;code&gt;input&lt;/code&gt; string is longer than 10 bytes, it will overflow the buffer and overwrite adjacent memory areas.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An attacker can send a payload to exploit this vulnerability, such as:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"AAAAAAAAAAAAAAAAAAAA&lt;/span&gt;&lt;span class="se"&gt;\xef\xbe\xad\xde&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this payload, the first 20 bytes are &lt;code&gt;A&lt;/code&gt; characters, which will fill up the &lt;code&gt;buffer&lt;/code&gt;. The last four bytes &lt;code&gt;\xef\xbe\xad\xde&lt;/code&gt; will overwrite adjacent memory areas and change the program's control flow to execute arbitrary code. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An attacker can send the payload to the program via input, and it will execute arbitrary code:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter input: AAAAAAAAAAAAAAAAAAAA����
Segmentation fault
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the program crashes due to a segmentation fault caused by the overwritten memory areas. But an attacker can use this technique to execute arbitrary code, which can be malicious and damaging. I’ve kept the example short, but an attacker might use &lt;code&gt;/bin/sh&lt;/code&gt; little endian representation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Little Endian: I’m not talking about my little brother. Little endian is a ridiculous looking way to pass values to x86. This is the following for &lt;code&gt;/bin/sh&lt;/code&gt;. It is least significant bit first.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;shellcode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
          &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// little endian representation of bin/sh payload shellcode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 &lt;em&gt;Reiterating!&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The attacker identifies the memory location of the return address on the stack that controls the program's execution after the vulnerable function completes.&lt;/li&gt;
&lt;li&gt;The attacker prepares a payload that includes a NOP sledge followed by shellcode. A NOP sledge is a series of no-operation instructions (\x90 bytes) that serve as a buffer between the overwritten return address and the shellcode. This buffer is important because it allows the processor to transition smoothly from the return address to the shellcode, without encountering any invalid instructions in the process.&lt;/li&gt;
&lt;li&gt;The attacker calculates the number of NOP bytes needed to reach the shellcode. Let's say the shellcode is 23 bytes long, and the return address is located 32 bytes after the start of the buffer. The attacker would need to add 9 NOP bytes to the payload to reach the shellcode (32 bytes - 23 bytes = 9 bytes).&lt;/li&gt;
&lt;li&gt;The attacker constructs the payload by filling the buffer with A characters (or any other character that can be easily identified in the debugger), followed by the NOP sledge and the shellcode. The return address is set to the location of the NOP sledge within the buffer.&lt;/li&gt;
&lt;li&gt;The attacker sends the payload to the program, which will copy the input string into the buffer and overwrite the return address with the address of the NOP sledge.&lt;/li&gt;
&lt;li&gt;When the vulnerable function completes and tries to return to the overwritten address, it will start executing the NOP sledge, which serves as a buffer to reach the shellcode. The shellcode will then be executed, allowing the attacker to take control of the program and execute arbitrary instructions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Ret2Libc: What if there’s an exit(0)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Return-to-libc is a technique used in computer security to exploit vulnerabilities in a program that allow an attacker to control the contents of the program's stack, even if they cannot execute injected code. The basic idea is to use existing code in the program, specifically functions in the libc library, to perform the attacker's desired actions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt; If the vulnerable function ends with an &lt;code&gt;exit()&lt;/code&gt; call, it will terminate the program, and the attacker will not be able to gain control of the program's execution flow. However, if the attacker can overwrite the &lt;code&gt;exit()&lt;/code&gt; function's return address with the address of their malicious code, they can redirect the program's execution to that code before the program exits, giving them control.&lt;/p&gt;

&lt;p&gt;Here's an example of how an attacker could still use a buffer overflow attack to redirect program execution to their shellcode even if the vulnerable function ends with an &lt;code&gt;exit()&lt;/code&gt; call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;vulnerable_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Input: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// vulnerable function ends with an exit call&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter input: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;vulnerable_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this modified program, the &lt;code&gt;vulnerable_function()&lt;/code&gt; ends with an &lt;code&gt;exit(0)&lt;/code&gt; call. However, an attacker can still exploit the buffer overflow vulnerability to redirect program execution to their shellcode. They can overwrite the &lt;code&gt;exit()&lt;/code&gt; function's return address on the stack with the address of their shellcode.&lt;/p&gt;

&lt;p&gt;To do this, the attacker can construct a payload that includes a "return-to-libc" attack. This type of attack replaces the return address on the stack with the address of a function in a shared library, such as &lt;code&gt;system()&lt;/code&gt; or &lt;code&gt;execve()&lt;/code&gt;, which the attacker can use to execute arbitrary shellcode. Here is an example payload that would execute &lt;code&gt;/bin/sh&lt;/code&gt; using the &lt;code&gt;system()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"AAAAAAAAAAAAAAAAAAAABBBBCCCC&lt;/span&gt;&lt;span class="se"&gt;\x18\xa0\x04\x08\xed\x1d\x83\x04&lt;/span&gt;&lt;span class="s"&gt;/bin/sh"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Payload length: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)())&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this payload, the first 20 bytes are &lt;code&gt;A&lt;/code&gt; characters, which will fill up the &lt;code&gt;buffer&lt;/code&gt;. The next 8 bytes are the original return address of &lt;code&gt;vulnerable_function()&lt;/code&gt;. The next 4 bytes are the address of the &lt;code&gt;system()&lt;/code&gt; function in the program's shared library (this address can be obtained using a debugger or other tools). Finally, the last 8 bytes are the argument to the &lt;code&gt;system()&lt;/code&gt; function, which is the string &lt;code&gt;/bin/sh&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;payload&lt;/code&gt; is executed, it will overflow the &lt;code&gt;buffer&lt;/code&gt; in &lt;code&gt;vulnerable_function()&lt;/code&gt; and overwrite the return address on the stack with the address of the &lt;code&gt;system()&lt;/code&gt; function. The &lt;code&gt;system()&lt;/code&gt; function will be called with the argument &lt;code&gt;/bin/sh&lt;/code&gt;, which will execute a shell with root privileges, giving the attacker complete control over the system.&lt;/p&gt;

&lt;p&gt;So, in summary, even if the vulnerable function ends with an &lt;code&gt;exit()&lt;/code&gt; call, an attacker can still use a buffer overflow attack to redirect program execution to their shellcode by overwriting the return address of the &lt;code&gt;exit()&lt;/code&gt; function on the stack.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Another example:&lt;/em&gt; Let's say we have a vulnerable program that takes input from the user and passes it to a function called &lt;code&gt;strcpy&lt;/code&gt; without any bounds checking. This creates a buffer overflow vulnerability that we can exploit.&lt;/p&gt;

&lt;p&gt;Our goal is to execute a command on the system using the &lt;code&gt;system&lt;/code&gt; function from the libc library, with our own argument string. However, we can't inject any code of our own.&lt;/p&gt;

&lt;p&gt;Instead, we can use the buffer overflow to overwrite the return address on the stack, which determines where the program will jump to after the &lt;code&gt;strcpy&lt;/code&gt; function returns. We can set the return address to point to the &lt;code&gt;system&lt;/code&gt; function in the libc library, and arrange the stack to include our desired argument string.&lt;/p&gt;

&lt;p&gt;So, when the vulnerable program returns from the &lt;code&gt;strcpy&lt;/code&gt; function, it will jump to the &lt;code&gt;system&lt;/code&gt; function instead of continuing with its normal execution. The &lt;code&gt;system&lt;/code&gt; function will then execute the command we specified with our argument string.&lt;/p&gt;

&lt;p&gt;This attack works because the &lt;code&gt;system&lt;/code&gt; function and other useful functions like &lt;code&gt;execve&lt;/code&gt; are commonly used in programs and are present in the libc library, which is linked into virtually all programs. However, modern defenses like stack canaries and non-executable memory have made return-to-libc attacks much more difficult to carry out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return Oriented Programming: Corrupting return address&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Return Oriented Programming (ROP) is a technique used by attackers to execute code in a program by chaining together small snippets of existing code called "gadgets". These gadgets typically end with a "return" instruction, hence the name "return-oriented" programming. By stringing together a series of these gadgets, an attacker can create arbitrary instructions that perform their intended malicious action.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;Here is a simple example of ROP:&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Let's say we have a vulnerable program that takes input from the user and stores it in a buffer without proper bounds checking. The attacker can overflow the buffer with a string of their choice and overwrite the return address on the stack with the address of a gadget in the program. This gadget could be a small piece of code that performs a useful operation, such as writing a specific value to a certain memory location.&lt;/p&gt;

&lt;p&gt;Next, the attacker can continue overflowing the buffer with additional gadgets, each ending with a "return" instruction that jumps to the next gadget in the chain. By chaining together a series of gadgets, the attacker can create arbitrary instructions that perform their intended malicious action, such as spawning a shell or stealing sensitive information.&lt;/p&gt;

&lt;p&gt;For example, let's say the vulnerable program has a gadget that performs the instruction "mov eax, 0xdeadbeef". The attacker could overwrite the return address on the stack with the address of this gadget, and then continue overflowing the buffer with additional gadgets that perform other useful operations, such as "mov ebx, 0xcafebabe" and "int 0x80" to spawn a shell. By chaining together these gadgets, the attacker can create the arbitrary instructions needed to perform their intended action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More videos on this topic&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stacks (&lt;a href="https://www.youtube.com/watch?v=5iQkR69H_1M"&gt;https://www.youtube.com/watch?v=5iQkR69H_1M&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Buffer overflows (&lt;a href="https://www.youtube.com/watch?v=B4v56Ns3QhQ"&gt;https://www.youtube.com/watch?v=B4v56Ns3QhQ&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;ROP exploits (&lt;a href="https://www.youtube.com/watch?v=ltEtjC6phT0"&gt;https://www.youtube.com/watch?v=ltEtjC6phT0&lt;/a&gt;)
– Corrupting the return address on the stack (&lt;a href="https://www.youtube.com/watch?v=V9lMxx3iFWU"&gt;https://www.youtube.com/watch?v=V9lMxx3iFWU&lt;/a&gt;)
– PLT entries and GOTs (&lt;a href="https://www.youtube.com/watch?v=kUk5pw4w0h4"&gt;https://www.youtube.com/watch?v=kUk5pw4w0h4&lt;/a&gt;)
– ctor, dtors
– vtable entries
– Tricking the heap manager using fake chunks (&lt;a href="https://www.youtube.com/watch?v=LsA-bYhPS6s"&gt;https://www.youtube.com/watch?v=LsA-bYhPS6s&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More good stuff-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=t1LH9D5cuK4"&gt;https://www.youtube.com/watch?v=t1LH9D5cuK4&lt;/a&gt; (general format string exploits)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=MrI3X5dNsmo"&gt;https://www.youtube.com/watch?v=MrI3X5dNsmo&lt;/a&gt; (writing payloads)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=HSlhY4Uy8SA"&gt;https://www.youtube.com/watch?v=HSlhY4Uy8SA&lt;/a&gt; (buffer overflow with shellcodes)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In you understand Hindi, The Cyber Expert is a great channel for understanding CTF basics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To be continued.&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Passwords in Solidity</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Fri, 29 Apr 2022 17:55:19 +0000</pubDate>
      <link>https://dev.to/abhinavmir/passwords-in-solidity-4fhj</link>
      <guid>https://dev.to/abhinavmir/passwords-in-solidity-4fhj</guid>
      <description>&lt;h3&gt;
  
  
  Why is it needed?
&lt;/h3&gt;

&lt;p&gt;Suppose you have locked liquidity on-contract which can be liquidated via &lt;code&gt;withdraw()&lt;/code&gt; that depends on a require function and access control. If someone has access to the responsible EOA, they can withdraw funds when needed. A better way to go about this is to also have a password, so that incase of a private key leak, the hacker still has to enter a password. &lt;/p&gt;

&lt;h3&gt;
  
  
  How is it implemented?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;User enters password, gets hashed via Keccak256 and appended with "0x" before the hash string.&lt;/li&gt;
&lt;li&gt;User sets password via constructor when deploying (&lt;code&gt;_setNewPassword()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;User then can decide to check password (&lt;code&gt;_testPassword()&lt;/code&gt;) and enter expected password and new password (since current password will be declared by on-chain data).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Solidity Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;onChainPassword&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;globalPassword&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;_hashedPassword&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;globalPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_hashedPassword&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;_checkPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;_newPassword&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encodePacked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_password&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="n"&gt;globalPassword&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_setNewPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_newPassword&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;decision&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;_setNewPassword&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;_newPassword&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;internal&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;globalPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_newPassword&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Disclaimer
&lt;/h3&gt;

&lt;p&gt;Never put Friday projects on main-net without testing extensively, I'm a scatterbrain.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Off-chain Sorting over on-chain Sorting in Solidity</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Tue, 26 Apr 2022 21:49:28 +0000</pubDate>
      <link>https://dev.to/abhinavmir/off-chain-sorting-over-on-chain-sorting-in-solidity-4of7</link>
      <guid>https://dev.to/abhinavmir/off-chain-sorting-over-on-chain-sorting-in-solidity-4of7</guid>
      <description>&lt;p&gt;This is not a unique approach but something I personally used when I had to shave off a few kilobytes off the contract size and also reduce the gas usage. In my use-case, I needed multiple sorting to decide certain winners, rankings, price distributions and such. &lt;/p&gt;

&lt;p&gt;Now, I don't have access to a backend code to try this on when writing this, maybe I could update this later with some scaffold-eth, but this is how I would possible implement this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Array&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;arrSorted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;_admin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]);&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sortOffChain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arrSorted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getSortedData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;arrData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;_admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;arrSorted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arrData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the backend, the algorithm would look something like this -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;event listener listening for &lt;code&gt;sort()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;calls &lt;code&gt;bubbleSort()&lt;/code&gt; [Or whatever suits you] upon getting triggered&lt;/li&gt;
&lt;li&gt;calls &lt;code&gt;getSortedData()&lt;/code&gt; function with returned value of &lt;code&gt;bubbleSort()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This of course shouldn't be used everywhere, but it is a quick and easy way of sorting data.&lt;/p&gt;

</description>
      <category>solidity</category>
    </item>
    <item>
      <title>Installing Echinda-test on MacOS</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Fri, 15 Apr 2022 23:13:13 +0000</pubDate>
      <link>https://dev.to/abhinavmir/installing-echinda-test-on-macos-c0h</link>
      <guid>https://dev.to/abhinavmir/installing-echinda-test-on-macos-c0h</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; : Install Echidna binaries. We are avoiding building it from scratch. Some other day for that.&lt;/p&gt;

&lt;p&gt;Add your Python modules to your path to allow you to use echidna dependencies. Add this to your &lt;code&gt;~/.zshrc&lt;/code&gt; or &lt;code&gt;~/.bashrc&lt;/code&gt; - or something similar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PATH=$PATH:/Users/abhinavmir/Library/Python/3.9/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add this via &lt;code&gt;source ~/.zshrc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now download the binaries from here: &lt;a href="https://github.com/crytic/echidna/releases"&gt;github.com/crytic/echidna/releases&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now move it into &lt;code&gt;/usr/local/bin&lt;/code&gt; using &lt;code&gt;mv echidna-test /usr/local/bin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once done, add &lt;code&gt;export PATH=$PATH:/usr/local/bin/echidna-test&lt;/code&gt; to &lt;code&gt;~/.zshrc&lt;/code&gt; - now &lt;code&gt;source ~/.zshrc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;pip3 install crytic-compile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;export PATH=$PATH:/Users/&amp;lt;you&amp;gt;/Library/Python/&amp;lt;your version&amp;gt;/bin&lt;/code&gt; - but if you're on Linux, change that to wherever Python binaries are. Restart your shell.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;echidna-test&lt;/code&gt; should run with no issues.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Stolen Notes on EVM (Ethereum Virtual Machine)</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Sat, 02 Apr 2022 23:00:59 +0000</pubDate>
      <link>https://dev.to/abhinavmir/stolen-notes-on-evm-ethereum-virtual-machine-3nn2</link>
      <guid>https://dev.to/abhinavmir/stolen-notes-on-evm-ethereum-virtual-machine-3nn2</guid>
      <description>&lt;p&gt;Author's Notes: These are notes stolen from 10-15 of my favourite articles. I'll cite them some day, but they're easily reverse-searchable. This is probably the best singular resource on EVM apart from the papers themselves.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the EVM? How does it work?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At a high level, Ethereum Virtual Machine is a deterministic function that outputs a certain value for a certain input. The EVM is much like any other computer, but it specializes in running smart contracts. EVM is like JVM - but instead of using any other process virtual machine, on Ethereum we use EVM because we need things like finite determinism and gas-implementations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The compiler design behind EVM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Solidity is a high-level language choice that compiles into bytecodes (machine readable set). Solidity is one of the more popular choices, otherwise Vyper, Flint, even C via ceagle can be your choices. &lt;/p&gt;

&lt;p&gt;Opcodes are low-level human-readable instruction sets that are representative of the deployed/creation bytecode. This creation bytecode is used to generate the runtime bytecode. The following contract is an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.10;
contract MyContract {
    uint i = (10 + 2) * 2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this, this is generated-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"object": "60806040526018600055348015601457600080fd5b50603f8060226000396000f3fe6080604052600080fdfea264697066735822122068ed984f1f49a5710584afd544f61110f039d04d2791e66a88e1d8914ffdff1464736f6c634300080b0033",

"opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x18 PUSH1 0x0 SSTORE CALLVALUE DUP1 ISZERO PUSH1 0x14 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x3F DUP1 PUSH1 0x22 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x0 DUP1 REVERT INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 PUSH9 0xED984F1F49A5710584 0xAF 0xD5 DIFFICULTY 0xF6 GT LT CREATE CODECOPY 0xD0 0x4D 0x27 SWAP2 0xE6 PUSH11 0x88E1D8914FFDFF1464736F PUSH13 0x634300080B0033000000000000 "
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;EVM Architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two types of accounts in Ethereum: EOA (Externally owned accounts) and Smart Contracts. Both are treated similarly by the EVM, have balances and &lt;code&gt;storage&lt;/code&gt;. Here's one of my favourite graphics for this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fhQV7rMA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cypherpunks-core.github.io/ethereumbook/images/evm-architecture.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fhQV7rMA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cypherpunks-core.github.io/ethereumbook/images/evm-architecture.png" alt="" width="880" height="1210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Focus on the lower two blocks. When a transaction caller sends a transactiom, a module to recieve input transaction recieves the input and updates storage, while storing the arguments in the stack with the program counter. The Smart Contract bytecode, with the stack is then fed to the EVM for gas estimation via opcodes. The gas fee is deducted, and the opcode is executed. THe memory is then updated. If the transaction fails, the memory is updated.&lt;/p&gt;

&lt;p&gt;EVM is stack-designed, so stack operations such as pop, push apply to it. The overall architecture is similar to any other FSM and instructions are implemented how they're implemented in other stack-based machines, with obvious differences.&lt;/p&gt;

&lt;p&gt;Different implementations of the Ethereum protocol are implemented in different ways/languages such as Geth, Parity, Mist - with different add-on features, but same core structure of EVM + State + Consensus.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory, Storage, State and such&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Ethereum Virtual Machine has three areas where it can store data- storage, memory and the stack, which are explained in the following paragraphs.&lt;/p&gt;

&lt;p&gt;Each account has a data area called storage, which is persistent between function calls and transactions. Storage is a key-value store that maps 256-bit words to 256-bit words. It is not possible to enumerate storage from within a contract, it is comparatively costly to read, and even more to initialise and modify storage. Because of this cost, you should minimize what you store in persistent storage to what the contract needs to run. Store data like derived calculations, caching, and aggregates outside of the contract. A contract can neither read nor write to any storage apart from its own.&lt;/p&gt;

&lt;p&gt;The second data area is called memory, of which a contract obtains a freshly cleared instance for each message call. Memory is linear and can be addressed at byte level, but reads are limited to a width of 256 bits, while writes can be either 8 bits or 256 bits wide. Memory is expanded by a word (256-bit), when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word). At the time of expansion, the cost in gas must be paid. Memory is more costly the larger it grows (it scales quadratically).&lt;/p&gt;

&lt;p&gt;The data in the Blockchain itself is stored via a Merkle Patricia-Trie tree Data Structure. A Patricia Trie or prefix Tree or radix Tree is an ordered structured tree, which takes the applications of usually the data it stores. A node’s position in the tree defines the key with which that node is associated, which makes tries different in comparison to binary search Trees, in which a node stores a key that corresponds only to that node.&lt;/p&gt;

&lt;p&gt;In different implementations, the state is maintained in different ways. For example in Go-Ethereum, it is done via &lt;code&gt;stateDB.go&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the EVM Turing-complete, why?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Turing complete machines are machines that despite any complexity of problem, will solve a problem given enough time. This is of course, an abstract idea developed using an infinite tape. EVM is quasi-Turing complete system, because the machine executes as far as the gas is provided. Gas is the fundamental fuel for the infrastructure and this solves the halting problem in a way - in that you cannot &lt;em&gt;generally&lt;/em&gt; determine whether an arbitrary problem provided will keep running forever or not, but the gas will not allow any problem to run forever. The gas is calculated on an opcode-basis. For example, to calculate one Keccak256 cryptographic hash it will take 30 gas each time a hash is calculated, plus a cost of 6 more gas for every 256 bits of data being hashed. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is Gas and why is it important for operations in the EVM?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ethereum gas is unique from Bitcoin due to how pieces of code are treated on the platform. On Bitcoin, gas/fee is charged per size unit basis, but since Ethereum allows for complex, decisive code to be run on the platform, it charges gas per operation unit basis. Gas isn't a token, but rather a unit of measurement of work, like joule. Now USD &amp;lt;&amp;gt; Joule value is decided by market rate, but unit of work done is decisive and lawful (one by nature, one by code). If I set a gas fee too low with my transaction (although post update, I must set a baseFee to my txn), no node will choose to pick my transaction and add it to a Block, thus resulting in no change in the Blockchain state - which is the end goal.&lt;/p&gt;

&lt;p&gt;Gas is important because it allows for monetization of operation. Monetization of operation means - 1. Nodes are rewarded for maintaining integrity of node, 2. There is a punishment for malicious code to cause a DOS-like attack. As disccused earlier, the gas feature of the EVM allows for the quasi-Turing complete state of the machine. Sure, you don't have an infinite tape, but gas allows for the scope of usage of tape to be reduced - if that makes sense.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are Jump Tables and why are they relevant for gas consumption?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The entry point to the actual code execution is the &lt;code&gt;run&lt;/code&gt; method of the EVM interpreter. This method is essentially going through the bytecode step by step and, for each opcode, looking up the opcode in a data structure known as jump table– Among other things, this table contains a reference to a Go function that is to be executed to process the instruction. More specifically, an entry in the jump table contains the following fields, which partially refer to other tables in other source code files.&lt;/p&gt;

&lt;p&gt;In a jump table, each entry has a couple of fields, we need to have a look at constant gas and dynamic gas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type operation struct {
    // execute is the operation function
    execute     executionFunc
    constantGas uint64
    dynamicGas  gasFunc
    // minStack tells how many stack items are required
    minStack int
    // maxStack specifies the max length the stack can have for this operation
    // to not overflow the stack.
    maxStack int

    // memorySize returns the memory size required for the operation
    memorySize memorySizeFunc

    halts   bool // indicates whether the operation should halt further execution
    jumps   bool // indicates whether the program counter should not increment
    writes  bool // determines whether this a state modifying operation
    reverts bool // determines whether the operation reverts state (implicitly halts)
    returns bool // determines whether the operations sets the return data content
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;constant gas is the constant gas value of the operation, and dynamic gas is the gas value of the operation as a function of the parameters.&lt;/p&gt;

&lt;p&gt;Since gas is computed from opcode, the library gas usage isn’t considered - since it is computed via jump tables from opcodes. But bytecode undergoes the same linking process as the linking process of libraries in C.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are Precompiled Contracts?&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;These are four so-called ‘precompiled’ contracts, meant as a preliminary piece of architecture that may later become native extensions. The four contracts in addresses 1, 2, 3 and 4 execute the elliptic curve public key recovery function, the SHA2 256-bit hash scheme, the RIPEMD 160-bit hash scheme and the identity function respectively.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Full, updated list-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recovery of ECDSA signature&lt;/li&gt;
&lt;li&gt;Hash function SHA256&lt;/li&gt;
&lt;li&gt;Hash function RIPEMD160&lt;/li&gt;
&lt;li&gt;Identity&lt;/li&gt;
&lt;li&gt;Modular exponentiation (EIP 198)&lt;/li&gt;
&lt;li&gt;Addition on elliptic curve alt_bn128 (EIP 196)&lt;/li&gt;
&lt;li&gt;Scalar multiplication on elliptic curve alt_bn128 (EIP 196)&lt;/li&gt;
&lt;li&gt;Checking a pairing equation on curve alt_bn128 (EIP 197)&lt;/li&gt;
&lt;li&gt;BLAKE2b hash function (EIP 152)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Implemented in &lt;code&gt;geth&lt;/code&gt; as such&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{
    common.BytesToAddress([]byte{1}): &amp;amp;ecrecover{},
    common.BytesToAddress([]byte{2}): &amp;amp;sha256hash{},
    common.BytesToAddress([]byte{3}): &amp;amp;ripemd160hash{},
    common.BytesToAddress([]byte{4}): &amp;amp;dataCopy{},
    common.BytesToAddress([]byte{5}): &amp;amp;bigModExp{},
    common.BytesToAddress([]byte{6}): &amp;amp;bn256Add{},
    common.BytesToAddress([]byte{7}): &amp;amp;bn256ScalarMul{},
    common.BytesToAddress([]byte{8}): &amp;amp;bn256Pairing{},
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Precompiled contracts are not native opcodes, but are calculations that are implemented in the EVM codebase for efficient calculations without EVM overheads. &lt;/p&gt;

&lt;p&gt;Ref 👉&lt;a href="https://ethereum.stackexchange.com/questions/440/whats-a-precompiled-contract-and-how-are-they-different-from-native-opcodes/28469"&gt;https://ethereum.stackexchange.com/questions/440/whats-a-precompiled-contract-and-how-are-they-different-from-native-opcodes/28469&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are the pros and cons of the EVM compared to other VMs?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JVM is another popular process VM - it has it's advantages, of course, but EVM allows for finality and gas-implementation, two important factors of the Ethereum ecosystem. &lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>solidity</category>
    </item>
    <item>
      <title>From Source to Binaries: The journey of a C++ program</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Thu, 15 Oct 2020 18:18:04 +0000</pubDate>
      <link>https://dev.to/abhinavmir/from-source-to-binaries-the-journey-of-a-c-program-4hlj</link>
      <guid>https://dev.to/abhinavmir/from-source-to-binaries-the-journey-of-a-c-program-4hlj</guid>
      <description>&lt;p&gt;If you couldn't already tell, I love Bjarne, and by extension, I love C++. In this article, I go over how C++ compiles a program to binaries, and why I love C++. This is partially to understand how C++ works. Documentation helps me understand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are only two kinds of languages: the ones people complain about and the ones nobody uses.&lt;br&gt;
― Bjarne Stroustrup, The C++ Programming Language&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I got pretty inspired by HaoranWang's &lt;u&gt;&lt;a href="https://github.com/onehr/crust" rel="noopener noreferrer"&gt;CRUST&lt;/a&gt;&lt;/u&gt; and thus wanted to write my own Compiler for C. I'll probably stick to Rust. Also, shoutout &lt;u&gt;&lt;a href="https://github.com/ShivamSarodia/ShivyC" rel="noopener noreferrer"&gt;ShivyC&lt;/a&gt;&lt;/u&gt;, that's where I got this idea from.&lt;/p&gt;

&lt;p&gt;Let's get started with the compilation pipeline!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo3jqrm6f4c4ehgn41hhk.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo3jqrm6f4c4ehgn41hhk.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you see above is the compilation flow taken from &lt;u&gt;&lt;a href="https://nerdyelectronics.com" rel="noopener noreferrer"&gt;NerdyElectronics.com&lt;/a&gt;&lt;/u&gt;. &lt;/p&gt;

&lt;p&gt;For the purpose of this article, we will use a simple addition problem with predefined values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;//a.cpp program&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sumOfTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// sum of two numbers in stored in variable sumOfTwoNumbers&lt;/span&gt;
    &lt;span class="n"&gt;sumOfTwoNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;secondNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Prints sum &lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;firstNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" + "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;  &lt;span class="n"&gt;secondNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" = "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;sumOfTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you go back to the diagram, you can see we are presently on the preprocessing stage. Let's have a quick look at the &lt;u&gt;&lt;a href="https://en.wikipedia.org/wiki/Translation_unit" rel="noopener noreferrer"&gt;Translation Unit&lt;/a&gt;&lt;/u&gt;. Translation Units is the input you give to the compiler, after it includes header files and expands macros.&lt;/p&gt;

&lt;p&gt;You can get your Translation unit dump using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;g++ &amp;lt;filename&amp;gt;.cpp &lt;span class="nt"&gt;-E&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dump looks something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;# 1 "a.cpp"
# 1 "&amp;lt;built-in&amp;gt;"
# 1 "&amp;lt;command-line&amp;gt;"
# 1 "a.cpp"
# 1 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\iostream" 1 3
# 36 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\iostream" 3
&lt;/span&gt;
&lt;span class="cp"&gt;# 37 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\iostream" 3
&lt;/span&gt;
&lt;span class="cp"&gt;# 1 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\mingw32\\bits\\c++config.h" 1 3
# 236 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\mingw32\\bits\\c++config.h" 3
&lt;/span&gt;
&lt;span class="cp"&gt;# 236 "c:\\mingw\\lib\\gcc\\mingw32\\8.2.0\\include\\c++\\mingw32\\bits\\c++config.h" 3
&lt;/span&gt;&lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's too damn long to post here (because it literally adds stdio header file, that's like 1k lines of code), but run it on your system if you're curious.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assembly Code
&lt;/h2&gt;

&lt;p&gt;Detouring for a bit, There's this grapevine that the closer you are to the hardware, the faster you will be. While there is a modicum of truth to this, often "slower" languages like Python are slow because they're interpreted or are memory hogs due to dynamic typing. There are plenty of Python-to-C/C++ compilers and there are plenty of projects that help you do Python "faster". Don't, for the love of God, develop something in a certain language because it is "closer to the hardware".&lt;/p&gt;

&lt;p&gt;Anyway, now run this on the a.cpp file we had&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gpp a.cpp &lt;span class="nt"&gt;-S&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you'll have something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .file   "a.cpp"
    .text
    .section .rdata,"dr"
__ZStL19piecewise_construct:
    .space 1
.lcomm __ZStL8__ioinit,1,1
    .def    ___main;    .scl    2;  .type   32; .endef
LC0:
    .ascii " + \0"
LC1:
    .ascii " = \0"
    .text
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
LFB1502:
    .cfi_startproc
    leal    4(%esp), %ecx
    .cfi_def_cfa 1, 0
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    .cfi_escape 0x10,0x5,0x2,0x75,0
    movl    %esp, %ebp
    pushl   %ecx
    .cfi_escape 0xf,0x3,0x75,0x7c,0x6
    subl    $36, %esp
    call    ___main
    movl    $2, -12(%ebp)
    movl    $4, -16(%ebp)
    movl    -12(%ebp), %edx
    movl    -16(%ebp), %eax
    addl    %edx, %eax
    movl    %eax, -20(%ebp)
    movl    -12(%ebp), %eax
    movl    %eax, (%esp)
    movl    $__ZSt4cout, %ecx
    call    __ZNSolsEi
    subl    $4, %esp
    movl    $LC0, 4(%esp)
    movl    %eax, (%esp)
    call    __ZStlsISt11char_
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, too damn long, try it out on your own system! This will be built for your target architecture. Find out what is your system's architecture as an exercise! Now, Each architecture has a different Instruction Set that is understood by its processor and your compiler splits this into processes: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an &lt;u&gt;&lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;Abstract Syntax Tree&lt;/a&gt;&lt;/u&gt;
&lt;/li&gt;
&lt;li&gt;Generate architecture dependent instructions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's go over what that means.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstract Syntax Trees
&lt;/h2&gt;

&lt;p&gt;Abstract Syntax tree is well, abstract from the target architecture. However, that's not where the "abstract" part of the term comes from. According to the &lt;u&gt;&lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree#Motivation" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/u&gt;, abstract refers to the fact that "it does not refer to every detail appearing in the real syntax, but rather just structural or content related details". ASTs are generated after syntax analysis. All programs can generate an AST. For our code, this is what the AST looks like&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffbqp9ihwp1an23l7adve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffbqp9ihwp1an23l7adve.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's how to do it yourself&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;g++ &lt;span class="nt"&gt;-fdump-tree-all-graph&lt;/span&gt; a.cpp &lt;span class="nt"&gt;-o&lt;/span&gt; a
dot &lt;span class="nt"&gt;-Tpng&lt;/span&gt; a.cpp.013t.cfg.dot &lt;span class="nt"&gt;-o&lt;/span&gt; a.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is built using GraphViz, install it for your command line. You can also copy paste the contents of &lt;code&gt;a.cpp.013t.cfg.dot&lt;/code&gt;  on any online GraphViz visualizer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object File and Linking
&lt;/h2&gt;

&lt;p&gt;Object File has object code, that is essentially machine code (or some intermediate code). It is the "object" of compiling process, as you can see in this classic &lt;u&gt;&lt;a href="https://www.geeksforgeeks.org/phases-of-a-compiler/" rel="noopener noreferrer"&gt;article&lt;/a&gt;&lt;/u&gt;. The reason I didn't use that fancy "Phases of Compiling Process" chart is because it kind of abstracts the real process of compilation. In due time, we will talk about that too. Create your object (.o) file using this, before we go ahead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;g++ a.cpp &lt;span class="nt"&gt;-c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's look at linking, which you do after you create your object files. The object files are linked together to create another object file that is executable. For this, let me divide the program into a header and a main CPP file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;//a.h&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printLinker&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's call that in another file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"a.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printLinker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, to show the linking, let's create another source file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;//We will name this a2.cpp&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printLinker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiling a.cpp would give me a &lt;code&gt;Hello World&lt;/code&gt;, as expected. But we need to see the linking, right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;g++ a.h &lt;span class="nt"&gt;-c&lt;/span&gt;
g++ a.cpp &lt;span class="nt"&gt;-c&lt;/span&gt;
g++ a2.cpp &lt;span class="nt"&gt;-c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we are back to having a .obj and a .gch (precompiled header, if this is not found, the compiler looks for the header). Let's link!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F27kceykouqzq1sy99tdd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F27kceykouqzq1sy99tdd.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc a.o a2.o &lt;span class="nt"&gt;-o&lt;/span&gt; a2.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice, you see how we just called the two object files and compiled them? Now we need to just run a2.exe, it would have printed &lt;code&gt;Hello World&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./a2.exe
Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perfect. If you want to see what lies inside these files, you use &lt;b&gt;nm&lt;/b&gt; tool.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nm a.o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000 b .bss
00000000 d .data
00000000 r .eh_frame
         U ___main
00000015 T _main
         U _printf
00000000 r .rdata
00000000 r .rdata$zzz
00000000 t .text
00000000 T __Z7print_av
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can do the same for the other object file! It is pretty clear what the files contain, it is well sectioned.&lt;/p&gt;

&lt;p&gt;Whew, that was a lot, that's how a compiler compiles. Let me just conclude real quick.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preprocessing&lt;/li&gt;
&lt;li&gt;Compilation&lt;/li&gt;
&lt;li&gt;Assembly&lt;/li&gt;
&lt;li&gt;Linking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's about it, folks! See you around in part 2, which I will update here.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>compilers</category>
    </item>
    <item>
      <title>Gameboy: Everything under the hood.</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Mon, 12 Oct 2020 10:53:11 +0000</pubDate>
      <link>https://dev.to/abhinavmir/gameboy-everything-under-the-hood-1p0b</link>
      <guid>https://dev.to/abhinavmir/gameboy-everything-under-the-hood-1p0b</guid>
      <description>&lt;p&gt;I don't like consumerism. One of my favorite Scholars, Bertrand Russell, once said&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"It is preoccupation with possessions, more than anything else, that prevents us from living freely and nobly."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But you can bet I have a soft spot in my heart for Nintendo products. Pokemon on Game Boy Advance was one the only games I have ever truly enjoyed before being dragged by the dark, drab realities of life. I'd love to have a shelf with Pokemon merch, but I digress.&lt;/p&gt;

&lt;p&gt;Now that we are done with that segment, let's get started by looking at the beautiful, beautiful Game Boy Classic!&lt;/p&gt;

&lt;p&gt;The Game Boy is as old as me, and it has more processing power than me. It has an 8-bit processor called the SHARP LR35902, which is a "hybrid" between Intel 8080 and Zilog Z80. Z80 is considered an enhancement of the 8080. Z80 had an enhanced instruction set that had single-bit addressing, shifts/rotates on memory and registers other than accumulator. You had program looping, program counter relative jumps, block I/O and byte search instructions. Z80 also had new IX and IY index registers and a better interrupt system. SHARP LR3590 had a little bit of both.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;8080's registers are used (Single Register file)&lt;/li&gt;
&lt;li&gt;Z80's Coding Syntax and Instruction Extender was used.&lt;/li&gt;
&lt;li&gt;However, neither IX, IY nor 8080's I/O scheme were used. SHARP used a completely memory scheme, so these were not needed.&lt;/li&gt;
&lt;li&gt;Like 8080, a 8-bit data bus and 16-bit address were used, where a memory of 64 KB of Memory could be addressed. &lt;/li&gt;
&lt;li&gt;The memory map had: Cartridge ROM, WRAM, Display RAM, I/O and interrupt enabler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We had a clock speed of about 4.2 MHz, which is achieved by a Crystal Oscillator. All of this worked on 3V DC, 0.6W supply.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fow7di4xzp038t0qsqnl2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fow7di4xzp038t0qsqnl2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(That's CPU + Power Processing Unit + Audio Processing Unit)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Gameboy's 8-bit CPU is called DMG CPU, equipped with 6 registers of 16 bit each: AF, BC, DE, HL, SP and PC. Here's what they mean - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;AF: AF are two 8-bit registers, A and F. A is the Accumulator (arithmetic operations usually take place here)&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flaxzpvjr3djtgc2w3wvv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flaxzpvjr3djtgc2w3wvv.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;If you dial back to your Microcontrollers class, this is the Von Neumann Architecture and there is the accumulator&lt;/em&gt;&lt;br&gt;
F is the Flag Register, it's a special purpose register where each of the 8-bit have different purposes.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fouj0pccxsl71hxl6cyi5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fouj0pccxsl71hxl6cyi5.png" alt="Flag Register"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Zero Flag (Z):
This bit is set when the result of a math operation
is zero or two values match when using the CP (call If Positive)
instruction.&lt;/li&gt;
&lt;li&gt;Subtract Flag (N):
This bit is set if a subtraction was performed in the
last math instruction.&lt;/li&gt;
&lt;li&gt;Half Carry Flag (H):
This bit is set if a carry occurred from the lower
nibble in the last math operation.&lt;/li&gt;
&lt;li&gt;Carry Flag (C):
This bit is set if a carry occurred from the last
math operation or if register A is the smaller value
when executing the CP instruction.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;BC, HL, DE are General Purpose Registers. They're used for 16-bit operations such as temporary storage, copy during execution of the program. Mostly memory stuff.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;PC is the program counter, which points to the next instruction in the memory. The CPU uses PC to fetch the next operation that needs to be executed.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;SP is the Stack Pointer, which points to the current stack position in the memory. This can get a little hairy, grab your closest notebook if it gets overwhelming. With a fair bit of Googling, if you've followed the article thus far, I'm sure you know what a &lt;a href="https://www.freecodecamp.org/news/data-structures-101-stacks-696b3282980/" rel="noopener noreferrer"&gt;Stack&lt;/a&gt; is.&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr6ipzpqc6pbsecljzb4o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr6ipzpqc6pbsecljzb4o.jpg" alt="Stack"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
The stack in GameBoy usually keeps the variables and return addresses. It is also used to pass arguments to subroutines. So, to push information to the stack you would use PUSH, CALL and RST instructions. POP, RET and RETI are used to take information off the stack. The GameBoy stack pointer (SP) is used to keep track of the top of Stack. Remember, the Stack is a first-in, first-out data structure, so the top of the Stack will be the most recent data.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Now in GameBoy, execution starts at location 0, which is where GameBoy's boot ROM is located. You'll find the first instruction for the cartridges at the location &lt;code&gt;0000000100000000&lt;/code&gt; , and we'll talk more about this in a minute. &lt;/p&gt;

&lt;p&gt;A typical way to fetch the instruction from memory would look like this charming little self-explanatory pseudocode.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="nx"&gt;program_Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0100&lt;/span&gt; 
&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch_Operation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;no_Operation&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;}&lt;/span&gt;

&lt;span class="c1"&gt;//The fetch_Operation would be&lt;/span&gt;
&lt;span class="nx"&gt;define&lt;/span&gt; &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;program_Counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, remember, Z80 and GB's CPU use syntactically similar languages, so when we look at some real GB codes, don't be too surprised to discover that Z80 and LR35902 have similar looking codes. For example, the instruction extender is same in both (opcode 0xcb).&lt;/p&gt;

&lt;h3&gt;
  
  
  An important note on commands
&lt;/h3&gt;

&lt;p&gt;Alright, now that these commands have been fetched, we need to execute these commands. There are 8-bit load commands and 16-bit load commands. They do the work of loading things from place A to place B (eg. loading the content of a register to a stack). Finally, there are 8-bit Arithmetical and Logical Operations (eg. increment a value stored in register). In the DMG CPU, there's not a lot of 16-bit arithmetical operations to start with, and they just operate on the 16-bit registers. There's also shifting and rotation (shifting but the bits wrap around the edges) commands (eg. &lt;code&gt;00010000&lt;/code&gt; -&amp;gt; &lt;code&gt;00100000&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;There are a few couple other control operations as well: &lt;em&gt;HALT&lt;/em&gt; (Halt until interrupt), &lt;em&gt;NOP&lt;/em&gt; (Don't do anything), Enable/Disable Interrupts. Then you've got Jump operations, using which you can jump to any location in the memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's talk about memory
&lt;/h2&gt;

&lt;p&gt;Data is a bit obscure to the CPU. All this hullabaloo of wires we deal with in real world is irrelevant to the CPU. It is a processing unit, it gets data and it processes it. CPUs process all data, some correctly, some wrongly, but it is a heartless, agnostic machine, that takes in data, churns out data. &lt;/p&gt;

&lt;p&gt;Address Bus is that good friend of the CPU that delivers it just that: Data. Address Bus are Read/Write lines that are used to transfer data.&lt;/p&gt;

&lt;p&gt;Address Bus is also everyone else's friend! The peripherals and cartridge are all on the address bus! Let's dig a little bit deeper now.&lt;/p&gt;

&lt;h4&gt;
  
  
  Memory and Memory-Mapped I/O
&lt;/h4&gt;

&lt;p&gt;Memory Mapped I/Os (MMIO) tend to share the memory space with the external memory. MMIO are just methods of connecting the CPU and the peripheries. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc7tfejeq04zp4xul27ex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc7tfejeq04zp4xul27ex.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
What you see above is the general memory map of GameBoy, that I took from &lt;a href="https://medium.com/@raphaelstaebler/memory-and-memory-mapped-i-o-of-the-gameboy-part-3-of-a-series-37025b40d89b" rel="noopener noreferrer"&gt;here&lt;/a&gt;. They write in depth about GameBoy and I've used their article among others (linked below) to research for this article, shout out!&lt;/p&gt;

&lt;p&gt;We will be looking specifically at ROM and Switchable ROM bank, which are the first two regions of the memory. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  0000-3FFF   16KB ROM Bank 00     (in cartridge, fixed at bank 00)
  4000-7FFF   16KB ROM Bank 01..NN (in cartridge, switchable bank number)
  8000-9FFF   8KB Video RAM (VRAM) (switchable bank 0-1 in CGB Mode)
  A000-BFFF   8KB External RAM     (in cartridge, switchable bank, if any)
  C000-CFFF   4KB Work RAM Bank 0 (WRAM)
  D000-DFFF   4KB Work RAM Bank 1 (WRAM)  (switchable bank 1-7 in CGB Mode)
  E000-FDFF   Same as C000-DDFF (ECHO)    (typically not used)
  FE00-FE9F   Sprite Attribute Table (OAM)
  FEA0-FEFF   Not Usable
  FF00-FF7F   I/O Ports
  FF80-FFFE   High RAM (HRAM)
  FFFF        Interrupt Enable Register


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The memory you see at 0100-014F is the internal information area. It has the Nintendo Logo, from here the entry point is established, the boot procedure then jumps to the main cartridge program. Now there are other metadata codes and flags that we will skip right now.&lt;/p&gt;

&lt;p&gt;Now that's all about the CPU of the GameBoy. I dug a little into how cartridges work, but nothing too much in depth. One day when I understand the cartridge system, I'll be back here!&lt;/p&gt;

&lt;p&gt;To read more about display in GameBoy, refer to Rodrigo's blogs &lt;a href="https://www.copetti.org/projects/consoles/game-boy/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. A lot of the research for the article, was done by reading Raphael's GameBoy building Series, which you can read &lt;a href="https://medium.com/@raphaelstaebler/building-a-gameboy-from-scratch-part-1-51d05496783e" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Of course, with this I leave you with my other two favorite resources, &lt;a href="http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf" rel="noopener noreferrer"&gt;Game Boy CPU Manual&lt;/a&gt; and Pan Doc's &lt;a href="http://bgb.bircd.org/pandocs.htm#aboutthepandocs" rel="noopener noreferrer"&gt;Everything You Always Wanted To Know About GameBoy&lt;/a&gt;. &lt;/p&gt;




&lt;p&gt;If you enjoyed reading the article, show some love with those funky like buttons. Let me know where could I improve, or if you have any questions. I'll be doing more such "Under the Hood" articles inspired by Rodrigo, so stick around!&lt;/p&gt;

&lt;p&gt;Abhinavmir, signing off.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Credits for images&lt;/em&gt;:&lt;br&gt;
[1] &lt;a href="http://meseec.ce.rit.edu/551-projects/spring2014/4-1.pdf" rel="noopener noreferrer"&gt;http://meseec.ce.rit.edu/551-projects/spring2014/4-1.pdf&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://www.quora.com/What-is-an-accumulator-in-computer-science" rel="noopener noreferrer"&gt;https://www.quora.com/What-is-an-accumulator-in-computer-science&lt;/a&gt;&lt;br&gt;
[3] GameBoy CPU Manual&lt;br&gt;
[4] FreeCode Camp on Stacks&lt;br&gt;
[5] Raphael's Building GameBoy article&lt;/p&gt;

</description>
      <category>microprocessors</category>
      <category>electronics</category>
      <category>gaming</category>
    </item>
    <item>
      <title>Bitcoin and the claim of Total Turingness.</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Thu, 08 Oct 2020 06:25:57 +0000</pubDate>
      <link>https://dev.to/abhinavmir/bitcoin-and-the-claim-of-total-turingness-l68</link>
      <guid>https://dev.to/abhinavmir/bitcoin-and-the-claim-of-total-turingness-l68</guid>
      <description>&lt;p&gt;Craig Wright is a very controversial man — Bitcoin SV, Claims of being Satoshi Nakamoto, major plagiarism accusations- Overall, not the face of Blockchain you'd want. But Craig Wright is also a very smart man, much more than the Twitter handles in the Crypto Community would have you believe. His degrees and years of experience are something you cannot ignore, or at least that's what the BSV community and Wright himself want you to believe.&lt;/p&gt;

&lt;p&gt;We give Dr. Wright the benefit of doubt today. In 2014, Dr. Wright came out with a paper titled “Bitcoin: A total Turing machine” [1] and today we talk will about it.&lt;/p&gt;

&lt;p&gt;If you don’t know about Turing Machines, now is a good time to talk about it. Turing machines are theoretical models of computation that represent an abstract machine. Turing machines are tapes divided into cells.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PeVlycEO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/621/0%2A2PykOMQs3Y3lwoe8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PeVlycEO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/621/0%2A2PykOMQs3Y3lwoe8.gif" alt="Image for TMs" width="497" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A Universal Turing Machine&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Turing machine was put forth by the legendary Alan Turing in ’36 and it proved something very important: the uncomputability of the Entscheidungsproblem. Turing Machines cannot be implemented in real life of course, because we don’t have infinite tapes in real life, and because these machines aren’t optimized for real-life implementations. For example. Real Life computers use RAMs, which TMs don’t. But TMs can compute anything a real computer can, given duration of computation is not an issue.&lt;/p&gt;

&lt;p&gt;Turing Completeness is the idea that any program that is Turing Complete will halt. And any program that halts will not tend to infinity. From here we deduce that a Turing Machine should run infinite time.&lt;br&gt;
How does this all relate to Bitcoin?&lt;/p&gt;

&lt;p&gt;Bitcoin’s Script is a stack-based, non-Turing Complete programming language. And there is a lot of conversation about this, especially attacking the Ethereum community for being Turing complete, and the lack of need to be Turing complete because “Post’s theorem”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P8xA16vL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/875/0%2AX3TkogsLcWPG0cR6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P8xA16vL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/875/0%2AX3TkogsLcWPG0cR6.png" alt="Image for Stack" width="773" height="540"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Representation of a Stack Data Structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Post’s theorem posits the connection between Arithmetical Hierarchy and Turing degrees [2]. It is often stated defending the lack of need of Turing Completeness, and safety of decidability as another. &lt;/p&gt;

&lt;p&gt;Wright proposes Bitcoin has been what he proposes to call a “Probabilistic Total Turing machine”. Let me try to summarize Craig’s claims&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wright proposes that Bitcoin is equivalent to the machine proposed in Wolfram’s conjecture, which states that a 2 state, 3 symbol Turing Machine is a Universal Turing Machine. By this, a proposition establishes that Bitcoin Script must also be a Universal Turing Script.&lt;/li&gt;
&lt;li&gt;Wright states that Bitcoin is Turing Complete and proves so using the Ackermann functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wright proposes that Bitcoin Script is a Probabilistic Total Turing Machine, which is something he proposes earlier. PTTM is different from PTMs. The alternate stack in the Script makes it Turing Complete.&lt;br&gt;
Lack of looping in Bitcoin Script does not it non-Turing Incomplete. Since Bitcoin is formed using Primitive Recursion Functions, the script construct is Turing Complete.&lt;/p&gt;

&lt;p&gt;Let’s try to dissect all this. Bitcoin does not support loops: This by itself does not make the script Turing Complete or Incomplete, but this does mean that the script lacks Control Structures. Sure, if-else is there, but without For loops, there is a lack of Control. This is not a bug in the language, it is a feature. A small script such as Busy Beaver [3] will cause the Bitcoin network to return DoS overtime at worst, and slow down the hash rate at best. This was implemented to increase decidability and safety.&lt;/p&gt;

&lt;p&gt;Two stacks don’t make a Turing Complete Machine: To Simulate a Two-Stack PDA (Which is Turing complete), you need a control structure. As I said in, Bitcoin lacks For loops.&lt;br&gt;
Bitcoin limits the number of non-Push operations you can do per script: If you look at Bitcoin’s Github, you will see the following piece of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;MAX_OPS_PER_SCRIPT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that you can do 201 non-Push ops per script. This is  to safeguard against undecidable problems. So even if you implemented a 2PDA, you would be practically limited by this.&lt;br&gt;
Whether or not Bitcoin is Turing Complete is a futile discussion. Bitcoin Network is “more” Turing than other networks because of its size (That statement holds no scientific value, I said it as a figure of speech). Bitcoin Script is better off Turing incomplete by design, even though there might be some “proof” of it being otherwise. P2P consensus networks need to implement some form of decidability in their scripts. Ethereum, for example, has a Gas-system. It’s quasi-Turing Complete for that reason. Bitcoin Script, if used outside of the Bitcoin network, would have issues because it is Turing Incomplete by design, but a few modifications, and you can build it to be Turing Complete. But in practicality, no system is truly Turing Complete. I would love your comments about this!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Further Reading&lt;/em&gt;&lt;br&gt;
[1] &lt;a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3265146"&gt;https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3265146&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://youtu.be/TGE6jrVmt_I"&gt;https://youtu.be/TGE6jrVmt_I&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://en.wikipedia.org/wiki/Busy_beaver"&gt;https://en.wikipedia.org/wiki/Busy_beaver&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>bitcoin</category>
      <category>compilers</category>
      <category>theory</category>
    </item>
    <item>
      <title>Deno: Everything you need to know.</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Mon, 18 May 2020 17:27:23 +0000</pubDate>
      <link>https://dev.to/abhinavmir/node-is-dead-welcome-deno-4l96</link>
      <guid>https://dev.to/abhinavmir/node-is-dead-welcome-deno-4l96</guid>
      <description>&lt;p&gt;In 2018, Ryan Dahl gave a talk titled "10 things I regret about Node.JS" - and at the end he introduced a new runtime called Deno. Before we get into Deno, let's talk about why Ryan might have wanted a new runtime in the first place.&lt;/p&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;What Node lacked&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In the talk, Ryan went over a few regrets he had with the Node ecosystem, and I love how he addressed all of it because with time, technologies change - And in the case of Node, the ecosystem around it had changed drastically. Deno solves a few important issues that Node has, and this is how.&lt;/p&gt;
&lt;h2&gt;
  
  
  Node has access to essential System Calls
&lt;/h2&gt;

&lt;p&gt;Node Programs can write to Filesystems and related networks because in the original Node, which was built in C++ by building a wrapper (of sorts) around V8 engine, had skipped some important security functions. This, I imagine is because V8 is a secure, solid sandbox, but it is to be used inside of Chrome (or whatever other browsers implement it), but Node can be used as a CLI tool. Node files could have access to a lot of essential system calls and they could, and have resulted in malicious behavior.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;crossenv&lt;/code&gt; malware on the npm registry&lt;br&gt;
(&lt;a href="https://blog.npmjs.org/post/163723642530/crossenv-malware-on-the-npm-registry"&gt;https://blog.npmjs.org/post/163723642530/crossenv-malware-on-the-npm-registry&lt;/a&gt;)&lt;/p&gt;
&lt;h2&gt;
  
  
  The devs dropping out Promises
&lt;/h2&gt;

&lt;p&gt;Node was designed before JS introduced the concept of Promises or Async/Await. Node instead found a way around promises with EventEmitters, and a lot of APIs are built around this - Sockets and HTTP for example. Async/Await is amazing when you consider how ergonomically handy it is to use. Emitters caused a lack of well-defined protocols to deal with backpressures in streams. While this was okay for some streams, in other cases it causes a buildup, like when the receiving process is slower than sending - eg TCP, MQTT. File read/write (Write is slower than Read). In modern JavaScript, Promises provide the delegation in the form of abstraction, but Node did not have this for its own APIs - and much newer Async APIs are becoming less compatible over time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Node Package Manager is clunky
&lt;/h2&gt;

&lt;p&gt;Package.JSON is a handy, nifty little file that helps you install your NPM packages on a new system in a quick function - But package.JSON has its own problems.&lt;br&gt;
Package.JSON aimed to create a local machine of sorts for Node in a folder, but it took a lot of time, was heavy, and usually ran into problems out the box. Package.JSON is also very cluttered with metadata.&lt;/p&gt;

&lt;p&gt;Deno does not have a package manager! Deno relies on URLs to host and import packages, which I am assuming will be through a CDN, thus we can take advantage of caching! Some people in the Deno community are also trying to have a Go-like dependency handling: Compiling the program into an executable that you can run without external dependencies - But it's not a thing yet.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Node Build System hasn't aged well
&lt;/h2&gt;

&lt;p&gt;Node uses GYP Build System, which is very complicated and somewhat broken. You can read a comparison of GYP to CMake Here -&lt;br&gt;
&lt;a href="https://gyp.gsrc.io/docs/GypVsCMake.md"&gt;https://gyp.gsrc.io/docs/GypVsCMake.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;cMake is essentially a Unix system tool, and it is not cross-platform: Thus GYP made sense at the time. But even Chromium moved from GYP to GN, another build system which was 20x faster for Chromium's use case. This is one of Dahl's biggest regret. Node is one of the only remaining GYP users.&lt;/p&gt;
&lt;h2&gt;
  
  
  Out of the box TypeScript Support
&lt;/h2&gt;

&lt;p&gt;TypeScript is amazing - Optionally static typing and Type interfaces are two of the best things about TypeScript. But setting up TS with Node is a pain: You need to install dependencies, you need to configure your tsconfig.json, you have to update package.json - It's all too much. With deno, it's out of the box, no additional tooling required.&lt;/p&gt;
&lt;h2&gt;
  
  
  Explicit is better than implicit
&lt;/h2&gt;

&lt;p&gt;For example, no .JS tags while importing a module!&lt;br&gt;
It is one of my biggest problems with Node, and Ryan mentioned that as well. It is needlessly less explicit. It is also unnatural: Browsers need you to have.JS extensions. I can understand where this came from, but we can also see how it is broken.&lt;/p&gt;


&lt;h1&gt;
  
  
  Is Node really dead?
&lt;/h1&gt;

&lt;p&gt;No, I was being sensationalist. Node will be alive for years to come since a lot of websites are securely built in Node, it is awesome and has a strong community around it. Small-time projects might see a shift to Deno - Personally, I have a Supply Chain project where I might use Deno.&lt;br&gt;
It is less clunky, lighter, more intuitive, and explicit. I also love how it uses Rust Crates and is not a monolith. I am not sure if Node was, but I think it was a monolith that directly called C++ APIs.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function hello(place: string): string { return `Hello ${place}`} console.log(hello('world'))&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;That is a simple 'hello world!' that runs like this&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;./deno hello.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;Hello world&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And a simple URL import would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { factorial } from "https://gist.githubusercontent.com/DanielRamosAcosta/ad514503b1c7cf8290dadb96a5fddee9/raw/4733e267f05d20110ba962c4418bab5e98abfe93/factorial.ts" 
console.log(factorial(10))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is beautiful, don't you think?&lt;/p&gt;




&lt;p&gt;🌺 Hey, I hope you enjoyed reading that article. I am Abhinav, editor @ The Crypto Element. It takes a lot of work to research for and write such an article, and a clap or a follow 👏 from you means the entire world 🌍 to me. It takes less than 10 seconds for you, and it helps me with reach! You can also ask me any questions, or point out anything, or just drop a "Hey" 👇 down there. I 💓making new friends!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>backend</category>
    </item>
    <item>
      <title>Developing React Apps by Consuming APIs</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Wed, 01 Jan 2020 17:16:44 +0000</pubDate>
      <link>https://dev.to/abhinavmir/developing-react-apps-by-consuming-apis-3lpd</link>
      <guid>https://dev.to/abhinavmir/developing-react-apps-by-consuming-apis-3lpd</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/@abhinavsrivastavmir/developing-react-node-websites-super-fast-using-a-headless-cms-8954b6278c6e"&gt;previous&lt;/a&gt; part, I talked about developing the backend with a GUI, you can serve your database as an API. Few benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AJAX-style loading, because page reloading is not needed.&lt;/li&gt;
&lt;li&gt;Hosting on two different servers is a lot more secure. It takes a little work to figure out where the &lt;em&gt;admin&lt;/em&gt; panel is, otherwise it’s just website.com/admin. Now website.com serves the frontend, fetching APIs from a backend you know the URL of.&lt;/li&gt;
&lt;li&gt;Super customizable.&lt;/li&gt;
&lt;li&gt;Faster, since all your code is not hosted on one server.&lt;/li&gt;
&lt;li&gt;If something breaks, you know where to dig in first.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Enough talk, let’s get coding. To be fair, this can be done without React. To also be fair, you can dig a pool using spoons. You don’t, right? I’ll explain how React works as we code, just keep in mind what we are doing: Fetching an API and displaying it but in &lt;em&gt;style.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, start your project with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app strapireactcscd strapireactcsyarn start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used the file name ‘strapireactcs’ because I want to make a differentiation that this is a CS- or Client-Side Code. This is purely for convenience.&lt;/p&gt;

&lt;p&gt;One more thing, make a parent directory for the Strapi backend and React frontend. It should look like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parentdir
|__strapireact
|__strapireactcs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a good practice.&lt;/p&gt;

&lt;p&gt;Now, let’s look at our project structure. You should have two subfolders named *Public *and *Src. *Heads up, we will be using Materialize CSS to style our app, so go ahead and add the CDN to index.html.&lt;/p&gt;

&lt;p&gt;Add this how you would to a normal HTML file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Compiled and minified CSS --&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="c"&gt;&amp;lt;!-- Compiled and minified JavaScript --&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be in index.html, which is where we will call* app.js.*&lt;/p&gt;

&lt;p&gt;Now let’s head over to app.js!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to have React in our JavaScript file to use it!&lt;/p&gt;

&lt;p&gt;Declare the class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;People&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic for our app goes into this. First we declare a state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;people&lt;/span&gt;&lt;span class="p"&gt;:[]};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to W3:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React components has a built-in state object.The state object is where you store property values that belongs to the component.When the state object changes, the component re-renders.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React components has a built-in &lt;code&gt;state&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; object is where you store property values that belongs to the component.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;state&lt;/code&gt; object changes, the component re-renders.&lt;/p&gt;

&lt;p&gt;*people *will be where the JSON data will be stored that we will get from the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;async&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;componentDidMount()&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;url=&lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:1337/todos"&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;response=await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;fetch(url);&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;data=await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;response.json();&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;console.log(data);&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;this.setState(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;todo:data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;);&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ComponentDidMount() is a function of React that runs the content inside the parenthesis after the page markdown has loaded.&lt;/p&gt;

&lt;p&gt;It’s a good place to setState, that is- Set a value to the state object.&lt;/p&gt;

&lt;p&gt;When I console.log’d data, you should be able to see an object in your inspect element console (CTRL+SHIFT+I on Windows).&lt;/p&gt;

&lt;p&gt;Let’s Render some HTML now?&lt;/p&gt;

&lt;p&gt;Render() function serves the HTML to browser. You can write JavaScript in {} and React will take the value and turn it to HTML Hypertext.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mystyle&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;10px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mystyle&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;mystyle&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;br&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/br&amp;gt;{todo.deadline}&amp;lt;br&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nx"&gt;br&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;     &amp;lt;/&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;))}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;;}&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now during deployment, you will just have to replace localhost URI with the URI of your backend server. &lt;strong&gt;Map&lt;/strong&gt; is an iterative function. Dot is used to access data members of an object.&lt;/p&gt;

&lt;p&gt;So for example, &lt;em&gt;todo.deadline&lt;/em&gt; is used to access deadline data from todo state object.&lt;/p&gt;

&lt;p&gt;So here is the original API fetch request would give us-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F60%2F1%2AX7Z8IGbRRTC7AtYy4IIG4w.png%3Fq%3D20" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F60%2F1%2AX7Z8IGbRRTC7AtYy4IIG4w.png%3Fq%3D20"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F990%2F1%2AX7Z8IGbRRTC7AtYy4IIG4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F990%2F1%2AX7Z8IGbRRTC7AtYy4IIG4w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And after fetching that data and serving it with React, we get&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F60%2F1%2Ajc-aX8cGPnpQvvWBMKF3hg.png%3Fq%3D20" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F60%2F1%2Ajc-aX8cGPnpQvvWBMKF3hg.png%3Fq%3D20"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1036%2F1%2Ajc-aX8cGPnpQvvWBMKF3hg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1036%2F1%2Ajc-aX8cGPnpQvvWBMKF3hg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we will talk about deployment to Heroku, you can also find the Github there.&lt;/p&gt;

</description>
      <category>react</category>
      <category>api</category>
      <category>javascript</category>
      <category>apps</category>
    </item>
    <item>
      <title>Bitcoin and the claim of Total Turingness.</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Sun, 29 Dec 2019 20:32:24 +0000</pubDate>
      <link>https://dev.to/abhinavmir/i-am-making-an-introductory-text-to-blockchain-and-making-your-own-decentralized-apps-let-me-know-if-anyone-wants-it-51l8</link>
      <guid>https://dev.to/abhinavmir/i-am-making-an-introductory-text-to-blockchain-and-making-your-own-decentralized-apps-let-me-know-if-anyone-wants-it-51l8</guid>
      <description>&lt;p&gt;Craig Wright is a very controversial man — Bitcoin SV, Claims of being Satoshi Nakamoto, major plagiarism accusations- Overall, not the face of Blockchain you'd want. But Craig Wright is also a very smart man, much more than the Twitter handles in the Crypto Community would have you believe. His degrees and years of experience are something you cannot ignore, or at least that's what the BSV community and Wright himself want you to believe.&lt;/p&gt;

&lt;p&gt;We give Dr. Wright the benefit of doubt today. In 2014, Dr. Wright came out with a paper titled “Bitcoin: A total Turing machine” [1] and today we talk will about it.&lt;/p&gt;

&lt;p&gt;If you don’t know about Turing Machines, now is a good time to talk about it. Turing machines are theoretical models of computation that represent an abstract machine. Turing machines are tapes divided into cells.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PeVlycEO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/621/0%2A2PykOMQs3Y3lwoe8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PeVlycEO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/621/0%2A2PykOMQs3Y3lwoe8.gif" alt="Image for TMs" width="497" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A Universal Turing Machine&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Turing machine was put forth by the legendary Alan Turing in ’36 and it proved something very important: the uncomputability of the Entscheidungsproblem. Turing Machines cannot be implemented in real life of course, because we don’t have infinite tapes in real life, and because these machines aren’t optimized for real-life implementations. For example. Real Life computers use RAMs, which TMs don’t. But TMs can compute anything a real computer can, given duration of computation is not an issue.&lt;/p&gt;

&lt;p&gt;Turing Completeness is the idea that any program that is Turing Complete will halt. And any program that halts will not tend to infinity. From here we deduce that a Turing Machine should run infinite time.&lt;br&gt;
How does this all relate to Bitcoin?&lt;/p&gt;

&lt;p&gt;Bitcoin’s Script is a stack-based, non-Turing Complete programming language. And there is a lot of conversation about this, especially attacking the Ethereum community for being Turing complete, and the lack of need to be Turing complete because “Post’s theorem”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P8xA16vL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/875/0%2AX3TkogsLcWPG0cR6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P8xA16vL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/875/0%2AX3TkogsLcWPG0cR6.png" alt="Image for Stack" width="773" height="540"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Representation of a Stack Data Structure&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Post’s theorem posits the connection between Arithmetical Hierarchy and Turing degrees [2]. It is often stated defending the lack of need of Turing Completeness, and safety of decidability as another. &lt;/p&gt;

&lt;p&gt;Wright proposes Bitcoin has been what he proposes to call a “Probabilistic Total Turing machine”. Let me try to summarize Craig’s claims&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wright proposes that Bitcoin is equivalent to the machine proposed in Wolfram’s conjecture, which states that a 2 state, 3 symbol Turing Machine is a Universal Turing Machine. By this, a proposition establishes that Bitcoin Script must also be a Universal Turing Script.&lt;/li&gt;
&lt;li&gt;Wright states that Bitcoin is Turing Complete and proves so using the Ackermann functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Wright proposes that Bitcoin Script is a Probabilistic Total Turing Machine, which is something he proposes earlier. PTTM is different from PTMs. The alternate stack in the Script makes it Turing Complete.&lt;br&gt;
Lack of looping in Bitcoin Script does not it non-Turing Incomplete. Since Bitcoin is formed using Primitive Recursion Functions, the script construct is Turing Complete.&lt;/p&gt;

&lt;p&gt;Let’s try to dissect all this. Bitcoin does not support loops: This by itself does not make the script Turing Complete or Incomplete, but this does mean that the script lacks Control Structures. Sure, if-else is there, but without For loops, there is a lack of Control. This is not a bug in the language, it is a feature. A small script such as Busy Beaver [3] will cause the Bitcoin network to return DoS overtime at worst, and slow down the hash rate at best. This was implemented to increase decidability and safety.&lt;/p&gt;

&lt;p&gt;Two stacks don’t make a Turing Complete Machine: To Simulate a Two-Stack PDA (Which is Turing complete), you need a control structure. As I said in, Bitcoin lacks For loops.&lt;br&gt;
Bitcoin limits the number of non-Push operations you can do per script: If you look at Bitcoin’s Github, you will see the following piece of code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;MAX_OPS_PER_SCRIPT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that you can do 201 non-Push ops per script. This is  to safeguard against undecidable problems. So even if you implemented a 2PDA, you would be practically limited by this.&lt;br&gt;
Whether or not Bitcoin is Turing Complete is a futile discussion. Bitcoin Network is “more” Turing than other networks because of its size (That statement holds no scientific value, I said it as a figure of speech). Bitcoin Script is better off Turing incomplete by design, even though there might be some “proof” of it being otherwise. P2P consensus networks need to implement some form of decidability in their scripts. Ethereum, for example, has a Gas-system. It’s quasi-Turing Complete for that reason. Bitcoin Script, if used outside of the Bitcoin network, would have issues because it is Turing Incomplete by design, but a few modifications, and you can build it to be Turing Complete. But in practicality, no system is truly Turing Complete. I would love your comments about this!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Further Reading&lt;/em&gt;&lt;br&gt;
[1] &lt;a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3265146"&gt;https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3265146&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://youtu.be/TGE6jrVmt_I"&gt;https://youtu.be/TGE6jrVmt_I&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://en.wikipedia.org/wiki/Busy_beaver"&gt;https://en.wikipedia.org/wiki/Busy_beaver&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Developing the simplest blockchain using Vanilla JavaScript</title>
      <dc:creator>abhinav the builder</dc:creator>
      <pubDate>Wed, 25 Dec 2019 05:10:39 +0000</pubDate>
      <link>https://dev.to/abhinavmir/developing-the-simplest-blockchain-using-vanilla-javascript-3il1</link>
      <guid>https://dev.to/abhinavmir/developing-the-simplest-blockchain-using-vanilla-javascript-3il1</guid>
      <description>&lt;p&gt;Understand the basics.&lt;/p&gt;

&lt;p&gt;Pre-requisites: Some rudimentary knowledge of OOPs in JavaScript.&lt;/p&gt;

&lt;p&gt;We are using Node for this exercise. You could use Python or Go for the same, but I am using this since I am going to show you how to integrate this with your Node app. I take &lt;strong&gt;heavy&lt;/strong&gt; inspiration from &lt;a href="https://www.youtube.com/channel/UCnxrdFPXJMeHru_b4Q_vTPQ"&gt;Savjee&lt;/a&gt;, I am using his series on blockchain as inspiration and putting my best understanding out there.&lt;/p&gt;

&lt;p&gt;If you don't have Node, feel free to install it from your favorite package manager or the internet. A little bit about Node first: Node is JavaScript outside of your browser. You don't need &lt;em&gt;script&lt;/em&gt; tags to run this JS, all you need to do is-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;filename.js&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because some madlad took Chrome's V8 engine out of Chrome and wrapped it in C and served us Node. Talk about &lt;a href="https://en.wikipedia.org/wiki/Jugaad"&gt;*Jugaad&lt;/a&gt;.*&lt;/p&gt;

&lt;p&gt;The first thing you'll need to do is make a new directory and CD into that. Then you need to init an NPM package, and create an index.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;blockchain-example&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;cd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;blockchain-example&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;npm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-y&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;vi&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index.js&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am using Vim because I am terribly lazy, feel free to use any text editor of choice. Now let's talk a little bit about what a blockchain &lt;em&gt;is&lt;/em&gt; before we delve further into code. The best explanation I have read is &lt;a href="https://ihodl.com/tutorials/2017-09-04/what-blockchain-laymans-terms/"&gt;this article&lt;/a&gt; by IHODL.&lt;/p&gt;

&lt;p&gt;To quote from their website-&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Blockchain is a distributed database existing on multiple computers at the same time. It is constantly growing as new sets of recordings or blocks, are added to it.&lt;/p&gt;

&lt;p&gt;Each block contains a timestamp and a link to the previous block, so they actually form a chain.&lt;/p&gt;

&lt;p&gt;The database is not managed by any particular body; instead, everyone in the network gets a copy of the whole database.&lt;/p&gt;

&lt;p&gt;All blocks are encrypted in a special way, so everyone can have access to all the information but only a user who owns a special cryptographic key is able to add a new record to a particular chain.&lt;/p&gt;

&lt;p&gt;In addition, cryptography is used to guarantee synchronization of copies of the blockchain on each computer (or node) in the network.&lt;/p&gt;

&lt;p&gt;Read more at &lt;a href="https://ihodl.com/tutorials/2017-09-04/what-blockchain-laymans-terms/"&gt;their website.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q2OoGZK2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ar6rAN-xe-bnH-ouc2EebOw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q2OoGZK2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ar6rAN-xe-bnH-ouc2EebOw.png" alt="[https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515](https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515)" width="773" height="705"&gt;&lt;/a&gt;&lt;em&gt;&lt;a href="https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515"&gt;https://www.researchgate.net/figure/Simple-example-of-blockchain-technology_fig4_325486515&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Look at the diagram above. All transactions are stored in Blocks, so every one knows what data is being transacted. Now before you are up in arms about security, this data is not open for everyone to see (It is in very few cases!). They are hashed, as we will see. You can only verify this transaction, verifying means that they can't be tampered with. For simplicity, these transactions are shown as currency here, but they could be any data.&lt;/p&gt;

&lt;p&gt;Let's jump into code and from there explain a little here and there?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sha256&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sha256&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Block&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevHash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prevHash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prevHash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;thisHash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prevHash&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you understand what happened? We made a goddamn block. This is the fundamental logic behind every blockchain unicorn founder that got rich. Pay attention.&lt;/p&gt;

&lt;p&gt;First, we import SHA256, it's an encryption algorithm. As I said, it's to encrypt the data.&lt;/p&gt;

&lt;p&gt;The class &lt;em&gt;BLOCK&lt;/em&gt; is a block template we will use for every block.&lt;/p&gt;

&lt;p&gt;Each block will have five elements: index, timestamp, data, Previous Hash and Present Hash.&lt;/p&gt;

&lt;p&gt;For CryptoCurrencies (esp the earlier ones like Bitcoin), the data is the address of the sender, receiver of bitcoin and amount of Bitcoin transacted.&lt;/p&gt;

&lt;p&gt;Now that you know what One block looks like, I am sure you have a better understanding. Let's move on to what would we do about the first block, right? Since our first block has no Previous Hash!&lt;/p&gt;

&lt;p&gt;We create something called the &lt;em&gt;Genesis Block.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;All of this will come under a class called &lt;em&gt;blockChain&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createGenesisBlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Genesis Block&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it do? Simple. Create an instance of the class with 0 as the index, present date as date, 'Genesis Block' as data (You can keep this as anything as long as you remember what your initial block is named) and 0 as the previous hash.&lt;/p&gt;

&lt;p&gt;We will need two functions: getLatestBlock() and calculateHash().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;getLatestBlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;calculateHash&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;SHA256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previousHash&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;getLatestBlock() fetches the latest block from the chain. Out of context it might be confusing, check the GitHub, you'll get a better understanding.&lt;/p&gt;

&lt;p&gt;calculateHash() calculates the Hash, we have talked about this before.&lt;/p&gt;

&lt;p&gt;Now let's create an addBlock function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;addBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;newBlock&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;newBlock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previousHash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getLatestBlock&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;newBlock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newBlock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;calculateHash&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newBlock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function takes in a parameter called newBlock, which is basically the block we will be adding. To use this function we will run it something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bkc_app&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;blockChain&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;bkc_app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now addBlock will take that block we need to add as parameter, add a previousHash to it by getting the hash of the latestBlock, adds a present Hash to it by doing a calculateHash() on it and pushes it to the chain.&lt;/p&gt;

&lt;p&gt;Yay, the simplest blockchain in the world is now ready!&lt;/p&gt;

&lt;p&gt;Let's take it for a spin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bkc_app&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blockchain&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;story&lt;/span&gt;&lt;span class="sc"&gt;':'&lt;/span&gt;&lt;span class="nc"&gt;Quick&lt;/span&gt; &lt;span class="nc"&gt;Brown&lt;/span&gt; &lt;span class="nc"&gt;Fox&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;bkc_app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addBlock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Block&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bkc_app&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stringify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;khbr&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://pastebin.com/JkZjCUCi"&gt;Check the final results here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We created a very basic blockchain. In the next few tutorials I will talk about Bitcoins, Ethereums, Cosmos/Matic, Hyperledger and concepts like Proof of Work and Proof of Stake. Stay tuned, give me a follow and clap if you enjoyed.&lt;/p&gt;

&lt;p&gt;Signing Off,&lt;/p&gt;

&lt;p&gt;&lt;a href="http://abhinavmir.tech"&gt;Abhinav Srivastava.&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
