Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Last active May 17, 2024 16:13
Show Gist options
  • Save Turupawn/6cbd87274aa3db1edb7e626cbfdbe084 to your computer and use it in GitHub Desktop.
Save Turupawn/6cbd87274aa3db1edb7e626cbfdbe084 to your computer and use it in GitHub Desktop.
zk intro

Install noir v17

Linux

mkdir -p $HOME/.nargo/bin && \
curl -o $HOME/.nargo/bin/nargo-x86_64-unknown-linux-gnu.tar.gz -L https://github.com/noir-lang/noir/releases/download/v0.17.0/nargo-x86_64-unknown-linux-gnu.tar.gz && \
tar -xvf $HOME/.nargo/bin/nargo-x86_64-unknown-linux-gnu.tar.gz -C $HOME/.nargo/bin/ && \
echo -e '\nexport PATH=$PATH:$HOME/.nargo/bin' >> ~/.bashrc && \
source ~/.bashrc

MacOs Apple Silicon

mkdir -p $HOME/.nargo/bin && \
curl -o $HOME/.nargo/bin/nargo-aarch64-apple-darwin.tar.gz -L https://github.com/noir-lang/noir/releases/download/v0.17.0/nargo-aarch64-apple-darwin.tar.gz && \
tar -xvf $HOME/.nargo/bin/nargo-aarch64-apple-darwin.tar.gz -C $HOME/.nargo/bin/ && \
echo '\nexport PATH=$PATH:$HOME/.nargo/bin' >> ~/.zshrc && \
source ~/.zshrc

MacOs Intel

mkdir -p $HOME/.nargo/bin && \
curl -o $HOME/.nargo/bin/nargo-x86_64-apple-darwin.tar.gz -L https://github.com/noir-lang/noir/releases/download/v0.17.0/nargo-x86_64-apple-darwin.tar.gz && \
tar -xvf $HOME/.nargo/bin/nargo-x86_64-apple-darwin.tar.gz -C $HOME/.nargo/bin/ && \
echo '\nexport PATH=$PATH:$HOME/.nargo/bin' >> ~/.zshrc && \
source ~/.zshrc

cd into the circuits directory

nargo new hello_world
cd hello_world
nargo build
nargo check

Edit circuit/Prover.toml

Example where x is private and y is public

x = "6"
y = "8"

Generate the zkp

nargo prove

Generate and deploy the verifier contract

nargo codegen-verifier

Now deploy the UltraVerifier contract located at circuit/contract/circuit/plonk_vk.sol to Scroll Sepolia. If you get the "Stack too deep error" while compiling, retry by enable the optimization to 200.

Deploy the Custom Logic contract (the verification counter)

We will generate storage proofs from this contract.

Deploy this by passing the verifier contract we just deployed

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

interface IUltraVerifier {
    function verify(bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool);
}

contract VerificationCounter
{
    uint public verifyCount;

    IUltraVerifier ultraVerifier;
    constructor(address ultraVerifierAddress)
    {
        ultraVerifier = IUltraVerifier(ultraVerifierAddress);
    }

    function sendProof(bytes calldata _proof, bytes32[] calldata _publicInputs) public
    {
        ultraVerifier.verify(_proof, _publicInputs);
        verifyCount+=1;
    }
}

Verify the proof

If you're verifying on remix, copy paste the proof located at circuit/contract/proofs/circuit.proof and append a 0x at the begining so remix doesn't complain. Next set the public input param that you can find on the Verifier.toml file on the following format ["0x0000000000000000000000000000000000000000000000000000000000000008"]. In our case this represents y=8 we set as the only public input

Now the verifyCount variable should be set to 1.

Learning resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment