Clarity for Ethereum?

I’ve today posted a Stacks Grants proposal for spreading Clarity adoption to the Ethereum ecosystem as well:

Background

What problems do you aim to solve? How does it serve the mission of a user owned internet?

I believe that Clarity is a much-needed evolution towards safer smart contracts, and I wish to see it much more broadly adopted. To that end, I would wish to bring Clarity also to the Ethereum developer community, who are indeed in sore need of it: as is widely understood, poorly-designed Solidity smart contracts have already resulted in hundreds of millions of dollars worth of mayhem.

Broader adoption of Clarity can help mitigate and solve these problems. Indeed, there is a chance that with sufficient and serious backing, Clarity could in time become an important industry standard for smart contracts on a range of blockchains. As it stands, Clarity is already available on Blockstack and soon on Arweave as well, and the Ethereum network would be an obvious–and in terms of current user base, no doubt the most important–next evolution.

Project Overview

What solution are you providing? Who will it serve?

I wish to develop a Clarity compiler to Ethereum virtual machine (EVM) bytecode, enabling the use of Clarity for writing smart contracts for Ethereum as another language choice alongside the current choices of Solidity and Vyper. And I wish to publicize the project widely through blogging and conferences, driving adoption of Clarity.

This will directly serve Ethereum developers in giving them a safer choice for writing their smart contracts, and it will directly serve Ethereum users in giving assurance that the behavior of contracts can be correctly understood. In particular, an audit of a Clarity smart contract will provide a higher level of assurance than can be said for audits of Solidity contracts, and audits can be significantly facilitated by automated tooling due to the decidable nature of Clarity.

The network effects of (much!) broader adoption of Clarity will directly and indirectly serve the Stacks Foundation and the Stacks developer community at least as follows:

  1. More visibility (hence adoption) for Stacks in the currently most important blockchain developer ecosystem
  2. More ease in porting contracts from Ethereum to Stacks (hence more adoption), and vice versa
  3. More cross-transfer of developer skills (hence more Stacks developers)
  4. More resources for best-of-class tooling for Clarity (hence a better developer experience for everyone)
  5. An additional implementation and thorough independent review of the Clarity language, nailing down murky corners of the language (of benefit to the Stacks 2.0 mainnet launch)

Scope

What are the components or technical specs of the project? What will the final deliverable look like? How will you measure success?

The project will be delivered as a public-domain GitHub project containing the full source code for the compiler, as well as offered downloads of pre-built standalone executables for Windows, macOS, and Linux. In addition, the executable will have a manual page and there will be further user documentation at https://clarc.dev.

The compiler will be written in OCaml, an excellent programming language for crafting compiler toolchains. Clarc will be a multi-pass compiler consisting of the following stages:

Success will be easier to measure than for most projects: we should be able to compile a substantial subset of existing Clarity contracts, with minimal changes for porting to Ethereum, into usable smart contracts for Ethereum.

Budget and Milestones

What grant amount are you seeking? How long will the project take in hours? If more than 20, please break down the project into milestones, with a clear output (e.g., low-fi mockup, MVP with two features) and include the estimated work hours for each milestone.

The total requested budget to bring the project to fruition and a 1.0 release is $5,000.

I would in fact request more, except for your current $5K limit during the beta grants program. Do kindly note that my actual opportunity cost is a multiple of the proposed budget, but I do consider this a hugely worthwhile project and am passionate to make it happen.

Based on my previous commercial work with Ethereum bytecode generation and analysis, as well as on my recent and ongoing work on a Clarity compiler for SmartWeave, I believe I’m in a good position to estimate this project fairly predictably.

I would be available to work on this part-time from next week and full-time from the middle of October, with project delivery by November. Therefore I suggest the following rough timeline and major milestones:

Release 0.1 (proof of concept) - 20h

Release 0.5 (prototype) - 40h

Release 1.0 (minimum viable product) - 60h

  • Compiles many or most of the Clarity Hackathon contracts (with minimal changes for porting to Ethereum) into usable EVM contracts.
  • Published user documentation.
  • Released as pre-built binaries for Windows, macOS, and Linux.

Team

Who is building this? What relevant experience do you bring to this project? Are there skills sets you are missing that you are seeking from the community?

I will be building this by myself. I am a prolific open-source author, have 20+ years of professional programming experience, and I have all the relevant deep expertise for toolchains, compilers, and the Ethereum virtual machine. See my GitHub, LinkedIn, and website for more about me.

Risks

What dependencies or obstacles do you anticipate? What contingency plans do you have in place?

The main obstacles on this project are twofold:

  1. The Clarity language is as yet underspecified and something of a moving target. This will necessitate some back and forth with Blockstack’s Clarity team (@lgalabru et al) to nail down murky corners of the language. This isn’t perhaps so much an obstacle as an opportunity to improve the documentation around Clarity and indeed move towards developing a proper formal specification for Clarity.

  2. Not all Clarity constructs map directly to the EVM as is. At the least, some Blockstack-specific functions will need to be omitted or emulated, and we will need to discuss and decide how to best handle the difference of Clarity’s 128-bit arithmetic given that the EVM is based on a 256-bit machine word size.

Community and Supporting Materials

Do you have previous projects, code commits, or experiences that are relevant to this application? What community feedback or input have you received? How do you plan to share your plan to the community over time and as the final deliverable?

Earlier this month, I was awarded the joint Blockstack and Arweave grant for enabling Clarity on Arweave’s smart contract protocol. That work will reach completion (that is, a 1.0 release) by next week, and directly motivates this grant proposal as a follow-up project while everything about the internals of Clarity is still fresh and clear in my mind. Some of the code (in particular, for parsing Clarity code) will be shared between the two projects.

I plan to regularly share my progress in tweets, Discord and forum updates, and educational and motivational blog posts over the course of the project. The blog posts will be targeted at both the Stacks and Ethereum developer communities in order to build interest and begin building up an open-source community of contributors around the project. In addition, I plan to speak at conferences about the construction of this compiler and showcasing how to write contracts in Clarity for Stacks, Arweave, and Ethereum, with the recordings made available on YouTube.

2 Likes

I have yesterday released Clarc 0.1.0, a proof-of-concept that’s able to compile the Clarity counter.clar example contract into usable Ethereum virtual machine (EVM) bytecode.

Here’s a demo screencast of compiling, deploying, and interacting with this contract on an EVM simulator:

Screencast

Here’s the symbolic EVM opcode output from Clarc for this contract:

$ clarc counter.clar -t opcode
PUSH1 0x00 PUSH1 0x00 SSTORE PUSH1 0x64 DUP1 PUSH1 0x10 PUSH1 0x00 CODECOPY
PUSH1 0x00 RETURN PUSH1 0xe0 PUSH1 0x02 EXP PUSH1 0x00 CALLDATALOAD DIV DUP1
PUSH4 0x8ada066e EQ PUSH1 0x28 JUMPI DUP1 PUSH4 0xd09de08a EQ PUSH1 0x36
JUMPI DUP1 PUSH4 0x2baeceb7 EQ PUSH1 0x4d JUMPI STOP JUMPDEST POP PUSH1 0x00
SLOAD PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN STOP JUMPDEST POP
PUSH1 0x01 PUSH1 0x00 SLOAD ADD PUSH1 0x00 SSTORE PUSH1 0x00 SLOAD PUSH1 0x00
MSTORE PUSH1 0x20 PUSH1 0x00 RETURN STOP JUMPDEST POP PUSH1 0x01 PUSH1 0x00
SLOAD SUB PUSH1 0x00 SSTORE PUSH1 0x00 SLOAD PUSH1 0x00 MSTORE PUSH1 0x20
PUSH1 0x00 RETURN STOP

Here’s the equivalent deployable EVM bytecode output from Clarc for this contract:

$ clarc counter.clar -t bytecode
600060005560648060106000396000f360e060020a6000350480638ada066e146028578063d09de08a1460365780632baeceb714604d57005b5060005460005260206000f3005b5060016000540160005560005460005260206000f3005b5060016000540360005560005460005260206000f300

The compiler source code can be found at lib/Clar2EVM/compile.ml and the compiler driver (i.e., the clarc program) at bin/clarc/clarc.ml. The EVM bytecode encoder can be found at lib/EVM/encode.ml and the Clarity language parser is maintained in my standalone Clarity.ml library subproject.

So far no blockers on this project, just a ton of work!