The Good Tech Companies - Starknet: Unlocking Better Performance With Cairo-Native Execution
Episode Date: January 27, 2025This story was originally published on HackerNoon at: https://hackernoon.com/starknet-unlocking-better-performance-with-cairo-native-execution. Starknet's Cairo-native e...xecution enhances scalability and reduces costs by utilizing the Cairo programming language, improving smart contract performance. Check more stories related to web3 at: https://hackernoon.com/c/web3. You can also check exclusive content about #ethereum-layer-2-scaling, #ethereum-blockchain, #ethereum-scalability, #ethereum-rollups, #starknet, #cairo-native-execution, #2077-research, #good-company, and more. This story was written by: @2077research. Learn more about this writer by checking @2077research's about page, and for more stories, please visit hackernoon.com. Starknet is an Ethereum Layer 2 protocol that utilizes the Cairo programming language to enhance scalability and reduce costs. The platform is transitioning from a virtual machine-based execution model to native execution, aiming to improve transaction processing speed and resource efficiency. Challenges include encouraging broader developer adoption, ensuring tooling compatibility, and balancing innovation with accessibility.
Transcript
Discussion (0)
This audio is presented by Hacker Noon, where anyone can learn anything about any technology.
Starknet. Unlocking better performance with Cairo native execution, by 2077 Research.
Starknet's Cairo native execution utilizes its custom programming language,
Cairo, to boost scalability, cut costs, and improve smart contract performance.
However, challenges arise from the need for broader developer adoption,
tooling compatibility, and balancing innovation with accessibility across the ecosystem.
Introduction. STARKNET and Cairo Execution Starknet is an Ethereum Layer 2
L2 protocol that leverages cryptographic Stark, succinct transparent arguments of knowledge,
proofs to power verifiable computation at scale.
Like other L2 blockchains, StarkNet is able to offer meaningful scalability and security for
L2 transactions while preserving decentralization. At the core of StarkNet lies the Cairo Virtual
Machine, VM, a purpose-built execution environment designed to leverage the cryptographic proof
capabilities of Starks. Unlike other VMs, StarkNet's Cairo VM is designed to leverage the cryptographic proof capabilities of Starks.
Unlike other VMs, StarkNet's Cairo VM is designed to run provable programs,
which reduces the overhead associated with generating proofs to verify computation.
This allows StarkNet to process a vast number of transactions while maintaining trustless verification. That said, the true measure of StarkNet's performance, as experienced buy-in
users, depends on the speed at which its sequencer processes transactions and confirms execution
results. Also known as soft confirmations, the sequencer's confirmation of transaction success
or failure are useful for time-sensitive users who want to avoid waiting for transactions to
finalize on Ethereum L1 before knowing a
transaction's status. As it stands today, Starknet's transaction throughput relies heavily
on the efficiency of the sequencer. The sequencer is responsible for receiving, organizing,
and proving transactions before submitting them to Ethereum for final verification.
Although Starknet uses a single sequencer currently, it plans to decentralize the sequencer
for better censorship resistance and higher failure tolerance. One offline sequencer cannot
take down the network. On October 29, StarkNet recorded an average of 127 transactions per second
TPS. Over a continuous 24-hour period, this was not just a one-time peak but a sustained
throughput milestone
achieved during normal operations, showcasing the relapsability to handle high transaction
volumes under real-world conditions. Alongside this achievement, StarkNet's performance stands
out for ITS lightning-fast transaction confirmations, averaging under 2 seconds,
and ultra-low fees, $0.002 per transaction, making it a cost-effective and efficient solution
for scaling Ethereum. Maintaining this level of performance requires constant innovation,
however, especially as user demand grows. This is where StarkNet's upcoming enhancements to
its execution model come into play, introducing optimizations that promise not only faster
transaction processing but also more
efficient resource usage. These developments mark a critical step toward elevating StarkNet's
execution environment to meet the demands of a rapidly evolving ecosystem. StarkNet has already
introduced transformative upgrades to its architecture to improve performance and bolster
UX, E, G, parallel execution and block packing via bolt upgrade. These innovations
have drastically reduced transaction processing times and gas costs by enabling the sequencer
to handle transactions more efficiently. To learn more about the bolt upgrade and how it
improves StarkNet's scalability, read our StarkNet Bolt Explainer. Building on this foundation,
StarkNet is now focusing on further optimizing its execution
environment with cairo native the upcoming enhancements will redefine how transactions
are processed transitioning from virtual machine-based emulation to native execution
these changes promise significant improvements in processing speed and resource efficiency
ensuring that starknet can meet the growing demands of users. In the following section,
we'll take a closer look at StarkNet's execution environment and dive deeper into how the Cairo VM
and the broader architecture are being reimagined to support the next phase of scalability and
efficiency. Let's dive in. A gentle introduction to Cairo native execution. The execution environment
of StarkNet has evolved significantly over time to improve performance and ensure safety. Initially, StarkNet relied on a straightforward pipeline
where high-level Cairo code was compiled directly into CASM, Cairo Assembly, for execution.
While this approach worked, it left the network vulnerable to inefficiencies and security risks.
For example, invalid code could not be proven, and the sequencer was exposed to
potential dose attacks due to the lack of safeguards in gas accounting. To address these
issues, the Cairo 1.0 upgrade introduced Sierra, an intermediate representation, IR, that enforces
safety by design. Inspired by languages like Rust, Sierra ensures developers follow stricter
coding patterns, effectively
eliminating runtime errors and enabling accurate gas accounting. This upgrade is already live on
StarkNet's mainnet and marks a crucial step toward a more robust and secure execution environment.
Cairo Native builds on this progress and further enhances StarkNet's performance by enabling native
execution. Native execution is a departure from the previous
pattern of executing programs in the Cairo VM and represents the next leap forward in terms
of performance for StarkNet. How does Cairo native execution work? Previously, the StarkNet sequencer
executed programs using a virtual machine that emulated code, which introduced significant
overhead and slowed down transaction processing. Emulation in this context involves programming the CPU-TO behave like a theoretical Cairo CPU,
interpreting instructions step by step. While functional, this process is inherently
inefficient because it prevents the CPU from fully utilizing its native capabilities and
execution styles. In contrast, native execution eliminates this awkwardness,
allowing programs torrent directly on the operating system in the CPU's native instruction set.
This change dramatically improves execution speed and resource efficiency,
enabling StarkNet to process transactions faster than ever.
To achieve this, Cairo native execution integrates advanced compilation technologies like MLIR
and LLVM.
These tools are widely used in modern software development to compile code into optimized
machine-level instructions.
Cairo Native takes Sierra programs and organizes them into an intermediate form that
also allows for sophisticated optimizations,
while LLVM further compiles this into native executables tailored for the underlying operating
system. This dual-layer compilation process ensures that the generated executables are fast,
efficient, and ready for direct execution without any additional overhead. The impact of this
upgrade on the sequencer's performance
and the network in general is profound.
Increased speed.
By removing the need for VM emulation,
native execution allows the sequencer
to process transactions significantly faster,
reducing latency across the network.
Better resource utilization.
The optimized executables generated by LLVM
make better use of system resources, enabling the sequencer to handle higher transaction volumes.
Lower costs
While faster execution alone doesn't immediately translate to reduced gas fees due to the absence of a fee mechanism, StarkNet continues to demonstrate cost efficiency.
For instance, during StarkNet's record-breaking performance, gas fees averaged around $0.002 per
transaction. The 0.13.4 update, which introduces Cairo Native, also includes pricing-related
improvements. However, these improvements are not directly related to Cairo Native itself.
While the full potential of Cairo Native scalability on transaction fees has yet to be
realized, combining this scalability with future fee-related enhancements positions
StarkNet as a highly affordable and scalable solution for Ethereum.
Enhanced innovation, performance gains from native execution enables more complex and
dynamic applications to thrive on StarkNet, opening the door to innovative use cases and
opportunities for developers.
Although native execution brings significant speed improvements, a natural concern arises for developers regarding the dual execution workflow between the sequencer and the prover.
While the sequencer benefits from running transactions using native artifacts for speed,
the prover still relies on the Cairo VM to execute transactions step-by-step,
ensuring they can be proven to Ethereum Stark Verifier. CASM is still used in this process,
as it is the representation validated by the Stark Verifier on Ethereum.
This duality, executing the same transactions through two different systems,
can raise valid questions about consistency and correctness. Specifically, how can we be certain
that the outcomes from native execution and Cairo VM execution will always match? The short answer
lies in rigorous testing and validation. Specifically, the Lambda class team conducted
extensive replay tests, a process where historical startnet blocks are re-executed under the new
system to ensure every transaction produces the same results.
Replay tests are a critical method for verifying that even with two distinct execution paths,
the outputs remain consistent. Thus, developers can rest assured that this extensive validation process significantly minimizes the risk of discrepancies. Additionally, it's worth noting
that StarkNet has always operated with two separate execution flows.
Even before Cairo Native, the sequencer did not full-irely on the Cairo VM for tasks like syscalls or state access. These were already handled differently, with the prover focusing
on producing cryptographic proofstall updates Starknet's state on Ethereum. In this sense,
the introduction of native execution represents an evolution, not a departure, from StarkNet's established architecture.
This also means developers should have few issues designing applications to work with
StarkNet's new execution model. Overall, Cairo Native unlocks a new level of scalability and
speed for StarkNet. By transitioning from VM-based emulation to native execution,
the network can support more users,
handle larger transaction volumes, and deliver a seamless experience for developers and end-users
alike. This transformation solidifies StarkNet's position as a leading L2 solution and paves way
for greater innovation in the Ethereum ecosystem. Cairo Native vs Cairo
A comparison of StarkNet execution models Cairo Native marks a groundbreaking improvement in
StarkNet's execution environment, one that delivers significant speedups over the traditional Cairo VM.
Through extensive benchmarks conducted by Lambda Class and Nethermind,
the performance gains from native execution have been quantified,
highlighting Cairo Native's ability to drastically enhance transaction and contract processing speeds.
Execution benchmarks The benchmarks revealed that Cairo Native achieves an average speedup of 5x
over the Cairo VM, with performance gains ranging from 1.5x to 20x, depending on the specific
contract. These results are more than just numbers, they illustrate how native execution
directly benefits users and developers.
For example, a simple STRK transfer between two Argent accounts not only completed 2.3x faster but also showed 4.8x speedup in ERC-20 contract logic. For users, this means lower waiting times
for transactions and smoother interactions with decentralized applications, DAPPs. For developers,
these improvements censure their applications perform faster and more reliably, even under
heavy network load. In more demanding scenarios like STRK, ETH swaps or gaming applications such
as Influenceth, the benefits are even more apparent. Influenceth transactions recorded a 10,
5x transaction speedup, with some contract
executions improving by as much as 70x. These advancements empower developers to create complex,
high-performance decentralized applications, paving the way for richer gaming experiences,
advanced DeFi protocols, and other computationally intensive use cases.
The transition from emulation to native execution underpins these advancements. By compiling programs directly into machine native code,
Cairo Native eliminates the inefficiencies of emulating Cairo instructions, where the CPU
essentially mimics another virtual CPU. Native execution allows the hardware to operate at full
capacity, delivering unmatched performance improvements across all transaction types. Compilation Benchmarks Cairo Native introduces a more advanced compilation
pipeline, adding several steps to translate Sierra code into native executables. This includes
process SSH as converting Sierra to MLIR, optimizing the code, and finally compiling
ITINTO machine native instructions. While this more complex
pipeline might intuitively seem like it would increase overall compilation time, benchmarks
show that the additional steps contribute to less than 15% of total compilation time,
demonstrating that the process remains efficient. It's also worth noting that native executables
are larger in size compared to CASM artifacts. This increase reflects the added detail and
optimization required for machine-native code but does not affect network performance.
Importantly, compilation is only performed once, at the time the contract IS deployed on the
network, ensuring that this step does not impact the real-time performance of the sequencer.
What are the implications of Cairo-native execution for StarkNet?
The implications of Cairo-native for Starknet's ecosystem are far-reaching.
By enabling faster, more efficient transaction execution, this upgrade is said to massively
enhance the user experience for all applications on the network. With the network projected to
handle nearly 1,000 transactions per second, TPS, by the end of 2024, Starknet will become one of the most scalable and
responsive L2 chains in the Ethereum ecosystem. For users, the Cairo native upgrade means quicker
transaction confirmation and even lower costs. During Starknet's record-breaking performance
earlier this year, gas fees averaged around $0.002 per transaction. With Cairo Native, these costs are expected to
decrease even further, solidifying StarkNet as one of the most affordable Layer 2 networks.
The introduction of Cairo Native execution will also supercharge the following use cases.
On-chain gaming applications StarkNet has already gained traction as a platform for
blockchain-based games, and Cairo Native takes this to the next level. Games like Influenceth, which rely on intricate logic and fast-paced
interactions, will benefit significantly from the speed improvements. Faster execution times
reduce in-game latency, enabling smoother experiences for players. Developers can also
introduce more complex game mechanics, such as real-time multiplayer capabilities and dynamic NFT interactions, without worrying about performance bottlenecks.
This can bring about a new class of on-chain gaming applications whose gameplay can rival
that of traditional competitors. Social media platforms
Decentralized social media platforms demand high transaction throughput and low latency
to ensure seamless user interactions,
such as posting, commenting, and voting. With Cairo Native, these platforms can scale to
support millions of users, offering real-time feeds and instantaneous reactions, rivaling the
performance of traditional Web2 counterparts. Lower gas costs further enhance accessibility,
making decentralized social media a more viable alternative to centralized platforms and preparing it for mainstream adoption.
Decentralized finance, DeFi. The DeFi ecosystem on StarkNet will also reap the benefits of Cairo
native. Complex financial operations like swaps, lending, and yield farming require high throughput
and precision. Faster execution times ensure that trades and liquidations happen in real-time, minimizing slippage and providing users with
better outcomes. Additionally, lower gas fees enable microtransactions, making DeFi protocols
more accessible to a broader audience. Conclusion
Cairo native execution is a critical milestone in the StarkNet roadmap.
By introducing a new level of
execution starknet not only solidifies its position as a leading layer 2 platform but also sets a
precedent for the broader ethereum roll-up ecosystem starknet's innovation showcases how
roll-ups can achieve unparalleled scalability and efficiency helping ethereum move closer to
its vision of mass adoption while maintaining its principles of decentralization and security cairo native provides developers with a robust high performance
environment that supports more dynamic and innovative applications by reducing execution
times and resource overhead developers can focus on creating richer more interactive experiences
that were previously constrained by network limitations. As noted in a tweet,
there is still potential for further optimization, meaning Cairo Native's performance may continue to
improve over time. In this sense, the upgrade to Cairo Native execution represents a crucial step
toward realizing StarkNet's full potential as a fast, scalable, and developer-friendly platform
and advancing the capabilities of roll-up technology across Ethereum's ecosystem. A version of this article was originally published here.
Thank you for listening to this HackerNoon story, read by Artificial Intelligence.
Visit HackerNoon.com to read, write, learn and publish.