Logo

dev-resources.site

for different kinds of informations.

Niftyzk Tutorials 5 - CosmWasm

Published at
1/11/2025
Categories
cosmwasm
circom
blockchain
web3
Author
strawberry666
Categories
4 categories in total
cosmwasm
open
circom
open
blockchain
open
web3
open
Author
13 person written this
strawberry666
open
Niftyzk Tutorials 5 - CosmWasm

This tutorial is the continuation of the series to explain Niftyzk for ZKP development and you should check out the previous tutorials to understand everything that’s going on here.

in Tutorials 4, we completed a Phase-2 ceremony for Groth-16 proving system, now we will continue that and export a CosmWasm contract

Previous tutorial

First, to export a cosmwasm contract, you will need a verification_key using niftyzk vkey and the unit tests need to pass.. You don’t need to finalize the circuits to create a verifier contract from it, however you will need to reexport your contract after finalization to match the new verification key.

Usage: niftyzk gencontract [options]

Generate a cosmwasm verifier smart contract

Options:
  --circuit        The full name of the circuit file to use. Defaults to circuit.circom
  --ark            Use the Arkworks Groth-16 verifier implementation
  --bellman        Use the Bellman Groth-16 verifier implementation
  --overwrite      If a contract directory already exists, you are required use this option to overwrite
                   it.
  --folder [name]  Specify the name of the generated contract's folder
  -h, --help       display help for command

Enter fullscreen mode Exit fullscreen mode

The niftyzk gencontract command accepts the above arguments. You can specify the name of the circuit if your using one with a different name. The --ark and --bellman allows you to select a Rust library and the --folder [name] lets you name the folder where you export the contract to. If the folder exists already, you need to explicitly overwrite it with the --overwrite flag.

Let’s export a contract now from a compiled circuit.

$ niftyzk gencontract --ark --folder contract
Writing .cargo/config.toml
Writing Cargo.toml
Writing .gitignore
Writing src/lib.rs
Writing src/msg.rs
Writing src/contract.rs
Writing src/verify.rs
Done

Enter fullscreen mode Exit fullscreen mode

You need to have rust installed. To compile the contract, the rust compiler backed must be installed: rustup target add wasm32-unknown-unknown and then run cargo test inside the directory, that will install the dependencies and runs the generated tests.

Build the contracts using cargo wasm and if you have installed cosmwasm-check with cargo insall cosmwasm-check you can verify that these are valid cosmwasm contracts cosmwasm-check ./target/wasm32-unknown-unknown/release/contract.wasm

now we verified that the contracts are correct. Let’s see what we got in the source code.

src/lib.rs is the entry point of our contract and /src/contract.rs contains the implementations. The zkp verification is found inside src/verify.rs

You are free to modify all code however if you change the inlined parameters your verification will fail.

To trigger the smart contract, you call the Query function with a VerifyProof message.
For the accepted proof format see the lib/contract.rs unit test test_verify

The proof_str variable is a json string that is serialized directly from the snarkjs groth16.fullProve proof , while the pub_input_str is the stringified publicSignals variable. Using it is pretty straight forward, which is the benefit of choosing Arkworks implementation. However while easy to use, the Arkworks library is not fully audited.
See the documentation to learn more:

arkworks.rs

arkworks github source code

Bellman

The Bellman library was originally developed for ZCash by Matter Labs and we are importing the DoraFactory fork, the old version has been archived.

$ niftyzk gencontract --bellman --folder secondcontract         
Writing .cargo/config.toml
Writing .gitignore
Writing Cargo.toml
Writing src/lib.rs
Writing src/msg.rs
Writing src/parser.rs
Writing src/types.rs
Writing src/verify.rs
Writing src/contract.rs
Writing lib/adapter.js
Done
Enter fullscreen mode Exit fullscreen mode

We generate a new contract inside the secondcontract directory and as you can see there are more things going on. It not only created a new contract directory with the rust code, but added a lib/adapter.js file to our lib!

The Bellman library uses uncompressed vkey and proofs which are in a different format.
Let’s see the adapter.
lib/adapter.js

