With just two lines of code, it is possible to easily generate and verify a ZK proof on the XRPL EVM Sidechain!✨
In this example, we will generate a zk proof (SNARK) and verify it on-chain on the XRPL EVM Sidechain using a Circom circuit representing the multiplication of two integers a
and b
such as a x b = c
.
We want to prove to the verifier that for a given result c, we know the integers a and b without revealing them.
This tutorial uses hardhat-circom, a Hardhat plugin integrating Circom and SnarkJS into Hardhat's build process.
npm i
- Add and populate
.env
withDEV_PRIVATE_KEY=your_DEV_priv_key
. - Fund your account with XRP on Devnet using this faucet: https://bridge.xrplevm.org/.
- You can configure
hardhat.config.ts
andcircuits.config.json
as you like.
You must provide a Powers of Tau from a Phase 1 ceremony. We recommend using one of the .ptau
files from the Hermez Protocol's ceremony, available from their Dropbox folder.
These are all named powersOfTau28_hez_final_*.ptau
where the *
is some number. This number indicates the number of constraints (2^x
) that can exist in your circuits.
Nb: I have already included in the repository, within the circuit folder, a .ptau file for a constraint count of up to (2^15).
Circuits are located within the circuits
folder. Each circuit will have its own sub-folder with the actual .circom
file, which represents the circuit, and an input.json
file containing the circuit's inputs (what you want to prove).
Run: npx hardhat circom --verbose
This will generate the out folder with multiplier.zkey
, multiplier.vkey
, multiplier.r1cs
, and circuit.wasm
, as well as the Solidity verifier MultiplierVerifier.sol
under the contracts
folder.
Run: npx hardhat run scripts/deploy_generate_verify.ts --network XRPL_EVM_Sidechain_Devnet
This script does four things:
- Deploys the
MultiplierVerifier.sol
contract. - Generates a proof from circuit intermediaries with
generateProof()
ingenerate.ts
. - Generates calldata with
generateCallData()
ingenerate.ts
.
After generating your proof, you will see a proof.json
file in the circuit's out folder. This is actually the proof and contains an attribute publicSignals
. For example, if you are proving that you know two numbers a
and b
such that a * b = c
, the value c
might be part of publicSignals
, while a
and b
remain private. The verifier checks that the proof is valid for the given c
without knowing a
and b
.
Here are some commands to generate new proofs and verify them once your circuit is generated and the verifier is deployed.
-
Generate a New Proof: Description: Generates a new proof and saves it to the proof.json file.
Associated Script:scripts/generate.ts
Note: Modify the value ofBASE_PATH
according to the circuit you want to use.
Run:npx hardhat run scripts/generate.ts --network XRPL_EVM_Sidechain_Devnet
-
Verify an Existing Proof: Description: Verify an existing proof by interacting with the deployed contract.
Associated Script:scripts/verify.ts
Note: Modify the value ofBASE_PATH
according to the circuit you want to use, and update the value ofVERIFIER_CONTRACT_ADDRESS
with the address of your deployed contract on-chain.
Command:npx hardhat run scripts/verify.ts --network XRPL_EVM_Sidechain_Devnet
-
Try modifying the inputs and generating a new proof, then verify it with the same verifier. What happens? Expected Outcome: The proof remains valid and the verifier accepts it because the circuit is the same!
-
Try modifying your proof in the proof.json file by changing the value of publicSignals, and then verify it. What happens? Expected Outcome: The proof is no longer valid! The new value of publicSignals, which represents the public statement, does not satisfy the relation defined in your circuit.
- Run
npx hardhat flatten contracts/MultiplierVerifier.sol > Flattened.sol
. - Go to https://explorer.xrplevm.org/ and verify your deployed contract using
Flattened.sol
.
- XRPL EVM Sidechain documentation: https://opensource.ripple.com/docs/evm-sidechain/intro-to-evm-sidechain/
- SnarkJS: https://github.com/iden3/snarkjs
- Based on tutorials from https://github.com/projectsophon/hardhat-circom & https://github.com/gmchad/zardkat.
You may encounter errors during deployment (solidity compiler version: * contracts/MultiplierVerifier.sol (^0.6.11)) of the contract related to the automatically generated verifier contract. Simply modify the Solidity version in the contract file to match the one specified in your Hardhat configuration file.
Repo | Link |
---|---|
Deploy your first Solidity contract on the XRPL EVM Sidechain using Hardhat 👷 | GitHub Repository |
Next.js x Rainbowkit Wallet configured for the XRPL EVM Sidechain 🌈 | GitHub Repository |
Resources | Link |
---|---|
Docs | https://docs.xrplevm.org/docs/evm-sidechain/intro-to-evm-sidechain/ |
Bridge & Faucet | https://bridge.xrplevm.org/ |
MetaMask | https://metamask.io/ |
Solidity | https://docs.soliditylang.org/en/v0.8.26/ |
Hardhat | https://hardhat.org/ |
Remix IDE | https://remix.ethereum.org/ |
Grants | https://xrplgrants.org/ |
Accelerator | https://xrplaccelerator.org/ |