Skip to content

Commit

Permalink
import circomkit
Browse files Browse the repository at this point in the history
  • Loading branch information
funkyenough committed Sep 20, 2024
1 parent eb4bb91 commit d045591
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/hardhat/circomkit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "2.1.4",
"verbose": true,
"prime": "bn128",
"optimization": 1,
"protocol": "groth16",
"circuits": "./circuits.json",
"dirPtau": "./circuit/ptau",
"dirCircuits": "./circuit/circuits",
"dirInputs": "./circuit/inputs",
"dirBuild": "./circuit/build"
}
50 changes: 50 additions & 0 deletions packages/hardhat/circuit/circuits/isCloser.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pragma circom 2.0.0;

// TODO Add Range Check

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

template isCloser() {
signal input treasureCoord[2];
signal input userPrevCoord[2];
signal input userCurrCoord[2];
// signal input hash;

signal prevXDiffSquared;
signal prevYDiffSquared;
signal prevDist;

signal currXDiffSquared;
signal currYDiffSquared;
signal currDist;

signal output isCloser;

component lt = LessThan(64);
// component hasher = Poseidon(2);

// hasher.in[0] <== treasureCoord[0];
// hasher.in[1] <== treasureCoord[1];
// hasher.out[0] === hash;

prevXDiffSquared <== (treasureCoord[0] - userPrevCoord[0]) * (treasureCoord[0] - userPrevCoord[0]);
prevYDiffSquared <== (treasureCoord[1] - userPrevCoord[1]) * (treasureCoord[1] - userPrevCoord[1]);
prevDist <== prevXDiffSquared + prevYDiffSquared;

currXDiffSquared <== (treasureCoord[0] - userCurrCoord[0]) * (treasureCoord[0] - userCurrCoord[0]);
currYDiffSquared <== (treasureCoord[1] - userCurrCoord[1]) * (treasureCoord[1] - userCurrCoord[1]);
currDist <== currXDiffSquared + currYDiffSquared;

lt.in[0] <== currDist;
lt.in[1] <== prevDist;
isCloser <== lt.out;
}

// component main {public [userPrevCoord, userCurrCoord]} = isCloser();

/* INPUT = {
"treasureCoord": ["12345", "12345"],
"userPrevCoord": ["12343", "12343"],
"userCurrCoord": ["12344", "123444"]
}*/
43 changes: 43 additions & 0 deletions packages/hardhat/circuit/circuits/isFurther.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pragma circom 2.0.0;

// TODO Add Range Check

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

template isFurther() {
signal input treasureCoord[2];
signal input userPrevCoord[2];
signal input userCurrCoord[2];

signal prevXDiffSquared;
signal prevYDiffSquared;
signal prevDist;

signal currXDiffSquared;
signal currYDiffSquared;
signal currDist;

signal output isFurther;

component gt = GreaterThan(64);

prevXDiffSquared <== (treasureCoord[0] - userPrevCoord[0]) * (treasureCoord[0] - userPrevCoord[0]);
prevYDiffSquared <== (treasureCoord[1] - userPrevCoord[1]) * (treasureCoord[1] - userPrevCoord[1]);
prevDist <== prevXDiffSquared + prevYDiffSquared;

currXDiffSquared <== (treasureCoord[0] - userCurrCoord[0]) * (treasureCoord[0] - userCurrCoord[0]);
currYDiffSquared <== (treasureCoord[1] - userCurrCoord[1]) * (treasureCoord[1] - userCurrCoord[1]);
currDist <== currXDiffSquared + currYDiffSquared;

gt.in[0] <== currDist;
gt.in[1] <== prevDist;
isFurther <== gt.out;
}

component main {public [userPrevCoord, userCurrCoord]} = isFurther();

/* INPUT = {
"treasureCoord": ["12345", "12345"],
"userPrevCoord": ["12343", "12343"],
"userCurrCoord": ["12344", "12344"]
}*/
37 changes: 37 additions & 0 deletions packages/hardhat/circuit/circuits/isTreasure.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma circom 2.0.0;

// TODO Add Range Check

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

template isTreasure() {
signal input treasureCoord[2];
signal input userCoord[2];

signal xDiffSquared;
signal yDiffSquared;
signal dist;

signal output isTreasure;

component iz = IsZero();

xDiffSquared <== (treasureCoord[0] - userCoord[0]) * (treasureCoord[0] - userCoord[0]);
yDiffSquared <== (treasureCoord[1] - userCoord[1]) * (treasureCoord[1] - userCoord[1]);
dist <== xDiffSquared + yDiffSquared;

iz.in <== dist;
isTreasure <== iz.out;
}

component main {public [userCoord]} = isTreasure();

/* INPUT = {
"treasureCoord": ["12345", "12345"],
"userCoord": ["12345", "12345"]
}*/

/* INPUT = {
"treasureCoord": ["12345", "12345"],
"userCoord": ["12345", "12344"]
}*/
14 changes: 14 additions & 0 deletions packages/hardhat/circuit/inputs/isCloser/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"treasureCoord": [
"12345",
"12345"
],
"userPrevCoord": [
"12343",
"12343"
],
"userCurrCoord": [
"12344",
"123444"
]
}
11 changes: 11 additions & 0 deletions packages/hardhat/circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"isCloser": {
"file": "isCloser",
"template": "isCloser",
"pubs": [
"treasureCoord",
"userPrevCoord",
"userCurrCoord"
]
}
}
1 change: 1 addition & 0 deletions packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@openzeppelin/contracts": "~4.8.1",
"@typechain/ethers-v6": "~0.5.1",
"circomkit": "^0.3.0",
"circomlib": "^2.0.5",
"dotenv": "~16.0.3",
"envfile": "~6.18.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# More info: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
NEXT_PUBLIC_ALCHEMY_API_KEY=
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID=

NEXT_PUBLIC_GOOGLE_MAP_API_KEY=
Loading

0 comments on commit d045591

Please sign in to comment.