knocks-public / 2024-CircuitBreaker

MIT License
2 stars 0 forks source link

update README and test code for sindri hub upload #119

Closed susumutomita closed 2 months ago

susumutomita commented 2 months ago

Update README and Test Code for Sindri Hub Upload

Description

This pull request updates the README file and test code to support uploading circuits to the Sindri Hub. The main changes include:

Changes

  1. Makefile

    • Added deploy and login commands to streamline the deployment process.
    .PHONY: init
    init:
        npx sindri init
    
    .PHONY: login
    login:
        npx sindri login
    
    .PHONY: deploy
    deploy:
        npx sindri deploy
  2. Nargo.toml

    • Updated the compiler_version to 0.28.0.
  3. README.md

    • Added a new section detailing how to deploy the circuit and generate proofs using a sample JavaScript program.
  4. Test Code in main.nr

    • Updated the main function to return a boolean value.
    • Modified test cases to use proper assertions.
    pub fn main(input: u8) -> pub bool {
        input >= 20
    }
    
    #[test]
    fn test_main_with_valid_age() {
        let result = main(20);
        assert(result == true);
    }
    
    #[test]
    fn test_main_with_invalid_age() {
        let result = main(19);
        assert(result == false);
    }
    
    #[test]
    fn test_main_with_zero() {
        let result = main(0);
        assert(result == false);
    }
    
    #[test]
    fn test_main_with_200() {
        let result = main(200);
        assert(result == true);
    }

Steps to Generate a Proof

  1. Deploy the Circuit

    make deploy
  2. Generate a Proof Use the following sample program to generate and verify proofs using the Sindri API.

    const process = require("process");
    const axios = require("axios");
    const toml = require('@iarna/toml');
    
    // NOTE: Provide your API key here.
    const API_KEY = process.env.SINDRI_API_KEY || "";
    const API_URL_PREFIX = process.env.SINDRI_API_URL || "https://sindri.app/api/";
    
    const API_VERSION = "v1";
    const API_URL = API_URL_PREFIX.concat(API_VERSION);
    
    const headersJson = {
      Accept: "application/json",
      Authorization: `Bearer ${API_KEY}`
    };
    
    async function pollForStatus(endpoint, timeout = 20 * 60) {
      for (let i = 0; i < timeout; i++) {
        const response = await axios.get(API_URL + endpoint, {
          headers: headersJson,
          validateStatus: (status) => status === 200,
        });
    
        const status = response.data.status;
        if (["Ready", "Failed"].includes(status)) {
          console.log(`Poll exited after ${i} seconds with status: ${status}`);
          return response;
        }
    
        await new Promise((r) => setTimeout(r, 1000));
      }
    
      throw new Error(`Polling timed out after ${timeout} seconds.`);
    }
    
    async function main() {
      try {
        const circuitId = "Specify the circuit id";
        console.log("Proving circuit...");
        const proofInput = toml.stringify({ input: 10 });
        const proveResponse = await axios.post(
          API_URL + `/circuit/${circuitId}/prove`,
          { proof_input: proofInput , perform_verify: true},
          { headers: headersJson, validateStatus: (status) => status === 201 },
        );
        const proofId = proveResponse.data.proof_id;
    
        const proofDetailResponse = await pollForStatus(`/proof/${proofId}/detail`);
    
        const proofDetailStatus = proofDetailResponse.data.status;
        if (proofDetailStatus === "Failed") {
          throw new Error("Proving failed");
        }
    
        const proverTomlContent = proofDetailResponse.data.proof_input['Prover.toml'];
        const verifierTomlContent = proofDetailResponse.data.public['Verifier.toml'];
    
        console.log(proverTomlContent);
        console.log(verifierTomlContent);
    
        const publicOutput = verifierTomlContent;
        console.log(`Circuit proof output signal: ${publicOutput}`);
      } catch (error) {
        if (error instanceof Error) {
          console.error(error.message);
        } else {
          console.error("An unknown error occurred.");
        }
      }
    }
    
    if (require.main === module) {
      main();
    }

Dependencies