Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add zk circuits and contracts #3

Merged
merged 8 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
138 changes: 130 additions & 8 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,136 @@
node_modules
.env
# 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
coverage.json
typechain
typechain-types
*.lcov

#Hardhat files
cache
artifacts
# 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

.debugger/
compiler_config.json
remix-compiler.config.js

# 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
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
circuits/build
artifacts
cache
*.ptau
deployments
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions backend/circuits/HashCheck/circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pragma circom 2.0.0;

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

template Main() {
signal input x;
signal input hash;

signal output out;

component mimc = MiMCSponge(1, 220, 1);
mimc.ins[0] <== x;
mimc.k <== 0;

out <== mimc.outs[0];

out === hash;
}

component main = Main();
43 changes: 43 additions & 0 deletions backend/circuits/HashCheck/compileHashCheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

mkdir build

rm build/circuit.r1cs
rm build/circuit.sym
rm build/circuit_0000.zkey
rm build/circuit_js
rm build/witness.wtns
rm build/pot12_0000.ptau
rm build/pot12_0001.ptau
rm build/pot12_final.ptau

if [ -f ./powersOfTau28_hez_final_12.ptau ]; then
echo "powersOfTau28_hez_final_12.ptau already exists. Skipping."
else
echo 'Downloading powersOfTau28_hez_final_12.ptau'
wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_12.ptau
fi

echo "Compiling circuit.circom..."

circom circuit.circom --r1cs --wasm --sym -o build
node build/circuit_js/generate_witness.js build/circuit_js/circuit.wasm build/input.json build/witness.wtns
# # # cp circuit/witness.wtns ../witness.wtns

snarkjs r1cs info build/circuit.r1cs

# # # phase 1 of ceremony
snarkjs powersoftau new bn128 12 build/pot12_0000.ptau -v
snarkjs powersoftau contribute build/pot12_0000.ptau build/pot12_0001.ptau --name="First contribution" -v

# # # phase 2 of ceremony
snarkjs powersoftau prepare phase2 build/pot12_0001.ptau build/pot12_final.ptau -v
snarkjs groth16 setup build/circuit.r1cs powersOfTau28_hez_final_12.ptau build/circuit_0000.zkey
snarkjs zkey contribute build/circuit_0000.zkey build/circuit_final.zkey --name="1st Contribution Name" -v -e="random text"
snarkjs zkey export verificationkey build/circuit_final.zkey build/verification_key.json
snarkjs groth16 prove build/circuit_final.zkey build/witness.wtns build/proof.json build/public.json
snarkjs groth16 verify build/verification_key.json build/public.json build/proof.json



snarkjs zkey export solidityverifier build/circuit_final.zkey build/circuitVerifier.sol

Binary file not shown.
Binary file not shown.
66 changes: 66 additions & 0 deletions backend/circuits/OTPVerification/circuit.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pragma circom 2.0.0;

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

template hashLeaves(){
signal input leftLeaf;
signal input rightLeaf;

signal output out;

component h = Poseidon(2);
h.inputs[0] <== leftLeaf;
h.inputs[1] <== rightLeaf;

out <== h.out;
}

template MerkleTreeInclusionVerification(n) {
signal input time;
signal input otp;
signal input pathElements[n];
signal input pathIndex[n];

signal output root;

signal leaf;

component hl = hashLeaves();
hl.leftLeaf <== time;
hl.rightLeaf <== otp;

leaf <== hl.out;

component h[n];
signal levelHashes[n+1];

levelHashes[0] <== leaf;

component mux[n];

for(var i=0; i<n; i++){

(pathIndex[i])*(1 - pathIndex[i]) === 0;

h[i] = hashLeaves();
mux[i] = MultiMux1(2);

mux[i].c[0][0] <== levelHashes[i];
mux[i].c[0][1] <== pathElements[i];

mux[i].c[1][0] <== pathElements[i];
mux[i].c[1][1] <== levelHashes[i];

mux[i].s <== pathIndex[i];

h[i].leftLeaf <== mux[i].out[0];
h[i].rightLeaf <== mux[i].out[1];

levelHashes[i+1] <== h[i].out;
}

root <== levelHashes[n];
}

component main { public [time] } = MerkleTreeInclusionVerification(7);
42 changes: 42 additions & 0 deletions backend/circuits/OTPVerification/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

mkdir build

