<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Smartcontract Developer at DaoVentures, Singapore.]]></description><link>https://seshanth.xyz</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1661096363840/Xrhl5ZMQq.png</url><title>Untitled Publication</title><link>https://seshanth.xyz</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 07:35:30 GMT</lastBuildDate><atom:link href="https://seshanth.xyz/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Huff Challenge #2]]></title><description><![CDATA[Huff is a low-level EVM language to write optimized smart contracts. 
Huff's official Twitter account has shared a few challenges recently. I'll try to solve and share them here :)
Challenge #2

Challenge #2 is to write an optimized Huff program to f...]]></description><link>https://seshanth.xyz/huff-challenge-2</link><guid isPermaLink="true">https://seshanth.xyz/huff-challenge-2</guid><category><![CDATA[EVM]]></category><category><![CDATA[Solidity]]></category><category><![CDATA[huff language]]></category><category><![CDATA[huff]]></category><category><![CDATA[Ethereum]]></category><dc:creator><![CDATA[Seshanth S]]></dc:creator><pubDate>Sun, 28 Aug 2022 05:57:05 GMT</pubDate><content:encoded><![CDATA[<p>Huff is a low-level EVM language to write optimized smart contracts. 
Huff's official Twitter account has shared a few challenges recently. I'll try to solve and share them here :)</p>
<h2 id="heading-challenge-2">Challenge #2</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1661612862576/PMuv6giKZ.png" alt="Challenge #2" class="image--center mx-auto" /></p>
<p>Challenge #2 is to write an optimized Huff program to find whether a number is Even. It should return 1 if the input is Even and 0 if the input is Odd. You can find the first challenge here <a target="_blank" href="https://seshanth.xyz/huff-challenge-1">(challenge #1)</a>. </p>
<h2 id="heading-solution">Solution</h2>
<p>I'll start from the solution I have arrived at, Then continue to a more optimized version shared by the EVM wizards.</p>
<pre><code><span class="hljs-comment">#define macro MAIN() = takes(0) returns (0) {</span>
    <span class="hljs-number">0x02</span>                       <span class="hljs-string">//[0x02]</span>
    <span class="hljs-number">0x00</span> <span class="hljs-string">calldataload</span>          <span class="hljs-string">//[input,</span> <span class="hljs-number">0x02</span><span class="hljs-string">]</span>
    <span class="hljs-string">mod</span>                        <span class="hljs-string">//[0</span> <span class="hljs-string">or</span> <span class="hljs-number">1</span><span class="hljs-string">]</span>
    <span class="hljs-string">iszero</span>                     <span class="hljs-string">//[1</span> <span class="hljs-string">or</span> <span class="hljs-number">0</span><span class="hljs-string">]</span>
    <span class="hljs-number">0x00</span> <span class="hljs-string">mstore</span>
    <span class="hljs-number">0x20</span> <span class="hljs-number">0x00</span> <span class="hljs-string">return</span>
