Skip to content

Commit

Permalink
Added apps/circuits (#10)
Browse files Browse the repository at this point in the history
* Added circom to perform range check

* using fully qualified name to get contract artifact

* minor update

* prettier:write

* adding to the test cases

* Adding apps/circuits

* added circuits components

* updated with rangecheck

* removed circuits from contracts

* Added script to copy app/circuits compiled assets over to app/contracts dir inside

* passing prettier:write
  • Loading branch information
jimmychu0807 authored Sep 9, 2024
1 parent dc09638 commit 52b9de8
Show file tree
Hide file tree
Showing 26 changed files with 1,194 additions and 1,254 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1 align="center">
PSE Core Hackathon
PSE Core Stage 2: Number Guessing Game
</h1>

<p align="center">
Expand All @@ -14,8 +14,8 @@
</a>
</p>

| The repository is divided into two components: [web app](./apps/web-app) and [contracts](./apps/contracts). Hopefully the app will be deployed on Sepolia. |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The repository is divided into two components: [web app](./apps/web-app), [contracts](./apps/contracts), and [circuits](./apps/circuits). Hopefully the app will be deployed on Sepolia. |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

This repo is modified from [Semaphore Boilerplate Template](https://github.com/semaphore-protocol/boilerplate)

Expand Down Expand Up @@ -92,3 +92,10 @@ or to automatically format the code:
```bash
yarn prettier:write
```

### Key Components

Key components for writing circuits

- [circomkit](https://github.com/erhant/circomkit/tree/main)
- [hardhat-circom](https://github.com/projectsophon/hardhat-circom)
2 changes: 2 additions & 0 deletions apps/circuits/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
artifacts
src/test
7 changes: 7 additions & 0 deletions apps/circuits/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": ["ts"],
"require": "ts-node/register",
"spec": "./tests/*.test.ts",
"timeout": 100000,
"exit": true
}
17 changes: 17 additions & 0 deletions apps/circuits/circomkit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"protocol": "plonk",
"optimization": 1,
"prime": "bn128",
"version": "2.1.6",
"circuits": "./circuits.json",

"dirCircuits": "./src",
"dirInputs": "./src/inputs",
"dirPtau": "./artifacts/ptau",
"dirBuild": "./artifacts/build",

"prettyCalldata": true,
"inspect": true,
"logLevel": "INFO",
"verbose": true
}
7 changes: 7 additions & 0 deletions apps/circuits/circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"submit-rangecheck-1-100": {
"file": "submit-rangecheck",
"template": "SubmissionRangeCheck",
"params": [1, 100, 7]
}
}
35 changes: 35 additions & 0 deletions apps/circuits/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "circuits",
"version": "0.0.1",
"description": "Guessing game circuits",
"license": "MIT",
"files": [
"**/*.circom",
"!main",
"!test",
"LICENSE",
"README.md"
],
"scripts": {
"compile": "./scripts/build-circuit.sh",
"prove": "timeout 5s circomkit witness submit-rangecheck-1-100 in-range; timeout 5s circomkit prove submit-rangecheck-1-100 in-range",
"verify": "timeout 5s circomkit verify submit-rangecheck-1-100 in-range",
"clear": "circomkit clear",
"test": "mocha"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"circomlib": "2.0.5",
"snarkjs": "^0.7.4"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/snarkjs": "^0.7.4",
"@zk-kit/baby-jubjub": "1.0.1",
"circomkit": "^0.2.1",
"mocha": "^10.2.0",
"poseidon-lite": "^0.2.0"
}
}
24 changes: 24 additions & 0 deletions apps/circuits/scripts/build-circuit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash -e
CIRCUIT=$1
TIMEOUT_SEC="6s"

# Yellow color escape code
YELLOW='\033[1;33m'
# Reset Color
NC='\033[0m'


echo -e "${YELLOW}Compiling ${CIRCUIT}${NC}"
yarn circomkit compile ${CIRCUIT}

echo -e "\n${YELLOW}Setting up ${CIRCUIT}${NC}"
timeout ${TIMEOUT_SEC} yarn circomkit setup ${CIRCUIT} || true
timeout ${TIMEOUT_SEC} yarn circomkit contract ${CIRCUIT} || true