/**
 * The verification key adapter transforms the verification key into a format usable by the Bellman_ce based verifier
 * @param {string} verification_key_string - The verification_key.json as a string
 * @returns {string} uncompressed_vkey - Returns an uncompressed version of the verificaiton_key.json which can be parsed inside the rust verifier contract
 */
export async function verificationKeyAdapter(verification_key_string);

Enter fullscreen mode Exit fullscreen mode

You need to adapt the verification key for use, this was done for you by the code generating process and the adapted vkey is inlined into the rust code.

/**
 * The proofAdapter adapts proofs generated with snarkjs into a format parsable by a bellman verifier.
 * Use this function on the front-end, to adapt proofs generated by the client for on-chain use
 * @param {string} verification_key_string - The contents of the verification_key.json as a string
 * @param {string} proof_string - The snarkjs generated proof as a string
 * @returns {string} - Returns the adapted proof to use inside rust bellman verifier
 */
export async function proofAdapter(verification_key_string, proof_string);
Enter fullscreen mode Exit fullscreen mode

The proof adapter is a function you will need to use on the client or server after you have created the proofs and want to call a smart contract with them.

If you check out the tests in src/contract.rs you will see an example of a adapted proof string and a adapted public input string.

The bellman library is more mature and recommended to use over the arkworks implementation, you should evaluate which fits your use-case better.

You can again run cargo wasm and use cosmwasm-check ./target/wasm32-unknown-unknown/release/contract.wasm

Once you have verified the contracts are valid they are ready for deployment.

I hope you like niftyzk and the tutorial series and keep on building!

blockchain Article's
30 articles in total
Favicon
Web3 Technologies: How the New Internet Era Works
Favicon
Emotional Resilience: A Key to Success Across Fields
Favicon
The Future of Web3 and Blockchain
Favicon
Blockchain: Connecting Digital Futures with Real-World Impact
Favicon
Vyper is redefining smart contract development with its focus on simplicity, security, and efficiency. With its rapidly growing community of developers, Vyper is becoming the go-to for smart contract development, creating a thriving ecosystem.
Favicon
Vyper - Write your First Python Smart Contract (Series)
Favicon
Closing the PKIX Working Group is, apparently, not news
Favicon
Top 5 Tools & Technologies for LLM Development 2025
Favicon
What is Blockchain?
Favicon
What is Manta Bridge? A Complete Guide
Favicon
Will the Ripple Case Set New Rules for Crypto?
Favicon
The Rise of Payable AI: Shaping the Future of Artificial Intelligence
Favicon
Optimism Bridge: Your Gateway to Faster and Cheaper Crypto Transfers
Favicon
What Is Proof of Attribution? A Complete Guide to Its Role in Blockchain and AI
Favicon
Niftyzk Tutorials 5 - CosmWasm
Favicon
Niftyzk tutorials 1
Favicon
#Vinnifinni
Favicon
Join Us at HederaCon 2025!
Favicon
Mint Blockchain Community Gathering Vol. 5: Embarking on an Exciting Journey in 2025!
Favicon
Mint Blockchain: Kicking Off the New Year with Record-Breaking Performance!
Favicon
ERC-4337 in 2024 Review, OpenZK’s L2 Launch, Arcana’s Chain Abstraction SDK, and Rethinking Blockchain Modularity in 2025
Favicon
Why is blockchain knowledge important even if you are not a programmer? 🔗
Favicon
下一代社交媒体:Web3 SocialFi的崛起与潜力
Favicon
AI + Web3 多领域的蓬勃发展与未来展望 | Nx.one
Favicon
Discover Top-Notch Decentralized Finance Exchange Development Services
Favicon
Solana Account Model Simplified
Favicon
Why Should You Choose Wazirx Clone Script for Crypto Business?
Favicon
Metaverse Trends: Holiverse & Meta’s Ambitions And The Sandbox & Decentraland’s Success
Favicon
Top 5 Strategies to Ensure Secure AI Model Training in 2025
Favicon
Digital Credentials and Identity Management Solutions

Featured ones: