Skip to main content

Circuit Developer

This tutorial will walk you through using the kalypso-sdk to create a market for your circuit and request proofs.

Pre-requisites

  • Ensure that your Noir application circuit can be compiled before proceeding with creating a market.

  • To get started, you will need to use the kalypso-sdk.

    • You can find multiple instructions/examples/scripts in the kalypso-sdk examples repository. These instructions are replicated below where required. However, you may browse through them now to gain familiarity if interested.

    • Clone the above repo

      git clone https://github.com/marlinprotocol/kalypso-sdk-examples
      git checkout noir_examples
    • Install all the required node_modules. This step will also install kalypso-sdk itself.

      npm install --include-dev
    • To run a specific script, use the below command.

      npm run ts-node ./path_to/script_file.ts`

Creating a market for your Noir circuit

Creating a Kalypso market for your Noir circuit helps users of your circuit to outsource proof generation to hardware operators in a secure and scalable way.

  1. Clone the helper tool to create an enclave image

    • The prover is required to be run within an enclave to ensure hardware operators can't access private inputs. To build the enclave image, clone the helper tool.
  2. Update the path to your circuit's repository

    • Replace the path to the circuit's repository with the GitHub link to your application's circuit here. It needn't be a GitHub link but a path to a local directory.

    • For the purpose of this tutorial, a test circuit is provided here.

    • Update the config.toml file to match the name of the your application

      toml_path = "/app/{project_name}"  # Path where Nargo.toml will be located
      output_path = "/app/{github_project_name}/proofs/{project_name}.proof" # Path for proof output
  3. Build the enclave image

    • Run build.sh to create the enclave image. This will generate nitro-enclave.eif.

      note

      The image built using this tutorial will be built for amd architecture. Thus, it can only be deployed on amd machines on AWS Nitro. Steps above will require a few changes to target arm architectures.

  4. Obtain the PCRs of the enclave

    • Once built successfully, you will get the PCRs of the enclave which will look something like this:

      Make a note of the PCR values:

      "PCR0": "79f30b845cf0fe67f960adc84cdd5e80bcf06a96640acf4dbc393e277ba73782020c16188ad3fd0f60b0121a2f2da968",
      "PCR1": "bcdf05fefccaa8e55bf2c8d6dee9e79bbff31e34bf28a99aa19e6b29c37ee80b214a414b7607236edf26fcb78654e63f",
      "PCR2": "c580b951db7b9981bde4ec14d7c1bc8cce2d51b873cdd4a47a34c5d4279163b0928c82bc3410cc405301be66357f3ddd"
  5. Host the enclave image

    • Host the nitro-enclave.eif file on Amazon S3 or IPFS to ensure availability of the enclave image

      info

      This is required as hard provider need to pull the image.

  6. Configure and deploy the TEE-based verifier contract

    • As proofs are expected to be generated within a TEE, the same TEE can attest as to whether inputs are invalid (i.e. proofs can not be generated for given inputs) and that the proof generated is correct. However, a smart contract is required to verify the TEE's signatures to ensure that such claims are generated from within a genuine enclave.

    • The scripts in the example repo cloned in the pre-requisite section can be used to deploy the smart contract.

    • Replaced the PCRs of the enclave in the linked file with the PCRs noted in the step above.

    • Invoke the script. This will print the address of TeeVerifier contract.

    • Save the address of TeeVerifier in requestData.json for it to be available to the other scripts.

  7. Create the market

    • Invoke the script provided here. You should receive a marketId here (this is unique to the market; please save it, preferably here as well so that other scripts can use it).

      note
      • There are additional parameters like minimum stake required per proof which can be configured. This is amount of tokens that generator would need to lock their per request in order to receive and process a proof request.
      • Creating a market also requires some USDC. You can reach out over discord to get help.

Making proof requests

Requesting proof starts with submitting a proof-request on chain and proof being submitted by the selected prover. To submit a proof-request you need to:

  1. Encode the inputs

    • Encode the inputs to transmit to the prover as shown in the linked script.
  2. Encrypt the private inputs

    • If there are private inputs, the SDK will automatically encrypt it with matching engine's public key.
    • If there are no private inputs, you can just leave the value as an empty string.
  3. Set an assignment deadline

    • Decide and set the number of blocks you are willing to wait for Kalypso to find a suitable prover.

      info

      If there is an idle prover with an acceptable price and time quote, your request will be assigned immediately.`

  4. Set the maximum proof generation time

    • Set the maximum proof generation time you wish to give the prover (after assignment) to generate the proof for your request.

      info

      Your task will not be assigned to a prover whose proving time guarantee is more than the your acceptable limit.

  5. Set the maximum price

    • Set the maximum price you would be willing to pay for the proof request. The same amount of USDC is then locked in the marketplace contract.

      info
      • Your task will not be assigned to a prover whose quoted cost to generate the proof is less than the your acceptable limit. If the task is assigned to a prover with a lower price quote, the difference will be refunded to your wallet post proof generation.

      • If the hardware operator fails to generate a valid proof within the time limit, the entire amount will be refunded to your wallet.

  6. Create the proof request

    • Ensure that the wallet making the request has sufficient ETH and USDC to create the request.

      tip

      You can request us testnet USDC on discord

    • Create the request. This will generate an unique askId.

  7. Monitor for the proof

    • Monitor the chain for the proof. Once an event corresponding to the askId is detected, the proof will be printed on the console. It is recommended to save the proof to a file.