rm build/circuit.r1cs
rm build/circuit.sym
rm build/circuit_0000.zkey
rm build/circuit_js
rm build/witness.wtns
rm build/pot12_0000.ptau
rm build/pot12_0001.ptau
rm build/pot12_final.ptau

if [ -f ./powersOfTau28_hez_final_12.ptau ]; then
echo "powersOfTau28_hez_final_12.ptau already exists. Skipping."
else
echo 'Downloading powersOfTau28_hez_final_12.ptau'
wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_12.ptau
fi

echo "Compiling circuit.circom..."

circom circuit.circom --r1cs --wasm --sym -o build
node build/circuit_js/generate_witness.js build/circuit_js/circuit.wasm build/input.json build/witness.wtns
# # # cp circuit/witness.wtns ../witness.wtns

snarkjs r1cs info build/circuit.r1cs

# # # phase 1 of ceremony
snarkjs powersoftau new bn128 12 build/pot12_0000.ptau -v
snarkjs powersoftau contribute build/pot12_0000.ptau build/pot12_0001.ptau --name="First contribution" -v

# # # phase 2 of ceremony
snarkjs powersoftau prepare phase2 build/pot12_0001.ptau build/pot12_final.ptau -v
snarkjs groth16 setup build/circuit.r1cs powersOfTau28_hez_final_12.ptau build/circuit_0000.zkey
snarkjs zkey contribute build/circuit_0000.zkey build/circuit_final.zkey --name="1st Contribution Name" -v -e="random text"
snarkjs zkey export verificationkey build/circuit_final.zkey build/verification_key.json
snarkjs groth16 prove build/circuit_final.zkey build/witness.wtns build/proof.json build/public.json
snarkjs groth16 verify build/verification_key.json build/public.json build/proof.json



snarkjs zkey export solidityverifier build/circuit_final.zkey build/circuitVerifier.sol
42 changes: 42 additions & 0 deletions backend/circuits/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

mkdir HashCheck

rm HashCheck/HashCheck.r1cs
rm HashCheck/HashCheck.sym
rm HashCheck/circuit_0000.zkey
rm HashCheck/HashCheck_js
rm HashCheck/witness.wtns
rm HashCheck/pot12_0000.ptau
rm HashCheck/pot12_0001.ptau
rm HashCheck/pot12_final.ptau

if [ -f ./powersOfTau28_hez_final_10.ptau ]; then
echo "powersOfTau28_hez_final_10.ptau already exists. Skipping."
else
echo 'Downloading powersOfTau28_hez_final_10.ptau'
wget https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_10.ptau
fi

echo "Compiling HashCheck.circom..."

circom HashCheck.circom --r1cs --wasm --sym -o HashCheck
node HashCheck/HashCheck_js/generate_witness.js HashCheck/HashCheck_js/HashCheck.wasm HashCheck/input.json HashCheck/witness.wtns
# # cp HashCheck/witness.wtns ../witness.wtns

snarkjs r1cs info HashCheck/HashCheck.r1cs

# # phase 1 of ceremony
snarkjs powersoftau new bn128 12 HashCheck/pot12_0000.ptau -v
snarkjs powersoftau contribute HashCheck/pot12_0000.ptau HashCheck/pot12_0001.ptau --name="First contribution" -v

# # phase 2 of ceremony
snarkjs powersoftau prepare phase2 HashCheck/pot12_0001.ptau HashCheck/pot12_final.ptau -v
snarkjs groth16 setup HashCheck/HashCheck.r1cs powersOfTau28_hez_final_10.ptau HashCheck/circuit_0000.zkey
snarkjs zkey contribute HashCheck/circuit_0000.zkey HashCheck/circuit_final.zkey --name="1st Contribution Name" -v -e="random text"
snarkjs zkey export verificationkey HashCheck/circuit_final.zkey HashCheck/verification_key.json
snarkjs groth16 prove HashCheck/circuit_final.zkey HashCheck/witness.wtns HashCheck/proof.json HashCheck/public.json
snarkjs groth16 verify HashCheck/verification_key.json HashCheck/public.json HashCheck/proof.json



# snarkjs zkey export solidityverifier HashCheck/circuit_final.zkey HashCheck/HashCheckVerifier.sol
34 changes: 0 additions & 34 deletions backend/contracts/Lock.sol

This file was deleted.