Skip to content

Commit

Permalink
refactor: circomkit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVikasRushi committed Jul 21, 2024
1 parent 45d56b1 commit 72c6048
Show file tree
Hide file tree
Showing 14 changed files with 709 additions and 382 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: tests

on:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install --yes \
build-essential \
libgmp-dev \
libsodium-dev \
nasm \
nlohmann-json3-dev
- name: Download Circom Binary v2.1.5
run: |
wget -qO /home/runner/work/circom https://github.com/iden3/circom/releases/download/v2.1.5/circom-linux-amd64
chmod +x /home/runner/work/circom
sudo mv /home/runner/work/circom /bin/circom
- name: Print Circom version
run: circom --version

- name: Install dependencies
run: yarn

- name: Run tests
run: yarn test
124 changes: 122 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,122 @@
node_modules
build
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# builds
build
dist

# circuit-specific powers of tau are ignored
*.ptau
# universal ptaus not ignored
!ptau/*
# temporary ptaus are ignored
tmp.ptau

# is this still a thing lol
.DS_Store

# ignore auto generated test circuits
circuits/test
ptau
7 changes: 7 additions & 0 deletions .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
}
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
printWidth: 120,
};
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["iden3.circom"]
}
32 changes: 0 additions & 32 deletions circuits.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
{
"multiplier_3": {
"file": "multiplier",
"template": "Multiplier",
"params": [3]
},
"sha256_32": {
"file": "sha256",
"template": "Sha256Bytes",
"params": [32]
},
"sudoku_9x9": {
"file": "sudoku",
"template": "Sudoku",
"pubs": ["puzzle"],
"params": [3]
},
"sudoku_4x4": {
"file": "sudoku",
"template": "Sudoku",
"pubs": ["puzzle"],
"params": [2]
},
"fp64": {
"file": "float_add",
"template": "FloatAdd",
"params": [11, 52]
},
"fp32": {
"file": "float_add",
"template": "FloatAdd",
"params": [8, 23]
},
"fibonacci_11": {
"file": "fibonacci",
"template": "Fibonacci",
Expand Down
33 changes: 33 additions & 0 deletions circuits/fibonacci.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pragma circom 2.0.0;

// Fibonacci with custom starting numbers
template Fibonacci(n) {
assert(n >= 2);
signal input in[2];
signal output out;

signal fib[n+1];
fib[0] <== in[0];
fib[1] <== in[1];
for (var i = 2; i <= n; i++) {
fib[i] <== fib[i-2] + fib[i-1];
}

out <== fib[n];
}

// Fibonacci with custom starting numbers, recursive & inefficient
template FibonacciRecursive(n) {
signal input in[2];
signal output out;
component f1, f2;
if (n <= 1) {
out <== in[n];
} else {
f1 = FibonacciRecursive(n-1);
f1.in <== in;
f2 = FibonacciRecursive(n-2);
f2.in <== in;
out <== f1.out + f2.out;
}
}
5 changes: 4 additions & 1 deletion circuits/subbytes.circom
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ pragma circom 2.0.0;

template SubBytes(n) {
signal input key[n];
log(key);
log("key" , key[0]);

signal output out;
out <==1;
}
3 changes: 3 additions & 0 deletions inputs/fibonacci_11/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"in": [1, 1]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"description": "AES",
"description": "AES circom",
"scripts": {
"start": "npx ts-node ./src/index.ts",
"test": "npx mocha"
"test": "npx mocha",
"compile:test": "npx circomkit compile fibonacci_11 && npx circomkit prove fibonacci_11 default && npx circomkit verify fibonacci_11 default"
},
"dependencies": {
"circomkit": "^0.0.22",
Expand Down
33 changes: 0 additions & 33 deletions src/index.ts

This file was deleted.

51 changes: 51 additions & 0 deletions tests/fibonacci.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { WitnessTester } from "circomkit";
import { circomkit } from "./common";

describe("fibonacci", () => {
const N = 7;
let circuit: WitnessTester<["in"], ["out"]>;

describe("vanilla", () => {
before(async () => {
circuit = await circomkit.WitnessTester(`fibonacci_${N}`, {
file: "fibonacci",
template: "Fibonacci",
params: [N],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("should compute correctly", async () => {
await circuit.expectPass({ in: [1, 1] }, { out: fibonacci([1, 1], N) });
});
});

describe("recursive", () => {
before(async () => {
circuit = await circomkit.WitnessTester(`fibonacci_${N}_recursive`, {
file: "fibonacci",
template: "FibonacciRecursive",
params: [N],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("should compute correctly", async () => {
await circuit.expectPass({ in: [1, 1] }, { out: fibonacci([1, 1], N) });
});
});
});

// simple fibonacci with 2 variables
function fibonacci(init: [number, number], n: number): number {
if (n < 0) {
throw new Error("N must be positive");
}

let [a, b] = init;
for (let i = 2; i <= n; i++) {
b = a + b;
a = b - a;
}
return n === 0 ? a : b;
}
17 changes: 8 additions & 9 deletions tests/subbytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import { WitnessTester } from "circomkit";
import { circomkit } from "./common";

describe("SubBytes", () => {
const f = (str: string) => Array.from(Buffer.from(str, "utf-8"));
const length = 128;
let circuit: WitnessTester<["key"], []>;
const N = 2;
let circuit: WitnessTester<["key"], ["out"]>;

describe("matcher test-1", () => {
describe("vanilla", () => {
before(async () => {
circuit = await circomkit.WitnessTester(`SubBytes_${length}`, {
circuit = await circomkit.WitnessTester(`SubBytes_${N}`, {
file: "subbytes",
template: "SubBytes",
params: [16],
params: [N],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it(async () => {
// circuit.compute({ key: f("abcd") }, []);
await circuit.expectPass({ key: f("abcd") });
it("should compute correctly", async () => {
const witness = await circuit.calculateWitness({ key: [1, 1] });
await circuit.expectPass({ key: [1, 1] }, { out: 1 });
});
});
});
Loading

0 comments on commit 72c6048

Please sign in to comment.