echo -e "\n${YELLOW}Copying circuit assets from app/circuits to app/contracts ${NC}"
mkdir -p ../contracts/artifacts/circuits
cp artifacts/build/${CIRCUIT}/${CIRCUIT}.r1cs ../contracts/artifacts/circuits/${CIRCUIT}.r1cs
cp artifacts/build/${CIRCUIT}/plonk_pkey.zkey ../contracts/artifacts/circuits/${CIRCUIT}.zkey
cp artifacts/build/${CIRCUIT}/${CIRCUIT}_js/${CIRCUIT}.wasm ../contracts/artifacts/circuits/${CIRCUIT}.wasm

cp artifacts/build/${CIRCUIT}/plonk_verifier.sol ../contracts/contracts/${CIRCUIT}_verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": 101
}
6 changes: 6 additions & 0 deletions apps/circuits/src/main/submit-rangecheck-1-100.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// auto-generated by circomkit
pragma circom 2.1.6;

include "../submit-rangecheck.circom";

component main = SubmissionRangeCheck(1, 100, 7);
17 changes: 17 additions & 0 deletions apps/circuits/src/submit-rangecheck.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma circom 2.1.6;

include "../../../node_modules/circomlib/circuits/comparators.circom";

template SubmissionRangeCheck(min, max, nBit) {
signal input in;
signal output out;

component lessEqThan = LessEqThan(nBit);
lessEqThan.in <== [in, max];

component greaterEqThan = GreaterEqThan(nBit);
greaterEqThan.in <== [in, min];

out <== lessEqThan.out * greaterEqThan.out;
out === 1;
}
11 changes: 11 additions & 0 deletions apps/circuits/tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Circomkit } from "circomkit";
import { readFileSync } from "fs";
import path from "path";

const configFilePath = path.join(__dirname, "../circomkit.json");
const config = JSON.parse(readFileSync(configFilePath, "utf-8"));

export const circomkit = new Circomkit({
...config,
verbose: false,
});
35 changes: 35 additions & 0 deletions apps/circuits/tests/submit-rangecheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { WitnessTester } from "circomkit";
import { circomkit } from "./common";

describe("submit-rangecheck", () => {
let circuit: WitnessTester<
["in"] // private inputs
>;

const [MIN, MAX, NBITS] = [1, 100, 7];

before(async () => {
circuit = await circomkit.WitnessTester("submit-rangecheck", {
file: "submit-rangecheck",
template: "SubmissionRangeCheck",
params: [MIN, MAX, NBITS],
});
});

it("Should pass when the value is within range", async () => {
let INPUT = { in: MIN };
const OUT = { out: 1 };
await circuit.expectPass(INPUT, OUT);

INPUT = { in: MAX };
await circuit.expectPass(INPUT, OUT);
});

it("Should fail when the value is out of range", async () => {
let INPUT = { in: MIN - 1 };
await circuit.expectFail(INPUT);

INPUT = { in: MAX + 1 };
await circuit.expectFail(INPUT);
});
});
8 changes: 8 additions & 0 deletions apps/circuits/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"esModuleInterop": true
},
"include": ["tests/**/*"]
}
24 changes: 0 additions & 24 deletions apps/contracts/circuits/lib.circom

This file was deleted.

8 changes: 0 additions & 8 deletions apps/contracts/circuits/lib.json

This file was deleted.

18 changes: 15 additions & 3 deletions apps/contracts/contracts/GuessingGame.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ contract GuessingGame is IGuessingGame, Ownable {
_;
}

// View functions
/**
* View functions
**/

function getGame(uint32 gameId) public view validGameId(gameId) returns (GameView memory) {
Game storage game = games[gameId];

Expand All @@ -86,7 +89,14 @@ contract GuessingGame is IGuessingGame, Ownable {
});
}

// Helper functions
function getGameHost(uint32 gameId) public view validGameId(gameId) returns (address) {
Game storage game = games[gameId];
return game.players[0];
}

/**
* Helpers functions
**/

function _verifyBidProof(
bytes32 proof,
Expand Down Expand Up @@ -134,7 +144,9 @@ contract GuessingGame is IGuessingGame, Ownable {
}
}

// Main functions
/**
* Main functions
**/

function newGame() external override returns (uint32 gameId) {
Game storage game = games.push();
Expand Down
Loading

0 comments on commit 52b9de8

Please sign in to comment.