Skip to main content

Who verifies the Verifier? We ran solc in a zkVM

· 9 min read

Source code verification has no proof​

Source code verification is a very simple process, if you have the contract's sources and compilation settings you can just call the compiler, see the output, and compare the bytecode from the compiler with the bytecode deployed onchain.

Average users don't usually do this process though, mainly because the users' wallets and apps don't do it for them. They, instead, put their trust into verification services like Sourcify that can reply with a verification status in the matter of milliseconds.

But what if Sourcify is hacked and malicious contracts appear as verified while they aren't? Speed and UX come at the price of trust. In theory the best way to verify would be to reproduce the verification locally, if you are willing to pay trust with some seconds of your life: your wallet recompiles the contracts directly on your machine and fetches their onchain bytecode from a light client, also running on your machine.

Verifying locally solves the trust problem on most devices (paying trust with time), with two exceptions:

  • Hardware wallets have a small processor, too little memory to run a compiler or a light client.
  • Smart contracts can read the onchain hash, but they cannot execute Solidity. So onchain source code verification is not possible.

What about a fast and trustworthy alternative?​

During Devconnect in Istanbul, we had the idea to run the Solidity compiler in a zero-knowledge virtual machine (zkVM). Theoretically any program running in a zkVM can generate a proof of its execution. Anyone possessing this proof can check, in the matter of milliseconds, that an output (the bytecode) was generated by a specific program (the Solidity compiler) given a specific input (the contract's sources + the compilation settings).

At the time, LLMs weren't as powerful as today, and zkVMs were not as mature. We gave it a try but compiling the Solidity compiler to be compatible with the zkVM was too hard. We gave it a try again recently and made it work, without changing a single line of the Solidity compiler!

The standard JSON input goes into solc, which runs inside the SP1 zkVM. The output is a claim and a proof. A wallet checks the proof offchain, and a smart contract checks it onchain.

How does it work?​

We used SP1, the zkVM built by Succinct. The program running inside the zkVM is the Solidity compiler itself (solc 0.8.38), built from the official release source. Not changing the compiler was very important for us, because otherwise the proof would be about "solc with Sourcify's patches" and you would have to trust our patches too.

From there the process is quite simple:

  1. The compiler gets its input. This is the standard JSON input of the contract, so the sources + the compilation settings. Sourcify already stores it for every verified contract.
  2. The compiler runs in the zkVM. At the end the program outputs a small record that we call the claim. The claim contains the hash of the input, the compiler version, the contract name and the hashes of the bytecode.
  3. SP1 generates the proof. The proof is only 356 bytes, and it says that this exact program produced this claim from this input.
  4. Anyone can check the proof. You don't need a compiler here, you just check the proof and then compare the bytecode hash in the claim with the hash of the bytecode deployed onchain.

To test it on a real contract, we deployed a small Counter contract on Sepolia and verified it on Sourcify. Then we generated its proof on the Succinct prover network, which cost us about €0.07. This is what the check looks like (we shortened the hashes to fit the page):

$ solc-zk-proof verify examples/sepolia-counter --rpc $RPC
The Groth16 proof is valid for program 0x00968b…1306a2 (389.7ms).
Claim: {
"inputHash": "0xc878a8…82bbda",
"compilerVersion": "0.8.38+commit.1835d247",
"contractName": "src/Counter.sol:Counter",
"creationCodeHash": "0xf28e5a…aaf3ff",
"runtimeCodeHash": "0x654072…bf3b6a",
"runtimeCodeLength": 481,
"immutables": [],
"metadataHash": "0x134964…06c204"
}
The code at 0xffD615…4D40E4 matches the claim.

The check took 390 milliseconds and no compiler was involved. Here are all the numbers:

WhatResult
Generate the proof on the Succinct prover network30 to 90 seconds, about €0.07
Size of the proof356 bytes
Check the proof offchainLess than 0.5 seconds
Check the proof onchain, once per compilationAbout 262,000 gas

There is also a nice side effect: the claim is about the bytecode and not about an address, so one proof covers every deployment of the same compilation. If there are ten thousand copies of the same token contract, a single proof is enough for all of them.

What about hardware wallets and smart contracts?​

Hardware wallets: checking a proof needs no compiler and no source files, and the work is always the same no matter how big the contract is. This should be possible on a small device too, but we haven't tried it on an actual hardware wallet yet. For now, it is an open question.

Smart contracts: this is the part that was simply impossible before. We wrote a contract called ProvenanceRegistry that anyone can call with a proof:

function register(
address target,
bytes calldata publicValues,
bytes calldata proofBytes
) external returns (bytes32 codehash);

The registry checks the proof with Succinct's verifier contract, then it reads the bytecode at target and compares its hash with the claim. If everything passes, it stores the claim. This costs about 262,000 gas but only once, from then on any contract can read the result from the registry for the price of a storage read.

So what can a contract do with it? Here is our example, a contract called ProvenanceGate (shortened):

function forward(address target, bytes calldata data)
external
returns (bytes memory result)
{
bytes32 inputHash = registry.provenanceOf(target).inputHash;
if (inputHash != approvedInputHash) revert NotApproved(target);
// ... forward the call to the target
}

The gate forwards calls only to contracts that are proven to come from one specific source. In other words the gate approves a source and not an address: a fresh deployment of the same source passes, anything else reverts. Both contracts are on Sepolia, here are the registry and the gate.

We can think of a few places where this is useful:

  • Upgrades: a DAO votes on a source instead of an address that someone claims is the right one, and the proxy accepts a new implementation only if it is proven to come from the approved source.
  • Audits: an auditor signs the hash of the source they reviewed, and anyone (or any contract) can check that a deployment is exactly that source.
  • Datasets: our dataset export is basically a list of statements from a verifier. With a proof next to each row, someone mirroring the data doesn't need to trust the verifier that wrote the row.

So is trust gone?​

Not completely, but it moves to fewer places:

  • The proof system: SP1 and its verifier contract. If there is a bug there, everything built on top of it breaks.
  • The program: the proof is about a specific program, but who says that this program is really solc 0.8.38? Our build is reproducible, so anyone can build it again and compare the result. This has to be done once per compiler version instead of once per contract, but someone other than us has to do it and nobody has yet.
  • The compiler and the source: the proof says that solc produced this bytecode, not that solc is correct or that the source is safe. Verified does not mean safe, with or without a proof.
  • The storage of the source: the claim contains only the hash of the input, so you still need someone like Sourcify to store the sources and give them to you. However, you can check what you receive, as the sources must hash to the input hash in the claim.

Also keep in mind that this is an experiment. It works for one compiler version (each solc version is a different zkVM program and we only built 0.8.38), and only for contracts whose onchain bytecode is equal to the compiler output byte for byte, i.e. an exact match. For the other contracts the zkVM program would also need to apply Sourcify's transformations, and we haven't built that.

The price problem​

Verifying locally you pay trust with time, with a proof you pay it with money. The Counter is the smallest contract you can think of, so we also ran bigger contracts in the zkVM without generating a proof (which is free) to estimate the price:

ContractEstimated price on the prover network
Counter, 1 source file€0.07 (the price we paid)
A typical contract, 7 source files€0.43 to €0.80
Large projects, 49 to 107 source files€7 to €34
Large projects compiled with viaIR€42 to €142

With these prices we cannot generate a proof for every contract verified on Sourcify. What we can think of instead is offering proofs for specific contracts with special needs: the new implementation behind a governance upgrade, a contract that an auditor signed off, or any contract that another contract has to check onchain. For these contracts a few euros (or even a hundred) is a small price compared to what is at stake.

Would you use such a proof?​

This was a side experiment and it is not on our roadmap. Before building a service we want to know if someone actually needs it, so we have a question for you: would you use such a proof in a wallet, in a governance process or in an audit report, and would you be willing to pay for it?

You can find the code at sourcifyeth/solc-zk-proof. Let us know what you think by opening an issue there, on X, or in our Matrix chat.