<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yevgeny Zaytman on Medium]]></title>
        <description><![CDATA[Stories by Yevgeny Zaytman on Medium]]></description>
        <link>https://medium.com/@yevgeny_3217?source=rss-89d40bfbb1b6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8wWTmM6NuwxJ5zne</url>
            <title>Stories by Yevgeny Zaytman on Medium</title>
            <link>https://medium.com/@yevgeny_3217?source=rss-89d40bfbb1b6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:14:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yevgeny_3217/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Reducing the step count of 256-bit operations in Cairo]]></title>
            <link>https://medium.com/nethermind-eth/reducing-the-step-count-for-256-bit-operations-in-cairo-61299649dc0c?source=rss-89d40bfbb1b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/61299649dc0c</guid>
            <category><![CDATA[arithmetic-operations]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[cairo-lang]]></category>
            <category><![CDATA[zero-knowledge-proofs]]></category>
            <category><![CDATA[starknet]]></category>
            <dc:creator><![CDATA[Yevgeny Zaytman]]></dc:creator>
            <pubDate>Tue, 25 Oct 2022 14:29:13 GMT</pubDate>
            <atom:updated>2022-10-26T14:48:34.633Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FOR09k4RbvehBdbSbDOjrA.png" /></figure><p>by <a href="https://www.linkedin.com/in/yevgeny-zaytman-726640238/">Yevgeny Zaytman</a> and <a href="https://twitter.com/0xAlbertG">Albert Garreta</a></p><p><em>Special thanks to </em><a href="https://www.linkedin.com/in/ignacio-manzur-0220121b7"><em>Ignacio Manzur</em></a><em> and </em><a href="https://twitter.com/0xNonCents"><em>NonCents</em></a><em> for their help with Cairo.</em></p><p>Cairo’s basic data type, the felt, has capacity for storing (non-negative) integers of up to 251 bits. However, many applications require managing 256-bit integers. As a result, Cairo’s standard library includes a sub-library that handles such functionality.</p><p>In this post, we present several improvements we have made in the implementations of these functions. We have generally reduced the number of steps and range check calls by about 25–45% in most functions. This improvement is greater in some cases (like in the function that computes square roots).</p><h3>Context</h3><p>Before we begin, let us review some Solidity 101.</p><p>Solidity’s default data type, Uint256, represents integers between 0 and 2²⁵⁶‒1, i.e., integers that can be written with at most 256 bits. Being Solidity’s default, this data type is ingrained in the whole EVM-based blockchain space. A prominent example (among many) is the ERC20 token interface, which works almost exclusively with Uint256 types.</p><p>On the other hand, Cairo has one and only one data type, called felt (an abbreviation for “field element”). This data type is used to represent integers between 0 and <em>p </em>‒<em> </em>1, where <em>p = </em>2²⁵¹ + 17·2¹⁹² + 1 is a prime of <strong>252 bits</strong>. Hence, a felt can store all positive integers of up to <strong>251</strong> <strong>bits</strong> and only a “bunch” of 252-bit integers.</p><p>All Cairo developers are fully aware of this discrepancy between Solidity’s and Cairo’s data type, and, indeed, the “251 vs. 256” difference has been the source of several memes within the community. On a more serious note, this discrepancy can be the root of some headaches, since many StarkNet projects must work with a 256-bit data type for L1←→L2 compatibility.</p><p>To allow working with 256-bit integers, Cairo’s standard library includes a <a href="https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/uint256.cairo">dedicated submodule</a>, in which a 256-bit integer <em>n</em> is represented as two 128-bit integers (sometimes referred to as <strong>chunks</strong> or <strong>limbs</strong>), <em>n</em>₀ and <em>n</em>₁ (called n.low and n.high in the library), so that <em>n = n</em>₀ + 2¹²⁸<em>n</em>₁. In other words, <em>n</em>₀ is the positive integer formed by the 128 least significant bits of <em>n</em>, and <em>n</em>₁ is the positive integer formed by the rest of the bits. Having 128 bits, each one of these chunks is small enough to be stored in a single felt. The two resulting felts are stored together within a 2-felt struct called Uint256.</p><p>So far, so good, but much work still needs to be done. Indeed, suppose you have stored two 256-bit integers <em>n, m</em> in this fashion. How can we now compute their product <em>n</em>·<em>m</em> in Cairo?</p><p>In principle, <em>n</em>·<em>m</em> may have up to 2 × 256 = 512 bits. Hence one generally needs two Uint256 instances to store <em>n</em>·<em>m</em> (or, equivalently, four 128-bit chunks). The standard method for doing this is similar to what we did before: split the 512-bit integer <em>n</em>·<em>m</em> into two 256-bit chunks <em>C</em>₁<em>, C</em>₂, so that <em>n</em>·<em>m = C</em>₁ + 2²⁵⁶<em>C</em>₂. Both <em>C</em>₁<em>, C</em>₂ will be stored as Uint256 instances.</p><p>The main question here is: how do we efficiently obtain <em>C</em>₁ and C₂, given that we only know <em>n</em>₀<em>, n</em>₁<em>, m</em>₀<em>, m</em>₁? There is not a clear definitive answer to this, and it is here where the bulk of our work resides.</p><p>So far, we have discussed only the multiplication of 256-bit integers, but similar issues arise with all other operations that involve 256-bit integers: addition, subtraction, squaring, taking a square root, etc.</p><h3>Contributions</h3><p>We next outline our contributions. We discuss exclusively 256-bit (non-negative) integers, but most improvements can be adapted to integers of other sizes (384, 512, etc.)</p><h3>Multiplication</h3><p>We have implemented a new method for multiplying two 256-bit integers. Compared to the corresponding function from Cairo’s standard library, our function is approximately 31% cheaper in Cairo steps and 36% cheaper in “range check builtin” calls:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*zG3-THXZDewmvJ5KC7gW-w.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L58">Link to code</a></p><h3>Division</h3><p>The multiplication improvement percolates into other functions such as division with remainder. In Cairo, a simple method to obtain the quotient and the remainder of dividing an integer <em>n</em> by another integer <em>m</em> is to:</p><ol><li>Compute the quotient <em>q</em> and the remainder <em>r</em> in a hint.</li><li>Verify in Cairo (outside the hint), that <em>n = m</em>·<em>q</em> + <em>r</em> and 0 <em>≤ r &lt; m</em>.</li></ol><p>Implementing this strategy using our new multiplication function (with some extra minor modifications) results in the following efficiency improvements for the division function in Cairo’s standard library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*FRT3eW3AGg4j2inT1faJ4g.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L133">Link to code</a></p><h3>Squaring</h3><p>Since many number theoretic algorithms make extensive use of squaring (i.e., computing <em>n</em>² for <em>n</em> a 256-bit integer), it is worthwhile to create a function to handle this particular case. The standard library performs squaring by calling the multiplication function with input <em>n, n</em>. We have improved upon that by building a dedicated squaring function, which, together with our new multiplication function, results in efficiency improvements of about 50%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*nCdntF6rcAvQrkTPbj8Yfg.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L81">Link to code</a></p><h3>Taking the square root</h3><p>We also improved the efficiency of taking a square root by 1) using a 128-bit squaring function and 2) removing one of the two felt multiplications the current function uses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*zug2zcBkOL2FNFg8nxC9EQ.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L98">Link to code</a></p><h3>Subtraction</h3><p>The subtraction function was also improved. We leveraged Cairo’s hints to save us one addition operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*i7QhYEUZY72DDJA2Avd4Ng.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L173">Link to code</a></p><h3>Uint256_expand</h3><p>Finally, we created a new class called Uint256_expand, an expanded version of Uint256 that stores the two 128 chunks of a 256-bit non-negative integer <em>n</em>, together with extra data that needs to be computed when multiplying <em>n</em> by another integer.</p><p>This class (which is more expensive to instantiate and consumes more memory than Uint256) is to be used for integers <em>n</em> that repeatedly appear in our operations. An example could be the scenario where we are working modulo an integer <em>n</em>. In this case, we will need to divide by <em>n</em> many times, and storing <em>n</em> as a Uint256_expand will save us some duplicate work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*GNEZKkSdWwu9lChv9MGw7g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*FQxGgnV8KmvnWvpIBr49Yg.png" /></figure><p><a href="https://github.com/NethermindEth/research-basic-Cairo-operations-big-integers/blob/main/lib/uint256_improvements.cairo#L242">Link to code</a></p><h3>Relevance for cryptography implementations</h3><p>Another application worth mentioning is that our techniques can be extended to integers of arbitrary size. This is of interest primarily for cryptography applications, where one often works with huge numbers, and efficiency is paramount. The BLS signature implemented over the BLS12–381 elliptic curve is a relevant example in the blockchain space. Such a scheme is native to Ethereum, and it operates with integers of 381 bits.</p><h3>Stay tuned</h3><p>Besides 256-bit operations, we have been working on other basic mathematical functionality such as prime field arithmetic (for large primes) and elliptic curve cryptography. Stay tuned for further updates about this work!</p><p><em>Nethermind is a team of world-class builders and researchers. We empower enterprises and developers worldwide to access and build upon the decentralized web. Our work touches every part of the Web3 ecosystem, from our Nethermind node to fundamental cryptography research and application-layer protocol development. We’re always looking for passionate people to join us in solving Ethereum’s most difficult challenges. Are you interested? Check out our job board</em> <a href="https://nethermind.io/company/">https://nethermind.io/company/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61299649dc0c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nethermind-eth/reducing-the-step-count-for-256-bit-operations-in-cairo-61299649dc0c">Reducing the step count of 256-bit operations in Cairo</a> was originally published in <a href="https://medium.com/nethermind-eth">Nethermind.eth</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>