<span class="hljs-string">}</span>
</code></pre><p>In Huff, execution always starts from the <code>MAIN</code> macro. <code>takes(0) returns(0)</code> implies that this macro doesn't read any values from the stack and doesn't push any value to the stack upon completion.</p>
<p>The logic we are going to use is that When an Even number is divided by 2 the remainder will be 0.</p>
<p>We start by pushing the inputs to the stack. </p>
<pre><code><span class="hljs-number">0x02</span>
<span class="hljs-number">0x00</span> <span class="hljs-string">calldataload</span>
</code></pre><p><code>0x02</code> pushes <strong>2</strong> to the stack.
<code>calldataload</code> opcode loads the transaction inputs to the stack. The opcode takes one argument, the offset to start load from <code>(0x00)</code>.</p>
<p>Calldata is the place where the function arguments are stored. Usually, the function selector takes the first 4 bytes of calldata, followed by the inputs. But in this case, the input starts from offset 0 (As per the challenge's description).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1661665263021/4-Mq7ur9p.png" alt="Calldata view with input 2" /></p>
<p><code>0x00 calldataload</code> - pushes <strong>0</strong> to the stack first, followed by <code>calldataload</code>. On execution, The calldataload opcode consumes 1 input (0x00) from the stack and pushes the output(our input number). The stack will now look like <strong>[INPUT, 0x02]</strong>.</p>
<p>We have the inputs in place in the stack. Now we need to find the remainder of the division.
<code>MOD</code> opcode returns the value we need.</p>
<pre><code><span class="hljs-keyword">mod</span>
iszero
</code></pre><p><code>mod</code> takes two inputs from the stack and returns the remainder.
The result of <code>MOD</code> will be either 0 or some number. Our goal is to return 0 for Odd numbers and 1(true) for Even numbers. </p>
<p><code>iszero</code> opcode returns takes the value at the top of the stack and returns <strong>1</strong> if the value at top of the stack is 0. So if the number is even, <code>mod</code> will return <strong>0</strong> and <code>iszero</code> turns it to <strong>1</strong> and vice-versa.</p>
<p>stack view: <strong>[0 or 1]</strong>.</p>
<p>We now have the required value at the required position in the stack. To return the value, we need to use the <code>RETURN</code> opcode. The RETURN opcode takes inputs from the memory, so we need to save our result to the memory.</p>
<p>To store a value in memory, <code>MSTORE</code> opcode is used.</p>
<pre><code>                                        <span class="hljs-string">//[0</span> <span class="hljs-string">or</span> <span class="hljs-number">1</span><span class="hljs-string">]</span>
    <span class="hljs-number">0x00</span> <span class="hljs-string">mstore</span>
    <span class="hljs-number">0x20</span> <span class="hljs-number">0x00</span> <span class="hljs-string">return</span>
</code></pre><p>MSTORE takes two inputs, </p>
<ol>
<li>The memory offset,</li>
<li>The data to store</li>
</ol>
<p>In the above snippet, We are storing the result at memory offset <strong>0x00</strong>. EVM operates on 32 bytes, so the result will occupy the first 32 bytes from <strong>0x00</strong> (0x00 to 0x1f)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1661664292880/o3Ebu-zsZ.png" alt="Result is stored in memory at offset 0x00" /></p>
<p>Finally, <code>RETURN</code> opcode is pushed, which returns the result. </p>
<p><code>RETURN</code> opcode expects 2 inputs, </p>
<ol>
<li>The memory offset of the data to return,</li>
<li>The size of the data in memory.</li>
</ol>
<p><code>0x00</code> is the memory offset and <code>0x20</code>(32 bytes) is the size of the data(our result).</p>
<h2 id="heading-optimization">Optimization</h2>
<p>The snippet in the previous section does the Job, But there is still room to save gas.
When a number is pushed to a stack (<code>0x00</code>), Huff replaces it with the <code>PUSH</code> opcode (PUSH 0x00). Push opcode costs 3 gas.</p>
<p>The EVM wizards in Huff Discord found another way to push 0 to the stack while saving 1 gas. 
<code>RETURNDATASIZE</code> opcode pushes the length of the data returned in the last CALL, DELEGATECALL, CALLCODE or STATICCALL. At this point we have not made any external calls to other contracts, so using <code>RETURNDATASIZE</code> will push <strong>0</strong> to the stack.</p>
<p>The final code is,</p>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> macro MAIN() = takes(0) returns (0) {</span>
    <span class="hljs-number">0x02</span>                                 <span class="hljs-comment">//[0x02]</span>
    returndatasize calldataload          <span class="hljs-comment">//[input, 0x02]</span>
    mod                                  <span class="hljs-comment">//[0 or 1]</span>
    iszero
    returndatasize mstore
    <span class="hljs-number">0x20</span> returndatasize <span class="hljs-keyword">return</span>
}
</code></pre><p>Thus by replacing <code>0x00</code> with <code>RETURNDATASIZE</code> in three places we are able to save 3 gas, which is equivalent to one <code>PUSH</code></p>
]]></content:encoded></item><item><title><![CDATA[Huff Challenge #1]]></title><description><![CDATA[I recently got to know about the new programming language called Huff. Huff is a low-level  language to write EVM smartcontracts. 
What makes it different from other languages is that in Hufff we code directly in opcodes. With Huff one can write more...]]></description><link>https://seshanth.xyz/huff-challenge-1</link><guid isPermaLink="true">https://seshanth.xyz/huff-challenge-1</guid><category><![CDATA[EVM]]></category><category><![CDATA[Solidity]]></category><category><![CDATA[Ethereum]]></category><category><![CDATA[huff]]></category><category><![CDATA[huff language]]></category><dc:creator><![CDATA[Seshanth S]]></dc:creator><pubDate>Sun, 21 Aug 2022 18:13:14 GMT</pubDate><content:encoded><![CDATA[<p>I recently got to know about the new programming language called Huff. Huff is a low-level  language to write EVM smartcontracts. </p>
<p>What makes it different from other languages is that in Hufff we code directly in opcodes. With Huff one can write more optimized smartcontracts, as It doesn’t abstract anything from the devs and gives complete control over the stack.</p>
<p>I’m learning huff to get to know more about the EVM.
Huff’s official Twitter has recently started posting challenges. I’ll try to solve them and document it here :).</p>
<h2 id="heading-challenge-1">Challenge #1</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1661103768467/igMIUpC8i.png" alt="Huff_Challenge1.png" />
<a target="_blank" href="https://twitter.com/huff_language/status/1559658361469095936">Huff Challenge #1</a></p>
<p>As the challenge description says, the target is to write as little huff code as possible to return the current block number.</p>
<h2 id="heading-solution">Solution</h2>
<pre><code><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> macro MAIN() = takes(0) returns(0) {</span>
  number              <span class="hljs-comment">//[blockNumber]</span>
  <span class="hljs-number">0x00</span>                <span class="hljs-comment">//[0x00, blockNumber]</span>
  mstore              <span class="hljs-comment">//saves blockNumber to mem offset 0x00</span>
                      <span class="hljs-comment">//[]</span>

  <span class="hljs-number">0x20</span>                <span class="hljs-comment">//[0x20]</span>
  <span class="hljs-number">0x00</span>                <span class="hljs-comment">//[0x00]</span>
  <span class="hljs-keyword">return</span>              <span class="hljs-comment">//returns </span>
}
</code></pre><p>Huff code always starts from the <code>MAIN()</code> macro.</p>
<p><code>takes(0) returns(0)</code> means that this macro doesn’t take any data from the stack and doesn’t push any data to the stack upon completion.</p>
<p>The following snippet gets the block number and saves it to memory at offset 0.</p>
<pre><code><span class="hljs-built_in">number</span>
<span class="hljs-number">0x00</span>
mstore
</code></pre><p>First, the <code>NUMBER</code> opcode is executed, which pushes the current block number to the stack.</p>
<p>The goal is to return the block number. To return any data, <code>RETURN</code> opcode should be used. The <code>RETURN</code> opcodes take inputs only from the memory, so the block number should be stored in the memory.</p>
<p>Followed by <code>NUMBER</code> is, <code>0x00</code> which pushes 0 to the stack. So the stack looks like <strong>[0x00, blockNumber]</strong>.</p>
<p><code>MSTORE</code> opcode stores the data (block number in this case) to the memory. It takes 2 inputs,</p>
<ol>
<li>The memory offset,</li>
<li>The data to store.</li>
</ol>
<p>Here, <strong>0x00</strong> is the memory offset and block number is the <strong>data</strong>. When <code>MSTORE</code> opcode is pushed to the stack, It saves the block number to the memory at offset 0.</p>
<h3 id="heading-return-the-block-number">RETURN the Block Number</h3>
<p>As mentioned before, The <code>RETURN</code> opcode is used to return the data.</p>
<pre><code><span class="hljs-number">0x20</span> 
<span class="hljs-number">0x00</span> 
<span class="hljs-keyword">return</span>
</code></pre><p><code>RETURN</code> takes 2 inputs,</p>
<ol>
<li>The memory offset to start reading from,</li>
<li>The size in bytes.</li>
</ol>
<p>First, the byte size <strong>0x20</strong> (32 bytes) is pushed into the stack. Followed by the memory offset <strong>0x00</strong>.</p>
<p>The stack now looks like, <strong>[0x00, 0x20]</strong></p>
<p>The <code>RETURN</code> opcode is then pushed to the stack, which reads 32 bytes (<strong>0x20</strong>) starting from the memory offset 0 and returns it.</p>
]]></content:encoded></item></channel></rss>